-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCParticleDamage.cpp
661 lines (481 loc) · 15.1 KB
/
CParticleDamage.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
// CParticleDamage.cpp
//
// CParticleDamage class
#include "PreComp.h"
#define LIFETIME_ATTRIB CONSTLIT("lifetime")
static CObjectClass<CParticleDamage>g_Class(OBJID_CPARTICLEDAMAGE, NULL);
CParticleDamage::CParticleDamage (void) : CSpaceObject(&g_Class),
m_pEffectPainter(NULL),
m_pParticlePainter(NULL)
// CParticleDamage constructor
{
}
CParticleDamage::~CParticleDamage (void)
// CParticleDamage destructor
{
if (m_pEffectPainter)
m_pEffectPainter->Delete();
if (m_pParticlePainter)
m_pParticlePainter->Delete();
}
ALERROR CParticleDamage::Create (CSystem *pSystem, SShotCreateCtx &Ctx, CParticleDamage **retpObj)
// Create
//
// Create the object
{
ALERROR error;
// We better have a particle description.
const CParticleSystemDesc *pSystemDesc = Ctx.pDesc->GetParticleSystemDesc();
ASSERT(pSystemDesc);
if (pSystemDesc == NULL)
return ERR_FAIL;
// Make sure we have a valid CWeaponFireDesc (otherwise we won't be
// able to save the object).
ASSERT(!Ctx.pDesc->GetUNID().IsBlank());
// Create the area
CParticleDamage *pParticles = new CParticleDamage;
if (pParticles == NULL)
return ERR_MEMORY;
pParticles->Place(Ctx.vPos, Ctx.vVel);
// Get notifications when other objects are destroyed
pParticles->SetObjectDestructionHook();
// Set non-linear move, meaning that we are responsible for
// setting the position and velocity in OnMove
pParticles->SetNonLinearMove();
pParticles->m_pDesc = Ctx.pDesc;
pParticles->m_pTarget = Ctx.pTarget;
pParticles->m_pEnhancements = Ctx.pEnhancements;
pParticles->m_iEmitTime = Max(1, pSystemDesc->GetEmitLifetime().Roll());
pParticles->m_iLifeLeft = Ctx.pDesc->GetMaxLifetime() + pParticles->m_iEmitTime;
pParticles->m_iTick = 0;
pParticles->m_iRotation = Ctx.iDirection;
pParticles->m_fPainterFade = false;
// Keep track of where we emitted particles relative to the source. We
// need this so we can continue to emit from this location later.
pParticles->m_Source = Ctx.Source;
if (!Ctx.Source.IsEmpty())
{
// Decompose the source position/velocity so that we can continue to
// emit later (after the source has changed).
//
// We start by computing the emission offset relative to the source
// object when it points at 0 degrees.
int iSourceRotation = Ctx.Source.GetObj()->GetRotation();
CVector vPosOffset = (Ctx.vPos - Ctx.Source.GetObj()->GetPos()).Rotate(-iSourceRotation);
// Remember these values so we can add them to the new source
// position/velocity.
pParticles->m_iEmitDirection = Ctx.iDirection - iSourceRotation;
pParticles->m_vEmitSourcePos = vPosOffset;
pParticles->m_vEmitSourceVel = CVector();
}
else
{
pParticles->m_iEmitDirection = Ctx.iDirection;
pParticles->m_vEmitSourcePos = CVector();
pParticles->m_vEmitSourceVel = CVector();
}
// Damage
pParticles->m_iDamage = Ctx.pDesc->GetDamage().RollDamage();
// Friendly fire
if (!Ctx.pDesc->CanHitFriends())
pParticles->SetNoFriendlyFire();
// Create the effect painter, if we've got one
bool bIsTracking = Ctx.pTarget && Ctx.pDesc->IsTracking();
pParticles->m_pEffectPainter = Ctx.pDesc->CreateSecondaryPainter(bIsTracking, true);
// Particle Painter
pParticles->m_pParticlePainter = Ctx.pDesc->CreateParticlePainter();
// Remember the sovereign of the source (in case the source is destroyed)
if (Ctx.Source.GetObj())
pParticles->m_pSovereign = Ctx.Source.GetObj()->GetSovereign();
else
pParticles->m_pSovereign = NULL;
// Compute the maximum number of particles that we might have
int iMaxCount = pParticles->m_iEmitTime * pSystemDesc->GetEmitRate().GetMaxValue();
pParticles->m_Particles.Init(iMaxCount);
// NOTE: We use the source velocity (instead of vVel) because Emit expects
// to add the particle velocity.
CVector vInitialVel;
if (!Ctx.Source.IsEmpty()
&& !pParticles->m_pDesc->IsTracking()
&& !(Ctx.dwFlags & SShotCreateCtx::CWF_FRAGMENT))
vInitialVel = Ctx.Source.GetObj()->GetVel();
// Create the initial particles.
int iInitCount;
pParticles->m_Particles.Emit(*pSystemDesc,
pParticles,
Ctx.vPos - pParticles->GetOrigin(),
vInitialVel,
Ctx.iDirection,
0,
&iInitCount);
// Figure out the number of particles that will cause full damage
if (pParticles->m_iEmitTime > 1)
pParticles->m_iParticleCount = pParticles->m_iEmitTime * pSystemDesc->GetEmitRate().GetAveValue();
else
pParticles->m_iParticleCount = iInitCount;
pParticles->m_iParticleCount = Max(1, pParticles->m_iParticleCount);
// Add to system
if (error = pParticles->AddToSystem(pSystem))
{
delete pParticles;
return error;
}
// Done
if (retpObj)
*retpObj = pParticles;
return NOERROR;
}
CString CParticleDamage::GetNamePattern (DWORD dwNounPhraseFlags, DWORD *retdwFlags) const
// GetName
//
// Returns the name of the object
{
// This name is used only if the source has been destroyed
if (retdwFlags)
*retdwFlags = 0;
return CONSTLIT("enemy weapon");
}
void CParticleDamage::OnDestroyed (SDestroyCtx &Ctx)
// OnDestroyed
//
// Shot destroyed
{
m_pDesc->FireOnDestroyShot(this);
}
void CParticleDamage::OnMove (const CVector &vOldPos, Metric rSeconds)
// OnMove
//
// Handle moving
{
// Update the painter motion
// Note that we do this even if we're destroyed because we might have
// some fading particles.
RECT rcEffectBounds;
if (m_pEffectPainter
&& WasPainted())
{
SEffectMoveCtx Ctx;
Ctx.pObj = this;
Ctx.vOldPos = vOldPos;
m_pEffectPainter->OnMove(Ctx);
// Get the bounds (we need to always get it because we always set it
// below).
m_pEffectPainter->GetBounds(&rcEffectBounds);
}
// If we're destroyed but just waiting for the effect to fade out, then we
// update bounds (but otherwise we're done).
if (m_fPainterFade)
{
if (m_pEffectPainter)
SetBounds(g_KlicksPerPixel * Max(RectWidth(rcEffectBounds), RectHeight(rcEffectBounds)));
// Continue moving (otherwise, exhaust trail effects won't work
// properly).
SetPos(vOldPos + (GetVel() * g_SecondsPerUpdate));
return;
}
// Update the single particle painter
if (m_pParticlePainter)
{
SEffectMoveCtx Ctx;
Ctx.pObj = this;
m_pParticlePainter->OnMove(Ctx);
}
// Update particle motion
bool bAlive;
CVector vNewPos;
m_Particles.UpdateMotionLinear(&bAlive, &vNewPos);
// If we're still alive, then set our position and bounds based on the
// particles.
if (bAlive)
{
// Set the position of the object based on the average particle position
SetPos(vNewPos);
// Set the bounds (note, we make the bounds twice as large to deal
// with the fact that we're moving).
RECT rcBounds = m_Particles.GetBounds();
if (m_pEffectPainter)
::UnionRect(&rcBounds, &rcBounds, &rcEffectBounds);
SetBounds(g_KlicksPerPixel * Max(RectWidth(rcBounds), RectHeight(rcBounds)));
}
// If we stay around for some fading effects, then set it up.
else if (SetMissileFade())
{
if (m_pEffectPainter)
SetBounds(g_KlicksPerPixel * Max(RectWidth(rcEffectBounds), RectHeight(rcEffectBounds)));
}
// Otherwise, we're dead
else
Destroy(removedFromSystem, CDamageSource());
}
void CParticleDamage::ObjectDestroyedHook (const SDestroyCtx &Ctx)
// ObjectDestroyedHook
//
// Called when another object is destroyed
{
m_Source.OnObjDestroyed(Ctx.pObj);
if (Ctx.pObj == m_pTarget)
m_pTarget = NULL;
}
void CParticleDamage::OnPaint (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx)
// OnPaint
//
// Paint
{
const CParticleSystemDesc *pSystemDesc = m_pDesc->GetParticleSystemDesc();
ASSERT(pSystemDesc);
if (pSystemDesc == NULL)
return;
CViewportPaintCtxSmartSave Save(Ctx);
Ctx.iTick = m_iTick;
Ctx.iVariant = 0;
Ctx.iRotation = 0;
Ctx.iDestiny = GetDestiny();
Ctx.iMaxLength = (int)((g_SecondsPerUpdate * Max(1, m_iTick) * m_pDesc->GetRatedSpeed()) / g_KlicksPerPixel);
// Start by painting the secondary effect, if any
if (m_pEffectPainter)
{
Ctx.iRotation = m_iRotation;
if (m_fPainterFade)
m_pEffectPainter->PaintFade(Dest, x, y, Ctx);
else
m_pEffectPainter->Paint(Dest, x, y, Ctx);
Ctx.iRotation = 0;
}
// If we're just fading out, then we're done
if (m_fPainterFade)
return;
// Painting is relative to the origin
int xOrigin, yOrigin;
Ctx.XForm.Transform(GetOrigin(), &xOrigin, &yOrigin);
// If we can get a paint descriptor, use that because it is faster
m_Particles.Paint(*pSystemDesc, Dest, xOrigin, yOrigin, m_pParticlePainter, Ctx);
}
void CParticleDamage::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Restore from stream
{
DWORD dwLoad;
#ifdef DEBUG_LOAD
::OutputDebugString("CParticleDamage::OnReadFromStream\n");
#endif
// Load descriptor
CString sDescUNID;
sDescUNID.ReadFromStream(Ctx.pStream);
m_pDesc = g_pUniverse->FindWeaponFireDesc(sDescUNID);
// Old style bonus
if (Ctx.dwVersion < 92)
{
int iBonus;
Ctx.pStream->Read((char *)&iBonus, sizeof(DWORD));
if (iBonus != 0)
{
m_pEnhancements.TakeHandoff(new CItemEnhancementStack);
m_pEnhancements->InsertHPBonus(iBonus);
}
}
// Load other stuff
if (Ctx.dwVersion >= 18 && Ctx.dwVersion < 137)
Ctx.pStream->Read((char *)&dwLoad, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iLifeLeft, sizeof(m_iLifeLeft));
m_Source.ReadFromStream(Ctx);
CSystem::ReadSovereignRefFromStream(Ctx, &m_pSovereign);
Ctx.pStream->Read((char *)&m_iTick, sizeof(m_iTick));
if (Ctx.dwVersion >= 120)
Ctx.pStream->Read((char *)&m_iRotation, sizeof(DWORD));
else
m_iRotation = VectorToPolar(GetVel());
Ctx.pStream->Read((char *)&m_iDamage, sizeof(m_iDamage));
if (Ctx.dwVersion >= 3 && Ctx.dwVersion < 67)
{
CVector vDummy;
Ctx.pStream->Read((char *)&vDummy, sizeof(CVector));
}
// The newer version uses a different particle array
if (Ctx.dwVersion >= 21)
{
Ctx.pStream->Read((char *)&m_vEmitSourcePos, sizeof(CVector));
Ctx.pStream->Read((char *)&m_vEmitSourceVel, sizeof(CVector));
Ctx.pStream->Read((char *)&m_iEmitDirection, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iEmitTime, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iParticleCount, sizeof(DWORD));
// Load painter
m_pParticlePainter = CEffectCreator::CreatePainterFromStreamAndCreator(Ctx, m_pDesc->GetParticleEffect());
m_Particles.ReadFromStream(Ctx);
}
// Read the previous version, but no need to convert
else
{
DWORD dwCount;
Ctx.pStream->Read((char *)&dwCount, sizeof(DWORD));
if (dwCount > 0)
{
char *pDummy = new char [5 * sizeof(DWORD) * dwCount];
Ctx.pStream->Read(pDummy, 5 * sizeof(DWORD) * dwCount);
delete pDummy;
}
m_iEmitTime = 0;
m_iEmitDirection = -1;
}
// Read the target
if (Ctx.dwVersion >= 67)
CSystem::ReadObjRefFromStream(Ctx, &m_pTarget);
else
m_pTarget = NULL;
// For now we don't bother saving the effect painter
m_pEffectPainter = NULL;
// Enhancements
if (Ctx.dwVersion >= 92)
m_pEnhancements = CItemEnhancementStack::ReadFromStream(Ctx);
// Flags
DWORD dwFlags = 0;
if (Ctx.dwVersion >= 120)
Ctx.pStream->Read((char *)&dwFlags, sizeof(DWORD));
m_fPainterFade = ((dwFlags & 0x00000010) ? true : false);
}
void CParticleDamage::OnUpdate (SUpdateCtx &Ctx, Metric rSecondsPerTick)
// OnUpdate
//
// Update
{
DEBUG_TRY
const CParticleSystemDesc *pSystemDesc = m_pDesc->GetParticleSystemDesc();
ASSERT(pSystemDesc);
if (pSystemDesc == NULL)
return;
m_iTick++;
// Set up context block for particle array update
SEffectUpdateCtx EffectCtx;
EffectCtx.pSystem = GetSystem();
EffectCtx.pObj = this;
EffectCtx.iTick = m_iTick;
EffectCtx.pDamageDesc = m_pDesc;
EffectCtx.iTotalParticleCount = m_iParticleCount;
EffectCtx.pEnhancements = m_pEnhancements;
EffectCtx.iCause = m_Source.GetCause();
EffectCtx.Attacker = m_Source;
EffectCtx.pTarget = m_pTarget;
// Update the effect painter
if (m_pEffectPainter
&& WasPainted())
{
SEffectUpdateCtx PainterCtx;
PainterCtx.pObj = this;
PainterCtx.iTick = m_iTick;
PainterCtx.bFade = m_fPainterFade;
m_pEffectPainter->OnUpdate(PainterCtx);
}
// If we're fading, then nothing else to do
if (m_fPainterFade)
{
if (--m_iLifeLeft <= 0)
Destroy(removedFromSystem, CDamageSource());
return;
}
// Update the single particle painter
if (m_pParticlePainter)
m_pParticlePainter->OnUpdate();
// Update (includes doing damage)
m_Particles.Update(*pSystemDesc, EffectCtx);
// Expired?
if (--m_iLifeLeft <= 0)
{
if (SetMissileFade())
{ }
else
Destroy(removedFromSystem, CDamageSource());
return;
}
// Emit new particles
if (m_iTick < m_iEmitTime && !m_Source.IsEmpty())
{
// Rotate the offsets appropriately
int iRotation = m_Source.GetObj()->GetRotation();
CVector vPos = m_vEmitSourcePos.Rotate(iRotation);
CVector vVel = m_vEmitSourceVel.Rotate(iRotation);
// Emit
m_Particles.Emit(*pSystemDesc,
this,
m_Source.GetObj()->GetPos() + vPos - GetOrigin(),
m_Source.GetObj()->GetVel() + vVel,
AngleMod(iRotation + m_iEmitDirection),
m_iTick);
}
DEBUG_CATCH
}
void CParticleDamage::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write out to stream
//
// CString CWeaponFireDesc UNID
// DWORD m_iLifeLeft
// DWORD m_Source (CSpaceObject ref)
// DWORD m_pSovereign (CSovereign ref)
// DWORD m_iTick
// DWORD m_iRotation
// DWORD m_iDamage
//
// CVector m_vEmitSourcePos
// CVector m_vEmitSourceVel
// DWORD m_iEmitDirection
// DWORD m_iEmitTime
// DWORD m_iParticleCount
// IEffectPainter
// CParticleArray
//
// CSpaceObject m_pTarget
//
// CItemEnhancementStack m_pEnhancements
//
// DWORD Flags
{
DWORD dwSave;
m_pDesc->GetUNID().WriteToStream(pStream);
pStream->Write((char *)&m_iLifeLeft, sizeof(m_iLifeLeft));
m_Source.WriteToStream(GetSystem(), pStream);
GetSystem()->WriteSovereignRefToStream(m_pSovereign, pStream);
pStream->Write((char *)&m_iTick, sizeof(m_iTick));
pStream->Write((char *)&m_iRotation, sizeof(m_iRotation));
pStream->Write((char *)&m_iDamage, sizeof(m_iDamage));
pStream->Write((char *)&m_vEmitSourcePos, sizeof(CVector));
pStream->Write((char *)&m_vEmitSourceVel, sizeof(CVector));
pStream->Write((char *)&m_iEmitDirection, sizeof(DWORD));
pStream->Write((char *)&m_iEmitTime, sizeof(DWORD));
pStream->Write((char *)&m_iParticleCount, sizeof(DWORD));
CEffectCreator::WritePainterToStream(pStream, m_pParticlePainter);
m_Particles.WriteToStream(pStream);
WriteObjRefToStream(m_pTarget, pStream);
// Enhancements
CItemEnhancementStack::WriteToStream(m_pEnhancements, pStream);
// Flags
dwSave = 0;
dwSave |= (m_fPainterFade ? 0x00000001 : 0);
pStream->Write((char *)&dwSave, sizeof(DWORD));
}
bool CParticleDamage::PointInObject (const CVector &vObjPos, const CVector &vPointPos) const
// PointInObject
//
// Returns TRUE if the given point is in the object
{
return false;
}
bool CParticleDamage::SetMissileFade (void)
// SetMissileFade
//
// Missile is destroyed, but we keep it alive to paint any effects that fade.
// We return TRUE if the missile should be kept alive.
{
// If we've got an effect that needs time to fade out, then keep
// the missile object alive
int iPainterFadeLife;
if (m_pEffectPainter && (iPainterFadeLife = m_pEffectPainter->GetFadeLifetime(m_iLifeLeft > 0)))
{
m_pEffectPainter->OnBeginFade();
m_fPainterFade = true;
m_iLifeLeft = iPainterFadeLife;
return true;
}
// Otherwise, we don't stay alive
return false;
}