forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimUtils.lua
622 lines (517 loc) · 22.5 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
-- 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, captured)
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())
local shareUpgrades
if ScenarioInfo.Options.Share == 'FullShare' then
shareUpgrades = true
end
table.sort(units, function (a, b) return a:GetBlueprint().Economy.BuildCostMass > b:GetBlueprint().Economy.BuildCostMass end)
local newUnits = {}
local upUnits = {}
local pauseKennels = {}
local upgradeKennels = {}
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 massKilledTrue = unit.Sync.totalMassKilledTrue
local unitHealth = unit:GetHealth()
local shieldIsOn = false
local ShieldHealth = 0
local hasFuel = false
local fuelRatio = 0
local enh = {} -- enhancements
local oldowner = unit.oldowner
local upgradesTo = unit.UpgradesTo
local defaultBuildRate
local upgradeBuildRate
local exclude
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
if bp.CategoriesHash.ENGINEERSTATION and bp.CategoriesHash.UEF then
--We have to kill drones which are idling inside Kennel at the moment of transfer
--otherwise additional dummy drone will appear after transfer
for _,drone in unit:GetCargo() do
drone:Destroy()
end
end
if unit.TransferUpgradeProgress and shareUpgrades then
local progress = unit:GetWorkProgress()
local upgradeBuildTime = unit.UpgradeBuildTime
defaultBuildRate = unit:GetBuildRate()
if progress > 0.05 then --5%. EcoManager & auto-paused mexes etc.
--What build rate do we need to reach required % in 1 tick?
upgradeBuildRate = upgradeBuildTime * progress * 10
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:CalculateVeterancyLevelAfterTransfer(massKilled, massKilledTrue)
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
if not upgradeBuildRate or not shareUpgrades then
if bp.CategoriesHash.UEF then
--use special thread for UEF Kennels.
--Give them 1 tick to spawn their drones and then pause both station and drone.
table.insert(pauseKennels, unit)
else --pause cybran hives immediately
unit:SetPaused(true)
end
elseif bp.CategoriesHash.UEF then
unit.UpgradesTo = upgradesTo
unit.DefaultBuildRate = defaultBuildRate
unit.UpgradeBuildRate = upgradeBuildRate
table.insert(upgradeKennels, unit)
exclude = true
end
end
if upgradeBuildRate and not exclude then
unit.UpgradesTo = upgradesTo
unit.DefaultBuildRate = defaultBuildRate
unit.UpgradeBuildRate = upgradeBuildRate
table.insert(upUnits, unit)
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
if not captured then
if upUnits[1] then
ForkThread(UpgradeTransferredUnits, upUnits)
end
if pauseKennels[1] then
ForkThread(PauseTransferredKennels, pauseKennels)
end
if upgradeKennels[1] then
ForkThread(UpgradeTransferredKennels, upgradeKennels)
end
end
return newUnits
end
function PauseTransferredKennels(pauseKennels)
WaitTicks(1) -- spawn drones
for _, unit in pauseKennels do
unit:SetPaused(true)
for _, pod in unit.PodData or {} do --pause drones
if pod.PodHandle then
pod.PodHandle:SetPaused(true)
end
end
end
end
function UpgradeTransferredUnits(units)
for _, unit in units do
IssueUpgrade({unit}, unit.UpgradesTo)
end
WaitTicks(3) --mex needs at least 3 ticks after IssueUpgrade()
for _, unit in units do
if not unit:BeenDestroyed() then
unit:SetBuildRate(unit.UpgradeBuildRate)
unit:SetConsumptionPerSecondMass(0)
unit:SetConsumptionPerSecondEnergy(0)
end
end
WaitTicks(1)
for _, unit in units do
if not unit:BeenDestroyed() then
unit:SetBuildRate(unit.DefaultBuildRate)
unit:SetPaused(true) --SetPaused() updates ConsumptionPerSecond values
end
end
end
function UpgradeTransferredKennels(upgradeKennels)
WaitTicks(1) --spawn drones
for _, unit in upgradeKennels do
if not unit:BeenDestroyed() then
for _, pod in unit.PodData or {} do --pause Kennels drones
if pod.PodHandle then
pod.PodHandle:SetPaused(true)
end
end
IssueUpgrade({unit}, unit.UpgradesTo)
end
end
WaitTicks(3)
for _, unit in upgradeKennels do
if not unit:BeenDestroyed() then
unit:SetBuildRate(unit.UpgradeBuildRate)
unit:SetConsumptionPerSecondMass(0)
unit:SetConsumptionPerSecondEnergy(0)
end
end
WaitTicks(1)
for _, unit in upgradeKennels do
if not unit:BeenDestroyed() then
unit:SetBuildRate(unit.DefaultBuildRate)
unit:SetPaused(true) --SetPaused() updates ConsumptionPerSecond values
end
end
end
function TransferUnfinishedUnitsAfterDeath(units, armies)
local unfinishedUnits = {}
local noUnits = true
local failedToTransfer = {}
local failedToTransferCounter = 0
local modifiedWrecks = {}
local modifiedUnits = {}
local createWreckIfTransferFailed = {}
for _, unit in EntityCategoryFilterDown(categories.EXPERIMENTAL + categories.TECH3 * categories.STRUCTURE * categories.ARTILLERY, units) do
--This transfer is pretty complex, so we do it only for really important units (EXPs and t3 arty).
if unit:IsBeingBuilt() then
local entityID = unit:GetEntityId()
unfinishedUnits[entityID] = unit
noUnits = nil --have to store units using entityID and not table.insert
end
end
if noUnits or not armies[1] then
return
end
for key, army in armies do
if key == 1 then --this is our first try and first army
local builders = {}
for ID, unit in unfinishedUnits do
local bp = unit:GetBlueprint()
local bplueprintID = bp.BlueprintId
local buildTime = bp.Economy.BuildTime
local health = unit:GetHealth()
local pos = unit:GetPosition()
local progress = unit:GetFractionComplete()
--create invisible drone which belongs to allied army. BuildRange = 10000
local builder = CreateUnitHPR('ZXA0001', army, 5, 20, 5, 0, 0, 0)
table.insert(builders, builder)
builder.UnitHealth = health
builder.UnitPos = pos
builder.UnitID = ID
builder.UnitBplueprintID = bplueprintID
builder.BuildRate = progress * buildTime * 10 --buildRate to reach required progress in 1 tick
builder.DefaultProgress = math.floor(progress * 1000) --save current progress for some later checks
--Save all important data because default unit will be destroyed during our first try
failedToTransfer[ID] = {}
failedToTransferCounter = failedToTransferCounter + 1
failedToTransfer[ID].UnitHealth = health
failedToTransfer[ID].UnitPos = pos
failedToTransfer[ID].Bp = bp
failedToTransfer[ID].BplueprintID = bplueprintID
failedToTransfer[ID].BuildRate = progress * buildTime * 10
failedToTransfer[ID].DefaultProgress = math.floor(progress * 1000)
failedToTransfer[ID].Orientation = unit:GetOrientation()
for _, reclaim in GetReclaimablesInRect(unit:GetSkirtRect()) do --wrecks can prevent drone from starting construction
if reclaim.IsWreckage then
reclaim:SetCollisionShape('None') --so we set collision shape 'None'
table.insert(modifiedWrecks, reclaim) --and save wrecks to revert our changes later
end
end
for _,u in GetUnitsInRect(unit:GetSkirtRect()) do --same as for wrecks
u:SetCollisionShape('None')
table.insert(modifiedUnits, u)
end
if progress > 0.5 then --if transfer failed, we have to create wreck manually. progress should be more than 50%
createWreckIfTransferFailed[ID] = true
end
unit:Destroy() --destroy unfinished unit
IssueBuildMobile({builder}, pos, bplueprintID, {}) --Give command to our drone
end
WaitTicks(3) --Wait some ticks (3 is minimum), IssueBuildMobile() is not instant
for _, builder in builders do
builder:SetBuildRate(builder.BuildRate) --Set crazy build rate and consumption = 0
builder:SetConsumptionPerSecondMass(0)
builder:SetConsumptionPerSecondEnergy(0)
end
WaitTicks(1)
for _, builder in builders do
local newUnit = builder:GetFocusUnit()
local builderProgress = math.floor(builder:GetWorkProgress() * 1000)
if newUnit and builderProgress == builder.DefaultProgress then --our drone is busy and progress == DefaultProgress. Everything is fine
--That's for cases when unit was damaged while being built
--For example: default unit had 100/10000 hp but 90% progress.
newUnit:SetHealth(newUnit, builder.UnitHealth)
failedToTransfer[builder.UnitID] = nil
createWreckIfTransferFailed[builder.UnitID] = nil
failedToTransferCounter = failedToTransferCounter - 1
end
builder:Destroy()
end
elseif failedToTransferCounter > 0 then --failed to transfer some units to first army, let's try others.
--This is just slightly modified version of our first try, no comments here
local builders = {}
for ID, data in failedToTransfer do
local bp = data.Bp
local bplueprintID = data.BplueprintID
local buildRate = data.BuildRate
local health = data.UnitHealth
local pos = data.UnitPos
local progress = data.DefaultProgress
local builder = CreateUnitHPR('ZXA0001', army, 5, 20, 5, 0, 0, 0)
table.insert(builders, builder)
builder.UnitHealth = health
builder.UnitPos = pos
builder.UnitID = ID
builder.UnitBplueprintID = bplueprintID
builder.BuildRate = buildRate
builder.DefaultProgress = progress
IssueBuildMobile({builder}, pos, bplueprintID, {})
end
WaitTicks(3)
for _, builder in builders do
builder:SetBuildRate(builder.BuildRate)
builder:SetConsumptionPerSecondMass(0)
builder:SetConsumptionPerSecondEnergy(0)
end
WaitTicks(1)
for _, builder in builders do
local newUnit = builder:GetFocusUnit()
local builderProgress = math.floor(builder:GetWorkProgress() * 1000)
if newUnit and builderProgress == builder.DefaultProgress then
newUnit:SetHealth(newUnit, builder.UnitHealth)
failedToTransfer[builder.UnitID] = nil
createWreckIfTransferFailed[builder.UnitID] = nil
failedToTransferCounter = failedToTransferCounter - 1
end
builder:Destroy()
end
end
end
local createWreckage = import('/lua/wreckage.lua').CreateWreckage
for ID,_ in createWreckIfTransferFailed do --create 50% wreck. Copied from Unit:CreateWreckageProp()
local data = failedToTransfer[ID]
local bp = data.Bp
local pos = data.UnitPos
local orientation = data.Orientation
local mass = bp.Economy.BuildCostMass * 0.57 --0.57 to compensate some multipliers in CreateWreckage()
local energy = 0
local time = (bp.Wreckage.ReclaimTimeMultiplier or 1) * 2
local wreck = createWreckage(bp, pos, orientation, mass, energy, time)
end
for key, wreck in modifiedWrecks do --revert wrecks collision shape. Copied from Prop.lua SetPropCollision()
local radius = wreck.CollisionRadius
local sizex = wreck.CollisionSizeX
local sizey = wreck.CollisionSizeY
local sizez = wreck.CollisionSizeZ
local centerx = wreck.CollisionCenterX
local centery = wreck.CollisionCenterY
local centerz = wreck.CollisionCenterZ
local shape = wreck.CollisionShape
if radius and shape == 'Sphere' then
wreck:SetCollisionShape(shape, centerx, centery, centerz, radius)
else
wreck:SetCollisionShape(shape, centerx, centery + sizey, centerz, sizex, sizey, sizez)
end
end
for _, u in modifiedUnits do
if not u:BeenDestroyed() then
u:RevertCollisionShape()
end
end
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)
-- Ignore observers and players trying to send resources to themselves
if data.From ~= -1 and data.From ~= data.To then
if not OkayToMessWithArmy(data.From) then
return
end
local fromBrain = GetArmyBrain(data.From)
local toBrain = GetArmyBrain(data.To)
-- Abort if any of the armies is defeated or if trying to send a negative value
if fromBrain:IsDefeated() or toBrain:IsDefeated() or data.Mass < 0 or data.Energy < 0 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