/*
* Warm-up Refrain (olds.ro workshop card AMX-081)
*
* Warm-up: players build a short call-and-response pattern together. Each
* player adds one beat per cycle from a menu (/refrain); when the pattern is
* full it is played back to everyone (text beats on a DHUD line plus a tone
* for each), and a new cycle starts. It all stops when live play begins.
*
* say /refrain open the beat menu (warm-up only)
*
* Beats and tones (stock Half-Life sounds, nothing to download):
* 1 Blip buttons/blip1.wav 2 Blop buttons/blip2.wav
* 3 Click buttons/button9.wav 4 Bell buttons/bell1.wav
*
* Warm-up phase: from map start until the first full restart (Game
* Commencing or sv_restart, which is how warm-up managers go live). A
* warm-up manager can also set the phase itself through the native
* olds_refrain_set_warmup(bool:warmup) library "olds_refrain"
* When live play starts: playback stops, the pattern and pending turns are
* dropped, and open beat menus close.
*
* Turns: one beat per player per cycle; a second beat from the same player is
* refused and the next player may go. A player who leaves keeps their beat in
* the current cycle, so it still finishes.
*
* Cvar: olds_refrain_len 4 (2-8). Command: olds_refrain_status (ADMIN_CFG).
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <amxmisc>
#include <reapi>
#define PLUGIN "Warm-up Refrain"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
const MAX_BEATS = 8
const BEAT_KINDS = 4
const TASK_PLAY = 8111
const Float:BEAT_GAP = 0.45
new const BEAT_SOUND[BEAT_KINDS][] = { "buttons/blip1.wav", "buttons/blip2.wav", "buttons/button9.wav", "buttons/bell1.wav" }
new const BEAT_KEY[BEAT_KINDS][] = { "REFRAIN_BEAT_1", "REFRAIN_BEAT_2", "REFRAIN_BEAT_3", "REFRAIN_BEAT_4" }
new g_pattern[MAX_BEATS] // beat kinds 0-3
new g_by[MAX_BEATS] // userid of the player who added each beat
new g_beats
new g_cycleLength // olds_refrain_len taken at the cycle's start
new bool:g_playing
new g_playStep
new bool:g_warmup = true
new g_menu[MAX_PLAYERS + 1] = { -1, ... }
new Float:g_nextUse[MAX_PLAYERS + 1]
new g_cycles
new g_length
public plugin_natives()
{
register_library("olds_refrain")
register_native("olds_refrain_set_warmup", "native_set_warmup")
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_refrain.txt")
bind_pcvar_num(create_cvar("olds_refrain_len", "4", FCVAR_NONE,
"Beats in one refrain cycle (2-8).", true, 2.0, true, float(MAX_BEATS)), g_length)
AutoExecConfig(true, "olds_refrain")
register_clcmd("say /refrain", "cmd_refrain")
register_clcmd("say_team /refrain", "cmd_refrain")
register_concmd("olds_refrain_status", "cmd_status", ADMIN_CFG, "- the refrain and the warm-up phase")
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
}
/* ---------- the phase ---------- */
public on_restart_round_pre()
{
if (g_warmup && get_member_game(m_bCompleteReset))
go_live()
return HC_CONTINUE
}
go_live()
{
g_warmup = false
stop_all()
}
stop_all()
{
remove_task(TASK_PLAY)
g_playing = false
g_beats = 0
g_cycleLength = 0
for (new id = 1; id <= MaxClients; id++)
close_menu(id)
}
public native_set_warmup(plugin, params)
{
new bool:warmup = get_param(1) != 0
if (warmup == g_warmup)
return 1
if (warmup)
g_warmup = true
else
go_live()
return 1
}
/* ---------- /refrain and the menu ---------- */
public cmd_refrain(id)
{
if (!is_user_connected(id))
return PLUGIN_HANDLED
new Float:now = get_gametime()
if (now < g_nextUse[id])
return PLUGIN_HANDLED
g_nextUse[id] = now + 1.0
if (!g_warmup)
{
client_print_color(id, print_team_default, "^4[Refrain]^1 %L", id, "REFRAIN_LIVE")
return PLUGIN_HANDLED
}
open_menu(id)
return PLUGIN_HANDLED
}
open_menu(id)
{
close_menu(id)
new title[96], pattern[96], item[32]
pattern_text(pattern, charsmax(pattern), id)
formatex(title, charsmax(title), "%L", id, "REFRAIN_MENU_TITLE", g_beats, cycle_length(), pattern)
new menu = menu_create(title, "menu_handler")
for (new k = 0; k < BEAT_KINDS; k++)
{
formatex(item, charsmax(item), "%L", id, BEAT_KEY[k])
menu_additem(menu, item)
}
g_menu[id] = menu
menu_display(id, menu)
}
close_menu(id)
{
new menu = g_menu[id]
if (menu < 0)
return
// Cancelling a shown menu runs its handler, which destroys it.
if (is_user_connected(id))
{
new oldMenu, newMenu
player_menu_info(id, oldMenu, newMenu)
if (newMenu == menu)
menu_cancel(id)
}
if (g_menu[id] == menu)
{
g_menu[id] = -1
menu_destroy(menu)
}
}
public menu_handler(id, menu, item)
{
if (g_menu[id] == menu)
g_menu[id] = -1
menu_destroy(menu)
if (item < 0 || item >= BEAT_KINDS || !is_user_connected(id))
return PLUGIN_HANDLED
add_beat(id, item)
return PLUGIN_HANDLED
}
cycle_length()
{
return g_cycleLength ? g_cycleLength : g_length
}
add_beat(id, kind)
{
if (!g_warmup)
{
client_print_color(id, print_team_default, "^4[Refrain]^1 %L", id, "REFRAIN_LIVE")
return
}
if (g_playing)
{
client_print_color(id, print_team_default, "^4[Refrain]^1 %L", id, "REFRAIN_BUSY")
return
}
new userid = get_user_userid(id)
for (new b = 0; b < g_beats; b++)
{
if (g_by[b] == userid)
{
client_print_color(id, print_team_default, "^4[Refrain]^1 %L", id, "REFRAIN_ONE_EACH")
return
}
}
if (!g_beats)
g_cycleLength = g_length
g_pattern[g_beats] = kind
g_by[g_beats] = userid
g_beats++
rg_send_audio(id, BEAT_SOUND[kind])
new name[MAX_NAME_LENGTH], beatName[16]
get_user_name(id, name, charsmax(name))
for (new p = 1; p <= MaxClients; p++)
{
if (!is_user_connected(p) || is_user_bot(p))
continue
formatex(beatName, charsmax(beatName), "%L", p, BEAT_KEY[kind])
client_print_color(p, print_team_default, "^4[Refrain]^1 %L", p, "REFRAIN_ADDED", name, beatName, g_beats,
g_cycleLength)
}
if (g_beats >= g_cycleLength)
start_playback()
}
/* ---------- playback ---------- */
start_playback()
{
g_playing = true
g_playStep = 0
set_task(0.6, "task_play", TASK_PLAY)
}
public task_play()
{
if (!g_warmup || !g_playing)
return
if (g_playStep >= g_beats)
{
g_playing = false
g_beats = 0
g_cycleLength = 0
g_cycles++
return
}
rg_send_audio(0, BEAT_SOUND[g_pattern[g_playStep]])
new text[96]
for (new p = 1; p <= MaxClients; p++)
{
if (!is_user_connected(p) || is_user_bot(p))
continue
pattern_text(text, charsmax(text), p, g_playStep)
set_dhudmessage(255, 220, 120, -1.0, 0.30, 0, 0.0, BEAT_GAP + 0.1, 0.0, 0.0)
show_dhudmessage(p, "%s", text)
}
g_playStep++
set_task(g_playStep >= g_beats ? 1.0 : BEAT_GAP, "task_play", TASK_PLAY)
}
/* The pattern as text; the beat being played (if any) is shown in brackets. */
pattern_text(output[], length, id, current = -1)
{
new written, beatName[16]
output[0] = EOS
for (new b = 0; b < g_beats; b++)
{
formatex(beatName, charsmax(beatName), "%L", id, BEAT_KEY[g_pattern[b]])
if (b == current)
written += formatex(output[written], length - written, "%s[%s]", b ? " - " : "", beatName)
else
written += formatex(output[written], length - written, "%s%s", b ? " - " : "", beatName)
}
if (!g_beats)
copy(output, length, "-")
}
public client_disconnected(id)
{
close_menu(id)
g_nextUse[id] = 0.0
}
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
new text[96]
pattern_text(text, charsmax(text), id)
console_print(id, "[Refrain] %s; %d of %d beats: %s%s; %d cycle(s) played", g_warmup ? "warm-up" : "live", g_beats,
cycle_length(), text, g_playing ? " (playing)" : "", g_cycles)
return PLUGIN_HANDLED
}