/
rounds.lua
193 lines (162 loc) · 4.93 KB
/
rounds.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
--[[ Rounds are handled here, obviously ]]--
-----------------------------------
-- THE HOOKS THAT THIS CALLS ARE --
-- OBJHUNT_RoundStart --
-- OBJHUNT_RoundEnd_Props --
-- OBJHUNT_RoundEnd_Hunters --
-- OBJHUNT_RoundLimit --
-----------------------------------
-- this var is used outside of this file
round = {}
round.state = ROUND_WAIT
round.current = 0
round.startTime = 0
round.endTime = 0
round.winner = "Newbrict"
local function SendRoundUpdate( sendMethod )
net.Start( "Round Update" )
net.WriteUInt(round.state, 8)
net.WriteUInt(round.current, 8)
net.WriteUInt(round.startTime, 32)
net.WriteUInt(CurTime(), 32)
sendMethod()
end
local function SwapTeams()
local hunters = team.GetPlayers(TEAM_HUNTERS)
local props = team.GetPlayers(TEAM_PROPS)
for _, v in pairs(hunters) do
if( IsValid(v) ) then
RemovePlayerProp( v )
v:SetTeam( TEAM_PROPS )
player_manager.SetPlayerClass( v, "player_prop" )
v:KillSilent()
v:Spawn()
end
end
for _, v in pairs(props) do
if( IsValid(v) ) then
RemovePlayerProp( v )
v:SetTeam( TEAM_HUNTERS )
player_manager.SetPlayerClass( v, "player_hunter" )
v:KillSilent()
v:Spawn()
end
end
end
local function WaitRound()
-- wait for everyone to connect and what not
local mapTime = CurTime()
if( mapTime < OBJHUNT_PRE_ROUND_TIME ) then return end
-- make sure we have at least one player on each team
local hunters = team.GetPlayers(TEAM_HUNTERS)
local props = team.GetPlayers(TEAM_PROPS)
if( #props == 0 || #hunters == 0 ) then return end
round.state = ROUND_START
end
local function StartRound()
round.current = round.current + 1
round.startTime = CurTime()
-- reset the map
game.CleanUpMap(false, {"player_prop_ent"})
-- swap teams, respawn everyone
SwapTeams()
round.state = ROUND_IN
hook.Call( "OBJHUNT_RoundStart" )
end
local function InRound()
local roundTime = CurTime() - round.startTime
-- make sure we have not gone over time
if( roundTime >= OBJHUNT_ROUND_TIME ) then
round.state = ROUND_END
round.endTime = CurTime()
round.winner = "Props"
hook.Call( "OBJHUNT_RoundEnd" )
return
end
-- make sure there is at least one living player left per team
local hunters = GetLivingPlayers(TEAM_HUNTERS)
local props = GetLivingPlayers(TEAM_PROPS)
if( #props == 0 ) then
round.state = ROUND_END
round.endTime = CurTime()
round.winner = "Hunters"
hook.Call( "OBJHUNT_RoundEnd" )
return
end
if( #hunters == 0 ) then
round.state = ROUND_END
round.endTime = CurTime()
round.winner = "Props"
hook.Call( "OBJHUNT_RoundEnd" )
return
end
-- unfreeze the hunters after their time is up
if( roundTime > OBJHUNT_HIDE_TIME && hunters[1]:IsFrozen() ) then
for _, v in pairs(hunters) do
v:Freeze( false )
end
end
end
local function EndRound()
-- if we've played enough times on this map
if( round.current >= OBJHUNT_ROUNDS ) then
hook.Call( "OBJHUNT_RoundLimit" )
return
end
-- make sure we have at least one player on each team
local hunters = team.GetPlayers(TEAM_HUNTERS)
local props = team.GetPlayers(TEAM_PROPS)
if( #props == 0 || #hunters == 0 ) then return end
-- start the round after we've waiting long enough
local waitTime = CurTime() - round.endTime
if( waitTime >= OBJHUNT_POST_ROUND_TIME ) then
round.state = ROUND_START
end
end
local roundHandler = {}
roundHandler[ROUND_WAIT] = WaitRound
roundHandler[ROUND_START] = StartRound
roundHandler[ROUND_IN] = InRound
roundHandler[ROUND_END] = EndRound
-- start the round orchestrator when the game has initialized
hook.Add( "Initialize", "Begin round functions", function()
hook.Add( "Tick", "Round orchestrator", function()
roundHandler[round.state]()
end )
end )
hook.Add( "OBJHUNT_RoundStart", "Round start stuff", function()
print( "Round "..round.current.." is Starting" )
-- send data to clients
SendRoundUpdate( function() return net.Broadcast() end )
-- reset previous round data
net.Start( "Clear Round State" )
net.Broadcast()
for _, v in pairs( player.GetAll() ) do
-- remove god mode from everyone
v:GodDisable()
-- freeze all the hunters
if( v:Team() == TEAM_HUNTERS ) then
v:Freeze( true )
end
end
end )
hook.Add( "OBJHUNT_RoundEnd", "Handle props winning", function()
print( "Props win" )
-- tell all the props that they won, good job props
SendRoundUpdate( function() return net.Broadcast() end )
for _, v in pairs( player.GetAll() ) do
v:PrintMessage( HUD_PRINTCENTER, round.winner.." Win!" )
-- give everyone god mode until round starts again
v:GodEnable()
end
end )
hook.Add( "OBJHUNT_RoundLimit", "Start map voting", function()
-- no longer need the round orchestrator
hook.Remove( "Tick", "Round orchestrator" )
MapVote.Start(30, false, 50, {"cs_", "ph_"})
print( "Map voting should start now" )
end )
hook.Add( "PlayerInitialSpawn", "Send Round data to client", function( ply )
-- sent to only the player, one time per join thing
SendRoundUpdate( function() return net.Send( ply ) end )
end)