forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultweapons.lua
More file actions
1338 lines (1157 loc) · 47.8 KB
/
defaultweapons.lua
File metadata and controls
1338 lines (1157 loc) · 47.8 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
-----------------------------------------------------------------
-- File : /lua/sim/DefaultWeapons.lua
-- Author(s): John Comes
-- Summary : Default definitions of weapons
-- Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
-----------------------------------------------------------------
local Weapon = import('/lua/sim/Weapon.lua').Weapon
local CollisionBeam = import('/lua/sim/CollisionBeam.lua').CollisionBeam
local Game = import('/lua/game.lua')
local CalculateBallisticAcceleration = import('/lua/sim/CalcBallisticAcceleration.lua').CalculateBallisticAcceleration
-- Most weapons derive from this class, including beam weapons later in this file
DefaultProjectileWeapon = Class(Weapon) {
FxRackChargeMuzzleFlash = {},
FxRackChargeMuzzleFlashScale = 1,
FxChargeMuzzleFlash = {},
FxChargeMuzzleFlashScale = 1,
FxMuzzleFlash = {
'/effects/emitters/default_muzzle_flash_01_emit.bp',
'/effects/emitters/default_muzzle_flash_02_emit.bp',
},
FxMuzzleFlashScale = 1,
-- Called when the weapon is created, almost always when the owning unit is created
OnCreate = function(self)
Weapon.OnCreate(self)
local bp = self:GetBlueprint()
local rof = self:GetWeaponRoF()
self.WeaponCanFire = true
if bp.RackRecoilDistance ~= 0 then
self.RecoilManipulators = {}
end
-- Make certain the weapon has essential aspects defined
if not bp.RackBones then
local strg = '*ERROR: No RackBones table specified, aborting weapon setup. Weapon: ' .. bp.DisplayName .. ' on Unit: ' .. self.unit.UnitId
error(strg, 2)
return
end
if not bp.MuzzleSalvoSize then
local strg = '*ERROR: No MuzzleSalvoSize specified, aborting weapon setup. Weapon: ' .. bp.DisplayName .. ' on Unit: ' .. self.unit.UnitId
error(strg, 2)
return
end
if not bp.MuzzleSalvoDelay then
local strg = '*ERROR: No MuzzleSalvoDelay specified, aborting weapon setup. Weapon: ' .. bp.DisplayName .. ' on Unit: ' .. self.unit.UnitId
error(strg, 2)
return
end
self.CurrentRackSalvoNumber = 1
-- Calculate recoil speed so that it finishes returning just as the next shot is ready
if bp.RackRecoilDistance ~= 0 then
local dist = bp.RackRecoilDistance
if bp.RackBones[1].TelescopeRecoilDistance then
local tpDist = bp.RackBones[1].TelescopeRecoilDistance
if math.abs(tpDist) > math.abs(dist) then
dist = tpDist
end
end
self.RackRecoilReturnSpeed = bp.RackRecoilReturnSpeed or math.abs(dist / ((1 / rof) - (bp.MuzzleChargeDelay or 0))) * 1.25
end
-- Ensure firing cycle is compatible internally
self.NumMuzzles = 0
for rk, rv in bp.RackBones do
self.NumMuzzles = self.NumMuzzles + table.getn(rv.MuzzleBones or 0)
end
self.NumMuzzles = self.NumMuzzles / table.getn(bp.RackBones)
local totalMuzzleFiringTime = (self.NumMuzzles - 1) * bp.MuzzleSalvoDelay
if totalMuzzleFiringTime > (1 / rof) then
local strg = '*ERROR: The total time to fire muzzles is longer than the RateOfFire allows, aborting weapon setup. Weapon: ' .. bp.DisplayName .. ' on Unit: ' .. self.unit.UnitId
error(strg, 2)
return false
end
if bp.RackRecoilDistance ~= 0 and bp.MuzzleSalvoDelay ~= 0 then
local strg = '*ERROR: You can not have a RackRecoilDistance with a MuzzleSalvoDelay not equal to 0, aborting weapon setup. Weapon: ' .. bp.DisplayName .. ' on Unit: ' .. self.unit.UnitId
error(strg, 2)
return false
end
if bp.EnergyChargeForFirstShot == false then
self.FirstShot = true
end
-- Set the firing cycle progress bar to full if required
if bp.RenderFireClock then
self.unit:SetWorkProgress(1)
end
ChangeState(self, self.IdleState)
end,
-- This function creates the projectile, and happens when the unit is trying to fire
-- Called from inside RackSalvoFiringState
CreateProjectileAtMuzzle = function(self, muzzle)
local proj = self:CreateProjectileForWeapon(muzzle)
if not proj or proj:BeenDestroyed() then
return proj
end
local bp = self:GetBlueprint()
if bp.DetonatesAtTargetHeight == true then
local pos = self:GetCurrentTargetPos()
if pos then
local theight = GetSurfaceHeight(pos[1], pos[3])
local hght = pos[2] - theight
proj:ChangeDetonateAboveHeight(hght)
end
end
if bp.Flare then
proj:AddFlare(bp.Flare)
end
if self.unit:GetCurrentLayer() == 'Water' and bp.Audio.FireUnderWater then
self:PlaySound(bp.Audio.FireUnderWater)
elseif bp.Audio.Fire then
self:PlaySound(bp.Audio.Fire)
end
self:CheckBallisticAcceleration(proj) -- Check weapon blueprint for trajectory fix request
return proj
end,
-- Used mainly for Bomb drop physics calculations
CheckBallisticAcceleration = function(self, proj)
local bp = self:GetBlueprint()
if bp.FixBombTrajectory then
local acc = CalculateBallisticAcceleration(self, proj)
proj:SetBallisticAcceleration(-acc) -- Change projectile trajectory so it hits the target
end
end,
-- Triggers when the weapon is moved horizontally, usually by owner's motion
OnMotionHorzEventChange = function(self, new, old)
Weapon.OnMotionHorzEventChange(self, new, old)
-- Handle weapons which must pack before moving
local bp = self:GetBlueprint()
if bp.WeaponUnpackLocksMotion == true and old == 'Stopped' then
self:PackAndMove()
end
-- Handle motion-triggered FiringRandomness changes
if old == 'Stopped' then
if bp.FiringRandomnessWhileMoving then
self:SetFiringRandomness(bp.FiringRandomnessWhileMoving)
end
elseif new == 'Stopped' and bp.FiringRandomnessWhileMoving then
self:SetFiringRandomness(bp.FiringRandomness)
end
end,
-- Called on horizontal motion event
PackAndMove = function(self)
ChangeState(self, self.WeaponPackingState)
end,
-- Create an economy event for those weapons which require Energy to fire
StartEconomyDrain = function(self)
if self.FirstShot then return end
if self.unit:GetFractionComplete() ~= 1 then return end
local bp = self:GetBlueprint()
if not self.EconDrain and bp.EnergyRequired and bp.EnergyDrainPerSecond then
local nrgReq = self:GetWeaponEnergyRequired()
local nrgDrain = self:GetWeaponEnergyDrain()
if nrgReq > 0 and nrgDrain > 0 then
local time = nrgReq / nrgDrain
if time < 0.1 then
time = 0.1
end
self.EconDrain = CreateEconomyEvent(self.unit, nrgReq, 0, time)
self.FirstShot = true
self.unit:ForkThread(function()
WaitFor(self.EconDrain)
RemoveEconomyEvent(self.unit, self.EconDrain)
self.EconDrain = nil
end)
end
end
end,
-- Determine how much Energy is required to fire
GetWeaponEnergyRequired = function(self)
local bp = self:GetBlueprint()
local weapNRG = (bp.EnergyRequired or 0) * (self.AdjEnergyMod or 1)
if weapNRG < 0 then
weapNRG = 0
end
return weapNRG
end,
-- Determine how much Energy should be drained per second
GetWeaponEnergyDrain = function(self)
local bp = self:GetBlueprint()
local weapNRG = (bp.EnergyDrainPerSecond or 0) * (self.AdjEnergyMod or 1)
return weapNRG
end,
GetWeaponRoF = function(self)
local bp = self:GetBlueprint()
return bp.RateOfFire / (self.AdjRoFMod or 1)
end,
-- Effect Functions Section
-- Play visual effects, animations, recoil etc
-- Played when a muzzle is fired. Mostly used for muzzle flashes
PlayFxMuzzleSequence = function(self, muzzle)
local bp = self:GetBlueprint()
for k, v in self.FxMuzzleFlash do
CreateAttachedEmitter(self.unit, muzzle, self.unit.Army, v):ScaleEmitter(self.FxMuzzleFlashScale)
end
end,
-- Played during the beginning of the MuzzleChargeDelay time when a muzzle in a rack is fired.
PlayFxMuzzleChargeSequence = function(self, muzzle)
local bp = self:GetBlueprint()
for k, v in self.FxChargeMuzzleFlash do
CreateAttachedEmitter(self.unit, muzzle, self.unit.Army, v):ScaleEmitter(self.FxChargeMuzzleFlashScale)
end
end,
-- Played when a rack salvo charges
-- Do not wait in here or the sequence in the blueprint will be messed up. Fork a thread instead
PlayFxRackSalvoChargeSequence = function(self)
local bp = self:GetBlueprint()
for k, v in self.FxRackChargeMuzzleFlash do
for ek, ev in bp.RackBones[self.CurrentRackSalvoNumber].MuzzleBones do
CreateAttachedEmitter(self.unit, ev, self.unit.Army, v):ScaleEmitter(self.FxRackChargeMuzzleFlashScale)
end
end
if bp.Audio.ChargeStart then
self:PlaySound(bp.Audio.ChargeStart)
end
if bp.AnimationCharge and not self.Animator then
self.Animator = CreateAnimator(self.unit)
self.Animator:PlayAnim(self:GetBlueprint().AnimationCharge):SetRate(bp.AnimationChargeRate or 1)
end
end,
-- Played when a rack salvo reloads
-- Do not wait in here or the sequence in the blueprint will be messed up. Fork a thread instead
PlayFxRackSalvoReloadSequence = function(self)
local bp = self:GetBlueprint()
if bp.AnimationReload and not self.Animator then
self.Animator = CreateAnimator(self.unit)
self.Animator:PlayAnim(self:GetBlueprint().AnimationReload):SetRate(bp.AnimationReloadRate or 1)
end
end,
-- Played when a rack reloads. Mostly used for Recoil
PlayFxRackReloadSequence = function(self)
local bp = self:GetBlueprint()
if bp.CameraShakeRadius and bp.CameraShakeMax and bp.CameraShakeMin and bp.CameraShakeDuration and
bp.CameraShakeRadius > 0 and bp.CameraShakeMax > 0 and bp.CameraShakeMin >= 0 and bp.CameraShakeDuration > 0 then
self.unit:ShakeCamera(bp.CameraShakeRadius, bp.CameraShakeMax, bp.CameraShakeMin, bp.CameraShakeDuration)
end
if bp.ShipRock == true then
local ix,iy,iz = self.unit:GetBoneDirection(bp.RackBones[self.CurrentRackSalvoNumber].RackBone)
self.unit:RecoilImpulse(-ix,-iy,-iz)
end
if bp.RackRecoilDistance ~= 0 then
self:PlayRackRecoil({bp.RackBones[self.CurrentRackSalvoNumber]})
end
end,
-- Played when a weapon unpacks
PlayFxWeaponUnpackSequence = function(self)
-- Deal with owner's audio cues
local unitBP = self.unit:GetBlueprint()
if unitBP.Audio.Activate then
self:PlaySound(unitBP.Audio.Activate)
end
if unitBP.Audio.Open then
self:PlaySound(unitBP.Audio.Open)
end
-- Deal with the Weapon's audio and animations
local bp = self:GetBlueprint()
if bp.Audio.Unpack then
self:PlaySound(bp.Audio.Unpack)
end
if bp.WeaponUnpackAnimation and not self.UnpackAnimator then
self.UnpackAnimator = CreateAnimator(self.unit)
self.UnpackAnimator:PlayAnim(bp.WeaponUnpackAnimation):SetRate(0)
self.UnpackAnimator:SetPrecedence(bp.WeaponUnpackAnimatorPrecedence or 0)
self.unit.Trash:Add(self.UnpackAnimator)
end
if self.UnpackAnimator then
self.UnpackAnimator:SetRate(bp.WeaponUnpackAnimationRate)
WaitFor(self.UnpackAnimator)
end
end,
-- Played when a weapon packs up
-- There is no target, and all rack salvos are complete
PlayFxWeaponPackSequence = function(self)
local bp = self:GetBlueprint()
local unitBP = self.unit:GetBlueprint()
if unitBP.Audio.Close then
self:PlaySound(unitBP.Audio.Close)
end
if bp.WeaponUnpackAnimation and self.UnpackAnimator then
self.UnpackAnimator:SetRate(-bp.WeaponUnpackAnimationRate)
end
if self.UnpackAnimator then
WaitFor(self.UnpackAnimator)
end
end,
-- Create the visual side of rack recoil
PlayRackRecoil = function(self, rackList)
local bp = self:GetBlueprint()
for k, v in rackList do
local tmpSldr = CreateSlider(self.unit, v.RackBone)
table.insert(self.RecoilManipulators, tmpSldr)
tmpSldr:SetPrecedence(11)
tmpSldr:SetGoal(0, 0, bp.RackRecoilDistance)
tmpSldr:SetSpeed(-1)
self.unit.Trash:Add(tmpSldr)
if v.TelescopeBone then
tmpSldr = CreateSlider(self.unit, v.TelescopeBone)
table.insert(self.RecoilManipulators, tmpSldr)
tmpSldr:SetPrecedence(11)
tmpSldr:SetGoal(0, 0, v.TelescopeRecoilDistance or bp.RackRecoilDistance)
tmpSldr:SetSpeed(-1)
self.unit.Trash:Add(tmpSldr)
end
end
self:ForkThread(self.PlayRackRecoilReturn, rackList)
end,
-- The opposite function to PlayRackRecoil, returns the rack to default position
PlayRackRecoilReturn = function(self, rackList)
WaitTicks(1)
for k, v in rackList do
for mk, mv in self.RecoilManipulators do
mv:SetGoal(0, 0, 0)
mv:SetSpeed(self.RackRecoilReturnSpeed)
end
end
end,
-- Wait for all recoil and animations
WaitForAndDestroyManips = function(self)
local manips = self.RecoilManipulators
if manips then
for k, v in manips do
WaitFor(v)
end
self:DestroyRecoilManips()
end
if self.Animator then
WaitFor(self.Animator)
self.Animator:Destroy()
self.Animator = nil
end
end,
-- Destroy the sliders which cause weapon visual recoil
DestroyRecoilManips = function(self)
local manips = self.RecoilManipulators
if manips then
for k, v in manips do
v:Destroy()
end
self.RecoilManipulators = {}
end
end,
-- Should be called whenever a target is lost
-- Includes the manual selection of a new target, and the issuing of a move order
OnLostTarget = function(self)
-- Issue 43
-- Tell the owner this weapon has lost the target
if self.unit then
self.unit:OnLostTarget(self)
end
Weapon.OnLostTarget(self)
local bp = self:GetBlueprint()
if bp.WeaponUnpacks == true then
ChangeState(self, self.WeaponPackingState)
else
ChangeState(self, self.IdleState)
end
end,
-- Sends the weapon to DeadState, probably called by the Owner
OnDestroy = function(self)
ChangeState(self, self.DeadState)
end,
-- Checks to see if the weapon is allowed to fire
CanWeaponFire = function(self)
return self.WeaponCanFire
end,
-- Present for Overcharge to hook into
OnWeaponFired = function(self)
end,
-- I think this is triggered whenever the state changes to anything but DeadState
OnEnterState = function(self)
if self.WeaponWantEnabled and not self.WeaponIsEnabled then
self.WeaponIsEnabled = true
self:SetWeaponEnabled(true)
elseif not self.WeaponWantEnabled and self.WeaponIsEnabled then
local bp = self:GetBlueprint()
if bp.CountedProjectile ~= true then
self.WeaponIsEnabled = false
self:SetWeaponEnabled(false)
end
end
if self.WeaponAimWantEnabled and not self.WeaponAimIsEnabled then
self.WeaponAimIsEnabled = true
self:AimManipulatorSetEnabled(true)
elseif not self.WeaponAimWantEnabled and self.WeaponAimIsEnabled then
self.WeaponAimIsEnabled = false
self:AimManipulatorSetEnabled(false)
end
end,
-- Weapon States
-- Idle state is when the weapon has no target and is done with any animations or unpacking
IdleState = State {
WeaponWantEnabled = true,
WeaponAimWantEnabled = true,
Main = function(self)
if self.unit.Dead then return end
self.unit:SetBusy(false)
self:WaitForAndDestroyManips()
local bp = self:GetBlueprint()
if not bp.RackBones then
error('Error on rackbones ' .. self.unit.UnitId)
end
for k, v in bp.RackBones do
if v.HideMuzzle == true then
for mk, mv in v.MuzzleBones do
self.unit:ShowBone(mv, true)
end
end
end
self:StartEconomyDrain()
if table.getn(bp.RackBones) > 1 and self.CurrentRackSalvoNumber > 1 then
WaitSeconds(self:GetBlueprint().RackReloadTimeout)
self:PlayFxRackSalvoReloadSequence()
self.CurrentRackSalvoNumber = 1
end
end,
OnGotTarget = function(self)
Weapon.OnGotTarget(self)
local bp = self:GetBlueprint()
-- Issue 43
if self.unit then
self.unit:OnGotTarget(self)
end
if bp.WeaponUnpackLockMotion ~= true or (bp.WeaponUnpackLocksMotion == true and not self.unit:IsUnitState('Moving')) then
if bp.CountedProjectile == true and not self:CanFire() then
return
end
if bp.WeaponUnpacks == true then
ChangeState(self, self.WeaponUnpackingState)
else
if bp.RackSalvoChargeTime and bp.RackSalvoChargeTime > 0 then
ChangeState(self, self.RackSalvoChargeState)
else
ChangeState(self, self.RackSalvoFireReadyState)
end
end
end
end,
OnFire = function(self)
local bp = self:GetBlueprint()
if bp.WeaponUnpacks == true then
ChangeState(self, self.WeaponUnpackingState)
else
if bp.RackSalvoChargeTime and bp.RackSalvoChargeTime > 0 then
ChangeState(self, self.RackSalvoChargeState)
-- SkipReadyState used for Janus and Corsair
elseif bp.SkipReadyState and bp.SkipReadyState == true then
ChangeState(self, self.RackSalvoFiringState)
else
ChangeState(self, self.RackSalvoFireReadyState)
end
end
end,
},
-- This state is for when the weapon is charging before firing
RackSalvoChargeState = State {
WeaponWantEnabled = true,
WeaponAimWantEnabled = true,
Main = function(self)
self.unit:SetBusy(true)
self:PlayFxRackSalvoChargeSequence()
local bp = self:GetBlueprint()
if bp.NotExclusive then
self.unit:SetBusy(false)
end
WaitSeconds(self:GetBlueprint().RackSalvoChargeTime)
if bp.NotExclusive then
self.unit:SetBusy(true)
end
if bp.RackSalvoFiresAfterCharge == true then
ChangeState(self, self.RackSalvoFiringState)
else
ChangeState(self, self.RackSalvoFireReadyState)
end
end,
OnFire = function(self)
end,
},
-- This state is for when the weapon is ready to fire
RackSalvoFireReadyState = State {
WeaponWantEnabled = true,
WeaponAimWantEnabled = true,
Main = function(self)
-- We change the state on counted projectiles because we won't get another OnFire call.
-- The second part is a hack for units with reload animations. They have the same problem
-- they need a RackSalvoReloadTime that's 1/RateOfFire set to avoid firing twice on the first shot
local bp = self:GetBlueprint()
if bp.CountedProjectile == true and bp.WeaponUnpacks == true then
self.unit:SetBusy(true)
else
self.unit:SetBusy(false)
end
self.WeaponCanFire = true
if self.EconDrain then
self.WeaponCanFire = false
WaitFor(self.EconDrain)
self.WeaponCanFire = true
end
-- This code has the effect of forcing a unit to wait on its firing state to change from Hold Fire
-- before resuming the sequence from this point
-- Introduced to fix a bug where units with this bp flag would go straight to projectile creation
-- from OnGotTarget, without waiting for OnFire() to be called from engine.
if bp.CountedProjectile == true or bp.AnimationReload then
if self.unit:GetFireState() == 1 then
while self.unit:GetFireState() == 1 do
WaitTicks(1)
end
end
ChangeState(self, self.RackSalvoFiringState)
end
-- To prevent weapon getting stuck targeting something out of fire range but withing tracking radius
WaitSeconds(5)
-- Check if there is a better target nearby
self:ResetTarget()
end,
OnFire = function(self)
if self.WeaponCanFire then
ChangeState(self, self.RackSalvoFiringState)
end
end,
},
-- This state is for when the weapon is actually in the process of firing
RackSalvoFiringState = State {
WeaponWantEnabled = true,
WeaponAimWantEnabled = true,
-- Render the fire recharge bar
RenderClockThread = function(self, rof)
local clockTime = math.round(10*rof)
local totalTime = clockTime
while clockTime >= 0 and
not self:BeenDestroyed() and
not self.unit.Dead do
self.unit:SetWorkProgress(1 - clockTime / totalTime)
clockTime = clockTime - 1
WaitSeconds(0.1)
end
end,
Main = function(self)
self.unit:SetBusy(true)
self:DestroyRecoilManips()
local bp = self:GetBlueprint()
local rof = self:GetWeaponRoF()
local numRackFiring = self.CurrentRackSalvoNumber
--This is done to make sure that when racks should fire together, they do
if bp.RackFireTogether == true then
numRackFiring = table.getsize(bp.RackBones)
end
-- Fork timer counter thread carefully
if not self:BeenDestroyed() and
not self.unit.Dead then
if bp.RenderFireClock and rof > 0 then
self:ForkThread(self.RenderClockThread, 1/rof)
end
end
-- Most of the time this will only run once, the only time it doesn't is when racks fire together
while self.CurrentRackSalvoNumber <= numRackFiring and not self.HaltFireOrdered do
local rackInfo = bp.RackBones[self.CurrentRackSalvoNumber]
local numMuzzlesFiring = bp.MuzzleSalvoSize
if bp.MuzzleSalvoDelay == 0 then
numMuzzlesFiring = table.getn(rackInfo.MuzzleBones)
end
if bp.FixedSpreadRadius then
local weaponPos = self.unit:GetPosition()
local targetPos = self:GetCurrentTargetPos()
local distance = VDist2(weaponPos[1], weaponPos[3], targetPos[1], targetPos[3])
-- This formula was obtained empirically and somehow it works :)
local randomness = bp.FixedSpreadRadius / (distance^2 / 12)
self:SetFiringRandomness(randomness)
end
local muzzleIndex = 1
for i = 1, numMuzzlesFiring do
if self.HaltFireOrdered then
continue
end
local muzzle = rackInfo.MuzzleBones[muzzleIndex]
if rackInfo.HideMuzzle == true then
self.unit:ShowBone(muzzle, true)
end
-- Deal with Muzzle charging sequence
if bp.MuzzleChargeDelay and bp.MuzzleChargeDelay > 0 then
if bp.Audio.MuzzleChargeStart then
self:PlaySound(bp.Audio.MuzzleChargeStart)
end
self:PlayFxMuzzleChargeSequence(muzzle)
if bp.NotExclusive then
self.unit:SetBusy(false)
end
WaitSeconds(bp.MuzzleChargeDelay)
if bp.NotExclusive then
self.unit:SetBusy(true)
end
end
self:PlayFxMuzzleSequence(muzzle)
if rackInfo.HideMuzzle == true then
self.unit:HideBone(muzzle, true)
end
if self.HaltFireOrdered then
continue
end
local proj = self:CreateProjectileAtMuzzle(muzzle)
-- Decrement the ammo if they are a counted projectile
if proj and not proj:BeenDestroyed() and bp.CountedProjectile == true then
if bp.NukeWeapon == true then
self.unit:NukeCreatedAtUnit()
-- Generate UI notification for automatic nuke ping
local launchData = { army = self.unit.Army-1, location = self:GetCurrentTargetPos()}
if not Sync.NukeLaunchData then Sync.NukeLaunchData = {} end
table.insert(Sync.NukeLaunchData, launchData)
self.unit:RemoveNukeSiloAmmo(1)
else
self.unit:RemoveTacticalSiloAmmo(1)
end
end
-- Deal with muzzle firing sequence
muzzleIndex = muzzleIndex + 1
if muzzleIndex > table.getn(rackInfo.MuzzleBones) then
muzzleIndex = 1
end
if bp.MuzzleSalvoDelay > 0 then
if bp.NotExclusive then
self.unit:SetBusy(false)
end
WaitSeconds(bp.MuzzleSalvoDelay)
if bp.NotExclusive then
self.unit:SetBusy(true)
end
end
end
self:PlayFxRackReloadSequence()
if self.CurrentRackSalvoNumber <= table.getn(bp.RackBones) then
self.CurrentRackSalvoNumber = self.CurrentRackSalvoNumber + 1
end
end
self:DoOnFireBuffs() -- Found in mohodata weapon.lua
self.FirstShot = false
self:StartEconomyDrain()
self:OnWeaponFired() -- Used primarily by Overcharge
-- We can fire again after reaching here
self.HaltFireOrdered = false
-- Deal with the rack firing sequence
if self.CurrentRackSalvoNumber > table.getn(bp.RackBones) then
self.CurrentRackSalvoNumber = 1
if bp.RackSalvoReloadTime > 0 then
ChangeState(self, self.RackSalvoReloadState)
elseif bp.RackSalvoChargeTime > 0 then
ChangeState(self, self.IdleState)
elseif bp.CountedProjectile == true and bp.WeaponUnpacks == true then
ChangeState(self, self.WeaponPackingState)
elseif bp.CountedProjectile == true and not bp.WeaponUnpacks then
ChangeState(self, self.IdleState)
else
ChangeState(self, self.RackSalvoFireReadyState)
end
elseif bp.CountedProjectile == true and not bp.WeaponUnpacks then
ChangeState(self, self.IdleState)
elseif bp.CountedProjectile == true and bp.WeaponUnpacks == true then
ChangeState(self, self.WeaponPackingState)
else
ChangeState(self, self.RackSalvoFireReadyState)
end
end,
OnLostTarget = function(self)
Weapon.OnLostTarget(self)
local bp = self:GetBlueprint()
if bp.WeaponUnpacks == true then
ChangeState(self, self.WeaponPackingState)
end
end,
-- Set a bool so we won't fire if the target reticle is moved
OnHaltFire = function(self)
self.HaltFireOrdered = true
end,
},
-- This state is for when the weapon is reloading
RackSalvoReloadState = State {
WeaponWantEnabled = true,
WeaponAimWantEnabled = true,
Main = function(self)
self.unit:SetBusy(true)
self:PlayFxRackSalvoReloadSequence()
local bp = self:GetBlueprint()
if bp.NotExclusive then
self.unit:SetBusy(false)
end
WaitSeconds(self:GetBlueprint().RackSalvoReloadTime)
self:WaitForAndDestroyManips()
if bp.NotExclusive then
self.unit:SetBusy(true)
end
if self:WeaponHasTarget() and bp.RackSalvoChargeTime > 0 and self:CanFire() then
ChangeState(self, self.RackSalvoChargeState)
elseif self:WeaponHasTarget() and self:CanFire() then
ChangeState(self, self.RackSalvoFireReadyState)
elseif not self:WeaponHasTarget() and bp.WeaponUnpacks == true and bp.WeaponUnpackLocksMotion ~= true then
ChangeState(self, self.WeaponPackingState)
else
ChangeState(self, self.IdleState)
end
end,
OnFire = function(self)
end,
},
-- This state is for weapons which have to unpack before firing
WeaponUnpackingState = State {
WeaponWantEnabled = false,
WeaponAimWantEnabled = false,
Main = function(self)
self.unit:SetBusy(true)
local bp = self:GetBlueprint()
if bp.WeaponUnpackLocksMotion then
self.unit:SetImmobile(true)
end
self:PlayFxWeaponUnpackSequence()
local rackSalvoChargeTime = self:GetBlueprint().RackSalvoChargeTime
if rackSalvoChargeTime and rackSalvoChargeTime > 0 then
ChangeState(self, self.RackSalvoChargeState)
else
ChangeState(self, self.RackSalvoFireReadyState)
end
end,
OnFire = function(self)
end,
},
-- This state is for weapons which have to pack up before moving or whatever
WeaponPackingState = State {
WeaponWantEnabled = true,
WeaponAimWantEnabled = true,
Main = function(self)
self.unit:SetBusy(true)
local bp = self:GetBlueprint()
WaitSeconds(self:GetBlueprint().WeaponRepackTimeout)
self:AimManipulatorSetEnabled(false)
self:PlayFxWeaponPackSequence()
if bp.WeaponUnpackLocksMotion then
self.unit:SetImmobile(false)
end
ChangeState(self, self.IdleState)
end,
OnGotTarget = function(self)
Weapon.OnGotTarget(self)
-- Issue 43
if self.unit then
self.unit:OnGotTarget(self)
end
if not self:GetBlueprint().ForceSingleFire then
ChangeState(self, self.WeaponUnpackingState)
end
end,
OnFire = function(self)
local bp = self:GetBlueprint()
if bp.CountedProjectile == true and not self:GetBlueprint().ForceSingleFire then
ChangeState(self, self.WeaponUnpackingState)
end
end,
},
-- This state is entered only when the owner of the weapon is dead
DeadState = State {
OnEnterState = function(self)
end,
Main = function(self)
end,
},
}
KamikazeWeapon = Class(Weapon) {
OnFire = function(self)
local myBlueprint = self:GetBlueprint()
DamageArea(self.unit, self.unit:GetPosition(), myBlueprint.DamageRadius, myBlueprint.Damage, myBlueprint.DamageType or 'Normal', myBlueprint.DamageFriendly or false)
self.unit:PlayUnitSound('Destroyed')
self.unit:Destroy()
end,
}
BareBonesWeapon = Class(Weapon) {
Data = {},
OnFire = function(self)
local myBlueprint = self:GetBlueprint()
local myProjectile = self.unit:CreateProjectile(myBlueprint.ProjectileId, 0, 0, 0, nil, nil, nil):SetCollision(false)
if self.Data then
myProjectile:PassData(self.Data)
end
end,
}
OverchargeWeapon = Class(DefaultProjectileWeapon) {
NeedsUpgrade = false,
AutoMode = false,
AutoThread = nil,
EnergyRequired = nil,
HasEnergy = function(self)
return self.unit:GetAIBrain():GetEconomyStored('ENERGY') >= self.EnergyRequired
end,
-- Can we use the OC weapon?
CanOvercharge = function(self)
return not self.unit:IsOverchargePaused() and self:HasEnergy() and not
self:UnitOccupied() and not
self.unit:IsUnitState('Enhancing') and not
self.unit:IsUnitState('Upgrading')
end,
StartEconomyDrain = function(self) -- OverchargeWeapon drains energy on impact
end,
-- Returns true if the unit is doing something that shouldn't allow any weapon fire
UnitOccupied = function(self)
return (self.unit:IsUnitState('Upgrading') and not self.unit:IsUnitState('Enhancing')) or -- Don't let us shoot if we're upgrading, unless it's an enhancement task
self.unit:IsUnitState('Building') or
self.unit:IsUnitState('Repairing') or
self.unit:IsUnitState('Reclaiming')
end,
-- The Overcharge cool-down function
PauseOvercharge = function(self)
if not self.unit:IsOverchargePaused() then
self.unit:SetOverchargePaused(true)
self:OnDisableWeapon()
WaitSeconds(1 / self:GetBlueprint().RateOfFire)
self.unit:SetOverchargePaused(false)
if self.AutoMode then
self.AutoThread = self:ForkThread(self.AutoEnable)
end
end
end,
AutoEnable = function(self)
while not self:CanOvercharge() do
WaitSeconds(0.1)
end
if self.AutoMode then
self:OnEnableWeapon()
end
end,
SetAutoOvercharge = function(self, auto)
self.AutoMode = auto
if self.AutoMode then
self.AutoThread = self:ForkThread(self.AutoEnable)
else
if self.AutoThread then
KillThread(self.AutoThread)
self.AutoThread = nil
end
if self.enabled then
self:OnDisableWeapon()
end
end
end,
OnCreate = function(self)
DefaultProjectileWeapon.OnCreate(self)
self.EnergyRequired = self:GetBlueprint().EnergyRequired
self:SetWeaponEnabled(false)
self.AimControl:SetEnabled(false)
self.AimControl:SetPrecedence(0)
self.unit:SetOverchargePaused(false)
end,
OnGotTarget = function(self)
if self:CanOvercharge() then
DefaultProjectileWeapon.OnGotTarget(self)
else
self:OnDisableWeapon()
end
end,
OnFire = function(self)
if self:CanOvercharge() then
DefaultProjectileWeapon.OnFire(self)
else
self:OnDisableWeapon()
end
end,
IsEnabled = function(self)
return self.enabled
end,
OnEnableWeapon = function(self)
if self:BeenDestroyed() then return end
DefaultProjectileWeapon.OnEnableWeapon(self)
self:SetWeaponEnabled(true)