2022-11-06 01:12:35 -06:00
|
|
|
#pragma once
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "channel-manager.h"
|
|
|
|
|
2022-12-08 12:21:17 -07:00
|
|
|
#include <TileTypes.h>
|
|
|
|
#include <LuaTools.h>
|
|
|
|
#include <LuaWrapper.h>
|
2022-11-06 01:12:35 -06:00
|
|
|
#include <modules/Maps.h>
|
|
|
|
#include <df/job.h>
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
#include <unordered_set>
|
2022-12-08 15:59:09 -07:00
|
|
|
#include <random>
|
2022-11-06 01:12:35 -06:00
|
|
|
|
2022-12-08 15:59:09 -07:00
|
|
|
#define Coord(id) (id).x][(id).y
|
2022-12-08 12:21:17 -07:00
|
|
|
#define COORD "%" PRIi16 ",%" PRIi16 ",%" PRIi16
|
2022-12-08 15:59:09 -07:00
|
|
|
#define COORDARGS(id) (id).x, (id).y, (id).z
|
2022-11-06 01:12:35 -06:00
|
|
|
|
|
|
|
namespace CSP {
|
|
|
|
extern std::unordered_set<df::coord> dignow_queue;
|
|
|
|
}
|
|
|
|
|
2022-12-08 12:21:17 -07:00
|
|
|
inline uint32_t calc_distance(df::coord p1, df::coord p2) {
|
|
|
|
// calculate chebyshev (chessboard) distance
|
|
|
|
uint32_t distance = abs(p2.z - p1.z);
|
2023-06-25 18:44:06 -06:00
|
|
|
distance += std::max(abs(p2.x - p1.x), abs(p2.y - p1.y));
|
2022-12-08 12:21:17 -07:00
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void get_connected_neighbours(const df::coord &map_pos, df::coord(&neighbours)[4]) {
|
|
|
|
neighbours[0] = map_pos;
|
|
|
|
neighbours[1] = map_pos;
|
|
|
|
neighbours[2] = map_pos;
|
|
|
|
neighbours[3] = map_pos;
|
|
|
|
neighbours[0].y--;
|
|
|
|
neighbours[1].x--;
|
|
|
|
neighbours[2].x++;
|
|
|
|
neighbours[3].y++;
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:16:27 -07:00
|
|
|
inline void get_neighbours(const df::coord &map_pos, df::coord(&neighbours)[8]) {
|
|
|
|
neighbours[0] = map_pos;
|
|
|
|
neighbours[1] = map_pos;
|
|
|
|
neighbours[2] = map_pos;
|
|
|
|
neighbours[3] = map_pos;
|
|
|
|
neighbours[4] = map_pos;
|
|
|
|
neighbours[5] = map_pos;
|
|
|
|
neighbours[6] = map_pos;
|
|
|
|
neighbours[7] = map_pos;
|
|
|
|
neighbours[0].x--; neighbours[0].y--;
|
|
|
|
neighbours[1].y--;
|
|
|
|
neighbours[2].x++; neighbours[2].y--;
|
|
|
|
neighbours[3].x--;
|
|
|
|
neighbours[4].x++;
|
|
|
|
neighbours[5].x--; neighbours[5].y++;
|
|
|
|
neighbours[6].y++;
|
|
|
|
neighbours[7].x++; neighbours[7].y++;
|
|
|
|
}
|
|
|
|
|
2022-12-08 12:21:17 -07:00
|
|
|
inline uint8_t count_accessibility(const df::coord &unit_pos, const df::coord &map_pos) {
|
|
|
|
df::coord neighbours[8];
|
|
|
|
df::coord connections[4];
|
|
|
|
get_neighbours(map_pos, neighbours);
|
|
|
|
get_connected_neighbours(map_pos, connections);
|
|
|
|
uint8_t accessibility = Maps::canWalkBetween(unit_pos, map_pos) ? 1 : 0;
|
|
|
|
for (auto n: neighbours) {
|
2023-02-24 16:51:11 -07:00
|
|
|
if unlikely(!Maps::isValidTilePos(n)) continue;
|
2022-12-08 12:21:17 -07:00
|
|
|
if (Maps::canWalkBetween(unit_pos, n)) {
|
|
|
|
accessibility++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto n : connections) {
|
2023-02-24 16:51:11 -07:00
|
|
|
if unlikely(Maps::isValidTilePos(n)) continue;
|
2022-12-08 12:21:17 -07:00
|
|
|
if (Maps::canWalkBetween(unit_pos, n)) {
|
|
|
|
accessibility++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return accessibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isEntombed(const df::coord &unit_pos, const df::coord &map_pos) {
|
2023-02-24 16:51:11 -07:00
|
|
|
if likely(Maps::canWalkBetween(unit_pos, map_pos)) {
|
2022-12-08 12:21:17 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
df::coord neighbours[8];
|
|
|
|
get_neighbours(map_pos, neighbours);
|
2022-12-08 15:59:09 -07:00
|
|
|
return std::all_of(neighbours+0, neighbours+8, [&unit_pos](df::coord n) {
|
2023-02-24 16:51:11 -07:00
|
|
|
return !Maps::isValidTilePos(n) || !Maps::canWalkBetween(unit_pos, n);
|
2022-12-08 15:59:09 -07:00
|
|
|
});
|
2022-12-08 12:21:17 -07:00
|
|
|
}
|
|
|
|
|
2022-11-06 01:12:35 -06:00
|
|
|
inline bool is_dig_job(const df::job* job) {
|
2023-02-24 16:51:11 -07:00
|
|
|
return job && (job->job_type == df::job_type::Dig || job->job_type == df::job_type::DigChannel);
|
2022-11-06 01:12:35 -06:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:16:27 -07:00
|
|
|
inline bool is_channel_job(const df::job* job) {
|
2023-02-24 16:51:11 -07:00
|
|
|
return job && (job->job_type == df::job_type::DigChannel);
|
2022-11-06 14:16:27 -07:00
|
|
|
}
|
|
|
|
|
2022-11-06 16:59:30 -07:00
|
|
|
inline bool is_group_job(const ChannelGroups &groups, const df::job* job) {
|
|
|
|
return groups.count(job->pos);
|
|
|
|
}
|
|
|
|
|
2022-11-06 01:12:35 -06:00
|
|
|
inline bool is_dig_designation(const df::tile_designation &designation) {
|
|
|
|
return designation.bits.dig != df::tile_dig_designation::No;
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:16:27 -07:00
|
|
|
inline bool is_channel_designation(const df::tile_designation &designation) {
|
2022-12-01 17:36:32 -07:00
|
|
|
return designation.bits.dig == df::tile_dig_designation::Channel;
|
2022-11-06 01:12:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_safe_fall(const df::coord &map_pos) {
|
|
|
|
df::coord below(map_pos);
|
|
|
|
for (uint8_t zi = 0; zi < config.fall_threshold; ++zi) {
|
|
|
|
below.z--;
|
2023-02-24 16:51:11 -07:00
|
|
|
// falling out of bounds is probably considerably unsafe for a dwarf
|
|
|
|
if unlikely(!Maps::isValidTilePos(below)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// if we require vision, and we can't see below.. we'll need to assume it's safe to get anything done
|
2022-11-06 01:12:35 -06:00
|
|
|
if (config.require_vision && Maps::getTileDesignation(below)->bits.hidden) {
|
2023-02-24 16:51:11 -07:00
|
|
|
return true;
|
2022-11-06 01:12:35 -06:00
|
|
|
}
|
2023-02-24 16:51:11 -07:00
|
|
|
// finally, if we're not looking at open space (air to fall through) it's safe to fall to
|
2022-11-06 01:12:35 -06:00
|
|
|
df::tiletype type = *Maps::getTileType(below);
|
2022-11-21 13:39:26 -07:00
|
|
|
if (!DFHack::isOpenTerrain(type)) {
|
2022-11-06 01:12:35 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 16:51:11 -07:00
|
|
|
// we exceeded the fall threshold, so it's not a safe fall
|
2022-11-06 01:12:35 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_safe_to_dig_down(const df::coord &map_pos) {
|
|
|
|
df::coord pos(map_pos);
|
|
|
|
|
2023-02-24 16:51:11 -07:00
|
|
|
// todo: probably should rely on is_safe_fall, it looks like it could be simplified a great deal
|
2022-11-06 01:12:35 -06:00
|
|
|
for (uint8_t zi = 0; zi <= config.fall_threshold; ++zi) {
|
2023-02-24 16:51:11 -07:00
|
|
|
// if we're digging out of bounds, the game can handle that (hopefully)
|
|
|
|
if unlikely(!Maps::isValidTilePos(pos)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// if we require vision, and we can't see the tiles in question.. we'll need to assume it's safe to dig to get anything done
|
2022-11-06 01:12:35 -06:00
|
|
|
if (config.require_vision && Maps::getTileDesignation(pos)->bits.hidden) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-24 16:51:11 -07:00
|
|
|
|
2022-11-06 01:12:35 -06:00
|
|
|
df::tiletype type = *Maps::getTileType(pos);
|
2022-11-21 13:39:26 -07:00
|
|
|
if (zi == 0 && DFHack::isOpenTerrain(type)) {
|
2023-02-24 16:51:11 -07:00
|
|
|
// todo: remove? this is probably not useful.. and seems like the only considerable difference to is_safe_fall (aside from where each stops looking)
|
2022-11-06 01:12:35 -06:00
|
|
|
// the starting tile is open space, that's obviously not safe
|
|
|
|
return false;
|
2022-11-21 13:39:26 -07:00
|
|
|
} else if (!DFHack::isOpenTerrain(type)) {
|
2022-11-06 01:12:35 -06:00
|
|
|
// a tile after the first one is not open space
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-24 16:51:11 -07:00
|
|
|
pos.z--; // todo: this can probably move to the beginning of the loop
|
2022-11-06 01:12:35 -06:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:16:27 -07:00
|
|
|
inline bool has_unit(const df::tile_occupancy* occupancy) {
|
|
|
|
return occupancy->bits.unit || occupancy->bits.unit_grounded;
|
2022-11-06 01:12:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool has_group_above(const ChannelGroups &groups, const df::coord &map_pos) {
|
|
|
|
df::coord above(map_pos);
|
|
|
|
above.z++;
|
|
|
|
if (groups.count(above)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool has_any_groups_above(const ChannelGroups &groups, const Group &group) {
|
|
|
|
// for each designation in the group
|
|
|
|
for (auto &pos : group) {
|
|
|
|
df::coord above(pos);
|
|
|
|
above.z++;
|
|
|
|
if (groups.count(above)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there are no incomplete groups above this group, then this group is ready
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void cancel_job(df::job* job) {
|
2022-12-08 12:21:17 -07:00
|
|
|
if (job) {
|
|
|
|
const df::coord &pos = job->pos;
|
2022-11-06 01:12:35 -06:00
|
|
|
df::map_block* job_block = Maps::getTileBlock(pos);
|
|
|
|
uint16_t x, y;
|
|
|
|
x = pos.x % 16;
|
|
|
|
y = pos.y % 16;
|
|
|
|
df::tile_designation &designation = job_block->designation[x][y];
|
2022-11-21 13:39:26 -07:00
|
|
|
auto type = job->job_type;
|
2022-12-08 12:21:17 -07:00
|
|
|
ChannelManager::Get().jobs.erase(pos);
|
|
|
|
Job::removeWorker(job);
|
|
|
|
Job::removePostings(job, true);
|
2022-11-21 13:39:26 -07:00
|
|
|
Job::removeJob(job);
|
2022-12-08 12:21:17 -07:00
|
|
|
job_block->flags.bits.designated = true;
|
|
|
|
job_block->occupancy[x][y].bits.dig_marked = true;
|
2022-11-21 13:39:26 -07:00
|
|
|
switch (type) {
|
2022-11-06 01:12:35 -06:00
|
|
|
case job_type::Dig:
|
|
|
|
designation.bits.dig = df::tile_dig_designation::Default;
|
|
|
|
break;
|
|
|
|
case job_type::CarveUpwardStaircase:
|
|
|
|
designation.bits.dig = df::tile_dig_designation::UpStair;
|
|
|
|
break;
|
|
|
|
case job_type::CarveDownwardStaircase:
|
|
|
|
designation.bits.dig = df::tile_dig_designation::DownStair;
|
|
|
|
break;
|
|
|
|
case job_type::CarveUpDownStaircase:
|
|
|
|
designation.bits.dig = df::tile_dig_designation::UpDownStair;
|
|
|
|
break;
|
|
|
|
case job_type::CarveRamp:
|
|
|
|
designation.bits.dig = df::tile_dig_designation::Ramp;
|
|
|
|
break;
|
|
|
|
case job_type::DigChannel:
|
|
|
|
designation.bits.dig = df::tile_dig_designation::Channel;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
designation.bits.dig = df::tile_dig_designation::No;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 15:54:39 -07:00
|
|
|
|
2022-12-08 12:21:17 -07:00
|
|
|
inline void cancel_job(const df::coord &map_pos) {
|
|
|
|
cancel_job(ChannelManager::Get().jobs.find_job(map_pos));
|
|
|
|
ChannelManager::Get().jobs.erase(map_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// executes dig designations for the specified tile coordinates
|
|
|
|
inline bool dig_now(color_ostream &out, const df::coord &map_pos) {
|
2022-12-08 15:59:09 -07:00
|
|
|
static std::default_random_engine rng;
|
|
|
|
std::uniform_int_distribution<> dist(0,5);
|
2022-12-08 12:21:17 -07:00
|
|
|
out.color(color_value::COLOR_YELLOW);
|
|
|
|
out.print("channel-safely: insta-dig: digging (" COORD ")<\n", COORDARGS(map_pos));
|
2022-12-08 15:59:09 -07:00
|
|
|
|
|
|
|
df::coord below(map_pos);
|
|
|
|
below.z--;
|
|
|
|
auto ttype_below = *Maps::getTileType(below);
|
|
|
|
if (isOpenTerrain(ttype_below) || isFloorTerrain(ttype_below)) {
|
|
|
|
*Maps::getTileType(map_pos) = tiletype::OpenSpace;
|
|
|
|
} else {
|
|
|
|
auto ttype_p = Maps::getTileType(map_pos);
|
|
|
|
if (isSoilMaterial(*ttype_p)) {
|
|
|
|
switch(dist(rng)) {
|
|
|
|
case 0:
|
|
|
|
*ttype_p = tiletype::SoilFloor1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*ttype_p = tiletype::SoilFloor2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*ttype_p = tiletype::SoilFloor3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
*ttype_p = tiletype::SoilFloor4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*ttype_p = tiletype::SoilFloor1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (isStoneMaterial(*ttype_p)) {
|
|
|
|
switch(dist(rng)) {
|
|
|
|
case 0:
|
|
|
|
*ttype_p = tiletype::FeatureFloor1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*ttype_p = tiletype::FeatureFloor2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*ttype_p = tiletype::FeatureFloor3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
*ttype_p = tiletype::FeatureFloor4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*ttype_p = tiletype::MineralFloor1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out.print("Unknown type\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
/*
|
2022-12-08 12:21:17 -07:00
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
lua_State* state = Lua::Core::State;
|
|
|
|
static const char* module_name = "plugins.dig-now";
|
|
|
|
static const char* fn_name = "dig_now_tile";
|
|
|
|
// the stack layout isn't likely to change, ever
|
|
|
|
static auto args_lambda = [&map_pos](lua_State* L) {
|
|
|
|
Lua::Push(L, map_pos);
|
|
|
|
};
|
|
|
|
static auto res_lambda = [&ret](lua_State* L) {
|
|
|
|
ret = lua_toboolean(L, -1);
|
|
|
|
};
|
|
|
|
|
|
|
|
Lua::StackUnwinder top(state);
|
|
|
|
Lua::CallLuaModuleFunction(out, state, module_name, fn_name, 1, 1, args_lambda, res_lambda);
|
|
|
|
return ret;
|
2022-12-08 15:59:09 -07:00
|
|
|
*/
|
2022-12-08 12:21:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// fully heals the unit specified, resurrecting if need be
|
|
|
|
inline void resurrect(color_ostream &out, const int32_t &unit) {
|
|
|
|
out.color(DFHack::COLOR_RED);
|
|
|
|
out.print("channel-safely: resurrecting [id: %d]\n", unit);
|
|
|
|
std::vector<std::string> params{"-r", "--unit", std::to_string(unit)};
|
|
|
|
Core::getInstance().runCommand(out,"full-heal", params);
|
|
|
|
}
|
|
|
|
|
2022-11-07 15:54:39 -07:00
|
|
|
template<class Ctr1, class Ctr2, class Ctr3>
|
|
|
|
void set_difference(const Ctr1 &c1, const Ctr2 &c2, Ctr3 &c3) {
|
|
|
|
for (const auto &a : c1) {
|
2022-11-21 13:39:26 -07:00
|
|
|
if (!c2.count(a)) {
|
2022-11-07 15:54:39 -07:00
|
|
|
c3.emplace(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Ctr1, class Ctr2, class Ctr3>
|
|
|
|
void map_value_difference(const Ctr1 &c1, const Ctr2 &c2, Ctr3 &c3) {
|
|
|
|
for (const auto &a : c1) {
|
|
|
|
bool matched = false;
|
|
|
|
for (const auto &b : c2) {
|
|
|
|
if (a.second == b.second) {
|
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!matched) {
|
|
|
|
c3.emplace(a.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|