first stage of blueprint overhaul

- make depth and name parameters optional
- allow depth to be negative to indicate top-down instead of the
  previous hard-coded bottom-up
- add --cursor for specifying start position (game cursor is not needed
  if this param is specified)
develop
myk002 2021-05-04 13:19:49 -07:00
parent 84a1b39daa
commit a7a5a48c7a
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 357 additions and 161 deletions

@ -8,9 +8,11 @@
#include <algorithm>
#include <sstream>
#include <Console.h>
#include <PluginManager.h>
#include "Console.h"
#include "DataDefs.h"
#include "DataIdentity.h"
#include "LuaTools.h"
#include "PluginManager.h"
#include "TileTypes.h"
#include "modules/Buildings.h"
@ -27,20 +29,61 @@
#include "df/building_trapst.h"
#include "df/building_water_wheelst.h"
#include "df/building_workshopst.h"
#include "df/world.h"
using std::string;
using std::endl;
using std::vector;
using std::ofstream;
using std::swap;
using std::find;
using std::pair;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("blueprint");
REQUIRE_GLOBAL(world);
enum phase {DIG=1, BUILD=2, PLACE=4, QUERY=8};
struct blueprint_options {
// whether to display help
bool help = false;
// starting tile coordinate of the translation area (if not set then all
// coordinates are set to -30000)
df::coord start;
// dimensions of translation area. width and height are guaranteed to be
// greater than 0. depth can be positive or negative, but not zero.
int32_t width = 0;
int32_t height = 0;
int32_t depth = 0;
// base name to use for generated files
string name;
// whether to autodetect which phases to output
bool auto_phase = false;
// if not autodetecting, which phases to output
bool dig = false;
bool build = false;
bool place = false;
bool query = false;
static struct_identity _identity;
};
static const struct_field_info blueprint_options_fields[] = {
{ struct_field_info::PRIMITIVE, "help", offsetof(blueprint_options, help), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::SUBSTRUCT, "start", offsetof(blueprint_options, start), &df::coord::_identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "width", offsetof(blueprint_options, width), &df::identity_traits<int32_t>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "height", offsetof(blueprint_options, height), &df::identity_traits<int32_t>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "depth", offsetof(blueprint_options, depth), &df::identity_traits<int32_t>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "name", offsetof(blueprint_options, name), df::identity_traits<string>::get(), 0, 0 },
{ struct_field_info::PRIMITIVE, "auto_phase", offsetof(blueprint_options, auto_phase), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "dig", offsetof(blueprint_options, dig), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "build", offsetof(blueprint_options, build), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "place", offsetof(blueprint_options, place), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::PRIMITIVE, "query", offsetof(blueprint_options, query), &df::identity_traits<bool>::identity, 0, 0 },
{ struct_field_info::END }
};
struct_identity blueprint_options::_identity(sizeof(blueprint_options), &df::allocator_fn<blueprint_options>, NULL, "blueprint_options", NULL, blueprint_options_fields);
command_result blueprint(color_ostream &out, vector<string> &parameters);
@ -55,25 +98,6 @@ DFhackCExport command_result plugin_shutdown(color_ostream &out)
return CR_OK;
}
command_result help(color_ostream &out)
{
out << "blueprint width height depth name [dig] [build] [place] [query]" << endl
<< " width, height, depth: area to translate in tiles" << endl
<< " name: base name for blueprint files" << endl
<< " dig: generate blueprints for digging" << endl
<< " build: generate blueprints for building" << endl
<< " place: generate blueprints for stockpiles" << endl
<< " query: generate blueprints for querying (room designations)" << endl
<< " defaults to generating all blueprints" << endl
<< endl
<< "blueprint translates a portion of your fortress into blueprints suitable for" << endl
<< " digfort/fortplan/quickfort. Blueprints are created in the \"blueprints\"" << endl
<< " subdirectory of the DF folder with names following a \"name-phase.csv\" pattern." << endl
<< " Translation starts at the current cursor location and includes all tiles in the" << endl
<< " range specified." << endl;
return CR_OK;
}
pair<uint32_t, uint32_t> get_building_size(df::building* b)
{
return pair<uint32_t, uint32_t>(b->x2 - b->x1 + 1, b->y2 - b->y1 + 1);
@ -562,7 +586,7 @@ string get_tile_query(df::building* b)
return " ";
}
void init_stream(ofstream &out, std::string basename, std::string target)
void init_stream(ofstream &out, string basename, string target)
{
std::ostringstream out_path;
out_path << basename << "-" << target << ".csv";
@ -570,19 +594,15 @@ void init_stream(ofstream &out, std::string basename, std::string target)
out << "#" << target << endl;
}
command_result do_transform(DFCoord start, DFCoord end, string name, uint32_t phases, std::ostringstream &err)
command_result do_transform(const DFCoord &start, const DFCoord &end,
const blueprint_options &options,
std::ostringstream &err)
{
ofstream dig, build, place, query;
std::string basename = "blueprints/" + name;
#ifdef _WIN32
// normalize to forward slashes
std::replace(basename.begin(), basename.end(), '\\', '/');
#endif
string basename = "blueprints/" + options.name;
size_t last_slash = basename.find_last_of("/");
std::string parent_path = basename.substr(0, last_slash);
string parent_path = basename.substr(0, last_slash);
// create output directory if it doesn't already exist
std::error_code ec;
@ -592,179 +612,200 @@ command_result do_transform(DFCoord start, DFCoord end, string name, uint32_t ph
return CR_FAILURE;
}
if (phases & QUERY)
if (options.auto_phase || options.query)
{
//query = ofstream((name + "-query.csv").c_str(), ofstream::trunc);
init_stream(query, basename, "query");
}
if (phases & PLACE)
if (options.auto_phase || options.place)
{
//place = ofstream(name + "-place.csv", ofstream::trunc);
init_stream(place, basename, "place");
}
if (phases & BUILD)
if (options.auto_phase || options.build)
{
//build = ofstream(name + "-build.csv", ofstream::trunc);
init_stream(build, basename, "build");
}
if (phases & DIG)
if (options.auto_phase || options.dig)
{
//dig = ofstream(name + "-dig.csv", ofstream::trunc);
init_stream(dig, basename, "dig");
}
if (start.x > end.x)
{
swap(start.x, end.x);
start.x++;
end.x++;
}
if (start.y > end.y)
{
swap(start.y, end.y);
start.y++;
end.y++;
}
if (start.z > end.z)
{
swap(start.z, end.z);
start.z++;
end.z++;
}
for (int32_t z = start.z; z < end.z; z++)
const int32_t z_inc = start.z < end.z ? 1 : -1;
const string z_key = start.z < end.z ? "#<" : "#>";
for (int32_t z = start.z; z != end.z; z += z_inc)
{
for (int32_t y = start.y; y < end.y; y++)
{
for (int32_t x = start.x; x < end.x; x++)
{
df::building* b = DFHack::Buildings::findAtTile(DFCoord(x, y, z));
if (phases & QUERY)
df::building* b = Buildings::findAtTile(DFCoord(x, y, z));
if (options.auto_phase || options.query)
query << get_tile_query(b) << ',';
if (phases & PLACE)
if (options.auto_phase || options.place)
place << get_tile_place(x, y, b) << ',';
if (phases & BUILD)
if (options.auto_phase || options.build)
build << get_tile_build(x, y, b) << ',';
if (phases & DIG)
if (options.auto_phase || options.dig)
dig << get_tile_dig(x, y, z) << ',';
}
if (phases & QUERY)
if (options.auto_phase || options.query)
query << "#" << endl;
if (phases & PLACE)
if (options.auto_phase || options.place)
place << "#" << endl;
if (phases & BUILD)
if (options.auto_phase || options.build)
build << "#" << endl;
if (phases & DIG)
if (options.auto_phase || options.dig)
dig << "#" << endl;
}
if (z < end.z - 1)
if (z != end.z - z_inc)
{
if (phases & QUERY)
query << "#<" << endl;
if (phases & PLACE)
place << "#<" << endl;
if (phases & BUILD)
build << "#<" << endl;
if (phases & DIG)
dig << "#<" << endl;
if (options.auto_phase || options.query)
query << z_key << endl;
if (options.auto_phase || options.place)
place << z_key << endl;
if (options.auto_phase || options.build)
build << z_key << endl;
if (options.auto_phase || options.dig)
dig << z_key << endl;
}
}
if (phases & QUERY)
if (options.auto_phase || options.query)
query.close();
if (phases & PLACE)
if (options.auto_phase || options.place)
place.close();
if (phases & BUILD)
if (options.auto_phase || options.build)
build.close();
if (phases & DIG)
if (options.auto_phase || options.dig)
dig.close();
return CR_OK;
}
bool cmd_option_exists(vector<string>& parameters, const string& option)
static bool get_options(blueprint_options &opts,
const vector<string> &parameters)
{
auto L = Lua::Core::State;
color_ostream_proxy out(Core::getInstance().getConsole());
Lua::StackUnwinder top(L);
if (!lua_checkstack(L, parameters.size() + 2) ||
!Lua::PushModulePublic(
out, L, "plugins.blueprint", "parse_commandline"))
{
out.printerr("Failed to load blueprint Lua code");
return false;
}
Lua::Push(L, &opts);
for (const string &param : parameters)
Lua::Push(L, param);
if (!Lua::SafeCall(out, L, parameters.size() + 1, 0))
return false;
return true;
}
static bool do_gui(const vector<string> &parameters)
{
auto L = Lua::Core::State;
color_ostream_proxy out(Core::getInstance().getConsole());
Lua::StackUnwinder top(L);
if (!lua_checkstack(L, parameters.size() + 1) ||
!Lua::PushModulePublic(out, L, "plugins.blueprint", "do_gui"))
{
return find(parameters.begin(), parameters.end(), option) != parameters.end();
out.printerr("Failed to load blueprint Lua code");
return false;
}
for (const string &param : parameters)
Lua::Push(L, param);
if (!Lua::SafeCall(out, L, parameters.size(), 0))
return false;
return true;
}
static void print_help()
{
auto L = Lua::Core::State;
color_ostream_proxy out(Core::getInstance().getConsole());
Lua::StackUnwinder top(L);
if (!lua_checkstack(L, 1) ||
!Lua::PushModulePublic(out, L, "plugins.blueprint", "print_help") ||
!Lua::SafeCall(out, L, 0, 0))
{
out.printerr("Failed to load blueprint Lua code");
}
}
command_result blueprint(color_ostream &out, vector<string> &parameters)
{
if (parameters.size() < 4 || parameters.size() > 8)
return help(out);
if (parameters.size() >= 1 and parameters[0] == "gui")
{
return do_gui(parameters) ? CR_OK : CR_FAILURE;
}
blueprint_options options;
if (!get_options(options, parameters) || options.help)
{
print_help();
return options.help ? CR_OK : CR_FAILURE;
}
CoreSuspender suspend;
if (!Maps::IsValid())
{
out.printerr("Map is not available!\n");
return CR_FAILURE;
}
// start coordinates can come from either the commandline or the map cursor
DFCoord start(options.start);
if (options.start.x == -30000)
{
int32_t x, y, z;
if (!Gui::getCursorCoords(x, y, z))
{
out.printerr("Can't get cursor coords! Make sure you have an active cursor in DF.\n");
out.printerr("Can't get cursor coords! Make sure you specify the"
" --cursor parameter or have an active cursor in DF.\n");
return CR_FAILURE;
}
DFCoord start (x, y, z);
DFCoord end (x + stoi(parameters[0]), y + stoi(parameters[1]), z + stoi(parameters[2]));
uint32_t option = 0;
if (parameters.size() == 4)
{
option = DIG | BUILD | PLACE | QUERY;
start.x = x;
start.y = y;
start.z = z;
}
else
if (!Maps::isValidTilePos(start))
{
if (cmd_option_exists(parameters, "dig"))
option |= DIG;
if (cmd_option_exists(parameters, "build"))
option |= BUILD;
if (cmd_option_exists(parameters, "place"))
option |= PLACE;
if (cmd_option_exists(parameters, "query"))
option |= QUERY;
}
std::ostringstream err;
DFHack::command_result result = do_transform(start, end, parameters[3], option, err);
if (result != CR_OK)
out.printerr("%s\n", err.str().c_str());
return result;
out.printerr("Invalid start position: %d,%d,%d\n",
start.x, start.y, start.z);
return CR_FAILURE;
}
static int create(lua_State *L, uint32_t options) {
df::coord start, end;
lua_settop(L, 3);
Lua::CheckDFAssign(L, &start, 1);
if (!start.isValid())
luaL_argerror(L, 1, "invalid start position");
Lua::CheckDFAssign(L, &end, 2);
if (!end.isValid())
luaL_argerror(L, 2, "invalid end position");
string filename(lua_tostring(L, 3));
// end coords are one beyond the last processed coordinate. note that
// options.depth can be negative.
DFCoord end(start.x + options.width, start.y + options.height,
start.z + options.depth);
// crop end coordinate to map bounds. we've already verified that start is
// a valid coordinate, and width, height, and depth are non-zero, so our
// final are is always going to be at least 1x1x1.
df::world::T_map &map = df::global::world->map;
if (end.x > map.x_count)
end.x = map.x_count;
if (end.y > map.y_count)
end.y = map.y_count;
if (end.z > map.z_count)
end.z = map.z_count;
if (end.z < -1)
end.z = -1;
std::ostringstream err;
DFHack::command_result result = do_transform(start, end, filename, options, err);
command_result result = do_transform(start, end, options, err);
if (result != CR_OK)
luaL_error(L, "%s", err.str().c_str());
lua_pushboolean(L, result);
return 1;
}
static int dig(lua_State *L) {
return create(L, DIG);
}
static int build(lua_State *L) {
return create(L, BUILD);
}
static int place(lua_State *L) {
return create(L, PLACE);
}
static int query(lua_State *L) {
return create(L, QUERY);
out.printerr("%s\n", err.str().c_str());
return result;
}
DFHACK_PLUGIN_LUA_COMMANDS {
DFHACK_LUA_COMMAND(dig),
DFHACK_LUA_COMMAND(build),
DFHACK_LUA_COMMAND(place),
DFHACK_LUA_COMMAND(query),
DFHACK_LUA_END
};

@ -1,14 +1,169 @@
local _ENV = mkmodule('plugins.blueprint')
--[[
local utils = require('utils')
Native functions:
-- the info here is very basic and minimal, so hopefully we won't need to change
-- it when features are added and the full blueprint docs in Plugins.rst are
-- updated.
local help_text =
[=[
* dig(start, end, name)
* build(start, end, name)
* place(start, end, name)
* query(start, end, name)
blueprint
=========
--]]
Records the structure of a portion of your fortress in quickfort blueprints.
Usage:
blueprint <width> <height> [<depth>] [<name> [<phases>]] [<options>]
blueprint gui [<name> [<phases>]] [<options>]
Examples:
blueprint gui
Runs gui/blueprint, the interactive blueprint frontend, where all
configuration can be set visually and interactively.
blueprint 30 40 bedrooms
Generates blueprints for an area 30 tiles wide by 40 tiles tall, starting
from the active cursor on the current z-level. Output is written to the
"bedrooms.csv" file in the "blueprints" directory.
See the online DFHack documentation for more examples and details.
]=]
valid_phases = utils.invert{
'dig',
'build',
'place',
'query',
}
function print_help()
print(help_text)
end
local function parse_cursor(opts, arg)
local _, _, x, y, z = arg:find('^(%d+),(%d+),(%d+)$')
if not x then
qerror(('invalid argument for --cursor option: "%s"; expected format' ..
' is "<x>,<y>,<z>", for example: "30,60,150"'):format(arg))
end
-- be careful not to replace struct members when called from C++, but also
-- create the table as needed when called from lua
if not opts.start then opts.start = {} end
opts.start.x = tonumber(x)
opts.start.y = tonumber(y)
opts.start.z = tonumber(z)
end
-- dimension must be a non-nil integer that is >= 1 (or at least non-zero if
-- negative_ok is true)
local function is_bad_dim(dim, negative_ok)
return not dim
or (not negative_ok and dim < 1 or dim == 0)
or dim ~= math.floor(dim)
end
local function parse_positionals(opts, args, start_argidx)
local argidx = start_argidx
-- set defaults
opts.name, opts.auto_phase = 'blueprint', true
local name = args[argidx]
if not name then return end
if name == '' then
qerror(('invalid basename: "%s"; must be a valid, non-empty pathname')
:format(args[argidx]))
end
argidx = argidx + 1
-- normalize paths to forward slashes
opts.name = name:gsub(package.config:sub(1,1), "/")
local auto_phase = true
local phase = args[argidx]
while phase do
if valid_phases[phase] then
auto_phase = false
opts[phase] = true
end
argidx = argidx + 1
phase = args[argidx]
end
opts.auto_phase = auto_phase
end
local function process_args(opts, args)
if args[1] == 'help' then
opts.help = true
return
end
return utils.processArgsGetopt(args, {
{'c', 'cursor', hasArg=true,
handler=function(optarg) parse_cursor(opts, optarg) end},
{'h', 'help', handler=function() opts.help = true end},
})
end
function parse_gui_commandline(opts, args)
local positionals = process_args(opts, args)
if opts.help then return end
parse_positionals(opts, positionals, 1)
end
function parse_commandline(opts, ...)
local positionals = process_args(opts, {...})
if opts.help then return end
local width, height = tonumber(positionals[1]), tonumber(positionals[2])
if is_bad_dim(width) or is_bad_dim(height) then
qerror(('invalid width and height: "%s" "%s"; width and height must' ..
' be positive integers'):format(positionals[1], positionals[2]))
end
opts.width, opts.height, opts.depth = width, height, 1
local depth = tonumber(positionals[3])
if depth then
if is_bad_dim(depth, true) then
qerror(('invalid depth: "%s"; must be a non-zero integer')
:format(positionals[3]))
end
opts.depth = depth
end
parse_positionals(opts, positionals, depth and 4 or 3)
end
function do_gui(command, ...)
local args = {...}
print('launching gui/blueprint')
dfhack.timeout(1, 'frames',
function() dfhack.run_script('gui/blueprint',
table.unpack(args)) end)
end
-- compatibility with old exported API. we route the request back through
-- run_command so we have a unified path for parameter processing and invariant
-- checking.
local function do_blueprint(start_pos, end_pos, name, phase)
local width = math.abs(start_pos.x - end_pos.x) + 1
local height = math.abs(start_pos.y - end_pos.y) + 1
local depth = math.abs(start_pos.z - end_pos.z) + 1
if start_pos.z > end_pos.z then depth = -depth end
local x = math.min(start_pos.x, end_pos.x)
local y = math.min(start_pos.y, end_pos.y)
local z = start_pos.z
local cursor = ('--cursor=%d,%d,%d'):format(x, y, z)
return dfhack.run_command{'blueprint',
width, height, depth, name, phase, cursor}
end
for phase in pairs(valid_phases) do
_ENV[phase] = function(s, e, n) do_blueprint(s, e, n, phase) end
end
return _ENV