forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfdestruct.lua
84 lines (69 loc) · 2.68 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
78
79
80
81
82
83
84
local GetEntityById = GetEntityById
local OkayToMessWithArmy = OkayToMessWithArmy
local StartCountdown = StartCountdown
local CancelCountdown = CancelCountdown
local ForkThread = ForkThread
local KillThread = KillThread
local TableInsert = table.insert
-- prevent magic numbers
local countdownDuration = 5
--- Destroys the given unit after a set duration
---@param unit Unit
local function SelfDestructThread(unit)
WaitSeconds(countdownDuration)
if unit:BeenDestroyed() then
return
end
unit:Kill()
end
--- Toggles the destruction of the units
---@param data table
---@param units Unit
function ToggleSelfDestruct(data, units)
-- suppress self destruct in tutorial missions as they screw up the mission end
if ScenarioInfo.tutorial and ScenarioInfo.tutorial == true then
return
end
-- do not allow observers to use this
if data.owner ~= -1 then
-- just take them all out
if data.noDelay then
for _, unit in units do
if OkayToMessWithArmy(unit.Army) then
if not (unit.Dead or unit:BeenDestroyed()) then
unit:Kill()
end
end
end
-- wait a few seconds, then destroy
else
-- if one is in the process of being destroyed, remove all destruction threads
local togglingOff = false
for _, unit in units do
if OkayToMessWithArmy(unit.Army) then
if unit.SelfDestructThread then
togglingOff = true
KillThread(unit.SelfDestructThread)
unit.SelfDestructThread = false
CancelCountdown(unit.EntityId) -- as defined in SymSync.lua
end
end
end
-- if none are in the process of being destroyed, destroy them after a delay
if not togglingOff then
for _, unit in units do
if OkayToMessWithArmy(unit.Army) then
-- allows fire beetle to be destroyed immediately
if unit.Blueprint.General.InstantDeathOnSelfDestruct then
unit:Kill()
-- destroy everything else after five seconds
else
StartCountdown(unit.EntityId, countdownDuration) -- as defined in SymSync.lua
unit.SelfDestructThread = ForkThread(SelfDestructThread, unit)
end
end
end
end
end
end
end