src/ai/api/ai_controller.hpp
branchnoai
changeset 10643 970417eef395
parent 9851 a5f5a7cf2b61
child 10871 326ee226e9d7
equal deleted inserted replaced
10642:dbb8fd36a99c 10643:970417eef395
     3 /** @file ai_controller.hpp The controller of the AI. */
     3 /** @file ai_controller.hpp The controller of the AI. */
     4 
     4 
     5 #ifndef AI_CONTROLLER_HPP
     5 #ifndef AI_CONTROLLER_HPP
     6 #define AI_CONTROLLER_HPP
     6 #define AI_CONTROLLER_HPP
     7 
     7 
     8 #include "../../stdafx.h"
       
     9 
       
    10 /**
     8 /**
    11  * Class that handles the core of every AI. Each AI needs to extend this class
     9  * The Controller, the class each AI should extend. It creates the AI, makes
    12  *  in order to work. The OpenTTD core makes sure GetTick gets increased when
    10  *  sure the logic kicks in correctly, and that GetTick() has a valid value.
    13  *  needed.
       
    14  */
    11  */
    15 class AIController {
    12 class AIController {
    16 private:
    13 private:
    17 	uint tick;
    14 	uint tick;
    18 
    15 
    19 public:
    16 public:
       
    17 	static const char *GetClassName() { return "AIController"; }
       
    18 
    20 	/**
    19 	/**
    21 	 * Initializer of the AIController.
    20 	 * Initializer of the AIController.
    22 	 */
    21 	 */
    23 	AIController() :
    22 	AIController(const char *script, const char *class_name);
    24 		tick(0)
       
    25 	{}
       
    26 
    23 
    27 	/**
    24 	/**
    28 	 * Destructor of the AIController.
    25 	 * Destructor of the AIController.
    29 	 */
    26 	 */
    30 	virtual ~AIController() { }
    27 	~AIController();
    31 
    28 
    32 	/**
    29 	/**
    33 	 * This function is called to start your AI. Your AI starts here. If you
    30 	 * This function is called to start your AI. Your AI starts here. If you
    34 	 *   return from this function, your AI dies, so make sure that doesn't
    31 	 *   return from this function, your AI dies, so make sure that doesn't
    35 	 *   happen. It is okay to use while() {} loops, as long as you put a
    32 	 *   happen. It is okay to use while() {} loops, as long as you put a
    36 	 *   Sleep() in it to give the rest of the game time to do things. Also
    33 	 *   Sleep() in it to give the rest of the game time to do things. Also
    37 	 *   don't keep the system busy for too long, as people will consider
    34 	 *   don't keep the system busy for too long, as people will consider
    38 	 *   that annoying.
    35 	 *   that annoying.
    39 	 * @note Cannot be called from within your AI.
    36 	 * @note Cannot be called from within your AI.
    40 	 */
    37 	 */
    41 	virtual void Start() = 0;
    38 	void Start();
    42 
    39 
    43 	/**
    40 	/**
    44 	 * When this function is called, the AI must stop as soon as possible.
    41 	 * When this function is called, the AI must stop as soon as possible.
    45 	 *   It may not call any function that might suspend, like DoCommand()
    42 	 *   It may not call any function that might suspend, like DoCommand()
    46 	 *   and Sleep().
    43 	 *   and Sleep().
    47 	 * @note Cannot be called from within your AI.
    44 	 * @note Cannot be called from within your AI.
    48 	 */
    45 	 */
    49 	virtual void Stop() = 0;
    46 	void Stop();
    50 
    47 
    51 	/**
    48 	/**
    52 	 * Increase the internal ticker. You should never call this yourself,
    49 	 * Increase the internal ticker. You should never call this yourself,
    53 	 *   as it is called by the OpenTTD system itself.
    50 	 *   as it is called by the OpenTTD system itself.
    54 	 * @note Cannot be called from within your AI.
    51 	 * @note Cannot be called from within your AI.
    55 	 */
    52 	 */
    56 	void IncreaseTick() { this->tick++; }
    53 	void IncreaseTick();
    57 
    54 
    58 	/**
    55 	/**
    59 	 * Find at which tick your AI currently is.
    56 	 * Find at which tick your AI currently is.
    60 	 * @return returns the current tick.
    57 	 * @return returns the current tick.
    61 	 */
    58 	 */
    62 	uint GetTick() { return this->tick; }
    59 	uint GetTick();
    63 
    60 
    64 	/**
    61 	/**
    65 	 * Sleep for X ticks. The code continues after this line when the X AI ticks
    62 	 * Sleep for X ticks. The code continues after this line when the X AI ticks
    66 	 *   are passed. Mind that an AI tick is different from in-game ticks and
    63 	 *   are passed. Mind that an AI tick is different from in-game ticks and
    67 	 *   differ per AI speed.
    64 	 *   differ per AI speed.
    77 	 * @param error_msg If true, it is a Squirrel error message.
    74 	 * @param error_msg If true, it is a Squirrel error message.
    78 	 * @param message The message Squirrel logged.
    75 	 * @param message The message Squirrel logged.
    79 	 * @note Use AILog.Info/Warning/Error instead of 'print'.
    76 	 * @note Use AILog.Info/Warning/Error instead of 'print'.
    80 	 */
    77 	 */
    81 	static void Print(bool error_msg, const char *message);
    78 	static void Print(bool error_msg, const char *message);
       
    79 
       
    80 private:
       
    81 	class Squirrel *engine;
       
    82 	HSQOBJECT *SQ_instance;
       
    83 
       
    84 	/**
       
    85 	 * Register all classes that are known inside the NoAI API.
       
    86 	 */
       
    87 	void RegisterClasses();
    82 };
    88 };
    83 
    89 
    84 #endif /* AI_CONTROLLER_HPP */
    90 #endif /* AI_CONTROLLER_HPP */