forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turn.lua
355 lines (323 loc) · 12.3 KB
/
turn.lua
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
function courseplay:turn(self, dt) --!!!
--[[ TURN STAGES:
0: raise implements
1: start forward turn
2:
3: reversing
4:
5:
6:
]]
local newTargetX, newTargetY, newTargetZ;
local moveForwards = true;
local updateWheels = true;
local turnOutTimer = 1500
--local frontMarker = Utils.getNoNil(self.cp.backMarkerOffset, -3)
--local backMarker = Utils.getNoNil(self.cp.aiFrontMarker,0)
local frontMarker = Utils.getNoNil(self.cp.aiFrontMarker, -3)
local backMarker = Utils.getNoNil(self.cp.backMarkerOffset,0)
if self.cp.noStopOnEdge then
turnOutTimer = 0
end
if not self.cp.checkMarkers then
self.cp.checkMarkers = true
for _,workTool in pairs(self.cp.workTools) do
courseplay:setMarkers(self, workTool)
end
end
self.cp.turnTimer = self.cp.turnTimer - dt;
-- TURN STAGES 1 - 6
if self.cp.turnTimer < 0 or self.cp.turnStage > 0 then
-- TURN STAGES 2 - 6
if self.cp.turnStage > 1 then
local x,y,z = getWorldTranslation(self.cp.DirectionNode);
local dirX, dirZ = self.aiTractorDirectionX, self.aiTractorDirectionZ;
local myDirX, myDirY, myDirZ = localDirectionToWorld(self.cp.DirectionNode, 0, 0, 1);
newTargetX = self.aiTractorTargetX;
newTargetY = y;
newTargetZ = self.aiTractorTargetZ;
-- TURN STAGE 2
if self.cp.turnStage == 2 then
self.turnStageTimer = self.turnStageTimer - dt;
if self.cp.isTurning == "left" then
AITractor.aiRotateLeft(self);
else
AITractor.aiRotateRight(self);
end
if myDirX*dirX + myDirZ*dirZ > 0.2 or self.turnStageTimer < 0 then
if self.cp.aiTurnNoBackward or
(courseplay:distance(newTargetX, newTargetZ, self.Waypoints[self.recordnumber-1].cx, self.Waypoints[self.recordnumber-1].cz) > self.cp.turnDiameter * 1.2) then
self.cp.turnStage = 4;
else
self.cp.turnStage = 3;
moveForwards = false;
end;
if self.turnStageTimer < 0 then
self.aiTractorTargetBeforeSaveX = self.aiTractorTargetX;
self.aiTractorTargetBeforeSaveZ = self.aiTractorTargetZ;
newTargetX = self.aiTractorTargetBeforeTurnX;
newTargetZ = self.aiTractorTargetBeforeTurnZ;
moveForwards = false;
self.cp.turnStage = 6;
self.turnStageTimer = Utils.getNoNil(self.turnStage6Timeout,3000)
else
self.turnStageTimer = Utils.getNoNil(self.turnStage3Timeout,20000)
end;
end;
-- TURN STAGE 3
elseif self.cp.turnStage == 3 then
self.turnStageTimer = self.turnStageTimer - dt;
if myDirX*dirX + myDirZ*dirZ > 0.95 or self.turnStageTimer < 0 then
self.cp.turnStage = 4;
else
moveForwards = false;
end;
-- TURN STAGE 4
elseif self.cp.turnStage == 4 then
local dx, dz = x-newTargetX, z-newTargetZ;
local dot = dx*dirX + dz*dirZ;
if self.cp.noStopOnEdge then
courseplay:lowerImplements(self, true, false)
end
if -dot < Utils.getNoNil(self.turnEndDistance, 4) then
if self.turnTargetMoveBack ~= nil and self.aiToolExtraTargetMoveBack ~= nil then
newTargetX = self.aiTractorTargetX + dirX*(self.turnTargetMoveBack + self.aiToolExtraTargetMoveBack);
newTargetY = y;
newTargetZ = self.aiTractorTargetZ + dirZ*(self.turnTargetMoveBack + self.aiToolExtraTargetMoveBack);
else
newTargetX = self.aiTractorTargetX
newTargetY = y;
newTargetZ = self.aiTractorTargetZ
end
self.cp.turnStage = 5;
end;
-- TURN STAGE 5
elseif self.cp.turnStage == 5 then
local backX, backY, backZ = localToWorld(self.cp.DirectionNode,0,0,frontMarker);
local dx, dz = backX-newTargetX, backZ-newTargetZ;
local dot = dx*dirX + dz*dirZ;
local moveback = 0
if self.turnEndBackDistance ~= nil and self.aiToolExtraTargetMoveBack ~= nil then
moveback = self.turnEndBackDistance+self.aiToolExtraTargetMoveBack
else
moveback = 10
end
if -dot < moveback then
self.cp.turnStage = 0;
local _,_,z1 = worldToLocal(self.cp.DirectionNode, self.Waypoints[self.recordnumber+1].cx, backY, self.Waypoints[self.recordnumber+1].cz);
local _,_,z2 = worldToLocal(self.cp.DirectionNode, self.Waypoints[self.recordnumber+2].cx, backY, self.Waypoints[self.recordnumber+2].cz);
local _,_,z3 = worldToLocal(self.cp.DirectionNode, self.Waypoints[self.recordnumber+3].cx, backY, self.Waypoints[self.recordnumber+3].cz);
if self.cp.isCombine then
if z2 > 6 then
courseplay:setRecordNumber(self, self.recordnumber + 2);
elseif z3 > 6 then
courseplay:setRecordNumber(self, self.recordnumber + 3);
else
courseplay:setRecordNumber(self, self.recordnumber + 4);
end
else
if z1 > 0 then
courseplay:setRecordNumber(self, self.recordnumber + 1);
elseif z2 > 0 then
courseplay:setRecordNumber(self, self.recordnumber + 2);
else
courseplay:setRecordNumber(self, self.recordnumber + 3);
end
end;
courseplay:lowerImplements(self, true, true)
self.cp.turnTimer = 8000
self.cp.isTurning = nil
self.cp.waitForTurnTime = self.timer + turnOutTimer;
end;
-- TURN STAGE 6
elseif self.cp.turnStage == 6 then
self.turnStageTimer = self.turnStageTimer - dt;
if self.turnStageTimer < 0 then
self.turnStageTimer = Utils.getNoNil(self.turnStage2Timeout,20000)
self.cp.turnStage = 2;
newTargetX = self.aiTractorTargetBeforeSaveX;
newTargetZ = self.aiTractorTargetBeforeSaveZ;
else
local x,y,z = getWorldTranslation(self.cp.DirectionNode);
local dirX, dirZ = -self.aiTractorDirectionX, -self.aiTractorDirectionZ;
-- just drive along direction
local targetX, targetZ = self.aiTractorTargetX, self.aiTractorTargetZ;
local dx, dz = x-targetX, z-targetZ;
local dot = dx*dirX + dz*dirZ;
local projTargetX = targetX +dirX*dot;
local projTargetZ = targetZ +dirZ*dot;
local aheadDistance = Utils.getNoNil(self.aiTractorLookAheadDistance,10)
newTargetX = projTargetX-dirX*aheadDistance;
newTargetZ = projTargetZ-dirZ*aheadDistance;
moveForwards = false;
end;
end;
if courseplay.debugChannels[12] then
drawDebugPoint(newTargetX, y+3, newTargetZ, 1, 1, 0, 1);
end;
-- TURN STAGE 1
elseif self.cp.turnStage == 1 then
-- turn
local dirX, dirZ = self.aiTractorDirectionX, self.aiTractorDirectionZ;
if self.cp.isTurning == "right" then
self.aiTractorTurnLeft = false;
else
self.aiTractorTurnLeft = true;
end;
local cx,cz = self.Waypoints[self.recordnumber+1].cx, self.Waypoints[self.recordnumber+1].cz;
if (self.cp.laneOffset ~= nil and self.cp.laneOffset ~= 0) or (self.cp.toolOffsetX ~= nil and self.cp.toolOffsetX ~= 0) then
cx,cz = courseplay:turnWithOffset(self)
end;
newTargetX = cx
newTargetY = y;
newTargetZ = cz
self.aiTractorTargetBeforeTurnX = self.aiTractorTargetX;
self.aiTractorTargetBeforeTurnZ = self.aiTractorTargetZ;
self.aiTractorDirectionX = -dirX;
self.aiTractorDirectionZ = -dirZ;
self.cp.turnStage = 2;
self.turnStageTimer = Utils.getNoNil(self.turnStage2Timeout,20000)
-- TURN STAGE ??? --TODO (Jakob): what's the situation here? turnStage not > 1 and not > 0 ? When do we get to this point?
else
self.cp.turnStage = 1;
if self.cp.noStopOnTurn == false then
self.waitForTurnTime = self.timer + 1500;
end
courseplay:lowerImplements(self, false, true)
updateWheels = false;
end;
-- TURN STAGE 0
else
local offset = Utils.getNoNil(self.cp.totalOffsetX, 0)
local x,y,z = localToWorld(self.cp.DirectionNode, offset, 0, backMarker)
local dist = courseplay:distance(self.Waypoints[self.recordnumber].cx, self.Waypoints[self.recordnumber].cz, x, z)
if backMarker <= 0 then
if dist < 0.5 then
if not self.cp.noStopOnTurn then
self.cp.waitForTurnTime = self.timer + 1500
end
courseplay:lowerImplements(self, false, false)
updateWheels = false;
self.cp.turnStage = 1;
end
else
if dist < 0.5 and self.cp.turnStage ~= -1 then
self.cp.turnStage = -1
courseplay:lowerImplements(self, false, false)
end
if dist > backMarker and self.cp.turnStage == -1 then
if self.cp.noStopOnTurn == false then
self.cp.waitForTurnTime = self.timer + 1500
end
updateWheels = false;
self.cp.turnStage = 1;
end
end
x,y,z = localToWorld(self.cp.DirectionNode, 0, 0, 1)
self.aiTractorTargetX, self.aiTractorTargetZ = x,z
x,y,z = getWorldTranslation(self.cp.DirectionNode);
local dirX, dirZ = self.aiTractorDirectionX, self.aiTractorDirectionZ;
local targetX, targetZ = self.aiTractorTargetX, self.aiTractorTargetZ;
local dx, dz = x-targetX, z-targetZ;
local dot = dx*dirX + dz*dirZ;
local projTargetX = targetX +dirX*dot;
local projTargetZ = targetZ +dirZ*dot;
newTargetX = projTargetX+self.aiTractorDirectionX
newTargetY = y;
newTargetZ = projTargetZ+self.aiTractorDirectionZ
end;
if updateWheels then
local allowedToDrive = true
local refSpeed = self.cp.speeds.turn
self.cp.speedDebugLine = ("turn("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
courseplay:setSpeed(self, refSpeed )
local lx, lz = AIVehicleUtil.getDriveDirection(self.cp.DirectionNode, newTargetX, newTargetY, newTargetZ);
if self.cp.turnStage == 3 and math.abs(lx) < 0.1 then
self.cp.turnStage = 4;
moveForwards = true;
end;
if self.cp.TrafficBrake then
moveForwards = self.movingDirection == -1;
lx = 0
lz = 1
end
if self.invertedDrivingDirection then
lx = -lx
end
AIVehicleUtil.driveInDirection(self, dt, 25, 1, 0.5, 20, true, moveForwards, lx, lz, refSpeed, 1);
courseplay:setTrafficCollision(self, lx, lz, true)
end;
if newTargetX ~= nil and newTargetZ ~= nil then
self.aiTractorTargetX = newTargetX;
self.aiTractorTargetZ = newTargetZ;
end;
end
function courseplay:lowerImplements(self, moveDown, workToolonOff)
--moveDown true= lower, false = raise , workToolonOff true = switch on worktool, false = switch off worktool
if moveDown == nil then
moveDown = false;
end;
local state = 1;
if moveDown then
state = -1;
end;
local specialTool;
for _,workTool in pairs(self.cp.workTools) do
--courseplay:handleSpecialTools(self,workTool,unfold,lower,turnOn,allowedToDrive,cover,unload)
specialTool = courseplay:handleSpecialTools(self,workTool,true,moveDown,workToolonOff,nil,nil,nil);
if not specialTool and workTool.setPickupState ~= nil then
if workTool.isPickupLowered ~= nil and workTool.isPickupLowered ~= moveDown then
workTool:setPickupState(moveDown, false);
end;
end;
end;
if not specialTool then
if self.setAIImplementsMoveDown ~= nil then
self:setAIImplementsMoveDown(moveDown,true);
elseif self.setFoldState ~= nil then
self:setFoldState(state, true);
end;
if self.cp.mode == 4 then
for _,workTool in pairs(self.cp.workTools) do
if workTool.setIsTurnedOn ~= nil and not courseplay:isFolding(workTool) and workTool ~= self and workTool.isTurnedOn ~= workToolonOff then
workTool:setIsTurnedOn(workToolonOff, false);
end;
end;
end;
end;
end;
function courseplay:turnWithOffset(self)
--SYMMETRIC LANE CHANGE
if self.cp.symmetricLaneChange then
if self.cp.switchLaneOffset then
courseplay:changeLaneOffset(self, nil, self.cp.laneOffset * -1);
self.cp.switchLaneOffset = false;
courseplay:debug(string.format("%s: cp.turnStage == 1, switchLaneOffset=true -> new laneOffset=%.1f, new totalOffset=%.1f, set switchLaneOffset to false", nameNum(self), self.cp.laneOffset, self.cp.totalOffsetX), 12);
end;
end;
--TOOL OFFSET TOGGLE
if self.cp.hasPlough then
if self.cp.switchToolOffset then
courseplay:changeToolOffsetX(self, nil, self.cp.toolOffsetX * -1, true);
self.cp.switchToolOffset = false;
courseplay:debug(string.format("%s: cp.turnStage == 1, switchToolOffset=true -> new toolOffset=%.1f, new totalOffset=%.1f, set switchToolOffset to false", nameNum(self), self.cp.toolOffsetX, self.cp.totalOffsetX), 12);
end;
end;
local curPoint = self.Waypoints[self.recordnumber+1]
local cx, cz = curPoint.cx, curPoint.cz;
local offsetX = self.cp.totalOffsetX
if curPoint.turnEnd and curPoint.laneDir ~= nil then --TODO (Jakob): use point's direction to next point to get the proper offset
local dir = curPoint.laneDir;
local turnDir = curPoint.turn;
if dir == "E" then
cz = curPoint.cz + offsetX;
elseif dir == "W" then
cz = curPoint.cz - offsetX;
elseif dir == "N" then
cx = curPoint.cx + offsetX;
elseif dir == "S" then
cx = curPoint.cx - offsetX;
end;
end;
return cx,cz
end