-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfile.lua
More file actions
53 lines (50 loc) · 1.83 KB
/
Copy pathfile.lua
File metadata and controls
53 lines (50 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- all functions dealing with reading/writing files
--
--
--- Reconstruct a field from a Courseplay saved course.
-- As the saved course does not contain the field data we use the
-- first headland track as the field boundary
--
function loadFieldFromSavedCourse( fileName )
local f = io.input( fileName )
local field = {}
field.name = fileName
field.boundary = {}
for line in io.lines( fileName ) do
local width, nHeadlandPasses, isClockwise = string.match( line, '<course workWidth="([%d%.-]+)" numHeadlandLanes="(%d+)" headlandDirectionCW="(%w+)"' )
if width then
field.width = tonumber( width )
field.nHeadlandPasses = tonumber( nHeadlandPasses )
field.isClockwise = isClockwise
end
local num, cx, cz, lane = string.match( line, '<waypoint(%d+).+pos="([%d%.-]+) ([%d%.-]+)" +lane="([%d-]+)"')
-- lane -1 is the outermost headland track
if lane == "-1" then
table.insert( field.boundary, { x=tonumber( cx ), y=-tonumber( cz )})
end
end
field.boundary:calculateData()
return field
end
--- Read the CP saved fields
function loadSavedFields( fileName )
local savedFields = {}
local ix = 0
for line in io.lines( fileName ) do
local fieldNum = string.match( line, '<field fieldNum="([%d%.-]+)"' )
if fieldNum then
-- a new field started
ix = tonumber( fieldNum )
savedFields[ ix ] = { number=fieldNum, boundary={}, islandNodes={}}
end
local num, cx, cz = string.match( line, '<point(%d+).+pos="([%d%.-]+) [%d%.-]+ ([%d%.-]+)"' )
if num then
table.insert( savedFields[ ix ].boundary, { x=tonumber( cx ), y=-tonumber( cz )})
end
num, cx, cz = string.match( line, '<islandNode(%d+).+pos="([%d%.-]+) +([%d%.-]+)"' )
if num then
table.insert( savedFields[ ix ].islandNodes, { x=tonumber( cx ), y=-tonumber( cz )})
end
end
return savedFields
end