Adds new event type NEW_UNIT_ACTIVE

develop
Josh Cooper 2022-01-26 09:41:09 -08:00 committed by Myk
parent 20b42145c1
commit ba5710f263
4 changed files with 35 additions and 0 deletions

@ -22,6 +22,7 @@ namespace DFHack {
JOB_INITIATED, JOB_INITIATED,
JOB_STARTED, //has a worker JOB_STARTED, //has a worker
JOB_COMPLETED, JOB_COMPLETED,
NEW_UNIT_ACTIVE,
UNIT_DEATH, UNIT_DEATH,
ITEM_CREATED, ITEM_CREATED,
BUILDING, BUILDING,

@ -149,6 +149,7 @@ static void manageTickEvent(color_ostream& out);
static void manageJobInitiatedEvent(color_ostream& out); static void manageJobInitiatedEvent(color_ostream& out);
static void manageJobStartedEvent(color_ostream& out); static void manageJobStartedEvent(color_ostream& out);
static void manageJobCompletedEvent(color_ostream& out); static void manageJobCompletedEvent(color_ostream& out);
static void manageNewUnitActiveEvent(color_ostream& out);
static void manageUnitDeathEvent(color_ostream& out); static void manageUnitDeathEvent(color_ostream& out);
static void manageItemCreationEvent(color_ostream& out); static void manageItemCreationEvent(color_ostream& out);
static void manageBuildingEvent(color_ostream& out); static void manageBuildingEvent(color_ostream& out);
@ -168,6 +169,7 @@ static const eventManager_t eventManager[] = {
manageJobInitiatedEvent, manageJobInitiatedEvent,
manageJobStartedEvent, manageJobStartedEvent,
manageJobCompletedEvent, manageJobCompletedEvent,
manageNewUnitActiveEvent,
manageUnitDeathEvent, manageUnitDeathEvent,
manageItemCreationEvent, manageItemCreationEvent,
manageBuildingEvent, manageBuildingEvent,
@ -190,6 +192,9 @@ static unordered_set<df::job*> startedJobs;
//job completed //job completed
static unordered_map<int32_t, df::job*> prevJobs; static unordered_map<int32_t, df::job*> prevJobs;
//new unit active
static unordered_set<int32_t> activeUnits;
//unit death //unit death
static unordered_set<int32_t> livingUnits; static unordered_set<int32_t> livingUnits;
@ -595,6 +600,27 @@ static void manageJobCompletedEvent(color_ostream& out) {
} }
} }
static void manageNewUnitActiveEvent(color_ostream& out) {
if (!df::global::world)
return;
unordered_set<int32_t> activeUnits_replacement;
multimap<Plugin*,EventHandler> copy(handlers[EventType::NEW_UNIT_ACTIVE].begin(), handlers[EventType::NEW_UNIT_ACTIVE].end());
int32_t tick = df::global::world->frame_counter;
for (auto unit : world->units.active) {
activeUnits_replacement.emplace(unit);
if(activeUnits.find(unit) == activeUnits.end()){
for (auto &iter : copy) {
auto &handler = iter.second;
if(tick - eventLastTick[handler.eventHandler] >= handler.freq) {
eventLastTick[handler.eventHandler] = tick;
handler.eventHandler(out, (void*) intptr_t(unit->id));
}
}
}
}
activeUnits.swap(activeUnits_replacement);
}
static void manageUnitDeathEvent(color_ostream& out) { static void manageUnitDeathEvent(color_ostream& out) {
if (!df::global::world) if (!df::global::world)
return; return;

@ -98,6 +98,7 @@ DEFINE_LUA_EVENT_NH_1(onBuildingCreatedDestroyed, int32_t);
DEFINE_LUA_EVENT_NH_1(onJobInitiated, df::job*); DEFINE_LUA_EVENT_NH_1(onJobInitiated, df::job*);
DEFINE_LUA_EVENT_NH_1(onJobStarted, df::job*); DEFINE_LUA_EVENT_NH_1(onJobStarted, df::job*);
DEFINE_LUA_EVENT_NH_1(onJobCompleted, df::job*); DEFINE_LUA_EVENT_NH_1(onJobCompleted, df::job*);
DEFINE_LUA_EVENT_NH_1(onNewUnitActive, int32_t);
DEFINE_LUA_EVENT_NH_1(onUnitDeath, int32_t); DEFINE_LUA_EVENT_NH_1(onUnitDeath, int32_t);
DEFINE_LUA_EVENT_NH_1(onItemCreated, int32_t); DEFINE_LUA_EVENT_NH_1(onItemCreated, int32_t);
DEFINE_LUA_EVENT_NH_1(onConstructionCreatedDestroyed, df::construction*); DEFINE_LUA_EVENT_NH_1(onConstructionCreatedDestroyed, df::construction*);
@ -125,6 +126,7 @@ DFHACK_PLUGIN_LUA_EVENTS {
DFHACK_LUA_EVENT(onJobInitiated), DFHACK_LUA_EVENT(onJobInitiated),
DFHACK_LUA_EVENT(onJobStarted), DFHACK_LUA_EVENT(onJobStarted),
DFHACK_LUA_EVENT(onJobCompleted), DFHACK_LUA_EVENT(onJobCompleted),
DFHACK_LUA_EVENT(onNewUnitActive),
DFHACK_LUA_EVENT(onUnitDeath), DFHACK_LUA_EVENT(onUnitDeath),
DFHACK_LUA_EVENT(onItemCreated), DFHACK_LUA_EVENT(onItemCreated),
DFHACK_LUA_EVENT(onSyndrome), DFHACK_LUA_EVENT(onSyndrome),
@ -152,6 +154,11 @@ void ev_mng_jobCompleted(color_ostream& out, void* job)
df::job* ptr=reinterpret_cast<df::job*>(job); df::job* ptr=reinterpret_cast<df::job*>(job);
onJobCompleted(out,ptr); onJobCompleted(out,ptr);
} }
void ev_mng_newUnitActive(color_ostream& out, void* ptr)
{
int32_t myId=(int32_t)(intptr_t)ptr;
onNewUnitActive(out,myId);
}
void ev_mng_unitDeath(color_ostream& out, void* ptr) void ev_mng_unitDeath(color_ostream& out, void* ptr)
{ {
int32_t myId=(int32_t)(intptr_t)ptr; int32_t myId=(int32_t)(intptr_t)ptr;

@ -154,6 +154,7 @@ eventType=invertTable{
"JOB_INITIATED", "JOB_INITIATED",
"JOB_STARTED", "JOB_STARTED",
"JOB_COMPLETED", "JOB_COMPLETED",
"NEW_UNIT_ACTIVE",
"UNIT_DEATH", "UNIT_DEATH",
"ITEM_CREATED", "ITEM_CREATED",
"BUILDING", "BUILDING",