EventManager: added syndrome event.

develop
expwnent 2013-01-02 18:30:15 -05:00
parent c3b2ae2137
commit 4e4e382b8f
4 changed files with 51 additions and 2 deletions

@ -19,6 +19,7 @@ namespace DFHack {
ITEM_CREATED,
BUILDING,
CONSTRUCTION,
SYNDROME,
EVENT_MAX
};
}
@ -36,6 +37,14 @@ namespace DFHack {
return !( *this == handle);
}
};
struct SyndromeData {
int32_t unitId;
int32_t syndromeIndex;
SyndromeData(int32_t unitId_in, int32_t syndromeIndex_in): unitId(unitId_in), syndromeIndex(syndromeIndex_in) {
}
};
DFHACK_EXPORT void registerListener(EventType::EventType e, EventHandler handler, int32_t freq, Plugin* plugin);
DFHACK_EXPORT void registerTick(EventHandler handler, int32_t when, Plugin* plugin, bool absolute=false);

@ -13,6 +13,7 @@
#include "df/job.h"
#include "df/job_list_link.h"
#include "df/unit.h"
#include "df/unit_syndrome.h"
#include "df/world.h"
#include <map>
@ -135,6 +136,7 @@ static void manageUnitDeathEvent(color_ostream& out);
static void manageItemCreationEvent(color_ostream& out);
static void manageBuildingEvent(color_ostream& out);
static void manageConstructionEvent(color_ostream& out);
static void manageSyndromeEvent(color_ostream& out);
//tick event
static uint32_t lastTick = 0;
@ -235,6 +237,10 @@ void DFHack::EventManager::manageEvents(color_ostream& out) {
manageConstructionEvent(out);
eventLastTick[EventType::CONSTRUCTION] = tick;
}
if ( tick - eventLastTick[EventType::SYNDROME] >= (*eventFrequency[EventType::SYNDROME].begin()).first ) {
manageSyndromeEvent(out);
eventLastTick[EventType::SYNDROME] = tick;
}
return;
}
@ -458,3 +464,29 @@ static void manageConstructionEvent(color_ostream& out) {
constructions.clear();
constructions.insert(constructionsNow.begin(), constructionsNow.end());
}
static void manageSyndromeEvent(color_ostream& out) {
if ( handlers[EventType::SYNDROME].empty() )
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::SYNDROME].begin(), handlers[EventType::SYNDROME].end());
for ( auto a = df::global::world->units.active.begin(); a != df::global::world->units.active.end(); a++ ) {
df::unit* unit = *a;
if ( unit->flags1.bits.dead )
continue;
for ( size_t b = 0; b < unit->syndromes.active.size(); b++ ) {
df::unit_syndrome* syndrome = unit->syndromes.active[b];
uint32_t startTime = syndrome->year*ticksPerYear + syndrome->year_time;
out.print("start time = %d, time = %d\n", startTime, eventLastTick[EventType::SYNDROME]);
if ( startTime < eventLastTick[EventType::SYNDROME] )
continue;
SyndromeData data(unit->id, b);
for ( auto c = copy.begin(); c != copy.end(); c++ ) {
EventHandler handle = (*c).second;
handle.eventHandler(out, (void*)&data);
}
}
}
}

@ -1 +1 @@
Subproject commit 22b01b80ad1f0e82c609dec56f09be1a46788921
Subproject commit fbf671a7d5aacb41cb44059eb16a1ee9cad419be

@ -23,10 +23,10 @@ void unitDeath(color_ostream& out, void* ptr);
void itemCreate(color_ostream& out, void* ptr);
void building(color_ostream& out, void* ptr);
void construction(color_ostream& out, void* ptr);
void syndrome(color_ostream& out, void* ptr);
command_result eventExample(color_ostream& out, vector<string>& parameters);
DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) {
commands.push_back(PluginCommand("eventExample", "Sets up a few event triggers.",eventExample));
return CR_OK;
@ -40,6 +40,7 @@ command_result eventExample(color_ostream& out, vector<string>& parameters) {
EventManager::EventHandler itemHandler(itemCreate);
EventManager::EventHandler buildingHandler(building);
EventManager::EventHandler constructionHandler(construction);
EventManager::EventHandler syndromeHandler(syndrome);
Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample");
EventManager::unregisterAll(me);
@ -53,6 +54,7 @@ command_result eventExample(color_ostream& out, vector<string>& parameters) {
EventManager::registerListener(EventManager::EventType::ITEM_CREATED, itemHandler, 1000, me);
EventManager::registerListener(EventManager::EventType::BUILDING, buildingHandler, 500, me);
EventManager::registerListener(EventManager::EventType::CONSTRUCTION, constructionHandler, 100, me);
EventManager::registerListener(EventManager::EventType::SYNDROME, syndromeHandler, 1, me);
out.print("Events registered.\n");
return CR_OK;
}
@ -91,3 +93,9 @@ void building(color_ostream& out, void* ptr) {
void construction(color_ostream& out, void* ptr) {
out.print("Construction created/destroyed: 0x%X\n", ptr);
}
void syndrome(color_ostream& out, void* ptr) {
EventManager::SyndromeData* data = (EventManager::SyndromeData*)ptr;
out.print("Syndrome started: unit %d, syndrome %d.\n", data->unitId, data->syndromeIndex);
}