src/ai/api/ai_controller.hpp
author truebrain
Fri, 22 Feb 2008 12:30:17 +0000
branchnoai
changeset 9737 ee408edf3851
parent 9607 9ad01142ea6b
child 9782 e8d8d8894f23
permissions -rw-r--r--
(svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
/* $Id$ */

/** @file ai_controller.hpp declaration of class for AIController class */

#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.
	 */
	void Sleep(uint ticks);
};

#endif /* AI_CONTROLLER_HPP */