(svn r13512) [NoAI] -Fix: don't load a library over and over, but keep track of which libraries we have loaded (per AI) and re-use it where possible (reduces memory-footprint)
[NoAI] -Fix: change the fake-library-name-counter to a per AI value, not global
[NoAI] -Fix: Load the script inside the thread, not in the main thread. This avoids unneeded error-handling
/* $Id$ */
/** @file ai_controller.hpp The controller of the AI. */
#ifndef AI_CONTROLLER_HPP
#define AI_CONTROLLER_HPP
#include <map>
#ifndef AI_SQUIRREL_HPP
struct ltstr { bool operator()(const char *s1, const char *s2) const { return strcmp(s1, s2) < 0; } };
#endif /* AI_SQUIRREL_HPP */
/**
* The Controller, the class each AI should extend. It creates the AI, makes
* sure the logic kicks in correctly, and that GetTick() has a valid value.
*/
class AIController {
friend class AISquirrel;
public:
static const char *GetClassName() { return "AIController"; }
/**
* Initializer of the AIController.
*/
AIController(const char *script, const char *class_name);
/**
* Destructor of the AIController.
*/
~AIController();
/**
* This function is called to start your AI. Your AI starts here. If you
* return from this function, your AI dies, so make sure that doesn't
* happen. It is okay to use while() {} loops, as long as you put a
* Sleep() in it to give the rest of the game time to do things. Also
* don't keep the system busy for too long, as people will consider
* that annoying.
* @note Cannot be called from within your AI.
*/
void Start();
/**
* Increase the internal ticker. You should never call this yourself,
* as it is called by the OpenTTD system itself.
* @note Cannot be called from within your AI.
*/
void IncreaseTick();
/**
* Find at which tick your AI currently is.
* @return returns the current tick.
*/
uint GetTick();
/**
* Sleep for X ticks. The code continues after this line when the X AI ticks
* are passed. Mind that an AI tick is different from in-game ticks and
* differ per AI speed.
* @param ticks the ticks to wait
* @post the value of GetTick() will be changed exactly 'ticks' in value after
* calling this.
*/
static void Sleep(uint ticks);
/**
* When Squirrel triggers a print, this function is called.
* Squirrel calls this when 'print' is used, or when the script made an error.
* @param error_msg If true, it is a Squirrel error message.
* @param message The message Squirrel logged.
* @note Use AILog.Info/Warning/Error instead of 'print'.
*/
static void Print(bool error_msg, const char *message);
private:
typedef std::map<const char *, const char *, ltstr> LoadedLibraryList;
uint tick;
class Squirrel *engine;
HSQOBJECT *SQ_instance;
LoadedLibraryList loaded_library;
int loaded_library_count;
const char *script;
const char *class_name;
/**
* Register all classes that are known inside the NoAI API.
*/
void RegisterClasses();
/**
* Check if a library is already loaded. If found, fake_class_name is filled
* with the fake class name as given via AddLoadedLibrary. If not found,
* next_number is set to the next number available for the fake namespace.
* @param library_name The library to check if already loaded.
* @param next_number The next available number for a library if not already loaded.
* @param fake_class_name The name the library has if already loaded.
* @param fake_class_name_len The maximum length of fake_class_name.
* @return True if the library is already loaded.
*/
bool LoadedLibrary(const char *library_name, int *next_number, char *fake_class_name, int fake_class_name_len);
/**
* Add a library as loaded.
*/
void AddLoadedLibrary(const char *library_name, const char *fake_class_name);
};
#endif /* AI_CONTROLLER_HPP */