File: CTaskHandler.h

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (232 lines) | stat: -rw-r--r-- 5,027 bytes parent folder | download
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
#ifndef CTASKHANDLER_H
#define CTASKHANDLER_H

#include <vector>
#include <map>
#include <stack>

#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};

class ATask: public ARegistrar {
	public:
		ATask(AIClasses *ai): 
			ARegistrar(counter, std::string("task")) {
			active = false;
			counter++;
			isMoving = true;
			pos = ZEROVECTOR;
			group = NULL;
  	        this->ai = ai;
		}
		~ATask(){}

		bool active;

		/* The task in {BUILD, ASSIST, ATTACK, MERGE, FACTORY_BUILD} */
		task t;

		/* Task counter */
		static int counter;

		/* The assisters assisting this task */
		std::list<ATask*> assisters;

		/* The group(s) involved */
		CGroup *group;

		/* Determine if all groups in this task are moving or not */
		bool isMoving;

		/* The position to navigate too */
		// TODO: make it as method because for assisting task this position
		// may vary depending on master task 
		float3 pos;

		/* 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 */
		void resourceScan();

		/* Scan and micro for enemy targets */
		void enemyScan(bool scout);

		/* Update this task */
		virtual void update() = 0;

		RegistrarType regtype() { return REGT_TASK; } 

		friend std::ostream& operator<<(std::ostream &out, const ATask &task);

		AIClasses *ai;
		char buf[512];
};

class CTaskHandler: public ARegistrar {
	public:
		CTaskHandler(AIClasses *ai);
		~CTaskHandler(){};

		struct BuildTask: public ATask {
			BuildTask(AIClasses *_ai): ATask(_ai){t = BUILD; timer = 0;}

			/* The build task */
			buildType bt;

			/* The ETA in frames */
			unsigned eta;

			/* Time this buildtask has been active */
			unsigned timer;

			/* The UnitType to build */
			UnitType *toBuild;

			/* Update the build task, assumes 1 group on a task! */
			void update();

			bool assistable(CGroup &group, float &travelTime);

			void reset(float3 &pos, buildType bt, UnitType *ut);
		};

		struct FactoryTask: public ATask {
			FactoryTask(AIClasses *_ai): ATask(_ai){t = FACTORY_BUILD;}

			/* set the factorytask to wait including assisters */
			void setWait(bool wait);

			/* If a factory is idle, make sure it gets something to build */
			void update();

			bool assistable(CGroup &group);

			void reset(CUnit &factory);
		};

		struct AssistTask: public ATask {
			AssistTask(AIClasses *_ai): ATask(_ai) {t = ASSIST;}

			/* The (build)task to assist */
			ATask *assist;

			/* Update the assist task */
			void update();

			/* overload */
			void remove();

			/* overload */
			void remove(ARegistrar& group);
			
			void reset(ATask &task);
		};

		struct AttackTask: public ATask {
			AttackTask(AIClasses *_ai): ATask(_ai) {t = ATTACK;}

			/* The target to attack */
			int target;

			/* Update the attack task */
			void update();

			std::string enemy;

			void reset(int target);
		};

		struct MergeTask: public ATask {
			MergeTask(AIClasses *_ai): ATask(_ai) {t = MERGE;}

			/* The minimal range at which groups can merge */
			float range;

			std::map<int, CGroup*> groups;

			/* Update the merge task */
			void update();

			/* 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);

		/* 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 */
		void addAttackTask(int target, CGroup &group);

		/* Add a fresh merge task */
		void addMergeTask(std::map<int,CGroup*> &groups);

		/* Add a fresh factory task */
		void addFactoryTask(CGroup &group);

		/* Remove a task (is not used) */
		void removeTask(CGroup &group);

		const ATask* getTask(CGroup &group);

		/* Get the group destination */
		float3 getPos(CGroup &group);

		/* Update call */
		void update();

	private:
		AIClasses *ai;
		char buf[1024];

		/* The active tasks to update */
		std::map<int, ATask*> activeTasks;

		/* The group to task table */
		std::map<int, ATask*> groupToTask;

		/* Calculate avg range and pos of groups */
		static void getGroupsPos(std::vector<CGroup*> &groups, float3 &pos);
};

#endif