Skip to content

Commit 8512283

Browse files
committed
Finishes re/moving OnUpdate code
1 parent f3d5309 commit 8512283

File tree

2 files changed

+109
-99
lines changed

2 files changed

+109
-99
lines changed

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

Lines changed: 100 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <active-job-manager.h>
22
#include <inlines.h>
3+
#include <on-tick.h>
34
#include <ranges>
45

56
#include <tile-cache.h>
@@ -11,6 +12,7 @@
1112
#include <df/report.h>
1213

1314
namespace CSP {
15+
extern OnTick tick_it_master;;
1416
extern std::unordered_set<df::coord> dignow_queue;
1517
}
1618

@@ -141,23 +143,17 @@ void ActiveJobManager::cleanup() {
141143
}
142144

143145
void ActiveJobManager::on_update(color_ostream &out) {
146+
INFO(jobs).print("onUpdate()");
144147
int32_t tick = df::global::world->frame_counter;
145148
// clean up stale df::job*
146149
if ((config.monitoring || config.resurrect) && tick - last_tick >= 1) {
147150
last_tick = tick;
148151
cleanup();
149152
}
150-
// cancel jobs in the cancel queue
151-
for (auto pos : cancel_queue) {
152-
cancel_job(pos);
153-
if (!ChannelManager::Get().manage_one(pos, true, true)) {
154-
DEBUG(jobs).print(" <- JobStartedEvent(): failed to cancel a job and marker the designation.");
155-
}
156-
}
157-
cancel_queue.clear();
153+
158154

159155
// monitoring activity
160-
if (config.monitoring && tick - last_monitor_tick >= config.monitor_freq) {
156+
if (config.monitoring && tick - last_monitor_tick >= 666){//config.monitor_freq) {
161157
last_monitor_tick = tick;
162158
TRACE(monitor).print("OnUpdate() monitoring now\n");
163159

@@ -166,70 +162,39 @@ void ActiveJobManager::on_update(color_ostream &out) {
166162
if unlikely(!ajob.worker) continue;
167163
if unlikely(!Units::isAlive(ajob.worker)) continue;
168164
if unlikely(!Maps::isValidTilePos(ajob.pos)) continue;
165+
if likely(ajob.worker->pos != ajob.pos) continue;
166+
if likely(is_safe_fall(ajob.pos)) continue;
169167

170-
// check for fall safety
171-
if (ajob.worker->pos == ajob.pos && !is_safe_fall(ajob.pos)) {
172-
// unsafe
173-
WARN(monitor).print(" -> unsafe job\n");
174-
Job::removeWorker(ajob.job);
175-
176-
// decide to insta-dig, marker mode, or break a few eggs to get it done before unbreaking them
177-
if (config.insta_dig) {
178-
// delete the job
179-
Job::removeJob(ajob.job);
180-
// queue digging the job instantly
181-
CSP::dignow_queue.emplace(ajob.pos);
182-
DEBUG(monitor).print(" -> insta-dig\n");
183-
} else if (!config.resurrect) {
184-
// set marker mode
185-
Maps::getTileOccupancy(ajob.pos)->bits.dig_marked = true;
186-
187-
using df_bsedp = df::block_square_event_designation_priorityst;
188-
// prevent algorithm from re-enabling designation
189-
for (auto &blk_evt: Maps::getBlock(ajob.pos)->block_events) {
190-
if (auto bsedp = virtual_cast<df_bsedp>(blk_evt)) {
191-
df::coord local(ajob.pos);
192-
local.x = local.x % 16;
193-
local.y = local.y % 16;
194-
bsedp->priority[Coord(local)] = config.ignore_threshold * 1000 + 1;
195-
break;
196-
}
168+
// unsafe
169+
WARN(monitor).print(" -> unsafe job\n");
170+
Job::removeWorker(ajob.job);
171+
172+
if (!config.resurrect) {
173+
// set marker mode
174+
Maps::getTileOccupancy(ajob.pos)->bits.dig_marked = true;
175+
176+
using df_bsedp = df::block_square_event_designation_priorityst;
177+
// prevent algorithm from re-enabling designation
178+
for (auto &blk_evt: Maps::getBlock(ajob.pos)->block_events) {
179+
if (auto bsedp = virtual_cast<df_bsedp>(blk_evt)) {
180+
df::coord local(ajob.pos);
181+
local.x = local.x % 16;
182+
local.y = local.y % 16;
183+
bsedp->priority[Coord(local)] = config.ignore_threshold * 1000 + 1;
184+
break;
197185
}
198-
DEBUG(monitor).print(" -> set marker mode\n");
199186
}
187+
DEBUG(monitor).print(" -> set marker mode\n");
200188
}
201189
}
202190
TRACE(monitor).print("OnUpdate() monitoring done\n");
203191
}
204-
205-
// Resurrect Dead Workers
206-
if (config.resurrect && tick - last_resurrect_tick >= 1) {
207-
last_resurrect_tick = tick;
208-
for (auto [id, aworker] : active_workers) {
209-
if (Units::isAlive(aworker.worker)) {
210-
continue;
211-
}
212-
resurrect(out, aworker.id);
213-
df::coord lowest = simulate_fall(aworker.last_safe_pos);
214-
Units::teleport(aworker.worker, lowest);
215-
}
216-
// resurrect any dead endangered units
217-
for (auto unit : df::global::world->units.all) {
218-
if (!endangered_units.contains(unit->id) || !safe_locations.contains(unit->id)) {
219-
continue;
220-
}
221-
if (Units::isAlive(unit)) {
222-
continue;
223-
}
224-
resurrect(out, unit->id);
225-
df::coord lowest = simulate_fall(safe_locations[unit->id]);
226-
Units::teleport(unit, lowest);
227-
}
228-
}
229192
}
230193

194+
extern DFHack::EventManager::EventHandler resurrectHandler;
195+
231196
void ActiveJobManager::on_job_start(df::job* job) {
232-
if (!ChannelManager::Get().exists(job->pos)) {
197+
if (!ChannelManager::Get().contains(job->pos)) {
233198
ChannelManager::Get().build_groups(false);
234199
}
235200
df::unit* worker = Job::getWorker(job);
@@ -246,6 +211,11 @@ void ActiveJobManager::on_job_start(df::job* job) {
246211
active_jobs.emplace(ajob.id,ajob);
247212
active_workers.emplace(ajob.id, aworker);
248213
safe_locations[aworker.id] = aworker.last_safe_pos;
214+
if (config.resurrect && !CSP::tick_it_master.resurrect_queued) {
215+
CSP::tick_it_master.resurrect_queued = true;
216+
// todo: verify this means NEXT tick.. or just use 0
217+
EventManager::registerTick(resurrectHandler,1);
218+
}
249219
}
250220
// cavein prevention is the rest of the function
251221
if (!config.riskaverse) {
@@ -352,13 +322,76 @@ void ActiveJobManager::on_report_event(df::report* report) {
352322
DEBUG(plugin).print(" [id %d] is/was an endangereed worker, we'll extend tracking them too.\n", aworker.id);
353323
}
354324
}
325+
if (!CSP::tick_it_master.resurrect_queued) {
326+
CSP::tick_it_master.resurrect_queued = true;
327+
// todo: verify this means NEXT tick.. or just use 0
328+
EventManager::registerTick(resurrectHandler,1);
329+
}
355330
}
356331
break;
357332
default:
358333
break;
359334
}
360335
}
361336

337+
extern DFHack::EventManager::EventHandler cancelHandler;
338+
339+
void ActiveJobManager::cancel(df::coord site) {
340+
if (!CSP::tick_it_master.cancel_queued) {
341+
CSP::tick_it_master.cancel_queued = true;
342+
// todo: verify this means NEXT tick.. or just use 0
343+
EventManager::registerTick(cancelHandler,1);
344+
}
345+
cancel_queue.emplace(site);
346+
}
347+
348+
void ActiveJobManager::handle_cancellation() {
349+
// cancel jobs in the cancel queue
350+
ChannelJobs jobs;
351+
jobs.load_channel_jobs();
352+
for (auto pos : cancel_queue) {
353+
INFO(jobs).print("Canceling job: " COORD, COORDARGS(pos));
354+
if (auto job_ptr = jobs.find_job(pos); job_ptr) {
355+
if unlikely(job_ptr->id < 0) {
356+
Job::removePostings(job_ptr, true);
357+
revert_designation(pos, job_ptr->job_type);
358+
continue;
359+
}
360+
Job::removeWorker(job_ptr);
361+
Job::removePostings(job_ptr, true);
362+
Job::removeJob(job_ptr);
363+
revert_designation(pos, job_ptr->job_type);
364+
}
365+
}
366+
cancel_queue.clear();
367+
}
368+
369+
void ActiveJobManager::handle_resurrect(color_ostream &out) {
370+
if (!config.resurrect) {
371+
return;
372+
}
373+
for (auto [id, aworker] : active_workers) {
374+
if (Units::isAlive(aworker.worker)) {
375+
continue;
376+
}
377+
resurrect(out, aworker.id);
378+
df::coord lowest = simulate_fall(aworker.last_safe_pos);
379+
Units::teleport(aworker.worker, lowest);
380+
}
381+
// resurrect any dead endangered units
382+
for (auto unit : df::global::world->units.all) {
383+
if (!endangered_units.contains(unit->id) || !safe_locations.contains(unit->id)) {
384+
continue;
385+
}
386+
if (Units::isAlive(unit)) {
387+
continue;
388+
}
389+
resurrect(out, unit->id);
390+
df::coord lowest = simulate_fall(safe_locations[unit->id]);
391+
Units::teleport(unit, lowest);
392+
}
393+
}
394+
362395
void ActiveJobManager::clear() {
363396
safe_locations.clear();
364397
endangered_units.clear();
@@ -368,3 +401,7 @@ void ActiveJobManager::clear() {
368401
cancel_queue.clear();
369402
}
370403

404+
bool ActiveJobManager::needs_resurrect_queued() {
405+
return !active_workers.empty() || !endangered_units.empty();
406+
}
407+

plugins/channel-safely/channel-safely-plugin.cpp

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ This skeletal logic has not been kept up-to-date since ~v0.5
7373

7474
#include <ranges>
7575
#include <cinttypes>
76+
#include <on-tick.h>
7677
#include <unordered_map>
7778
#include <unordered_set>
7879

@@ -117,16 +118,13 @@ enum SettingConfigData {
117118
};
118119

119120
// dig-now.cpp
121+
extern void refresh(DFHack::color_ostream&, void*);
120122

121123
namespace CSP {
124+
OnTick tick_it_master;
122125
ActiveJobManager active_job_manager;
123126
std::unordered_set<df::coord> dignow_queue;
124127

125-
static int32_t last_tick = 0;
126-
static int32_t last_monitor_tick = 0;
127-
static int32_t last_refresh_tick = 0;
128-
static int32_t last_resurrect_tick = 0;
129-
130128
void ClearData() {
131129
ChannelManager::Get().destroy_groups();
132130
dignow_queue.clear();
@@ -218,30 +216,6 @@ namespace CSP {
218216
}
219217
active_job_manager.on_report_event(report);
220218
}
221-
222-
void OnUpdate(color_ostream &out) {
223-
if (World::ReadPauseState())
224-
return;
225-
226-
active_job_manager.on_update(out);
227-
int32_t tick = world->frame_counter;
228-
// Refreshing the group data with full scanning
229-
if (tick - last_refresh_tick >= config.refresh_freq) {
230-
last_refresh_tick = tick;
231-
TRACE(monitor).print("OnUpdate() refreshing now\n");
232-
if (config.insta_dig) {
233-
TRACE(monitor).print(" -> evaluate dignow queue\n");
234-
for (auto iter = dignow_queue.begin(); iter != dignow_queue.end();) {
235-
auto map_pos = *iter;
236-
dig_now(out, map_pos); // teleports units to the bottom of a simulated fall
237-
ChannelManager::Get().mark_done(map_pos);
238-
iter = dignow_queue.erase(iter);
239-
}
240-
}
241-
UnpauseEvent(false);
242-
TRACE(monitor).print("OnUpdate() refresh done\n");
243-
}
244-
}
245219
}
246220

247221
command_result channel_safely(color_ostream &out, std::vector<std::string> &parameters);
@@ -250,7 +224,7 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginC
250224
commands.push_back(PluginCommand("channel-safely",
251225
"Automatically manage channel designations.",
252226
channel_safely,
253-
false));
227+
false));;
254228
return CR_OK;
255229
}
256230

@@ -260,10 +234,6 @@ DFhackCExport command_result plugin_shutdown(color_ostream &out) {
260234
}
261235

262236
DFhackCExport command_result plugin_load_site_data (color_ostream &out) {
263-
CSP::last_tick = 0;
264-
CSP::last_monitor_tick = 0;
265-
CSP::last_refresh_tick = 0;
266-
CSP::last_resurrect_tick = 0;
267237

268238
CSP::LoadSettings();
269239
if (enabled) {
@@ -281,17 +251,21 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
281251

282252
if (enable && !enabled) {
283253
// register events to check jobs / update tracking
254+
//EM::EventHandler updateHandler(plugin_self,CSP::OnUpdate, 0);
284255
EM::EventHandler jobStartHandler(plugin_self,CSP::JobStartedEvent, 0);
285256
EM::EventHandler jobCompletionHandler(plugin_self,CSP::JobCompletedEvent, 0);
286257
EM::EventHandler reportHandler(plugin_self,CSP::NewReportEvent, 0);
258+
//EM::registerTick(updateHandler,1);
259+
//EM::registerListener(EventType::TICK, updateHandler);
287260
EM::registerListener(EventType::REPORT, reportHandler);
288261
EM::registerListener(EventType::JOB_STARTED, jobStartHandler);
289262
EM::registerListener(EventType::JOB_COMPLETED, jobCompletionHandler);
290263
// manage designations to start off (first time building groups [very important])
291264
out.print("channel-safely: enabled!\n");
292-
CSP::UnpauseEvent(true);
265+
refresh(out, nullptr); // scans and queues the next tick event
293266
} else if (!enable) {
294267
// don't need the groups if the plugin isn't going to be enabled
268+
CSP::ClearData();
295269
EM::unregisterAll(plugin_self);
296270
out.print("channel-safely: disabled!\n");
297271
}
@@ -326,7 +300,6 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
326300
}
327301

328302
DFhackCExport command_result plugin_onupdate(color_ostream &out, state_change_event event) {
329-
CSP::OnUpdate(out);
330303
return DFHack::CR_OK;
331304
}
332305

0 commit comments

Comments
 (0)