Localize Simulation::Before/AfterSim control to GameModel

This commit is contained in:
Tamás Bálint Misius 2023-01-07 07:53:00 +01:00
parent 169aa47685
commit 1f22e209f1
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
8 changed files with 87 additions and 78 deletions

View File

@ -14,7 +14,6 @@ ParticleDebug::ParticleDebug(unsigned int id, Simulation * sim, GameModel * mode
void ParticleDebug::Debug(int mode, int x, int y)
{
int debug_currentParticle = sim->debug_currentParticle;
int i = 0;
String logmessage;
@ -22,7 +21,7 @@ void ParticleDebug::Debug(int mode, int x, int y)
{
if (!sim->NUM_PARTS)
return;
i = debug_currentParticle;
i = sim->debug_nextToUpdate;
while (i < NPART - 1 && !sim->parts[i].type)
i++;
if (i == NPART - 1)
@ -32,30 +31,31 @@ void ParticleDebug::Debug(int mode, int x, int y)
}
else if (mode == 1)
{
if (x < 0 || x >= XRES || y < 0 || y >= YRES || !sim->pmap[y][x] || (i = ID(sim->pmap[y][x])) < debug_currentParticle)
i = NPART - 1;
if (x >= 0 && x < XRES && y >= 0 && y < YRES)
{
i = NPART - 1;
logmessage = String::Build("Updated particles from #", debug_currentParticle, " to end, updated sim");
if (sim->pmap[y][x] && ID(sim->pmap[y][x]) >= sim->debug_nextToUpdate)
{
i = ID(sim->pmap[y][x]);
}
else if (sim->photons[y][x] && ID(sim->photons[y][x]) >= sim->debug_nextToUpdate)
{
i = ID(sim->photons[y][x]);
}
}
else
logmessage = String::Build("Updated particles #", debug_currentParticle, " through #", i);
}
model->Log(logmessage, false);
if (sim->debug_currentParticle == 0)
sim->framerender = 1;
auto prevToUpdate = sim->debug_nextToUpdate;
model->UpdateUpTo(i + 1);
if (sim->debug_nextToUpdate)
{
sim->framerender = 1;
sim->BeforeSim();
sim->framerender = 0;
logmessage = String::Build("Updated particles from #", prevToUpdate, " through #", i);
}
sim->UpdateParticles(debug_currentParticle, i);
if (i < NPART-1)
sim->debug_currentParticle = i+1;
else
{
sim->AfterSim();
sim->debug_currentParticle = 0;
logmessage = String::Build("Updated particles from #", prevToUpdate, " to end");
}
model->Log(logmessage, false);
}
bool ParticleDebug::KeyPress(int key, int scan, bool shift, bool ctrl, bool alt, ui::Point currentMouse)
@ -86,13 +86,11 @@ bool ParticleDebug::KeyPress(int key, int scan, bool shift, bool ctrl, bool alt,
{
if (ctrl)
return true;
if (sim->debug_currentParticle > 0)
if (sim->debug_nextToUpdate > 0)
{
sim->UpdateParticles(sim->debug_currentParticle, NPART - 1);
sim->AfterSim();
String logmessage = String::Build("Updated particles from #", sim->debug_currentParticle, " to end, updated sim");
String logmessage = String::Build("Updated particles from #", sim->debug_nextToUpdate, " to end due to frame step");
model->UpdateUpTo(NPART);
model->Log(logmessage, false);
sim->debug_currentParticle = 0;
}
else
{
@ -103,8 +101,3 @@ bool ParticleDebug::KeyPress(int key, int scan, bool shift, bool ctrl, bool alt,
}
return true;
}
ParticleDebug::~ParticleDebug()
{
}

View File

@ -11,5 +11,4 @@ public:
ParticleDebug(unsigned int id, Simulation * sim, GameModel * model);
void Debug(int mode, int x, int y);
bool KeyPress(int key, int scan, bool shift, bool ctrl, bool alt, ui::Point currentMouse) override;
virtual ~ParticleDebug();
};

View File

@ -91,6 +91,7 @@ GameController::GameController():
gameView->SetDebugHUD(Client::Ref().GetPrefBool("Renderer.DebugMode", false));
commandInterface = CommandInterface::Create(this, gameModel);
gameModel->commandInterface = commandInterface;
Client::Ref().AddListener(this);
@ -880,11 +881,13 @@ void GameController::Update()
gameView->SetSample(gameModel->GetSimulation()->GetSample(pos.X, pos.Y));
Simulation * sim = gameModel->GetSimulation();
sim->BeforeSim();
if (!sim->sys_pause || sim->framerender)
{
sim->UpdateParticles(0, NPART - 1);
sim->AfterSim();
gameModel->UpdateUpTo(NPART);
}
else
{
gameModel->BeforeSim();
}
//if either STKM or STK2 isn't out, reset it's selected element. Defaults to PT_DUST unless right selected is something else

View File

@ -14,6 +14,7 @@
#include "Notification.h"
#include "TriangleBrush.h"
#include "QuickOptions.h"
#include "lua/CommandInterface.h"
#include "client/Client.h"
#include "client/GameSave.h"
@ -1239,12 +1240,10 @@ void GameModel::SetUser(User user)
void GameModel::SetPaused(bool pauseState)
{
if (!pauseState && sim->debug_currentParticle > 0)
if (!pauseState && sim->debug_nextToUpdate > 0)
{
String logmessage = String::Build("Updated particles from #", sim->debug_currentParticle, " to end due to unpause");
sim->UpdateParticles(sim->debug_currentParticle, NPART - 1);
sim->AfterSim();
sim->debug_currentParticle = 0;
String logmessage = String::Build("Updated particles from #", sim->debug_nextToUpdate, " to end due to unpause");
UpdateUpTo(NPART);
Log(logmessage, false);
}
@ -1668,3 +1667,40 @@ bool GameModel::RemoveCustomGOLType(const ByteString &identifier)
BuildMenus();
return removedAny;
}
void GameModel::UpdateUpTo(int upTo)
{
if (upTo < sim->debug_nextToUpdate)
{
upTo = NPART;
}
if (sim->debug_nextToUpdate == 0)
{
BeforeSim();
}
sim->UpdateParticles(sim->debug_nextToUpdate, upTo);
if (upTo < NPART)
{
sim->debug_nextToUpdate = upTo;
}
else
{
AfterSim();
sim->debug_nextToUpdate = 0;
}
}
void GameModel::BeforeSim()
{
if (!sim->sys_pause || sim->framerender)
{
commandInterface->HandleEvent(BeforeSimEvent{});
}
sim->BeforeSim();
}
void GameModel::AfterSim()
{
sim->AfterSim();
commandInterface->HandleEvent(AfterSimEvent{});
}

View File

@ -23,6 +23,7 @@ class Renderer;
class Snapshot;
struct SnapshotDelta;
class GameSave;
class CommandInterface;
class ToolSelection
{
@ -247,4 +248,10 @@ public:
ByteString SelectNextIdentifier;
int SelectNextTool;
void UpdateUpTo(int upTo);
void BeforeSim();
void AfterSim();
CommandInterface *commandInterface = nullptr;
};

View File

@ -2520,39 +2520,22 @@ int LuaScriptInterface::simulation_lastUpdatedID(lua_State *l)
int LuaScriptInterface::simulation_updateUpTo(lua_State *l)
{
// sim.updateUpTo dispatches an update to the range [current, upTo], but GameModel::UpdateUpTo takes a range [current, upTo).
// As a result, upTo here will be one smaller than it's logical for the duration of this function.
int upTo = NPART - 1;
if (lua_gettop(l) > 0)
{
upTo = luaL_checkinteger(l, 1);
}
if (upTo < 0 || upTo >= NPART)
if (upTo < -1 || upTo >= NPART) // -1 instead of 0 to allow for the empty range [0, -1] aka [0, 0)
{
return luaL_error(l, "ID not in valid range");
}
if (upTo < luacon_sim->debug_currentParticle)
{
upTo = NPART - 1;
}
if (luacon_sim->debug_currentParticle == 0)
{
luacon_sim->framerender = 1;
luacon_sim->BeforeSim();
luacon_sim->framerender = 0;
}
luacon_sim->UpdateParticles(luacon_sim->debug_currentParticle, upTo);
if (upTo < NPART - 1)
{
luacon_sim->debug_currentParticle = upTo + 1;
}
else
{
luacon_sim->AfterSim();
luacon_sim->debug_currentParticle = 0;
}
luacon_sim->framerender = 1;
luacon_model->UpdateUpTo(upTo + 1);
return 0;
}
int LuaScriptInterface::simulation_temperatureScale(lua_State *l)
{
if (lua_gettop(l) == 0)

View File

@ -32,11 +32,6 @@
#include "common/tpt-thread-local.h"
#include "gui/game/Brush.h"
#ifdef LUACONSOLE
#include "lua/LuaScriptInterface.h"
#include "lua/LuaScriptHelper.h"
#endif
extern int Element_PPIP_ppip_changed;
extern int Element_LOLZ_RuleTable[9][9];
extern int Element_LOLZ_lolz[XRES/9][YRES/9];
@ -2262,7 +2257,8 @@ void Simulation::create_arc(int sx, int sy, int dx, int dy, int midpoints, int v
void Simulation::clear_sim(void)
{
debug_currentParticle = 0;
debug_nextToUpdate = 0;
debug_mostRecentlyUpdated = -1;
emp_decor = 0;
emp_trigger_count = 0;
signs.clear();
@ -3490,7 +3486,7 @@ void Simulation::UpdateParticles(int start, int end)
bool transitionOccurred;
//the main particle loop function, goes over all particles.
for (i = start; i <= end && i <= parts_lastActiveIndex; i++)
for (i = start; i < end && i <= parts_lastActiveIndex; i++)
if (parts[i].type)
{
debug_mostRecentlyUpdated = i;
@ -4998,10 +4994,6 @@ void Simulation::BeforeSim()
{
if (!sys_pause||framerender)
{
#ifdef LUACONSOLE
luacon_ci->HandleEvent(BeforeSimEvent{});
#endif
air->update_air();
if(aheat_enable)
@ -5039,7 +5031,7 @@ void Simulation::BeforeSim()
gravWallChanged = false;
}
if (debug_currentParticle == 0)
if (debug_nextToUpdate == 0)
RecalcFreeParticles(true);
if (!sys_pause || framerender)
@ -5202,10 +5194,6 @@ void Simulation::AfterSim()
Element_EMP_Trigger(this, emp_trigger_count);
emp_trigger_count = 0;
}
#ifdef LUACONSOLE
luacon_ci->HandleEvent(AfterSimEvent{});
#endif
}
Simulation::~Simulation()
@ -5217,7 +5205,7 @@ Simulation::~Simulation()
Simulation::Simulation():
replaceModeSelected(0),
replaceModeFlags(0),
debug_currentParticle(0),
debug_nextToUpdate(0),
ISWIRE(0),
force_stacking_check(false),
emp_decor(0),

View File

@ -53,7 +53,7 @@ public:
int replaceModeFlags;
char can_move[PT_NUM][PT_NUM];
int debug_currentParticle;
int debug_nextToUpdate;
int debug_mostRecentlyUpdated = -1; // -1 when between full update loops
int parts_lastActiveIndex;
int pfree;
@ -163,7 +163,7 @@ public:
void set_emap(int x, int y);
int parts_avg(int ci, int ni, int t);
void create_arc(int sx, int sy, int dx, int dy, int midpoints, int variance, int type, int flags);
void UpdateParticles(int start, int end); // range inclusive on both ends
void UpdateParticles(int start, int end); // Dispatches an update to the range [start, end).
void SimulateGoL();
void RecalcFreeParticles(bool do_life_dec);
void CheckStacking();