From 24255d3195a481f060ea30aae149dbb78a84b127 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Fri, 6 Aug 2021 06:02:23 -0700 Subject: [PATCH] Adds event type JOB_STARTED --- library/include/modules/EventManager.h | 1 + library/modules/EventManager.cpp | 33 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/library/include/modules/EventManager.h b/library/include/modules/EventManager.h index 185497025..6f9c69547 100644 --- a/library/include/modules/EventManager.h +++ b/library/include/modules/EventManager.h @@ -20,6 +20,7 @@ namespace DFHack { enum EventType { TICK, JOB_INITIATED, + JOB_STARTED, //has a worker JOB_COMPLETED, UNIT_DEATH, ITEM_CREATED, diff --git a/library/modules/EventManager.cpp b/library/modules/EventManager.cpp index 3dbe866f5..193de787d 100644 --- a/library/modules/EventManager.cpp +++ b/library/modules/EventManager.cpp @@ -144,6 +144,7 @@ void DFHack::EventManager::unregisterAll(Plugin* plugin) { static void manageTickEvent(color_ostream& out); static void manageJobInitiatedEvent(color_ostream& out); +static void manageJobStartedEvent(color_ostream& out); static void manageJobCompletedEvent(color_ostream& out); static void manageUnitDeathEvent(color_ostream& out); static void manageItemCreationEvent(color_ostream& out); @@ -162,6 +163,7 @@ typedef void (*eventManager_t)(color_ostream&); static const eventManager_t eventManager[] = { manageTickEvent, manageJobInitiatedEvent, + manageJobStartedEvent, manageJobCompletedEvent, manageUnitDeathEvent, manageItemCreationEvent, @@ -179,6 +181,9 @@ static const eventManager_t eventManager[] = { //job initiated static int32_t lastJobId = -1; +//job started +static unordered_set startedJobs; + //job completed static unordered_map prevJobs; @@ -187,7 +192,6 @@ static unordered_set livingUnits; //item creation static int32_t nextItem; - //building static int32_t nextBuilding; static unordered_set buildings; @@ -420,6 +424,32 @@ static void manageJobInitiatedEvent(color_ostream& out) { lastJobId = *df::global::job_next_id - 1; } +static void manageJobStartedEvent(color_ostream& out){ + if (!df::global::world) + return; + + multimap copy(handlers[EventType::JOB_STARTED].begin(), handlers[EventType::JOB_STARTED].end()); + int32_t tick = df::global::world->frame_counter; + + std::vector newly_started_jobs; + + for(auto &iter : copy) { //iterate handlers + auto &handler = iter.second; + // build a list of newly started jobs + for ( df::job_list_link* link = &df::global::world->jobs.list; link != NULL; link = link->next ) { + df::job* job = link->item; + if(Job::getWorker(job) && startedJobs.emplace(job).second){ + newly_started_jobs.push_back(job); + } + } + if (tick - eventLastTick[handler.eventHandler] >= handler.freq) { + eventLastTick[handler.eventHandler] = tick; + for(auto job : newly_started_jobs){ + handler.eventHandler(out, (void*)job); + } + } + } +} //helper function for manageJobCompletedEvent //static int32_t getWorkerID(df::job* job) { // auto ref = findRef(job->general_refs, general_ref_type::UNIT_WORKER); @@ -542,6 +572,7 @@ static void manageJobCompletedEvent(color_ostream& out) { //erase old jobs, copy over possibly altered jobs for (auto job_iter = prevJobs.begin(); job_iter != prevJobs.end(); ++job_iter ) { Job::deleteJobStruct((*job_iter).second, true); + startedJobs.erase(job_iter->second); } prevJobs.clear();