forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweapon.lua
More file actions
555 lines (493 loc) · 20.3 KB
/
weapon.lua
File metadata and controls
555 lines (493 loc) · 20.3 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
-- ****************************************************************************
-- **
-- ** File : /lua/sim/Weapon.lua
-- ** Author(s): John Comes
-- **
-- ** Summary : The base weapon class for all weapons in the game.
-- **
-- ** Copyright � 2005 Gas Powered Games, Inc. All rights reserved.
-- ****************************************************************************
local Entity = import('/lua/sim/Entity.lua').Entity
local NukeDamage = import('/lua/sim/NukeDamage.lua').NukeAOE
local Set = import('/lua/system/setutils.lua')
local ParseEntityCategoryProperly = import('/lua/sim/CategoryUtils.lua').ParseEntityCategoryProperly
local cachedPriorities = false
local function ParsePriorities()
local idlist = EntityCategoryGetUnitList(categories.ALLUNITS)
local finalPriorities = {}
for _, id in idlist do
local weapons = GetUnitBlueprintByName(id).Weapon
for weaponNum, weapon in weapons or {} do
for line, priority in weapon.TargetPriorities or {} do
if not finalPriorities[priority] then
if string.find(priority, '%(') then
finalPriorities[priority] = ParseEntityCategoryProperly(priority)
else
finalPriorities[priority] = ParseEntityCategory(priority)
end
end
end
end
end
return finalPriorities
end
Weapon = Class(moho.weapon_methods) {
__init = function(self, unit)
self.unit = unit
end,
OnCreate = function(self)
if not self.unit.Trash then
self.unit.Trash = TrashBag()
end
self:SetValidTargetsForCurrentLayer(self.unit:GetCurrentLayer())
if self:GetBlueprint().Turreted == true then
self:SetupTurret()
end
self:SetWeaponPriorities()
self.DisabledBuffs = {}
self.DamageMod = 0
self.DamageRadiusMod = 0
self.NumTargets = 0
local bp = self:GetBlueprint()
local initStore = bp.InitialProjectileStorage
if initStore and initStore > 0 then
if bp.MaxProjectileStorage and bp.MaxProjectileStorage < initStore then
initStore = bp.MaxProjectileStorage
end
local nuke = false
if bp.NukeWeapon then
nuke = true
end
self:ForkThread(self.AmmoThread, nuke, bp.InitialProjectileStorage)
end
self.CollideFriendly = bp.CollideFriendly == true
end,
AmmoThread = function(self, nuke, amount)
WaitSeconds(0.1)
if nuke then
self.unit:GiveNukeSiloAmmo(amount)
else
self.unit:GiveTacticalSiloAmmo(amount)
end
end,
SetupTurret = function(self)
local bp = self:GetBlueprint()
local yawBone = bp.TurretBoneYaw
local pitchBone = bp.TurretBonePitch
local muzzleBone = bp.TurretBoneMuzzle
local precedence = bp.AimControlPrecedence or 10
local pitchBone2
local muzzleBone2
if bp.TurretBoneDualPitch and bp.TurretBoneDualPitch ~= '' then
pitchBone2 = bp.TurretBoneDualPitch
end
if bp.TurretBoneDualMuzzle and bp.TurretBoneDualMuzzle ~= '' then
muzzleBone2 = bp.TurretBoneDualMuzzle
end
if not (self.unit:ValidateBone(yawBone) and self.unit:ValidateBone(pitchBone) and self.unit:ValidateBone(muzzleBone)) then
error('*ERROR: Bone aborting turret setup due to bone issues.', 2)
return
elseif pitchBone2 and muzzleBone2 then
if not (self.unit:ValidateBone(pitchBone2) and self.unit:ValidateBone(muzzleBone2)) then
error('*ERROR: Bone aborting turret setup due to pitch/muzzle bone2 issues.', 2)
return
end
end
if yawBone and pitchBone and muzzleBone then
if bp.TurretDualManipulators then
self.AimControl = CreateAimController(self, 'Torso', yawBone)
self.AimRight = CreateAimController(self, 'Right', pitchBone, pitchBone, muzzleBone)
self.AimLeft = CreateAimController(self, 'Left', pitchBone2, pitchBone2, muzzleBone2)
self.AimControl:SetPrecedence(precedence)
self.AimRight:SetPrecedence(precedence)
self.AimLeft:SetPrecedence(precedence)
if EntityCategoryContains(categories.STRUCTURE, self.unit) then
self.AimControl:SetResetPoseTime(9999999)
end
self:SetFireControl('Right')
self.unit.Trash:Add(self.AimControl)
self.unit.Trash:Add(self.AimRight)
self.unit.Trash:Add(self.AimLeft)
else
self.AimControl = CreateAimController(self, 'Default', yawBone, pitchBone, muzzleBone)
if EntityCategoryContains(categories.STRUCTURE, self.unit) then
self.AimControl:SetResetPoseTime(9999999)
end
self.unit.Trash:Add(self.AimControl)
self.AimControl:SetPrecedence(precedence)
if bp.RackSlavedToTurret and table.getn(bp.RackBones) > 0 then
for k, v in bp.RackBones do
if v.RackBone ~= pitchBone then
local slaver = CreateSlaver(self.unit, v.RackBone, pitchBone)
slaver:SetPrecedence(precedence-1)
self.unit.Trash:Add(slaver)
end
end
end
end
else
error('*ERROR: Trying to setup a turreted weapon but there are yaw bones, pitch bones or muzzle bones missing from the blueprint.', 2)
end
local numbersexist = true
local turretyawmin, turretyawmax, turretyawspeed
local turretpitchmin, turretpitchmax, turretpitchspeed
-- SETUP MANIPULATORS AND SET TURRET YAW, PITCH AND SPEED
if self:GetBlueprint().TurretYaw and self:GetBlueprint().TurretYawRange then
turretyawmin, turretyawmax = self:GetTurretYawMinMax()
else
numbersexist = false
end
if self:GetBlueprint().TurretYawSpeed then
turretyawspeed = self:GetTurretYawSpeed()
else
numbersexist = false
end
if self:GetBlueprint().TurretPitch and self:GetBlueprint().TurretPitchRange then
turretpitchmin, turretpitchmax = self:GetTurretPitchMinMax()
else
numbersexist = false
end
if self:GetBlueprint().TurretPitchSpeed then
turretpitchspeed = self:GetTurretPitchSpeed()
else
numbersexist = false
end
if numbersexist then
self.AimControl:SetFiringArc(turretyawmin, turretyawmax, turretyawspeed, turretpitchmin, turretpitchmax, turretpitchspeed)
if self.AimRight then
self.AimRight:SetFiringArc(turretyawmin/12, turretyawmax/12, turretyawspeed, turretpitchmin, turretpitchmax, turretpitchspeed)
end
if self.AimLeft then
self.AimLeft:SetFiringArc(turretyawmin/12, turretyawmax/12, turretyawspeed, turretpitchmin, turretpitchmax, turretpitchspeed)
end
else
local strg = '*ERROR: TRYING TO SETUP A TURRET WITHOUT ALL TURRET NUMBERS IN BLUEPRINT, ABORTING TURRET SETUP. WEAPON: ' .. self:GetBlueprint().Label .. ' UNIT: '.. self.unit.UnitId
error(strg, 2)
end
end,
AimManipulatorSetEnabled = function(self, enabled)
if self.AimControl then
self.AimControl:SetEnabled(enabled)
end
end,
GetAimManipulator = function(self)
return self.AimControl
end,
SetTurretYawSpeed = function(self, speed)
local turretyawmin, turretyawmax = self:GetTurretYawMinMax()
local turretpitchmin, turretpitchmax = self:GetTurretPitchMinMax()
local turretpitchspeed = self:GetTurretPitchSpeed()
if self.AimControl then
self.AimControl:SetFiringArc(turretyawmin, turretyawmax, speed, turretpitchmin, turretpitchmax, turretpitchspeed)
end
end,
SetTurretPitchSpeed = function(self, speed)
local turretyawmin, turretyawmax = self:GetTurretYawMinMax()
local turretpitchmin, turretpitchmax = self:GetTurretPitchMinMax()
local turretpitchspeed = self:GetTurretYawSpeed()
if self.AimControl then
self.AimControl:SetFiringArc(turretyawmin, turretyawmax, turretyawspeed, turretpitchmin, turretpitchmax, speed)
end
end,
GetTurretYawMinMax = function(self)
local halfrange = self:GetBlueprint().TurretYawRange
local yaw = self:GetBlueprint().TurretYaw
turretyawmin = yaw - halfrange
turretyawmax = yaw + halfrange
return turretyawmin, turretyawmax
end,
GetTurretYawSpeed = function(self)
return self:GetBlueprint().TurretYawSpeed
end,
GetTurretPitchMinMax = function(self)
local halfrange = self:GetBlueprint().TurretPitchRange
local pitch = self:GetBlueprint().TurretPitch
turretpitchmin = pitch - halfrange
turretpitchmax = pitch + halfrange
return turretpitchmin, turretpitchmax
end,
GetTurretPitchSpeed = function(self)
return self:GetBlueprint().TurretPitchSpeed
end,
OnFire = function(self)
local bp = self:GetBlueprint()
if bp.Audio.Fire then
self:PlaySound(bp.Audio.Fire)
end
self:DoOnFireBuffs()
end,
OnEnableWeapon = function(self)
end,
OnGotTarget = function(self)
if self.DisabledFiringBones and self.unit.Animator then
for key, value in self.DisabledFiringBones do
self.unit.Animator:SetBoneEnabled(value, false)
end
end
self.NumTargets = self.NumTargets + 1
end,
OnLostTarget = function(self)
if self.DisabledFiringBones and self.unit.Animator then
for key, value in self.DisabledFiringBones do
self.unit.Animator:SetBoneEnabled(value, true)
end
end
self.NumTargets = self.NumTargets - 1
if self.NumTargets < 0 then
self.NumTargets = 0
end
end,
OnStartTracking = function(self, label)
self:PlayWeaponSound('BarrelStart')
self:PlayWeaponAmbientSound('BarrelLoop')
end,
OnStopTracking = function(self, label)
self:PlayWeaponSound('BarrelStop')
self:StopWeaponAmbientSound('BarrelLoop')
if EntityCategoryContains(categories.STRUCTURE, self.unit) then
self.AimControl:SetResetPoseTime(9999999)
end
end,
PlayWeaponSound = function(self, sound)
local bp = self:GetBlueprint()
if not bp.Audio[sound] then return end
self:PlaySound(bp.Audio[sound])
end,
PlayWeaponAmbientSound = function(self, sound)
local bp = self:GetBlueprint()
if not bp.Audio[sound] then return end
if not self.AmbientSounds then
self.AmbientSounds = {}
end
if not self.AmbientSounds[sound] then
local sndEnt = Entity {}
self.AmbientSounds[sound] = sndEnt
self.unit.Trash:Add(sndEnt)
sndEnt:AttachTo(self.unit,-1)
end
self.AmbientSounds[sound]:SetAmbientSound(bp.Audio[sound], nil)
end,
StopWeaponAmbientSound = function(self, sound)
if not self.AmbientSounds then return end
if not self.AmbientSounds[sound] then return end
local bp = self:GetBlueprint()
if not bp.Audio[sound] then return end
self.AmbientSounds[sound]:Destroy()
self.AmbientSounds[sound] = nil
end,
OnEnableWeapon = function(self)
end,
OnMotionHorzEventChange = function(self, new, old)
end,
GetDamageTableInternal = function(self)
local weaponBlueprint = self:GetBlueprint()
local damageTable = {}
damageTable.InitialDamageAmount = weaponBlueprint.InitialDamage or 0
damageTable.DamageRadius = weaponBlueprint.DamageRadius + (self.DamageRadiusMod or 0)
damageTable.DamageAmount = weaponBlueprint.Damage + (self.DamageMod or 0)
damageTable.DamageType = weaponBlueprint.DamageType
damageTable.DamageFriendly = weaponBlueprint.DamageFriendly
if damageTable.DamageFriendly == nil then
damageTable.DamageFriendly = true
end
damageTable.CollideFriendly = weaponBlueprint.CollideFriendly or false
damageTable.DoTTime = weaponBlueprint.DoTTime
damageTable.DoTPulses = weaponBlueprint.DoTPulses
damageTable.MetaImpactAmount = weaponBlueprint.MetaImpactAmount
damageTable.MetaImpactRadius = weaponBlueprint.MetaImpactRadius
damageTable.ArtilleryShieldBlocks = weaponBlueprint.ArtilleryShieldBlocks
-- Add buff
damageTable.Buffs = {}
if weaponBlueprint.Buffs ~= nil then
for k, v in weaponBlueprint.Buffs do
if not self.DisabledBuffs[v.BuffType] then
damageTable.Buffs[k] = v
end
end
end
return damageTable
end,
damageTableCache = false,
GetDamageTable = function(self)
if not self.damageTableCache then self.damageTableCache = self:GetDamageTableInternal() end
return self.damageTableCache
end,
CreateProjectileForWeapon = function(self, bone)
local proj = self:CreateProjectile(bone)
local damageTable = self:GetDamageTable()
if proj and not proj:BeenDestroyed() then
proj:PassDamageData(damageTable)
local bp = self:GetBlueprint()
if bp.NukeOuterRingDamage and bp.NukeOuterRingRadius and bp.NukeOuterRingTicks and bp.NukeOuterRingTotalTime and
bp.NukeInnerRingDamage and bp.NukeInnerRingRadius and bp.NukeInnerRingTicks and bp.NukeInnerRingTotalTime then
proj.InnerRing = NukeDamage()
proj.InnerRing:OnCreate(bp.NukeInnerRingDamage, bp.NukeInnerRingRadius, bp.NukeInnerRingTicks, bp.NukeInnerRingTotalTime)
proj.OuterRing = NukeDamage()
proj.OuterRing:OnCreate(bp.NukeOuterRingDamage, bp.NukeOuterRingRadius, bp.NukeOuterRingTicks, bp.NukeOuterRingTotalTime)
-- Need to store these three for later, in case the missile lands after the launcher dies
proj.Launcher = self.unit
proj.Army = self.unit.Army
proj.Brain = self.unit:GetAIBrain()
end
end
return proj
end,
SetValidTargetsForCurrentLayer = function(self, newLayer)
-- LOG('SetValidTargetsForCurrentLayer, layer = ', newLayer)
local weaponBlueprint = self:GetBlueprint()
if weaponBlueprint.FireTargetLayerCapsTable then
if weaponBlueprint.FireTargetLayerCapsTable[newLayer] then
-- LOG('Setting Target Layer Caps to ', weaponBlueprint.FireTargetLayerCapsTable[newLayer])
self:SetFireTargetLayerCaps(weaponBlueprint.FireTargetLayerCapsTable[newLayer])
else
-- LOG('Setting Target Layer Caps to None')
self:SetFireTargetLayerCaps('None')
end
end
end,
OnDestroy = function(self)
end,
SetWeaponPriorities = function(self, priTable)
if not cachedPriorities then
cachedPriorities = ParsePriorities()
end
if not priTable then
local bp = self:GetBlueprint().TargetPriorities
if bp then
local priorityTable = {}
for k, v in bp do
if cachedPriorities[v] then
table.insert(priorityTable, cachedPriorities[v])
else
if string.find(v, '%(') then
cachedPriorities[v] = ParseEntityCategoryProperly(v)
table.insert(priorityTable, cachedPriorities[v])
else
cachedPriorities[v] = ParseEntityCategory(v)
table.insert(priorityTable, cachedPriorities[v])
end
end
end
self:SetTargetingPriorities(priorityTable)
end
else
if type(priTable[1]) == 'string' then
local priorityTable = {}
for k, v in priTable do
table.insert(priorityTable, ParseEntityCategory(v))
end
self:SetTargetingPriorities(priorityTable)
else
self:SetTargetingPriorities(priTable)
end
end
end,
WeaponUsesEnergy = function(self)
local bp = self:GetBlueprint()
if bp.EnergyRequired and bp.EnergyRequired > 0 then
return true
end
return false
end,
ForkThread = function(self, fn, ...)
if fn then
local thread = ForkThread(fn, self, unpack(arg))
self.unit.Trash:Add(thread)
return thread
else
return nil
end
end,
OnVeteranLevel = function(self, old, new)
local bp = self:GetBlueprint().Buffs
if not bp then return end
local lvlkey = 'VeteranLevel' .. new
for k, v in bp do
if v.Add[lvlkey] == true then
self:AddBuff(v)
end
end
end,
AddBuff = function(self, buffTbl)
self.unit:AddWeaponBuff(buffTbl, self)
end,
AddDamageMod = function(self, dmgMod)
self.DamageMod = self.DamageMod + dmgMod
self.damageTableCache = false
end,
AddDamageRadiusMod = function(self, dmgRadMod)
self.DamageRadiusMod = self.DamageRadiusMod + (dmgRadMod or 0)
self.damageTableCache = false
end,
DoOnFireBuffs = function(self)
local data = self:GetBlueprint()
if data.Buffs then
for k, v in data.Buffs do
if v.Add.OnFire == true then
self.unit:AddBuff(v)
end
end
end
end,
DisableBuff = function(self, buffname)
if buffname then
self.DisabledBuffs[buffname] = true
else
-- Error
error('ERROR: DisableBuff in weapon.lua does not have a buffname')
end
self.damageTableCache = false
end,
ReEnableBuff = function(self, buffname)
if buffname then
self.DisabledBuffs[buffname] = nil
else
-- Error
error('ERROR: ReEnableBuff in weapon.lua does not have a buffname')
end
self.damageTableCache = false
end,
-- Method to mark weapon when parent unit gets loaded on to a transport unit
SetOnTransport = function(self, transportstate)
self.onTransport = transportstate
if not transportstate then
-- send a message to tell the weapon that the unit just got dropped and needs to restart aim
self:OnLostTarget()
end
-- Disable weapon if on transport and not allowed to fire from it
if not self.unit:GetBlueprint().Transport.CanFireFromTransport then
if transportstate then
self.WeaponDisabledOnTransport = true
self:SetWeaponEnabled(false)
else
self:SetWeaponEnabled(true)
self.WeaponDisabledOnTransport = false
end
end
end,
-- Method to retreive onTransport information. True if the parent unit has been loaded on to a transport unit
GetOnTransport = function(self)
return self.onTransport
end,
-- This is the function to set a weapon enabled.
-- If the weapon is enhabled by an enhancement, this will check to see if the unit has the enhancement before
-- allowing it to try to be enabled or disabled.
SetWeaponEnabled = function(self, enable)
if not enable then
self:SetEnabled(enable)
return
end
local bp = self:GetBlueprint().EnabledByEnhancement
if bp then
for k, v in SimUnitEnhancements[self.unit.EntityId] or {} do
if v == bp then
self:SetEnabled(enable)
return
end
end
-- Enhancement needed but doesn't have it, don't allow weapon to be enabled.
return
end
self:SetEnabled(enable)
end,
}