/*
* Advanced Map Vote and Rotation (olds.ro workshop card LIB-0977)
* Copyright (C) 2026 daemon (olds.ro)
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details: <https://www.gnu.org/licenses/>.
*
* End-of-map vote (2 minutes before the time limit), rock the vote (say rtv), nominations
* (say nominate <map>), weighted random picks (configs/mapweights.ini) and say /nextmap.
* The vote menu (menu_create) holds "Extend" first when the map may still be extended, then
* the nominated maps, then weighted picks, olds_mv_maxmaps maps in all. A tie is settled by a
* runoff between the tied maps (olds_mv_tie 1) or at random (0). The winner of an rtv vote is
* played 5 seconds later; the winner of the timed vote at the end of the map.
*/
#include <amxmodx>
#include <amxmisc>
#include <reapi>
#define PLUGIN "Advanced Map Vote"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define MAX_POOL 128
#define MAX_ITEMS 9
#define MAP_LEN 32
#define TASK_CHECK 97700
#define TASK_END 97701
#define TASK_CHANGE 97702
#define RECENT_MAX 5
#define EXTEND_ITEM -1
new Float:g_voteTime, Float:g_rtvRatio, g_maxMaps, g_minPlayed, g_extend, g_tie, g_recentCount
new g_pool[MAX_POOL][MAP_LEN], g_weight[MAX_POOL], g_poolSize
new g_current[MAP_LEN]
new g_recent[RECENT_MAX][MAP_LEN], g_recentHave
new g_nominated[MAX_ITEMS][MAP_LEN], g_nominator[MAX_ITEMS], g_nominations
new bool:g_rtv[MAX_PLAYERS + 1], g_rtvCount
new bool:g_rtvDone
new bool:g_voting, bool:g_isRtv, bool:g_runoff
new g_items, g_item[MAX_ITEMS] // pool index per item, EXTEND_ITEM for extend
new g_votes[MAX_ITEMS]
new bool:g_voted[MAX_PLAYERS + 1]
new g_menu = -1
new bool:g_timedDone, bool:g_extended
new g_next[MAP_LEN]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_mapvote.txt")
bind_pcvar_float(create_cvar("olds_mv_votetime", "20", FCVAR_NONE, "Seconds a vote lasts", true, 5.0, true, 120.0), g_voteTime)
bind_pcvar_float(create_cvar("olds_mv_rtvratio", "0.6", FCVAR_NONE, "Share of players needed to rock the vote", true, 0.05, true, 1.0), g_rtvRatio)
bind_pcvar_num(create_cvar("olds_mv_maxmaps", "7", FCVAR_NONE, "Maps in a vote", true, 2.0, true, float(MAX_ITEMS - 1)), g_maxMaps)
bind_pcvar_num(create_cvar("olds_mv_minplayed", "10", FCVAR_NONE, "Minutes into a map before rtv is allowed", true, 0.0, true, 120.0), g_minPlayed)
bind_pcvar_num(create_cvar("olds_mv_extend", "15", FCVAR_NONE, "Minutes added when extend wins (0 = no extend)", true, 0.0, true, 120.0), g_extend)
bind_pcvar_num(create_cvar("olds_mv_tie", "1", FCVAR_NONE, "Tie: 1 = runoff vote, 0 = random of the tied", true, 0.0, true, 1.0), g_tie)
bind_pcvar_num(create_cvar("olds_mv_recent", "3", FCVAR_NONE, "Last maps played that are not offered", true, 0.0, true, float(RECENT_MAX)), g_recentCount)
AutoExecConfig(true, "olds_mapvote")
register_clcmd("say", "on_say")
register_clcmd("say_team", "on_say")
register_concmd("amx_mapvote_status", "cmd_status", ADMIN_MAP, "- shows the map vote state")
register_concmd("amx_mapvote_start", "cmd_start", ADMIN_MAP, "- starts the end-of-map vote now")
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_post", true)
RegisterHookChain(RG_CSGameRules_GoToIntermission, "on_intermission_post", true)
get_mapname(g_current, charsmax(g_current))
}
public plugin_cfg()
{
load_recent()
load_pool()
set_task(1.0, "check_time", TASK_CHECK, _, _, "b")
}
/* ------------------------------------------------------------------ the map pool */
pool_index(const map[])
{
for (new i = 0; i < g_poolSize; i++)
if (equali(g_pool[i], map))
return i
return -1
}
add_to_pool(const map[], weight)
{
if (!map[0] || !is_map_valid(map))
return -1
new i = pool_index(map)
if (i < 0)
{
if (g_poolSize >= MAX_POOL)
return -1
i = g_poolSize++
copy(g_pool[i], MAP_LEN - 1, map)
}
g_weight[i] = weight
return i
}
load_pool()
{
/* The map cycle: weight 1 each. */
new cycle[128], line[96], map[MAP_LEN]
get_cvar_string("mapcyclefile", cycle, charsmax(cycle))
new file = fopen(cycle, "rt")
if (file)
{
while (!feof(file))
{
fgets(file, line, charsmax(line))
trim(line)
if (!line[0] || line[0] == ';' || line[0] == '/')
continue
parse(line, map, charsmax(map))
add_to_pool(map, 1)
}
fclose(file)
}
/* configs/mapweights.ini: "<map> <weight>", which adds maps and sets their weight. */
new path[160], weight[8]
get_configsdir(path, charsmax(path))
add(path, charsmax(path), "/mapweights.ini")
file = fopen(path, "rt")
if (file)
{
while (!feof(file))
{
fgets(file, line, charsmax(line))
trim(line)
if (!line[0] || line[0] == ';' || line[0] == '/')
continue
if (parse(line, map, charsmax(map), weight, charsmax(weight)) < 2)
continue
if (add_to_pool(map, clamp(str_to_num(weight), 0, 100)) < 0)
log_amx("mapweights.ini: %s is not a valid map, skipped", map)
}
fclose(file)
}
if (g_poolSize < 2)
log_amx("Only %d valid map(s) in the map cycle and mapweights.ini: votes will be short", g_poolSize)
}
/* Recent maps live in data/olds_mapvote_recent.txt, newest first. */
recent_path(path[], len)
{
get_datadir(path, len)
add(path, len, "/olds_mapvote_recent.txt")
}
load_recent()
{
new path[160], line[MAP_LEN]
recent_path(path, charsmax(path))
new file = fopen(path, "rt")
if (file)
{
while (!feof(file) && g_recentHave < RECENT_MAX)
{
fgets(file, line, charsmax(line))
trim(line)
if (line[0] && !equali(line, g_current))
copy(g_recent[g_recentHave++], MAP_LEN - 1, line)
}
fclose(file)
}
/* This map goes to the front for the next maps. */
file = fopen(path, "wt")
if (!file)
return
fprintf(file, "%s^n", g_current)
for (new i = 0; i < g_recentHave && i < RECENT_MAX - 1; i++)
fprintf(file, "%s^n", g_recent[i])
fclose(file)
}
bool:is_recent(const map[])
{
for (new i = 0; i < g_recentHave && i < g_recentCount; i++)
if (equali(g_recent[i], map))
return true
return false
}
bool:is_nominated(const map[])
{
for (new i = 0; i < g_nominations; i++)
if (equali(g_nominated[i], map))
return true
return false
}
/* ------------------------------------------------------------------ chat commands */
public on_say(id)
{
new text[96]
read_args(text, charsmax(text))
remove_quotes(text)
trim(text)
if (equali(text, "rtv") || equali(text, "rockthevote") || equali(text, "/rtv"))
{
rock_the_vote(id)
return PLUGIN_HANDLED
}
if (equali(text, "/nextmap") || equali(text, "nextmap"))
{
if (g_next[0])
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_NEXTMAP", g_next)
else
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_NEXTMAP_VOTE")
return PLUGIN_CONTINUE
}
if (equali(text, "nominate ", 9) || equali(text, "/nominate ", 10))
{
new map[MAP_LEN], cmd[16]
parse(text, cmd, charsmax(cmd), map, charsmax(map))
nominate(id, map)
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
nominate(id, const map[])
{
new i = pool_index(map)
if (i < 0 && is_map_valid(map))
i = add_to_pool(map, 0)
if (i < 0)
{
client_print(id, print_chat, "%L", id, "MV_NOM_UNKNOWN", map)
return
}
if (g_voting || g_next[0])
{
client_print(id, print_chat, "%L", id, "MV_NOM_CLOSED")
return
}
if (equali(g_pool[i], g_current) || is_recent(g_pool[i]))
{
client_print(id, print_chat, "%L", id, "MV_NOM_RECENT", g_pool[i])
return
}
if (is_nominated(g_pool[i]))
{
client_print(id, print_chat, "%L", id, "MV_NOM_TWICE", g_pool[i])
return
}
/* One nomination each: a new one replaces the player's old one. */
new slot = -1
for (new k = 0; k < g_nominations; k++)
if (g_nominator[k] == id)
slot = k
if (slot < 0)
{
if (g_nominations >= g_maxMaps - 1)
{
client_print(id, print_chat, "%L", id, "MV_NOM_FULL")
return
}
slot = g_nominations++
}
copy(g_nominated[slot], MAP_LEN - 1, g_pool[i])
g_nominator[slot] = id
new name[MAX_NAME_LENGTH]
get_user_name(id, name, charsmax(name))
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_NOMINATED", name, g_pool[i])
}
public client_disconnected(id)
{
if (g_rtv[id])
{
g_rtv[id] = false
g_rtvCount--
}
g_voted[id] = false
for (new k = 0; k < g_nominations; k++)
{
if (g_nominator[k] != id)
continue
for (new m = k; m < g_nominations - 1; m++)
{
copy(g_nominated[m], MAP_LEN - 1, g_nominated[m + 1])
g_nominator[m] = g_nominator[m + 1]
}
g_nominations--
break
}
}
players_now()
{
new n
for (new id = 1; id <= MaxClients; id++)
if (is_user_connected(id) && !is_user_hltv(id))
n++
return n
}
rtv_needed()
{
return max(1, floatround(float(players_now()) * g_rtvRatio, floatround_ceil))
}
rock_the_vote(id)
{
if (g_rtvDone || g_voting || g_timedDone)
{
client_print(id, print_chat, "%L", id, "MV_RTV_DONE")
return
}
new Float:played = get_gametime() / 60.0
if (played < float(g_minPlayed))
{
client_print(id, print_chat, "%L", id, "MV_RTV_WAIT", floatround(float(g_minPlayed) - played, floatround_ceil))
return
}
if (g_rtv[id])
{
client_print(id, print_chat, "%L", id, "MV_RTV_ALREADY", g_rtvCount, rtv_needed())
return
}
g_rtv[id] = true
g_rtvCount++
new name[MAX_NAME_LENGTH]
get_user_name(id, name, charsmax(name))
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_RTV", name, g_rtvCount, rtv_needed())
if (g_rtvCount >= rtv_needed())
{
g_rtvDone = true
start_vote(true)
}
}
/* ------------------------------------------------------------------ when to vote */
public on_restart_post()
{
check_time()
}
public check_time()
{
if (g_voting || g_timedDone || g_next[0] || get_cvar_float("mp_timelimit") <= 0.0)
return
if (get_timeleft() <= 120)
start_vote(false)
}
public cmd_start(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
if (g_voting)
console_print(id, "[MapVote] A vote is already running.")
else
start_vote(false)
return PLUGIN_HANDLED
}
/* ------------------------------------------------------------------ the vote */
vote_kind(kind[], len)
{
if (g_isRtv)
copy(kind, len, "rtv")
else
copy(kind, len, "timed")
}
bool:item_has(index)
{
for (new k = 0; k < g_items; k++)
if (g_item[k] == index)
return true
return false
}
/* A weighted random pick among the maps not yet in the vote, or -1. */
weighted_pick()
{
new total
for (new i = 0; i < g_poolSize; i++)
if (g_weight[i] > 0 && !equali(g_pool[i], g_current) && !is_recent(g_pool[i]) && !item_has(i))
total += g_weight[i]
if (total <= 0)
return -1
new roll = random(total)
for (new i = 0; i < g_poolSize; i++)
{
if (g_weight[i] <= 0 || equali(g_pool[i], g_current) || is_recent(g_pool[i]) || item_has(i))
continue
roll -= g_weight[i]
if (roll < 0)
return i
}
return -1
}
start_vote(bool:rtv)
{
g_isRtv = rtv
g_runoff = false
g_items = 0
if (!rtv && !g_extended && g_extend > 0 && get_cvar_float("mp_timelimit") > 0.0)
g_item[g_items++] = EXTEND_ITEM
new maps
for (new k = 0; k < g_nominations && maps < g_maxMaps; k++)
{
new i = pool_index(g_nominated[k])
if (i >= 0 && !item_has(i))
{
g_item[g_items++] = i
maps++
}
}
while (maps < g_maxMaps && g_items < MAX_ITEMS)
{
new i = weighted_pick()
if (i < 0)
break
g_item[g_items++] = i
maps++
}
if (!maps)
{
log_amx("No map to vote for: the pool is empty")
if (!rtv)
g_timedDone = true
return
}
open_vote()
}
open_vote()
{
g_voting = true
arrayset(g_votes, 0, sizeof g_votes)
for (new id = 1; id <= MaxClients; id++)
g_voted[id] = false
client_cmd(0, "spk buttons/blip1")
new text[64]
if (g_runoff)
formatex(text, charsmax(text), "%L", LANG_SERVER, "MV_RUNOFF_TITLE")
else
formatex(text, charsmax(text), "%L", LANG_SERVER, "MV_TITLE")
if (g_menu >= 0)
menu_destroy(g_menu)
g_menu = menu_create(text, "on_vote")
for (new k = 0; k < g_items; k++)
{
if (g_item[k] == EXTEND_ITEM)
formatex(text, charsmax(text), "%L", LANG_SERVER, "MV_EXTEND", g_current, g_extend)
else
copy(text, charsmax(text), g_pool[g_item[k]])
menu_additem(g_menu, text)
}
menu_setprop(g_menu, MPROP_EXIT, MEXIT_NEVER)
menu_setprop(g_menu, MPROP_PERPAGE, 0)
new time = floatround(g_voteTime)
for (new id = 1; id <= MaxClients; id++)
if (is_user_connected(id) && !is_user_hltv(id))
menu_display(id, g_menu, 0, time)
new kind[8]
vote_kind(kind, charsmax(kind))
log_amx("Map vote started (%s, %d choices)", kind, g_items)
remove_task(TASK_END)
set_task(g_voteTime + 0.5, "end_vote", TASK_END)
}
public on_vote(id, menu, item)
{
if (!g_voting || menu != g_menu || item < 0 || item >= g_items || g_voted[id])
return PLUGIN_HANDLED
g_voted[id] = true
g_votes[item]++
new name[MAX_NAME_LENGTH], text[64]
get_user_name(id, name, charsmax(name))
item_label(item, text, charsmax(text))
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_VOTED", name, text)
return PLUGIN_HANDLED
}
item_label(item, text[], len)
{
if (g_item[item] == EXTEND_ITEM)
formatex(text, len, "%L", LANG_SERVER, "MV_EXTEND", g_current, g_extend)
else
copy(text, len, g_pool[g_item[item]])
}
public end_vote()
{
if (!g_voting)
return
g_voting = false
/* Close the menu for those who did not vote. */
for (new id = 1; id <= MaxClients; id++)
{
new oldMenu, newMenu
if (is_user_connected(id) && player_menu_info(id, oldMenu, newMenu) && newMenu == g_menu)
show_menu(id, 0, "^n", 1)
}
new best = -1, tied[MAX_ITEMS], ties
for (new k = 0; k < g_items; k++)
{
if (best < 0 || g_votes[k] > best)
{
best = g_votes[k]
ties = 0
}
if (g_votes[k] == best)
tied[ties++] = k
}
if (best <= 0)
{
/* Nobody voted: a weighted map wins (never extend). */
new pick = -1
for (new k = 0; k < g_items; k++)
if (g_item[k] != EXTEND_ITEM)
{
pick = k
break
}
if (pick < 0)
return
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_NO_VOTES")
winner(pick)
return
}
if (ties > 1)
{
if (g_tie && !g_runoff)
{
/* Runoff between the tied choices. */
new keep[MAX_ITEMS]
for (new k = 0; k < ties; k++)
keep[k] = g_item[tied[k]]
g_items = ties
for (new k = 0; k < ties; k++)
g_item[k] = keep[k]
g_runoff = true
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_TIE_RUNOFF", ties)
log_amx("Map vote tie between %d choices: runoff", ties)
open_vote()
return
}
new pick = tied[random(ties)]
new text[64]
item_label(pick, text, charsmax(text))
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_TIE_RANDOM", text)
log_amx("Map vote tie between %d choices: %s drawn at random", ties, text)
winner(pick)
return
}
winner(tied[0])
}
winner(item)
{
if (!g_isRtv)
g_timedDone = true
if (g_item[item] == EXTEND_ITEM)
{
g_extended = true
g_timedDone = false /* a vote comes again near the new end, without extend */
new Float:limit = get_cvar_float("mp_timelimit") + float(g_extend)
set_cvar_float("mp_timelimit", limit)
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_EXTENDED", g_current, g_extend)
log_amx("Map vote: %s extended by %d minutes (time limit %.1f)", g_current, g_extend, limit)
return
}
copy(g_next, charsmax(g_next), g_pool[g_item[item]])
if (cvar_exists("amx_nextmap"))
set_cvar_string("amx_nextmap", g_next)
new kind[8]
vote_kind(kind, charsmax(kind))
log_amx("Map vote: %s wins (%s)", g_next, kind)
if (g_isRtv)
{
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_WIN_NOW", g_next)
set_task(5.0, "change_now", TASK_CHANGE)
}
else
client_print(0, print_chat, "%L", LANG_PLAYER, "MV_WIN_END", g_next)
}
/* The time limit is over: change to the winner before the game picks its own next map. */
public on_intermission_post()
{
if (!g_next[0])
return
new Float:chat = floatmax(get_cvar_float("mp_chattime") - 1.0, 0.5)
remove_task(TASK_CHANGE)
set_task(chat, "change_now", TASK_CHANGE)
}
public change_now()
{
if (g_next[0] && is_map_valid(g_next))
server_cmd("changelevel %s", g_next)
}
/* ------------------------------------------------------------------ admin */
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
console_print(id, "[MapVote] map %s, pool %d, time left %d s, rtv %d/%d (done %d), timed vote done %d, extended %d, next %s",
g_current, g_poolSize, get_timeleft(), g_rtvCount, rtv_needed(), g_rtvDone, g_timedDone, g_extended, g_next)
new list[256]
for (new k = 0; k < g_nominations; k++)
{
add(list, charsmax(list), " ")
add(list, charsmax(list), g_nominated[k])
}
console_print(id, "[MapVote] nominated:%s", list)
if (g_voting)
{
new text[64]
new kind[8]
vote_kind(kind, charsmax(kind))
if (g_runoff)
console_print(id, "[MapVote] voting (%s, runoff):", kind)
else
console_print(id, "[MapVote] voting (%s):", kind)
for (new k = 0; k < g_items; k++)
{
item_label(k, text, charsmax(text))
console_print(id, "[MapVote] %d. %s - %d vote(s)", k + 1, text, g_votes[k])
}
}
return PLUGIN_HANDLED
}