forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTeam.cpp
More file actions
2751 lines (2326 loc) · 78.5 KB
/
Team.cpp
File metadata and controls
2751 lines (2326 loc) · 78.5 KB
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
/*
** Command & Conquer Generals(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program 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 3 of the License, or
** (at your option) any later version.
**
** This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: Team.cpp /////////////////////////////////////////////////////////////////////////////////
// Team interface implementation
// Author: Michael S. Booth, March 2001
///////////////////////////////////////////////////////////////////////////////////////////////////
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "Common/GameState.h"
#include "Common/Team.h"
#include "Common/ThingFactory.h"
#include "Common/PerfTimer.h"
#include "Common/Player.h"
#include "Common/PlayerList.h"
#include "Common/PlayerTemplate.h"
#include "Common/ThingTemplate.h"
#include "Common/WellKnownKeys.h"
#include "Common/Xfer.h"
#include "GameClient/Drawable.h"
#include "GameLogic/SidesList.h"
#include "GameLogic/Object.h"
#include "GameLogic/Module/BodyModule.h"
#include "GameLogic/Module/ContainModule.h"
#include "GameLogic/PolygonTrigger.h"
#include "GameLogic/Module/AIUpdate.h"
#include "GameLogic/PartitionManager.h"
#include "GameLogic/ScriptActions.h"
#include "GameLogic/ScriptEngine.h"
///@todo - do delayed script evaluations for team scripts. jba.
// GLOBALS ////////////////////////////////////////////////////////////////////
TeamFactory *TheTeamFactory = nullptr;
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
TeamRelationMap::TeamRelationMap()
{
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
TeamRelationMap::~TeamRelationMap()
{
// maek sure the data is clear
m_map.clear();
}
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void TeamRelationMap::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------------------------------
/** Xfer method
* Version Info;
* 1: Initial version */
// ------------------------------------------------------------------------------------------------
void TeamRelationMap::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
// team relation count
TeamRelationMapType::iterator teamRelationIt;
UnsignedShort teamRelationCount = m_map.size();
xfer->xferUnsignedShort( &teamRelationCount );
// team relations
TeamID teamID;
Relationship r;
if( xfer->getXferMode() == XFER_SAVE )
{
// go through all team relations
for( teamRelationIt = m_map.begin(); teamRelationIt != m_map.end(); ++teamRelationIt )
{
// write team ID
teamID = (*teamRelationIt).first;
xfer->xferUser( &teamID, sizeof( TeamID ) );
// write relationship
r = (*teamRelationIt).second;
xfer->xferUser( &r, sizeof( Relationship ) );
}
}
else
{
for( UnsignedShort i = 0; i < teamRelationCount; ++i )
{
// read team ID
xfer->xferUser( &teamID, sizeof( TeamID ) );
// read relationship
xfer->xferUser( &r, sizeof( Relationship ) );
// assign relationship
m_map[teamID] = r;
}
}
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void TeamRelationMap::loadPostProcess()
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// STATIC FUNCTIONS ///////////////////////////////////////////////////////////
static Bool locoSetMatches(LocomotorSurfaceTypeMask lstm, UnsignedInt surfaceBitFlags)
{
surfaceBitFlags = (surfaceBitFlags & 0x01) | ((surfaceBitFlags & 0x02) << 2);
return (surfaceBitFlags & lstm) != 0;
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
TeamFactory::TeamFactory()
{
m_uniqueTeamPrototypeID = TEAM_PROTOTYPE_ID_INVALID;
m_uniqueTeamID = TEAM_ID_INVALID;
}
// ------------------------------------------------------------------------
TeamFactory::~TeamFactory()
{
clear();
}
// ------------------------------------------------------------------------
void TeamFactory::init()
{
clear();
}
// ------------------------------------------------------------------------
void TeamFactory::reset()
{
m_uniqueTeamPrototypeID = TEAM_PROTOTYPE_ID_INVALID;
m_uniqueTeamID = TEAM_ID_INVALID;
clear();
}
// ------------------------------------------------------------------------
void TeamFactory::update()
{
}
// ------------------------------------------------------------------------
void TeamFactory::clear()
{
// must remove it from the map before deleting the TeamProto, since
// the TeamProto will try to remove itself from the list when it goes away
TeamPrototypeMap tmp = m_prototypes;
m_prototypes.clear();
for (TeamPrototypeMap::iterator it = tmp.begin(); it != tmp.end(); ++it)
{
deleteInstance(it->second);
}
}
// ------------------------------------------------------------------------
void TeamFactory::initFromSides(SidesList *sides)
{
clear();
// create the teams we need.
for(Int i = 0; i < sides->getNumTeams(); i++)
{
Dict *d = sides->getTeamInfo(i)->getDict();
AsciiString tname = d->getAsciiString(TheKey_teamName);
AsciiString oname = d->getAsciiString(TheKey_teamOwner);
Bool singleton = d->getBool(TheKey_teamIsSingleton);
initTeam(tname, oname, singleton, d);
}
}
// ------------------------------------------------------------------------
void TeamFactory::initTeam(const AsciiString& name, const AsciiString& owner, Bool isSingleton, Dict *d)
{
DEBUG_ASSERTCRASH(findTeamPrototype(name)==nullptr,("team already exists"));
Player *pOwner = ThePlayerList->findPlayerWithNameKey(NAMEKEY(owner));
DEBUG_ASSERTCRASH(pOwner, ("no owner found for team %s (%s)",name.str(),owner.str()));
if (!pOwner)
pOwner = ThePlayerList->getNeutralPlayer();
/*TeamPrototype *tp =*/ newInstance(TeamPrototype)(this, name, pOwner, isSingleton, d, ++m_uniqueTeamPrototypeID);
if (isSingleton) {
// Create the singleton team.
createInactiveTeam(name);
}
}
//=============================================================================
void TeamFactory::addTeamPrototypeToList(TeamPrototype* team)
{
NameKeyType nk = NAMEKEY(team->getName());
TeamPrototypeMap::iterator it = m_prototypes.find(nk);
if (it != m_prototypes.end())
{
DEBUG_ASSERTCRASH((*it).second==team, ("TeamFactory::addTeamPrototypeToList: Team %s already exists... skipping.", team->getName().str()));
return; // already present
}
m_prototypes[nk] = team;
}
//=============================================================================
void TeamFactory::removeTeamPrototypeFromList(TeamPrototype* team)
{
NameKeyType nk = NAMEKEY(team->getName());
TeamPrototypeMap::iterator it = m_prototypes.find(nk);
if (it != m_prototypes.end())
m_prototypes.erase(it);
}
// ------------------------------------------------------------------------
TeamPrototype *TeamFactory::findTeamPrototype(const AsciiString& name)
{
NameKeyType nk = NAMEKEY(name);
TeamPrototypeMap::iterator it = m_prototypes.find(nk);
if (it != m_prototypes.end())
return it->second;
return nullptr;
}
// ------------------------------------------------------------------------
TeamPrototype *TeamFactory::findTeamPrototypeByID( TeamPrototypeID id )
{
TeamPrototypeMap::iterator it;
TeamPrototype *prototype = nullptr;
for( it = m_prototypes.begin(); it != m_prototypes.end(); ++it )
{
prototype = it->second;
if( prototype->getID() == id )
return prototype;
}
// not found
return nullptr;
}
// ------------------------------------------------------------------------
Team *TeamFactory::findTeamByID( TeamID teamID )
{
// simple case
if( teamID == TEAM_ID_INVALID )
return nullptr;
// search all prototypes for the matching team ID
TeamPrototype *tp;
TeamPrototypeMap::iterator it;
Team *team;
for( it = m_prototypes.begin(); it != m_prototypes.end(); ++it )
{
tp = (*it).second;
team = tp->findTeamByID( teamID );
if( team )
return team;
}
return nullptr;
}
// ------------------------------------------------------------------------
/** Creates an inactive team, suitable for adding members to as they are built.
Call team->setActive() when all members are added. */
Team *TeamFactory::createInactiveTeam(const AsciiString& name)
{
TeamPrototype *tp = findTeamPrototype(name);
if (!tp) {
DEBUG_CRASH(( "Team prototype '%s' does not exist", name.str() ));
return nullptr;
}
Team *t = nullptr;
if (tp->getIsSingleton())
{
t = tp->getFirstItemIn_TeamInstanceList();
if (t) {
if (tp->getTemplateInfo()->m_executeActions) {
const Script *script = TheScriptEngine->findScriptByName(tp->getTemplateInfo()->m_productionCondition);
if (script) {
TheScriptEngine->friend_executeAction(script->getAction());
}
}
return t;
}
}
t = newInstance(Team)(tp, ++m_uniqueTeamID );
if (tp->getTemplateInfo()->m_executeActions) {
const Script *script = TheScriptEngine->findScriptByName(tp->getTemplateInfo()->m_productionCondition);
if (script) {
TheScriptEngine->friend_executeAction(script->getAction());
}
}
return t;
}
// ------------------------------------------------------------------------
Team *TeamFactory::createTeam(const AsciiString& name)
{
Team *t = NULL;
t = createInactiveTeam(name);
if (t == nullptr) {
return nullptr;
}
t->setActive();
return t;
}
// ------------------------------------------------------------------------
Team *TeamFactory::createTeamOnPrototype( TeamPrototype *prototype )
{
if( prototype == nullptr )
throw ERROR_BAD_ARG;
Team *t = nullptr;
if( prototype->getIsSingleton() )
{
t = prototype->getFirstItemIn_TeamInstanceList();
if( t )
return t;
}
t = newInstance(Team)( prototype, ++m_uniqueTeamID );
t->setActive();
return t;
}
// ------------------------------------------------------------------------
Team* TeamFactory::findTeam(const AsciiString& name)
{
TeamPrototype *tp = findTeamPrototype(name);
if (tp)
{
Team *t = tp->getFirstItemIn_TeamInstanceList();
if (t == nullptr && !tp->getIsSingleton())
{
t = createInactiveTeam(name);
}
return t;
}
return nullptr;
}
// ------------------------------------------------------------------------
void TeamFactory::teamAboutToBeDeleted(Team* team)
{
for (TeamPrototypeMap::iterator it = m_prototypes.begin(); it != m_prototypes.end(); ++it)
{
it->second->teamAboutToBeDeleted(team);
}
if (ThePlayerList)
ThePlayerList->teamAboutToBeDeleted(team);
}
// ------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------
void TeamFactory::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------
/** Xfer method
* Version Info:
* 1: Initial version */
// ------------------------------------------------------------------------
void TeamFactory::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
// unique team ID counter
xfer->xferUser( &m_uniqueTeamID, sizeof( TeamID ) );
// how many team prototypes of data do we have to write
UnsignedShort prototypeCount = m_prototypes.size();
xfer->xferUnsignedShort( &prototypeCount );
//
// prototypes cannot change in number during run time so the count should be the
// same as that already loaded into us from a map load
//
if( prototypeCount != m_prototypes.size() )
{
DEBUG_CRASH(( "TeamFactory::xfer - Prototype count mismatch '%d should be '%d'",
prototypeCount, m_prototypes.size() ));
throw SC_INVALID_DATA;
}
// xfer each of the prototype information
TeamPrototypeMap::iterator it;
TeamPrototypeID teamPrototypeID;
TeamPrototype *teamPrototype;
AsciiString prototypeName;
if( xfer->getXferMode() == XFER_SAVE )
{
// iterate each prototype and xfer if it needs to be in the save file
for( it = m_prototypes.begin(); it != m_prototypes.end(); ++it )
{
// get prototype
teamPrototype = it->second;
// xfer prototype id
teamPrototypeID = teamPrototype->getID();
xfer->xferUser( &teamPrototypeID, sizeof( TeamPrototypeID ) );
// xfer prototype data
xfer->xferSnapshot( teamPrototype );
}
}
else
{
// read all the team prototype info
for( UnsignedShort i = 0; i < prototypeCount; ++i )
{
// read prototype ID
xfer->xferUser( &teamPrototypeID, sizeof( TeamPrototypeID ) );
// find the prototype
teamPrototype = findTeamPrototypeByID( teamPrototypeID );
// sanity
if( teamPrototype == nullptr )
{
DEBUG_CRASH(( "TeamFactory::xfer - Unable to find team prototype by id" ));
throw SC_INVALID_DATA;
}
// xfer prototype data
xfer->xferSnapshot( teamPrototype );
}
}
/*
// SAVE_LOAD_DEBUG
if( xfer->getXferMode() == XFER_SAVE )
{
FILE *fp = fopen( "TeamCheckSave.txt", "w+t" );
if( fp == nullptr )
return;
Object *obj;
TeamPrototypeMap::iterator prototypeIt;
TeamPrototype *prototype;
Team *team;
for( prototypeIt = m_prototypes.begin(); prototypeIt != m_prototypes.end(); ++prototypeIt )
{
prototype = prototypeIt->second;
fprintf( fp, "Prototype '%s' for player index '%d'\n", prototype->getName().str(), prototype->getControllingPlayer()->getPlayerIndex() );
for( DLINK_ITERATOR<Team> teamIt = prototype->iterate_TeamInstanceList(); !teamIt.done(); teamIt.advance() )
{
team = teamIt.cur();
fprintf( fp, " Team Instance '%s', id is '%d'\n", team->getName().str(), team->getID() );
for( DLINK_ITERATOR<Object> objIt = team->iterate_TeamMemberList(); !objIt.done(); objIt.advance() )
{
obj = objIt.cur();
fprintf( fp, " Member '%s', id '%d'\n", obj->getTemplate()->getName().str(), obj->getID() );
}
}
}
fclose( fp );
}
*/
}
// ------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------
void TeamFactory::loadPostProcess()
{
// set the next unique team and prototype ID to just over the highest one in use
m_uniqueTeamID = 0;
m_uniqueTeamPrototypeID = 0;
TeamPrototypeMap::iterator prototypeIt;
TeamPrototype *prototype;
Team *team;
for( prototypeIt = m_prototypes.begin(); prototypeIt != m_prototypes.end(); ++prototypeIt )
{
// get prototype
prototype = prototypeIt->second;
// do protype ID check
if( prototype->getID() >= m_uniqueTeamPrototypeID )
m_uniqueTeamPrototypeID = prototype->getID() + 1;
// iterate team instances on each prototype and do the team ID check
for( DLINK_ITERATOR<Team> iter = prototype->iterate_TeamInstanceList(); !iter.done(); iter.advance() )
{
team = iter.cur();
if( team->getID() >= m_uniqueTeamID )
m_uniqueTeamID = team->getID() + 1;
}
}
/*
// SAVE_LOAD_DEBUG
FILE *fp = fopen( "TeamCheckLoad.txt", "w+t" );
if( fp == nullptr )
return;
Object *obj;
for( prototypeIt = m_prototypes.begin(); prototypeIt != m_prototypes.end(); ++prototypeIt )
{
prototype = prototypeIt->second;
fprintf( fp, "Prototype '%s' for player index '%d'\n", prototype->getName().str(), prototype->getControllingPlayer()->getPlayerIndex() );
for( DLINK_ITERATOR<Team> teamIt = prototype->iterate_TeamInstanceList(); !teamIt.done(); teamIt.advance() )
{
team = teamIt.cur();
fprintf( fp, " Team Instance '%s', id is '%d'\n", team->getName().str(), team->getID() );
team->reverse_TeamMemberList();
for( DLINK_ITERATOR<Object> objIt = team->iterate_TeamMemberList(); !objIt.done(); objIt.advance() )
{
obj = objIt.cur();
fprintf( fp, " Member '%s', id '%d'\n", obj->getTemplate()->getName().str(), obj->getID() );
}
}
}
fclose( fp );
*/
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
TeamTemplateInfo::TeamTemplateInfo(Dict *d) :
m_numUnitsInfo(0)
{
Bool exists;
Int min, max;
AsciiString templateName;
min = d->getInt(TheKey_teamUnitMinCount1, &exists);
max = d->getInt(TheKey_teamUnitMaxCount1, &exists);
templateName = d->getAsciiString(TheKey_teamUnitType1, &exists);
if (max>0 && exists) {
m_unitsInfo[m_numUnitsInfo].minUnits = min;
m_unitsInfo[m_numUnitsInfo].maxUnits = max;
m_unitsInfo[m_numUnitsInfo].unitThingName = templateName;
m_numUnitsInfo++;
}
min = d->getInt(TheKey_teamUnitMinCount2, &exists);
max = d->getInt(TheKey_teamUnitMaxCount2, &exists);
templateName = d->getAsciiString(TheKey_teamUnitType2, &exists);
if (max>0 && exists) {
m_unitsInfo[m_numUnitsInfo].minUnits = min;
m_unitsInfo[m_numUnitsInfo].maxUnits = max;
m_unitsInfo[m_numUnitsInfo].unitThingName = templateName;
m_numUnitsInfo++;
}
min = d->getInt(TheKey_teamUnitMinCount3, &exists);
max = d->getInt(TheKey_teamUnitMaxCount3, &exists);
templateName = d->getAsciiString(TheKey_teamUnitType3, &exists);
if (max>0 && exists) {
m_unitsInfo[m_numUnitsInfo].minUnits = min;
m_unitsInfo[m_numUnitsInfo].maxUnits = max;
m_unitsInfo[m_numUnitsInfo].unitThingName = templateName;
m_numUnitsInfo++;
}
min = d->getInt(TheKey_teamUnitMinCount4, &exists);
max = d->getInt(TheKey_teamUnitMaxCount4, &exists);
templateName = d->getAsciiString(TheKey_teamUnitType4, &exists);
if (max>0 && exists) {
m_unitsInfo[m_numUnitsInfo].minUnits = min;
m_unitsInfo[m_numUnitsInfo].maxUnits = max;
m_unitsInfo[m_numUnitsInfo].unitThingName = templateName;
m_numUnitsInfo++;
}
min = d->getInt(TheKey_teamUnitMinCount5, &exists);
max = d->getInt(TheKey_teamUnitMaxCount5, &exists);
templateName = d->getAsciiString(TheKey_teamUnitType5, &exists);
if (max>0 && exists) {
m_unitsInfo[m_numUnitsInfo].minUnits = min;
m_unitsInfo[m_numUnitsInfo].maxUnits = max;
m_unitsInfo[m_numUnitsInfo].unitThingName = templateName;
m_numUnitsInfo++;
}
min = d->getInt(TheKey_teamUnitMinCount6, &exists);
max = d->getInt(TheKey_teamUnitMaxCount6, &exists);
templateName = d->getAsciiString(TheKey_teamUnitType6, &exists);
if (max>0 && exists) {
m_unitsInfo[m_numUnitsInfo].minUnits = min;
m_unitsInfo[m_numUnitsInfo].maxUnits = max;
m_unitsInfo[m_numUnitsInfo].unitThingName = templateName;
m_numUnitsInfo++;
}
min = d->getInt(TheKey_teamUnitMinCount7, &exists);
max = d->getInt(TheKey_teamUnitMaxCount7, &exists);
templateName = d->getAsciiString(TheKey_teamUnitType7, &exists);
if (max>0 && exists) {
m_unitsInfo[m_numUnitsInfo].minUnits = min;
m_unitsInfo[m_numUnitsInfo].maxUnits = max;
m_unitsInfo[m_numUnitsInfo].unitThingName = templateName;
m_numUnitsInfo++;
}
AsciiString waypoint = d->getAsciiString(TheKey_teamHome, &exists);
m_homeLocation.x = m_homeLocation.y = 0;
m_homeLocation.z = 0;
m_hasHomeLocation = false;
if (exists) {
for (Waypoint *way = TheTerrainLogic->getFirstWaypoint(); way; way = way->getNext()) {
if (way->getName() == waypoint) {
m_homeLocation = *way->getLocation();
m_hasHomeLocation = true;
}
}
}
m_scriptOnCreate = d->getAsciiString(TheKey_teamOnCreateScript, &exists);
m_isAIRecruitable = d->getBool(TheKey_teamIsAIRecruitable, &exists);
if (!exists) {
m_isAIRecruitable = false;
}
m_isBaseDefense = d->getBool(TheKey_teamIsBaseDefense, &exists);
m_isPerimeterDefense = d->getBool(TheKey_teamIsPerimeterDefense, &exists);
m_automaticallyReinforce = d->getBool(TheKey_teamAutoReinforce, &exists);
Int interact = d->getInt(TheKey_teamAggressiveness, &exists);
m_initialTeamAttitude = ATTITUDE_NORMAL;
if (exists) {
m_initialTeamAttitude = (AttitudeType) interact;
}
m_transportsReturn = d->getBool(TheKey_teamTransportsReturn, &exists);
m_avoidThreats = d->getBool(TheKey_teamAvoidThreats, &exists);
m_attackCommonTarget = d->getBool(TheKey_teamAttackCommonTarget, &exists);
m_maxInstances = d->getInt(TheKey_teamMaxInstances, &exists);
m_scriptOnIdle = d->getAsciiString(TheKey_teamOnIdleScript, &exists);
m_initialIdleFrames = d->getInt(TheKey_teamInitialIdleFrames, &exists);
m_scriptOnEnemySighted = d->getAsciiString(TheKey_teamEnemySightedScript, &exists);
m_scriptOnAllClear = d->getAsciiString(TheKey_teamAllClearScript, &exists);
m_scriptOnDestroyed = d->getAsciiString(TheKey_teamOnDestroyedScript, &exists);
m_destroyedThreshold = d->getReal(TheKey_teamDestroyedThreshold, &exists);
m_scriptOnUnitDestroyed = d->getAsciiString(TheKey_teamOnUnitDestroyedScript, &exists);
m_productionPriority = d->getInt(TheKey_teamProductionPriority, &exists);
m_productionPrioritySuccessIncrease = d->getInt(TheKey_teamProductionPrioritySuccessIncrease, &exists);
m_productionPriorityFailureDecrease = d->getInt(TheKey_teamProductionPriorityFailureDecrease, &exists);
// Production scripts stuff
m_productionCondition = d->getAsciiString(TheKey_teamProductionCondition, &exists);
m_executeActions = d->getBool(TheKey_teamExecutesActionsOnCreate, &exists);
// Which scripts to attempt during run?
for (int i = 0; i < MAX_GENERIC_SCRIPTS; ++i) {
AsciiString keyName;
// Store the result of keyToName in a local variable to avoid dangling pointer
AsciiString hookName = TheNameKeyGenerator->keyToName(TheKey_teamGenericScriptHook);
keyName.format("%s%d", hookName.str(), i);
m_teamGenericScripts[i] = d->getAsciiString(NAMEKEY(keyName), &exists);
if (!exists) {
m_teamGenericScripts[i].clear();
}
}
// reinforcement team info.
m_transportUnitType = d->getAsciiString(TheKey_teamTransport, &exists);
m_transportsExit = d->getBool(TheKey_teamTransportsExit, &exists);
m_teamStartsFull = d->getBool(TheKey_teamStartsFull, &exists);
m_startReinforceWaypoint = d->getAsciiString(TheKey_teamReinforcementOrigin, &exists);
m_veterancy = (VeterancyLevel)d->getInt(TheKey_teamVeterancy, &exists);
}
// ------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------
void TeamTemplateInfo::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------
/** Xfer method
* Version Info:
* 1: Initial version */
// ------------------------------------------------------------------------
void TeamTemplateInfo::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
// xfer the production priority
xfer->xferInt( &m_productionPriority );
}
// ------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------
void TeamTemplateInfo::loadPostProcess()
{
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
TeamPrototype::TeamPrototype( TeamFactory *tf,
const AsciiString &name,
Player *ownerPlayer,
Bool isSingleton,
Dict *d,
TeamPrototypeID id ) :
m_id(id),
m_factory(tf),
m_name(name),
m_owningPlayer(ownerPlayer),
m_flags(isSingleton ? TeamPrototype::TEAM_SINGLETON : 0),
m_teamTemplate(d),
m_productionConditionAlwaysFalse(false),
m_productionConditionScript(nullptr)
{
DEBUG_ASSERTCRASH(!(m_owningPlayer == nullptr), ("bad args to TeamPrototype ctor"));
if (m_factory)
m_factory->addTeamPrototypeToList(this);
if (m_owningPlayer)
m_owningPlayer->addTeamToList(this);
m_retrievedGenericScripts = false;
for (Int i = 0; i < MAX_GENERIC_SCRIPTS; ++i) {
m_genericScriptsToRun[i] = nullptr;
}
}
// ------------------------------------------------------------------------
static void deleteTeamCallback(Team* o)
{
if (o)
{
TheTeamFactory->teamAboutToBeDeleted(o);
deleteInstance(o);
}
}
TeamPrototype::~TeamPrototype()
{
removeAll_TeamInstanceList(deleteTeamCallback);
if (m_owningPlayer)
m_owningPlayer->removeTeamFromList(this);
if (m_factory)
m_factory->removeTeamPrototypeFromList(this);
deleteInstance(m_productionConditionScript);
m_productionConditionScript = nullptr;
for (Int i = 0; i < MAX_GENERIC_SCRIPTS; ++i)
{
deleteInstance(m_genericScriptsToRun[i]);
m_genericScriptsToRun[i] = nullptr;
}
}
// ------------------------------------------------------------------------
Player *TeamPrototype::getControllingPlayer() const
{
return m_owningPlayer;
}
// ------------------------------------------------------------------------
Team *TeamPrototype::findTeamByID( TeamID teamID )
{
for (DLINK_ITERATOR<Team> iter = iterate_TeamInstanceList(); !iter.done(); iter.advance())
{
if( iter.cur()->getID() == teamID )
return iter.cur();
}
return nullptr;
}
// ------------------------------------------------------------------------
void TeamPrototype::setControllingPlayer(Player *newController)
{
DEBUG_ASSERTCRASH(newController, ("Attempted to set null player as team-owner, illegal."));
if (!newController) {
return;
}
if (m_owningPlayer)
m_owningPlayer->removeTeamFromList(this);
m_owningPlayer = newController;
// impossible to get here with a nullptr pointer.
m_owningPlayer->addTeamToList(this);
}
// ------------------------------------------------------------------------
void TeamPrototype::countObjectsByThingTemplate(Int numTmplates, const ThingTemplate* const* things, Bool ignoreDead, Int *counts, Bool ignoreUnderConstruction) const
{
for (DLINK_ITERATOR<Team> iter = iterate_TeamInstanceList(); !iter.done(); iter.advance())
{
iter.cur()->countObjectsByThingTemplate(numTmplates, things, ignoreDead, counts, ignoreUnderConstruction);
}
}
// ------------------------------------------------------------------------
void TeamPrototype::teamAboutToBeDeleted(Team* team)
{
for (DLINK_ITERATOR<Team> iter = iterate_TeamInstanceList(); !iter.done(); iter.advance())
{
// iter.cur()->removeOverrideTeamRelationship(team);
iter.cur()->removeOverrideTeamRelationship(team ? team->getID() : TEAM_ID_INVALID );
}
}
// ------------------------------------------------------------------------
Script *TeamPrototype::getGenericScript(Int scriptToRetrieve)
{
if (!m_retrievedGenericScripts) {
m_retrievedGenericScripts = TRUE; // set this to true so we won't do the lookup again.
// Go get them from the script engine, and duplicate each one.
for (Int i = 0; i < MAX_GENERIC_SCRIPTS; ++i) {
const Script *tmpScript = nullptr;
Script *scriptToSave = nullptr;
if (!m_teamTemplate.m_teamGenericScripts[i].isEmpty()) {
tmpScript = TheScriptEngine->findScriptByName(m_teamTemplate.m_teamGenericScripts[i]);
if (tmpScript) {
scriptToSave = tmpScript->duplicate();
} else {
DEBUG_CRASH(("We attempted to find a generic script, but couldn't. ('%s')", m_teamTemplate.m_teamGenericScripts[i].str()));
}
}
// I now own this one. I'm responsible for cleaning it up on destruction.
m_genericScriptsToRun[i] = scriptToSave;
}
}
return m_genericScriptsToRun[scriptToRetrieve];
}
// ------------------------------------------------------------------------
// Make a team more likely to be selected by the ai for building due to success.
void TeamPrototype::increaseAIPriorityForSuccess() const
{
m_teamTemplate.m_productionPriority += m_teamTemplate.m_productionPrioritySuccessIncrease;
}
// ------------------------------------------------------------------------
// Make a team more likely to be selected by the ai for building due to success.
void TeamPrototype::decreaseAIPriorityForFailure() const
{
m_teamTemplate.m_productionPriority -= m_teamTemplate.m_productionPriorityFailureDecrease;
}
// ------------------------------------------------------------------------
Int TeamPrototype::countBuildings()
{
int retVal = 0;
for (DLINK_ITERATOR<Team> iter = iterate_TeamInstanceList(); !iter.done(); iter.advance()) {
retVal += iter.cur()->countBuildings();
}
return retVal;
}
// ------------------------------------------------------------------------
Int TeamPrototype::countObjects(KindOfMaskType setMask, KindOfMaskType clearMask)
{
int retVal = 0;
for (DLINK_ITERATOR<Team> iter = iterate_TeamInstanceList(); !iter.done(); iter.advance()) {
retVal += iter.cur()->countObjects(setMask, clearMask);
}
return retVal;
}
// ------------------------------------------------------------------------
void TeamPrototype::healAllObjects()
{
for (DLINK_ITERATOR<Team> iter = iterate_TeamInstanceList(); !iter.done(); iter.advance())
{
iter.cur()->healAllObjects();
}
}
// ------------------------------------------------------------------------
void TeamPrototype::iterateObjects( ObjectIterateFunc func, void *userData )
{
for (DLINK_ITERATOR<Team> iter = iterate_TeamInstanceList(); !iter.done(); iter.advance())
{
iter.cur()->iterateObjects( func, userData );
}
}
// ------------------------------------------------------------------------
/**
* Count the number of teams that have been instanced by this prototype
*/
Int TeamPrototype::countTeamInstances()