-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCWeaponFireDesc.cpp
2517 lines (1872 loc) · 62.6 KB
/
CWeaponFireDesc.cpp
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
// CWeaponFireDesc.cpp
//
// CWeaponFireDesc class
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
#define DAMAGE_TAG CONSTLIT("Damage")
#define EFFECT_TAG CONSTLIT("Effect")
#define ENHANCED_TAG CONSTLIT("Enhanced")
#define EVENTS_TAG CONSTLIT("Events")
#define MISSILE_EXHAUST_TAG CONSTLIT("Exhaust")
#define FIRE_EFFECT_TAG CONSTLIT("FireEffect")
#define FRAGMENT_TAG CONSTLIT("Fragment")
#define HIT_EFFECT_TAG CONSTLIT("HitEffect")
#define IMAGE_TAG CONSTLIT("Image")
#define PARTICLE_SYSTEM_TAG CONSTLIT("ParticleSystem")
#define ACCELERATION_FACTOR_ATTRIB CONSTLIT("accelerationFactor")
#define AMMO_ID_ATTRIB CONSTLIT("ammoID")
#define AREA_DAMAGE_DENSITY_ATTRIB CONSTLIT("areaDamageDensity")
#define AUTO_TARGET_ATTRIB CONSTLIT("autoAcquireTarget")
#define CAN_HIT_SOURCE_ATTRIB CONSTLIT("canHitSource")
#define COUNT_ATTRIB CONSTLIT("count")
#define EXHAUST_RATE_ATTRIB CONSTLIT("creationRate")
#define DAMAGE_ATTRIB CONSTLIT("damage")
#define DIRECTIONAL_ATTRIB CONSTLIT("directional")
#define EXHAUST_DRAG_ATTRIB CONSTLIT("drag")
#define EFFECT_ATTRIB CONSTLIT("effect")
#define EXPANSION_SPEED_ATTRIB CONSTLIT("expansionSpeed")
#define EXPLOSION_TYPE_ATTRIB CONSTLIT("explosionType")
#define FAILSAFE_ATTRIB CONSTLIT("failsafe")
#define FIRE_EFFECT_ATTRIB CONSTLIT("fireEffect")
#define FIRE_RATE_ATTRIB CONSTLIT("fireRate")
#define FRAGMENT_COUNT_ATTRIB CONSTLIT("fragmentCount")
#define FRAGMENT_INTERVAL_ATTRIB CONSTLIT("fragmentInterval")
#define FRAGMENT_MAX_RADIUS_ATTRIB CONSTLIT("fragmentMaxRadius")
#define FRAGMENT_MIN_RADIUS_ATTRIB CONSTLIT("fragmentMinRadius")
#define FRAGMENT_RADIUS_ATTRIB CONSTLIT("fragmentRadius")
#define FRAGMENT_TARGET_ATTRIB CONSTLIT("fragmentTarget")
#define HIT_EFFECT_ATTRIB CONSTLIT("hitEffect")
#define HIT_POINTS_ATTRIB CONSTLIT("hitPoints")
#define IDLE_POWER_USE_ATTRIB CONSTLIT("idlePowerUse")
#define INITIAL_DELAY_ATTRIB CONSTLIT("initialDelay")
#define INTERACTION_ATTRIB CONSTLIT("interaction")
#define EXHAUST_LIFETIME_ATTRIB CONSTLIT("lifetime")
#define LIFETIME_ATTRIB CONSTLIT("lifetime")
#define MANEUVERABILITY_ATTRIB CONSTLIT("maneuverability")
#define MANEUVER_RATE_ATTRIB CONSTLIT("maneuverRate")
#define MAX_MISSILE_SPEED_ATTRIB CONSTLIT("maxMissileSpeed")
#define MAX_RADIUS_ATTRIB CONSTLIT("maxRadius")
#define MIN_DAMAGE_ATTRIB CONSTLIT("minDamage")
#define MIN_RADIUS_ATTRIB CONSTLIT("minRadius")
#define MISSILE_SPEED_ATTRIB CONSTLIT("missileSpeed")
#define MULTI_TARGET_ATTRIB CONSTLIT("multiTarget")
#define NO_FRIENDLY_FIRE_ATTRIB CONSTLIT("noFriendlyFire")
#define NO_IMMOBILE_HITS_ATTRIB CONSTLIT("noImmobileHits")
#define NO_IMMUTABLE_HITS_ATTRIB CONSTLIT("noImmutableHits")
#define NO_SHIP_HITS_ATTRIB CONSTLIT("noShipHits")
#define NO_STATION_HITS_ATTRIB CONSTLIT("noStationHits")
#define NO_WORLD_HITS_ATTRIB CONSTLIT("noWorldHits")
#define PARTICLE_COUNT_ATTRIB CONSTLIT("particleCount")
#define PARTICLE_EMIT_TIME_ATTRIB CONSTLIT("particleEmitTime")
#define PARTICLE_MISS_CHANCE_ATTRIB CONSTLIT("particleMissChance")
#define PARTICLE_SPLASH_CHANCE_ATTRIB CONSTLIT("particleSplashChance")
#define PARTICLE_SPREAD_ANGLE_ATTRIB CONSTLIT("particleSpreadAngle")
#define PARTICLE_SPREAD_WIDTH_ATTRIB CONSTLIT("particleSpreadWidth")
#define PASSTHROUGH_ATTRIB CONSTLIT("passthrough")
#define POWER_USE_ATTRIB CONSTLIT("powerUse")
#define RELATIVISTIC_SPEED_ATTRIB CONSTLIT("relativisticSpeed")
#define BEAM_CONTINUOUS_ATTRIB CONSTLIT("repeating")
#define CONTINUOUS_FIRE_DELAY_ATTRIB CONSTLIT("repeatingDelay")
#define SOUND_ATTRIB CONSTLIT("sound")
#define SPEED_ATTRIB CONSTLIT("speed")
#define STEALTH_ATTRIB CONSTLIT("stealth")
#define TARGET_REQUIRED_ATTRIB CONSTLIT("targetRequired")
#define TRAIL_ATTRIB CONSTLIT("trail")
#define FIRE_TYPE_ATTRIB CONSTLIT("type")
#define VAPOR_TRAIL_ATTRIB CONSTLIT("vaporTrail")
#define VAPOR_TRAIL_COLOR_ATTRIB CONSTLIT("vaporTrailColor")
#define VAPOR_TRAIL_LENGTH_ATTRIB CONSTLIT("vaporTrailLength")
#define VAPOR_TRAIL_WIDTH_ATTRIB CONSTLIT("vaporTrailWidth")
#define VAPOR_TRAIL_WIDTH_INC_ATTRIB CONSTLIT("vaporTrailWidthInc")
#define FIELD_PARTICLE_COUNT CONSTLIT("particleCount")
#define FIELD_SOUND CONSTLIT("sound")
#define FIRE_TYPE_BEAM CONSTLIT("beam")
#define FIRE_TYPE_CONTINUOUS_BEAM CONSTLIT("continuousBeam")
#define FIRE_TYPE_MISSILE CONSTLIT("missile")
#define FIRE_TYPE_AREA CONSTLIT("area")
#define FIRE_TYPE_PARTICLES CONSTLIT("particles")
#define FIRE_TYPE_RADIUS CONSTLIT("radius")
#define ON_CREATE_SHOT_EVENT CONSTLIT("OnCreateShot")
#define ON_DAMAGE_OVERLAY_EVENT CONSTLIT("OnDamageOverlay")
#define ON_DAMAGE_SHIELDS_EVENT CONSTLIT("OnDamageShields")
#define ON_DAMAGE_ARMOR_EVENT CONSTLIT("OnDamageArmor")
#define ON_DAMAGE_ABANDONED_EVENT CONSTLIT("OnDamageAbandoned")
#define ON_DESTROY_OBJ_EVENT CONSTLIT("OnDestroyObj")
#define ON_DESTROY_SHOT_EVENT CONSTLIT("OnDestroyShot")
#define ON_FRAGMENT_EVENT CONSTLIT("OnFragment")
#define PROPERTY_LIFETIME CONSTLIT("lifetime")
#define PROPERTY_TRACKING CONSTLIT("tracking")
#define STR_SHIELD_REFLECT CONSTLIT("reflect")
const Metric DEFAULT_FRAG_THRESHOLD = 4.0; // 4 light-seconds (~95 pixels)
CWeaponFireDesc::SOldEffects CWeaponFireDesc::m_NullOldEffects;
static char *CACHED_EVENTS[CWeaponFireDesc::evtCount] =
{
"OnCreateShot",
"OnDamageAbandoned",
"OnDamageArmor",
"OnDamageOverlay",
"OnDamageShields",
"OnDestroyObj",
"OnDestroyShot",
"OnFragment",
};
CWeaponFireDesc::CWeaponFireDesc (void)
// CWeaponFireDesc constructor
{
}
CWeaponFireDesc::~CWeaponFireDesc (void)
// CWeaponFireDesc destructor
{
SFragmentDesc *pNext = m_pFirstFragment;
while (pNext)
{
SFragmentDesc *pDelete = pNext;
pNext = pNext->pNext;
delete pDelete->pDesc;
delete pDelete;
}
if (m_pParticleDesc)
delete m_pParticleDesc;
if (m_pOldEffects)
delete m_pOldEffects;
if (m_pScalable)
delete[] m_pScalable;
}
void CWeaponFireDesc::AddTypesUsed (TSortMap<DWORD, bool> *retTypesUsed)
// AddTypesUsed
//
// Adds types used by this class
{
retTypesUsed->SetAt(m_pAmmoType.GetUNID(), true);
retTypesUsed->SetAt(GetImage().GetBitmapUNID(), true);
retTypesUsed->SetAt(GetExhaust().ExhaustImage.GetBitmapUNID(), true);
retTypesUsed->SetAt(m_pEffect.GetUNID(), true);
retTypesUsed->SetAt(m_pHitEffect.GetUNID(), true);
retTypesUsed->SetAt(m_pFireEffect.GetUNID(), true);
retTypesUsed->SetAt(m_pExplosionType.GetUNID(), true);
if (m_pParticleDesc)
m_pParticleDesc->AddTypesUsed(retTypesUsed);
SFragmentDesc *pNext = m_pFirstFragment;
while (pNext)
{
pNext->pDesc->AddTypesUsed(retTypesUsed);
pNext = pNext->pNext;
}
}
void CWeaponFireDesc::ApplyAcceleration (CSpaceObject *pMissile) const
// ApplyAcceleration
//
// Accelerates, if necessary. This should be called exactly once per 10 ticks.
{
if (m_iAccelerationFactor > 0)
{
if (m_iAccelerationFactor < 100
|| pMissile->GetVel().Length() < m_rMaxMissileSpeed)
pMissile->SetVel(pMissile->GetVel() * (Metric)(m_iAccelerationFactor / 100.0));
}
}
Metric CWeaponFireDesc::CalcMaxEffectiveRange (void) const
// CalcMaxEffectiveRange
//
// Computes the max effective range given current stats
{
int iMaxLifetime = m_Lifetime.GetMaxValue();
Metric rRange;
if (m_iFireType == ftArea)
rRange = (m_ExpansionSpeed.GetAveValueFloat() * LIGHT_SECOND / 100.0) * Ticks2Seconds(iMaxLifetime) * 0.75;
else if (m_iFireType == ftRadius)
rRange = m_rMaxRadius;
else
{
Metric rEffectiveLifetime;
if (m_iManeuverability > 0)
rEffectiveLifetime = Ticks2Seconds(iMaxLifetime) * 0.75;
else
rEffectiveLifetime = Min(Ticks2Seconds(iMaxLifetime), 200.0);
Metric rSpeed = (m_rMissileSpeed + m_rMaxMissileSpeed) / 2;
rRange = rSpeed * rEffectiveLifetime;
}
// If we have fragments, add to the effective range
if (m_pFirstFragment)
rRange += m_pFirstFragment->pDesc->m_rMaxEffectiveRange;
// Done
return rRange;
}
Metric CWeaponFireDesc::CalcSpeed (Metric rPercentOfLight) const
// CalcSpeed
//
// Converts from 0-100 speed to kps. If necessary, account for light-lag.
{
Metric rSpeed = rPercentOfLight * LIGHT_SPEED / 100.0;
// If specified, we increase speed to simulate light-lag.
if (m_fRelativisticSpeed)
rSpeed = CSystem::CalcApparentSpeedAdj(rSpeed) * rSpeed;
return rSpeed;
}
bool CWeaponFireDesc::CanHit (CSpaceObject *pObj) const
// CanHit
//
// Returns TRUE if we can hit the given object.
{
// Can we hit worlds?
if (m_fNoWorldHits
&& (pObj->GetScale() == scaleWorld || pObj->GetScale() == scaleStar))
return false;
// Can we hit immutables?
if (m_fNoImmutableHits
&& pObj->IsImmutable())
return false;
// Can we hit stations
if (m_fNoStationHits
&& pObj->GetScale() == scaleStructure)
return false;
// Can we hit immobile objects
if (m_fNoImmobileHits
&& !pObj->CanThrust())
return false;
// Can we hit ships?
if (m_fNoShipHits
&& pObj->GetScale() == scaleShip)
return false;
// OK
return true;
}
IEffectPainter *CWeaponFireDesc::CreateEffectPainter (SShotCreateCtx &CreateCtx)
// CreateEffectPainter
//
// Creates an effect to paint the projectile. The caller is responsible for
// calling Delete on the result.
//
// NOTE: We may return NULL if the weapon has no effect.
{
CCreatePainterCtx PainterCtx;
PainterCtx.SetWeaponFireDesc(this);
PainterCtx.SetTrackingObject(CreateCtx.pTarget && IsTracking());
PainterCtx.SetUseObjectCenter(true);
// If this is an explosion, then we pass the source as an anchor.
CSpaceObject *pAnchor;
if ((CreateCtx.dwFlags | SShotCreateCtx::CWF_EXPLOSION)
&& (pAnchor = CreateCtx.Source.GetObj()))
{
PainterCtx.SetAnchor(pAnchor);
}
// We set the default lifetime of the effect to whatever the descriptor defines.
PainterCtx.SetDefaultParam(LIFETIME_ATTRIB, CEffectParamDesc(m_Lifetime.GetMaxValue()));
// Create the painter
return m_pEffect.CreatePainter(PainterCtx);
}
void CWeaponFireDesc::CreateFireEffect (CSystem *pSystem, CSpaceObject *pSource, const CVector &vPos, const CVector &vVel, int iDir)
// CreateFireEffect
//
// Creates a fire effect.
{
// If we have a source, then we add the fire effect as an effect on the source.
if (pSource)
{
// Create a painter.
CCreatePainterCtx Ctx;
Ctx.SetWeaponFireDesc(this);
IEffectPainter *pPainter = m_pFireEffect.CreatePainter(Ctx, g_pUniverse->FindDefaultFireEffect(m_Damage.GetDamageType()));
if (pPainter == NULL)
return;
// Add the effect
pSource->AddEffect(pPainter, vPos, 0, iDir);
}
// Otherwise, we add a stand-alone effect
else
{
CEffectCreator *pFireEffect = GetFireEffect();
if (pFireEffect == NULL)
return;
pFireEffect->CreateEffect(pSystem, pSource, vPos, vVel, iDir);
}
}
void CWeaponFireDesc::CreateHitEffect (CSystem *pSystem, SDamageCtx &DamageCtx)
// CreateHitEffect
//
// Creates an effect when the weapon hits an object
{
// Create the hit effect painter.
CCreatePainterCtx Ctx;
Ctx.SetWeaponFireDesc(this);
Ctx.SetDamageCtx(DamageCtx);
IEffectPainter *pPainter = m_pHitEffect.CreatePainter(Ctx, g_pUniverse->FindDefaultHitEffect(m_Damage.GetDamageType()));
if (pPainter == NULL)
return;
// Now create the effect
if (CEffect::Create(pPainter,
pSystem,
((DamageCtx.pObj && !DamageCtx.pObj->IsDestroyed()) ? DamageCtx.pObj : NULL),
DamageCtx.vHitPos,
(DamageCtx.pObj ? DamageCtx.pObj->GetVel() : CVector()),
DamageCtx.iDirection) != NOERROR)
{
delete pPainter;
return;
}
}
IEffectPainter *CWeaponFireDesc::CreateParticlePainter (void)
// CreateParticlePainter
//
// Creates an effect for a particle
{
if (m_pParticleDesc == NULL || m_iFireType != ftParticles)
return NULL;
CCreatePainterCtx Ctx;
Ctx.SetWeaponFireDesc(this);
// We set the lifetime of the particle to whatever the descriptor defines.
Ctx.SetDefaultParam(LIFETIME_ATTRIB, CEffectParamDesc(m_pParticleDesc->GetParticleLifetime().GetMaxValue()));
// See if the particle descriptor has an effect.
IEffectPainter *pPainter = m_pParticleDesc->CreateParticlePainter(Ctx);
if (pPainter)
return pPainter;
// Otherwise, we use the projectile effect (for backwards compatibility)
return m_pEffect.CreatePainter(Ctx);
}
IEffectPainter *CWeaponFireDesc::CreateSecondaryPainter (bool bTrackingObj, bool bUseObjectCenter)
// CreateSecondaryPainter
//
// Particle damage object use the particle effect to paint each particle, but
// sometimes they use the main effect as an overall effect. In that case, we
// return the effect here.
//
// NOTE: We may return NULL if the weapon has no effect.
{
// If we DON'T have a particle painter, then we don't have a secondary
// effect (since we're using it for the particle effect).
if (m_pParticleDesc == NULL || m_pParticleDesc->GetParticleEffect() == NULL)
return NULL;
// Otherwise, create it if we've got it.
CCreatePainterCtx Ctx;
Ctx.SetWeaponFireDesc(this);
Ctx.SetTrackingObject(bTrackingObj);
Ctx.SetUseObjectCenter(bUseObjectCenter);
return m_pEffect.CreatePainter(Ctx);
}
IEffectPainter *CWeaponFireDesc::CreateShockwavePainter (bool bTrackingObj, bool bUseObjectCenter)
// CreateShockwavePainter
//
// Creates an effect to paint a shockwave. The caller is responsible for
// calling Delete on the result.
//
// NOTE: We may return NULL if the weapon has no effect.
{
CCreatePainterCtx Ctx;
Ctx.SetWeaponFireDesc(this);
Ctx.SetTrackingObject(bTrackingObj);
Ctx.SetUseObjectCenter(bUseObjectCenter);
// We need to match the expansion speed
Ctx.SetDefaultParam(SPEED_ATTRIB, CEffectParamDesc(m_ExpansionSpeed.Roll()));
return m_pEffect.CreatePainter(Ctx);
}
bool CWeaponFireDesc::FindDataField (const CString &sField, CString *retsValue) const
// FindDataField
//
// Returns data field for a weapon fire descriptor
{
if (strEquals(sField, FIELD_PARTICLE_COUNT))
{
if (m_pParticleDesc)
*retsValue = strFromInt((int)GetAveParticleCount());
else
*retsValue = NULL_STR;
}
else if (strEquals(sField, FIELD_SOUND))
*retsValue = (m_FireSound.GetSound() != -1 ? strFromInt(m_FireSound.GetUNID(), false) : NULL_STR);
else
return false;
return true;
}
ALERROR CWeaponFireDesc::FinishBindDesign (SDesignLoadCtx &Ctx)
// FinishBindDesign
//
// Last pass
{
ALERROR error;
int i;
#ifdef DEBUG_BIND
CItemType *pDevice;
CItemType *pAmmo = GetWeaponType(&pDevice);
if (pAmmo)
::kernelDebugLogPattern("Bind design for: %s [%s]\n", pAmmo->GetNounPhrase(), GetUNID());
#endif
// Load some events for efficiency
for (i = 0; i < evtCount; i++)
{
if (!FindEventHandler(CString(CACHED_EVENTS[i], -1, true), &m_CachedEvents[i]))
{
m_CachedEvents[i].pExtension = NULL;
m_CachedEvents[i].pCode = NULL;
}
}
// If we have an OnFragment event, then we enable proximity blast
// (But only if we don't have periodic fragmentation.)
if (HasOnFragmentEvent() && m_FragInterval.IsEmpty())
m_fProximityBlast = true;
// Fragment
SFragmentDesc *pNext = m_pFirstFragment;
while (pNext)
{
if (error = pNext->pDesc->FinishBindDesign(Ctx))
return error;
pNext = pNext->pNext;
}
// Scaled levels
if (m_pScalable)
{
for (i = 0; i < m_iScaledLevels; i++)
if (error = m_pScalable[i].FinishBindDesign(Ctx))
return error;
}
return NOERROR;
}
Metric CWeaponFireDesc::GetAveParticleCount (void) const
// GetAveParticleCount
//
// If this is a particle effect, then return the average number of particles.
{
if (m_pParticleDesc)
{
Metric rEmitLifetime = m_pParticleDesc->GetEmitLifetime().GetAveValueFloat();
if (rEmitLifetime > 0.0)
return m_pParticleDesc->GetEmitRate().GetAveValueFloat() * rEmitLifetime;
else
return m_pParticleDesc->GetEmitRate().GetAveValueFloat();
}
else
return 0.0;
}
CEffectCreator *CWeaponFireDesc::FindEffectCreator (const CString &sUNID)
// FindEffectCreator
//
// Finds effect creator from a partial UNID
{
char *pPos;
// Get the appropriate weapon fire desc and the parse position
CWeaponFireDesc *pDesc = FindWeaponFireDesc(sUNID, &pPos);
if (pDesc == NULL)
return NULL;
// Parse the effect
if (*pPos == ':')
{
pPos++;
switch (*pPos)
{
case 'e':
return pDesc->m_pEffect;
case 'h':
return pDesc->m_pHitEffect;
case 'f':
return pDesc->m_pFireEffect;
case 'p':
return (pDesc->m_pParticleDesc ? pDesc->m_pParticleDesc->GetParticleEffect() : NULL);
default:
return NULL;
}
}
else
return NULL;
}
bool CWeaponFireDesc::FindEventHandler (const CString &sEvent, SEventHandlerDesc *retEvent) const
// FindEventHandler
//
// Returns an event handler (if found)
{
// Look for an event handler at the weapon fire level
ICCItem *pCode;
if (m_Events.FindEvent(sEvent, &pCode))
{
if (retEvent)
{
retEvent->pExtension = m_pExtension;
retEvent->pCode = pCode;
}
return true;
}
// Then look for an event handler at the item level
CItemType *pDevice;
CItemType *pAmmo = GetWeaponType(&pDevice);
if (pAmmo && pAmmo->FindEventHandler(sEvent, retEvent))
return true;
if (pDevice && pAmmo != pDevice && pDevice->FindEventHandler(sEvent, retEvent))
return true;
// Otherwise, we have no event
return false;
}
ICCItem *CWeaponFireDesc::FindProperty (const CString &sProperty) const
// FindProperty
//
// Finds a property. We return NULL if not found.
{
CCodeChain &CC = g_pUniverse->GetCC();
ICCItem *pResult;
CString sValue;
SpecialDamageTypes iSpecial;
// We handle some properties
if (strEquals(sProperty, PROPERTY_LIFETIME))
return CC.CreateNumber(m_Lifetime.GetAveValueFloat());
else if (strEquals(sProperty, PROPERTY_TRACKING))
return CC.CreateBool(IsTrackingOrHasTrackingFragments());
// See if this is one of the special damage properties
else if ((iSpecial = DamageDesc::ConvertPropertyToSpecialDamageTypes(sProperty)) != specialNone)
return CC.CreateInteger(GetSpecialDamage(iSpecial));
// Check the damage structure
else if (pResult = m_Damage.FindProperty(sProperty))
return pResult;
// Otherwise, get it from a data field
else if (FindDataField(sProperty, &sValue))
return CreateResultFromDataField(CC, sValue);
// Otherwise, not found
else
return NULL;
}
CWeaponFireDesc *CWeaponFireDesc::FindWeaponFireDesc (const CString &sUNID, char **retpPos)
// FindWeaponFireDesc
//
// Finds the weapon fire desc from a partial UNID
{
char *pPos = sUNID.GetASCIIZPointer();
// If we're done, then we want this descriptor
if (*pPos == '\0')
return this;
// If a slash, then we have another weapon fire desc
else if (*pPos == '/')
{
pPos++;
// The enhanced section
if (*pPos == 'e')
{
pPos++;
return NULL;
}
// A fragment
else if (*pPos == 'f')
{
pPos++;
int iIndex = strParseInt(pPos, 0, &pPos);
SFragmentDesc *pNext = m_pFirstFragment;
while (iIndex-- > 0 && pNext)
pNext = pNext->pNext;
if (pNext)
return pNext->pDesc->FindWeaponFireDesc(CString(pPos, -1, TRUE), retpPos);
else
return NULL;
}
else
return NULL;
}
// Otherwise, we have an effect
else if (*pPos == ':')
{
if (retpPos)
*retpPos = pPos;
return this;
}
else
return NULL;
}
CWeaponFireDesc *CWeaponFireDesc::FindWeaponFireDescFromFullUNID (const CString &sUNID)
// FindWeaponFireDesc
//
// Finds the descriptor by name
{
char *pPos = sUNID.GetPointer();
// Get the UNID of the type
DWORD dwUNID = (DWORD)strParseInt(pPos, 0, &pPos);
if (dwUNID == 0)
return NULL;
// Get the type
CDesignType *pType = g_pUniverse->FindDesignType(dwUNID);
if (pType == NULL)
return NULL;
// If this is an item, then it must be a weapon
if (pType->GetType() == designItemType)
{
CItemType *pItemType = CItemType::AsType(pType);
ASSERT(pItemType);
CWeaponFireDesc *pMissileDesc;
CDeviceClass *pDevice;
// If this is a device, then parse as weapon
if (pDevice = pItemType->GetDeviceClass())
{
CWeaponClass *pClass = pDevice->AsWeaponClass();
if (pClass == NULL)
return NULL;
// Get the ordinal
int iOrdinal = 0;
if (*pPos == '/')
{
pPos++;
iOrdinal = strParseInt(pPos, 0, &pPos);
}
// Convert the ordinal to an ammo type
CItem Ammo;
if (iOrdinal < pClass->GetAmmoItemCount())
Ammo = CItem(pClass->GetAmmoItem(iOrdinal), 1);
// Get the weapon fire desc of the ordinal
CWeaponFireDesc *pDesc = pClass->GetWeaponFireDesc(CItemCtx(), Ammo);
if (pDesc == NULL)
return NULL;
// Continue parsing
return pDesc->FindWeaponFireDesc(CString(pPos));
}
// Otherwise, see if this is a missile
else if (pMissileDesc = pItemType->GetMissileDesc())
return pMissileDesc->FindWeaponFireDesc(CString(pPos));
// Nothing
else
return NULL;
}
// If this is an effect, then get it from that
else if (pType->GetType() == designEffectType)
{
CEffectCreator *pEffectType = CEffectCreator::AsType(pType);
ASSERT(pEffectType);
// Expect /d
ASSERT(*pPos == '/');
pPos++;
ASSERT(*pPos == 'd');
pPos++;
CWeaponFireDesc *pDesc = pEffectType->GetDamageDesc();
if (pDesc == NULL)
return NULL;
// Continue parsing
return pDesc->FindWeaponFireDesc(CString(pPos));
}
// Otherwise, we don't know
else
return NULL;
}
void CWeaponFireDesc::FireOnCreateShot (const CDamageSource &Source, CSpaceObject *pShot, CSpaceObject *pTarget)
// FireOnCreateShot
//
// Fire OnCreateShot
{
SEventHandlerDesc Event;
if (FindEventHandler(evtOnCreateShot, &Event))
{
// Setup arguments
CCodeChainCtx CCCtx;
CCCtx.DefineContainingType(GetWeaponType());
CCCtx.SaveAndDefineSourceVar(pShot);
CCCtx.DefineSpaceObject(CONSTLIT("aTargetObj"), pTarget);
CCCtx.DefineSpaceObject(CONSTLIT("aAttacker"), Source.GetObj());
CCCtx.DefineSpaceObject(CONSTLIT("aOrderGiver"), Source.GetOrderGiver());
CCCtx.DefineItemType(CONSTLIT("aWeaponType"), GetWeaponType());
ICCItem *pResult = CCCtx.Run(Event);
if (pResult->IsError())
pShot->ReportEventError(ON_CREATE_SHOT_EVENT, pResult);
CCCtx.Discard(pResult);
}
}
bool CWeaponFireDesc::FireOnDamageAbandoned (SDamageCtx &Ctx)
// FireOnDamageAbandoned
//
// Fire OnDamageAbandoned event. Returns TRUE if we should skip further armor damage
{
SEventHandlerDesc Event;
if (FindEventHandler(evtOnDamageAbandoned, &Event))
{
// Setup arguments
CCodeChainCtx CCCtx;
CCCtx.DefineContainingType(GetWeaponType());
CCCtx.SaveAndDefineSourceVar(Ctx.pObj);
CCCtx.DefineDamageCtx(Ctx);
ICCItem *pResult = CCCtx.Run(Event);
if (pResult->IsError())
Ctx.pObj->ReportEventError(ON_DAMAGE_ABANDONED_EVENT, pResult);
// If we return Nil, then we continue processing
bool bResult;
if (pResult->IsNil())
bResult = false;
// Otherwise, the result is the damage left
else
{
Ctx.iDamage = pResult->GetIntegerValue();
bResult = true;
}
CCCtx.Discard(pResult);
return bResult;
}
else
return false;
}
bool CWeaponFireDesc::FireOnDamageArmor (SDamageCtx &Ctx)
// FireOnDamageArmor
//
// Fire OnDamageArmor event. Returns TRUE if we should skip further armor damage
{
SEventHandlerDesc Event;
if (FindEventHandler(evtOnDamageArmor, &Event))
{
// Setup arguments
CCodeChainCtx CCCtx;
CCCtx.DefineContainingType(GetWeaponType());
CCCtx.SaveAndDefineSourceVar(Ctx.pObj);
CCCtx.DefineDamageCtx(Ctx);
ICCItem *pResult = CCCtx.Run(Event);
if (pResult->IsError())
Ctx.pObj->ReportEventError(ON_DAMAGE_ARMOR_EVENT, pResult);
// If we return Nil, then we continue processing
bool bResult;
if (pResult->IsNil())
bResult = false;
// Otherwise, the result is the damage left
else
{
Ctx.iDamage = pResult->GetIntegerValue();
bResult = true;
}
CCCtx.Discard(pResult);
return bResult;
}
else
return false;
}
bool CWeaponFireDesc::FireOnDamageOverlay (SDamageCtx &Ctx, COverlay *pOverlay)
// FireOnDamageOverlay
//
// Fire OnDamageOverlay event. Returns TRUE if we should skip further overlay damage
{
SEventHandlerDesc Event;
if (FindEventHandler(evtOnDamageOverlay, &Event))
{
// Setup arguments
CCodeChainCtx CCCtx;
CCCtx.DefineContainingType(GetWeaponType());
CCCtx.SaveAndDefineSourceVar(Ctx.pObj);
CCCtx.DefineDamageCtx(Ctx);
CCCtx.DefineInteger(CONSTLIT("aOverlayID"), pOverlay->GetID());
ICCItem *pResult = CCCtx.Run(Event);
if (pResult->IsError())
Ctx.pObj->ReportEventError(ON_DAMAGE_OVERLAY_EVENT, pResult);
// If we return Nil, then we continue processing
bool bResult;
if (pResult->IsNil())
bResult = false;