From bcbcdbe06ba66d4caa1581090060d984477bff8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sat, 17 Apr 2021 21:23:39 +0200 Subject: [PATCH] Make custom can_move settings persistent (fixes #764) --- src/lua/LuaScriptInterface.cpp | 47 +++++++++++++++++++++++++++++++--- src/lua/LuaScriptInterface.h | 4 +++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index d9afd3df4..c4ea4e8f0 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -133,6 +133,14 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): luacon_ren = m->GetRenderer(); luacon_ci = this; + for (auto moving = 0; moving < PT_NUM; ++moving) + { + for (auto into = 0; into < PT_NUM; ++into) + { + custom_can_move[moving][into] = 0; + } + } + //New TPT API l = luaL_newstate(); tpt_lua_setmainthread(l); @@ -384,6 +392,21 @@ tpt.partsdata = nil"); } } +void LuaScriptInterface::custom_init_can_move() +{ + luacon_sim->init_can_move(); + for (auto moving = 0; moving < PT_NUM; ++moving) + { + for (auto into = 0; into < PT_NUM; ++into) + { + if (custom_can_move[moving][into] & 0x80) + { + luacon_sim->can_move[moving][into] = custom_can_move[moving][into] & 0x7F; + } + } + } +} + void LuaScriptInterface::Init() { if(Client::Ref().FileExists("autorun.lua")) @@ -1957,7 +1980,9 @@ int LuaScriptInterface::simulation_canMove(lua_State * l) } else { - luacon_sim->can_move[movingElement][destinationElement] = luaL_checkint(l, 3); + int setting = luaL_checkint(l, 3) & 0x7F; + luacon_ci->custom_can_move[movingElement][destinationElement] = setting | 0x80; + luacon_sim->can_move[movingElement][destinationElement] = setting; return 0; } } @@ -2711,7 +2736,14 @@ int LuaScriptInterface::elements_loadDefault(lua_State * l) } luacon_model->BuildMenus(); - luacon_sim->init_can_move(); + for (auto moving = 0; moving < PT_NUM; ++moving) + { + for (auto into = 0; into < PT_NUM; ++into) + { + luacon_ci->custom_can_move[moving][into] = 0; + } + } + luacon_ci->custom_init_can_move(); std::fill(luacon_ren->graphicscache, luacon_ren->graphicscache+PT_NUM, gcache_item()); return 0; } @@ -2780,6 +2812,13 @@ int LuaScriptInterface::elements_allocate(lua_State * l) lua_pop(l, 1); } + for (auto elem = 0; elem < PT_NUM; ++elem) + { + luacon_ci->custom_can_move[elem][newID] = 0; + luacon_ci->custom_can_move[newID][elem] = 0; + } + luacon_ci->custom_init_can_move(); + lua_pushinteger(l, newID); return 1; } @@ -2987,7 +3026,7 @@ int LuaScriptInterface::elements_element(lua_State * l) lua_pop(l, 1); luacon_model->BuildMenus(); - luacon_sim->init_can_move(); + luacon_ci->custom_init_can_move(); luacon_ren->graphicscache[id].isready = 0; return 0; @@ -3054,7 +3093,7 @@ int LuaScriptInterface::elements_property(lua_State * l) } luacon_model->BuildMenus(); - luacon_sim->init_can_move(); + luacon_ci->custom_init_can_move(); luacon_ren->graphicscache[id].isready = 0; } else if (propertyName == "Update") diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index 99f76421b..6cc2ee588 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -8,6 +8,7 @@ #include "CommandInterface.h" #include "lua/LuaEvents.h" #include "simulation/StructProperty.h" +#include "simulation/ElementDefs.h" #include @@ -200,6 +201,9 @@ public: std::map grabbed_components; LuaScriptInterface(GameController * c, GameModel * m); + char custom_can_move[PT_NUM][PT_NUM]; + void custom_init_can_move(); + void OnTick() override; bool HandleEvent(LuaEvents::EventTypes eventType, Event * event) override;