fix element function replacement option

This commit is contained in:
jacob1 2013-01-06 12:25:13 -05:00
parent 15040b32b0
commit 8e09651990
4 changed files with 27 additions and 19 deletions

View File

@ -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

View File

@ -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);

View File

@ -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());

View File

@ -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);