Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b66cbb

Browse files
committed
add resident-related functions
1 parent e783b5e commit 4b66cbb

7 files changed

Lines changed: 39 additions & 15 deletions

File tree

docs/changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ Template for new versions:
8686
## API
8787
- ``Units::isForgottenBeast``: property check for forgotten beasts
8888
- ``Units::isGreatDanger``: now includes forgotten beasts
89+
- ``Units::isResident``: property check for residents (as opposed to citizens)
90+
- ``Units::getCitizens``: now includes residents by default
8991

9092
## Lua
9193
- ``widgets.Label``: ``*pen`` attributes can now either be a pen or a function that dynamically returns a pen
9294
- `helpdb`: ``search_entries`` now returns a match if *all* filters in the ``include`` list are matched. previous behavior was to match if *any* ``include`` filter matched.
9395
- ``matinfo.decode``: now directly handles plant objects
9496
- ``dfhack.units.isForgottenBeast``: make new units method available to Lua
97+
- ``dfhack.units.getCitizens``: now includes residents by default
9598

9699
## Removed
97100

docs/dev/Lua API.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,9 +1542,11 @@ Units module
15421542

15431543
Returns a list of units (possibly empty) assigned to the given noble role.
15441544

1545-
* ``dfhack.units.getCitizens([ignore_sanity])``
1545+
* ``dfhack.units.getCitizens([exclude_residents, [include_insane]])``
15461546

1547-
Returns a list of all living citizens that are currently on the map.
1547+
Returns a list of all living, sane, citizens and residents that are currently
1548+
on the map. Pass ``exclude_residents`` and ``include_insane`` both default
1549+
to ``false`` but can be overridden.
15481550

15491551
* ``dfhack.units.teleport(unit, pos)``
15501552

library/LuaApi.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,10 +2150,11 @@ static int units_getUnitsInBox(lua_State *state)
21502150
}
21512151

21522152
static int units_getCitizens(lua_State *L) {
2153-
bool ignore_sanity = lua_toboolean(L, -1); // defaults to false
2153+
bool exclude_residents = lua_toboolean(L, 1); // defaults to false
2154+
bool include_insane = lua_toboolean(L, 2); // defaults to false
21542155

21552156
std::vector<df::unit *> citizens;
2156-
if (Units::getCitizens(citizens, ignore_sanity)) {
2157+
if (Units::getCitizens(citizens, exclude_residents, include_insane)) {
21572158
Lua::PushVector(L, citizens);
21582159
return 1;
21592160
}

library/include/modules/Units.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ DFHACK_EXPORT bool isUnitInBox(df::unit* u,
7878

7979
DFHACK_EXPORT bool isActive(df::unit *unit);
8080
DFHACK_EXPORT bool isVisible(df::unit* unit);
81-
DFHACK_EXPORT bool isCitizen(df::unit *unit, bool ignore_sanity = false);
81+
DFHACK_EXPORT bool isCitizen(df::unit *unit, bool include_insane = false);
82+
DFHACK_EXPORT bool isResident(df::unit *unit, bool include_insane = false);
8283
DFHACK_EXPORT bool isFortControlled(df::unit *unit);
8384
DFHACK_EXPORT bool isOwnCiv(df::unit* unit);
8485
DFHACK_EXPORT bool isOwnGroup(df::unit* unit);
@@ -158,7 +159,7 @@ DFHACK_EXPORT bool getUnitsInBox(std::vector<df::unit*> &units,
158159
int16_t x2, int16_t y2, int16_t z2);
159160
DFHACK_EXPORT bool getUnitsByNobleRole(std::vector<df::unit *> &units, std::string noble);
160161
DFHACK_EXPORT df::unit *getUnitByNobleRole(std::string noble);
161-
DFHACK_EXPORT bool getCitizens(std::vector<df::unit *> &citizens, bool ignore_sanity = false);
162+
DFHACK_EXPORT bool getCitizens(std::vector<df::unit *> &citizens, bool exclude_residents = false, bool include_insane = false);
162163

163164
DFHACK_EXPORT int32_t findIndexById(int32_t id);
164165

library/modules/Units.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ bool Units::isVisible(df::unit* unit)
129129
return Maps::isTileVisible(unit->pos);
130130
}
131131

132-
bool Units::isCitizen(df::unit *unit, bool ignore_sanity)
132+
bool Units::isCitizen(df::unit *unit, bool include_insane)
133133
{
134134
CHECK_NULL_POINTER(unit);
135135

@@ -149,12 +149,24 @@ bool Units::isCitizen(df::unit *unit, bool ignore_sanity)
149149
unit->flags2.bits.resident)
150150
return false;
151151

152-
if (!ignore_sanity && !isSane(unit))
152+
if (!include_insane && !isSane(unit))
153153
return false;
154154

155155
return isOwnGroup(unit);
156156
}
157157

158+
bool Units::isResident(df::unit *unit, bool include_insane){
159+
CHECK_NULL_POINTER(unit);
160+
161+
if (!include_insane && !isSane(unit))
162+
return false;
163+
164+
return isOwnCiv(unit) &&
165+
!isAnimal(unit) &&
166+
!isVisitor(unit) &&
167+
!isCitizen(unit, true);
168+
}
169+
158170
bool Units::isFortControlled(df::unit *unit)
159171
{ // Reverse-engineered from ambushing unit code
160172
CHECK_NULL_POINTER(unit);
@@ -908,9 +920,14 @@ df::unit *Units::getUnitByNobleRole(string noble) {
908920
return units[0];
909921
}
910922

911-
bool Units::getCitizens(std::vector<df::unit *> &citizens, bool ignore_sanity) {
923+
bool Units::getCitizens(std::vector<df::unit *> &citizens, bool exclude_residents, bool include_insane) {
912924
for (auto &unit : world->units.active) {
913-
if (isCitizen(unit, ignore_sanity) && isAlive(unit) && isActive(unit))
925+
if (!isAlive(unit) || !isActive(unit))
926+
continue;
927+
928+
if (isCitizen(unit, include_insane))
929+
citizens.emplace_back(unit);
930+
else if (!exclude_residents && isResident(unit, include_insane))
914931
citizens.emplace_back(unit);
915932
}
916933
return true;

plugins/autochop.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ static int32_t do_cycle(color_ostream &out, bool force_designate) {
574574
int32_t expected_yield;
575575
TreesBySize designatable_trees_by_size;
576576
vector<df::unit *> citizens;
577-
Units::getCitizens(citizens);
577+
Units::getCitizens(citizens, true);
578578
int32_t newly_marked = scan_trees(out, &expected_yield,
579579
&designatable_trees_by_size, true, citizens);
580580

@@ -662,7 +662,7 @@ static void autochop_printStatus(color_ostream &out) {
662662
int32_t designated_trees, expected_yield, accessible_yield;
663663
map<int32_t, int32_t> tree_counts, designated_tree_counts;
664664
vector<df::unit *> citizens;
665-
Units::getCitizens(citizens);
665+
Units::getCitizens(citizens, true);
666666
scan_logs(out, &usable_logs, citizens, &inaccessible_logs);
667667
scan_trees(out, &expected_yield, NULL, false, citizens, &accessible_trees, &inaccessible_trees,
668668
&designated_trees, &accessible_yield, &tree_counts, &designated_tree_counts);
@@ -767,7 +767,7 @@ static int autochop_getLogCounts(lua_State *L) {
767767
DEBUG(control,*out).print("entering autochop_getNumLogs\n");
768768
int32_t usable_logs, inaccessible_logs;
769769
vector<df::unit *> citizens;
770-
Units::getCitizens(citizens);
770+
Units::getCitizens(citizens, true);
771771
scan_logs(*out, &usable_logs, citizens, &inaccessible_logs);
772772
Lua::Push(L, usable_logs);
773773
Lua::Push(L, inaccessible_logs);
@@ -830,7 +830,7 @@ static int autochop_getTreeCountsAndBurrowConfigs(lua_State *L) {
830830
int32_t designated_trees, expected_yield, accessible_yield;
831831
map<int32_t, int32_t> tree_counts, designated_tree_counts;
832832
vector<df::unit *> citizens;
833-
Units::getCitizens(citizens);
833+
Units::getCitizens(citizens, true);
834834
scan_trees(*out, &expected_yield, NULL, false, citizens, &accessible_trees, &inaccessible_trees,
835835
&designated_trees, &accessible_yield, &tree_counts, &designated_tree_counts);
836836

plugins/seedwatch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static void scan_seeds(color_ostream &out, unordered_map<int32_t, int32_t> *acce
309309
static const BadFlags bad_flags;
310310

311311
vector<df::unit *> citizens;
312-
Units::getCitizens(citizens);
312+
Units::getCitizens(citizens, false);
313313

314314
for (auto &item : world->items.other[items_other_id::SEEDS]) {
315315
MaterialInfo mat(item);

0 commit comments

Comments
 (0)