File: WaitCommandsAI.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (198 lines) | stat: -rw-r--r-- 5,323 bytes parent folder | download | duplicates (3)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef WAIT_COMMANDS_AI_H
#define WAIT_COMMANDS_AI_H

#include <deque>
#include <string>

#include "System/Object.h"
#include "System/Misc/SpringTime.h"
#include "System/UnorderedMap.hpp"
#include "System/UnorderedSet.hpp"

class float3;
class CObject;
class CUnit;
struct Command;
class CCommandQueue;

class CWaitCommandsAI {
	CR_DECLARE_STRUCT(CWaitCommandsAI)
	CR_DECLARE_SUB(Wait)
	CR_DECLARE_SUB(TimeWait)
	CR_DECLARE_SUB(DeathWait)
	CR_DECLARE_SUB(SquadWait)
	CR_DECLARE_SUB(GatherWait)

	public:
		typedef spring::unordered_set<int> CUnitSet;

		CWaitCommandsAI();
		~CWaitCommandsAI();

		void Update();
		void DrawCommands() const;

		// called from SelectedUnits
		void AddTimeWait(const Command& cmd);
		void AddDeathWait(const Command& cmd);
		void AddSquadWait(const Command& cmd);
		void AddGatherWait(const Command& cmd);

		/// acknowledge a command received from the network
		void AcknowledgeCommand(const Command& cmd);

		/// search a new unit's queue and add it to its wait commands
		void AddLocalUnit(CUnit* unit, const CUnit* builder);

		void ClearUnitQueue(CUnit* unit, const CCommandQueue& queue);
		void RemoveWaitCommand(CUnit* unit, const Command& cmd);

		void AddIcon(const Command& cmd, const float3& pos) const;

	private:
		class Wait;
		bool InsertWaitObject(Wait* wait);
		void RemoveWaitObject(Wait* wait);

	private:
		typedef int KeyType;
		typedef spring::unordered_map<KeyType, Wait*> WaitMap;
		WaitMap waitMap;
		WaitMap unackedMap;

	private:
		// Wait Base Class
		class Wait : public CObject {
			CR_DECLARE_DERIVED(Wait)
			public:
				virtual ~Wait();
				virtual void DependentDied(CObject* o) override = 0; // from CObject
				virtual void AddUnit(CUnit* unit) = 0;
				virtual void RemoveUnit(CUnit* unit) = 0;
				virtual void Update() = 0;
				virtual void Draw() const {}
				virtual void AddUnitPosition(const float3& pos) {}
				virtual const std::string& GetStateText() const { return noText; }
			public:
				spring_time GetDeadTime() const { return deadTime; }
				float GetCode() const { return code; }
				KeyType GetKey() const { return key; }
			public:
				static KeyType GetKeyFromFloat(float f);
				static float GetFloatFromKey(KeyType k);
			protected:
				Wait(float code);
				enum WaitState {
					Active, Queued, Missing
				};
				WaitState GetWaitState(const CUnit* unit) const;
				bool IsWaitingOn(const CUnit* unit) const;
				void SendCommand(const Command& cmd, const CUnitSet& unitSet);
				void SendWaitCommand(const CUnitSet& unitSet);

			protected:
				float code;
				KeyType key;
				bool valid;
				spring_time deadTime;
			protected:
				static KeyType GetNewKey();
			private:
				static KeyType keySource;
				static const std::string noText;
				void PostLoad();
		};

		// TimeWait
		class TimeWait : public Wait {
			CR_DECLARE_DERIVED(TimeWait)
			public:
				static TimeWait* New(const Command& cmd, CUnit* unit);
				static TimeWait* New(int duration, CUnit* unit);
				~TimeWait();
				void DependentDied(CObject* o) override;
				void AddUnit(CUnit* unit) override;
				void RemoveUnit(CUnit* unit) override;
				void Update() override;
				void Draw() const override;
				const std::string& GetStateText() const override;
				int GetDuration() const { return duration; }
			private:
				TimeWait(const Command& cmd, CUnit* unit);
				TimeWait(int duration, CUnit* unit);
			private:
				CUnit* unit;
				bool enabled;
				int duration;
				int endFrame;
				bool factory;
		};

		// DeathWait
		class DeathWait : public Wait {
			CR_DECLARE_DERIVED(DeathWait)
			public:
				static DeathWait* New(const Command& cmd);
				~DeathWait();
				void DependentDied(CObject* o) override;
				void AddUnit(CUnit* unit) override;
				void RemoveUnit(CUnit* unit) override;
				void Update() override;
				void Draw() const override;
				void AddUnitPosition(const float3& pos) override;
			private:
				DeathWait(const Command& cmd);
				void SelectAreaUnits(const float3& pos0, const float3& pos1,
				                     CUnitSet& units, bool enemies);
			private:
				CUnitSet waitUnits;
				CUnitSet deathUnits;
				std::vector<float3> unitPos;
		};

		// SquadWait
		class SquadWait : public Wait {
			CR_DECLARE_DERIVED(SquadWait)
			public:
				static SquadWait* New(const Command& cmd);
				~SquadWait();
				void DependentDied(CObject* o) override;
				void AddUnit(CUnit* unit) override;
				void RemoveUnit(CUnit* unit) override;
				void Update() override;
				void Draw() const override;
				const std::string& GetStateText() const override { return stateText; }
			private:
				SquadWait(const Command& cmd);
				void UpdateText();
			private:
				int squadCount;
				CUnitSet buildUnits;
				CUnitSet waitUnits;
				std::string stateText;
		};

		// GatherWait
		class GatherWait : public Wait {
			CR_DECLARE_DERIVED(GatherWait)
			public:
				static GatherWait* New(const Command& cmd);
				~GatherWait();
				void DependentDied(CObject * o) override;
				void AddUnit(CUnit* unit) override;
				void RemoveUnit(CUnit* unit) override;
				void Update() override;
			private:
				GatherWait(const Command& cmd);
			private:
				CUnitSet waitUnits;
		};
};


extern CWaitCommandsAI waitCommandsAI;


#endif /* WAIT_COMMANDS_AI_H */