forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathDozerAIUpdate.cpp
More file actions
2536 lines (2000 loc) · 85.9 KB
/
Copy pathDozerAIUpdate.cpp
File metadata and controls
2536 lines (2000 loc) · 85.9 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: DozerAIUpdate.cpp ////////////////////////////////////////////////////////////////////////
// Author: Colin Day, February 2002
// Desc: Dozer AI behavior
///////////////////////////////////////////////////////////////////////////////////////////////////
// USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "Common/ActionManager.h"
#include "Common/Team.h"
#include "Common/StateMachine.h"
#include "Common/BuildAssistant.h"
#include "Common/ThingTemplate.h"
#include "Common/ThingFactory.h"
#include "Common/Player.h"
#include "Common/Money.h"
#include "Common/Radar.h"
#include "Common/RandomValue.h"
#include "Common/GameState.h"
#include "Common/GlobalData.h"
#include "Common/Xfer.h"
#include "GameClient/Drawable.h"
#include "GameClient/GameText.h"
#include "GameLogic/AIPathfind.h"
#include "GameLogic/PartitionManager.h"
#include "GameLogic/Locomotor.h"
#include "GameLogic/Module/BodyModule.h"
#include "GameLogic/Module/BridgeBehavior.h"
#include "GameLogic/Module/BridgeTowerBehavior.h"
#include "GameLogic/Module/CreateModule.h"
#include "GameLogic/Module/DozerAIUpdate.h"
#include "GameClient/InGameUI.h"
// FORWARD DECLARATIONS ///////////////////////////////////////////////////////////////////////////
class DozerPrimaryStateMachine;
class DozerActionStateMachine;
static const Real MIN_ACTION_TOLERANCE = 70.0f;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
/** Available Dozer actions */
//-------------------------------------------------------------------------------------------------
enum DozerActionType CPP_11(: Int)
{
DOZER_ACTION_PICK_ACTION_POS, ///< pick a location "around" the target to do our action
DOZER_ACTION_MOVE_TO_ACTION_POS,///< move to our action pos we've picked
DOZER_ACTION_DO_ACTION, ///< do our action at the position, build/repair/etc ...
};
//-------------------------------------------------------------------------------------------------
/** Dozer picks a position around the target to do the action */
//-------------------------------------------------------------------------------------------------
class DozerActionPickActionPosState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(DozerActionPickActionPosState, "DozerActionPickActionPosState")
public:
DozerActionPickActionPosState( StateMachine *machine, DozerTask task );
virtual StateReturnType update() override;
protected:
// snapshot interface
virtual void crc( Xfer *xfer ) override;
virtual void xfer( Xfer *xfer ) override;
virtual void loadPostProcess() override;
protected:
DozerTask m_task; ///< our task
Int m_failedAttempts; /**< counter for successive unsuccessfull attempts to pick
and move to an action position */
};
EMPTY_DTOR(DozerActionPickActionPosState)
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
DozerActionPickActionPosState::DozerActionPickActionPosState( StateMachine *machine,
DozerTask task ) :
State( machine, "DozerActionPickActionPosState" )
{
m_task = task;
m_failedAttempts = 0;
}
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void DozerActionPickActionPosState::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method */
// ------------------------------------------------------------------------------------------------
void DozerActionPickActionPosState::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferUser(&m_task, sizeof(m_task));
xfer->xferInt(&m_failedAttempts);
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void DozerActionPickActionPosState::loadPostProcess()
{
}
//-------------------------------------------------------------------------------------------------
/** Pick a position around the target */
//-------------------------------------------------------------------------------------------------
StateReturnType DozerActionPickActionPosState::update()
{
StateMachine *machine = getMachine();
Object *dozer = machine->getOwner();
// get dozer ai update interface
AIUpdateInterface *ai = dozer->getAIUpdateInterface();
if( !ai )
{
return STATE_FAILURE;
}
DozerAIInterface *dozerAI = ai->getDozerAIInterface();
if( !dozerAI )
{
return STATE_FAILURE;
}
// get the object we're concerned with for our task
Object *goalObject = TheGameLogic->findObjectByID( dozerAI->getTaskTarget( m_task ) );
// if there is no goal, get out of this machine with a failure code (success is done in the action state )
if( goalObject == nullptr )
{
// to be clean get rid of the goal object we set
getMachine()->setGoalObject( nullptr );
// cancel our task
dozerAI->cancelTask( m_task );
// exit with failure
return STATE_FAILURE;
}
// pick a location to move to
Coord3D goalPos;
const Coord3D *pos = dozerAI->getDockPoint( m_task, DOZER_DOCK_POINT_START );
if( pos )
goalPos = *pos;
else
{
// pick a spot to use
//
// find the vector from goal object to us ... we will start our search on this angle so
// that we "approach" a closer point rather than a point on a random side
//
Coord2D v;
v.x = dozer->getPosition()->x - goalObject->getPosition()->x;
v.y = dozer->getPosition()->y - goalObject->getPosition()->y;
Real radius = goalObject->getGeometryInfo().getBoundingSphereRadius();
FindPositionOptions fpOptions;
fpOptions.minRadius = radius;
fpOptions.maxRadius = radius;
fpOptions.startAngle = v.toAngle();
if( ThePartitionManager->findPositionAround( goalObject->getPosition(),
&fpOptions,
&goalPos ) == FALSE )
{
// return STATE_FAILURE; no, we don't ever want dozers to fail, particularly
// if ai.
goalPos = *goalObject->getPosition();
}
//
// we only ignore the goal object when we did the point selection in this more
// "random" method cause we could pick a place inside the object
//
ai->ignoreObstacle( goalObject );
}
// set goal position and object
machine->setGoalObject( goalObject );
machine->setGoalPosition( &goalPos );
ai->ignoreObstacle(goalObject);
ai->aiMoveToPosition( &goalPos, CMD_FROM_AI );
return STATE_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
/** Dozer moves to the action position */
//-------------------------------------------------------------------------------------------------
class DozerActionMoveToActionPosState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(DozerActionMoveToActionPosState, "DozerActionMoveToActionPosState")
public:
DozerActionMoveToActionPosState( StateMachine *machine, DozerTask task ) : State( machine, "DozerActionMoveToActionPosState" ) { m_task = task; }
virtual StateReturnType update() override;
protected:
// snapshot interface
virtual void crc( Xfer *xfer ) override;
virtual void xfer( Xfer *xfer ) override;
virtual void loadPostProcess() override;
protected:
DozerTask m_task; ///< our task
};
EMPTY_DTOR(DozerActionMoveToActionPosState)
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void DozerActionMoveToActionPosState::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method */
// ------------------------------------------------------------------------------------------------
void DozerActionMoveToActionPosState::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferUser(&m_task, sizeof(m_task));
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void DozerActionMoveToActionPosState::loadPostProcess()
{
}
//-------------------------------------------------------------------------------------------------
/** We are supposed to be on route to our action position now, see when we get there or
* detect that we have encountered a problem that's going to cause it to give up */
//-------------------------------------------------------------------------------------------------
StateReturnType DozerActionMoveToActionPosState::update()
{
Object *goalObject = getMachineGoalObject();
Object *dozer = getMachineOwner();
// sanity
if( goalObject == nullptr || dozer == nullptr )
return STATE_FAILURE;
AIUpdateInterface *ai = dozer->getAIUpdateInterface();
ObjectID currentRepairer = goalObject->getSoleHealingBenefactor();
if( m_task==DOZER_TASK_REPAIR && currentRepairer != INVALID_ID && currentRepairer != dozer->getID() )//oops I guess someone beat me to it!
{
if ( ai )
{
DozerAIInterface *dozerAI = ai->getDozerAIInterface();
if ( dozerAI )
dozerAI->internalTaskComplete( m_task );
}
getMachine()->setGoalObject( nullptr );
return STATE_FAILURE;
}
// Double oops, someone else has taken over the build responsibility for this building. We can't double up.
// This can happen because a guy who is moving to build who is told to build will idle for a frame after registering.
// Two workers, two started buildings.
// Grab both workers, resume on one building
// First will register, second will see first as active and say no
// Grab both, click on other building
// First will register, and idle for a frame, second will see first as not active and say yes
// Next frame, first will start the build task, without reasking validity
// Infinite number of workers can be told to build something with just two in progress buildings
if( (m_task == DOZER_TASK_BUILD) && goalObject && (goalObject->getBuilderID() != dozer->getID()) )
{
// Geebus. Returning failure is ignored, so you have to explicitly make the ai stop trying to restart the machine
if ( ai )
{
DozerAIInterface *dozerAI = ai->getDozerAIInterface();
if ( dozerAI )
dozerAI->internalTaskComplete( m_task );
}
getMachine()->setGoalObject( nullptr );
return STATE_FAILURE;
}
// if distance between us and our goal position is close enough
const Coord3D *goalPos = getMachine()->getGoalPosition();
Real distSqr = ThePartitionManager->getDistanceSquared( dozer, goalPos, FROM_BOUNDINGSPHERE_2D );
const Real SLOP = 15.0f;
Real allowableDistanceSqr = sqr(max( MIN_ACTION_TOLERANCE, dozer->getGeometryInfo().getBoundingSphereRadius() + SLOP ));
if( distSqr <= allowableDistanceSqr )
{
if( m_task == DOZER_TASK_BUILD )
{
//
// the object is now no longer awaiting construction, it is being constructed ... note
// that we might possibly be here multiple times for a single object being built
// but the setting of the same model condition doesn't have any adverse effects
//
goalObject->clearAndSetModelConditionFlags(
MAKE_MODELCONDITION_MASK(MODELCONDITION_AWAITING_CONSTRUCTION),
MAKE_MODELCONDITION_MASK2(MODELCONDITION_PARTIALLY_CONSTRUCTED, MODELCONDITION_ACTIVELY_BEING_CONSTRUCTED));
}
return STATE_SUCCESS;
}
// if we're in the idle state fail our move
// Failure transition is back to DOZER_ACTION_PICK_ACTION_POS, so
// it is ok to fail. jba.
if( ai && ai->isIdle() )
return STATE_FAILURE;
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
/** Dozer does the "action" */
//-------------------------------------------------------------------------------------------------
class DozerActionDoActionState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(DozerActionDoActionState, "DozerActionDoActionState")
public:
DozerActionDoActionState( StateMachine *machine, DozerTask task ) : State( machine, "DozerActionDoActionState" )
{
m_task = task;
m_enterFrame = 0;
}
virtual StateReturnType update() override;
virtual StateReturnType onEnter() override;
virtual void onExit( StateExitType status ) override { }
protected:
// snapshot interface
virtual void crc( Xfer *xfer ) override;
virtual void xfer( Xfer *xfer ) override;
virtual void loadPostProcess() override;
protected:
DozerTask m_task; ///< our task
UnsignedInt m_enterFrame; ///< frame we entered this state on
};
EMPTY_DTOR(DozerActionDoActionState)
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void DozerActionDoActionState::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method */
// ------------------------------------------------------------------------------------------------
void DozerActionDoActionState::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferUser(&m_task, sizeof(m_task));
xfer->xferUnsignedInt(&m_enterFrame);
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void DozerActionDoActionState::loadPostProcess()
{
}
//-------------------------------------------------------------------------------------------------
/** Entering the do action state */
//-------------------------------------------------------------------------------------------------
StateReturnType DozerActionDoActionState::onEnter()
{
Object *dozer = getMachineOwner();
DozerAIInterface *dozerAI = dozer->getAIUpdateInterface()->getDozerAIInterface();
if( !dozerAI )
{
return STATE_FAILURE;
}
// DozerAIUpdate *dozerAI = static_cast<DozerAIUpdate *>(dozer->getAIUpdateInterface());
// record the frame we came in on
m_enterFrame = TheGameLogic->getFrame();
// when building, we have additional movement that we will for docking
if( m_task == DOZER_TASK_BUILD )
dozerAI->setBuildSubTask( DOZER_SELECT_BUILD_DOCK_LOCATION );
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
/** Do the action */
//-------------------------------------------------------------------------------------------------
StateReturnType DozerActionDoActionState::update()
{
Object *goalObject = getMachineGoalObject();
Object *dozer = getMachineOwner();
AIUpdateInterface *ai = dozer->getAIUpdateInterface();
if( !ai )
{
return STATE_FAILURE;
}
DozerAIInterface *dozerAI = ai->getDozerAIInterface();
if( !dozerAI )
{
return STATE_FAILURE;
}
// DozerAIUpdate *dozerAI = static_cast<DozerAIUpdate *>(dozer->getAIUpdateInterface());
// const UnsignedInt ACTION_TIME = LOGICFRAMES_PER_SECOND * 4 ; // frames to spend here in this state doing the action
// check for object gone
if( goalObject == nullptr )
return STATE_FAILURE;
if ( dozer->isDisabledByType( DISABLED_UNMANNED ) )// Yipes, I've been sniped!
return STATE_FAILURE;
// do the task
Bool complete = FALSE;
switch( m_task )
{
//---------------------------------------------------------------------------------------------
case DOZER_TASK_BUILD:
{
//GS Moved this inside Build, since you are allowed to Repair things that are not your player (canRepairObject handles it)
if (dozer->getControllingPlayer() != goalObject->getControllingPlayer())//Yipes, SOmehow I have changed sides in mid build!
return STATE_FAILURE;
// if we need to select the dock location and move there do so
if( dozerAI->getBuildSubTask() == DOZER_SELECT_BUILD_DOCK_LOCATION )
{
const Coord3D *dockLocation = dozerAI->getDockPoint( m_task, DOZER_DOCK_POINT_ACTION );
if( dockLocation )
ai->aiMoveToPosition( dockLocation, CMD_FROM_AI );
// we're now moving to the dock location
dozerAI->setBuildSubTask( DOZER_MOVING_TO_BUILD_DOCK_LOCATION );
}
// if we're moving to the build dock location, when we become idle we are there
if( dozerAI->getBuildSubTask() == DOZER_MOVING_TO_BUILD_DOCK_LOCATION )
{
if( ai->isIdle() )
{
dozerAI->setBuildSubTask( DOZER_DO_BUILD_AT_DOCK );
// Get the audio sound and start playing the construction sound (get the sound
// from the building itself)
dozerAI->startBuildingSound( goalObject->getTemplate()->getPerUnitSound( "UnderConstruction" ), goalObject->getID() );
}
}
// only do the build if we've moved into the dock position
if( dozerAI->getBuildSubTask() == DOZER_DO_BUILD_AT_DOCK )
{
// the builder is now actively constructing something
dozer->setModelConditionState( MODELCONDITION_ACTIVELY_CONSTRUCTING );
// increase the construction percent of the goal object
Int framesToBuild = goalObject->getTemplate()->calcTimeToBuild( dozer->getControllingPlayer() );
Real percentProgressThisFrame = 100.0f / framesToBuild;
goalObject->setConstructionPercent( goalObject->getConstructionPercent() +
percentProgressThisFrame );
//
// every time we construct a piece of the goal object, the goal object gets a little
// bit o health, note that we're bypassing the regular healing/damage methods
// here and going straight for the change of the health
//
BodyModuleInterface *body = goalObject->getBodyModule();
body->internalChangeHealth( body->getMaxHealth() / INT_TO_REAL( framesToBuild ) );
//
// since we've just actually contributed a "piece" to this building, we'll say that
// we are now the producer and the builder
//
goalObject->setProducer( dozer );
goalObject->setBuilder( dozer );
// check for construction complete
if( goalObject->getConstructionPercent() >= 100.0f )
{
// clear the under construction status
goalObject->clearStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_UNDER_CONSTRUCTION ) );
goalObject->clearStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_RECONSTRUCTING ) );
// stop playing the construction sound!
dozerAI->finishBuildingSound();
// object will now be idle instead of in one of the construction actions
goalObject->clearModelConditionFlags(
MAKE_MODELCONDITION_MASK3(MODELCONDITION_AWAITING_CONSTRUCTION, MODELCONDITION_PARTIALLY_CONSTRUCTED, MODELCONDITION_ACTIVELY_BEING_CONSTRUCTED));
// set the construction at the 100% enum value
goalObject->setConstructionPercent( CONSTRUCTION_COMPLETE );
//
// now that we're done with construction we evaluate the visual condition of
// the object since while under construction visual changes due to damage state
// do not occur. Note that is was important to call this after we cleared the
// model conditions involving construction because part of this evaluation
// is to auto populate the model with particle systems when special named
// bones for the current given state are provided
//
body->evaluateVisualCondition();
// this object now has energy influence in the player
Player *player = goalObject->getControllingPlayer();
if( player )
{
// notification for build completion
player->onStructureConstructionComplete( dozer, goalObject, dozerAI->getIsRebuild() );
//
// Now onCreates were called at construction start. Now at finish is when we
// want the Game side of OnCreate
//
for (BehaviorModule** m = goalObject->getBehaviorModules(); *m; ++m)
{
CreateModuleInterface* create = (*m)->getCreate();
if (!create)
continue;
create->onBuildComplete();
}
}
// Creation is another valid and essential time to call this. This building now Looks.
goalObject->handlePartitionCellMaintenance();
// this object now has influence in the controlling players' tech tree
/// @todo need to write this
// do some UI stuff for the controlling player
if( dozer->isLocallyViewed() )
{
// message the the building player
UnicodeString format = TheGameText->fetch( "DOZER:ConstructionComplete" );
UnicodeString objectName = goalObject->getTemplate()->getDisplayName();
if( objectName.isEmpty() )
{
UnicodeString format = TheGameText->fetch( "INI:MissingDisplayName" );
objectName.format( format, goalObject->getTemplate()->getName().str() );
}
UnicodeString msg;
msg.format( format.str(), objectName.str() );
TheInGameUI->message( msg );
AudioEventRTS audio = *dozer->getTemplate()->getVoiceTaskComplete();
audio.setObjectID(dozer->getID());
TheAudio->addAudioEvent(&audio);
/// make radar neat-o attention grabber event at build location
TheRadar->createEvent( goalObject->getPosition(), RADAR_EVENT_CONSTRUCTION );
}
// this will allow us to exit the state machine with success
complete = TRUE;
// move off to the end dock position if present
const Coord3D *endPos = dozerAI->getDockPoint( m_task, DOZER_DOCK_POINT_END );
Coord3D pos = *dozer->getPosition();
if( endPos ) {
pos = *endPos;
}
// Our goal may be inside a building, if we are packing them in tight, so try adjusting. jba.
TheAI->pathfinder()->adjustToPossibleDestination(dozer, ai->getLocomotorSet(), &pos);
ai->aiMoveToPosition( &pos, CMD_FROM_AI );
}
}
break;
}
//---------------------------------------------------------------------------------------------
case DOZER_TASK_REPAIR:
{
BodyModuleInterface *body = goalObject->getBodyModule();
// check for fully "repaired"
if( body->getHealth() == body->getMaxHealth() )
{
// issue repair complete message, we might want to remove this later cause it could be annoying
TheInGameUI->message( "DOZER:RepairComplete" );
// we're now complete
complete = TRUE;
}
else
{
Bool canHeal = TRUE;
// if we are repairing a bridge, create scaffolding over the bridge if we need to
if( goalObject->isKindOf( KINDOF_BRIDGE_TOWER ) )
dozerAI->createBridgeScaffolding( goalObject );
// the builder is now actively repairing something, we'll borrow the constructing animation
dozer->setModelConditionState( MODELCONDITION_ACTIVELY_CONSTRUCTING );
//
// when repairing bridges, we cannot actually do any repairing until the
// scaffolding is extended and all the way complete
//
if( goalObject->isKindOf( KINDOF_BRIDGE_TOWER ) )
{
BridgeTowerBehaviorInterface *btbi = BridgeTowerBehavior::getBridgeTowerBehaviorInterfaceFromObject( goalObject );
DEBUG_ASSERTCRASH( btbi, ("Unable to find bridge tower interface") );
Object *bridgeObject = TheGameLogic->findObjectByID( btbi->getBridgeID() );
DEBUG_ASSERTCRASH( bridgeObject, ("Unable to find bridge center object") );
BridgeBehaviorInterface *bbi = BridgeBehavior::getBridgeBehaviorInterfaceFromObject( bridgeObject );
DEBUG_ASSERTCRASH( bbi, ("Unable to find bridge interface from tower goal object during repair") );
if( bbi->isScaffoldInMotion() == TRUE )
canHeal = FALSE;
}
// do healing
if( canHeal )
{
// figure out how much health we will restore this frame
Real health = body->getMaxHealth() * dozerAI->getRepairHealthPerSecond() /
LOGICFRAMES_PER_SECOND;
// try to give it a little bit-o-health
if ( ! goalObject->attemptHealingFromSoleBenefactor(health, dozer, 2) )//this frame and the next
{
// goalObject->setStatus( OBJECT_STATUS_UNDERGOING_REPAIR );
// This bit used to be set way back in DozerAIUpdate::privateRepair(), but it has been outmoded
// so that several dozers/workers can target the same sick building for repair, but the first one to begin
// this is done by attemptHealingFromSoleBenefactor() which lets the beneficiary of the healing
// remember who has been healing it, and will return false to everybody else
//or if the goalObject is already receiving healing, I must stop, since my healing is getting rejected here
dozerAI->internalTaskComplete( m_task );
getMachine()->setGoalObject( nullptr );
return STATE_FAILURE;
}
}
}
// play a repairing sound
break;
}
//---------------------------------------------------------------------------------------------
case DOZER_TASK_FORTIFY:
{
/// @todo write me
break;
}
//---------------------------------------------------------------------------------------------
default:
{
DEBUG_CRASH(( "Unknown task for the dozer action do action state" ));
return STATE_FAILURE;
}
}
// if we're complete with the task we exit success
if( complete == TRUE )
{
// this task is now complete, remove it from dozer consideration
dozerAI->internalTaskComplete( m_task );
// to be clean get rid of the goal object we set
getMachine()->setGoalObject( nullptr );
getMachineOwner()->setWeaponSetFlag(WEAPONSET_MINE_CLEARING_DETAIL);//maybe go clear some mines, if I feel like it
// we're done
return STATE_SUCCESS;
}
// just continue sitting here for a while
getMachineOwner()->clearWeaponSetFlag(WEAPONSET_MINE_CLEARING_DETAIL);// no mine clearing fun while I'm on the job
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
/** The Dozer action state machine */
//-------------------------------------------------------------------------------------------------
class DozerActionStateMachine : public StateMachine
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( DozerActionStateMachine, "DozerActionStateMachine" );
public:
DozerActionStateMachine( Object *owner, DozerTask task );
// virtual destructor prototypes provided by memory pool object
protected:
// snapshot interface
virtual void crc( Xfer *xfer ) override;
virtual void xfer( Xfer *xfer ) override;
virtual void loadPostProcess() override;
protected:
DozerTask m_task; ///< the task of this action state machine
};
EMPTY_DTOR(DozerActionStateMachine)
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
DozerActionStateMachine::DozerActionStateMachine( Object *owner, DozerTask task ) :
StateMachine( owner, "DozerActionStateMachine" )
{
// initialize our task
m_task = task;
// order matters: first state is the default state.
defineState( DOZER_ACTION_PICK_ACTION_POS, newInstance(DozerActionPickActionPosState)( this, task ), DOZER_ACTION_MOVE_TO_ACTION_POS, EXIT_MACHINE_WITH_FAILURE );
defineState( DOZER_ACTION_MOVE_TO_ACTION_POS, newInstance(DozerActionMoveToActionPosState)( this, task ), DOZER_ACTION_DO_ACTION, DOZER_ACTION_PICK_ACTION_POS );
defineState( DOZER_ACTION_DO_ACTION, newInstance(DozerActionDoActionState)( this, task ), EXIT_MACHINE_WITH_SUCCESS, EXIT_MACHINE_WITH_FAILURE );
}
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void DozerActionStateMachine::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method */
// ------------------------------------------------------------------------------------------------
void DozerActionStateMachine::xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferUser(&m_task, sizeof(m_task));
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void DozerActionStateMachine::loadPostProcess()
{
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
static Object *findObjectToRepair( Object *dozer )
{
// sanity
if( dozer == nullptr )
return nullptr;
if( !dozer->getAIUpdateInterface() )
{
return nullptr;
}
const DozerAIInterface *dozerAI = dozer->getAIUpdateInterface()->getDozerAIInterface();
PartitionFilterSamePlayer filter1( dozer->getControllingPlayer() );
PartitionFilterAcceptByKindOf filter2( MAKE_KINDOF_MASK( KINDOF_STRUCTURE ),
KINDOFMASK_NONE );
PartitionFilterSameMapStatus filterMapStatus(dozer);
PartitionFilter *filters[] = { &filter1, &filter2, &filterMapStatus, nullptr };
ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( dozer->getPosition(),
dozerAI->getBoredRange(),
FROM_CENTER_2D,
filters );
MemoryPoolObjectHolder hold( iter );
Object *obj;
Object *closestRepairTarget = nullptr;
Real closestRepairTargetDistSqr = 0.0f;
for( obj = iter->first(); obj; obj = iter->next() )
{
// ignore objects we cant repair
if( TheActionManager->canRepairObject( dozer, obj, CMD_FROM_AI ) == FALSE )
continue;
// target the closest valid repair target
if( closestRepairTarget == nullptr )
{
closestRepairTarget = obj;
closestRepairTargetDistSqr = ThePartitionManager->getDistanceSquared( dozer, obj, FROM_CENTER_2D );
}
else
{
// only use this command center if it's closer than the last one we found
Real distSqr = ThePartitionManager->getDistanceSquared( dozer, obj, FROM_CENTER_2D );
if( distSqr < closestRepairTargetDistSqr )
{
closestRepairTarget = obj;
closestRepairTargetDistSqr = distSqr;
}
}
}
return closestRepairTarget;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
static Object *findMine( Object *dozer )
{
// sanity
if( dozer == nullptr )
return nullptr;
if( !dozer->getAIUpdateInterface() )
{
return nullptr;
}
const DozerAIInterface *dozerAI = dozer->getAIUpdateInterface()->getDozerAIInterface();
// srj sez: only clear enemy or neutal mines. clearing allied mines (ie, OURS) is really dumb.
// (and no, PossibleToAttack won't necessarily filter these out.)
PartitionFilterRelationship filterTeam(dozer, PartitionFilterRelationship::ALLOW_ENEMIES | PartitionFilterRelationship::ALLOW_NEUTRAL);
PartitionFilterPossibleToAttack filterAttack(ATTACK_NEW_TARGET, dozer, CMD_FROM_DOZER);
PartitionFilterSameMapStatus filterMapStatus(dozer);
PartitionFilter *filters[] = { &filterTeam, &filterAttack, &filterMapStatus, nullptr };
Object* mine = ThePartitionManager->getClosestObject(dozer, dozerAI->getBoredRange(), FROM_CENTER_2D, filters);
return mine;
}
//-------------------------------------------------------------------------------------------------
/** Available primary Dozer states */
//-------------------------------------------------------------------------------------------------
enum
{
DOZER_PRIMARY_IDLE, ///< dozer is idle
DOZER_PRIMARY_BUILD, ///< dozer is building a structure for the player
DOZER_PRIMARY_REPAIR, ///< dozer is repairing something
DOZER_PRIMARY_FORTIFY, ///< dozer is fortifying a civilian building, making it stronger
DOZER_PRIMARY_GO_HOME, ///< dozer has nothing else to do so it will return a command center
};
//-------------------------------------------------------------------------------------------------
/** Dozer primary idle state */
//-------------------------------------------------------------------------------------------------
class DozerPrimaryIdleState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(DozerPrimaryIdleState, "DozerPrimaryIdleState")
public:
DozerPrimaryIdleState( StateMachine *machine ) : State( machine, "DozerPrimaryIdleState" )
{
m_idleTooLongTimestamp = 0;
m_idlePlayerNumber = 0;
m_isMarkedAsIdle = FALSE;
}
virtual StateReturnType update() override;
virtual StateReturnType onEnter() override;
virtual void onExit( StateExitType status ) override;
protected:
// snapshot interface
virtual void crc( Xfer *xfer ) override;
virtual void xfer( Xfer *xfer ) override;
virtual void loadPostProcess() override;
protected:
UnsignedInt m_idleTooLongTimestamp; ///< when this is more than our idle too long time we try to do something about it
Int m_idlePlayerNumber; ///< Remember what list we were added to.
Bool m_isMarkedAsIdle;
};
EMPTY_DTOR(DozerPrimaryIdleState)
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void DozerPrimaryIdleState::crc( Xfer *xfer )
{
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method */
// ------------------------------------------------------------------------------------------------
void DozerPrimaryIdleState::xfer( Xfer *xfer )
{
// version