truelight@9361: /* $Id$ */ truelight@9361: truelight@9388: /** @file ai_controler.hpp declaration of class for AIController class */ truelight@9361: truelight@9361: #ifndef AI_CONTROLLER_HPP truelight@9361: #define AI_CONTROLLER_HPP truelight@9361: truelight@9361: #include "../../stdafx.h" truelight@9361: truelight@9448: /** truelight@9448: * Class that handles the core of every AI. Each AI needs to extend this class truelight@9448: * in order to work. The OpenTTD core makes sure GetTick gets increased when truelight@9448: * needed. truelight@9448: */ truelight@9361: class AIController { truelight@9372: private: truelight@9361: uint tick; truelight@9361: truelight@9372: public: truelight@9448: /** truelight@9448: * Initializer of the AIController. truelight@9448: */ truelight@9361: AIController() : truelight@9361: tick(0) truelight@9361: {} truelight@9361: truelight@9448: /** truelight@9448: * Destructor of the AIController. truelight@9448: */ truelight@9361: virtual ~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: */ truelight@9441: virtual void Start() = 0; truelight@9361: truelight@9361: /** truelight@9448: * When this function is called, the AI must stop as soon as possible. truelight@9448: * It may not call any function that might suspend, like DoCommand() truelight@9448: * and Sleep(). truelight@9448: * @note Cannot be called from within your AI. rubidium@9444: */ rubidium@9444: virtual void Stop() = 0; rubidium@9444: rubidium@9444: /** 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: */ truelight@9361: void IncreaseTick() { this->tick++; } truelight@9372: truelight@9372: /** truelight@9448: * Find at which tick your AI currently is. truelight@9448: * @return returns the current tick. truelight@9372: */ truelight@9372: uint GetTick() { return this->tick; } 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: */ truelight@9441: void Sleep(uint ticks); truelight@9361: }; truelight@9361: rubidium@9439: #endif /* AI_CONTROLLER_HPP */ rubidium@9439: rubidium@9439: /* rubidium@9439: * The next part of the code is outside of the multiple inclusions rubidium@9439: * protection. This is done because the Squirrel code needs the rubidium@9439: * AIController class declaration to make the Squirrel registration rubidium@9439: * functions. So in the first include DEFINE_SQUIRREL_CLASS is not rubidium@9439: * defined, but in the second one it will when included from the rubidium@9439: * Squirrel AI implementation. rubidium@9439: * All other instances that include this header twice must not have rubidium@9439: * set DEFINE_SQUIRREL_CLASS, so this part will not be included rubidium@9439: * twice under those circumstances. rubidium@9439: */ rubidium@9439: truelight@9425: #ifdef DEFINE_SQUIRREL_CLASS truelight@9422: void SQAIControllerRegister(Squirrel *engine) { truelight@9404: DefSQClass SQAIController("AIController"); truelight@9404: SQAIController.PreRegister(engine); truelight@9530: SQAIController.DefSQMethod(engine, &AIControllerSquirrel::GetTick, "GetTick"); truelight@9530: SQAIController.DefSQMethod(engine, &AIControllerSquirrel::Sleep, "Sleep"); truelight@9404: SQAIController.PostRegister(engine); truelight@9404: } rubidium@9520: #endif /* DEFINE_SQUIRREL_CLASS */