MCPcopy Create free account
hub / github.com/DFHack/dfhack / units_getUnitsInBox

Function units_getUnitsInBox

library/LuaApi.cpp:2328–2371  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2326}
2327
2328static int units_getUnitsInBox(lua_State *state) {
2329 vector<df::unit *> units;
2330 cuboid box;
2331
2332 int max_arg = lua_gettop(state);
2333 if (max_arg > 3) {
2334 int x1 = luaL_checkint(state, 1);
2335 int y1 = luaL_checkint(state, 2);
2336 int z1 = luaL_checkint(state, 3);
2337 int x2 = luaL_checkint(state, 4);
2338 int y2 = luaL_checkint(state, 5);
2339 int z2 = luaL_checkint(state, 6);
2340 box = cuboid(x1,y1,z1,x2,y2,z2);
2341 }
2342 else {
2343 df::coord pos1, pos2;
2344 Lua::CheckDFAssign(state, &pos1, 1);
2345 Lua::CheckDFAssign(state, &pos2, 2);
2346 box = cuboid(pos1, pos2);
2347 }
2348
2349 int fn_arg = max_arg > 3 ? 7 : 3;
2350 bool ok = false;
2351 if (max_arg < fn_arg || lua_isnil(state, fn_arg)) // Default filter
2352 ok = Units::getUnitsInBox(units, box);
2353 else {
2354 luaL_checktype(state, fn_arg, LUA_TFUNCTION);
2355 if (max_arg > fn_arg) // Something after filter on stack
2356 luaL_argerror(state, fn_arg+1, "too many arguments!");
2357
2358 ok = Units::getUnitsInBox(units, box, [&state](df::unit *unit) {
2359 lua_dup(state); // Copy function
2360 Lua::PushDFObject(state, unit);
2361 lua_call(state, 1, 1);
2362 bool ret = lua_toboolean(state, -1);
2363 lua_pop(state, 1); // Remove return value
2364 return ret;
2365 });
2366 }
2367
2368 Lua::PushVector(state, units);
2369 lua_pushboolean(state, ok);
2370 return 2;
2371}
2372
2373static int units_getCitizens(lua_State *L) {
2374 bool exclude_residents = lua_toboolean(L, 1); // defaults to false

Callers

nothing calls this directly

Calls 11

lua_gettopFunction · 0.85
cuboidClass · 0.85
CheckDFAssignFunction · 0.85
getUnitsInBoxFunction · 0.85
luaL_checktypeFunction · 0.85
luaL_argerrorFunction · 0.85
lua_dupFunction · 0.85
PushDFObjectFunction · 0.85
lua_tobooleanFunction · 0.85
PushVectorFunction · 0.85
lua_pushbooleanFunction · 0.85

Tested by

no test coverage detected