forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimUtils.lua
283 lines (241 loc) · 9.17 KB
/
SimUtils.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
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
-- Copyright © 2005 Gas Powered Games, Inc. All rights reserved.
--
-- General Sim scripts
-- ==============================================================================
-- Diplomacy
-- ==============================================================================
local sharedUnits = {}
function BreakAlliance(data)
-- You cannot change alliances in a team game
if ScenarioInfo.TeamGame then
return
end
if OkayToMessWithArmy(data.From) then
SetAlliance(data.From,data.To,"Enemy")
if Sync.BrokenAlliances == nil then
Sync.BrokenAlliances = {}
end
table.insert(Sync.BrokenAlliances, { From = data.From, To = data.To })
end
import('/lua/SimPing.lua').OnAllianceChange()
end
function OnAllianceResult(resultData)
-- You cannot change alliances in a team game
if ScenarioInfo.TeamGame then
return
end
if OkayToMessWithArmy(resultData.From) then
if resultData.ResultValue == "accept" then
SetAlliance(resultData.From,resultData.To,"Ally")
if Sync.FormedAlliances == nil then
Sync.FormedAlliances = {}
end
table.insert(Sync.FormedAlliances, { From = resultData.From, To = resultData.To })
end
end
import('/lua/SimPing.lua').OnAllianceChange()
end
import('/lua/SimPlayerQuery.lua').AddResultListener("OfferAlliance", OnAllianceResult)
function KillSharedUnits(owner)
if sharedUnits[owner] and table.getn(sharedUnits[owner]) > 0 then
for index,unit in sharedUnits[owner] do
if not unit.Dead and unit.oldowner == owner then
unit:Kill()
end
end
sharedUnits[owner] = {}
end
end
function TransferUnitsOwnership(units, ToArmyIndex)
local toBrain = GetArmyBrain(ToArmyIndex)
if not toBrain or toBrain:IsDefeated() or not units or table.getn(units) < 1 then
return
end
local fromBrain = GetArmyBrain(units[1]:GetArmy())
table.sort(units, function (a, b) return a:GetBlueprint().Economy.BuildCostMass > b:GetBlueprint().Economy.BuildCostMass end)
local newUnits = {}
for k,v in units do
local owner = v:GetArmy()
-- Only allow units not attached to be given. This is because units will give all of it's children over
-- aswell, so we only want the top level units to be given.
-- Units currently being captured is also denied
local disallowTransfer = owner == ToArmyIndex or
v:GetParent() ~= v or (v.Parent and v.Parent ~= v) or
v.CaptureProgress > 0
if disallowTransfer then
continue
end
local unit = v
local bp = unit:GetBlueprint()
local unitId = unit:GetUnitId()
-- B E F O R E
local numNukes = unit:GetNukeSiloAmmoCount() -- looks like one of these 2 works for SMDs also
local numTacMsl = unit:GetTacticalSiloAmmoCount()
local massKilled = unit.Sync.totalMassKilled
local unitHealth = unit:GetHealth()
local shieldIsOn = false
local ShieldHealth = 0
local hasFuel = false
local fuelRatio = 0
local enh = {} -- enhancements
local oldowner = unit.oldowner
if unit.MyShield then
shieldIsOn = unit:ShieldIsOn()
ShieldHealth = unit.MyShield:GetHealth()
end
if bp.Physics.FuelUseTime and bp.Physics.FuelUseTime > 0 then -- going through the BP to check for fuel
fuelRatio = unit:GetFuelRatio() -- usage is more reliable then unit.HasFuel
hasFuel = true -- cause some buildings say they use fuel
end
local posblEnh = bp.Enhancements
if posblEnh then
for k,v in posblEnh do
if unit:HasEnhancement(k) then
table.insert(enh, k)
end
end
end
unit.IsBeingTransferred = true
-- changing owner
unit = ChangeUnitArmy(unit,ToArmyIndex)
if not unit then
continue
end
table.insert(newUnits, unit)
unit.oldowner = oldowner
if IsAlly(owner, ToArmyIndex) then
if not unit.oldowner then
unit.oldowner = owner
end
if not sharedUnits[unit.oldowner] then
sharedUnits[unit.oldowner] = {}
end
table.insert(sharedUnits[unit.oldowner], unit)
end
-- A F T E R
if massKilled and massKilled > 0 then
unit:CalculateVeterancyLevel(massKilled)
end
if enh and table.getn(enh) > 0 then
for k, v in enh do
unit:CreateEnhancement(v)
end
end
if unitHealth > unit:GetMaxHealth() then
unitHealth = unit:GetMaxHealth()
end
unit:SetHealth(unit,unitHealth)
if hasFuel then
unit:SetFuelRatio(fuelRatio)
end
if numNukes and numNukes > 0 then
unit:GiveNukeSiloAmmo((numNukes - unit:GetNukeSiloAmmoCount()))
end
if numTacMsl and numTacMsl > 0 then
unit:GiveTacticalSiloAmmo((numTacMsl - unit:GetTacticalSiloAmmoCount()))
end
if unit.MyShield then
unit.MyShield:SetHealth(unit, ShieldHealth)
if shieldIsOn then
unit:EnableShield()
else
unit:DisableShield()
end
end
if EntityCategoryContains(categories.ENGINEERSTATION, unit) then
unit:SetPaused(true)
end
unit.IsBeingTransferred = false
v:OnGiven(unit)
end
if table.getn(EntityCategoryFilterDown(categories.RESEARCH, newUnits)) > 0 then
for _,aiBrain in {fromBrain, toBrain} do
local buildRestrictionVictims = aiBrain:GetListOfUnits(categories.FACTORY + categories.ENGINEER, false)
for _, victim in buildRestrictionVictims do
victim:updateBuildRestrictions()
end
end
end
return newUnits
end
function GiveUnitsToPlayer(data, units)
if units then
local owner = units[1]:GetArmy()
if OkayToMessWithArmy(owner) and IsAlly(owner,data.To) then
TransferUnitsOwnership(units, data.To)
end
end
end
function SetResourceSharing(data)
if not OkayToMessWithArmy(data.Army) then return end
local brain = GetArmyBrain(data.Army)
brain:SetResourceSharing(data.Value)
end
function RequestAlliedVictory(data)
-- You cannot change this in a team game
if ScenarioInfo.TeamGame then
return
end
if not OkayToMessWithArmy(data.Army) then return end
local brain = GetArmyBrain(data.Army)
brain.RequestingAlliedVictory = data.Value
end
function SetOfferDraw(data)
if not OkayToMessWithArmy(data.Army) then return end
local brain = GetArmyBrain(data.Army)
brain.OfferingDraw = data.Value
end
-- ==============================================================================
-- UNIT CAP
-- ==============================================================================
function UpdateUnitCap(deadArmy)
-- If we are asked to share out unit cap for the defeated army, do the following...
local mode = ScenarioInfo.Options.ShareUnitCap
if not mode or mode == 'none' then return end
local totalCount = 0
local aliveCount = 0
local alive = {}
for index, brain in ArmyBrains do
if (mode == 'all' or (mode == 'allies' and IsAlly(deadArmy, index))) and not ArmyIsCivilian(index) then
if not brain:IsDefeated() then
brain.index = index
table.insert(alive, brain)
aliveCount = aliveCount + 1
end
totalCount = totalCount + 1
end
end
if aliveCount > 0 then
local currentCap = GetArmyUnitCap(alive[1].index) -- First time, this is the initial army cap, but it will update each time this function runs
local totalCap = (aliveCount + 1) * currentCap -- Total cap for the team/game. Uses aliveCount to take account of currentCap updating
local newCap = math.floor(totalCap / aliveCount)
for _, brain in alive do
SetArmyUnitCap(brain.index, newCap)
end
end
end
function SendChatToReplay(data)
if data.Sender and data.Msg then
if not Sync.UnitData.Chat then
Sync.UnitData.Chat = {}
end
table.insert(Sync.UnitData.Chat, {sender=data.Sender, msg=data.Msg})
end
end
function GiveResourcesToPlayer(data)
SendChatToReplay(data)
if data.From != -1 then
if not OkayToMessWithArmy(data.From) then
return
end
local fromBrain = GetArmyBrain(data.From)
local toBrain = GetArmyBrain(data.To)
if fromBrain:IsDefeated() or toBrain:IsDefeated() then
return
end
local massTaken = fromBrain:TakeResource('Mass',data.Mass * fromBrain:GetEconomyStored('Mass'))
local energyTaken = fromBrain:TakeResource('Energy',data.Energy * fromBrain:GetEconomyStored('Energy'))
toBrain:GiveResource('Mass',massTaken)
toBrain:GiveResource('Energy',energyTaken)
end
end