fix #3396 lua_function= doesn't work in [effect][filter]

unfortunately this adds an annoying const_cast. The old code 'worked around'
this by getting a nonconst reference to the same unit via the unit map,
which i think is not better just hides the issue.

(cherry-picked from commit 08b7d1b1ac)
This commit is contained in:
gfgtdf 2018-09-10 20:32:41 +02:00
parent c48735c0fb
commit 21e6b399e8

View file

@ -4763,12 +4763,16 @@ bool game_lua_kernel::run_filter(char const *name, const map_location& l)
bool game_lua_kernel::run_filter(char const *name, const unit& u)
{
lua_State *L = mState;
unit_map::const_unit_iterator ui = units().find(u.get_location());
if (!ui.valid()) return false;
// Pass the unit as argument.
luaW_pushunit(L, ui->underlying_id());
return run_filter(name, 1);
lua_unit* lu = luaW_pushlocalunit(L, const_cast<unit&>(u));
// stack: unit
// put the unit to the stack twice to prevent gc.
lua_pushvalue(L, -1);
// stack: unit, unit
bool res = run_filter(name, 1);
// stack: unit
lu->clear_ref();
lua_pop(L, 1);
return res;
}
/**
* Runs a script from a filter.