| 360 | } |
| 361 | |
| 362 | void OnUpdate(color_ostream &out) { |
| 363 | if (World::ReadPauseState()) |
| 364 | return; |
| 365 | |
| 366 | int32_t tick = world->frame_counter; |
| 367 | |
| 368 | // Refreshing the group data with full scanning |
| 369 | if (tick - last_refresh_tick >= config.refresh_freq) { |
| 370 | last_refresh_tick = tick; |
| 371 | TRACE(monitor).print("OnUpdate() refreshing now\n"); |
| 372 | if (config.insta_dig) { |
| 373 | TRACE(monitor).print(" -> evaluate dignow queue\n"); |
| 374 | for (auto iter = dignow_queue.begin(); iter != dignow_queue.end();) { |
| 375 | auto map_pos = *iter; |
| 376 | dig_now(out, map_pos); // teleports units to the bottom of a simulated fall |
| 377 | ChannelManager::Get().mark_done(map_pos); |
| 378 | iter = dignow_queue.erase(iter); |
| 379 | } |
| 380 | } |
| 381 | UnpauseEvent(false); |
| 382 | TRACE(monitor).print("OnUpdate() refresh done\n"); |
| 383 | } |
| 384 | |
| 385 | // Clean up stale df::job* |
| 386 | if ((config.monitoring || config.resurrect) && tick - last_tick >= 1) { |
| 387 | last_tick = tick; |
| 388 | // make note of valid jobs |
| 389 | std::unordered_map<int32_t, df::job*> valid_jobs; |
| 390 | for (df::job_list_link* link = &world->jobs.list; link != nullptr; link = link->next) { |
| 391 | df::job* job = link->item; |
| 392 | if (job && active_jobs.count(job->id)) { |
| 393 | valid_jobs.emplace(job->id, job); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // erase the active jobs that aren't valid |
| 398 | std::unordered_set<df::job*> erase; |
| 399 | map_value_difference(active_jobs, valid_jobs, erase); |
| 400 | for (auto j : erase) { |
| 401 | auto id = job_id_map[j]; |
| 402 | job_id_map.erase(j); |
| 403 | active_jobs.erase(id); |
| 404 | active_workers.erase(id); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // Monitoring Active and Resurrecting Dead |
| 409 | if (config.monitoring && tick - last_monitor_tick >= config.monitor_freq) { |
| 410 | last_monitor_tick = tick; |
| 411 | TRACE(monitor).print("OnUpdate() monitoring now\n"); |
| 412 | |
| 413 | // iterate active jobs |
| 414 | for (auto pair: active_jobs) { |
| 415 | df::job* job = pair.second; |
| 416 | df::unit* unit = active_workers[job->id]; |
| 417 | if (!unit) continue; |
| 418 | if (!Maps::isValidTilePos(job->pos)) continue; |
| 419 | TRACE(monitor).print(" -> check for job in tracking\n"); |
no test coverage detected