Implements plugin: channel-safely v0.5.1

develop
Josh Cooper 2022-11-06 16:18:39 -08:00
parent e5dbaac85d
commit 9db0d809fa
2 changed files with 55 additions and 53 deletions

@ -148,68 +148,70 @@ namespace CSP {
void JobStartedEvent(color_ostream &out, void* p) { void JobStartedEvent(color_ostream &out, void* p) {
if (enabled && World::isFortressMode() && Maps::IsValid()) { if (enabled && World::isFortressMode() && Maps::IsValid()) {
INFO(monitor).print("JobStartedEvent()\n"); if (config.monitor_active) {
auto job = (df::job*) p; INFO(monitor).print("JobStartedEvent()\n");
// validate job type auto job = (df::job*) p;
if (is_channel_job(job)) { // validate job type
DEBUG(monitor).print(" valid channel job:\n"); if (is_channel_job(job)) {
df::unit* worker = Job::getWorker(job); DEBUG(monitor).print(" valid channel job:\n");
// there is a valid worker (living citizen) on the job? right.. df::unit* worker = Job::getWorker(job);
if (worker && Units::isAlive(worker) && Units::isCitizen(worker)) { // there is a valid worker (living citizen) on the job? right..
DEBUG(monitor).print(" valid worker:\n"); if (worker && Units::isAlive(worker) && Units::isCitizen(worker)) {
df::coord local(job->pos); DEBUG(monitor).print(" valid worker:\n");
local.x = local.x % 16; df::coord local(job->pos);
local.y = local.y % 16; local.x = local.x % 16;
// check pathing exists to job local.y = local.y % 16;
if (can_reach_designation(worker->pos, job->pos)) { // check pathing exists to job
DEBUG(monitor).print(" can path from (" COORD ") to (" COORD ")\n", if (can_reach_designation(worker->pos, job->pos)) {
COORDARGS(worker->pos), COORDARGS(job->pos)); DEBUG(monitor).print(" can path from (" COORD ") to (" COORD ")\n",
// track workers on jobs COORDARGS(worker->pos), COORDARGS(job->pos));
active_workers.emplace(job->id, Units::findIndexById(Job::getWorker(job)->id)); // track workers on jobs
// set tile to restricted active_workers.emplace(job->id, Units::findIndexById(Job::getWorker(job)->id));
TRACE(monitor).print(" setting job tile to restricted\n"); // set tile to restricted
Maps::getTileDesignation(job->pos)->bits.traffic = df::tile_traffic::Restricted; TRACE(monitor).print(" setting job tile to restricted\n");
} else { Maps::getTileDesignation(job->pos)->bits.traffic = df::tile_traffic::Restricted;
DEBUG(monitor).print(" no path exists to job:\n"); } else {
// if we can't get there, then we should remove the worker and cancel the job (restore tile designation) DEBUG(monitor).print(" no path exists to job:\n");
Job::removeWorker(job); // if we can't get there, then we should remove the worker and cancel the job (restore tile designation)
cancel_job(job); Job::removeWorker(job);
if (!config.insta_dig) { cancel_job(job);
TRACE(monitor).print(" setting marker mode for (" COORD ")\n", COORDARGS(job->pos)); if (!config.insta_dig) {
// set to marker mode TRACE(monitor).print(" setting marker mode for (" COORD ")\n", COORDARGS(job->pos));
auto occupancy = Maps::getTileOccupancy(job->pos); // set to marker mode
if (!occupancy) { auto occupancy = Maps::getTileOccupancy(job->pos);
WARN(monitor).print(" <X> Could not acquire tile occupancy*\n"); if (!occupancy) {
return; WARN(monitor).print(" <X> Could not acquire tile occupancy*\n");
} return;
occupancy->bits.dig_marked = true; }
// prevent algorithm from re-enabling designation occupancy->bits.dig_marked = true;
df::map_block* block = Maps::getTileBlock(job->pos); // prevent algorithm from re-enabling designation
if (!block) { df::map_block* block = Maps::getTileBlock(job->pos);
WARN(monitor).print(" <X> Could not acquire block*\n"); if (!block) {
return; WARN(monitor).print(" <X> Could not acquire block*\n");
} return;
for (auto &be: block->block_events) { ;
if (auto bsedp = virtual_cast<df::block_square_event_designation_priorityst>(be)) {
TRACE(monitor).print(" re-setting priority\n");
bsedp->priority[Coord(local)] = config.ignore_threshold * 1000 + 1;
} }
for (auto &be: block->block_events) { ;
if (auto bsedp = virtual_cast<df::block_square_event_designation_priorityst>(be)) {
TRACE(monitor).print(" re-setting priority\n");
bsedp->priority[Coord(local)] = config.ignore_threshold * 1000 + 1;
}
}
} else {
TRACE(monitor).print(" deleting job, and queuing insta-dig)\n");
// queue digging the job instantly
dignow_queue.emplace(job->pos);
} }
} else {
TRACE(monitor).print(" deleting job, and queuing insta-dig)\n");
// queue digging the job instantly
dignow_queue.emplace(job->pos);
} }
} }
} }
INFO(monitor).print(" <- JobStartedEvent() exits normally\n");
} }
INFO(monitor).print(" <- JobStartedEvent() exits normally\n");
} }
} }
void JobCompletedEvent(color_ostream &out, void* job_ptr) { void JobCompletedEvent(color_ostream &out, void* job_ptr) {
if (enabled && World::isFortressMode() && Maps::IsValid()) { if (enabled && World::isFortressMode() && Maps::IsValid()) {
INFO(monitor).print("JobCompletedEvent()\n"); INFO(jobs).print("JobCompletedEvent()\n");
auto job = (df::job*) job_ptr; auto job = (df::job*) job_ptr;
// we only care if the job is a channeling one // we only care if the job is a channeling one
if (ChannelManager::Get().groups.count(job->pos)) { if (ChannelManager::Get().groups.count(job->pos)) {
@ -226,7 +228,7 @@ namespace CSP {
// the job can be considered done // the job can be considered done
df::coord below(job->pos); df::coord below(job->pos);
below.z--; below.z--;
WARN(monitor).print(" -> Marking tile done and managing the group below.\n"); WARN(jobs).print(" -> Marking tile done and managing the group below.\n");
// mark done and manage below // mark done and manage below
block->designation[Coord(local)].bits.traffic = df::tile_traffic::Normal; block->designation[Coord(local)].bits.traffic = df::tile_traffic::Normal;
ChannelManager::Get().mark_done(job->pos); ChannelManager::Get().mark_done(job->pos);

@ -97,7 +97,7 @@ inline bool can_reach_designation(const df::coord &start, const df::coord &end)
df::coord neighbours[8]; df::coord neighbours[8];
get_neighbours(end, neighbours); get_neighbours(end, neighbours);
for (auto &pos : neighbours) { for (auto &pos : neighbours) {
if (Maps::canWalkBetween(start, pos)) { if (Maps::isValidTilePos(pos) && Maps::canWalkBetween(start, pos)) {
return true; return true;
} }
} }