forked from Dukefarming/FS25-lua-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleDebug.lua
More file actions
2621 lines (2124 loc) · 119 KB
/
VehicleDebug.lua
File metadata and controls
2621 lines (2124 loc) · 119 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
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
function VehicleDebug.setState(state)
if VehicleDebug.state == 0 then
VehicleDebug.debugActionEvents = {}
for i=1, VehicleDebug.NUM_STATES do
local _, actionEventId = g_inputBinding:registerActionEvent(InputAction["DEBUG_VEHICLE_"..i], VehicleDebug, VehicleDebug.debugActionCallback, false, true, false, true, i)
g_inputBinding:setActionEventTextVisibility(actionEventId, false)
table.insert(VehicleDebug.debugActionEvents, actionEventId)
end
elseif state == 0 then
for i=1, #VehicleDebug.debugActionEvents do
g_inputBinding:removeActionEvent(VehicleDebug.debugActionEvents[i])
end
end
if state == VehicleDebug.DEBUG_ATTACHER_JOINTS then
if VehicleDebug.attacherJointUpperEventId == nil and VehicleDebug.attacherJointLowerEventId == nil then
local _, upperEventId = g_inputBinding:registerActionEvent(InputAction.AXIS_FRONTLOADER_ARM, VehicleDebug, VehicleDebug.moveUpperRotation, false, false, true, true)
g_inputBinding:setActionEventTextVisibility(upperEventId, false)
VehicleDebug.attacherJointUpperEventId = upperEventId
local _, lowerEventId = g_inputBinding:registerActionEvent(InputAction.AXIS_FRONTLOADER_TOOL, VehicleDebug, VehicleDebug.moveLowerRotation, false, false, true, true)
g_inputBinding:setActionEventTextVisibility(lowerEventId, false)
VehicleDebug.attacherJointLowerEventId = lowerEventId
end
else
g_inputBinding:removeActionEvent(VehicleDebug.attacherJointUpperEventId)
g_inputBinding:removeActionEvent(VehicleDebug.attacherJointLowerEventId)
VehicleDebug.attacherJointUpperEventId = nil
VehicleDebug.attacherJointLowerEventId = nil
end
if state == VehicleDebug.DEBUG_AI and g_currentMission ~= nil then
for _, vehicle in pairs(g_currentMission.vehicleSystem.vehicles) do
if vehicle.spec_aiDrivable ~= nil and vehicle:getIsActiveForInput(true, true) then
if vehicle.spec_aiDrivable.agentId ~= nil then
enableVehicleNavigationAgentDebugRendering(vehicle.spec_aiDrivable.agentId, true)
end
end
end
end
local wheelMaskUpdated = false
if state == VehicleDebug.DEBUG_TUNING then
-- disable displacement collision while tuning mode is active
-- to get smooth motor values to set up the tools properly
WheelPhysics.COLLISION_MASK = CollisionMask.ALL - CollisionFlag.TERRAIN_DISPLACEMENT
wheelMaskUpdated = true
elseif VehicleDebug.state == VehicleDebug.DEBUG_TUNING then
if WheelPhysics.COLLISION_MASK ~= CollisionMask.ALL then
WheelPhysics.COLLISION_MASK = CollisionMask.ALL
wheelMaskUpdated = true
end
end
if wheelMaskUpdated then
for _, vehicle in pairs(g_currentMission.vehicleSystem.vehicles) do
if vehicle.getWheels ~= nil then
for i, wheel in ipairs(vehicle:getWheels()) do
wheel.physics:updateBase()
end
end
end
end
local ret = false
if VehicleDebug.state == state then
VehicleDebug.state = 0
else
VehicleDebug.state = state
ret = true
end
if g_currentMission ~= nil then
for _, vehicle in pairs(g_currentMission.vehicleSystem.vehicles) do
vehicle:updateSelectableObjects()
vehicle:updateActionEvents()
vehicle:setSelectedVehicle(vehicle)
end
end
return ret
end
---
function VehicleDebug.delete(self)
if self.isServer then
local motorSpec = self.spec_motorized
if motorSpec ~= nil then
local motor = motorSpec.motor
if motor ~= nil then
if motor.debugCurveOverlay ~= nil then
delete(motor.debugCurveOverlay)
end
if motor.debugTorqueGraph ~= nil then
motor.debugTorqueGraph:delete()
end
if motor.debugPowerGraph ~= nil then
motor.debugPowerGraph:delete()
end
if motor.debugGraphs ~= nil then
for _, graph in ipairs(motor.debugGraphs) do
graph:delete()
end
end
if motor.debugLoadGraph ~= nil then
motor.debugLoadGraph:delete()
end
if motor.debugLoadGraphSmooth ~= nil then
motor.debugLoadGraphSmooth:delete()
end
if motor.debugLoadGraphSound ~= nil then
motor.debugLoadGraphSound:delete()
end
if motor.debugRPMGraphSmooth ~= nil then
motor.debugRPMGraphSmooth:delete()
end
if motor.debugRPMGraphSound ~= nil then
motor.debugRPMGraphSound:delete()
end
if motor.debugRPMGraph ~= nil then
motor.debugRPMGraph:delete()
end
if motor.debugAccelerationGraph ~= nil then
motor.debugAccelerationGraph:delete()
end
end
end
end
end
---
function VehicleDebug.debugActionCallback(self, actionName, inputValue, callbackState, isAnalog)
if VehicleDebug.state ~= callbackState then
VehicleDebug.setState(callbackState)
log(string.format("VehicleDebug set to '%s'", VehicleDebug.STATE_NAMES[VehicleDebug.state]))
end
end
---
function VehicleDebug.updateDebug(vehicle, dt)
if VehicleDebug.state == VehicleDebug.DEBUG_ATTRIBUTES then
VehicleDebug.drawDebugAttributeRendering(vehicle)
elseif VehicleDebug.state == VehicleDebug.DEBUG_ATTACHER_JOINTS then
VehicleDebug.drawDebugAttacherJoints(vehicle)
elseif VehicleDebug.state == VehicleDebug.DEBUG_AI then
VehicleDebug.drawDebugAIRendering(vehicle)
elseif VehicleDebug.state == VehicleDebug.DEBUG_TUNING then
VehicleDebug.updateTuningDebugRendering(vehicle, dt)
end
if VehicleDebug.state == VehicleDebug.DEBUG then
if vehicle:getIsActiveForInput() or vehicle.rootVehicle ~= g_localPlayer:getCurrentVehicle() then
VehicleDebug.drawDebugValues(vehicle)
end
end
end
---
function VehicleDebug.drawDebug(vehicle)
if vehicle.getIsEntered ~= nil and vehicle:getIsEntered() then
local v = vehicle:getSelectedVehicle()
if v == nil then
v = vehicle
end
if VehicleDebug.state == VehicleDebug.DEBUG_PHYSICS then
VehicleDebug.drawDebugRendering(v)
elseif VehicleDebug.state == VehicleDebug.DEBUG_SOUNDS then
VehicleDebug.drawSoundDebugValues(v)
elseif VehicleDebug.state == VehicleDebug.DEBUG_ANIMATIONS then
VehicleDebug.drawAnimationDebug(v)
elseif VehicleDebug.state == VehicleDebug.DEBUG_TRANSMISSION then
VehicleDebug.drawTransmissionDebug(v)
elseif VehicleDebug.state == VehicleDebug.DEBUG_TUNING then
VehicleDebug.drawTuningDebug(v)
end
if VehicleDebug.state > 0 then
setTextAlignment(RenderText.ALIGN_CENTER)
for i=1, VehicleDebug.NUM_STATES do
local partSize = 1 / (VehicleDebug.NUM_STATES + 1)
local x = partSize * i
if VehicleDebug.state == i then
setTextColor(0, 1, 0, 1)
renderText(x, 0.01, 0.03, string.format("%s", VehicleDebug.STATE_NAMES[i]))
else
setTextColor(1, 1, 0, 1)
renderText(x, 0.01, 0.015, string.format("SHIFT + %d: '%s'", i, VehicleDebug.STATE_NAMES[i]))
end
end
setTextColor(1,1,1,1)
setTextAlignment(RenderText.ALIGN_LEFT)
end
end
end
---
function VehicleDebug.registerActionEvents(vehicle)
if vehicle.getIsEntered ~= nil and vehicle:getIsEntered() then
if VehicleDebug.state == VehicleDebug.DEBUG_ANIMATIONS then
vehicle:addActionEvent(vehicle.actionEvents, InputAction.DEBUG_PLAYER_ENABLE, vehicle, function() VehicleDebug.selectedAnimation = VehicleDebug.selectedAnimation + 1 end, false, true, false, true, nil)
end
end
if VehicleDebug.state > 0 then
if VehicleDebug.debugActionEvents ~= nil then
for i=1, #VehicleDebug.debugActionEvents do
g_inputBinding:removeActionEvent(VehicleDebug.debugActionEvents[i])
end
end
VehicleDebug.debugActionEvents = {}
for i=1, 9 do
local _, actionEventId = g_inputBinding:registerActionEvent(InputAction["DEBUG_VEHICLE_"..i], VehicleDebug, VehicleDebug.debugActionCallback, false, true, false, true, i)
g_inputBinding:setActionEventTextVisibility(actionEventId, false)
table.insert(VehicleDebug.debugActionEvents, actionEventId)
end
end
end
---
function VehicleDebug.drawBaseDebugRendering(self, x, y)
local vx,_,vz = getWorldTranslation(self.components[1].node)
local fieldOwned = g_farmlandManager:getIsOwnedByFarmAtWorldPosition(g_currentMission:getFarmId(), vx, vz)
local str1, str2, str3, str4 = "", "", "", ""
local motorSpec = self.spec_motorized
local diffSpeed = nil
if motorSpec ~= nil then
local motor = motorSpec.motor
local torque = motor:getMotorAvailableTorque() -- kNm
local neededPtoTorque = motor:getMotorExternalTorque()
local motorPower = motor:getMotorRotSpeed()* (torque - neededPtoTorque)*1000
str1 = str1.."motor:\n" ; str2 = str2..string.format("%1.2frpm\n", motor:getNonClampedMotorRpm())
str1 = str1.."clutch:\n" ; str2 = str2..string.format("%1.2frpm\n", motor:getClutchRotSpeed()*30/math.pi)
str1 = str1.."available power:\n"; str2 = str2..string.format("%1.2fhp %1.2fkW\n", motorPower/735.49875, motorPower/1000) -- motor power reduced by current consumed pto power
str1 = str1.."gear:\n" ; str2 = str2..string.format("%d %d (%d, %1.2f)\n", motor.gear, motor.targetGear * motor.currentDirection, motor.activeGearGroupIndex or 0, motor:getGearRatio())
str1 = str1.."motor load:\n" ; str2 = str2..string.format("%1.2fkN %1.2fkN\n", torque, motor:getMotorAppliedTorque())
local ptoPower = motor:getNonClampedMotorRpm()*math.pi/30 * neededPtoTorque
local ptoLoad = neededPtoTorque / motor:getPeakTorque()
str3 = str3.."pto load:\n" ; str4 = str4..string.format("%.2f%% %.2fhp %.2fkW %1.2fkN\n", ptoLoad*100, ptoPower*1.359621, ptoPower, neededPtoTorque)
str3 = str3.."motor load:\n" ; str4 = str4..string.format("%.2f%%\n", motorSpec.smoothedLoadPercentage*100)
str3 = str3.."motor rpm for sounds:\n" ; str4 = str4..string.format("%drpm\n", motor:getLastMotorRpm())
str3 = str3.."brakeForce:\n" ; str4 = str4..string.format("%.2f (max. %.2f)\n", (self.spec_wheels or {brakePedal=0}).brakePedal, self:getBrakeForce() * 0.5)
local fuelFillUnitIndex = self:getConsumerFillUnitIndex(FillType.DIESEL) or self:getConsumerFillUnitIndex(FillType.ELECTRICCHARGE) or self:getConsumerFillUnitIndex(FillType.METHANE)
if fuelFillUnitIndex ~= nil then
local fillLevel = self:getFillUnitFillLevel(fuelFillUnitIndex)
local fillType = self:getFillUnitFillType(fuelFillUnitIndex)
local unit = fillType == FillType.ELECTRICCHARGE and "kw" or (fillType == FillType.METHANE and "kg" or "l")
str3 = str3..string.format("%s:\n", g_fillTypeManager:getFillTypeNameByIndex(fillType)) ; str4 = str4..string.format("%.2f%s/h (%.2f%s)\n", motorSpec.lastFuelUsage, unit, fillLevel, unit)
end
local defFillUnitIndex = self:getConsumerFillUnitIndex(FillType.DEF)
if defFillUnitIndex ~= nil then
local fillLevel = self:getFillUnitFillLevel(defFillUnitIndex)
str3 = str3.."DEF:\n" ; str4 = str4..string.format("%.2fl/h (%.2fl)\n", motorSpec.lastDefUsage, fillLevel)
end
local airFillUnitIndex = self:getConsumerFillUnitIndex(FillType.AIR)
if airFillUnitIndex ~= nil then
local fillLevel = self:getFillUnitFillLevel(airFillUnitIndex)
str3 = str3.."AIR:\n" ; str4 = str4..string.format("%.2fl/sec (%.2fl)\n", motorSpec.lastAirUsage, fillLevel)
end
diffSpeed = motor.differentialRotSpeed * 3.6
end
str1 = str1.."vel acc[m/s2]:\n" ; str2 = str2..string.format("%1.4f\n", self.lastSpeedAcceleration*1000*1000)
if diffSpeed ~= nil then
str1 = str1.."vel[km/h]:\n" ; str2 = str2..string.format("%1.3f\n", self:getLastSpeed())
local lastSpeedReal = self.lastSpeedReal * 3600
local slip = 0
if diffSpeed > 0.01 and lastSpeedReal > 0.01 then
slip = (diffSpeed / lastSpeedReal - 1) * 100
end
str1 = str1.."differential[km/h]:\n" ; str2 = str2..string.format("%1.3f (slip: %d%%)\n", diffSpeed, slip)
else
str1 = str1.."vel[km/h]:\n" ; str2 = str2..string.format("%1.3f\n", self:getLastSpeed())
end
str1 = str1.."field owned:\n" ; str2 = str2..tostring(fieldOwned).."\n"
str1 = str1.."mass:\n" ; str2 = str2..string.format("%1.1fkg\n", self:getTotalMass(true)*1000)
str1 = str1.."mass incl. attach:\n" ; str2 = str2..string.format("%1.1fkg\n", self:getTotalMass()*1000)
if self.spec_attachable ~= nil then
local brakePedal = 0
if self.spec_wheels ~= nil then
brakePedal = self.spec_wheels.brakePedal
end
local force = self:getBrakeForce() / 10
str1 = str1.."brakeForce:\n" ; str2 = str2..string.format("%1.2f / %1.2f\n", force*brakePedal, force)
end
local textSize = getCorrectTextSize(0.02)
Utils.renderMultiColumnText(x, y, textSize, {str1,str2}, 0.008, {RenderText.ALIGN_RIGHT,RenderText.ALIGN_LEFT})
Utils.renderMultiColumnText(x + 0.22, y, textSize, {str3,str4}, 0.008, {RenderText.ALIGN_RIGHT,RenderText.ALIGN_LEFT})
return getTextHeight(textSize, str1), getTextHeight(textSize, str3)
end
---
function VehicleDebug.drawWheelInfoRendering(self, x, y)
if self.isServer then
local specWheels = self.spec_wheels
if specWheels ~= nil and #specWheels.wheels > 0 then
local debugTable = WheelDebug.getDebugValueHeader()
for i, wheel in ipairs(specWheels.wheels) do
wheel.debug:fillDebugValues(debugTable)
-- draw wheel indices and driveNodes for easier identification if more than 4
if #specWheels.wheels > 4 and DebugUtil.isNodeInCameraRange(wheel.repr, 30) then
local wx,wy,wz = getWorldTranslation(wheel.repr)
Utils.renderTextAtWorldPosition(wx,wy,wz, string.format("%d\n%s", i, getName(wheel.driveNode or wheel.linkNode)), getCorrectTextSize(0.008))
end
end
local textSize = getCorrectTextSize(0.02)
Utils.renderMultiColumnText(x, y, textSize, debugTable, 0.008, {RenderText.ALIGN_RIGHT, RenderText.ALIGN_LEFT})
return getTextHeight(textSize, debugTable[1])
end
end
return 0
end
---
function VehicleDebug.drawWheelSlipGraphs(self)
if self.isServer then
local specWheels = self.spec_wheels
if specWheels ~= nil then
for i, wheel in ipairs(specWheels.wheels) do
wheel.debug:drawSlipGraphs()
end
end
end
end
---
function VehicleDebug.drawDifferentialInfoRendering(self, x, y)
local motorSpec = self.spec_motorized
if motorSpec ~= nil and motorSpec.differentials ~= nil then
local getSpeedsOfDifferential
getSpeedsOfDifferential = function(diff)
local specWheels = self.spec_wheels
local speed1, speed2
if diff.diffIndex1IsWheel then
local wheel = specWheels.wheels[diff.diffIndex1]
speed1 = 0
if wheel.physics.wheelShapeCreated then
speed1 = getWheelShapeAxleSpeed(wheel.node, wheel.physics.wheelShape) * wheel.physics.radius
end
else
local s1,s2 = getSpeedsOfDifferential(motorSpec.differentials[diff.diffIndex1+1])
speed1 = (s1+s2)/2
end
if diff.diffIndex2IsWheel then
local wheel = specWheels.wheels[diff.diffIndex2]
speed2 = 0
if wheel.physics.wheelShapeCreated then
speed2 = getWheelShapeAxleSpeed(wheel.node, wheel.physics.wheelShape) * wheel.physics.radius
end
else
local s1,s2 = getSpeedsOfDifferential(motorSpec.differentials[diff.diffIndex2+1])
speed2 = (s1+s2)/2
end
return speed1,speed2
end
local getRatioOfDifferential = function(speed1, speed2)
-- Note: this is only correct if both rpm values have the same sign
local ratio = math.max(math.abs(speed1),math.abs(speed2)) / math.max(math.min(math.abs(speed1),math.abs(speed2)), 0.001)
return ratio
end
local diffStrs = {"\n", "torqueRatio\n", "maxSpeedRatio\n", "actualSpeedRatio\n" }
for i,diff in pairs(motorSpec.differentials) do
diffStrs[1] = diffStrs[1]..string.format("%d:\n", i)
diffStrs[2] = diffStrs[2]..string.format("%2.2f\n", diff.torqueRatio)
diffStrs[3] = diffStrs[3]..string.format("%2.2f\n", diff.maxSpeedRatio)
local speed1, speed2 = getSpeedsOfDifferential(diff)
local ratio = getRatioOfDifferential(speed1, speed2)
diffStrs[4] = diffStrs[4]..string.format("%2.2f\n", ratio)
end
Utils.renderMultiColumnText(x, y, getCorrectTextSize(0.02), diffStrs, 0.008, {RenderText.ALIGN_RIGHT,RenderText.ALIGN_LEFT})
end
end
---
function VehicleDebug.drawMotorGraphs(self, x, y, sizeX, sizeY, horizontal)
if self.isServer then
local motorSpec = self.spec_motorized
if motorSpec ~= nil then
local motor = motorSpec.motor
local curveOverlay = motor.debugCurveOverlay
if curveOverlay == nil then
curveOverlay = createImageOverlay("dataS/menu/base/graph_pixel.png")
setOverlayColor(curveOverlay, 0, 1, 0, 0.2)
motor.debugCurveOverlay = curveOverlay
end
local torqueCurve = motor:getTorqueCurve()
local numTorqueValues = #torqueCurve.keyframes
local minRpm = math.min(motor:getMinRpm(), torqueCurve.keyframes[1].time)
local maxRpm = math.max(motor:getMaxRpm(), torqueCurve.keyframes[numTorqueValues].time)
local torqueGraph = motor.debugTorqueGraph
local powerGraph = motor.debugPowerGraph
if torqueGraph == nil then
local numValues = numTorqueValues * 32
torqueGraph = Graph.new(numValues, x, y, sizeX, sizeY, 0, 0.0001, true, "kN", Graph.STYLE_LINES)
torqueGraph:setColor(1, 1, 1, 1)
motor.debugTorqueGraph = torqueGraph
powerGraph = Graph.new(numValues, x, y, sizeX, sizeY, 0, 0.0001, false, "", Graph.STYLE_LINES)
powerGraph:setColor(1, 0, 0, 1)
motor.debugPowerGraph = powerGraph
torqueGraph.maxValue = 0.01
powerGraph.maxValue = 0.01
for s=1, numValues do
local rpm = (s-1)/(numValues-1) * (torqueCurve.keyframes[numTorqueValues].time - torqueCurve.keyframes[1].time) + torqueCurve.keyframes[1].time
local torque = motor:getTorqueCurveValue(rpm)
local power = torque*1000 * rpm*math.pi/30
local hpPower = power/735.49875
local posX = (rpm - minRpm) / (maxRpm-minRpm)
torqueGraph:setValue(s, torque)
torqueGraph.maxValue = math.max(torqueGraph.maxValue, torque)
torqueGraph:setXPosition(s, posX)
powerGraph:setValue(s, hpPower)
powerGraph.maxValue = math.max(powerGraph.maxValue, hpPower)
powerGraph:setXPosition(s, posX)
end
else
torqueGraph.left, torqueGraph.bottom, torqueGraph.width, torqueGraph.height = x, y, sizeX, sizeY
powerGraph.left, powerGraph.bottom, powerGraph.width, powerGraph.height = x, y, sizeX, sizeY
end
torqueGraph:draw()
powerGraph:draw()
renderOverlay(curveOverlay, x, y, sizeX*math.clamp((motor:getNonClampedMotorRpm()-minRpm)/(maxRpm-minRpm), 0, 1), sizeY)
if horizontal then
x = x + sizeX + 0.013
else
y = y - sizeY - 0.013
end
local maxSpeed = motor:getMaximumForwardSpeed()
local debugGraphs = motor.debugGraphs
if debugGraphs == nil then
local numVelocityValues = 20
local numGears = 1
local gears = motor.forwardGears
if motor.currentDirection < 0 then
gears = motor.backwardGears or gears
end
if motor.minForwardGearRatio == nil and gears ~= nil then
numGears = #gears
end
debugGraphs = {}
motor.debugGraphs = debugGraphs
for gear = 1, numGears do
local effTorqueGraph = Graph.new(numVelocityValues, x, y, sizeX, sizeY, 0, 0.0001, true, "kN", Graph.STYLE_LINES)
effTorqueGraph:setColor(1, 1, 1, 1)
table.insert(debugGraphs, effTorqueGraph)
local effPowerGraph = Graph.new(numVelocityValues, x, y, sizeX, sizeY, 0, 0.0001, false, "", Graph.STYLE_LINES)
effPowerGraph:setColor(1, 0, 0, 1)
table.insert(debugGraphs, effPowerGraph)
local effGearRatioGraph = Graph.new(numVelocityValues, x, y, sizeX, sizeY, 0, 0.0001, false, "", Graph.STYLE_LINES)
effGearRatioGraph:setColor(0.35, 1, 0.85, 1)
table.insert(debugGraphs, effGearRatioGraph)
local effRpmGraph = Graph.new(numVelocityValues, x, y, sizeX, sizeY, 0, 0.0001, false, "", Graph.STYLE_LINES)
effRpmGraph:setColor(0.18, 0.18, 1, 1)
table.insert(debugGraphs, effRpmGraph)
effTorqueGraph.maxValue = 0.01
effPowerGraph.maxValue = 0.01
effGearRatioGraph.maxValue = 0.01
effRpmGraph.maxValue = 0.01
for s=1, numVelocityValues do
local speed = (s-1)/(numVelocityValues-1) * maxSpeed
local _, gearRatio
if numGears == 1 then
_, gearRatio = motor:getBestGear(1, speed*30/math.pi, 0, math.huge, 0)
else
gearRatio = gears[gear].ratio
end
local gearRpm = speed*30/math.pi * gearRatio
local torque = torqueCurve:get(gearRpm)
local power = torque*1000 * gearRpm*math.pi/30
local hpPower = power/735.49875
if gearRpm >= minRpm and gearRpm <= maxRpm then
effTorqueGraph:setValue(s, torque)
effTorqueGraph.maxValue = math.max(effTorqueGraph.maxValue, torque)
effPowerGraph:setValue(s, hpPower)
effPowerGraph.maxValue = math.max(effPowerGraph.maxValue, hpPower)
effGearRatioGraph:setValue(s, gearRatio)
effGearRatioGraph.maxValue = math.max(effGearRatioGraph.maxValue, gearRatio)
effRpmGraph:setValue(s, gearRpm)
effRpmGraph.maxValue = math.max(effRpmGraph.maxValue, gearRpm)
end
end
end
else
for i=1, #debugGraphs do
local graph = debugGraphs[i]
graph.left, graph.bottom, graph.width, graph.height = x, y, sizeX, sizeY
end
end
for _, graph in pairs(debugGraphs) do
graph:draw()
end
renderOverlay(curveOverlay, x, y, sizeX*math.clamp(self.lastSpeedReal*1000/maxSpeed, 0, 1), sizeY)
if horizontal then
x = x + sizeX + 0.013
else
y = y - sizeY - 0.013
end
VehicleDebug.drawMotorLoadGraph(self, x, y, sizeX, sizeY)
end
end
end
---
function VehicleDebug.drawMotorLoadGraph(self, x, y, sizeX, sizeY)
if self.isServer then
local motorSpec = self.spec_motorized
if motorSpec ~= nil then
local motor = motorSpec.motor
local numValues = 500
local loadGraph = motor.debugLoadGraph
local loadGraphSmooth = motor.debugLoadGraphSmooth
local loadGraphSound = motor.debugLoadGraphSound
if loadGraph == nil then
loadGraph = Graph.new(numValues, x, y, sizeX, sizeY, 0, 100, true, "%", Graph.STYLE_LINES, 0.1, "time")
loadGraph:setColor(1, 1, 1, 0.3)
motor.debugLoadGraph = loadGraph
loadGraphSmooth = Graph.new(numValues, x, y, sizeX, sizeY, 0, 100, false, "", Graph.STYLE_LINES)
loadGraphSmooth:setColor(0, 1, 0, 1)
motor.debugLoadGraphSmooth = loadGraphSmooth
loadGraphSound = Graph.new(numValues, x, y, sizeX, sizeY, 0, 100, false, "", Graph.STYLE_LINES)
loadGraphSound:setColor(0, 1, 1, 1)
motor.debugLoadGraphSound = loadGraphSound
else
loadGraph.left, loadGraph.bottom, loadGraph.width, loadGraph.height = x, y, sizeX, sizeY
loadGraphSmooth.left, loadGraphSmooth.bottom, loadGraphSmooth.width, loadGraphSmooth.height = x, y, sizeX, sizeY
loadGraphSound.left, loadGraphSound.bottom, loadGraphSound.width, loadGraphSound.height = x, y, sizeX, sizeY
end
if loadGraph ~= nil and loadGraphSmooth ~= nil and loadGraphSound ~= nil then
local rawLoad = motor:getMotorAppliedTorque() / math.max(motor:getMotorAvailableTorque(), 0.0001)
loadGraph:addValue(rawLoad * 100, nil, true)
loadGraphSmooth:addValue(motorSpec.smoothedLoadPercentage * 100, nil, true)
for i=1, #motorSpec.motorSamples do
local sample = motorSpec.motorSamples[i]
if sample.isGlsFile then
loadGraphSound:addValue(getSampleLoopSynthesisLoadFactor(sample.soundSample) * 100, nil, true)
break
end
end
end
loadGraph:draw()
loadGraphSmooth:draw()
loadGraphSound:draw()
end
end
end
---
function VehicleDebug.drawMotorRPMGraph(self, x, y, sizeX, sizeY)
if self.isServer then
local motorSpec = self.spec_motorized
if motorSpec ~= nil then
local motor = motorSpec.motor
local numValues = 500
local rpmGraph = motor.debugRPMGraph
local rpmGraphSmooth = motor.debugRPMGraphSmooth
local rpmGraphSound = motor.debugRPMGraphSound
if rpmGraph == nil then
rpmGraph = Graph.new(numValues, x, y, sizeX, sizeY, motor:getMinRpm(), motor:getMaxRpm(), true, " RPM", Graph.STYLE_LINES, 0.1, "")
rpmGraph:setColor(1, 1, 1, 0.3)
motor.debugRPMGraph = rpmGraph
rpmGraphSmooth = Graph.new(numValues, x, y, sizeX, sizeY, motor:getMinRpm(), motor:getMaxRpm(), false, "", Graph.STYLE_LINES)
rpmGraphSmooth:setColor(0, 1, 0, 1)
motor.debugRPMGraphSmooth = rpmGraphSmooth
local minSoundRpm, maxSoundRpm = motor:getMinRpm(), motor:getMaxRpm()
for i=1, #motorSpec.motorSamples do
local sample = motorSpec.motorSamples[i]
if sample.isGlsFile then
minSoundRpm, maxSoundRpm = getSampleLoopSynthesisMinRPM(sample.soundSample), getSampleLoopSynthesisMaxRPM(sample.soundSample)
break
end
end
rpmGraphSound = Graph.new(numValues, x, y, sizeX, sizeY, minSoundRpm, maxSoundRpm, false, "", Graph.STYLE_LINES)
rpmGraphSound:setColor(0, 1, 1, 1)
motor.debugRPMGraphSound = rpmGraphSound
else
rpmGraph.left, rpmGraph.bottom, rpmGraph.width, rpmGraph.height = x, y, sizeX, sizeY
rpmGraphSmooth.left, rpmGraphSmooth.bottom, rpmGraphSmooth.width, rpmGraphSmooth.height = x, y, sizeX, sizeY
rpmGraphSound.left, rpmGraphSound.bottom, rpmGraphSound.width, rpmGraphSound.height = x, y, sizeX, sizeY
end
if rpmGraph ~= nil and rpmGraphSmooth ~= nil and rpmGraphSound ~= nil then
rpmGraph:addValue(motor:getLastRealMotorRpm(), nil, true)
rpmGraphSmooth:addValue(motor:getLastModulatedMotorRpm(), nil, true)
for i=1, #motorSpec.motorSamples do
local sample = motorSpec.motorSamples[i]
if sample.isGlsFile then
rpmGraphSound:addValue(getSampleLoopSynthesisRPM(sample.soundSample, false), nil, true)
break
end
end
end
rpmGraph:draw()
rpmGraphSmooth:draw()
rpmGraphSound:draw()
end
end
end
---
function VehicleDebug.drawMotorAccelerationGraph(self, x, y, sizeX, sizeY)
if self.isServer then
local motorSpec = self.spec_motorized
if motorSpec ~= nil then
local motor = motorSpec.motor
local numValues = 250
local accGraph = motor.debugAccelerationGraph
if accGraph == nil then
accGraph = Graph.new(numValues, x, y, sizeX, sizeY, 0, 1, true, " Load Factor", Graph.STYLE_LINES, 0.1, "")
accGraph:setColor(1, 1, 1, 0.3)
motor.debugAccelerationGraph = accGraph
motor.debugAccelerationGraphAddValue = true
else
accGraph.left, accGraph.bottom, accGraph.width, accGraph.height = x, y, sizeX, sizeY
end
if accGraph ~= nil then
if motor.debugAccelerationGraphAddValue then
accGraph:addValue(motor.constantAccelerationCharge, nil, true)
end
motor.debugAccelerationGraphAddValue = not motor.debugAccelerationGraphAddValue
end
accGraph:draw()
end
end
end
---
function VehicleDebug.drawDebugRendering(self)
local textHeight1, _ = VehicleDebug.drawBaseDebugRendering(self, 0.015, 0.65)
local x, y = 0.015, 0.64 - textHeight1 - 0.005
local height = VehicleDebug.drawWheelInfoRendering(self, x, y)
VehicleDebug.drawDifferentialInfoRendering(self, x, y - (height + getCorrectTextSize(0.02)))
VehicleDebug.drawWheelSlipGraphs(self)
VehicleDebug.drawMotorGraphs(self, 0.65, 0.44, 0.25, 0.2, false)
end
---
function VehicleDebug.drawTuningDebug(self)
local textHeight1, _ = VehicleDebug.drawBaseDebugRendering(self, 0.015, 0.9)
local x, y = 0.015, 0.89 - textHeight1 - 0.005
local height = VehicleDebug.drawWheelInfoRendering(self, x, y)
VehicleDebug.drawDifferentialInfoRendering(self, x, y - (height + getCorrectTextSize(0.02)))
end
---
function VehicleDebug.drawTransmissionDebug(self)
local textHeight1, _ = VehicleDebug.drawBaseDebugRendering(self, 0.015, 0.65)
VehicleDebug.drawMotorGraphs(self, 0.01, 0.73, 0.25, 0.2, true)
local str1, str2 = "", ""
local motorSpec = self.spec_motorized
if motorSpec ~= nil then
local motor = motorSpec.motor
str1 = str1.."\ngear start values:\n" ; str2 = str2.."\n\n"
str1 = str1.."peakPower:\n" ; str2 = str2..string.format("%d/%dkW\n", motor.startGearValues.availablePower, motor.peakMotorPower)
str1 = str1.."maxForce:\n" ; str2 = str2..string.format("%.2fkN\n", motor.startGearValues.maxForce)
str1 = str1.."mass:\n" ; str2 = str2..string.format("%.2fto\n", motor.startGearValues.mass)
str1 = str1.."slope angle:\n" ; str2 = str2..string.format("%.2f°\n", math.deg(motor.startGearValues.slope))
str1 = str1.."slope percentage:\n" ; str2 = str2..string.format("%.2f%%\n", math.atan(motor.startGearValues.slope) * 100)
str1 = str1.."dirDiffXZ:\n" ; str2 = str2..string.format("%.2f\n", motor.startGearValues.massDirectionDifferenceXZ)
str1 = str1.."dirDiffY:\n" ; str2 = str2..string.format("%.2f\n", motor.startGearValues.massDirectionDifferenceY)
str1 = str1.."dirFac:\n" ; str2 = str2..string.format("%.2f\n", motor.startGearValues.massDirectionFactor)
str1 = str1.."massFac:\n" ; str2 = str2..string.format("%.2f\n", motor.startGearValues.massFactor)
str1 = str1.."speedLimit:\n" ; str2 = str2..string.format("%.1f / %.1f \n", motor.speedLimit, self:getSpeedLimit(true))
str1 = str1.."auto shift allowed:\n" ; str2 = str2..string.format("%s\n", self:getIsAutomaticShiftingAllowed())
str1 = str1.."gear/group change allowed:\n" ; str2 = str2..string.format("%s/%s\n", motor:getIsGearChangeAllowed(), motor:getIsGearGroupChangeAllowed())
str1 = str1.."gear group shift timer:\n" ; str2 = str2..string.format("%.1f/%.1f sec\n", motor.gearGroupUpShiftTimer / 1000, motor.gearGroupUpShiftTime / 1000)
str1 = str1.."clutch slipping simer:\n" ; str2 = str2..string.format("%d ms\n", motor.clutchSlippingTimer)
str1 = str1.."motor can run:\n" ; str2 = str2..string.format("%s\n", motor:getCanMotorRun())
str1 = str1.."stall timer:\n" ; str2 = str2..string.format("%.2f\n", motor.stallTimer)
str1 = str1.."turbo scale:\n" ; str2 = str2..string.format("%d%%\n", motor.lastTurboScale * 100)
str1 = str1.."blowOffValveState:\n" ; str2 = str2..string.format("%d%%\n", motor.blowOffValveState * 100)
Utils.renderMultiColumnText(0.015, 0.65 - textHeight1, getCorrectTextSize(0.018), {str1,str2}, 0.008, {RenderText.ALIGN_RIGHT,RenderText.ALIGN_LEFT})
if motor.forwardGears or motor.backwardGears then
local x = 0.222
local y = 0.15
local infoWidth = 0.05
local minWidthPerGear = 0.035
local gears = motor.forwardGears
if motor.currentDirection < 0 then
gears = motor.backwardGears or gears
end
local width = #gears * minWidthPerGear + infoWidth
local height = 0.35
drawOutlineRect(x, y, width, height, g_pixelSizeX, g_pixelSizeY, 0, 0, 0, 1)
drawFilledRect(x, y, width, height, 0, 0, 0, 0.4)
local gearAreaWidth = width - infoWidth
drawFilledRect(x + infoWidth, y, g_pixelSizeX, height, 0, 0, 0, 1)
drawFilledRect(x + infoWidth, y + height * 0.9, gearAreaWidth, g_pixelSizeY, 0, 0, 0, 1)
drawFilledRect(x + infoWidth, y + height * 0.3, gearAreaWidth, g_pixelSizeY, 0, 0, 0, 1)
local groupRatioReal = motor:getGearRatioMultiplier()
local groupRatio = math.abs(motor:getGearRatioMultiplier())
local numGears = #gears
local gearWidth = gearAreaWidth / numGears
local gearMaxHeight = height * 0.6
local textOffset = 0.0075
local maxDiffSpeed = 1
for i=1, numGears do
maxDiffSpeed = math.max(maxDiffSpeed, motor.maxRpm * math.pi / (30 * gears[i].ratio * groupRatio) * 3.6)
end
local numGearValues = 5
local offsetPerValue = height * 0.3 / numGearValues
local lastDiffSpeedAfterChange
local lastMaxPower
for i=1, numGears do
local gear = gears[i]
lastDiffSpeedAfterChange = lastDiffSpeedAfterChange or gear.lastDiffSpeedAfterChange
lastMaxPower = lastMaxPower or gear.lastMaxPower
local minGearSpeed = motor.minRpm * math.pi / (30 * gear.ratio * groupRatio) * 3.6
local maxGearSpeed = motor.maxRpm * math.pi / (30 * gear.ratio * groupRatio) * 3.6
local pos = (minGearSpeed / maxDiffSpeed) * gearMaxHeight
local h = ((maxGearSpeed-minGearSpeed) / maxDiffSpeed) * gearMaxHeight
local gearX = x + infoWidth + gearWidth * (i - 1)
local posY = y + height * 0.3 + g_pixelSizeY + pos
drawFilledRect(gearX, posY, gearWidth, h, (motor.gear ~= i and gear.lastHasPower) and 1 or 0.05, (motor.gear == i or gear.lastHasPower) and 1 or 0.05, 0.05, 0.85)
setTextAlignment(RenderText.ALIGN_CENTER)
renderText(gearX + gearWidth * 0.5, posY + textOffset * 0.5, 0.015, string.format("%.2f", gear.ratio * groupRatio))
local factor = motor:getStartInGearFactor(gear.ratio * groupRatio)
if factor < motor.startGearThreshold then
setTextColor(0, 1, 0, 1)
else
setTextColor(1, 0, 0, 1)
end
renderText(gearX + gearWidth * 0.5, y + height * 0.3 + g_pixelSizeY + gearMaxHeight - textOffset * 2, 0.015, string.format("%.2f", factor))
if groupRatioReal ~= groupRatio then
factor = motor:getStartInGearFactor(gear.ratio * groupRatioReal)
if factor < motor.startGearThreshold then
setTextColor(0, 1, 0, 1)
else
setTextColor(1, 0, 0, 1)
end
renderText(gearX + gearWidth * 0.5, y + height * 0.3 + g_pixelSizeY + gearMaxHeight - textOffset * 4, 0.012, string.format("%.2f", factor))
end
setTextColor(1, 1, 1, 1)
renderText(gearX + gearWidth * 0.5, y + textOffset , 0.0125, string.format("%.2f %.2f", gear.lastPowerFactor or 0, gear.lastRpmFactor or 0))
renderText(gearX + gearWidth * 0.5, y + textOffset + offsetPerValue * 1, 0.0125, string.format("%.2f %.2f", gear.lastGearChangeFactor or 0, gear.lastRpmPreferenceFactor or 0))
if gear.nextPowerValid then
setTextColor(0, 1, 0, 1)
else
setTextColor(1, 0, 0, 1)
end
renderText(gearX + gearWidth * 0.5, y + textOffset + offsetPerValue * 2, 0.015, string.format("%d", gear.lastNextPower or -1))
if gear.nextRpmValid then
setTextColor(0, 1, 0, 1)
else
setTextColor(1, 0, 0, 1)
end
renderText(gearX + gearWidth * 0.5, y + textOffset + offsetPerValue * 3, 0.015, string.format("%d", gear.lastNextRpm or -1))
setTextColor(1, 1, 1, 1)
renderText(gearX + gearWidth * 0.5, y + textOffset + offsetPerValue * 4, 0.015, string.format("%.2f", gear.lastTradeoff or 0))
end
setTextAlignment(RenderText.ALIGN_CENTER)
renderText(x + (infoWidth * 0.5), y + height * 0.3 + g_pixelSizeY + gearMaxHeight - textOffset * 2, 0.015, "startFactor")
local bestGear, maxFactorGroup = motor:getBestStartGear(motor.currentGears)
renderText(x + (infoWidth * 0.5), y + height * 0.3 + g_pixelSizeY + gearMaxHeight - textOffset * 4, 0.015, string.format("best %d>%d", maxFactorGroup, bestGear))
renderText(x + (infoWidth * 0.5), y + textOffset , 0.01, "pwr/rpm")
renderText(x + (infoWidth * 0.5), y + textOffset + offsetPerValue * 1, 0.01, "gearC/rpmPref")
renderText(x + (infoWidth * 0.5), y + textOffset + offsetPerValue * 2, 0.01, string.format("nextPwr (%d)", lastMaxPower or -1))
renderText(x + (infoWidth * 0.5), y + textOffset + offsetPerValue * 3, 0.01, "nextRpm")
renderText(x + (infoWidth * 0.5), y + textOffset + offsetPerValue * 4, 0.01, "tradeoff")
local diffSpeed = math.abs(motor.differentialRotSpeed * 3.6)
local speedHeight = y + height * 0.3 + ((diffSpeed/maxDiffSpeed) * (gearMaxHeight-g_pixelSizeY)) + g_pixelSizeY
setTextBold(true)
setTextAlignment(RenderText.ALIGN_CENTER)
renderText(x + infoWidth * 0.5, speedHeight - 0.005, 0.015, string.format("%.2f", diffSpeed))
setTextBold(false)
if lastDiffSpeedAfterChange ~= nil then
setTextAlignment(RenderText.ALIGN_LEFT)
renderText(x + infoWidth * 1.1, y + height * 0.95-0.005, 0.01, string.format("Speed after change: %.2fkm/h (%.1f sec)", lastDiffSpeedAfterChange*3.6, motor.gearChangeTime / 1000))
end
drawFilledRect(x + infoWidth, speedHeight, gearAreaWidth, g_pixelSizeY, 0, 1, 0, 0.5)
end
end
end
---
function VehicleDebug.drawDebugAttributeRendering(vehicle)
if vehicle.debugSizeOffsetNode == nil then
vehicle.debugSizeOffsetNode = createTransformGroup("debugSizeOffsetNode")
link(vehicle.rootNode, vehicle.debugSizeOffsetNode)
local storeItem = g_storeManager:getItemByXMLFilename(vehicle.configFileName)
if storeItem ~= nil then
local shopTransOffset = storeItem.shopTranslationOffset
if shopTransOffset ~= nil then
setTranslation(vehicle.debugSizeOffsetNode, -shopTransOffset[1], -shopTransOffset[2], -shopTransOffset[3])
end
local rotOffset = storeItem.shopRotationOffset
if rotOffset ~= nil then
setRotation(vehicle.debugSizeOffsetNode, -rotOffset[1], -rotOffset[2], -rotOffset[3])
end
end
end
-- display vehicle size
local offsetX, offsetY, offsetZ = vehicle.size.widthOffset, vehicle.size.heightOffset + vehicle.size.height/2, vehicle.size.lengthOffset
DebugBox.renderAtNodeWithOffset(vehicle.debugSizeOffsetNode, offsetX, offsetY, offsetZ, vehicle.size.width, vehicle.size.height, vehicle.size.length, Color.PRESETS.BLUE, true, "size")
-- display attacher joint height to ground
if vehicle.spec_attacherJoints ~= nil then
for _, implement in pairs(vehicle.spec_attacherJoints.attachedImplements) do
if implement.object ~= nil then
local jointDesc = vehicle.spec_attacherJoints.attacherJoints[implement.jointDescIndex]
local x, y, z = getWorldTranslation(jointDesc.jointTransform)
drawDebugPoint(x, y, z, 1, 0, 0, 1)
local groundRaycastResult = {
raycastCallback = function(self, transformId, x, y, z, distance)
if vehicle.vehicleNodes[transformId] == nil and implement.object.vehicleNodes[transformId] == nil then