EventManager: added item creation event.

develop
expwnent 2012-12-15 16:49:13 -05:00
parent b0314755e0
commit 86e002f3ff
3 changed files with 60 additions and 4 deletions

@ -17,8 +17,8 @@ namespace DFHack {
JOB_INITIATED, JOB_INITIATED,
JOB_COMPLETED, JOB_COMPLETED,
UNIT_DEATH, UNIT_DEATH,
//ITEM_CREATED, ITEM_CREATED,
EVENT_MAX=UNIT_DEATH EVENT_MAX
}; };
} }

@ -3,8 +3,9 @@
#include "modules/Job.h" #include "modules/Job.h"
#include "modules/World.h" #include "modules/World.h"
#include "df/job.h"
#include "df/global_objects.h" #include "df/global_objects.h"
#include "df/item.h"
#include "df/job.h"
#include "df/job_list_link.h" #include "df/job_list_link.h"
#include "df/unit.h" #include "df/unit.h"
#include "df/world.h" #include "df/world.h"
@ -87,11 +88,13 @@ static void manageTickEvent(color_ostream& out);
static void manageJobInitiatedEvent(color_ostream& out); static void manageJobInitiatedEvent(color_ostream& out);
static void manageJobCompletedEvent(color_ostream& out); static void manageJobCompletedEvent(color_ostream& out);
static void manageUnitDeathEvent(color_ostream& out); static void manageUnitDeathEvent(color_ostream& out);
static void manageItemCreationEvent(color_ostream& out);
static uint32_t lastTick = 0; static uint32_t lastTick = 0;
static int32_t lastJobId = -1; static int32_t lastJobId = -1;
static map<int32_t, df::job*> prevJobs; static map<int32_t, df::job*> prevJobs;
static set<int32_t> livingUnits; static set<int32_t> livingUnits;
static int32_t nextItem;
void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) { void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) {
if ( event == DFHack::SC_MAP_UNLOADED ) { if ( event == DFHack::SC_MAP_UNLOADED ) {
@ -103,6 +106,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
prevJobs.clear(); prevJobs.clear();
tickQueue.clear(); tickQueue.clear();
livingUnits.clear(); livingUnits.clear();
nextItem = -1;
} else if ( event == DFHack::SC_MAP_LOADED ) { } else if ( event == DFHack::SC_MAP_LOADED ) {
uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear
+ DFHack::World::ReadCurrentTick(); + DFHack::World::ReadCurrentTick();
@ -113,6 +117,8 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
tickQueue.clear(); tickQueue.clear();
tickQueue.insert(newTickQueue.begin(), newTickQueue.end()); tickQueue.insert(newTickQueue.begin(), newTickQueue.end());
nextItem = *df::global::item_next_id;
} }
} }
@ -130,7 +136,8 @@ void DFHack::EventManager::manageEvents(color_ostream& out) {
manageJobInitiatedEvent(out); manageJobInitiatedEvent(out);
manageJobCompletedEvent(out); manageJobCompletedEvent(out);
manageUnitDeathEvent(out); manageUnitDeathEvent(out);
manageItemCreationEvent(out);
return; return;
} }
@ -241,3 +248,34 @@ static void manageUnitDeathEvent(color_ostream& out) {
} }
} }
static void manageItemCreationEvent(color_ostream& out) {
if ( handlers[EventType::ITEM_CREATED].empty() ) {
return;
}
if ( nextItem >= *df::global::item_next_id ) {
return;
}
size_t index = df::item::binsearch_index(df::global::world->items.all, nextItem, false);
for ( size_t a = index; a < df::global::world->items.all.size(); a++ ) {
df::item* item = df::global::world->items.all[a];
//invaders
if ( item->flags.bits.foreign )
continue;
//traders who bring back your items?
if ( item->flags.bits.trader )
continue;
//migrants
if ( item->flags.bits.owned )
continue;
//spider webs don't count
if ( item->flags.bits.spider_web )
continue;
for ( auto i = handlers[EventType::ITEM_CREATED].begin(); i != handlers[EventType::ITEM_CREATED].end(); i++ ) {
(*i).second.eventHandler(out, (void*)item->id);
}
}
nextItem = *df::global::item_next_id;
}

@ -6,6 +6,9 @@
#include "modules/EventManager.h" #include "modules/EventManager.h"
#include "DataDefs.h" #include "DataDefs.h"
#include "df/item.h"
#include "df/world.h"
using namespace DFHack; using namespace DFHack;
DFHACK_PLUGIN("eventExample"); DFHACK_PLUGIN("eventExample");
@ -14,12 +17,14 @@ void jobInitiated(color_ostream& out, void* job);
void jobCompleted(color_ostream& out, void* job); void jobCompleted(color_ostream& out, void* job);
void timePassed(color_ostream& out, void* ptr); void timePassed(color_ostream& out, void* ptr);
void unitDeath(color_ostream& out, void* ptr); void unitDeath(color_ostream& out, void* ptr);
void itemCreate(color_ostream& out, void* ptr);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) { DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) {
EventManager::EventHandler initiateHandler(jobInitiated); EventManager::EventHandler initiateHandler(jobInitiated);
EventManager::EventHandler completeHandler(jobCompleted); EventManager::EventHandler completeHandler(jobCompleted);
EventManager::EventHandler timeHandler(timePassed); EventManager::EventHandler timeHandler(timePassed);
EventManager::EventHandler deathHandler(unitDeath); EventManager::EventHandler deathHandler(unitDeath);
EventManager::EventHandler itemHandler(itemCreate);
Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample"); Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample");
EventManager::registerListener(EventManager::EventType::JOB_INITIATED, initiateHandler, me); EventManager::registerListener(EventManager::EventType::JOB_INITIATED, initiateHandler, me);
@ -29,6 +34,7 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginC
EventManager::registerTick(timeHandler, 4, me); EventManager::registerTick(timeHandler, 4, me);
EventManager::registerTick(timeHandler, 8, me); EventManager::registerTick(timeHandler, 8, me);
EventManager::registerListener(EventManager::EventType::UNIT_DEATH, deathHandler, me); EventManager::registerListener(EventManager::EventType::UNIT_DEATH, deathHandler, me);
EventManager::registerListener(EventManager::EventType::ITEM_CREATED, itemHandler, me);
return CR_OK; return CR_OK;
} }
@ -48,3 +54,15 @@ void timePassed(color_ostream& out, void* ptr) {
void unitDeath(color_ostream& out, void* ptr) { void unitDeath(color_ostream& out, void* ptr) {
out.print("Death: %d\n", (int32_t)(ptr)); out.print("Death: %d\n", (int32_t)(ptr));
} }
void itemCreate(color_ostream& out, void* ptr) {
int32_t item_index = df::item::binsearch_index(df::global::world->items.all, (int32_t)ptr);
if ( item_index == -1 ) {
out.print("%s, %d: Error.\n", __FILE__, __LINE__);
}
df::item* item = df::global::world->items.all[item_index];
df::item_type type = item->getType();
df::coord pos = item->pos;
out.print("Item created: %d, %s, at (%d,%d,%d)\n", (int32_t)(ptr), ENUM_KEY_STR(item_type, type).c_str(), pos.x, pos.y, pos.z);
}