#include "Core.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "df/graphic.h" #include "df/building_siegeenginest.h" #include "df/builtin_mats.h" #include "df/world.h" #include "df/buildings_other_id.h" #include "df/job.h" #include "df/building_drawbuffer.h" #include "df/ui.h" #include "df/viewscreen_dwarfmodest.h" #include "df/ui_build_selector.h" #include "df/flow_info.h" #include "df/report.h" #include "df/proj_itemst.h" #include "df/unit.h" #include "df/unit_soul.h" #include "df/unit_skill.h" #include "df/physical_attribute_type.h" #include "df/creature_raw.h" #include "df/caste_raw.h" #include "df/caste_raw_flags.h" #include "df/assumed_identity.h" #include "df/game_mode.h" #include "df/unit_misc_trait.h" #include "MiscUtils.h" using std::vector; using std::string; using std::stack; using namespace DFHack; using namespace df::enums; using df::global::gamemode; using df::global::gps; using df::global::world; using df::global::ui; using df::global::ui_build_selector; using Screen::Pen; DFHACK_PLUGIN("siege-engine"); /* * Misc. utils */ typedef std::pair coord_range; static void set_range(coord_range *target, df::coord p1, df::coord p2) { if (!p1.isValid() || !p2.isValid()) { *target = coord_range(); } else { target->first.x = std::min(p1.x, p2.x); target->first.y = std::min(p1.y, p2.y); target->first.z = std::min(p1.z, p2.z); target->second.x = std::max(p1.x, p2.x); target->second.y = std::max(p1.y, p2.y); target->second.z = std::max(p1.z, p2.z); } } static bool is_range_valid(const coord_range &target) { return target.first.isValid() && target.second.isValid(); } static bool is_in_range(const coord_range &target, df::coord pos) { return target.first.isValid() && target.second.isValid() && target.first.x <= pos.x && pos.x <= target.second.x && target.first.y <= pos.y && pos.y <= target.second.y && target.first.z <= pos.z && pos.z <= target.second.z; } static std::pair get_engine_range(df::building_siegeenginest *bld) { if (bld->type == siegeengine_type::Ballista) return std::make_pair(0, 200); else return std::make_pair(30, 100); } static void orient_engine(df::building_siegeenginest *bld, df::coord target) { int dx = target.x - bld->centerx; int dy = target.y - bld->centery; if (abs(dx) > abs(dy)) bld->facing = (dx > 0) ? df::building_siegeenginest::Right : df::building_siegeenginest::Left; else bld->facing = (dy > 0) ? df::building_siegeenginest::Down : df::building_siegeenginest::Up; } static int random_int(int val) { return int(int64_t(rand())*val/RAND_MAX); } static int point_distance(df::coord speed) { return std::max(abs(speed.x), std::max(abs(speed.y), abs(speed.z))); } /* * Configuration management */ static bool enable_plugin(); struct EngineInfo { int id; df::building_siegeenginest *bld; df::coord center; coord_range building_rect; bool is_catapult; int proj_speed, hit_delay; std::pair fire_range; coord_range target; bool hasTarget() { return is_range_valid(target); } bool onTarget(df::coord pos) { return is_in_range(target, pos); } df::coord getTargetSize() { return target.second - target.first; } bool isInRange(int dist) { return dist >= fire_range.first && dist <= fire_range.second; } }; static std::map engines; static std::map coord_engines; static EngineInfo *find_engine(df::building *bld, bool create = false) { auto ebld = strict_virtual_cast(bld); if (!ebld) return NULL; auto it = engines.find(bld); if (it != engines.end()) { it->second.bld = ebld; return &it->second; } if (!create) return NULL; auto *obj = &engines[bld]; obj->id = bld->id; obj->bld = ebld; obj->center = df::coord(bld->centerx, bld->centery, bld->z); obj->building_rect = coord_range( df::coord(bld->x1, bld->y1, bld->z), df::coord(bld->x2, bld->y2, bld->z) ); obj->is_catapult = (ebld->type == siegeengine_type::Catapult); obj->proj_speed = 2; obj->hit_delay = 3; obj->fire_range = get_engine_range(ebld); coord_engines[obj->center] = bld; return obj; } static EngineInfo *find_engine(lua_State *L, int idx, bool create = false) { auto bld = Lua::CheckDFObject(L, idx); auto engine = find_engine(bld); if (!engine) luaL_error(L, "no such engine"); return engine; } static EngineInfo *find_engine(df::coord pos) { auto engine = find_engine(coord_engines[pos]); if (engine) { auto bld0 = df::building::find(engine->id); auto bld = strict_virtual_cast(bld0); if (!bld) return NULL; engine->bld = bld; } return engine; } static void clear_engines() { engines.clear(); coord_engines.clear(); } static void load_engines() { clear_engines(); auto pworld = Core::getInstance().getWorld(); std::vector vec; pworld->GetPersistentData(&vec, "siege-engine/target/", true); for (auto it = vec.begin(); it != vec.end(); ++it) { auto engine = find_engine(df::building::find(it->ival(0)), true); if (!engine) continue; engine->target.first = df::coord(it->ival(1), it->ival(2), it->ival(3)); engine->target.second = df::coord(it->ival(4), it->ival(5), it->ival(6)); } } static int getTargetArea(lua_State *L) { auto bld = Lua::CheckDFObject(L, 1); if (!bld) luaL_argerror(L, 1, "null building"); auto engine = find_engine(bld); if (engine && engine->hasTarget()) { Lua::Push(L, engine->target.first); Lua::Push(L, engine->target.second); } else { lua_pushnil(L); lua_pushnil(L); } return 2; } static void clearTargetArea(df::building_siegeenginest *bld) { CHECK_NULL_POINTER(bld); if (auto engine = find_engine(bld)) engine->target = coord_range(); auto pworld = Core::getInstance().getWorld(); auto key = stl_sprintf("siege-engine/target/%d", bld->id); pworld->DeletePersistentData(pworld->GetPersistentData(key)); } static bool setTargetArea(df::building_siegeenginest *bld, df::coord target_min, df::coord target_max) { CHECK_NULL_POINTER(bld); CHECK_INVALID_ARGUMENT(target_min.isValid() && target_max.isValid()); if (!enable_plugin()) return false; auto pworld = Core::getInstance().getWorld(); auto key = stl_sprintf("siege-engine/target/%d", bld->id); auto entry = pworld->GetPersistentData(key, NULL); if (!entry.isValid()) return false; auto engine = find_engine(bld, true); set_range(&engine->target, target_min, target_max); entry.ival(0) = bld->id; entry.ival(1) = engine->target.first.x; entry.ival(2) = engine->target.first.y; entry.ival(3) = engine->target.first.z; entry.ival(4) = engine->target.second.x; entry.ival(5) = engine->target.second.y; entry.ival(6) = engine->target.second.z; df::coord sum = target_min + target_max; orient_engine(bld, df::coord(sum.x/2, sum.y/2, sum.z/2)); return true; } static int getShotSkill(df::building_siegeenginest *bld) { CHECK_NULL_POINTER(bld); auto engine = find_engine(bld); if (!engine) return 0; auto &active = world->units.active; for (size_t i = 0; i < active.size(); i++) if (active[i]->pos == engine->center && Units::isCitizen(active[i])) return Units::getEffectiveSkill(active[i], job_skill::SIEGEOPERATE); return 0; } /* * Trajectory */ struct ProjectilePath { static const int DEFAULT_FUDGE = 31; df::coord origin, goal, target, fudge_delta; int divisor, fudge_factor; df::coord speed, direction; ProjectilePath(df::coord origin, df::coord goal) : origin(origin), goal(goal), fudge_factor(1) { fudge_delta = df::coord(0,0,0); calc_line(); } ProjectilePath(df::coord origin, df::coord goal, df::coord delta, int factor) : origin(origin), goal(goal), fudge_delta(delta), fudge_factor(factor) { calc_line(); } ProjectilePath(df::coord origin, df::coord goal, float zdelta, int factor = DEFAULT_FUDGE) : origin(origin), goal(goal), fudge_factor(factor) { fudge_delta = df::coord(0,0,int(factor * zdelta)); calc_line(); } void calc_line() { speed = goal - origin; speed.x *= fudge_factor; speed.y *= fudge_factor; speed.z *= fudge_factor; speed = speed + fudge_delta; target = origin + speed; divisor = point_distance(speed); if (divisor <= 0) divisor = 1; direction = df::coord(speed.x>=0?1:-1,speed.y>=0?1:-1,speed.z>=0?1:-1); } df::coord operator[] (int i) const { int div2 = divisor * 2; int bias = divisor-1; return origin + df::coord( (2*speed.x*i + direction.x*bias)/div2, (2*speed.y*i + direction.y*bias)/div2, (2*speed.z*i + direction.z*bias)/div2 ); } }; static ProjectilePath decode_path(lua_State *L, int idx, df::coord origin) { idx = lua_absindex(L, idx); Lua::StackUnwinder frame(L); df::coord goal; lua_getfield(L, idx, "target"); Lua::CheckDFAssign(L, &goal, frame[1]); lua_getfield(L, idx, "delta"); if (!lua_isnil(L, frame[2])) { lua_getfield(L, idx, "factor"); int factor = luaL_optnumber(L, frame[3], ProjectilePath::DEFAULT_FUDGE); if (lua_isnumber(L, frame[2])) return ProjectilePath(origin, goal, lua_tonumber(L, frame[2]), factor); df::coord delta; Lua::CheckDFAssign(L, &delta, frame[2]); return ProjectilePath(origin, goal, delta, factor); } return ProjectilePath(origin, goal); } static int projPosAtStep(lua_State *L) { auto engine = find_engine(L, 1); auto path = decode_path(L, 2, engine->center); int step = luaL_checkint(L, 3); Lua::Push(L, path[step]); return 1; } static bool isPassableTile(df::coord pos) { auto ptile = Maps::getTileType(pos); return !ptile || FlowPassable(*ptile); } static bool isTargetableTile(df::coord pos) { auto ptile = Maps::getTileType(pos); return ptile && FlowPassable(*ptile) && !isOpenTerrain(*ptile); } static bool isTreeTile(df::coord pos) { auto ptile = Maps::getTileType(pos); return ptile && tileShape(*ptile) == tiletype_shape::TREE; } static bool adjustToTarget(EngineInfo *engine, df::coord *pos) { if (isTargetableTile(*pos)) return true; for (df::coord fudge = *pos; fudge.z < engine->target.second.z; fudge.z++) { if (!isTargetableTile(fudge)) continue; *pos = fudge; return true; } for (df::coord fudge = *pos; fudge.z > engine->target.first.z; fudge.z--) { if (!isTargetableTile(fudge)) continue; *pos = fudge; return true; } return false; } static int adjustToTarget(lua_State *L) { auto engine = find_engine(L, 1, true); df::coord pos; Lua::CheckDFAssign(L, &pos, 2); bool ok = adjustToTarget(engine, &pos); Lua::Push(L, pos); Lua::Push(L, ok); return 2; } static const char* const hit_type_names[] = { "wall", "floor", "ceiling", "map_edge", "tree" }; struct PathMetrics { enum CollisionType { Impassable, Floor, Ceiling, MapEdge, Tree } hit_type; int collision_step, collision_z_step; int goal_step, goal_z_step, goal_distance; bool hits() const { return collision_step > goal_step; } PathMetrics(const ProjectilePath &path) { compute(path); } void compute(const ProjectilePath &path) { collision_step = goal_step = goal_z_step = 1000000; collision_z_step = 0; goal_distance = point_distance(path.origin - path.goal); int step = 0; df::coord prev_pos = path.origin; for (;;) { df::coord cur_pos = path[++step]; if (cur_pos == prev_pos) break; if (cur_pos.z == path.goal.z) { goal_z_step = std::min(step, goal_z_step); if (cur_pos == path.goal) goal_step = step; } if (!Maps::isValidTilePos(cur_pos)) { hit_type = PathMetrics::MapEdge; break; } if (!isPassableTile(cur_pos)) { if (isTreeTile(cur_pos)) { // The projectile code has a bug where it will // hit a tree on the same tick as a Z level change. if (cur_pos.z != prev_pos.z) { hit_type = Tree; break; } } else { hit_type = Impassable; break; } } if (cur_pos.z != prev_pos.z) { int top_z = std::max(prev_pos.z, cur_pos.z); auto ptile = Maps::getTileType(cur_pos.x, cur_pos.y, top_z); if (ptile && !LowPassable(*ptile)) { hit_type = (cur_pos.z > prev_pos.z ? Ceiling : Floor); break; } collision_z_step = step; } prev_pos = cur_pos; } collision_step = step; } }; enum TargetTileStatus { TARGET_OK, TARGET_RANGE, TARGET_BLOCKED, TARGET_SEMIBLOCKED }; static const char* const target_tile_type_names[] = { "ok", "out_of_range", "blocked", "semi_blocked" }; static TargetTileStatus calcTileStatus(EngineInfo *engine, const PathMetrics &raytrace) { if (raytrace.hits()) { if (engine->isInRange(raytrace.goal_step)) return TARGET_OK; else return TARGET_RANGE; } else return TARGET_BLOCKED; } static int projPathMetrics(lua_State *L) { auto engine = find_engine(L, 1); auto path = decode_path(L, 2, engine->center); PathMetrics info(path); lua_createtable(L, 0, 7); Lua::SetField(L, hit_type_names[info.hit_type], -1, "hit_type"); Lua::SetField(L, info.collision_step, -1, "collision_step"); Lua::SetField(L, info.collision_z_step, -1, "collision_z_step"); Lua::SetField(L, info.goal_distance, -1, "goal_distance"); if (info.goal_step < info.collision_step) Lua::SetField(L, info.goal_step, -1, "goal_step"); if (info.goal_z_step < info.collision_step) Lua::SetField(L, info.goal_z_step, -1, "goal_z_step"); Lua::SetField(L, target_tile_type_names[calcTileStatus(engine, info)], -1, "status"); return 1; } static TargetTileStatus calcTileStatus(EngineInfo *engine, df::coord target, float zdelta) { ProjectilePath path(engine->center, target, zdelta); PathMetrics raytrace(path); return calcTileStatus(engine, raytrace); } static TargetTileStatus calcTileStatus(EngineInfo *engine, df::coord target) { auto status = calcTileStatus(engine, target, 0.0f); if (status == TARGET_BLOCKED) { if (calcTileStatus(engine, target, 0.5f) < TARGET_BLOCKED) return TARGET_SEMIBLOCKED; if (calcTileStatus(engine, target, -0.5f) < TARGET_BLOCKED) return TARGET_SEMIBLOCKED; } return status; } static std::string getTileStatus(df::building_siegeenginest *bld, df::coord tile_pos) { auto engine = find_engine(bld, true); if (!engine) return "invalid"; return target_tile_type_names[calcTileStatus(engine, tile_pos)]; } static void paintAimScreen(df::building_siegeenginest *bld, df::coord view, df::coord2d ltop, df::coord2d size) { auto engine = find_engine(bld, true); CHECK_NULL_POINTER(engine); for (int x = 0; x < size.x; x++) { for (int y = 0; y < size.y; y++) { df::coord tile_pos = view + df::coord(x,y,0); if (is_in_range(engine->building_rect, tile_pos)) continue; Pen cur_tile = Screen::readTile(ltop.x+x, ltop.y+y); if (!cur_tile.valid()) continue; int color; switch (calcTileStatus(engine, tile_pos)) { case TARGET_OK: color = COLOR_GREEN; break; case TARGET_RANGE: color = COLOR_CYAN; break; case TARGET_BLOCKED: color = COLOR_RED; break; case TARGET_SEMIBLOCKED: color = COLOR_BROWN; break; } if (cur_tile.fg && cur_tile.ch != ' ') { cur_tile.fg = color; cur_tile.bg = 0; } else { cur_tile.fg = 0; cur_tile.bg = color; } cur_tile.bold = engine->onTarget(tile_pos); if (cur_tile.tile) cur_tile.tile_mode = Pen::CharColor; Screen::paintTile(cur_tile, ltop.x+x, ltop.y+y); } } } /* * Unit tracking */ static const float MAX_TIME = 1000000.0f; struct UnitPath { df::unit *unit; std::map path; struct Hit { UnitPath *path; df::coord pos; int dist; float time, lmargin, rmargin; }; static std::map cache; static UnitPath *get(df::unit *unit) { auto &cv = cache[unit]; if (!cv) cv = new UnitPath(unit); return cv; }; UnitPath(df::unit *unit) : unit(unit) { if (unit->flags1.bits.rider) { auto mount = df::unit::find(unit->relations.rider_mount_id); if (mount) { path = get(mount)->path; return; } } df::coord pos = unit->pos; df::coord dest = unit->path.dest; auto &upath = unit->path.path; if (dest.isValid() && !upath.x.empty()) { float time = unit->counters.job_counter+0.5f; float speed = Units::computeMovementSpeed(unit)/100.0f; for (size_t i = 0; i < upath.size(); i++) { df::coord new_pos = upath[i]; if (new_pos == pos) continue; float delay = speed; if (new_pos.x != pos.x && new_pos.y != pos.y) delay *= 362.0/256.0; path[time] = pos; pos = new_pos; time += delay + 1; } } path[MAX_TIME] = pos; } void get_margin(std::map::iterator &it, float time, float *lmargin, float *rmargin) { auto it2 = it; *lmargin = (it == path.begin()) ? MAX_TIME : time - (--it2)->first; *rmargin = (it->first == MAX_TIME) ? MAX_TIME : it->first - time; } df::coord posAtTime(float time, float *lmargin = NULL, float *rmargin = NULL) { CHECK_INVALID_ARGUMENT(time < MAX_TIME); auto it = path.upper_bound(time); if (lmargin) get_margin(it, time, lmargin, rmargin); return it->second; } bool findHits(EngineInfo *engine, std::vector *hit_points, float bias) { df::coord origin = engine->center; Hit info; info.path = this; for (auto it = path.begin(); it != path.end(); ++it) { info.pos = it->second; info.dist = point_distance(origin - info.pos); info.time = float(info.dist)*(engine->proj_speed+1) + engine->hit_delay + bias; get_margin(it, info.time, &info.lmargin, &info.rmargin); if (info.lmargin > 0 && info.rmargin > 0) { if (engine->onTarget(info.pos) && engine->isInRange(info.dist)) hit_points->push_back(info); } } return !hit_points->empty(); } }; std::map UnitPath::cache; static void push_margin(lua_State *L, float margin) { if (margin == MAX_TIME) lua_pushnil(L); else lua_pushnumber(L, margin); } static int traceUnitPath(lua_State *L) { auto unit = Lua::CheckDFObject(L, 1); CHECK_NULL_POINTER(unit); size_t idx = 1; auto info = UnitPath::get(unit); lua_createtable(L, info->path.size(), 0); float last_time = 0.0f; for (auto it = info->path.begin(); it != info->path.end(); ++it) { Lua::Push(L, it->second); if (idx > 1) { lua_pushnumber(L, last_time); lua_setfield(L, -2, "from"); } if (idx < info->path.size()) { lua_pushnumber(L, it->first); lua_setfield(L, -2, "to"); } lua_rawseti(L, -2, idx++); last_time = it->first; } return 1; } static int unitPosAtTime(lua_State *L) { auto unit = Lua::CheckDFObject(L, 1); float time = luaL_checknumber(L, 2); CHECK_NULL_POINTER(unit); float lmargin, rmargin; auto info = UnitPath::get(unit); Lua::Push(L, info->posAtTime(time, &lmargin, &rmargin)); push_margin(L, lmargin); push_margin(L, rmargin); return 3; } static bool canTargetUnit(df::unit *unit) { CHECK_NULL_POINTER(unit); if (unit->flags1.bits.dead || unit->flags3.bits.ghostly || unit->flags1.bits.caged || unit->flags1.bits.hidden_in_ambush) return false; return true; } static void proposeUnitHits(EngineInfo *engine, std::vector *hits, float bias) { auto &active = world->units.active; for (size_t i = 0; i < active.size(); i++) { auto unit = active[i]; if (!canTargetUnit(unit)) continue; UnitPath::get(unit)->findHits(engine, hits, bias); } } static int proposeUnitHits(lua_State *L) { auto engine = find_engine(L, 1); float bias = luaL_optnumber(L, 2, 0); if (!engine->hasTarget()) luaL_error(L, "target not set"); std::vector hits; proposeUnitHits(engine, &hits, bias); lua_createtable(L, hits.size(), 0); for (size_t i = 0; i < hits.size(); i++) { auto &hit = hits[i]; lua_createtable(L, 0, 6); Lua::SetField(L, hit.path->unit, -1, "unit"); Lua::SetField(L, hit.pos, -1, "pos"); Lua::SetField(L, hit.dist, -1, "dist"); Lua::SetField(L, hit.time, -1, "time"); push_margin(L, hit.lmargin); lua_setfield(L, -2, "lmargin"); push_margin(L, hit.rmargin); lua_setfield(L, -2, "rmargin"); lua_rawseti(L, -2, i+1); } return 1; } /* * Projectile hook */ struct projectile_hook : df::proj_itemst { typedef df::proj_itemst interpose_base; void aimAtPoint(EngineInfo *engine, const ProjectilePath &path) { target_pos = path.target; PathMetrics raytrace(path); // Materialize map blocks, or the projectile will crash into them for (int i = 0; i < raytrace.collision_step; i++) Maps::ensureTileBlock(path[i]); // Find valid hit point for catapult stones if (flags.bits.high_flying) { if (raytrace.hits()) fall_threshold = raytrace.goal_step; else fall_threshold = (raytrace.collision_z_step+raytrace.collision_step-1)/2; while (fall_threshold < raytrace.collision_step-1) { if (isTargetableTile(path[fall_threshold])) break; fall_threshold++; } } fall_threshold = std::max(fall_threshold, engine->fire_range.first); fall_threshold = std::min(fall_threshold, engine->fire_range.second); } void aimAtArea(EngineInfo *engine) { df::coord target, last_passable; df::coord tbase = engine->target.first; df::coord tsize = engine->getTargetSize(); bool success = false; for (int i = 0; i < 50; i++) { target = tbase + df::coord( random_int(tsize.x), random_int(tsize.y), random_int(tsize.z) ); if (adjustToTarget(engine, &target)) last_passable = target; else continue; ProjectilePath path(engine->center, target, engine->is_catapult ? 0.5f : 0.0f); PathMetrics raytrace(path); if (raytrace.hits() && engine->isInRange(raytrace.goal_step)) { aimAtPoint(engine, path); return; } } if (!last_passable.isValid()) last_passable = target; aimAtPoint(engine, ProjectilePath(engine->center, last_passable)); } static int safeAimProjectile(lua_State *L) { color_ostream &out = *Lua::GetOutput(L); auto proj = (projectile_hook*)lua_touserdata(L, 1); auto engine = (EngineInfo*)lua_touserdata(L, 2); if (!Lua::PushModulePublic(out, L, "plugins.siege-engine", "doAimProjectile")) luaL_error(L, "Projectile aiming AI not available"); Lua::PushDFObject(L, engine->bld); Lua::Push(L, engine->target.first); Lua::Push(L, engine->target.second); lua_call(L, 3, 1); if (lua_isnil(L, -1)) proj->aimAtArea(engine); else proj->aimAtPoint(engine, decode_path(L, -1, engine->center)); return 0; } void doCheckMovement() { if (distance_flown != 0 || fall_counter != fall_delay) return; auto engine = find_engine(origin_pos); if (!engine || !engine->hasTarget()) return; auto L = Lua::Core::State; CoreSuspendClaimer suspend; color_ostream_proxy out(Core::getInstance().getConsole()); lua_pushcfunction(L, safeAimProjectile); lua_pushlightuserdata(L, this); lua_pushlightuserdata(L, engine); if (!Lua::Core::SafeCall(out, 2, 0)) aimAtArea(engine); } DEFINE_VMETHOD_INTERPOSE(bool, checkMovement, ()) { if (flags.bits.high_flying || flags.bits.piercing) doCheckMovement(); return INTERPOSE_NEXT(checkMovement)(); } }; IMPLEMENT_VMETHOD_INTERPOSE(projectile_hook, checkMovement); /* * Initialization */ DFHACK_PLUGIN_LUA_FUNCTIONS { DFHACK_LUA_FUNCTION(clearTargetArea), DFHACK_LUA_FUNCTION(setTargetArea), DFHACK_LUA_FUNCTION(getTileStatus), DFHACK_LUA_FUNCTION(paintAimScreen), DFHACK_LUA_FUNCTION(canTargetUnit), DFHACK_LUA_FUNCTION(isPassableTile), DFHACK_LUA_FUNCTION(isTreeTile), DFHACK_LUA_FUNCTION(isTargetableTile), DFHACK_LUA_END }; DFHACK_PLUGIN_LUA_COMMANDS { DFHACK_LUA_COMMAND(getTargetArea), DFHACK_LUA_COMMAND(projPosAtStep), DFHACK_LUA_COMMAND(projPathMetrics), DFHACK_LUA_COMMAND(adjustToTarget), DFHACK_LUA_COMMAND(traceUnitPath), DFHACK_LUA_COMMAND(unitPosAtTime), DFHACK_LUA_COMMAND(proposeUnitHits), DFHACK_LUA_END }; static bool is_enabled = false; static void enable_hooks(bool enable) { is_enabled = enable; INTERPOSE_HOOK(projectile_hook, checkMovement).apply(enable); if (enable) load_engines(); else clear_engines(); } static bool enable_plugin() { if (is_enabled) return true; auto pworld = Core::getInstance().getWorld(); auto entry = pworld->GetPersistentData("siege-engine/enabled", NULL); if (!entry.isValid()) return false; enable_hooks(true); return true; } static void clear_caches() { if (!UnitPath::cache.empty()) { for (auto it = UnitPath::cache.begin(); it != UnitPath::cache.end(); ++it) delete it->second; UnitPath::cache.clear(); } } DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { switch (event) { case SC_MAP_LOADED: { auto pworld = Core::getInstance().getWorld(); bool enable = pworld->GetPersistentData("siege-engine/enabled").isValid(); if (enable) { out.print("Enabling the siege engine plugin.\n"); enable_hooks(true); } else enable_hooks(false); } break; case SC_MAP_UNLOADED: enable_hooks(false); break; default: break; } return CR_OK; } DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) { if (Core::getInstance().isMapLoaded()) plugin_onstatechange(out, SC_MAP_LOADED); return CR_OK; } DFhackCExport command_result plugin_shutdown ( color_ostream &out ) { enable_hooks(false); return CR_OK; } DFhackCExport command_result plugin_onupdate ( color_ostream &out ) { clear_caches(); return CR_OK; }