2012-12-14 21:29:28 -07:00
|
|
|
#include "Core.h"
|
2012-12-15 16:08:59 -07:00
|
|
|
#include "Console.h"
|
2012-12-14 21:29:28 -07:00
|
|
|
#include "modules/EventManager.h"
|
|
|
|
#include "modules/Job.h"
|
|
|
|
#include "modules/World.h"
|
|
|
|
|
|
|
|
#include "df/global_objects.h"
|
2012-12-15 14:49:13 -07:00
|
|
|
#include "df/item.h"
|
|
|
|
#include "df/job.h"
|
2012-12-14 21:29:28 -07:00
|
|
|
#include "df/job_list_link.h"
|
2012-12-15 12:40:11 -07:00
|
|
|
#include "df/unit.h"
|
2012-12-14 21:29:28 -07:00
|
|
|
#include "df/world.h"
|
|
|
|
|
|
|
|
//#include <list>
|
|
|
|
#include <map>
|
|
|
|
//#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace EventManager;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* error checking
|
|
|
|
**/
|
|
|
|
|
|
|
|
//map<uint32_t, vector<DFHack::EventManager::EventHandler> > tickQueue;
|
|
|
|
multimap<uint32_t, EventHandler> tickQueue;
|
|
|
|
|
|
|
|
multimap<Plugin*, EventHandler> handlers[EventType::EVENT_MAX];
|
|
|
|
|
|
|
|
const uint32_t ticksPerYear = 403200;
|
|
|
|
|
|
|
|
void DFHack::EventManager::registerListener(EventType::EventType e, EventHandler handler, Plugin* plugin) {
|
|
|
|
handlers[e].insert(pair<Plugin*, EventHandler>(plugin, handler));
|
|
|
|
}
|
|
|
|
|
2012-12-15 16:08:59 -07:00
|
|
|
void DFHack::EventManager::registerTick(EventHandler handler, int32_t when, Plugin* plugin, bool absolute) {
|
2012-12-14 21:29:28 -07:00
|
|
|
uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear
|
|
|
|
+ DFHack::World::ReadCurrentTick();
|
|
|
|
if ( !Core::getInstance().isWorldLoaded() ) {
|
|
|
|
tick = 0;
|
2012-12-15 16:08:59 -07:00
|
|
|
if ( absolute ) {
|
|
|
|
Core::getInstance().getConsole().print("Warning: absolute flag will not be honored.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( absolute ) {
|
|
|
|
tick = 0;
|
2012-12-14 21:29:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
tickQueue.insert(pair<uint32_t, EventHandler>(tick+(uint32_t)when, handler));
|
|
|
|
handlers[EventType::TICK].insert(pair<Plugin*,EventHandler>(plugin,handler));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DFHack::EventManager::unregister(EventType::EventType e, EventHandler handler, Plugin* plugin) {
|
|
|
|
for ( multimap<Plugin*, EventHandler>::iterator i = handlers[e].find(plugin); i != handlers[e].end(); i++ ) {
|
|
|
|
if ( (*i).first != plugin )
|
|
|
|
break;
|
|
|
|
EventHandler handle = (*i).second;
|
|
|
|
if ( handle == handler ) {
|
|
|
|
handlers[e].erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DFHack::EventManager::unregisterAll(Plugin* plugin) {
|
|
|
|
for ( auto i = handlers[EventType::TICK].find(plugin); i != handlers[EventType::TICK].end(); i++ ) {
|
|
|
|
if ( (*i).first != plugin )
|
|
|
|
break;
|
|
|
|
|
|
|
|
//shenanigans to avoid concurrent modification
|
|
|
|
EventHandler getRidOf = (*i).second;
|
|
|
|
bool didSomething;
|
|
|
|
do {
|
|
|
|
didSomething = false;
|
|
|
|
for ( auto j = tickQueue.begin(); j != tickQueue.end(); j++ ) {
|
|
|
|
EventHandler candidate = (*j).second;
|
|
|
|
if ( getRidOf != candidate )
|
|
|
|
continue;
|
|
|
|
tickQueue.erase(j);
|
|
|
|
didSomething = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(didSomething);
|
|
|
|
}
|
|
|
|
for ( size_t a = 0; a < (size_t)EventType::EVENT_MAX; a++ ) {
|
|
|
|
handlers[a].erase(plugin);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void manageTickEvent(color_ostream& out);
|
|
|
|
static void manageJobInitiatedEvent(color_ostream& out);
|
|
|
|
static void manageJobCompletedEvent(color_ostream& out);
|
2012-12-15 12:40:11 -07:00
|
|
|
static void manageUnitDeathEvent(color_ostream& out);
|
2012-12-15 14:49:13 -07:00
|
|
|
static void manageItemCreationEvent(color_ostream& out);
|
2012-12-14 21:29:28 -07:00
|
|
|
|
2012-12-15 12:40:11 -07:00
|
|
|
static uint32_t lastTick = 0;
|
|
|
|
static int32_t lastJobId = -1;
|
2012-12-14 21:29:28 -07:00
|
|
|
static map<int32_t, df::job*> prevJobs;
|
2012-12-15 12:40:11 -07:00
|
|
|
static set<int32_t> livingUnits;
|
2012-12-15 14:49:13 -07:00
|
|
|
static int32_t nextItem;
|
2012-12-14 21:29:28 -07:00
|
|
|
|
|
|
|
void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) {
|
|
|
|
if ( event == DFHack::SC_MAP_UNLOADED ) {
|
|
|
|
lastTick = 0;
|
|
|
|
lastJobId = -1;
|
|
|
|
for ( auto i = prevJobs.begin(); i != prevJobs.end(); i++ ) {
|
|
|
|
Job::deleteJobStruct((*i).second);
|
|
|
|
}
|
|
|
|
prevJobs.clear();
|
|
|
|
tickQueue.clear();
|
2012-12-15 12:40:11 -07:00
|
|
|
livingUnits.clear();
|
2012-12-15 14:49:13 -07:00
|
|
|
nextItem = -1;
|
2012-12-14 21:29:28 -07:00
|
|
|
} else if ( event == DFHack::SC_MAP_LOADED ) {
|
|
|
|
uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear
|
|
|
|
+ DFHack::World::ReadCurrentTick();
|
|
|
|
multimap<uint32_t,EventHandler> newTickQueue;
|
|
|
|
for ( auto i = tickQueue.begin(); i != tickQueue.end(); i++ ) {
|
|
|
|
newTickQueue.insert(pair<uint32_t,EventHandler>(tick + (*i).first, (*i).second));
|
|
|
|
}
|
|
|
|
tickQueue.clear();
|
|
|
|
|
|
|
|
tickQueue.insert(newTickQueue.begin(), newTickQueue.end());
|
2012-12-15 14:49:13 -07:00
|
|
|
|
|
|
|
nextItem = *df::global::item_next_id;
|
2012-12-14 21:29:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DFHack::EventManager::manageEvents(color_ostream& out) {
|
|
|
|
if ( !Core::getInstance().isWorldLoaded() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear
|
|
|
|
+ DFHack::World::ReadCurrentTick();
|
|
|
|
if ( tick <= lastTick )
|
|
|
|
return;
|
|
|
|
lastTick = tick;
|
|
|
|
|
|
|
|
manageTickEvent(out);
|
|
|
|
manageJobInitiatedEvent(out);
|
|
|
|
manageJobCompletedEvent(out);
|
2012-12-15 12:40:11 -07:00
|
|
|
manageUnitDeathEvent(out);
|
2012-12-15 14:49:13 -07:00
|
|
|
manageItemCreationEvent(out);
|
|
|
|
|
2012-12-14 21:29:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void manageTickEvent(color_ostream& out) {
|
|
|
|
uint32_t tick = DFHack::World::ReadCurrentYear()*ticksPerYear
|
|
|
|
+ DFHack::World::ReadCurrentTick();
|
|
|
|
while ( !tickQueue.empty() ) {
|
|
|
|
if ( tick < (*tickQueue.begin()).first )
|
|
|
|
break;
|
|
|
|
EventHandler handle = (*tickQueue.begin()).second;
|
|
|
|
tickQueue.erase(tickQueue.begin());
|
|
|
|
handle.eventHandler(out, (void*)tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void manageJobInitiatedEvent(color_ostream& out) {
|
|
|
|
if ( handlers[EventType::JOB_INITIATED].empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( lastJobId == -1 ) {
|
|
|
|
lastJobId = *df::global::job_next_id - 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( lastJobId+1 == *df::global::job_next_id ) {
|
|
|
|
return; //no new jobs
|
|
|
|
}
|
2012-12-16 13:39:39 -07:00
|
|
|
multimap<Plugin*,EventHandler> copy(handlers[EventType::JOB_INITIATED].begin(), handlers[EventType::JOB_INITIATED].end());
|
2012-12-14 21:29:28 -07:00
|
|
|
|
|
|
|
for ( df::job_list_link* link = &df::global::world->job_list; link != NULL; link = link->next ) {
|
|
|
|
if ( link->item == NULL )
|
|
|
|
continue;
|
|
|
|
if ( link->item->id <= lastJobId )
|
|
|
|
continue;
|
2012-12-16 13:39:39 -07:00
|
|
|
for ( auto i = copy.begin(); i != copy.end(); i++ ) {
|
2012-12-14 21:29:28 -07:00
|
|
|
(*i).second.eventHandler(out, (void*)link->item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastJobId = *df::global::job_next_id - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void manageJobCompletedEvent(color_ostream& out) {
|
|
|
|
if ( handlers[EventType::JOB_COMPLETED].empty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-16 13:39:39 -07:00
|
|
|
multimap<Plugin*,EventHandler> copy(handlers[EventType::JOB_COMPLETED].begin(), handlers[EventType::JOB_COMPLETED].end());
|
2012-12-14 21:29:28 -07:00
|
|
|
map<int32_t, df::job*> nowJobs;
|
|
|
|
for ( df::job_list_link* link = &df::global::world->job_list; link != NULL; link = link->next ) {
|
|
|
|
if ( link->item == NULL )
|
|
|
|
continue;
|
|
|
|
nowJobs[link->item->id] = link->item;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( map<int32_t, df::job*>::iterator i = prevJobs.begin(); i != prevJobs.end(); i++ ) {
|
|
|
|
if ( nowJobs.find((*i).first) != nowJobs.end() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//recently finished or cancelled job!
|
2012-12-16 13:39:39 -07:00
|
|
|
for ( auto j = copy.begin(); j != copy.end(); j++ ) {
|
2012-12-14 21:29:28 -07:00
|
|
|
(*j).second.eventHandler(out, (void*)(*i).second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//erase old jobs, copy over possibly altered jobs
|
|
|
|
for ( map<int32_t, df::job*>::iterator i = prevJobs.begin(); i != prevJobs.end(); i++ ) {
|
|
|
|
Job::deleteJobStruct((*i).second);
|
|
|
|
}
|
|
|
|
prevJobs.clear();
|
|
|
|
|
|
|
|
//create new jobs
|
|
|
|
for ( map<int32_t, df::job*>::iterator j = nowJobs.begin(); j != nowJobs.end(); j++ ) {
|
|
|
|
/*map<int32_t, df::job*>::iterator i = prevJobs.find((*j).first);
|
|
|
|
if ( i != prevJobs.end() ) {
|
|
|
|
continue;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
df::job* newJob = Job::cloneJobStruct((*j).second, true);
|
|
|
|
prevJobs[newJob->id] = newJob;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*//get rid of old pointers to deallocated jobs
|
|
|
|
for ( size_t a = 0; a < toDelete.size(); a++ ) {
|
|
|
|
prevJobs.erase(a);
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
2012-12-15 12:40:11 -07:00
|
|
|
static void manageUnitDeathEvent(color_ostream& out) {
|
|
|
|
if ( handlers[EventType::UNIT_DEATH].empty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-16 13:39:39 -07:00
|
|
|
multimap<Plugin*,EventHandler> copy(handlers[EventType::UNIT_DEATH].begin(), handlers[EventType::UNIT_DEATH].end());
|
2012-12-15 12:40:11 -07:00
|
|
|
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;
|
|
|
|
|
2012-12-16 13:39:39 -07:00
|
|
|
for ( auto i = copy.begin(); i != copy.end(); i++ ) {
|
2012-12-15 12:40:11 -07:00
|
|
|
(*i).second.eventHandler(out, (void*)unit->id);
|
|
|
|
}
|
|
|
|
livingUnits.erase(unit->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 14:49:13 -07:00
|
|
|
static void manageItemCreationEvent(color_ostream& out) {
|
|
|
|
if ( handlers[EventType::ITEM_CREATED].empty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( nextItem >= *df::global::item_next_id ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-16 13:39:39 -07:00
|
|
|
multimap<Plugin*,EventHandler> copy(handlers[EventType::ITEM_CREATED].begin(), handlers[EventType::ITEM_CREATED].end());
|
2012-12-15 14:49:13 -07:00
|
|
|
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;
|
2012-12-16 13:39:39 -07:00
|
|
|
for ( auto i = copy.begin(); i != copy.end(); i++ ) {
|
2012-12-15 14:49:13 -07:00
|
|
|
(*i).second.eventHandler(out, (void*)item->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextItem = *df::global::item_next_id;
|
|
|
|
}
|
|
|
|
|