Added lua interface (in eventful) for EventManager module.

develop
Warmist 2013-10-20 21:44:07 +03:00
parent e7c102646c
commit 0dff26aa23
4 changed files with 168 additions and 4 deletions

@ -3000,6 +3000,42 @@ List of events
Is called after calling (or not) native fillSidebarMenu(). Useful for job button Is called after calling (or not) native fillSidebarMenu(). Useful for job button
tweaking (e.g. adding custom reactions) tweaking (e.g. adding custom reactions)
Events from EventManager
------------------------
These events are straight from EventManager module. Each of them first needs to be enabled. See functions for more info.
1. ``onBuildingCreatedDestroyed(building_id)``
Gets called when building is created or destroyed.
2. ``onConstructionCreatedDestroyed(building_id)``
Gets called when construction is created or destroyed.
3. ``onJobInitiated(job)``
Gets called when job is issued.
4. ``onJobCompleted(job)``
Gets called when job is finished. The job that is passed to this function is a copy.
5. ``onUnitDeath(unit_id)``
Gets called on unit death.
6. ``onItemCreated(item_id)``
Gets called when item is created (except due to traders, migrants, invaders and spider webs).
7. ``onSyndrome(unit_id,syndrome_index)``
Gets called when new syndrome appears on a unit.
8. ``onInvasion(invasion_id)``
Gets called when new invasion happens.
Functions Functions
--------- ---------
@ -3015,6 +3051,10 @@ Functions
Add a custom reaction to the building. Add a custom reaction to the building.
4. ``enableEvent(evType,frequency)``
Enable event checking for EventManager events. For event types use ``eventType`` table.
Examples Examples
-------- --------
Spawn dragon breath on each item attempt to contaminate wound:: Spawn dragon breath on each item attempt to contaminate wound::

@ -26,10 +26,11 @@ namespace DFHack {
} }
struct EventHandler { struct EventHandler {
void (*eventHandler)(color_ostream&, void*); //called when the event happens typedef void (*callback_t)(color_ostream&, void*); //called when the event happens
callback_t eventHandler;
int32_t freq; int32_t freq;
EventHandler(void (*eventHandlerIn)(color_ostream&, void*), int32_t freqIn): eventHandler(eventHandlerIn), freq(freqIn) { EventHandler(callback_t eventHandlerIn, int32_t freqIn): eventHandler(eventHandlerIn), freq(freqIn) {
} }
bool operator==(EventHandler& handle) const { bool operator==(EventHandler& handle) const {

@ -23,6 +23,12 @@
#include "MiscUtils.h" #include "MiscUtils.h"
#include "LuaTools.h" #include "LuaTools.h"
#include "modules/EventManager.h"
#include "df/job.h"
#include "df/building.h"
#include "df/construction.h"
using std::vector; using std::vector;
using std::string; using std::string;
using std::stack; using std::stack;
@ -108,7 +114,20 @@ DEFINE_LUA_EVENT_2(onProjItemCheckImpact, handle_projitem_ci, df::proj_itemst*,b
DEFINE_LUA_EVENT_1(onProjItemCheckMovement, handle_projitem_cm, df::proj_itemst*); DEFINE_LUA_EVENT_1(onProjItemCheckMovement, handle_projitem_cm, df::proj_itemst*);
DEFINE_LUA_EVENT_2(onProjUnitCheckImpact, handle_projunit_ci, df::proj_unitst*,bool ); DEFINE_LUA_EVENT_2(onProjUnitCheckImpact, handle_projunit_ci, df::proj_unitst*,bool );
DEFINE_LUA_EVENT_1(onProjUnitCheckMovement, handle_projunit_cm, df::proj_unitst* ); DEFINE_LUA_EVENT_1(onProjUnitCheckMovement, handle_projunit_cm, df::proj_unitst* );
//event manager
static void handle_int32t(color_ostream &out,int32_t){}; //we don't use this so why not use it everywhere
static void handle_job_init(color_ostream &out,df::job*){};
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){};
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*);
DEFINE_LUA_EVENT_1(onUnitDeath,handle_int32t,int32_t);
DEFINE_LUA_EVENT_1(onItemCreated,handle_int32t,int32_t);
DEFINE_LUA_EVENT_1(onConstructionCreatedDestroyed, handle_constructions, df::construction*);
DEFINE_LUA_EVENT_2(onSyndrome, handle_syndrome, int32_t,int32_t);
DEFINE_LUA_EVENT_1(onInvasion,handle_int32t,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),
@ -118,6 +137,101 @@ DFHACK_PLUGIN_LUA_EVENTS {
DFHACK_LUA_EVENT(onProjItemCheckMovement), DFHACK_LUA_EVENT(onProjItemCheckMovement),
DFHACK_LUA_EVENT(onProjUnitCheckImpact), DFHACK_LUA_EVENT(onProjUnitCheckImpact),
DFHACK_LUA_EVENT(onProjUnitCheckMovement), DFHACK_LUA_EVENT(onProjUnitCheckMovement),
/* event manager events */
DFHACK_LUA_EVENT(onBuildingCreatedDestroyed),
DFHACK_LUA_EVENT(onConstructionCreatedDestroyed),
DFHACK_LUA_EVENT(onJobInitiated),
DFHACK_LUA_EVENT(onJobCompleted),
DFHACK_LUA_EVENT(onUnitDeath),
DFHACK_LUA_EVENT(onItemCreated),
DFHACK_LUA_EVENT(onSyndrome),
DFHACK_LUA_EVENT(onInvasion),
DFHACK_LUA_END
};
static void ev_mng_jobInitiated(color_ostream& out, void* job)
{
df::job* ptr=reinterpret_cast<df::job*>(job);
onJobInitiated(out,ptr);
}
void ev_mng_jobCompleted(color_ostream& out, void* job)
{
df::job* ptr=reinterpret_cast<df::job*>(job);
onJobCompleted(out,ptr);
}
void ev_mng_unitDeath(color_ostream& out, void* ptr)
{
int32_t myId=int32_t(ptr);
onUnitDeath(out,myId);
}
void ev_mng_itemCreate(color_ostream& out, void* ptr)
{
int32_t myId=int32_t(ptr);
onItemCreated(out,myId);
}
void ev_mng_construction(color_ostream& out, void* ptr)
{
df::construction* cons=reinterpret_cast<df::construction*>(ptr);
onConstructionCreatedDestroyed(out,cons);
}
void ev_mng_syndrome(color_ostream& out, void* ptr)
{
EventManager::SyndromeData* data=reinterpret_cast<EventManager::SyndromeData*>(ptr);
onSyndrome(out,data->unitId,data->syndromeIndex);
}
void ev_mng_invasion(color_ostream& out, void* ptr)
{
int32_t myId=int32_t(ptr);
onInvasion(out,myId);
}
static void ev_mng_building(color_ostream& out, void* ptr)
{
int32_t myId=int32_t(ptr);
onBuildingCreatedDestroyed(out,myId);
}
std::vector<int> enabledEventManagerEvents(EventManager::EventType::EVENT_MAX,-1);
static void enableEvent(int evType,int freq)
{
EventManager::EventType::EventType typeToEnable=static_cast<EventManager::EventType::EventType>(evType);
EventManager::EventHandler::callback_t fun_ptr;
switch(typeToEnable)
{
case EventManager::EventType::BUILDING:
fun_ptr=ev_mng_building;
break;
case EventManager::EventType::JOB_INITIATED:
fun_ptr=ev_mng_jobInitiated;
break;
case EventManager::EventType::JOB_COMPLETED:
fun_ptr=ev_mng_jobCompleted;
break;
case EventManager::EventType::UNIT_DEATH:
fun_ptr=ev_mng_unitDeath;
break;
case EventManager::EventType::ITEM_CREATED:
fun_ptr=ev_mng_itemCreate;
break;
case EventManager::EventType::CONSTRUCTION:
fun_ptr=ev_mng_construction;
break;
case EventManager::EventType::SYNDROME:
fun_ptr=ev_mng_syndrome;
break;
case EventManager::EventType::INVASION:
fun_ptr=ev_mng_invasion;
break;
default:
throw std::runtime_error("Invalid event type to enable");
}
if(enabledEventManagerEvents[typeToEnable]!=-1)
EventManager::unregister(typeToEnable,EventManager::EventHandler(fun_ptr,enabledEventManagerEvents[typeToEnable]),plugin_self);
if(freq!=-1)
EventManager::registerListener(typeToEnable,EventManager::EventHandler(fun_ptr,freq),plugin_self);
enabledEventManagerEvents[typeToEnable]=freq;
}
DFHACK_PLUGIN_LUA_FUNCTIONS{
DFHACK_LUA_FUNCTION(enableEvent),
DFHACK_LUA_END DFHACK_LUA_END
}; };
struct workshop_hook : df::building_workshopst{ struct workshop_hook : df::building_workshopst{
@ -323,5 +437,6 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{ {
disable_all_hooks(out); disable_all_hooks(out);
EventManager::unregisterAll(plugin_self);
return CR_OK; return CR_OK;
} }

@ -108,5 +108,13 @@ function addReactionToShop(reaction_name,shop_name)
postWorkshopFillSidebarMenu._library=onPostSidebar postWorkshopFillSidebarMenu._library=onPostSidebar
dfhack.onStateChange.eventful=unregall dfhack.onStateChange.eventful=unregall
end end
local function invertTable(tbl)
local ret={}
for k,v in pairs(tbl) do
ret[v]=k
end
return ret
end
eventType=invertTable{[0]="TICK","JOB_INITIATED","JOB_COMPLETED","UNIT_DEATH","ITEM_CREATED",
"BUILDING","CONSTRUCTION","SYNDROME","INVASION"}
return _ENV return _ENV