Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jan 5, 2021. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppf.lua
More file actions
Latest commit
359 lines (309 loc) · 11.1 KB
/
Copy pathcppf.lua
File metadata and controls
359 lines (309 loc) · 11.1 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
--[[
@title: Courseplay Pathfinding
@description: ---
@author: Jakob Tischler
@date: 28 Oct 2013
@version: 0.1
]]
cppf= {}
cppf.modDir=g_currentModDirectory;
cppf.modName="Courseplay Pathfinding";
cppf.author="Jakob Tischler";
cppf.version=0.1;
cppf.debug=true;
functioncppf:loadMap(name)
print(string.format("## %s v%.1f by %s loaded", cppf.modName, cppf.version, cppf.author));
if (self.initialized) then
return;
end;
--self:loadFilesInDir(cppf.modDir .. "pathfindingClasses");
source(cppf.modDir.."pathfindingLibrary.lua");
self.pathPointsVis= { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z" }
-- self.testCourse = "f9 umrand";
self.testCourse="f17";
--self.route = { from = { x = 114, z = 42 }, to = { x = 71, z = 56 } }; --f9 umrand
-- self.route = { from = { x = 60, z = 56 }, to = { x = 71, z = 1 } }; --f9 umrand
--self.route = { from = { x = 10, z = 1 }, to = { x = 34, z = 62 } }; --w1 umrand
self.route= { from= { x=2, z=2 }, to= { x=14, z=31 } }; -- f17
self.tileSize=5;
self.walkable=0;
self.unwalkable=1;
self.initialized=true;
end;
functioncppf:loadFilesInDir(dir)
--self:debug("loadFilesInDir(" .. tostring(dir) .. ")");
localfiles=Files:new(dir);
fork,fileinipairs(files.files) do
iffile.isDirectorythen
self:loadFilesInDir(dir.."/" ..file.filename);
else
localpath=dir.."/" ..file.filename;
iffileExists(path) then
--self:debug("\tsource(" .. path .. ")");
source(path);
else
--self:debug("\tError: file \"" .. path .. "\" could not be loaded!");
end;
end;
end;
end;
functioncppf:deleteMap()
self.initialized=false;
end;
functioncppf:keyEvent(unicode, sym, modifier, isDown)
end;
functioncppf:update(dt)
ifInputBinding.hasEvent(InputBinding.CPPF_HANDLEMARKERCOURSE) then
localcourse=self:findCourseplayCourse(self.testCourse);
ifcourse==nilthen
self:debug("CPPF: course \"" ..self.testCourse.."\" not found");
return;
end;
self.map,self.mapCoords=self:createGridMapFromCourse(course);
ifself.map==nilor#self.map==0then
self:debug("CPPF: map for \"" ..self.testCourse.."\" could not be created");
return;
end;
localgrid=cppf.Grid:new(self.map);
localmyFinder=cppf.Pathfinder:new(grid, 'JPS', 0);
-- myFinder.allowDiagonal = false;
-- Calculates the path, and its length
localpath=myFinder:getPath(self.route.from.x, self.route.from.z, self.route.to.x, self.route.to.z)
ifpaththen
self.displayPathNodes= {};
print(('Path found! Length: %.2f'):format(path:getLength()))
fornode, countinpath:nodes() do
--print(('Step: %d - x: %d - y: %d'):format(count, node:getX(), node:getY()))
print(('Step: %d - x,y=%d,%d'):format(count, node.x, node.y))
--self.map[node.y][node.x] = count;
self.map[node.y][node.x] =self.pathPointsVis[count];
localp= {};
p.x=self.mapCoords[node.y][node.x].x;
p.z=self.mapCoords[node.y][node.x].z;
p.y=getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, p.x, 300, p.z) +3;
self.displayPathNodes[count] =p;
end
self:debug(self:printMap(self.map));
end
end;
ifself.displayPathNodes~=nilthen
--self:debug(self:tableShow(self.displayPathNodes, "self.displayPathNodes"));
self:drawDebugPoints();
end;
ifInputBinding.hasEvent(InputBinding.CPPF_GETTILEFROMCOORDS) andself.map~=nilandself.mapData~=nilthen
localtestX, testZ=0, -1100;
localtileX, tileZ=self:getTileFromCoords(testX, testZ);
localtileCoords=self.mapCoords[tileZ][tileX];
self:debug(string.format("tile at coords %d,%d is: tileX=%s,tileZ=%s, tileCoords=%s,%s", testX, testZ, tostring(tileX), tostring(tileZ), tostring(tileCoords.x), tostring(tileCoords.z)));
end;
end;
functioncppf:updateTick(dt)
end;
functioncppf:draw()
end;
functioncppf:mouseEvent(posX, posY, isDown, isUp, button)
end;
functioncppf:getTileFromCoords(x, z)
localtileX=Utils.clamp(math.ceil((x-self.mapData.minX) /self.tileSize), 1, self.mapData.numXtilesNeeded);
localtileZ=Utils.clamp(math.ceil((z-self.mapData.minZ) /self.tileSize), 1, self.mapData.numZtilesNeeded);
returntileX, tileZ;
end;
functioncppf:drawDebugPoints()
fori,nodeinpairs(self.displayPathNodes) do
ifi==1then
drawDebugPoint(node.x, node.y, node.z, 0,1,0,1);
elseifi==#self.displayPathNodesthen
drawDebugPoint(node.x, node.y, node.z, 1,0,0,1);
else
drawDebugPoint(node.x, node.y, node.z, 1,1,0,1);
end;
ifi<#self.displayPathNodesthen
localnextNode=self.displayPathNodes[i+1];
drawDebugLine(node.x,node.y,node.z, 0,0,1, nextNode.x,nextNode.y,nextNode.z, 0,0,1);
end;
end;
end;
functioncppf:createGridMapFromCourse(course)
self.mapData= {};
self.mapData.xValues, self.mapData.zValues= {}, {};
self.mapData.minX, self.mapData.maxX, self.mapData.minZ, self.mapData.maxZ=999999, -999999, 999999, -999999;
fori,wpinpairs(course.waypoints) do
ifwp.cx<self.mapData.minXthenself.mapData.minX=wp.cx; end;
ifwp.cx>self.mapData.maxXthenself.mapData.maxX=wp.cx; end;
ifwp.cz<self.mapData.minZthenself.mapData.minZ=wp.cz; end;
ifwp.cz>self.mapData.maxZthenself.mapData.maxZ=wp.cz; end;
table.insert(self.mapData.xValues, wp.cx);
table.insert(self.mapData.zValues, wp.cz);
end;
self.mapData.width, self.mapData.height=self.mapData.maxX-self.mapData.minX, self.mapData.maxZ-self.mapData.minZ;
self:debug(string.format("minX,maxX,width=%s,%s,%s / minZ,maxZ,height=%s,%s,%s", tostring(self.mapData.minX),tostring(self.mapData.maxX),tostring(self.mapData.width),tostring(self.mapData.minZ),tostring(self.mapData.maxZ),tostring(self.mapData.height)));
self.mapData.numXtilesNeeded=math.ceil(self.mapData.width/self.tileSize);
self.mapData.numZtilesNeeded=math.ceil(self.mapData.height/self.tileSize);
self:debug(string.format("numXtilesNeeded, numZtilesNeeded = %s, %s", tostring(self.mapData.numXtilesNeeded),tostring(self.mapData.numZtilesNeeded)));
localmap= {};
localmapCoords= {};
forline=1,self.mapData.numZtilesNeededdo
localz=self.mapData.minZ-self.tileSize/2+ (line*self.tileSize);
ifline==self.mapData.numZtilesNeededthen
localprevZ= (self.mapData.minZ-self.tileSize/2+ ((line-1)*self.tileSize));
z=prevZ+ (self.mapData.maxZ-prevZ)/2;
end;
map[line] = {};
mapCoords[line] = {};
forcol=1,self.mapData.numXtilesNeededdo
localx=self.mapData.minX-self.tileSize/2+ (col*self.tileSize);
ifcol==self.mapData.numXtilesNeededthen
localprevX= (self.mapData.minX-self.tileSize/2+ ((col-1)*self.tileSize));
x=prevX+ (self.mapData.maxX-prevX)/2;
end;
localpoint= { x=x, z=z };
mapCoords[line][col] =point;
--WALKABLE vs. UNWALKABLE
map[line][col] =self.walkable;
localisInPoly=cppf:pointInPolygon_v2(course.waypoints, self.mapData.xValues, self.mapData.zValues, x, z);
-- local hasFruit = courseplay:area_has_fruit(x, z, FruitUtil.fruitTypes["wheat"].index, self.tileSize/2, self.tileSize/2); --TODO: current fruit --> e.g. combine.grainTankFillType --> FruitUtil.fillTypeToFruitType[fillType]
localhasFruit=courseplay:area_has_fruit(x, z, nil, self.tileSize/2, self.tileSize/2);
-- if hasFruit then
-- print('has fruit')
-- end
ifnotisInPolyorhasFruitthen
map[line][col] =self.unwalkable;
end;
end;
end;
--self:debug(self:tableShow(map, self.testCourse .. ": map"));
--self:debug(self:tableShow(mapCoords, self.testCourse .. ": mapCoords"));
--self:debug(self:printMap(map));
returnmap, mapCoords;
end;
functioncppf:printMap(map)
localstr="RESULTMAP = {\r";
forz=1,#mapdo
str=str.."\t{";
forx=1,#map[z] do
ifmap[z][x] ==self.walkablethen
str=str.."";
elseifmap[z][x] ==self.unwalkablethen
str=str.."x";
else
str=str..map[z][x];
end;
ifx<#map[z] then
str=str..",";
end;
end;
str=str.."},\r";
end;
str=str.."};";
returnstr;
end;
functioncppf:findCourseplayCourse(name)
ifg_currentMission.cp_courses~=nilthen
fork,courseinpairs(g_currentMission.cp_courses) do
ifcourse.name:lower() ==name:lower() then
returncourse;
end;
end;
end;
returnnil;
end;
functioncppf:pointInPolygon_v2(polygon, xValues, zValues, x, z) --@src: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
--nvert: Number of vertices in the polygon. Whether to repeat the first vertex at the end.
--vertx, verty: Arrays containing the x- and y-coordinates of the polygon's vertices.
--testx, testy: X- and y-coordinate of the test point.
localnvert=#polygon;
localvertx, verty=xValues, zValues;
localtestx, testy=x, z;
locali, j;
localc=false;
fori=1, nvertdo
ifi==1then
j=nvert;
else
j=i-1;
end;
if ((verty[i]>testy) ~= (verty[j]>testy)) and (testx< (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) +vertx[i]) then
c=notc;
end;
end;
returnc;
end;
functioncppf:rgba(r, g, b, a)
return { r/255, g/255, b/255, a };
end;
functioncppf:debug(str)
ifcppf.debugthen
print(str);
end;
end;
functioncppf:tableShow(t, name, indent)
localcart-- a container
localautoref-- for self references
--[[ counts the number of elements in a table
local function tablecount(t)
local n = 0
for _, _ in pairs(t) do n = n+1 end
return n
end
]]
-- (RiciLake) returns true if the table is empty
localfunctionisemptytable(t) returnnext(t) ==nilend
localfunctionbasicSerialize(o)
localso=tostring(o)
iftype(o) =="function" then
localinfo=debug.getinfo(o, "S")
-- info.name is nil because o is not a calling level
ifinfo.what=="C" then
returnstring.format("%q", so..", C function")
else
-- the information is defined through lines
returnstring.format("%q", so..", defined in (" ..
info.linedefined.."-" ..info.lastlinedefined..
")" ..info.source)
end
elseiftype(o) =="number" then
returnso
else
returnstring.format("%q", so)
end
end
localfunctionaddtocart(value, name, indent, saved, field)
indent=indentor""
saved=savedor {}
field=fieldorname
cart=cart..indent..field
iftype(value) ~="table" then
cart=cart.." = " ..basicSerialize(value) ..";\n"
else
ifsaved[value] then
cart=cart.." = {}; -- " ..saved[value]
.." (self reference)\n"
autoref=autoref..name.." = " ..saved[value] ..";\n"
else
saved[value] =name
--if tablecount(value) == 0 then
ifisemptytable(value) then
cart=cart.." = {};\n"
else
cart=cart.." = {\n"
fork, vinpairs(value) do
k=basicSerialize(k)
localfname=string.format("%s[%s]", name, k)
field=string.format("[%s]", k)
-- three spaces between levels
addtocart(v, fname, indent.."\t", saved, field)
end
cart=cart..indent.."};\n"
end
end
end
end
name=nameor"__unnamed__"
iftype(t) ~="table" then
returnname.." = " ..basicSerialize(t)
end
cart, autoref="", ""
addtocart(t, name, indent)
returncart..autoref
end;
addModEventListener(cppf);