-
-
Save shaver/e9eb444b25076740bd67 to your computer and use it in GitHub Desktop.
Serialize stockpile settings for later reuse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- serialize stockpile settings | |
-- usage: save-stockpile <id/name> <filename> | |
-- save-stockpile <filename> # uses currently-selected stockpile | |
local args = {...} | |
local pile = nil | |
local filename, file | |
if #args > 1 then | |
id = tostring(args[1]) | |
for k, v in pairs(df.global.world.buildings.all) do | |
if v:getType() == df.building_type.Stockpile then | |
if v.name == id or tostring(v.stockpile_number) == id then | |
pile = v | |
break | |
end | |
end | |
end | |
if pile == nil then | |
qerror("no stockpile found named or having number '" .. id .. "'") | |
return | |
end | |
filename = args[2] | |
else | |
pile = dfhack.gui.getSelectedBuilding() | |
if pile == nil then | |
return | |
end | |
filename = args[1] | |
end | |
local settings = pile.settings; | |
local function propAndValue(...) | |
local propname = table.concat({...}, ".") | |
local val = settings | |
for k,v in pairs({...}) do | |
val = val[v] | |
end | |
return 'S.'..propname, val | |
end | |
local function saveProp(...) | |
local propname, val = propAndValue(...) | |
file:write(propname .. " = " .. tostring(val) .. '\n') | |
end | |
local function saveCharVector(...) | |
local propname, vec = propAndValue(...) | |
file:write("rV(" .. propname .. ", " .. #vec .. ')\n') | |
local start = -1 | |
for i = 0, #vec - 1 do | |
local v = vec[i] | |
if (v == 0) then | |
if start >= 0 then | |
file:write('sV(' .. propname .. ', ' .. start .. ', ' .. i .. ')\n') | |
start = -1 | |
end | |
elseif start < 0 then | |
start = i | |
end | |
end | |
if start >= 0 then | |
file:write('sV(' .. propname .. ', ' .. start .. ', ' .. #vec - 1 .. ')\n') | |
start = -1 | |
end | |
end | |
local function saveCharVectors(base, vecnames) | |
for k,v in pairs(vecnames) do | |
saveCharVector(base, v) | |
end | |
end | |
local function saveBoolArray(...) | |
local propname, array = propAndValue(...) | |
for k,v in pairs(array) do | |
if type(k) == 'number' then | |
file:write(propname..'['..k..'] = ' .. tostring(v) .. "\n") | |
else | |
file:write(propname..'.'..k..' = ' .. tostring(v) .. "\n") | |
end | |
end | |
end | |
local function saveBoolArrays(base, arrayNames) | |
for k,v in pairs(arrayNames) do | |
saveBoolArray(base, v) | |
end | |
end | |
print('saving stockpile settings for '..pile.name..' #'..pile.stockpile_number.. ' to ' .. filename) | |
file, err = io.open(filename, "w+") | |
if file == nil then | |
qerror(err) | |
return | |
end | |
file:write('-- stockpile settings for '..pile.name..' #'..pile.stockpile_number..'\n') | |
file:write("-- Flags\n") | |
saveBoolArray('flags') | |
file:write("-- Animals\n") | |
saveProp('animals', 'empty_cages') | |
saveProp('animals', 'empty_traps') | |
saveCharVector('animals', 'enabled') | |
file:write("-- Food\n") | |
saveCharVectors('food', {'meat', 'fish', 'unprepared_fish', 'egg', 'plants', | |
'drink_plant', 'drink_animal', | |
'cheese_plant', 'cheese_animal', 'seeds', 'leaves', 'powder_plant', 'powder_creature', 'glob', | |
'glob_paste', 'glob_pressed', 'liquid_plant', 'liquid_animal', 'liquid_misc'}) | |
saveProp('food', 'prepared_meals') | |
file:write("-- Furniture\n") | |
saveCharVectors('furniture', {'type', 'other_mats', 'mats'}) | |
saveBoolArrays('furniture', {'quality_core', 'quality_total'}) | |
saveProp('furniture', 'sand_bags') | |
file:write("-- Refuse\n") | |
saveCharVectors('refuse', {'type', 'corpses', 'body_parts', 'skulls', 'bones', 'hair', | |
'shells','teeth', 'horns'}) | |
saveProp('refuse', 'fresh_raw_hide') | |
saveProp('refuse', 'rotten_raw_hide') | |
file:write("-- Stone\n") | |
saveCharVector('stone', 'mats') | |
file:write("-- Ore\n") | |
saveCharVector('ore', 'mats') | |
file:write("--Ammo\n") | |
saveCharVectors('ammo', {'type', 'other_mats'}) | |
saveBoolArrays('ammo', {'quality_core', 'quality_total'}) | |
file:write("-- Coins\n") | |
saveCharVector('coins', 'mats') | |
file:write("-- Bars and Blocks\n") | |
saveCharVectors('bars_blocks', {'bars_other_mats', 'blocks_other_mats', 'bars_mats', 'blocks_mats'}) | |
file:write("-- Gems\n") | |
saveCharVectors('gems', {'rough_other_mats', 'cut_other_mats', 'rough_mats', 'cut_mats'}) | |
file:write("-- Finished Goods\n") | |
saveCharVectors('finished_goods', {'type', 'other_mats', 'mats'}) | |
saveBoolArrays('finished_goods', {'quality_core', 'quality_total'}) | |
file:write("-- Leather\n") | |
saveCharVector('leather', 'mats') | |
file:write("-- Cloth\n") | |
saveCharVectors('cloth', {'thread_silk', 'thread_plant', 'thread_yarn', 'thread_metal', | |
'cloth_silk', 'cloth_plant', 'cloth_yarn', 'cloth_metal'}) | |
file:write("-- Wood\n") | |
saveCharVector('wood', 'mats') | |
file:write("-- Weapons\n") | |
saveCharVectors('weapons', {'weapon_type', 'trapcomp_type', 'other_mats', 'mats'}) | |
saveBoolArrays('weapons', {'quality_core', 'quality_total'}) | |
saveProp('weapons', 'usable') | |
saveProp('weapons', 'unusable') | |
file:write("-- Armor\n") | |
saveCharVectors('armor', {'body', 'head', 'feet', 'hands', 'legs', 'shield', | |
'other_mats', 'mats'}) | |
saveBoolArrays('armor', {'quality_core', 'quality_total'}) | |
saveProp('armor', 'usable') | |
saveProp('armor', 'unusable') | |
file:write("-- Settings\n") | |
saveProp('allow_organic') | |
saveProp('allow_inorganic') | |
file:close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment