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 diff --git a/plugins/syndromeTrigger.cpp b/plugins/syndromeTrigger.cpp index ee75efc36..475d1d56a 100644 --- a/plugins/syndromeTrigger.cpp +++ b/plugins/syndromeTrigger.cpp @@ -20,8 +20,7 @@ using namespace DFHack; using namespace std; -static bool enabled = false; - +DFHACK_PLUGIN_IS_ENABLED(enabled); DFHACK_PLUGIN("syndromeTrigger"); void syndromeHandler(color_ostream& out, void* ptr); @@ -43,34 +42,42 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector & parameters) { if ( parameters.size() > 1 ) return CR_WRONG_USAGE; - bool wasEnabled = enabled; + bool enable; if ( parameters.size() == 1 ) { if ( parameters[0] == "enable" ) { - enabled = true; + enable = true; } else if ( parameters[0] == "disable" ) { - enabled = false; + enable = false; } else { int32_t a = atoi(parameters[0].c_str()); if ( a < 0 || a > 1 ) return CR_WRONG_USAGE; - enabled = (bool)a; + enable = (bool)a; } } - + + plugin_enable(out, enable); out.print("syndromeTrigger is %s\n", enabled ? "enabled" : "disabled"); - if ( enabled == wasEnabled ) - return CR_OK; - - EventManager::unregisterAll(plugin_self); - if ( enabled ) { - EventManager::EventHandler handle(syndromeHandler, 1); - EventManager::registerListener(EventManager::EventType::SYNDROME, handle, plugin_self); - } return CR_OK; }