From e0308ae0044bf05e744b69c66cf2aed867b193de Mon Sep 17 00:00:00 2001 From: Peter Vaiko Date: Sat, 28 Sep 2024 18:41:37 -0400 Subject: [PATCH] fix: small island save When compaction is enabled we only save the start and end of a row, except when it isn't straight, for instance because it is bypassing an island. For those rows, we now save all waypoints. --- scripts/Course.lua | 47 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/scripts/Course.lua b/scripts/Course.lua index 59806f20c..63d3665bb 100644 --- a/scripts/Course.lua +++ b/scripts/Course.lua @@ -1473,14 +1473,51 @@ function Course:getProgress(ix) return self.waypoints[ix].dToHere / self.length, ix, ix == #self.waypoints end +--- When compaction is enabled we only save the start and end of a row, except when it isn't straight, for instance +--- because it is bypassing an island. For those rows, we save all waypoints. +--- Baseline edge courses (with rows following a curved field edge) have the compaction disabled anyway. +---@return number, number index of next unsaved waypoint, index of next waypoint entry in the XML file +local function saveRowToXml(waypoints, xmlFile, key, compact, rowStartIx, xmlIx) + local row = {} + local i, straight, rowAngle = rowStartIx, true, waypoints[rowStartIx].angle + while i <= #waypoints and not waypoints[i]:isRowEnd() do + table.insert(row, waypoints[i]) + if math.abs(waypoints[i].angle - rowAngle) > 0.01 then + straight = false + end + i = i + 1 + end + -- i points to the row end, add it to the row + local rowEndIx = i + table.insert(row, waypoints[i]) + if straight and compact then + -- only save the row start and end + waypoints[rowStartIx]:setXmlValue(xmlFile, key, xmlIx) + waypoints[rowEndIx]:setXmlValue(xmlFile, key, xmlIx + 1) + return rowEndIx + 1, xmlIx + 2 + else + i = xmlIx + -- save all waypoints + for _, wp in ipairs(row) do + wp:setXmlValue(xmlFile, key, i) + i = i + 1 + end + return rowEndIx + 1, i + end +end + ---@param compact boolean skip waypoints between row start and end (as for straight rows, these can be regenerated --- easily after the course is loaded) local function saveWaypointsToXml(waypoints, xmlFile, key, compact) - local i = 1 - for _, wp in ipairs(waypoints) do - if not compact or (not wp:getRowNumber() or wp:isRowStart() or wp:isRowEnd()) then - wp:setXmlValue(xmlFile, key, i) - i = i + 1 + local wpIx, xmlIx = 1, 1 + while wpIx <= #waypoints do + local wp = waypoints[wpIx] + if wp:isRowStart() then + wpIx, xmlIx = saveRowToXml(waypoints, xmlFile, key, compact, wpIx, xmlIx) + else + wp:setXmlValue(xmlFile, key, xmlIx) + wpIx = wpIx + 1 + xmlIx = xmlIx + 1 end end end