feat: indkøbsrådgiver i statusvinduet (1.62.203)

This commit is contained in:
2026-10-10 18:05:43 +02:00
parent fefdf2a85e
commit e7da81df18
8 changed files with 114 additions and 3 deletions
+84
View File
@@ -0,0 +1,84 @@
-- AD Shop Advice
-- Indkøbsrådgiver (brugerønske 10/10): mangler gården et redskab til en rolle, foreslås den billigste butiksvare der
-- kan rollen, sammen med hvor mange hektar der venter. Ren logik øverst (testes med luajit), spil-adapter nederst.
ADShopAdvice = {}
-- rolle -> butikkens kategorier (StoreManager sammenligner med store bogstaver; navne fra spillets data/vehicles)
ADShopAdvice.CATEGORIES = {
plow = {"PLOWS"},
cultivator = {"CULTIVATORS", "DISCHARROWS", "SUBSOILERS", "POWERHARROWS", "SPADERS"},
seeder = {"SEEDERS", "PLANTERS"},
roller = {"ROLLERS"},
weeder = {"WEEDERS"},
fertilizer = {"FERTILIZERSPREADERS", "SPRAYERS"},
limer = {"FERTILIZERSPREADERS"},
mulcher = {"MULCHERS"},
mower = {"MOWERS"},
tedder = {"TEDDERS"},
rake = {"WINDROWERS"},
baler = {"BALERSROUND", "BALERSSQUARE", "BALERS"},
collector = {"LOADERWAGONS"},
baleCollector = {"BALELOADERS"},
unloader = {"TRAILERS", "AUGERWAGONS"},
harvester = {"HARVESTERS"},
stonePicker = {"STONEPICKERS"},
}
-- items = {{name, price, categoryNames}} -> den billigste (pris > 0) i rollens kategorier, eller nil
function ADShopAdvice.cheapest(items, role)
local wanted = {}
for _, name in ipairs(ADShopAdvice.CATEGORIES[role] or {}) do wanted[name] = true end
if next(wanted) == nil then return nil end
local best = nil
for _, item in ipairs(items or {}) do
local matches = false
for _, category in ipairs(item.categoryNames or {}) do
if wanted[string.upper(category)] then matches = true break end
end
if matches and (item.price or 0) > 0 and (best == nil or item.price < best.price) then best = item end
end
return best
end
function ADShopAdvice.sumHectares(areas)
local total = 0
for index = 1, table.maxn(areas or {}) do total = total + (areas[index] or 0) end
return total
end
-- ---- spil-adapter ----
local cheapestByRole = {}
-- Butikkens køretøjer (kun dem der kan købes): {name, price, categoryNames}. Læses én gang pr. rolle.
local function storeItems()
local items = {}
if g_storeManager == nil then return items end
for _, storeItem in ipairs(g_storeManager:getItems() or {}) do
local isVehicle = StoreSpecies == nil or storeItem.species == StoreSpecies.VEHICLE
if isVehicle and storeItem.showInStore and not storeItem.isBundleItem and g_storeManager:getIsItemUnlocked(storeItem) then
local brand = g_brandManager ~= nil and g_brandManager:getBrandByIndex(storeItem.brandIndex) or nil
local name = tostring(storeItem.name)
if brand ~= nil and brand.title ~= nil and brand.title ~= "" then name = tostring(brand.title) .. " " .. name end
table.insert(items, {name = name, price = storeItem.price or 0, categoryNames = storeItem.categoryNames})
end
end
return items
end
-- {name, price} eller nil — gemt pr. rolle (butikken ændrer sig ikke undervejs)
function ADShopAdvice.cheapestFor(role)
if cheapestByRole[role] == nil then
cheapestByRole[role] = ADShopAdvice.cheapest(storeItems(), role) or false
end
return cheapestByRole[role] or nil
end
function ADShopAdvice.formatPrice(price)
if g_i18n ~= nil and g_i18n.formatMoney ~= nil then
local ok, text = pcall(g_i18n.formatMoney, g_i18n, price, 0, true, true)
if ok and text ~= nil then return text end
end
return string.format("%d", math.floor(price or 0))
end
+20 -1
View File
@@ -236,7 +236,13 @@ function ADStatusHud.missingEntries(proxy)
for _, group in ipairs(ADStatusView.groupMissing(found)) do
local roleName = proxy.roleText ~= nil and guarded("roleText", proxy.roleText, proxy, group.role) or tostring(group.role)
local ok, text = pcall(string.format, textOf("spu_status_missing_" .. group.kind), roleName, ADStatusView.fieldList(group.fieldIds))
table.insert(entries, {text = ok and text or textOf("spu_status_missing_" .. group.kind), level = "error", target = FIELDS_TAB})
text = ok and text or textOf("spu_status_missing_" .. group.kind)
-- indkøbsrådgiver: hvor meget der venter, og den billigste maskine der kan opgaven (kun når rollen helt mangler)
if group.kind == "noRig" and ADShopAdvice ~= nil then
local hint = guarded("buyHint", ADStatusHud.buyHint, group.role, group.fieldIds)
if hint ~= nil then text = text .. hint end
end
table.insert(entries, {text = text, level = "error", target = FIELDS_TAB})
end
return entries
end
@@ -295,6 +301,19 @@ function ADStatusHud.stuckEntries()
return entries
end
-- " — 42 ha venter; billigste: Lemken Juwel 8 (38.000 kr)" til en manglende rolle, eller nil.
function ADStatusHud.buyHint(role, fieldIds)
local areas = {}
for index, fieldId in ipairs(fieldIds or {}) do
local info = ADFields.getInfo(fieldId)
areas[index] = info ~= nil and ADFields.polygonAreaHa(info.verts) or 0
end
local item = ADShopAdvice.cheapestFor(role)
if item == nil then return nil end
return string.format(textOf("spu_status_buyHint"), string.format("%.1f", ADShopAdvice.sumHectares(areas)), item.name,
ADShopAdvice.formatPrice(item.price))
end
-- Maskiner uden parkering der er færdige på en mark: "kør X væk fra F38" (gul) til de har forladt marken.
function ADStatusHud.pickupEntries()
local entries = {}
+1
View File
@@ -547,6 +547,7 @@
<text name="spu_set_playerPopup" text="Besked når du skal køre en maskine"/>
<text name="spu_set_playerPopup_on" text="Pop-up"/>
<text name="spu_set_playerPopup_off" text="Kun statusvinduet"/>
<text name="spu_status_buyHint" text=" — %s ha venter; billigste: %s (%s)"/>
<text name="spu_status_pickup" text="Kør '%s' væk fra F%s"/>
<text name="spu_status_parkHint" text="Giv '%s' en parkering — du har kørt den selv %d gange"/>
<text name="spu_set_stableSiloRange" text="Stald-silo: afstand fra stald til egen silo"/>
+1
View File
@@ -547,6 +547,7 @@
<text name="spu_set_playerPopup" text="Message when you must drive a machine"/>
<text name="spu_set_playerPopup_on" text="Pop-up"/>
<text name="spu_set_playerPopup_off" text="Status window only"/>
<text name="spu_status_buyHint" text=" — %s ha waiting; cheapest: %s (%s)"/>
<text name="spu_status_pickup" text="Drive '%s' away from F%s"/>
<text name="spu_status_parkHint" text="Give '%s' a parking spot — you have driven it yourself %d times"/>
<text name="spu_set_stableSiloRange" text="Barn silo: distance from barn to its own silo"/>
+2 -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.202.0</version>
<version>1.62.203.0</version>
<title>
<en>AD Smart Pickup</en>
</title>
@@ -74,6 +74,7 @@
<sourceFile filename="adChain.lua"/>
<sourceFile filename="adChainRun.lua"/>
<sourceFile filename="adChainView.lua"/>
<sourceFile filename="adShopAdvice.lua"/>
<sourceFile filename="adStatusView.lua"/>
<sourceFile filename="adStatusHud.lua"/>
<sourceFile filename="adSmartPickup.lua"/>