@@ -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
254252void 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}
0 commit comments