fix lua element update bugs, fixes #244

also add new mode for functions run before the normal update function
This commit is contained in:
jacob1 2015-02-13 14:18:55 -05:00
parent 84f13cfe63
commit 03e07945e3
4 changed files with 24 additions and 10 deletions

View File

@ -699,10 +699,12 @@ int luatpt_element_func(lua_State *l)
if(element > 0 && element < PT_NUM)
{
lua_el_func[element] = function;
if(replace)
lua_el_mode[element] = 2;
if (replace == 2)
lua_el_mode[element] = 3; //update before
else if (replace)
lua_el_mode[element] = 2; //replace
else
lua_el_mode[element] = 1;
lua_el_mode[element] = 1; //update after
return 0;
}
else

View File

@ -2381,19 +2381,21 @@ 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);
if (args > 3)
{
luaL_checktype(l, 4, LUA_TNUMBER);
int replace = lua_tointeger(l, 4);
if (replace == 1)
lua_el_mode[id] = 2;
if (replace == 2)
lua_el_mode[id] = 3; //update before
else if (replace == 1)
lua_el_mode[id] = 2; //replace
else
lua_el_mode[id] = 1;
lua_el_mode[id] = 1; //update after
}
else
lua_el_mode[id] = 1;
lua_pushvalue(l, 3);
lua_el_func[id] = luaL_ref(l, LUA_REGISTRYINDEX);
}
else if(lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3))
{

View File

@ -3817,6 +3817,15 @@ void Simulation::UpdateParticles(int start, int end)
//call the particle update function, if there is one
#if !defined(RENDERER) && defined(LUACONSOLE)
if (lua_el_mode[parts[i].type] == 3)
{
if (luacon_elementReplacement(this, i, x, y, surround_space, nt, parts, pmap) || t != parts[i].type)
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);
}
if (elements[t].Update && lua_el_mode[t] != 2)
#else
if (elements[t].Update)
@ -3832,9 +3841,9 @@ void Simulation::UpdateParticles(int start, int end)
}
}
#if !defined(RENDERER) && defined(LUACONSOLE)
if(lua_el_mode[t])
if (lua_el_mode[parts[i].type] && lua_el_mode[parts[i].type] != 3)
{
if(luacon_elementReplacement(this, i, x, y, surround_space, nt, parts, pmap))
if (luacon_elementReplacement(this, i, x, y, surround_space, nt, parts, pmap) || t != parts[i].type)
continue;
// Need to update variables, in case they've been changed by Lua
x = (int)(parts[i].x+0.5f);

View File

@ -102,6 +102,7 @@ int Element_TUNG::update(UPDATE_FUNC_ARGS)
{
sim->part_change_type(i,x,y,PT_BRMT);
parts[i].ctype = PT_TUNG;
return 1;
}
return 0;
}