src/ai/api/ai_base.hpp
author rubidium
Sun, 25 Mar 2007 14:19:59 +0000
branchnoai
changeset 9526 a4ad60ba03be
parent 9524 283d23931bb4
child 9529 5f26f4bc574b
permissions -rw-r--r--
(svn r9446) [NoAI] -Add: simple script to make changing/adding classes to export a little simpler.
/* $Id$ */

/** @file ai_base.hpp declaration of class for AIBase class */

#ifndef AI_BASE_HPP
#define AI_BASE_HPP

#include "ai_object.hpp"

/**
 * Class that handles some basic functions.
 *
 * @note The random functions are not called Random and RandomRange, because
 *       when including them the RANDOM_DEBUG stuff messes with their names.
 *       However, because in MP we cannot use Random because that will cause
 *       desyncs (AIs are ran locally, not on all clients). This means that
 *       we use InteractiveRandom in MP, thus the whole random debugging is
 *       pointless for the AIs. Therefor the random functions are called
 *       differently.
 */
class AIBase : public AIObject {
public:
	/**
	 * Get a random value.
	 * @return a random value between 0 and MAX(uint32).
	 */
	uint32 Rand();

	/**
	 * Get a random value in a range.
	 * @param max the maximum value it will return.
	 * @return a random value between 0 .. max.
	 */
	uint RandRange(uint max);

	/**
	 * Returns approximatelly 'out' times true when called 'max' times.
	 *   After all, it is a random function.
	 * @return true if the chance worked out.
	 */
	bool Chance(uint out, uint max);
};

#ifdef DEFINE_SQUIRREL_CLASS
namespace SQConvert {
	/* Allow AIBase to be used as Squirrel parameter */
	template <> AIBase *GetParam(ForceType<AIBase *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIBase *)instance; }
}; // namespace SQConvert

void SQAIBaseRegister(Squirrel *engine) {
	DefSQClass <AIBase> SQAIBase("AIBase");
	SQAIBase.PreRegister(engine);
	SQAIBase.AddConstructor(engine);

	SQAIBase.DefSQFunction(engine, &AIBase::Rand,      "Rand");
	SQAIBase.DefSQFunction(engine, &AIBase::RandRange, "RandRange");
	SQAIBase.DefSQFunction(engine, &AIBase::Chance,    "Chance");

	SQAIBase.PostRegister(engine);
}
#endif /* DEFINE_SQUIRREL_CLASS */

#endif /* AI_BASE_HPP */