From 7313c18a2a5481750ef2ec0b7dec46d9e3f45b26 Mon Sep 17 00:00:00 2001 From: expwnent Date: Mon, 23 Jun 2014 09:15:46 -0400 Subject: [PATCH] EventManager/eventful: added ON_REPORT event and exposed it to Lua. --- library/include/modules/EventManager.h | 1 + library/modules/EventManager.cpp | 32 ++++++++++++++++++ plugins/eventful.cpp | 45 ++++++++++++++------------ plugins/lua/eventful.lua | 2 +- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/library/include/modules/EventManager.h b/library/include/modules/EventManager.h index f73a94253..f1c54ac1d 100644 --- a/library/include/modules/EventManager.h +++ b/library/include/modules/EventManager.h @@ -26,6 +26,7 @@ namespace DFHack { SYNDROME, INVASION, INVENTORY_CHANGE, + REPORT, EVENT_MAX }; } diff --git a/library/modules/EventManager.cpp b/library/modules/EventManager.cpp index 88f4d56d6..ee99cf6a8 100644 --- a/library/modules/EventManager.cpp +++ b/library/modules/EventManager.cpp @@ -7,6 +7,7 @@ #include "modules/Job.h" #include "modules/World.h" +#include "df/announcement_type.h" #include "df/building.h" #include "df/construction.h" #include "df/general_ref.h" @@ -16,6 +17,7 @@ #include "df/item.h" #include "df/job.h" #include "df/job_list_link.h" +#include "df/report.h" #include "df/ui.h" #include "df/unit.h" #include "df/unit_flags1.h" @@ -116,6 +118,7 @@ static void manageConstructionEvent(color_ostream& out); static void manageSyndromeEvent(color_ostream& out); static void manageInvasionEvent(color_ostream& out); static void manageEquipmentEvent(color_ostream& out); +static void manageReportEvent(color_ostream& out); typedef void (*eventManager_t)(color_ostream&); @@ -130,6 +133,7 @@ static const eventManager_t eventManager[] = { manageSyndromeEvent, manageInvasionEvent, manageEquipmentEvent, + manageReportEvent }; //job initiated @@ -162,6 +166,9 @@ static int32_t nextInvasion; //static unordered_map > equipmentLog; static unordered_map > equipmentLog; +//report +static int32_t lastReport; + void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event event) { static bool doOnce = false; // 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(); Buildings::clearBuildings(out); + lastReport = -1; gameLoaded = false; } else if ( event == DFHack::SC_MAP_LOADED ) { /* @@ -235,6 +243,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event lastSyndromeTime = startTime; } } + lastReport = -1; for ( size_t a = 0; a < EventType::EVENT_MAX; a++ ) { eventLastTick[a] = -1;//-1000000; } @@ -694,3 +703,26 @@ static void manageEquipmentEvent(color_ostream& out) { } } +static void manageReportEvent(color_ostream& out) { + multimap copy(handlers[EventType::REPORT].begin(), handlers[EventType::REPORT].end()); + std::vector& 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; + } +} + diff --git a/plugins/eventful.cpp b/plugins/eventful.cpp index 4bfed38d9..b39947f57 100644 --- a/plugins/eventful.cpp +++ b/plugins/eventful.cpp @@ -1,36 +1,32 @@ #include "Core.h" #include "Error.h" -#include -#include -#include -#include -#include - -#include +#include "Console.h" +#include "Export.h" +#include "LuaTools.h" +#include "MiscUtils.h" +#include "PluginManager.h" +#include "VTableInterpose.h" +#include "df/building.h" #include "df/building_workshopst.h" - -#include "df/unit.h" -#include "df/unit_inventory_item.h" +#include "df/construction.h" #include "df/item.h" #include "df/item_actual.h" -#include "df/unit_wound.h" -#include "df/world.h" +#include "df/job.h" +#include "df/proj_itemst.h" +#include "df/proj_unitst.h" #include "df/reaction.h" #include "df/reaction_reagent_itemst.h" #include "df/reaction_product_itemst.h" - -#include "df/proj_itemst.h" -#include "df/proj_unitst.h" - -#include "MiscUtils.h" -#include "LuaTools.h" +#include "df/unit.h" +#include "df/unit_inventory_item.h" +#include "df/unit_wound.h" +#include "df/world.h" #include "modules/EventManager.h" -#include "df/job.h" -#include "df/building.h" -#include "df/construction.h" +#include +#include using std::vector; 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_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_report(color_ostream& out,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(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_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_1(onReport,handle_report,int32_t); DFHACK_PLUGIN_LUA_EVENTS { DFHACK_LUA_EVENT(onWorkshopFillSidebarMenu), DFHACK_LUA_EVENT(postWorkshopFillSidebarMenu), @@ -152,6 +150,7 @@ DFHACK_PLUGIN_LUA_EVENTS { DFHACK_LUA_EVENT(onSyndrome), DFHACK_LUA_EVENT(onInvasion), DFHACK_LUA_EVENT(onInventoryChange), + DFHACK_LUA_EVENT(onReport), 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); } +static void ev_mng_report(color_ostream& out, void* ptr) { + onReport(out,(int32_t)ptr); +} std::vector enabledEventManagerEvents(EventManager::EventType::EVENT_MAX,-1); typedef void (*handler_t) (color_ostream&,void*); static const handler_t eventHandlers[] = { @@ -225,6 +227,7 @@ static const handler_t eventHandlers[] = { ev_mng_syndrome, ev_mng_invasion, ev_mng_inventory, + ev_mng_report, }; static void enableEvent(int evType,int freq) { diff --git a/plugins/lua/eventful.lua b/plugins/lua/eventful.lua index 2aa713583..0a73be09f 100644 --- a/plugins/lua/eventful.lua +++ b/plugins/lua/eventful.lua @@ -133,5 +133,5 @@ local function invertTable(tbl) return ret end 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