forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectCreationList.cpp
More file actions
1642 lines (1423 loc) · 60.3 KB
/
ObjectCreationList.cpp
File metadata and controls
1642 lines (1423 loc) · 60.3 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 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. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: ObjectCreationList.cpp ///////////////////////////////////////////////////////////////////////////////
// Author: Steven Johnson, December 2001
// Desc: ObjectCreationList descriptions
///////////////////////////////////////////////////////////////////////////////////////////////////
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#define DEFINE_SHADOW_NAMES // for TheShadowNames[]
#define DEFINE_WEAPONSLOTTYPE_NAMES
#define NO_DEBUG_CRC
#include "Common/AudioEventRTS.h"
#include "Common/DrawModule.h"
#include "Common/GlobalData.h"
#include "Common/INI.h"
#include "Common/Player.h"
#include "Common/PlayerList.h"
#include "Common/ThingTemplate.h"
#include "Common/ThingFactory.h"
#include "Common/GameLOD.h"
#include "GameClient/Drawable.h"
#include "GameClient/FXList.h"
#include "GameClient/ParticleSys.h"
#include "GameClient/Shadow.h"
#include "GameLogic/ExperienceTracker.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/Locomotor.h"
#include "GameLogic/Module/BodyModule.h"
#include "GameLogic/Module/ContainModule.h"
#include "GameLogic/Module/DeliverPayloadAIUpdate.h"
#include "GameLogic/Module/FloatUpdate.h"
#include "GameLogic/Module/PhysicsUpdate.h"
#include "GameLogic/Module/SpecialPowerCompletionDie.h"
#include "GameLogic/Module/LifetimeUpdate.h"
#include "GameLogic/Module/RadiusDecalUpdate.h"
#include "GameLogic/Object.h"
#include "GameLogic/ObjectCreationList.h"
#include "GameLogic/PartitionManager.h"
#include "GameLogic/ScriptEngine.h"
#include "GameLogic/Weapon.h"
#include "GameLogic/WeaponSet.h"
#include "GameLogic/AIPathfind.h"
#include "Common/CRCDebug.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
ObjectCreationListStore *TheObjectCreationListStore = nullptr; ///< the ObjectCreationList store definition
//-------------------------------------------------------------------------------------------------
static void adjustVector(Coord3D *vec, const Matrix3D* mtx)
{
if (mtx)
{
Vector3 vectmp;
vectmp.X = vec->x;
vectmp.Y = vec->y;
vectmp.Z = vec->z;
vectmp = mtx->Rotate_Vector(vectmp);
vec->x = vectmp.X;
vec->y = vectmp.Y;
vec->z = vectmp.Z;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// PRIVATE CLASSES ///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
Object* ObjectCreationNugget::create( const Object* primary, const Object* secondary, UnsignedInt lifetimeFrames ) const
{
return create( primary, primary ? primary->getPosition() : nullptr, secondary ? secondary->getPosition() : nullptr, INVALID_ANGLE, lifetimeFrames );
}
//-------------------------------------------------------------------------------------------------
//void ObjectCreationNugget::create( const Object* primaryObj, const Coord3D *primary, const Coord3D *secondary, Real angle, UnsignedInt lifetimeFrames ) const
//{
// create( primaryObj, primary ? primary->getPosition() : nullptr, secondary ? secondary->getPosition() : nullptr, angle, lifetimeFrames );
//}
//-------------------------------------------------------------------------------------------------
//This one is called only when we have a nugget that doesn't care about createOwner.
Object* ObjectCreationNugget::create( const Object *primaryObj, const Coord3D *primary, const Coord3D *secondary, Bool createOwner, UnsignedInt lifetimeFrames ) const
{
return create( primaryObj, primary, secondary, INVALID_ANGLE, lifetimeFrames );
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class FireWeaponNugget : public ObjectCreationNugget
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(FireWeaponNugget, "FireWeaponNugget")
public:
FireWeaponNugget() :
m_weapon(nullptr)
{
}
virtual Object* create( const Object* primaryObj, const Coord3D *primary, const Coord3D* secondary, Real angle, UnsignedInt lifetimeFrames = 0 ) const override
{
if (!primaryObj || !primary || !secondary)
{
DEBUG_CRASH(("You must have a primary and secondary source for this effect"));
return nullptr;
}
if (m_weapon)
{
TheWeaponStore->createAndFireTempWeapon( m_weapon, primaryObj, secondary );
}
return nullptr;
}
static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
{
static const FieldParse myFieldParse[] =
{
{ "Weapon", INI::parseWeaponTemplate, nullptr, offsetof( FireWeaponNugget, m_weapon ) },
{ nullptr, nullptr, nullptr, 0 }
};
FireWeaponNugget* nugget = newInstance(FireWeaponNugget);
ini->initFromINI(nugget, myFieldParse);
((ObjectCreationList*)instance)->addObjectCreationNugget(nugget);
}
private:
const WeaponTemplate* m_weapon;
};
EMPTY_DTOR(FireWeaponNugget)
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class AttackNugget : public ObjectCreationNugget
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(AttackNugget, "AttackNugget")
public:
AttackNugget() :
m_numberOfShots(1),
m_weaponSlot(PRIMARY_WEAPON),
m_deliveryDecalRadius(0)
{
}
virtual Object* create( const Object* primaryObj, const Coord3D *primary, const Coord3D* secondary, Real angle, UnsignedInt lifetimeFrames = 0 ) const override
{
if (!primaryObj || !primary || !secondary)
{
DEBUG_CRASH(("You must have a primary and secondary source for this effect"));
return nullptr;
}
// Star trekkin, across the universe.
// Boldly going forward now, cause we can't find reverse!
// 1:30 left on the clock, Demo looming, should I de-const all of OCL since this one effect needs the
// Primary to help make the objects? Should I rewrite superweapons to completely subsume them
// into normal special powers, so I can have a spell attack with a global timer and a spinny clock?
// Should I const cast? Should I eat them? No! I'd rather hurt them stomp them crush them hurt them
// stomp them while I dance! Down. Those insects make me dance! Down. I'll hurt them while I dance.
Object *primaryObject = const_cast<Object *>(primaryObj);
AIUpdateInterface *ai = primaryObject->getAIUpdateInterface();
if( ai )
{
// lock merely fires till the weapon is empty or the attack is "done"
primaryObject->setWeaponLock( m_weaponSlot, LOCKED_TEMPORARILY );
ai->aiAttackPosition( secondary, m_numberOfShots, CMD_FROM_AI );
}
static NameKeyType key_RadiusDecalUpdate = NAMEKEY("RadiusDecalUpdate");
RadiusDecalUpdate *rd = (RadiusDecalUpdate*)primaryObject->findUpdateModule(key_RadiusDecalUpdate);
if (rd)
{
rd->createRadiusDecal(m_deliveryDecalTemplate, m_deliveryDecalRadius, *secondary);
rd->killWhenNoLongerAttacking(true);
}
return nullptr;
}
static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
{
static const FieldParse myFieldParse[] =
{
{ "NumberOfShots", INI::parseInt, nullptr, offsetof( AttackNugget, m_numberOfShots ) },
{ "WeaponSlot", INI::parseLookupList, TheWeaponSlotTypeNamesLookupList, offsetof( AttackNugget, m_weaponSlot ) },
{ "DeliveryDecal", RadiusDecalTemplate::parseRadiusDecalTemplate, nullptr, offsetof( AttackNugget, m_deliveryDecalTemplate ) },
{ "DeliveryDecalRadius", INI::parseReal, nullptr, offsetof(AttackNugget, m_deliveryDecalRadius) },
{ nullptr, nullptr, nullptr, 0 }
};
AttackNugget* nugget = newInstance(AttackNugget);
ini->initFromINI(nugget, myFieldParse);
((ObjectCreationList*)instance)->addObjectCreationNugget(nugget);
}
private:
RadiusDecalTemplate m_deliveryDecalTemplate;
Real m_deliveryDecalRadius;
Int m_numberOfShots;
WeaponSlotType m_weaponSlot;
};
EMPTY_DTOR(AttackNugget)
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class DeliverPayloadNugget : public ObjectCreationNugget
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(DeliverPayloadNugget, "DeliverPayloadNugget")
public:
DeliverPayloadNugget() :
m_startAtPreferredHeight(true),
m_startAtMaxSpeed(false),
m_formationSize(1),
m_formationSpacing(25.0f),
m_errorRadius(0.0f),
m_delayDeliveryFramesMax(0),
m_convergenceFactor( 0.0f )
{
//Note: m_data is constructed with default values.
m_payload.clear();
m_putInContainerName.clear();
m_transportName.clear();
}
virtual Object* create(const Object *primaryObj, const Coord3D *primary, const Coord3D *secondary, Real angle, UnsignedInt lifetimeFrames = 0 ) const override
{
return create( primaryObj, primary, secondary, true, lifetimeFrames );
}
virtual Object* create(const Object* primaryObj, const Coord3D *primary, const Coord3D* secondary, Bool createOwner, UnsignedInt lifetimeFrames = 0 ) const override
{
if (!primaryObj || !primary || !secondary)
{
DEBUG_CRASH(("You must have a primary and secondary source for this effect"));
return nullptr;
}
Team* owner = primaryObj ? primaryObj->getControllingPlayer()->getDefaultTeam() : nullptr;
//What I'm doing for the purposes of the formations is to calculate the relative positions of
//each member of the formation. To do so, we take the vector from the target location to the
//lead plane location, normalize it, then rotate it 90 degrees (CW and CCW). When we add the
//resultant vectors to the initial vectors, we can calculate the delta positions for each plane.
Real CCWx = 0.0f, CCWy = 0.0f, CWx = 0.0f, CWy = 0.0f;
if( m_formationSize > 1 )
{
//Get the delta x and y values from the target to the origin.
Real dx = primary->x - secondary->x;
Real dy = primary->y - secondary->y;
//Calc length
Real length = sqrt( dx*dx + dy*dy );
//Normalize length
dx /= length;
dy /= length;
//Rotate 90 degrees CCW.
Real radians = 90.0f * PI / 180.0f;
Real s = Sin( radians );
Real c = Cos( radians );
CCWx = dx * c + dy * -s + dx;
CCWy = dx * s + dy * c + dy;
//Rotate 90 degrees CW
s = Sin( -radians );
c = Cos( -radians );
CWx = dx * c + dy * -s + dx;
CWy = dx * s + dy * c + dy;
}
Object *firstTransport = nullptr;
for( UnsignedInt formationIndex = 0; formationIndex < m_formationSize; formationIndex++ )
{
Coord3D offset;
offset.zero();
Int offsetMultiplier = ( formationIndex + 1 ) / 2 * m_formationSpacing;
if( formationIndex % 2 )
{
//Formation index is odd -- use the CCW deltas
offset.x = CCWx * offsetMultiplier;
offset.y = CCWy * offsetMultiplier;
}
else
{
//Formation index is even -- use the CW deltas
offset.x = CWx * offsetMultiplier;
offset.y = CWy * offsetMultiplier;
}
Coord3D startPos = *primary;
Coord3D moveToPos = *secondary;
startPos.add( &offset );
//Also give our moveToPos the same offset to maintain perfect formation.
moveToPos.add( &offset );
Coord3D targetPos = *secondary;
//Our target position only applies when using fireweapon and when we have multiple planes,
//as is the case with the napalm strike. The target position either be somewhere between the
//moveToPos of the lead plane and that of the relative offset -- determined by the convergenceFactor.
targetPos.x += offset.x * (1.0f - m_convergenceFactor);
targetPos.y += offset.y * (1.0f - m_convergenceFactor);
// first guy in each formation is always spot-on (to keep targeting cursor well-matched)
if ( m_errorRadius > 1.0f && formationIndex > 0 )
{
Real randomRadius = GameLogicRandomValueReal(0, m_errorRadius );
Real randomAngle = GameLogicRandomValueReal(0, PI*2 );
targetPos.x += randomRadius * Cos( randomAngle );
targetPos.y += randomRadius * Sin( randomAngle );
}
Real orient = atan2( moveToPos.y - startPos.y, moveToPos.x - startPos.x);
if( m_data.m_distToTarget > 0 )
{
const Real SLOP = 1.5f;
startPos.x -= Cos(orient) * m_data.m_distToTarget * SLOP;
startPos.y -= Sin(orient) * m_data.m_distToTarget * SLOP;
}
Object *transport;
if( createOwner )
{
const ThingTemplate* ttn = TheThingFactory->findTemplate(m_transportName);
transport = TheThingFactory->newObject( ttn, owner );
if( !transport )
{
return nullptr;
}
if( !firstTransport )
{
firstTransport = transport;
}
transport->setPosition(&startPos);
transport->setOrientation(orient);
transport->setProducer(primaryObj);
//Adding this nifty flag allows enemy players to target it manually with weapons :)
transport->setScriptStatus( OBJECT_STATUS_SCRIPT_TARGETABLE );
if ( m_delayDeliveryFramesMax > 0 )
{
transport->setDisabledUntil( DISABLED_DEFAULT, TheGameLogic->getFrame() + GameLogicRandomValue(0, m_delayDeliveryFramesMax) );
}
}
else
{
transport = (Object*)primaryObj;
}
// Notify special power tracking
SpecialPowerCompletionDie *die = transport->findSpecialPowerCompletionDie();
if (die)
{
if (formationIndex == 0)
die->setCreator(primaryObj->getID());
else
die->setCreator(INVALID_ID);
}
static NameKeyType key_DeliverPayloadAIUpdate = NAMEKEY("DeliverPayloadAIUpdate");
DeliverPayloadAIUpdate *ai = (DeliverPayloadAIUpdate*)transport->findUpdateModule(key_DeliverPayloadAIUpdate);
if( ai )
{
if( m_startAtMaxSpeed && createOwner )
{
PhysicsBehavior* physics = transport->getPhysics();
if (physics)
{
Coord3D startingForce = *transport->getUnitDirectionVector2D();
Real maxSpeed = ai->getCurLocomotor()->getMaxSpeedForCondition(transport->getBodyModule()->getDamageState());
Real factor = maxSpeed * physics->getMass();
startingForce.x *= factor;
startingForce.y *= factor;
startingForce.z *= factor;
physics->applyMotiveForce( &startingForce );
}
}
// only the first guy in each formation gets a delivery decal
DeliverPayloadData data = m_data;
if (formationIndex != 0)
data.m_deliveryDecalRadius = 0;
ai->deliverPayload( &moveToPos, &targetPos, &data );
if( m_startAtPreferredHeight && createOwner )
{
startPos.z = TheTerrainLogic->getGroundHeight(startPos.x, startPos.y) + ai->getCurLocomotor()->getPreferredHeight();
transport->setPosition(&startPos);
}
const ThingTemplate* putInContainerTmpl = m_putInContainerName.isEmpty() ? nullptr : TheThingFactory->findTemplate(m_putInContainerName);
for (std::vector<Payload>::const_iterator it = m_payload.begin(); it != m_payload.end(); ++it)
{
const ThingTemplate* payloadTmpl = TheThingFactory->findTemplate(it->m_payloadName);
if( !payloadTmpl )
{
DEBUG_CRASH( ("DeliverPayloadNugget::create() -- %s couldn't create %s (template not found).",
transport->getTemplate()->getName().str(), it->m_payloadName.str() ) );
return nullptr;
}
for (int i = 0; i < it->m_payloadCount; ++i)
{
Object* payload = TheThingFactory->newObject( payloadTmpl, owner );
payload->setPosition(&startPos);
payload->setProducer(transport);
// Notify special power tracking
SpecialPowerCompletionDie *die = payload->findSpecialPowerCompletionDie();
if (die)
{
if (formationIndex == 0 && i == 0)
die->setCreator(primaryObj->getID());
else
die->setCreator(INVALID_ID);
}
if (putInContainerTmpl)
{
Object* container = TheThingFactory->newObject( putInContainerTmpl, owner );
container->setPosition(&startPos);
container->setProducer(transport);
// Notify special power tracking
SpecialPowerCompletionDie *die = container->findSpecialPowerCompletionDie();
if (die)
{
if (formationIndex == 0 && i == 0)
die->setCreator(primaryObj->getID());
else
die->setCreator(INVALID_ID);
}
if (container->getContain() && container->getContain()->isValidContainerFor(payload, true))
{
container->getContain()->addToContain(payload);
payload = container;
}
else
{
DEBUG_CRASH(("DeliverPayload: PutInContainer %s is full, or not valid for the payload %s!",m_putInContainerName.str(),it->m_payloadName.str()));
}
}
if (transport->getContain() && transport->getContain()->isValidContainerFor(payload, true))
{
transport->getContain()->addToContain(payload);
}
else
{
DEBUG_CRASH(("DeliverPayload: transport %s is full, or not valid for the payload %s!",m_transportName.str(),it->m_payloadName.str()));
}
}
}
}
else
{
DEBUG_CRASH(("You should really have a DeliverPayloadAIUpdate here"));
}
}
return firstTransport;
}
static void parsePayload( INI* ini, void *instance, void *store, const void* /*userData*/ )
{
DeliverPayloadNugget* self = (DeliverPayloadNugget*)instance;
const char* name = ini->getNextToken();
const char* countStr = ini->getNextTokenOrNull();
Int count = countStr ? INI::scanInt(countStr) : 1;
Payload p;
p.m_payloadName.set(name);
p.m_payloadCount = count;
self->m_payload.push_back(p);
}
static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
{
static const FieldParse myFieldParse[] =
{
//***************************************************************
//OBJECT CREATION LIST SPECIFIC DATA -- once created data no longer needed
//The transport(s) that carry all the payload items (and initial physics information)
{ "Transport", INI::parseAsciiString, nullptr, offsetof(DeliverPayloadNugget, m_transportName) },
{ "StartAtPreferredHeight", INI::parseBool, nullptr, offsetof(DeliverPayloadNugget, m_startAtPreferredHeight) },
{ "StartAtMaxSpeed", INI::parseBool, nullptr, offsetof(DeliverPayloadNugget, m_startAtMaxSpeed) },
//For multiple transports, this defines the formation (and convergence if all weapons will hit same target)
{ "FormationSize", INI::parseUnsignedInt, nullptr, offsetof( DeliverPayloadNugget, m_formationSize) },
{ "FormationSpacing", INI::parseReal, nullptr, offsetof( DeliverPayloadNugget, m_formationSpacing) },
{ "WeaponConvergenceFactor", INI::parseReal, nullptr, offsetof( DeliverPayloadNugget, m_convergenceFactor ) },
{ "WeaponErrorRadius", INI::parseReal, nullptr, offsetof( DeliverPayloadNugget, m_errorRadius ) },
{ "DelayDeliveryMax", INI::parseDurationUnsignedInt, nullptr, offsetof( DeliverPayloadNugget, m_delayDeliveryFramesMax ) },
//Payload information (it's all created now and stored inside)
{ "Payload", parsePayload, nullptr, 0 },
{ "PutInContainer", INI::parseAsciiString, nullptr, offsetof( DeliverPayloadNugget, m_putInContainerName) },
//END OBJECT CREATION LIST SPECIFIC DATA
//***************************************************************
//***************************************************************
//DELIVERPAYLOADDATA contains the rest (and most) of the parsed data.
//***************************************************************
{ nullptr, nullptr, nullptr, 0 }
};
DeliverPayloadNugget* nugget = newInstance(DeliverPayloadNugget);
MultiIniFieldParse p;
p.add(myFieldParse);
p.add(DeliverPayloadData::getFieldParse(), offsetof( DeliverPayloadNugget, m_data ));
ini->initFromINIMulti(nugget, p);
((ObjectCreationList*)instance)->addObjectCreationNugget(nugget);
}
private:
struct Payload
{
AsciiString m_payloadName;
Int m_payloadCount;
};
//Specific data needed to create the transport(s), internal payload, and initial physics.
AsciiString m_transportName;
AsciiString m_putInContainerName;
std::vector<Payload> m_payload;
Real m_formationSpacing;
Real m_convergenceFactor;
Real m_errorRadius;
UnsignedInt m_delayDeliveryFramesMax;
UnsignedInt m_formationSize;
Bool m_startAtPreferredHeight;
Bool m_startAtMaxSpeed;
//AI specific data passed over to DeliverPayloadAIUpdate::deliver()
DeliverPayloadData m_data;
};
EMPTY_DTOR(DeliverPayloadNugget)
//-------------------------------------------------------------------------------------------------
static void calcRandomForce(Real minMag, Real maxMag, Real minPitch, Real maxPitch, Coord3D* force)
{
Real angle = GameLogicRandomValueReal(0, 2*PI);
Real pitch = GameLogicRandomValueReal(minPitch, maxPitch);
Real mag = GameLogicRandomValueReal(minMag, maxMag);
Matrix3D mtx(1);
mtx.Scale(mag);
mtx.Rotate_Z(angle);
mtx.Rotate_Y(-pitch);
Vector3 v = mtx.Get_X_Vector();
force->x = v.X;
force->y = v.Y;
force->z = v.Z;
}
//-------------------------------------------------------------------------------------------------
class ApplyRandomForceNugget : public ObjectCreationNugget
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ApplyRandomForceNugget, "ApplyRandomForceNugget")
public:
ApplyRandomForceNugget() :
m_spinRate(0.0f),
m_minMag(0.0f),
m_maxMag(0.0f),
m_minPitch(0.0f),
m_maxPitch(0.0f)
{
}
virtual Object* create( const Object* primary, const Object* secondary, UnsignedInt lifetimeFrames = 0 ) const override
{
if (primary)
{
/// @todo srj -- ack. const_cast is evil.
PhysicsBehavior* p = const_cast<Object*>(primary)->getPhysics();
if (p)
{
Coord3D force;
calcRandomForce(m_minMag, m_maxMag, m_minPitch, m_maxPitch, &force);
p->applyForce(&force);
Real yaw = GameLogicRandomValueReal( -m_spinRate, m_spinRate );
Real roll = GameLogicRandomValueReal( -m_spinRate, m_spinRate );
Real pitch = GameLogicRandomValueReal( -m_spinRate, m_spinRate );
p->setYawRate(yaw);
p->setRollRate(roll);
p->setPitchRate(pitch);
}
else
{
DEBUG_CRASH(("You must have a Physics module source for this effect"));
}
}
else
{
DEBUG_CRASH(("You must have a primary source for this effect"));
}
return nullptr;
}
virtual Object* create(const Object* primaryObj, const Coord3D *primary, const Coord3D* secondary, Real angle, UnsignedInt lifetimeFrames = 0 ) const override
{
DEBUG_CRASH(("You must call this effect with an object, not a location"));
return nullptr;
}
static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
{
static const FieldParse myFieldParse[] =
{
{ "SpinRate", INI::parseAngularVelocityReal, nullptr, offsetof(ApplyRandomForceNugget, m_spinRate) },
{ "MinForceMagnitude", INI::parseReal, nullptr, offsetof(ApplyRandomForceNugget, m_minMag) },
{ "MaxForceMagnitude", INI::parseReal, nullptr, offsetof(ApplyRandomForceNugget, m_maxMag) },
{ "MinForcePitch", INI::parseAngleReal, nullptr, offsetof(ApplyRandomForceNugget, m_minPitch) },
{ "MaxForcePitch", INI::parseAngleReal, nullptr, offsetof(ApplyRandomForceNugget, m_maxPitch) },
{ nullptr, nullptr, nullptr, 0 }
};
ApplyRandomForceNugget* nugget = newInstance(ApplyRandomForceNugget);
ini->initFromINI(nugget, myFieldParse);
((ObjectCreationList*)instance)->addObjectCreationNugget(nugget);
}
protected:
private:
Real m_spinRate;
Real m_minMag, m_maxMag;
Real m_minPitch, m_maxPitch;
};
EMPTY_DTOR(ApplyRandomForceNugget)
//-------------------------------------------------------------------------------------------------
enum DebrisDisposition CPP_11(: Int)
{
LIKE_EXISTING = 0x00000001,
ON_GROUND_ALIGNED = 0x00000002,
SEND_IT_FLYING = 0x00000004,
SEND_IT_UP = 0x00000008,
SEND_IT_OUT = 0x00000010,
RANDOM_FORCE = 0x00000020,
FLOATING = 0x00000040,
INHERIT_VELOCITY = 0x00000080,
WHIRLING = 0x00000100
};
static const char* const DebrisDispositionNames[] =
{
"LIKE_EXISTING",
"ON_GROUND_ALIGNED",
"SEND_IT_FLYING",
"SEND_IT_UP",
"SEND_IT_OUT",
"RANDOM_FORCE",
"FLOATING",
"INHERIT_VELOCITY",
"WHIRLING",
nullptr
};
std::vector<AsciiString> debrisModelNamesGlobalHack;
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
static void parseFrictionPerSec( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
{
Real fricPerSec = INI::scanReal(ini->getNextToken());
Real fricPerFrame = fricPerSec * SECONDS_PER_LOGICFRAME_REAL;
*(Real *)store = fricPerFrame;
}
//-------------------------------------------------------------------------------------------------
class GenericObjectCreationNugget : public ObjectCreationNugget
{
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(GenericObjectCreationNugget, "GenericObjectCreationNugget")
public:
GenericObjectCreationNugget() :
m_requiresLivePlayer(FALSE),
m_debrisToGenerate(1),
m_mass(0),
m_extraBounciness(0),
m_extraFriction(0),
m_disposition(ON_GROUND_ALIGNED),
m_dispositionIntensity(0.0f),
m_spinRate(-1.0f),
m_yawRate(-1.0f),
m_rollRate(-1.0f),
m_pitchRate(-1.0f),
m_nameAreObjects(true),
m_okToChangeModelColor(false),
m_minLODRequired(STATIC_GAME_LOD_LOW),
m_ignorePrimaryObstacle(false),
m_inheritsVeterancy(false),
m_diesOnBadLand(FALSE),
m_skipIfSignificantlyAirborne(false),
m_invulnerableTime(0),
m_containInsideSourceObject(FALSE),
m_minHealth(1.0f),
m_maxHealth(1.0f),
m_orientInForceDirection(false),
m_spreadFormation(false),
m_minDistanceAFormation(0.0f),
m_minDistanceBFormation(0.0f),
m_maxDistanceFormation(0.0f),
m_fadeIn(false),
m_fadeOut(false),
m_fadeFrames(0),
m_fadeSoundName(AsciiString::TheEmptyString),
m_particleSysName(AsciiString::TheEmptyString),
m_putInContainer(AsciiString::TheEmptyString),
m_minMag(0.0f),
m_maxMag(0.0f),
m_minPitch(0.0f),
m_maxPitch(0.0f),
m_minFrames(0),
m_maxFrames(0),
m_shadowType(SHADOW_NONE),
m_fxFinal(nullptr),
m_preserveLayer(true),
m_objectCount(0)
{
m_offset.zero();
}
virtual Object* create(const Object* primary, const Object* secondary, UnsignedInt lifetimeFrames = 0 ) const override
{
if (primary)
{
if (m_skipIfSignificantlyAirborne && primary->isSignificantlyAboveTerrain())
return nullptr;
return reallyCreate( primary->getPosition(), primary->getTransformMatrix(), primary->getOrientation(), primary, lifetimeFrames );
}
else
{
DEBUG_CRASH(("You must have a primary source for this effect"));
}
return nullptr;
}
virtual Object* create(const Object* primaryObj, const Coord3D *primary, const Coord3D* secondary, Real angle, UnsignedInt lifetimeFrames = 0 ) const override
{
if (primary)
{
const Matrix3D *xfrm = nullptr;
if( angle == INVALID_ANGLE )
{
//Vast majority of OCL's don't care about the angle, so if it comes in invalid, default the angle to 0.
angle = 0.0f;
}
return reallyCreate( primary, xfrm, angle, primaryObj, lifetimeFrames );
}
else
{
DEBUG_CRASH(("You must have a primary source for this effect"));
}
return nullptr;
}
static const FieldParse* getCommonFieldParse()
{
static const FieldParse commonFieldParse[] =
{
{ "PutInContainer", INI::parseAsciiString, nullptr, offsetof( GenericObjectCreationNugget, m_putInContainer) },
{ "ParticleSystem", INI::parseAsciiString, nullptr, offsetof( GenericObjectCreationNugget, m_particleSysName) },
{ "Count", INI::parseInt, nullptr, offsetof( GenericObjectCreationNugget, m_debrisToGenerate ) },
{ "IgnorePrimaryObstacle", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_ignorePrimaryObstacle) },
{ "OrientInForceDirection", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_orientInForceDirection) },
{ "ExtraBounciness", INI::parseReal, nullptr, offsetof( GenericObjectCreationNugget, m_extraBounciness ) },
{ "ExtraFriction", parseFrictionPerSec, nullptr, offsetof( GenericObjectCreationNugget, m_extraFriction ) },
{ "Offset", INI::parseCoord3D, nullptr, offsetof( GenericObjectCreationNugget, m_offset ) },
{ "Disposition", INI::parseBitString32, DebrisDispositionNames, offsetof( GenericObjectCreationNugget, m_disposition ) },
{ "DispositionIntensity", INI::parseReal, nullptr, offsetof( GenericObjectCreationNugget, m_dispositionIntensity ) },
{ "SpinRate", INI::parseAngularVelocityReal, nullptr, offsetof(GenericObjectCreationNugget, m_spinRate) },
{ "YawRate", INI::parseAngularVelocityReal, nullptr, offsetof(GenericObjectCreationNugget, m_yawRate) },
{ "RollRate", INI::parseAngularVelocityReal, nullptr, offsetof(GenericObjectCreationNugget, m_rollRate) },
{ "PitchRate", INI::parseAngularVelocityReal, nullptr, offsetof(GenericObjectCreationNugget, m_pitchRate) },
{ "MinForceMagnitude", INI::parseReal, nullptr, offsetof(GenericObjectCreationNugget, m_minMag) },
{ "MaxForceMagnitude", INI::parseReal, nullptr, offsetof(GenericObjectCreationNugget, m_maxMag) },
{ "MinForcePitch", INI::parseAngleReal, nullptr, offsetof(GenericObjectCreationNugget, m_minPitch) },
{ "MaxForcePitch", INI::parseAngleReal, nullptr, offsetof(GenericObjectCreationNugget, m_maxPitch) },
{ "MinLifetime", INI::parseDurationUnsignedInt, nullptr, offsetof( GenericObjectCreationNugget, m_minFrames ) },
{ "MaxLifetime", INI::parseDurationUnsignedInt, nullptr, offsetof( GenericObjectCreationNugget, m_maxFrames ) },
{ "SpreadFormation", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_spreadFormation) },
{ "MinDistanceAFormation", INI::parseReal, nullptr, offsetof(GenericObjectCreationNugget, m_minDistanceAFormation) },
{ "MinDistanceBFormation", INI::parseReal, nullptr, offsetof(GenericObjectCreationNugget, m_minDistanceBFormation) },
{ "MaxDistanceFormation", INI::parseReal, nullptr, offsetof(GenericObjectCreationNugget, m_maxDistanceFormation) },
{ "FadeIn", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_fadeIn) },
{ "FadeOut", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_fadeOut) },
{ "FadeTime", INI::parseDurationUnsignedInt, nullptr, offsetof(GenericObjectCreationNugget, m_fadeFrames) },
{ "FadeSound", INI::parseAsciiString, nullptr, offsetof( GenericObjectCreationNugget, m_fadeSoundName) },
{ "PreserveLayer", INI::parseBool, nullptr, offsetof( GenericObjectCreationNugget, m_preserveLayer) },
{ "DiesOnBadLand", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_diesOnBadLand) },
{ nullptr, nullptr, nullptr, 0 }
};
return commonFieldParse;
}
static void parseObject(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
{
static const FieldParse myFieldParse[] =
{
{ "ContainInsideSourceObject", INI::parseBool, nullptr, offsetof( GenericObjectCreationNugget, m_containInsideSourceObject) },
{ "ObjectNames", parseDebrisObjectNames, nullptr, 0 },
{ "ObjectCount", INI::parseInt, nullptr, offsetof(GenericObjectCreationNugget, m_objectCount) },
{ "InheritsVeterancy", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_inheritsVeterancy) },
{ "SkipIfSignificantlyAirborne", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_skipIfSignificantlyAirborne) },
{ "InvulnerableTime", INI::parseDurationUnsignedInt, nullptr, offsetof(GenericObjectCreationNugget, m_invulnerableTime) },
{ "MinHealth", INI::parsePercentToReal, nullptr, offsetof(GenericObjectCreationNugget, m_minHealth) },
{ "MaxHealth", INI::parsePercentToReal, nullptr, offsetof(GenericObjectCreationNugget, m_maxHealth) },
{ "RequiresLivePlayer", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_requiresLivePlayer) },
{ nullptr, nullptr, nullptr, 0 }
};
MultiIniFieldParse p;
p.add(getCommonFieldParse());
p.add(myFieldParse);
GenericObjectCreationNugget* nugget = newInstance(GenericObjectCreationNugget);
nugget->m_nameAreObjects = true;
ini->initFromINIMulti(nugget, p);
((ObjectCreationList*)instance)->addObjectCreationNugget(nugget);
}
static void parseDebris(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
{
static const FieldParse myFieldParse[] =
{
{ "ModelNames", parseDebrisObjectNames, nullptr, 0 },
{ "Mass", INI::parsePositiveNonZeroReal, nullptr, offsetof( GenericObjectCreationNugget, m_mass ) },
{ "AnimationSet", parseAnimSet, nullptr, offsetof( GenericObjectCreationNugget, m_animSets) },
{ "FXFinal", INI::parseFXList, nullptr, offsetof( GenericObjectCreationNugget, m_fxFinal) },
{ "OkToChangeModelColor", INI::parseBool, nullptr, offsetof(GenericObjectCreationNugget, m_okToChangeModelColor) },
{ "MinLODRequired", INI::parseStaticGameLODLevel, nullptr, offsetof(GenericObjectCreationNugget, m_minLODRequired) },
{ "Shadow", INI::parseBitString32, TheShadowNames, offsetof( GenericObjectCreationNugget, m_shadowType ) },
{ "BounceSound", INI::parseAudioEventRTS, nullptr, offsetof( GenericObjectCreationNugget, m_bounceSound) },
{ nullptr, nullptr, nullptr, 0 }
};
MultiIniFieldParse p;
p.add(getCommonFieldParse());
p.add(myFieldParse);
GenericObjectCreationNugget* nugget = newInstance(GenericObjectCreationNugget);
nugget->m_nameAreObjects = false;
ini->initFromINIMulti(nugget, p);
DEBUG_ASSERTCRASH(nugget->m_mass > 0.0f, ("Zero masses are not allowed for debris!"));
((ObjectCreationList*)instance)->addObjectCreationNugget(nugget);
}
static void parseAnimSet(INI *ini, void * /*instance*/, void* store, const void* /*userData*/)
{
AnimSet anim;
anim.m_animInitial = ini->getNextAsciiString();
anim.m_animFlying = ini->getNextAsciiString();
anim.m_animFinal = ini->getNextAsciiString();
((std::vector<AnimSet>*)store)->push_back(anim);
}
protected:
void doStuffToObj(
Object* obj,
const AsciiString& modelName,
const Coord3D *pos,
const Matrix3D *mtx,
Real orientation,
const Object *sourceObj,
UnsignedInt lifetimeFrames
) const
{
obj->setProducer(sourceObj);
static NameKeyType key_LifetimeUpdate = NAMEKEY("LifetimeUpdate");
LifetimeUpdate* lup = (LifetimeUpdate*)obj->findUpdateModule(key_LifetimeUpdate);
if( lup )
{
if( lifetimeFrames )
{
//Passed in override, use this value for a specific lifetime!!!
lup->setLifetimeRange( lifetimeFrames, lifetimeFrames );
}
else if( m_maxFrames > 0 )
{
// They will both be zero if no lifetime was specified in the OCL. It could be in the Object so don't mess with it.
// So the OCL listing will override the Object listing for lifetime, but ONLY if there is one.
lup->setLifetimeRange(m_minFrames, m_maxFrames);
}
}
if (!m_nameAreObjects)
{
for (DrawModule** dm = obj->getDrawable()->getDrawModules(); *dm; ++dm)
{
DebrisDrawInterface* di = (*dm)->getDebrisDrawInterface();
if (di)
{
di->setModelName(modelName, m_okToChangeModelColor ? obj->getIndicatorColor() : 0, m_shadowType);
if (!m_animSets.empty())
{
Int which = GameLogicRandomValue(0, m_animSets.size()-1);
di->setAnimNames(m_animSets[which].m_animInitial, m_animSets[which].m_animFlying, m_animSets[which].m_animFinal, m_fxFinal);
}
}
}
}
Coord3D offset = m_offset;
if (mtx)
adjustVector(&offset, mtx);
Coord3D chunkPos;
chunkPos.x = pos->x + offset.x;
chunkPos.y = pos->y + offset.y;
chunkPos.z = pos->z + offset.z;
if (!m_particleSysName.isEmpty())
{
const ParticleSystemTemplate *tmp = TheParticleSystemManager->findTemplate(m_particleSysName);
if (tmp)
{
ParticleSystem *sys = TheParticleSystemManager->createParticleSystem(tmp);
sys->attachToObject(obj);
}
}
if (m_ignorePrimaryObstacle)
{
PhysicsBehavior* p = obj->getPhysics();
if (p)
p->setIgnoreCollisionsWith(sourceObj);
}
// set its beginning health
BodyModuleInterface *body = obj->getBodyModule();
Real healthPercent = GameLogicRandomValueReal( m_minHealth, m_maxHealth );
if (body)
body->setInitialHealth(healthPercent * 100.0f);
// If they have a SlavedUpdate, then I have to tell them who their daddy is from now on.
for (BehaviorModule** update = obj->getBehaviorModules(); *update; ++update)
{
SlavedUpdateInterface* sdu = (*update)->getSlavedUpdateInterface();