-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAICombine.lua
833 lines (727 loc) · 28.1 KB
/
AICombine.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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
AICombine = {};
source("dataS/scripts/vehicles/specializations/AICombineSetStartedEvent.lua");
source("dataS/scripts/vehicles/specializations/AISetImplementsMoveDownEvent.lua");
function AICombine.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Hirable, specializations) and Spec ializationUtil.hasSpecialization(Combine, specializations);
end;
function AICombine:load(xmlFile)
self.startAIThreshing = SpecializationUtil.callSpecializationsFunction("startA IThreshing");
self.stopAIThreshing = SpecializationUtil.callSpecializationsFunction("stopAIT hreshing");
self.setAIImplementsMoveDown = SpecializationUtil.callSpecializationsFunction( "setAIImplementsMoveDown");
self.onTrafficCollisionTrigger = AICombine.onTrafficCollisionTrigger;
self.canStartAIThreshing = AICombine.canStartAIThreshing;
self.getIsAIThreshingAllowed = AICombine.getIsAIThreshingAllowed;
self.isAIThreshing = false;
self.aiTreshingDirectionNode = Utils.indexToObject(self.components, getXMLStri ng(xmlFile, "vehicle.aiTreshingDirectionNode#index"));
if self.aiTreshingDirectionNode == nil then
self.aiTreshingDirectionNode = self.components[1].node;
end;
self.lookAheadDistance = 10;
self.turnTimeout = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.turnTimeout"), 200);
self.turnTimeoutLong = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.turnTimeou tLong"), 6000);
self.turnTimer = self.turnTimeout;
self.turnEndDistance = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.turnEndDis tance"), 4);
self.waitForTurnTimeout = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.waitFor TurnTime"), 1500);
self.waitForTurnTime = 0;
self.sideWatchDirOffset = -8;
self.sideWatchDirSize = 8;
self.frontAreaSize = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.frontAreaSiz e#value"), 2);
self.waitingForTrailerToUnload = false;
self.waitingForDischarge = false;
self.waitForDischargeTime = 0;
self.waitForDischargeTimeout = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.wa itForDischargeTime"), 10000);
self.turnStage = 0;
self.aiLeftMarker = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.aiLeftMarker#index"));
self.aiRightMarker = Utils.indexToObject(self.components, getXMLString(xmlFile , "vehicle.aiRightMarker#index"));
self.aiTrafficCollisionTrigger = Utils.indexToObject(self.components, getXMLSt ring(xmlFile, "vehicle.aiTrafficCollisionTrigger#index"));
self.aiTurnThreshWidthScale = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.aiT urnThreshWidthScale#value"), 0.9);
self.aiTurnThreshWidthMaxDifference = Utils.getNoNil(getXMLFloat(xmlFile, "veh icle.aiTurnThreshWidthMaxDifference#value"), 0.6); -- do at most a 0.6m overlap
self.trafficCollisionIgnoreList = {};
for k,v in pairs(self.components) do
self.trafficCollisionIgnoreList[v.node] = true;
end;
self.numCollidingVehicles = {};
self.driveBackTimeout = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.driveBack Timeout"), 1000);
self.driveBackTime = 0;
self.driveBackAfterDischarge = false;
self.dtSum = 0;
self.turnStage1Timeout = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.turnForw ardTimeout"), 20000);
self.turnStage1AngleCosThreshold = math.cos(math.rad(Utils.getNoNil(getXMLFloa t(xmlFile, "vehicle.turnForwardAngleThreshold"), 75)));
self.turnStage2Timeout = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.turnBack wardTimeout"), 20000);
self.turnStage2AngleCosThreshold = math.cos(math.rad(Utils.getNoNil(getXMLFloa t(xmlFile, "vehicle.turnBackwardAngleThreshold"), 30)));
self.turnStage4Timeout = 3000;
self.waitingForWeather = false;
self.aiRescueTimeout = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.aiRescue#t imeout"), 10000);
self.aiRescueTimer = self.aiRescueTimeout;
self.aiRescueForce = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.aiRescue#for ce"), 60);
self.aiRescueSpeedThreshold = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.aiR escue#speedThreshold"), 0.0001);
self.aiRescueNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.aiRescue#index"));
if self.aiRescueNode == nil then
self.aiRescueNode = self.components[1].node;
end;
self.numAttachedTrailers = 0;
--self.debugDirection = loadI3DFile("data/debugDirection.i3d");
--link(self.aiTreshingDirectionNode, self.debugDirection);
--self.debugPosition = loadI3DFile("data/debugPosition.i3d");
--link(self.aiTreshingDirectionNode, self.debugPosition);
end;
function AICombine:delete()
--self:stopAIThreshing();
AICombine.removeCollisionTrigger(self, self);
for _,implement in pairs(self.attachedImplements) do
if implement.object ~= nil then
AICombine.removeCollisionTrigger(self, implement.object);
end
end;
end;
function AICombine:readStream(streamId, connection)
local isAIThreshing = streamReadBool(streamId);
if isAIThreshing then
self:startAIThreshing(true);
else
self:stopAIThreshing(true);
end;
end;
function AICombine:writeStream(streamId, connection)
streamWriteBool(streamId, self.isAIThreshing);
end;
function AICombine:mouseEvent(posX, posY, isDown, isUp, button)
end;
function AICombine:keyEvent(unicode, sym, modifier, isDown)
end;
function AICombine:update(dt)
if self:getIsActiveForInput(false) then
if InputBinding.hasEvent(InputBinding.TOGGLE_AI) then
if g_currentMission:getHasPermission("hireAI") then
if self.isAIThreshing then
self:stopAIThreshing();
else
if self:canStartAIThreshing() then
self:startAIThreshing();
end;
end;
end;
end;
end;
end;
function AICombine:updateTick(dt)
if self.isServer then
if self.isAIThreshing then
if self.isBroken then
self:stopAIThreshing();
end;
self.dtSum = self.dtSum + dt;
if self.dtSum > 50 then
AICombine.updateAIMovement(self, self.dtSum);
self.dtSum = 0;
end;
if self.isAIThreshing then
if (self.grainTankFillLevel > 0 or self.grainTankCapacity <= 0) an d (self.grainTankFillLevel >= self.grainTankCapacity*0.8 or next(self.combineTrailersInRange) ~= nil) then
local pipeState = self:getCombineTrailerInRangePipeState();
if pipeState > 0 then
self:setPipeState(pipeState);
else
self:setPipeState(2);
end;
if next(self.combineTrailersInRange) ~= nil then
self.waitForDischargeTime = self.time + self.waitForDischa rgeTimeout;
end;
if self.grainTankFillLevel >= self.grainTankCapacity and self. grainTankCapacity > 0 then
self.driveBackAfterDischarge = true;
self.waitingForDischarge = true;
self.waitForDischargeTime = self.time + self.waitForDischa rgeTimeout;
end;
else
--no trailer in range and not full
if (self.waitingForDischarge and self.grainTankFillLevel <= 0) or self.waitForDischargeTime <= self.time then
self.waitingForDischarge = false;
if self.driveBackAfterDischarge then
self.driveBackTime = self.time + self.driveBackTimeout ;
self.driveBackAfterDischarge = false;
end;
if next(self.combineTrailersInRange) == nil then
-- only close the pipe if no trailer is in range
self:setPipeState(1);
end;
if self:getIsThreshingAllowed(true) then
self:setIsThreshing(true);
end;
end;
end;
end
else
self.dtSum = 0;
end;
end;
end;
function AICombine:draw()
if g_currentMission:getHasPermission("hireAI") then
if self.isAIThreshing then
g_currentMission:addHelpButtonText(g_i18n:getText("DismissEmployee"), InputBinding.TOGGLE_AI);
else
if self:canStartAIThreshing() then
g_currentMission:addHelpButtonText(g_i18n:getText("HireEmployee"), InputBinding.TOGGLE_AI);
end;
end;
end
end;
function AICombine:startAIThreshing(noEventSend)
if noEventSend == nil or noEventSend == false then
if g_server ~= nil then
g_server:broadcastEvent(AICombineSetStartedEvent:new(self, true), nil, nil, self);
else
g_client:getServerConnection():sendEvent(AICombineSetStartedEvent:new( self, true));
end;
end;
self:hire();
if not self.isAIThreshing then
self.isAIThreshing = true;
if self.isServer then
self.turnTimer = self.turnTimeoutLong;
self.turnStage = 0;
local dx,_,dz = localDirectionToWorld(self.aiTreshingDirectionNode, 0, 0, 1);
if g_currentMission.snapAIDirection then
local snapAngle = self:getDirectionSnapAngle();
snapAngle = math.max(snapAngle, math.pi/(g_currentMission.terrainD etailAngleMaxValue+1));
local angleRad = Utils.getYRotationFromDirection(dx, dz)
angleRad = math.floor(angleRad / snapAngle + 0.5) * snapAngle;
self.aiThreshingDirectionX, self.aiThreshingDirectionZ = Utils.get DirectionFromYRotation(angleRad);
else
local length = Utils.vector2Length(dx,dz);
self.aiThreshingDirectionX = dx/length;
self.aiThreshingDirectionZ = dz/length;
end;
local x,y,z = getWorldTranslation(self.aiTreshingDirectionNode);
self.aiThreshingTargetX = x;
self.aiThreshingTargetZ = z;
AICombine.addCollisionTrigger(self, self);
end;
for _,implement in pairs(self.attachedImplements) do
if implement.object ~= nil then
if implement.object.needsLowering and implement.object.aiNeedsLowe ring then
self:setJointMoveDown(implement.jointDescIndex, true, true)
end;
implement.object:aiTurnOn();
if self.isServer then
AICombine.addCollisionTrigger(self, implement.object);
end;
end;
end;
if self.threshingStartAnimation ~= nil and self.playAnimation ~= nil then
self:playAnimation(self.threshingStartAnimation, self.threshingStartAn imationSpeedScale, nil, true);
end
self.waitingForDischarge = false;
self:setIsThreshing(true, true);
self.checkSpeedLimit = false;
self.waitingForWeather = false;
end;
end;
function AICombine:stopAIThreshing(noEventSend)
if noEventSend == nil or noEventSend == false then
if g_server ~= nil then
g_server:broadcastEvent(AICombineSetStartedEvent:new(self, false), nil , nil, self);
else
g_client:getServerConnection():sendEvent(AICombineSetStartedEvent:new( self, false));
end;
end;
self:dismiss();
if self.isAIThreshing then
self.isAIThreshing = false;
self.allowsThreshing = true;
self.checkSpeedLimit = true;
self.waitingForWeather = false;
self:setIsThreshing(false, true);
if self.isServer then
--restore allowsThreshing flag
self.motor:setSpeedLevel(0, false);
self.motor.maxRpmOverride = nil;
WheelsUtil.updateWheelsPhysics(self, 0, self.lastSpeed, 0, false, self .requiredDriveMode);
AICombine.removeCollisionTrigger(self, self);
end;
for _,implement in pairs(self.attachedImplements) do
if implement.object ~= nil then
if implement.object.needsLowering and implement.object.aiNeedsLowe ring then
self:setJointMoveDown(implement.jointDescIndex, false, true)
end;
AICombine.removeCollisionTrigger(self, implement.object);
implement.object:aiTurnOff();
end
end;
if not self:getIsActive() then
self:onLeave();
end;
end;
end;
function AICombine:onEnter(isControlling)
end;
function AICombine:onLeave()
end;
function AICombine.updateAIMovement(self, dt)
if not self:getIsAIThreshingAllowed() then
self:stopAIThreshing();
return;
end;
if not self.isControlled then
if g_currentMission.environment.needsLights then
self:setLightsVisibility(true);
else
self:setLightsVisibility(false);
end;
end;
local allowedToDrive = true;
if self.grainTankCapacity == 0 then
if not self.pipeStateIsUnloading[self.currentPipeState] then
allowedToDrive = false;
end
if not self.isPipeUnloading and (self.lastArea > 0 or self.lastLostGrainTa nkFillLevel > 0) then
-- there is some fruit to unload, but there is no trailer. Stop and wa it for a trailer
self.waitingForTrailerToUnload = true;
end;
else
if self.grainTankFillLevel >= self.grainTankCapacity then
allowedToDrive = false;
end
end
if self.waitingForTrailerToUnload then
if self.lastValidGrainTankFruitType ~= FruitUtil.FRUITTYPE_UNKNOWN then
local trailer = self:findTrailerToUnload(self.lastValidGrainTankFruitT ype);
if trailer ~= nil then
-- there is a trailer to unload. Continue working
self.waitingForTrailerToUnload = false;
end;
else
-- we did not cut anything yet. We shouldn't have ended in this state. Just continue working
self.waitingForTrailerToUnload = false;
end;
end;
if (self.grainTankFillLevel >= self.grainTankCapacity and self.grainTankCapaci ty > 0) or self.waitingForTrailerToUnload or self.waitingForDischarge then
allowedToDrive = false;
end;
for _,v in pairs(self.numCollidingVehicles) do
if v > 0 then
allowedToDrive = false;
break;
end;
end;
if self.turnStage > 0 then
if self.waitForTurnTime > self.time or (self.pipeIsUnloading and self.turn Stage < 3) then
allowedToDrive = false;
end;
end;
if not self:getIsThreshingAllowed(true) then
allowedToDrive = false;
self:setIsThreshing(false);
self.waitingForWeather = true;
else
if self.waitingForWeather then
if self.turnStage == 0 then
self.driveBackTime = self.time + self.driveBackTimeout;
end;
self:startThreshing();
self.waitingForWeather = false;
end;
end;
if not allowedToDrive then
--local x,y,z = getWorldTranslation(self.aiTreshingDirectionNode);
--local lx, lz = 0, 1; --AIVehicleUtil.getDriveDirection(self.aiTreshingDi rectionNode, self.aiThreshingTargetX, y, self.aiThreshingTargetZ);
--AIVehicleUtil.driveInDirection(self, dt, 30, 0, 0, 28, FALSE, moveForwar) ds, lx, lz)
AIVehicleUtil.driveInDirection(self, dt, 30, 0, 0, 28, false, moveForwards , nil, nil)
return;
end;
local speedLevel = 2;
local leftMarker = self.aiLeftMarker;
local rightMarker = self.aiRightMarker;
local hasFruitPreparer = false;
local fruitType = self.lastValidInputFruitType;
if self.fruitPreparerFruitType ~= nil and self.fruitPreparerFruitType == fruit Type then
hasFruitPreparer = true;
end
for cutter,implement in pairs(self.attachedCutters) do
if cutter.aiLeftMarker ~= nil and leftMarker == nil then
leftMarker = cutter.aiLeftMarker;
end;
if cutter.aiRightMarker ~= nil and rightMarker == nil then
rightMarker = cutter.aiRightMarker;
end;
if Cutter.getUseLowSpeedLimit(cutter) then
speedLevel = 1;
end;
end;
if leftMarker == nil or rightMarker == nil then
self:stopAIThreshing();
return;
end;
if self.driveBackTime >= self.time then
local x,y,z = getWorldTranslation(self.aiTreshingDirectionNode);
local lx, lz = AIVehicleUtil.getDriveDirection(self.aiTreshingDirectionNod e, self.aiThreshingTargetX, y, self.aiThreshingTargetZ);
AIVehicleUtil.driveInDirection(self, dt, 30, 0, 0, 28, true, false, lx, lz , speedLevel, 1)
return;
end;
local hasArea = true;
if self.lastArea < 1 then
local x,y,z = getWorldTranslation(self.aiTreshingDirectionNode);
local dirX, dirZ = self.aiThreshingDirectionX, self.aiThreshingDirectionZ;
local lInX, lInY, lInZ = getWorldTranslation(leftMarker);
local rInX, rInY, rInZ = getWorldTranslation(rightMarker);
local heightX = lInX + dirX * self.frontAreaSize;
local heightZ = lInZ + dirZ * self.frontAreaSize;
local area = Utils.getFruitArea(fruitType, lInX, lInZ, rInX, rInZ, heightX , heightZ, hasFruitPreparer);
if area < 1 then
hasArea = false;
end;
end;
if hasArea then
self.turnTimer = self.turnTimeout;
else
self.turnTimer = self.turnTimer - dt;
end;
local newTargetX, newTargetY, newTargetZ;
local moveForwards = true;
local updateWheels = true;
if self.turnTimer < 0 or self.turnStage > 0 then
if self.turnStage > 0 then
local x,y,z = getWorldTranslation(self.aiTreshingDirectionNode);
local dirX, dirZ = self.aiThreshingDirectionX, self.aiThreshingDirecti onZ;
local myDirX, myDirY, myDirZ = localDirectionToWorld(self.aiTreshingDi rectionNode, 0, 0, 1);
newTargetX = self.aiThreshingTargetX;
newTargetY = y;
newTargetZ = self.aiThreshingTargetZ;
if self.turnStage == 1 then
self.turnStageTimer = self.turnStageTimer - dt;
if self.lastSpeed < self.aiRescueSpeedThreshold then
self.aiRescueTimer = self.aiRescueTimer - dt;
else
self.aiRescueTimer = self.aiRescueTimeout;
end;
if myDirX*dirX + myDirZ*dirZ > self.turnStage1AngleCosThreshold or self.turnStageTimer < 0 or self.aiRescueTimer < 0 then
self.turnStage = 2;
moveForwards = false;
if self.turnStageTimer < 0 or self.aiRescueTimer < 0 then
self.aiThreshingTargetBeforeSaveX = self.aiThreshingTarget X;
self.aiThreshingTargetBeforeSaveZ = self.aiThreshingTarget Z;
newTargetX = self.aiThreshingTargetBeforeTurnX;
newTargetZ = self.aiThreshingTargetBeforeTurnZ;
moveForwards = false;
self.turnStage = 4;
self.turnStageTimer = self.turnStage4Timeout;
else
self.turnStageTimer = self.turnStage2Timeout;
end;
self.aiRescueTimer = self.aiRescueTimeout;
end;
elseif self.turnStage == 2 then
self.turnStageTimer = self.turnStageTimer - dt;
if self.lastSpeed < self.aiRescueSpeedThreshold then
self.aiRescueTimer = self.aiRescueTimer - dt;
else
self.aiRescueTimer = self.aiRescueTimeout;
end;
if myDirX*dirX + myDirZ*dirZ > self.turnStage2AngleCosThreshold or self.turnStageTimer < 0 or self.aiRescueTimer < 0 then
AICombine.switchToTurnStage3(self);
else
moveForwards = false;
end;
elseif self.turnStage == 3 then
--[[if Utils.vector2Length(x-newTargetX, z-newTargetZ) < self.turn EndDistance then
self.turnTimer = self.turnTimeoutLong;
self.turnStage = 0;
--print("turning done");
end;]]
if self.lastSpeed < self.aiRescueSpeedThreshold then
self.aiRescueTimer = self.aiRescueTimer - dt;
else
self.aiRescueTimer = self.aiRescueTimeout;
end;
local dx, dz = x-newTargetX, z-newTargetZ;
local dot = dx*dirX + dz*dirZ;
if -dot < self.turnEndDistance then
self.turnTimer = self.turnTimeoutLong;
self.turnStage = 0;
elseif self.aiRescueTimer < 0 then
self.aiThreshingTargetBeforeSaveX = self.aiThreshingTargetX;
self.aiThreshingTargetBeforeSaveZ = self.aiThreshingTargetZ;
newTargetX = self.aiThreshingTargetBeforeTurnX;
newTargetZ = self.aiThreshingTargetBeforeTurnZ;
moveForwards = false;
self.turnStage = 4;
self.turnStageTimer = self.turnStage4Timeout;
end;
elseif self.turnStage == 4 then
self.turnStageTimer = self.turnStageTimer - dt;
if self.lastSpeed < self.aiRescueSpeedThreshold then
self.aiRescueTimer = self.aiRescueTimer - dt;
else
self.aiRescueTimer = self.aiRescueTimeout;
end;
if self.aiRescueTimer < 0 then
self.aiRescueTimer = self.aiRescueTimeout;
local x,y,z = localDirectionToWorld(self.aiRescueNode, 0, 0, - 1);
local scale = self.aiRescueForce/Utils.vector2Length(x,z);
addForce(self.aiRescueNode, x*scale, 0, z*scale, 0, 0, 0, true );
end;
if self.turnStageTimer < 0 then
self.aiRescueTimer = self.aiRescueTimeout;
self.turnStageTimer = self.turnStage1Timeout;
self.turnStage = 1;
newTargetX = self.aiThreshingTargetBeforeSaveX;
newTargetZ = self.aiThreshingTargetBeforeSaveZ;
else
local dirX, dirZ = -dirX, -dirZ;
-- just drive along direction
local targetX, targetZ = self.aiThreshingTargetX, self.aiThres hingTargetZ;
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-dirX*self.lookAheadDistance;
newTargetZ = projTargetZ-dirZ*self.lookAheadDistance;
moveForwards = false;
end;
end;
elseif fruitType == FruitUtil.FRUITTYPE_UNKNOWN then
self:stopAIThreshing();
return;
else
-- turn
local x,y,z = getWorldTranslation(self.aiTreshingDirectionNode);
local dirX, dirZ = self.aiThreshingDirectionX, self.aiThreshingDirecti onZ;
local sideX, sideZ = -dirZ, dirX;
local lInX, lInY, lInZ = getWorldTranslation(leftMarker);
local rInX, rInY, rInZ = getWorldTranslation(rightMarker);
local threshWidth = Utils.vector2Length(lInX-rInX, lInZ-rInZ);
local turnLeft = true;
local lWidthX = x - sideX*0.5*threshWidth + dirX * self.sideWatchDirOf fset;
local lWidthZ = z - sideZ*0.5*threshWidth + dirZ * self.sideWatchDirOf fset;
local lStartX = lWidthX - sideX*0.7*threshWidth;
local lStartZ = lWidthZ - sideZ*0.7*threshWidth;
local lHeightX = lStartX + dirX*self.sideWatchDirSize;
local lHeightZ = lStartZ + dirZ*self.sideWatchDirSize;
local rWidthX = x + sideX*0.5*threshWidth + dirX * self.sideWatchDirOf fset;
local rWidthZ = z + sideZ*0.5*threshWidth + dirZ * self.sideWatchDirOf fset;
local rStartX = rWidthX + sideX*0.7*threshWidth;
local rStartZ = rWidthZ + sideZ*0.7*threshWidth;
local rHeightX = rStartX + dirX*self.sideWatchDirSize;
local rHeightZ = rStartZ + dirZ*self.sideWatchDirSize;
local leftFruit = Utils.getFruitArea(fruitType, lStartX, lStartZ, lWid thX, lWidthZ, lHeightX, lHeightZ, hasFruitPreparer);
local rightFruit = Utils.getFruitArea(fruitType, rStartX, rStartZ, rWi dthX, rWidthZ, rHeightX, rHeightZ, hasFruitPreparer);
-- turn to where more fruit is to cut
if leftFruit > 0 or rightFruit > 0 then
if leftFruit > rightFruit then
turnLeft = true;
else
turnLeft = false;
end
else
self:stopAIThreshing();
return;
end;
local targetX, targetZ = self.aiThreshingTargetX, self.aiThreshingTarg etZ;
--local dx, dz = x-targetX, z-targetZ;
--local dot = dx*dirX + dz*dirZ;
--local x, z = targetX + dirX*dot, targetZ + dirZ*dot;
--threshWidth = threshWidth*self.aiTurnThreshWidthScale;
local markerSideOffset;
if turnLeft then
markerSideOffset, _, _ = worldToLocal(self.aiTreshingDirectionNode , lInX, lInY, lInZ);
else
markerSideOffset, _, _ = worldToLocal(self.aiTreshingDirectionNode , rInX, rInY, rInZ);
end
markerSideOffset = 2*markerSideOffset;
local areaOverlap = math.min(threshWidth*(1-self.aiTurnThreshWidthScal e), self.aiTurnThreshWidthMaxDifference);
if markerSideOffset > 0 then
markerSideOffset = math.max(markerSideOffset - areaOverlap, 0.01);
else
markerSideOffset = math.min(markerSideOffset + areaOverlap, -0.01) ;
end
local x,z = Utils.projectOnLine(x, z, targetX, targetZ, dirX, dirZ)
newTargetX = x-sideX*markerSideOffset;
newTargetY = y;
newTargetZ = z-sideZ*markerSideOffset;
self.aiThreshingDirectionX = -dirX;
self.aiThreshingDirectionZ = -dirZ;
self.turnStage = 1;
self.aiRescueTimer = self.aiRescueTimeout;
self.turnStageTimer = self.turnStage1Timeout;
self.aiThreshingTargetBeforeTurnX = self.aiThreshingTargetX;
self.aiThreshingTargetBeforeTurnZ = self.aiThreshingTargetZ;
self.waitForTurnTime = self.time + self.waitForTurnTimeout;
self:setAIImplementsMoveDown(false);
-- do not thresh while turning
self.allowsThreshing = false;
updateWheels = false;
if turnLeft then
--print("turning left ", threshWidth);
else
--print("turning right ", threshWidth);
end;
end;
else
local x,y,z = getWorldTranslation(self.aiTreshingDirectionNode);
local dirX, dirZ = self.aiThreshingDirectionX, self.aiThreshingDirectionZ;
-- just drive along direction
local targetX, targetZ = self.aiThreshingTargetX, self.aiThreshingTargetZ;
local dx, dz = x-targetX, z-targetZ;
local dot = dx*dirX + dz*dirZ;
local projTargetX = targetX +dirX*dot;
local projTargetZ = targetZ +dirZ*dot;
--print("old target: "..targetX.." ".. targetZ .. " distOnDir " .. dot.." proj: "..projTargetX.." "..projTargetZ);
newTargetX = projTargetX+self.aiThreshingDirectionX*self.lookAheadDistance ;
newTargetY = y;
newTargetZ = projTargetZ+self.aiThreshingDirectionZ*self.lookAheadDistance ;
--print(distOnDir.." target: "..newTargetX.." ".. newTargetZ);
end;
if updateWheels then
local lx, lz = AIVehicleUtil.getDriveDirection(self.aiTreshingDirectionNod e, newTargetX, newTargetY, newTargetZ);
if self.turnStage == 2 and math.abs(lx) < 0.1 then
AICombine.switchToTurnStage3(self);
moveForwards = true;
end;
AIVehicleUtil.driveInDirection(self, dt, 25, 0.5, 0.5, 20, true, moveForwa rds, lx, lz, speedLevel, 0.9);
--local maxAngle = 0.785398163; --45;
local maxlx = 0.7071067; --math.sin(maxAngle);
local colDirX = lx;
local colDirZ = lz;
if colDirX > maxlx then
colDirX = maxlx;
colDirZ = 0.7071067; --math.cos(maxAngle);
elseif colDirX < -maxlx then
colDirX = -maxlx;
colDirZ = 0.7071067; --math.cos(maxAngle);
end;
for triggerId,_ in pairs(self.numCollidingVehicles) do
AIVehicleUtil.setCollisionDirection(self.aiTreshingDirectionNode, trig gerId, colDirX, colDirZ);
end;
end;
self.aiThreshingTargetX = newTargetX;
self.aiThreshingTargetZ = newTargetZ;
end;
function AICombine.switchToDirection(self, myDirX, myDirZ)
self.aiThreshingDirectionX = myDirX;
self.aiThreshingDirectionZ = myDirZ;
--print("switch to direction");
end;
function AICombine:setAIImplementsMoveDown(moveDown)
if self.isServer then
g_server:broadcastEvent(AISetImplementsMoveDownEvent:new(self, moveDown) , nil, nil, self);
end;
for _,implement in pairs(self.attachedImplements) do
if implement.object ~= nil then
if implement.object.needsLowering and implement.object.aiNeedsLowering then
self:setJointMoveDown(implement.jointDescIndex, moveDown, true);
end;
if moveDown then
implement.object:aiLower();
else
implement.object:aiRaise();
end
end
end;
if self.threshingStartAnimation ~= nil and self.playAnimation ~= nil then
if moveDown then
self:playAnimation(self.threshingStartAnimation, self.threshingStartAn imationSpeedScale, nil, true);
else
self:playAnimation(self.threshingStartAnimation, -self.threshingStartA nimationSpeedScale, nil, true);
end
end
end;
function AICombine.addCollisionTrigger(self, object)
if self.isServer then
if object.aiTrafficCollisionTrigger ~= nil then
addTrigger(object.aiTrafficCollisionTrigger, "onTrafficCollisionTrigge r", self);
self.numCollidingVehicles[object.aiTrafficCollisionTrigger] = 0;
end
if object ~= self then
for _,v in pairs(object.components) do
self.trafficCollisionIgnoreList[v.node] = true;
end
end
end
end
function AICombine:removeCollisionTrigger(object)
if self.isServer then
if object.aiTrafficCollisionTrigger ~= nil then
removeTrigger(object.aiTrafficCollisionTrigger);
self.numCollidingVehicles[object.aiTrafficCollisionTrigger] = nil;
end
if object ~= self then
for _,v in pairs(object.components) do
self.trafficCollisionIgnoreList[v.node] = nil;
end
end
end
end
function AICombine:attachImplement(implement)
local object = implement.object;
if object.attacherJoint.jointType == Vehicle.JOINTTYPE_CUTTER then
if self.isAIThreshing and self.isServer then
AICombine.addCollisionTrigger(self, object);
end;
elseif object.attacherJoint.jointType == Vehicle.JOINTTYPE_TRAILER or object.a ttacherJoint.jointType == Vehicle.JOINTTYPE_TRAILERLOW then
self.numAttachedTrailers = self.numAttachedTrailers+1;
end;
end;
function AICombine:detachImplement(implementIndex)
local object = self.attachedImplements[implementIndex].object;
if object ~= nil then
if object.attacherJoint.jointType == Vehicle.JOINTTYPE_CUTTER then
if self.isAIThreshing and self.isServer then
AICombine.removeCollisionTrigger(self, object);
end;
elseif object.attacherJoint.jointType == Vehicle.JOINTTYPE_TRAILER or obje ct.attacherJoint.jointType == Vehicle.JOINTTYPE_TRAILERLOW then
self.numAttachedTrailers = self.numAttachedTrailers-1;
end;
end
end;
function AICombine:onTrafficCollisionTrigger(triggerId, otherId, onEnter, onLeave, onStay, otherShapeId)
if onEnter or onLeave then
if g_currentMission.players[otherId] ~= nil then
if onEnter then
self.numCollidingVehicles[triggerId] = self.numCollidingVehicles[t riggerId]+1;
elseif onLeave then
self.numCollidingVehicles[triggerId] = math.max(self.numCollidingV ehicles[triggerId]-1, 0);
end;
else
local vehicle = g_currentMission.nodeToVehicle[otherId];
if vehicle ~= nil and self.trafficCollisionIgnoreList[otherId] == nil then
if onEnter then
self.numCollidingVehicles[triggerId] = self.numCollidingVehicl es[triggerId]+1;
elseif onLeave then
self.numCollidingVehicles[triggerId] = math.max(self.numCollid ingVehicles[triggerId]-1, 0);
end;
end;
end;
end;
end;
function AICombine.switchToTurnStage3(self)
self.turnStage = 3;
self:setAIImplementsMoveDown(true);
self.allowsThreshing = true;
self.aiRescueTimer = self.aiRescueTimeout;
end;
function AICombine:canStartAIThreshing()
if g_currentMission.disableCombineAI then
return false;
end;
if not self:getIsStartThreshingAllowed() then
return false;
end
if self.numAttachedTrailers > 0 then
return false;
end;
if Hirable.numHirablesHired >= g_currentMission.maxNumHirables then
return false;
end;
return true;
end;
function AICombine:getIsAIThreshingAllowed()
if g_currentMission.disableCombineAI then
return false;
end;
if not self:getIsStartThreshingAllowed() then
return false;
end
if self.numAttachedTrailers > 0 then
return false;
end;
return true;
end;