EventManager: added invasion event.

develop
expwnent 2013-01-03 15:52:56 -05:00
parent 5865579b23
commit 910e398a7b
3 changed files with 35 additions and 0 deletions

@ -20,6 +20,7 @@ namespace DFHack {
BUILDING, BUILDING,
CONSTRUCTION, CONSTRUCTION,
SYNDROME, SYNDROME,
INVASION,
EVENT_MAX EVENT_MAX
}; };
} }

@ -12,6 +12,7 @@
#include "df/item.h" #include "df/item.h"
#include "df/job.h" #include "df/job.h"
#include "df/job_list_link.h" #include "df/job_list_link.h"
#include "df/ui.h"
#include "df/unit.h" #include "df/unit.h"
#include "df/unit_syndrome.h" #include "df/unit_syndrome.h"
#include "df/world.h" #include "df/world.h"
@ -137,6 +138,7 @@ static void manageItemCreationEvent(color_ostream& out);
static void manageBuildingEvent(color_ostream& out); static void manageBuildingEvent(color_ostream& out);
static void manageConstructionEvent(color_ostream& out); static void manageConstructionEvent(color_ostream& out);
static void manageSyndromeEvent(color_ostream& out); static void manageSyndromeEvent(color_ostream& out);
static void manageInvasionEvent(color_ostream& out);
//tick event //tick event
static uint32_t lastTick = 0; static uint32_t lastTick = 0;
@ -161,6 +163,9 @@ static unordered_set<int32_t> buildings;
static unordered_set<df::construction*> constructions; static unordered_set<df::construction*> constructions;
static bool gameLoaded; static bool gameLoaded;
//invasion
static int32_t nextInvasion;
void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) { void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) {
static bool doOnce = false; static bool doOnce = false;
if ( !doOnce ) { if ( !doOnce ) {
@ -186,6 +191,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
Buildings::clearBuildings(out); Buildings::clearBuildings(out);
gameLoaded = false; gameLoaded = false;
nextInvasion = -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();
@ -200,6 +206,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
nextItem = 0; nextItem = 0;
nextBuilding = 0; nextBuilding = 0;
lastTick = 0; lastTick = 0;
nextInvasion = df::global::ui->invasions.next_id;
gameLoaded = true; gameLoaded = true;
} }
} }
@ -244,6 +251,10 @@ void DFHack::EventManager::manageEvents(color_ostream& out) {
manageSyndromeEvent(out); manageSyndromeEvent(out);
eventLastTick[EventType::SYNDROME] = tick; eventLastTick[EventType::SYNDROME] = tick;
} }
if ( tick - eventLastTick[EventType::INVASION] >= (*eventFrequency[EventType::INVASION].begin()).first ) {
manageInvasionEvent(out);
eventLastTick[EventType::INVASION] = tick;
}
return; return;
} }
@ -492,3 +503,19 @@ static void manageSyndromeEvent(color_ostream& out) {
} }
} }
static void manageInvasionEvent(color_ostream& out) {
if ( handlers[EventType::INVASION].empty() )
return;
multimap<Plugin*,EventHandler> copy(handlers[EventType::INVASION].begin(), handlers[EventType::INVASION].end());
if ( df::global::ui->invasions.next_id <= nextInvasion )
return;
nextInvasion = df::global::ui->invasions.next_id;
for ( auto a = copy.begin(); a != copy.end(); a++ ) {
EventHandler handle = (*a).second;
handle.eventHandler(out, (void*)nextInvasion);
}
}

@ -24,6 +24,7 @@ void itemCreate(color_ostream& out, void* ptr);
void building(color_ostream& out, void* ptr); void building(color_ostream& out, void* ptr);
void construction(color_ostream& out, void* ptr); void construction(color_ostream& out, void* ptr);
void syndrome(color_ostream& out, void* ptr); void syndrome(color_ostream& out, void* ptr);
void invasion(color_ostream& out, void* ptr);
command_result eventExample(color_ostream& out, vector<string>& parameters); command_result eventExample(color_ostream& out, vector<string>& parameters);
@ -41,6 +42,7 @@ command_result eventExample(color_ostream& out, vector<string>& parameters) {
EventManager::EventHandler buildingHandler(building); EventManager::EventHandler buildingHandler(building);
EventManager::EventHandler constructionHandler(construction); EventManager::EventHandler constructionHandler(construction);
EventManager::EventHandler syndromeHandler(syndrome); EventManager::EventHandler syndromeHandler(syndrome);
EventManager::EventHandler invasionHandler(invasion);
Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample"); Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample");
EventManager::unregisterAll(me); EventManager::unregisterAll(me);
@ -55,6 +57,7 @@ command_result eventExample(color_ostream& out, vector<string>& parameters) {
EventManager::registerListener(EventManager::EventType::BUILDING, buildingHandler, 500, me); EventManager::registerListener(EventManager::EventType::BUILDING, buildingHandler, 500, me);
EventManager::registerListener(EventManager::EventType::CONSTRUCTION, constructionHandler, 100, me); EventManager::registerListener(EventManager::EventType::CONSTRUCTION, constructionHandler, 100, me);
EventManager::registerListener(EventManager::EventType::SYNDROME, syndromeHandler, 1, me); EventManager::registerListener(EventManager::EventType::SYNDROME, syndromeHandler, 1, me);
EventManager::registerListener(EventManager::EventType::INVASION, invasionHandler, 1, me);
out.print("Events registered.\n"); out.print("Events registered.\n");
return CR_OK; return CR_OK;
} }
@ -99,3 +102,7 @@ void syndrome(color_ostream& out, void* ptr) {
out.print("Syndrome started: unit %d, syndrome %d.\n", data->unitId, data->syndromeIndex); out.print("Syndrome started: unit %d, syndrome %d.\n", data->unitId, data->syndromeIndex);
} }
void invasion(color_ostream& out, void* ptr) {
out.print("New invasion! %d\n", (int32_t)ptr);
}