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
-------------------------------------------------------------------------------
-- Module API
-------------------------------------------------------------------------------
function hc.maps.init()
if #hc.MAP_LIST < 2 then
print("No maps to choose between - map voting disabled.")
else
hc.add_menu_command("Change Map", hc.maps.change_map_command, hc.CHANGE_MAP_LEVEL, hc.ADMIN_MENU_KEY)
addhook("serveraction", "hc.maps.serveraction_hook")
addhook("post_join", "hc.maps.post_join_hook")
addhook("init_player", "hc.maps.init_player_hook")
addhook("delete_player", "hc.maps.delete_player_hook")
addhook("startround", "hc.maps.startround_hook")
addhook("endround", "hc.maps.endround_hook")
addhook("init", "hc.maps.init_hook")
-- addhook("remove",	 "hc.maps.remove_hook")
hc.maps.rounds_left = hc.MAP_ROUNDS
hc.maps.set_map_round_timer()
hc.maps.set_game_mode()
end
end
-------------------------------------------------------------------------------
-- Hooks
-------------------------------------------------------------------------------
function hc.maps.post_join_hook(p)
-- hc.maps.display_map_votes_player(p)
hc.maps.display_map_votes_all()
end
function hc.maps.delete_player_hook(p, reason)
hc.maps.display_map_votes()
end
function hc.maps.init_player_hook(p, reason)
hc.players[p].map_vote = {}
end
function hc.maps.startround_hook(mode)
local set_timer = hc.MAP_ROUND_LIMIT ~= nil and hc.MAP_ROUND_LIMIT > 0 and hc.maps.rounds_left > 1
if hc.maps.rounds_left <= 0 then
local map = hc.maps.next_map
if map ~= nil and map ~= game("sv_map") then
parse("sv_map " .. hc.maps.next_map)
else
-- Same map
--set_timer = false
hc.maps.new_round()
end
end
hc.maps.display_map_votes_all()
if set_timer then
hc.maps.set_map_round_timer()
end
end
function hc.maps.endround_hook(mode)
if hc.maps.map_round_timer ~= nil then
hc.maps.map_round_timer = nil
freetimer("hc.maps.map_round_timeout")
end
hc.maps.rounds_left = hc.maps.rounds_left - 1
if hc.maps.rounds_left <= 0 then
hc.maps.next_map = hc.maps.get_next_map()
timer(1000, "hc.maps.show_next_map")
end
end
function hc.maps.serveraction_hook(p, action)
if action == hc.MAP_VOTE_MENU_KEY then
hc.maps.display_menu(p)
return 1
end
end
function hc.maps.init_hook()
hc.maps.display_map_votes_all()
end
-------------------------------------------------------------------------------
-- Menu callbacks
-------------------------------------------------------------------------------
function hc.maps.change_map_cb(p, id)
local map = hc.maps.get_map_name(id)
if map ~= game("sv_map") then
hc.exec(p, "sv_map " .. map)
else
if hc.maps.rounds_left == 1 then
hc.maps.rounds_left = 2
end
hc.exec(p, "restart")
end
end
-------------------------------------------------------------------------------
-- Menu commands
-------------------------------------------------------------------------------
function hc.maps.change_map_command(p, id)
local maps = hc.maps.get_map_menu(true)
hc.show_menu(p, "Change Map", maps, hc.maps.change_map_cb)
end
-------------------------------------------------------------------------------
-- Internal functions
-------------------------------------------------------------------------------
function hc.maps.set_game_mode()
local map = game("sv_map")
for id = 1, #hc.MAP_LIST do
if map == hc.maps.get_map_name(id) then
local mode = hc.maps.get_map_game_mode(id)
if mode ~= nil then
parse("sv_gamemode " .. mode)
end
break
end
end
end
function hc.maps.get_map_name(n)
if type(hc.MAP_LIST[n]) == "table" then
return hc.MAP_LIST[n].name
else
return hc.MAP_LIST[n]
end
end
function hc.maps.get_map_game_mode(n)
if type(hc.MAP_LIST[n]) == "table" then
return hc.MAP_LIST[n].mode
else
return nil
end
end
function hc.maps.new_round()
-- Reset the map votes
for i=1,hc.SLOTS do
if hc.player_exists(i) then
hc.players[i].map_vote.vote = nil
hc.players[i].map_vote.time = nil
end
end
hc.maps.rounds_left = hc.MAP_ROUNDS + 1 -- endround_hook will decrease by one
hc.next_map = nil
parse("restart")
end
function hc.maps.display_map_votes_all()
parse("hudtxt 48 \"Next map [in " .. hc.get_agreed_string(hc.maps.rounds_left, "round") .. "]:\" 4 423 0")
hc.maps.display_map_votes()
end
function hc.maps.show_next_map()
hc.event("Next Map: " .. hc.maps.next_map .. "@C")
end
function hc.maps.set_map_round_timer()
if hc.maps.map_round_timer ~= nil then
print("Error: Map round timer already set!")
else
timer(hc.MAP_ROUND_LIMIT * 60 * 1000, "hc.maps.map_round_timeout", nil, hc.maps.rounds_left - 1)
hc.maps.map_round_timer = true
end
end
function hc.maps.map_round_timeout()
hc.maps.rounds_left = hc.maps.rounds_left - 1
hc.maps.display_map_votes_all()
hc.event("Map round limit reached.")
if hc.maps.rounds_left <= 1 then
hc.maps.map_round_timer = nil
end
end
function hc.maps.get_map_votes()
local votes = {}
local max_votes = 0
for p=1,hc.SLOTS do
if hc.player_exists(p) then
local id = hc.players[p].map_vote.vote
local time = hc.players[p].map_vote.time
if id ~= nil then
if votes[id] == nil then
votes[id] = { count = 1, last_time = hc.players[p].map_vote.time }
else
votes[id] = {
count = votes[id].count + 1,
last_time = math.max(votes[id].last_time, time)
}
end
if votes[id].count > max_votes then
max_votes = votes[id].count
end
end
end
end
return votes, max_votes
end
function hc.maps.get_next_map()
local votes, max_votes = hc.maps.get_map_votes()
-- If several maps get the same number of votes, the map that first reached
-- that number of votes wins.
local map
local vote_time = 2 ^ 32
for id,v in pairs(votes) do
if v.count == max_votes and v.last_time < vote_time then
map = id
vote_time = v.last_time
end
end
if map == nil then
return game("sv_map"), 0
else
return hc.maps.get_map_name(map), max_votes
end
end
function hc.maps.display_map_votes()
local map, votes = hc.maps.get_next_map()
parse("hudtxt 47 \"" .. map .. " [" .. hc.get_agreed_string(votes, "vote") .. "]\" 164 423 0")
end
function hc.maps.display_menu(p)
local maps = hc.maps.get_map_menu(true)
hc.show_menu(p, "Map Vote", maps,
function(p, id)
local now = os.time()
if hc.players[p].map_vote.time ~= nil and
(now - hc.players[p].map_vote.time) < hc.MAP_VOTE_COOLDOWN_TIME then
hc.event(p, "You must wait " .. hc.MAP_VOTE_COOLDOWN_TIME .. " seconds before voting again.")
else
hc.players[p].map_vote.vote = id
hc.players[p].map_vote.time = os.time()
hc.maps.display_map_votes()
hc.event(player(p, "name") .. " votes for " .. hc.maps.get_map_name(id) .. ".")
end
end)
end
function hc.maps.get_map_menu()
local votes, max_votes = hc.maps.get_map_votes()
local maps = {}
local current_map = game("sv_map")
for id = 1, #hc.MAP_LIST do
local map = hc.maps.get_map_name(id)
if map == current_map then
maps[id] = map .. " [current]"
else
maps[id] = map
end
if votes[id] then
maps[id] = maps[id] .. "|" .. votes[id].count
end
end
return maps
end