Skip to content

Commit f7109e9

Browse files
committed
Revises ChannelJobs, ChannelGroups, and ChannelManager implementations
1 parent eaac477 commit f7109e9

File tree

5 files changed

+67
-65
lines changed

5 files changed

+67
-65
lines changed

plugins/channel-safely/channel-groups.cpp

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,41 @@ void ChannelJobs::load_channel_jobs() {
2020
}
2121
}
2222

23-
bool ChannelJobs::has_cavein_conditions(const df::coord &map_pos) {
24-
if likely(Maps::isValidTilePos(map_pos)) {
25-
auto p = map_pos;
26-
auto ttype = *Maps::getTileType(p);
27-
if (!DFHack::isOpenTerrain(ttype)) {
28-
// check shared neighbour for cave-in conditions
29-
df::coord neighbours[4];
30-
get_connected_neighbours(map_pos, neighbours);
31-
int connectedness = 4;
32-
for (auto n: neighbours) {
33-
if (!Maps::isValidTilePos(n) || active.count(n) || DFHack::isOpenTerrain(*Maps::getTileType(n))) {
34-
connectedness--;
35-
}
23+
bool ChannelJobs::has_cavein_conditions(const df::coord &map_pos) const {
24+
auto p = map_pos;
25+
auto ttype = *Maps::getTileType(p);
26+
if (!DFHack::isOpenTerrain(ttype)) {
27+
// check shared neighbour for cave-in conditions
28+
df::coord neighbours[4];
29+
get_connected_neighbours(map_pos, neighbours);
30+
int connectedness = 4;
31+
for (auto n: neighbours) {
32+
if (!Maps::isValidTilePos(n) || active.count(n) || DFHack::isOpenTerrain(*Maps::getTileType(n))) {
33+
connectedness--;
3634
}
37-
if (!connectedness) {
38-
// do what?
39-
p.z--;
40-
if (!Maps::isValidTilePos(p)) return false;
41-
ttype = *Maps::getTileType(p);
42-
if (DFHack::isOpenTerrain(ttype) || DFHack::isFloorTerrain(ttype)) {
43-
return true;
44-
}
35+
}
36+
if (!connectedness) {
37+
// do what?
38+
p.z--;
39+
if (!Maps::isValidTilePos(p)) return false;
40+
ttype = *Maps::getTileType(p);
41+
if (DFHack::isOpenTerrain(ttype) || DFHack::isFloorTerrain(ttype)) {
42+
return true;
4543
}
4644
}
4745
}
4846
return false;
4947
}
5048

51-
bool ChannelJobs::possible_cavein(const df::coord &job_pos) {
52-
for (auto iter : active) {
53-
if (iter == job_pos) continue;
54-
if (calc_distance(job_pos, iter) <= 2) {
49+
bool ChannelJobs::possible_cavein(const df::coord &job_pos) const {
50+
for (auto dig_pos : active) {
51+
if (dig_pos == job_pos) continue;
52+
if (calc_distance(job_pos, dig_pos) <= 2) {
5553
// find neighbours
5654
df::coord n1[8];
5755
df::coord n2[8];
5856
get_neighbours(job_pos, n1);
59-
get_neighbours(iter, n2);
57+
get_neighbours(dig_pos, n2);
6058
// find shared neighbours
6159
for (int i = 0; i < 7; ++i) {
6260
for (int j = i + 1; j < 8; ++j) {
@@ -181,25 +179,7 @@ void ChannelGroups::scan(bool full_scan) {
181179
full_scan = sometimes_scanFULLY(RNG);
182180
}
183181

184-
// save current jobs, then clear and load the current jobs
185-
std::set<df::coord> last_jobs;
186-
for (auto &pos : jobs) {
187-
last_jobs.emplace(pos);
188-
}
189-
jobs.load_channel_jobs();
190-
// transpose channel jobs to
191-
std::set<df::coord> new_jobs;
192-
std::set<df::coord> gone_jobs;
193-
set_difference(last_jobs, jobs, gone_jobs);
194-
set_difference(jobs, last_jobs, new_jobs);
195-
INFO(groups).print("gone jobs: %zd\nnew jobs: %zd\n",gone_jobs.size(), new_jobs.size());
196-
for (auto &pos : new_jobs) {
197-
add(pos);
198-
}
199-
for (auto &pos : gone_jobs){
200-
remove(pos);
201-
}
202-
182+
scan_jobs();
203183
DEBUG(groups).print(" scan()\n");
204184
// foreach block
205185
for (int32_t z = mapz - 1; z >= 0; --z) {
@@ -263,6 +243,28 @@ void ChannelGroups::scan(bool full_scan) {
263243
INFO(groups).print("scan() exits\n");
264244
}
265245

246+
// updates groupings of adjacent channel designations based on changes to the job list
247+
void ChannelGroups::scan_jobs() {
248+
// save current jobs, then clear and load the current jobs
249+
std::set<df::coord> last_jobs;
250+
for (auto &pos : jobs) {
251+
last_jobs.emplace(pos);
252+
}
253+
jobs.load_channel_jobs();
254+
// transpose channel jobs to
255+
std::set<df::coord> new_jobs;
256+
std::set<df::coord> gone_jobs;
257+
set_difference(last_jobs, jobs, gone_jobs);
258+
set_difference(jobs, last_jobs, new_jobs);
259+
INFO(groups).print("gone jobs: %zd\nnew jobs: %zd\n",gone_jobs.size(), new_jobs.size());
260+
for (auto &pos : new_jobs) {
261+
add(pos);
262+
}
263+
for (auto &pos : gone_jobs){
264+
remove(pos);
265+
}
266+
}
267+
266268
// clears out the containers for unloading maps or disabling the plugin
267269
void ChannelGroups::clear() {
268270
debug_map();

plugins/channel-safely/channel-manager.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#include <active-job-manager.h>
12
#include <channel-manager.h>
23
#include <tile-cache.h>
34
#include <inlines.h>
45

56
#include <modules/EventManager.h> //hash function for df::coord
67
#include <df/block_square_event_designation_priorityst.h>
8+
#include <df/unit.h>
79

810
#define NUMARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
911
#define d_assert(condition, ...) \
@@ -13,8 +15,11 @@
1315
assert(0); \
1416
}
1517

18+
namespace CSP {
19+
extern ActiveJobManager active_job_manager;
20+
}
1621

17-
df::unit* find_dwarf(const df::coord &map_pos) {
22+
df::unit* find_nearest_dwarf(const df::coord &map_pos) {
1823

1924
df::unit* nearest = nullptr;
2025
uint32_t distance;
@@ -76,7 +81,7 @@ void ChannelManager::manage_group(const Group &group, bool set_marker_mode, bool
7681
* To count access, we find a random miner dwarf and count how many tile neighbours they can path to
7782
* */
7883
// find a dwarf to path from
79-
df::coord miner_pos = find_dwarf(*group.begin())->pos;
84+
df::coord miner_pos = find_nearest_dwarf(*group.begin())->pos;
8085

8186
// Analyze designations
8287
for (const auto &pos: group) {
@@ -101,6 +106,7 @@ void ChannelManager::manage_group(const Group &group, bool set_marker_mode, bool
101106
}
102107
} else {
103108
WARN(manager).print(" has %d access\n", access);
109+
// if any
104110
cavein_possible = config.riskaverse;
105111
cavein_candidates.emplace(pos, access);
106112
least_access = std::min(access, least_access);
@@ -182,7 +188,9 @@ bool ChannelManager::manage_one(const df::coord &map_pos, bool set_marker_mode,
182188
}
183189
if (marker_mode) {
184190
if (jobs.count(map_pos)) {
185-
cancel_job(map_pos);
191+
// todo: this can still execute in the JobStartedEvent() stack
192+
CSP::active_job_manager.cancel(map_pos);
193+
//cancel_job(map_pos);
186194
}
187195
} else if (!block->flags.bits.designated) {
188196
block->flags.bits.designated = true;
@@ -202,6 +210,4 @@ bool ChannelManager::manage_one(const df::coord &map_pos, bool set_marker_mode,
202210
void ChannelManager::mark_done(const df::coord &map_pos) {
203211
groups.remove(map_pos);
204212
jobs.erase(map_pos);
205-
CSP::dignow_queue.erase(map_pos);
206-
TileCache::Get().uncache(map_pos);
207213
}

plugins/channel-safely/include/active-job-manager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ class ActiveJobManager {
5959
void on_job_start(df::job* job);
6060
void on_job_completed(color_ostream &out, df::job* job);
6161
void on_report_event(df::report* report);
62+
void cancel(df::coord site) {cancel_queue.emplace(site);}
6263
void clear();
63-
};
64+
};

plugins/channel-safely/include/channel-groups.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ChannelGroups {
3333
GroupsMap groups_map;
3434
Groups groups;
3535
ChannelJobs &jobs;
36-
std::set<int> free_spots;
36+
std::set<int> free_spots; // ids of groups that are empty, recycling empty groups
3737
protected:
3838
void add(const df::coord &map_pos);
3939
public:
@@ -46,6 +46,7 @@ class ChannelGroups {
4646
explicit ChannelGroups(ChannelJobs &jobs) : jobs(jobs) { groups.reserve(200); }
4747
void scan_one(const df::coord &map_pos);
4848
void scan(bool full_scan = false);
49+
void scan_jobs();
4950
void clear();
5051
void remove(const df::coord &map_pos);
5152
Groups::const_iterator find(const df::coord &map_pos) const;

plugins/channel-safely/include/channel-jobs.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,16 @@ using namespace DFHack;
2121
*/
2222
class ChannelJobs {
2323
private:
24-
using Jobs = std::unordered_set<df::coord>; // job* will exist until it is complete, and likely beyond
2524
std::unordered_map<df::coord, df::job*> jobs;
26-
Jobs locations;
27-
Jobs active;
28-
protected:
29-
bool has_cavein_conditions(const df::coord &map_pos);
25+
std::unordered_set<df::coord> locations;
3026
public:
3127
void load_channel_jobs();
32-
bool possible_cavein(const df::coord &job_pos);
33-
void mark_active(const df::coord &map_pos) { active.emplace(map_pos); }
3428
void clear() {
35-
active.clear();
3629
locations.clear();
3730
jobs.clear();
3831
}
3932
int count(const df::coord &map_pos) const { return locations.count(map_pos); }
40-
Jobs::iterator erase(const df::coord &map_pos) {
41-
active.erase(map_pos);
33+
std::unordered_set<df::coord>::iterator erase(const df::coord &map_pos) {
4234
jobs.erase(map_pos);
4335
auto iter = locations.find(map_pos);
4436
if (iter != locations.end()) {
@@ -47,7 +39,7 @@ class ChannelJobs {
4739
return iter;
4840
}
4941
df::job* find_job(const df::coord &map_pos) const { return jobs.count(map_pos) ? jobs.find(map_pos)->second : nullptr; }
50-
Jobs::const_iterator find(const df::coord &map_pos) const { return locations.find(map_pos); }
51-
Jobs::const_iterator begin() const { return locations.begin(); }
52-
Jobs::const_iterator end() const { return locations.end(); }
42+
std::unordered_set<df::coord>::const_iterator find(const df::coord &map_pos) const { return locations.find(map_pos); }
43+
std::unordered_set<df::coord>::const_iterator begin() const { return locations.begin(); }
44+
std::unordered_set<df::coord>::const_iterator end() const { return locations.end(); }
5345
};

0 commit comments

Comments
 (0)