From 604eb47f8774159759efac8c14b1d1f4b4296ab8 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 2 Nov 2023 13:55:08 -0700 Subject: [PATCH] implement burrow name matching with ignoring plus --- docs/dev/Lua API.rst | 6 +++-- library/include/modules/Burrows.h | 2 +- library/modules/Burrows.cpp | 37 +++++++++---------------------- plugins/burrow.cpp | 2 +- 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index dd057fe01..abab94829 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1949,9 +1949,11 @@ Maps module Burrows module -------------- -* ``dfhack.burrows.findByName(name)`` +* ``dfhack.burrows.findByName(name[, ignore_final_plus])`` - Returns the burrow pointer or *nil*. + Returns the burrow pointer or *nil*. if ``ignore_final_plus`` is ``true``, + then ``+`` characters at the end of the names are ignored, both for the + specified ``name`` and the names of the burrows that it matches against. * ``dfhack.burrows.clearUnits(burrow)`` diff --git a/library/include/modules/Burrows.h b/library/include/modules/Burrows.h index 49b7df3c1..b2e3e56b4 100644 --- a/library/include/modules/Burrows.h +++ b/library/include/modules/Burrows.h @@ -45,7 +45,7 @@ namespace DFHack { namespace Burrows { - DFHACK_EXPORT df::burrow *findByName(std::string name); + DFHACK_EXPORT df::burrow *findByName(std::string name, bool ignore_final_plus = false); // Units DFHACK_EXPORT void clearUnits(df::burrow *burrow); diff --git a/library/modules/Burrows.cpp b/library/modules/Burrows.cpp index c2fe45eae..9e63c0dbc 100644 --- a/library/modules/Burrows.cpp +++ b/library/modules/Burrows.cpp @@ -51,13 +51,21 @@ using namespace df::enums; using df::global::world; using df::global::plotinfo; -df::burrow *Burrows::findByName(std::string name) +df::burrow *Burrows::findByName(std::string name, bool ignore_final_plus) { auto &vec = df::burrow::get_vector(); - for (size_t i = 0; i < vec.size(); i++) - if (vec[i]->name == name) + if (ignore_final_plus && name.ends_with('+')) + name = name.substr(0, name.length() - 1); + + for (size_t i = 0; i < vec.size(); i++) { + std::string bname = vec[i]->name; + if (ignore_final_plus && bname.ends_with('+')) + bname = bname.substr(0, bname.length() - 1); + + if (bname == name) return vec[i]; + } return NULL; } @@ -75,18 +83,6 @@ void Burrows::clearUnits(df::burrow *burrow) } burrow->units.clear(); - -/* TODO: understand how this changes for v50 - // Sync plotinfo if active - if (plotinfo && plotinfo->main.mode == ui_sidebar_mode::Burrows && - plotinfo->burrows.in_add_units_mode && plotinfo->burrows.sel_id == burrow->id) - { - auto &sel = plotinfo->burrows.sel_units; - - for (size_t i = 0; i < sel.size(); i++) - sel[i] = false; - } -*/ } bool Burrows::isAssignedUnit(df::burrow *burrow, df::unit *unit) @@ -114,17 +110,6 @@ void Burrows::setAssignedUnit(df::burrow *burrow, df::unit *unit, bool enable) erase_from_vector(unit->burrows, burrow->id); erase_from_vector(burrow->units, unit->id); } - -/* TODO: understand how this changes for v50 - // Sync plotinfo if active - if (plotinfo && plotinfo->main.mode == ui_sidebar_mode::Burrows && - plotinfo->burrows.in_add_units_mode && plotinfo->burrows.sel_id == burrow->id) - { - int idx = linear_index(plotinfo->burrows.list_units, unit); - if (idx >= 0) - plotinfo->burrows.sel_units[idx] = enable; - } -*/ } void Burrows::listBlocks(std::vector *pvec, df::burrow *burrow) diff --git a/plugins/burrow.cpp b/plugins/burrow.cpp index f33e9e910..413a8eb6e 100644 --- a/plugins/burrow.cpp +++ b/plugins/burrow.cpp @@ -279,7 +279,7 @@ static df::burrow* get_burrow(lua_State *L, int idx) { if (lua_isuserdata(L, idx)) burrow = Lua::GetDFObject(L, idx); else if (lua_isstring(L, idx)) - burrow = Burrows::findByName(luaL_checkstring(L, idx)); + burrow = Burrows::findByName(luaL_checkstring(L, idx), true); else if (lua_isinteger(L, idx)) burrow = df::burrow::find(luaL_checkinteger(L, idx)); return burrow;