From 0b0f40ec94c574002db887a34e7cbb7b85946d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sun, 11 Jun 2023 18:12:49 +0200 Subject: [PATCH] Eliminate a GameSave copy in Simulation::Load --- src/simulation/Simulation.cpp | 123 +++++++++++++--------------------- 1 file changed, 46 insertions(+), 77 deletions(-) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 14f87fabe..60ed9f6db 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -19,10 +19,8 @@ extern int Element_LOLZ_lolz[XRES/9][YRES/9]; extern int Element_LOVE_RuleTable[9][9]; extern int Element_LOVE_love[XRES/9][YRES/9]; -void Simulation::Load(const GameSave *originalSave, bool includePressure, Vec2 blockP) // block coordinates +void Simulation::Load(const GameSave *save, bool includePressure, Vec2 blockP) // block coordinates { - auto save = std::unique_ptr(new GameSave(*originalSave)); - auto partP = blockP * CELL; unsigned int pmapmask = (1<pmapbits)-1; @@ -57,40 +55,33 @@ void Simulation::Load(const GameSave *originalSave, bool includePressure, Vec2 soapList; for (int n = 0; n < NPART && n < save->particlesCount; n++) { - Particle *tempPart = &save->particles[n]; - auto &type = tempPart->type; - if (!type) + Particle tempPart = save->particles[n]; + if (tempPart.type <= 0 || tempPart.type >= PT_NUM) { continue; } - tempPart->x += (float)partP.X; - tempPart->y += (float)partP.Y; - int x = int(tempPart->x + 0.5f); - int y = int(tempPart->y + 0.5f); + tempPart.x += (float)partP.X; + tempPart.y += (float)partP.Y; + int x = int(tempPart.x + 0.5f); + int y = int(tempPart.y + 0.5f); // Check various scenarios where we are unable to spawn the element, and set type to 0 to block spawning later if (!InBounds(x, y)) { - type = 0; continue; } - if (type < 0 || type >= PT_NUM) - { - type = 0; - continue; - } - type = partMap[type]; + tempPart.type = partMap[tempPart.type]; for (auto index : possiblyCarriesType) { - if (elements[type].CarriesTypeIn & (1U << index)) + if (elements[tempPart.type].CarriesTypeIn & (1U << index)) { - auto *prop = reinterpret_cast(reinterpret_cast(tempPart) + properties[index].Offset); + auto *prop = reinterpret_cast(reinterpret_cast(&tempPart) + properties[index].Offset); auto carriedType = *prop & int(pmapmask); auto extra = *prop >> save->pmapbits; if (carriedType >= 0 && carriedType < PT_NUM) @@ -102,73 +93,26 @@ void Simulation::Load(const GameSave *originalSave, bool includePressure, Vec2 new ID - std::map soapList; - for (int n = 0; n < NPART && n < save->particlesCount; n++) - { - Particle tempPart = save->particles[n]; - if (!tempPart.type) + if (tempPart.type == PT_FIGH && !Element_FIGH_CanAlloc(this)) { continue; } + if (!elements[tempPart.type].Enabled) + { + continue; + } + + // Mark location to be cleaned of existing particles. + pmap[y][x] = -1; if (elements[tempPart.type].CreateAllowed) { @@ -272,10 +216,35 @@ void Simulation::Load(const GameSave *originalSave, bool includePressure, Vec2 new particle ID