Merge branch 'develop' into scriptOrganization

develop
expwnent 2014-06-27 22:03:09 -04:00
commit 2443568998
5 changed files with 80 additions and 37 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

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