forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjmem.cpp
1019 lines (893 loc) · 26.7 KB
/
objmem.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 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
*/
/*
* ObjMem.c
*
* Object memory management functions.
*
*/
#include <string.h>
#include "lib/framework/frame.h"
#include "objects.h"
#include "lib/gamelib/gtime.h"
#include "lib/netplay/netplay.h"
#include "hci.h"
#include "map.h"
#include "power.h"
#include "objects.h"
#include "lib/script/script.h"
#include "scriptvals.h"
#include "scripttabs.h"
#include "scriptcb.h"
#include "mission.h"
#include "structuredef.h"
#include "structure.h"
#include "droid.h"
#include "mapgrid.h"
#include "combat.h"
#include "visibility.h"
#include "qtscript.h"
// the initial value for the object ID
#define OBJ_ID_INIT 20000
/* The id number for the next object allocated
* Each object will have a unique id number irrespective of type
*/
uint32_t unsynchObjID;
uint32_t synchObjID;
/* The lists of objects allocated */
DROID *apsDroidLists[MAX_PLAYERS];
STRUCTURE *apsStructLists[MAX_PLAYERS];
FEATURE *apsFeatureLists[MAX_PLAYERS]; ///< Only player zero is valid for features. TODO: Reduce to single list.
STRUCTURE *apsExtractorLists[MAX_PLAYERS];
FEATURE *apsOilList[1];
BASE_OBJECT *apsSensorList[1]; ///< List of sensors in the game.
/*The list of Flag Positions allocated */
FLAG_POSITION *apsFlagPosLists[MAX_PLAYERS];
/* The list of destroyed objects */
BASE_OBJECT *psDestroyedObj=NULL;
/* Forward function declarations */
#ifdef DEBUG
static void objListIntegCheck(void);
#endif
/* Initialise the object heaps */
bool objmemInitialise(void)
{
// reset the object ID number
unsynchObjID = OBJ_ID_INIT/2; // /2 so that object IDs start around OBJ_ID_INIT*8, in case that's important when loading maps.
synchObjID = OBJ_ID_INIT*4; // *4 so that object IDs start around OBJ_ID_INIT*8, in case that's important when loading maps.
return true;
}
/* Release the object heaps */
void objmemShutdown(void)
{
}
/* Remove an object from the destroyed list, finally freeing its memory
* Hopefully by this time, no pointers still refer to it! */
static bool objmemDestroy(BASE_OBJECT *psObj)
{
switch (psObj->type)
{
case OBJ_DROID:
debug(LOG_MEMORY, "freeing droid at %p", psObj);
if (!droidCheckReferences((DROID *)psObj))
{
return false;
}
break;
case OBJ_STRUCTURE:
debug(LOG_MEMORY, "freeing structure at %p", psObj);
if (!structureCheckReferences((STRUCTURE *)psObj))
{
return false;
}
break;
case OBJ_FEATURE:
debug(LOG_MEMORY, "freeing feature at %p", psObj);
break;
default:
ASSERT(!"unknown object type", "objmemDestroy: unknown object type in destroyed list at 0x%p", psObj);
}
delete psObj;
debug(LOG_MEMORY, "BASE_OBJECT* 0x%p is freed.", psObj);
return true;
}
/* General housekeeping for the object system */
void objmemUpdate(void)
{
BASE_OBJECT *psCurr, *psNext, *psPrev;
#ifdef DEBUG
// do a general validity check first
objListIntegCheck();
#endif
// tell the script system about any destroyed objects
if (psDestroyedObj != NULL)
{
scrvUpdateBasePointers();
}
/* Go through the destroyed objects list looking for objects that
were destroyed before this turn */
/* First remove the objects from the start of the list */
while (psDestroyedObj != NULL && psDestroyedObj->died <= gameTime - deltaGameTime)
{
psNext = psDestroyedObj->psNext;
if (!objmemDestroy(psDestroyedObj))
{
if (psDestroyedObj->type == OBJ_DROID)
{
debug(LOG_DEATH, "skipping %p (%s: id %u) this round, will try again later.", psDestroyedObj, ((DROID *)psDestroyedObj)->aName, ((DROID *)psDestroyedObj)->id);
return;
}
}
psDestroyedObj = psNext;
}
/* Now see if there are any further down the list
Keep track of the previous object to set its Next pointer*/
for(psCurr = psPrev = psDestroyedObj; psCurr != NULL; psCurr = psNext)
{
psNext = psCurr->psNext;
if (psCurr->died <= gameTime - deltaGameTime)
{
objmemDestroy(psCurr);
/*set the linked list up - you will never be deleting the top
of the list, so don't have to check*/
psPrev->psNext = psNext;
}
else
{
// do the object died callback
psCBObjDestroyed = psCurr;
eventFireCallbackTrigger((TRIGGER_TYPE)CALL_OBJ_DESTROYED);
switch (psCurr->type)
{
case OBJ_DROID:
eventFireCallbackTrigger((TRIGGER_TYPE)CALL_DROID_DESTROYED);
break;
case OBJ_STRUCTURE:
eventFireCallbackTrigger((TRIGGER_TYPE)CALL_STRUCT_DESTROYED);
break;
case OBJ_FEATURE:
eventFireCallbackTrigger((TRIGGER_TYPE)CALL_FEATURE_DESTROYED);
break;
default:
break;
}
psCBObjDestroyed = NULL;
triggerEventDestroyed(psCurr);
psPrev = psCurr;
}
}
}
uint32_t generateNewObjectId(void)
{
// Generate even ID for unsynchronized objects. This is needed for debug objects, templates and other border lines cases that should preferably be removed one day.
return unsynchObjID++*MAX_PLAYERS*2 + selectedPlayer*2; // Was taken from createObject, where 'player' was used instead of 'selectedPlayer'. Hope there are no stupid hacks that try to recover 'player' from the last 3 bits.
}
uint32_t generateSynchronisedObjectId(void)
{
// Generate odd ID for synchronized objects
uint32_t ret = synchObjID++*2 + 1;
syncDebug("New objectId = %u", ret);
return ret;
}
/* Add the object to its list
* \param list is a pointer to the object list
*/
template <typename OBJECT>
static inline void addObjectToList(OBJECT *list[], OBJECT *object, int player)
{
ASSERT(object != NULL, "Invalid pointer");
// Prepend the object to the top of the list
object->psNext = list[player];
list[player] = object;
}
/* Add the object to its list
* \param list is a pointer to the object list
*/
template <typename OBJECT>
static inline void addObjectToFuncList(OBJECT *list[], OBJECT *object, int player)
{
ASSERT(object != NULL, "Invalid pointer");
ASSERT_OR_RETURN(, static_cast<OBJECT *>(object->psNextFunc) == NULL, "%s(%p) is already in a function list!", objInfo(object), object);
// Prepend the object to the top of the list
object->psNextFunc = list[player];
list[player] = object;
}
/* Move an object from the active list to the destroyed list.
* \param list is a pointer to the object list
* \param del is a pointer to the object to remove
*/
template <typename OBJECT>
static inline void destroyObject(OBJECT* list[], OBJECT* object)
{
ASSERT(object != NULL,
"destroyObject: Invalid pointer");
scriptRemoveObject(object);
// If the message to remove is the first one in the list then mark the next one as the first
if (list[object->player] == object)
{
list[object->player] = list[object->player]->psNext;
object->psNext = psDestroyedObj;
psDestroyedObj = (BASE_OBJECT *)object;
object->died = gameTime;
return;
}
// Iterate through the list and find the item before the object to delete
OBJECT *psPrev = NULL, *psCurr;
for(psCurr = list[object->player]; (psCurr != object) && (psCurr != NULL); psCurr = psCurr->psNext)
{
psPrev = psCurr;
}
ASSERT(psCurr != NULL,
"destroyObject: object not found in list");
if (psCurr != NULL)
{
// Modify the "next" pointer of the previous item to
// point to the "next" item of the item to delete.
psPrev->psNext = psCurr->psNext;
// Prepend the object to the destruction list
object->psNext = psDestroyedObj;
psDestroyedObj = object;
// Set destruction time
object->died = gameTime;
}
}
/* Remove an object from the active list
* \param list is a pointer to the object list
* \param remove is a pointer to the object to remove
* \param type is the type of the object
*/
template <typename OBJECT>
static inline void removeObjectFromList(OBJECT *list[], OBJECT *object, int player)
{
ASSERT_OR_RETURN(, object != NULL, "Invalid pointer");
// If the message to remove is the first one in the list then mark the next one as the first
if (list[player] == object)
{
list[player] = list[player]->psNext;
return;
}
// Iterate through the list and find the item before the object to delete
OBJECT *psPrev = NULL, *psCurr;
for(psCurr = list[player]; (psCurr != object) && (psCurr != NULL); psCurr = psCurr->psNext)
{
psPrev = psCurr;
}
ASSERT_OR_RETURN(, psCurr != NULL, "Object %p not found in list", object);
// Modify the "next" pointer of the previous item to
// point to the "next" item of the item to delete.
psPrev->psNext = psCurr->psNext;
}
/* Remove an object from the relevant function list. An object can only be in one function list at a time!
* \param list is a pointer to the object list
* \param remove is a pointer to the object to remove
* \param type is the type of the object
*/
template <typename OBJECT>
static inline void removeObjectFromFuncList(OBJECT *list[], OBJECT *object, int player)
{
ASSERT_OR_RETURN(, object != NULL, "Invalid pointer");
// If the message to remove is the first one in the list then mark the next one as the first
if (list[player] == object)
{
list[player] = list[player]->psNextFunc;
object->psNextFunc = NULL;
return;
}
// Iterate through the list and find the item before the object to delete
OBJECT *psPrev = NULL, *psCurr;
for(psCurr = list[player]; psCurr != object && psCurr != NULL; psCurr = psCurr->psNextFunc)
{
psPrev = psCurr;
}
ASSERT_OR_RETURN(, psCurr != NULL, "Object %p not found in list", object);
// Modify the "next" pointer of the previous item to
// point to the "next" item of the item to delete.
psPrev->psNextFunc = psCurr->psNextFunc;
object->psNextFunc = NULL;
}
template <typename OBJECT>
static inline void releaseAllObjectsInList(OBJECT *list[])
{
// Iterate through all players' object lists
for (unsigned i = 0; i < MAX_PLAYERS; ++i)
{
// Iterate through all objects in list
OBJECT *psNext;
for (OBJECT *psCurr = list[i]; psCurr != NULL; psCurr = psNext)
{
psNext = psCurr->psNext;
// FIXME: the next call is disabled for now, yes, it will leak memory again.
// issue is with campaign games, and the swapping pointers 'trick' Pumpkin uses.
// visRemoveVisibility(psCurr);
// Release object's memory
delete psCurr;
}
list[i] = NULL;
}
}
/***************************************************************************************
*
* The actual object memory management functions for the different object types
*/
/*************************** DROID *********************************/
/* add the droid to the Droid Lists */
void addDroid(DROID *psDroidToAdd, DROID *pList[MAX_PLAYERS])
{
DROID_GROUP *psGroup;
addObjectToList(pList, psDroidToAdd, psDroidToAdd->player);
/* Whenever a droid gets added to a list other than the current list
* its died flag is set to NOT_CURRENT_LIST so that anything targetting
* it will cancel itself - HACK?! */
if (pList[psDroidToAdd->player] == apsDroidLists[psDroidToAdd->player])
{
psDroidToAdd->died = false;
if (psDroidToAdd->droidType == DROID_SENSOR)
{
addObjectToFuncList(apsSensorList, (BASE_OBJECT*)psDroidToAdd, 0);
}
// commanders have to get their group back
if (psDroidToAdd->droidType == DROID_COMMAND)
{
psGroup = grpCreate();
psGroup->add(psDroidToAdd);
}
}
else if (pList[psDroidToAdd->player] == mission.apsDroidLists[psDroidToAdd->player])
{
if (psDroidToAdd->droidType == DROID_SENSOR)
{
addObjectToFuncList(mission.apsSensorList, (BASE_OBJECT*)psDroidToAdd, 0);
}
}
}
/* Destroy a droid */
void killDroid(DROID *psDel)
{
int i;
ASSERT( psDel->type == OBJ_DROID,
"killUnit: pointer is not a unit" );
ASSERT( psDel->player < MAX_PLAYERS,
"killUnit: invalid player for unit" );
setDroidTarget(psDel, NULL);
for (i = 0; i < DROID_MAXWEAPS; i++)
{
setDroidActionTarget(psDel, NULL, i);
}
setDroidBase(psDel, NULL);
if (psDel->droidType == DROID_SENSOR)
{
removeObjectFromFuncList(apsSensorList, (BASE_OBJECT *)psDel, 0);
}
destroyObject(apsDroidLists, psDel);
}
/* Remove all droids */
void freeAllDroids(void)
{
releaseAllObjectsInList(apsDroidLists);
}
/*Remove a single Droid from a list*/
void removeDroid(DROID *psDroidToRemove, DROID *pList[MAX_PLAYERS])
{
ASSERT( psDroidToRemove->type == OBJ_DROID,
"removeUnit: pointer is not a unit" );
ASSERT( psDroidToRemove->player < MAX_PLAYERS,
"removeUnit: invalid player for unit" );
removeObjectFromList(pList, psDroidToRemove, psDroidToRemove->player);
/* Whenever a droid is removed from the current list its died
* flag is set to NOT_CURRENT_LIST so that anything targetting
* it will cancel itself, and we know it is not really on the map. */
if (pList[psDroidToRemove->player] == apsDroidLists[psDroidToRemove->player])
{
if (psDroidToRemove->droidType == DROID_SENSOR)
{
removeObjectFromFuncList(apsSensorList, (BASE_OBJECT*)psDroidToRemove, 0);
}
psDroidToRemove->died = NOT_CURRENT_LIST;
}
else if (pList[psDroidToRemove->player] == mission.apsDroidLists[psDroidToRemove->player])
{
if (psDroidToRemove->droidType == DROID_SENSOR)
{
removeObjectFromFuncList(mission.apsSensorList, (BASE_OBJECT*)psDroidToRemove, 0);
}
}
}
/*Removes all droids that may be stored in the mission lists*/
void freeAllMissionDroids(void)
{
releaseAllObjectsInList(mission.apsDroidLists);
}
/*Removes all droids that may be stored in the limbo lists*/
void freeAllLimboDroids(void)
{
releaseAllObjectsInList(apsLimboDroids);
}
/************************** STRUCTURE *******************************/
/* add the structure to the Structure Lists */
void addStructure(STRUCTURE *psStructToAdd)
{
addObjectToList(apsStructLists, psStructToAdd, psStructToAdd->player);
if (psStructToAdd->pStructureType->pSensor
&& psStructToAdd->pStructureType->pSensor->location == LOC_TURRET)
{
addObjectToFuncList(apsSensorList, (BASE_OBJECT*)psStructToAdd, 0);
}
else if (psStructToAdd->pStructureType->type == REF_RESOURCE_EXTRACTOR)
{
addObjectToFuncList(apsExtractorLists, psStructToAdd, psStructToAdd->player);
}
}
/* Destroy a structure */
void killStruct(STRUCTURE *psBuilding)
{
int i;
ASSERT( psBuilding->type == OBJ_STRUCTURE,
"killStruct: pointer is not a droid" );
ASSERT( psBuilding->player < MAX_PLAYERS,
"killStruct: invalid player for stucture" );
if (psBuilding->pStructureType->pSensor
&& psBuilding->pStructureType->pSensor->location == LOC_TURRET)
{
removeObjectFromFuncList(apsSensorList, (BASE_OBJECT*)psBuilding, 0);
}
else if (psBuilding->pStructureType->type == REF_RESOURCE_EXTRACTOR)
{
removeObjectFromFuncList(apsExtractorLists, psBuilding, psBuilding->player);
}
for (i = 0; i < STRUCT_MAXWEAPS; i++)
{
setStructureTarget(psBuilding, NULL, i, ORIGIN_UNKNOWN);
}
if (psBuilding->pFunctionality != NULL)
{
if (StructIsFactory(psBuilding))
{
FACTORY *psFactory = &psBuilding->pFunctionality->factory;
// remove any commander from the factory
if (psFactory->psCommander != NULL)
{
assignFactoryCommandDroid(psBuilding, NULL);
}
// remove any assembly points
if (psFactory->psAssemblyPoint != NULL)
{
removeFlagPosition(psFactory->psAssemblyPoint);
psFactory->psAssemblyPoint = NULL;
}
}
else if (psBuilding->pStructureType->type == REF_REPAIR_FACILITY)
{
REPAIR_FACILITY *psRepair = &psBuilding->pFunctionality->repairFacility;
if (psRepair->psDeliveryPoint)
{
// free up repair fac stuff
removeFlagPosition(psRepair->psDeliveryPoint);
psRepair->psDeliveryPoint = NULL;
}
}
}
destroyObject(apsStructLists, psBuilding);
}
/* Remove heapall structures */
void freeAllStructs(void)
{
releaseAllObjectsInList(apsStructLists);
}
/*Remove a single Structure from a list*/
void removeStructureFromList(STRUCTURE *psStructToRemove, STRUCTURE *pList[MAX_PLAYERS])
{
ASSERT( psStructToRemove->type == OBJ_STRUCTURE,
"removeStructureFromList: pointer is not a structure" );
ASSERT( psStructToRemove->player < MAX_PLAYERS,
"removeStructureFromList: invalid player for structure" );
removeObjectFromList(pList, psStructToRemove, psStructToRemove->player);
if (psStructToRemove->pStructureType->pSensor
&& psStructToRemove->pStructureType->pSensor->location == LOC_TURRET)
{
removeObjectFromFuncList(apsSensorList, (BASE_OBJECT*)psStructToRemove, 0);
}
else if (psStructToRemove->pStructureType->type == REF_RESOURCE_EXTRACTOR)
{
removeObjectFromFuncList(apsExtractorLists, psStructToRemove, psStructToRemove->player);
}
}
/************************** FEATURE *********************************/
/* add the feature to the Feature Lists */
void addFeature(FEATURE *psFeatureToAdd)
{
addObjectToList(apsFeatureLists, psFeatureToAdd, 0);
if (psFeatureToAdd->psStats->subType == FEAT_OIL_RESOURCE)
{
addObjectToFuncList(apsOilList, psFeatureToAdd, 0);
}
}
/* Destroy a feature */
// set the player to 0 since features have player = maxplayers+1. This screws up destroyObject
// it's a bit of a hack, but hey, it works
void killFeature(FEATURE *psDel)
{
ASSERT( psDel->type == OBJ_FEATURE,
"killFeature: pointer is not a feature" );
psDel->player = 0;
destroyObject(apsFeatureLists, psDel);
if (psDel->psStats->subType == FEAT_OIL_RESOURCE)
{
removeObjectFromFuncList(apsOilList, psDel, 0);
}
}
/* Remove all features */
void freeAllFeatures(void)
{
releaseAllObjectsInList(apsFeatureLists);
}
/************************** FLAG_POSITION ********************************/
/* Create a new Flag Position */
bool createFlagPosition(FLAG_POSITION **ppsNew, UDWORD player)
{
ASSERT( player<MAX_PLAYERS, "createFlagPosition: invalid player number" );
*ppsNew = (FLAG_POSITION *)calloc(1, sizeof(FLAG_POSITION));
if (*ppsNew == NULL)
{
debug(LOG_ERROR, "Out of memory");
return false;
}
(*ppsNew)->type = POS_DELIVERY;
(*ppsNew)->player = player;
(*ppsNew)->frameNumber = 0;
(*ppsNew)->selected = false;
(*ppsNew)->coords.x = ~0;
(*ppsNew)->coords.y = ~0;
(*ppsNew)->coords.z = ~0;
return true;
}
/* add the Flag Position to the Flag Position Lists */
void addFlagPosition(FLAG_POSITION *psFlagPosToAdd)
{
ASSERT( psFlagPosToAdd != NULL,
"addFlagPosition: Invalid FlagPosition pointer" );
ASSERT(psFlagPosToAdd->coords.x != ~0, "flag has invalid position");
psFlagPosToAdd->psNext = apsFlagPosLists[psFlagPosToAdd->player];
apsFlagPosLists[psFlagPosToAdd->player] = psFlagPosToAdd;
}
/* Remove a Flag Position from the Lists */
void removeFlagPosition(FLAG_POSITION *psDel)
{
FLAG_POSITION *psPrev=NULL, *psCurr;
ASSERT( psDel != NULL,
"removeFlagPosition: Invalid Flag Position pointer" );
if (apsFlagPosLists[psDel->player] == psDel)
{
apsFlagPosLists[psDel->player] = apsFlagPosLists[psDel->player]->psNext;
free(psDel);
}
else
{
for(psCurr = apsFlagPosLists[psDel->player]; (psCurr != psDel) &&
(psCurr != NULL); psCurr = psCurr->psNext)
{
psPrev = psCurr;
}
if (psCurr != NULL)
{
psPrev->psNext = psCurr->psNext;
free(psCurr);
}
}
}
// free all flag positions
void freeAllFlagPositions(void)
{
FLAG_POSITION *psNext;
SDWORD player;
for(player=0; player<MAX_PLAYERS; player++)
{
while (apsFlagPosLists[player])
{
psNext = apsFlagPosLists[player]->psNext;
free(apsFlagPosLists[player]);
apsFlagPosLists[player] = psNext;
}
}
}
#ifdef DEBUG
// check all flag positions for duplicate delivery points
void checkFactoryFlags(void)
{
static std::vector<unsigned> factoryDeliveryPointCheck[NUM_FLAG_TYPES]; // Static to save allocations.
//check the flags
for (unsigned player = 0; player < MAX_PLAYERS; ++player)
{
//clear the check array
for (int type = 0; type < NUM_FLAG_TYPES; ++type)
{
factoryDeliveryPointCheck[type].clear();
}
FLAG_POSITION *psFlag = apsFlagPosLists[player];
while (psFlag)
{
if ((psFlag->type == POS_DELIVERY) &&//check this is attached to a unique factory
(psFlag->factoryType != REPAIR_FLAG))
{
unsigned type = psFlag->factoryType;
unsigned factory = psFlag->factoryInc;
factoryDeliveryPointCheck[type].push_back(factory);
}
psFlag = psFlag->psNext;
}
for (int type = 0; type < NUM_FLAG_TYPES; ++type)
{
std::sort(factoryDeliveryPointCheck[type].begin(), factoryDeliveryPointCheck[type].end());
ASSERT(std::unique(factoryDeliveryPointCheck[type].begin(), factoryDeliveryPointCheck[type].end()) == factoryDeliveryPointCheck[type].end(), "DUPLICATE FACTORY DELIVERY POINT FOUND");
}
}
}
#endif
/************************** OBJECT ACCESS FUNCTIONALITY ********************************/
// Find a base object from it's id
BASE_OBJECT *getBaseObjFromData(unsigned id, unsigned player, OBJECT_TYPE type)
{
BASE_OBJECT *psObj;
DROID *psTrans;
for (int i = 0; i < 3; ++i)
{
psObj = NULL;
switch (i)
{
case 0:
switch (type)
{
case OBJ_DROID: psObj = apsDroidLists[player]; break;
case OBJ_STRUCTURE: psObj = apsStructLists[player]; break;
case OBJ_FEATURE: psObj= apsFeatureLists[0];
default: break;
}
break;
case 1:
switch (type)
{
case OBJ_DROID: psObj = mission.apsDroidLists[player]; break;
case OBJ_STRUCTURE: psObj = mission.apsStructLists[player]; break;
case OBJ_FEATURE: psObj = mission.apsFeatureLists[0]; break;
default: break;
}
break;
case 2:
if (player == 0 && type == OBJ_DROID)
{
psObj = apsLimboDroids[0];
}
break;
}
while (psObj)
{
if (psObj->id == id)
{
return psObj;
}
// if transporter check any droids in the grp
if ((psObj->type == OBJ_DROID) && ((((DROID*)psObj)->droidType == DROID_TRANSPORTER || ((DROID*)psObj)->droidType == DROID_SUPERTRANSPORTER)))
{
for(psTrans = ((DROID*)psObj)->psGroup->psList; psTrans != NULL; psTrans = psTrans->psGrpNext)
{
if (psTrans->id == id)
{
return (BASE_OBJECT*)psTrans;
}
}
}
psObj = psObj->psNext;
}
}
ASSERT(false, "failed to find id %d for player %d", id, player);
return NULL;
}
// Find a base object from it's id
BASE_OBJECT *getBaseObjFromId(UDWORD id)
{
unsigned int i;
UDWORD player;
BASE_OBJECT *psObj;
DROID *psTrans;
for(i = 0; i < 7; ++i)
{
for(player = 0; player < MAX_PLAYERS; ++player)
{
switch (i)
{
case 0:
psObj=(BASE_OBJECT *)apsDroidLists[player];
break;
case 1:
psObj=(BASE_OBJECT *)apsStructLists[player];
break;
case 2:
if (player == 0)
{
psObj=(BASE_OBJECT *)apsFeatureLists[0];
}
else
{
psObj = NULL;
}
break;
case 3:
psObj=(BASE_OBJECT *)mission.apsDroidLists[player];
break;
case 4:
psObj=(BASE_OBJECT *)mission.apsStructLists[player];
break;
case 5:
if (player == 0)
{
psObj=(BASE_OBJECT *)mission.apsFeatureLists[0];
}
else
{
psObj = NULL;
}
break;
case 6:
if (player == 0)
{
psObj=(BASE_OBJECT *)apsLimboDroids[0];
}
else
{
psObj = NULL;
}
break;
default:
psObj = NULL;
break;
}
while (psObj)
{
if (psObj->id == id)
{
return psObj;
}
// if transporter check any droids in the grp
if ((psObj->type == OBJ_DROID) && ((((DROID*)psObj)->droidType == DROID_TRANSPORTER || ((DROID*)psObj)->droidType == DROID_SUPERTRANSPORTER)))
{
for(psTrans = ((DROID*)psObj)->psGroup->psList; psTrans != NULL; psTrans = psTrans->psGrpNext)
{
if (psTrans->id == id)
{
return (BASE_OBJECT*)psTrans;
}
}
}
psObj = psObj->psNext;
}
}
}
ASSERT(!"couldn't find a BASE_OBJ with ID", "getBaseObjFromId() failed for id %d", id);
return NULL;
}
UDWORD getRepairIdFromFlag(FLAG_POSITION *psFlag)
{
unsigned int i;
UDWORD player;
STRUCTURE *psObj;
REPAIR_FACILITY *psRepair;
player = psFlag->player;
//probably dont need to check mission list
for(i = 0; i < 2; ++i)
{
switch (i)
{
case 0:
psObj=(STRUCTURE *)apsStructLists[player];
break;
case 1:
psObj=(STRUCTURE *)mission.apsStructLists[player];
break;
default:
psObj = NULL;
break;
}
while (psObj)
{
if (psObj->pFunctionality)
{
if (psObj->pStructureType->type == REF_REPAIR_FACILITY)
{
//check for matching delivery point
psRepair = ((REPAIR_FACILITY *)psObj->pFunctionality);
if (psRepair->psDeliveryPoint == psFlag)
{
return psObj->id;
}
}
}
psObj = psObj->psNext;
}
}
ASSERT(!"unable to find repair id for FLAG_POSITION", "getRepairIdFromFlag() failed");
return UDWORD_MAX;
}
// check a base object exists for an ID
bool checkValidId(UDWORD id)
{
return getBaseObjFromId(id) != NULL;
}
// integrity check the lists
#ifdef DEBUG
static void objListIntegCheck(void)
{
SDWORD player;
BASE_OBJECT *psCurr;
for(player = 0; player <MAX_PLAYERS; player += 1)
{
for(psCurr = (BASE_OBJECT*)apsDroidLists[player]; psCurr; psCurr=psCurr->psNext)
{
ASSERT( psCurr->type == OBJ_DROID &&
(SDWORD)psCurr->player == player,
"objListIntegCheck: misplaced object in the droid list for player %d",
player );
}
}
for(player = 0; player <MAX_PLAYERS; player += 1)
{
for(psCurr = (BASE_OBJECT*)apsStructLists[player]; psCurr; psCurr=psCurr->psNext)
{
ASSERT( psCurr->type == OBJ_STRUCTURE &&
(SDWORD)psCurr->player == player,
"objListIntegCheck: misplaced %s(%p) in the structure list for player %d, is owned by %d",
objInfo(psCurr), psCurr, player, (int)psCurr->player);
}
}
for(psCurr = (BASE_OBJECT*)apsFeatureLists[0]; psCurr; psCurr=psCurr->psNext)
{
ASSERT( psCurr->type == OBJ_FEATURE,
"objListIntegCheck: misplaced object in the feature list" );
}
for (psCurr = (BASE_OBJECT*)psDestroyedObj; psCurr; psCurr = psCurr->psNext)
{
ASSERT( psCurr->died > 0, "objListIntegCheck: Object in destroyed list but not dead!" );
}
}
#endif
void objCount(int *droids, int *structures, int *features)
{
*droids = 0;
*structures = 0;
*features = 0;
for (int i = 0; i < MAX_PLAYERS; i++)
{
for (DROID *psDroid = apsDroidLists[i]; psDroid; psDroid = psDroid->psNext)
{
(*droids)++;
if (psDroid->droidType == DROID_TRANSPORTER || psDroid->droidType == DROID_SUPERTRANSPORTER)
{
DROID *psTrans = psDroid->psGroup->psList;