Files

764 lines
38 KiB
Lua

-- AD Flow Guide
-- "Opsæt flow": flowguidens trin oven på ADGuide (ren Lua, ingen spil-API) — samme trin-skærm og knapper
-- som markguiden. Kladden er en kopi af flowets felter, så intet ændres før [Gem]/[Start flow]. Det
-- spil-afhængige kommer i `context` (GUI'et henter det ud fra kladden, se previewRun):
-- {buildings = {{id, name, label, hasMarker, text}} -- kandidater til Fra (udkørsel) / Til (forsyning)
-- fillTypes = {{name, title}} -- varer målet tager (forsyning, Vælg selv)
-- products = {{name, title}} -- varer kilderne har (udkørsel)
-- storeBuildings = {{id, name, text}} -- lagre der kan tage flowets lagervarer
-- feedBuildings = {{id, name, text}} -- "Lever også til": bygninger der tager kildernes varer
-- waitPools = {{group, count}}
-- vehicles = {{id, label, text, otherRunId, otherRunName, blocked, solo}} -- label = kort navn (resumé)
-- wagons = {{id, label}}} -- ledige vogne til solo-traktorer (bedste først)
-- Solo-traktor (ingen vogn bagpå) får en ledig vogn fra context.wagons (draft.wagons = {[traktorId] = vognId}),
-- som den henter ved "Connect <vognens parkering>" — uden at vognens parkering skal omdøbes.
-- Gem: ADRunsController.createRun/updateRun + assignVehicle/unassignVehicle (GUI'et).
ADFlowGuide = {}
ADFlowGuide.ORDER = {"name", "direction", "buildings", "goods", "store", "feed", "rules", "wait", "tractors", "summary"}
ADFlowGuide.POLICIES = {"off", "store", "sell", "sellElseStore", "keep"}
ADFlowGuide.MAX_NAMED_TRACTORS = 3
ADFlowGuide.MAX_NAMED_GOODS = 3
local function L(key, args) return {key = key, args = args} end
local function T(text) return {text = text} end
local function isOut(guide) return guide.draft.kind == "outbound" end
local function hasStorePolicy(draft)
for _, product in ipairs(draft.products or {}) do
if product.policy == "store" or product.policy == "sellElseStore" then return true end
end
return false
end
local VISIBLE = {
name = function(guide) return guide.draft.isNew == true end,
store = function(guide) return isOut(guide) and hasStorePolicy(guide.draft) end,
-- kun når der er noget at vælge (eller noget er valgt): skjul det irrelevante
feed = function(guide)
return isOut(guide) and (#((guide.context or {}).feedBuildings or {}) > 0 or #(guide.draft.feedBuildingIds or {}) > 0)
end,
}
function ADFlowGuide.isVisible(step, guide)
local rule = VISIBLE[step]
if rule ~= nil then return rule(guide) == true end
for _, id in ipairs(ADFlowGuide.ORDER) do
if id == step then return true end
end
return false
end
ADFlowGuide.DEF = {order = ADFlowGuide.ORDER, isVisible = ADFlowGuide.isVisible}
-- Overskrift pr. trin; "Fra"/"Til" følger retningen.
function ADFlowGuide.stepKey(step, guide)
if step == "buildings" then
return (guide ~= nil and isOut(guide)) and "spu_guide_flow_step_from" or "spu_guide_flow_step_to"
end
return "spu_guide_flow_step_" .. tostring(step)
end
local KIND_HINTS = {buildings = true, goods = true, rules = true}
function ADFlowGuide.hintKey(step, guide)
if KIND_HINTS[step] then
return "spu_guide_flow_hint_" .. step .. ((guide ~= nil and isOut(guide)) and "_out" or "_in")
end
return "spu_guide_flow_hint_" .. tostring(step)
end
-- ---- kladde ----
local RUN_FIELDS = {
"id", "name", "kind", "sourceBuildingIds", "targetBuildingIds", "storeBuildingIds", "products",
"ingredientMode", "ingredients", "sellAtPercent", "storeAbovePercent", "sellAbovePercent", "minSourcePercent", "collectRound", "loops",
"returnBelowPercent", "waitPoolGroup", "vehicleIds", "wagons", "chain", "spareWagons", "borrowWagonId", "sellUnused",
"feedBuildingIds", "lockSet",
}
local function draftFrom(run, isNew)
local draft = {}
for _, key in ipairs(RUN_FIELDS) do draft[key] = ADGuide.deepCopy(run[key]) end
for _, key in ipairs({"sourceBuildingIds", "targetBuildingIds", "storeBuildingIds", "feedBuildingIds", "products", "ingredients", "vehicleIds"}) do
draft[key] = draft[key] or {}
end
draft.kind = draft.kind == "outbound" and "outbound" or "supply"
draft.ingredientMode = draft.ingredientMode == "manual" and "manual" or "auto"
draft.sellAtPercent = draft.sellAtPercent or ADRuns.DEFAULT_SELL_AT_PERCENT
draft.storeAbovePercent = draft.storeAbovePercent or ADRuns.DEFAULT_STORE_ABOVE_PERCENT
draft.sellAbovePercent = draft.sellAbovePercent or ADRuns.DEFAULT_SELL_ABOVE_PERCENT
draft.minSourcePercent = draft.minSourcePercent or ADRuns.DEFAULT_MIN_SOURCE_PERCENT
draft.returnBelowPercent = draft.returnBelowPercent or ADRuns.DEFAULT_RETURN_BELOW_PERCENT
draft.loops = draft.loops or 0
draft.waitPoolGroup = draft.waitPoolGroup or ""
draft.wagons = draft.wagons or {}
draft.spareWagons = draft.spareWagons or {}
draft.collectRound = draft.collectRound == true
draft.lockSet = draft.lockSet == true
draft.isNew = isNew == true
draft.showAllBuildings = false
draft.storeAuto = #draft.storeBuildingIds == 0
draft.borrowTractor = draft.borrowWagonId ~= nil
draft.wasBorrow = draft.borrowTractor -- allerede lån: flowets traktor er en lånt, der kører turen færdig
return draft
end
function ADFlowGuide.open(run, context, options)
return ADGuide.new(ADFlowGuide.DEF, draftFrom(run, false), context, options)
end
-- Nyt flow: intet oprettes før [Gem]; kladden har standardværdierne fra ADRuns.createRun.
function ADFlowGuide.openNew(name, context)
local _, run = ADRuns.createRun(ADRuns.newStore(), name, "supply")
local draft = draftFrom(run, true)
draft.id = nil
return ADGuide.new(ADFlowGuide.DEF, draft, context)
end
-- Run-formet tabel til controllerens opslag (varer, vogne) før noget er gemt: markører fra de valgte
-- bygninger. markerIds(buildingId, role) -> {wayPointId, ...}.
function ADFlowGuide.previewRun(draft, markerIds)
local run = ADGuide.deepCopy(draft)
local function resolve(ids, role)
local result, seen = {}, {}
for _, buildingId in ipairs(ids or {}) do
for _, wayPointId in ipairs(markerIds(buildingId, role) or {}) do
if not seen[wayPointId] then
seen[wayPointId] = true
table.insert(result, wayPointId)
end
end
end
return result
end
run.sourceWayPointIds = resolve(draft.sourceBuildingIds, "load")
run.targetWayPointIds = resolve(draft.targetBuildingIds, "unload")
run.targetWayPointId = run.targetWayPointIds[1]
return run
end
-- ---- vogne til solo-traktorer ----
local function soloIds(context)
local set = {}
for _, vehicle in ipairs((context or {}).vehicles or {}) do
if vehicle.solo then set[vehicle.id] = true end
end
return set
end
-- Ny vogn-tabel: fravalgte traktorer slipper deres vogn; valgte solo-traktorer uden (gyldig) vogn får den første
-- ledige i context.wagons, som ingen anden traktor i flowet har. Vogne til ikke-solo-traktorer bevares (husket).
function ADFlowGuide.assignWagons(draft, context)
local chosen, solo, offered = {}, soloIds(context), {}
for _, id in ipairs(draft.vehicleIds or {}) do chosen[id] = true end
for _, wagon in ipairs((context or {}).wagons or {}) do offered[wagon.id] = true end
local wagons, used = {}, {}
-- ekstra vogne er optaget (én traktor, flere vogne): de gives ikke til en anden traktor
for tractorId, list in pairs(draft.spareWagons or {}) do
if chosen[tractorId] then for _, id in ipairs(list) do used[id] = true end end
end
for tractorId, wagonId in pairs(draft.wagons or {}) do
local valid = chosen[tractorId] and (not solo[tractorId] or offered[wagonId])
if valid and not used[wagonId] then
wagons[tractorId] = wagonId
used[wagonId] = true
end
end
for _, tractorId in ipairs(draft.vehicleIds or {}) do
if solo[tractorId] and wagons[tractorId] == nil then
for _, wagon in ipairs((context or {}).wagons or {}) do
if not used[wagon.id] then
wagons[tractorId] = wagon.id
used[wagon.id] = true
break
end
end
end
end
return wagons
end
-- ---- valg ----
local SET_VALUES = {
kind = true, ingredientMode = true, sellAtPercent = true, storeAbovePercent = true, sellAbovePercent = true, minSourcePercent = true, loops = true,
returnBelowPercent = true, waitPoolGroup = true, borrowWagonId = true,
}
local BOOL_VALUES = {showAllBuildings = true, storeAuto = true, collectRound = true, borrowTractor = true, sellUnused = true, lockSet = true}
local LIST_VALUES = {ingredients = true, storeBuildingIds = true, feedBuildingIds = true, vehicleIds = true}
local function setPolicy(products, name, policy)
local result = {}
for _, product in ipairs(products or {}) do
if product.fillType ~= name then table.insert(result, {fillType = product.fillType, policy = product.policy}) end
end
if policy ~= "off" then table.insert(result, {fillType = name, policy = policy}) end
return result
end
function ADFlowGuide.choose(guide, id, value)
if id == "showBlocked" then
-- visning, ikke en del af flowet: fold listen over traktorer der ikke kan bruges ud/ind
local copy = {}
for key, field in pairs(guide) do copy[key] = field end
copy.showBlocked = not guide.showBlocked
return copy
end
local draft = ADGuide.deepCopy(guide.draft)
if SET_VALUES[id] then
draft[id] = value
elseif BOOL_VALUES[id] then
draft[id] = value == true
elseif LIST_VALUES[id] then
draft[id] = ADGuide.toggleItem(draft[id], value.item, value.on)
if id == "vehicleIds" then draft.wagons = ADFlowGuide.assignWagons(draft, guide.context) end
elseif id == "wagon" then
draft.wagons = ADGuide.deepCopy(draft.wagons or {})
draft.wagons[value.item] = value.value
-- valgt som aktiv: ikke længere ekstra
draft.spareWagons = ADGuide.deepCopy(draft.spareWagons or {})
local list = {}
for _, spareId in ipairs(draft.spareWagons[value.item] or {}) do if spareId ~= value.value then table.insert(list, spareId) end end
draft.spareWagons[value.item] = list
elseif id == "spareWagon" then
-- én ekstra vogn pr. traktor i guiden ("" = ingen); traktoren skifter mellem ture (ADWagonSwap)
draft.spareWagons = ADGuide.deepCopy(draft.spareWagons or {})
draft.spareWagons[value.item] = (value.value ~= nil and value.value ~= "") and {value.value} or {}
elseif id == "buildings" then
local key = draft.kind == "outbound" and "sourceBuildingIds" or "targetBuildingIds"
draft[key] = ADGuide.toggleItem(draft[key], value.item, value.on)
elseif id == "policy" then
draft.products = setPolicy(draft.products, value.item, value.value)
elseif id == "name" then
if type(value) ~= "string" or not value:match("%S") then return guide end
draft.name = value
else
return guide
end
return ADGuide.withDraft(guide, draft)
end
-- ---- rækker ----
-- Hjælpetekst pr. række (in-game 19/10: alle valgrækker viste varepolitikkens tekst, fordi de er kloner af varerækken)
ADFlowGuide.TOOLTIPS = {
policy = "spu_policy_tooltip", ingredientMode = "spu_ingredientMode_tooltip", sellAtPercent = "spu_sellAt_tooltip",
storeAbovePercent = "spu_storeAbove_tooltip", sellAbovePercent = "spu_sellAbove_tooltip", minSourcePercent = "spu_minSource_tooltip",
collectRound = "spu_collectRound_tooltip", loops = "spu_loops_tooltip", returnBelowPercent = "spu_returnBelow_tooltip",
waitPoolGroup = "spu_waitPool_tooltip", sellUnused = "spu_guide_flow_sellUnused_tooltip", borrowTractor = "spu_guide_flow_borrow_tooltip", borrowWagonId = "spu_guide_flow_borrowWagon_tooltip",
lockSet = "spu_guide_flow_lockSet_tooltip",
}
local function choice(id, title, options, value, item)
return {type = "choice", id = id, title = title, options = options, value = value, item = item, tooltip = ADFlowGuide.TOOLTIPS[id]}
end
local function toggle(id, title, value)
return {type = "toggle", id = id, title = title, off = L("ui_no"), on = L("ui_yes"), value = value == true, tooltip = ADFlowGuide.TOOLTIPS[id]}
end
local function check(id, item, text, value)
return {type = "check", id = id, item = item, title = T(text), value = value == true}
end
local function text(key, args) return {type = "text", label = L(key, args)} end
local function percentOptions(from, to, step)
local options = {}
for percent = from, to, step do table.insert(options, {label = T(string.format("%d %%", percent)), value = percent}) end
return options
end
-- "Sælg altid over": Fra, 50-100 %
local function sellAboveOptions()
local options = {{label = L("spu_sellAbove_off"), value = 0}}
for _, option in ipairs(percentOptions(50, ADRuns.MAX_SELL_ABOVE_PERCENT, 5)) do table.insert(options, option) end
return options
end
local function minSourceOptions()
local options = {{label = L("spu_minSource_off"), value = 0}}
for _, option in ipairs(percentOptions(10, 90, 10)) do table.insert(options, option) end
return options
end
local function chosenSet(list)
local set = {}
for _, value in ipairs(list or {}) do set[value] = true end
return set
end
local function buildingIds(draft)
return draft.kind == "outbound" and draft.sourceBuildingIds or draft.targetBuildingIds
end
local function rowsName(guide)
return {
{type = "text", label = T(guide.draft.name or "")},
{type = "link", id = "rename", value = true, label = L("spu_guide_flow_rename")},
}
end
local function rowsDirection(guide)
return {choice("kind", L("spu_direction"), {
{label = L("spu_direction_in"), value = "supply"}, {label = L("spu_direction_out"), value = "outbound"}}, guide.draft.kind)}
end
-- Bygninger i fast rækkefølge (navn, markør); uden markør kun med "Vis alle" (eller når valgt).
local function rowsBuildings(guide)
local draft, rows = guide.draft, {}
local chosen = chosenSet(buildingIds(draft))
local candidates = guide.context.buildings or {}
local shown = ADFlowView.buildingRows(candidates, chosen, draft.showAllBuildings)
local hidden = ADFlowView.hiddenCount(candidates, shown)
if hidden > 0 or draft.showAllBuildings then
table.insert(rows, toggle("showAllBuildings", L("spu_showAllBuildings", {hidden}), draft.showAllBuildings))
end
for _, building in ipairs(shown) do
table.insert(rows, check("buildings", building.id, building.text or building.name, chosen[building.id]))
end
if #shown == 0 then table.insert(rows, text("spu_noBuildings")) end
return rows
end
local function rowsGoods(guide)
local draft, context, rows = guide.draft, guide.context, {}
if isOut(guide) then
local products = context.products or {}
if #products == 0 then
-- kilde valgt, men den kan intet levere — ikke "vælg først en kilde" (in-game 11/10)
local chosenSources = buildingIds(draft) or {}
return {text(#chosenSources > 0 and "spu_guide_flow_noProductsAtSource" or "spu_guide_flow_noProducts")}
end
local options = {}
for _, policy in ipairs(ADFlowGuide.POLICIES) do table.insert(options, {label = L("spu_policy_" .. policy), value = policy}) end
-- øverst: sælg alt, gården ikke bruger (varerne mærket "ikke brugt på gården"; valgte varer følger deres egen politik)
table.insert(rows, toggle("sellUnused", L("spu_guide_flow_sellUnused"), draft.sellUnused))
for _, product in ipairs(products) do
table.insert(rows, choice("policy", T(product.text or product.title), options, ADRuns.getProductPolicy(draft, product.name), product.name))
end
return rows
end
table.insert(rows, choice("ingredientMode", L("spu_guide_flow_goods"), {
{label = L("spu_guide_flow_goodsAutoChoice"), value = "auto"}, {label = L("spu_guide_manual"), value = "manual"}}, draft.ingredientMode))
if draft.ingredientMode ~= "manual" then
table.insert(rows, text("spu_guide_flow_goodsAutoText"))
return rows
end
local chosen = chosenSet(draft.ingredients)
for _, fillType in ipairs(context.fillTypes or {}) do
table.insert(rows, check("ingredients", fillType.name, " " .. tostring(fillType.title), chosen[fillType.name]))
end
if #(context.fillTypes or {}) == 0 then table.insert(rows, text("spu_guide_flow_noGoodsTo")) end
return rows
end
local function rowsStore(guide)
local draft, rows = guide.draft, {}
table.insert(rows, choice("storeAuto", L("spu_guide_store"), {
{label = L("spu_guide_flow_storeAutoChoice"), value = true}, {label = L("spu_guide_manual"), value = false}}, draft.storeAuto))
if draft.storeAuto then
table.insert(rows, text("spu_guide_flow_storeAutoText"))
return rows
end
local chosen = chosenSet(draft.storeBuildingIds)
for _, building in ipairs(guide.context.storeBuildings or {}) do
table.insert(rows, check("storeBuildingIds", building.id, building.text, chosen[building.id]))
end
if #(guide.context.storeBuildings or {}) == 0 then table.insert(rows, text("spu_noBuildings")) end
return rows
end
-- "Lever også til" (20/10): bygninger der fodres først med kildernes varer (fiskefoder til Young Fish Breeding)
local function rowsFeed(guide)
local draft, rows = guide.draft, {text("spu_guide_flow_feedText")}
local chosen = chosenSet(draft.feedBuildingIds)
for _, building in ipairs(guide.context.feedBuildings or {}) do
table.insert(rows, check("feedBuildingIds", building.id, building.text, chosen[building.id]))
end
if #(guide.context.feedBuildings or {}) == 0 then table.insert(rows, text("spu_guide_flow_noFeedBuildings")) end
return rows
end
local function productPolicies(draft)
local policies = {}
for _, product in ipairs(draft.products or {}) do table.insert(policies, product.policy) end
if draft.sellUnused then table.insert(policies, "sell") end
return policies
end
-- Kun regler varernes politik bruger: rent lager-flow viser ikke salgsgrænser og omvendt (in-game 29/9).
local function rowsRules(guide)
local draft = guide.draft
if isOut(guide) then
local sells, stores = ADFlowView.policyUse(productPolicies(draft))
local rows = {}
if sells then
-- "Altid" (0) øverst: sælg uanset pris (in-game 30/9)
local options = {{label = L("spu_sellAt_always"), value = 0}}
for _, option in ipairs(percentOptions(ADRuns.MIN_SELL_AT_PERCENT, ADRuns.MAX_SELL_AT_PERCENT, 5)) do table.insert(options, option) end
table.insert(rows, choice("sellAtPercent", L("spu_sellAt"), options, draft.sellAtPercent))
end
if stores then table.insert(rows, choice("storeAbovePercent", L("spu_storeAbove"), percentOptions(ADRuns.MIN_STORE_ABOVE_PERCENT, ADRuns.MAX_STORE_ABOVE_PERCENT, 5), draft.storeAbovePercent)) end
if sells then table.insert(rows, choice("sellAbovePercent", L("spu_sellAbove"), sellAboveOptions(), draft.sellAbovePercent)) end
if sells then table.insert(rows, choice("minSourcePercent", L("spu_minSource"), minSourceOptions(), draft.minSourcePercent or 0)) end
table.insert(rows, toggle("collectRound", L("spu_collectRound"), draft.collectRound))
return rows
end
local loops = {{label = L("spu_loops_infinite"), value = 0}}
for count = 1, ADRuns.MAX_LOOPS do table.insert(loops, {label = T(tostring(count)), value = count}) end
return {
choice("loops", L("spu_loops"), loops, draft.loops),
choice("returnBelowPercent", L("spu_returnBelow"), percentOptions(ADRuns.MIN_RETURN_BELOW_PERCENT, ADRuns.MAX_RETURN_BELOW_PERCENT, 10), draft.returnBelowPercent),
}
end
local function rowsWait(guide)
local options = {{label = L("spu_waitPool_all"), value = ""}}
for _, pool in ipairs(guide.context.waitPools or {}) do
table.insert(options, {label = T(string.format("%s (%d)", tostring(pool.group), pool.count or 0)), value = pool.group})
end
return {choice("waitPoolGroup", L("spu_waitPool"), options, guide.draft.waitPoolGroup or "")}
end
-- Vognvalg under en valgt solo-traktor: dens egen vogn + ledige vogne ingen anden traktor i flowet har.
local function rowWagon(guide, tractorId, ownWagon)
local wagons = guide.draft.wagons or {}
local usedByOthers = {}
for otherId, wagonId in pairs(wagons) do
if otherId ~= tractorId then usedByOthers[wagonId] = true end
end
-- andre traktorers ekstra vogne er også optaget
for otherId, list in pairs(guide.draft.spareWagons or {}) do
if otherId ~= tractorId then for _, id in ipairs(list) do usedByOthers[id] = true end end
end
local options = {}
-- vognen der hænger på traktoren nu står først (vælg en anden for at bytte ved næste start)
if ownWagon ~= nil and ownWagon.id ~= nil then table.insert(options, {label = T(tostring(ownWagon.label)), value = ownWagon.id}) end
for _, wagon in ipairs(guide.context.wagons or {}) do
if not usedByOthers[wagon.id] and (ownWagon == nil or wagon.id ~= ownWagon.id) then
table.insert(options, {label = T(tostring(wagon.label)), value = wagon.id})
end
end
local current = wagons[tractorId] or (ownWagon ~= nil and ownWagon.id or nil)
if #options == 0 or current == nil then return text("spu_guide_flow_noFreeWagon") end
return choice("wagon", L("spu_guide_flow_wagon"), options, current, tractorId)
end
-- Ekstra vogn under en traktor med vogn (én traktor, flere vogntyper): "ingen" + ledige vogne, der hverken er en
-- traktors aktive vogn eller en anden traktors ekstra vogn. nil = ingen ledige at vælge imellem.
local function rowSpare(guide, tractorId, ownWagon)
local draft = guide.draft
local taken = {}
for _, wagonId in pairs(draft.wagons or {}) do taken[wagonId] = true end
if ownWagon ~= nil and ownWagon.id ~= nil then taken[ownWagon.id] = true end
for otherId, list in pairs(draft.spareWagons or {}) do
if otherId ~= tractorId then for _, id in ipairs(list) do taken[id] = true end end
end
local options = {{label = L("spu_guide_flow_noSpare"), value = ""}}
for _, wagon in ipairs(guide.context.wagons or {}) do
if not taken[wagon.id] then table.insert(options, {label = T(tostring(wagon.label)), value = wagon.id}) end
end
if #options == 1 then return nil end
local current = ((draft.spareWagons or {})[tractorId] or {})[1] or ""
return choice("spareWagon", L("spu_guide_flow_spareWagon"), options, current, tractorId)
end
-- Vogne flowets egne traktorer har på nu og som kan parkeres og hentes ved Connect (in-game 19/10: honning-flowets
-- Autoload-vogn sad på 1050'en og stod derfor ikke som ledig — spilleren skulle stoppe flowet først). -> {{id, label}}
function ADFlowGuide.ownParkableWagons(draft, context)
-- et flow der allerede låner: traktoren er lånt og sættes ikke af ved gem (review 19/10: teksten lovede det)
if draft.wasBorrow then return {} end
local chosen, list = chosenSet(draft.vehicleIds), {}
for _, vehicle in ipairs((context or {}).vehicles or {}) do
local own = vehicle.ownWagon
if chosen[vehicle.id] and own ~= nil and own.id ~= nil and own.parkName ~= nil then
table.insert(list, {id = own.id, park = own.parkName, tractor = tostring(vehicle.label or vehicle.text)})
end
end
return list
end
-- Lånevognen: den valgte (flowets egen — også mens en lånt traktor kører med den og den derfor ikke står som ledig),
-- ellers den vogn flowets traktor har på nu, ellers den første ledige vogn der passer til varerne.
function ADFlowGuide.borrowWagon(draft, context)
if draft.borrowWagonId ~= nil and draft.borrowWagonId ~= "" then return draft.borrowWagonId end
local own = ADFlowGuide.ownParkableWagons(draft, context)[1]
if own ~= nil then return own.id end
local first = ((context or {}).wagons or {})[1]
return first ~= nil and first.id or nil
end
-- Lånt traktor (brugerønske 19/10): kun en vogn vælges; ingen traktor er bundet til flowet.
local function rowsBorrow(guide)
local rows = {toggle("borrowTractor", L("spu_guide_flow_borrow"), true)}
local options, listed = {}, false
local current = ADFlowGuide.borrowWagon(guide.draft, guide.context)
for _, wagon in ipairs(ADFlowGuide.ownParkableWagons(guide.draft, guide.context)) do
table.insert(options, {label = L("spu_guide_flow_borrowOwnWagon", {wagon.park, wagon.tractor}), value = wagon.id})
if wagon.id == current then listed = true end
end
for _, wagon in ipairs(guide.context.wagons or {}) do
table.insert(options, {label = T(tostring(wagon.label)), value = wagon.id})
if wagon.id == current then listed = true end
end
-- flowets vogn er ude at køre (sidder på den lånte traktor): den står ikke som ledig, men er stadig valget
if current ~= nil and not listed then table.insert(options, 1, {label = L("spu_guide_flow_borrowWagonInUse"), value = current}) end
if current == nil then
-- hvad der mangler, så spilleren kan rette det (in-game 19/10: Autoload-vognene havde ingen AD-parkering)
table.insert(rows, text("spu_guide_flow_borrowNoWagon"))
else
table.insert(rows, choice("borrowWagonId", L("spu_guide_flow_borrowWagon"), options, current))
end
table.insert(rows, text("spu_guide_flow_borrowText"))
return rows
end
-- Traktorer: flowets egne først, så de ledige. Traktorer i andre flows og blokerede (vognen passer ikke)
-- skjules med en linje hver.
local function rowsTractors(guide)
local draft, rows = guide.draft, {}
if draft.borrowTractor then return rowsBorrow(guide) end
table.insert(rows, toggle("borrowTractor", L("spu_guide_flow_borrow"), false))
-- låst sæt (20/10): traktor + vogn lånes aldrig ud og køres ikke hjem
table.insert(rows, toggle("lockSet", L("spu_guide_flow_lockSet"), draft.lockSet))
local inRun = chosenSet(draft.vehicleIds)
local entries = {}
local other, blocked = 0, 0
local blockedTexts = {}
for _, vehicle in ipairs(guide.context.vehicles or {}) do
local state = "free"
if inRun[vehicle.id] then
state = "in"
elseif vehicle.otherRunId ~= nil and vehicle.otherRunId ~= draft.id then
state = "other"
other = other + 1
elseif vehicle.blocked then
state = "blocked"
blocked = blocked + 1
table.insert(blockedTexts, tostring(vehicle.text))
end
table.insert(entries, {id = vehicle.id, label = tostring(vehicle.text), state = state, ownWagon = vehicle.ownWagon})
end
local solo = soloIds(guide.context)
for _, entry in ipairs(ADFlowView.vehicleRows(entries, false)) do
table.insert(rows, check("vehicleIds", entry.id, entry.label, entry.state == "in"))
local own = nil
for _, e in ipairs(entries) do if e.id == entry.id then own = e.ownWagon end end
if entry.state == "in" and (solo[entry.id] or own ~= nil) then
local wagonRow = rowWagon(guide, entry.id, own)
if wagonRow ~= nil then table.insert(rows, wagonRow) end
-- kun forsyningsflows skifter vogn mellem ture
if not isOut(guide) and wagonRow ~= nil and wagonRow.type == "choice" then
local spareRow = rowSpare(guide, entry.id, own)
if spareRow ~= nil then table.insert(rows, spareRow) end
end
end
end
if #rows == 2 then table.insert(rows, text("spu_guide_flow_noTractors")) end
if other > 0 then table.insert(rows, text("spu_guide_flow_otherFlows", {other})) end
if blocked > 0 then
-- foldet sammen som standard; linjen med antallet folder navn + grund ud/ind
local key = guide.showBlocked and "spu_guide_flow_blockedHide" or "spu_guide_flow_blockedShow"
table.insert(rows, {type = "link", id = "showBlocked", value = true, label = L(key, {blocked})})
if guide.showBlocked then
table.sort(blockedTexts)
for _, line in ipairs(blockedTexts) do table.insert(rows, {type = "text", label = T(" " .. line)}) end
end
end
return rows
end
local function rowsSummary(guide)
local rows = {}
for _, line in ipairs(ADFlowGuide.summary(guide)) do
table.insert(rows, {type = "link", id = "jump", value = line.step, line = line})
end
return rows
end
local ROWS = {
name = rowsName, direction = rowsDirection, buildings = rowsBuildings, goods = rowsGoods, store = rowsStore, feed = rowsFeed,
rules = rowsRules, wait = rowsWait, tractors = rowsTractors, summary = rowsSummary,
}
function ADFlowGuide.rows(guide)
local builder = ROWS[guide.step]
return builder ~= nil and builder(guide) or {}
end
-- ---- resumé ----
-- Korte navne til resuméet (field, ellers navn, ellers rækkens tekst).
local function namesFor(ids, entries, field)
local byId = {}
for _, entry in ipairs(entries or {}) do byId[entry.id or entry.name] = entry[field] or entry.name or entry.text end
local names = {}
for _, id in ipairs(ids or {}) do table.insert(names, tostring(byId[id] or id)) end
return names
end
local SUMMARY = {
name = function(guide) return {T(guide.draft.name or "")} end,
direction = function(guide) return {L(isOut(guide) and "spu_direction_out" or "spu_direction_in")} end,
buildings = function(guide)
local names = namesFor(buildingIds(guide.draft), guide.context.buildings, "name")
if #names == 0 then return {L("spu_guide_flow_none")} end
return {T(ADFlowView.joinCounted(names, " + ", ""))}
end,
goods = function(guide)
local draft = guide.draft
if isOut(guide) then
local parts = {}
local titles = {}
for _, product in ipairs(guide.context.products or {}) do titles[product.name] = product.title end
for _, product in ipairs(draft.products or {}) do
table.insert(parts, {key = "spu_policy_" .. product.policy, prefix = tostring(titles[product.fillType] or product.fillType) .. ": "})
end
if draft.sellUnused then table.insert(parts, L("spu_guide_flow_sumSellUnused")) end
if #parts == 0 then return {L("spu_guide_flow_none")} end
return parts
end
if draft.ingredientMode ~= "manual" then return {L("spu_guide_flow_goodsAuto")} end
if #draft.ingredients == 0 then return {L("spu_guide_flow_none")} end
if #draft.ingredients > ADFlowGuide.MAX_NAMED_GOODS then return {L("spu_guide_flow_sumGoods", {#draft.ingredients})} end
local byName = {}
for _, fillType in ipairs(guide.context.fillTypes or {}) do byName[fillType.name] = fillType.title end
local names = {}
for _, name in ipairs(draft.ingredients) do table.insert(names, tostring(byName[name] or name)) end
return {T(table.concat(names, " + "))}
end,
store = function(guide)
local draft = guide.draft
if draft.storeAuto or #draft.storeBuildingIds == 0 then return {L("spu_guide_sum_auto")} end
if #draft.storeBuildingIds > 2 then return {L("spu_guide_sum_stores", {#draft.storeBuildingIds})} end
return {T(table.concat(namesFor(draft.storeBuildingIds, guide.context.storeBuildings, "name"), " + "))}
end,
feed = function(guide)
local ids = guide.draft.feedBuildingIds or {}
if #ids == 0 then return {L("spu_guide_flow_none")} end
if #ids > 2 then return {L("spu_guide_flow_sumFeed", {#ids})} end
return {T(table.concat(namesFor(ids, guide.context.feedBuildings, "name"), " + "))}
end,
rules = function(guide)
local draft = guide.draft
if isOut(guide) then
local sells, stores = ADFlowView.policyUse(productPolicies(draft))
local parts = {}
if sells then table.insert(parts, draft.sellAtPercent == 0 and L("spu_guide_flow_sumSellAlways") or L("spu_guide_flow_sumSellAt", {draft.sellAtPercent})) end
if stores then table.insert(parts, L("spu_guide_flow_sumStoreAbove", {draft.storeAbovePercent})) end
if sells and (draft.sellAbovePercent or 0) > 0 then table.insert(parts, L("spu_guide_flow_sumSellAbove", {draft.sellAbovePercent})) end
if sells and (draft.minSourcePercent or 0) > 0 then table.insert(parts, L("spu_guide_flow_sumMinSource", {draft.minSourcePercent})) end
if draft.collectRound then table.insert(parts, L("spu_guide_flow_sumRound")) end
return parts
end
local loops = (draft.loops or 0) > 0 and L("spu_guide_flow_sumLoops", {draft.loops}) or L("spu_guide_flow_sumLoopsInf")
return {loops, L("spu_guide_flow_sumReturn", {draft.returnBelowPercent})}
end,
wait = function(guide)
local group = guide.draft.waitPoolGroup or ""
return {group == "" and L("spu_waitPool_all") or T(group)}
end,
tractors = function(guide)
if guide.draft.borrowTractor then
local wagonId = ADFlowGuide.borrowWagon(guide.draft, guide.context)
if wagonId == nil then return {L("spu_guide_flow_sumNoTractors")} end
for _, wagon in ipairs(guide.context.wagons or {}) do
if wagon.id == wagonId then return {L("spu_guide_flow_sumBorrow", {tostring(wagon.label)})} end
end
for _, wagon in ipairs(ADFlowGuide.ownParkableWagons(guide.draft, guide.context)) do
if wagon.id == wagonId then return {L("spu_guide_flow_sumBorrow", {wagon.park})} end
end
return {L("spu_guide_flow_sumBorrowInUse")}
end
local ids = guide.draft.vehicleIds
if #ids == 0 then return {L("spu_guide_flow_sumNoTractors")} end
if #ids > ADFlowGuide.MAX_NAMED_TRACTORS then return {L("spu_guide_flow_sumTractors", {#ids})} end
-- traktorer der ikke findes (solgt, eller ikke indlæst endnu) vises ikke som rå id
local known = {}
for _, vehicle in ipairs(guide.context.vehicles or {}) do known[vehicle.id] = true end
local present = {}
for _, id in ipairs(ids) do if known[id] then table.insert(present, id) end end
if #present == 0 then return {L("spu_guide_flow_sumNoTractors")} end
local parts = {T(table.concat(namesFor(present, guide.context.vehicles, "label"), ", "))}
if guide.draft.lockSet then table.insert(parts, L("spu_guide_flow_sumLocked")) end
return parts
end,
}
-- Resumé-linjer {step, title, parts}: Navn (altid) + de synlige trin.
function ADFlowGuide.summary(guide)
local lines = {}
for _, step in ipairs(ADFlowGuide.ORDER) do
local build = SUMMARY[step]
if build ~= nil and (step == "name" or ADFlowGuide.isVisible(step, guide)) then
table.insert(lines, {step = step, title = L(ADFlowGuide.stepKey(step, guide)), parts = build(guide)})
end
end
return lines
end
-- ---- gem ----
function ADFlowGuide.result(guide)
local draft = guide.draft
local storeIds = ADGuide.deepCopy(draft.storeBuildingIds)
if ADFlowGuide.isVisible("store", guide) and draft.storeAuto then storeIds = {} end
local changes = {
kind = draft.kind, sourceBuildingIds = ADGuide.deepCopy(draft.sourceBuildingIds),
targetBuildingIds = ADGuide.deepCopy(draft.targetBuildingIds), storeBuildingIds = storeIds,
products = ADGuide.deepCopy(draft.products), ingredientMode = draft.ingredientMode,
ingredients = ADGuide.deepCopy(draft.ingredients), sellAtPercent = draft.sellAtPercent,
storeAbovePercent = draft.storeAbovePercent, sellAbovePercent = draft.sellAbovePercent or 0,
minSourcePercent = draft.minSourcePercent or 0, sellUnused = draft.sellUnused == true,
collectRound = draft.collectRound, loops = draft.loops,
returnBelowPercent = draft.returnBelowPercent, waitPoolGroup = draft.waitPoolGroup or "",
feedBuildingIds = draft.kind == "outbound" and ADGuide.deepCopy(draft.feedBuildingIds or {}) or {},
lockSet = draft.lockSet == true and not draft.borrowTractor, -- en lånt traktor kan ikke låses
}
if draft.name ~= nil and draft.name:match("%S") then changes.name = draft.name end
-- lånt traktor: ingen faste traktorer (de slippes ved gem), kun vognen huskes; uden ledig vogn = faste traktorer
local borrowWagonId = draft.borrowTractor and ADFlowGuide.borrowWagon(draft, guide.context) or nil
changes.borrowWagonId = borrowWagonId or false
if borrowWagonId ~= nil then
-- spillerens eget flow nu: kæden må ikke regne det som sit tomme flow (slette/lægge sammen/bemande — review 19/10)
changes.chain = ""
changes.lockSet = false -- en lånt traktor kan ikke låses til flowet
return {isNew = draft.isNew == true, runId = draft.id, name = draft.name, kind = draft.kind, changes = changes,
vehicleIds = {}, wagons = {}, spareWagons = {}, borrow = true}
end
return {isNew = draft.isNew == true, runId = draft.id, name = draft.name, kind = draft.kind, changes = changes,
vehicleIds = ADGuide.deepCopy(draft.vehicleIds), wagons = ADFlowGuide.resultWagons(draft),
spareWagons = ADFlowGuide.resultSpares(draft)}
end
-- Vogne for de valgte traktorer (til run.wagons ved gem).
function ADFlowGuide.resultWagons(draft)
local chosen, wagons = chosenSet(draft.vehicleIds), {}
for tractorId, wagonId in pairs(draft.wagons or {}) do
if chosen[tractorId] then wagons[tractorId] = wagonId end
end
return wagons
end
-- Ekstra vogne for de valgte traktorer (til run.spareWagons ved gem); tomme lister udelades.
function ADFlowGuide.resultSpares(draft)
local chosen, spares = chosenSet(draft.vehicleIds), {}
for tractorId, list in pairs(draft.spareWagons or {}) do
if chosen[tractorId] and #list > 0 then spares[tractorId] = ADGuide.deepCopy(list) end
end
return spares
end
-- Traktorer der skal tildeles/fjernes efter gem.
function ADFlowGuide.vehicleChanges(oldIds, newIds)
local old, new = chosenSet(oldIds), chosenSet(newIds)
local added, removed = {}, {}
for _, id in ipairs(newIds or {}) do if not old[id] then table.insert(added, id) end end
for _, id in ipairs(oldIds or {}) do if not new[id] then table.insert(removed, id) end end
return added, removed
end