-
Notifications
You must be signed in to change notification settings - Fork 15
/
LumberJack.lua
1193 lines (997 loc) · 41.1 KB
/
LumberJack.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
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
-- ============================================================= --
-- LUMBERJACK MOD for FS25 - loki_79
-- ============================================================= --
LumberJack = {}
LumberJack.name = g_currentModName
LumberJack.path = g_currentModDirectory
-- DEFAULTS
LumberJack.cutAnywhere = true
LumberJack.createWoodchips = false
LumberJack.superStrengthValue = 1000
LumberJack.normalStrengthValue = 0.2
LumberJack.superDistanceValue = 12
LumberJack.normalDistanceValue = 3
LumberJack.minCutDistance = 0.1
LumberJack.maxCutDistance = 4.0
LumberJack.defaultCutDuration = 3
LumberJack.destroyFoliageSize = 2
LumberJack.showDebug = false
-- VARIABLES
LumberJack.longHoldThreshold = 1000
LumberJack.doubleTapThreshold = 500
LumberJack.maxWoodchips = 2000
-- FLAGS
LumberJack.superStrength = false
LumberJack.lockStrength = false
LumberJack.strengthHeld = false
LumberJack.useChainsawFlag = false
LumberJack.stumpGrindingPossible = false
LumberJack.bushCuttingPossible = false
LumberJack.destroyAllFoliage = false
LumberJack.initialised = false
-- FOUND USING RAY CAST
LumberJack.closestObject = nil
-- FOUND USING GIANTS RING SELECTOR
LumberJack.chainsawShape = nil
LumberJack.chainsawCanCut = nil
-- BUSH LAYER STRINGS
LumberJack.foliageSearchNames = {
"bush",
"forest"
}
LumberJack.ringColour = {
white = {1, 1, 1, 0},
orange = {1, 0.27, 0, 1},
red = {0.8, 0.05, 0.05, 1},
green = {0.395, 0.925, 0.115, 1},
yellow = {0.925, 0.395, 0.05, 1},
}
source(g_currentModDirectory .. 'LumberJackSettings.lua')
source(g_currentModDirectory .. 'events/DeleteShapeEvent.lua')
source(g_currentModDirectory .. 'events/CreateSawdustEvent.lua')
source(g_currentModDirectory .. 'events/SuperStrengthEvent.lua')
source(g_currentModDirectory .. 'events/ToggleServerSettingEvent.lua')
addModEventListener(LumberJack)
function debugPrint(str)
if LumberJack.showDebug then
print("[LumberJack] " .. str)
end
end
-- DETECT SUPER STRENGTH CONSOLE COMMAND
executeConsoleCommand = Utils.overwrittenFunction(executeConsoleCommand,
function(command, superFunc, ...)
if command == 'gsPlayerSuperStrengthToggle' then
debugPrint("called gsPlayerSuperStrengthToggle - " .. tostring(LumberJack.superStrength))
end
superFunc(command, ...)
end
)
HandToolHands.consoleCommandToggleSuperStrength = Utils.overwrittenFunction(HandToolHands.consoleCommandToggleSuperStrength,
function(self, superFunc, ...)
local result = superFunc(self, ...)
LumberJack.superStrength = self.spec_hands.hasSuperStrength
debugPrint("called consoleCommandToggleSuperStrength - " .. tostring(LumberJack.superStrength))
return result
end
)
-- ALLOW TREE SPRAYING ANYWHERE ON THE MAP
HandToolSprayCan.getIsSprayingAllowed = Utils.overwrittenFunction(HandToolSprayCan.getIsSprayingAllowed,
function(self, superFunc, ...)
if g_currentMission:getHasPlayerPermission("cutTrees") then
-- debugPrint("can spray tree..")
return true
end
return superFunc(self, ...)
end
)
-- GET CHAINSAW TARGET SHAPE
HandToolChainsaw.updateRingSelector = Utils.overwrittenFunction(HandToolChainsaw.updateRingSelector,
function(self, superFunc, shape, canCut, ...)
if shape and shape.node then
-- debugPrint("shape: " .. tostring(shape))
-- debugPrint("canCut: " .. tostring(canCut))
LumberJack.chainsawShape = shape.node
LumberJack.chainsawCanCut = canCut
end
return superFunc(self, shape, canCut, ...)
end
)
-- ALLOW CHAINSAW CUTTING ANYWHERE ON THE MAP
HandToolChainsaw.testIfCutAllowed = Utils.overwrittenFunction(HandToolChainsaw.testIfCutAllowed,
function(self, superFunc, shape, x, z, ...)
local canCutTrees = g_currentMission:getHasPlayerPermission("cutTrees")
local canChainsaw = g_currentMission:getHasPlayerPermission("chainsawSettings")
local canAccess = g_currentMission.accessHandler:canFarmAccessLand(self.carryingPlayer.farmId, x, z)
local isAllowed = canCutTrees and ((canChainsaw and LumberJack.cutAnywhere) or canAccess)
if isAllowed then
-- debugPrint("can cut shape: " .. tostring(shape))
return true
else
return superFunc(self, shape, x, z, ...)
end
end
)
-- DETECT SPLITSHAPES FROM CHAINSAW CALLBACK
function LumberJack:targetRaycastCallback(hitObjectId, x, y, z, distance)
if hitObjectId ~= 0 and hitObjectId ~= nil then
LumberJack.targetLocation = {x, y, z}
LumberJack.targetDistance = distance
LumberJack.targetOnGround = hitObjectId==g_currentMission.terrainRootNode
if LumberJack.targetedSplitShapeId ~= hitObjectId then
if getHasClassId(hitObjectId, ClassIds.MESH_SPLIT_SHAPE) then
LumberJack.targetedSplitShapeId = hitObjectId
else
LumberJack.targetedSplitShapeId = nil
end
end
end
end
function LumberJack.updateTargetRaycast()
LumberJack.targetLocation = nil
LumberJack.targetOnGround = nil
local cameraNode = g_localPlayer:getCurrentCameraNode()
if cameraNode then
local x, y, z = getWorldTranslation(cameraNode)
local dx, dy, dz = unProject(0.52, 0.4, 1)
dx, dy, dz = dx-x, dy-y, dz-z
dx, dy, dz = MathUtil.vector3Normalize(dx, dy, dz)
local collisionMask = CollisionFlag.DEFAULT + CollisionFlag.TERRAIN + CollisionFlag.TREE + CollisionFlag.VEHICLE
raycastClosest(x, y, z, dx, dy, dz, LumberJack.superDistanceValue, "targetRaycastCallback", LumberJack, collisionMask)
end
end
-- ALTERNATIVE TO "FIND SPLIT SHAPE"
function LumberJack:getSplitShape(shape)
local objectId = shape or LumberJack.closestObject and LumberJack.closestObject.id
if objectId~=nil and objectId~=0 and entityExists(objectId) then
if getHasClassId(objectId, ClassIds.MESH_SPLIT_SHAPE) then
if getSplitType(objectId) ~= 0 then
local isSplit = getIsSplitShapeSplit(objectId)
local isStatic = getRigidBodyType(objectId) == RigidBodyType.STATIC
local isDynamic = getRigidBodyType(objectId) == RigidBodyType.DYNAMIC
local isTree = isStatic and not isSplit
local isStump = isStatic and isSplit
local isBranch = isDynamic and isSplit
return objectId, isTree, isStump, isBranch
end
end
end
return nil
end
-- ADD SHORTCUT KEY SELECTION TO OPTIONS MENU
function LumberJack:registerActionEvents(force)
if force or not LumberJack.actionEventId then
debugPrint("LUMBERJACK: ADDING ACTION EVENT")
local success, actionEventId = g_inputBinding:registerActionEvent('LUMBERJACK_STRENGTH', LumberJack, LumberJack.strengthKeyCallback, true, true, false, true)
debugPrint(" success = " .. tostring(success))
LumberJack.actionEventId = success and actionEventId or nil
end
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_LOW)
g_inputBinding:setActionEventActive(actionEventId, true)
g_inputBinding:setActionEventText(actionEventId, g_i18n:getText("menu_TOGGLE_STRENGTH"))
g_inputBinding:setActionEventTextVisibility(actionEventId, true)
end
-- LUMBERJACK FUNCTIONS:
function LumberJack:loadMap(name)
-- print("Load Mod: 'LumberJack'")
LumberJack.readSettings()
LumberJack.injectMenu()
if not g_startMissionInfo.isMultiplayer then
print("adding console command - 'lumberjackLoadSettings'")
addConsoleCommand("lumberjackLoadSettings", "Load LumberJack settings from the local mod settings file", "readSettings", LumberJack)
else
print("LumberJack console command is disabled for multiplayer")
end
end
function LumberJack:strengthKeyCallback(id, state)
LumberJack.strengthKeyState = state
LumberJack.strengthKeyAction = true
end
function LumberJack.doToggleStrength()
--debugPrint("doToggleStrength")
if g_currentMission:getHasPlayerPermission("superStrength") then
LumberJack.superStrength = not LumberJack.superStrength
--executeConsoleCommand('gsPlayerSuperStrengthToggle')
SuperStrengthEvent.sendEvent(LumberJack.superStrength)
if LumberJack.superStrength then
debugPrint("SUPER-STRENGTH IS ON")
else
debugPrint("SUPER-STRENGTH IS OFF")
end
else
if LumberJack.superStrength then
debugPrint("SUPER STRENGTH PERMISSON MISSING")
LumberJack.superStrength = false
SuperStrengthEvent.sendEvent(LumberJack.superStrength)
end
end
end
function LumberJack.updateStrength(dt)
if LumberJack.playerIsEntered and not g_gui:getIsGuiVisible() then
LumberJack.tapCount = LumberJack.tapCount or 0
LumberJack.holdTime = LumberJack.holdTime or 0
LumberJack.doubleTapTime = LumberJack.doubleTapTime or 0
if LumberJack.tapCount == 1 then
LumberJack.doubleTapTime = LumberJack.doubleTapTime + dt
if LumberJack.doubleTapTime > LumberJack.doubleTapThreshold then
LumberJack.tapCount = 0
LumberJack.doubleTapTime = 0
end
end
if LumberJack.strengthKeyState == 1 then -- KEY DOWN
LumberJack.holdTime = LumberJack.holdTime + dt
--debugPrint("Running Strength Update with hold time: " .. LumberJack.holdTime)
if LumberJack.strengthKeyAction == true then
LumberJack.tapCount = LumberJack.tapCount + 1
--debugPrint("TAP COUNT = " .. LumberJack.tapCount .. ", HOLD TIME = " .. LumberJack.holdTime)
end
if LumberJack.tapCount == 1 then
local holdThreshold = math.max(LumberJack.longHoldThreshold, LumberJack.doubleTapThreshold)
if LumberJack.holdTime > holdThreshold + 1 then
if not LumberJack.superStrength and not LumberJack.strengthHeld then
-- debugPrint("LONG PRESS HOLD")
LumberJack.strengthHeld = true
LumberJack.doToggleStrength()
end
--debugPrint("RESET TAP COUNT TO 0")
LumberJack.tapCount = 0
end
LumberJack.doubleTapTime = 0
elseif LumberJack.tapCount == 2 then
-- debugPrint("DOUBLE TAP")
LumberJack.tapCount = 0
LumberJack.doToggleStrength()
end
else -- KEY UP
if LumberJack.superStrength and LumberJack.strengthHeld then
-- debugPrint("LONG PRESS RELEASE")
LumberJack.doToggleStrength()
end
LumberJack.holdTime = 0
LumberJack.strengthHeld = false
end
LumberJack.strengthKeyAction = false
end
end
function LumberJack.setSuperStrenth(handToolHands, superStrengthActive, mass, distance)
--debugPrint("setSuperStrenthServer.")
local spec = handToolHands.spec_hands
local previousStatus = spec.hasSuperStrength
spec.hasSuperStrength = superStrengthActive
spec.currentMaximumMass = mass
spec.pickupDistance = distance
local carryingPlayer = handToolHands:getCarryingPlayer()
if carryingPlayer and carryingPlayer.isOwner then
carryingPlayer.targeter:removeTargetType(HandToolHands)
carryingPlayer.targeter:addTargetType(HandToolHands, HandToolHands.TARGET_MASK, 0.5, spec.pickupDistance)
end
if previousStatus ~= spec.hasSuperStrength then
if spec.hasSuperStrength then
debugPrint("Enabled super strength.")
else
debugPrint("Disabled super strength.")
end
-- else
-- debugPrint("Updated super strength without change.")
end
end
function LumberJack.updateVariables(dt)
local function compareFloats(a, b)
local epsilon = 0.001
return math.abs(a - b) < epsilon
end
if g_currentMission:getHasPlayerPermission('superStrength') then
-- INCREASE PICKUP MASS
if not compareFloats(HandToolHands.MAXIMUM_PICKUP_MASS, LumberJack.normalStrengthValue) then
HandToolHands.MAXIMUM_PICKUP_MASS = LumberJack.normalStrengthValue
debugPrint("MAXIMUM_PICKUP_MASS = " .. string.format("%.3f", LumberJack.normalStrengthValue))
end
if not compareFloats(HandToolHands.SUPER_STRENGTH_PICKUP_MASS, LumberJack.superStrengthValue) then
HandToolHands.SUPER_STRENGTH_PICKUP_MASS = LumberJack.superStrengthValue
debugPrint("SUPER_STRENGTH_PICKUP_MASS = " .. LumberJack.superStrengthValue)
end
-- INCREASE PICKUP DISTANCE
if LumberJack.superStrength then
g_currentMission:addExtraPrintText(g_i18n:getText("input_LUMBERJACK_STRENGTH") .. ": " .. g_i18n:getText("ui_on"))
if not compareFloats(HandToolHands.PICKUP_DISTANCE, LumberJack.superDistanceValue) then
HandToolHands.PICKUP_DISTANCE = LumberJack.superDistanceValue
debugPrint("PICKUP_DISTANCE = " .. LumberJack.superDistanceValue)
end
else
--g_currentMission:addExtraPrintText(g_i18n:getText("input_LUMBERJACK_STRENGTH") .. ": " .. g_i18n:getText("ui_off"))
if not compareFloats(HandToolHands.PICKUP_DISTANCE, LumberJack.normalDistanceValue) then
HandToolHands.PICKUP_DISTANCE = LumberJack.normalDistanceValue
debugPrint("PICKUP_DISTANCE = " .. LumberJack.normalDistanceValue)
end
end
end
if g_currentMission:getHasPlayerPermission('chainsawSettings') then
-- INCREASE CUT DISTANCE
if not compareFloats(HandToolChainsaw.MINIMUM_CUT_DISTANCE, LumberJack.minCutDistance) then
HandToolChainsaw.MINIMUM_CUT_DISTANCE = LumberJack.minCutDistance
debugPrint("MINIMUM_CUT_DISTANCE = " .. LumberJack.minCutDistance)
end
if not compareFloats(HandToolChainsaw.MAXIMUM_CUT_DISTANCE, LumberJack.maxCutDistance) then
HandToolChainsaw.MAXIMUM_CUT_DISTANCE = LumberJack.maxCutDistance
debugPrint("MAXIMUM_CUT_DISTANCE = " .. LumberJack.maxCutDistance)
end
end
end
function LumberJack.updateChainsaw(dt)
local player = g_localPlayer
if player and player.isHoldingChainsaw then
LumberJack.stumpGrindingPossible = false
-- g_currentMission:addExtraPrintText("chainsawShape: " .. tostring(LumberJack.chainsawShape))
-- g_currentMission:addExtraPrintText("chainsawCanCut: " .. tostring(LumberJack.chainsawCanCut))
--CHECK FOR CHAINSAW
local handTool = player.currentHandTool
if not handTool or not handTool.spec_chainsaw then
debugPrint("NOT A CHAINSAW")
return
end
-- RUNNING WHITH CHAINSAW
if handTool.runMultiplier == 0 then
debugPrint("ALLOW RUNNING WITH CHAINSAW")
handTool.runMultiplier = 0.75
handTool.walkMultiplier = 0.90
end
local chainsaw = handTool.spec_chainsaw
if LumberJack.chainsawShape and chainsaw.ringNode then
if LumberJack.chainsawCanCut then
-- SHOW GREEN RING SELECTOR
local c = LumberJack.ringColour['green']
setShaderParameter(chainsaw.ringNode, "colorScale", c[1], c[2], c[3], c[4], false)
else
-- SHOW ORANGE/YELLOW RING SELECTOR
local c = LumberJack.ringColour['yellow']
setShaderParameter(chainsaw.ringNode, "colorScale", c[1], c[2], c[3], c[4], false)
end
end
if LumberJack.originalDefaultCutDuration == nil then
LumberJack.originalChainsawStartupTime = chainsaw.startupTime
LumberJack.originalDefaultCutDuration = chainsaw.cutTimePerSquareMeter / 1000
LumberJack.originalMaximumCutDiameter = chainsaw.maximumCutDiameter --2.5
LumberJack.originalMaximumDelimbDiameter = chainsaw.maximumDelimbDiameter --1
end
if g_currentMission:getHasPlayerPermission('chainsawSettings') then
-- INCREASE CUTTING SPEED
chainsaw.startupTime = LumberJack.defaultCutDuration/4 * 1000
chainsaw.cutTimePerSquareMeter = LumberJack.defaultCutDuration * 1000
else
chainsaw.startupTime = LumberJack.originalChainsawStartupTime
chainsaw.cutTimePerSquareMeter = LumberJack.originalDefaultCutDuration
chainsaw.maximumCutDiameter = LumberJack.originalMaximumCutDiameter
chainsaw.maximumDelimbDiameter = LumberJack.originalMaximumDelimbDiameter
end
-- DESTROY SMALL LOGS WHEN USING THE CHAINSAW --
if chainsaw.isCutting and chainsaw.currentCutState == 3 then
-- debugPrint("CHAINSAW CUTTING " .. tostring(chainsaw.currentCutState))
if not LumberJack.useChainsawFlag and not g_startMissionInfo.isMultiplayer then
if LumberJack.chainsawShape and entityExists(LumberJack.chainsawShape) then
local splitShape = LumberJack.chainsawShape
local volume = getVolume(splitShape)
if volume < 0.100 then
-- DELETE THE SHAPE if too small to worry about (e.g. felling wedge or thin branch)
LumberJack.deleteSplitShape(splitShape)
end
end
LumberJack.useChainsawFlag = true
end
LumberJack.createSawdust(chainsaw)
else
-- debugPrint("CHAINSAW NOT CUTTING " .. tostring(chainsaw.currentCutState))
local motor = handTool.spec_motorized
local isChainsawIdle = motor.currentRPM < 1.1*motor.minRPM
local isChainsawActive = motor.currentRPM > 0.8*motor.maxRPM
if not LumberJack.chainsawCanCut then
-- STUMP GRINDING
if not LumberJack.bushCuttingActive then
if LumberJack.closestObject and LumberJack.splitShape ~= LumberJack.closestObject then
local splitShape = LumberJack.closestObject.splitShape
if splitShape and entityExists(splitShape) then
LumberJack.splitShape = LumberJack.closestObject
else
LumberJack.splitShape = nil
end
end
if LumberJack.splitShape and entityExists(LumberJack.splitShape.id) then
if LumberJack.showDebug then
if LumberJack.splitShape.isStump then
g_currentMission:addExtraPrintText("Stump")
elseif LumberJack.splitShape.isTree then
g_currentMission:addExtraPrintText("Tree")
elseif LumberJack.splitShape.isBranch then
g_currentMission:addExtraPrintText("Branch")
end
end
if LumberJack.superStrength then
local shape = LumberJack.splitShape and LumberJack.splitShape.id
if shape and entityExists(shape) then
local rx, _, rz = getWorldTranslation(chainsaw.ringNode)
LumberJack.stumpGrindingPossible = handTool:testIfCutAllowed(shape, rx, rz)
if not LumberJack.stumpGrindingPossible then
debugPrint("Stump Grinding NOT allowed")
end
end
else
if LumberJack.splitShape.isStump then
local shape = LumberJack.splitShape and LumberJack.splitShape.id or 0
local x0,y0,z0 = getWorldTranslation(shape)
local y = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x0, y0, z0)
local lenBelow, lenAbove = getSplitShapePlaneExtents(shape, 0,y,0, 0,1,0)
if lenAbove < 1.5 then
local rx, _, rz = getWorldTranslation(chainsaw.ringNode)
LumberJack.stumpGrindingPossible = handTool:testIfCutAllowed(shape, rx, rz)
else
if LumberJack.showDebug then
g_currentMission:addExtraPrintText("Stump is too tall")
end
end
-- g_currentMission:addExtraPrintText(string.format("below:%.3f above:%.3f", lenBelow,lenAbove))
end
end
-- else
-- g_currentMission:addExtraPrintText("*** NO SPLITSHAPE AVAILABLE ***")
end
end
-- BUSH GRINDING
if LumberJack.destroyFoliageSize == 0 or LumberJack.stumpGrindingPossible or LumberJack.stumpGrindingActive then
LumberJack.bushCuttingPossible = false
else
LumberJack.updateTargetRaycast()
if not LumberJack.targetLocation or not LumberJack.targetOnGround then
LumberJack.bushCuttingPossible = false
elseif isChainsawIdle or LumberJack.superStrength then
local shape = LumberJack.targetedSplitShapeId or 0
local rx, rz = LumberJack.targetLocation[1], LumberJack.targetLocation[3]
LumberJack.bushCuttingPossible = handTool:testIfCutAllowed(shape, rx, rz) and LumberJack:seekAndDestroyFoliage(rx, rz)
end
end
if LumberJack.stumpGrindingPossible then
-- SHOW RED RING SELECTOR
local c = LumberJack.ringColour['red']
setShaderParameter(chainsaw.ringNode, "colorScale", c[1], c[2], c[3], c[4], false)
end
else
-- CHAINSAW HAS FOUND A PLACE TO CUT THE TREE
if LumberJack.chainsawShape and entityExists(LumberJack.chainsawShape) then
local shape = LumberJack.chainsawShape
if getVolume(shape) < 0.100 then
-- SHOW RED RING SELECTOR if too small to worry about (e.g. felling wedge or thin branch)
-- local c = LumberJack.ringColour.red
-- setShaderParameter(chainsaw.ringNode, "colorScale", c[1], c[2], c[3], c[4], false)
else
-- SHOW DIMENSIONS AFTER NEW CUT
local function getCutStartEnd(chainsaw)
local pos = {getWorldTranslation(chainsaw.ringNode)}
local scale = {getScale(chainsaw.ringNode)}
local dir = {localDirectionToWorld(chainsaw.ringNode, 0, 1, 0)}
local startPos = {
pos[1] - 0.5 * scale[1] * dir[1],
pos[2] - 0.5 * scale[2] * dir[2],
pos[3] - 0.5 * scale[3] * dir[3]
}
local endPos = {
pos[1] + 0.5 * scale[1] * dir[1],
pos[2] + 0.5 * scale[2] * dir[2],
pos[3] + 0.5 * scale[3] * dir[3]
}
return startPos[1], startPos[2], startPos[3], endPos[1], endPos[2], endPos[3]
end
local cutStartX, cutStartY, cutStartZ, cutEndX, cutEndY, cutEndZ = getCutStartEnd(chainsaw)
local x0, y0, z0 = (cutStartX+cutEndX)/2, (cutStartY+cutEndY)/2, (cutStartZ+cutEndZ)/2
local below, above = getSplitShapePlaneExtents(shape, x0, y0, z0, localDirectionToWorld(shape, 0, 1, 0))
g_currentMission:addExtraPrintText(g_i18n:getText("infohud_length") .. string.format(": %.1fm | %.1fm", below, above))
-- if LumberJack.showDebug then
-- drawDebugLine(cutStartX,cutStartY,cutStartZ,1,1,1,cutEndX,cutEndY,cutEndZ,1,1,1)
-- end
end
end
end
-- GRIND STUMPS USING THE CHAINSAW --
if LumberJack.stumpGrindingPossible and isChainsawActive then
LumberJack.stumpGrindingActive = true
LumberJack.stumpGrindingTime = (LumberJack.stumpGrindingTime or 0) + dt
if LumberJack.stumpGrindingTime < 3000 then
-- STUMP GRINDING
local shape = LumberJack.splitShape and LumberJack.splitShape.id
if shape and entityExists(shape) then
local target = {getWorldTranslation(shape)}
if LumberJack.splitShape.isStump then
target[2] = target[2] + 0.5
elseif LumberJack.splitShape.isTree then
target[2] = target[2] + 1.0
elseif LumberJack.splitShape.isBranch then
target = LumberJack.targetLocation
end
setShaderParameter(chainsaw.ringNode, "colorScale", 0, 0, 0, 0, false)
if target then
local cutTranslation = {worldToLocal(handTool.graphicalNodeParent, target[1], target[2], target[3])}
setTranslation(handTool.graphicalNode, cutTranslation[1]/3, cutTranslation[2]/3, cutTranslation[3]/3)
end
--handTool:updateParticles()
LumberJack.createSawdust(chainsaw, 0, target)
end
else
if LumberJack.splitShape then
debugPrint("DELETE SPLIT SHAPE " .. LumberJack.splitShape.id)
if LumberJack.deleteSplitShape(LumberJack.splitShape.id) then
LumberJack.splitShape = nil
end
end
LumberJack.stumpGrindingTime = nil
LumberJack.stumpGrindingActive = false
LumberJack.stumpGrindingPossible = false
LumberJack.createSawdust(chainsaw, -2)
end
elseif LumberJack.bushCuttingPossible and isChainsawActive then
LumberJack.bushCuttingActive = true
LumberJack.stumpGrindingTime = (LumberJack.stumpGrindingTime or 0) + dt
if (LumberJack.superStrength and LumberJack.stumpGrindingTime < 100)
or (not LumberJack.superStrength and LumberJack.stumpGrindingTime < 1000) then
local target = LumberJack.targetLocation
local cutTranslation = {worldToLocal(handTool.graphicalNodeParent, target[1], target[2], target[3])}
setTranslation(handTool.graphicalNode, cutTranslation[1]/5, cutTranslation[2]/5, cutTranslation[3]/5)
--handTool:updateParticles()
else
debugPrint("Bush Cutting - DELETE BUSH")
local rx, rz = LumberJack.targetLocation[1], LumberJack.targetLocation[3]
LumberJack:seekAndDestroyFoliage(rx, rz, true)
LumberJack.stumpGrindingTime = nil
LumberJack.bushCuttingPossible = false
end
else
LumberJack.stumpGrindingTime = nil
LumberJack.bushCuttingActive = false
LumberJack.stumpGrindingActive = false
end
if LumberJack.useChainsawFlag then
LumberJack.useChainsawFlag = false
local abortedCut = LumberJack.chainsawShape and entityExists(LumberJack.chainsawShape)
if abortedCut then
debugPrint("ABORTED CUT")
LumberJack.createSawdust(chainsaw, -2)
else
-- debugPrint("COMPLETED CUT")
LumberJack.createSawdust(chainsaw, -1)
end
end
LumberJack.chainsawShape = nil
LumberJack.chainsawCanCut = nil
end
end
end
function LumberJack.moveChainsawCameraFocus(chainsaw, x0, y0, z0)
if chainsaw ~= nil and chainsaw.chainsawCameraFocus then
local x,y,z = worldToLocal(getParent(chainsaw.chainsawCameraFocus), x0,y0,z0)
setTranslation(chainsaw.chainsawCameraFocus, x,y,z)
end
end
function LumberJack.resetRingSelector(chainsaw)
if chainsaw ~= nil and chainsaw.ringSelector then
local x,y,z = getWorldTranslation(chainsaw.chainsawCameraFocus)
setWorldTranslation(chainsaw.ringSelector, x, y, z)
setScale(chainsaw.ringSelector, 1, 1, 1)
end
end
BaseMission.loadMap = Utils.overwrittenFunction(BaseMission.loadMap,
function(self, superFunc, filename, ...)
LumberJack.mapI3dFilename = filename
superFunc(self, filename, ...)
end
)
function LumberJack.getDecoFunctionData()
local functionData = LumberJack.decoFunctionData
local function createFoliageModifier(foliage, terrainRootNode)
if foliage.terrainDataPlaneId and foliage.startStateChannel and foliage.numStateChannels and terrainRootNode then
local modifier = DensityMapModifier.new(foliage.terrainDataPlaneId, foliage.startStateChannel, foliage.numStateChannels, terrainRootNode)
modifier:setNewTypeIndexMode(DensityIndexCompareMode.ZERO)
local filter = DensityMapFilter.new(foliage.terrainDataPlaneId, foliage.startStateChannel, foliage.numStateChannels, terrainRootNode)
filter:setValueCompareParams(DensityValueCompareType.GREATER, 0)
return {modifier = modifier, filter = filter}
end
end
if functionData == nil then
local terrainRootNode = g_currentMission.terrainRootNode
local decoFoliages = g_currentMission.foliageSystem.decoFoliages
local paintableFoliages = g_currentMission.foliageSystem.paintableFoliages
functionData = {
foliageIsBush = {},
foliageModifiers = {},
foliageMultilayers = {},
foliagesByName = {}
}
local modifiers = functionData.foliageModifiers
local foliagesByName = functionData.foliagesByName
debugPrint("DECO FOLIAGES")
for index, foliage in ipairs(decoFoliages) do
if foliage and foliage.layerName then
debugPrint(tostring(foliage.terrainDataPlaneId) .. " [" .. tostring(index) .. "] : " .. tostring(foliage.layerName))
modifiers[foliage] = modifiers[foliage] or createFoliageModifier(foliage, terrainRootNode)
foliagesByName[foliage.layerName] = foliagesByName[foliage.layerName] or foliage
end
end
debugPrint("PAINTABLE FOLIAGES")
for index, foliage in ipairs(paintableFoliages) do
if foliage and foliage.layerName then
debugPrint(tostring(foliage.terrainDataPlaneId) .. " [" .. tostring(index) .. "] : " .. tostring(foliage.layerName))
modifiers[foliage] = modifiers[foliage] or createFoliageModifier(foliage, terrainRootNode)
foliagesByName[foliage.layerName] = foliagesByName[foliage.layerName] or foliage
end
end
if LumberJack.mapI3dFilename then
local i = 1
debugPrint("search for bush layers in : " .. tostring(LumberJack.mapI3dFilename))
local xmlFile = XMLFile.load("MapI3d", LumberJack.mapI3dFilename)
if xmlFile then
local rootKey = "i3D.Scene.TerrainTransformGroup.Layers.FoliageSystem"
while true do
local layerKey = string.format(rootKey..".FoliageMultiLayer(%d)", i-1)
if not xmlFile:hasProperty(layerKey) then
break
end
local densityMapId = xmlFile:getInt(layerKey.."#densityMapId")
local numTypeIndexChannels = xmlFile:getInt(layerKey.."#numTypeIndexChannels")
debugPrint("["..i.."] densityMapId: " .. tostring(densityMapId))
functionData.foliageIsBush[i] = {}
functionData.foliageMultilayers[i] = {}
functionData.foliageMultilayers[i].densityMapId = densityMapId
functionData.foliageMultilayers[i].numTypeIndexChannels = numTypeIndexChannels
local j = 1
while true do
local typeKey = string.format(layerKey..".FoliageType(%d)", j-1)
if not xmlFile:hasProperty(typeKey) then
break
end
local isBush = false
local layerName = xmlFile:getString(typeKey.."#name", "MISSING")
for _, name in pairs(LumberJack.foliageSearchNames) do
if string.find(layerName:lower(), name) then
isBush = true
local foliage = foliagesByName[layerName]
if foliage then
functionData.foliageIsBush[i][j] = foliage
if not functionData.foliageMultilayers[i].terrainDataPlaneId then
functionData.foliageMultilayers[i].terrainDataPlaneId = foliage.terrainDataPlaneId
end
end
end
end
debugPrint(" j="..j.." : " .. tostring(layerName))
j = j + 1
end
i = i + 1
end
xmlFile:delete()
else
debugPrint("COULD NOT LOAD MAP FOR FOLIAGE LAYERS")
end
LumberJack.decoFunctionData = functionData
end
end
return functionData
end
function LumberJack:removeDeco(startWorldX, startWorldZ, areaSize, destroyAll)
local functionData = LumberJack.getDecoFunctionData()
if functionData ~= nil then
local h = areaSize/2
for layer, layerData in pairs(functionData.foliageMultilayers) do
local numChannels = layerData.numTypeIndexChannels
local decoFoliageId = layerData.terrainDataPlaneId
if not decoFoliageId or not numChannels then
break
end
for index, foliage in pairs(functionData.foliageIsBush[layer]) do
local modifiers = functionData.foliageModifiers[foliage]
if modifiers.modifier and modifiers.filter then
modifiers.modifier:setParallelogramWorldCoords(startWorldX-h, startWorldZ-h, startWorldX+h, startWorldZ-h, startWorldX-h, startWorldZ+h, DensityCoordType.POINT_POINT_POINT)
modifiers.modifier:executeSet(0, modifiers.filter)
end
end
end
end
end
function LumberJack:seekAndDestroyFoliage(startWorldX, startWorldZ, destroy)
local functionData = LumberJack.getDecoFunctionData()
if functionData ~= nil then
if not functionData.foliageMultilayers then
debugPrint("NO MULTILAYERS FOUND")
return
end
if destroy and LumberJack.destroyAllFoliage then
debugPrint("DESTROY ALL FOLIAGE")
LumberJack:removeDeco(startWorldX, startWorldZ, LumberJack.destroyFoliageSize, destroy)
return
end
local foundAny = false
for layer, layerData in pairs(functionData.foliageMultilayers) do
local numChannels = layerData.numTypeIndexChannels
local decoFoliageId = layerData.terrainDataPlaneId
if not decoFoliageId or not numChannels then
break
end
local squareSize = 0.05
local areaSize = LumberJack.destroyFoliageSize
local numSquares = math.ceil(areaSize / squareSize)
local offset = (areaSize - (numSquares * squareSize)) / 2
for i = 0, numSquares-1 do
for j = 0, numSquares-1 do
local found = false
local rx = startWorldX-areaSize/2 + i*squareSize + offset
local rz = startWorldZ-areaSize/2 + j*squareSize + offset
local bits = getDensityAtWorldPos(decoFoliageId, rx+squareSize/2, 0, rz+squareSize/2)
local index = numChannels == 0 and 1 or bitAND(bits, 2^numChannels - 1)
local value = bitShiftRight(bits, numChannels)
if value > 0 and (functionData.foliageIsBush[layer][index] or LumberJack.destroyAllFoliage) then
found = true
foundAny = true
end
if found and destroy then
LumberJack:removeDeco(rx, rz, squareSize)
end
if LumberJack.showDebug then
local d = 0.025*squareSize
if found then
DebugUtil.drawDebugAreaRectangle(rx+d,0,rz+d, rx+squareSize-d,0,rz+d, rx+d,0,rz+squareSize-d, true, 0,0,1)
end
end
end
end
if LumberJack.showDebug then
local n=LumberJack.destroyFoliageSize
local scale = g_currentMission.terrainSize / getDensityMapSize(decoFoliageId)
DebugUtil.drawDebugAreaRectangle(startWorldX-n/2,0,startWorldZ-n/2, startWorldX+n/2,0,startWorldZ-n/2, startWorldX-n/2,0,startWorldZ+n/2, true, 1,1,1)
for x = -n, n do
for z = -n, n do
local rx,rz = math.floor((startWorldX+x*scale)/scale)*scale, math.floor((startWorldZ+z*scale)/scale)*scale
local bits = getDensityAtWorldPos(decoFoliageId, startWorldX+x*scale, 0, startWorldZ+z*scale)
local index = numChannels == 0 and 1 or bitAND(bits, 2^numChannels - 1)
local value = bitShiftRight(bits, numChannels)
local valueString = string.format("%d - %d", index, value)
local d = 0.025
local yg = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, rx+scale/2,0,rz+scale/2)
if value > 0 and (functionData.foliageIsBush[layer][index] or LumberJack.destroyAllFoliage) then
DebugUtil.drawDebugAreaRectangle(rx+d,0,rz+d, rx+scale-d,0,rz+d, rx+d,0,rz+scale-d, true, 0,1,0)
-- Utils.renderTextAtWorldPosition(rx+scale/2, yg+0.1, rz+scale/2, valueString, getCorrectTextSize(0.012), 0, {1,1,1})
else
DebugUtil.drawDebugAreaRectangle(rx+d,0,rz+d, rx+scale-d,0,rz+d, rx+d,0,rz+scale-d, true, 0.15,0.15,0.15)
-- Utils.renderTextAtWorldPosition(rx+scale/2, yg+0.1, rz+scale/2, valueString, getCorrectTextSize(0.012), 0, {0.3,0.3,0.3})
end
end
end
end
if LumberJack.showDebug and foundAny then
g_currentMission:addExtraPrintText("Bush")
end
end
return foundAny
end
end
function LumberJack.getClosestTarget(player)
if player.currentHandTool and player.currentHandTool.spec_chainsaw then
local chainsaw = player.currentHandTool.spec_chainsaw
local chainsawCutting = chainsaw.isCutting and chainsaw.currentCutState == 3
if chainsawCutting or LumberJack.bushCuttingActive or LumberJack.stumpGrindingActive then
-- g_currentMission:addExtraPrintText("chainsaw cutting - NO UPDATE")
return
end
end
LumberJack.closestObject = nil
if not player.targeter.closestTargetsByKey then
-- debugPrint("NO closestTargetsByKey")
return
end
local size = 0
local distance = math.huge
for k, v in pairs(player.targeter.closestTargetsByKey) do
size = size + 1
if distance > v.distance then
LumberJack.closestObject = LumberJack.closestObject or {}
LumberJack.closestObject.id = v.node
LumberJack.closestObject.distance = v.distance
local splitShapeId, isTree, isStump, isBranch = LumberJack:getSplitShape(v.node)
LumberJack.closestObject.splitShape = splitShapeId
LumberJack.closestObject.isTree = isTree
LumberJack.closestObject.isStump = isStump
LumberJack.closestObject.isBranch = isBranch
end
end
if LumberJack.closestObject and entityExists(LumberJack.closestObject.id) then
if LumberJack.closestObject.id ~= LumberJack.lastClosestObjectId then
LumberJack.lastClosestObjectId = LumberJack.closestObject.id
if LumberJack.closestObject.splitShape then
-- TODO: client may not know these?
if LumberJack.closestObject.isTree then
debugPrint("FOUND TREE: " .. tostring(LumberJack.closestObject.id))
elseif LumberJack.closestObject.isStump then
debugPrint("FOUND STUMP: " .. tostring(LumberJack.closestObject.id))
elseif LumberJack.closestObject.isBranch then
debugPrint("FOUND BRANCH: " .. tostring(LumberJack.closestObject.id))
end
if LumberJack.closestObject.splitShape ~= LumberJack.closestObject.id then
debugPrint("split shape id = " .. LumberJack.closestObject.splitShape)
end
else
-- debugPrint("FOUND OBJECT: " .. tostring(LumberJack.closestObject.id))
end
-- debugPrint(size .. " objects in range")
end
return LumberJack.closestObject
end
end
function LumberJack:doUpdate(dt)
local masterServer = g_masterServerConnection.masterServerCallbackTarget
local isSaving = masterServer.isSaving
local isLoadingMap = masterServer.isLoadingMap
local isExitingGame = masterServer.isExitingGame
local isSynchronizingWithPlayers = masterServer.isSynchronizingWithPlayers
if isLoadingMap or isExitingGame or isSaving or isSynchronizingWithPlayers then
return
end
LumberJack.startupTime = LumberJack.startupTime or 0
if LumberJack.startupTime < 1000 then
LumberJack.startupTime = LumberJack.startupTime + dt
-- debugPrint("wait for startup.." .. LumberJack.startupTime)
return
end
-- Dedicated Server has no player
if not g_localPlayer then
debugPrint("Warning: no player ID")
return
end
local player = g_localPlayer
local isInVehicle = player:getIsInVehicle()