Expose getUnitsInBox to Lua

develop
lethosor 2017-06-10 21:03:42 -04:00
parent 778ffb0971
commit 9b63c451b1
2 changed files with 38 additions and 0 deletions

@ -1059,6 +1059,13 @@ Units module
Returns true *x,y,z* of the unit, or *nil* if invalid; may be not equal to unit.pos if caged. Returns true *x,y,z* of the unit, or *nil* if invalid; may be not equal to unit.pos if caged.
* ``dfhack.getUnitsInBox(x1,y1,z1,x2,y2,z2[,filter])``
Returns a table of all units within the specified coordinates. If the ``filter``
argument is given, only units where ``filter(unit)`` returns true will be included.
Note that ``pos2xyz()`` cannot currently be used to convert coordinate objects to
the arguments required by this function.
* ``dfhack.units.getGeneralRef(unit, type)`` * ``dfhack.units.getGeneralRef(unit, type)``
Searches for a general_ref with the given type. Searches for a general_ref with the given type.

@ -1638,9 +1638,40 @@ static int units_getNoblePositions(lua_State *state)
return 1; return 1;
} }
static int units_getUnitsInBox(lua_State *state)
{
std::vector<df::unit*> units;
int x1 = luaL_checkint(state, 1);
int y1 = luaL_checkint(state, 2);
int z1 = luaL_checkint(state, 3);
int x2 = luaL_checkint(state, 4);
int y2 = luaL_checkint(state, 5);
int z2 = luaL_checkint(state, 6);
bool ok = Units::getUnitsInBox(units, x1, y1, z1, x2, y2, z2);
if (ok && !lua_isnone(state, 7))
{
luaL_checktype(state, 7, LUA_TFUNCTION);
units.erase(std::remove_if(units.begin(), units.end(), [&state](df::unit *unit) -> bool {
lua_dup(state); // copy function
Lua::PushDFObject(state, unit);
lua_call(state, 1, 1);
bool ret = lua_toboolean(state, -1);
lua_pop(state, 1); // remove return value
return !ret;
}), units.end());
}
Lua::PushVector(state, units);
lua_pushboolean(state, ok);
return 2;
}
static const luaL_Reg dfhack_units_funcs[] = { static const luaL_Reg dfhack_units_funcs[] = {
{ "getPosition", units_getPosition }, { "getPosition", units_getPosition },
{ "getNoblePositions", units_getNoblePositions }, { "getNoblePositions", units_getNoblePositions },
{ "getUnitsInBox", units_getUnitsInBox },
{ NULL, NULL } { NULL, NULL }
}; };