From e2f1598547e12b4ff304b2346bf9ef19af56d9ac Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 24 Sep 2024 13:30:56 -0700 Subject: [PATCH 1/8] Update Units.cpp --- library/modules/Units.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index 6f58a38b2f..3fd279a69f 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -122,7 +122,11 @@ constexpr uint32_t exclude_flags2 = ( bool Units::isActive(df::unit *unit) { CHECK_NULL_POINTER(unit); - return !unit->flags1.bits.inactive; + if (unit->flags1.bits.inactive) + return false; + else if (unit->flags1.bits.move_state || unit->flags1.bits.can_swap) + return true; // These are always unset when leaving the map + return linear_index(world->units.active, &df::unit::id, unit->id) >= 0; } bool Units::isVisible(df::unit *unit) { @@ -658,7 +662,7 @@ bool Units::isGreatDanger(df::unit *unit) { bool Units::isUnitInBox(df::unit *u, const cuboid &box) { CHECK_NULL_POINTER(u); - return box.containsPos(getPosition(u)); + return isActive(u) ? box.containsPos(getPosition(u)) : false; } bool Units::getUnitsInBox(vector &units, const cuboid &box, std::function filter) { @@ -743,6 +747,8 @@ bool Units::getCitizens(vector &citizens, bool exclude_residents, bo df::coord Units::getPosition(df::unit *unit) { CHECK_NULL_POINTER(unit); + if (!isActive(unit)) + return df::coord(); if (unit->flags1.bits.caged) { if (auto cage = getContainer(unit)) return Items::getPosition(cage); From 6f51b36fcd7a77687de2f5f1a9c8266e33532f96 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 24 Sep 2024 13:35:04 -0700 Subject: [PATCH 2/8] Update Units.h --- library/include/modules/Units.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index 7eaa61ec62..f2fdd453b3 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -202,7 +202,7 @@ inline auto citizensRange(std::vector &vec, bool exclude_residents = DFHACK_EXPORT void forCitizens(std::function fn, bool exclude_residents = false, bool include_insane = false); DFHACK_EXPORT bool getCitizens(std::vector &citizens, bool exclude_residents = false, bool include_insane = false); -// Returns the true position of the unit (non-trivial in case of caged). +// Get the true position of the unit (non-trivial in case of caged or inactive). Returns invalid coord if dead or off map. DFHACK_EXPORT df::coord getPosition(df::unit *unit); // Moves unit and any riders to the target coordinates. Sets tile occupancy flags. From b0da75df1548d8c3473276794653582033e76678 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 24 Sep 2024 13:36:41 -0700 Subject: [PATCH 3/8] Update Lua API.rst --- docs/dev/Lua API.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 232daea9eb..bb15ae378e 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -1643,7 +1643,7 @@ Units module Returns the true *x,y,z* of the unit, or *nil* if invalid. You should generally use this method instead of reading *unit.pos* directly since - that field can be inaccurate when the unit is caged. + that field can be inaccurate when the unit is caged, off map, or dead. * ``dfhack.units.teleport(unit, pos)`` From 556a8bdd46b66c7abc21490cf5c72f61a98b8af4 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 24 Sep 2024 13:46:45 -0700 Subject: [PATCH 4/8] Update changelog.txt --- docs/changelog.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 656a31a8b0..1ac58a7e04 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -58,6 +58,8 @@ Template for new versions: ## Fixes - `preserve-rooms`: don't reserve a room for citizens that you expel from the fort - `autobutcher`: fix regression in ordering of butcherable animals +- ``Units::getPosition``, ``Items::getPosition``: return invalid coord for inactive units, items held by units off map +- ``Units::isActive``: return false for units off map ## Misc Improvements From 8d1ddb7a2a1ef9cf6c99f73474a26cf0f658077b Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 24 Sep 2024 13:49:49 -0700 Subject: [PATCH 5/8] Update RemoteTools.cpp Don't follow units that are out on raid. --- library/RemoteTools.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/RemoteTools.cpp b/library/RemoteTools.cpp index c8fc810c8b..79fcf7b225 100644 --- a/library/RemoteTools.cpp +++ b/library/RemoteTools.cpp @@ -584,7 +584,7 @@ static command_result ListUnits(color_ostream &stream, if (in->scan_all()) { - auto &vec = df::unit::get_vector(); + auto &vec = df::global::world->units.active; for (size_t i = 0; i < vec.size(); i++) { From 6f196e252e75fbbd193d65a5bd18a2a685633495 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 24 Sep 2024 13:52:32 -0700 Subject: [PATCH 6/8] Update changelog.txt --- docs/changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 1ac58a7e04..ed96d904e8 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -59,6 +59,7 @@ Template for new versions: - `preserve-rooms`: don't reserve a room for citizens that you expel from the fort - `autobutcher`: fix regression in ordering of butcherable animals - ``Units::getPosition``, ``Items::getPosition``: return invalid coord for inactive units, items held by units off map +- ``Units::isUnitInBox``, ``Units::getUnitsInBox``: don't include inactive units - ``Units::isActive``: return false for units off map ## Misc Improvements From 2750fcf0332ba2d6e0b909d734b1a074a9c206d3 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 24 Sep 2024 14:11:56 -0700 Subject: [PATCH 7/8] Update Items.h --- library/include/modules/Items.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 79aed92c36..e2f5727676 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -137,7 +137,7 @@ DFHACK_EXPORT df::building *getHolderBuilding(df::item *item); // Get unit that holds the item or NULL. DFHACK_EXPORT df::unit *getHolderUnit(df::item *item); -// Returns the true position of the item (non-trivial if in inventory). +// Get the true position of the item (non-trivial if in inventory). Return invalid coord if holder off map. DFHACK_EXPORT df::coord getPosition(df::item *item); /// Returns the title of a codex or "tool", either as the codex title or as the title of the From 2995ab9a98b7aaf434b7bc35ba48975f5fb5edc3 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Wed, 25 Sep 2024 17:54:44 -0700 Subject: [PATCH 8/8] Update changelog.txt --- docs/changelog.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 2b3d7f5104..2f777f1797 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -56,6 +56,9 @@ Template for new versions: ## New Features ## Fixes +- ``Units::getPosition``, ``Items::getPosition``: return invalid coord for inactive units, items held by units off map +- ``Units::isUnitInBox``, ``Units::getUnitsInBox``: don't include inactive units +- ``Units::isActive``: return false for units off map ## Misc Improvements @@ -76,9 +79,6 @@ Template for new versions: ## Fixes - `preserve-rooms`: don't reserve a room for citizens that you expel from the fort - `autobutcher`: fix regression in ordering of butcherable animals -- ``Units::getPosition``, ``Items::getPosition``: return invalid coord for inactive units, items held by units off map -- ``Units::isUnitInBox``, ``Units::getUnitsInBox``: don't include inactive units -- ``Units::isActive``: return false for units off map ## Misc Improvements