forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode10.lua
699 lines (615 loc) · 26.7 KB
/
mode10.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
function courseplay:handleMode10(vehicle,allowedToDrive,lx,lz, dt)
if vehicle.cp.mode9TargetSilo == nil then
vehicle.cp.mode9TargetSilo = courseplay:getMode9TargetBunkerSilo(vehicle,1)
end
if not vehicle.cp.mode9TargetSilo then
courseplay:setInfoText(vehicle, courseplay:loc('COURSEPLAY_MODE10_NOSILO'));
return true, false;
end
local emptyTimer = 2
local x,y,z = getWorldTranslation(vehicle.cp.workTools[1].rootNode)
local dx,dy,dz = getWorldTranslation(vehicle.cp.DirectionNode)
local ty = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 1, z);
local dty = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, dx, 1, dz)
local nodeX,nodeY,nodeZ = worldToLocal(vehicle.cp.DirectionNode,x,y,z)
local nx, ny, nz = localDirectionToWorld(vehicle.cp.DirectionNode, 0, 0, -1)
local zeroX,zeroY,zeroZ = dx+(nx*-nodeZ) ,dy+(ny*-nodeZ),dz+(nz*-nodeZ)
local offSetX,offSetY,offSetZ = worldToLocal(vehicle.cp.DirectionNode,zeroX,zeroY,zeroZ)
local diffY = (zeroY-ty)-(dy-dty)
local totalFillLevel = vehicle.cp.mode9TargetSilo.fillLevel
local compactedFillLevel = vehicle.cp.mode9TargetSilo.compactedFillLevel
local compactedRate = (vehicle.cp.mode9TargetSilo.compactedFillLevel*100)/vehicle.cp.mode9TargetSilo.fillLevel
local fwd = true
local cx,cy,cz =0,0,0;
local goSave = false
if vehicle.cp.mode10.searchCourseplayersOnly then
for rootNode,courseplayer in pairs (CpManager.activeCoursePlayers) do
local distance = courseplay:distanceToPoint(courseplayer,vehicle.Waypoints[1].cx,ty ,vehicle.Waypoints[1].cz) --courseplay:nodeToNodeDistance(vehicle.cp.DirectionNode, rootNode)
--print(string.format("%s: distance = %s",tostring(rootNode),tostring(distance)))
if distance < vehicle.cp.mode10.searchRadius and courseplayer ~= vehicle and courseplayer.cp.totalFillLevel ~= nil and courseplayer.cp.totalFillLevel > 1 then
local insert = true
for i=1,#vehicle.cp.mode10.stoppedCourseplayers do
if courseplayer == vehicle.cp.mode10.stoppedCourseplayers[i] then
insert = false
end
end
if insert then
table.insert(vehicle.cp.mode10.stoppedCourseplayers,courseplayer)
end
end
end
else
for _,steerable in pairs(g_currentMission.steerables) do
local x,y,z = getWorldTranslation(steerable.rootNode)
local distance = courseplay:distance(x,z,vehicle.Waypoints[1].cx,vehicle.Waypoints[1].cz)
if distance < vehicle.cp.mode10.searchRadius and steerable ~= vehicle and steerable.isMotorStarted then
local insert = true
for i=1,#vehicle.cp.mode10.stoppedCourseplayers do
if steerable == vehicle.cp.mode10.stoppedCourseplayers[i] then
insert = false
end
end
if insert then
table.insert(vehicle.cp.mode10.stoppedCourseplayers,steerable)
end
end
end
end
local targetIsEmpty = vehicle.cp.actualTarget and vehicle.cp.actualTarget.empty
local targetIsFilled = vehicle.cp.mode10.deadline and vehicle.cp.mode10.deadline == vehicle.cp.mode10.firstLine
if #vehicle.cp.mode10.stoppedCourseplayers > 0 then
for i=1, #vehicle.cp.mode10.stoppedCourseplayers do
if i > 1 or not vehicle.Waypoints[vehicle.cp.previousWaypointIndex].wait then
vehicle.cp.mode10.stoppedCourseplayers[i].cp.isNotAllowedToDrive = true
end
end
goSave = true
elseif not targetIsEmpty and not targetIsFilled then
courseplay:setVehicleWait(vehicle, false);
vehicle.cp.mode10.deadline = nil
end
local inBunker = vehicle.cp.waypointIndex == 1
local refSpeed = math.min(vehicle.cp.speeds.reverse,30)
if vehicle.cp.hasDriveControl then
courseplay:setFourWheelDrive(vehicle,inBunker)
end
if inBunker or vehicle.cp.mode10.isStuck then
if vehicle.cp.actualTarget == nil or vehicle.cp.BunkerSiloMap == nil then
courseplay:getActualTarget(vehicle)
end
if vehicle.cp.modeState == 1 then --push
courseplay:setShieldTarget(vehicle,"down")
fwd = false
local newSx = vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][1].sx
local newSz = vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][1].sz
local newWx = vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][#vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap]].wx
local newWz = vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][#vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap]].wz
local newHx = vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][1].hx
local newHz = vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][1].hz
local wY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, newWx, 1, newWz);
local hY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, newHx, 1, newHz);
local fillType = TipUtil.getFillTypeAtLine(newWx, wY, newWz, newHx, hY, newHz, 5)
local newFillLevel = TipUtil.getFillLevelAtArea(fillType, newSx, newSz, newWx, newWz, newHx, newHz )
local leveled = vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][1].leveled
local hasFillLevel = vehicle.cp.totalFillLevel > 100 and vehicle.cp.actualTarget.line > 1
if vehicle.cp.mode10FillLevel == nil then
vehicle.cp.mode10FillLevel = newFillLevel
end
if vehicle.cp.fillLevelGo == nil then
vehicle.cp.fillLevelGo = true
end
if vehicle.cp.actualTarget.empty or (vehicle.cp.mode10.deadline and vehicle.cp.mode10.deadline == vehicle.cp.mode10.firstLine) then
goSave = true
end
local currentHeight = courseplay:round(y-ty,2)
vehicle.cp.currentHeigth = currentHeight
local shouldBHeight = 0
local targetUnit = vehicle.cp.BunkerSiloMap[vehicle.cp.actualTarget.line][vehicle.cp.actualTarget.column]
--print(string.format("targetUnit.height(%s)-(dy-dty)(%s)",tostring(targetUnit.height),tostring(y-dty)))
---(dy-dty) --vehicle.cp.mode10.shieldHeight
cx ,cz = targetUnit.cx, targetUnit.cz
cy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 1, cz);
local distance2Target = courseplay:distance(x,z, cx, cz) --distance from shovel to target
if vehicle.cp.mode10.leveling then
if vehicle.cp.speeds.bunkerSilo > 15 then
vehicle.cp.speeds.bunkerSilo = 15
elseif vehicle.cp.speeds.bunkerSilo > 10 then
emptyTimer = 0.8
end
if vehicle.cp.mode10.automaticSpeed then --hard code till soultion for variable speed is available
vehicle.cp.speeds.bunkerSilo = 10
end
--[[if leveled then
courseplay:setInfoText(vehicle, ('verdichten %d'):format(compactedRate).."%");
else
courseplay:setInfoText(vehicle, 'verteilen');
end]]
if vehicle.cp.mode10.automaticHeigth then
local tempHeight = targetUnit.height
if vehicle.cp.mode10.drivingThroughtLoading and vehicle.cp.actualTarget.line == #vehicle.cp.BunkerSiloMap and tempHeight < 2 then --1.2 then
shouldBHeight = tempHeight + 0.3
elseif vehicle.cp.actualTarget.line < 2 then
shouldBHeight = Utils.clamp(tempHeight,0.3,0.5)
elseif vehicle.cp.actualTarget.line == 2 and tempHeight > 0.5 then
shouldBHeight = ((tempHeight - 0.5) /distance2Target)+ 0.5
else
shouldBHeight = math.max(0.3,tempHeight)
end
else
shouldBHeight = vehicle.cp.mode10.shieldHeight +(dy-dty)
end
if not leveled then
refSpeed = math.min(15,vehicle.cp.speeds.bunkerSilo)
end
else
refSpeed = math.min(20,vehicle.cp.speeds.bunkerSilo)
end
local heightDiff = courseplay:round(shouldBHeight,2) - courseplay:round(currentHeight,2)
local targetHeigth = courseplay:round(shouldBHeight,3) + (courseplay:round(shouldBHeight,3)- currentHeight) -(dy-dty) - diffY + vehicle.cp.mode10.bladeOffset
vehicle.cp.shouldBHeight = courseplay:round(shouldBHeight,2)
vehicle.cp.tractorHeight = courseplay:round(dy-dty,2)
vehicle.cp.diffY = courseplay:round(diffY,2)
vehicle.cp.mode10.targetHeigth = courseplay:round(targetHeigth,2)
if not vehicle.cp.mode10.leveling then
targetHeigth = 0
end
if leveled then
targetHeigth = 2
distance2Target = courseplay:distanceToPoint(vehicle, cx, cy, cz) --distance from vehicle to target
end
--[[if vehicle.cp.shieldState == "down" and targetHeigth < 0 then --TODO find a proper way to trigger swinging
--print("jump detected : "..tostring(vehicle.cp.mode10.lastDiffDiff))
if not vehicle.cp.mode10.jumpIsCounted then
vehicle.cp.mode10.jumpsPerRun = vehicle.cp.mode10.jumpsPerRun + 1
vehicle.cp.mode10.jumpIsCounted = true
end
else
vehicle.cp.mode10.jumpIsCounted = false
end]]
if vehicle.cp.shieldState == "down" and math.abs(heightDiff) > 0.01 then -- math.abs(heightDiff) > 0.05 then
--check whether we have the target height in our table or set the closest
local closestIndex = 99
local closestValue = 99
if vehicle.cp.mode10.alphaList[targetHeigth] then
--print(string.format("vehicle.cp.mode10.alphaList[targetHeigth(%s) exists]",tostring(targetHeigth)))
closestIndex = targetHeigth
else
--print(string.format("vehicle.cp.mode10.alphaList[targetHeigth(%s) doesnt exist serach nearest]",tostring(targetHeigth)))
for indexHeight,alpha in pairs (vehicle.cp.mode10.alphaList) do
--print("try "..tostring(indexHeight))
local diff = math.abs(targetHeigth-indexHeight)
if closestValue > diff then
--print(string.format("%s is closer- set as closest",tostring(closestValue)))
closestIndex = indexHeight
closestValue = diff
end
end
end
--print("set MoveShield to ".. tostring(closestIndex).." Alpha: "..tostring(vehicle.cp.mode10.alphaList[closestIndex]))
courseplay:moveShield(vehicle,"up",dt,vehicle.cp.mode10.alphaList[closestIndex])
--[[local groundContact = true
for index, wheel in pairs(vehicle.wheels) do
print(" check Wheel "..tostring(index).." hasGroundContact: "..tostring(wheel.hasGroundContact))
if not wheel.hasGroundContact then
groundContact = false
end
end ]]
elseif vehicle.cp.shieldState == "up" then
--make a table of moveAlphas per shield height
local height = courseplay:round(currentHeight-(dy-dty)-diffY,3)
if vehicle.cp.mode10.leveling then
if vehicle.cp.mode10.alphaList[height] == nil then
vehicle.cp.mode10.alphaList[height] = vehicle.cp.workTools[1].attacherJointControl.controls[1].moveAlpha
--print("add "..tostring(height).." to alphaList Alpha: "..tostring(vehicle.cp.mode10.alphaList[height]))
if vehicle.cp.mode10.lowestAlpha > height then
vehicle.cp.mode10.lowestAlpha = height
end
end
else
if vehicle.cp.mode10.tempSchieldHeight == nil then
vehicle.cp.mode10.tempSchieldHeight = currentHeight +0.5
end
if currentHeight < 0.2 then
if currentHeight == vehicle.cp.mode10.tempSchieldHeight then
if vehicle.cp.mode10.alphaList[0] == nil then
vehicle.cp.mode10.alphaList[0] = vehicle.cp.workTools[1].attacherJointControl.controls[1].moveAlpha
vehicle.cp.mode10.tempSchieldHeight = nil
end
else
vehicle.cp.mode10.tempSchieldHeight = currentHeight
end
end
end
return true,false
end
if vehicle.cp.fillLevelGo and hasFillLevel then
vehicle.cp.fillLevelGo = false
end
local emptyTimerIsThrought = courseplay:timerIsThrough(vehicle,'levelerEmpty',false)
if emptyTimerIsThrought then
courseplay:resetCustomTimer(vehicle, 'levelerEmpty',true);
end
--check the target where to go next
if distance2Target < 1 then
vehicle.cp.actualTarget.line = math.min(vehicle.cp.actualTarget.line + 1,#vehicle.cp.BunkerSiloMap)
if vehicle.cp.mode10.bladeOffset == 0 then
vehicle.cp.mode10.bladeOffset = courseplay:round( shouldBHeight-currentHeight,2)
end
if vehicle.cp.actualTarget.line == #vehicle.cp.BunkerSiloMap then
targetUnit = vehicle.cp.BunkerSiloMap[vehicle.cp.actualTarget.line][vehicle.cp.actualTarget.column]
cx ,cz = targetUnit.cx, targetUnit.cz
cy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 1, cz);
if leveled then
distance2Target = courseplay:distanceToPoint(vehicle, cx, cy, cz)
else
distance2Target = courseplay:distance(x,z, cx, cz)
end
end
end
local is2Heigh = (y-ty > 4 and not vehicle.cp.mode10.leveling)
local isAtEnd = (vehicle.cp.actualTarget.line == #vehicle.cp.BunkerSiloMap and distance2Target < math.max((shouldBHeight*1.74),1))--1)
local isBladeEmptyTillNow = (not hasFillLevel and vehicle.cp.fillLevelGo and vehicle.cp.actualTarget.line == #vehicle.cp.BunkerSiloMap and distance2Target < 4 and not leveled)
local fillLevelChanged = vehicle.cp.mode10FillLevel ~= newFillLevel and newFillLevel~= 0 and not vehicle.cp.mode10.leveling
local lastLine = vehicle.cp.mode10.leveling and targetUnit.lastLine and distance2Target < 1 and vehicle.cp.mode10.drivingThroughtLoading and not leveled
if (vehicle.cp.slippingStage > 0 and not vehicle.cp.mode10.isStuck )
or fillLevelChanged
or emptyTimerIsThrought
or is2Heigh
or isBladeEmptyTillNow
--or (vehicle.cp.mode10.jumpsPerRun >= 2 and vehicle.cp.actualTarget.line > 3)
or isAtEnd
or lastLine
then
--print(string.format("dropOut reason: fillLevelChanged(%s);emptyTimerIsThrought(%s);is2Heigh(%s);isBladeEmptyTillNow(%s)",tostring(fillLevelChanged),tostring(emptyTimerIsThrought),tostring(is2Heigh),tostring(isBladeEmptyTillNow)))
if goSave then
courseplay:setWaypointIndex(vehicle, 2);
vehicle.cp.fillLevelGo = true
courseplay:setModeState(vehicle, 2);
else
courseplay:setModeState(vehicle, 2);
vehicle.cp.mode10.isStuck = false
end
--autmatic speed
--[[if vehicle.cp.mode10.automaticSpeed and vehicle.cp.mode10.leveling then
vehicle.cp.speeds.bunkerSilo = 8
if vehicle.cp.mode10.jumpsPerRun >= 1 then
vehicle.cp.speeds.bunkerSilo = math.max(vehicle.cp.speeds.bunkerSilo - 1,3)
elseif vehicle.cp.mode10.jumpsPerRun == 0 and vehicle.cp.actualTarget.line > #vehicle.cp.BunkerSiloMap*0.8 then
vehicle.cp.speeds.bunkerSilo = math.min(vehicle.cp.speeds.bunkerSilo + 1,15)
end
end]]
if is2Heigh then
vehicle.cp.mode10.deadline = math.max(2,vehicle.cp.actualTarget.line-2)
--print(string.format("set deadline to %s ",tostring(vehicle.cp.mode10.deadline)))
elseif not vehicle.cp.mode10.leveling and (isAtEnd or isBladeEmptyTillNow) then
vehicle.cp.mode10.deadline = math.max(2,vehicle.cp.actualTarget.line-1)
--print(string.format("set deadline to %s ",tostring(vehicle.cp.mode10.deadline)))
end
if courseplay:getCustomTimerExists(vehicle, 'levelerEmpty') then
courseplay:resetCustomTimer(vehicle, 'levelerEmpty',true);
end
--vehicle.cp.mode10.jumpsPerRun = 0
vehicle.cp.mode10FillLevel = nil
vehicle.cp.mode10.lastTargetLine = vehicle.cp.actualTarget.line
vehicle.cp.mode10.shieldHeightChanged = false
if vehicle.cp.slippingStage > 0 then
courseplay:resetSlippingTimers(vehicle)
courseplay:setSlippingStage(vehicle, 0);
end
return true,false
else
vehicle.cp.mode10FillLevel = newFillLevel
end
if not vehicle.cp.fillLevelGo and not hasFillLevel and vehicle.cp.actualTarget.line > 2 then
if courseplay:timerIsThrough(vehicle,'levelerEmpty') then
courseplay:setCustomTimer(vehicle, "levelerEmpty", emptyTimer);
end
else
courseplay:resetCustomTimer(vehicle, 'levelerEmpty',true);
end
if vehicle.cp.actualTarget.line >= #vehicle.cp.BunkerSiloMap-1 and distance2Target < 10 then
refSpeed= 6
end
elseif vehicle.cp.modeState == 2 then --pull
courseplay:setShieldTarget(vehicle,"up")
cx,cz = vehicle.Waypoints[vehicle.cp.numWaypoints].cx, vehicle.Waypoints[vehicle.cp.numWaypoints].cz
cy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 1, cz);
if courseplay:distance3D(x,y,z,cx,cy,cz) < 3 then
courseplay:setModeState(vehicle, 1);
vehicle.cp.fillLevelGo = true
vehicle.cp.maxHeight = 0
if vehicle.cp.shouldBHeight < 1 then
vehicle.cp.mode10.bladeOffset = 0
end
courseplay:getActualTarget(vehicle)
--return true, false
end
if vehicle.cp.slippingStage == 1 and vehicle.cp.actualTarget.line > 4 then
if vehicle.cp.actualTarget.column > 1 then
cx, _, cz = localToWorld(vehicle.cp.DirectionNode, 2, 0, 3);
else
cx, _, cz = localToWorld(vehicle.cp.DirectionNode, -2, 0, 3);
end
elseif vehicle.cp.slippingStage > 1 then
vehicle.cp.mode10.isStuck = true
end
else
vehicle.cp.fillLevelGo = true
vehicle.cp.modeState = 1
end
if vehicle.cp.mode10.isStuck then
if math.abs(vehicle.lastSpeedReal*3600) > 2 then
if courseplay:timerIsThrough(vehicle,'stuckTimer',false ) then
vehicle.cp.mode10.isStuck = false
end
if courseplay:timerIsThrough(vehicle,'stuckTimer') then
courseplay:setCustomTimer(vehicle, "stuckTimer", 2);
end
else
courseplay:resetCustomTimer(vehicle, 'stuckTimer',true);
end
local otherColumn = vehicle.cp.actualTarget.column +1
local otherLine = math.min(vehicle.cp.actualTarget.line,vehicle.cp.actualTarget.line+2)
if otherColumn > #vehicle.cp.BunkerSiloMap[1] then
otherColumn = 1
end
local targetUnit = vehicle.cp.BunkerSiloMap[otherLine][otherColumn]
cx ,cz = targetUnit.cx, targetUnit.cz
fwd = false
end
lx,lz = AIVehicleUtil.getDriveDirection(vehicle.cp.DirectionNode, cx, cy, cz);
if math.abs(vehicle.lastSpeedReal) < 0.0001 and not g_currentMission.missionInfo.stopAndGoBraking then
if not fwd then
vehicle.nextMovingDirection = -1
else
vehicle.nextMovingDirection = 1
end;
end;
local steeringAngle = vehicle.cp.steeringAngle;
if vehicle.cp.isFourWheelSteering and vehicle.cp.curSpeed > 20 then
-- We are a four wheel steered vehicle, so dampen the steeringAngle when driving fast, since we turn double as fast as normal and will cause oscillating.
steeringAngle = vehicle.cp.steeringAngle * 2;
end;
lx,lz = AIVehicleUtil.getDriveDirection(vehicle.cp.DirectionNode, cx, cy, cz);
if not fwd then
lx = -lx
lz = -lz
end
if vehicle.isReverseDriving then
lz = -lz
end
--drawDebugLine(x, y, z, 1, 0, 0, cx, cy+5, cz, 1, 1, 0);
courseplay:handleSlipping(vehicle, refSpeed);
AIVehicleUtil.driveInDirection(vehicle, dt, steeringAngle, 1, 0.5, 20, true, fwd, lx, lz, refSpeed, 1);
return false,true;
else
courseplay:setShieldTarget(vehicle,"up")
if vehicle.cp.previousWaypointIndex < 2 and vehicle.cp.slippingStage > 1 then
vehicle.cp.mode10.isStuck = true
end
end --end inBunker
--print(string.format(" return true, allowedToDrive(%s)",tostring(allowedToDrive)))
return true ,allowedToDrive;
end;
function courseplay:setShieldTarget(vehicle,target)
if vehicle.cp.targetShieldState ~= target then
vehicle.cp.targetShieldState = target
end
end
function courseplay:moveShield(vehicle,moveUp,dt,fixAlpha)
local leveler = vehicle.cp.workTools[1]
local move = 0
local factor = 0.8
if moveUp == "up" then
move = -factor
elseif moveUp == "down" then
move = factor
end
if leveler.attacherJointControl ~= nil and leveler.attacherJointControl.controls ~= nil and leveler.attacherJointControl.jointDesc ~= nil then
--for i=1,2 do
local control = leveler.attacherJointControl.controls[1] --[i]; --TODO figure out which attacher is to move
if control.controlActionIndex ~= nil then
local jointDesc = leveler.attacherJointControl.jointDesc;
if fixAlpha then
control.moveAlpha = fixAlpha
else
local moveAlpha = control.moveAlpha + (0.001 * dt * move);
moveAlpha = Utils.clamp(moveAlpha, jointDesc.upperAlpha, jointDesc.lowerAlpha);
if moveAlpha == jointDesc.upperAlpha
or moveAlpha == jointDesc.lowerAlpha
then
return true
end
control.moveAlpha = moveAlpha
end
end
--end
local isDirty = false;
for i=1,2 do
local control = leveler.attacherJointControl.controls[i];
if control.moveAlphaSend ~= control.moveAlpha then
control.moveAlphaSend = control.moveAlpha;
isDirty = true;
end
end
if isDirty then
leveler:raiseDirtyFlags(leveler.attacherJointControl.dirtyFlag);
leveler:updateAttacherJoint(leveler.attacherJointControl.controls[1].moveAlpha, leveler.attacherJointControl.controls[2].moveAlpha);
end
end;
end
function courseplay:getActualTarget(vehicle)
--print(string.format("courseplay:getActualTarget(vehicle) called by %s",tostring(courseplay.utils:getFnCallPath(3))))
local newApproach = vehicle.cp.actualTarget == nil
vehicle.cp.BunkerSiloMap = courseplay:createBunkerSiloMap(vehicle, vehicle.cp.mode9TargetSilo)
if vehicle.cp.BunkerSiloMap ~= nil then
local stopSearching = false
local mostFillLevelAtLine = 0
local mostFillLevelIndex = 2
local fillLevelsPerColumn = {}
local levelingTarget = {}
local fillingTarget = {}
local totalFillLevel = vehicle.cp.mode9TargetSilo.fillLevel
if vehicle.cp.mode10.leveling then
--calculate the height of the shield base on the volume of a truncated pyramid
local compactedFillLevel = vehicle.cp.mode9TargetSilo.compactedFillLevel
local realFillLevel = vehicle.cp.mode10.drivingThroughtLoading and (((totalFillLevel-compactedFillLevel)*1.4)+compactedFillLevel) or totalFillLevel
local bunkerLength = vehicle.cp.BunkerSiloMap[1][1].bunkerLength;
local bunkerWidth = vehicle.cp.BunkerSiloMap[1][1].bunkerWidth;
local totalHeight = 3.5
local elevationWidth = 1.05/totalHeight
local elevationLengthFactor = vehicle.cp.mode10.drivingThroughtLoading and 12 or 6
local elevationLength = elevationLengthFactor/totalHeight
local height = 0
local baseArea = 0
local topArea = 0
local volume = 0
local tuneFactor = 0.75
local area = vehicle.cp.mode9TargetSilo.bunkerSiloArea
courseplay:debug("BunkeSilo: realFillLevel: "..tostring(realFillLevel).." compactRate: "..tostring((compactedFillLevel*100)/totalFillLevel),10)
for i=0.01,totalHeight,0.01 do
local bunkerLengthClean = bunkerLength-2
baseArea = bunkerLengthClean * (bunkerWidth-1)
topArea = ((bunkerWidth-1)+(elevationWidth*i))* (bunkerLengthClean - (elevationLength*i))
volume = i/3* (baseArea + topArea + math.sqrt(baseArea*topArea))
height = i
--print(string.format("try with %s; width %s; length %s; volume %s ",tostring(i),tostring(((bunkerWidth-1)+(elevationWidth*i))),tostring((bunkerLength - (elevationLength*i))),tostring(courseplay:round(volume,1))))
if courseplay:round(volume,1)> realFillLevel/1000 then
--print("break with "..tostring(height))
break
end
end
--print(string.format("height(%s) = (realFillLevel(%s)/1000) / volume(%s))",tostring(height),tostring(realFillLevel),tostring(volume)))
courseplay:setFillLevels(vehicle,height)
--decide which cloumn is the next target and when the bunkersilo is leveled
local leveled = true
local counters = {}
local lastLineInColumn = {}
local nonCountedUnit = #vehicle.cp.BunkerSiloMap
if vehicle.cp.mode10.drivingThroughtLoading then
nonCountedUnit = nonCountedUnit -1
end
for lineIndex, line in pairs(vehicle.cp.BunkerSiloMap) do
local columns = {}
for column, fillUnit in pairs(line) do
fillUnit.averageFillLevel = totalFillLevel / (#vehicle.cp.BunkerSiloMap * #line)
local ratio = (fillUnit.fillLevel*100)/fillUnit.averageFillLevel
fillUnit.leveled = ratio < 105
if counters[column]== nil then
counters[column] = 0
lastLineInColumn[column] = 0
end
if ratio <= 100 then
columns[column] = "[ ]"
elseif ratio <= 150 then
columns[column] = "[0]"
else
columns[column] = "[X]"
if lineIndex < nonCountedUnit then
counters[column] = counters[column]+1
lastLineInColumn[column] = lineIndex
leveled = false
end
end
end
if courseplay.debugChannels[10] then
local printString = ""
for index =1,#columns do
printString = printString..columns[index]
end
print(string.format(" %s",printString))
end
end
if leveled then
courseplay:debug("set Leveled",10)
vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][1].leveled = true
else
vehicle.cp.BunkerSiloMap[#vehicle.cp.BunkerSiloMap][1].leveled = false
end
local fullestColumn = 0
if vehicle.cp.mode10.lastActualTarget then
local allEqual = false
local counterIndex = 0
local counterValue = 0
for i=1,#counters do
if counterValue == counters[i] and counterValue > 0 then
allEqual = true
end
if counterValue < counters[i] then
counterValue = counters[i]
counterIndex = i
end
end
if allEqual or leveled then
local nextColumnIndex = vehicle.cp.mode10.lastActualTarget+1
if nextColumnIndex > #vehicle.cp.BunkerSiloMap[1] then
nextColumnIndex = 1
end
fullestColumn = nextColumnIndex
else
local index = math.min(lastLineInColumn[counterIndex]+1,#vehicle.cp.BunkerSiloMap)
vehicle.cp.BunkerSiloMap[index][counterIndex].lastLine = true
fullestColumn = counterIndex
end
else
vehicle.cp.mode10.lastActualTarget = 1
fullestColumn = 1
end
if newApproach then
fullestColumn = math.ceil(#vehicle.cp.BunkerSiloMap[1]/2)
--print("first run column: "..tostring(fullestColumn).." of "..tostring(#vehicle.cp.BunkerSiloMap[1]))
end
levelingTarget = {
line = 1;
column = fullestColumn;
empty = false;
}
vehicle.cp.mode10.lastActualTarget = fullestColumn
end
-- find column with most fillLevel and figure out whether its empty
for lineIndex, line in pairs(vehicle.cp.BunkerSiloMap) do
if stopSearching then
break
end
mostFillLevelAtLine = 0
for column, fillUnit in pairs(line) do
if mostFillLevelAtLine < fillUnit.fillLevel then
mostFillLevelAtLine = fillUnit.fillLevel
mostFillLevelIndex = column
end
if column == #line and mostFillLevelAtLine > 0 then
fillUnit = line[mostFillLevelIndex]
fillingTarget = {
line = lineIndex;
column = mostFillLevelIndex;
empty = false;
}
stopSearching = true
break
end
end
end
if mostFillLevelAtLine == 0 then
fillingTarget = {
line = 1;
column = 1;
empty = true;
}
end
if vehicle.cp.mode10.leveling and not fillingTarget.empty then
vehicle.cp.actualTarget = levelingTarget
else
vehicle.cp.actualTarget = fillingTarget
end
vehicle.cp.mode10.firstLine = vehicle.cp.actualTarget.line
end
end
function courseplay:setFillLevels(vehicle,forceHeight)
for lineIndex, line in pairs(vehicle.cp.BunkerSiloMap) do
for column, fillUnit in pairs(line) do
if forceHeight ~= nil then
fillUnit.height = forceHeight
end
end
end
end