Files
fs25-adsmartpickup/FS25_ADSmartPickup/adLivestock.lua
T

167 lines
8.8 KiB
Lua

-- AD Livestock
-- Slagtedyr (brugerønske 19/10 16:01: "kan vi få systemet til at hente køer når de er fuldt udvokset og klar til
-- slagteriet"). Fase 1: konsolkommandoen spuAnimals viser, hvad moden kan se — stalde (dyr, alder, pris, hvornår
-- prisen topper), dyrehandlere, kreaturvogne og AD-markørerne ved læsseområderne — så spillets dyre-API kan bekræftes
-- in-game, før transporten bygges. Ren logik øverst (testes med luajit), spil-adapter nederst.
ADLivestock = {}
ADLivestock.LOG_PREFIX = "ADSmartPickup: dyr: "
ADLivestock.MARKER_M = 40 -- AD-markør så tæt på en dyre-læssetrigger hører til den
ADLivestock.MAX_AGE_SCAN = 240 -- måneder: så langt frem søges efter salgsprisens top
ADLivestock.READY_SHARE = 0.99 -- "klar": prisen er mindst så stor en del af toppen
-- ---- ren logik ----
-- Alderen (måneder), hvor prisen første gang når READY_SHARE af toppen. priceAt(age) -> pris eller nil. -> alder, toppris
function ADLivestock.readyAge(priceAt, maxAge)
local best = nil
for age = 0, maxAge or ADLivestock.MAX_AGE_SCAN do
local price = priceAt(age)
if type(price) == "number" and (best == nil or price > best) then best = price end
end
if best == nil or best <= 0 then return nil, nil end
for age = 0, maxAge or ADLivestock.MAX_AGE_SCAN do
local price = priceAt(age)
if type(price) == "number" and price >= best * ADLivestock.READY_SHARE then return age, best end
end
return nil, best
end
-- ---- spil-adapter ----
local function call(object, method, ...)
if object == nil or object[method] == nil then return nil end
local ok, result = pcall(object[method], object, ...)
return ok and result or nil
end
local function placeableName(placeable)
return tostring(call(placeable, "getName") or "?")
end
local function nodePos(node)
if node == nil or getWorldTranslation == nil then return nil end
local ok, x, _, z = pcall(getWorldTranslation, node)
if not ok then return nil end
return x, z
end
-- Nærmeste AD-markør (ikke P/Connect/Wait/service) til et punkt: "navn (12 m)" eller "ingen inden for 40 m"
local function nearestMarker(adEnv, x, z)
if adEnv == nil or x == nil then return "ingen position" end
local best, bestD = nil, ADLivestock.MARKER_M
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
local candidate = ADBuildings == nil or ADBuildings.isCandidateMarker == nil or ADBuildings.isCandidateMarker(marker)
local wp = candidate and adEnv.ADGraphManager:getWayPointById(marker.id) or nil
if wp ~= nil then
local d = math.sqrt((wp.x - x) ^ 2 + (wp.z - z) ^ 2)
if d < bestD then best, bestD = marker, d end
end
end
if best == nil then return string.format("ingen inden for %d m", ADLivestock.MARKER_M) end
return string.format("'%s' (%.0f m)", tostring(best.name), bestD)
end
local function subTypeOf(subTypeIndex)
local system = g_currentMission ~= nil and g_currentMission.animalSystem or nil
return system ~= nil and call(system, "getSubTypeByIndex", subTypeIndex) or nil
end
local function typeName(typeIndex)
local system = g_currentMission ~= nil and g_currentMission.animalSystem or nil
local animalType = system ~= nil and call(system, "getTypeByIndex", typeIndex) or nil
return tostring(animalType ~= nil and (animalType.name or animalType.title) or typeIndex)
end
-- Pris pr. dyr ved en alder fra undertypens salgskurve (spillets felt hedder sellPrice; andre navne prøves også)
local function curvePrice(subType, age)
if subType == nil then return nil end
for _, key in ipairs({"sellPrice", "sellPriceCurve"}) do
local curve = subType[key]
if type(curve) == "table" and curve.get ~= nil then
local ok, price = pcall(curve.get, curve, age)
if ok and type(price) == "number" then return price end
end
end
return nil
end
local function clusterLine(cluster)
local subTypeIndex = call(cluster, "getSubTypeIndex") or cluster.subTypeIndex
local subType = subTypeOf(subTypeIndex)
local age = call(cluster, "getAge") or cluster.age
local health = call(cluster, "getHealth") or cluster.health
local sellPrice = call(cluster, "getSellPrice")
local curve = curvePrice(subType, age or 0)
local readyAge, topPrice = ADLivestock.readyAge(function(a) return curvePrice(subType, a) end)
return string.format("%s: %s dyr, alder %s mdr, sundhed %s, pris getSellPrice=%s kurve=%s, klar fra %s mdr (top %s)",
tostring(subType ~= nil and (subType.name or subType.title) or subTypeIndex), tostring(cluster.numAnimals),
tostring(age), tostring(health), tostring(sellPrice), tostring(curve), tostring(readyAge), tostring(topPrice))
end
function ADLivestock.survey(adEnv)
local lines = {}
local farmId = g_currentMission:getFarmId()
local placeables = g_currentMission.placeableSystem ~= nil and g_currentMission.placeableSystem.placeables or {}
for _, placeable in ipairs(placeables) do
local okPlaceable, errPlaceable = pcall(function()
local animals = placeable.spec_husbandryAnimals
local owned = call(placeable, "getOwnerFarmId") == farmId
if animals ~= nil and owned then
local trigger = animals.animalLoadingTrigger
local x, z = nodePos(trigger ~= nil and trigger.triggerNode or nil)
table.insert(lines, string.format("STALD %s: %s, %s/%s dyr, læssetrigger %s, markør %s", placeableName(placeable),
typeName(animals.animalTypeIndex), tostring(call(placeable, "getNumOfAnimals")), tostring(call(placeable, "getMaxNumOfAnimals")),
trigger ~= nil and "ja" or "NEJ", nearestMarker(adEnv, x, z)))
for _, cluster in ipairs(call(placeable, "getClusters") or {}) do
local ok, line = pcall(clusterLine, cluster)
table.insert(lines, " " .. (ok and line or ("fejl: " .. tostring(line))))
end
end
local dealerSpec = placeable.spec_animalLoadingTrigger
local dealer = dealerSpec ~= nil and dealerSpec.animalLoadingTrigger or nil
if dealer ~= nil and dealer.isDealer then
local x, z = nodePos(dealer.triggerNode)
local types = {}
for _, typeIndex in ipairs(dealer.animalTypes or {}) do table.insert(types, typeName(typeIndex)) end
table.insert(lines, string.format("HANDLER %s (%s): tager %s, markør %s", placeableName(placeable), tostring(dealer.title),
#types > 0 and table.concat(types, ", ") or "alle", nearestMarker(adEnv, x, z)))
end
end)
-- én underlig bygning må ikke skjule resten (review)
if not okPlaceable then table.insert(lines, string.format("fejl ved %s: %s", placeableName(placeable), tostring(errPlaceable))) end
end
for _, vehicle in pairs(g_currentMission.vehicleSystem.vehicles or {}) do
local spec = vehicle.spec_livestockTrailer
local okVehicle, errVehicle = pcall(function()
if spec ~= nil and call(vehicle, "getOwnerFarmId") == farmId then
local parts = {}
for typeIndex, place in pairs(spec.animalTypeIndexToPlaces or {}) do
table.insert(parts, string.format("%s %d pladser", typeName(typeIndex), #(place.slots or {})))
end
local parkIndex = vehicle.advd ~= nil and vehicle.advd.parkDestination or nil
local park = type(parkIndex) == "number" and parkIndex >= 1 and adEnv.ADGraphManager:getMapMarkerById(parkIndex) or nil
local connect = park ~= nil and ADPool ~= nil and ADPool.connectFor(park.name, adEnv.ADGraphManager:getMapMarkers()) or nil
table.insert(lines, string.format("VOGN %s: %s; i vognen %s dyr; P %s, Connect %s", tostring(call(vehicle, "getName")),
table.concat(parts, ", "), tostring(call(vehicle, "getNumOfAnimals")), tostring(park ~= nil and park.name or "-"),
tostring(connect ~= nil and connect.name or "-")))
end
end)
if not okVehicle then table.insert(lines, string.format("fejl ved vogn %s: %s", tostring(call(vehicle, "getName")), tostring(errVehicle))) end
end
return lines
end
function ADLivestock.consoleSurvey()
local adEnv = FS25_AutoDrive
if adEnv == nil or adEnv.ADGraphManager == nil or g_currentMission == nil then return "dyr: AutoDrive er ikke indlæst" end
local ok, lines = pcall(ADLivestock.survey, adEnv)
if not ok then return "dyr: fejl: " .. tostring(lines) end
for _, line in ipairs(lines) do Logging.info(ADLivestock.LOG_PREFIX .. "%s", line) end
return string.format("dyr: %d linjer (se også log.txt)\n%s", #lines, table.concat(lines, "\n"))
end
if addConsoleCommand ~= nil then
addConsoleCommand("spuAnimals", "Stalde (dyr, alder, pris, klar-alder), dyrehandlere, kreaturvogne og AD-markører ved læsseområderne", "consoleSurvey", ADLivestock)
end