Skip to content

Commit

Permalink
fix: small island save
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pvaiko committed Sep 28, 2024
1 parent 5f36b35 commit e0308ae
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions scripts/Course.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e0308ae

Please sign in to comment.