2020-08-12 16:12:53 -06:00
|
|
|
local _ENV = mkmodule('plugins.buildingplan')
|
|
|
|
|
2020-08-16 00:03:49 -06:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Native functions:
|
|
|
|
|
2020-10-16 14:52:23 -06:00
|
|
|
* bool isPlannableBuilding(df::building_type type, int16_t subtype, int32_t custom)
|
2020-11-13 11:18:54 -07:00
|
|
|
* bool isPlannedBuilding(df::building *bld)
|
2020-08-16 00:03:49 -06:00
|
|
|
* void addPlannedBuilding(df::building *bld)
|
|
|
|
* void doCycle()
|
2020-10-16 14:52:23 -06:00
|
|
|
* void scheduleCycle()
|
2020-08-16 00:03:49 -06:00
|
|
|
|
|
|
|
--]]
|
|
|
|
|
2023-01-17 05:05:17 -07:00
|
|
|
local argparse = require('argparse')
|
2023-02-09 01:13:53 -07:00
|
|
|
local gui = require('gui')
|
|
|
|
local guidm = require('gui.dwarfmode')
|
|
|
|
local overlay = require('plugins.overlay')
|
|
|
|
local utils = require('utils')
|
|
|
|
local widgets = require('gui.widgets')
|
2020-10-16 15:03:05 -06:00
|
|
|
require('dfhack.buildings')
|
|
|
|
|
2023-01-17 05:05:17 -07:00
|
|
|
local function process_args(opts, args)
|
|
|
|
if args[1] == 'help' then
|
|
|
|
opts.help = true
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return argparse.processArgsGetopt(args, {
|
|
|
|
{'h', 'help', handler=function() opts.help = true end},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function parse_commandline(...)
|
|
|
|
local args, opts = {...}, {}
|
|
|
|
local positionals = process_args(opts, args)
|
|
|
|
|
|
|
|
if opts.help then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-02-08 19:47:10 -07:00
|
|
|
local command = table.remove(positionals, 1)
|
|
|
|
if not command or command == 'status' then
|
|
|
|
printStatus()
|
|
|
|
elseif command == 'set' then
|
|
|
|
setSetting(positionals[1], positionals[2] == 'true')
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-01-17 05:05:17 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-10-16 15:08:52 -06:00
|
|
|
function get_num_filters(btype, subtype, custom)
|
2023-02-09 01:13:53 -07:00
|
|
|
local filters = dfhack.buildings.getFiltersByType({}, btype, subtype, custom)
|
|
|
|
return filters and #filters or 0
|
2020-10-16 15:08:52 -06:00
|
|
|
end
|
|
|
|
|
2023-02-15 17:54:38 -07:00
|
|
|
function get_job_item(btype, subtype, custom, index)
|
|
|
|
local filters = dfhack.buildings.getFiltersByType({}, btype, subtype, custom)
|
|
|
|
if not filters or not filters[index] then return nil end
|
|
|
|
local obj = df.job_item:new()
|
|
|
|
obj:assign(filters[index])
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
|
|
|
|
local reset_counts_flag = false
|
|
|
|
function reset_counts()
|
|
|
|
reset_counts_flag = true
|
|
|
|
end
|
|
|
|
|
2023-02-09 01:13:53 -07:00
|
|
|
--------------------------------
|
2023-02-15 20:10:42 -07:00
|
|
|
-- PlannerOverlay
|
2023-02-09 01:13:53 -07:00
|
|
|
--
|
|
|
|
|
|
|
|
local uibs = df.global.buildreq
|
|
|
|
|
|
|
|
local function cur_building_has_no_area()
|
|
|
|
if uibs.building_type == df.building_type.Construction then return false end
|
|
|
|
local filters = dfhack.buildings.getFiltersByType({},
|
|
|
|
uibs.building_type, uibs.building_subtype, uibs.custom_type)
|
|
|
|
-- this works because all variable-size buildings have either no item
|
|
|
|
-- filters or a quantity of -1 for their first (and only) item
|
|
|
|
return filters and filters[1] and (not filters[1].quantity or filters[1].quantity > 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_choosing_area()
|
|
|
|
return uibs.selection_pos.x >= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_cur_area_dims()
|
2023-02-11 03:10:07 -07:00
|
|
|
if not is_choosing_area() then return 1, 1, 1 end
|
2023-02-09 01:13:53 -07:00
|
|
|
return math.abs(uibs.selection_pos.x - uibs.pos.x) + 1,
|
2023-02-11 03:10:07 -07:00
|
|
|
math.abs(uibs.selection_pos.y - uibs.pos.y) + 1,
|
|
|
|
math.abs(uibs.selection_pos.z - uibs.pos.z) + 1
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_cur_filters()
|
|
|
|
return dfhack.buildings.getFiltersByType({}, uibs.building_type,
|
|
|
|
uibs.building_subtype, uibs.custom_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_plannable()
|
|
|
|
return get_cur_filters() and
|
|
|
|
not (uibs.building_type == df.building_type.Construction
|
|
|
|
and uibs.building_subtype == df.construction_type.TrackNSEW)
|
|
|
|
end
|
|
|
|
|
2023-02-11 03:10:07 -07:00
|
|
|
local function is_stairs()
|
|
|
|
return uibs.building_type == df.building_type.Construction
|
|
|
|
and uibs.building_subtype == df.construction_type.UpDownStair
|
|
|
|
end
|
|
|
|
|
2023-02-09 01:13:53 -07:00
|
|
|
local direction_panel_frame = {t=4, h=13, w=46, r=28}
|
|
|
|
|
|
|
|
local direction_panel_types = utils.invert{
|
|
|
|
df.building_type.Bridge,
|
|
|
|
df.building_type.ScrewPump,
|
|
|
|
df.building_type.WaterWheel,
|
|
|
|
df.building_type.AxleHorizontal,
|
|
|
|
df.building_type.Rollers,
|
|
|
|
}
|
|
|
|
|
|
|
|
local function has_direction_panel()
|
|
|
|
return direction_panel_types[uibs.building_type]
|
|
|
|
or (uibs.building_type == df.building_type.Trap
|
|
|
|
and uibs.building_subtype == df.trap_type.TrackStop)
|
|
|
|
end
|
|
|
|
|
2023-02-11 03:10:07 -07:00
|
|
|
local pressure_plate_panel_frame = {t=4, h=37, w=46, r=28}
|
|
|
|
|
|
|
|
local function has_pressure_plate_panel()
|
|
|
|
return uibs.building_type == df.building_type.Trap
|
|
|
|
and uibs.building_subtype == df.trap_type.PressurePlate
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_over_options_panel()
|
|
|
|
local frame = nil
|
|
|
|
if has_direction_panel() then
|
|
|
|
frame = direction_panel_frame
|
|
|
|
elseif has_pressure_plate_panel() then
|
|
|
|
frame = pressure_plate_panel_frame
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local v = widgets.Widget{frame=frame}
|
2023-02-09 01:13:53 -07:00
|
|
|
local rect = gui.mkdims_wh(0, 0, dfhack.screen.getWindowSize())
|
|
|
|
v:updateLayout(gui.ViewRect{rect=rect})
|
|
|
|
return v:getMousePos()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function to_title_case(str)
|
|
|
|
str = str:gsub('(%a)([%w_]*)',
|
|
|
|
function (first, rest) return first:upper()..rest:lower() end)
|
|
|
|
str = str:gsub('_', ' ')
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2023-02-15 17:54:38 -07:00
|
|
|
ItemLine = defclass(ItemLine, widgets.Panel)
|
|
|
|
ItemLine.ATTRS{
|
|
|
|
idx=DEFAULT_NIL,
|
|
|
|
}
|
|
|
|
|
|
|
|
function ItemLine:init()
|
|
|
|
self.frame.h = 1
|
|
|
|
self.visible = function() return #get_cur_filters() >= self.idx end
|
|
|
|
self:addviews{
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, l=0},
|
|
|
|
text={{text=function() return self:get_item_line_text() end}},
|
|
|
|
},
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, l=22},
|
|
|
|
text='[filter][x]',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function ItemLine:reset()
|
|
|
|
self.desc = nil
|
|
|
|
self.available = nil
|
|
|
|
end
|
|
|
|
|
2023-02-15 20:10:42 -07:00
|
|
|
function get_desc(filter)
|
2023-02-09 01:13:53 -07:00
|
|
|
local desc = 'Unknown'
|
2023-02-16 04:08:55 -07:00
|
|
|
if filter.has_tool_use and filter.has_tool_use > -1 then
|
2023-02-09 01:13:53 -07:00
|
|
|
desc = to_title_case(df.tool_uses[filter.has_tool_use])
|
2023-02-16 04:08:55 -07:00
|
|
|
elseif filter.flags2 and filter.flags2.screw then
|
|
|
|
desc = 'Screw'
|
|
|
|
elseif filter.item_type and filter.item_type > -1 then
|
2023-02-09 01:13:53 -07:00
|
|
|
desc = to_title_case(df.item_type[filter.item_type])
|
2023-02-16 04:08:55 -07:00
|
|
|
elseif filter.vector_id and filter.vector_id > -1 then
|
|
|
|
desc = to_title_case(df.job_item_vector_id[filter.vector_id])
|
|
|
|
elseif filter.flags2 and filter.flags2.building_material then
|
2023-02-16 18:25:15 -07:00
|
|
|
desc = 'Building material';
|
2023-02-09 01:13:53 -07:00
|
|
|
if filter.flags2.fire_safe then
|
2023-02-15 20:10:42 -07:00
|
|
|
desc = 'Fire-safe material';
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
if filter.flags2.magma_safe then
|
2023-02-15 20:10:42 -07:00
|
|
|
desc = 'Magma-safe material';
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-13 17:24:10 -07:00
|
|
|
if desc:endswith('s') then
|
|
|
|
desc = desc:sub(1,-2)
|
|
|
|
end
|
|
|
|
if desc == 'Trappart' then
|
|
|
|
desc = 'Mechanism'
|
2023-02-16 04:08:55 -07:00
|
|
|
elseif desc == 'Wood' then
|
|
|
|
desc = 'Log'
|
2023-02-13 17:24:10 -07:00
|
|
|
end
|
2023-02-15 17:54:38 -07:00
|
|
|
return desc
|
|
|
|
end
|
2023-02-13 17:24:10 -07:00
|
|
|
|
2023-02-15 17:54:38 -07:00
|
|
|
local function get_quantity(filter)
|
2023-02-09 01:13:53 -07:00
|
|
|
local quantity = filter.quantity or 1
|
2023-02-11 03:10:07 -07:00
|
|
|
local dimx, dimy, dimz = get_cur_area_dims()
|
2023-02-09 01:13:53 -07:00
|
|
|
if quantity < 1 then
|
2023-02-11 03:10:07 -07:00
|
|
|
quantity = (((dimx * dimy) // 4) + 1) * dimz
|
2023-02-09 01:13:53 -07:00
|
|
|
else
|
2023-02-11 03:10:07 -07:00
|
|
|
quantity = quantity * dimx * dimy * dimz
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
2023-02-15 17:54:38 -07:00
|
|
|
return quantity
|
|
|
|
end
|
2023-02-13 17:24:10 -07:00
|
|
|
|
2023-02-15 17:54:38 -07:00
|
|
|
function ItemLine:get_item_line_text()
|
|
|
|
local idx = self.idx
|
|
|
|
local filter = get_cur_filters()[idx]
|
|
|
|
local quantity = get_quantity(filter)
|
|
|
|
|
|
|
|
self.desc = self.desc or get_desc(filter)
|
|
|
|
local line = ('%d %s%s'):format(quantity, self.desc, quantity == 1 and '' or 's')
|
|
|
|
|
|
|
|
self.available = self.available or countAvailableItems(uibs.building_type,
|
2023-02-13 17:24:10 -07:00
|
|
|
uibs.building_subtype, uibs.custom_type, idx - 1)
|
2023-02-15 17:54:38 -07:00
|
|
|
local note = self.available >= quantity and
|
2023-02-13 17:24:10 -07:00
|
|
|
'Can build now' or 'Will wait for item'
|
|
|
|
|
2023-02-16 18:25:15 -07:00
|
|
|
return ('%-21s%s%s'):format(line:sub(1,21), (' '):rep(14), note)
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
|
2023-02-15 17:54:38 -07:00
|
|
|
function ItemLine:reduce_quantity()
|
|
|
|
if not self.available then return end
|
|
|
|
local filter = get_cur_filters()[self.idx]
|
|
|
|
self.available = math.max(0, self.available - get_quantity(filter))
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
2023-02-08 20:26:39 -07:00
|
|
|
|
2023-02-16 18:25:15 -07:00
|
|
|
local function get_placement_errors()
|
|
|
|
local out = ''
|
|
|
|
for _,str in ipairs(uibs.errors) do
|
|
|
|
if #out > 0 then out = out .. NEWLINE end
|
|
|
|
out = out .. str.value
|
|
|
|
end
|
|
|
|
return out
|
|
|
|
end
|
|
|
|
|
2023-02-08 20:26:39 -07:00
|
|
|
PlannerOverlay = defclass(PlannerOverlay, overlay.OverlayWidget)
|
|
|
|
PlannerOverlay.ATTRS{
|
2023-02-16 18:25:15 -07:00
|
|
|
default_pos={x=5,y=9},
|
2023-02-08 20:26:39 -07:00
|
|
|
default_enabled=true,
|
|
|
|
viewscreens='dwarfmode/Building/Placement',
|
2023-02-16 18:25:15 -07:00
|
|
|
frame={w=56, h=18},
|
2023-02-08 20:26:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function PlannerOverlay:init()
|
2023-02-16 18:25:15 -07:00
|
|
|
local main_panel = widgets.Panel{
|
|
|
|
frame={t=0, l=0, r=0, h=14},
|
|
|
|
frame_style=gui.MEDIUM_FRAME,
|
|
|
|
frame_background=gui.CLEAR_PEN,
|
|
|
|
}
|
|
|
|
|
|
|
|
main_panel:addviews{
|
2023-02-09 01:13:53 -07:00
|
|
|
widgets.Label{
|
|
|
|
frame={},
|
|
|
|
auto_width=true,
|
|
|
|
text='No items required.',
|
|
|
|
visible=function() return #get_cur_filters() == 0 end,
|
2023-02-08 20:26:39 -07:00
|
|
|
},
|
2023-02-16 18:25:15 -07:00
|
|
|
ItemLine{view_id='item1', frame={t=0, l=0, r=0}, idx=1},
|
|
|
|
ItemLine{view_id='item2', frame={t=2, l=0, r=0}, idx=2},
|
|
|
|
ItemLine{view_id='item3', frame={t=4, l=0, r=0}, idx=3},
|
|
|
|
ItemLine{view_id='item4', frame={t=6, l=0, r=0}, idx=4},
|
2023-02-11 03:10:07 -07:00
|
|
|
widgets.CycleHotkeyLabel{
|
2023-02-15 20:10:42 -07:00
|
|
|
view_id='stairs_top_subtype',
|
2023-02-16 18:25:15 -07:00
|
|
|
frame={t=4, l=4},
|
2023-02-15 20:10:42 -07:00
|
|
|
key='CUSTOM_R',
|
|
|
|
label='Top Stair Type: ',
|
2023-02-11 03:10:07 -07:00
|
|
|
visible=is_stairs,
|
|
|
|
options={
|
|
|
|
{label='Auto', value='auto'},
|
|
|
|
{label='UpDown', value=df.construction_type.UpDownStair},
|
|
|
|
{label='Down', value=df.construction_type.DownStair},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
widgets.CycleHotkeyLabel {
|
2023-02-15 20:10:42 -07:00
|
|
|
view_id='stairs_bottom_subtype',
|
2023-02-16 18:25:15 -07:00
|
|
|
frame={t=5, l=4},
|
2023-02-15 20:10:42 -07:00
|
|
|
key='CUSTOM_B',
|
|
|
|
label='Bottom Stair Type: ',
|
2023-02-11 03:10:07 -07:00
|
|
|
visible=is_stairs,
|
|
|
|
options={
|
|
|
|
{label='Auto', value='auto'},
|
|
|
|
{label='UpDown', value=df.construction_type.UpDownStair},
|
|
|
|
{label='Up', value=df.construction_type.UpStair},
|
|
|
|
},
|
|
|
|
},
|
2023-02-09 01:13:53 -07:00
|
|
|
widgets.Label{
|
2023-02-16 18:25:15 -07:00
|
|
|
frame={b=3, l=17},
|
2023-02-09 01:13:53 -07:00
|
|
|
text={
|
|
|
|
'Selected area: ',
|
|
|
|
{text=function()
|
2023-02-13 17:24:10 -07:00
|
|
|
return ('%dx%dx%d'):format(get_cur_area_dims())
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
visible=is_choosing_area,
|
2023-02-08 20:26:39 -07:00
|
|
|
},
|
2023-02-15 20:10:42 -07:00
|
|
|
widgets.CycleHotkeyLabel{
|
|
|
|
view_id='safety',
|
2023-02-16 18:25:15 -07:00
|
|
|
frame={b=0, l=2},
|
|
|
|
key='CUSTOM_G',
|
|
|
|
label='Safety: ',
|
2023-02-15 20:10:42 -07:00
|
|
|
options={
|
|
|
|
{label='None', value='none'},
|
|
|
|
{label='Magma', value='magma'},
|
|
|
|
{label='Fire', value='fire'},
|
|
|
|
},
|
|
|
|
},
|
2023-02-16 18:25:15 -07:00
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame={b=1, l=0},
|
|
|
|
key='SELECT',
|
|
|
|
label='Choose item',
|
|
|
|
},
|
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame={b=1, l=21},
|
|
|
|
key='CUSTOM_F',
|
|
|
|
label='Filter',
|
|
|
|
},
|
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame={b=1, l=33},
|
|
|
|
key='CUSTOM_X',
|
|
|
|
label='Clear filter',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
local error_panel = widgets.ResizingPanel{
|
|
|
|
view_id='errors',
|
|
|
|
frame={t=14, l=0, r=0, h=3},
|
|
|
|
frame_style=gui.MEDIUM_FRAME,
|
|
|
|
frame_background=gui.CLEAR_PEN,
|
|
|
|
}
|
|
|
|
|
|
|
|
error_panel:addviews{
|
|
|
|
widgets.WrappedLabel{
|
|
|
|
text_pen=COLOR_LIGHTRED,
|
|
|
|
text_to_wrap=get_placement_errors,
|
|
|
|
},
|
|
|
|
widgets.Label{
|
|
|
|
text_pen=COLOR_GREEN,
|
|
|
|
text='OK to build',
|
|
|
|
visible=function() return #uibs.errors == 0 end,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
self:addviews{
|
|
|
|
main_panel,
|
|
|
|
error_panel,
|
2023-02-08 20:26:39 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-02-15 17:54:38 -07:00
|
|
|
function PlannerOverlay:reset()
|
|
|
|
self.subviews.item1:reset()
|
|
|
|
self.subviews.item2:reset()
|
|
|
|
self.subviews.item3:reset()
|
|
|
|
self.subviews.item4:reset()
|
|
|
|
reset_counts_flag = false
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function PlannerOverlay:onInput(keys)
|
|
|
|
if not is_plannable() then return false end
|
|
|
|
if keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
|
2023-02-15 17:54:38 -07:00
|
|
|
if uibs.selection_pos:isValid() then
|
|
|
|
uibs.selection_pos:clear()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
self:reset()
|
2023-02-09 01:13:53 -07:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
if PlannerOverlay.super.onInput(self, keys) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if keys._MOUSE_L_DOWN then
|
2023-02-11 03:10:07 -07:00
|
|
|
if is_over_options_panel() then return false end
|
2023-02-09 01:13:53 -07:00
|
|
|
if self:getMouseFramePos() then return true end
|
|
|
|
if #uibs.errors > 0 then return true end
|
|
|
|
local pos = dfhack.gui.getMousePos()
|
|
|
|
if pos then
|
|
|
|
if is_choosing_area() or cur_building_has_no_area() then
|
|
|
|
if #get_cur_filters() == 0 then
|
|
|
|
return false -- we don't add value; let the game place it
|
|
|
|
end
|
2023-02-15 17:54:38 -07:00
|
|
|
self.subviews.item1:reduce_quantity()
|
|
|
|
self.subviews.item2:reduce_quantity()
|
|
|
|
self.subviews.item3:reduce_quantity()
|
|
|
|
self.subviews.item4:reduce_quantity()
|
2023-02-09 01:13:53 -07:00
|
|
|
self:place_building()
|
|
|
|
uibs.selection_pos:clear()
|
|
|
|
return true
|
|
|
|
elseif not is_choosing_area() then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return keys._MOUSE_L
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlannerOverlay:render(dc)
|
|
|
|
if not is_plannable() then return end
|
2023-02-16 18:25:15 -07:00
|
|
|
self.subviews.errors:updateLayout()
|
2023-02-09 01:13:53 -07:00
|
|
|
PlannerOverlay.super.render(self, dc)
|
|
|
|
end
|
|
|
|
|
|
|
|
local to_pen = dfhack.pen.parse
|
|
|
|
local GOOD_PEN = to_pen{ch='o', fg=COLOR_GREEN,
|
|
|
|
tile=dfhack.screen.findGraphicsTile('CURSORS', 1, 2)}
|
|
|
|
local BAD_PEN = to_pen{ch='X', fg=COLOR_RED,
|
|
|
|
tile=dfhack.screen.findGraphicsTile('CURSORS', 3, 0)}
|
|
|
|
|
|
|
|
function PlannerOverlay:onRenderFrame(dc, rect)
|
|
|
|
PlannerOverlay.super.onRenderFrame(self, dc, rect)
|
|
|
|
|
2023-02-15 17:54:38 -07:00
|
|
|
if reset_counts_flag then
|
|
|
|
self:reset()
|
|
|
|
end
|
|
|
|
|
2023-02-09 01:13:53 -07:00
|
|
|
if not is_choosing_area() then return end
|
|
|
|
|
|
|
|
local bounds = {
|
|
|
|
x1 = math.min(uibs.selection_pos.x, uibs.pos.x),
|
|
|
|
x2 = math.max(uibs.selection_pos.x, uibs.pos.x),
|
|
|
|
y1 = math.min(uibs.selection_pos.y, uibs.pos.y),
|
|
|
|
y2 = math.max(uibs.selection_pos.y, uibs.pos.y),
|
|
|
|
}
|
|
|
|
|
|
|
|
local pen = #uibs.errors > 0 and BAD_PEN or GOOD_PEN
|
|
|
|
|
|
|
|
local function get_overlay_pen(pos)
|
|
|
|
return pen
|
|
|
|
end
|
|
|
|
|
|
|
|
guidm.renderMapOverlay(get_overlay_pen, bounds)
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlannerOverlay:place_building()
|
|
|
|
local direction = uibs.direction
|
2023-02-11 03:10:07 -07:00
|
|
|
local width, height, depth = get_cur_area_dims()
|
2023-02-09 01:13:53 -07:00
|
|
|
local _, adjusted_width, adjusted_height = dfhack.buildings.getCorrectSize(
|
|
|
|
width, height, uibs.building_type, uibs.building_subtype,
|
|
|
|
uibs.custom_type, direction)
|
2023-02-11 03:10:07 -07:00
|
|
|
-- get the upper-left corner of the building/area at min z-level
|
|
|
|
local has_selection = is_choosing_area()
|
|
|
|
local start_pos = xyz2pos(
|
2023-02-09 01:13:53 -07:00
|
|
|
has_selection and math.min(uibs.selection_pos.x, uibs.pos.x) or uibs.pos.x - adjusted_width//2,
|
|
|
|
has_selection and math.min(uibs.selection_pos.y, uibs.pos.y) or uibs.pos.y - adjusted_height//2,
|
2023-02-11 03:10:07 -07:00
|
|
|
has_selection and math.min(uibs.selection_pos.z, uibs.pos.z) or uibs.pos.z
|
2023-02-09 01:13:53 -07:00
|
|
|
)
|
2023-02-11 03:10:07 -07:00
|
|
|
if uibs.building_type == df.building_type.ScrewPump then
|
|
|
|
if direction == df.screw_pump_direction.FromSouth then
|
|
|
|
start_pos.y = start_pos.y + 1
|
|
|
|
elseif direction == df.screw_pump_direction.FromEast then
|
|
|
|
start_pos.x = start_pos.x + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local min_x, max_x = start_pos.x, start_pos.x
|
|
|
|
local min_y, max_y = start_pos.y, start_pos.y
|
|
|
|
local min_z, max_z = start_pos.z, start_pos.z
|
|
|
|
if adjusted_width == 1 and adjusted_height == 1
|
|
|
|
and (width > 1 or height > 1 or depth > 1) then
|
2023-02-09 01:13:53 -07:00
|
|
|
max_x = min_x + width - 1
|
|
|
|
max_y = min_y + height - 1
|
2023-02-11 03:10:07 -07:00
|
|
|
max_z = math.max(uibs.selection_pos.z, uibs.pos.z)
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
local blds = {}
|
2023-02-11 03:10:07 -07:00
|
|
|
local subtype = uibs.building_subtype
|
|
|
|
for z=min_z,max_z do for y=min_y,max_y do for x=min_x,max_x do
|
|
|
|
local pos = xyz2pos(x, y, z)
|
|
|
|
if is_stairs() then
|
|
|
|
if z == min_z then
|
|
|
|
subtype = self.subviews.stairs_bottom_subtype:getOptionValue()
|
|
|
|
if subtype == 'auto' then
|
|
|
|
local tt = dfhack.maps.getTileType(pos)
|
|
|
|
local shape = df.tiletype.attrs[tt].shape
|
|
|
|
if shape == df.tiletype_shape.STAIR_DOWN then
|
|
|
|
subtype = uibs.building_subtype
|
|
|
|
else
|
|
|
|
subtype = df.construction_type.UpStair
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif z == max_z then
|
|
|
|
subtype = self.subviews.stairs_top_subtype:getOptionValue()
|
|
|
|
if subtype == 'auto' then
|
|
|
|
local tt = dfhack.maps.getTileType(pos)
|
|
|
|
local shape = df.tiletype.attrs[tt].shape
|
|
|
|
if shape == df.tiletype_shape.STAIR_UP then
|
|
|
|
subtype = uibs.building_subtype
|
|
|
|
else
|
|
|
|
subtype = df.construction_type.DownStair
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
subtype = uibs.building_subtype
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local bld, err = dfhack.buildings.constructBuilding{pos=pos,
|
|
|
|
type=uibs.building_type, subtype=subtype, custom=uibs.custom_type,
|
2023-02-09 01:13:53 -07:00
|
|
|
width=adjusted_width, height=adjusted_height, direction=direction}
|
|
|
|
if err then
|
|
|
|
for _,b in ipairs(blds) do
|
|
|
|
dfhack.buildings.deconstruct(b)
|
|
|
|
end
|
2023-02-11 03:10:07 -07:00
|
|
|
dfhack.printerr(err .. (' (%d, %d, %d)'):format(pos.x, pos.y, pos.z))
|
2023-02-09 01:13:53 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
-- assign fields for the types that need them. we can't pass them all in
|
|
|
|
-- to the call to constructBuilding since attempting to assign unrelated
|
|
|
|
-- fields to building types that don't support them causes errors.
|
|
|
|
for k,v in pairs(bld) do
|
|
|
|
if k == 'friction' then bld.friction = uibs.friction end
|
|
|
|
if k == 'use_dump' then bld.use_dump = uibs.use_dump end
|
|
|
|
if k == 'dump_x_shift' then bld.dump_x_shift = uibs.dump_x_shift end
|
|
|
|
if k == 'dump_y_shift' then bld.dump_y_shift = uibs.dump_y_shift end
|
|
|
|
if k == 'speed' then bld.speed = uibs.speed end
|
|
|
|
end
|
|
|
|
table.insert(blds, bld)
|
2023-02-11 03:10:07 -07:00
|
|
|
end end end
|
2023-02-09 01:13:53 -07:00
|
|
|
for _,bld in ipairs(blds) do
|
|
|
|
addPlannedBuilding(bld)
|
|
|
|
end
|
2023-02-13 17:24:10 -07:00
|
|
|
scheduleCycle()
|
2023-02-09 01:13:53 -07:00
|
|
|
end
|
|
|
|
|
2023-02-15 20:10:42 -07:00
|
|
|
--------------------------------
|
|
|
|
-- InspectorOverlay
|
|
|
|
--
|
|
|
|
|
|
|
|
local function get_building_filters()
|
|
|
|
local bld = dfhack.gui.getSelectedBuilding()
|
|
|
|
return dfhack.buildings.getFiltersByType({},
|
|
|
|
bld:getType(), bld:getSubtype(), bld:getCustomType())
|
|
|
|
end
|
|
|
|
|
|
|
|
InspectorLine = defclass(InspectorLine, widgets.Panel)
|
|
|
|
InspectorLine.ATTRS{
|
|
|
|
idx=DEFAULT_NIL,
|
|
|
|
}
|
|
|
|
|
|
|
|
function InspectorLine:init()
|
|
|
|
self.frame.h = 2
|
|
|
|
self.visible = function() return #get_building_filters() >= self.idx end
|
|
|
|
self:addviews{
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, l=0},
|
2023-02-16 04:08:55 -07:00
|
|
|
text={{text=function() return getDescString(dfhack.gui.getSelectedBuilding(), self.idx-1) end}},
|
2023-02-15 20:10:42 -07:00
|
|
|
},
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=1, l=2},
|
|
|
|
text={{text=self:callback('get_status_line')}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function InspectorLine:get_status_line()
|
|
|
|
local queue_pos = getQueuePosition(dfhack.gui.getSelectedBuilding(), self.idx-1)
|
|
|
|
if queue_pos <= 0 then
|
|
|
|
return 'Item attached'
|
|
|
|
end
|
|
|
|
return ('Position in line: %d'):format(queue_pos)
|
|
|
|
end
|
|
|
|
|
2023-02-08 20:26:39 -07:00
|
|
|
InspectorOverlay = defclass(InspectorOverlay, overlay.OverlayWidget)
|
|
|
|
InspectorOverlay.ATTRS{
|
|
|
|
default_pos={x=-41,y=14},
|
|
|
|
default_enabled=true,
|
|
|
|
viewscreens='dwarfmode/ViewSheets/BUILDING',
|
2023-02-15 20:10:42 -07:00
|
|
|
frame={w=30, h=14},
|
2023-02-08 20:26:39 -07:00
|
|
|
frame_style=gui.MEDIUM_FRAME,
|
|
|
|
frame_background=gui.CLEAR_PEN,
|
|
|
|
}
|
|
|
|
|
|
|
|
function InspectorOverlay:init()
|
|
|
|
self:addviews{
|
|
|
|
widgets.Label{
|
|
|
|
frame={t=0, l=0},
|
|
|
|
text='Waiting for items:',
|
|
|
|
},
|
2023-02-15 20:10:42 -07:00
|
|
|
InspectorLine{view_id='item1', frame={t=2, l=0}, idx=1},
|
|
|
|
InspectorLine{view_id='item2', frame={t=4, l=0}, idx=2},
|
|
|
|
InspectorLine{view_id='item3', frame={t=6, l=0}, idx=3},
|
|
|
|
InspectorLine{view_id='item4', frame={t=8, l=0}, idx=4},
|
2023-02-11 03:21:19 -07:00
|
|
|
widgets.HotkeyLabel{
|
2023-02-15 20:10:42 -07:00
|
|
|
frame={t=10, l=0},
|
2023-02-11 03:21:19 -07:00
|
|
|
label='adjust filters',
|
|
|
|
key='CUSTOM_CTRL_F',
|
|
|
|
},
|
|
|
|
widgets.HotkeyLabel{
|
2023-02-15 20:10:42 -07:00
|
|
|
frame={t=11, l=0},
|
2023-02-08 20:26:39 -07:00
|
|
|
label='make top priority',
|
|
|
|
key='CUSTOM_CTRL_T',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function InspectorOverlay:onInput(keys)
|
|
|
|
if not isPlannedBuilding(dfhack.gui.getSelectedBuilding()) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return InspectorOverlay.super.onInput(self, keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
function InspectorOverlay:render(dc)
|
|
|
|
if not isPlannedBuilding(dfhack.gui.getSelectedBuilding()) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
InspectorOverlay.super.render(self, dc)
|
|
|
|
end
|
|
|
|
|
|
|
|
OVERLAY_WIDGETS = {
|
|
|
|
planner=PlannerOverlay,
|
|
|
|
inspector=InspectorOverlay,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-17 05:05:17 -07:00
|
|
|
local dialogs = require('gui.dialogs')
|
|
|
|
local guidm = require('gui.dwarfmode')
|
|
|
|
|
2020-10-31 03:25:26 -06:00
|
|
|
-- returns whether the items matched by the specified filter can have a quality
|
|
|
|
-- rating. This also conveniently indicates whether an item can be decorated.
|
|
|
|
-- does not need the core suspended
|
|
|
|
-- reverse_idx is 0-based and is expected to be counted from the *last* filter
|
|
|
|
function item_can_be_improved(btype, subtype, custom, reverse_idx)
|
|
|
|
local filter = get_filter(btype, subtype, custom, reverse_idx)
|
|
|
|
if filter.flags2 and filter.flags2.building_material then
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
return filter.item_type ~= df.item_type.WOOD and
|
|
|
|
filter.item_type ~= df.item_type.BLOCKS and
|
|
|
|
filter.item_type ~= df.item_type.BAR and
|
|
|
|
filter.item_type ~= df.item_type.BOULDER
|
|
|
|
end
|
|
|
|
|
2020-10-16 15:03:05 -06:00
|
|
|
|
2020-10-22 22:37:49 -06:00
|
|
|
--
|
|
|
|
-- GlobalSettings dialog
|
|
|
|
--
|
|
|
|
|
|
|
|
local GlobalSettings = defclass(GlobalSettings, dialogs.MessageBox)
|
|
|
|
GlobalSettings.focus_path = 'buildingplan_globalsettings'
|
|
|
|
|
|
|
|
GlobalSettings.ATTRS{
|
|
|
|
settings = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function GlobalSettings:onDismiss()
|
|
|
|
for k,v in pairs(self.settings) do
|
|
|
|
-- call back into C++ to save changes
|
|
|
|
setSetting(k, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- does not need the core suspended.
|
|
|
|
function show_global_settings_dialog(settings)
|
|
|
|
GlobalSettings{
|
2023-02-15 20:10:42 -07:00
|
|
|
frame_title='Buildingplan Global Settings',
|
2020-10-22 22:37:49 -06:00
|
|
|
settings=settings,
|
|
|
|
}:show()
|
|
|
|
end
|
|
|
|
|
|
|
|
function GlobalSettings:toggle_setting(name)
|
|
|
|
self.settings[name] = not self.settings[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
function GlobalSettings:get_setting_string(name)
|
|
|
|
if self.settings[name] then return 'On' end
|
|
|
|
return 'Off'
|
|
|
|
end
|
|
|
|
|
2020-10-28 22:02:59 -06:00
|
|
|
function GlobalSettings:get_setting_pen(name)
|
|
|
|
if self.settings[name] then return COLOR_LIGHTGREEN end
|
|
|
|
return COLOR_LIGHTRED
|
|
|
|
end
|
|
|
|
|
2020-10-22 22:37:49 -06:00
|
|
|
function GlobalSettings:is_setting_enabled(name)
|
|
|
|
return self.settings[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
function GlobalSettings:make_setting_label_token(text, key, name, width)
|
2020-10-28 22:02:59 -06:00
|
|
|
return {text=text, key=key, key_sep=': ', key_pen=COLOR_LIGHTGREEN,
|
2020-10-22 22:37:49 -06:00
|
|
|
on_activate=self:callback('toggle_setting', name), width=width}
|
|
|
|
end
|
|
|
|
|
|
|
|
function GlobalSettings:make_setting_value_token(name)
|
|
|
|
return {text=self:callback('get_setting_string', name),
|
|
|
|
enabled=self:callback('is_setting_enabled', name),
|
2020-10-28 22:02:59 -06:00
|
|
|
pen=self:callback('get_setting_pen', name),
|
|
|
|
dpen=COLOR_GRAY}
|
2020-10-22 22:37:49 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
-- mockup:
|
|
|
|
--[[
|
|
|
|
Buildingplan Global Settings
|
|
|
|
|
|
|
|
e: Enable all: Off
|
|
|
|
Enables buildingplan for all building types. Use this to avoid having to
|
|
|
|
manually enable buildingplan for each building type that you want to plan.
|
|
|
|
Note that DFHack quickfort will use buildingplan to manage buildings
|
|
|
|
regardless of whether buildingplan is "enabled" for the building type.
|
|
|
|
|
|
|
|
Allowed types for generic, fire-safe, and magma-safe building material:
|
|
|
|
b: Blocks: On
|
|
|
|
s: Boulders: On
|
|
|
|
w: Wood: On
|
|
|
|
r: Bars: Off
|
|
|
|
Changes to these settings will be applied to newly-planned buildings.
|
|
|
|
|
|
|
|
A: Apply building material filter settings to existing planned buildings
|
|
|
|
Use this if your planned buildings can't be completed because the settings
|
|
|
|
above were too restrictive when the buildings were originally planned.
|
|
|
|
|
|
|
|
M: Edit list of materials to avoid
|
|
|
|
potash
|
|
|
|
pearlash
|
|
|
|
ash
|
|
|
|
coal
|
|
|
|
Buildingplan will avoid using these material types when a planned building's
|
|
|
|
material filter is set to 'any'. They can stil be matched when they are
|
|
|
|
explicitly allowed by a planned building's material filter. Changes to this
|
|
|
|
list take effect for existing buildings immediately.
|
|
|
|
|
|
|
|
g: Allow bags: Off
|
|
|
|
This allows bags to be placed where a 'coffer' is planned.
|
|
|
|
|
|
|
|
f: Legacy Quickfort Mode: Off
|
|
|
|
Compatibility mode for the legacy Python-based Quickfort application. This
|
|
|
|
setting is not needed for DFHack quickfort.
|
|
|
|
--]]
|
|
|
|
function GlobalSettings:init()
|
2020-11-04 09:52:19 -07:00
|
|
|
|
2020-10-22 22:37:49 -06:00
|
|
|
self.subviews.label:setText{
|
2020-11-04 09:52:19 -07:00
|
|
|
self:make_setting_label_token('Enable all', 'CUSTOM_E', 'all_enabled', 12),
|
|
|
|
self:make_setting_value_token('all_enabled'), '\n',
|
|
|
|
' Enables buildingplan for all building types. Use this to avoid having\n',
|
|
|
|
' to manually enable buildingplan for each building type that you want\n',
|
|
|
|
' to plan. Note that DFHack quickfort will use buildingplan to manage\n',
|
|
|
|
' buildings regardless of whether buildingplan is "enabled" for the\n',
|
|
|
|
' building type.\n',
|
|
|
|
'\n',
|
2020-10-22 22:37:49 -06:00
|
|
|
'Allowed types for generic, fire-safe, and magma-safe building material:\n',
|
|
|
|
self:make_setting_label_token('Blocks', 'CUSTOM_B', 'blocks', 10),
|
|
|
|
self:make_setting_value_token('blocks'), '\n',
|
|
|
|
self:make_setting_label_token('Boulders', 'CUSTOM_S', 'boulders', 10),
|
|
|
|
self:make_setting_value_token('boulders'), '\n',
|
|
|
|
self:make_setting_label_token('Wood', 'CUSTOM_W', 'logs', 10),
|
|
|
|
self:make_setting_value_token('logs'), '\n',
|
|
|
|
self:make_setting_label_token('Bars', 'CUSTOM_R', 'bars', 10),
|
|
|
|
self:make_setting_value_token('bars'), '\n',
|
|
|
|
' Changes to these settings will be applied to newly-planned buildings.\n',
|
2020-10-26 22:13:11 -06:00
|
|
|
' If no types are enabled above, then any building material is allowed.\n',
|
2020-10-22 22:37:49 -06:00
|
|
|
'\n',
|
|
|
|
self:make_setting_label_token('Legacy Quickfort Mode', 'CUSTOM_F',
|
|
|
|
'quickfort_mode', 23),
|
|
|
|
self:make_setting_value_token('quickfort_mode'), '\n',
|
|
|
|
' Compatibility mode for the legacy Python-based Quickfort application.\n',
|
|
|
|
' This setting is not needed for DFHack quickfort.'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-08-12 16:12:53 -06:00
|
|
|
return _ENV
|