Repository navigation
Expand file tree
/
Copy pathfile.lua
More file actions
255 lines (240 loc) · 8.47 KB
/
Copy pathfile.lua
File metadata and controls
255 lines (240 loc) · 8.47 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
-- all functions dealing with reading/writing files
--
require( "geo" )
inputFields = {}
--- convert a field from the CP representation to a format we are
-- more comfortable with, for example turn it into x,y from x,-z
function fromCpField( fileName, field )
result = { boundary = {}, name=fileName }
for i in ipairs( field ) do
-- z axis is actually y and is from north to south
-- so need to invert it to get a useful direction
result.boundary[ i ] = { x=field[ i ].cx, y=-field[ i ].cz }
end
return result
end
function loadFieldFromPickle( fileName )
local f = io.input( fileName .. ".pickle" )
local unpickled = unpickle( io.read( "*all" ))
local field = fromCpField( fileName, unpickled.boundary )
io.close( f )
--local reversed = reverse( unpickled.boundary )
--local new = { name=fileName, boundary=reversed }
--io.output( fileName .. "_reversed.pickle" )
--io.write( pickle( new ))
f = io.open( fileName .. "_vehicle.pickle" )
if f then
field.vehicle = unpickle( f:read( "*all" ))
io.close( f )
end
return field
end
--
-- read the log.txt to get field polygons. I changed generateCourse.lua
-- so it writes the coordinates to log.txt when a course is generated for a field.
--
function loadFieldsFromLogFile( fileName, fieldName )
local i = 1
for line in io.lines(fileName ) do
local match = string.match( line, '%[dbg7 %w+%] generateCourse%(%) called for "Field (%w+)"' )
if match then
-- start of a new field data
field = match
print("Reading field " .. field )
i = 1
inputFields[ field ] = { boundary = {}, name=field }
end
x, y, z = string.match( line, "%[dbg7 %w+%] ([%d%.-]+) ([%d%.-]+) ([%d%.-]+)" )
if x then
inputFields[ field ].boundary[ i ] = { cx=tonumber(x), cz=tonumber(z) }
i = i + 1
end
end
for key, field in pairs( inputFields ) do
io.output( field.name .. ".pickle" )
io.write( pickle( field ))
fields[ key ] = { boundary = {}, name=field.name }
for i in ipairs( field.boundary ) do
-- z axis is actually y and is from north to south
-- so need to invert it to get a useful direction
fields[ key ].boundary[ i ] = { x=field.boundary[ i ].cx, y=-field.boundary[ i ].cz }
end
end
end
--- 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
calculatePolygonData( field.boundary )
return field
end
--- Convert our angle representation (measured from the x axis up in radians)
-- into CP's, where 0 is to the south, to our negative y axis.
--
function toCpAngle( angle )
local a = math.deg( angle ) + 90
if a > 180 then
a = a - 360
end
return a
end
--- Write field.course to a CP saved course file
--
function writeCourseToFile( field, fileName )
local f = io.output( fileName )
local clockwise
if field.isClockwise then clockwise = "true"
else clockwise = "false" end
io.write(
string.format( '<course workWidth="%.6f" numHeadlandLanes="%d" headlandDirectionCW="%s">\n',
field.width, field.nHeadlandPasses, clockwise ))
local wp = 1
for i, point in ipairs( field.course ) do
local lane = ""
if point.passNumber then
lane = string.format( 'lane="%d"', -point.passNumber)
end
local turn = ""
if point.turnStart then
turn = 'turnstart="1"'
end
if point.turnEnd then
turn = 'turnend="1"'
end
local crossing = ""
if i == 1 then
crossing = 'crossing="1" wait="1"'
end
if i == #field.course then
crossing = 'crossing="1" wait="1"'
end
io.write(
string.format( ' <waypoint%d angle="%.2f" generated="true" speed="0" pos="%.2f %.2f" %s %s %s/>\n',
wp, toCpAngle( point.nextEdge.angle ), point.x, -point.y, turn, lane, crossing ))
wp = wp + 1
end
io.write( " </course>" )
io.close( f )
end
function parseCourseData( line )
local fileName = string.match( line, 'slot.+fileName="([%w%.]+)"' )
local id = string.match( line, 'slot.+id="(%d+)"' )
local name = string.match( line, 'slot.+name="(.+)"' )
local parent = string.match( line, 'slot.+parent="(%d+)"' )
local sequence = string.match( line, 'courseStorage(%d+).xml' )
return fileName, id, name, sequence, parent
end
--
--- Read the savegame carreer file to find out the map name
-- because that's where courseplay stores the courses.
-- saved in that directory
function getMapId( f )
for line in f:lines() do
local mapId = string.match( line, '<mapId>(.+)</mapId>')
if mapId then
print( mapId )
return mapId
end
end
end
--- Read the CP saved fields
function loadSavedFields( fileName )
local savedFields = {}
local f = io.input( fileName )
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 = ix + 1
savedFields[ ix ] = { number=fieldNum, boundary={}}
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
end
return savedFields
end
--- Save a field loaded from a CP saved field into a CP course
function writeSavedFieldToCourseFile( field, courseFilename )
calculatePolygonData( field.boundary )
-- fake a course from the field boundary
field.course = field.boundary
-- some reasonable defaults
field.width = 3
field.nHeadlandPasses = 1
field.isClockwise = field.boundary.isClockwise
for i, point in ipairs( field.course ) do
-- this will set lane = -1 so we'll interpret it as the outermost headland pass
point.passNumber = 1
end
writeCourseToFile( field, courseFilename )
end
--- Read the CP course manager files to find out which courses are
-- saved in that directory
function getSavedCourses( f )
local savedCourses = {}
local maxId = -1
local maxSequence = -1
for line in f:lines() do
local fileName, id, name, sequence, parent = parseCourseData( line )
if id then
id = tonumber( id )
sequence = tonumber( sequence )
savedCourses[ sequence ] = { fileName=fileName, id=id, name=name, sequence=sequence, parent=parent }
if ( id > maxId ) then maxId = id end
if ( id > maxSequence ) then maxSequence = sequence end
end
end
return savedCourses, maxId + 1, maxSequence + 1
end
--- Copy oldCourse to newCourse in dir, and also update the manager file
function copyCourse( dir, oldCourse, newCourse, managerFilename )
-- first copy the course file
print( string.format( "Copying %s to %s", oldCourse.fileName, newCourse.fileName ))
local from = io.open( dir .. "/" .. oldCourse.fileName, "r" )
local to = io.open( dir .. "/" .. newCourse.fileName, "w" )
for line in from:lines() do
to:write( line .. "\n" )
end
from:close()
to:close()
end
function addCourseToManagerFile( dir, managerFilename, newCourse)
-- now update the manager file
local managerFileContents = ""
local man = io.open( dir .. "/" .. managerFilename, "r" )
for line in man:lines() do
managerFileContents = managerFileContents .. line .. "\n"
local fileName, id, name, sequence = parseCourseData( line )
-- append new course data after the original courses
if sequence and tonumber( sequence ) == ( newCourse.sequence - 1 ) then
managerFileContents = managerFileContents .. string.format(
' <slot fileName="%s" id="%d" parent="%d" isUsed="true" name="%s"/>\n',
newCourse.fileName, newCourse.id, newCourse.parent, newCourse.name )
end
end
man:close()
local man = io.open( dir .. "/" .. managerFilename, "w" )
man:write( managerFileContents )
man:close()
end