Files
fs2open.github.com/code/particle/ParticleSource.cpp
T
BMagnu 82dfdbc962 Cleanup of Level 0 Particles (#6808)
* Make scripting use the full-blown particle system

* Add reverse option

* Add animation reversal as an option to modern particles

* Start implementation of beam particles

* Add multi-emission particle source API for legacy particles

* Attach beam muzzle particles to the correct host

* Reorder code in order to facilitate apparent visual size as modular curve input for particles

* fix lua api

* Typo

* Fix scripting and beam muzzle particles

* Remove muzzleflash code, prepare to replace with particles

* Begin mflash effect conversion

* Pass final parameters to mflashes and fix curve

* Remove deprecated particle function

* Add debris flame particles

* remove weapon_explosion table

* Convert shield explosion particles

* Fix modular curve full inputs and convert particle curve inputs accordingly

* Allow particle volume offset and rotation with curve-based rot around fvec

* Add Ring and Point Volumes

* Convert PSPEW to new system

* Make particle curve timing aware of interp

* Update todos

* get rid of performance waste

* Add last parameter for converted pspews

* Fix pspew bugs

* Fix direction for default pspew

* Fix order for voluem rotation

* particle vel interp

* particle vel interp 2

* Make beam muzzle stay parented to the firing ship, as it's likely the more commonly intended variamnt

* fix scripting particle lifetime

* Allow beam muzzle particle override

* Skip invalid pspews

* remove pspew dcf code

* Fix sparkler stretch

* Correct sparkler bia

* General sparkler weirdness

* Tune distribution of sparklers

* Convert Asteroid particles

* Fix conversion warning

* Fix conversion warning again

* Fix conversion warning again again

* Really clang?
2025-07-09 16:47:14 +09:00

114 lines
3.7 KiB
C++

#include <math/bitarray.h>
#include "freespace.h"
#include "particle/ParticleSource.h"
#include "weapon/weapon.h"
#include "ship/ship.h"
namespace particle {
ParticleSource::ParticleSource() : m_normal(std::nullopt), m_effect(ParticleEffectHandle::invalid()) {
for (size_t i = 0; i < 64; i++) {
m_effect_is_running[i] = true;
}
}
bool ParticleSource::isValid() const {
if (!m_effect_is_running.any()) {
return false;
}
if (!m_effect.isValid()) {
return false;
}
if (!m_host->isValid()) {
return false;
}
return true;
}
void ParticleSource::finishCreation() {
m_host->setupProcessing();
for (const auto& effect : ParticleManager::get()->getEffect(m_effect)) {
const auto& [begin, end] = effect.getEffectDuration();
m_timing.emplace_back(SourceTiming{timestamp_delta(begin, 0), begin, end});
}
}
const SCP_vector<ParticleEffect>& ParticleSource::getEffect() const {
return ParticleManager::get()->getEffect(m_effect);
}
bool ParticleSource::process() {
Assertion(m_host != nullptr, "Particle Source has no host!");
const auto& effectList = getEffect();
const vec3d& vel = m_host->getVelocity();
const auto& [parent, parent_sig] = m_host->getParentObjAndSig();
float parent_radius = m_host->getScale();
float parent_lifetime = m_host->getLifetime();
float particleMultiplier = m_host->getParticleMultiplier();
bool result = false;
for (size_t i = 0; i < effectList.size(); i++) {
const auto& effect = effectList[i];
auto& timing = m_timing[i];
if (m_effect_is_running[i]) {
bool needs_to_continue_running = true;
while(timestamp_elapsed(timing.m_nextCreation) && needs_to_continue_running) {
//Find "time" in last frame where particle spawned
float interp = static_cast<float>(timestamp_since(timing.m_nextCreation)) / (f2fl(Frametime) * 1000.0f);
// Some of these
float freqMult = effect.processSource(interp, *this, i, vel, parent, parent_sig, parent_lifetime, parent_radius, particleMultiplier);
// we need to clamp this to 1 because a spawn delay lower than it takes to spawn the particle in ms means we try to spawn infinite particles
auto time_diff_ms = std::max(fl2i(effect.getNextSpawnDelay() / freqMult * MILLISECONDS_PER_SECOND), 1);
timing.m_nextCreation = timestamp_delta(timing.m_nextCreation, time_diff_ms);
bool isDone = effect.isOnetime() || timestamp_compare(timing.m_endTimestamp, timing.m_nextCreation) < 0;
m_effect_is_running[i] = !isDone;
needs_to_continue_running = !isDone;
}
result |= needs_to_continue_running;
}
}
return result;
}
void ParticleSource::setNormal(const vec3d& normal) {
m_normal = normal;
}
void ParticleSource::setTriggerRadius(float radius) {
m_triggerRadius = radius;
}
void ParticleSource::setTriggerVelocity(float velocity) {
m_triggerVelocity = velocity;
}
void ParticleSource::setHost(std::unique_ptr<EffectHost> host) {
m_host = std::move(host);
}
float ParticleSource::getEffectRemainingTime(const std::tuple<const ParticleSource&, const size_t&>& source) {
const auto& timing = std::get<0>(source).m_timing[std::get<1>(source)];
return i2fl(timestamp_get_delta(timing.m_nextCreation, timing.m_endTimestamp)) / i2fl(MILLISECONDS_PER_SECOND);
}
float ParticleSource::getEffectRunningTime(const std::tuple<const ParticleSource&, const size_t&>& source) {
const auto& timing = std::get<0>(source).m_timing[std::get<1>(source)];
return i2fl(timestamp_get_delta(timing.m_startTimestamp, timing.m_nextCreation)) / i2fl(MILLISECONDS_PER_SECOND);
}
float ParticleSource::getEffectVisualSize(const std::tuple<const ParticleSource&, const size_t&, const vec3d&>& source) {
return std::get<0>(source).getEffect()[std::get<1>(source)].getApproximateVisualSize(std::get<2>(source));
}
}