forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathProductionUpdate.cpp
More file actions
1412 lines (1110 loc) · 45.1 KB
/
ProductionUpdate.cpp
File metadata and controls
1412 lines (1110 loc) · 45.1 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: ProductionUpdate.cpp /////////////////////////////////////////////////////////////////////
// Author: Colin Day, March 2002
// Desc: This module allows things to be "constructed" from a building
///////////////////////////////////////////////////////////////////////////////////////////////////
// USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "Common/BitFlagsIO.h"
#include "Common/BuildAssistant.h"
#include "Common/CRCDebug.h"
#include "Common/GameState.h"
#include "Common/Player.h"
#include "Common/Radar.h"
#include "Common/ThingFactory.h"
#include "Common/ThingTemplate.h"
#include "Common/Upgrade.h"
#include "Common/Xfer.h"
#include "Common/XferCRC.h"
#include "GameClient/ControlBar.h"
#include "GameClient/Drawable.h"
#include "GameClient/Eva.h"
#include "GameClient/GameText.h"
#include "GameClient/InGameUI.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/Module/CreateModule.h"
#include "GameLogic/Module/ParkingPlaceBehavior.h"
#include "GameLogic/Module/ProductionUpdate.h"
#include "GameLogic/Object.h"
#include "GameLogic/ScriptEngine.h"
// PUBLIC /////////////////////////////////////////////////////////////////////////////////////////
static const ModelConditionFlagType theOpeningFlags[DOOR_COUNT_MAX] =
{
MODELCONDITION_DOOR_1_OPENING,
MODELCONDITION_DOOR_2_OPENING,
MODELCONDITION_DOOR_3_OPENING,
MODELCONDITION_DOOR_4_OPENING
};
static const ModelConditionFlagType theClosingFlags[DOOR_COUNT_MAX] =
{
MODELCONDITION_DOOR_1_CLOSING,
MODELCONDITION_DOOR_2_CLOSING,
MODELCONDITION_DOOR_3_CLOSING,
MODELCONDITION_DOOR_4_CLOSING
};
static const ModelConditionFlagType theWaitingOpenFlags[DOOR_COUNT_MAX] =
{
MODELCONDITION_DOOR_1_WAITING_OPEN,
MODELCONDITION_DOOR_2_WAITING_OPEN,
MODELCONDITION_DOOR_3_WAITING_OPEN,
MODELCONDITION_DOOR_4_WAITING_OPEN
};
static const ModelConditionFlagType theWaitingToCloseFlags[DOOR_COUNT_MAX] =
{
MODELCONDITION_DOOR_1_WAITING_TO_CLOSE,
MODELCONDITION_DOOR_2_WAITING_TO_CLOSE,
MODELCONDITION_DOOR_3_WAITING_TO_CLOSE,
MODELCONDITION_DOOR_4_WAITING_TO_CLOSE
};
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ProductionUpdateModuleData::ProductionUpdateModuleData()
{
// someday, might need separate times for each door. but not yet.
m_numDoorAnimations = 0;
m_doorOpeningTime = 0;
m_doorWaitOpenTime = 0;
m_doorClosingTime = 0;
m_constructionCompleteDuration = 0;
m_quantityModifiers.clear();
m_maxQueueEntries = 9;
m_disabledTypesToProcess = MAKE_DISABLED_MASK(DISABLED_HELD);
}
//-------------------------------------------------------------------------------------------------
/*static*/ void ProductionUpdateModuleData::parseAppendQuantityModifier( INI* ini, void *instance, void *store, const void* /*userData*/ )
{
ProductionUpdateModuleData* data = (ProductionUpdateModuleData*)instance;
const char* name = ini->getNextToken();
const char* countStr = ini->getNextTokenOrNull();
Int count = countStr ? INI::scanInt(countStr) : 1;
QuantityModifier qm;
qm.m_quantity = count;
qm.m_templateName.set( name );
data->m_quantityModifiers.push_back( qm );
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
/*static*/ void ProductionUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
{
UpdateModuleData::buildFieldParse( p );
static const FieldParse dataFieldParse[] =
{
{ "MaxQueueEntries", INI::parseInt, nullptr, offsetof( ProductionUpdateModuleData, m_maxQueueEntries ) },
{ "NumDoorAnimations", INI::parseInt, nullptr, offsetof( ProductionUpdateModuleData, m_numDoorAnimations ) },
{ "DoorOpeningTime", INI::parseDurationUnsignedInt, nullptr, offsetof( ProductionUpdateModuleData, m_doorOpeningTime ) },
{ "DoorWaitOpenTime", INI::parseDurationUnsignedInt, nullptr, offsetof( ProductionUpdateModuleData, m_doorWaitOpenTime ) },
{ "DoorCloseTime", INI::parseDurationUnsignedInt, nullptr, offsetof( ProductionUpdateModuleData, m_doorClosingTime ) },
{ "ConstructionCompleteDuration", INI::parseDurationUnsignedInt, nullptr, offsetof( ProductionUpdateModuleData, m_constructionCompleteDuration ) },
{ "QuantityModifier", parseAppendQuantityModifier, nullptr, offsetof( ProductionUpdateModuleData, m_quantityModifiers ) },
{ "DisabledTypesToProcess", DisabledMaskType::parseFromINI, nullptr, offsetof( ProductionUpdateModuleData, m_disabledTypesToProcess ) },
{ nullptr, nullptr, nullptr, 0 }
};
p.add(dataFieldParse);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ProductionEntry::ProductionEntry()
{
m_type = PRODUCTION_INVALID;
m_objectToProduce = nullptr;
m_upgradeToResearch = nullptr;
m_productionID = (ProductionID)1;
m_percentComplete = 0.0f;
m_framesUnderConstruction = 0;
m_next = nullptr;
m_prev = nullptr;
m_productionQuantityProduced = 0;
m_productionQuantityTotal = 0;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ProductionEntry::~ProductionEntry()
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ProductionUpdate::ProductionUpdate( Thing *thing, const ModuleData* moduleData ) :
UpdateModule( thing, moduleData )
{
m_productionQueue = nullptr;
m_productionQueueTail = nullptr;
m_productionCount = 0;
m_uniqueID = (ProductionID)1;
for (Int i = 0; i < DOOR_COUNT_MAX; ++i)
{
m_doors[i].m_doorOpenedFrame = 0;
m_doors[i].m_doorWaitOpenFrame = 0;
m_doors[i].m_doorClosedFrame = 0;
m_doors[i].m_holdOpen = false;
}
m_constructionCompleteFrame = 0;
m_clearFlags.clear();
m_setFlags.clear();
m_flagsDirty = FALSE;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ProductionUpdate::~ProductionUpdate()
{
// destroy any queued productions
ProductionEntry *production;
while( m_productionQueue )
{
production = m_productionQueue;
removeFromProductionQueue( production );
// TheSuperHackers @fix Mauller 13/04/2025 Delete instance of production item
deleteInstance(production);
}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
CanMakeType ProductionUpdate::canQueueUpgrade( const UpgradeTemplate *upgrade ) const
{
if (m_productionCount >= getProductionUpdateModuleData()->m_maxQueueEntries)
return CANMAKE_QUEUE_FULL;
return CANMAKE_OK;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
CanMakeType ProductionUpdate::canQueueCreateUnit( const ThingTemplate *unitType ) const
{
/// @todo srj -- this is horrible, but the "right" way to do it is to move
// ProductionUpdate to be part of ParkingPlaceBehavior, which I don't currently
// have time for...
ParkingPlaceBehaviorInterface* pp = nullptr;
for (BehaviorModule** i = getObject()->getBehaviorModules(); *i; ++i)
{
if ((pp = (*i)->getParkingPlaceBehaviorInterface()) != nullptr)
{
if (pp->shouldReserveDoorWhenQueued(unitType) && !pp->hasAvailableSpaceFor(unitType))
return CANMAKE_PARKING_PLACES_FULL;
}
}
if (m_productionCount >= getProductionUpdateModuleData()->m_maxQueueEntries)
return CANMAKE_QUEUE_FULL;
return CANMAKE_OK;
}
//-------------------------------------------------------------------------------------------------
/** Queue an upgrade to be produced */
//-------------------------------------------------------------------------------------------------
Bool ProductionUpdate::queueUpgrade( const UpgradeTemplate *upgrade )
{
// sanity
if( upgrade == nullptr )
return FALSE;
// get the player
Player *player = getObject()->getControllingPlayer();
// sanity check to make sure we can build this upgrade
if( upgrade->getUpgradeType() == UPGRADE_TYPE_PLAYER &&
TheUpgradeCenter->canAffordUpgrade( player, upgrade ) == FALSE )
return FALSE;
else if( upgrade->getUpgradeType() == UPGRADE_TYPE_OBJECT &&
(getObject()->hasUpgrade( upgrade ) == TRUE ||
getObject()->affectedByUpgrade( upgrade ) == FALSE) )
return FALSE;
// you cannot queue the production of an upgrade twice in this queue
if( isUpgradeInQueue( upgrade ) == TRUE )
return FALSE;
// STOP cheaters by making sure they can actually build this
if( !getObject()->canProduceUpgrade(upgrade) )
return FALSE;
//
// you cannot queue a player upgrade production if you are producing one already somewhere else
// (or that somewhere else could even possibly be here)
//
if( upgrade->getUpgradeType() == UPGRADE_TYPE_PLAYER &&
(player->hasUpgradeComplete( upgrade ) || player->hasUpgradeInProduction( upgrade )) )
return FALSE;
if (m_productionCount >= getProductionUpdateModuleData()->m_maxQueueEntries)
{
DEBUG_CRASH(("Production Queue is full... how did we get here?"));
return FALSE;
}
// take the cost for the build away from the player
Money *money = player->getMoney();
money->withdraw( upgrade->calcCostToBuild( player ) );
// allocate a new production entry
ProductionEntry *production = newInstance(ProductionEntry);
// assign production entry data
production->m_type = PRODUCTION_UPGRADE;
production->m_upgradeToResearch = upgrade;
production->m_productionID = PRODUCTIONID_INVALID; // not needed for upgrades, you can only have one of
// this type in the queue
// tie to the end of the production queue
addToProductionQueue( production );
// add this upgrade as in progress in the player
player->addUpgrade( upgrade, UPGRADE_STATUS_IN_PRODUCTION );
return TRUE; // queued
}
//-------------------------------------------------------------------------------------------------
/** Cancel an upgrade being produced here */
//-------------------------------------------------------------------------------------------------
void ProductionUpdate::cancelUpgrade( const UpgradeTemplate *upgrade )
{
// sanity
if( upgrade == nullptr )
return;
// get the player
Player *player = getObject()->getControllingPlayer();
// sanity, you can't cancel it if the player isn't actually building one
if( upgrade->getUpgradeType() == UPGRADE_TYPE_PLAYER && player->hasUpgradeInProduction( upgrade ) == FALSE )
return;
//
// find the production entry for this upgrade in the queue here, there can only be one
// of this type in the queue
//
ProductionEntry *production;
for( production = m_productionQueue; production; production = production->m_next )
{
if( production->m_type == PRODUCTION_UPGRADE &&
production->m_upgradeToResearch == upgrade )
break;
}
// sanity, entry not found
if( production == nullptr )
return;
// refund money back to the player
Money *money = player->getMoney();
money->deposit( production->m_upgradeToResearch->calcCostToBuild( player ), TRUE, FALSE );
// remove this production from the queue
removeFromProductionQueue( production );
// delete production instance
deleteInstance(production);
//
// remove the IN_PRODUCTION status of this upgrade from the player, object upgrades don't
// have any other IN_PRODUCTION status other than their existence in the build queue
//
if( upgrade->getUpgradeType() == UPGRADE_TYPE_PLAYER )
player->removeUpgrade( upgrade );
}
//-------------------------------------------------------------------------------------------------
/** Queue the production of a unit. Returns TRUE if unit was added to queue, FALSE if it
* was not */
//-------------------------------------------------------------------------------------------------
Bool ProductionUpdate::queueCreateUnit( const ThingTemplate *unitType, ProductionID productionID )
{
const ProductionUpdateModuleData *data = getProductionUpdateModuleData();
// if we can't create the unit do nothing
if( TheBuildAssistant->canMakeUnit( getObject(), unitType ) != CANMAKE_OK )
return FALSE;
ExitDoorType exitDoor = DOOR_NONE_AVAILABLE;
/// @todo srj -- this is horrible, but the "right" way to do it is to move
// ProductionUpdate to be part of ParkingPlaceBehavior, which I don't currently
// have time for...
ParkingPlaceBehaviorInterface* pp = nullptr;
for (BehaviorModule** i = getObject()->getBehaviorModules(); *i; ++i)
{
if ((pp = (*i)->getParkingPlaceBehaviorInterface()) != nullptr)
{
if (pp->shouldReserveDoorWhenQueued(unitType))
{
ExitInterface* exitInterface = getObject()->getObjectExitInterface();
if (exitInterface)
{
exitDoor = exitInterface->reserveDoorForExit(unitType, nullptr);
}
if (exitDoor == DOOR_NONE_AVAILABLE)
{
return FALSE;
}
break;
}
}
}
if (m_productionCount >= getProductionUpdateModuleData()->m_maxQueueEntries)
{
DEBUG_CRASH(("Production Queue is full... how did we get here?"));
return FALSE;
}
// take the cost for the build away from the player
Player *player = getObject()->getControllingPlayer();
Money *money = player->getMoney();
money->withdraw( unitType->calcCostToBuild( player ) );
// allocate a new production entry
ProductionEntry *production = newInstance(ProductionEntry);
// Check to see if there is a quantity modifier entry. What this means is if we are building a particular unit
// and it has a modifier, we build that number of objects instead of just one. A good example is the Chinese
// barracks building redguards -- they are built 4 at a time, but the user or script only pays for one and
// builds four for that price!
production->m_productionQuantityTotal = 1;
production->m_productionQuantityProduced = 0;
for( std::vector<QuantityModifier>::const_iterator it = data->m_quantityModifiers.begin(); it != data->m_quantityModifiers.end(); ++it )
{
const ThingTemplate* productionTemplate = TheThingFactory->findTemplate( it->m_templateName );
if( productionTemplate && productionTemplate->isEquivalentTo( unitType ) )
{
production->m_productionQuantityTotal = it->m_quantity;
break;
}
}
// assign production entry data
production->m_type = PRODUCTION_UNIT;
production->m_objectToProduce = unitType;
production->m_productionID = productionID;
production->m_exitDoor = exitDoor;
// tie to the end of the production queue
addToProductionQueue( production );
return TRUE; // unit queued
}
//-------------------------------------------------------------------------------------------------
/** Cancel the construction of the unit with the matching production ID */
//-------------------------------------------------------------------------------------------------
void ProductionUpdate::cancelUnitCreate( ProductionID productionID, Bool forceCancel )
{
// search for the production entry in our queue
ProductionEntry *production;
for( production = m_productionQueue; production; production = production->m_next )
{
// are we at the one we want get rid of it
if( production->m_productionID == productionID )
{
Player *player = getObject()->getControllingPlayer();
#if !RETAIL_COMPATIBLE_CRC
// TheSuperHackers @bugfix arcticdolphin 08/03/2026 Prevent cancel once units are produced to avoid free unit exploit
if( !forceCancel && production->getProductionQuantityRemaining() < production->getProductionQuantity() )
{
return;
}
#endif
// give the player the cost of the object back
Money *money = player->getMoney();
money->deposit( production->m_objectToProduce->calcCostToBuild( player ), TRUE, FALSE );
// remove from queue list
removeFromProductionQueue( production );
// delete the production entry
deleteInstance(production);
return;
}
}
}
//-------------------------------------------------------------------------------------------------
/** Cancel all production of type unitType */
//-------------------------------------------------------------------------------------------------
void ProductionUpdate::cancelAllUnitsOfType( const ThingTemplate *unitType)
{
// search for the production entry in our queue
ProductionEntry *production;
for( production = m_productionQueue; production; )
{
// are we at the one we want get rid of it
if( production->getProductionType() == PRODUCTION_UNIT &&
production->getProductionObject() == unitType )
{
ProductionEntry *temp = production->m_next;
// cancel the production
cancelUnitCreate( production->getProductionID() );
// advance
production = temp;
}
else
{
// advance
production = production->m_next;
}
}
}
//-------------------------------------------------------------------------------------------------
/** Update the door behavior */
//-------------------------------------------------------------------------------------------------
void ProductionUpdate::updateDoors()
{
const ProductionUpdateModuleData *d = getProductionUpdateModuleData();
UnsignedInt now = TheGameLogic->getFrame();
for (Int i = 0; i < DOOR_COUNT_MAX; ++i)
{
//
// if we have an open door, see if enough time has passed for us to close it ... likewise
// if we were closing a door we will clear the closing condition after an amount of time
// has passed
//
if( m_doors[i].m_doorOpenedFrame )
{
if( now - m_doors[i].m_doorOpenedFrame > d->m_doorOpeningTime )
{
// set our frame markers for door states
m_doors[i].m_doorOpenedFrame = 0;
m_doors[i].m_doorWaitOpenFrame = now;
// set the flags that will show the difference in the model
m_clearFlags.set( theOpeningFlags[i], true );
m_setFlags.set( theOpeningFlags[i], false );
m_setFlags.set( theWaitingOpenFlags[i] );
m_flagsDirty = TRUE;
}
}
else if( m_doors[i].m_doorWaitOpenFrame )
{
if( now - m_doors[i].m_doorWaitOpenFrame > d->m_doorWaitOpenTime && !m_doors[i].m_holdOpen )
{
// set our frame marker for closing
m_doors[i].m_doorWaitOpenFrame = 0;
m_doors[i].m_doorClosedFrame = now;
// set the flags that show the difference in the art
m_clearFlags.set( theWaitingOpenFlags[i], true );
m_setFlags.set( theWaitingOpenFlags[i], false );
m_setFlags.set( theClosingFlags[i] );
m_flagsDirty = TRUE;
}
}
else if( m_doors[i].m_doorClosedFrame && !m_doors[i].m_holdOpen )
{
if( now - m_doors[i].m_doorClosedFrame > d->m_doorClosingTime )
{
// set our frame marker for the closed door frame
m_doors[i].m_doorClosedFrame = 0;
// set the flags that will show the difference in the model
m_clearFlags.set( theClosingFlags[i], true );
m_setFlags.set( theClosingFlags[i], false );
m_flagsDirty = TRUE;
}
}
}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
UpdateSleepTime ProductionUpdate::update()
{
/// @todo srj use SLEEPY_UPDATE here
ProductionEntry *production = m_productionQueue;
const ProductionUpdateModuleData *d = getProductionUpdateModuleData();
UnsignedInt now = TheGameLogic->getFrame();
Object *us = getObject();
// update the door behaviors
if( d->m_numDoorAnimations > 0 )
updateDoors();
//
// if we're in the construction complete state ... we need to check frame
// we're in it and get out of it when the time is up
//
if( m_constructionCompleteFrame )
{
if( now - m_constructionCompleteFrame > d->m_constructionCompleteDuration )
{
// set our frame marker to be out of construction complete state
m_constructionCompleteFrame = 0;
// set the flags that show the difference in the model
m_clearFlags.set( MODELCONDITION_CONSTRUCTION_COMPLETE, true );
m_setFlags.set( MODELCONDITION_CONSTRUCTION_COMPLETE, false );
m_flagsDirty = TRUE;
}
}
// if we have dirty bits that need to be set and cleared, do it here all at once
if( m_flagsDirty == TRUE )
{
Drawable *draw = us->getDrawable();
if( draw )
draw->clearAndSetModelConditionFlags( m_clearFlags, m_setFlags );
// clear the things we just set and turn off our dirty flag
m_clearFlags.clear();
m_setFlags.clear();
m_flagsDirty = FALSE;
}
// if nothing in the queue get outta here
if( production == nullptr )
return UPDATE_SLEEP_NONE;
//
// if we've become OBJECT_STATUS_SOLD, halt all production ... leave things in the
// queue because if the sell process completes we get money back for them, for now we
// will be just frozen in time
// Actually, there will be nothing in the queue since everything gets cancel/refunded
// at the start of sell, but we still don't want to do anything here.
//
if( us->getStatusBits().test( OBJECT_STATUS_SOLD ) )
return UPDATE_SLEEP_NONE;
// get the player that is building this thing
Player *player = us->getControllingPlayer();
// sanity
if( player == nullptr )
{
// remove from queue list
removeFromProductionQueue( production );
// delete the production entry
deleteInstance(production);
return UPDATE_SLEEP_NONE;
}
//
// you can disallow types of units on the fly in scripts, so if there is something
// in the queue that suddenly can't be build then get rid of it
//
if( production->getProductionType() == PRODUCTION_UNIT &&
player->allowedToBuild( production->getProductionObject() ) == FALSE )
{
// Don't cancel dozers in the queue. jba.
if (!production->getProductionObject()->isKindOf(KINDOF_DOZER))
{
#if RETAIL_COMPATIBLE_CRC
cancelUnitCreate(production->getProductionID());
return UPDATE_SLEEP_NONE;
#else
// TheSuperHackers @bugfix arcticdolphin 13/03/2026 Let partial production finish naturally when script-disallowed.
if( production->getProductionQuantityRemaining() == production->getProductionQuantity() )
{
cancelUnitCreate(production->getProductionID());
return UPDATE_SLEEP_NONE;
}
#endif
}
}
// increase the frames we've been under production for
production->m_framesUnderConstruction++;
// how many total logic frames does it take to produce this unit
Int totalProductionFrames;
if( production->m_type == PRODUCTION_UNIT )
totalProductionFrames = production->m_objectToProduce->calcTimeToBuild( player );
else
totalProductionFrames = production->m_upgradeToResearch->calcTimeToBuild( player );
// figure out our percent complete
production->m_percentComplete = INT_TO_REAL( production->m_framesUnderConstruction ) /
INT_TO_REAL( totalProductionFrames ) *
100.0f;
// if we've reached 100% or more we're done, tada!
if( production->m_percentComplete >= 100.0f )
{
// handle unit production items
if( production->m_type == PRODUCTION_UNIT )
{
Object *creationBuilding = us;
// create this unit
//if I've got a ExitModule
ExitInterface *exitInterface = creationBuilding->getObjectExitInterface();
if( exitInterface )
{
//
// only exit if the exit interface will let us ... if we can't we'll just keep this
// unit in the production and keep "overbuilding" it, every frame we will check to see
// if we can exit and then move onto production of the next one
//
Int numberToTry = production->getProductionQuantityRemaining();// this will change midloop, so it can't be the For clause
for( int i = 0; i < numberToTry; i++ )
{
ExitDoorType exitDoor = production->getExitDoor();
if (exitDoor == DOOR_NONE_AVAILABLE)
{
exitDoor = exitInterface->reserveDoorForExit(production->m_objectToProduce, nullptr);
production->setExitDoor(exitDoor);
}
if (exitDoor != DOOR_NONE_AVAILABLE)
{
// note, could be DOOR_NONE_NEEDED! so door could be null. (srj)
DoorInfo* door = (exitDoor >= 0 && exitDoor < DOOR_COUNT_MAX) ? &m_doors[exitDoor] : nullptr;
//
// if the producing structure has a door opening animation we will set the condition
// bits to open the door (note that we must also take care to remove any condition
// that had us previously closing a door)
//
const ProductionUpdateModuleData *d = getProductionUpdateModuleData();
if( d->m_numDoorAnimations > 0 && door != nullptr )
{
// if the door is closed, open it
if( door->m_doorOpenedFrame == 0 && door->m_doorWaitOpenFrame == 0 && door->m_doorClosedFrame == 0 )
{
door->m_doorOpenedFrame = now;
m_setFlags.set( theOpeningFlags[exitDoor] );
m_flagsDirty = TRUE;
}
// if the door is waiting-open, keep it there
else if( door->m_doorWaitOpenFrame != 0 )
{
door->m_doorWaitOpenFrame = now;
}
// if the door is closing, for now, pop it to waiting open
else if( door->m_doorClosedFrame != 0 )
{
door->m_doorWaitOpenFrame = now;
m_clearFlags.set( theOpeningFlags[exitDoor], true );
m_clearFlags.set( theClosingFlags[exitDoor], true );
m_setFlags.set( theOpeningFlags[exitDoor], false );
m_setFlags.set( theClosingFlags[exitDoor], false );
m_setFlags.set( theWaitingOpenFlags[exitDoor] );
m_flagsDirty = TRUE;
}
}
// we now go into the construction complete condition for a while
if( m_constructionCompleteFrame == 0 )
{
m_constructionCompleteFrame = now;
m_setFlags.set( MODELCONDITION_CONSTRUCTION_COMPLETE );
m_flagsDirty = TRUE;
}
//
// make a new object, note that for production buildings that have door
// animations we will not make the object until the door has been totally
// opened
//
if( d->m_numDoorAnimations == 0 || door == nullptr || door->m_doorWaitOpenFrame != 0 )
{
Object *newObj = TheThingFactory->newObject( production->m_objectToProduce,
creationBuilding->getControllingPlayer()->getDefaultTeam() );
newObj->setProducer(creationBuilding);
// call the exit interface to do the rally point and position stuff
exitInterface->exitObjectViaDoor( newObj, exitDoor );
// since we successfully exited via this door, we should NOT call unreserveDoorForExit
// when we toss this production entry. so set it back to an innocuous value.
production->setExitDoor(DOOR_NONE_AVAILABLE);
// Now, play the sound associated with the new object's production.
AudioEventRTS voiceCreate = *newObj->getTemplate()->getVoiceCreated();
voiceCreate.setObjectID(newObj->getID());
TheAudio->addAudioEvent(&voiceCreate);
// call the onUnitCreated for the player
creationBuilding->getControllingPlayer()->onUnitCreated( creationBuilding, newObj );
// onCreates have been called on newObj, and after that the owner was set,
// so now is the time to call the game side of CreateModules
for (BehaviorModule** m = newObj->getBehaviorModules(); *m; ++m)
{
CreateModuleInterface* create = (*m)->getCreate();
if (!create)
continue;
create->onBuildComplete();
}
if( production->getProductionQuantity() == production->getProductionQuantityRemaining() )
{
//Call the voice created for the 1st object -- because it's possible to create multiple objects like redguards!
AudioEventRTS sound = *newObj->getTemplate()->getPerUnitSound( "VoiceCreate" );
sound.setObjectID( newObj->getID() );
TheAudio->addAudioEvent( &sound );
}
//We created one guy, but we may want to do more so we should stay in this node of production.
// This is last so the voice check can easily check for "first" guy
production->oneProductionSuccessful();
}
}
}
if( production->getProductionQuantityRemaining() == 0 )
{
// remove this production entry so we can go on to the next if we are totally finished
removeFromProductionQueue( production );
// delete the production entry
deleteInstance(production);
}
}
else
{
// there is no exit interface, this is an error
DEBUG_CRASH( ("Cannot create '%s', there is no ExitUpdate interface defined for producer object '%s'",
production->m_objectToProduce->getName().str(),
creationBuilding->getTemplate()->getName().str()) );
// remove this item from the production queue
removeFromProductionQueue( production );
// delete the production entry
deleteInstance(production);
}
}
else if( production->m_type == PRODUCTION_UPGRADE )
{
const UpgradeTemplate *upgrade = production->m_upgradeToResearch;
// we finished an upgrade, lets add that money spent on it to the scorekeeper
player->getScoreKeeper()->addMoneySpent(upgrade->calcCostToBuild(player));
// notify the script engine
TheScriptEngine->notifyOfCompletedUpgrade(
us->getControllingPlayer()->getPlayerIndex(),
upgrade->getUpgradeName(),
us->getID());
// print a message to the local player
if( us->isLocallyViewed() )
{
UnicodeString msg;
UnicodeString format = TheGameText->fetch( "UPGRADE:UpgradeComplete" );
UnicodeString upgradeName = TheGameText->fetch( upgrade->getDisplayNameLabel().str() );
msg.format( format.str(), upgradeName.str() );
TheInGameUI->message( msg );
// upgrades are a more rare event, play a nifty radar event thingy
TheRadar->createEvent( us->getPosition(), RADAR_EVENT_UPGRADE );
//Play the sound for the upgrade, because we just built it!
AudioEventRTS sound = *upgrade->getResearchCompleteSound();
if( TheAudio->isValidAudioEvent( &sound ) )
{
//We have a custom upgrade complete sound.
sound.setObjectID( us->getID() );
TheAudio->addAudioEvent( &sound );
}
else
{
//Use a generic EVA event.
TheEva->setShouldPlay(EVA_UpgradeComplete);
}
//Play any available unit specific sound for upgrade.
sound = *upgrade->getUnitSpecificSound();
sound.setObjectID( us->getID() );
TheAudio->addAudioEvent( &sound );
}
// update the upgrade status in the player or give the upgrade to the object
if( upgrade->getUpgradeType() == UPGRADE_TYPE_PLAYER )
{
player->addUpgrade( upgrade, UPGRADE_STATUS_COMPLETE );
}
else
{
us->giveUpgrade( upgrade );
}
//Also mark the UI dirty -- incase object with upgrade cameo is selected.
Drawable *draw = TheInGameUI->getFirstSelectedDrawable();
Object *selectedObject = draw ? draw->getObject() : nullptr;
if( selectedObject )
{
const ThingTemplate *thing = selectedObject->getTemplate();
for( Int i = 0; i < MAX_UPGRADE_CAMEO_UPGRADES; i++ )
{
AsciiString upgradeName = thing->getUpgradeCameoName( i );
const UpgradeTemplate *testUpgrade = TheUpgradeCenter->findUpgrade( upgradeName );
if( testUpgrade == upgrade )
{
//Our selected object has the upgrade
TheControlBar->markUIDirty();
break;
}
}
}
// remove this production entry so we can go on to the next
removeFromProductionQueue( production );
// delete the production entry
deleteInstance(production);
}
}
return UPDATE_SLEEP_NONE;
}
//-------------------------------------------------------------------------------------------------
/** Add the production entry to the *END* of the production queue list */
//-------------------------------------------------------------------------------------------------
void ProductionUpdate::addToProductionQueue( ProductionEntry *production )
{
// check for empty list
if( m_productionQueue == nullptr )