dfhack/plugins/lua/stockflow.lua

1232 lines
46 KiB
Lua

2013-05-24 12:43:40 -06:00
local _ENV = mkmodule('plugins.stockflow')
local gui = require "gui"
2013-05-24 12:43:40 -06:00
reaction_list = reaction_list or {}
saved_orders = saved_orders or {}
jobs_to_create = jobs_to_create or {}
2013-05-24 12:43:40 -06:00
triggers = {
{filled = false, divisor = 1, name = "Per empty space"},
{filled = true, divisor = 1, name = "Per stored item"},
{filled = false, divisor = 2, name = "Per two empty spaces"},
{filled = true, divisor = 2, name = "Per two stored items"},
{filled = false, divisor = 3, name = "Per three empty spaces"},
{filled = true, divisor = 3, name = "Per three stored items"},
{filled = false, divisor = 4, name = "Per four empty spaces"},
{filled = true, divisor = 4, name = "Per four stored items"},
{name = "Never"},
}
entry_ints = {
stockpile_id = 1,
order_number = 2,
trigger_number = 3,
}
FirstRow = 3
CenterCol = 38
ExtraLines = 9
2013-05-24 12:43:40 -06:00
-- Populate the reaction and stockpile order lists.
-- To be called whenever a world is loaded.
function initialize_world()
-- Clear old reactions, just in case.
clear_caches()
2013-05-24 12:43:40 -06:00
reaction_list = collect_reactions()
saved_orders = collect_orders()
end
-- Clear all caches.
-- Called when a world is loaded, or when the plugin is disabled.
function clear_caches()
-- Free the C++ objects in the reaction list.
for _, value in ipairs(reaction_list) do
value.order:delete()
end
2013-05-24 12:43:40 -06:00
reaction_list = {}
saved_orders = {}
jobs_to_create = {}
end
function trigger_name(cache)
local trigger = triggers[cache.entry.ints[entry_ints.trigger_number]]
2013-05-24 12:43:40 -06:00
return trigger and trigger.name or "Never"
end
function list_orders()
local listed = false
for _, spec in pairs(saved_orders) do
local num = spec.stockpile.stockpile_number
local name = spec.entry.value
local trigger = trigger_name(spec)
print("Stockpile #"..num, name, trigger)
listed = true
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if not listed then
print("No manager jobs have been set for your stockpiles.")
print("Use j in a stockpile menu to create one...")
end
end
-- Save the stockpile jobs for later creation.
-- Called when the bookkeeper starts updating stockpile records.
function start_bookkeeping()
local result = {}
2013-05-24 12:43:40 -06:00
for reaction_id, quantity in pairs(check_stockpiles()) do
local amount = order_quantity(reaction_list[reaction_id].order, quantity)
if amount > 0 then
result[reaction_id] = amount
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
jobs_to_create = result
end
-- Insert any saved jobs.
-- Called when the bookkeeper finishes updating stockpile records.
function finish_bookkeeping()
for reaction, amount in pairs(jobs_to_create) do
create_orders(reaction_list[reaction].order, amount)
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
jobs_to_create = {}
end
function stockpile_settings(sp)
local order = saved_orders[sp.id]
if not order then
return "No job selected", ""
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return order.entry.value, trigger_name(order)
end
-- Toggle the trigger condition for a stockpile.
function toggle_trigger(sp)
local saved = saved_orders[sp.id]
if saved then
saved.entry.ints[entry_ints.trigger_number] = (saved.entry.ints[entry_ints.trigger_number] % #triggers) + 1
2013-05-24 12:43:40 -06:00
saved.entry:save()
end
end
function collect_orders()
local result = {}
local entries = dfhack.persistent.get_all("stockflow/entry", true)
if entries then
for _, entry in ipairs(entries) do
local spid = entry.ints[entry_ints.stockpile_id]
local stockpile = df.building.find(spid)
2013-05-24 12:43:40 -06:00
if stockpile then
local order_number = entry.ints[entry_ints.order_number]
if reaction_list[order_number] and entry.value == reaction_list[order_number].name then
result[spid] = {
stockpile = stockpile,
entry = entry,
}
else
-- Todo: Search reaction_list for the name.
-- This can happen when loading an old save in a new version.
-- It's even possible that the reaction has been removed.
local found = false
for number, reaction in ipairs(reaction_list) do
if reaction.name == entry.value then
print("Adjusting stockflow entry for stockpile #"..stockpile.stockpile_number..": "..entry.value.." ("..order_number.." => "..number..")")
entry.ints[entry_ints.order_number] = number
entry:save()
result[spid] = {
stockpile = stockpile,
entry = entry,
}
found = true
break
end
end
if not found then
print("Unmatched stockflow entry for stockpile #"..stockpile.stockpile_number..": "..entry.value.." ("..order_number..")")
end
end
else
-- The stockpile no longer exists.
-- Perhaps it has been deleted, or perhaps this is a different fortress.
-- print("Missing stockflow pile "..spid)
entry:delete()
2013-05-24 12:43:40 -06:00
end
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return result
end
-- Choose an order that the stockpile should add to the manager queue.
function select_order(stockpile)
screen:reset(stockpile)
screen:show()
end
function reaction_entry(reactions, job_type, values, name)
if not job_type then
-- Perhaps df.job_type.something returned nil for an unknown job type.
-- We could warn about it; in any case, don't add it to the list.
return
end
2013-05-24 12:43:40 -06:00
local order = df.manager_order:new()
-- These defaults differ from the newly created order's.
order:assign{
job_type = job_type,
item_type = -1,
2013-05-24 12:43:40 -06:00
item_subtype = -1,
mat_type = -1,
mat_index = -1,
}
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if values then
-- Override default attributes.
order:assign(values)
end
2015-02-14 20:53:06 -07:00
table.insert(reactions, {
2013-05-24 12:43:40 -06:00
name = name or df.job_type.attrs[job_type].caption,
order = order,
})
2013-05-24 12:43:40 -06:00
end
function resource_reactions(reactions, job_type, mat_info, keys, items, options)
local values = {}
for key, value in pairs(mat_info.management) do
values[key] = value
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, itemid in ipairs(keys) do
local itemdef = items[itemid]
local start = options.verb or mat_info.verb or "Make"
if options.adjective then
start = start.." "..itemdef.adjective
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if (not options.permissible) or options.permissible(itemdef) then
local item_name = " "..itemdef[options.name_field or "name"]
if options.capitalize then
item_name = string.gsub(item_name, " .", string.upper)
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
values.item_subtype = itemid
reaction_entry(reactions, job_type, values, start.." "..mat_info.adjective..item_name)
2013-05-24 12:43:40 -06:00
end
end
end
function material_reactions(reactions, itemtypes, mat_info)
-- Expects a list of {job_type, verb, item_name} tuples.
for _, row in ipairs(itemtypes) do
local line = row[2].." "..mat_info.adjective
if row[3] then
line = line.." "..row[3]
end
2015-02-14 20:53:06 -07:00
reaction_entry(reactions, row[1], mat_info.management, line)
2013-05-24 12:43:40 -06:00
end
end
function clothing_reactions(reactions, mat_info, filter)
local resources = df.historical_entity.find(df.global.ui.civ_id).resources
2013-05-24 12:43:40 -06:00
local itemdefs = df.global.world.raws.itemdefs
local job_types = df.job_type
2014-05-04 15:02:12 -06:00
resource_reactions(reactions, job_types.MakeArmor, mat_info, resources.armor_type, itemdefs.armor, {permissible = filter})
resource_reactions(reactions, job_types.MakePants, mat_info, resources.pants_type, itemdefs.pants, {permissible = filter})
resource_reactions(reactions, job_types.MakeGloves, mat_info, resources.gloves_type, itemdefs.gloves, {permissible = filter})
resource_reactions(reactions, job_types.MakeHelm, mat_info, resources.helm_type, itemdefs.helms, {permissible = filter})
resource_reactions(reactions, job_types.MakeShoes, mat_info, resources.shoes_type, itemdefs.shoes, {permissible = filter})
2013-05-24 12:43:40 -06:00
end
-- Find the reaction types that should be listed in the management interface.
function collect_reactions()
-- The sequence here tries to match the native manager screen.
-- It should also be possible to collect the sequence from somewhere native,
-- but I currently can only find it while the job selection screen is active.
-- Even that list doesn't seem to include their names.
local result = {}
2015-02-14 20:53:06 -07:00
-- Caching the enumeration might not be important, but saves lookups.
local job_types = df.job_type
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
local materials = {
rock = {
adjective = "rock",
management = {mat_type = 0},
},
}
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, name in ipairs{"wood", "cloth", "leather", "silk", "yarn", "bone", "shell", "tooth", "horn", "pearl"} do
materials[name] = {
adjective = name,
management = {material_category = {[name] = true}},
}
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
materials.wood.adjective = "wooden"
materials.tooth.adjective = "ivory/tooth"
materials.leather.clothing_flag = "LEATHER"
materials.shell.short = true
materials.pearl.short = true
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
-- Collection and Entrapment
reaction_entry(result, job_types.CollectWebs)
reaction_entry(result, job_types.CollectSand)
reaction_entry(result, job_types.CollectClay)
reaction_entry(result, job_types.CatchLiveLandAnimal)
reaction_entry(result, job_types.CatchLiveFish)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Cutting, encrusting, and metal extraction.
local rock_types = df.global.world.raws.inorganics
for rock_id = #rock_types-1, 0, -1 do
local material = rock_types[rock_id].material
local rock_name = material.state_adj.Solid
if material.flags.IS_STONE or material.flags.IS_GEM then
reaction_entry(result, job_types.CutGems, {
2014-05-04 15:02:12 -06:00
mat_type = 0,
mat_index = rock_id,
}, "Cut "..rock_name)
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.EncrustWithGems, {
2013-05-24 12:43:40 -06:00
mat_type = 0,
mat_index = rock_id,
item_category = {finished_goods = true},
}, "Encrust Finished Goods With "..rock_name)
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.EncrustWithGems, {
2013-05-24 12:43:40 -06:00
mat_type = 0,
mat_index = rock_id,
item_category = {furniture = true},
}, "Encrust Furniture With "..rock_name)
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.EncrustWithGems, {
2013-05-24 12:43:40 -06:00
mat_type = 0,
mat_index = rock_id,
item_category = {ammo = true},
}, "Encrust Ammo With "..rock_name)
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if #rock_types[rock_id].metal_ore.mat_index > 0 then
reaction_entry(result, job_types.SmeltOre, {mat_type = 0, mat_index = rock_id}, "Smelt "..rock_name.." Ore")
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if #rock_types[rock_id].thread_metal.mat_index > 0 then
reaction_entry(result, job_types.ExtractMetalStrands, {mat_type = 0, mat_index = rock_id})
2013-05-24 12:43:40 -06:00
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Glass cutting and encrusting, with different job numbers.
-- We could search the entire table, but glass is less subject to raws.
local glass_types = df.global.world.raws.mat_table.builtin
local glasses = {}
for glass_id = 3, 5 do
local material = glass_types[glass_id]
local glass_name = material.state_adj.Solid
if material.flags.IS_GLASS then
-- For future use.
table.insert(glasses, {
adjective = glass_name,
management = {mat_type = glass_id},
})
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.CutGlass, {mat_type = glass_id}, "Cut "..glass_name)
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.EncrustWithGlass, {
2013-05-24 12:43:40 -06:00
mat_type = glass_id,
item_category = {finished_goods = true},
}, "Encrust Finished Goods With "..glass_name)
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.EncrustWithGlass, {
2013-05-24 12:43:40 -06:00
mat_type = glass_id,
item_category = {furniture = true},
}, "Encrust Furniture With "..glass_name)
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.EncrustWithGlass, {
2013-05-24 12:43:40 -06:00
mat_type = glass_id,
item_category = {ammo = true},
}, "Encrust Ammo With "..glass_name)
2013-05-24 12:43:40 -06:00
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Dyeing
reaction_entry(result, job_types.DyeThread)
reaction_entry(result, job_types.DyeCloth)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Sew Image
local cloth_mats = {materials.cloth, materials.silk, materials.yarn, materials.leather}
2013-05-24 12:43:40 -06:00
for _, material in ipairs(cloth_mats) do
2014-05-04 15:02:12 -06:00
material_reactions(result, {{job_types.SewImage, "Sew", "Image"}}, material)
material.cloth = true
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, spec in ipairs{materials.bone, materials.shell, materials.tooth, materials.horn, materials.pearl} do
2014-05-04 15:02:12 -06:00
material_reactions(result, {{job_types.DecorateWith, "Decorate With"}}, spec)
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
reaction_entry(result, job_types.MakeTotem)
reaction_entry(result, job_types.ButcherAnimal)
reaction_entry(result, job_types.MillPlants)
reaction_entry(result, job_types.MakePotashFromLye)
reaction_entry(result, job_types.MakePotashFromAsh)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Kitchen
reaction_entry(result, job_types.PrepareMeal, {mat_type = 2}, "Prepare Easy Meal")
reaction_entry(result, job_types.PrepareMeal, {mat_type = 3}, "Prepare Fine Meal")
reaction_entry(result, job_types.PrepareMeal, {mat_type = 4}, "Prepare Lavish Meal")
2015-02-14 20:53:06 -07:00
-- Brew Drink
reaction_entry(result, job_types.BrewDrink)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Weaving
reaction_entry(result, job_types.WeaveCloth, {material_category = {plant = true}}, "Weave Thread into Cloth")
reaction_entry(result, job_types.WeaveCloth, {material_category = {silk = true}}, "Weave Thread into Silk")
reaction_entry(result, job_types.WeaveCloth, {material_category = {yarn = true}}, "Weave Yarn into Cloth")
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Extracts, farmer's workshop, and wood burning
reaction_entry(result, job_types.ExtractFromPlants)
reaction_entry(result, job_types.ExtractFromRawFish)
reaction_entry(result, job_types.ExtractFromLandAnimal)
reaction_entry(result, job_types.PrepareRawFish)
reaction_entry(result, job_types.MakeCheese)
reaction_entry(result, job_types.MilkCreature)
reaction_entry(result, job_types.ShearCreature)
reaction_entry(result, job_types.SpinThread, {material_category = {strand = true}})
reaction_entry(result, job_types.MakeLye)
reaction_entry(result, job_types.ProcessPlants)
reaction_entry(result, job_types.ProcessPlantsBag)
reaction_entry(result, job_types.ProcessPlantsVial)
reaction_entry(result, job_types.ProcessPlantsBarrel)
reaction_entry(result, job_types.MakeCharcoal)
reaction_entry(result, job_types.MakeAsh)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Reactions defined in the raws.
-- Not all reactions are allowed to the civilization.
-- That includes "Make sharp rock" by default.
local entity = df.historical_entity.find(df.global.ui.civ_id)
if not entity then
-- No global civilization; arena mode?
-- Anyway, skip remaining reactions, since many depend on the civ.
return result
end
2013-05-24 12:43:40 -06:00
for _, reaction_id in ipairs(entity.entity_raw.workshops.permitted_reaction_id) do
local reaction = df.global.world.raws.reactions[reaction_id]
local name = string.gsub(reaction.name, "^.", string.upper)
reaction_entry(result, job_types.CustomReaction, {reaction_name = reaction.code}, name)
2013-05-24 12:43:40 -06:00
end
-- Reactions generated by the game.
for _, reaction in ipairs(df.global.world.raws.reactions) do
if reaction.source_enid == entity.id then
local name = string.gsub(reaction.name, "^.", string.upper)
reaction_entry(result, job_types.CustomReaction, {reaction_name = reaction.code}, name)
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Metal forging
local itemdefs = df.global.world.raws.itemdefs
for rock_id = 0, #rock_types - 1 do
local material = rock_types[rock_id].material
local rock_name = material.state_adj.Solid
local mat_flags = {
adjective = rock_name,
management = {mat_type = 0, mat_index = rock_id},
verb = "Forge",
}
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.IS_METAL then
reaction_entry(result, job_types.StudWith, mat_flags.management, "Stud With "..rock_name)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_WEAPON then
-- Todo: Are these really the right flags to check?
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeWeapon, mat_flags, entity.resources.weapon_type, itemdefs.weapons, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.skill_ranged == -1 end),
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Is this entirely disconnected from the entity?
2015-03-15 16:26:38 -06:00
material_reactions(result, {{job_types.MakeBallistaArrowHead, "Forge", "Ballista Arrow Head"}}, mat_flags)
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeTrapComponent, mat_flags, entity.resources.trapcomp_type, itemdefs.trapcomps, {
2013-05-24 12:43:40 -06:00
adjective = true,
})
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.AssembleSiegeAmmo, mat_flags, entity.resources.siegeammo_type, itemdefs.siege_ammo, {
2013-05-24 12:43:40 -06:00
verb = "Assemble",
})
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_WEAPON_RANGED then
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeWeapon, mat_flags, entity.resources.weapon_type, itemdefs.weapons, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.skill_ranged >= 0 end),
})
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_DIGGER then
-- Todo: Ranged or training digging weapons?
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeWeapon, mat_flags, entity.resources.digger_type, itemdefs.weapons, {
2013-05-24 12:43:40 -06:00
})
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_AMMO then
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeAmmo, mat_flags, entity.resources.ammo_type, itemdefs.ammo, {
2013-05-24 12:43:40 -06:00
name_field = "name_plural",
})
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_ANVIL then
2014-05-04 15:02:12 -06:00
material_reactions(result, {{job_types.ForgeAnvil, "Forge", "Anvil"}}, mat_flags)
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_ARMOR then
local metalclothing = (function(itemdef) return itemdef.props.flags.METAL end)
clothing_reactions(result, mat_flags, metalclothing)
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeShield, mat_flags, entity.resources.shield_type, itemdefs.shields, {
2013-05-24 12:43:40 -06:00
})
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_SOFT then
local metalclothing = (function(itemdef) return itemdef.props.flags.SOFT and not itemdef.props.flags.METAL end)
clothing_reactions(result, mat_flags, metalclothing)
end
2015-02-14 20:53:06 -07:00
resource_reactions(result, job_types.MakeTool, mat_flags, entity.resources.tool_type, itemdefs.tools, {
permissible = (function(itemdef) return ((material.flags.ITEMS_HARD and itemdef.flags.HARD_MAT) or (material.flags.ITEMS_METAL and itemdef.flags.METAL_MAT)) and not itemdef.flags.NO_DEFAULT_JOB end),
capitalize = true,
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_HARD then
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.ConstructDoor, "Construct", "Door"},
{job_types.ConstructFloodgate, "Construct", "Floodgate"},
{job_types.ConstructHatchCover, "Construct", "Hatch Cover"},
{job_types.ConstructGrate, "Construct", "Grate"},
{job_types.ConstructThrone, "Construct", "Throne"},
{job_types.ConstructCoffin, "Construct", "Sarcophagus"},
{job_types.ConstructTable, "Construct", "Table"},
{job_types.ConstructSplint, "Construct", "Splint"},
{job_types.ConstructCrutch, "Construct", "Crutch"},
{job_types.ConstructArmorStand, "Construct", "Armor Stand"},
{job_types.ConstructWeaponRack, "Construct", "Weapon Rack"},
{job_types.ConstructCabinet, "Construct", "Cabinet"},
{job_types.MakeGoblet, "Forge", "Goblet"},
{job_types.MakeInstrument, "Forge", "Instrument"},
{job_types.MakeToy, "Forge", "Toy"},
{job_types.ConstructStatue, "Construct", "Statue"},
{job_types.ConstructBlocks, "Construct", "Blocks"},
{job_types.MakeAnimalTrap, "Forge", "Animal Trap"},
{job_types.MakeBarrel, "Forge", "Barrel"},
{job_types.MakeBucket, "Forge", "Bucket"},
{job_types.ConstructBin, "Construct", "Bin"},
{job_types.MakePipeSection, "Forge", "Pipe Section"},
{job_types.MakeCage, "Forge", "Cage"},
{job_types.MintCoins, "Mint", "Coins"},
{job_types.ConstructChest, "Construct", "Chest"},
{job_types.MakeFlask, "Forge", "Flask"},
{job_types.MakeChain, "Forge", "Chain"},
{job_types.MakeCrafts, "Make", "Crafts"},
{job_types.MakeFigurine, "Make", "Figurine"},
{job_types.MakeAmulet, "Make", "Amulet"},
{job_types.MakeScepter, "Make", "Scepter"},
{job_types.MakeCrown, "Make", "Crown"},
{job_types.MakeRing, "Make", "Ring"},
{job_types.MakeEarring, "Make", "Earring"},
{job_types.MakeBracelet, "Make", "Bracelet"},
{job_types.MakeGem, "Make Large", "Gem"},
{job_types.ConstructMechanisms, "Construct", "Mechanisms"},
2013-05-24 12:43:40 -06:00
}, mat_flags)
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if material.flags.ITEMS_SOFT then
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.MakeBackpack, "Make", "Backpack"},
{job_types.MakeQuiver, "Make", "Quiver"},
{job_types.ConstructCatapultParts, "Construct", "Catapult Parts"},
{job_types.ConstructBallistaParts, "Construct", "Ballista Parts"},
2013-05-24 12:43:40 -06:00
}, mat_flags)
end
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Traction Bench
reaction_entry(result, job_types.ConstructTractionBench)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Non-metal weapons
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeWeapon, materials.wood, entity.resources.weapon_type, itemdefs.weapons, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.skill_ranged >= 0 end),
})
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeWeapon, materials.wood, entity.resources.training_weapon_type, itemdefs.weapons, {
2013-05-24 12:43:40 -06:00
})
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeWeapon, materials.bone, entity.resources.weapon_type, itemdefs.weapons, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.skill_ranged >= 0 end),
})
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeWeapon, materials.rock, entity.resources.weapon_type, itemdefs.weapons, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.flags.CAN_STONE end),
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Wooden items
-- Closely related to the ITEMS_HARD list.
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.ConstructDoor, "Construct", "Door"},
{job_types.ConstructFloodgate, "Construct", "Floodgate"},
{job_types.ConstructHatchCover, "Construct", "Hatch Cover"},
{job_types.ConstructGrate, "Construct", "Grate"},
{job_types.ConstructThrone, "Construct", "Chair"},
{job_types.ConstructCoffin, "Construct", "Casket"},
{job_types.ConstructTable, "Construct", "Table"},
{job_types.ConstructArmorStand, "Construct", "Armor Stand"},
{job_types.ConstructWeaponRack, "Construct", "Weapon Rack"},
{job_types.ConstructCabinet, "Construct", "Cabinet"},
{job_types.MakeGoblet, "Make", "Cup"},
{job_types.MakeInstrument, "Make", "Instrument"},
2013-05-24 12:43:40 -06:00
}, materials.wood)
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeTool, materials.wood, entity.resources.tool_type, itemdefs.tools, {
-- permissible = (function(itemdef) return itemdef.flags.WOOD_MAT and not itemdef.flags.NO_DEFAULT_JOB end),
permissible = (function(itemdef) return not itemdef.flags.NO_DEFAULT_JOB end),
2013-05-24 12:43:40 -06:00
capitalize = true,
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.MakeToy, "Make", "Toy"},
{job_types.ConstructBlocks, "Construct", "Blocks"},
{job_types.ConstructSplint, "Construct", "Splint"},
{job_types.ConstructCrutch, "Construct", "Crutch"},
{job_types.MakeAnimalTrap, "Make", "Animal Trap"},
{job_types.MakeBarrel, "Make", "Barrel"},
{job_types.MakeBucket, "Make", "Bucket"},
{job_types.ConstructBin, "Construct", "Bin"},
{job_types.MakeCage, "Make", "Cage"},
{job_types.MakePipeSection, "Make", "Pipe Section"},
2013-05-24 12:43:40 -06:00
}, materials.wood)
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeTrapComponent, materials.wood, entity.resources.trapcomp_type, itemdefs.trapcomps, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.flags.WOOD end),
adjective = true,
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Rock items
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.ConstructDoor, "Construct", "Door"},
{job_types.ConstructFloodgate, "Construct", "Floodgate"},
{job_types.ConstructHatchCover, "Construct", "Hatch Cover"},
{job_types.ConstructGrate, "Construct", "Grate"},
{job_types.ConstructThrone, "Construct", "Throne"},
{job_types.ConstructCoffin, "Construct", "Coffin"},
{job_types.ConstructTable, "Construct", "Table"},
{job_types.ConstructArmorStand, "Construct", "Armor Stand"},
{job_types.ConstructWeaponRack, "Construct", "Weapon Rack"},
{job_types.ConstructCabinet, "Construct", "Cabinet"},
{job_types.MakeGoblet, "Make", "Mug"},
{job_types.MakeInstrument, "Make", "Instrument"},
2013-05-24 12:43:40 -06:00
}, materials.rock)
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeTool, materials.rock, entity.resources.tool_type, itemdefs.tools, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.flags.HARD_MAT end),
capitalize = true,
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.MakeToy, "Make", "Toy"},
{job_types.ConstructQuern, "Construct", "Quern"},
{job_types.ConstructMillstone, "Construct", "Millstone"},
{job_types.ConstructSlab, "Construct", "Slab"},
{job_types.ConstructStatue, "Construct", "Statue"},
{job_types.ConstructBlocks, "Construct", "Blocks"},
2013-05-24 12:43:40 -06:00
}, materials.rock)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Glass items
for _, mat_info in ipairs(glasses) do
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.ConstructDoor, "Construct", "Portal"},
{job_types.ConstructFloodgate, "Construct", "Floodgate"},
{job_types.ConstructHatchCover, "Construct", "Hatch Cover"},
{job_types.ConstructGrate, "Construct", "Grate"},
{job_types.ConstructThrone, "Construct", "Throne"},
{job_types.ConstructCoffin, "Construct", "Coffin"},
{job_types.ConstructTable, "Construct", "Table"},
{job_types.ConstructArmorStand, "Construct", "Armor Stand"},
{job_types.ConstructWeaponRack, "Construct", "Weapon Rack"},
{job_types.ConstructCabinet, "Construct", "Cabinet"},
{job_types.MakeGoblet, "Make", "Goblet"},
{job_types.MakeInstrument, "Make", "Instrument"},
2013-05-24 12:43:40 -06:00
}, mat_info)
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeTool, mat_info, entity.resources.tool_type, itemdefs.tools, {
2013-05-24 12:43:40 -06:00
permissible = (function(itemdef) return itemdef.flags.HARD_MAT end),
capitalize = true,
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.MakeToy, "Make", "Toy"},
{job_types.ConstructStatue, "Construct", "Statue"},
{job_types.ConstructBlocks, "Construct", "Blocks"},
{job_types.MakeCage, "Make", "Terrarium"},
{job_types.MakePipeSection, "Make", "Tube"},
2013-05-24 12:43:40 -06:00
}, mat_info)
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeTrapComponent, mat_info, entity.resources.trapcomp_type, itemdefs.trapcomps, {
2013-05-24 12:43:40 -06:00
adjective = true,
})
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Bed, specified as wooden.
reaction_entry(result, job_types.ConstructBed, materials.wood.management)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Windows
for _, mat_info in ipairs(glasses) do
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.MakeWindow, "Make", "Window"},
2013-05-24 12:43:40 -06:00
}, mat_info)
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Rock Mechanisms
reaction_entry(result, job_types.ConstructMechanisms, materials.rock.management)
2015-02-14 20:53:06 -07:00
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.AssembleSiegeAmmo, materials.wood, entity.resources.siegeammo_type, itemdefs.siege_ammo, {
2013-05-24 12:43:40 -06:00
verb = "Assemble",
})
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, mat_info in ipairs(glasses) do
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.MakeRawGlass, "Make Raw", nil},
2013-05-24 12:43:40 -06:00
}, mat_info)
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
material_reactions(result, {
2014-05-04 15:02:12 -06:00
{job_types.MakeBackpack, "Make", "Backpack"},
{job_types.MakeQuiver, "Make", "Quiver"},
2013-05-24 12:43:40 -06:00
}, materials.leather)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, material in ipairs(cloth_mats) do
clothing_reactions(result, material, (function(itemdef) return itemdef.props.flags[material.clothing_flag or "SOFT"] end))
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Boxes, Bags, and Ropes
local boxmats = {
2013-05-24 12:43:40 -06:00
{mats = {materials.wood}, box = "Chest"},
{mats = {materials.rock}, box = "Coffer"},
{mats = glasses, box = "Box", flask = "Vial"},
{mats = {materials.cloth}, box = "Bag", chain = "Rope"},
{mats = {materials.leather}, box = "Bag", flask = "Waterskin"},
{mats = {materials.silk, materials.yarn}, box = "Bag", chain = "Rope"},
}
for _, boxmat in ipairs(boxmats) do
for _, mat in ipairs(boxmat.mats) do
2014-05-04 15:02:12 -06:00
material_reactions(result, {{job_types.ConstructChest, "Construct", boxmat.box}}, mat)
2013-05-24 12:43:40 -06:00
if boxmat.chain then
2014-05-04 15:02:12 -06:00
material_reactions(result, {{job_types.MakeChain, "Make", boxmat.chain}}, mat)
2013-05-24 12:43:40 -06:00
end
if boxmat.flask then
2014-05-04 15:02:12 -06:00
material_reactions(result, {{job_types.MakeFlask, "Make", boxmat.flask}}, mat)
2013-05-24 12:43:40 -06:00
end
end
end
2015-02-14 20:53:06 -07:00
-- Crafts
2013-05-24 12:43:40 -06:00
for _, mat in ipairs{
materials.wood,
materials.rock,
materials.cloth,
materials.leather,
materials.shell,
materials.bone,
materials.silk,
materials.tooth,
materials.horn,
materials.pearl,
materials.yarn,
} do
material_reactions(result, {
{job_types.MakeCrafts, "Make", "Crafts"},
{job_types.MakeAmulet, "Make", "Amulet"},
{job_types.MakeBracelet, "Make", "Bracelet"},
{job_types.MakeEarring, "Make", "Earring"},
}, mat)
if not mat.cloth then
material_reactions(result, {
{job_types.MakeCrown, "Make", "Crown"},
{job_types.MakeFigurine, "Make", "Figurine"},
{job_types.MakeRing, "Make", "Ring"},
{job_types.MakeGem, "Make Large", "Gem"},
}, mat)
if not mat.short then
material_reactions(result, {
{job_types.MakeScepter, "Make", "Scepter"},
}, mat)
end
end
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Siege engine parts
reaction_entry(result, job_types.ConstructCatapultParts, materials.wood.management)
reaction_entry(result, job_types.ConstructBallistaParts, materials.wood.management)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, mat in ipairs{materials.wood, materials.bone} do
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeAmmo, mat, entity.resources.ammo_type, itemdefs.ammo, {
2013-05-24 12:43:40 -06:00
name_field = "name_plural",
})
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- BARRED and SCALED as flag names don't quite seem to fit, here.
clothing_reactions(result, materials.bone, (function(itemdef) return itemdef.props.flags.BARRED end))
clothing_reactions(result, materials.shell, (function(itemdef) return itemdef.props.flags.SCALED end))
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, mat in ipairs{materials.wood, materials.leather} do
2014-05-04 15:02:12 -06:00
resource_reactions(result, job_types.MakeShield, mat, entity.resources.shield_type, itemdefs.shields, {})
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
-- Melt a Metal Object
reaction_entry(result, job_types.MeltMetalObject)
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return result
end
screen = gui.FramedScreen {
frame_title = "Select Stockpile Order",
}
function screen:onRenderBody(dc)
-- Emulates the built-in manager screen.
if not (self.page_size == self.frame_rect.height - ExtraLines) then
-- The screen size has changed.
self:refilter()
end
-- Top instruction line.
2013-05-24 12:43:40 -06:00
dc:seek(1, 1):string("Type in parts of the name to narrow your search. ", COLOR_WHITE)
2016-05-15 08:58:45 -06:00
dc:key("LEAVESCREEN"):string(" to abort.", COLOR_WHITE)
-- Search term, if any.
dc:seek(1, FirstRow + self.page_size + 1):string(self.search_string, COLOR_LIGHTCYAN)
-- Bottom instruction line.
dc:seek(1, FirstRow + self.page_size + 2)
2016-05-15 08:58:45 -06:00
dc:key("STANDARDSCROLL_UP"):key("STANDARDSCROLL_DOWN")
dc:key("STANDARDSCROLL_PAGEUP"):key("STANDARDSCROLL_PAGEDOWN")
dc:key("STANDARDSCROLL_LEFT"):key("STANDARDSCROLL_RIGHT")
dc:string(": Select", COLOR_WHITE)
dc:seek(CenterCol, FirstRow + self.page_size + 2)
2016-05-15 08:58:45 -06:00
dc:key("SETUPGAME_SAVE_PROFILE_ABORT"):string(": No order", COLOR_WHITE)
-- Reaction lines.
2013-05-24 12:43:40 -06:00
for _, item in ipairs(self.displayed) do
dc:seek(item.x, item.y):string(item.name, item.color)
end
end
function screen:onInput(keys)
if keys.LEAVESCREEN then
self:dismiss()
elseif keys.SELECT then
self:dismiss()
local selected = self.reactions[self.position]
if selected then
store_order(self.stockpile, selected.index)
end
elseif keys.SETUPGAME_SAVE_PROFILE_ABORT then
self:dismiss()
clear_order(self.stockpile)
2013-05-24 12:43:40 -06:00
elseif keys.STANDARDSCROLL_UP then
self.position = self.position - 1
elseif keys.STANDARDSCROLL_DOWN then
self.position = self.position + 1
elseif keys.STANDARDSCROLL_LEFT then
if self.position == 1 then
-- Move from the very first item to the very last item.
self.position = #self.reactions
elseif self.position < self.page_size then
-- On the first column, move to the very first item.
self.position = 1
else
-- Move to the same position on the previous column.
self.position = self.position - self.page_size
end
2013-05-24 12:43:40 -06:00
elseif keys.STANDARDSCROLL_RIGHT then
if self.position == #self.reactions then
-- Move from the very last item to the very first item.
self.position = 1
else
-- Move to the same position on the next column.
self.position = self.position + self.page_size
if self.position > #self.reactions then
-- If that's past the end, move to the very last item.
self.position = #self.reactions
end
end
2013-05-24 12:43:40 -06:00
elseif keys.STANDARDSCROLL_PAGEUP then
if self.position == 1 then
-- Move from the very first item to the very last item.
self.position = #self.reactions
elseif self.position < self.page_size*2 then
-- On the first page, move to the very first item.
self.position = 1
else
-- Move to the same position on the previous page.
self.position = self.position - self.page_size*2
end
2013-05-24 12:43:40 -06:00
elseif keys.STANDARDSCROLL_PAGEDOWN then
if self.position == #self.reactions then
-- Move from the very last item to the very first item.
self.position = 1
else
-- Move to the same position on the next page.
self.position = self.position + self.page_size*2
if self.position > #self.reactions then
-- If that's past the end, move to the very last item.
self.position = #self.reactions
end
end
2013-05-24 12:43:40 -06:00
elseif keys.STRING_A000 then
-- This seems like an odd way to check for Backspace.
self.search_string = string.sub(self.search_string, 1, -2)
self.position = 1
2013-05-24 12:43:40 -06:00
elseif keys._STRING and keys._STRING >= 32 then
-- This interface only accepts letters and spaces.
local char = string.char(keys._STRING)
if char == " " or string.find(char, "^%a") then
self.search_string = self.search_string .. string.upper(char)
self.position = 1
2013-05-24 12:43:40 -06:00
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
self:refilter()
end
function screen:reset(stockpile)
self.stockpile = stockpile
self.search_string = ""
self.position = 1
self:refilter()
end
function matchall(haystack, needles)
for _, needle in ipairs(needles) do
if not string.find(haystack, needle) then
return false
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return true
end
function splitstring(full, pattern)
local last = string.len(full)
local result = {}
local n = 1
while n <= last do
local start, stop = string.find(full, pattern, n)
if not start then
result[#result+1] = string.sub(full, n)
break
elseif start > n then
result[#result+1] = string.sub(full, n, start - 1)
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if stop < n then
-- The pattern matches an empty string.
-- Avoid an infinite loop.
break
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
n = stop + 1
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return result
end
function screen:refilter()
-- Determine which rows to show, and in which colors.
-- Todo: The official one now has three categories of search results:
-- * Cyan: Contains at least one exact word from the search terms
-- * Yellow: At least one word starts with at least one search term
-- * Grey: Each search term is found in the middle of a word
self.page_size = self.frame_rect.height - ExtraLines
2013-05-24 12:43:40 -06:00
local filtered = {}
local needles = splitstring(self.search_string, " ")
for key, value in ipairs(reaction_list) do
if matchall(string.upper(value.name), needles) then
filtered[#filtered+1] = {
index = key,
name = value.name
}
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if self.position < 1 then
self.position = #filtered
elseif self.position > #filtered then
self.position = 1
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
local start = 1
while self.position >= start + self.page_size*2 do
start = start + self.page_size*2
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
local displayed = {}
for n = 0, self.page_size*2 - 1 do
2013-05-24 12:43:40 -06:00
local item = filtered[start + n]
if not item then
break
end
local name = item.name
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
local x = 1
local y = FirstRow + n
if n >= self.page_size then
x = CenterCol
y = y - self.page_size
name = " "..name
2013-05-24 12:43:40 -06:00
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
local color = COLOR_CYAN
if start + n == self.position then
color = COLOR_LIGHTCYAN
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
displayed[n + 1] = {
x = x,
y = y,
name = name,
2013-05-24 12:43:40 -06:00
color = color,
}
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
self.reactions = filtered
self.displayed = displayed
end
function clear_order(stockpile)
local saved = saved_orders[stockpile.id]
if saved then
saved.entry:delete()
saved_orders[stockpile.id] = nil
end
end
2013-05-24 12:43:40 -06:00
function store_order(stockpile, order_number)
local name = reaction_list[order_number].name
-- print("Setting stockpile #"..stockpile.stockpile_number.." to "..name.." (#"..order_number..")")
local saved = saved_orders[stockpile.id]
if saved then
saved.entry.value = name
saved.entry.ints[entry_ints.order_number] = order_number
2013-05-24 12:43:40 -06:00
saved.entry:save()
else
saved_orders[stockpile.id] = {
stockpile = stockpile,
entry = dfhack.persistent.save{
key = "stockflow/entry/"..stockpile.id,
value = name,
ints = {
stockpile.id,
order_number,
1,
},
},
}
end
end
-- Compare the job specification of two orders.
function orders_match(a, b)
local fields = {
"job_type",
"item_subtype",
"reaction_name",
"mat_type",
"mat_index",
}
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, fieldname in ipairs(fields) do
if a[fieldname] ~= b[fieldname] then
return false
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
local subtables = {
"item_category",
"material_category",
}
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
for _, fieldname in ipairs(subtables) do
local aa = a[fieldname]
local bb = b[fieldname]
for key, value in ipairs(aa) do
if bb[key] ~= value then
return false
end
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return true
end
-- Reduce the quantity by the number of matching orders in the queue.
function order_quantity(order, quantity)
local amount = quantity
for _, managed in ipairs(df.global.world.manager_orders) do
if orders_match(order, managed) then
amount = amount - managed.amount_left
if amount < 0 then
return 0
end
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return amount
end
-- Place a new copy of the order onto the manager's queue.
function create_orders(order, amount)
local new_order = order:new()
new_order.amount_left = amount
new_order.amount_total = amount
-- Todo: Create in a validated state if the fortress is small enough?
new_order.status.validated = false
new_order.status.active = false
new_order.id = df.global.world.manager_order_next_id
df.global.world.manager_order_next_id = df.global.world.manager_order_next_id + 1
2013-05-24 12:43:40 -06:00
df.global.world.manager_orders:insert('#', new_order)
end
function countContents(container, settings)
local total = 0
for _, item in ipairs(dfhack.items.getContainedItems(container)) do
if item.flags.container then
-- Recursively count the total of items contained.
-- Not likely to go more than two levels deep.
local subtotal = countContents(item, settings)
2013-05-24 12:43:40 -06:00
if subtotal > 0 then
-- Ignore the inner container itself;
-- generally, only the contained items matter.
total = total + subtotal
elseif matches_stockpile(item, settings) then
-- The container may or may not be empty,
-- but is stockpiled as a container itself.
total = total + 1
end
elseif matches_stockpile(item, settings) then
total = total + 1
end
end
2015-02-14 20:53:06 -07:00
return total
2013-05-24 12:43:40 -06:00
end
function check_stockpiles(verbose)
local result = {}
for _, spec in pairs(saved_orders) do
local trigger = triggers[spec.entry.ints[entry_ints.trigger_number]]
2013-05-24 12:43:40 -06:00
if trigger and trigger.divisor then
local reaction = spec.entry.ints[entry_ints.order_number]
2013-05-24 12:43:40 -06:00
local filled, empty = check_pile(spec.stockpile, verbose)
local amount = math.floor((trigger.filled and filled or empty) / trigger.divisor)
2013-05-24 12:43:40 -06:00
result[reaction] = (result[reaction] or 0) + amount
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return result
end
function check_pile(sp, verbose)
local numspaces = 0
local filled = 0
local empty = 0
for y = sp.y1, sp.y2 do
for x = sp.x1, sp.x2 do
2014-08-23 17:46:35 -06:00
if dfhack.buildings.containsTile(sp, x, y) then
2013-05-24 12:43:40 -06:00
numspaces = numspaces + 1
local designation, occupancy = dfhack.maps.getTileFlags(x, y, sp.z)
if not designation.liquid_type then
if not occupancy.item then
empty = empty + 1
end
end
end
end
end
2015-02-14 20:53:06 -07:00
for _, item in ipairs(dfhack.buildings.getStockpileContents(sp)) do
if item:isAssignedToThisStockpile(sp.id) then
-- This is a bin or barrel associated with the stockpile.
filled = filled + countContents(item, sp.settings)
elseif matches_stockpile(item, sp.settings) then
filled = filled + 1
end
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
if verbose then
print("Stockpile #"..sp.stockpile_number,
string.format("%3d spaces", numspaces),
string.format("%4d items", filled),
string.format("%4d empty spaces", empty))
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return filled, empty
end
function matches_stockpile(item, settings)
-- Check whether the item matches the stockpile.
-- FIXME: This is starting to look like a whole lot of work.
if df.item_barst:is_instance(item) then
return settings.flags.bars_blocks
elseif df.item_blocksst:is_instance(item) then
return settings.flags.bars_blocks
elseif df.item_smallgemst:is_instance(item) then
return settings.flags.gems
elseif df.item_boulderst:is_instance(item) then
return settings.flags.stone
elseif df.item_woodst:is_instance(item) then
return settings.flags.wood
elseif df.item_seedsst:is_instance(item) then
return settings.flags.food
elseif df.item_meatst:is_instance(item) then
return settings.flags.food
elseif df.item_plantst:is_instance(item) then
return settings.flags.food
elseif df.item_cheesest:is_instance(item) then
return settings.flags.food
elseif df.item_globst:is_instance(item) then
return settings.flags.food
elseif df.item_fishst:is_instance(item) then
return settings.flags.food
elseif df.item_fish_rawst:is_instance(item) then
return settings.flags.food
elseif df.item_foodst:is_instance(item) then
return settings.flags.food
elseif df.item_drinkst:is_instance(item) then
return settings.flags.food
elseif df.item_eggst:is_instance(item) then
return settings.flags.food
elseif df.item_skin_tannedst:is_instance(item) then
return settings.flags.leather
elseif df.item_remainsst:is_instance(item) then
return settings.flags.refuse
elseif df.item_verminst:is_instance(item) then
return settings.flags.animals
elseif df.item_petst:is_instance(item) then
return settings.flags.animals
elseif df.item_threadst:is_instance(item) then
return settings.flags.cloth
end
2015-02-14 20:53:06 -07:00
2013-05-24 12:43:40 -06:00
return true
end
return _ENV