forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathChinookAIUpdate.cpp
More file actions
1287 lines (1096 loc) · 40.6 KB
/
ChinookAIUpdate.cpp
File metadata and controls
1287 lines (1096 loc) · 40.6 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. //
// //
////////////////////////////////////////////////////////////////////////////////
// ChinookAIUpdate.cpp //////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#define DEFINE_VETERANCY_NAMES // for TheVeterancyNames[]
#include "Common/ActionManager.h"
#include "Common/DrawModule.h"
#include "Common/GameState.h"
#include "Common/GameUtility.h"
#include "Common/GlobalData.h"
#include "Common/RandomValue.h"
#include "Common/Team.h"
#include "Common/ThingFactory.h"
#include "Common/ThingTemplate.h"
#include "Common/Xfer.h"
#include "GameClient/Drawable.h"
#include "GameClient/GameClient.h"
#include "GameLogic/AIPathfind.h"
#include "GameLogic/Locomotor.h"
#include "GameLogic/Module/ContainModule.h"
#include "GameLogic/Module/ChinookAIUpdate.h"
#include "GameLogic/Module/PhysicsUpdate.h"
#include "GameLogic/PartitionManager.h"
const Real BIGNUM = 99999.0f;
//-------------------------------------------------------------------------------------------------
enum ChinookAIStateType CPP_11(: Int)
{
// note that these must be distinct (numerically) from AIStateType. ick.
ChinookAIStateType_FIRST = 1000,
TAKING_OFF,
LANDING,
MOVE_TO_AND_LAND,
MOVE_TO_AND_EVAC,
LAND_AND_EVAC,
EVAC_AND_TAKEOFF,
MOVE_TO_AND_EVAC_AND_EXIT,
LAND_AND_EVAC_AND_EXIT,
EVAC_AND_EXIT,
TAKEOFF_AND_EXIT,
HEAD_OFF_MAP,
MOVE_TO_COMBAT_DROP,
DO_COMBAT_DROP
};
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
static Real calcDistSqr(const Coord3D& a, const Coord3D& b)
{
return sqr(a.x-b.x) + sqr(a.y-b.y) + sqr(a.z-b.z);
}
//-------------------------------------------------------------------------------------------------
static Object* getPotentialRappeller(Object* obj)
{
const ContainedItemsList* items = obj->getContain() ? obj->getContain()->getContainedItemsList() : nullptr;
if (items)
{
for (ContainedItemsList::const_iterator it = items->begin(); it != items->end(); ++it )
{
Object* rider = *it;
if (rider->isKindOf(KINDOF_CAN_RAPPEL))
{
return rider;
}
}
}
return nullptr;
}
//----------------------------------------------------------------------------------------------------------
class ChinookEvacuateState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookEvacuateState, "ChinookEvacuateState")
protected:
// snapshot interface STUBBED - no member vars to save. jba.
virtual void crc( Xfer *xfer ){};
virtual void xfer( Xfer *xfer ){};
virtual void loadPostProcess(){};
public:
ChinookEvacuateState( StateMachine *machine ) : State( machine, "ChinookEvacuateState" ) { }
StateReturnType onEnter()
{
Object* obj = getMachineOwner();
if( obj->getContain() )
{
obj->getContain()->removeAllContained(FALSE);
}
obj->getTeam()->setActive(); // why? I don't know.
return STATE_SUCCESS;
}
virtual StateReturnType update()
{
return STATE_SUCCESS;
}
};
EMPTY_DTOR(ChinookEvacuateState)
//-------------------------------------------------------------------------------------------------
class ChinookHeadOffMapState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookHeadOffMapState, "ChinookHeadOffMapState")
//I'm outta here
protected:
// snapshot interface STUBBED - no member vars to save. jba.
virtual void crc( Xfer *xfer ){};
virtual void xfer( Xfer *xfer ){};
virtual void loadPostProcess(){};
public:
ChinookHeadOffMapState( StateMachine *machine ) : State( machine, "ChinookHeadOffMapState" ) {}
StateReturnType onEnter() // Give move order out of town
{
Object *owner = getMachineOwner();
AIUpdateInterface *ai = owner->getAIUpdateInterface();
// just keep moving straight ahead till we exit the map.
Coord3D exitCoord = *owner->getPosition();
const Coord3D* dir = owner->getUnitDirectionVector2D();
Region3D terrainExtent;
TheTerrainLogic->getExtent( &terrainExtent );
const Real FUDGE = 1.2f;
Real HUGE_DIST = FUDGE * sqrt(sqr(terrainExtent.hi.x - terrainExtent.lo.x) + sqr(terrainExtent.hi.y - terrainExtent.lo.y));
exitCoord.x += dir->x * HUGE_DIST;
exitCoord.y += dir->y * HUGE_DIST;
ai->aiMoveToPosition( &exitCoord, CMD_FROM_AI );
ai->getCurLocomotor()->setAllowInvalidPosition(true);
return STATE_CONTINUE;
}
StateReturnType update()
{
Object *owner = getMachineOwner();
Region3D mapRegion;
TheTerrainLogic->getExtentIncludingBorder( &mapRegion );
if (!mapRegion.isInRegionNoZ( owner->getPosition() ))
{
TheGameLogic->destroyObject(owner);
return STATE_SUCCESS;
}
return STATE_CONTINUE;
}
};
EMPTY_DTOR(ChinookHeadOffMapState)
//-------------------------------------------------------------------------------------------------
class ChinookTakeoffOrLandingState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookTakeoffOrLandingState, "ChinookTakeoffOrLandingState")
private:
Coord3D m_destLoc;
Bool m_landing;
protected:
// snapshot interface
virtual void crc( Xfer *xfer )
{
// empty
}
virtual void xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferCoord3D(&m_destLoc);
xfer->xferBool(&m_landing);
}
virtual void loadPostProcess()
{
// empty
}
public:
ChinookTakeoffOrLandingState( StateMachine *machine, Bool landing ) : m_landing(landing), State( machine, "ChinookTakeoffOrLandingState" )
{
m_destLoc.zero();
}
virtual StateReturnType onEnter()
{
Object* obj = getMachineOwner();
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
ai->friend_setFlightStatus(m_landing ? CHINOOK_LANDING : CHINOOK_TAKING_OFF);
if( m_landing )
{
// A chinook given transport duty loses his supplies.
while( ai->loseOneBox() );
}
// kill any drift...
obj->getPhysics()->scrubVelocity2D(0);
ai->chooseLocomotorSet(LOCOMOTORSET_NORMAL);
Locomotor* loco = ai->getCurLocomotor();
loco->setUsePreciseZPos(true);
loco->setUltraAccurate(true);
m_destLoc = *obj->getPosition();
const Bool onlyHealthyBridges = true; // ignore dead bridges.
PathfindLayerEnum layerAtDest = TheTerrainLogic->getHighestLayerForDestination(&m_destLoc, onlyHealthyBridges);
m_destLoc.z = TheTerrainLogic->getLayerHeight(m_destLoc.x, m_destLoc.y, layerAtDest);
if (m_landing)
{
Coord3D tmp;
FindPositionOptions options;
options.maxRadius = obj->getGeometryInfo().getBoundingCircleRadius() * 100.0f;
if (ThePartitionManager->findPositionAround(&m_destLoc, &options, &tmp))
{
m_destLoc = tmp;
TheAI->pathfinder()->adjustToLandingDestination(obj, &m_destLoc);
}
// recalc, since it may have changed. note that findPositionAround() will ALWAYS
// return a position on the ground proper, so if our initial search start pos was
// above a bridge, this will put us below the bridge, which would be unfortunate.
// so recheck to be sure. (note that the partitionmgr is 2d-only, so if it sez that
// the position on the ground at that xy is clear, it will be clear for both the
// ground proper and the bridge itself.) also note: don't call objectInteractsWithBridgeLayer(),
// since it assumes that things that aren't close in z shouldn't interact.
tmp = m_destLoc;
tmp.z = obj->getPosition()->z;
layerAtDest = TheTerrainLogic->getHighestLayerForDestination(&tmp, onlyHealthyBridges);
m_destLoc.z = TheTerrainLogic->getLayerHeight(m_destLoc.x, m_destLoc.y, layerAtDest);
obj->setLayer(layerAtDest);
}
else
{
m_destLoc.z += loco->getPreferredHeight();
obj->setLayer(LAYER_GROUND);
}
return STATE_CONTINUE;
}
virtual StateReturnType update()
{
Object* obj = getMachineOwner();
if (obj->isEffectivelyDead())
return STATE_FAILURE;
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
ai->setLocomotorGoalPositionExplicit(m_destLoc);
const Real THRESH = 3.0f;
const Real THRESH_SQR = THRESH*THRESH;
if (calcDistSqr(*obj->getPosition(), m_destLoc) <= THRESH_SQR)
return STATE_SUCCESS;
return STATE_CONTINUE;
}
virtual void onExit( StateExitType status )
{
Object* obj = getMachineOwner();
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
ai->friend_setFlightStatus(m_landing ? CHINOOK_LANDED : CHINOOK_FLYING);
// Paranoia checks - sometimes onExit is called when we are
// shutting down, and not all pieces are valid. CurLocomotor
// is definitely null in some cases. jba.
Locomotor* loco = ai->getCurLocomotor();
if (loco)
{
loco->setUsePreciseZPos(false);
loco->setUltraAccurate(false);
// don't restore lift if dead -- this may fight with JetSlowDeathBehavior!
if (!obj->isEffectivelyDead())
loco->setMaxLift(BIGNUM);
}
if (m_landing)
{
ai->chooseLocomotorSet(LOCOMOTORSET_TAXIING);
}
else
{
// when takeoff is complete, always go back to layer-ground, rather than
// some bridge layer.
obj->setLayer(LAYER_GROUND);
}
}
};
EMPTY_DTOR(ChinookTakeoffOrLandingState)
//-------------------------------------------------------------------------------------------------
class ChinookCombatDropState : public State
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookCombatDropState, "ChinookCombatDropState")
private:
struct RopeInfo
{
Drawable* ropeDrawable;
DrawableID ropeID; // used only during save-load process
Matrix3D dropStartMtx;
Real ropeSpeed;
Real ropeLen;
Real ropeLenMax;
UnsignedInt nextDropTime;
std::list<ObjectID> rappellerIDs;
};
std::vector<RopeInfo> m_ropes;
void removeDoneRappellers()
{
for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
{
for (std::list<ObjectID>::iterator oit = it->rappellerIDs.begin(); oit != it->rappellerIDs.end(); )
{
Object* rappeller = TheGameLogic->findObjectByID(*oit);
#if RETAIL_COMPATIBLE_CRC
if (rappeller == nullptr || rappeller->isEffectivelyDead() || !rappeller->isAboveTerrain())
#else
if (rappeller == nullptr || rappeller->isEffectivelyDead() || !rappeller->isAboveTerrain() || rappeller->isContained())
#endif
{
oit = it->rappellerIDs.erase(oit);
}
else
{
++oit;
}
}
}
}
static void initRopeParms(Drawable* rope, Real length, Real width, const RGBColor& color, Real wobbleLen, Real wobbleAmp, Real wobbleRate)
{
RopeDrawInterface* tdi = nullptr;
for (DrawModule** d = rope->getDrawModules(); *d; ++d)
{
if ((tdi = (*d)->getRopeDrawInterface()) != nullptr)
{
tdi->initRopeParms(length, width, color, wobbleLen, wobbleAmp, wobbleRate);
}
}
}
static void setRopeCurLen(Drawable* rope, Real length)
{
RopeDrawInterface* tdi = nullptr;
for (DrawModule** d = rope->getDrawModules(); *d; ++d)
{
if ((tdi = (*d)->getRopeDrawInterface()) != nullptr)
{
tdi->setRopeCurLen(length);
}
}
}
static void setRopeSpeed(Drawable* rope, Real curSpeed, Real maxSpeed, Real accel)
{
RopeDrawInterface* tdi = nullptr;
for (DrawModule** d = rope->getDrawModules(); *d; ++d)
{
if ((tdi = (*d)->getRopeDrawInterface()) != nullptr)
{
tdi->setRopeSpeed(curSpeed, maxSpeed, accel);
}
}
}
protected:
// snapshot interface
virtual void crc( Xfer *xfer )
{
// empty
}
virtual void xfer( Xfer *xfer )
{
// version
const XferVersion currentVersion = 2;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
Int numRopes = m_ropes.size();
xfer->xferInt(&numRopes);
if (version >= 2)
{
if (xfer->getXferMode() == XFER_LOAD)
{
if (!m_ropes.empty())
{
DEBUG_CRASH(( "ChinookCombatDropState - ropes should be empty" ));
throw SC_INVALID_DATA;
}
m_ropes.resize(numRopes);
}
for (Int i = 0; i < numRopes; ++i)
{
RopeInfo info;
if (xfer->getXferMode() == XFER_SAVE)
{
info = m_ropes[i];
// always overwrite this, since it's probably stale
info.ropeID = info.ropeDrawable ? info.ropeDrawable->getID() : INVALID_DRAWABLE_ID;
}
xfer->xferDrawableID(&info.ropeID);
xfer->xferMatrix3D(&info.dropStartMtx);
xfer->xferReal(&info.ropeSpeed);
xfer->xferReal(&info.ropeLen);
xfer->xferReal(&info.ropeLenMax);
xfer->xferUnsignedInt(&info.nextDropTime);
xfer->xferSTLObjectIDList(&info.rappellerIDs);
if (xfer->getXferMode() == XFER_LOAD)
{
info.ropeDrawable = nullptr; // filled in via loadPostProcess
m_ropes[i] = info;
}
}
}
}
virtual void loadPostProcess()
{
for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
{
it->ropeDrawable = TheGameClient->findDrawableByID(it->ropeID);
// always nuke this, since we're done with it till we save/load again
it->ropeID = INVALID_DRAWABLE_ID;
}
}
public:
ChinookCombatDropState( StateMachine *machine ): State( machine, "ChinookCombatDropState" ) { }
// --------------
virtual StateReturnType onEnter()
{
Object* obj = getMachineOwner();
Drawable* draw = obj->getDrawable();
if (draw == nullptr)
return STATE_FAILURE;
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
const ChinookAIUpdateModuleData* d = ai->friend_getData();
obj->setDisabled( DISABLED_HELD );
ai->friend_setFlightStatus(CHINOOK_DOING_COMBAT_DROP);
// A chinook given combat drop duty also loses his supplies.
while( ai->loseOneBox() );
UnsignedInt now = TheGameLogic->getFrame();
const ThingTemplate* ropeTmpl = TheThingFactory->findTemplate(d->m_ropeName);
const Int MAX_BONES = 32;
Coord3D ropePos[MAX_BONES];
Matrix3D dropMtx[MAX_BONES];
Int ropeCount = draw->getPristineBonePositions("RopeStart", 1, ropePos, nullptr, MAX_BONES);
Int dropCount = draw->getPristineBonePositions("RopeEnd", 1, nullptr, dropMtx, MAX_BONES);
Int numRopes = d->m_numRopes;
if (numRopes > ropeCount) numRopes = ropeCount;
if (numRopes > dropCount) numRopes = dropCount;
if (numRopes <= 0)
return STATE_FAILURE;
m_ropes.clear();
for (Int i = 0; i < numRopes; ++i)
{
RopeInfo info;
obj->convertBonePosToWorldPos( nullptr, &dropMtx[i], nullptr, &info.dropStartMtx );
info.ropeDrawable = ropeTmpl ? TheThingFactory->newDrawable(ropeTmpl) : nullptr;
if (info.ropeDrawable)
{
obj->convertBonePosToWorldPos( &ropePos[i], nullptr, &ropePos[i], nullptr );
info.ropeDrawable->setPosition(&ropePos[i]);
info.ropeSpeed = 0.0f;
info.ropeLen = 1.0f;
const Bool onlyHealthyBridges = true; // ignore dead bridges.
PathfindLayerEnum layerAtDest = TheTerrainLogic->getHighestLayerForDestination(&ropePos[i], onlyHealthyBridges);
info.ropeLenMax = ropePos[i].z - TheTerrainLogic->getLayerHeight(ropePos[i].x, ropePos[i].y, layerAtDest) - d->m_ropeFinalHeight;
initRopeParms(info.ropeDrawable, info.ropeLenMax, d->m_ropeWidth, d->m_ropeColor, d->m_ropeWobbleLen, d->m_ropeWobbleAmp, d->m_ropeWobbleRate);
}
info.nextDropTime = now + GameLogicRandomValue(d->m_perRopeDelayMin, d->m_perRopeDelayMax) - d->m_perRopeDelayMin;
info.rappellerIDs.clear();
m_ropes.push_back(info);
}
return STATE_CONTINUE;
}
// --------------
virtual StateReturnType update()
{
Object* obj = getMachineOwner();
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
const ChinookAIUpdateModuleData* d = ai->friend_getData();
if (obj->isEffectivelyDead())
{
return STATE_FAILURE;
}
// first, eliminate "done" rappellers
removeDoneRappellers();
UnsignedInt now = TheGameLogic->getFrame();
// ok, now check each rope: if it's empty, or we're at the next drop time, spawn a new rappeller
Int numRopesInUse = 0;
for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
{
if (it->ropeLen < it->ropeLenMax)
{
it->ropeSpeed += fabs(TheGlobalData->m_gravity);
if (it->ropeSpeed > d->m_ropeDropSpeed)
it->ropeSpeed = d->m_ropeDropSpeed;
it->ropeLen += it->ropeSpeed;
setRopeCurLen(it->ropeDrawable, it->ropeLen);
if (d->m_waitForRopesToDrop)
{
// can't use this rope till it's dropped all the way
++it->nextDropTime;
continue;
}
}
if (now >= it->nextDropTime)
{
Object* rappeller = getPotentialRappeller(obj);
if (rappeller != nullptr)
{
#if RETAIL_COMPATIBLE_CRC
ExitInterface *exitInterface = obj->getObjectExitInterface();
ExitDoorType exitDoor = exitInterface ? exitInterface->reserveDoorForExit(rappeller->getTemplate(), rappeller) : DOOR_NONE_AVAILABLE;
if(exitDoor != DOOR_NONE_AVAILABLE)
{
exitInterface->exitObjectViaDoor(rappeller, exitDoor);
}
else
{
DEBUG_CRASH(("rappeller is not free to exit... what?"));
}
#else
// TheSuperHackers @bugfix 03/01/2026 Bypass door reservation as rappellers are always
// expected to be free to exit. This avoids prior rappel conditions in getAiFreeToExit
// from allowing rappellers to be freely 'dropped' from the Chinook during this state.
if (ExitInterface* exitInterface = obj->getObjectExitInterface())
{
exitInterface->exitObjectViaDoor(rappeller, DOOR_1);
}
#endif
rappeller->setTransformMatrix(&it->dropStartMtx);
AIUpdateInterface* rappellerAI = rappeller ? rappeller->getAIUpdateInterface() : nullptr;
if (rappellerAI)
{
rappellerAI->setDesiredSpeed(d->m_rappelSpeed);
rappellerAI->aiRappelInto(getMachineGoalObject(), *getMachineGoalPosition(), CMD_FROM_AI);
}
it->rappellerIDs.push_back(rappeller->getID());
it->nextDropTime = now + GameLogicRandomValue(d->m_perRopeDelayMin, d->m_perRopeDelayMax);
}
}
if (!it->rappellerIDs.empty())
{
++numRopesInUse;
}
}
if (numRopesInUse == 0 && getPotentialRappeller(obj) == nullptr)
{
// we're done!
return STATE_SUCCESS;
}
return STATE_CONTINUE;
}
// --------------
virtual void onExit( StateExitType status )
{
Object* obj = getMachineOwner();
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
const ChinookAIUpdateModuleData* d = ai->friend_getData();
obj->clearDisabled( DISABLED_HELD );
ai->friend_setFlightStatus(CHINOOK_FLYING);
if (obj->isEffectivelyDead())
{
// oops. drop the rangers.
for (std::vector<RopeInfo>::iterator it = m_ropes.begin(); it != m_ropes.end(); ++it)
{
for (std::list<ObjectID>::iterator oit = it->rappellerIDs.begin(); oit != it->rappellerIDs.end(); ++oit)
{
Object* rappeller = TheGameLogic->findObjectByID(*oit);
AIUpdateInterface* rappellerAI = rappeller ? rappeller->getAIUpdateInterface() : nullptr;
if (rappellerAI != nullptr)
{
rappellerAI->aiIdle(CMD_FROM_AI);
}
}
}
}
UnsignedInt now = TheGameLogic->getFrame();
for (size_t i = 0; i < m_ropes.size(); ++i)
{
if (m_ropes[i].ropeDrawable)
{
const UnsignedInt ROPE_EXPIRATION_TIME = LOGICFRAMES_PER_SECOND * 5;
const Real initialSpeed = TheGlobalData->m_gravity * 30; // give it a little kick
setRopeSpeed(m_ropes[i].ropeDrawable, initialSpeed, d->m_ropeDropSpeed, TheGlobalData->m_gravity);
m_ropes[i].ropeDrawable->setExpirationDate(now + ROPE_EXPIRATION_TIME);
m_ropes[i].ropeDrawable = nullptr; // we're done with it, so null it so we won't save it
}
}
m_ropes.clear();
}
};
EMPTY_DTOR(ChinookCombatDropState)
//-----------------------------------------------------------------------------------------------------------
/**
* Move to the GoalPosition, or GoalObject.
*/
class ChinookMoveToBldgState : public AIMoveToState
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ChinookMoveToBldgState, "ChinookMoveToBldgState")
private:
Real m_oldPreferredHeight;
Real m_newPreferredHeight;
Real m_destZ;
protected:
// snapshot interface
virtual void crc( Xfer *xfer )
{
// empty
}
virtual void xfer( Xfer *xfer )
{
// version
XferVersion currentVersion = 1;
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
xfer->xferReal(&m_oldPreferredHeight);
xfer->xferReal(&m_newPreferredHeight);
xfer->xferReal(&m_destZ);
}
virtual void loadPostProcess()
{
// empty
}
public:
ChinookMoveToBldgState( StateMachine *machine ): AIMoveToState( machine ) { }
virtual StateReturnType onEnter()
{
Object* obj = getMachineOwner();
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
const ChinookAIUpdateModuleData* d = ai->friend_getData();
Locomotor* loco = ai->getCurLocomotor();
loco->setUltraAccurate(true);
m_oldPreferredHeight = loco->getPreferredHeight();
m_newPreferredHeight = m_oldPreferredHeight;
const Coord3D* destPos;
Object* bldg = getMachineGoalObject();
if (bldg != nullptr && !bldg->isEffectivelyDead() && bldg->isKindOf(KINDOF_STRUCTURE))
{
destPos = bldg->getPosition();
m_newPreferredHeight = bldg->getGeometryInfo().getMaxHeightAbovePosition() + d->m_minDropHeight;
if (m_newPreferredHeight < m_oldPreferredHeight)
m_newPreferredHeight = m_oldPreferredHeight;
}
else
{
destPos = getMachineGoalPosition();
}
loco->setPreferredHeight(m_newPreferredHeight);
m_destZ = TheTerrainLogic->getGroundHeight(destPos->x, destPos->y) + m_newPreferredHeight;
return AIMoveToState::onEnter();
}
virtual StateReturnType update()
{
Object* obj = getMachineOwner();
// the normal moveto state will bail when 2d pos matches; we need z, too
StateReturnType status = AIMoveToState::update();
const Real THRESH = 3.0f;
if (status != STATE_CONTINUE && fabs(obj->getPosition()->z - m_destZ) > THRESH)
status = STATE_CONTINUE;
return status;
}
virtual void onExit( StateExitType status )
{
Object* obj = getMachineOwner();
ChinookAIUpdate* ai = (ChinookAIUpdate*)obj->getAIUpdateInterface();
Locomotor* loco = ai->getCurLocomotor();
loco->setPreferredHeight(m_oldPreferredHeight);
loco->setUltraAccurate(false);
AIMoveToState::onExit(status);
}
};
EMPTY_DTOR(ChinookMoveToBldgState)
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class ChinookAIStateMachine : public AIStateMachine
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( ChinookAIStateMachine, "ChinookAIStateMachine" );
public:
ChinookAIStateMachine( Object *owner, AsciiString name );
};
//-------------------------------------------------------------------------------------------------
ChinookAIStateMachine::ChinookAIStateMachine(Object *owner, AsciiString name) : AIStateMachine(owner, name)
{
defineState( TAKING_OFF, newInstance(ChinookTakeoffOrLandingState)( this, false ), AI_IDLE, AI_IDLE );
defineState( LANDING, newInstance(ChinookTakeoffOrLandingState)( this, true ), AI_IDLE, AI_IDLE );
defineState( MOVE_TO_COMBAT_DROP, newInstance(ChinookMoveToBldgState)( this ), DO_COMBAT_DROP, AI_IDLE );
defineState( DO_COMBAT_DROP, newInstance(ChinookCombatDropState)( this ), AI_IDLE, AI_IDLE );
defineState( MOVE_TO_AND_LAND, newInstance(AIMoveToState)( this ), LANDING, AI_IDLE );
defineState( MOVE_TO_AND_EVAC, newInstance(AIMoveToState)( this ), LAND_AND_EVAC, AI_IDLE );
defineState( LAND_AND_EVAC, newInstance(ChinookTakeoffOrLandingState)( this, true ), EVAC_AND_TAKEOFF, AI_IDLE );
defineState( EVAC_AND_TAKEOFF, newInstance(ChinookEvacuateState)( this ), TAKING_OFF, AI_IDLE );
defineState( MOVE_TO_AND_EVAC_AND_EXIT, newInstance(AIMoveToState)( this ), LAND_AND_EVAC_AND_EXIT, AI_IDLE );
defineState( LAND_AND_EVAC_AND_EXIT, newInstance(ChinookTakeoffOrLandingState)( this, true ), EVAC_AND_EXIT, AI_IDLE );
defineState( EVAC_AND_EXIT, newInstance(ChinookEvacuateState)( this ), TAKEOFF_AND_EXIT, AI_IDLE );
defineState( TAKEOFF_AND_EXIT, newInstance(ChinookTakeoffOrLandingState)( this, false ), HEAD_OFF_MAP, AI_IDLE );
defineState( HEAD_OFF_MAP, newInstance(ChinookHeadOffMapState)( this ), AI_IDLE, AI_IDLE );
}
//-------------------------------------------------------------------------------------------------
ChinookAIStateMachine::~ChinookAIStateMachine()
{
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ChinookAIUpdateModuleData::ChinookAIUpdateModuleData()
{
m_numRopes = 4;
m_ropeWidth = 0.5f;
m_ropeColor.red = 0.9f;
m_ropeColor.green = 0.8f;
m_ropeColor.blue = 0.7f;
m_perRopeDelayMin = 0x7fffffff;
m_perRopeDelayMax = 0x7fffffff;
m_ropeName = "GenericRope";
m_waitForRopesToDrop = true;
m_minDropHeight = 30.0f;
m_ropeFinalHeight = 0.0f;
m_ropeDropSpeed = 1e10f; // um, fast.
m_rappelSpeed = fabs(TheGlobalData->m_gravity) * LOGICFRAMES_PER_SECOND * 0.5f;
m_ropeWobbleLen = 10.0f;
m_ropeWobbleAmp = 1.0f;
m_ropeWobbleRate = 0.1f;
}
//-------------------------------------------------------------------------------------------------
/*static*/ void ChinookAIUpdateModuleData::buildFieldParse(MultiIniFieldParse& p)
{
SupplyTruckAIUpdateModuleData::buildFieldParse(p);
static const FieldParse dataFieldParse[] =
{
{ "RappelSpeed", INI::parseVelocityReal, 0, offsetof(ChinookAIUpdateModuleData, m_rappelSpeed) },
{ "RopeDropSpeed", INI::parseVelocityReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeDropSpeed) },
{ "RopeName", INI::parseAsciiString, 0, offsetof(ChinookAIUpdateModuleData, m_ropeName) },
{ "RopeFinalHeight", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeFinalHeight) },
{ "RopeWidth", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWidth) },
{ "RopeWobbleLen", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWobbleLen) },
{ "RopeWobbleAmplitude", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWobbleAmp) },
{ "RopeWobbleRate", INI::parseAngularVelocityReal, 0, offsetof(ChinookAIUpdateModuleData, m_ropeWobbleRate) },
{ "RopeColor", INI::parseRGBColor, 0, offsetof(ChinookAIUpdateModuleData, m_ropeColor) },
{ "NumRopes", INI::parseUnsignedInt, 0, offsetof(ChinookAIUpdateModuleData, m_numRopes) },
{ "PerRopeDelayMin", INI::parseDurationUnsignedInt, 0, offsetof(ChinookAIUpdateModuleData, m_perRopeDelayMin) },
{ "PerRopeDelayMax", INI::parseDurationUnsignedInt, 0, offsetof(ChinookAIUpdateModuleData, m_perRopeDelayMax) },
{ "MinDropHeight", INI::parseReal, 0, offsetof(ChinookAIUpdateModuleData, m_minDropHeight) },
{ "WaitForRopesToDrop", INI::parseBool, 0, offsetof(ChinookAIUpdateModuleData, m_waitForRopesToDrop) },
{ 0, 0, 0, 0 }
};
p.add(dataFieldParse);
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
AIStateMachine* ChinookAIUpdate::makeStateMachine()
{
return newInstance(ChinookAIStateMachine)( getObject(), "ChinookAIStateMachine");
}
//-------------------------------------------------------------------------------------------------
ChinookAIUpdate::ChinookAIUpdate( Thing *thing, const ModuleData* moduleData ) : SupplyTruckAIUpdate( thing, moduleData )
{
m_hasPendingCommand = false;
m_flightStatus = CHINOOK_FLYING; // yep, that's right, even if we start "on ground"
m_airfieldForHealing = INVALID_ID;
}
//-------------------------------------------------------------------------------------------------
ChinookAIUpdate::~ChinookAIUpdate()
{
}
//-------------------------------------------------------------------------------------------------
static ParkingPlaceBehaviorInterface* getPP(ObjectID id)
{
Object* airfield = TheGameLogic->findObjectByID( id );
if (airfield == nullptr || airfield->isEffectivelyDead() || !airfield->isKindOf(KINDOF_AIRFIELD))
return nullptr;
ParkingPlaceBehaviorInterface* pp = nullptr;
for (BehaviorModule** i = airfield->getBehaviorModules(); *i; ++i)
{
if ((pp = (*i)->getParkingPlaceBehaviorInterface()) != nullptr)
break;
}
return pp;
}
//-------------------------------------------------------------------------------------------------
void ChinookAIUpdate::setAirfieldForHealing(ObjectID id)
{
// make sure we de-register with current one, if any
if (m_airfieldForHealing != INVALID_ID && m_airfieldForHealing != id)
{
ParkingPlaceBehaviorInterface* pp = getPP(m_airfieldForHealing);
if (pp != nullptr)
{
pp->setHealee(getObject(), false);
}
}
m_airfieldForHealing = id;
}
//-------------------------------------------------------------------------------------------------
Bool ChinookAIUpdate::isIdle() const
{
// we need to do this because we enter an idle state briefly between takeoff/landing in these cases,
// but scripting relies on us never claiming to be "idle"...
if (m_hasPendingCommand)
return false;
Bool result = SupplyTruckAIUpdate::isIdle();
if (result && m_flightStatus == CHINOOK_LANDED)
{
// ditto: if we are waiting to disgorge some folks, we aren't 'idle'
ContainModuleInterface* contain = getObject()->getContain();
if (contain && contain->hasObjectsWantingToEnterOrExit())
result = false;
}
return result;
}
//-------------------------------------------------------------------------------------------------
Bool ChinookAIUpdate::isCurrentlyFerryingSupplies() const
{
return SupplyTruckAIUpdate::isCurrentlyFerryingSupplies();
}
//-------------------------------------------------------------------------------------------------
Bool ChinookAIUpdate::isAvailableForSupplying() const
{
if (!SupplyTruckAIUpdate::isAvailableForSupplying())
return false;
ContainModuleInterface* contain = getObject()->getContain();
if( !contain || contain->hasObjectsWantingToEnterOrExit() || contain->getContainCount())
return false;
return true;
}
//-------------------------------------------------------------------------------------------------
Bool ChinookAIUpdate::isAllowedToAdjustDestination() const
{
if (m_flightStatus == CHINOOK_LANDED)
return false;
return SupplyTruckAIUpdate::isAllowedToAdjustDestination();
}
//-------------------------------------------------------------------------------------------------
ObjectID ChinookAIUpdate::getBuildingToNotPathAround() const
{
if (getAIStateType() == MOVE_TO_COMBAT_DROP || getAIStateType() == DO_COMBAT_DROP)
{
const Object* goalObj = getStateMachine()->getGoalObject();
if (goalObj)
return goalObj->getID();
}
return INVALID_ID;
}
//-------------------------------------------------------------------------------------------------
AIFreeToExitType ChinookAIUpdate::getAiFreeToExit(const Object* exiter) const
{
#if RETAIL_COMPATIBLE_CRC
if (m_flightStatus == CHINOOK_LANDED
|| (m_flightStatus == CHINOOK_DOING_COMBAT_DROP && exiter->isKindOf(KINDOF_CAN_RAPPEL)))
#else
if (m_flightStatus == CHINOOK_LANDED)