-- Smart Pickup — fanen Marker -- Oversigt: tabel over alle gårdens marker (Mark · Afgrøde · Plan · Kørsel · Status · Maskiner); klik på en -- række viser markens resumé (én linje pr. emne — klik på en linje for at rette netop det trin), hele -- statusteksten og markøren. [Opsæt mark] åbner guiden (gui/SmartPickupFieldGuide.lua + SmartPickupGuide.lua), -- [Markflåde] viser den fælles markflåde. Al logik ligger i ADFieldJobs/ADFieldModel/ADFieldView/ADFieldGuide. -- Funktionerne lægges på SmartPickupFrame (indlæses efter SmartPickupFrame.lua). -- Markflådens rækker (vehiclePrefab/ingredientPrefab) routes til onClickFieldRow. SmartPickupFrame.FIELD_ROW_KINDS = {fleetVehicle = true, fleetMode = true} SmartPickupFrame.ROLE_KEYS = { mower = "spu_field_role_mower", tedder = "spu_field_role_tedder", rake = "spu_field_role_rake", collector = "spu_field_role_collector", harvester = "spu_field_role_harvester", unloader = "spu_field_role_unloader", mulcher = "spu_field_role_mulcher", limer = "spu_field_role_limer", plow = "spu_field_role_plow", cultivator = "spu_field_role_cultivator", seeder = "spu_field_role_seeder", roller = "spu_field_role_roller", weeder = "spu_field_role_weeder", fertilizer = "spu_field_role_fertilizer", baler = "spu_field_role_baler", baleCollector = "spu_field_role_baleCollector", } -- ---- tekster ---- function SmartPickupFrame:roleText(role) return self.i18n:getText(SmartPickupFrame.ROLE_KEYS[role] or "spu_field_role_mower") end function SmartPickupFrame:fieldReasonText(reason) if type(reason) ~= "table" then return nil end if reason.text ~= nil then return reason.text end local first = reason.name or (reason.role ~= nil and self:roleText(reason.role)) or "" local template = self.i18n:getText(reason.key) local args = reason.args or {} local ok, text = pcall(string.format, template, first, args[1] or 0, args[2] or 0) return ok and text or template end function SmartPickupFrame:soilText(info) local texts = {} for _, part in ipairs(ADFieldView.soilParts(info)) do table.insert(texts, self.i18n:getText("spu_field_need_" .. part)) end return table.concat(texts, ", ") end -- Kort årsag til tabellens statuskolonne ("bredde ukendt" i stedet for hele forklaringen, som står i -- markens detaljer). Fritekst-årsager (fx "… er tom for X") forkortes til "se detaljer". function SmartPickupFrame:fieldReasonShort(reason) if type(reason) ~= "table" then return nil end if reason.text ~= nil then return self.i18n:getText("spu_field_reasonShort_text") end local shortKey = reason.key ~= nil and string.gsub(reason.key, "^spu_field_reason_", "spu_field_reasonShort_") or nil if shortKey ~= nil then local okHas, has = pcall(function() return self.i18n.hasText == nil or self.i18n:hasText(shortKey) end) if okHas and has then return self.i18n:getText(shortKey) end end return self:fieldReasonText(reason) end -- short = tabellens korte udgave (trin + kort årsag); ellers hele teksten (markens detaljer). function SmartPickupFrame:fieldStatusText(field, info, short) local text = self.i18n:getText(ADFieldView.statusKey(field, info)) if info.markerId == nil then return string.format(text, info.id) end if short then if (field.stage == "idle" or field.stage == "ready") and info.kind ~= "bare" and info.readyRatio ~= nil then text = string.format("%s %d %%", text, math.floor((info.readyRatio or 0) * 100 + 0.5)) end local reason = self:fieldReasonShort(field.reason) return reason ~= nil and (text .. ": " .. reason) or text end if (field.stage == "idle" or field.stage == "ready") and info.kind ~= "bare" and info.readyRatio ~= nil then text = string.format("%s %d %%", text, math.floor((info.readyRatio or 0) * 100 + 0.5)) end local decision = ADFieldJobs.lastDecision[info.id] if decision ~= nil and decision.care ~= nil and not ADFieldModel.isWorking(field) then text = text .. " — " .. string.format(self.i18n:getText("spu_field_careNeed"), self.i18n:getText("spu_field_stage_" .. decision.care)) end local target = field.plan.targetFruit if target ~= nil and target ~= "" and info.kind == "bare" and field.stage == "idle" and not ADFieldJobs.environment().plantable(target) then text = text .. " — " .. string.format(self.i18n:getText("spu_field_waitSowing"), self:fruitTitle(target)) end local reason = self:fieldReasonText(field.reason) if reason ~= nil then text = text .. ": " .. reason end return text end function SmartPickupFrame:fruitTitle(name) local fruit = g_fruitTypeManager ~= nil and name ~= nil and name ~= "" and g_fruitTypeManager:getFruitTypeByName(name) or nil return fruit ~= nil and fruit.fillType ~= nil and tostring(fruit.fillType.title) or tostring(name) end function SmartPickupFrame:fieldPlanText(field, info) if field.plan.targetFruit ~= nil and field.plan.targetFruit ~= "" then return "-> " .. self:fruitTitle(field.plan.targetFruit) end local text = self.i18n:getText(ADFieldView.planKey(field.plan, info.kind)) if field.plan.rake and info.kind == "mowable" then text = text .. " + " .. self.i18n:getText("spu_field_plan_rake") end if field.plan.sow and info.kind ~= "mowable" then text = text .. " + " .. self.i18n:getText("spu_field_plan_sow") end if (field.plan.rigs or 1) > 1 then text = string.format("%s (x%d)", text, field.plan.rigs) end return text end function SmartPickupFrame:fieldCells(row) local info, field = row.info, row.field local names = {} for _, assignment in ipairs(ADFieldJobs.assignmentsFor(info.id)) do table.insert(names, ADRuns.vehicleLabel(assignment.vehicle)) end local name = string.format("%s%sF%d", info.id == self.selectedFieldId and "> " or "", info.markerId == nil and "! " or "", info.id) local crop = ADFieldView.cropText(info, function(key) return self.i18n:getText(key) end) return { name, crop, self:fieldPlanText(field, info), self.i18n:getText(field.plan.mode == "auto" and "spu_field_mode_auto" or "spu_field_mode_manual"), self:fieldStatusText(field, info, true), ADFlowView.joinCounted(names, ", ", "-"), } end -- ---- opbygning ---- function SmartPickupFrame:fillFieldRows(adEnv) self.fieldRows = {} if self:hasGuideOn(SmartPickupFrame.TAB_FIELDS) and self:fillGuideRows(adEnv) then return end if self.fieldView == "fleet" then self:fillFleetRows(adEnv) return end local i18n = self.i18n local infos = ADFields.list() if #infos == 0 then self:addTextRow(i18n:getText("spu_field_scanning")) else self:addTextRow(i18n:getText("spu_field_hint")) self:addCellRow(self.fieldPrefab, {i18n:getText("spu_col_field"), i18n:getText("spu_col_crop"), i18n:getText("spu_col_plan"), i18n:getText("spu_col_mode"), i18n:getText("spu_col_status"), i18n:getText("spu_col_tractors")}) for _, row in ipairs(ADFieldView.rows(infos, ADFieldJobs.state.fields)) do local cells = self:addCellRow(self.fieldPrefab, self:fieldCells(row)) local fieldId = row.info.id if cells[1] ~= nil and cells[1].parent ~= nil then self:makeRowClickable(cells[1].parent, function(frame) frame.selectedFieldId = fieldId frame:requestRebuild() end) end table.insert(self.fieldRows, {id = fieldId, cells = cells}) end end local selected = self.selectedFieldId ~= nil and ADFields.getInfo(self.selectedFieldId) or nil if selected ~= nil then self:fillFieldOverview(adEnv, selected) end end -- Den valgte marks resumé: én klikbar linje pr. emne ([Ret] åbner kun det trin), hele status, markør og -- advarsel om marksilo der ikke tømmes hjem. function SmartPickupFrame:fillFieldOverview(adEnv, info) local i18n = self.i18n local field = ADFieldJobs.getField(info.id) local crop = ADFieldView.cropText(info, function(key) return i18n:getText(key) end) self:addTextRow(string.format(i18n:getText("spu_field_section"), info.id, crop)) self:addTextRow(" " .. i18n:getText("spu_guide_sum_hint")) local context = self:fieldGuideContext(adEnv, info, field.plan, field.plan.storeBuildingIds) local guide = ADFieldGuide.open(field, context) local getText = function(key) return i18n:getText(key) end local fieldId = info.id for _, line in ipairs(ADFieldGuide.summary(guide)) do local step = line.step self:addLinkRow(" " .. ADGuide.lineText(line, getText) .. i18n:getText("spu_guide_editSuffix"), function(frame) frame:openFieldStep(fieldId, step) end) end self:addTextRow(" " .. string.format(i18n:getText("spu_field_statusFull"), self:fieldStatusText(field, info))) if info.markerId ~= nil then self:addTextRow(string.format(i18n:getText("spu_field_marker"), tostring(info.markerName), info.markerDistance or 0)) else self:addTextRow(" " .. string.format(i18n:getText("spu_guide_noMarker"), info.id)) end self:addFieldSiloWarnings(context.fieldSilos) end -- Marksiloer skal tømmes hjem af et udkørselsflow. function SmartPickupFrame:addFieldSiloWarnings(fieldSilos) for _, silo in ipairs(fieldSilos or {}) do local emptied = false for _, run in ipairs(ADRunsController.store.runs) do for _, sourceId in ipairs(run.kind == "outbound" and run.sourceBuildingIds or {}) do if sourceId == silo.id then emptied = true end end end if not emptied then self:addTextRow(" " .. string.format(self.i18n:getText("spu_field_fieldSiloNotEmptied"), tostring(silo.name or silo.text))) end end end -- Ekstra oplysning pr. sæt i markflåden (afgrøder, frø, strigle/sprøjte, bredde over 50 m, mark). function SmartPickupFrame:rigDetails(rig) local i18n = self.i18n local parts = {} if rig.role == "harvester" then local count = 0 for _ in pairs(rig.fruits or {}) do count = count + 1 end table.insert(parts, string.format(i18n:getText("spu_field_rig_crops"), count)) elseif rig.role == "seeder" then table.insert(parts, i18n:getText(rig.hasSeeds and "spu_field_rig_seeds" or "spu_field_rig_noSeeds")) elseif rig.role == "weeder" then table.insert(parts, i18n:getText(rig.mechanical and "spu_field_rig_mechanical" or "spu_field_rig_herbicide")) end -- samme grænse som udsendelsen (50,0 m med afrunding er ikke "over 50 m") if (rig.width or 0) > ADFieldModel.CP_MAX_WIDTH + ADFieldModel.WIDTH_TOLERANCE then table.insert(parts, i18n:getText("spu_field_tooWideShort")) end if (rig.width or 0) < ADFieldModel.MIN_WORK_WIDTH and not ADFieldModel.NO_WIDTH_ROLES[rig.role] then table.insert(parts, i18n:getText("spu_field_widthUnknownShort")) end -- fast på en mark (valgt under markens "Faste maskiner") og hvor den arbejder nu for fieldId, field in pairs(ADFieldJobs.state.fields) do for _, id in ipairs(field.plan ~= nil and field.plan.rigIds or {}) do if id == rig.id then table.insert(parts, string.format(i18n:getText("spu_field_dedicatedElsewhere"), tostring(fieldId))) end end end local assignment = ADFieldJobs.state.assignments[rig.id] if assignment ~= nil then table.insert(parts, string.format(i18n:getText("spu_field_rig_working"), tostring(assignment.fieldId))) end return #parts > 0 and (" — " .. table.concat(parts, " — ")) or "" end -- Markflåden (egen visning): alle maskiner med flueben, "fast på F22", "i gang på F108", bredde-advarsler. function SmartPickupFrame:fillFleetRows(adEnv) local i18n = self.i18n self:addTextRow(i18n:getText("spu_field_section_fleet")) local useAll = ADFieldJobs.state.fleetMode ~= "ticked" self:addToggleRow("fleetMode", i18n:getText("spu_field_fleetAll"), {i18n:getText("ui_no"), i18n:getText("ui_yes")}, useAll) local rigs = {} for _, rig in ipairs(ADFieldJobs.getRigs(adEnv)) do table.insert(rigs, rig) end table.sort(rigs, function(a, b) if a.role ~= b.role then return a.role < b.role end return ADFlowView.naturalLess(a.label, b.label) end) for _, rig in ipairs(rigs) do local row = self:addRow(self.vehiclePrefab, {kind = "fleetVehicle", vehicleId = rig.id}) local title = string.format(" %s — %s, %.1f m%s", tostring(rig.label), self:roleText(rig.role), rig.width or 0, self:rigDetails(rig)) row:getDescendantByName("title"):setText(title) row:getDescendantByName("option"):setState(rig.enabled and SmartPickupFrame.STATE_ON or SmartPickupFrame.STATE_OFF, false) end if #rigs == 0 then self:addTextRow(" " .. i18n:getText("spu_field_noRigs")) end end -- ---- live ---- function SmartPickupFrame:refreshFieldRows(adEnv) if self:hasGuideOn(SmartPickupFrame.TAB_FIELDS) or self.fieldView == "fleet" then self:setStatusText(self.statusOverride or "") return end local rowsById = {} for _, row in ipairs(ADFieldView.rows(ADFields.list(), ADFieldJobs.state.fields)) do rowsById[row.info.id] = row end local working, total = 0, 0 for _, entry in ipairs(self.fieldRows or {}) do local row = rowsById[entry.id] if row ~= nil then SmartPickupFrame.setCells(entry.cells, self:fieldCells(row)) total = total + 1 if ADFieldModel.isWorking(row.field) then working = working + 1 end end end self:setStatusText(self.statusOverride or string.format(self.i18n:getText("spu_field_status"), working, total)) local selected = self.selectedFieldId ~= nil and ADFieldJobs.getField(self.selectedFieldId) or nil local info = self.selectedFieldId ~= nil and ADFields.getInfo(self.selectedFieldId) or nil local isWorking = selected ~= nil and ADFieldModel.isWorking(selected) self.fieldStartButtonInfo.text = self.i18n:getText(isWorking and "spu_field_button_stop" or "spu_field_button_start") self.fieldStartButtonInfo.disabled = selected == nil or info == nil or (not isWorking and info.markerId == nil) end -- ---- klik (markflåden) ---- function SmartPickupFrame:onClickFieldRow(state, data) self:safe(function(frame) local isOn = state == SmartPickupFrame.STATE_ON if data.kind == "fleetVehicle" then ADFieldJobs.toggleFleet(data.vehicleId) elseif data.kind == "fleetMode" then ADFieldJobs.setFleetMode(isOn and "all" or "ticked") end frame:requestRebuild() end) end -- Start nu: pleje hvis marken trænger (og vokser), ellers hele kæden (høst/slå, eller klargøring på bar mark). function SmartPickupFrame:onClickFieldStartStop() self:safe(function(frame) local id = frame.selectedFieldId if id == nil then return end local field = ADFieldJobs.getField(id) if ADFieldModel.isWorking(field) then ADFieldJobs.stopField(frame.getAdEnv(), id) else local decision = ADFieldJobs.lastDecision[id] if decision == nil or decision.care == nil then ADFieldJobs.forceReady(id) end ADFieldJobs.requestStart(id) frame.statusOverride = string.format(frame.i18n:getText("spu_field_startRequested"), id) frame.statusOverrideTicks = SmartPickupFrame.STATUS_OVERRIDE_TICKS end frame:requestRebuild() end) end