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

Skip to content

Commit 05b8c01

Browse files
committed
expose range to callers
so they can control their own for loops
1 parent a1018a6 commit 05b8c01

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

library/include/modules/Units.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ distribution.
4141
#include "df/unit_action.h"
4242
#include "df/unit_action_type_group.h"
4343

44+
#include <ranges>
45+
4446
namespace df
4547
{
4648
struct activity_entry;
@@ -159,6 +161,17 @@ DFHACK_EXPORT bool getUnitsInBox(std::vector<df::unit*> &units,
159161
int16_t x2, int16_t y2, int16_t z2);
160162
DFHACK_EXPORT bool getUnitsByNobleRole(std::vector<df::unit *> &units, std::string noble);
161163
DFHACK_EXPORT df::unit *getUnitByNobleRole(std::string noble);
164+
165+
inline auto citizensRange(std::vector<df::unit *> &vec, bool exclude_residents = false, bool include_insane = false) {
166+
return vec |
167+
std::views::filter([=](df::unit * unit) {
168+
if (isDead(unit) || !isActive(unit))
169+
return false;
170+
return isCitizen(unit, include_insane) ||
171+
(!exclude_residents && isResident(unit, include_insane));
172+
});
173+
}
174+
162175
DFHACK_EXPORT void forCitizens(std::function<void(df::unit *)> fn, bool exclude_residents = false, bool include_insane = false);
163176
DFHACK_EXPORT bool getCitizens(std::vector<df::unit *> &citizens, bool exclude_residents = false, bool include_insane = false);
164177

library/modules/Units.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ distribution.
8989
#include <algorithm>
9090
#include <numeric>
9191
#include <functional>
92-
#include <ranges>
9392

9493
using std::string;
9594
using std::vector;
@@ -921,23 +920,13 @@ df::unit *Units::getUnitByNobleRole(string noble) {
921920
return units[0];
922921
}
923922

924-
static auto citizensRange(bool exclude_residents, bool include_insane) {
925-
return world->units.active |
926-
std::views::filter([=](df::unit * unit) {
927-
if (Units::isDead(unit) || !Units::isActive(unit))
928-
return false;
929-
return Units::isCitizen(unit, include_insane) ||
930-
(!exclude_residents && Units::isResident(unit, include_insane));
931-
});
932-
}
933-
934923
void Units::forCitizens(std::function<void(df::unit *)> fn, bool exclude_residents, bool include_insane) {
935-
for (auto unit : citizensRange(exclude_residents, include_insane))
924+
for (auto unit : citizensRange(world->units.active, exclude_residents, include_insane))
936925
fn(unit);
937926
}
938927

939-
bool Units::getCitizens(std::vector<df::unit *> & citizens, bool exclude_residents, bool include_insane) {
940-
for (auto unit : citizensRange(exclude_residents, include_insane))
928+
bool Units::getCitizens(vector<df::unit *> & citizens, bool exclude_residents, bool include_insane) {
929+
for (auto unit : citizensRange(world->units.active, exclude_residents, include_insane))
941930
citizens.emplace_back(unit);
942931
return true;
943932
}

plugins/reveal.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,10 @@ command_result revflood(color_ostream &out, vector<string> & params) {
445445
pos = Units::getPosition(unit);
446446
}
447447
if (!pos.isValid()) {
448-
Units::forCitizens([&](auto unit) {
448+
for (auto unit : Units::citizensRange(world->units.active)) {
449449
pos = Units::getPosition(unit);
450-
});
450+
break;
451+
}
451452
}
452453
if (!pos.isValid()) {
453454
out.printerr("Please select a unit or place the keyboard cursor at some empty space you want to be unhidden.\n");

plugins/tailor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,10 @@ class Tailor {
242242

243243
void scan_replacements()
244244
{
245-
Units::forCitizens([&](auto u) {
245+
for (auto u : Units::citizensRange(world->units.active)) {
246246
if (Units::isBaby(u) ||
247247
!Units::casteFlagSet(u->race, u->caste, df::enums::caste_raw_flags::EQUIPS))
248-
return; // skip units we don't control or that can't wear clothes
248+
continue; // skip units we don't control or that can't wear clothes
249249

250250
std::set <df::item_type> wearing;
251251
std::set <df::item_type> ordered;
@@ -326,7 +326,7 @@ class Tailor {
326326
needed[std::make_pair(ty, usize)] += 1;
327327
}
328328
}
329-
});
329+
}
330330
}
331331

332332
void create_orders()

0 commit comments

Comments
 (0)