/*
* Captain Pick Clock (olds.ro workshop idea IDEA-C851217B818E1B1E)
* 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/>.
*
* Every pick turn of a captain draft gets a countdown of olds_cappick_time seconds, shown each
* second to both captains and to the spectators (the pool). At olds_cappick_warn seconds left
* they hear blip2 once. When it runs out, the unpicked player who joined the server first
* (get_systime at client_putinserver) is picked for that captain through the core's native.
* Needs a draft core with the olds_captain_draft.inc API; without one the plugin stays idle.
*/
#include <amxmodx>
#include <reapi>
#include <olds_captain_draft>
#define PLUGIN "Captain Pick Clock"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define TASK_TICK 85100
new g_time, g_warn
new g_captain // whose turn it is, 0 = no clock
new g_team
new Float:g_end
new bool:g_warned
new g_joined[MAX_PLAYERS + 1] // get_systime at client_putinserver
new g_joinSeq[MAX_PLAYERS + 1] // order of joining, for players of the same second
new g_seq
new g_hud
new bool:g_noCore
public plugin_natives()
{
set_native_filter("native_filter")
}
public native_filter(const name[], index, trap)
{
if (!trap && (equal(name, "olds_cap_pick") || equal(name, "olds_cap_in_pool")))
{
g_noCore = true
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_cappick.txt")
bind_pcvar_num(create_cvar("olds_cappick_time", "20", FCVAR_NONE, "Seconds a captain has for each pick", true, 3.0, true, 120.0), g_time)
bind_pcvar_num(create_cvar("olds_cappick_warn", "5", FCVAR_NONE, "Beep at this many seconds left (0 = never)", true, 0.0, true, 60.0), g_warn)
AutoExecConfig(true, "olds_cappick")
register_concmd("amx_cappick_status", "cmd_status", ADMIN_RCON, "- shows the pick clock")
g_hud = CreateHudSyncObj()
if (g_noCore)
log_amx("No captain draft core (library olds_captain) is loaded: the pick clock stays idle.")
}
public client_putinserver(id)
{
g_joined[id] = get_systime()
g_joinSeq[id] = ++g_seq
}
public client_disconnected(id)
{
g_joined[id] = 0
g_joinSeq[id] = 0
if (id == g_captain)
stop_clock() // the core names the next captain with a new turn
}
/* ------------------------------------------------------------------ the core's forwards */
public olds_cap_pick_turn(captain, team)
{
if (g_noCore || !is_user_connected(captain))
return
g_captain = captain
g_team = team
g_end = get_gametime() + float(g_time)
g_warned = false
remove_task(TASK_TICK)
show_clock(g_time)
set_task(1.0, "tick", TASK_TICK, _, _, "b")
}
public olds_cap_picked(captain, player)
{
if (captain == g_captain)
stop_clock()
}
public olds_cap_draft_end()
{
stop_clock()
}
/* ------------------------------------------------------------------ the clock */
stop_clock()
{
remove_task(TASK_TICK)
if (!g_captain)
return
g_captain = 0
for (new id = 1; id <= MaxClients; id++)
if (is_user_connected(id))
ClearSyncHud(id, g_hud)
}
/* Captains and spectators see the clock. */
bool:watcher(id)
{
if (!is_user_connected(id) || is_user_bot(id))
return false
if (id == g_captain)
return true
new TeamName:team = TeamName:get_member(id, m_iTeam)
return team == TEAM_SPECTATOR || team == TEAM_UNASSIGNED || olds_cap_in_pool(id) != 0
}
show_clock(left)
{
new name[MAX_NAME_LENGTH]
get_user_name(g_captain, name, charsmax(name))
if (g_team == 1)
set_hudmessage(255, 90, 60, -1.0, 0.22, 0, 0.0, 1.1, 0.0, 0.1, -1)
else
set_hudmessage(90, 150, 255, -1.0, 0.22, 0, 0.0, 1.1, 0.0, 0.1, -1)
for (new id = 1; id <= MaxClients; id++)
{
if (!watcher(id))
continue
ShowSyncHudMsg(id, g_hud, "%L", id, "CAPPICK_CLOCK", name, left)
}
}
public tick()
{
if (!g_captain || !is_user_connected(g_captain))
{
stop_clock()
return
}
new left = floatround(g_end - get_gametime(), floatround_ceil)
if (left <= 0)
{
auto_pick()
return
}
show_clock(left)
if (!g_warned && g_warn > 0 && left <= g_warn)
{
g_warned = true
for (new id = 1; id <= MaxClients; id++)
if (watcher(id))
client_cmd(id, "spk buttons/blip2")
}
}
/* The unpicked player who has waited longest. */
longest_waiting()
{
new best, bestTime, bestSeq
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id) || !g_joinSeq[id] || !olds_cap_in_pool(id))
continue
if (!best || g_joined[id] < bestTime || (g_joined[id] == bestTime && g_joinSeq[id] < bestSeq))
{
best = id
bestTime = g_joined[id]
bestSeq = g_joinSeq[id]
}
}
return best
}
auto_pick()
{
new captain = g_captain
new player = longest_waiting()
stop_clock()
if (!player)
return
new cname[MAX_NAME_LENGTH], pname[MAX_NAME_LENGTH]
get_user_name(captain, cname, charsmax(cname))
get_user_name(player, pname, charsmax(pname))
/* The core fires olds_cap_picked and then the next turn, which starts a new clock. */
if (olds_cap_pick(captain, player))
client_print(0, print_chat, "%L", LANG_PLAYER, "CAPPICK_AUTO", cname, pname)
else
log_amx("The draft core refused the automatic pick of %s for %s.", pname, cname)
}
/* ------------------------------------------------------------------ admin */
public cmd_status(id, level, cid)
{
if (id && !(get_user_flags(id) & level))
{
console_print(id, "You have no access to that command.")
return PLUGIN_HANDLED
}
if (g_noCore)
{
console_print(id, "[PickClock] idle: no captain draft core is loaded")
return PLUGIN_HANDLED
}
if (!g_captain)
{
console_print(id, "[PickClock] no clock running")
return PLUGIN_HANDLED
}
new name[MAX_NAME_LENGTH], next = longest_waiting(), nextName[MAX_NAME_LENGTH]
get_user_name(g_captain, name, charsmax(name))
if (next)
get_user_name(next, nextName, charsmax(nextName))
console_print(id, "[PickClock] %s (team %d) has %d s left, beeped %d, auto-pick would take #%d %s",
name, g_team, floatround(g_end - get_gametime(), floatround_ceil), g_warned, next, nextName)
return PLUGIN_HANDLED
}