forked from Error323/E323AI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCTaskHandler.h
253 lines (203 loc) · 6.07 KB
/
CTaskHandler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifndef CTASKHANDLER_H
#define CTASKHANDLER_H
#include <vector>
#include <map>
#include <stack>
#include "CAI.h"
#include "ARegistrar.h"
#include "headers/Defines.h"
#include "headers/HEngine.h"
class UnitType;
class AIClasses;
class CGroup;
class CUnit;
enum task{BUILD, ASSIST, ATTACK, MERGE, FACTORY_BUILD, REPAIR, UPGRADE};
class ATask: public ARegistrar {
public:
ATask(AIClasses *ai):
ARegistrar(counter++, std::string("task")) {
this->ai = ai;
active = false;
isMoving = true;
pos = ZEROVECTOR;
group = NULL;
bornFrame = ai->cb->GetCurrentFrame();
validateInterval = 5 * 30;
nextValidateFrame = validateInterval;
nextTask = 0;
}
~ATask() {}
bool active;
// task is active
int bornFrame;
// frame when task was created
int validateInterval;
// validate interval in frames
int nextValidateFrame;
// next frame to execute task validation
task t;
// type of the task: BUILD, ASSIST, ATTACK, etc.
static int counter;
// task counter, used as task key; shared among all AI instances
std::list<ATask*> assisters;
// the assisters assisting this task
CGroup *future_group;
// the group to be involved; for Merge task it is master-group
CGroup *group;
// the group involved; for Merge task it is master-group
ATask* nextTask;
// task to execute after this one finished
bool isMoving;
// determine if all groups in this task are moving or not
float3 pos;
// the position to navigate too
// TODO: make it as method because for assisting task this position
// may vary depending on master task
/* Remove this task, unreg groups involved, and make them available
again */
virtual void remove();
/* Overload */
void remove(ARegistrar &group);
/* Add a group to this task */
void addGroup(CGroup &group);
/* Scan and micro for resources */
bool resourceScan();
/* Scan and micro for damaged units */
bool repairScan();
/* Scan and micro for enemy targets */
bool enemyScan();
/* Task lifetime in frames */
int lifeFrames() const;
/* Task lifetime in sec */
float lifeTime() const;
/* Update this task */
virtual void update();
/* Task validation function */
virtual bool validate() { return true; }
RegistrarType regtype() { return REGT_TASK; }
friend std::ostream& operator<<(std::ostream &out, const ATask &task);
AIClasses *ai;
};
class CTaskHandler: public ARegistrar {
public:
CTaskHandler(AIClasses *ai);
~CTaskHandler();
struct UpgradeTask: public ATask {
UpgradeTask(AIClasses *_ai): ATask(_ai) {t = UPGRADE;}
/* overload */
void update();
/* overload */
bool validate();
};
struct BuildTask: public ATask {
BuildTask(AIClasses *_ai): ATask(_ai) {t = BUILD;}
/* The build task */
buildType bt;
/* The ETA in frames */
unsigned int eta;
/* The UnitType to build */
UnitType *toBuild;
/* overload */
void update();
/* overload */
bool validate();
bool assistable(CGroup &group, float &travelTime);
};
struct FactoryTask: public ATask {
FactoryTask(AIClasses *_ai): ATask(_ai) {t = FACTORY_BUILD;}
/* set the factorytask to wait including assisters */
void setWait(bool wait);
/* overload */
void update();
bool assistable(CGroup &group);
/* overload */
bool validate();
};
struct AssistTask: public ATask {
AssistTask(AIClasses *_ai): ATask(_ai) {t = ASSIST;}
/* Task to assist */
ATask *assist;
/* overload */
void update();
/* overload */
void remove();
/* overload */
void remove(ARegistrar& group);
};
struct AttackTask: public ATask {
AttackTask(AIClasses *_ai): ATask(_ai) {t = ATTACK;}
bool urgent;
// if task is urgent then enemy scanning is disable while moving
int target;
// the target to attack
std::string enemy;
// enemy user name
/* Update the attack task */
void update();
/* overload */
bool validate();
};
struct MergeTask: public ATask {
MergeTask(AIClasses *_ai): ATask(_ai) { t = MERGE; isRetreating = false; }
bool isRetreating;
// are groups retreating?
float range;
// the minimal range at which groups can merge
std::map<int, CGroup*> groups;
// groups involved in merge process
std::map<int, bool> mergable; // key = <group_id>, value = <not_used>
// groups ready to merge
bool reelectMasterGroup();
/* Update the merge task */
void update();
/* overload */
bool validate();
/* overload */
void remove();
/* overload */
void remove(ARegistrar& group);
};
/* The -to be removed- tasks */
std::stack<ATask*> obsoleteTasks;
/* The active tasks per type */
std::map<int, BuildTask*> activeBuildTasks;
std::map<int, AssistTask*> activeAssistTasks;
std::map<int, AttackTask*> activeAttackTasks;
std::map<int, MergeTask*> activeMergeTasks;
std::map<int, FactoryTask*> activeFactoryTasks;
/* build type to string */
static std::map<buildType, std::string> buildStr;
/* task type to string */
static std::map<task, std::string> taskStr;
/* Overload */
void remove(ARegistrar &task);
/* activate previosly enqueued task */
void activateTask(ATask *atask);
/* Add an upgrade task */
void addUpgradeTask(buildType build, UnitType *toBuild, CGroup &group, float3 &pos);
/* Add a fresh build task */
void addBuildTask(buildType build, UnitType *toBuild, CGroup &group, float3 &pos);
/* Add a fresh assist task */
void addAssistTask(ATask &task, CGroup &group);
/* Add a fresh attack task */
bool addAttackTask(int target, CGroup &group, bool urgent = false);
/* Add a fresh merge task */
void addMergeTask(std::map<int,CGroup*> &groups);
/* Add a fresh factory task */
void addFactoryTask(CGroup &group);
ATask* getTask(CGroup &group);
ATask* getTaskByTarget(int);
/* Get the group destination */
float3 getPos(CGroup &group);
/* Update call */
void update();
private:
AIClasses *ai;
/* The active tasks to update */
std::map<int, ATask*> activeTasks;
/* The group to task table */
std::map<int, ATask*> groupToTask;
int statsMaxTasks;
int statsMaxActiveTasks;
};
#endif