forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.cpp
309 lines (265 loc) · 7.25 KB
/
group.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2019 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file group.c
*
* Link droids together into a group for AI etc.
*
*/
#include "lib/framework/frame.h"
#include "lib/netplay/netplay.h"
#include "multiplay.h"
#include "group.h"
#include "droid.h"
#include "order.h"
#include <map>
// Group system variables: grpGlobalManager enables to remove all the groups to Shutdown the system
static std::map<int, DROID_GROUP *> grpGlobalManager;
static bool grpInitialized = false;
// initialise the group system
bool grpInitialise()
{
grpGlobalManager.clear();
grpInitialized = true;
return true;
}
// shutdown the group system
void grpShutDown()
{
/* Since we are not very diligent removing groups after we have
* created them; we need this hack to remove them on level end. */
std::map<int, DROID_GROUP *>::iterator iter;
for (iter = grpGlobalManager.begin(); iter != grpGlobalManager.end(); iter++)
{
delete(iter->second);
}
grpGlobalManager.clear();
grpInitialized = false;
}
// Constructor
DROID_GROUP::DROID_GROUP()
{
type = GT_NORMAL;
refCount = 0;
psList = nullptr;
psCommander = nullptr;
}
// create a new group
DROID_GROUP *grpCreate(int id)
{
ASSERT(grpInitialized, "Group code not initialized yet");
DROID_GROUP *psGroup = new DROID_GROUP;
if (id == -1)
{
int i;
for (i = 0; grpGlobalManager.find(i) != grpGlobalManager.end(); i++) {} // surly hack
psGroup->id = i;
}
else
{
ASSERT(grpGlobalManager.find(id) == grpGlobalManager.end(), "Group %d is already created!", id);
psGroup->id = id;
}
grpGlobalManager.emplace(psGroup->id, psGroup);
return psGroup;
}
DROID_GROUP *grpFind(int id)
{
DROID_GROUP *psGroup = nullptr;
std::map<int, DROID_GROUP *>::iterator it = grpGlobalManager.find(id);
if (it != grpGlobalManager.end())
{
psGroup = it->second;
}
if (!psGroup)
{
psGroup = grpCreate(id);
}
return psGroup;
}
// add a droid to a group
void DROID_GROUP::add(DROID *psDroid)
{
ASSERT(grpInitialized, "Group code not initialized yet");
refCount += 1;
// if psDroid == NULL just increase the refcount don't add anything to the list
if (psDroid != nullptr)
{
if (psList && psDroid->player != psList->player)
{
ASSERT(false, "grpJoin: Cannot have more than one players droids in a group");
return;
}
if (psDroid->psGroup != nullptr)
{
psDroid->psGroup->remove(psDroid);
}
psDroid->psGroup = this;
if (isTransporter(psDroid))
{
ASSERT_OR_RETURN(, (type == GT_NORMAL), "grpJoin: Cannot have two transporters in a group");
ASSERT_OR_RETURN(, psList == nullptr, "Adding transporter to non-empty list.");
type = GT_TRANSPORTER;
psDroid->psGrpNext = psList;
psList = psDroid;
}
else if ((psDroid->droidType == DROID_COMMAND) && (type != GT_TRANSPORTER))
{
ASSERT_OR_RETURN(, (type == GT_NORMAL) && (psCommander == nullptr), "grpJoin: Cannot have two command droids in a group");
type = GT_COMMAND;
psCommander = psDroid;
}
else
{
psDroid->psGrpNext = psList;
psList = psDroid;
}
if (type == GT_COMMAND)
{
syncDebug("Droid %d joining command group %d", psDroid->id, psCommander != nullptr ? psCommander->id : 0);
}
}
}
// remove a droid from a group
void DROID_GROUP::remove(DROID *psDroid)
{
DROID *psPrev, *psCurr;
ASSERT_OR_RETURN(, grpInitialized, "Group code not initialized yet");
if (psDroid != nullptr && psDroid->psGroup != this)
{
ASSERT(false, "grpLeave: droid group does not match");
return;
}
// SyncDebug
if (psDroid != nullptr && type == GT_COMMAND)
{
syncDebug("Droid %d leaving command group %d", psDroid->id, psCommander != nullptr ? psCommander->id : 0);
}
refCount -= 1;
// if psDroid == NULL just decrease the refcount don't remove anything from the list
if (psDroid != nullptr)
{
// update group list of droids and droids' psGrpNext
if (psDroid->droidType != DROID_COMMAND || type != GT_COMMAND)
{
psPrev = nullptr;
for (psCurr = psList; psCurr; psCurr = psCurr->psGrpNext)
{
if (psCurr == psDroid)
{
break;
}
psPrev = psCurr;
}
ASSERT(psCurr != nullptr, "grpLeave: droid not found");
if (psCurr != nullptr)
{
if (psPrev)
{
psPrev->psGrpNext = psCurr->psGrpNext;
}
else
{
psList = psList->psGrpNext;
}
}
}
psDroid->psGroup = nullptr;
psDroid->psGrpNext = nullptr;
// update group's type
if ((psDroid->droidType == DROID_COMMAND) && (type == GT_COMMAND))
{
type = GT_NORMAL;
psCommander = nullptr;
}
else if (isTransporter(psDroid) && (type == GT_TRANSPORTER))
{
type = GT_NORMAL;
}
}
// free the group if necessary
if (refCount <= 0)
{
grpGlobalManager.erase(id);
delete this;
}
}
// count the members of a group
unsigned int DROID_GROUP::getNumMembers()
{
const DROID *psCurr;
unsigned int num;
ASSERT(grpInitialized, "Group code not initialized yet");
num = 0;
for (psCurr = psList; psCurr; psCurr = psCurr->psGrpNext)
{
num += 1;
}
return num;
}
// remove all droids from a group
void DROID_GROUP::removeAll()
{
DROID *psCurr, *psNext;
ASSERT(grpInitialized, "Group code not initialized yet");
for (psCurr = psList; psCurr; psCurr = psNext)
{
psNext = psCurr->psGrpNext;
remove(psCurr);
}
}
// Give a group of droids an order
void DROID_GROUP::orderGroup(DROID_ORDER order)
{
DROID *psCurr;
ASSERT(grpInitialized, "Group code not initialized yet");
for (psCurr = psList; psCurr; psCurr = psCurr->psGrpNext)
{
orderDroid(psCurr, order, ModeQueue);
}
}
// Give a group of droids an order (using a Location)
void DROID_GROUP::orderGroup(DROID_ORDER order, UDWORD x, UDWORD y)
{
DROID *psCurr;
ASSERT(grpInitialized, "Group code not initialized yet");
ASSERT_OR_RETURN(, validOrderForLoc(order), "orderGroup: Bad order");
for (psCurr = psList; psCurr != nullptr; psCurr = psCurr->psGrpNext)
{
orderDroidLoc(psCurr, order, x, y, bMultiMessages ? ModeQueue : ModeImmediate);
}
}
// Give a group of droids an order (using an Object)
void DROID_GROUP::orderGroup(DROID_ORDER order, BASE_OBJECT *psObj)
{
DROID *psCurr;
ASSERT_OR_RETURN(, validOrderForObj(order), "orderGroup: Bad order");
for (psCurr = psList; psCurr != nullptr; psCurr = psCurr->psGrpNext)
{
orderDroidObj(psCurr, order, (BASE_OBJECT *)psObj, bMultiMessages ? ModeQueue : ModeImmediate);
}
}
// Set the secondary state for a group of droids
void DROID_GROUP::setSecondary(SECONDARY_ORDER sec, SECONDARY_STATE state)
{
DROID *psCurr;
ASSERT(grpInitialized, "Group code not initialized yet");
for (psCurr = psList; psCurr; psCurr = psCurr->psGrpNext)
{
secondarySetState(psCurr, sec, state);
}
}