metaforce/Runtime/Particle/CIntElement.cpp

303 lines
7.7 KiB
C++
Raw Permalink Normal View History

2019-10-04 23:20:04 +00:00
#include "Runtime/Particle/CIntElement.hpp"
#include <algorithm>
2019-10-04 23:20:04 +00:00
#include "Runtime/CRandom16.hpp"
#include "Runtime/Particle/CElementGen.hpp"
#include "Runtime/Particle/CGenDescription.hpp"
#include "Runtime/Particle/CParticleGlobals.hpp"
2016-04-03 05:25:34 +00:00
/* Documentation at: https://wiki.axiodl.com/w/Particle_Script#Int_Elements */
2016-02-28 06:55:05 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2018-12-08 05:30:43 +00:00
CIEKeyframeEmitter::CIEKeyframeEmitter(CInputStream& in) {
x4_percent = in.readUint32Big();
x8_unk1 = in.readUint32Big();
xc_loop = in.readBool();
xd_unk2 = in.readBool();
x10_loopEnd = in.readUint32Big();
x14_loopStart = in.readUint32Big();
u32 count = in.readUint32Big();
x18_keys.reserve(count);
for (u32 i = 0; i < count; ++i)
x18_keys.push_back(in.readInt32Big());
}
bool CIEKeyframeEmitter::GetValue([[maybe_unused]] int frame, int& valOut) const {
2018-12-08 05:30:43 +00:00
if (!x4_percent) {
int emitterTime = CParticleGlobals::instance()->m_EmitterTime;
2018-12-08 05:30:43 +00:00
int calcKey = emitterTime;
if (xc_loop) {
if (emitterTime >= x10_loopEnd) {
int v1 = emitterTime - x14_loopStart;
int v2 = x10_loopEnd - x14_loopStart;
calcKey = v1 % v2;
calcKey += x14_loopStart;
}
} else {
int v1 = x10_loopEnd - 1;
if (v1 < emitterTime)
calcKey = v1;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
valOut = x18_keys[calcKey];
} else {
int ltPerc = CParticleGlobals::instance()->m_ParticleLifetimePercentage;
float ltPercRem = CParticleGlobals::instance()->m_ParticleLifetimePercentageRemainder;
2018-12-08 05:30:43 +00:00
if (ltPerc == 100)
valOut = x18_keys[100];
2016-02-07 00:19:59 +00:00
else
2018-12-08 05:30:43 +00:00
valOut = ltPercRem * x18_keys[ltPerc + 1] + (1.0f - ltPercRem) * x18_keys[ltPerc];
}
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEKeyframeEmitter::GetMaxValue() const {
return *std::max_element(x18_keys.cbegin(), x18_keys.cend());
2018-06-08 01:15:46 +00:00
}
2018-12-08 05:30:43 +00:00
bool CIEDeath::GetValue(int frame, int& valOut) const {
x4_a->GetValue(frame, valOut);
int b;
x8_b->GetValue(frame, b);
/* Not 100% sure about this, originally some kinda branchless comparison */
return frame > b;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEDeath::GetMaxValue() const { return x4_a->GetMaxValue(); }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIEClamp::GetValue(int frame, int& valOut) const {
int a, b;
x4_min->GetValue(frame, a);
x8_max->GetValue(frame, b);
xc_val->GetValue(frame, valOut);
valOut = std::clamp(valOut, a, b);
2018-12-08 05:30:43 +00:00
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEClamp::GetMaxValue() const {
const int a = x4_min->GetMaxValue();
const int b = x8_max->GetMaxValue();
const int valOut = xc_val->GetMaxValue();
return std::clamp(valOut, a, b);
2018-06-08 01:15:46 +00:00
}
2018-12-08 05:30:43 +00:00
bool CIETimeChain::GetValue(int frame, int& valOut) const {
int v;
xc_swFrame->GetValue(frame, v);
if (frame >= v)
return x8_b->GetValue(frame, valOut);
else
return x4_a->GetValue(frame, valOut);
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIETimeChain::GetMaxValue() const { return std::max(x8_b->GetMaxValue(), x4_a->GetMaxValue()); }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIEAdd::GetValue(int frame, int& valOut) const {
int a, b;
x4_a->GetValue(frame, a);
x8_b->GetValue(frame, b);
valOut = a + b;
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEAdd::GetMaxValue() const {
const int a = x4_a->GetMaxValue();
const int b = x8_b->GetMaxValue();
2018-12-08 05:30:43 +00:00
return a + b;
2018-06-08 01:15:46 +00:00
}
bool CIEConstant::GetValue([[maybe_unused]] int frame, int& valOut) const {
2018-12-08 05:30:43 +00:00
valOut = x4_val;
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEConstant::GetMaxValue() const { return x4_val; }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIEImpulse::GetValue(int frame, int& valOut) const {
if (frame == 0)
x4_a->GetValue(frame, valOut);
else
valOut = 0;
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEImpulse::GetMaxValue() const { return x4_a->GetMaxValue(); }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIELifetimePercent::GetValue(int frame, int& valOut) const {
int a;
x4_percentVal->GetValue(frame, a);
a = std::max(0, a);
valOut = (a / 100.0f) * CParticleGlobals::instance()->m_ParticleLifetimeReal + 0.5f;
2018-12-08 05:30:43 +00:00
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIELifetimePercent::GetMaxValue() const {
const int a = std::max(0, x4_percentVal->GetMaxValue());
// Assume 10000 frames max (not ideal estimate)
return int((float(a) / 100.0f) * 10000 + 0.5f);
2018-06-08 01:15:46 +00:00
}
2018-12-08 05:30:43 +00:00
bool CIEInitialRandom::GetValue(int frame, int& valOut) const {
if (frame == 0) {
int a, b;
x4_a->GetValue(frame, a);
x8_b->GetValue(frame, b);
valOut = CRandom16::GetRandomNumber()->Range(a, b);
}
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEInitialRandom::GetMaxValue() const { return x8_b->GetMaxValue(); }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIEPulse::GetValue(int frame, int& valOut) const {
int a, b;
x4_aDuration->GetValue(frame, a);
x8_bDuration->GetValue(frame, b);
2020-04-12 09:31:00 +00:00
int cv = a + b + 1;
if (cv < 0) {
cv = 1;
}
2018-12-08 05:30:43 +00:00
2020-04-12 09:31:00 +00:00
if (b < 1 || frame % cv <= a) {
2018-12-08 05:30:43 +00:00
xc_aVal->GetValue(frame, valOut);
2020-04-12 09:31:00 +00:00
} else {
x10_bVal->GetValue(frame, valOut);
}
2018-12-08 05:30:43 +00:00
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEPulse::GetMaxValue() const { return std::max(xc_aVal->GetMaxValue(), x10_bVal->GetMaxValue()); }
2016-02-07 00:19:59 +00:00
2018-12-08 05:30:43 +00:00
bool CIEMultiply::GetValue(int frame, int& valOut) const {
int a, b;
x4_a->GetValue(frame, a);
x8_b->GetValue(frame, b);
valOut = a * b;
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEMultiply::GetMaxValue() const { return x4_a->GetMaxValue() * x8_b->GetMaxValue(); }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIESampleAndHold::GetValue(int frame, int& valOut) const {
if (x8_nextSampleFrame < frame) {
int b, c;
xc_waitFramesMin->GetValue(frame, b);
x10_waitFramesMax->GetValue(frame, c);
x8_nextSampleFrame = CRandom16::GetRandomNumber()->Range(b, c) + frame;
2018-12-08 05:30:43 +00:00
x4_sampleSource->GetValue(frame, valOut);
x14_holdVal = valOut;
} else {
2018-12-08 05:30:43 +00:00
valOut = x14_holdVal;
}
2018-12-08 05:30:43 +00:00
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIESampleAndHold::GetMaxValue() const { return x4_sampleSource->GetMaxValue(); }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIERandom::GetValue(int frame, int& valOut) const {
int a, b;
x4_min->GetValue(frame, a);
x8_max->GetValue(frame, b);
if (a > 0)
valOut = CRandom16::GetRandomNumber()->Range(a, b);
else
valOut = CRandom16::GetRandomNumber()->Next();
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIERandom::GetMaxValue() const {
if (x4_min->GetMaxValue() > 0)
return x8_max->GetMaxValue();
else
return 65535;
2018-06-08 01:15:46 +00:00
}
2018-12-08 05:30:43 +00:00
bool CIETimeScale::GetValue(int frame, int& valOut) const {
float a;
x4_a->GetValue(frame, a);
valOut = float(frame) * a;
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIETimeScale::GetMaxValue() const { return 10000; /* Assume 10000 frames max (not ideal estimate) */ }
2018-06-08 01:15:46 +00:00
bool CIEGetCumulativeParticleCount::GetValue([[maybe_unused]] int frame, int& valOut) const {
valOut = CParticleGlobals::instance()->m_currentParticleSystem->x4_system->GetCumulativeParticleCount();
2018-12-08 05:30:43 +00:00
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEGetCumulativeParticleCount::GetMaxValue() const { return 256; }
2018-06-08 01:15:46 +00:00
bool CIEGetActiveParticleCount::GetValue([[maybe_unused]] int frame, int& valOut) const {
valOut = CParticleGlobals::instance()->m_currentParticleSystem->x4_system->GetParticleCount();
2018-12-08 05:30:43 +00:00
return false;
2016-02-11 20:32:42 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEGetActiveParticleCount::GetMaxValue() const { return 256; }
2018-06-08 01:15:46 +00:00
bool CIEGetEmitterTime::GetValue([[maybe_unused]] int frame, int& valOut) const {
valOut = CParticleGlobals::instance()->m_currentParticleSystem->x4_system->GetEmitterTime();
2018-12-08 05:30:43 +00:00
return false;
2016-02-11 20:32:42 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEGetEmitterTime::GetMaxValue() const { return 10000; /* Assume 10000 frames max (not ideal estimate) */ }
2018-06-08 01:15:46 +00:00
2018-12-08 05:30:43 +00:00
bool CIEModulo::GetValue(int frame, int& valOut) const {
int a, b;
x4_a->GetValue(frame, a);
x8_b->GetValue(frame, b);
if (b != 0)
valOut = a % b;
else
valOut = a;
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIEModulo::GetMaxValue() const {
const int a = x4_a->GetMaxValue();
const int b = x8_b->GetMaxValue();
if (b != 0) {
2018-12-08 05:30:43 +00:00
return b - 1;
}
return a;
2018-06-08 01:15:46 +00:00
}
2018-12-08 05:30:43 +00:00
bool CIESubtract::GetValue(int frame, int& valOut) const {
int a, b;
x4_a->GetValue(frame, a);
x8_b->GetValue(frame, b);
valOut = a - b;
return false;
2016-02-07 00:19:59 +00:00
}
2018-12-08 05:30:43 +00:00
int CIESubtract::GetMaxValue() const {
const int a = x4_a->GetMaxValue();
const int b = x8_b->GetMaxValue();
2018-12-08 05:30:43 +00:00
return a - b;
2018-06-08 01:15:46 +00:00
}
bool CIERealToInt::GetValue(int frame, int& valOut) const {
float a = 0.0f;
float b = 1.0f;
x8_b->GetValue(frame, b);
x4_a->GetValue(frame, a);
valOut = static_cast<int>(a * b);
return false;
}
int CIERealToInt::GetMaxValue() const {
// TODO: Implement
return 1;
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce