author | truelight |
Wed, 14 Mar 2007 23:00:06 +0000 | |
branch | noai |
changeset 9391 | 2d6da2e48112 |
parent 9390 | 4042c17c8055 |
child 9392 | 7791ceeea57b |
permissions | -rw-r--r-- |
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> |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
11 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
12 |
#include "ai_base.hpp" |
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 |
{ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
23 |
static Factories s_factories; |
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 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
31 |
void RegisterFactory(AIFactoryBase *instance, const char *name) |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
32 |
{ |
9391
2d6da2e48112
(svn r9183) [NoAI] -Fix: use 'instance' instead of 'this' in RegisterFactory
truelight
parents:
9390
diff
changeset
|
33 |
std::pair<Factories::iterator, bool> P = GetFactories().insert(Factories::value_type(name, instance)); |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
34 |
assert(P.second); |
9391
2d6da2e48112
(svn r9183) [NoAI] -Fix: use 'instance' instead of 'this' in RegisterFactory
truelight
parents:
9390
diff
changeset
|
35 |
instance->name = name; |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
36 |
} |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
37 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
38 |
public: |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
39 |
virtual ~AIFactoryBase() { GetFactories().erase(this->name); } |
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 |
static AIController *SelectRandomAI() |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
42 |
{ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
43 |
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
|
44 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
45 |
/* Find a random AI */ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
46 |
uint i; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
47 |
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
|
48 |
else i = RandomRange(GetFactories().size()); |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
49 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
50 |
/* 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
|
51 |
Factories::iterator it = GetFactories().begin(); |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
52 |
Factories::iterator first_it; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
53 |
for (; i > 0; i--) it++; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
54 |
first_it = it; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
55 |
AIFactoryBase *f = (*it).second; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
56 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
57 |
if (!f->AllowStartup()) { |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
58 |
/* 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
|
59 |
do { |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
60 |
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
|
61 |
else it++; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
62 |
/* 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
|
63 |
if (first_it == it) break; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
64 |
f = (*it).second; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
65 |
} while (!f->AllowStartup()); |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
66 |
/* Unable to start any AI */ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
67 |
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
|
68 |
} |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
69 |
return f->CreateInstance(); |
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 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
72 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
73 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
74 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
75 |
* 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
|
76 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
77 |
virtual const char *GetAuthor() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
78 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
79 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
80 |
* Get a short name for the AI. |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
81 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
82 |
virtual const char *GetName() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
83 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
84 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
85 |
* 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
|
86 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
87 |
virtual const char *GetDescription() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
88 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
89 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
90 |
* 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
|
91 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
92 |
virtual int GetVersion() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
93 |
|
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 |
* 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
|
96 |
* @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
|
97 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
98 |
virtual const char *GetDate() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
99 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
100 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
101 |
* 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
|
102 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
103 |
virtual AIController *CreateInstance() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
104 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
105 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
106 |
* 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
|
107 |
* 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
|
108 |
* 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
|
109 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
110 |
virtual bool AllowStartup() { return true; } |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
111 |
}; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
112 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
113 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
114 |
* 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
|
115 |
* works correctly on initialization. |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
116 |
* 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
|
117 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
118 |
template <class T> |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
119 |
class AIFactory: public AIFactoryBase { |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
120 |
public: |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
121 |
AIFactory() { this->RegisterFactory(this, ((T *)this)->GetName()); } |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
122 |
}; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
123 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
124 |
#endif /* AI_FACTORY_HPP */ |