From 2eee738a9fd30ec77bbe5bdb3b905997571ac845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Tue, 12 Dec 2023 22:08:31 +0100 Subject: [PATCH] Fix another extremely rare pmap corruption The real fix was made in Simulation::kill_part, where photons[y][x] wasn't cleared when i = 0 because the pmap branch triggered instead. Broken since ff7428fc7038, which only partially fixed some related bug. I decided to also fix every other case, even if they are not strictly necessary because they write 0 to pmap/photons anyway. Reproduce with sim.loadSave(3062273, 1) sim.ensureDeterminism(true) tpt.setfpscap(2) local function F() print(sim.framerender(), sim.randomseed()) if sim.framerender() == 0 then local a = 3498763327 print(sim.hash(), a) sim.framerender(1200) sim.reloadSave() sim.randomseed(a, a + 1, a + 2, a + 3) tpt.set_pause(0) end end event.register(event.tick, F) --- src/simulation/Simulation.cpp | 22 +++++++++++----------- src/simulation/elements/BANG.cpp | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index b1040f7d9..2fb58ae58 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -1411,7 +1411,7 @@ int Simulation::try_move(int i, int x, int y, int nx, int ny) return 1; } - if (ID(pmap[ny][nx]) == ri) + if (pmap[ny][nx] && ID(pmap[ny][nx]) == ri) pmap[ny][nx] = 0; parts[ri].x += float(x - nx); parts[ri].y += float(y - ny); @@ -1469,9 +1469,9 @@ bool Simulation::move(int i, int x, int y, float nxf, float nyf) parts[i].y = nyf; if (ny != y || nx != x) { - if (ID(pmap[y][x]) == i) + if (pmap[y][x] && ID(pmap[y][x]) == i) pmap[y][x] = 0; - if (ID(photons[y][x]) == i) + if (photons[y][x] && ID(photons[y][x]) == i) photons[y][x] = 0; // kill_part if particle is out of bounds if (nx < CELL || nx >= XRES - CELL || ny < CELL || ny >= YRES - CELL) @@ -1676,9 +1676,9 @@ void Simulation::kill_part(int i)//kills particle number i if (x >= 0 && y >= 0 && x < XRES && y < YRES) { - if (ID(pmap[y][x]) == i) + if (pmap[y][x] && ID(pmap[y][x]) == i) pmap[y][x] = 0; - else if (ID(photons[y][x]) == i) + else if (photons[y][x] && ID(photons[y][x]) == i) photons[y][x] = 0; } @@ -1726,13 +1726,13 @@ bool Simulation::part_change_type(int i, int x, int y, int t) if (elements[t].Properties & TYPE_ENERGY) { photons[y][x] = PMAP(i, t); - if (ID(pmap[y][x]) == i) + if (pmap[y][x] && ID(pmap[y][x]) == i) pmap[y][x] = 0; } else { pmap[y][x] = PMAP(i, t); - if (ID(photons[y][x]) == i) + if (photons[y][x] && ID(photons[y][x]) == i) photons[y][x] = 0; } return false; @@ -1828,9 +1828,9 @@ int Simulation::create_part(int p, int x, int y, int t, int v) { int oldX = (int)(parts[p].x + 0.5f); int oldY = (int)(parts[p].y + 0.5f); - if (ID(pmap[oldY][oldX]) == p) + if (pmap[oldY][oldX] && ID(pmap[oldY][oldX]) == p) pmap[oldY][oldX] = 0; - if (ID(photons[oldY][oldX]) == p) + if (photons[oldY][oldX] && ID(photons[oldY][oldX]) == p) photons[oldY][oldX] = 0; oldType = parts[p].type; @@ -2889,9 +2889,9 @@ killed: } if (ny!=y || nx!=x) { - if (ID(pmap[y][x]) == i) + if (pmap[y][x] && ID(pmap[y][x]) == i) pmap[y][x] = 0; - else if (ID(photons[y][x]) == i) + else if (photons[y][x] && ID(photons[y][x]) == i) photons[y][x] = 0; if (nx=XRES-CELL || ny=YRES-CELL) { diff --git a/src/simulation/elements/BANG.cpp b/src/simulation/elements/BANG.cpp index 3e9db3f81..e53c8e164 100644 --- a/src/simulation/elements/BANG.cpp +++ b/src/simulation/elements/BANG.cpp @@ -73,7 +73,7 @@ static int update(UPDATE_FUNC_ARGS) } else if(parts[i].tmp==1) { - if ((ID(pmap[y][x]) == i)) + if (pmap[y][x] && ID(pmap[y][x]) == i) { sim->flood_prop(x, y, Particle::GetProperties()[FIELD_TMP], 2); }