forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvictory.lua
More file actions
132 lines (113 loc) · 3.68 KB
/
victory.lua
File metadata and controls
132 lines (113 loc) · 3.68 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
gameOver = false
local victoryCategories = {
demoralization=categories.COMMAND,
domination=categories.STRUCTURE + categories.ENGINEER - categories.WALL,
eradication=categories.ALLUNITS - categories.WALL,
}
function AllUnitsInCategoryDead(brain,categoryCheck)
local ListOfUnits = brain:GetListOfUnits(categoryCheck, false)
for index, unit in ListOfUnits do
if unit.CanBeKilled and not unit.Dead and unit:GetFractionComplete() == 1 then
return false
end
end
return true
end
function ObserverAfterDeath(armyIndex)
if not ScenarioInfo.Options.AllowObservers then return end
local humans = {}
local humanIndex = 0
for i, data in ArmyBrains do
if data.BrainType == 'Human' then
if IsAlly(armyIndex, i) then
if not ArmyIsOutOfGame(i) then
return
end
table.insert(humans, humanIndex)
end
humanIndex = humanIndex + 1
end
end
for _, index in humans do
for i in ArmyBrains do
SetCommandSource(i - 1, index, false)
end
end
end
function CheckVictory(scenarioInfo)
local categoryCheck = victoryCategories[scenarioInfo.Options.Victory]
if not categoryCheck then return end
-- tick number we are going to issue a victory on. Or nil if we are not.
local victoryTime = nil
local potentialWinners = {}
while true do
-- Look for newly defeated brains and tell them they're dead
local stillAlive = {}
for _, brain in ArmyBrains do
if not brain:IsDefeated() and not ArmyIsCivilian(brain:GetArmyIndex()) then
if AllUnitsInCategoryDead(brain,categoryCheck) then
brain:OnDefeat()
ObserverAfterDeath(brain:GetArmyIndex())
else
table.insert(stillAlive, brain)
end
end
end
-- uh-oh, there is nobody alive... It's a draw.
if table.empty(stillAlive) then
CallEndGame()
return
end
-- check to see if everyone still alive is allied and is requesting an allied victory.
local win = true
local draw = true
for i, brain in stillAlive do
if not brain.OfferingDraw then
draw = false
end
if not (draw or win) then break end
for j, other in stillAlive do
if i ~= j then
if not brain.RequestingAlliedVictory or not IsAlly(brain:GetArmyIndex(), other:GetArmyIndex()) then
win = false
break
end
end
end
end
local callback = nil
if win then
local equal = table.equal(stillAlive, potentialWinners)
if not equal then
victoryTime = GetGameTimeSeconds() + 5
potentialWinners = stillAlive
end
if equal and GetGameTimeSeconds() >= victoryTime then
callback = 'OnVictory'
end
elseif draw then
callback = 'OnDraw'
else
victoryTime = nil
potentialWinners = {}
end
if callback then
for _, brain in stillAlive do
brain[callback](brain)
end
CallEndGame()
return
end
WaitSeconds(3)
end
end
function CallEndGame()
gameOver = true
ForkThread(function()
WaitSeconds(2.9)
import('/lua/sim/score.lua').SyncScores()
Sync.GameEnded = true
WaitSeconds(0.1)
EndGame()
end)
end