forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSupplyTruckAIUpdate.cpp
More file actions
835 lines (713 loc) · 29 KB
/
SupplyTruckAIUpdate.cpp
File metadata and controls
835 lines (713 loc) · 29 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
/*
** Command & Conquer Generals Zero Hour(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. //
// //
////////////////////////////////////////////////////////////////////////////////
// SupplyTruckAIUpdate.cpp ////////////
// Author: Graham Smallwood, February 2002
// Desc: State machine that controls when and with who a Truck docks
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "Common/Player.h"
#include "Common/ResourceGatheringManager.h"
#include "Common/ThingTemplate.h"
#include "GameLogic/Object.h"
#include "GameLogic/PartitionManager.h"
#include "GameLogic/Module/SupplyTruckAIUpdate.h"
#include "GameLogic/Module/SupplyCenterDockUpdate.h"
#include "GameLogic/Module/SupplyWarehouseDockUpdate.h"
#include "GameLogic/Module/WorkerAIUpdate.h"
#include "GameClient/Drawable.h"
#include "GameClient/InGameUI.h"
#if defined(RTS_DEBUG)
#define NO_DEBUG_SUPPLY_STATE
#endif
#ifdef DEBUG_SUPPLY_STATE
static const char* statenames[] =
{
"ST_IDLE", ///< Not doing anything. Should I autopilot?
"ST_BUSY", ///< Direct player involvement (move) has taken me off autopilot
"ST_WANTING", ///< Search for warehouse or center and dock with it
"ST_REGROUPING", ///< Wanting failed, so hang out at base until something changes. Still on autopilot, but resting.
"ST_DOCKING" ///< Docking substates are running, wait for them to finish
};
#endif
enum {
REGROUP_SUCCESS_DISTANCE_SQUARED = 225
};
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
AIStateMachine* SupplyTruckAIUpdate::makeStateMachine()
{
return newInstance(AIStateMachine)( getObject(), "SupplyTruckAIUpdateMachine");
}
//-------------------------------------------------------------------------------------------------
SupplyTruckAIUpdate::SupplyTruckAIUpdate( Thing *thing, const ModuleData* moduleData ) : AIUpdateInterface( thing, moduleData )
{
m_supplyTruckStateMachine = nullptr;
m_preferredDock = INVALID_ID;
m_numberBoxes = 0;
m_forcePending = FALSE;
m_forcedBusyPending = FALSE;
m_supplyTruckStateMachine = newInstance(SupplyTruckStateMachine)( getObject() );
m_supplyTruckStateMachine->initDefaultState();
m_suppliesDepletedVoice = getSupplyTruckAIUpdateModuleData()->m_suppliesDepletedVoice;
}
//-------------------------------------------------------------------------------------------------
SupplyTruckAIUpdate::~SupplyTruckAIUpdate()
{
if (m_supplyTruckStateMachine)
m_supplyTruckStateMachine->halt();
deleteInstance(m_supplyTruckStateMachine);
}
//-------------------------------------------------------------------------------------------------
UpdateSleepTime SupplyTruckAIUpdate::update()
{
StateReturnType stRet = m_supplyTruckStateMachine->updateStateMachine();
UpdateSleepTime mine = IS_STATE_SLEEP(stRet) ? UPDATE_SLEEP(GET_STATE_SLEEP_FRAMES(stRet)) : UPDATE_SLEEP_NONE;
// extend
UpdateSleepTime ret = AIUpdateInterface::update();
return (mine < ret) ? mine : ret;
}
//-------------------------------------------------------------------------------------------------
Bool SupplyTruckAIUpdate::isCurrentlyFerryingSupplies() const
{
if (m_supplyTruckStateMachine)
{
switch (m_supplyTruckStateMachine->getCurrentStateID())
{
case ST_IDLE:
case ST_BUSY:
case ST_REGROUPING:
return false;
case ST_WANTING:
case ST_DOCKING:
return true;
}
}
return false;
}
//-------------------------------------------------------------------------------------------------
Bool SupplyTruckAIUpdate::isAvailableForSupplying() const
{
return true;
}
//-------------------------------------------------------------------------------------------------
Bool SupplyTruckAIUpdate::loseOneBox()
{
if( m_numberBoxes == 0 )
return FALSE;
--m_numberBoxes;
Drawable *draw = getObject()->getDrawable();
if( draw )
{
draw->updateDrawableSupplyStatus( getSupplyTruckAIUpdateModuleData()->m_maxBoxesData, m_numberBoxes );
}
return TRUE;
}
//-------------------------------------------------------------------------------------------------
Bool SupplyTruckAIUpdate::gainOneBox( Int remainingStock )
{
if( getSupplyTruckAIUpdateModuleData() && m_numberBoxes >= getSupplyTruckAIUpdateModuleData()->m_maxBoxesData )
return FALSE;
++m_numberBoxes;
//if I just took the last box,
//i will announce that this supply source is now empty
if (remainingStock == 0)
{
Object* bestWarehouse = getObject()->getControllingPlayer()->getResourceGatheringManager()->findBestSupplyWarehouse( getObject() );
Bool playDepleted = FALSE;
if ( bestWarehouse )
{
//figure out whether the best one is considerably far from the previous one (current position)
Coord3D delta = *getObject()->getPosition();
delta.sub( bestWarehouse->getPosition() );
if ( delta.length() > getWarehouseScanDistance()/4)
playDepleted = TRUE;
}
else
playDepleted = TRUE;
if (playDepleted && m_suppliesDepletedVoice.getEventName().isEmpty() == false)
{
m_suppliesDepletedVoice.setObjectID(getObject()->getID());
m_suppliesDepletedVoice.setPlayingHandle(TheAudio->addAudioEvent(&m_suppliesDepletedVoice));
}
}
Drawable *draw = getObject()->getDrawable();
if( draw )
{
draw->updateDrawableSupplyStatus( getSupplyTruckAIUpdateModuleData()->m_maxBoxesData, m_numberBoxes );
}
return TRUE;
}
//----------------------------------------------------------------------------------------
void SupplyTruckAIUpdate::privateIdle(CommandSourceType cmdSource)
{
// If the user gives a stop command, I have to turn off autopilot
if( cmdSource == CMD_FROM_PLAYER )
setForceBusyState(TRUE);
AIUpdateInterface::privateIdle(cmdSource);
}
//----------------------------------------------------------------------------------------
void SupplyTruckAIUpdate::privateDock( Object *dock, CommandSourceType cmdSource )
{
AIUpdateInterface::privateDock( dock, cmdSource );
// If this is a command from a player, I will remember this as my favorite dock to override
// ResourceManager searches.
if ((cmdSource == CMD_FROM_PLAYER) && dock)
{
// Please note, there is not a separate Warehouse and Center memory by Design. Because
// we lack a UI way to click Warehouse and drag to center to set up a specific path, the
// practical realization has been made that you do not want separate memory.
m_preferredDock = dock->getID();
}
}
//----------------------------------------------------------------------------------------
UnsignedInt SupplyTruckAIUpdate::getActionDelayForDock( Object *dock )
{
// Decide whether to use my Center or Warehouse delay time
static const NameKeyType key_warehouseUpdate = NAMEKEY("SupplyWarehouseDockUpdate");
SupplyWarehouseDockUpdate *warehouseModule = (SupplyWarehouseDockUpdate*) dock->findUpdateModule( key_warehouseUpdate );
if (warehouseModule) {
return getSupplyTruckAIUpdateModuleData()->m_warehouseDelay;
}
static const NameKeyType key_centerUpdate = NAMEKEY("SupplyCenterDockUpdate");
SupplyCenterDockUpdate *centerModule = (SupplyCenterDockUpdate*) dock->findUpdateModule( key_centerUpdate );
if (centerModule) {
return getSupplyTruckAIUpdateModuleData()->m_centerDelay;
}
return 0;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
Real SupplyTruckAIUpdate::getWarehouseScanDistance() const
{
// Ai players get larger scan range. jba.
if (getObject()->getControllingPlayer()->getPlayerType() == PLAYER_COMPUTER) {
return 2 * getSupplyTruckAIUpdateModuleData()->m_warehouseScanDistance;
}
return getSupplyTruckAIUpdateModuleData()->m_warehouseScanDistance;
}
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void SupplyTruckAIUpdate::crc( Xfer *xfer )
{
// extend base class
AIUpdateInterface::crc(xfer);
}
// ------------------------------------------------------------------------------------------------
/** Xfer method
* Version Info:
* 1: Initial version */
// ------------------------------------------------------------------------------------------------
void SupplyTruckAIUpdate::xfer( Xfer *xfer )
{
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
// extend base class
AIUpdateInterface::xfer(xfer);
xfer->xferSnapshot(m_supplyTruckStateMachine);
xfer->xferObjectID(&m_preferredDock);
xfer->xferInt(&m_numberBoxes);
xfer->xferBool(&m_forcePending);
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void SupplyTruckAIUpdate::loadPostProcess()
{
// extend base class
AIUpdateInterface::loadPostProcess();
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
class SupplyTruckBusyState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(SupplyTruckBusyState, "SupplyTruckBusyState")
protected:
// snapshot interface STUBBED.
virtual void crc( Xfer *xfer ) override {};
virtual void xfer( Xfer *xfer ) override {};
virtual void loadPostProcess() override {};
public:
SupplyTruckBusyState( StateMachine *machine ) : State( machine, "SupplyTruckBusyState" ) { }
virtual StateReturnType onEnter() override
{
if( getMachineOwner() && getMachineOwner()->getAI() )
{
// Have to check, since constructor sets a state. Phhbbt. Constructor = set up, init = do first thing.
SupplyTruckAIInterface *update = getMachineOwner()->getAI()->getSupplyTruckAIInterface();
if( update )
{
// Turn off the Busy latch when we make it to Busy
update->setForceBusyState( FALSE );
}
}
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("entering busy state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return STATE_CONTINUE;
}
virtual StateReturnType update() override
{
return STATE_CONTINUE;
}
virtual void onExit(StateExitType status) override
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("exiting busy state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
}
};
EMPTY_DTOR(SupplyTruckBusyState)
//-----------------------------------------------------------------------------------------------------------
class SupplyTruckIdleState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(SupplyTruckIdleState, "SupplyTruckIdleState")
protected:
// snapshot interface STUBBED.
virtual void crc( Xfer *xfer ) override {};
virtual void xfer( Xfer *xfer ) override {};
virtual void loadPostProcess() override {};
public:
SupplyTruckIdleState( StateMachine *machine ) : State( machine, "SupplyTruckIdleState" ) { }
virtual StateReturnType onEnter() override;
virtual StateReturnType update() override
{
return STATE_CONTINUE;
}
virtual void onExit(StateExitType status) override
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("exiting idle state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
}
};
EMPTY_DTOR(SupplyTruckIdleState)
StateReturnType SupplyTruckIdleState::onEnter()
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("entering idle state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
Object *owner = getMachineOwner();
if (owner != nullptr) {
AIUpdateInterface * ownerAI = owner->getAIUpdateInterface();
if (ownerAI != nullptr) {
// This is to get idle workers to always show up on the
// "idle worker button."
// Basically if you have a worker interface, and we are entering
// the idle state for the supply truck, we let the worker interface
// know so it can decide which idle state it wants us to actually
// be in from its perspective.
WorkerAIInterface *workerAI = ownerAI->getWorkerAIInterface();
if (workerAI != nullptr) {
workerAI->exitingSupplyTruckState();
}
}
}
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
SupplyTruckStateMachine::SupplyTruckStateMachine( Object *owner ) : StateMachine( owner, "SupplyTruckStateMachine" )
{
static const StateConditionInfo busyConditions[] =
{
StateConditionInfo(ownerIdle, ST_IDLE, nullptr),
StateConditionInfo(ownerDocking, ST_DOCKING, nullptr),
StateConditionInfo(nullptr, INVALID_STATE_ID, nullptr)
};
static const StateConditionInfo idleConditions[] =
{
StateConditionInfo(isForcedIntoBusyState, ST_BUSY, nullptr),
StateConditionInfo(isForcedIntoWantingState, ST_WANTING, nullptr),
StateConditionInfo(ownerDocking, ST_DOCKING, nullptr),
StateConditionInfo(ownerNotDockingOrIdle, ST_BUSY, nullptr),
StateConditionInfo(nullptr, INVALID_STATE_ID, nullptr)
};
static const StateConditionInfo wantingConditions[] =
{
StateConditionInfo(ownerDocking, ST_DOCKING, nullptr),
StateConditionInfo(ownerNotDockingOrIdle, ST_BUSY, nullptr),
StateConditionInfo(nullptr, INVALID_STATE_ID, nullptr)
};
static const StateConditionInfo regroupingConditions[] =
{
StateConditionInfo(ownerPlayerCommanded, ST_BUSY, nullptr),
StateConditionInfo(nullptr, INVALID_STATE_ID, nullptr)
};
static const StateConditionInfo dockingConditions[] =
{
StateConditionInfo(isForcedIntoBusyState, ST_BUSY, nullptr),
StateConditionInfo(ownerAvailableForSupplying, ST_WANTING, nullptr),
StateConditionInfo(ownerNotDockingOrIdle, ST_BUSY, nullptr),
StateConditionInfo(nullptr, INVALID_STATE_ID, nullptr)
};
// order matters: first state is the default state.
defineState( ST_BUSY, newInstance(SupplyTruckBusyState)( this ), ST_BUSY, ST_BUSY, busyConditions );
defineState( ST_IDLE, newInstance(SupplyTruckIdleState)( this ), ST_BUSY, ST_BUSY, idleConditions );
defineState( ST_WANTING, newInstance(SupplyTruckWantsToPickUpOrDeliverBoxesState)( this ), ST_BUSY, ST_REGROUPING, wantingConditions );
defineState( ST_REGROUPING, newInstance(RegroupingState)( this ), ST_WANTING, ST_BUSY, regroupingConditions );
defineState( ST_DOCKING, newInstance(DockingState)( this ), ST_BUSY, ST_BUSY, dockingConditions );
}
//-------------------------------------------------------------------------------------------------
SupplyTruckStateMachine::~SupplyTruckStateMachine()
{
}
// ------------------------------------------------------------------------------------------------
/** CRC */
// ------------------------------------------------------------------------------------------------
void SupplyTruckStateMachine::crc( Xfer *xfer )
{
StateMachine::crc(xfer);
}
// ------------------------------------------------------------------------------------------------
/** Xfer Method */
// ------------------------------------------------------------------------------------------------
void SupplyTruckStateMachine::xfer( Xfer *xfer )
{
XferVersion cv = 1;
XferVersion v = cv;
xfer->xferVersion( &v, cv );
StateMachine::xfer(xfer);
}
// ------------------------------------------------------------------------------------------------
/** Load post process */
// ------------------------------------------------------------------------------------------------
void SupplyTruckStateMachine::loadPostProcess()
{
StateMachine::loadPostProcess();
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
StateReturnType SupplyTruckWantsToPickUpOrDeliverBoxesState::onEnter()
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("entering wanting state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
Object *owner = getMachineOwner();
SupplyTruckAIInterface *update = owner->getAIUpdateInterface()->getSupplyTruckAIInterface();
if( !update )
{
return STATE_FAILURE;
}
// We can only force Wanting for one go at Wanting. It could fail and we would be stuck forever.
// So a force means one try at the wanting state, then back to normal.
update->setForceWantingState(false);
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
void SupplyTruckWantsToPickUpOrDeliverBoxesState::onExit(StateExitType status)
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("exiting wanting state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
}
//-------------------------------------------------------------------------------------------------
StateReturnType SupplyTruckWantsToPickUpOrDeliverBoxesState::update()
{
Object* owner = getMachineOwner();
AIUpdateInterface* ownerAI = owner->getAIUpdateInterface();
if (!ownerAI)
return STATE_FAILURE;
Player* ownerPlayer = owner->getControllingPlayer();
ResourceGatheringManager *manager = ownerPlayer->getResourceGatheringManager();
SupplyTruckAIInterface* update = ownerAI->getSupplyTruckAIInterface();
if (!update)
return STATE_FAILURE;
if (!update->isAvailableForSupplying())
{
DEBUG_CRASH(("SupplyTruckWantsToPickUpOrDeliverBoxesState: isAvailableForSupplying==false; should not get here"));
return STATE_FAILURE;
}
Int numBoxes = update->getNumberBoxes();
if (numBoxes > 0)
{
// want a center.
Object *bestCenter = manager->findBestSupplyCenter( owner );
if( bestCenter )
{
ownerAI->aiDock( bestCenter, CMD_FROM_AI );
return STATE_SUCCESS;
}
}
else
{
// want a warehouse.
Object* bestWarehouse = manager->findBestSupplyWarehouse( owner );
if( bestWarehouse )
{
ownerAI->aiDock( bestWarehouse, CMD_FROM_AI );
return STATE_SUCCESS;
}
}
return STATE_FAILURE;// we aren't going to wait right here, we will go back to base and wait for
// wanting to succeed some place safe.
}
//-------------------------------------------------------------------------------------------------
StateReturnType RegroupingState::onEnter()
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("entering regrouping state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
// I have failed to find a dock, so my first choice is to go hang out at a Supply Center (I may have
// failed to find a Warehouse). My second choices is to go to a ConYard. My last choice is just to
// go to a friendly building.
Object* owner = getMachineOwner();
AIUpdateInterface* ownerAI = owner->getAIUpdateInterface();
Player* ownerPlayer = owner->getControllingPlayer();
if( !ownerPlayer || !ownerAI )
return STATE_FAILURE;
ownerAI->ignoreObstacle( nullptr );
SupplyTruckAIInterface *update = owner->getAIUpdateInterface()->getSupplyTruckAIInterface();
if( !update )
{
return STATE_FAILURE;
}
Object *destinationObject = nullptr;
KindOfMaskType kindof;
KindOfMaskType kindofnot;
kindof.set(KINDOF_CASH_GENERATOR);
kindofnot.clear();
// can't do best supply center of the player's resource brain, because that adds canTransfer checks.
destinationObject = ownerPlayer->findClosestByKindOf( owner, kindof, kindofnot );
if( !destinationObject )
{
kindof.clear();
kindof.set(KINDOF_COMMANDCENTER);
kindofnot.clear();
destinationObject = ownerPlayer->findClosestByKindOf( owner, kindof, kindofnot );
}
if( !destinationObject )
{
kindof.clear();
kindof.set( KINDOF_STRUCTURE );
kindofnot.clear();
destinationObject = ownerPlayer->findClosestByKindOf( owner, kindof, kindofnot );
}
if( !destinationObject )
{
return STATE_FAILURE;
}
if( ThePartitionManager->getDistanceSquared(owner, destinationObject, FROM_BOUNDINGSPHERE_2D) < REGROUP_SUCCESS_DISTANCE_SQUARED )
return STATE_CONTINUE; // Don't say Success so we don't spin the machine. After one update we'll go back.
Coord3D destination;
FindPositionOptions fpOptions;
fpOptions.minRadius = 0.0f;
fpOptions.maxRadius = 100.0f;
if( ! ThePartitionManager->findPositionAround( destinationObject->getPosition(), &fpOptions, &destination ) )
return STATE_FAILURE;
ownerAI->aiMoveToPosition( &destination, CMD_FROM_AI );
return STATE_CONTINUE;// Remember to say continue when you change ai command inside a state
}
//-------------------------------------------------------------------------------------------------
StateReturnType RegroupingState::update()
{
Object *owner = getMachineOwner();
if( owner->getAI()->isIdle() )
return STATE_SUCCESS; // Once we have regrouped, chill with the regrouping. We will succeed to Wanting, which can handle Busy.
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
void RegroupingState::onExit(StateExitType status)
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("exiting regroup state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
}
//-------------------------------------------------------------------------------------------------
StateReturnType DockingState::onEnter()
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("entering docking state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
Object *owner = getMachineOwner();
SupplyTruckAIInterface *update = owner->getAIUpdateInterface()->getSupplyTruckAIInterface();
if( !update )
{
return STATE_FAILURE;
}
// after we dock the first time, we clear this, and then follow our normal state machine path
update->setForceWantingState(false);
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
StateReturnType DockingState::update()
{
return STATE_CONTINUE;
}
//-------------------------------------------------------------------------------------------------
void DockingState::onExit(StateExitType status)
{
#ifdef DEBUG_SUPPLY_STATE
TheInGameUI->DEBUG_addFloatingText("exiting docking state", getMachineOwner()->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
/* static */ Bool SupplyTruckStateMachine::isForcedIntoWantingState( State *thisState, void* userData )
{
Object *owner = thisState->getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
if( !ai )
return false;
SupplyTruckAIInterface *update = ai->getSupplyTruckAIInterface();
if( !update )
return false;
if (update->isForcedIntoWantingState())
{
#ifdef DEBUG_SUPPLY_STATE
AsciiString tmp;
tmp.format("isForcedIntoWantingState returns true (%s)",statenames[thisState->getID()]);
TheInGameUI->DEBUG_addFloatingText(tmp, owner->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return true;
}
return false;
}
//-------------------------------------------------------------------------------------------------
/* static */ Bool SupplyTruckStateMachine::isForcedIntoBusyState( State *thisState, void* userData )
{
Object *owner = thisState->getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
if( !ai )
return false;
SupplyTruckAIInterface *update = ai->getSupplyTruckAIInterface();
if( !update )
return false;
if (update->isForcedIntoBusyState())
{
#ifdef DEBUG_SUPPLY_STATE
AsciiString tmp;
tmp.format("isForcedIntoBusytate returns true (%s)",statenames[thisState->getID()]);
TheInGameUI->DEBUG_addFloatingText(tmp, owner->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return true;
}
return false;
}
//-------------------------------------------------------------------------------------------------
/* static */ Bool SupplyTruckStateMachine::ownerDocking( State *thisState, void* userData )
{
Object *owner = thisState->getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
if( !ai )
return false;
AIStateType masterState = ai->getAIStateType();
if (masterState == AI_DOCK)
{
#ifdef DEBUG_SUPPLY_STATE
AsciiString tmp;
tmp.format("ownerDocking returns true (%s)",statenames[thisState->getID()]);
TheInGameUI->DEBUG_addFloatingText(tmp, owner->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return true;
}
return false;
}
//-------------------------------------------------------------------------------------------------
/* static */ Bool SupplyTruckStateMachine::ownerPlayerCommanded( State *thisState, void* userData )
{
Object *owner = thisState->getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
if( !ai )
return false;
if( ai->getLastCommandSource() == CMD_FROM_PLAYER )
{
#ifdef DEBUG_SUPPLY_STATE
AsciiString tmp;
tmp.format("ownerPlayerCommanded returns true (%s)",statenames[thisState->getID()]);
TheInGameUI->DEBUG_addFloatingText(tmp, owner->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return true;
}
return false;
}
//-------------------------------------------------------------------------------------------------
/* static */ Bool SupplyTruckStateMachine::ownerIdle( State *thisState, void* userData )
{
Object *owner = thisState->getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
if( !ai )
return false;
if (ai->isIdle())
{
#ifdef DEBUG_SUPPLY_STATE
AsciiString tmp;
tmp.format("ownerIdle returns true (%s)",statenames[thisState->getID()]);
TheInGameUI->DEBUG_addFloatingText(tmp, owner->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return true;
}
return false;
}
//-------------------------------------------------------------------------------------------------
/* static */ Bool SupplyTruckStateMachine::ownerAvailableForSupplying( State *thisState, void* userData )
{
Object *owner = thisState->getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
if( !ai )
return false;
SupplyTruckAIInterface *update = ai->getSupplyTruckAIInterface();
if( !update )
return false;
if (update->isAvailableForSupplying() && ai->isIdle())
{
#ifdef DEBUG_SUPPLY_STATE
AsciiString tmp;
tmp.format("ownerAvailableForSupplying returns true (%s)",statenames[thisState->getID()]);
TheInGameUI->DEBUG_addFloatingText(tmp, owner->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return true;
}
return false;
}
//-------------------------------------------------------------------------------------------------
/* static */ Bool SupplyTruckStateMachine::ownerNotDockingOrIdle( State *thisState, void* userData )
{
Object *owner = thisState->getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
if( !ai )
return false;
if (!ai->isIdle() && ai->getAIStateType() != AI_DOCK)
{
#ifdef DEBUG_SUPPLY_STATE
AsciiString tmp;
tmp.format("ownerNotDockingOrIdle returns true (%s)",statenames[thisState->getID()]);
TheInGameUI->DEBUG_addFloatingText(tmp, owner->getPosition(), GameMakeColor(255, 0, 0, 255));
#endif
return true;
}
return false;
}