diff --git a/docs/Lua API.rst b/docs/Lua API.rst index c2b84f0f0..75196e7b9 100644 --- a/docs/Lua API.rst +++ b/docs/Lua API.rst @@ -1906,6 +1906,13 @@ Constructions module coordinates, designates it for removal, or instantly cancels the planned one. Returns *true, was_only_planned* if removed; or *false* if none found. +* ``dfhack.constructions.findAtTile(pos)``, or ``findAtTile(x,y,z)`` + + Returns the construction at the given position, or ``nil`` if there isn't one. + +* ``dfhack.constructions.insert(construction)`` + + Properly inserts the given construction into the game. Kitchen module -------------- diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 3a83f7aef..463cf3fcc 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2276,8 +2276,17 @@ static int constructions_designateRemove(lua_State *L) return 2; } +static int constructions_findAtTile(lua_State *L) +{ + auto pos = CheckCoordXYZ(L, 1, true); + Lua::PushDFObject(L, Constructions::findAtTile(pos)); + return 1; +} + static const luaL_Reg dfhack_constructions_funcs[] = { { "designateRemove", constructions_designateRemove }, + { "findAtTile", constructions_findAtTile }, + WRAPM(Constructions, insert), { NULL, NULL } }; diff --git a/library/modules/Constructions.cpp b/library/modules/Constructions.cpp index 5fc632500..407e95f76 100644 --- a/library/modules/Constructions.cpp +++ b/library/modules/Constructions.cpp @@ -54,11 +54,18 @@ using df::global::world; df::construction * Constructions::findAtTile(df::coord pos) { - for (auto it = world->constructions.begin(); it != world->constructions.end(); ++it) { - if ((*it)->pos == pos) - return *it; + int index = binsearch_index(world->constructions, pos); + if (index == -1) { + return NULL; } - return NULL; + return world->constructions[index]; +} + +bool Constructions::insert(df::construction * constr) +{ + bool toInsert; + insert_into_vector(world->constructions, &df::construction::pos, constr, &toInsert); + return toInsert; } bool Constructions::designateNew(df::coord pos, df::construction_type type,