EventManager/eventful: added ON_REPORT event and exposed it to Lua.

develop
expwnent 2014-06-23 09:15:46 -04:00
parent 9afc861a2a
commit 7313c18a2a
4 changed files with 58 additions and 22 deletions

@ -26,6 +26,7 @@ namespace DFHack {
SYNDROME, SYNDROME,
INVASION, INVASION,
INVENTORY_CHANGE, INVENTORY_CHANGE,
REPORT,
EVENT_MAX EVENT_MAX
}; };
} }

@ -7,6 +7,7 @@
#include "modules/Job.h" #include "modules/Job.h"
#include "modules/World.h" #include "modules/World.h"
#include "df/announcement_type.h"
#include "df/building.h" #include "df/building.h"
#include "df/construction.h" #include "df/construction.h"
#include "df/general_ref.h" #include "df/general_ref.h"
@ -16,6 +17,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/report.h"
#include "df/ui.h" #include "df/ui.h"
#include "df/unit.h" #include "df/unit.h"
#include "df/unit_flags1.h" #include "df/unit_flags1.h"
@ -116,6 +118,7 @@ static void manageConstructionEvent(color_ostream& out);
static void manageSyndromeEvent(color_ostream& out); static void manageSyndromeEvent(color_ostream& out);
static void manageInvasionEvent(color_ostream& out); static void manageInvasionEvent(color_ostream& out);
static void manageEquipmentEvent(color_ostream& out); static void manageEquipmentEvent(color_ostream& out);
static void manageReportEvent(color_ostream& out);
typedef void (*eventManager_t)(color_ostream&); typedef void (*eventManager_t)(color_ostream&);
@ -130,6 +133,7 @@ static const eventManager_t eventManager[] = {
manageSyndromeEvent, manageSyndromeEvent,
manageInvasionEvent, manageInvasionEvent,
manageEquipmentEvent, manageEquipmentEvent,
manageReportEvent
}; };
//job initiated //job initiated
@ -162,6 +166,9 @@ static int32_t nextInvasion;
//static unordered_map<int32_t, vector<df::unit_inventory_item> > equipmentLog; //static unordered_map<int32_t, vector<df::unit_inventory_item> > equipmentLog;
static unordered_map<int32_t, vector<InventoryItem> > equipmentLog; static unordered_map<int32_t, vector<InventoryItem> > equipmentLog;
//report
static int32_t lastReport;
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;
// const string eventNames[] = {"world loaded", "world unloaded", "map loaded", "map unloaded", "viewscreen changed", "core initialized", "begin unload", "paused", "unpaused"}; // const string eventNames[] = {"world loaded", "world unloaded", "map loaded", "map unloaded", "viewscreen changed", "core initialized", "begin unload", "paused", "unpaused"};
@ -186,6 +193,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
equipmentLog.clear(); equipmentLog.clear();
Buildings::clearBuildings(out); Buildings::clearBuildings(out);
lastReport = -1;
gameLoaded = false; gameLoaded = false;
} else if ( event == DFHack::SC_MAP_LOADED ) { } else if ( event == DFHack::SC_MAP_LOADED ) {
/* /*
@ -235,6 +243,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event
lastSyndromeTime = startTime; lastSyndromeTime = startTime;
} }
} }
lastReport = -1;
for ( size_t a = 0; a < EventType::EVENT_MAX; a++ ) { for ( size_t a = 0; a < EventType::EVENT_MAX; a++ ) {
eventLastTick[a] = -1;//-1000000; eventLastTick[a] = -1;//-1000000;
} }
@ -694,3 +703,26 @@ static void manageEquipmentEvent(color_ostream& out) {
} }
} }
static void manageReportEvent(color_ostream& out) {
multimap<Plugin*,EventHandler> copy(handlers[EventType::REPORT].begin(), handlers[EventType::REPORT].end());
std::vector<df::report*>& reports = df::global::world->status.reports;
if (reports.size() == 0) {
return;
}
size_t a = df::report::binsearch_index(reports, lastReport, false);
//this may or may not be needed: I don't know if binsearch_index goes earlier or later if it can't hit the target exactly
while (a < reports.size() && reports[a]->id <= lastReport) {
a++;
}
for ( ; a < reports.size(); a++ ) {
df::report* report = reports[a];
df::announcement_type type = report->type;
//starts with COMBAT_ or UNIT_PROJECTILE_
for ( auto b = copy.begin(); b != copy.end(); b++ ) {
EventHandler handle = (*b).second;
handle.eventHandler(out, (void*)report->id);
}
lastReport = report->id;
}
}

@ -1,36 +1,32 @@
#include "Core.h" #include "Core.h"
#include "Error.h" #include "Error.h"
#include <Console.h> #include "Console.h"
#include <Export.h> #include "Export.h"
#include <PluginManager.h> #include "LuaTools.h"
#include <string.h> #include "MiscUtils.h"
#include <stdexcept> #include "PluginManager.h"
#include "VTableInterpose.h"
#include <VTableInterpose.h>
#include "df/building.h"
#include "df/building_workshopst.h" #include "df/building_workshopst.h"
#include "df/construction.h"
#include "df/unit.h"
#include "df/unit_inventory_item.h"
#include "df/item.h" #include "df/item.h"
#include "df/item_actual.h" #include "df/item_actual.h"
#include "df/unit_wound.h" #include "df/job.h"
#include "df/world.h" #include "df/proj_itemst.h"
#include "df/proj_unitst.h"
#include "df/reaction.h" #include "df/reaction.h"
#include "df/reaction_reagent_itemst.h" #include "df/reaction_reagent_itemst.h"
#include "df/reaction_product_itemst.h" #include "df/reaction_product_itemst.h"
#include "df/unit.h"
#include "df/proj_itemst.h" #include "df/unit_inventory_item.h"
#include "df/proj_unitst.h" #include "df/unit_wound.h"
#include "df/world.h"
#include "MiscUtils.h"
#include "LuaTools.h"
#include "modules/EventManager.h" #include "modules/EventManager.h"
#include "df/job.h" #include <string.h>
#include "df/building.h" #include <stdexcept>
#include "df/construction.h"
using std::vector; using std::vector;
using std::string; using std::string;
@ -124,6 +120,7 @@ static void handle_job_complete(color_ostream &out,df::job*){};
static void handle_constructions(color_ostream &out,df::construction*){}; static void handle_constructions(color_ostream &out,df::construction*){};
static void handle_syndrome(color_ostream &out,int32_t,int32_t){}; static void handle_syndrome(color_ostream &out,int32_t,int32_t){};
static void handle_inventory_change(color_ostream& out,int32_t,int32_t,df::unit_inventory_item*,df::unit_inventory_item*){}; static void handle_inventory_change(color_ostream& out,int32_t,int32_t,df::unit_inventory_item*,df::unit_inventory_item*){};
static void handle_report(color_ostream& out,int32_t){};
DEFINE_LUA_EVENT_1(onBuildingCreatedDestroyed, handle_int32t, int32_t); DEFINE_LUA_EVENT_1(onBuildingCreatedDestroyed, handle_int32t, int32_t);
DEFINE_LUA_EVENT_1(onJobInitiated,handle_job_init,df::job*); DEFINE_LUA_EVENT_1(onJobInitiated,handle_job_init,df::job*);
DEFINE_LUA_EVENT_1(onJobCompleted,handle_job_complete,df::job*); DEFINE_LUA_EVENT_1(onJobCompleted,handle_job_complete,df::job*);
@ -133,6 +130,7 @@ DEFINE_LUA_EVENT_1(onConstructionCreatedDestroyed, handle_constructions, df::con
DEFINE_LUA_EVENT_2(onSyndrome, handle_syndrome, int32_t,int32_t); DEFINE_LUA_EVENT_2(onSyndrome, handle_syndrome, int32_t,int32_t);
DEFINE_LUA_EVENT_1(onInvasion,handle_int32t,int32_t); DEFINE_LUA_EVENT_1(onInvasion,handle_int32t,int32_t);
DEFINE_LUA_EVENT_4(onInventoryChange,handle_inventory_change,int32_t,int32_t,df::unit_inventory_item*,df::unit_inventory_item*); DEFINE_LUA_EVENT_4(onInventoryChange,handle_inventory_change,int32_t,int32_t,df::unit_inventory_item*,df::unit_inventory_item*);
DEFINE_LUA_EVENT_1(onReport,handle_report,int32_t);
DFHACK_PLUGIN_LUA_EVENTS { DFHACK_PLUGIN_LUA_EVENTS {
DFHACK_LUA_EVENT(onWorkshopFillSidebarMenu), DFHACK_LUA_EVENT(onWorkshopFillSidebarMenu),
DFHACK_LUA_EVENT(postWorkshopFillSidebarMenu), DFHACK_LUA_EVENT(postWorkshopFillSidebarMenu),
@ -152,6 +150,7 @@ DFHACK_PLUGIN_LUA_EVENTS {
DFHACK_LUA_EVENT(onSyndrome), DFHACK_LUA_EVENT(onSyndrome),
DFHACK_LUA_EVENT(onInvasion), DFHACK_LUA_EVENT(onInvasion),
DFHACK_LUA_EVENT(onInventoryChange), DFHACK_LUA_EVENT(onInventoryChange),
DFHACK_LUA_EVENT(onReport),
DFHACK_LUA_END DFHACK_LUA_END
}; };
@ -212,6 +211,9 @@ static void ev_mng_inventory(color_ostream& out, void* ptr)
} }
onInventoryChange(out,unitId,itemId,item_old,item_new); onInventoryChange(out,unitId,itemId,item_old,item_new);
} }
static void ev_mng_report(color_ostream& out, void* ptr) {
onReport(out,(int32_t)ptr);
}
std::vector<int> enabledEventManagerEvents(EventManager::EventType::EVENT_MAX,-1); std::vector<int> enabledEventManagerEvents(EventManager::EventType::EVENT_MAX,-1);
typedef void (*handler_t) (color_ostream&,void*); typedef void (*handler_t) (color_ostream&,void*);
static const handler_t eventHandlers[] = { static const handler_t eventHandlers[] = {
@ -225,6 +227,7 @@ static const handler_t eventHandlers[] = {
ev_mng_syndrome, ev_mng_syndrome,
ev_mng_invasion, ev_mng_invasion,
ev_mng_inventory, ev_mng_inventory,
ev_mng_report,
}; };
static void enableEvent(int evType,int freq) static void enableEvent(int evType,int freq)
{ {

@ -133,5 +133,5 @@ local function invertTable(tbl)
return ret return ret
end end
eventType=invertTable{[0]="TICK","JOB_INITIATED","JOB_COMPLETED","UNIT_DEATH","ITEM_CREATED", eventType=invertTable{[0]="TICK","JOB_INITIATED","JOB_COMPLETED","UNIT_DEATH","ITEM_CREATED",
"BUILDING","CONSTRUCTION","SYNDROME","INVASION","INVENTORY_CHANGE","EVENT_MAX"} "BUILDING","CONSTRUCTION","SYNDROME","INVASION","INVENTORY_CHANGE","REPORT","EVENT_MAX"}
return _ENV return _ENV