/
game_flagManager.lua
479 lines (429 loc) · 17.6 KB
/
game_flagManager.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
function gadget:GetInfo()
return {
name = "Game - Beacon Manager",
desc = "Populates maps with flags and handles control",
author = "FLOZi",
date = "Adopted from S44 flagManager 10/02/2011",
license = "GNU GPL v2",
layer = 1, -- must be before game_spawn
enabled = true -- loaded by default?
}
end
-- function localisations
local floor = math.floor
-- Synced Read
local AreTeamsAllied = Spring.AreTeamsAllied
local GetFeatureDefID = Spring.GetFeatureDefID
local GetFeaturePosition = Spring.GetFeaturePosition
local GetFeaturesInRectangle = Spring.GetFeaturesInRectangle
local GetGroundHeight = Spring.GetGroundHeight
local GetGroundInfo = Spring.GetGroundInfo
local GetUnitHeading = Spring.GetUnitHeading
local GetUnitPosition = Spring.GetUnitPosition
local GetUnitsInCylinder = Spring.GetUnitsInCylinder
local GetUnitTeam = Spring.GetUnitTeam
local GetTeamInfo = Spring.GetTeamInfo
local GetTeamRulesParam = Spring.GetTeamRulesParam
local GetTeamUnitDefCount = Spring.GetTeamUnitDefCount
-- Synced Ctrl
local CallCOBScript = Spring.CallCOBScript
local CreateUnit = Spring.CreateUnit
local DestroyFeature = Spring.DestroyFeature
local GameOver = Spring.GameOver
local GiveOrderToUnit = Spring.GiveOrderToUnit
local KillTeam = Spring.KillTeam
local SetGameRulesParam = Spring.SetGameRulesParam
local SetTeamRulesParam = Spring.SetTeamRulesParam
local SetUnitAlwaysVisible = Spring.SetUnitAlwaysVisible
local SetUnitMetalExtraction = Spring.SetUnitMetalExtraction
local SetUnitNeutral = Spring.SetUnitNeutral
local SetUnitNoSelect = Spring.SetUnitNoSelect
local SetUnitResourcing = Spring.SetUnitResourcing
local SetUnitRulesParam = Spring.SetUnitRulesParam
local TransferUnit = Spring.TransferUnit
-- constants
local GAIA_TEAM_ID = Spring.GetGaiaTeamID()
local GAIA_ALLY_ID = select(6, GetTeamInfo(GAIA_TEAM_ID))
local PROFILE_PATH = "maps/flagConfig/" .. Game.mapName .. "_profile.lua"
local DEBUG = false -- enable to print out flag locations in profile format
local CAP_MULT = 1 --multiplies against the FBI defined CapRate
local DEF_MULT = 1 --multiplies against the FBI defined DefRate
local BEACON_ID = UnitDefNames["beacon"].id
-- variables
local unitCapStrengths = {} -- unitCapStrengths[unitID] = number or 1
local unitDefsToIgnore = {}
local flagTypes = {"beacon"}
local flags = {} -- flags[flagType][index] == flagUnitID
local numFlags = {} -- numFlags[flagType] == numberOfFlagsOfType
local totalFlags = 0
local flagTypeSpots = {} -- flagTypeSpots[flagType][metalSpotCount] == {x = x_coord, z = z_coord}
for _, flagType in pairs(flagTypes) do
flags[flagType] = {}
numFlags[flagType] = 0
flagTypeSpots[flagType] = {}
end
local CAP_THRESHOLD = 20
local CAP_RADIUS = 460
local REGEN = 1
local flagCapStatuses = {} -- flagCapStatus[flagID] = {allyTeamID = {team = teamID, cap = cap}, ...}
local teams = Spring.GetTeamList()
local teamUnitCounts = {}
for _, teamID in pairs(Spring.GetTeamList()) do
teamUnitCounts[teamID] = 0
end
local modOptions
if (Spring.GetModOptions) then
modOptions = Spring.GetModOptions()
end
-- BEACON TICKET DECLARATIONS
local START_TICKETS = tonumber(modOptions.start_tickets) or 100
local TICKET_LOSS_PER_BEACON = 1
local CAP_BONUS = tonumber(modOptions.capincome) or 2000
local beaconsPerAllyTeam = {}
local tickets = {}
local bleedTimes = {}
local allyTeamAlive = {}
local deadAllyTeams = 0
local allyTeams = Spring.GetAllyTeamList()
for i = 1, #allyTeams do
local allyTeam = allyTeams[i]
if allyTeam == GAIA_ALLY_ID then
allyTeams[i] = nil
else
tickets[allyTeam] = START_TICKETS
bleedTimes[allyTeam] = 0
beaconsPerAllyTeam[allyTeam] = 0
allyTeamAlive[allyTeam] = true
end
end
if (gadgetHandler:IsSyncedCode()) then
-- SYNCED
local DelayCall = GG.Delay.DelayCall
local function SetUnitCapStrength(unitID, mult)
unitCapStrengths[unitID] = unitCapStrengths[unitID] * mult
end
GG.SetUnitCapStrength = SetUnitCapStrength
function UpdateBeacons(teamID, num)
if teamID == GAIA_TEAM_ID then return end -- Gaia does not have tickets
local allyTeam = select(6, GetTeamInfo(teamID))
beaconsPerAllyTeam[allyTeam] = beaconsPerAllyTeam[allyTeam] + num
SetTeamRulesParam(teamID, "beacons", (GetTeamRulesParam(teamID, "beacons") or 0) + num, {public = true})
SendToUnsynced("BEACONUPDATE", allyTeam, beaconsPerAllyTeam[allyTeam])
end
function DecrementTickets(allyTeam)
if allyTeam == GAIA_ALLY_ID then return end -- Gaia does not have tickets
if tickets[allyTeam] > 0 then
tickets[allyTeam] = tickets[allyTeam] - 1
--Spring.Echo("AllyTeam " .. allyTeam .. "[".. beaconsPerAllyTeam[allyTeam] .. "] lost 1 ticket (" .. tickets[allyTeam] .. ")")
SetGameRulesParam("tickets" .. allyTeam, tickets[allyTeam], {public = true})
end
if tickets[allyTeam] == 0 then
if allyTeamAlive[allyTeam] then
local teams = Spring.GetTeamList(allyTeam)
for i = 1, #teams do
local teamID = teams[i]
local teamUnits = Spring.GetTeamUnits(teamID)
for i = 1, #teamUnits do
TransferUnit(teamUnits[i], GAIA_TEAM_ID, false)
end
KillTeam(teamID)
end
allyTeamAlive[allyTeam] = false
deadAllyTeams = deadAllyTeams + 1
if deadAllyTeams == (#allyTeams - 1) then
for team, alive in pairs(allyTeamAlive) do
if alive then GameOver({team}) break end
end
end
end
end
end
-- this function is used to add any additional flagType specific behaviour
function FlagSpecialBehaviour(action, flagType, flagID, flagTeamID, teamID)
--[[if action == "placed" then
elseif action == "capped" then
end]]
end
function PlaceFlag(spot, flagType, newFlag)
if DEBUG then
Spring.Echo("{")
Spring.Echo(" x = " .. spot.x .. ",")
Spring.Echo(" z = " .. spot.z .. ",")
Spring.Echo("},")
end
if not newFlag then
newFlag = CreateUnit(flagType, spot.x, Spring.GetGroundHeight(spot.x, spot.z), spot.z, 0, GAIA_TEAM_ID)
end
numFlags[flagType] = numFlags[flagType] + 1
totalFlags = totalFlags + 1
flags[flagType][numFlags[flagType]] = newFlag
flagCapStatuses[newFlag] = {}
SetUnitNeutral(newFlag, true)
SetUnitAlwaysVisible(newFlag, true)
local squareSize = 100
local features = GetFeaturesInRectangle(spot.x - squareSize, spot.z - squareSize, spot.x + squareSize, spot.z + squareSize)
for i = 1, #features do
DestroyFeature(features[i])
end
FlagSpecialBehaviour("placed", flagType, newFlag, GAIA_TEAM_ID, GAIA_TEAM_ID)
end
function gadget:GamePreload()
if DEBUG then Spring.Echo(PROFILE_PATH) end
-- CHECK FOR PROFILES
if VFS.FileExists(PROFILE_PATH) then
local flagSpots, temps = VFS.Include(PROFILE_PATH)
if flagSpots and #flagSpots > 0 then
Spring.Echo("Map Beacon Profile found. Loading " .. (#flagSpots or 0) .. " Beacon positions...")
flagTypeSpots["beacon"] = flagSpots
end
-- setup default ambient temps
if not temps then temps = {} end
temps.ambient = temps.ambient or 20
temps.water = temps.water or 10
GG.MapTemperatures = temps
Spring.SetGameRulesParam("MAP_TEMP_AMBIENT", temps.ambient)
Spring.SetGameRulesParam("MAP_TEMP_WATER", temps.water)
else
for i=1,20 do
Spring.Echo("NO MAP PROFILE FOUND FOR " .. Game.mapName)
end
local temps = {ambient = 20, water = 10}
GG.MapTemperatures = temps
Spring.SetGameRulesParam("MAP_TEMP_AMBIENT", temps.ambient)
Spring.SetGameRulesParam("MAP_TEMP_WATER", temps.water)
end
for unitDefID, ud in pairs(UnitDefs) do -- cache ignored unitDefIDs
if ud.canFly or string.tobool(ud.customParams.ignoreatbeacon) then
unitDefsToIgnore[unitDefID] = true
end
end
end
function gadget:GameStart()
-- FLAG PLACEMENT
for _, flagType in pairs(flagTypes) do
if DEBUG then Spring.Echo("-- flagType is " .. flagType) end
for i = 1, #flagTypeSpots[flagType] do
PlaceFlag(flagTypeSpots[flagType][i], flagType)
end
GG[flagType .. "s"] = flags[flagType] -- nicer to have GG.flags rather than GG.flag
end
end
local function StripUnits(unitsAtFlag)
for i = #unitsAtFlag, 1, -1 do -- iterate over list in reverse so removing doesn't screw with index
local unitID = unitsAtFlag[i]
local unitDefID = Spring.GetUnitDefID(unitID)
-- bad defs are cached, but also ignore any units currently being transported
if unitDefsToIgnore[unitDefID] or Spring.GetUnitTransporter(unitID) then
table.remove(unitsAtFlag, i)
end
end
end
local function TransferFlag(flagID, flagTeamID, teamID)
local capped = flagTeamID == GAIA_TEAM_ID
TransferUnit(flagID, capped and teamID or GAIA_TEAM_ID, false)
Spring.AddTeamResource(teamID, "metal", CAP_BONUS) -- 2k CBills for neut or cap
if capped then -- new team is gaining a beacon
-- Update the new team
UpdateBeacons(teamID, 1)
else -- neutralised, so old team is losing a beacon
-- Update the old team
UpdateBeacons(flagTeamID, -1)
end
-- Turn flag back on TODO: check if this can be avoided in MCL?
GiveOrderToUnit(flagID, CMD.ONOFF, {1}, {})
-- Flag has changed team, reset capping statuses
flagCapStatuses[flagID] = {}
for _, cleanTeamID in pairs(Spring.GetTeamList()) do
SetUnitRulesParam(flagID, "cap" .. tostring(cleanTeamID), 0, {public = true})
end
end
local function FlagCapChange(flagID, flagTeamID, allyTeamID, teamID, change)
if not flagCapStatuses[flagID][allyTeamID] then
flagCapStatuses[flagID][allyTeamID] = {team = teamID, cap = 0}
end
if (flagCapStatuses[flagID][allyTeamID].cap == 0) and change < 0 then
return -- trying to regen when already 0, quit
end
if flagCapStatuses[flagID][allyTeamID].team ~= teamID then
SetUnitRulesParam(flagID, "cap" .. tostring(flagCapStatuses[flagID][allyTeamID].team), 0, {public = true}) -- reset the old team to 0
flagCapStatuses[flagID][allyTeamID].team = teamID -- update teamID, switched to another team in the same alliance
end
flagCapStatuses[flagID][allyTeamID].cap = flagCapStatuses[flagID][allyTeamID].cap + change -- add the cap increase
SetUnitRulesParam(flagID, "cap" .. tostring(teamID), flagCapStatuses[flagID][allyTeamID].cap, {public = true}) -- set the rules param per team for team colour
if flagCapStatuses[flagID][allyTeamID].cap <= 0 then
-- TODO: Really we need to check that __all__ non-ally statuses are 0
GG.PlaySoundForTeam(flagTeamID, "BB_Beacon_Secured", 1)
SetUnitRulesParam(flagID, "secure", 1, {public = true})
elseif flagCapStatuses[flagID][allyTeamID].cap == 1 then -- first cap step
GG.PlaySoundForTeam(flagTeamID, "BB_Beacon_UnderAttack", 1)
SetUnitRulesParam(flagID, "secure", 0, {public = true})
elseif flagCapStatuses[flagID][allyTeamID].cap > CAP_THRESHOLD and teamID ~= flagTeamID then -- capped or neutralised
TransferFlag(flagID, flagTeamID, teamID)
GG.PlaySoundForTeam(flagTeamID, "BB_Beacon_Lost", 1)
if flagTeamID == GAIA_TEAM_ID then -- flag was capped, not neutralised
GG.PlaySoundForTeam(teamID, "BB_Beacon_Captured", 1)
SetUnitRulesParam(flagID, "secure", 1, {public = true})
end
end
end
-- updates caps for all teams
local function FlagCapRegen(flagID, flagTeamID, change)
for _, teamID in pairs(Spring.GetTeamList()) do
local isDead, _, _, allyTeamID = select(3, Spring.GetTeamInfo(teamID))
if teamID ~= flagTeamID and not isDead then
FlagCapChange(flagID, flagTeamID, allyTeamID, teamID, change)
end
end
end
function gadget:GameFrame(n)
-- FLAG CONTROL
if n % 30 == 5 then -- every second with a 5 frame offset
for _, flagType in pairs(flagTypes) do
--for spotNum, flagID in pairs(flags[flagType]) do
for spotNum = 1, numFlags[flagType] do -- WARNING: Assumes flags are placed in order they exist in flags[flagType]
local flagID = flags[flagType][spotNum]
local flagTeamID = GetUnitTeam(flagID)
local flagAllyTeam = select(6, Spring.GetTeamInfo(flagTeamID))
local spots = flagTypeSpots[flagType]
local flagTeamCounts = {} -- flagTeamCounts[teamID] = number
local flagAllyTeamCounts = {} -- flagAllyTeamCounts[teamID] = number
-- First check if there are any friendly (ally) units here -> flag is defended
local unitsAtFlag = GetUnitsInCylinder(spots[spotNum].x, spots[spotNum].z, CAP_RADIUS)
StripUnits(unitsAtFlag) -- strips table (in place) of ignored unitdefs
for i, unitID in ipairs(unitsAtFlag) do
local teamID = Spring.GetUnitTeam(unitID)
flagTeamCounts[teamID] = (flagTeamCounts[teamID] or 0) + (unitCapStrengths[unitID] or 1)
local allyTeamID = select(6, Spring.GetTeamInfo(teamID))
flagAllyTeamCounts[allyTeamID] = (flagAllyTeamCounts[allyTeamID] or 0) + (unitCapStrengths[unitID] or 1)
end
if flagAllyTeamCounts[flagAllyTeam] or 0 > 0 then -- flag is defended, reduce all cap status
FlagCapRegen(flagID, flagTeamID, -REGEN)
else -- flag is undefended, proceed to check for cappers
local numberOfAllyTeamsAtFlag = 0
local cappingAllyTeam
for allyTeamID, count in pairs(flagAllyTeamCounts) do
if count > 0 then
numberOfAllyTeamsAtFlag = numberOfAllyTeamsAtFlag + 1
cappingAllyTeam = allyTeamID
end
end
if numberOfAllyTeamsAtFlag == 1 then -- only one allyteam present, we can cap!
-- check for the highest team count and set this as the allyteams cap team
local capStatus = flagCapStatuses[flagID][cappingAllyTeam]
local cappingTeam = capStatus and capStatus.team or nil
local maxCount = flagTeamCounts[cappingTeam] or 0
for teamID, count in pairs(flagTeamCounts) do
if count > maxCount then
maxCount = count
cappingTeam = teamID
end
end
FlagCapChange(flagID, flagTeamID, cappingAllyTeam, cappingTeam, maxCount)
-- TODO: if equal then go for team with fewest beacons
-- TODO: if equal pick first unit in list
elseif numberOfAllyTeamsAtFlag > 1 then -- flag is contested, pause cap status
-- probably don't need to actually do anything here
elseif flagAllyTeam == GAIA_ALLY_ID then -- no allyTeam units present, flag is an empty neutral flag
--Spring.Echo(flagID, "Ronery, so rorenery")
FlagCapRegen(flagID, flagTeamID, -REGEN)
else -- no allyTeam units present, flag is empty
--Spring.Echo("Nothing at flag, regen")
FlagCapRegen(flagID, flagTeamID, -REGEN)
end
end
end
end
local maxBeacon = 0
-- manage tickets
for allyTeam, numBeacon in pairs(beaconsPerAllyTeam) do
maxBeacon = math.max(numBeacon, maxBeacon)
end
for allyTeam, numBeacon in pairs(beaconsPerAllyTeam) do
if numBeacon < maxBeacon then
bleedTimes[allyTeam] = math.floor(totalFlags / (maxBeacon - numBeacon) * 30) -- 7/1 = 7,
else
bleedTimes[allyTeam] = 0
end
end
end
for allyTeam, bleed in pairs(bleedTimes) do
if bleed > 0 and n % bleed == 0 then
DecrementTickets(allyTeam)
end
end
end
function gadget:UnitCreated(unitID, unitDefID, unitTeam, builderID)
local ud = UnitDefs[unitDefID]
local cp = ud.customParams
if cp.baseclass == "mech" then
teamUnitCounts[unitTeam] = teamUnitCounts[unitTeam] + 1
unitCapStrengths[unitID] = 1
end
if unitDefID == BEACON_ID and unitTeam ~= GAIA_TEAM_ID then
local x,_, z = Spring.GetUnitPosition(unitID)
local newSpot = {["x"] = x, ["z"] = z}
table.insert(flagTypeSpots["beacon"], newSpot)
PlaceFlag(newSpot, "beacon", unitID)
UpdateBeacons(unitTeam, 1)
end
end
function SetTickets(allyTeam, amount)
tickets[allyTeam] = math.min(amount, tickets[allyTeam])
DecrementTickets(allyTeam)
end
GG.SetTickets = SetTickets
function CheckAllyTeamUnits(unitTeam)
if unitTeam == GAIA_TEAM_ID then return end -- Gaia does not have tickets
if teamUnitCounts[unitTeam] + Spring.GetTeamUnitDefCount(unitTeam, BEACON_ID) == 0 then
-- Check if this was last unit on whole allyteam
local allyTeam = select(6, Spring.GetTeamInfo(unitTeam))
local allyTeamUnitCount = 0
for _, teamID in pairs(Spring.GetTeamList(allyTeam)) do
allyTeamUnitCount = allyTeamUnitCount + teamUnitCounts[teamID] + Spring.GetTeamUnitDefCount(teamID, BEACON_ID)
end
-- If it was, this implies they lost all beacons and DZ so can't get any more
if allyTeamUnitCount == 0 then
-- Therefore deduct (nearly) all their remaining tickets
SetTickets(allyTeam, 5)
end
end
end
function gadget:UnitDestroyed(unitID, unitDefID, unitTeam, attackerID, attackerDefID, attackerTeam)
local ud = UnitDefs[unitDefID]
if ud.customParams.baseclass == "mech" and Spring.GetUnitRulesParam(unitID, "sold") ~= 1 then
teamUnitCounts[unitTeam] = teamUnitCounts[unitTeam] - 1
-- Remove 1 ticket for each mech killed
local allyTeam = select(6, Spring.GetTeamInfo(unitTeam))
DelayCall(DecrementTickets, {allyTeam}, 1)
DelayCall(CheckAllyTeamUnits, {unitTeam}, 1)
end
end
function gadget:UnitGiven(unitID, unitDefID, newTeam, oldTeam)
local ud = UnitDefs[unitDefID]
if ud.customParams.baseclass == "mech" then
teamUnitCounts[oldTeam] = teamUnitCounts[oldTeam] - 1
teamUnitCounts[newTeam] = teamUnitCounts[newTeam] + 1
end
DelayCall(CheckAllyTeamUnits, {oldTeam}, 1)
end
function gadget:Initialize()
if Spring.GetGameFrame() > 1 then
gadget:GamePreload()
--[[for _,unitID in ipairs(Spring.GetAllUnits()) do
local teamID = Spring.GetUnitTeam(unitID)
local unitDefID = Spring.GetUnitDefID(unitID)
gadget:UnitCreated(unitID, unitDefID, teamID)
end]]
end
end
else
-- UNSYNCED
function PassToUI(eventID, allyTeamID, new)
if Script.LuaUI.BEACONUPDATE then
Script.LuaUI.BEACONUPDATE(allyTeamID, new)
end
end
function gadget:Initialize()
gadgetHandler:AddSyncAction("BEACONUPDATE", PassToUI)
end
end