Add Lua support for partial sim updates

This commit is contained in:
Tamás Bálint Misius 2022-12-20 09:05:31 +01:00
parent ca93e69b19
commit 06802949ab
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
4 changed files with 55 additions and 0 deletions

View File

@ -939,6 +939,8 @@ void LuaScriptInterface::initSimulationAPI()
{"listCustomGol", simulation_listCustomGol},
{"addCustomGol", simulation_addCustomGol},
{"removeCustomGol", simulation_removeCustomGol},
{"lastUpdatedID", simulation_lastUpdatedID},
{"updateUpTo", simulation_updateUpTo},
{NULL, NULL}
};
luaL_register(l, "simulation", simulationAPIMethods);
@ -2477,6 +2479,53 @@ int LuaScriptInterface::simulation_removeCustomGol(lua_State *l)
return 1;
}
int LuaScriptInterface::simulation_lastUpdatedID(lua_State *l)
{
if (luacon_sim->debug_mostRecentlyUpdated != -1)
{
lua_pushinteger(l, luacon_sim->debug_mostRecentlyUpdated);
}
else
{
lua_pushnil(l);
}
return 1;
}
int LuaScriptInterface::simulation_updateUpTo(lua_State *l)
{
int upTo = NPART - 1;
if (lua_gettop(l) > 0)
{
upTo = luaL_checkinteger(l, 1);
}
if (upTo < 0 || upTo >= NPART)
{
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;
}
return 0;
}
//// Begin Renderer API
void LuaScriptInterface::initRendererAPI()

View File

@ -119,6 +119,8 @@ class LuaScriptInterface: public CommandInterface
static int simulation_listCustomGol(lua_State *l);
static int simulation_addCustomGol(lua_State *l);
static int simulation_removeCustomGol(lua_State *l);
static int simulation_lastUpdatedID(lua_State *l);
static int simulation_updateUpTo(lua_State *l);
//Renderer

View File

@ -3484,6 +3484,7 @@ void Simulation::UpdateParticles(int start, int end)
for (i = start; i <= end && i <= parts_lastActiveIndex; i++)
if (parts[i].type)
{
debug_mostRecentlyUpdated = i;
t = parts[i].type;
x = (int)(parts[i].x+0.5f);
@ -5183,6 +5184,8 @@ void Simulation::BeforeSim()
void Simulation::AfterSim()
{
debug_mostRecentlyUpdated = -1;
if (emp_trigger_count)
{
// pitiful attempt at trying to keep code relating to a given element in the same file

View File

@ -55,6 +55,7 @@ public:
char can_move[PT_NUM][PT_NUM];
int debug_currentParticle;
int debug_mostRecentlyUpdated = -1; // -1 when between full update loops
int parts_lastActiveIndex;
int pfree;
int NUM_PARTS;