From 8e09651990d45d8b25b8795cbd1cfffeecfd3f5f Mon Sep 17 00:00:00 2001 From: jacob1 Date: Sun, 6 Jan 2013 12:25:13 -0500 Subject: [PATCH] fix element function replacement option --- src/cat/LegacyLuaAPI.cpp | 6 +++++- src/cat/LuaScriptHelper.h | 1 - src/cat/LuaScriptInterface.cpp | 24 ++++++++++++++++++------ src/simulation/Simulation.cpp | 15 ++++----------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 9a478e74e..80a7f82f5 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -648,13 +648,17 @@ int luatpt_element_func(lua_State *l) if(lua_isfunction(l, 1)) { int element = luaL_optint(l, 2, 0); + int replace = luaL_optint(l, 3, 0); int function; lua_pushvalue(l, 1); function = luaL_ref(l, LUA_REGISTRYINDEX); if(element > 0 && element < PT_NUM) { lua_el_func[element] = function; - luacon_sim->elements[element].Update = &luacon_elementReplacement; + if(replace) + lua_el_mode[element] = 2; + else + lua_el_mode[element] = 1; return 0; } else diff --git a/src/cat/LuaScriptHelper.h b/src/cat/LuaScriptHelper.h index 37920b5f4..17678d5a4 100644 --- a/src/cat/LuaScriptHelper.h +++ b/src/cat/LuaScriptHelper.h @@ -35,7 +35,6 @@ int luacon_step(int mx, int my, int selectl, int selectr, int bsx, int bsy); int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel); int luacon_keyevent(int key, int modifier, int event); int luacon_eval(char *command); -int luacon_part_update(int t, int i, int x, int y, int surround_space, int nt); char *luacon_geterror(); void luacon_close(); int luacon_partsread(lua_State* l); diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 869d717a2..5954c4bb6 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -913,11 +913,12 @@ int LuaScriptInterface::elements_element(lua_State * l) if(lua_type(l, -1) == LUA_TFUNCTION) { lua_el_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); - luacon_sim->elements[id].Update = &luacon_elementReplacement; + lua_el_mode[id] = 1; } else if(lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1)) { lua_el_func[id] = 0; + lua_el_mode[id] = 0; luacon_sim->elements[id].Update = NULL; } else @@ -926,12 +927,12 @@ int LuaScriptInterface::elements_element(lua_State * l) lua_getfield(l, -1, "Graphics"); if(lua_type(l, -1) == LUA_TFUNCTION) { - lua_el_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); + lua_gr_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); luacon_sim->elements[id].Graphics = &luacon_graphicsReplacement; } else if(lua_type(l, -1) == LUA_TBOOLEAN && !lua_toboolean(l, -1)) { - lua_el_func[id] = 0; + lua_gr_func[id] = 0; luacon_sim->elements[id].Graphics = NULL; } else @@ -1068,7 +1069,17 @@ int LuaScriptInterface::elements_property(lua_State * l) { lua_pushvalue(l, 3); lua_el_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); - luacon_sim->elements[id].Update = &luacon_elementReplacement; + if (args > 3) + { + luaL_checktype(l, 4, LUA_TNUMBER); + int replace = lua_tointeger(l, 4); + if (replace == 1) + lua_el_mode[id] = 2; + else + lua_el_mode[id] = 1; + } + else + lua_el_mode[id] = 1; } else if(lua_type(l, 3) == LUA_TLIGHTUSERDATA) { @@ -1078,6 +1089,7 @@ int LuaScriptInterface::elements_property(lua_State * l) else if(lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3)) { lua_el_func[id] = 0; + lua_el_mode[id] = 0; luacon_sim->elements[id].Update = NULL; } } @@ -1086,12 +1098,12 @@ int LuaScriptInterface::elements_property(lua_State * l) if(lua_type(l, 3) == LUA_TFUNCTION) { lua_pushvalue(l, 3); - lua_el_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); + lua_gr_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); luacon_sim->elements[id].Graphics = &luacon_graphicsReplacement; } else if(lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, -1)) { - lua_el_func[id] = 0; + lua_gr_func[id] = 0; luacon_sim->elements[id].Graphics = NULL; } std::fill(luacon_ren->graphicscache, luacon_ren->graphicscache+PT_NUM, gcache_item()); diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index a8cc41f37..92b6be3f8 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -24,8 +24,8 @@ #include "Snapshot.h" //#include "StorageClasses.h" -#undef LUACONSOLE -//#include "cat/LuaScriptHelper.h" +#include "cat/LuaScriptInterface.h" +#include "cat/LuaScriptHelper.h" int Simulation::Load(GameSave * save) { @@ -4132,13 +4132,9 @@ void Simulation::update_particles_i(int start, int inc) } //call the particle update function, if there is one -#ifdef LUACONSOLE if (elements[t].Update && lua_el_mode[t] != 2) -#else - if (elements[t].Update) -#endif { - if ((*(elements[t].Update))(this, i,x,y,surround_space,nt, parts, pmap)) + if ((*(elements[t].Update))(this, i, x, y, surround_space, nt, parts, pmap)) continue; else if (t==PT_WARP) { @@ -4147,17 +4143,14 @@ void Simulation::update_particles_i(int start, int inc) y = (int)(parts[i].y+0.5f); } } -#ifdef LUACONSOLE if(lua_el_mode[t]) { - if(luacon_part_update(t,i,x,y,surround_space,nt)) + if(luacon_elementReplacement(this, i, x, y, surround_space, nt, parts, pmap)) continue; // Need to update variables, in case they've been changed by Lua x = (int)(parts[i].x+0.5f); y = (int)(parts[i].y+0.5f); } -#endif - if(legacy_enable)//if heat sim is off Element::legacyUpdate(this, i,x,y,surround_space,nt, parts, pmap);