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

View File

@ -11,5 +11,4 @@ public:
ParticleDebug(unsigned int id, Simulation * sim, GameModel * model); ParticleDebug(unsigned int id, Simulation * sim, GameModel * model);
void Debug(int mode, int x, int y); void Debug(int mode, int x, int y);
bool KeyPress(int key, int scan, bool shift, bool ctrl, bool alt, ui::Point currentMouse) override; 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)); gameView->SetDebugHUD(Client::Ref().GetPrefBool("Renderer.DebugMode", false));
commandInterface = CommandInterface::Create(this, gameModel); commandInterface = CommandInterface::Create(this, gameModel);
gameModel->commandInterface = commandInterface;
Client::Ref().AddListener(this); Client::Ref().AddListener(this);
@ -880,11 +881,13 @@ void GameController::Update()
gameView->SetSample(gameModel->GetSimulation()->GetSample(pos.X, pos.Y)); gameView->SetSample(gameModel->GetSimulation()->GetSample(pos.X, pos.Y));
Simulation * sim = gameModel->GetSimulation(); Simulation * sim = gameModel->GetSimulation();
sim->BeforeSim();
if (!sim->sys_pause || sim->framerender) if (!sim->sys_pause || sim->framerender)
{ {
sim->UpdateParticles(0, NPART - 1); gameModel->UpdateUpTo(NPART);
sim->AfterSim(); }
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 //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 "Notification.h"
#include "TriangleBrush.h" #include "TriangleBrush.h"
#include "QuickOptions.h" #include "QuickOptions.h"
#include "lua/CommandInterface.h"
#include "client/Client.h" #include "client/Client.h"
#include "client/GameSave.h" #include "client/GameSave.h"
@ -1239,12 +1240,10 @@ void GameModel::SetUser(User user)
void GameModel::SetPaused(bool pauseState) 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"); String logmessage = String::Build("Updated particles from #", sim->debug_nextToUpdate, " to end due to unpause");
sim->UpdateParticles(sim->debug_currentParticle, NPART - 1); UpdateUpTo(NPART);
sim->AfterSim();
sim->debug_currentParticle = 0;
Log(logmessage, false); Log(logmessage, false);
} }
@ -1668,3 +1667,40 @@ bool GameModel::RemoveCustomGOLType(const ByteString &identifier)
BuildMenus(); BuildMenus();
return removedAny; 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; class Snapshot;
struct SnapshotDelta; struct SnapshotDelta;
class GameSave; class GameSave;
class CommandInterface;
class ToolSelection class ToolSelection
{ {
@ -247,4 +248,10 @@ public:
ByteString SelectNextIdentifier; ByteString SelectNextIdentifier;
int SelectNextTool; 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) 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; int upTo = NPART - 1;
if (lua_gettop(l) > 0) if (lua_gettop(l) > 0)
{ {
upTo = luaL_checkinteger(l, 1); 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"); 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->framerender = 1;
luacon_sim->BeforeSim(); luacon_model->UpdateUpTo(upTo + 1);
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;
}
return 0; return 0;
} }
int LuaScriptInterface::simulation_temperatureScale(lua_State *l) int LuaScriptInterface::simulation_temperatureScale(lua_State *l)
{ {
if (lua_gettop(l) == 0) if (lua_gettop(l) == 0)

View File

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

View File

@ -53,7 +53,7 @@ public:
int replaceModeFlags; int replaceModeFlags;
char can_move[PT_NUM][PT_NUM]; char can_move[PT_NUM][PT_NUM];
int debug_currentParticle; int debug_nextToUpdate;
int debug_mostRecentlyUpdated = -1; // -1 when between full update loops int debug_mostRecentlyUpdated = -1; // -1 when between full update loops
int parts_lastActiveIndex; int parts_lastActiveIndex;
int pfree; int pfree;
@ -163,7 +163,7 @@ public:
void set_emap(int x, int y); void set_emap(int x, int y);
int parts_avg(int ci, int ni, int t); 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 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 SimulateGoL();
void RecalcFreeParticles(bool do_life_dec); void RecalcFreeParticles(bool do_life_dec);
void CheckStacking(); void CheckStacking();