/*
* Captain Team Draft (olds.ro workshop card LIB-0054)
*
* An admin starts a draft: everyone goes to spectators, two captains are
* chosen (at random, by a vote, or named by the admin), and the captains
* take turns picking players from a menu. When everyone is picked the teams
* are set and the round restarts.
*
* amx_captain start: captains at random or by vote
* amx_captain <name> <name> start with these two captains
* amx_captain status | stop the draft so far / cancel it
* All need ADMIN_BAN. At least 4 players (bots count, HLTV does not).
*
* Captains: the first captain takes the terrorists, the second the
* counter-terrorists. With olds_cap_random 0 every player gets a vote menu
* for olds_cap_picktime seconds and the two most voted become captains
* (ties and no votes: random).
* Picks: olds_cap_pickorder 1 alternates (A B A B ...), 2 snakes
* (A B B A A B ...). A captain who does not choose within olds_cap_picktime
* seconds gets a random player. A captain who leaves is replaced by a
* player of their team (or, if none yet, of the pool), who continues.
* Players who join during the draft enter the pool. Team changes are
* blocked while it runs.
*
* Cvars: olds_cap_picktime 15 (5-60), olds_cap_random 1, olds_cap_pickorder 1 (1-2).
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <reapi>
#define PLUGIN "Captain Team Draft"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
const MIN_PLAYERS = 4
const TASK_TURN = 5411
const TASK_VOTE = 5412
enum
{
STATE_NONE = 0,
STATE_POOL,
STATE_TEAM_A, // terrorists
STATE_TEAM_B // counter-terrorists
}
enum
{
DRAFT_OFF = 0,
DRAFT_VOTE,
DRAFT_PICKING
}
new g_draft
new g_state[MAX_PLAYERS + 1]
new g_captain[2]
new g_pick // picks made so far
new g_turn // 0 = captain A, 1 = captain B
new g_votes[MAX_PLAYERS + 1] // captain votes received
new bool:g_voted[MAX_PLAYERS + 1]
new g_pickTime
new g_random
new g_order
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_captain.txt")
bind_pcvar_num(create_cvar("olds_cap_picktime", "15", FCVAR_NONE,
"Seconds a captain has for each pick, and the captain vote's length (5-60).", true, 5.0, true, 60.0), g_pickTime)
bind_pcvar_num(create_cvar("olds_cap_random", "1", FCVAR_NONE,
"1 = captains at random, 0 = players vote for them.", true, 0.0, true, 1.0), g_random)
bind_pcvar_num(create_cvar("olds_cap_pickorder", "1", FCVAR_NONE,
"1 = alternate picks (A B A B), 2 = snake (A B B A).", true, 1.0, true, 2.0), g_order)
AutoExecConfig(true, "olds_captain")
register_concmd("amx_captain", "cmd_captain", ADMIN_BAN, "[<captain> <captain> | status | stop] - team draft")
register_clcmd("chooseteam", "block_team")
register_clcmd("jointeam", "block_team")
RegisterHookChain(RG_HandleMenu_ChooseTeam, "on_choose_team_pre", false)
}
/* ---------- players ---------- */
bool:draftable(id)
{
return is_user_connected(id) && !is_user_hltv(id)
}
count_draftable()
{
new count
for (new id = 1; id <= MaxClients; id++)
if (draftable(id))
count++
return count
}
to_team(id, CsTeams:team)
{
if (is_user_alive(id))
user_kill(id, 1)
cs_set_user_team(id, team)
}
pool_size()
{
new count
for (new id = 1; id <= MaxClients; id++)
if (g_state[id] == STATE_POOL && is_user_connected(id))
count++
return count
}
/* A random player in this state, or 0. */
random_in(wanted)
{
new list[MAX_PLAYERS], count
for (new id = 1; id <= MaxClients; id++)
if (g_state[id] == wanted && is_user_connected(id))
list[count++] = id
return count ? list[random(count)] : 0
}
/* ---------- start ---------- */
public cmd_captain(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
new arg[32], arg2[32]
read_argv(1, arg, charsmax(arg))
read_argv(2, arg2, charsmax(arg2))
if (equali(arg, "status"))
{
print_status(id)
return PLUGIN_HANDLED
}
if (equali(arg, "stop"))
{
if (g_draft == DRAFT_OFF)
console_print(id, "[Captain] no draft is running")
else
{
end_draft()
announce("CAP_STOPPED")
console_print(id, "[Captain] draft cancelled; players stay where they are")
}
return PLUGIN_HANDLED
}
if (g_draft != DRAFT_OFF)
{
console_print(id, "[Captain] a draft is already running (amx_captain status, amx_captain stop)")
return PLUGIN_HANDLED
}
new players = count_draftable()
if (players < MIN_PLAYERS)
{
console_print(id, "[Captain] %d player(s): a draft needs at least %d", players, MIN_PLAYERS)
if (id)
say_to(id, "CAP_TOO_FEW", players, MIN_PLAYERS)
return PLUGIN_HANDLED
}
new a, b
if (arg[0])
{
a = cmd_target(id, arg, CMDTARGET_ALLOW_SELF)
b = arg2[0] ? cmd_target(id, arg2, CMDTARGET_ALLOW_SELF) : 0
if (!a || !b || a == b)
{
console_print(id, "[Captain] name two different players, or none for %s captains",
g_random ? "random" : "voted")
return PLUGIN_HANDLED
}
}
// Everyone to spectators, into the pool.
for (new p = 1; p <= MaxClients; p++)
{
g_state[p] = STATE_NONE
if (draftable(p))
{
g_state[p] = STATE_POOL
to_team(p, CS_TEAM_SPECTATOR)
}
}
g_pick = 0
g_captain[0] = 0
g_captain[1] = 0
console_print(id, "[Captain] draft started with %d players", players)
if (a)
start_picking(a, b)
else if (g_random)
start_picking(random_in(STATE_POOL), 0)
else
start_vote()
return PLUGIN_HANDLED
}
/* b = 0: a second random captain. */
start_picking(a, b)
{
set_captain(0, a)
set_captain(1, b ? b : random_in(STATE_POOL))
g_draft = DRAFT_PICKING
new nameA[MAX_NAME_LENGTH], nameB[MAX_NAME_LENGTH]
get_user_name(g_captain[0], nameA, charsmax(nameA))
get_user_name(g_captain[1], nameB, charsmax(nameB))
announce("CAP_CAPTAINS", nameA, nameB)
begin_turn()
}
set_captain(side, id)
{
g_captain[side] = id
g_state[id] = side ? STATE_TEAM_B : STATE_TEAM_A
to_team(id, side ? CS_TEAM_CT : CS_TEAM_T)
}
/* ---------- captain vote ---------- */
start_vote()
{
g_draft = DRAFT_VOTE
arrayset(g_votes, 0, sizeof(g_votes))
arrayset(g_voted, false, sizeof(g_voted))
for (new id = 1; id <= MaxClients; id++)
if (g_state[id] == STATE_POOL && is_user_connected(id) && !is_user_bot(id))
show_vote(id)
announce("CAP_VOTE", g_pickTime)
set_task(float(g_pickTime), "task_vote_end", TASK_VOTE)
}
show_vote(id)
{
new menu = menu_create(fmt("%L", id, "CAP_VOTE_TITLE"), "on_vote")
new name[MAX_NAME_LENGTH], info[8]
for (new p = 1; p <= MaxClients; p++)
{
if (g_state[p] != STATE_POOL || !is_user_connected(p))
continue
get_user_name(p, name, charsmax(name))
num_to_str(get_user_userid(p), info, charsmax(info))
menu_additem(menu, name, info)
}
menu_display(id, menu)
}
public on_vote(id, menu, item)
{
if (item < 0 || g_draft != DRAFT_VOTE || g_voted[id])
{
menu_destroy(menu)
return PLUGIN_HANDLED
}
new choice = item_player(menu, item)
menu_destroy(menu)
if (choice && g_state[choice] == STATE_POOL)
{
g_voted[id] = true
g_votes[choice]++
}
return PLUGIN_HANDLED
}
public task_vote_end()
{
if (g_draft != DRAFT_VOTE)
return
new first = top_voted(0)
new second = top_voted(first)
if (!first || !second)
{
abort_draft()
return
}
start_picking(first, second)
}
/* Most voted pool player other than `skip`; ties at random. */
top_voted(skip)
{
new best[MAX_PLAYERS], count, most = -1
for (new id = 1; id <= MaxClients; id++)
{
if (id == skip || g_state[id] != STATE_POOL || !is_user_connected(id))
continue
if (g_votes[id] > most)
{
most = g_votes[id]
count = 0
}
if (g_votes[id] == most)
best[count++] = id
}
return count ? best[random(count)] : 0
}
/* ---------- picking ---------- */
begin_turn()
{
if (g_draft != DRAFT_PICKING)
return
if (!pool_size())
{
finish_draft()
return
}
g_turn = (g_order == 2) ? (((g_pick + 1) / 2) % 2) : (g_pick % 2)
new captain = g_captain[g_turn]
remove_task(TASK_TURN)
set_task(float(g_pickTime), "task_turn_timeout", TASK_TURN)
if (!is_user_bot(captain))
show_picks(captain)
new name[MAX_NAME_LENGTH]
get_user_name(captain, name, charsmax(name))
announce("CAP_TURN", name, g_pickTime)
}
show_picks(id)
{
new menu = menu_create(fmt("%L", id, "CAP_PICK_TITLE", g_pickTime), "on_pick")
new name[MAX_NAME_LENGTH], info[8]
for (new p = 1; p <= MaxClients; p++)
{
if (g_state[p] != STATE_POOL || !is_user_connected(p))
continue
get_user_name(p, name, charsmax(name))
num_to_str(get_user_userid(p), info, charsmax(info))
menu_additem(menu, name, info)
}
menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
menu_display(id, menu)
}
public on_pick(id, menu, item)
{
if (item < 0 || g_draft != DRAFT_PICKING || id != g_captain[g_turn])
{
menu_destroy(menu)
return PLUGIN_HANDLED
}
new choice = item_player(menu, item)
menu_destroy(menu)
if (!choice || g_state[choice] != STATE_POOL)
{
// Gone meanwhile: choose again.
show_picks(id)
return PLUGIN_HANDLED
}
make_pick(choice, false)
return PLUGIN_HANDLED
}
public task_turn_timeout()
{
if (g_draft != DRAFT_PICKING)
return
new choice = random_in(STATE_POOL)
if (choice)
make_pick(choice, true)
else
finish_draft()
}
make_pick(choice, bool:timedOut)
{
remove_task(TASK_TURN)
new captain = g_captain[g_turn]
g_state[choice] = g_turn ? STATE_TEAM_B : STATE_TEAM_A
to_team(choice, g_turn ? CS_TEAM_CT : CS_TEAM_T)
g_pick++
// A captain who sees a menu has it closed by the next one, or here.
if (is_user_connected(captain) && !is_user_bot(captain))
show_menu(captain, 0, "^n", 1)
new captainName[MAX_NAME_LENGTH], name[MAX_NAME_LENGTH]
get_user_name(captain, captainName, charsmax(captainName))
get_user_name(choice, name, charsmax(name))
announce(timedOut ? "CAP_PICKED_RANDOM" : "CAP_PICKED", captainName, name)
begin_turn()
}
finish_draft()
{
end_draft()
announce("CAP_DONE")
server_cmd("sv_restart 1")
}
abort_draft()
{
end_draft()
announce("CAP_ABORTED")
}
end_draft()
{
g_draft = DRAFT_OFF
remove_task(TASK_TURN)
remove_task(TASK_VOTE)
for (new id = 1; id <= MaxClients; id++)
g_state[id] = STATE_NONE
g_captain[0] = 0
g_captain[1] = 0
}
/* The player stored in a menu item (by userid), or 0 if they left. */
item_player(menu, item)
{
new info[8], access, callback
menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
return find_player_ex(FindPlayer_MatchUserId, str_to_num(info))
}
/* ---------- joining and leaving ---------- */
public client_putinserver(id)
{
g_state[id] = STATE_NONE
if (g_draft != DRAFT_OFF && draftable(id))
g_state[id] = STATE_POOL
}
public client_disconnected(id)
{
new side = (g_draft != DRAFT_OFF && id == g_captain[0]) ? 0 : ((g_draft != DRAFT_OFF && id == g_captain[1]) ? 1 : -1)
g_state[id] = STATE_NONE
g_votes[id] = 0
g_voted[id] = false
if (side < 0)
return
// A new captain from their team, else from the pool.
new next = random_in(side ? STATE_TEAM_B : STATE_TEAM_A)
if (!next)
next = random_in(STATE_POOL)
if (!next)
{
abort_draft()
return
}
set_captain(side, next)
new name[MAX_NAME_LENGTH]
get_user_name(next, name, charsmax(name))
announce("CAP_NEW_CAPTAIN", name)
if (g_draft == DRAFT_PICKING && g_turn == side)
begin_turn()
}
public block_team(id)
{
if (g_draft == DRAFT_OFF)
return PLUGIN_CONTINUE
say_to(id, "CAP_LOCKED")
return PLUGIN_HANDLED
}
public on_choose_team_pre(id, MenuChooseTeam:slot)
{
if (g_draft == DRAFT_OFF)
return HC_CONTINUE
say_to(id, "CAP_LOCKED")
SetHookChainReturn(ATYPE_INTEGER, 0)
return HC_SUPERCEDE
}
/* ---------- messages ---------- */
announce(const key[], any:...)
{
new pattern[160], message[192]
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id) || is_user_bot(id))
continue
formatex(pattern, charsmax(pattern), "%L", id, key)
vformat(message, charsmax(message), pattern, 2)
client_print_color(id, print_team_default, "^4[Captain]^1 %s", message)
}
formatex(pattern, charsmax(pattern), "%L", LANG_SERVER, key)
vformat(message, charsmax(message), pattern, 2)
server_print("[Captain] %s", message)
}
say_to(id, const key[], any:...)
{
new pattern[160], message[192]
formatex(pattern, charsmax(pattern), "%L", id, key)
vformat(message, charsmax(message), pattern, 3)
client_print_color(id, print_team_default, "^4[Captain]^1 %s", message)
}
print_status(id)
{
new const names[][] = { "off", "captain vote", "picking" }
console_print(id, "[Captain] draft %s; pick %d; %d in the pool; pick time %d s, order %s, captains %s",
names[g_draft], g_pick + 1, pool_size(), g_pickTime, g_order == 2 ? "snake" : "alternate",
g_random ? "random" : "voted")
new name[MAX_NAME_LENGTH]
for (new p = 1; p <= MaxClients; p++)
{
if (!is_user_connected(p))
continue
get_user_name(p, name, charsmax(name))
new team[16]
switch (cs_get_user_team(p))
{
case CS_TEAM_T: copy(team, charsmax(team), "T")
case CS_TEAM_CT: copy(team, charsmax(team), "CT")
case CS_TEAM_SPECTATOR: copy(team, charsmax(team), "spectator")
default: copy(team, charsmax(team), "none")
}
console_print(id, "[Captain] %s: %s%s", name, team, (p == g_captain[0] || p == g_captain[1]) && g_draft ? " (captain)" : "")
}
}