forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfdestruct.lua
77 lines (72 loc) · 2.99 KB
/
selfdestruct.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
function ToggleSelfDestruct(data)
-- Suppress self destruct in tutorial missions as they screw up the mission end
if ScenarioInfo.tutorial and ScenarioInfo.tutorial == true then
return
end
if data.owner ~= -1 then
local unitEntities = {}
for _, unitId in data.units do
local unit = GetEntityById(unitId)
if OkayToMessWithArmy(unit:GetArmy()) then
table.insert(unitEntities, unit)
end
end
if table.getsize(unitEntities) > 0 then
if data.noDelay then -- Kill these units instantly
for _, unit in unitEntities do
if unit:BeenDestroyed() or unit.Dead then return end
FireSelfdestructWeapons(unit)
unit:Kill()
end
else
local togglingOff = false
for _, unit in unitEntities do -- Rescue anything in the process of dying, and skip the next bit
if unit.SelfDestructThread then
togglingOff = true
KillThread(unit.SelfDestructThread)
unit.SelfDestructThread = false
local entityId = unit:GetEntityId()
CancelCountdown(entityId)
end
end
if not togglingOff then
for _, unitEnt in unitEntities do
local unit = unitEnt
-- Unit and weapon bp flags can be used to control behaviour on SelfDestruct
-- Instant kill if InstantDeathOnSelfDestruct = true variable set in units general table
-- Fires weapons with FireOnSelfDestruct = true in units weapon table
local bp = unit:GetBlueprint()
if bp.General.InstantDeathOnSelfDestruct then
FireSelfdestructWeapons(unit)
unit:Kill()
else
-- Regular self destruct cycle
local entityId = unit:GetEntityId()
StartCountdown(entityId)
unit.SelfDestructThread = ForkThread(function()
WaitSeconds(5)
if unit:BeenDestroyed() then return end
FireSelfdestructWeapons(unit)
unit:Kill()
end)
end
end
end
end
end
end
end
function FireSelfdestructWeapons(unit)
local wepCount = unit:GetWeaponCount()
for i = 1, wepCount do
local wep = unit:GetWeapon(i)
local wepBP = wep:GetBlueprint()
if wepBP.FireOnSelfDestruct then
if wep.Fire then
wep.Fire()
else
wep.OnFire(wep)
end
end
end
end