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
|
/*
* The ManaPlus Client
* Copyright (C) 2006-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2017 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* 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 2 of the License, or
* 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/>.
*/
#include "particle/particle.h"
#include "actormanager.h"
#include "logger.h"
#include "being/actorsprite.h"
#include "particle/animationparticle.h"
#include "particle/particleemitter.h"
#include "particle/rotationalparticle.h"
#include "resources/animation/simpleanimation.h"
#include "resources/dye/dye.h"
#include "resources/image/image.h"
#include "resources/loaders/imageloader.h"
#include "resources/loaders/xmlloader.h"
#include "utils/delete2.h"
#include "utils/dtor.h"
#include "utils/foreach.h"
#include "utils/mathutils.h"
#include "utils/mrand.h"
#include "debug.h"
static const float SIN45 = 0.707106781F;
static const double PI = M_PI;
static const float PI2 = 2 * M_PI;
class Graphics;
Particle::Particle() :
Actor(),
mAlpha(1.0F),
mLifetimeLeft(-1),
mLifetimePast(0),
mFadeOut(0),
mFadeIn(0),
mVelocity(),
mAlive(AliveStatus::ALIVE),
mType(ParticleType::Normal),
mAnimation(nullptr),
mImage(nullptr),
mActor(BeingId_zero),
mChildEmitters(),
mChildParticles(),
mChildMoveParticles(),
mDeathEffect(),
mGravity(0.0F),
mBounce(0.0F),
mAcceleration(0.0F),
mInvDieDistance(-1.0F),
mMomentum(1.0F),
mTarget(nullptr),
mRandomness(0),
mDeathEffectConditions(0x00),
mAutoDelete(true),
mAllowSizeAdjust(false),
mFollow(false)
{
ParticleEngine::particleCount++;
}
Particle::~Particle()
{
if (mActor != BeingId_zero &&
(actorManager != nullptr))
{
ActorSprite *const actor = actorManager->findActor(mActor);
if (actor != nullptr)
actor->controlParticleDeleted(this);
}
// Delete child emitters and child particles
clear();
delete2(mAnimation);
if (mImage != nullptr)
{
if (mType == ParticleType::Image)
{
const std::string &restrict name = mImage->mIdPath;
StringIntMapIter it
= ImageParticle::imageParticleCountByName.find(name);
if (it != ImageParticle::imageParticleCountByName.end())
{
int &cnt = (*it).second;
if (cnt > 0)
cnt --;
}
mImage->decRef();
}
mImage = nullptr;
}
ParticleEngine::particleCount--;
}
void Particle::draw(Graphics *restrict const graphics A_UNUSED,
const int offsetX A_UNUSED,
const int offsetY A_UNUSED) const restrict2
{
}
void Particle::updateSelf() restrict2
{
// calculate particle movement
if (A_LIKELY(mMomentum != 1.0F))
mVelocity *= mMomentum;
if ((mTarget != nullptr) && mAcceleration != 0.0F)
{
Vector dist = mPos - mTarget->mPos;
dist.x *= SIN45;
float invHypotenuse;
switch (ParticleEngine::fastPhysics)
{
case 1:
invHypotenuse = fastInvSqrt(
dist.x * dist.x + dist.y * dist.y + dist.z * dist.z);
break;
case 2:
if (dist.x == 0.0f)
{
invHypotenuse = 0;
break;
}
invHypotenuse = 2.0F / (static_cast<float>(fabs(dist.x))
+ static_cast<float>(fabs(dist.y))
+ static_cast<float>(fabs(dist.z)));
break;
default:
invHypotenuse = 1.0F / static_cast<float>(sqrt(
dist.x * dist.x + dist.y * dist.y + dist.z * dist.z));
break;
}
if (invHypotenuse != 0.0f)
{
if (mInvDieDistance > 0.0F && invHypotenuse > mInvDieDistance)
mAlive = AliveStatus::DEAD_IMPACT;
const float accFactor = invHypotenuse * mAcceleration;
mVelocity -= dist * accFactor;
}
}
if (A_LIKELY(mRandomness >= 10)) // reduce useless calculations
{
const int rand2 = mRandomness * 2;
mVelocity.x += static_cast<float>(mrand() % rand2 - mRandomness)
/ 1000.0F;
mVelocity.y += static_cast<float>(mrand() % rand2 - mRandomness)
/ 1000.0F;
mVelocity.z += static_cast<float>(mrand() % rand2 - mRandomness)
/ 1000.0F;
}
mVelocity.z -= mGravity;
// Update position
mPos.x += mVelocity.x;
mPos.y += mVelocity.y * SIN45;
mPos.z += mVelocity.z * SIN45;
// Update other stuff
if (A_LIKELY(mLifetimeLeft > 0))
mLifetimeLeft--;
mLifetimePast++;
if (mPos.z < 0.0F)
{
if (mBounce > 0.0F)
{
mPos.z *= -mBounce;
mVelocity *= mBounce;
mVelocity.z = -mVelocity.z;
}
else
{
mAlive = AliveStatus::DEAD_FLOOR;
}
}
else if (mPos.z > ParticleEngine::PARTICLE_SKY)
{
mAlive = AliveStatus::DEAD_SKY;
}
// Update child emitters
if ((ParticleEngine::emitterSkip != 0) &&
(mLifetimePast - 1) % ParticleEngine::emitterSkip == 0)
{
FOR_EACH (EmitterConstIterator, e, mChildEmitters)
{
std::vector<Particle*> newParticles;
(*e)->createParticles(mLifetimePast, newParticles);
FOR_EACH (std::vector<Particle*>::const_iterator,
it,
newParticles)
{
Particle *const p = *it;
p->moveBy(mPos);
mChildParticles.push_back(p);
if (p->mFollow)
mChildMoveParticles.push_back(p);
}
}
}
// create death effect when the particle died
if (A_UNLIKELY(mAlive != AliveStatus::ALIVE &&
mAlive != AliveStatus::DEAD_LONG_AGO))
{
if ((CAST_U32(mAlive) & mDeathEffectConditions)
> 0x00 && !mDeathEffect.empty())
{
Particle *restrict const deathEffect = particleEngine->addEffect(
mDeathEffect, 0, 0);
if (deathEffect != nullptr)
deathEffect->moveBy(mPos);
}
mAlive = AliveStatus::DEAD_LONG_AGO;
}
}
bool Particle::update() restrict2
{
if (A_LIKELY(mAlive == AliveStatus::ALIVE))
{
if (A_UNLIKELY(mLifetimeLeft == 0))
{
mAlive = AliveStatus::DEAD_TIMEOUT;
if (mChildParticles.empty())
{
if (mAutoDelete)
return false;
return true;
}
}
else
{
if (mAnimation != nullptr)
{
if (mType == ParticleType::Animation)
{
// particle engine is updated every 10ms
mAnimation->update(10);
}
else // ParticleType::Rotational
{
// TODO: cache velocities to avoid spamming atan2()
const int size = mAnimation->getLength();
if (size == 0)
return false;
float rad = static_cast<float>(atan2(mVelocity.x,
mVelocity.y));
if (rad < 0)
rad = PI2 + rad;
const float range = static_cast<float>(PI / size);
// Determines which frame the particle should play
if (A_UNLIKELY(rad < range || rad > PI2 - range))
{
mAnimation->setFrame(0);
}
else
{
const float range2 = 2 * range;
// +++ need move condition outside of for
for (int c = 1; c < size; c++)
{
const float cRange = static_cast<float>(c) *
range2;
if (cRange - range < rad &&
rad < cRange + range)
{
mAnimation->setFrame(c);
break;
}
}
}
}
mImage = mAnimation->getCurrentImage();
}
const Vector oldPos = mPos;
updateSelf();
const Vector change = mPos - oldPos;
if (mChildParticles.empty())
{
if (mAlive != AliveStatus::ALIVE &&
mAutoDelete)
{
return false;
}
return true;
}
for (ParticleIterator p = mChildMoveParticles.begin(),
fp2 = mChildMoveParticles.end(); p != fp2; )
{
// move particle with its parent if desired
(*p)->moveBy(change);
++p;
}
}
// Update child particles
for (ParticleIterator p = mChildParticles.begin(),
fp2 = mChildParticles.end(); p != fp2; )
{
Particle *restrict const particle = *p;
// update particle
if (A_LIKELY(particle->update()))
{
++p;
}
else
{
mChildMoveParticles.remove(*p);
delete particle;
p = mChildParticles.erase(p);
}
}
if (A_UNLIKELY(mAlive != AliveStatus::ALIVE &&
mChildParticles.empty() &&
mAutoDelete))
{
return false;
}
}
else
{
if (mChildParticles.empty())
{
if (mAutoDelete)
return false;
return true;
}
// Update child particles
for (ParticleIterator p = mChildParticles.begin(),
fp2 = mChildParticles.end(); p != fp2; )
{
Particle *restrict const particle = *p;
// update particle
if (A_LIKELY(particle->update()))
{
++p;
}
else
{
mChildMoveParticles.remove(*p);
delete particle;
p = mChildParticles.erase(p);
}
}
if (A_UNLIKELY(mChildParticles.empty() &&
mAutoDelete))
{
return false;
}
}
return true;
}
void Particle::moveBy(const Vector &restrict change) restrict2
{
mPos += change;
FOR_EACH (ParticleConstIterator, p, mChildMoveParticles)
{
(*p)->moveBy(change);
}
}
void Particle::moveTo(const float x, const float y) restrict2
{
moveTo(Vector(x, y, mPos.z));
}
Particle *Particle::addEffect(const std::string &restrict particleEffectFile,
const int pixelX, const int pixelY,
const int rotation) restrict2
{
Particle *newParticle = nullptr;
const size_t pos = particleEffectFile.find('|');
const std::string dyePalettes = (pos != std::string::npos)
? particleEffectFile.substr(pos + 1) : "";
XML::Document *doc = Loader::getXml(particleEffectFile.substr(0, pos),
UseVirtFs_true,
SkipError_false);
if (doc == nullptr)
return nullptr;
XmlNodeConstPtrConst rootNode = doc->rootNode();
if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "effect"))
{
logger->log("Error loading particle: %s", particleEffectFile.c_str());
doc->decRef();
return nullptr;
}
// Parse particles
for_each_xml_child_node(effectChildNode, rootNode)
{
// We're only interested in particles
if (!xmlNameEqual(effectChildNode, "particle"))
continue;
// Determine the exact particle type
XmlNodePtr node;
// Animation
if ((node = XML::findFirstChildByName(effectChildNode, "animation")) !=
nullptr)
{
newParticle = new AnimationParticle(node, dyePalettes);
newParticle->setMap(mMap);
}
// Rotational
else if ((node = XML::findFirstChildByName(
effectChildNode, "rotation")) != nullptr)
{
newParticle = new RotationalParticle(node, dyePalettes);
newParticle->setMap(mMap);
}
// Image
else if ((node = XML::findFirstChildByName(effectChildNode,
"image")) != nullptr)
{
std::string imageSrc;
if (XmlHaveChildContent(node))
imageSrc = XmlChildContent(node);
if (!imageSrc.empty() && !dyePalettes.empty())
Dye::instantiate(imageSrc, dyePalettes);
Image *const img = Loader::getImage(imageSrc);
newParticle = new ImageParticle(img);
newParticle->setMap(mMap);
}
// Other
else
{
newParticle = new Particle;
newParticle->setMap(mMap);
}
// Read and set the basic properties of the particle
const float offsetX = static_cast<float>(XML::getFloatProperty(
effectChildNode, "position-x", 0));
const float offsetY = static_cast<float>(XML::getFloatProperty(
effectChildNode, "position-y", 0));
const float offsetZ = static_cast<float>(XML::getFloatProperty(
effectChildNode, "position-z", 0));
const Vector position(mPos.x + static_cast<float>(pixelX) + offsetX,
mPos.y + static_cast<float>(pixelY) + offsetY,
mPos.z + offsetZ);
newParticle->moveTo(position);
const int lifetime = XML::getProperty(effectChildNode, "lifetime", -1);
newParticle->setLifetime(lifetime);
const bool resizeable = "false" != XML::getProperty(effectChildNode,
"size-adjustable", "false");
newParticle->setAllowSizeAdjust(resizeable);
// Look for additional emitters for this particle
for_each_xml_child_node(emitterNode, effectChildNode)
{
if (xmlNameEqual(emitterNode, "emitter"))
{
ParticleEmitter *restrict const newEmitter =
new ParticleEmitter(
emitterNode,
newParticle,
mMap,
rotation,
dyePalettes);
newParticle->addEmitter(newEmitter);
}
else if (xmlNameEqual(emitterNode, "deatheffect"))
{
std::string deathEffect;
if ((node != nullptr) && XmlHaveChildContent(node))
deathEffect = XmlChildContent(emitterNode);
char deathEffectConditions = 0x00;
if (XML::getBoolProperty(emitterNode, "on-floor", true))
{
deathEffectConditions += CAST_S8(
AliveStatus::DEAD_FLOOR);
}
if (XML::getBoolProperty(emitterNode, "on-sky", true))
{
deathEffectConditions += CAST_S8(
AliveStatus::DEAD_SKY);
}
if (XML::getBoolProperty(emitterNode, "on-other", false))
{
deathEffectConditions += CAST_S8(
AliveStatus::DEAD_OTHER);
}
if (XML::getBoolProperty(emitterNode, "on-impact", true))
{
deathEffectConditions += CAST_S8(
AliveStatus::DEAD_IMPACT);
}
if (XML::getBoolProperty(emitterNode, "on-timeout", true))
{
deathEffectConditions += CAST_S8(
AliveStatus::DEAD_TIMEOUT);
}
newParticle->setDeathEffect(
deathEffect, deathEffectConditions);
}
}
mChildParticles.push_back(newParticle);
}
doc->decRef();
return newParticle;
}
void Particle::adjustEmitterSize(const int w, const int h) restrict2
{
if (mAllowSizeAdjust)
{
FOR_EACH (EmitterConstIterator, e, mChildEmitters)
(*e)->adjustSize(w, h);
}
}
void Particle::prepareToDie() restrict2
{
FOR_EACH (ParticleIterator, p, mChildParticles)
{
Particle *restrict const particle = *p;
if (particle == nullptr)
continue;
particle->prepareToDie();
if (particle->isAlive() &&
particle->mLifetimeLeft == -1 &&
particle->mAutoDelete)
{
particle->kill();
}
}
}
void Particle::clear() restrict2
{
delete_all(mChildEmitters);
mChildEmitters.clear();
delete_all(mChildParticles);
mChildParticles.clear();
mChildMoveParticles.clear();
}
|