metaforce/Runtime/Particle/CIntElement.cpp

316 lines
7.9 KiB
C++
Raw Normal View History

2019-10-04 16:20:04 -07:00
#include "Runtime/Particle/CIntElement.hpp"
#include <algorithm>
2019-10-04 16:20:04 -07:00
#include "Runtime/CRandom16.hpp"
#include "Runtime/Particle/CElementGen.hpp"
#include "Runtime/Particle/CGenDescription.hpp"
#include "Runtime/Particle/CParticleGlobals.hpp"
2016-04-02 22:25:34 -07:00
/* Documentation at: https://wiki.axiodl.com/w/Particle_Script#Int_Elements */
2016-02-27 22:55:05 -08:00
2021-04-10 01:42:06 -07:00
namespace metaforce {
2018-12-07 21:30:43 -08:00
CIEKeyframeEmitter::CIEKeyframeEmitter(CInputStream& in) {
x4_percent = in.ReadLong();
x8_unk1 = in.ReadLong();
xc_loop = in.ReadBool();
xd_unk2 = in.ReadBool();
x10_loopEnd = in.ReadLong();
x14_loopStart = in.ReadLong();
2018-12-07 21:30:43 -08:00
u32 count = in.ReadLong();
2018-12-07 21:30:43 -08:00
x18_keys.reserve(count);
for (u32 i = 0; i < count; ++i)
x18_keys.push_back(in.ReadInt32());
2018-12-07 21:30:43 -08:00
}
bool CIEKeyframeEmitter::GetValue([[maybe_unused]] int frame, int& valOut) const {
if (x4_percent == 0) {
int emitterTime = CParticleGlobals::instance()->m_EmitterTime;
2018-12-07 21:30:43 -08:00
if (xc_loop) {
if (emitterTime >= x10_loopEnd) {
emitterTime -= x14_loopStart;
emitterTime = emitterTime % (x10_loopEnd - x14_loopStart);
emitterTime += x14_loopStart;
2018-12-07 21:30:43 -08:00
}
valOut = x18_keys[emitterTime];
2018-12-07 21:30:43 -08:00
} else {
emitterTime = std::min<int>(emitterTime, x10_loopEnd - 1);
valOut = x18_keys[emitterTime];
2016-02-06 16:19:59 -08:00
}
return false;
2018-12-07 21:30:43 -08:00
} else {
int ltPerc = CParticleGlobals::instance()->m_ParticleLifetimePercentage;
if (ltPerc == 100) {
valOut = x18_keys[ltPerc];
} else {
float ltPercRem = CParticleGlobals::instance()->m_ParticleLifetimePercentageRemainder;
float lerp = (1.0f - ltPercRem) * x18_keys[ltPerc] + ltPercRem * x18_keys[ltPerc + 1];
valOut = static_cast<int>(lerp);
}
return false;
2018-12-07 21:30:43 -08:00
}
2016-02-06 16:19:59 -08:00
}
2021-06-07 12:29:18 -07:00
int CIEKeyframeEmitter::GetMaxValue() const { return *std::max_element(x18_keys.cbegin(), x18_keys.cend()); }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08:00
bool CIEDeath::GetValue(int frame, int& valOut) const {
int b;
x4_a->GetValue(frame, valOut);
2018-12-07 21:30:43 -08:00
x8_b->GetValue(frame, b);
return frame >= b;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEDeath::GetMaxValue() const { return x4_a->GetMaxValue(); }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08: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);
if (valOut > b) {
valOut = b;
}
if (valOut < a) {
valOut = a;
}
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08: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-07 18:15:46 -07:00
}
2018-12-07 21:30:43 -08:00
bool CIETimeChain::GetValue(int frame, int& valOut) const {
int v;
xc_swFrame->GetValue(frame, v);
if (frame < v) {
2018-12-07 21:30:43 -08:00
return x4_a->GetValue(frame, valOut);
} else {
return x8_b->GetValue(frame - v, valOut);
}
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIETimeChain::GetMaxValue() const { return std::max(x8_b->GetMaxValue(), x4_a->GetMaxValue()); }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08: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-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEAdd::GetMaxValue() const {
const int a = x4_a->GetMaxValue();
const int b = x8_b->GetMaxValue();
2018-12-07 21:30:43 -08:00
return a + b;
2018-06-07 18:15:46 -07:00
}
bool CIEConstant::GetValue([[maybe_unused]] int frame, int& valOut) const {
2018-12-07 21:30:43 -08:00
valOut = x4_val;
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEConstant::GetMaxValue() const { return x4_val; }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08:00
bool CIEImpulse::GetValue(int frame, int& valOut) const {
if (frame == 0) {
2018-12-07 21:30:43 -08:00
x4_a->GetValue(frame, valOut);
} else {
2018-12-07 21:30:43 -08:00
valOut = 0;
}
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEImpulse::GetMaxValue() const { return x4_a->GetMaxValue(); }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08:00
bool CIELifetimePercent::GetValue(int frame, int& valOut) const {
int a = 0;
2018-12-07 21:30:43 -08:00
x4_percentVal->GetValue(frame, a);
if (a < 0) {
a = 0;
}
valOut = (a / 100.0f) * CParticleGlobals::instance()->m_ParticleLifetimeReal + 0.5f;
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08: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-07 18:15:46 -07:00
}
2018-12-07 21:30:43 -08: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-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEInitialRandom::GetMaxValue() const { return x8_b->GetMaxValue(); }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08: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 02:31:00 -07:00
int cv = a + b + 1;
if (cv < 0) {
cv = 1;
}
2018-12-07 21:30:43 -08:00
if (b >= 1) {
if (frame % cv > a) {
x10_bVal->GetValue(frame, valOut);
} else {
xc_aVal->GetValue(frame, valOut);
}
2020-04-12 02:31:00 -07:00
} else {
xc_aVal->GetValue(frame, valOut);
2020-04-12 02:31:00 -07:00
}
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEPulse::GetMaxValue() const { return std::max(xc_aVal->GetMaxValue(), x10_bVal->GetMaxValue()); }
2016-02-06 16:19:59 -08:00
2018-12-07 21:30:43 -08: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-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEMultiply::GetMaxValue() const { return x4_a->GetMaxValue() * x8_b->GetMaxValue(); }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08:00
bool CIESampleAndHold::GetValue(int frame, int& valOut) const {
bool ret;
2018-12-07 21:30:43 -08:00
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;
ret = x4_sampleSource->GetValue(frame, valOut);
x14_holdVal = valOut;
} else {
2018-12-07 21:30:43 -08:00
valOut = x14_holdVal;
ret = false;
}
return ret;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIESampleAndHold::GetMaxValue() const { return x4_sampleSource->GetMaxValue(); }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08: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) {
2018-12-07 21:30:43 -08:00
valOut = CRandom16::GetRandomNumber()->Range(a, b);
} else {
2018-12-07 21:30:43 -08:00
valOut = CRandom16::GetRandomNumber()->Next();
}
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIERandom::GetMaxValue() const {
if (x4_min->GetMaxValue() > 0)
return x8_max->GetMaxValue();
else
return 65535;
2018-06-07 18:15:46 -07:00
}
2018-12-07 21:30:43 -08:00
bool CIETimeScale::GetValue(int frame, int& valOut) const {
float a;
x4_a->GetValue(frame, a);
valOut = static_cast< float >(frame) * a;
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIETimeScale::GetMaxValue() const { return 10000; /* Assume 10000 frames max (not ideal estimate) */ }
2018-06-07 18:15:46 -07:00
bool CIEGetCumulativeParticleCount::GetValue([[maybe_unused]] int frame, int& valOut) const {
valOut = CParticleGlobals::instance()->m_currentParticleSystem->x4_system->GetCumulativeParticleCount();
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEGetCumulativeParticleCount::GetMaxValue() const { return 256; }
2018-06-07 18:15:46 -07:00
bool CIEGetActiveParticleCount::GetValue([[maybe_unused]] int frame, int& valOut) const {
valOut = CParticleGlobals::instance()->m_currentParticleSystem->x4_system->GetParticleCount();
2018-12-07 21:30:43 -08:00
return false;
2016-02-11 12:32:42 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEGetActiveParticleCount::GetMaxValue() const { return 256; }
2018-06-07 18:15:46 -07:00
bool CIEGetEmitterTime::GetValue([[maybe_unused]] int frame, int& valOut) const {
valOut = CParticleGlobals::instance()->m_currentParticleSystem->x4_system->GetEmitterTime();
2018-12-07 21:30:43 -08:00
return false;
2016-02-11 12:32:42 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEGetEmitterTime::GetMaxValue() const { return 10000; /* Assume 10000 frames max (not ideal estimate) */ }
2018-06-07 18:15:46 -07:00
2018-12-07 21:30:43 -08: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) {
2018-12-07 21:30:43 -08:00
valOut = a % b;
} else {
2018-12-07 21:30:43 -08:00
valOut = a;
}
2018-12-07 21:30:43 -08:00
return false;
2016-02-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIEModulo::GetMaxValue() const {
const int a = x4_a->GetMaxValue();
const int b = x8_b->GetMaxValue();
if (b != 0) {
2018-12-07 21:30:43 -08:00
return b - 1;
}
return a;
2018-06-07 18:15:46 -07:00
}
2018-12-07 21:30:43 -08: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-06 16:19:59 -08:00
}
2018-12-07 21:30:43 -08:00
int CIESubtract::GetMaxValue() const {
const int a = x4_a->GetMaxValue();
const int b = x8_b->GetMaxValue();
2018-12-07 21:30:43 -08:00
return a - b;
2018-06-07 18:15:46 -07: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 01:42:06 -07:00
} // namespace metaforce