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