fix(kæde): kæden tager aldrig spillerens traktorer, respekterer slukkede produktioner og handler ikke på delvise data
This commit is contained in:
@@ -8,17 +8,32 @@ ADChainRun = {}
|
||||
local function log(fmt, ...) Logging.info(ADChain.LOG_PREFIX .. fmt, ...) end
|
||||
local function warn(fmt, ...) Logging.warning(ADChain.LOG_PREFIX .. fmt, ...) end
|
||||
|
||||
-- Første puljevogn der kan bære varen + den bedst egnede ledige traktor (maskinpuljens regler: hk og
|
||||
-- kategori via ADPool.bestFit). used = {[id] = true}: maskiner der allerede er brugt i dette tick.
|
||||
function ADChainRun.choosePair(implements, tractors, fillTypeIndex, used)
|
||||
local UNLOADER = "unloader"
|
||||
|
||||
-- Samme rolletjek som ADPool.pairAll("unloader", …) (hasRole er lokal i adPool).
|
||||
local function isUnloader(implement)
|
||||
return implement.role == UNLOADER or (implement.roles ~= nil and implement.roles[UNLOADER] == true)
|
||||
end
|
||||
|
||||
-- Traktor i et flow (også et STOPPET): ADPool.collect regner den ledig til korte lån, men ADRuns.assignVehicle
|
||||
-- ville flytte den permanent ud af spillerens flow — kæden tager den aldrig.
|
||||
local function belongsToFlow(tractor)
|
||||
return tractor.vehicle ~= nil and ADRunsController.getRunForVehicle(tractor.vehicle) ~= nil
|
||||
end
|
||||
|
||||
-- Første puljetømmevogn der kan bære varen + den bedst egnede ledige traktor (maskinpuljens regler: hk og
|
||||
-- kategori via ADPool.bestFit). used = {[id] = true}: brugt i dette tick; blocked = ADPool.blocked(nu).
|
||||
function ADChainRun.choosePair(implements, tractors, fillTypeIndex, used, blocked)
|
||||
if fillTypeIndex == nil then return nil end
|
||||
used = used or {}
|
||||
used, blocked = used or {}, blocked or {}
|
||||
local free = {}
|
||||
for _, tractor in ipairs(tractors or {}) do
|
||||
if not used[tractor.id] and not tractor.offNetwork then table.insert(free, tractor) end
|
||||
if not used[tractor.id] and not blocked[tractor.id] and not tractor.offNetwork and not belongsToFlow(tractor) then
|
||||
table.insert(free, tractor)
|
||||
end
|
||||
end
|
||||
for _, implement in ipairs(implements or {}) do
|
||||
if not used[implement.id] and ADPool.canCarry(implement, fillTypeIndex) then
|
||||
if not used[implement.id] and not blocked[implement.id] and isUnloader(implement) and ADPool.canCarry(implement, fillTypeIndex) then
|
||||
local tractor = ADPool.bestFit(free, implement, "unloader")
|
||||
if tractor ~= nil then return {implement = implement, tractor = tractor} end
|
||||
end
|
||||
@@ -49,7 +64,7 @@ function ADChainRun.switchFields(data, actions)
|
||||
local text = string.format("F%s: %s -> %s (%.1f ha) til %s", tostring(action.fieldId), tostring(action.from),
|
||||
tostring(action.to), action.hectares or 0, tostring(action.reason))
|
||||
log("%s", text)
|
||||
ADFieldJobs.notify(text, false)
|
||||
pcall(ADFieldJobs.notify, text, false)
|
||||
else
|
||||
warn("F%s kunne ikke skiftes: %s", tostring(action.fieldId), tostring(err))
|
||||
end
|
||||
@@ -70,14 +85,23 @@ local function setProduction(data, key, isOn)
|
||||
return ok
|
||||
end
|
||||
|
||||
function ADChainRun.toggleProductions(data, prod)
|
||||
local byChain = ADChain.state.byChain
|
||||
-- Delvise data: intet slukkes (en rod der ikke kunne læses, ligner en rod der er slået fra).
|
||||
function ADChainRun.toggleProductions(data, prod, needRecipes)
|
||||
local state = ADChain.state
|
||||
local byChain = state.byChain
|
||||
local overrides = ADChain.playerOverrides(data.recipes, needRecipes, state.userOff, data.partial == true)
|
||||
state.userOff = overrides.userOff
|
||||
for _, key in ipairs(overrides.added) do
|
||||
byChain[key] = nil
|
||||
log("spilleren slukkede %s — kæden tænder den ikke igen", key)
|
||||
end
|
||||
for _, key in ipairs(prod.enable or {}) do
|
||||
if setProduction(data, key, true) then
|
||||
if not state.userOff[key] and setProduction(data, key, true) then
|
||||
byChain[key] = true
|
||||
log("tænder %s (mellemled i kæden)", key)
|
||||
end
|
||||
end
|
||||
if data.partial then return end
|
||||
for _, key in ipairs(prod.disable or {}) do
|
||||
if setProduction(data, key, false) then
|
||||
byChain[key] = nil
|
||||
@@ -119,7 +143,8 @@ local function createFlow(adEnv, data, edge, pool, used)
|
||||
return
|
||||
end
|
||||
if pool.implements == nil then pool.implements, pool.tractors = collectPool(adEnv) end
|
||||
local pair = ADChainRun.choosePair(pool.implements, pool.tractors, fillIndex, used)
|
||||
if pool.blocked == nil then pool.blocked = ADPool.blocked(ADFieldJobs.now or 0) end
|
||||
local pair = ADChainRun.choosePair(pool.implements, pool.tractors, fillIndex, used, pool.blocked)
|
||||
if pair == nil then
|
||||
createStopped(run, "ingen ledig traktor/vogn til %s", tostring(title))
|
||||
return
|
||||
@@ -159,12 +184,16 @@ function ADChainRun.stopFlows(adEnv, chain, edges)
|
||||
if rootKey ~= nil then
|
||||
ADRunsController.updateRun(run.id, {chain = rootKey})
|
||||
log("flow '%s' hører nu til kæden %s", tostring(run.name), tostring(rootKey))
|
||||
changed = changed + 1
|
||||
else
|
||||
local ok, err = pcall(ADRunsController.stopRun, adEnv, run.id)
|
||||
if ok then log("flow '%s' stoppet: kæden %s er slået fra", tostring(run.name), tostring(run.chain))
|
||||
else warn("flow '%s' kunne ikke stoppes: %s", tostring(run.name), tostring(err)) end
|
||||
if ok then
|
||||
log("flow '%s' stoppet: kæden %s er slået fra", tostring(run.name), tostring(run.chain))
|
||||
changed = changed + 1
|
||||
else
|
||||
warn("flow '%s' kunne ikke stoppes: %s", tostring(run.name), tostring(err))
|
||||
end
|
||||
end
|
||||
changed = changed + 1
|
||||
end
|
||||
return changed
|
||||
end
|
||||
@@ -173,12 +202,16 @@ end
|
||||
function ADChainRun.apply(adEnv, data, result)
|
||||
local okFields, errFields = pcall(ADChainRun.switchFields, data, result.actions)
|
||||
if not okFields then warn("markskift fejlede: %s", tostring(errFields)) end
|
||||
local okProd, errProd = pcall(ADChainRun.toggleProductions, data, result.prod)
|
||||
local okProd, errProd = pcall(ADChainRun.toggleProductions, data, result.prod, result.chain.needRecipes)
|
||||
if not okProd then warn("mellemled fejlede: %s", tostring(errProd)) end
|
||||
local okNew, created = pcall(ADChainRun.createFlows, adEnv, data, result.edges)
|
||||
if not okNew then warn("nye flows fejlede: %s", tostring(created)) end
|
||||
local okStop, stopped = pcall(ADChainRun.stopFlows, adEnv, result.chain, result.edges)
|
||||
if not okStop then warn("stop af flows fejlede: %s", tostring(stopped)) end
|
||||
-- delvise data: en rod der ikke kunne læses, må ikke få sine flows stoppet
|
||||
local okStop, stopped = true, 0
|
||||
if not data.partial then
|
||||
okStop, stopped = pcall(ADChainRun.stopFlows, adEnv, result.chain, result.edges)
|
||||
if not okStop then warn("stop af flows fejlede: %s", tostring(stopped)) end
|
||||
end
|
||||
if (okNew and created > 0) or (okStop and stopped > 0) then
|
||||
local okSave, errSave = pcall(ADRunsController.save)
|
||||
if not okSave then warn("flows kunne ikke gemmes: %s", tostring(errSave)) end
|
||||
|
||||
Reference in New Issue
Block a user