Skip to content

Commit 5142070

Browse files
committed
Updates logging
1 parent a7852cd commit 5142070

File tree

6 files changed

+83
-118
lines changed

6 files changed

+83
-118
lines changed

plugins/channel-safely/active-job-manager.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ bool ActiveJobManager::possible_cavein(const df::coord &map_pos) const {
123123
for (int j = i + 1; j < 8; ++j) {
124124
if (n1[i] == n2[j]) {
125125
if (has_cavein_conditions(n1[i])) {
126-
WARN(jobs).print("Channel-Safely::jobs: Cave-in conditions detected at (" COORD ")\n", COORDARGS(n1[i]));
126+
WARN(jobs).print("Cave-in conditions detected at ({})\n", n1[i]);
127127
return true;
128128
}
129129
}
@@ -230,6 +230,7 @@ void ActiveJobManager::on_update(color_ostream &out) {
230230
extern DFHack::EventManager::EventHandler resurrectHandler;
231231

232232
void ActiveJobManager::on_job_start(df::job* job) {
233+
INFO(jobs).print("{} job started", job->pos);
233234
if (!ChannelManager::Get().contains(job->pos)) {
234235
ChannelManager::Get().build_groups(false);
235236
}
@@ -259,6 +260,7 @@ void ActiveJobManager::on_job_start(df::job* job) {
259260
}
260261
// if a cavein is possible - we'll try to cancel the job
261262
if (has_cavein_conditions(pos)) {
263+
INFO(jobs).print("cavein conditions found at {}", pos);
262264
/* todo:
263265
* test if the game crashes or the jobs start polluting the list indefinitely
264266
* prediction is that the jobs will cause the tiles to flash forever
@@ -277,12 +279,15 @@ void ActiveJobManager::on_job_start(df::job* job) {
277279
}
278280

279281
void ActiveJobManager::on_job_completed(color_ostream &out, df::job* job) {
282+
INFO(jobs).print("job completed at {}\n", job->pos);
280283
if (!active_jobs.contains(job->id)) {
284+
DEBUG(jobs).print("job completed [id: {}] but was not an active job at {}\n", job->id, job->pos);
281285
return;
282286
}
283287
auto ajob = active_jobs[job->id];
284288
auto aworker = active_workers[ajob.id];
285289
if (config.resurrect && !Units::isAlive(aworker.worker)) {
290+
DEBUG(jobs).print("worker [id: {}] is dead, resurrect is enabled.\n", aworker.id);
286291
resurrect(out, aworker.id);
287292
df::coord lowest = simulate_fall(aworker.last_safe_pos);
288293
Units::teleport(aworker.worker, lowest);
@@ -294,6 +299,7 @@ void ActiveJobManager::on_job_completed(color_ostream &out, df::job* job) {
294299
local.x = local.x % 16;
295300
local.y = local.y % 16;
296301
if (!TileCache::Get().hasChanged(ajob.pos, block->tiletype[Coord(local)])) {
302+
DEBUG(jobs).print("active job completed [id: {}] at {} but the tile is the same as before.\n", job->id, job->pos);
297303
return;
298304
}
299305
// the job can be considered done
@@ -302,7 +308,7 @@ void ActiveJobManager::on_job_completed(color_ostream &out, df::job* job) {
302308
block->designation[Coord(local)].bits.traffic = df::tile_traffic::Normal;
303309
df::coord below(ajob.pos);
304310
below.z--;
305-
DEBUG(jobs).print(" -> (" COORD ") is marked done, managing group below.\n", COORDARGS(ajob.pos));
311+
DEBUG(jobs).print(" -> ({}) is marked done, managing group below.\n", COORDARGS(ajob.pos));
306312
ChannelManager::Get().manage_group(below);
307313

308314
// erase tracked data

plugins/channel-safely/channel-groups.cpp

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ void ChannelJobs::load_channel_jobs() {
1212
df::job_list_link* node = df::global::world->jobs.list.next;
1313
while (node) {
1414
df::job* job = node->item;
15-
node = node->next;
1615
if (is_channel_job(job)) {
16+
TRACE(jobs).print("parsing jobs list, found job [id: {}] at {}\n", job->id, job->pos);
1717
job_ptrs[job->pos] = job;
1818
}
19+
node = node->next;
1920
}
2021
}
2122

@@ -29,6 +30,7 @@ void ChannelGroups::add(const df::coord &map_pos) {
2930
* So what we do is we look at neighbours to see if they belong to one or more existing groups
3031
* If there is more than one group, we'll be merging them
3132
*/
33+
// x=47, y=63, z=166
3234
df::coord neighbors[8];
3335
get_neighbours(map_pos, neighbors);
3436
Group* group = nullptr;
@@ -53,24 +55,23 @@ void ChannelGroups::add(const df::coord &map_pos) {
5355
// different. merge
5456
Group* group2 = &groups_array.at(index2);
5557
// which N is smaller, transfer it's elements
56-
if (groups_array[group_index].size() > groups_array[index2].size()) {
57-
for (auto pos: *group2) {
58-
group->emplace(pos);
59-
pos_to_groups_idx_map[pos] = group_index;
58+
auto merge_groups = [&](Group* bigger, Group* smaller, int i1, int i2) {
59+
for (auto pos: *smaller) {
60+
bigger->emplace(pos);
61+
pos_to_groups_idx_map[pos] = i1;
6062
}
61-
group2->clear();
62-
free_spots.emplace(index2);
63-
// group2 merged into group
63+
smaller->clear();
64+
free_spots.emplace(i2);
65+
};
66+
if (groups_array[group_index].size() > groups_array[index2].size()) {
67+
// group2 merges into group
68+
merge_groups(group, group2, group_index, index2);
6469
} else {
65-
for (auto pos: *group) {
66-
group2->emplace(pos);
67-
pos_to_groups_idx_map[pos] = index2;
68-
}
69-
group->clear();
70-
group = group2;
71-
free_spots.emplace(group_index);
70+
// group merges into group2
71+
merge_groups(group2, group, index2, group_index);
72+
// group info updated
7273
group_index = index2;
73-
// group merged into group2. group updated
74+
group = group2;
7475
}
7576
}
7677
// if we haven't found at least one group by now we need to create/get one
@@ -80,6 +81,7 @@ void ChannelGroups::add(const df::coord &map_pos) {
8081
// first element in a set is always the lowest value, so we re-use from the front of the vector
8182
group_index = *free_spots.begin();
8283
group = &groups_array[group_index];
84+
group->clear(); // just to be safe i.e. redundant?
8385
free_spots.erase(free_spots.begin());
8486
} else {
8587
// we create a brand-new group to use
@@ -91,13 +93,7 @@ void ChannelGroups::add(const df::coord &map_pos) {
9193
// puts the "add" in "ChannelGroups::add"
9294
pos_to_groups_idx_map[map_pos] = group_index;
9395
group->emplace(map_pos);
94-
DEBUG(groups).print(" = group[%d] of (" COORD ") is size: %zu\n", group_index, COORDARGS(map_pos), group->size());
95-
96-
// we may have performed a merge, so we update all the `coord -> group index` mappings
97-
// for (auto &wpos: *group) {
98-
// pos_to_groups_idx_map[wpos] = group_index;
99-
// }
100-
DEBUG(groups).print(" <- add() exits, there are %zu mappings\n", pos_to_groups_idx_map.size());
96+
DEBUG(groups).print("group {} added {}, group size is now {}\n", group_index, map_pos, group->size());
10197
}
10298

10399
// scans a single tile for channel designations
@@ -110,13 +106,14 @@ void ChannelGroups::scan_one(const df::coord &map_pos) {
110106
for (df::block_square_event* event: block->block_events) {
111107
if (auto evT = virtual_cast<df::block_square_event_designation_priorityst>(event)) {
112108
// we want to let the user keep some designations free of being managed
113-
TRACE(groups).print(" tile designation priority: %d\n", evT->priority[lx][ly]);
114-
if (evT->priority[lx][ly] < 1001 * config.ignore_threshold) {
109+
if (evT->priority[lx][ly] < 1 + (1000 * config.ignore_threshold)) {
110+
TRACE(groups).print("scan_one, adding {}.\n", map_pos);
115111
add(map_pos);
116112
}
117113
}
118114
}
119115
} else if (TileCache::Get().hasChanged(map_pos, block->tiletype[lx][ly])) {
116+
DEBUG(groups).print("scan_one, the tile {} changed.\n", map_pos);
120117
TileCache::Get().uncache(map_pos);
121118
remove(map_pos);
122119
if (jobs.contains(map_pos)) {
@@ -135,7 +132,7 @@ void ChannelGroups::scan(bool full_scan) {
135132
}
136133

137134
scan_jobs();
138-
DEBUG(groups).print(" scan()\n");
135+
TRACE(groups).print(" scan()\n");
139136
// foreach block
140137
for (int32_t z = mapz - 1; z >= 0; --z) {
141138
for (int32_t by = 0; by < mapy; ++by) {
@@ -158,7 +155,7 @@ void ChannelGroups::scan(bool full_scan) {
158155
}
159156
}
160157
}
161-
INFO(groups).print("scan() exits\n");
158+
TRACE(groups).print("scan() exits\n");
162159
}
163160

164161
// updates groupings of adjacent channel designations based on changes to the job list
@@ -178,6 +175,7 @@ void ChannelGroups::scan_jobs() {
178175
set_difference(current_job_locations, last_job_locations, new_jobs);
179176

180177
for (auto &pos : new_jobs) {
178+
DEBUG(groups).print("new job at {}\n", pos);
181179
add(pos);
182180
}
183181
}
@@ -241,9 +239,9 @@ void ChannelGroups::debug_groups() {
241239
int idx = 0;
242240
DEBUG(groups).print(" debugging group data\n");
243241
for (auto &group: groups_array) {
244-
DEBUG(groups).print(" group %d (size: %zu)\n", idx, group.size());
242+
DEBUG(groups).print(" group {} (size: {})\n", idx, group.size());
245243
for (auto &pos: group) {
246-
DEBUG(groups).print(" (%d,%d,%d)\n", pos.x, pos.y, pos.z);
244+
DEBUG(groups).print(" {}\n", pos);
247245
}
248246
idx++;
249247
}
@@ -253,9 +251,9 @@ void ChannelGroups::debug_groups() {
253251
// prints debug info group mappings
254252
void ChannelGroups::debug_map() {
255253
if (DFHack::debug_groups.isEnabled(DebugCategory::LTRACE)) {
256-
INFO(groups).print("Group Mappings: %zu\n", pos_to_groups_idx_map.size());
254+
INFO(groups).print("Group Mappings: {}\n", pos_to_groups_idx_map.size());
257255
for (auto &pair: pos_to_groups_idx_map) {
258-
TRACE(groups).print(" map[" COORD "] = %d\n", COORDARGS(pair.first), pair.second);
256+
TRACE(groups).print(" map[{}] = {}\n", pair.first, pair.second);
259257
}
260258
}
261259
}

plugins/channel-safely/channel-manager.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ void ChannelManager::manage_groups() {
4444
// make sure we've got a fort map to analyze
4545
if (World::isFortressMode() && Maps::IsValid()) {
4646
// iterate the groups we built/updated
47+
INFO(manager).print("manage_groups(): iterating groups\n");
4748
for (const auto &group: groups.keys()) {
49+
DEBUG(manager).print("managing group starting at {}\n",*group.begin());
4850
manage_group(group, true, has_any_groups_above(groups, group));
4951
}
5052
}
@@ -78,27 +80,27 @@ void ChannelManager::manage_group(const std::set<df::coord> &group, bool use_mm_
7880
continue;
7981
}
8082
const auto below_ttype = *Maps::getTileType(below);
81-
if ((visible || !config.require_vision) && !DFHack::isOpenTerrain(below_ttype) && !DFHack::isFloorTerrain(below_ttype)) {
83+
if (!DFHack::isOpenTerrain(below_ttype) && !DFHack::isFloorTerrain(below_ttype)) {
8284
// skipping because not floor or open space below while visible or not requiring vision
8385
continue;
8486
}
8587
// open space below /or floor
86-
DEBUG(manager).print("analysis: cave-in condition found\n");
88+
TRACE(manager).print("analysis: cave-in condition found\n");
8789
auto access = count_accessibility(miner_pos, pos);
8890
// if any
8991
cavein_possible = config.riskaverse;
9092
cavein_candidates.emplace(pos, access);
9193
least_access = std::min(access, least_access);
9294
}
93-
DEBUG(manager).print("cavein possible(%d)\n"
94-
"%zu candidates\n"
95-
"least access %d\n", cavein_possible, cavein_candidates.size(), least_access);
95+
DEBUG(manager).print("cavein possible({})\n"
96+
"{} candidates\n"
97+
"least access {}\n", cavein_possible, cavein_candidates.size(), least_access);
9698
}
9799
for (auto &pos: group) {
98100
// if no cave-in is possible [or we don't check for], we'll just execute normally and move on
99101
if likely(marker_mode || !config.riskaverse || !cavein_possible) {
100102
TRACE(manager).print("cave-in evaluated false\n");
101-
d_assert(manage_one(pos, true, marker_mode), "manage_one() failed. L122");
103+
d_assert(manage_one(pos, true, marker_mode), "manage_one() failed. L101");
102104
continue;
103105
}
104106
// marker_mode = false
@@ -107,7 +109,7 @@ void ChannelManager::manage_group(const std::set<df::coord> &group, bool use_mm_
107109
//const static uint8_t OFFSET = 2; //value has been tweaked to avoid cave-ins whilst activating as many designations as possible
108110
if (!cavein_candidates.contains(pos)) {
109111
// not a cavein candidate
110-
d_assert(manage_one(pos, true, marker_mode), "manage_one() failed. L131");
112+
d_assert(manage_one(pos, true, marker_mode), "manage_one() failed. L110");
111113
continue;
112114
}
113115
// if (cavein_candidates[pos] > least_access+OFFSET) {
@@ -129,7 +131,7 @@ void ChannelManager::manage_group(const std::set<df::coord> &group, bool use_mm_
129131
// evT->priority[Coord(local)] = v;
130132
// }
131133
// }
132-
d_assert(manage_one(pos, true, marker_mode), "manage_one() failed. L154");
134+
d_assert(manage_one(pos, true, marker_mode), "manage_one() failed. L132");
133135
}
134136
}
135137

@@ -171,8 +173,8 @@ bool ChannelManager::manage_one(const df::coord &map_pos, bool use_mm_arg_value,
171173
block->designation[Coord(local)].bits.dig = tile_dig_designation::Channel;
172174
block->occupancy[Coord(local)].bits.dig_marked = marker_mode;
173175
//block->occupancy[Coord(local)].bits.
174-
DEBUG(manager).print("manage_one((" COORD "), %d, %d): marker mode: %s\n",
175-
COORDARGS(map_pos), use_mm_arg_value, marker_mode, marker_mode ? "ENABLED" : "DISABLED");
176+
DEBUG(manager).print("manage_one({}, {}, {}): marker mode: {}\n",
177+
map_pos, use_mm_arg_value, marker_mode, marker_mode ? "ENABLED" : "DISABLED");
176178
}
177179
return true;
178180
}

0 commit comments

Comments
 (0)