feat(mp): etape 1 - automatik kun på serveren, fælles gård via ADUtil.farmId, klienter skriver aldrig modens filer

This commit is contained in:
2026-10-25 20:48:18 +01:00
parent b4004616eb
commit 1f639af77a
34 changed files with 125 additions and 54 deletions
+52
View File
@@ -7,6 +7,58 @@ local function logInfo(...) return (ADLog or Logging).info(...) end
ADUtil = {}
-- Gården ADSmartPickup kører for (multiplayer etape 1; én fælles gård, brugerens valg 25/10). Med lokal spiller
-- (singleplayer/vært) spillerens gård. På en dedikeret server er der ingen spiller (tilskuer-gården): så den
-- indstillede gård (serverFarmId), ellers gården med flest AutoDrive-køretøjer, ellers 1.
-- Server/klient (multiplayer): singleplayer og værten er server; en MP-klient er klient uden server.
function ADUtil.isServer() return g_server ~= nil end
function ADUtil.isMpClient() return g_client ~= nil and g_server == nil end
ADUtil.FARM_CACHE_MS = 60000
local farmCache = {id = nil, atMs = -math.huge}
function ADUtil.resetFarmCache() farmCache = {id = nil, atMs = -math.huge} end
function ADUtil.configuredFarmId()
local value = ADSettings ~= nil and ADSettings.get ~= nil and tonumber(ADSettings.get("serverFarmId")) or 0
return math.floor(value)
end
local function spectatorFarmId()
return FarmManager ~= nil and FarmManager.SPECTATOR_FARM_ID or 0
end
local function dominantFarmId()
local counts, best, bestCount = {}, nil, 0
local system = g_currentMission ~= nil and g_currentMission.vehicleSystem or nil
for _, vehicle in ipairs(system ~= nil and system.vehicles or {}) do
if vehicle.ad ~= nil and vehicle.getOwnerFarmId ~= nil then
local farm = vehicle:getOwnerFarmId()
if farm ~= nil and farm ~= spectatorFarmId() then
counts[farm] = (counts[farm] or 0) + 1
if counts[farm] > bestCount or (counts[farm] == bestCount and farm < best) then best, bestCount = farm, counts[farm] end
end
end
end
return best
end
function ADUtil.farmId()
local mission = g_currentMission
if mission ~= nil and mission.getFarmId ~= nil then
local own = mission:getFarmId()
if own ~= nil and own ~= spectatorFarmId() then return own end
end
local configured = ADUtil.configuredFarmId()
if configured ~= nil and configured > 0 then return configured end
if mission == nil then return 1 end
local nowMs = ADUtil.now()
if farmCache.id == nil or nowMs - farmCache.atMs > ADUtil.FARM_CACHE_MS or nowMs < farmCache.atMs then
farmCache = {id = dominantFarmId() or 1, atMs = nowMs}
end
return farmCache.id
end
-- Spiltid i ms (g_currentMission.time); uden spil (tests) processorens tid.
function ADUtil.now()
if g_currentMission ~= nil and type(g_currentMission.time) == "number" then return g_currentMission.time end