From 961d033ade9450e396fcc19ff40bd36c03279c6c Mon Sep 17 00:00:00 2001 From: expwnent Date: Sun, 29 Jun 2014 10:03:55 -0400 Subject: [PATCH] EventManager: unload event for convenience. --- NEWS | 4 +++- library/include/modules/EventManager.h | 1 + library/modules/EventManager.cpp | 7 +++++++ plugins/eventful.cpp | 9 ++++++++- plugins/lua/eventful.lua | 3 +-- 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 2316f8a80..e2921924a 100644 --- a/NEWS +++ b/NEWS @@ -1,7 +1,9 @@ DFHack future Internals: - EventManager: added ON_REPORT and UNIT_ATTACK events and exposed them to Lua. + EventManager: + added ON_REPORT and UNIT_ATTACK events and exposed them to Lua. + added UNLOAD event for convenience New scripts: add-syndrome.lua add a syndrome to a unit. many configurable options diff --git a/library/include/modules/EventManager.h b/library/include/modules/EventManager.h index f95a75e8d..fdd42cf03 100644 --- a/library/include/modules/EventManager.h +++ b/library/include/modules/EventManager.h @@ -30,6 +30,7 @@ namespace DFHack { INVENTORY_CHANGE, REPORT, UNIT_ATTACK, + UNLOAD, EVENT_MAX }; } diff --git a/library/modules/EventManager.cpp b/library/modules/EventManager.cpp index 8c2670e17..c0ecff743 100644 --- a/library/modules/EventManager.cpp +++ b/library/modules/EventManager.cpp @@ -127,6 +127,7 @@ static void manageInvasionEvent(color_ostream& out); static void manageEquipmentEvent(color_ostream& out); static void manageReportEvent(color_ostream& out); static void manageUnitAttackEvent(color_ostream& out); +static void manageUnloadEvent(color_ostream& out){}; typedef void (*eventManager_t)(color_ostream&); @@ -143,6 +144,7 @@ static const eventManager_t eventManager[] = { manageEquipmentEvent, manageReportEvent, manageUnitAttackEvent, + manageUnloadEvent, }; //job initiated @@ -208,6 +210,11 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event lastReport = -1; lastReportUnitAttack = -1; gameLoaded = false; + + multimap copy(handlers[EventType::UNLOAD].begin(), handlers[EventType::UNLOAD].end()); + for (auto a = copy.begin(); a != copy.end(); a++ ) { + (*a).second.eventHandler(out, NULL); + } } else if ( event == DFHack::SC_MAP_LOADED ) { /* int32_t tick = df::global::world->frame_counter; diff --git a/plugins/eventful.cpp b/plugins/eventful.cpp index 5dbed2a5d..bb1b4c319 100644 --- a/plugins/eventful.cpp +++ b/plugins/eventful.cpp @@ -122,6 +122,7 @@ 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){}; static void handle_unitAttack(color_ostream& out,int32_t,int32_t,int32_t){}; +static void handle_unload(color_ostream& out){}; 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*); @@ -132,7 +133,8 @@ 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); -DEFINE_LUA_EVENT_3(onUnitAttack,handle_unitAttack,int32_t,int32_t,int32_t) +DEFINE_LUA_EVENT_3(onUnitAttack,handle_unitAttack,int32_t,int32_t,int32_t); +DEFINE_LUA_EVENT_0(onUnload,handle_unload); DFHACK_PLUGIN_LUA_EVENTS { DFHACK_LUA_EVENT(onWorkshopFillSidebarMenu), DFHACK_LUA_EVENT(postWorkshopFillSidebarMenu), @@ -154,6 +156,7 @@ DFHACK_PLUGIN_LUA_EVENTS { DFHACK_LUA_EVENT(onInventoryChange), DFHACK_LUA_EVENT(onReport), DFHACK_LUA_EVENT(onUnitAttack), + DFHACK_LUA_EVENT(onUnload), DFHACK_LUA_END }; @@ -221,6 +224,9 @@ static void ev_mng_unitAttack(color_ostream& out, void* ptr) { EventManager::UnitAttackData* data = (EventManager::UnitAttackData*)ptr; onUnitAttack(out,data->attacker,data->defender,data->wound); } +static void ev_mng_unload(color_ostream& out, void* ptr) { + onUnload(out); +} std::vector enabledEventManagerEvents(EventManager::EventType::EVENT_MAX,-1); typedef void (*handler_t) (color_ostream&,void*); static const handler_t eventHandlers[] = { @@ -236,6 +242,7 @@ static const handler_t eventHandlers[] = { ev_mng_inventory, ev_mng_report, ev_mng_unitAttack, + ev_mng_unload, }; static void enableEvent(int evType,int freq) { diff --git a/plugins/lua/eventful.lua b/plugins/lua/eventful.lua index 3c3865ce8..5d60e9dcf 100644 --- a/plugins/lua/eventful.lua +++ b/plugins/lua/eventful.lua @@ -132,6 +132,5 @@ local function invertTable(tbl) end return ret end -eventType=invertTable{[0]="TICK","JOB_INITIATED","JOB_COMPLETED","UNIT_DEATH","ITEM_CREATED", - "BUILDING","CONSTRUCTION","SYNDROME","INVASION","INVENTORY_CHANGE","REPORT","UNIT_ATTACK","EVENT_MAX"} +eventType=invertTable{[0]="TICK","JOB_INITIATED","JOB_COMPLETED","UNIT_DEATH","ITEM_CREATED", "BUILDING","CONSTRUCTION","SYNDROME","INVASION","INVENTORY_CHANGE","REPORT","UNIT_ATTACK","UNLOAD","EVENT_MAX"} return _ENV