3131#include " df/report.h"
3232#include " df/plotinfost.h"
3333#include " df/unit.h"
34+ #include " df/unit_action.h"
3435#include " df/unit_flags1.h"
3536#include " df/unit_inventory_item.h"
3637#include " df/unit_report_type.h"
@@ -224,12 +225,28 @@ static std::vector<int32_t> startedJobs;
224225struct JobCompleteData {
225226 int32_t id;
226227 int32_t completion_timer;
227- uint32_t flags_bits_repeat ;
228+ df::job_flags flags ;
228229};
229230
231+ /*
232+ * Some jobs are driven by the worker's Job unit action timer rather than by
233+ * job.completion_timer, which stays -1 for the job's entire lifetime (e.g.
234+ * HarvestPlants). DF deletes these jobs in the same tick the action expires,
235+ * so they are never observed with completion_timer == 0. Snapshot the worker's
236+ * action timer for claimed jobs that are in their work phase but whose
237+ * completion_timer isn't counting down; when such a job disappears, a timer
238+ * that was within reach of expiring during the elapsed ticks indicates a
239+ * completion rather than a cancellation.
240+ */
241+ struct ActionDrivenJobData {
242+ int32_t unit_id;
243+ int32_t unit_index; // cache for Units::get_cached_unit_by_global_id
244+ int32_t action_timer; // -1 = worker's Job action not observed yet
245+ };
230246
231247static std::unordered_map<int32_t , Job::JobUniquePtr> seenJobs;
232248static std::vector<JobCompleteData> prevJobs;
249+ static std::unordered_map<int32_t , ActionDrivenJobData> actionDrivenJobs;
233250
234251// active units
235252static unordered_set<int32_t > activeUnits;
@@ -312,6 +329,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
312329 startedJobs.clear ();
313330 seenJobs.clear ();
314331 prevJobs.clear ();
332+ actionDrivenJobs.clear ();
315333 tickQueue.clear ();
316334 livingUnits.clear ();
317335 buildings.clear ();
@@ -560,10 +578,34 @@ static void manageJobCompletedEvent(color_ostream& out) {
560578 || (seenJob->general_refs .size () != job.general_refs .size ())) {
561579 seenIt->second = Job::JobUniquePtr (Job::cloneJobStruct (&job, true ));
562580 }
563- } else if (job.completion_timer != -1 ) {
581+ } else if (job.completion_timer != -1 || job. flags . bits . working ) {
564582 // Restrict additions to seenJobs to jobs that we know have started.
565583 seenJobs.emplace (job.id , Job::JobUniquePtr (Job::cloneJobStruct (&job, true )));
566584 }
585+
586+ if (job.completion_timer == -1 && job.flags .bits .working ) {
587+ auto ait = actionDrivenJobs.find (job.id );
588+ if (ait == actionDrivenJobs.end ()) {
589+ if (df::unit* worker = Job::getWorker (&job))
590+ ait = actionDrivenJobs.emplace (job.id ,
591+ ActionDrivenJobData{worker->id , -1 , -1 }).first ;
592+ }
593+ if (ait != actionDrivenJobs.end ()) {
594+ auto & data = ait->second ;
595+ df::unit* unit = Units::get_cached_unit_by_global_id (data.unit_id , data.unit_index );
596+ if (!unit || unit->job .current_job != &job) {
597+ // worker lost or swapped; re-register on a later poll
598+ actionDrivenJobs.erase (ait);
599+ } else {
600+ for (auto action : unit->actions ) {
601+ if (action->type == unit_action_type::Job) {
602+ data.action_timer = action->data .job .timer ;
603+ break ;
604+ }
605+ }
606+ }
607+ }
608+ }
567609 /*
568610 * We still need to push back all jobs to maintain the invariant of the
569611 * algorithm used with prevJobs and nowJobs.
@@ -590,7 +632,7 @@ static void manageJobCompletedEvent(color_ostream& out) {
590632 * 1 in nowJobs is less than the smallest ID in prevJobs,
591633 * which breaks the algorithm.
592634 */
593- nowJobs.emplace_back (job.id , job.completion_timer , ( bool ) job.flags . bits . repeat );
635+ nowJobs.emplace_back (job.id , job.completion_timer , job.flags );
594636 }
595637
596638 // Do we want this check?
@@ -663,6 +705,7 @@ static void manageJobCompletedEvent(color_ostream& out) {
663705
664706 auto prevIt = prevJobs.begin ();
665707 auto nowIt = nowJobs.begin ();
708+ int32_t tick = df::global::world->frame_counter ;
666709 /*
667710 * Iterate through two ordered sets, prevJobs and nowJobs, where job IDs in nowJobs are invariably
668711 * greater than or equal to job IDs in prevJobs. The algorithm maintains the invariant that for each
@@ -674,7 +717,27 @@ static void manageJobCompletedEvent(color_ostream& out) {
674717 auto & prevJob = *prevIt;
675718 if (nowIt == nowJobs.end () || prevJob.id != nowIt->id ) { // job ID is in prevJobs. ID does not exist in nowJobs.
676719 // recently finished or cancelled job
677- if (!prevJob.flags_bits_repeat && prevJob.completion_timer == 0 ) {
720+ bool completed = prevJob.completion_timer == 0 ;
721+ if (!completed) {
722+ /*
723+ * Jobs whose work is tracked by the worker's Job unit action
724+ * are deleted the same tick they complete, before
725+ * completion_timer ever reaches 0. If the last observed action
726+ * timer was close enough to expiring that it could have hit 0
727+ * during the elapsed ticks, count it as a completion.
728+ */
729+ auto ait = actionDrivenJobs.find (prevJob.id );
730+ if (ait != actionDrivenJobs.end ()
731+ && prevJob.flags .bits .working
732+ && !prevJob.flags .bits .suspend
733+ && !prevJob.flags .bits .item_lost
734+ && ait->second .action_timer >= 0
735+ && ait->second .action_timer <= tick - eventLastTick[EventType::JOB_COMPLETED ]) {
736+ completed = true ;
737+ }
738+ }
739+ actionDrivenJobs.erase (prevJob.id );
740+ if (completed && !prevJob.flags .bits .repeat ) {
678741 // It should be in seenJobs.
679742 auto seenIt = seenJobs.find (prevJob.id );
680743 if (seenIt != seenJobs.end ()) {
@@ -688,7 +751,7 @@ static void manageJobCompletedEvent(color_ostream& out) {
688751 }
689752 } else { // prevIt job ID and nowIt job ID are equal.
690753 // could have just finished if it's a repeat job
691- if (prevJob.flags_bits_repeat && prevJob.completion_timer == 0
754+ if (prevJob.flags . bits . repeat && prevJob.completion_timer == 0
692755 && nowIt->completion_timer == -1 ) {
693756 // It should be in seenJobs.
694757 auto seenIt = seenJobs.find (prevJob.id );
0 commit comments