From 462460b6b3f0907b9b47549d958bfe47f8070f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sun, 5 Jan 2025 20:39:54 +0100 Subject: [PATCH] Manage particle lifetime with create/kill_part only This should fix the long-standing issue of Create/ChangeType callbacks not being called in all cases when particles were created or changed type. I'm eagerly awaiting reports of the horrendous crashes this will inevitably cause. --- src/simulation/Simulation.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 47ce66574..a5fb0cd89 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -127,15 +127,16 @@ void Simulation::Load(const GameSave *save, bool includePressure, Vec2 bloc removeExistingParticles({ x, y }); // Allocate particle (this location is guaranteed to be empty due to "full scan" logic above) - if (pfree == -1) + auto i = create_part(-3, x, y, tempPart.type); + if (i == -1) + { break; - auto i = pfree; - pfree = parts[i].life; - NUM_PARTS += 1; + } if (i > parts.lastActiveIndex) + { parts.lastActiveIndex = i; + } parts[i] = tempPart; - elementCount[tempPart.type]++; switch (parts[i].type) @@ -977,6 +978,13 @@ int Simulation::parts_avg(int ci, int ni,int t) void Simulation::clear_sim(void) { + for (auto i = 0; i <= parts.lastActiveIndex; i++) + { + if (parts[i].type) + { + kill_part(i); + } + } ensureDeterminism = false; frameCount = 0; debug_nextToUpdate = 0;