2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/** @file ai_instance.hpp The AIInstance tracks an AI. */
|
|
|
|
|
|
|
|
#ifndef AI_INSTANCE_HPP
|
|
|
|
#define AI_INSTANCE_HPP
|
|
|
|
|
2010-01-15 16:41:15 +00:00
|
|
|
#include <squirrel.h>
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* The callback function when an AI suspends.
|
|
|
|
*/
|
|
|
|
typedef void (AISuspendCallbackProc)(class AIInstance *instance);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A throw-class that is given when the VM wants to suspend.
|
|
|
|
*/
|
|
|
|
class AI_VMSuspend {
|
|
|
|
public:
|
|
|
|
AI_VMSuspend(int time, AISuspendCallbackProc *callback) :
|
|
|
|
time(time),
|
|
|
|
callback(callback)
|
2009-08-19 16:14:15 +00:00
|
|
|
{}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
int GetSuspendTime() { return time; }
|
|
|
|
AISuspendCallbackProc *GetSuspendCallback() { return callback; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int time;
|
|
|
|
AISuspendCallbackProc *callback;
|
|
|
|
};
|
|
|
|
|
2009-08-19 16:14:15 +00:00
|
|
|
/**
|
|
|
|
* A throw-class that is given when the AI made a fatal error.
|
|
|
|
*/
|
|
|
|
class AI_FatalError {
|
|
|
|
public:
|
|
|
|
AI_FatalError(const char *msg) :
|
|
|
|
msg(msg)
|
|
|
|
{}
|
|
|
|
|
|
|
|
const char *GetErrorMessage() { return msg; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char *msg;
|
|
|
|
};
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
class AIInstance {
|
|
|
|
public:
|
2009-06-01 22:00:47 +00:00
|
|
|
friend class AIObject;
|
2009-01-12 17:11:45 +00:00
|
|
|
AIInstance(class AIInfo *info);
|
|
|
|
~AIInstance();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An AI in multiplayer waits for the server to handle his DoCommand.
|
|
|
|
* It keeps waiting for this until this function is called.
|
|
|
|
*/
|
|
|
|
void Continue();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the GameLoop of an AI.
|
|
|
|
*/
|
|
|
|
void GameLoop();
|
|
|
|
|
2009-02-03 22:42:42 +00:00
|
|
|
/**
|
|
|
|
* Let the VM collect any garbage.
|
|
|
|
*/
|
2010-01-07 00:09:27 +00:00
|
|
|
void CollectGarbage() const;
|
2009-02-03 22:42:42 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* Get the storage of this AI.
|
|
|
|
*/
|
|
|
|
static class AIStorage *GetStorage();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a true/false reply for a DoCommand.
|
|
|
|
*/
|
|
|
|
static void DoCommandReturn(AIInstance *instance);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a VehicleID reply for a DoCommand.
|
|
|
|
*/
|
|
|
|
static void DoCommandReturnVehicleID(AIInstance *instance);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a SignID reply for a DoCommand.
|
|
|
|
*/
|
|
|
|
static void DoCommandReturnSignID(AIInstance *instance);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a GroupID reply for a DoCommand.
|
|
|
|
*/
|
|
|
|
static void DoCommandReturnGroupID(AIInstance *instance);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the controller attached to the instance.
|
|
|
|
*/
|
|
|
|
class AIController *GetController() { return controller; }
|
|
|
|
|
2009-06-10 19:23:25 +00:00
|
|
|
/**
|
|
|
|
* Return the "this AI died" value
|
|
|
|
*/
|
2010-01-07 00:09:27 +00:00
|
|
|
inline bool IsDead() const { return this->is_dead; }
|
2009-06-10 19:23:25 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* Call the AI Save function and save all data in the savegame.
|
|
|
|
*/
|
|
|
|
void Save();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Don't save any data in the savegame.
|
|
|
|
*/
|
|
|
|
static void SaveEmpty();
|
|
|
|
|
|
|
|
/**
|
2009-02-03 20:49:08 +00:00
|
|
|
* Load data from a savegame and store it on the stack.
|
2009-01-13 01:46:46 +00:00
|
|
|
* @param version The version of the AI when saving, or -1 if this was
|
|
|
|
* not the original AI saving the game.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2009-01-13 01:46:46 +00:00
|
|
|
void Load(int version);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-02-03 20:49:08 +00:00
|
|
|
/**
|
|
|
|
* Call the AI Load function if it exists and data was loaded
|
|
|
|
* from a savegame.
|
|
|
|
*/
|
2009-02-13 17:17:34 +00:00
|
|
|
bool CallLoad();
|
2009-02-03 20:49:08 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* Load and discard data from a savegame.
|
|
|
|
*/
|
|
|
|
static void LoadEmpty();
|
|
|
|
|
2010-04-02 17:35:20 +00:00
|
|
|
/**
|
|
|
|
* Reduces the number of opcodes the AI have left to zero. Unless
|
|
|
|
* the AI is in a state where it cannot suspend it will be suspended
|
|
|
|
* for the reminder of the current tick. This function is safe to
|
|
|
|
* call from within a function called by the AI.
|
|
|
|
*/
|
|
|
|
void Suspend();
|
2009-01-12 17:11:45 +00:00
|
|
|
private:
|
|
|
|
class AIController *controller;
|
|
|
|
class AIStorage *storage;
|
|
|
|
class Squirrel *engine;
|
|
|
|
SQObject *instance;
|
|
|
|
|
|
|
|
bool is_started;
|
|
|
|
bool is_dead;
|
2009-07-15 19:47:06 +00:00
|
|
|
bool is_save_data_on_stack;
|
2009-01-12 17:11:45 +00:00
|
|
|
int suspend;
|
|
|
|
AISuspendCallbackProc *callback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register all API functions to the VM.
|
|
|
|
*/
|
|
|
|
void RegisterAPI();
|
|
|
|
|
2009-08-18 18:51:42 +00:00
|
|
|
/**
|
|
|
|
* Load squirrel scipts to emulate an older API.
|
|
|
|
*/
|
|
|
|
bool LoadCompatibilityScripts(const char *api_version);
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* Tell the AI it died.
|
|
|
|
*/
|
|
|
|
void Died();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save one object (int / string / arrray / table) to the savegame.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param vm The virtual machine to get all the data from.
|
2009-01-12 17:11:45 +00:00
|
|
|
* @param index The index on the squirrel stack of the element to save.
|
|
|
|
* @param max_depth The maximum depth recursive arrays / tables will be stored
|
|
|
|
* with before an error is returned.
|
|
|
|
* @param test If true, don't really store the data but only check if it is
|
|
|
|
* valid.
|
2010-02-27 12:29:44 +00:00
|
|
|
* @return True if the saving was successful.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
|
|
|
static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all objects from a savegame.
|
2010-02-27 12:29:44 +00:00
|
|
|
* @return True if the loading was successful.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
|
|
|
static bool LoadObjects(HSQUIRRELVM vm);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* AI_INSTANCE_HPP */
|