2023-10-28 01:37:24 -06:00
|
|
|
local _ENV = mkmodule('plugins.burrow')
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
2023-10-31 19:54:38 -06:00
|
|
|
local argparse = require('argparse')
|
2023-10-28 01:37:24 -06:00
|
|
|
local overlay = require('plugins.overlay')
|
|
|
|
local widgets = require('gui.widgets')
|
|
|
|
|
2023-10-28 20:26:15 -06:00
|
|
|
---------------------------------
|
|
|
|
-- BurrowDesignationOverlay
|
|
|
|
--
|
|
|
|
|
2023-10-28 01:37:24 -06:00
|
|
|
local selection_rect = df.global.selection_rect
|
|
|
|
local if_burrow = df.global.game.main_interface.burrow
|
|
|
|
|
|
|
|
local function is_choosing_area(pos)
|
|
|
|
return if_burrow.doing_rectangle and
|
|
|
|
selection_rect.start_x >= 0 and
|
|
|
|
(pos or dfhack.gui.getMousePos())
|
|
|
|
end
|
|
|
|
|
|
|
|
local function reset_selection_rect()
|
|
|
|
selection_rect.start_x = -30000
|
|
|
|
selection_rect.start_y = -30000
|
|
|
|
selection_rect.start_z = -30000
|
|
|
|
end
|
|
|
|
|
2023-10-28 20:26:15 -06:00
|
|
|
local function get_bounds(pos1, pos2)
|
|
|
|
pos1 = pos1 or dfhack.gui.getMousePos()
|
|
|
|
pos2 = pos2 or xyz2pos(selection_rect.start_x, selection_rect.start_y, selection_rect.start_z)
|
2023-10-28 01:37:24 -06:00
|
|
|
local bounds = {
|
2023-10-28 20:26:15 -06:00
|
|
|
x1=math.min(pos1.x, pos2.x),
|
|
|
|
x2=math.max(pos1.x, pos2.x),
|
|
|
|
y1=math.min(pos1.y, pos2.y),
|
|
|
|
y2=math.max(pos1.y, pos2.y),
|
|
|
|
z1=math.min(pos1.z, pos2.z),
|
|
|
|
z2=math.max(pos1.z, pos2.z),
|
2023-10-28 01:37:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
-- clamp to map edges
|
|
|
|
bounds = {
|
|
|
|
x1=math.max(0, bounds.x1),
|
|
|
|
x2=math.min(df.global.world.map.x_count-1, bounds.x2),
|
|
|
|
y1=math.max(0, bounds.y1),
|
|
|
|
y2=math.min(df.global.world.map.y_count-1, bounds.y2),
|
|
|
|
z1=math.max(0, bounds.z1),
|
|
|
|
z2=math.min(df.global.world.map.z_count-1, bounds.z2),
|
|
|
|
}
|
|
|
|
|
|
|
|
return bounds
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_cur_area_dims()
|
|
|
|
local bounds = get_bounds()
|
|
|
|
return bounds.x2 - bounds.x1 + 1,
|
|
|
|
bounds.y2 - bounds.y1 + 1,
|
|
|
|
bounds.z2 - bounds.z1 + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
BurrowDesignationOverlay = defclass(BurrowDesignationOverlay, overlay.OverlayWidget)
|
|
|
|
BurrowDesignationOverlay.ATTRS{
|
2023-10-28 20:26:15 -06:00
|
|
|
default_pos={x=6,y=9},
|
2023-10-28 01:37:24 -06:00
|
|
|
viewscreens='dwarfmode/Burrow/Paint',
|
|
|
|
default_enabled=true,
|
2023-10-28 20:26:15 -06:00
|
|
|
frame={w=54, h=1},
|
2023-10-28 01:37:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function BurrowDesignationOverlay:init()
|
|
|
|
self:addviews{
|
|
|
|
widgets.BannerPanel{
|
|
|
|
frame={t=0, l=0},
|
|
|
|
subviews={
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, l=1},
|
2023-10-29 15:09:42 -06:00
|
|
|
text='Double-click to fill. Shift double-click to 3D fill.',
|
2023-10-28 20:26:15 -06:00
|
|
|
auto_width=true,
|
2023-10-29 15:09:42 -06:00
|
|
|
visible=function() return not is_choosing_area() end,
|
2023-10-28 01:37:24 -06:00
|
|
|
},
|
|
|
|
widgets.Label{
|
2023-10-29 15:09:42 -06:00
|
|
|
frame={t=0, l=1},
|
2023-10-28 01:37:24 -06:00
|
|
|
text_pen=COLOR_DARKGREY,
|
|
|
|
text={
|
2023-10-29 15:09:42 -06:00
|
|
|
'3D box select enabled: ',
|
2023-10-28 01:37:24 -06:00
|
|
|
{text=function() return ('%dx%dx%d'):format(get_cur_area_dims()) end},
|
|
|
|
},
|
2023-10-28 20:26:15 -06:00
|
|
|
visible=is_choosing_area,
|
2023-10-28 01:37:24 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-10-29 15:09:42 -06:00
|
|
|
local function flood_fill(pos, erasing, do_3d, painting_burrow)
|
|
|
|
local opts = {zlevel=not do_3d}
|
2023-10-28 20:26:15 -06:00
|
|
|
if erasing then
|
2023-10-29 15:09:42 -06:00
|
|
|
burrow_tiles_flood_remove(painting_burrow, pos, opts)
|
2023-10-28 20:26:15 -06:00
|
|
|
else
|
2023-10-29 15:09:42 -06:00
|
|
|
burrow_tiles_flood_add(painting_burrow, pos, opts)
|
2023-10-28 20:26:15 -06:00
|
|
|
end
|
2023-10-28 01:37:24 -06:00
|
|
|
reset_selection_rect()
|
|
|
|
end
|
|
|
|
|
2023-10-28 20:26:15 -06:00
|
|
|
local function box_fill(bounds, erasing, painting_burrow)
|
2023-10-28 01:37:24 -06:00
|
|
|
if bounds.z1 == bounds.z2 then return end
|
2023-10-28 20:26:15 -06:00
|
|
|
if erasing then
|
2023-10-29 15:09:42 -06:00
|
|
|
burrow_tiles_box_remove(painting_burrow, bounds)
|
2023-10-28 20:26:15 -06:00
|
|
|
else
|
2023-10-29 15:09:42 -06:00
|
|
|
burrow_tiles_box_add(painting_burrow, bounds)
|
2023-10-28 20:26:15 -06:00
|
|
|
end
|
2023-10-28 01:37:24 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function BurrowDesignationOverlay:onInput(keys)
|
|
|
|
if self:inputToSubviews(keys) then
|
|
|
|
return true
|
|
|
|
-- don't perform burrow modifications immediately -- painting_burrow may not yet
|
|
|
|
-- have been initialized. instead, allow clicks to go through so that vanilla
|
|
|
|
-- behavior is triggered before we modify the burrow further
|
|
|
|
elseif keys._MOUSE_L then
|
|
|
|
local pos = dfhack.gui.getMousePos()
|
|
|
|
if pos then
|
|
|
|
local now_ms = dfhack.getTickCount()
|
|
|
|
if not same_xyz(pos, self.saved_pos) then
|
|
|
|
self.last_click_ms = now_ms
|
|
|
|
self.saved_pos = pos
|
|
|
|
else
|
|
|
|
if now_ms - self.last_click_ms <= widgets.DOUBLE_CLICK_MS then
|
|
|
|
self.last_click_ms = 0
|
2023-10-29 15:09:42 -06:00
|
|
|
self.pending_fn = curry(flood_fill, pos, if_burrow.erasing, dfhack.internal.getModifiers().shift)
|
2023-10-28 01:37:24 -06:00
|
|
|
return
|
|
|
|
else
|
|
|
|
self.last_click_ms = now_ms
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if is_choosing_area(pos) then
|
2023-10-28 20:26:15 -06:00
|
|
|
self.pending_fn = curry(box_fill, get_bounds(pos), if_burrow.erasing)
|
2023-10-28 01:37:24 -06:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function BurrowDesignationOverlay:onRenderBody(dc)
|
|
|
|
BurrowDesignationOverlay.super.onRenderBody(self, dc)
|
|
|
|
local pending_fn = self.pending_fn
|
|
|
|
self.pending_fn = nil
|
|
|
|
if pending_fn and if_burrow.painting_burrow then
|
|
|
|
pending_fn(if_burrow.painting_burrow)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
OVERLAY_WIDGETS = {
|
|
|
|
designation=BurrowDesignationOverlay,
|
|
|
|
}
|
|
|
|
|
2012-04-26 02:56:28 -06:00
|
|
|
rawset_default(_ENV, dfhack.burrows)
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
2023-10-28 20:26:15 -06:00
|
|
|
---------------------------------
|
|
|
|
-- commandline handling
|
|
|
|
--
|
|
|
|
|
2023-10-31 19:54:38 -06:00
|
|
|
local function set_add_remove(mode, which, params, _)
|
2023-10-28 20:26:15 -06:00
|
|
|
local target_burrow = table.remove(params, 1)
|
2023-10-31 19:54:38 -06:00
|
|
|
_ENV[('burrow_%s_%s'):format(mode, which)](target_burrow, params)
|
2023-10-28 20:26:15 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local function tiles_box_add_remove(which, params, opts)
|
|
|
|
local target_burrow = table.remove(params, 1)
|
|
|
|
local pos1 = argparse.coords(params[1] or 'here', 'pos')
|
|
|
|
local pos2 = opts.cursor or argparse.coords(params[2] or 'here', 'pos')
|
|
|
|
local bounds = get_bounds(pos1, pos2)
|
2023-10-31 19:54:38 -06:00
|
|
|
_ENV['burrow_tiles_box_'..which](target_burrow, bounds)
|
2023-10-28 20:26:15 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local function tiles_flood_add_remove(which, params, opts)
|
|
|
|
local target_burrow = table.remove(params, 1)
|
|
|
|
local pos = opts.cursor or argparse.coords('here', 'pos')
|
2023-10-31 19:54:38 -06:00
|
|
|
_ENV['burrow_tiles_flood_'..which](target_burrow, pos, opts)
|
2023-10-28 20:26:15 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local function run_command(mode, command, params, opts)
|
|
|
|
if mode == 'tiles' then
|
|
|
|
if command == 'clear' then
|
2023-10-31 19:54:38 -06:00
|
|
|
burrow_tiles_clear(params)
|
2023-10-28 20:26:15 -06:00
|
|
|
elseif command == 'set' or command == 'add' or command == 'remove' then
|
2023-10-31 19:54:38 -06:00
|
|
|
set_add_remove('tiles', command, params, opts)
|
2023-10-28 20:26:15 -06:00
|
|
|
elseif command == 'box-add' or command == 'box-remove' then
|
2023-10-31 19:54:38 -06:00
|
|
|
tiles_box_add_remove(command:sub(5), params, opts)
|
2023-10-28 20:26:15 -06:00
|
|
|
elseif command == 'flood-add' or command == 'flood-remove' then
|
2023-10-31 19:54:38 -06:00
|
|
|
tiles_flood_add_remove(command:sub(7), params, opts)
|
2023-10-28 20:26:15 -06:00
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
elseif mode == 'units' then
|
|
|
|
if command == 'clear' then
|
2023-10-31 19:54:38 -06:00
|
|
|
burrow_units_clear(params)
|
2023-10-28 20:26:15 -06:00
|
|
|
elseif command == 'set' or command == 'add' or command == 'remove' then
|
2023-10-31 19:54:38 -06:00
|
|
|
set_add_remove('units', command, params, opts)
|
2023-10-28 20:26:15 -06:00
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2023-10-31 19:54:38 -06:00
|
|
|
return true
|
2023-10-28 20:26:15 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function parse_commandline(...)
|
|
|
|
local args, opts = {...}, {}
|
|
|
|
|
|
|
|
if args[1] == 'help' then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local positionals = argparse.processArgsGetopt(args, {
|
|
|
|
{'c', 'cursor', hasArg=true,
|
|
|
|
handler=function(optarg) opts.cursor = argparse.coords(optarg, 'cursor') end},
|
|
|
|
{'h', 'help', handler=function() opts.help = true end},
|
|
|
|
{'z', 'cur-zlevel', handler=function() opts.zlevel = true end},
|
|
|
|
})
|
|
|
|
|
|
|
|
if opts.help then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local mode = table.remove(positionals, 1)
|
|
|
|
local command = table.remove(positionals, 1)
|
|
|
|
local ret = run_command(mode, command, positionals, opts)
|
|
|
|
|
|
|
|
if not ret then return false end
|
|
|
|
|
2023-10-31 19:54:38 -06:00
|
|
|
print('done')
|
2023-10-28 20:26:15 -06:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-04-12 12:48:19 -06:00
|
|
|
return _ENV
|