-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArenaState.lua
694 lines (487 loc) · 15.8 KB
/
ArenaState.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
-- The table that contains all the player states.
g_ArenaStates = {}
function CreateArenaState(a_WorldName, a_LobbySpawn)
assert(cRoot:Get():GetWorld(a_WorldName) ~= nil)
assert(tolua.type(a_LobbySpawn) == 'Vector3<float>')
local m_WorldName = a_WorldName
local LobbySpawn = a_LobbySpawn
local m_HasStarted = false
local m_SpawnPointsBlue = {}
local m_SpawnPointsRed = {}
local m_SpawnPointsSpectator = {}
local m_Teams =
{
Blue = {},
Red = {},
Spectator = {}
}
local m_Stats =
{
Kills = 0,
TeamAttacks = 0,
ShotsFired = 0,
}
local m_Inventories = {}
-- Create the object with all the functions.
local self = {}
-- Loops through each player and calls the given callback with the player object as parameter.
function self:ForEachPlayer(a_Callback)
assert(type(a_Callback) == 'function')
local World = cRoot:Get():GetWorld(m_WorldName)
local ShouldStop = false
for TeamName, PlayerList in pairs(m_Teams) do
for PlayerName, _ in pairs(PlayerList) do
if (ShouldStop) then
return
end
World:DoWithPlayer(PlayerName,
function(a_Player)
if (a_Callback(a_Player)) then
ShouldStop = true
end
end
)
end
end
end
-- Returns the coordinates of the lobby.
function self:GetLobbySpawn()
return LobbySpawn
end
-- Returns if the arena has started or not.
function self:HasStarted()
return m_HasStarted
end
-- Starts the arena and teleports all the players to one of the spawn points of their team.
function self:StartArena()
m_HasStarted = true
local World = cRoot:Get():GetWorld(m_WorldName)
-- Teleport everyone to one of the spawnpoints of their team.
-- The red players
for PlayerName, _ in pairs(m_Teams.Red) do
World:DoWithPlayer(PlayerName,
function(a_Player)
local Coords = m_SpawnPointsRed[math.random(1, #m_SpawnPointsRed)]
a_Player:TeleportToCoords(Coords.x, Coords.y, Coords.z)
local Items = cItems()
a_Player:GetInventory():CopyToItems(Items)
m_Inventories[PlayerName] = Items
GiveSnowballs(a_Player)
m_Teams.Red[PlayerName].IsPlaying = true
a_Player:SendMessage(cChatColor.Rose .. "Game started.")
end
) -- cWorld:DoWithPlayer
end
-- The blue players
for PlayerName, _ in pairs(m_Teams.Blue) do
World:DoWithPlayer(PlayerName,
function(a_Player)
local Coords = m_SpawnPointsBlue[math.random(1, #m_SpawnPointsBlue)]
a_Player:TeleportToCoords(Coords.x, Coords.y, Coords.z)
local Items = cItems()
a_Player:GetInventory():CopyToItems(Items)
m_Inventories[PlayerName] = Items
GiveSnowballs(a_Player)
m_Teams.Blue[PlayerName].IsPlaying = true
a_Player:SendMessage(cChatColor.Rose .. "Game started.")
end
) -- cWorld:DoWithPlayer
end
-- The spectators
for PlayerName, _ in pairs(m_Teams.Spectator) do
World:DoWithPlayer(PlayerName,
function(a_Player)
local Coords = m_SpawnPointsSpectator[math.random(1, #m_SpawnPointsSpectator)]
a_Player:TeleportToCoords(Coords.x, Coords.y, Coords.z)
local Items = cItems()
a_Player:GetInventory():CopyToItems(Items)
m_Inventories[PlayerName] = Items
m_Teams.Spectator[PlayerName].IsPlaying = true
a_Player:SendMessage(cChatColor.Rose .. "Game started.")
end
) -- cWorld:DoWithPlayer
end
end
-- Stops the arena and teleports all the players who were in the arena to the lobby.
function self:StopArena(a_ShouldShowStopMessage)
local SendStats
if (m_HasStarted) then
function SendStats(a_Player)
a_Player:SendMessage(cChatColor.Purple .. "Kills: " .. cChatColor.LightGreen .. m_Stats.Kills)
a_Player:SendMessage(cChatColor.Purple .. "TeamAttacks: " .. cChatColor.LightGreen .. m_Stats.TeamAttacks)
a_Player:SendMessage(cChatColor.Purple .. "ShotsFired: " .. cChatColor.LightGreen .. m_Stats.ShotsFired)
end
else
SendStats = function() end
end
-- Teleport everyone to the lobby.
self:ForEachPlayer(
function(a_Player)
if (a_ShouldShowStopMessage) then
a_Player:SendMessage(cChatColor.Rose .. "Arena stopped. The match is over.")
end
-- Return the inventory to the player.
if (m_Inventories[a_Player:GetName()] ~= nil) then
local Inventory = a_Player:GetInventory()
Inventory:Clear()
Inventory:AddItems(m_Inventories[a_Player:GetName()], true, true)
end
SendStats(a_Player)
a_Player:TeleportToCoords(LobbySpawn.x, LobbySpawn.y, LobbySpawn.z)
local State = GetPlayerState(a_Player)
State:JoinArena(nil)
end
) -- self:ForEachPlayer
-- Reset the teams.
m_Teams =
{
Blue = {},
Red = {},
Spectator = {}
}
-- Reset the stats.
m_Stats =
{
Kills = 0,
TeamAttacks = 0,
ShotsFired = 0,
}
-- Reset the inventories.
m_Inventories = {}
m_HasStarted = false
end
-- Sends an message to all the players who have joined
function self:BroadcastMessage(a_Message)
self:ForEachPlayer(
function(a_Player)
a_Player:SendMessage(a_Message)
end
)
end
-- This part adds spawnpoints to the teams.
do
-- Adds one spawnpoint to the blue team.
function self:AddSpawnPointBlue(a_Pos)
assert(tolua.type(a_Pos) == 'Vector3<float>')
table.insert(m_SpawnPointsBlue, a_Pos)
end
-- Add one spawnpoint to the red team.
function self:AddSpawnPointRed(a_Pos)
assert(tolua.type(a_Pos) == 'Vector3<float>')
table.insert(m_SpawnPointsRed, a_Pos)
end
-- Add one spawnpoint to the spectators.
function self:AddSpawnPointSpectator(a_Pos)
assert(tolua.type(a_Pos) == 'Vector3<float>')
table.insert(m_SpawnPointsSpectator, a_Pos)
end
end
-- This part manages the Get spawnpoint functions
do
-- Returns one of the spawnpoints from the red team randomly:
function self:GetRandomRedSpawn()
return m_SpawnPointsRed[math.random(1, #m_SpawnPointsRed)]
end
-- Returns one of the spawnpoints from the red team randomly:
function self:GetRandomBlueSpawn()
return m_SpawnPointsBlue[math.random(1, #m_SpawnPointsBlue)]
end
-- Returns one of the spawnpoints from the spectators randomly:
function self:GetRandomSpectatorSpawn()
return m_SpawnPointsSpectator[math.random(1, #m_SpawnPointsSpectator)]
end
-- Returns the table where all the red spawnpoints are in.
function self:GetRedSpawnpoints()
return m_SpawnPointsRed
end
-- Returns the table where all the blue spawnpoints are in.
function self:GetBlueSpawnpoints()
return m_SpawnPointsBlue
end
-- Returns the table where all the spectator spawnpoints are in.
function self:GetSpectatorSpawnpoints()
return m_SpawnPointsSpectator
end
-- returns the amount of spawnpoints team red has.
function self:GetNumRedSpawnpoints()
return #m_SpawnPointsRed
end
-- returns the amount of spawnpoints team blue has.
function self:GetNumBlueSpawnpoints()
return #m_SpawnPointsBlue
end
-- returns the amount of spawnpoints the spectators have.
function self:GetNumSpectatorSpawnpoints()
return #m_SpawnPointsSpectator
end
end
-- This part returns the amount of playing players.
do
-- Amount of playing red players.
function self:GetNumPlayingRedPlayers()
local Count = 0
for PlayerName, Data in pairs(m_Teams.Red) do
if (Data.IsPlaying) then
Count = Count + 1
end
end
return Count
end
-- Amount of playing blue players
function self:GetNumPlayingBluePlayers()
local Count = 0
for PlayerName, Data in pairs(m_Teams.Blue) do
if (Data.IsPlaying) then
Count = Count + 1
end
end
return Count
end
-- Amount of playing players.
function self:GetNumPlayingPlayers()
return (self:GetNumPlayingRedPlayers() + self:GetNumPlayingBluePlayers())
end
-- Returns the amount of red players
function self:GetNumRedPlayers()
local Count = 0
for PlayerName, Data in pairs(m_Teams.Red) do
Count = Count + 1
end
return Count
end
-- Returns the amount of blue players
function self:GetNumBluePlayers()
local Count = 0
for PlayerName, Data in pairs(m_Teams.Blue) do
Count = Count + 1
end
return Count
end
-- Returns the amount of players.
function self:GetNumPlayers()
return (self:GetNumRedPlayers() + self:GetNumBluePlayers())
end
end
-- JoinTeam functions.
do
local function CheckIfCanStart()
if (m_HasStarted) then
return
end
local MaxNeeded = MAXPLAYERSNEEDED / 2
if (
(self:GetNumRedPlayers() >= MaxNeeded) and
(self:GetNumBluePlayers() >= MaxNeeded)
) then
self:StartArena()
end
end
-- Join red team.
function self:JoinRedTeam(a_PlayerName)
m_Teams.Red[a_PlayerName] =
{
Kills = 0,
Deaths = 0,
IsPlaying = false,
Team = "Red",
}
CheckIfCanStart()
end
-- Join blue team.
function self:JoinBlueTeam(a_PlayerName)
m_Teams.Blue[a_PlayerName] =
{
Kills = 0,
Deaths = 0,
IsPlaying = false,
Team = "Blue",
}
CheckIfCanStart()
end
-- Join spectator. A spectator doesn't have any kills or deaths so we just mark him as "There"
function self:JoinSpectators(a_PlayerName)
m_Teams.Spectator[a_PlayerName] = true
CheckIfCanStart()
end
-- Joins one of the teams. If blue has less then red you join blue and the other way around. If they have an equal amount of players you will join one randomly
function self:JoinArena(a_Player)
local NumPlayersTeamRed = self:GetNumRedPlayers()
local NumPlayersTeamBlue = self:GetNumBluePlayers()
-- Choose a team. choose the team with the lowest players, and if the amount of players is equal then choose randomly.
if (NumPlayersTeamRed < NumPlayersTeamBlue) then
self:JoinRedTeam(a_Player)
elseif (NumPlayersTeamRed > NumPlayersTeamBlue) then
self:JoinBlueTeam(a_Player)
else
local Team = math.random(1, 2)
if (Team == 1) then -- Red team
self:JoinRedTeam(a_Player)
elseif (Team == 2) then -- Blue team
self:JoinBlueTeam(a_Player)
end
end
end
end
-- Just teleport the player to the lobby and remove the player from all the teams.
function self:LeaveArena(a_PlayerName)
local World = cRoot:Get():GetWorld(m_WorldName)
World:DoWithPlayer(a_PlayerName,
function(a_Player)
a_Player:TeleportToCoords(LobbySpawn.x, LobbySpawn.y, LobbySpawn.z)
end
)
m_Teams.Red[a_PlayerName] = nil
m_Teams.Blue[a_PlayerName] = nil
m_Teams.Spectator[a_PlayerName] = nil
self:CheckIfEnoughPlayers()
end
-- Returns the the table wich contains all the teams
function self:GetTeams()
return m_Teams
end
-- Returns the name wich the arena is in.
function self:GetWorldName()
return m_WorldName
end
-- Checks if one of the teams doesn't have enough players to keep playing.
function self:CheckIfEnoughPlayers()
if (not m_HasStarted) then
return
end
local NumBluePlayers = self:GetNumPlayingBluePlayers()
local NumRedPlayers = self:GetNumPlayingRedPlayers()
if (NumBluePlayers <= 0) then
self:BroadcastMessage(cChatColor.Rose .. "Red team wins.")
self:StopArena()
return true
elseif (NumRedPlayers <= 0) then
self:BroadcastMessage(cChatColor.Blue .. "Blue team wins.")
self:StopArena()
return true
end
return false
end
-- Returns the player info of a player. false if it doesn't exist.
function self:GetPlayerInfo(a_PlayerName)
return (m_Teams.Red[a_PlayerName] or m_Teams.Blue[a_PlayerName] or false)
end
-- Checks if it was friendly fire, plays a nice Pop sound and stops the arena if there are not enough players left.
function self:HitPlayer(a_Attacker, a_Receiver)
local ColorPrefix = cChatColor.Blue
local AttackerName = a_Attacker:GetName()
local ReceiverName = a_Receiver:GetName()
if (m_Teams.Red[AttackerName]) then
ColorPrefix = cChatColor.Rose
end
local ReceiverInfo = self:GetPlayerInfo(ReceiverName)
local AttackerInfo = self:GetPlayerInfo(AttackerName)
if (ReceiverInfo.Team == AttackerInfo.Team) then
self:AddStatsTeamFire()
return
end
ReceiverInfo.Deaths = ReceiverInfo.Deaths + 1
AttackerInfo.Kills = AttackerInfo.Kills + 1
local World = a_Receiver:GetWorld()
World:BroadcastSoundEffect("random.pop", a_Receiver:GetPosX() * 8, a_Receiver:GetPosY() * 8, a_Receiver:GetPosZ() * 8, 1, 126)
World:BroadcastSoundEffect("random.pop", a_Attacker:GetPosX() * 8, a_Attacker:GetPosY() * 8, a_Attacker:GetPosZ() * 8, 1, 126)
self:AddStatsKills()
local Coords = nil
if (ReceiverInfo.Team == "Blue") then
Coords = self:GetRandomBlueSpawn()
self:BroadcastMessage(cChatColor.Rose .. AttackerName .. " hit " .. ReceiverName .. " [" .. AttackerInfo.Kills .. "]")
else
Coords = self:GetRandomRedSpawn()
self:BroadcastMessage(cChatColor.Blue .. AttackerName .. " hit " .. ReceiverName .. " [" .. AttackerInfo.Kills .. "]")
end
if (ReceiverInfo.Deaths >= g_DeathsNeeded) then
ReceiverInfo.IsPlaying = false
a_Receiver:SendMessage(cChatColor.Rose .. "You died. You had " .. ReceiverInfo.Kills .. " kills.")
if (not self:CheckIfEnoughPlayers()) then
Coords = LobbyCoords
end
return
end
GiveSnowballs(a_Receiver)
a_Receiver:TeleportToCoords(Coords.x, Coords.y, Coords.z)
end
-- This part manages all the stats.
do
-- Add one to the kills stats.
function self:AddStatsKills()
m_Stats.Kills = m_Stats.Kills + 1
end
-- Add one to the TeamFire stats.
function self:AddStatsTeamFire()
m_Stats.TeamAttacks = m_Stats.TeamAttacks + 1
end
-- Add one to the shots fired stats.
function self:AddStatsShotsFired()
m_Stats.ShotsFired = m_Stats.ShotsFired + 1
end
end
function self:CollectTop5Players()
local Top5 = {}
local function HandlePlayer(PlayerName, PlayerInfo, PrefixColor)
if (#Top5 == 0) then
table.insert(Top5, {PlayerName = PlayerName, Kills = PlayerInfo.Kills, Color = PrefixColor})
return
end
for Idx = 1, #Top5 do
if (Top5[Idx] == nil) then
table.insert(Top5, Idx, {PlayerName = PlayerName, Kills = PlayerInfo.Kills, Color = PrefixColor})
break
else
if (Top5[Idx].Kills < PlayerInfo.Kills) then
table.insert(Top5, Idx, {PlayerName = PlayerName, Kills = PlayerInfo.Kills, Color = PrefixColor})
elseif (#Top5 < 5) then
table.insert(Top5, {PlayerName = PlayerName, Kills = PlayerInfo.Kills, Color = PrefixColor})
end
end
end
end
for PlayerName, PlayerInfo in pairs(m_Teams.Red) do
HandlePlayer(PlayerName, PlayerInfo, cChatColor.Rose)
end
for PlayerName, PlayerInfo in pairs(m_Teams.Blue) do
HandlePlayer(PlayerName, PlayerInfo, cChatColor.Blue)
end
return Top5
end
return self
end
-- Returns the arenastate.
function GetArenaState(a_Arena)
if (g_ArenaStates[a_Arena] == nil) then
return false
end
return g_ArenaStates[a_Arena]
end
-- Creates an arenastate and adds it to the g_ArenaStates table. Returns false if the arenastate already exists.
function InitializeArenaState(a_ArenaName, a_WorldName, a_LobbySpawn)
assert(type(a_ArenaName) == 'string')
assert(cRoot:Get():GetWorld(a_WorldName) ~= nil)
assert(tolua.type(a_LobbySpawn) == 'Vector3<float>')
if (g_ArenaStates[a_ArenaName]) then
return false
end
local ArenaState = CreateArenaState(a_WorldName, a_LobbySpawn)
g_ArenaStates[a_ArenaName] = ArenaState
return ArenaState
end
-- Returns true if the state exists.
function ArenaStateExists(a_Arena)
if (g_ArenaStates[a_Arena]) then
return true
end
return false
end
-- Loops through each arena and calls the given callback. The parameters are: a_ArenaState, a_ArenaName
function ForEachArena(a_Callback)
assert(type(a_Callback) == 'function')
for ArenaName, _ in pairs(g_ArenaStates) do
if (a_Callback(GetArenaState(ArenaName), ArenaName)) then
return true
end
end
return false
end