fix(måned): visne-overgange læses fra afgrødens XML — høst der lukker vises (v1.62.261)

This commit is contained in:
2026-10-16 13:36:46 +02:00
parent 4618925521
commit 3581021b71
4 changed files with 75 additions and 3 deletions
+41 -1
View File
@@ -29,11 +29,51 @@ local function findsTransition(data, fromState, toState, depth)
return false
end
-- Periodenavne i afgrødens <growth><seasonal> (FS25: periode 1 = EARLY_SPRING)
ADMonthView.PERIOD_NAMES = {EARLY_SPRING = 1, MID_SPRING = 2, LATE_SPRING = 3, EARLY_SUMMER = 4, MID_SUMMER = 5,
LATE_SUMMER = 6, EARLY_AUTUMN = 7, MID_AUTUMN = 8, LATE_AUTUMN = 9, EARLY_WINTER = 10, MID_WINTER = 11,
LATE_WINTER = 12}
-- Stadienavn/-tal fra XML -> growthState (nameToGrowthState har store bogstaver). nil = ukendt.
local function stateIndex(fruit, value)
if value == nil then return nil end
local number = tonumber(value)
if number ~= nil then return number end
local names = fruit.nameToGrowthState
return type(names) == "table" and names[string.upper(tostring(value))] or nil
end
-- Overgange pr. periode læst fra afgrødens XML: {[periode] = {{from, to}, ...}}.
-- readPeriods(xmlFilename) -> liste af {name = "LATE_AUTUMN", updates = {{startState, endState}, ...}} eller nil.
function ADMonthView.xmlTransitions(fruit, readPeriods)
if fruit == nil or readPeriods == nil or fruit.xmlFilename == nil then return nil end
local list = readPeriods(fruit.xmlFilename)
if type(list) ~= "table" then return nil end
local result = {}
for _, period in ipairs(list) do
local index = ADMonthView.PERIOD_NAMES[string.upper(tostring(period.name or ""))]
if index ~= nil then
local transitions = result[index] or {} -- samme periode to gange: overgangene lægges sammen
for _, update in ipairs(period.updates or {}) do
local from, to = stateIndex(fruit, update.startState), stateIndex(fruit, update.endState)
if from ~= nil and to ~= nil then table.insert(transitions, {from = from, to = to}) end
end
result[index] = transitions
end
end
return result
end
-- Visner afgrøden (stadie state) ved overgangen til periode nextPeriod?
function ADMonthView.withersIn(fruit, nextPeriod, state)
-- transitions = ADMonthView.xmlTransitions(...) (valgfri): growthDataSeasonal gemmer ikke overgangene i en
-- form vi kan finde (in-game 16/10: bomuld 35 % høstet i oktober viste "ok", visner i LATE_AUTUMN).
function ADMonthView.withersIn(fruit, nextPeriod, state, transitions)
if fruit == nil or type(state) ~= "number" or state <= 0 then return false end
local withered = fruit.witheredState
if type(withered) ~= "number" or withered <= 0 then return false end
for _, transition in ipairs(transitions ~= nil and transitions[nextPeriod] or {}) do
if transition.from == state and transition.to == withered then return true end
end
local periods = fruit.growthDataSeasonal ~= nil and fruit.growthDataSeasonal.periods or nil
local data = periods ~= nil and periods[nextPeriod] or nil
return findsTransition(data, state, withered, 0)
+32 -1
View File
@@ -41,6 +41,35 @@ function SmartPickupFrame.monthCpProgress(fieldId)
return count > 0 and sum / count or nil
end
-- <growth><seasonal><period name><update startState endState> fra afgrødens XML (én gang pr. fil)
SmartPickupFrame.seasonalCache = {}
function SmartPickupFrame.readSeasonalPeriods(xmlFilename)
local cached = SmartPickupFrame.seasonalCache[xmlFilename]
if cached ~= nil then return cached or nil end
local periods = false
SmartPickupFrame.seasonalCache[xmlFilename] = false -- fejler indlæsningen, prøves den ikke igen
local xmlFile = XMLFile.loadIfExists("adspSeasonal", xmlFilename)
if xmlFile ~= nil then
periods = {}
xmlFile:iterate("foliageType.growth.seasonal.period", function(_, key)
local updates = {}
xmlFile:iterate(key .. ".update", function(_, updateKey)
table.insert(updates, {startState = xmlFile:getString(updateKey .. "#startState"),
endState = xmlFile:getString(updateKey .. "#endState")})
end)
table.insert(periods, {name = xmlFile:getString(key .. "#name"), updates = updates})
end)
xmlFile:delete()
end
local updates = 0
for _, entry in ipairs(periods or {}) do updates = updates + #entry.updates end
Logging.info(SmartPickupFrame.LOG_PREFIX .. "månedsfane: vækstdata '%s': %d perioder, %d overgange",
tostring(xmlFilename), periods and #periods or -1, updates)
SmartPickupFrame.seasonalCache[xmlFilename] = periods
return periods or nil
end
function SmartPickupFrame.monthContext(period)
return {
period = period,
@@ -50,7 +79,9 @@ function SmartPickupFrame.monthContext(period)
end,
withers = function(info, nextPeriod)
local fruit = info.fruitIndex ~= nil and g_fruitTypeManager:getFruitTypeByIndex(info.fruitIndex) or nil
local ok, result = pcall(ADMonthView.withersIn, fruit, nextPeriod, info.growthState)
local okXml, transitions = pcall(ADMonthView.xmlTransitions, fruit, SmartPickupFrame.readSeasonalPeriods)
local ok, result = pcall(ADMonthView.withersIn, fruit, nextPeriod, info.growthState,
okXml and transitions or nil)
return ok and result == true
end,
cpProgress = SmartPickupFrame.monthCpProgress,
+1 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<modDesc descVersion="107">
<author>masterdraco</author>
<version>1.62.260.0</version>
<version>1.62.261.0</version>
<title>
<en>AD Smart Pickup</en>
</title>