2012-12-14 19:05:38 -07:00
|
|
|
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "Export.h"
|
2012-12-14 20:14:38 -07:00
|
|
|
#include "PluginManager.h"
|
2012-12-14 19:05:38 -07:00
|
|
|
#include "modules/EventManager.h"
|
|
|
|
#include "DataDefs.h"
|
|
|
|
|
2012-12-15 14:49:13 -07:00
|
|
|
#include "df/item.h"
|
|
|
|
#include "df/world.h"
|
|
|
|
|
2012-12-15 15:43:41 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2012-12-14 19:05:38 -07:00
|
|
|
using namespace DFHack;
|
2012-12-15 15:43:41 -07:00
|
|
|
using namespace std;
|
2012-12-14 19:05:38 -07:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("eventExample");
|
|
|
|
|
|
|
|
void jobInitiated(color_ostream& out, void* job);
|
|
|
|
void jobCompleted(color_ostream& out, void* job);
|
2012-12-14 21:29:28 -07:00
|
|
|
void timePassed(color_ostream& out, void* ptr);
|
2012-12-15 12:40:11 -07:00
|
|
|
void unitDeath(color_ostream& out, void* ptr);
|
2012-12-15 14:49:13 -07:00
|
|
|
void itemCreate(color_ostream& out, void* ptr);
|
2012-12-18 16:34:38 -07:00
|
|
|
void building(color_ostream& out, void* ptr);
|
|
|
|
void construction(color_ostream& out, void* ptr);
|
2013-01-02 16:30:15 -07:00
|
|
|
void syndrome(color_ostream& out, void* ptr);
|
2013-01-03 13:52:56 -07:00
|
|
|
void invasion(color_ostream& out, void* ptr);
|
2012-12-14 19:05:38 -07:00
|
|
|
|
2012-12-15 15:43:41 -07:00
|
|
|
command_result eventExample(color_ostream& out, vector<string>& parameters);
|
|
|
|
|
2012-12-14 19:05:38 -07:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, std::vector<PluginCommand> &commands) {
|
2012-12-15 15:43:41 -07:00
|
|
|
commands.push_back(PluginCommand("eventExample", "Sets up a few event triggers.",eventExample));
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result eventExample(color_ostream& out, vector<string>& parameters) {
|
2013-01-03 17:31:29 -07:00
|
|
|
EventManager::EventHandler initiateHandler(jobInitiated, 10);
|
|
|
|
EventManager::EventHandler completeHandler(jobCompleted, 5);
|
|
|
|
EventManager::EventHandler timeHandler(timePassed, 1);
|
|
|
|
EventManager::EventHandler deathHandler(unitDeath, 500);
|
|
|
|
EventManager::EventHandler itemHandler(itemCreate, 1000);
|
|
|
|
EventManager::EventHandler buildingHandler(building, 500);
|
|
|
|
EventManager::EventHandler constructionHandler(construction, 100);
|
|
|
|
EventManager::EventHandler syndromeHandler(syndrome, 1);
|
|
|
|
EventManager::EventHandler invasionHandler(invasion, 1000);
|
2012-12-14 20:14:38 -07:00
|
|
|
Plugin* me = Core::getInstance().getPluginManager()->getPluginByName("eventExample");
|
2012-12-18 16:34:38 -07:00
|
|
|
EventManager::unregisterAll(me);
|
2012-12-14 19:05:38 -07:00
|
|
|
|
2013-01-03 17:31:29 -07:00
|
|
|
EventManager::registerListener(EventManager::EventType::JOB_INITIATED, initiateHandler, me);
|
|
|
|
EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, completeHandler, me);
|
2012-12-14 21:29:28 -07:00
|
|
|
EventManager::registerTick(timeHandler, 1, me);
|
|
|
|
EventManager::registerTick(timeHandler, 2, me);
|
|
|
|
EventManager::registerTick(timeHandler, 4, me);
|
|
|
|
EventManager::registerTick(timeHandler, 8, me);
|
2013-01-03 17:31:29 -07:00
|
|
|
EventManager::registerListener(EventManager::EventType::UNIT_DEATH, deathHandler, me);
|
|
|
|
EventManager::registerListener(EventManager::EventType::ITEM_CREATED, itemHandler, me);
|
|
|
|
EventManager::registerListener(EventManager::EventType::BUILDING, buildingHandler, me);
|
|
|
|
EventManager::registerListener(EventManager::EventType::CONSTRUCTION, constructionHandler, me);
|
|
|
|
EventManager::registerListener(EventManager::EventType::SYNDROME, syndromeHandler, me);
|
|
|
|
EventManager::registerListener(EventManager::EventType::INVASION, invasionHandler, me);
|
2012-12-15 15:43:41 -07:00
|
|
|
out.print("Events registered.\n");
|
2012-12-14 19:05:38 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jobInitiated(color_ostream& out, void* job) {
|
|
|
|
out.print("Job initiated! 0x%X\n", job);
|
|
|
|
}
|
|
|
|
|
|
|
|
void jobCompleted(color_ostream& out, void* job) {
|
|
|
|
out.print("Job completed! 0x%X\n", job);
|
|
|
|
}
|
|
|
|
|
2012-12-14 21:29:28 -07:00
|
|
|
void timePassed(color_ostream& out, void* ptr) {
|
|
|
|
out.print("Time: %d\n", (int32_t)(ptr));
|
|
|
|
}
|
2012-12-15 12:40:11 -07:00
|
|
|
|
|
|
|
void unitDeath(color_ostream& out, void* ptr) {
|
|
|
|
out.print("Death: %d\n", (int32_t)(ptr));
|
|
|
|
}
|
2012-12-15 14:49:13 -07:00
|
|
|
|
|
|
|
void itemCreate(color_ostream& out, void* ptr) {
|
|
|
|
int32_t item_index = df::item::binsearch_index(df::global::world->items.all, (int32_t)ptr);
|
|
|
|
if ( item_index == -1 ) {
|
|
|
|
out.print("%s, %d: Error.\n", __FILE__, __LINE__);
|
|
|
|
}
|
|
|
|
df::item* item = df::global::world->items.all[item_index];
|
|
|
|
df::item_type type = item->getType();
|
|
|
|
df::coord pos = item->pos;
|
|
|
|
out.print("Item created: %d, %s, at (%d,%d,%d)\n", (int32_t)(ptr), ENUM_KEY_STR(item_type, type).c_str(), pos.x, pos.y, pos.z);
|
|
|
|
}
|
|
|
|
|
2012-12-18 16:34:38 -07:00
|
|
|
void building(color_ostream& out, void* ptr) {
|
|
|
|
out.print("Building created/destroyed: %d\n", (int32_t)ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void construction(color_ostream& out, void* ptr) {
|
|
|
|
out.print("Construction created/destroyed: 0x%X\n", ptr);
|
|
|
|
}
|
2013-01-02 16:30:15 -07:00
|
|
|
|
|
|
|
void syndrome(color_ostream& out, void* ptr) {
|
|
|
|
EventManager::SyndromeData* data = (EventManager::SyndromeData*)ptr;
|
|
|
|
out.print("Syndrome started: unit %d, syndrome %d.\n", data->unitId, data->syndromeIndex);
|
|
|
|
}
|
|
|
|
|
2013-01-03 13:52:56 -07:00
|
|
|
void invasion(color_ostream& out, void* ptr) {
|
|
|
|
out.print("New invasion! %d\n", (int32_t)ptr);
|
|
|
|
}
|
|
|
|
|