Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on May 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathDevHelper.lua
More file actions
Latest commit
382 lines (329 loc) · 16.8 KB
/
Copy pathDevHelper.lua
File metadata and controls
382 lines (329 loc) · 16.8 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2019 Peter Vaiko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
--- Development helper utilities to easily test and diagnose things.
--- To test the pathfinding:
--- 1. mark the start location/heading with Alt + <
--- 2. mark the goal location/heading with Alt + >
--- 3. watch the path generated ...
--- 4. use Ctrl + > to regenerate the path
---
--- Also showing field/fruit/collision information when walking around
DevHelper=CpObject()
functionDevHelper:init()
self.data= {}
self.isVisualDebugEnabled=false
end
functionDevHelper:debug(...)
print(string.format(...))
end
functionDevHelper:update()
ifnotCpManager.isDeveloperthenreturnend
locallx, lz, hasCollision, vehicle
-- make sure not calling this for something which does not have courseplay installed (only ones with spec_aiVehicle)
ifg_currentMission.controlledVehicleandg_currentMission.controlledVehicle.spec_aiVehiclethen
ifself.vehicle~=g_currentMission.controlledVehiclethen
self.vehicleData=PathfinderUtil.VehicleData(g_currentMission.controlledVehicle, true)
end
self.vehicle=g_currentMission.controlledVehicle
self.node=g_currentMission.controlledVehicle.rootNode
lx, _, lz=localDirectionToWorld(self.node, 0, 0, 1)
self:updateProximitySensors(self.vehicle)
else
-- camera node looks backwards so need to flip everything by 180 degrees
self.node=g_currentMission.player.cameraNode
lx, _, lz=localDirectionToWorld(self.node, 0, 0, -1)
ifnotself.proxySensorthen
self.proxySensor=ProximitySensor(self.node, 180, 10, 1, 0)
else
self.proxySensor:update()
self.proxySensor:showDebugInfo()
end
end
self.yRot=math.atan2( lx, lz )
self.data.yRotDeg=math.deg(self.yRot)
self.data.yRotDeg2=math.deg(MathUtil.getYRotationFromDirection(lx, lz))
self.data.x, self.data.y, self.data.z=getWorldTranslation(self.node)
self.data.fieldNum=courseplay.fields:getFieldNumForPosition(self.data.x, self.data.z)
self.data.hasFruit, self.data.fruitValue, self.data.fruit=PathfinderUtil.hasFruit(self.data.x, self.data.z, 5, 3.6)
self.data.isField, self.fieldArea, self.totalFieldArea=courseplay:isField(self.data.x, self.data.z, 3, 3)
self.data.landId=PathfinderUtil.getFieldIdAtWorldPosition(self.data.x, self.data.z)
self.data.owned=PathfinderUtil.isWorldPositionOwned(self.data.x, self.data.z)
self.data.farmlandId=g_farmlandManager:getFarmlandIdAtWorldPosition(self.data.x, self.data.z)
self.data.farmland=g_farmlandManager:getFarmlandAtWorldPosition(self.data.x, self.data.z)
self.data.fieldAreaPercent=100*self.fieldArea/self.totalFieldArea
localy=getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, self.data.x, self.data.y, self.data.z)
self.data.nx, self.data.ny, self.data.nz=getTerrainNormalAtWorldPos(g_currentMission.terrainRootNode, self.data.x, y, self.data.z)
localxRot, yRot, zRot=PathfinderUtil.getNormalWorldRotation(self.data.x, self.data.z)
self.data.txRotDeg=math.deg(xRot)
self.data.tyRotDeg=math.deg(yRot)
self.data.tzRotDeg=math.deg(zRot)
self.data.collidingShapes=''
overlapBox(self.data.x, self.data.y+0.2, self.data.z, 0, self.yRot, 0, 1.6, 1, 8, "overlapBoxCallback", self,
bitOR(AIVehicleUtil.COLLISION_MASK, 2), true, true, true)
ifself.pathfinderandself.pathfinder:isActive() then
localdone, path=self.pathfinder:resume()
ifdonethen
self:loadPath(path)
end
end
end
functionDevHelper:overlapBoxCallback(transformId)
localcollidingObject=g_currentMission.nodeToObject[transformId]
localtext
ifcollidingObjectthen
ifcollidingObject.getRootVehiclethen
text='vehicle' ..collidingObject:getName()
else
ifcollidingObject:isa(Bale) then
text='Bale ' ..tostring(collidingObject) ..'' ..tostring(NetworkUtil.getObjectId(collidingObject))
else
text=collidingObject.getNameandcollidingObject:getName() or'N/A'
end
end
else
text=''
forkey, classIdinpairs(ClassIds) do
ifgetHasClassId(transformId, classId) then
text=text..'' ..key
end
end
end
self.data.collidingShapes=self.data.collidingShapes..'|' ..text
end
functionDevHelper:updateProximitySensors(vehicle)
ifvehicleandvehicle.cp.driverthen
ifvehicle.cp.driver.forwardLookingProximitySensorPackthen
locald, otherVehicle, object, deg, dAvg=
vehicle.cp.driver.forwardLookingProximitySensorPack:getClosestObjectDistanceAndRootVehicle()
--renderText(0.6, 0.4, 0.018, string.format('d=%.1f %s deg=%.1f dAvg=%.1f dx=%.1f (%s)',
-- d, nameNum(otherVehicle), deg, dAvg, dAvg * math.sin(math.rad(deg)), nameNum(vehicle)))
end
ifvehicle.cp.driver.backwardLookingProximitySensorPackthen
locald, otherVehicle, object, deg, dAvg=
vehicle.cp.driver.backwardLookingProximitySensorPack:getClosestObjectDistanceAndRootVehicle()
end
end
end
-- Left-Alt + , (<) = mark current position as start for pathfinding
-- Left-Alt + , (<) = mark current position as start for pathfinding
-- Left-Alt + . (>) = mark current position as goal for pathfinding
-- Left-Ctrl + . (>) = start pathfinding from marked start to marked goal
-- Left-Ctrl + , (<) = mark current field as field for pathfinding
-- Left-Alt + Space = save current vehicle position
-- Left-Ctrl + Space = restore current vehicle position
functionDevHelper:keyEvent(unicode, sym, modifier, isDown)
ifnotCpManager.isDeveloperthenreturnend
ifbitAND(modifier, Input.MOD_LALT) ~=0andisDownandsym==Input.KEY_commathen
-- Left Alt + < mark start
self.start=State3D(self.data.x, -self.data.z, courseGenerator.fromCpAngleDeg(self.data.yRotDeg))
self:debug('Start %s', tostring(self.start))
PathfinderUtil.checkForObstaclesAhead(self.vehicle, 6)
elseifbitAND(modifier, Input.MOD_LALT) ~=0andisDownandsym==Input.KEY_periodthen
-- Left Alt + > mark goal
self.goal=State3D(self.data.x, -self.data.z, courseGenerator.fromCpAngleDeg(self.data.yRotDeg))
localx, y, z=getWorldTranslation(self.node)
local_, yRot, _=getRotation(self.node)
ifself.goalNodethen
setTranslation( self.goalNode, x, y, z );
setRotation( self.goalNode, 0, yRot, 0);
else
self.goalNode=courseplay.createNode('devhelper', x, z, yRot)
end
self:debug('Goal %s', tostring(self.goal))
--self:startPathfinding()
elseifbitAND(modifier, Input.MOD_LCTRL) ~=0andisDownandsym==Input.KEY_periodthen
-- Left Ctrl + > find path
self:debug('Calculate')
self:startPathfinding()
elseifbitAND(modifier, Input.MOD_LCTRL) ~=0andisDownandsym==Input.KEY_commathen
self.fieldNumForPathfinding=PathfinderUtil.getFieldNumUnderNode(self.node)
self:debug('Set field %d for pathfinding', self.fieldNumForPathfinding)
elseifbitAND(modifier, Input.MOD_LALT) ~=0andisDownandsym==Input.KEY_spacethen
-- save vehicle position
g_currentMission.controlledVehicle.vehiclePositionData= {}
DevHelper.saveVehiclePosition(g_currentMission.controlledVehicle, g_currentMission.controlledVehicle.vehiclePositionData)
elseifbitAND(modifier, Input.MOD_LCTRL) ~=0andisDownandsym==Input.KEY_spacethen
-- restore vehicle position
DevHelper.restoreVehiclePosition(g_currentMission.controlledVehicle)
end
end
functionDevHelper:startPathfinding()
self.pathfinderStartTime=g_time
self:debug('Starting pathfinding between %s and %s', tostring(self.start), tostring(self.goal))
localdone, path
ifself.vehicleandself.vehicle.cp.driverandself.vehicle.cp.driver.fieldworkCoursethen
self:debug('Starting pathfinding for turn between %s and %s', tostring(self.start), tostring(self.goal))
self.pathfinder, done, path=PathfinderUtil.findPathForTurn(self.vehicle, 0, self.goalNode, 0,
1.05*self.vehicle.cp.settings.turnDiameter:get() /2, false, self.vehicle.cp.driver.fieldworkCourse)
else
self:debug('Starting pathfinding (no reverse) between %s and %s, field %d',
tostring(self.start), tostring(self.goal), self.fieldNumForPathfindingor0)
localstart=State3D:copy(self.start)
self.pathfinder, done, path=PathfinderUtil.startPathfindingFromVehicleToGoal(self.vehicle, self.goal,
false, self.fieldNumForPathfindingor0, {}, {}, 10)
end
ifdonethen
ifpaththen
self:loadPath(path)
else
self:debug('No path found')
end
end
end
functionDevHelper:mouseEvent(posX, posY, isDown, isUp, mouseKey)
end
functionDevHelper:toggleVisualDebug()
self.isVisualDebugEnabled=notself.isVisualDebugEnabled
end
functionDevHelper:draw()
ifnotCpManager.isDeveloperthenreturnend
ifnotself.isVisualDebugEnabledthenreturnend
localdata= {}
forkey, valueinpairs(self.data) do
table.insert(data, {name=key, value=value})
end
DebugUtil.renderTable(0.65, 0.3, 0.013, data, 0.05)
self:drawCourse()
self:showVehicleSize()
self:showFillNodes()
for_, vehicleinpairs(g_currentMission.vehicles) do
ifvehicle~=g_currentMission.controlledVehicleandvehicle.cpandvehicle.cp.driverthen
vehicle.cp.driver:onDraw()
end
end
PathfinderUtil.showNodes(self.pathfinder)
PathfinderUtil.showOverlapBoxes()
ifnotself.tNodethen
self.tNode=createTransformGroup("devhelper")
link(g_currentMission.terrainRootNode, self.tNode)
end
PathfinderUtil.setWorldPositionAndRotationOnTerrain(self.tNode, self.data.x, self.data.z, self.yRot, 0.5)
DebugUtil.drawDebugNode(self.tNode, 'Terrain normal')
localnx, ny, nz=getTerrainNormalAtWorldPos(g_currentMission.terrainRootNode, self.data.x, self.data.y, self.data.z)
localx, y, z=localToWorld(self.node, 0, -1, -3)
cpDebug:drawLine(x, y, z, 1, 1, 1, x+nx, y+ny, z+nz)
localxRot, yRot, zRot=getWorldRotation(self.tNode)
DebugUtil.drawOverlapBox(self.data.x, self.data.y, self.data.z, xRot, yRot, zRot, 4, 1, 4, 0, 100, 0)
end
---@parampathState3D[]
functionDevHelper:loadPath(path)
ifpaththen
self:debug('Path with %d waypoint found, finished in %d ms', #path, g_time-self.pathfinderStartTime)
self.course=Course(nil, courseGenerator.pointsToXzInPlace(path), true)
else
self:debug('No path!')
end
end
functionDevHelper:drawCourse()
ifnotself.coursethenreturnend
fori=1, self.course:getNumberOfWaypoints() do
localx, y, z=self.course:getWaypointPosition(i)
cpDebug:drawPoint(x, y+3, z, 10, 0, 0)
Utils.renderTextAtWorldPosition(x, y+3.2, z, tostring(i), getCorrectTextSize(0.012), 0)
ifi<self.course:getNumberOfWaypoints() then
localnx, ny, nz=self.course:getWaypointPosition(i+1)
cpDebug:drawLine(x, y+3, z, 0, 0, 100, nx, ny+3, nz)
end
end
end
functionDevHelper:showFillNodes()
for_, vehicleinpairs(g_currentMission.vehicles) do
ifSpecializationUtil.hasSpecialization(Trailer, vehicle.specializations) then
DebugUtil.drawDebugNode(vehicle.rootNode, 'Root node')
localfillUnits=vehicle:getFillUnits()
fori=1, #fillUnitsdo
localfillRootNode=vehicle:getFillUnitExactFillRootNode(i)
iffillRootNodethenDebugUtil.drawDebugNode(fillRootNode, 'Fill node ' ..tostring(i)) end
end
end
end
end
functionDevHelper:showVehicleSize()
localvehicle=g_currentMission.controlledVehicle
ifnotvehiclethenreturnend
localx, z, yRot=PathfinderUtil.getNodePositionAndDirection(vehicle.rootNode)
localnode=State3D(x, -z, courseGenerator.fromCpAngle(yRot))
ifnotg_devHelper.helperNodethen
g_devHelper.helperNode=courseplay.createNode('pathfinderHelper', node.x, -node.y, 0)
end
localy=getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, node.x, 0, -node.y);
setTranslation(g_devHelper.helperNode, node.x, y, -node.y)
setRotation(g_devHelper.helperNode, 0, courseGenerator.toCpAngle(node.t), 0)
ifself.vehicleDatathen
for_, rectangleinipairs(self.vehicleData.rectangles) do
localx1,y1,z1=localToWorld(g_devHelper.helperNode, rectangle.dRight, 2, rectangle.dFront);
localx2,y2,z2=localToWorld(g_devHelper.helperNode, rectangle.dLeft, 2, rectangle.dFront);
localx3,y3,z3=localToWorld(g_devHelper.helperNode, rectangle.dRight, 2, rectangle.dRear);
localx4,y4,z4=localToWorld(g_devHelper.helperNode, rectangle.dLeft, 2, rectangle.dRear);
drawDebugLine(x1,y1,z1,0.2, 0.2 ,1,x2,y2,z2,0.2, 0.2,1);
drawDebugLine(x1,y1,z1,0.2, 0.2 ,1,x3,y3,z3,0.2, 0.2,1);
drawDebugLine(x2,y2,z2,0.2, 0.2 ,1,x4,y4,z4,0.2, 0.2,1);
drawDebugLine(x3,y3,z3,0.2, 0.2 ,1,x4,y4,z4,0.2, 0.2,1);
end
ifself.vehicleData.trailerRectanglethen
PathfinderUtil.initializeTrailerHeading(node, self.vehicleData)
localx, y, z=localToWorld(g_devHelper.helperNode, 0, 0, self.vehicleData.trailerHitchOffset)
setTranslation(g_devHelper.helperNode, x, y, z)
setRotation(g_devHelper.helperNode, 0, courseGenerator.toCpAngle(node.tTrailer), 0)
localx1,y1,z1=localToWorld(g_devHelper.helperNode, self.vehicleData.trailerRectangle.dRight, 2, self.vehicleData.trailerRectangle.dFront);
localx2,y2,z2=localToWorld(g_devHelper.helperNode, self.vehicleData.trailerRectangle.dLeft, 2, self.vehicleData.trailerRectangle.dFront);
localx3,y3,z3=localToWorld(g_devHelper.helperNode, self.vehicleData.trailerRectangle.dRight, 2, self.vehicleData.trailerRectangle.dRear);
localx4,y4,z4=localToWorld(g_devHelper.helperNode, self.vehicleData.trailerRectangle.dLeft, 2, self.vehicleData.trailerRectangle.dRear);
drawDebugLine(x1,y1,z1,0,1,0,x2,y2,z2,0,1,0);
drawDebugLine(x1,y1,z1,0,1,0,x3,y3,z3,0,1,0);
drawDebugLine(x2,y2,z2,0,1,0,x4,y4,z4,0,1,0);
drawDebugLine(x3,y3,z3,0,1,0,x4,y4,z4,0,1,0);
end
end
DebugUtil.drawDebugNode(g_devHelper.helperNode, 'devhelper')
end
functionDevHelper.saveVehiclePosition(vehicle, vehiclePositionData)
localsavePosition=function(object)
localsavedPosition= {}
savedPosition.x, savedPosition.y, savedPosition.z=getWorldTranslation(object.rootNode)
savedPosition.xRot, savedPosition.yRot, savedPosition.zRot=getWorldRotation(object.rootNode)
returnsavedPosition
end
ifnotvehicle.getAttachedImplementsthenreturnend
table.insert(vehiclePositionData, {vehicle, savePosition(vehicle)})
for_,implinpairs(vehicle:getAttachedImplements()) do
DevHelper.saveVehiclePosition(impl.object, vehiclePositionData)
end
courseplay.info('Saved position of %s', nameNum(vehicle))
end
functionDevHelper.restoreVehiclePosition(vehicle)
ifvehicle.vehiclePositionDatathen
for_, savedPositioninpairs(vehicle.vehiclePositionData) do
savedPosition[1]:setAbsolutePosition(savedPosition[2].x, savedPosition[2].y, savedPosition[2].z,
savedPosition[2].xRot, savedPosition[2].yRot, savedPosition[2].zRot)
courseplay.info('Restored position of %s', nameNum(savedPosition[1]))
end
end
end
functionDevHelper.restoreAllVehiclePositions()
for_, vehicleinpairs(g_currentMission.vehicles) do
ifvehicle.vehiclePositionDatathen
DevHelper.restoreVehiclePosition(vehicle)
end
end
end
functionDevHelper.saveAllVehiclePositions()
for_, vehicleinpairs(g_currentMission.vehicles) do
vehicle.vehiclePositionData= {}
DevHelper.saveVehiclePosition(vehicle, vehicle.vehiclePositionData)
end
end
-- make sure to recreate the global dev helper whenever this script is (re)loaded
g_devHelper=DevHelper()