author | truebrain |
Mon, 25 Feb 2008 18:18:35 +0000 | |
branch | noai |
changeset 9770 | ad3c5f807d7c |
parent 9723 | eee46cb39750 |
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> |
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" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9471
diff
changeset
|
13 |
#include "../network/network.h" |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
14 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
15 |
class AIController; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
16 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
17 |
class AIFactoryBase { |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
18 |
private: |
9471
467ee8749166
(svn r9309) [NoAI] -Fix: stdup the AI name, as else it can do weird things from SQ (glx)
truelight
parents:
9468
diff
changeset
|
19 |
char *name; |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
20 |
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
|
21 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
22 |
static Factories &GetFactories() |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
23 |
{ |
9443
c29c91993080
(svn r9258) [NoAI] -Fix r9182: somehow a static variable became NULL if not initialised manually
glx
parents:
9442
diff
changeset
|
24 |
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
|
25 |
return s_factories; |
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 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
28 |
protected: |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
29 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
30 |
* 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
|
31 |
*/ |
9392
7791ceeea57b
(svn r9184) [NoAI] -Revert r9183: now that was a bullshit commit
truelight
parents:
9391
diff
changeset
|
32 |
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
|
33 |
{ |
9400
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
34 |
/* Don't register nameless Factories */ |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
35 |
if (name == NULL) return; |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
36 |
|
9471
467ee8749166
(svn r9309) [NoAI] -Fix: stdup the AI name, as else it can do weird things from SQ (glx)
truelight
parents:
9468
diff
changeset
|
37 |
this->name = strdup(name); |
9392
7791ceeea57b
(svn r9184) [NoAI] -Revert r9183: now that was a bullshit commit
truelight
parents:
9391
diff
changeset
|
38 |
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
|
39 |
assert(P.second); |
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 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
42 |
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
|
43 |
AIFactoryBase() : |
1b606e5fac13
(svn r9256) [NoAI] -Fix: use initializer values, as they are good for doing what they say: initializing
truelight
parents:
9433
diff
changeset
|
44 |
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
|
45 |
{} |
9471
467ee8749166
(svn r9309) [NoAI] -Fix: stdup the AI name, as else it can do weird things from SQ (glx)
truelight
parents:
9468
diff
changeset
|
46 |
virtual ~AIFactoryBase() { if (this->name != NULL) GetFactories().erase(this->name); free(this->name); } |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
47 |
|
9400
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
48 |
/** |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
49 |
* Run all initializers. |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
50 |
*/ |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
51 |
static void RunInitializers() |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
52 |
{ |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
53 |
/* 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
|
54 |
static bool run_once = false; |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
55 |
if (run_once) return; |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
56 |
run_once = true; |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
57 |
if (GetFactories().size() == 0) return; |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
58 |
Factories::iterator it = GetFactories().begin(); |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
59 |
for (; it != GetFactories().end(); it++) { |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
60 |
AIFactoryBase *f = (*it).second; |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
61 |
f->Initializer(); |
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 |
/** |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
66 |
* 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
|
67 |
*/ |
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
68 |
static AIFactoryBase *SelectRandomAI() |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
69 |
{ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
70 |
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
|
71 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
72 |
/* Find a random AI */ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
73 |
uint i; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
74 |
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
|
75 |
else i = RandomRange(GetFactories().size()); |
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 |
/* 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
|
78 |
Factories::iterator it = GetFactories().begin(); |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
79 |
Factories::iterator first_it; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
80 |
for (; i > 0; i--) it++; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
81 |
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
|
82 |
AIFactoryBase *f = it->second; |
9390
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 |
if (!f->AllowStartup()) { |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
85 |
/* 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
|
86 |
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
|
87 |
it++; |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
88 |
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
|
89 |
/* 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
|
90 |
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
|
91 |
f = it->second; |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
92 |
} while (!f->AllowStartup()); |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
93 |
/* Unable to start any AI */ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
94 |
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
|
95 |
} |
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
96 |
return f; |
9390
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 |
|
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
99 |
/** |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
100 |
* 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
|
101 |
* @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
|
102 |
*/ |
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
103 |
static AIFactoryBase *SelectAI(const char *name) |
9467
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
104 |
{ |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
105 |
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
|
106 |
|
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
107 |
Factories::iterator it = GetFactories().begin(); |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
108 |
for (; it != GetFactories().end(); it++) { |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
109 |
AIFactoryBase *f = (*it).second; |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
110 |
|
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
111 |
if (strcasecmp(name, f->name) == 0 && f->AllowStartup()) { |
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
112 |
return f; |
9467
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 |
|
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
116 |
return NULL; |
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 |
/** |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
120 |
* 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
|
121 |
* @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
|
122 |
* @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
|
123 |
* @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
|
124 |
*/ |
730cae121ae3
(svn r9305) [NoAI] -Add: option to force-select an AI from the console.
rubidium
parents:
9443
diff
changeset
|
125 |
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
|
126 |
{ |
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 |
|
9770
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
140 |
/** |
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
141 |
* Get the name of the AI internally. |
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
142 |
*/ |
ad3c5f807d7c
(svn r12260) [NoAI] -Change: make SelectAI return the factory, so some GUI might read how the AI is called, and who wrote it, etc etc
truebrain
parents:
9723
diff
changeset
|
143 |
const char *GetAIName() { return this->name; } |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
144 |
|
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 |
* 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
|
147 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
148 |
virtual const char *GetAuthor() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
149 |
|
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 |
* 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
|
152 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
153 |
virtual const char *GetDescription() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
154 |
|
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 |
* 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
|
157 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
158 |
virtual int GetVersion() = 0; |
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 |
/** |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
161 |
* 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
|
162 |
* @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
|
163 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
164 |
virtual const char *GetDate() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
165 |
|
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 |
* 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
|
168 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
169 |
virtual AIController *CreateInstance() = 0; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
170 |
|
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 |
* 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
|
173 |
* 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
|
174 |
* 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
|
175 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
176 |
virtual bool AllowStartup() { return true; } |
9400
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
177 |
|
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
178 |
/** |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
179 |
* 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
|
180 |
* 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
|
181 |
* 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
|
182 |
* 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
|
183 |
* @note Avoid using it! |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
184 |
*/ |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
185 |
virtual void Initializer() { } |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
186 |
}; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
187 |
|
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 |
* 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
|
190 |
* works correctly on initialization. |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
191 |
* 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
|
192 |
*/ |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
193 |
template <class T> |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
194 |
class AIFactory: public AIFactoryBase { |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
195 |
public: |
9392
7791ceeea57b
(svn r9184) [NoAI] -Revert r9183: now that was a bullshit commit
truelight
parents:
9391
diff
changeset
|
196 |
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
|
197 |
|
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
198 |
/** |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
199 |
* 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
|
200 |
*/ |
6da42d57fda2
(svn r9195) [NoAI] -Fix: move GetName to AIFactory template, as otherwise GetName()
truelight
parents:
9392
diff
changeset
|
201 |
const char *GetName(); |
9390
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
202 |
}; |
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
203 |
|
4042c17c8055
(svn r9182) [NoAI] -Add: added AIFactory template which all AIs must define, as this
truelight
parents:
diff
changeset
|
204 |
#endif /* AI_FACTORY_HPP */ |