Skip to content

Commit

Permalink
fix lua element update bugs, fixes #244
Browse files Browse the repository at this point in the history
also add new mode for functions run before the normal update function
  • Loading branch information
jacob1 committed Feb 13, 2015
1 parent 84f13cf commit 03e0794
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -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))
{
Expand Down
13 changes: 11 additions & 2 deletions src/simulation/Simulation.cpp
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/simulation/elements/TUNG.cpp
Expand Up @@ -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;
}
Expand Down

0 comments on commit 03e0794

Please sign in to comment.