use reveal.unhideFlood instead of our dup impl

develop
myk002 2021-06-02 22:18:42 -07:00
parent a5b70ba163
commit fc932d2850
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 27 additions and 50 deletions

@ -111,7 +111,7 @@ if(BUILD_SUPPORTED)
dfhack_plugin(deramp deramp.cpp) dfhack_plugin(deramp deramp.cpp)
dfhack_plugin(debug debug.cpp LINK_LIBRARIES jsoncpp_lib_static) dfhack_plugin(debug debug.cpp LINK_LIBRARIES jsoncpp_lib_static)
dfhack_plugin(dig dig.cpp) dfhack_plugin(dig dig.cpp)
dfhack_plugin(dig-now dig-now.cpp) dfhack_plugin(dig-now dig-now.cpp LINK_LIBRARIES lua)
dfhack_plugin(digFlood digFlood.cpp) dfhack_plugin(digFlood digFlood.cpp)
add_subdirectory(diggingInvaders) add_subdirectory(diggingInvaders)
dfhack_plugin(dwarfvet dwarfvet.cpp) dfhack_plugin(dwarfvet dwarfvet.cpp)

@ -5,7 +5,9 @@
#include "DataFuncs.h" #include "DataFuncs.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "TileTypes.h" #include "TileTypes.h"
#include "LuaTools.h"
#include "modules/Maps.h"
#include "modules/MapCache.h" #include "modules/MapCache.h"
#include <df/tile_designation.h> #include <df/tile_designation.h>
@ -18,50 +20,16 @@ REQUIRE_GLOBAL(world);
using namespace DFHack; using namespace DFHack;
// returns true iff tile is in map bounds and was hidden before this function static void flood_unhide(color_ostream &out, const DFCoord &pos) {
// unhid it. auto L = Lua::Core::State;
static bool unhide(MapExtras::MapCache &map, const DFCoord &pos) { Lua::StackUnwinder top(L);
// ensures coords are in map bounds and ensures that the map block exists
// so we can unhide the tiles
if (!map.ensureBlockAt(pos))
return false;
df::tile_designation td = map.designationAt(pos);
if (!td.bits.hidden)
return false;
td.bits.hidden = false;
return map.setDesignationAt(pos, td);
}
// unhide adjacent tiles if hidden and flood fill unhidden state if (!lua_checkstack(L, 2)
static void flood_unhide(MapExtras::MapCache &map, const DFCoord &pos) { || !Lua::PushModulePublic(out, L, "plugins.reveal", "unhideFlood"))
df::tiletype tt = map.tiletypeAt(pos);
if (tileShape(tt) == df::tiletype_shape::WALL
&& tileMaterial(tt) != df::tiletype_material::TREE)
return; return;
for (int32_t xoff = -1; xoff <= 1; ++xoff) { Lua::Push(L, pos);
for (int32_t yoff = -1; yoff <= 1; ++yoff) { Lua::SafeCall(out, L, 1, 0);
if (xoff == 0 && yoff == 0)
continue;
if (unhide(map, DFCoord(pos.x+xoff, pos.y+yoff, pos.z)))
flood_unhide(map, DFCoord(pos.x+xoff, pos.y+yoff, pos.z));
}
}
DFCoord pos_below(pos.x, pos.y, pos.z-1);
if (LowPassable(tt) && unhide(map, pos_below))
flood_unhide(map, pos_below);
// note that checking HighPassable for the current tile gives false
// positives. You have to check LowPassable for the tile above.
DFCoord pos_above(pos.x, pos.y, pos.z+1);
if (map.ensureBlockAt(pos_above)
&& LowPassable(map.tiletypeAt(pos_above))
&& unhide(map, pos_above)) {
flood_unhide(map, pos_above);
}
} }
// inherit flags from passable tiles above and propagate to passable tiles below // inherit flags from passable tiles above and propagate to passable tiles below
@ -69,7 +37,7 @@ static void propagate_vertical_flags(MapExtras::MapCache &map,
const DFCoord &pos) { const DFCoord &pos) {
df::tile_designation td = map.designationAt(pos); df::tile_designation td = map.designationAt(pos);
if (!map.ensureBlockAt(DFCoord(pos.x, pos.y, pos.z-1))) { if (!map.ensureBlockAt(DFCoord(pos.x, pos.y, pos.z+1))) {
// only the sky above // only the sky above
td.bits.light = true; td.bits.light = true;
td.bits.outside = true; td.bits.outside = true;
@ -248,7 +216,6 @@ static bool dig_tile(color_ostream &out, MapExtras::MapCache &map,
dig_tile(out, map, pos_below, dig_tile(out, map, pos_below,
df::tile_dig_designation::Ramp)) { df::tile_dig_designation::Ramp)) {
clean_ramps(map, pos_below); clean_ramps(map, pos_below);
flood_unhide(map, pos);
// if we successfully dug out the ramp below, that took care // if we successfully dug out the ramp below, that took care
// of the ramp top here // of the ramp top here
return true; return true;
@ -296,16 +263,14 @@ static bool dig_tile(color_ostream &out, MapExtras::MapCache &map,
pos.x, pos.y, pos.z, designation); pos.x, pos.y, pos.z, designation);
} }
// fail if no change to tile // fail if unhandled or no change to tile
if (target_type == df::tiletype::Void || target_type == tt) if (target_type == df::tiletype::Void || target_type == tt)
return false; return false;
dig_type(map, pos, target_type); dig_type(map, pos, target_type);
// set flags for current and adjacent tiles // let light filter down to newly exposed tiles
unhide(map, pos); propagate_vertical_flags(map, pos);
flood_unhide(map, pos); // in case we breached a cavern
propagate_vertical_flags(map, pos); // for new channels
return true; return true;
} }
@ -334,9 +299,12 @@ command_result dig_dug(color_ostream &out, std::vector<std::string> &) {
uint32_t endx, endy, endz; uint32_t endx, endy, endz;
Maps::getTileSize(endx, endy, endz); Maps::getTileSize(endx, endy, endz);
// use the proxy layer for the layer material-setting ease-of-use functions // use the proxy layer for the layer material-setting ease-of-use functions.
MapExtras::MapCache map; MapExtras::MapCache map;
// tracks which positions to unhide
std::vector<DFCoord> dug_coords;
for (uint32_t z = 0; z <= endz; ++z) { for (uint32_t z = 0; z <= endz; ++z) {
for (uint32_t y = 0; y <= endy; ++y) { for (uint32_t y = 0; y <= endy; ++y) {
for (uint32_t x = 0; x <= endx; ++x) { for (uint32_t x = 0; x <= endx; ++x) {
@ -353,6 +321,7 @@ command_result dig_dug(color_ostream &out, std::vector<std::string> &) {
td = map.designationAt(pos); td = map.designationAt(pos);
td.bits.dig = df::tile_dig_designation::No; td.bits.dig = df::tile_dig_designation::No;
map.setDesignationAt(pos, td); map.setDesignationAt(pos, td);
dug_coords.push_back(pos);
} }
} else if (td.bits.smooth > 0) { } else if (td.bits.smooth > 0) {
bool want_engrave = td.bits.smooth == 2; bool want_engrave = td.bits.smooth == 2;
@ -380,6 +349,14 @@ command_result dig_dug(color_ostream &out, std::vector<std::string> &) {
map.WriteAll(); map.WriteAll();
// unhide newly dug tiles. we can't do this in the loop above since our
// MapCache wouldn't detect the changes made by reveal.unhideFlood() without
// invalidating and reinitializing on every call.
for (DFCoord pos : dug_coords) {
if (Maps::getTileDesignation(pos)->bits.hidden)
flood_unhide(out, pos);
}
// Force the game to recompute its walkability cache // Force the game to recompute its walkability cache
world->reindex_pathfinding = true; world->reindex_pathfinding = true;