src/ai/api/ai_controller.hpp
author truebrain
Mon, 31 Mar 2008 18:33:33 +0000
branchnoai
changeset 9851 a5f5a7cf2b61
parent 9829 80fbe02a4184
child 10643 970417eef395
permissions -rw-r--r--
(svn r12519) [NoAI] -Add: added AILog with Info(), Warning(), and Error()
[NoAI] -Add: redirect AI outputs to the AI Debug GUI to show it per AI, in a clear way ingame (no more need for stderr viewing)
NOTE: it still does output to stderr, but on an other DEBUG() level (depending on the message).
/* $Id$ */

/** @file ai_controller.hpp The controller of the AI. */

#ifndef AI_CONTROLLER_HPP
#define AI_CONTROLLER_HPP

#include "../../stdafx.h"

/**
 * Class that handles the core of every AI. Each AI needs to extend this class
 *  in order to work. The OpenTTD core makes sure GetTick gets increased when
 *  needed.
 */
class AIController {
private:
	uint tick;

public:
	/**
	 * Initializer of the AIController.
	 */
	AIController() :
		tick(0)
	{}

	/**
	 * Destructor of the AIController.
	 */
	virtual ~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.
	 */
	virtual void Start() = 0;

	/**
	 * When this function is called, the AI must stop as soon as possible.
	 *   It may not call any function that might suspend, like DoCommand()
	 *   and Sleep().
	 * @note Cannot be called from within your AI.
	 */
	virtual void Stop() = 0;

	/**
	 * 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() { this->tick++; }

	/**
	 * Find at which tick your AI currently is.
	 * @return returns the current tick.
	 */
	uint GetTick() { return this->tick; }

	/**
	 * 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);
};

#endif /* AI_CONTROLLER_HPP */