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 "DataDefs.h"
|
2014-06-28 00:31:34 -06:00
|
|
|
#include "VTableInterpose.h"
|
|
|
|
|
|
|
|
#include "modules/EventManager.h"
|
2012-12-14 19:05:38 -07:00
|
|
|
|
2014-06-28 00:31:34 -06:00
|
|
|
#include "df/body_part_raw.h"
|
|
|
|
#include "df/caste_body_info.h"
|
2013-10-20 12:14:02 -06:00
|
|
|
#include "df/construction.h"
|
|
|
|
#include "df/coord.h"
|
2012-12-15 14:49:13 -07:00
|
|
|
#include "df/item.h"
|
2014-06-28 00:31:34 -06:00
|
|
|
#include "df/item_actual.h"
|
2013-05-25 07:44:17 -06:00
|
|
|
#include "df/job.h"
|
2014-06-28 00:31:34 -06:00
|
|
|
#include "df/unit.h"
|
|
|
|
#include "df/unit_wound.h"
|
2012-12-15 14:49:13 -07:00
|
|
|
#include "df/world.h"
|
|
|
|
|
2012-12-15 15:43:41 -07:00
|
|
|
#include <vector>
|
2014-06-28 00:31:34 -06:00
|
|
|
#include <set>
|
2012-12-15 15:43:41 -07:00
|
|
|
|
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);
|
2014-06-28 00:31:34 -06:00
|
|
|
void unitAttack(color_ostream& out, void* ptr);
|
|
|
|
|
|
|
|
//bool interposed = false;
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-28 00:31:34 -06:00
|
|
|
/*
|
|
|
|
//df::item::contaminateWound(df::unit*,df::unit_wound*,uint8_t,int16_t);
|
|
|
|
struct my_contaminate : df::item_actual {
|
|
|
|
typedef df::item_actual interpose_base;
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, contaminateWound, (df::unit* unit, df::unit_wound* wound, uint8_t unk1, int16_t unk2)) {
|
|
|
|
INTERPOSE_NEXT(contaminateWound)(unit,wound,unk1,unk2);
|
|
|
|
CoreSuspendClaimer suspend;
|
|
|
|
Core::getInstance().print("contaminateWound: item=%d, unit=%d, wound attacker = %d, unk1 = %d, unk2 = %d\n", this->id, unit->id, wound->unit_id, (int32_t)unk1, (int32_t)unk2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(my_contaminate, contaminateWound);
|
|
|
|
*/
|
|
|
|
|
2012-12-15 15:43:41 -07:00
|
|
|
command_result eventExample(color_ostream& out, vector<string>& parameters) {
|
2014-06-28 00:31:34 -06:00
|
|
|
/*
|
|
|
|
if ( !interposed ) {
|
|
|
|
interposed = true;
|
|
|
|
if ( !INTERPOSE_HOOK(my_contaminate, contaminateWound).apply() ) {
|
|
|
|
out.print("Error: could not interpose contaminateWound.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2013-05-25 07:44:17 -06:00
|
|
|
EventManager::EventHandler initiateHandler(jobInitiated, 1);
|
|
|
|
EventManager::EventHandler completeHandler(jobCompleted, 0);
|
2013-01-03 17:31:29 -07:00
|
|
|
EventManager::EventHandler timeHandler(timePassed, 1);
|
|
|
|
EventManager::EventHandler deathHandler(unitDeath, 500);
|
2013-05-25 07:44:17 -06:00
|
|
|
EventManager::EventHandler itemHandler(itemCreate, 1);
|
2013-01-03 17:31:29 -07:00
|
|
|
EventManager::EventHandler buildingHandler(building, 500);
|
|
|
|
EventManager::EventHandler constructionHandler(construction, 100);
|
|
|
|
EventManager::EventHandler syndromeHandler(syndrome, 1);
|
|
|
|
EventManager::EventHandler invasionHandler(invasion, 1000);
|
2014-06-28 00:31:34 -06:00
|
|
|
EventManager::EventHandler unitAttackHandler(unitAttack, 1);
|
2013-10-20 20:49:22 -06:00
|
|
|
EventManager::unregisterAll(plugin_self);
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2013-10-20 20:49:22 -06:00
|
|
|
EventManager::registerListener(EventManager::EventType::JOB_INITIATED, initiateHandler, plugin_self);
|
|
|
|
EventManager::registerListener(EventManager::EventType::JOB_COMPLETED, completeHandler, plugin_self);
|
|
|
|
EventManager::registerListener(EventManager::EventType::UNIT_DEATH, deathHandler, plugin_self);
|
|
|
|
EventManager::registerListener(EventManager::EventType::ITEM_CREATED, itemHandler, plugin_self);
|
|
|
|
EventManager::registerListener(EventManager::EventType::BUILDING, buildingHandler, plugin_self);
|
|
|
|
EventManager::registerListener(EventManager::EventType::CONSTRUCTION, constructionHandler, plugin_self);
|
|
|
|
EventManager::registerListener(EventManager::EventType::SYNDROME, syndromeHandler, plugin_self);
|
|
|
|
EventManager::registerListener(EventManager::EventType::INVASION, invasionHandler, plugin_self);
|
2014-06-28 00:31:34 -06:00
|
|
|
EventManager::registerListener(EventManager::EventType::UNIT_ATTACK, unitAttackHandler, plugin_self);
|
2013-10-20 20:49:22 -06:00
|
|
|
EventManager::registerTick(timeHandler, 1, plugin_self);
|
|
|
|
EventManager::registerTick(timeHandler, 2, plugin_self);
|
|
|
|
EventManager::registerTick(timeHandler, 4, plugin_self);
|
|
|
|
EventManager::registerTick(timeHandler, 8, plugin_self);
|
|
|
|
int32_t t = EventManager::registerTick(timeHandler, 16, plugin_self);
|
|
|
|
timeHandler.freq = t;
|
|
|
|
EventManager::unregister(EventManager::EventType::TICK, timeHandler, plugin_self);
|
|
|
|
t = EventManager::registerTick(timeHandler, 32, plugin_self);
|
|
|
|
t = EventManager::registerTick(timeHandler, 32, plugin_self);
|
|
|
|
t = EventManager::registerTick(timeHandler, 32, plugin_self);
|
|
|
|
timeHandler.freq = t;
|
|
|
|
EventManager::unregister(EventManager::EventType::TICK, timeHandler, plugin_self);
|
|
|
|
EventManager::unregister(EventManager::EventType::TICK, timeHandler, plugin_self);
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-12-15 15:43:41 -07:00
|
|
|
out.print("Events registered.\n");
|
2012-12-14 19:05:38 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-25 07:44:17 -06:00
|
|
|
//static int timerCount=0;
|
|
|
|
//static int timerDenom=0;
|
|
|
|
void jobInitiated(color_ostream& out, void* job_) {
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("Job initiated! %p\n", job_);
|
2013-05-25 07:44:17 -06:00
|
|
|
/*
|
|
|
|
df::job* job = (df::job*)job_;
|
|
|
|
out.print(" completion_timer = %d\n", job->completion_timer);
|
|
|
|
if ( job->completion_timer != -1 ) timerCount++;
|
|
|
|
timerDenom++;
|
|
|
|
out.print(" frac = %d / %d\n", timerCount, timerDenom);
|
|
|
|
*/
|
2012-12-14 19:05:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void jobCompleted(color_ostream& out, void* job) {
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("Job completed! %p\n", job);
|
2012-12-14 19:05:38 -07:00
|
|
|
}
|
|
|
|
|
2012-12-14 21:29:28 -07:00
|
|
|
void timePassed(color_ostream& out, void* ptr) {
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("Time: %zi\n", (intptr_t)(ptr));
|
2012-12-14 21:29:28 -07:00
|
|
|
}
|
2012-12-15 12:40:11 -07:00
|
|
|
|
|
|
|
void unitDeath(color_ostream& out, void* ptr) {
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("Death: %zi\n", (intptr_t)(ptr));
|
2012-12-15 12:40:11 -07:00
|
|
|
}
|
2012-12-15 14:49:13 -07:00
|
|
|
|
|
|
|
void itemCreate(color_ostream& out, void* ptr) {
|
2016-07-28 09:17:55 -06:00
|
|
|
int32_t item_index = df::item::binsearch_index(df::global::world->items.all, (intptr_t)ptr);
|
2012-12-15 14:49:13 -07:00
|
|
|
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;
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("Item created: %zi, %s, at (%d,%d,%d)\n", (intptr_t)(ptr), ENUM_KEY_STR(item_type, type).c_str(), pos.x, pos.y, pos.z);
|
2012-12-15 14:49:13 -07:00
|
|
|
}
|
|
|
|
|
2012-12-18 16:34:38 -07:00
|
|
|
void building(color_ostream& out, void* ptr) {
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("Building created/destroyed: %zi\n", (intptr_t)ptr);
|
2012-12-18 16:34:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void construction(color_ostream& out, void* ptr) {
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("Construction created/destroyed: %p\n", ptr);
|
2013-10-20 12:14:02 -06:00
|
|
|
df::construction* constr = (df::construction*)ptr;
|
|
|
|
df::coord pos = constr->pos;
|
|
|
|
out.print(" (%d,%d,%d)\n", pos.x, pos.y, pos.z);
|
|
|
|
if ( df::construction::find(pos) == NULL )
|
|
|
|
out.print(" construction destroyed\n");
|
|
|
|
else
|
|
|
|
out.print(" construction created\n");
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-12-18 16:34:38 -07:00
|
|
|
}
|
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) {
|
2018-06-20 10:03:14 -06:00
|
|
|
out.print("New invasion! %zi\n", (intptr_t)ptr);
|
2013-01-03 13:52:56 -07:00
|
|
|
}
|
|
|
|
|
2014-06-28 00:31:34 -06:00
|
|
|
void unitAttack(color_ostream& out, void* ptr) {
|
|
|
|
EventManager::UnitAttackData* data = (EventManager::UnitAttackData*)ptr;
|
|
|
|
out.print("unit %d attacks unit %d\n", data->attacker, data->defender);
|
|
|
|
df::unit* defender = df::unit::find(data->defender);
|
2019-10-05 19:05:26 -06:00
|
|
|
if (!defender) {
|
|
|
|
out.printerr("defender %d does not exist\n", data->defender);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-28 00:31:34 -06:00
|
|
|
int32_t woundIndex = df::unit_wound::binsearch_index(defender->body.wounds, data->wound);
|
2019-10-05 19:05:26 -06:00
|
|
|
df::unit_wound* wound = vector_get(defender->body.wounds, woundIndex);
|
|
|
|
if (!wound) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-28 00:31:34 -06:00
|
|
|
set<int32_t> parts;
|
|
|
|
for ( auto a = wound->parts.begin(); a != wound->parts.end(); a++ ) {
|
|
|
|
parts.insert((*a)->body_part_id);
|
|
|
|
}
|
|
|
|
for ( auto a = parts.begin(); a != parts.end(); a++ ) {
|
|
|
|
int32_t body_part_id = (*a);
|
|
|
|
df::body_part_raw* part = defender->body.body_plan->body_parts[body_part_id];
|
|
|
|
out.print(" %s\n", part->name_singular[0]->c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|