src/ai/ai_factory.hpp
author rubidium
Sun, 18 Mar 2007 23:14:44 +0000
branchnoai
changeset 9467 730cae121ae3
parent 9443 c29c91993080
child 9468 e3eb6d371fbb
permissions -rw-r--r--
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     1
/* $Id$ */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     2
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     3
/** @file ai_factory.hpp declaration of class for AIFactory class */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     4
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     5
#ifndef AI_FACTORY_HPP
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     6
#define AI_FACTORY_HPP
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     7
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     8
#include <string>
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
     9
#include <map>
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    10
#include <assert.h>
9433
d439c1c469f4 (svn r9238) [NoAI] -Codechange: removed ai/api/ai_factory.h, as it isn't an api header
truelight
parents: 9427
diff changeset
    11
#include "../stdafx.h"
d439c1c469f4 (svn r9238) [NoAI] -Codechange: removed ai/api/ai_factory.h, as it isn't an api header
truelight
parents: 9427
diff changeset
    12
#include "../functions.h"
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    13
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    14
class AIController;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    15
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    16
class AIFactoryBase {
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    17
private:
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    18
	const char *name;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    19
	typedef std::map<std::string, AIFactoryBase *> Factories;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    20
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    21
	static Factories &GetFactories()
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    22
	{
9443
c29c91993080 (svn r9258) [NoAI] -Fix r9182: somehow a static variable became NULL if not initialised manually
glx
parents: 9442
diff changeset
    23
		static Factories &s_factories = *new Factories();
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    24
		return s_factories;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    25
	}
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    26
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    27
protected:
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    28
	/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    29
	 * This registers your AI to the main system so we know about you.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    30
	 */
9392
7791ceeea57b (svn r9184) [NoAI] -Revert r9183: now that was a bullshit commit
truelight
parents: 9391
diff changeset
    31
	void RegisterFactory(const char *name)
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    32
	{
9400
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    33
		/* Don't register nameless Factories */
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    34
		if (name == NULL) return;
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    35
9442
1b606e5fac13 (svn r9256) [NoAI] -Fix: use initializer values, as they are good for doing what they say: initializing
truelight
parents: 9433
diff changeset
    36
		this->name = name;
9392
7791ceeea57b (svn r9184) [NoAI] -Revert r9183: now that was a bullshit commit
truelight
parents: 9391
diff changeset
    37
		std::pair<Factories::iterator, bool> P = GetFactories().insert(Factories::value_type(name, this));
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    38
		assert(P.second);
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    39
	}
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    40
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    41
public:
9442
1b606e5fac13 (svn r9256) [NoAI] -Fix: use initializer values, as they are good for doing what they say: initializing
truelight
parents: 9433
diff changeset
    42
	AIFactoryBase() :
1b606e5fac13 (svn r9256) [NoAI] -Fix: use initializer values, as they are good for doing what they say: initializing
truelight
parents: 9433
diff changeset
    43
		name(NULL)
1b606e5fac13 (svn r9256) [NoAI] -Fix: use initializer values, as they are good for doing what they say: initializing
truelight
parents: 9433
diff changeset
    44
	{}
9400
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    45
	virtual ~AIFactoryBase() { if (this->name != NULL) GetFactories().erase(this->name); }
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    46
9400
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    47
	/**
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    48
	 * Run all initializers.
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    49
	 */
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    50
	static void RunInitializers()
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    51
	{
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    52
		/* Protect this function from being run several times */
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    53
		static bool run_once = false;
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    54
		if (run_once) return;
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    55
		run_once = true;
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    56
		if (GetFactories().size() == 0) return;
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    57
		Factories::iterator it = GetFactories().begin();
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    58
		for (; it != GetFactories().end(); it++) {
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    59
			AIFactoryBase *f = (*it).second;
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    60
			f->Initializer();
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    61
		}
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    62
	}
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    63
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    64
	/**
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    65
	 * Select a random AI from all known AIs.
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
    66
	 */
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    67
	static AIController *SelectRandomAI()
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    68
	{
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    69
		if (GetFactories().size() == 0) return NULL;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    70
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    71
		/* Find a random AI */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    72
		uint i;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    73
		if (_networking) i = InteractiveRandomRange(GetFactories().size());
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    74
		else             i =            RandomRange(GetFactories().size());
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    75
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    76
		/* Find the Nth item from the array */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    77
		Factories::iterator it = GetFactories().begin();
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    78
		Factories::iterator first_it;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    79
		for (; i > 0; i--) it++;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    80
		first_it = it;
9402
45581b218c98 (svn r9197) [NoAI] -Fix: std::map.end() is the item AFTER the last valid item, not the last item
truelight
parents: 9401
diff changeset
    81
		AIFactoryBase *f = it->second;
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    82
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    83
		if (!f->AllowStartup()) {
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    84
			/* We can't start this AI, try to find the next best */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    85
			do {
9402
45581b218c98 (svn r9197) [NoAI] -Fix: std::map.end() is the item AFTER the last valid item, not the last item
truelight
parents: 9401
diff changeset
    86
				it++;
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    87
				if (it == GetFactories().end()) it = GetFactories().begin();
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    88
				/* Back at the beginning? Drop out! */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    89
				if (first_it == it) break;
9402
45581b218c98 (svn r9197) [NoAI] -Fix: std::map.end() is the item AFTER the last valid item, not the last item
truelight
parents: 9401
diff changeset
    90
				f = it->second;
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    91
			} while (!f->AllowStartup());
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    92
			/* Unable to start any AI */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    93
			if (first_it == it) return NULL;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    94
		}
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    95
		return f->CreateInstance();
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    96
	}
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
    97
9467
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
    98
	/**
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
    99
	 * Select the AI with the given name.
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   100
	 * @param name the AI to select.
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   101
	 */
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   102
	static AIController *SelectAI(const char *name)
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   103
	{
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   104
		if (GetFactories().size() == 0) return NULL;
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   105
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   106
		Factories::iterator it = GetFactories().begin();
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   107
		for (; it != GetFactories().end(); it++) {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   108
			AIFactoryBase *f = (*it).second;
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   109
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   110
			if (strcasecmp(name, f->name) == 0 && f->AllowStartup()) {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   111
				return f->CreateInstance();
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   112
			}
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   113
		}
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   114
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   115
		return NULL;
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   116
	}
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   117
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   118
	/**
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   119
	 * Fills the given buffer with the AIs we know.
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   120
	 * @param p    start of the buffer.
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   121
	 * @param last till were we may write.
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   122
	 * @return till were we have written.
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   123
	 */
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   124
	static char *GetAIConsoleList(char *p, const char *last)
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   125
	{
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   126
		printf("%d\n", GetFactories().size());
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   127
		p += snprintf(p, last - p, "List of AIs:\n");
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   128
		Factories::iterator it = GetFactories().begin();
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   129
		for (; it != GetFactories().end(); it++) {
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   130
			AIFactoryBase *f = (*it).second;
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   131
			if (!f->AllowStartup()) continue;
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   132
			p += snprintf(p, last - p, "%10s: %s\n", f->name, f->GetDescription());
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   133
		}
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   134
		p += snprintf(p, last - p, "\n");
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   135
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   136
		return p;
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   137
	}
730cae121ae3 (svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents: 9443
diff changeset
   138
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   139
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   140
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   141
	/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   142
	 * Get the author of the AI.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   143
	 */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   144
	virtual const char *GetAuthor() = 0;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   145
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   146
	/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   147
	 * Get a description of the AI.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   148
	 */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   149
	virtual const char *GetDescription() = 0;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   150
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   151
	/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   152
	 * Get the version of this AI.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   153
	 */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   154
	virtual int GetVersion() = 0;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   155
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   156
	/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   157
	 * Get the date of the version.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   158
	 * @return A string like '2007-08-29'.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   159
	 */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   160
	virtual const char *GetDate() = 0;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   161
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   162
	/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   163
	 * Create an instance of your AI.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   164
	 */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   165
	virtual AIController *CreateInstance() = 0;
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   166
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   167
	/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   168
	 * In this function you can do some very limited querying of the game.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   169
	 *  If you for example only supply road-vehicle-based AI, and RVs are
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   170
	 *  disabled, you return 'false', so you won't boot.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   171
	 */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   172
	virtual bool AllowStartup() { return true; }
9400
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   173
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   174
	/**
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   175
	 * Normally you just make a static instance of AIFactory, and you
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   176
	 *  will be registered. In very limited cases, like Squirrel-module,
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   177
	 *  it is needed to be called when OpenTTD is launched (so not as
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   178
	 *  constructor). Then you can use this function.
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   179
	 * @note Avoid using it!
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   180
	 */
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   181
	virtual void Initializer() { }
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   182
};
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   183
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   184
/**
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   185
 * The template to define your AIFactory. This makes sure RegisterFactory
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   186
 *  works correctly on initialization.
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   187
 * Example: class FYourClass: public AIFactory<FYourClass> { }
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   188
 */
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   189
template <class T>
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   190
class AIFactory: public AIFactoryBase {
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   191
public:
9392
7791ceeea57b (svn r9184) [NoAI] -Revert r9183: now that was a bullshit commit
truelight
parents: 9391
diff changeset
   192
	AIFactory() { this->RegisterFactory(((T *)this)->GetName()); }
9400
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   193
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   194
	/**
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   195
	 * Get a short name for the AI.
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   196
	 */
6da42d57fda2 (svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents: 9392
diff changeset
   197
	const char *GetName();
9390
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   198
};
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   199
4042c17c8055 (svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff changeset
   200
#endif /* AI_FACTORY_HPP */