EventManager: added unit death event.

develop
expwnent 2012-12-15 14:40:11 -05:00
parent 155a4d044c
commit b0314755e0
3 changed files with 39 additions and 8 deletions

@ -14,14 +14,11 @@ namespace DFHack {
enum EventType {
NONE,
TICK,
TICK_TILE,
JOB_INITIATED,
JOB_COMPLETED,
LIFE,
CREATURE,
ITEM,
TILE,
EVENT_MAX=TILE
UNIT_DEATH,
//ITEM_CREATED,
EVENT_MAX=UNIT_DEATH
};
}

@ -6,6 +6,7 @@
#include "df/job.h"
#include "df/global_objects.h"
#include "df/job_list_link.h"
#include "df/unit.h"
#include "df/world.h"
//#include <list>
@ -85,10 +86,12 @@ void DFHack::EventManager::unregisterAll(Plugin* plugin) {
static void manageTickEvent(color_ostream& out);
static void manageJobInitiatedEvent(color_ostream& out);
static void manageJobCompletedEvent(color_ostream& out);
static void manageUnitDeathEvent(color_ostream& out);
uint32_t lastTick = 0;
int32_t lastJobId = -1;
static uint32_t lastTick = 0;
static int32_t lastJobId = -1;
static map<int32_t, df::job*> prevJobs;
static set<int32_t> livingUnits;
void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) {
if ( event == DFHack::SC_MAP_UNLOADED ) {
@ -99,6 +102,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
}
prevJobs.clear();
tickQueue.clear();
livingUnits.clear();
} else if ( event == DFHack::SC_MAP_LOADED ) {
uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear
+ DFHack::World::ReadCurrentTick();
@ -125,6 +129,7 @@ void DFHack::EventManager::manageEvents(color_ostream& out) {
manageTickEvent(out);
manageJobInitiatedEvent(out);
manageJobCompletedEvent(out);
manageUnitDeathEvent(out);
return;
}
@ -214,3 +219,25 @@ static void manageJobCompletedEvent(color_ostream& out) {
}*/
}
static void manageUnitDeathEvent(color_ostream& out) {
if ( handlers[EventType::UNIT_DEATH].empty() ) {
return;
}
for ( size_t a = 0; a < df::global::world->units.active.size(); a++ ) {
df::unit* unit = df::global::world->units.active[a];
if ( unit->counters.death_id == -1 ) {
livingUnits.insert(unit->id);
continue;
}
//dead: if dead since last check, trigger events
if ( livingUnits.find(unit->id) == livingUnits.end() )
continue;
for ( auto i = handlers[EventType::UNIT_DEATH].begin(); i != handlers[EventType::UNIT_DEATH].end(); i++ ) {
(*i).second.eventHandler(out, (void*)unit->id);
}
livingUnits.erase(unit->id);
}
}

@ -13,11 +13,13 @@ DFHACK_PLUGIN("eventExample");
void jobInitiated(color_ostream& out, void* job);
void jobCompleted(color_ostream& out, void* job);
void timePassed(color_ostream& out, void* ptr);
void unitDeath(color_ostream& out, void* ptr);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) {
EventManager::EventHandler initiateHandler(jobInitiated);
EventManager::EventHandler completeHandler(jobCompleted);
EventManager::EventHandler timeHandler(timePassed);
EventManager::EventHandler deathHandler(unitDeath);
Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample");
EventManager::registerListener(EventManager::EventType::JOB_INITIATED, initiateHandler, me);
@ -26,6 +28,7 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginC
EventManager::registerTick(timeHandler, 2, me);
EventManager::registerTick(timeHandler, 4, me);
EventManager::registerTick(timeHandler, 8, me);
EventManager::registerListener(EventManager::EventType::UNIT_DEATH, deathHandler, me);
return CR_OK;
}
@ -41,3 +44,7 @@ void jobCompleted(color_ostream& out, void* job) {
void timePassed(color_ostream& out, void* ptr) {
out.print("Time: %d\n", (int32_t)(ptr));
}
void unitDeath(color_ostream& out, void* ptr) {
out.print("Death: %d\n", (int32_t)(ptr));
}