diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index dd5000ab5..1d51dc354 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -141,7 +141,7 @@ dfhack_plugin(hotkeys hotkeys.cpp LINK_LIBRARIES lua) #dfhack_plugin(nestboxes nestboxes.cpp) #dfhack_plugin(orders orders.cpp LINK_LIBRARIES jsoncpp_static) dfhack_plugin(overlay overlay.cpp LINK_LIBRARIES lua) -#dfhack_plugin(pathable pathable.cpp LINK_LIBRARIES lua) +dfhack_plugin(pathable pathable.cpp LINK_LIBRARIES lua) #dfhack_plugin(petcapRemover petcapRemover.cpp) #dfhack_plugin(plants plants.cpp) #dfhack_plugin(probe probe.cpp) diff --git a/plugins/lua/pathable.lua b/plugins/lua/pathable.lua index b5f5addfc..f3247cffe 100644 --- a/plugins/lua/pathable.lua +++ b/plugins/lua/pathable.lua @@ -2,7 +2,7 @@ local _ENV = mkmodule('plugins.pathable') --[[ -Native functions: (see Plugins.rst for details) +Native functions: (see docs/dev/Lua API.rst for details) - paintScreen(cursor[,skip_unrevealed]) diff --git a/plugins/pathable.cpp b/plugins/pathable.cpp index e5a9bebcc..9d5ae8bef 100644 --- a/plugins/pathable.cpp +++ b/plugins/pathable.cpp @@ -1,82 +1,96 @@ -#include "Console.h" -#include "Core.h" -#include "DataDefs.h" -#include "DataFuncs.h" -#include "DataIdentity.h" -#include "Export.h" -#include "LuaTools.h" -#include "PluginManager.h" -#include "modules/Gui.h" +#include "df/graphic_viewportst.h" + #include "modules/Maps.h" #include "modules/Screen.h" -#include "df/world.h" + +#include "Debug.h" +#include "LuaTools.h" +#include "PluginManager.h" using namespace DFHack; DFHACK_PLUGIN("pathable"); -REQUIRE_GLOBAL(world); + +REQUIRE_GLOBAL(gps); REQUIRE_GLOBAL(window_x); REQUIRE_GLOBAL(window_y); REQUIRE_GLOBAL(window_z); +REQUIRE_GLOBAL(world); + +namespace DFHack { + DBG_DECLARE(pathable, log, DebugCategory::LINFO); +} -DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) -{ +DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { return CR_OK; } -DFhackCExport command_result plugin_shutdown(color_ostream &out) -{ +DFhackCExport command_result plugin_shutdown(color_ostream &out) { return CR_OK; } -static void paintScreen(df::coord cursor, bool skip_unrevealed = false) -{ - auto dims = Gui::getDwarfmodeViewDims(); - for (int y = dims.map_y1; y <= dims.map_y2; y++) - { - for (int x = dims.map_x1; x <= dims.map_x2; x++) - { - Screen::Pen cur_tile = Screen::readTile(x, y, true); - if (!cur_tile.valid()) - continue; +static void paintScreen(df::coord target, bool skip_unrevealed = false) { + DEBUG(log).print("entering paintScreen\n"); + + bool use_graphics = Screen::inGraphicsMode(); - df::coord map_pos( - *window_x + x - dims.map_x1, - *window_y + y - dims.map_y1, - *window_z - ); + auto dimx = use_graphics ? gps->main_viewport->dim_x : gps->dimx; + auto dimy = use_graphics ? gps->main_viewport->dim_y : gps->dimy; + for (int y = 0; y < dimy; ++y) { + for (int x = 0; x < dimx; ++x) { + df::coord map_pos(*window_x + x, *window_y + y, *window_z); - // Keep yellow cursor - if (map_pos == cursor) + if (!Maps::isValidTilePos(map_pos)) continue; - if (map_pos.x < 0 || map_pos.x >= world->map.x_count || - map_pos.y < 0 || map_pos.y >= world->map.y_count || - map_pos.z < 0 || map_pos.z >= world->map.z_count) - { + // don't overwrite the target tile + if (!use_graphics && map_pos == target) { + TRACE(log).print("skipping target tile\n"); continue; } - if (skip_unrevealed && !Maps::isTileVisible(map_pos)) + if (skip_unrevealed && !Maps::isTileVisible(map_pos)) { + TRACE(log).print("skipping hidden tile\n"); continue; + } - int color = Maps::canWalkBetween(cursor, map_pos) ? COLOR_GREEN : COLOR_RED; + DEBUG(log).print("scanning map tile at offset %d, %d\n", x, y); + Screen::Pen cur_tile = Screen::readTile(x, y, true); + DEBUG(log).print("tile data: ch=%d, fg=%d, bg=%d, bold=%s\n", + cur_tile.ch, cur_tile.fg, cur_tile.bg, cur_tile.bold ? "true" : "false"); + DEBUG(log).print("tile data: tile=%d, tile_mode=%d, tile_fg=%d, tile_bg=%d\n", + cur_tile.tile, cur_tile.tile_mode, cur_tile.tile_fg, cur_tile.tile_bg); - if (cur_tile.fg && cur_tile.ch != ' ') - { - cur_tile.fg = color; - cur_tile.bg = 0; - } - else - { - cur_tile.fg = 0; - cur_tile.bg = color; + if (!cur_tile.valid()) { + DEBUG(log).print("cannot read tile at offset %d, %d\n", x, y); + continue; } - cur_tile.bold = false; + bool can_walk = Maps::canWalkBetween(target, map_pos); + DEBUG(log).print("tile is %swalkable at offset %d, %d\n", + can_walk ? "" : "not ", x, y); + + if (use_graphics) { + if (map_pos == target) { + cur_tile.tile = 100711; // highlighted selection + } else{ + cur_tile.tile = can_walk ? 779 : 782; + } + } else { + int color = can_walk ? COLOR_GREEN : COLOR_RED; + if (cur_tile.fg && cur_tile.ch != ' ') { + cur_tile.fg = color; + cur_tile.bg = 0; + } else { + cur_tile.fg = 0; + cur_tile.bg = color; + } - if (cur_tile.tile) - cur_tile.tile_mode = Screen::Pen::CharColor; + cur_tile.bold = false; + + if (cur_tile.tile) + cur_tile.tile_mode = Screen::Pen::CharColor; + } Screen::paintTile(cur_tile, x, y, true); }