dfhack/plugins/lua/buildingplan.lua

131 lines
3.6 KiB
Lua

local _ENV = mkmodule('plugins.buildingplan')
2020-08-16 00:03:49 -06:00
--[[
Public native functions:
2020-08-16 00:03:49 -06:00
prep buildingplan for core algorithm changes Lots of refactoring and reorganizing, with only cosmetic player-visible changes. - show quickfort mode hotlkey label regardless of whether the current building type has buildingplan enabled. before, it was only shown after the user enabled buildingplan for the current building. this eliminates the extra step when enabling quickfort mode, which force-enables all building types. - changed signature of lua-exported isPlannableBuilding to take subtype and custom type in addition to building type. this is only used by quickfort, and it already sends all three params in preparation for this change - added lua-exported scheduleCycle(), which is like doCycle(), but only takes effect on the next non-paused frame. this lets quickfort run only one buildingplan cycle regardless of how many #build blueprints were run - declared a few dfhack library methods and params const so buildingplan could call them from const methods - converted buildingplan internal debug logging fn to have a printf api - reshaped buildingplan-planner API and refactored implementation in preparation for upcoming core algorithm changes for supporing all building types (no externally-visible functionality changes) - changed df::building_type params to type, subtype, custom tuple keys - introduced capability to return multiple filters per building type (though the current buildings all only have one filter per) - split monolith hook functions in buildingplan.cpp into one per scope. this significantly cleans up the code and preps the hooks to handle iterating through multiple item filters. - got rid of send_key function and replaced with better reporting of whether keys have been handled
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()
prep buildingplan for core algorithm changes Lots of refactoring and reorganizing, with only cosmetic player-visible changes. - show quickfort mode hotlkey label regardless of whether the current building type has buildingplan enabled. before, it was only shown after the user enabled buildingplan for the current building. this eliminates the extra step when enabling quickfort mode, which force-enables all building types. - changed signature of lua-exported isPlannableBuilding to take subtype and custom type in addition to building type. this is only used by quickfort, and it already sends all three params in preparation for this change - added lua-exported scheduleCycle(), which is like doCycle(), but only takes effect on the next non-paused frame. this lets quickfort run only one buildingplan cycle regardless of how many #build blueprints were run - declared a few dfhack library methods and params const so buildingplan could call them from const methods - converted buildingplan internal debug logging fn to have a printf api - reshaped buildingplan-planner API and refactored implementation in preparation for upcoming core algorithm changes for supporing all building types (no externally-visible functionality changes) - changed df::building_type params to type, subtype, custom tuple keys - introduced capability to return multiple filters per building type (though the current buildings all only have one filter per) - split monolith hook functions in buildingplan.cpp into one per scope. this significantly cleans up the code and preps the hooks to handle iterating through multiple item filters. - got rid of send_key function and replaced with better reporting of whether keys have been handled
2020-10-16 14:52:23 -06:00
* void scheduleCycle()
2020-08-16 00:03:49 -06:00
--]]
local argparse = require('argparse')
local inspector = require('plugins.buildingplan.inspectoroverlay')
local pens = require('plugins.buildingplan.pens')
local planner = require('plugins.buildingplan.planneroverlay')
require('dfhack.buildings')
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
local command = table.remove(positionals, 1)
if not command or command == 'status' then
printStatus()
elseif command == 'set' and positionals then
setSetting(positionals[1], positionals[2] == 'true')
else
return false
end
return true
end
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
end
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 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
function get_desc(filter)
local desc = 'Unknown'
if filter.has_tool_use and filter.has_tool_use > -1 then
desc = to_title_case(df.tool_uses[filter.has_tool_use])
elseif filter.flags2 and filter.flags2.screw then
desc = 'Screw'
elseif filter.item_type and filter.item_type > -1 then
desc = to_title_case(df.item_type[filter.item_type])
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
if filter.flags2.magma_safe then
desc = 'Magma-safe material'
elseif filter.flags2.fire_safe then
desc = 'Fire-safe material'
else
desc = 'Building material'
end
end
if desc:endswith('s') then
desc = desc:sub(1,-2)
end
if desc == 'Trappart' then
desc = 'Mechanism'
elseif desc == 'Wood' then
desc = 'Log'
end
2023-02-09 01:13:53 -07:00
return desc
end
function reload_pens()
pens.reload_pens()
end
function signal_reset()
planner.reset_counts_flag = true
inspector.reset_inspector_flag = true
end
-- for use during development to reload all buildingplan modules
function reload_modules()
-- ensure circular deps are refreshed
reload('plugins.buildingplan.pens')
reload('plugins.buildingplan')
reload('plugins.buildingplan.filterselection')
reload('plugins.buildingplan.itemselection')
reload('plugins.buildingplan.planneroverlay')
reload('plugins.buildingplan.inspectoroverlay')
reload('plugins.buildingplan')
end
OVERLAY_WIDGETS = {
planner=planner.PlannerOverlay,
inspector=inspector.InspectorOverlay,
}
return _ENV