/*
* Captain Knife Duel (olds.ro workshop idea IDEA-03C1F3999BB7F7B5)
* 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/>.
*
* Before a captain draft the two captains fight a knife duel; everyone else watches from the
* spectators. The winner (a kill, or more health when olds_capduel_time runs out; equal health:
* random) chooses the first pick or the side; the loser gets the other. Then the draft goes on.
*/
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>
#include <olds_captain_duel>
#define PLUGIN "Captain Knife Duel"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define TASK_DUEL 86100
#define TASK_MENU 86200
#define MENU_TIME 15
new g_enabled, g_duelTime
new bool:g_noCore
new bool:g_dueling
new g_cap[2] // captain A (terrorists), captain B (counter-terrorists)
new g_winner, g_loser
new bool:g_choosing
new bool:g_frozen[MAX_PLAYERS + 1]
new Float:g_spot[2][3], bool:g_haveSpot[2]
public plugin_natives()
{
set_native_filter("native_filter")
}
public native_filter(const name[], index, trap)
{
if (!trap && (equal(name, "olds_cap_set_first_pick") || equal(name, "olds_cap_set_side") || equal(name, "olds_cap_release_draft")))
{
g_noCore = true
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_capduel.txt")
bind_pcvar_num(create_cvar("olds_capduel", "1", FCVAR_NONE, "1 = the captains duel before the draft", true, 0.0, true, 1.0), g_enabled)
bind_pcvar_num(create_cvar("olds_capduel_time", "60", FCVAR_NONE, "Seconds the duel lasts before health decides", true, 10.0, true, 600.0), g_duelTime)
AutoExecConfig(true, "olds_capduel")
RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_post", true)
RegisterHookChain(RG_CBasePlayer_HasRestrictItem, "on_restrict_pre", false)
register_concmd("amx_capduel_status", "cmd_status", ADMIN_RCON, "- shows the captain duel")
load_spots()
if (g_noCore)
log_amx("No captain draft core (library olds_captain) is loaded: no duels.")
}
/* configs/olds_capduel.ini: <map> ax ay az bx by bz */
load_spots()
{
new path[160], map[32]
get_configsdir(path, charsmax(path))
add(path, charsmax(path), "/olds_capduel.ini")
get_mapname(map, charsmax(map))
new file = fopen(path, "rt")
if (!file)
return
new line[192], parts[7][24]
while (!feof(file))
{
fgets(file, line, charsmax(line))
trim(line)
if (!line[0] || line[0] == ';' || line[0] == '/')
continue
if (parse(line, parts[0], 23, parts[1], 23, parts[2], 23, parts[3], 23, parts[4], 23, parts[5], 23, parts[6], 23) != 7)
continue
if (!equali(parts[0], map))
continue
for (new k = 0; k < 3; k++)
{
g_spot[0][k] = str_to_float(parts[1 + k])
g_spot[1][k] = str_to_float(parts[4 + k])
}
g_haveSpot[0] = true
g_haveSpot[1] = true
break
}
fclose(file)
}
public client_disconnected(id)
{
g_frozen[id] = false
if (g_dueling && (id == g_cap[0] || id == g_cap[1]))
{
new other = g_cap[0]
if (id == g_cap[0])
other = g_cap[1]
if (is_user_connected(other))
end_duel(other, id, "CAPDUEL_WIN_LEFT")
else
finish_draft()
}
else if (g_choosing && id == g_winner)
{
/* The winner left before choosing: the loser takes the first pick. */
remove_task(TASK_MENU)
g_choosing = false
if (is_user_connected(g_loser))
olds_cap_set_first_pick(g_loser)
finish_draft()
}
}
/* ------------------------------------------------------------------ the core's forward */
public olds_cap_captains_ready(captainA, captainB)
{
if (g_noCore || !g_enabled || g_dueling || g_choosing)
return PLUGIN_CONTINUE
if (!is_user_connected(captainA) || !is_user_connected(captainB) || captainA == captainB)
return PLUGIN_CONTINUE
g_cap[0] = captainA
g_cap[1] = captainB
start_duel()
return PLUGIN_HANDLED
}
/* ------------------------------------------------------------------ the duel */
team_spawn(const classname[], Float:out[3])
{
new ent = engfunc(EngFunc_FindEntityByString, -1, "classname", classname)
if (ent > 0)
{
get_entvar(ent, var_origin, out)
return 1
}
return 0
}
place_captain(side)
{
new id = g_cap[side]
if (!is_user_alive(id))
rg_round_respawn(id)
if (!is_user_alive(id))
return
new Float:spot[3]
if (g_haveSpot[side])
{
spot = g_spot[side]
engfunc(EngFunc_SetOrigin, id, spot)
}
else if (side && team_spawn("info_player_start", spot))
engfunc(EngFunc_SetOrigin, id, spot)
else if (!side && team_spawn("info_player_deathmatch", spot))
engfunc(EngFunc_SetOrigin, id, spot)
set_entvar(id, var_velocity, Float:{0.0, 0.0, 0.0})
set_entvar(id, var_flags, get_entvar(id, var_flags) & ~FL_FROZEN)
rg_remove_all_items(id)
rg_give_item(id, "weapon_knife")
set_entvar(id, var_health, 100.0)
set_entvar(id, var_armorvalue, 0.0)
}
start_duel()
{
g_dueling = true
g_winner = 0
g_loser = 0
/* Captains first, so no team runs empty while the others leave. */
rg_set_user_team(g_cap[0], TEAM_TERRORIST)
rg_set_user_team(g_cap[1], TEAM_CT)
place_captain(0)
place_captain(1)
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id) || id == g_cap[0] || id == g_cap[1])
continue
if (is_user_alive(id))
{
/* Frozen first, in case the move fails; then out to watch. */
set_entvar(id, var_flags, get_entvar(id, var_flags) | FL_FROZEN)
g_frozen[id] = true
user_silentkill(id)
}
if (TeamName:get_member(id, m_iTeam) != TEAM_SPECTATOR)
rg_set_user_team(id, TEAM_SPECTATOR)
}
new a[MAX_NAME_LENGTH], b[MAX_NAME_LENGTH]
get_user_name(g_cap[0], a, charsmax(a))
get_user_name(g_cap[1], b, charsmax(b))
client_print(0, print_chat, "%L", LANG_PLAYER, "CAPDUEL_START", a, b, g_duelTime)
log_amx("Captain duel: %s vs %s, %d s", a, b, g_duelTime)
remove_task(TASK_DUEL)
set_task(float(g_duelTime), "duel_timeout", TASK_DUEL)
}
/* A new round during the duel puts the captains back on their spots. */
public on_restart_post()
{
if (!g_dueling)
return
set_task(0.1, "replace_captains")
}
public replace_captains()
{
if (!g_dueling)
return
place_captain(0)
place_captain(1)
}
/* Captains may not buy or pick up anything during the duel. */
public on_restrict_pre(id, ItemID:item, ItemRestType:type)
{
if (g_dueling && (id == g_cap[0] || id == g_cap[1]) && (type == ITEM_TYPE_BUYING || type == ITEM_TYPE_TOUCHED))
{
SetHookChainReturn(ATYPE_BOOL, true)
return HC_SUPERCEDE
}
return HC_CONTINUE
}
public on_killed_post(victim, killer, gib)
{
if (!g_dueling || (victim != g_cap[0] && victim != g_cap[1]))
return
new other = g_cap[0]
if (victim == g_cap[0])
other = g_cap[1]
end_duel(other, victim, "CAPDUEL_WIN_KILL")
}
public duel_timeout()
{
if (!g_dueling)
return
new ha = 0, hb = 0
if (is_user_alive(g_cap[0]))
ha = get_user_health(g_cap[0])
if (is_user_alive(g_cap[1]))
hb = get_user_health(g_cap[1])
if (ha > hb)
end_duel(g_cap[0], g_cap[1], "CAPDUEL_WIN_HEALTH")
else if (hb > ha)
end_duel(g_cap[1], g_cap[0], "CAPDUEL_WIN_HEALTH")
else
{
new side = random(2)
end_duel(g_cap[side], g_cap[1 - side], "CAPDUEL_WIN_RANDOM")
}
}
end_duel(winner, loser, const why[])
{
g_dueling = false
remove_task(TASK_DUEL)
unfreeze_all()
g_winner = winner
g_loser = loser
new w[MAX_NAME_LENGTH], l[MAX_NAME_LENGTH]
get_user_name(winner, w, charsmax(w))
if (is_user_connected(loser))
get_user_name(loser, l, charsmax(l))
client_print(0, print_chat, "%L", LANG_PLAYER, why, w, l)
log_amx("Captain duel won by %s (%s)", w, why)
g_choosing = true
show_choice(winner)
remove_task(TASK_MENU)
set_task(float(MENU_TIME) + 0.5, "choice_timeout", TASK_MENU)
}
unfreeze_all()
{
for (new id = 1; id <= MaxClients; id++)
{
if (g_frozen[id] && is_user_connected(id))
set_entvar(id, var_flags, get_entvar(id, var_flags) & ~FL_FROZEN)
g_frozen[id] = false
}
}
/* ------------------------------------------------------------------ the winner's choice */
show_choice(id)
{
new text[64]
formatex(text, charsmax(text), "%L", id, "CAPDUEL_MENU", MENU_TIME)
new menu = menu_create(text, "on_choice")
formatex(text, charsmax(text), "%L", id, "CAPDUEL_FIRST_PICK")
menu_additem(menu, text)
formatex(text, charsmax(text), "%L", id, "CAPDUEL_SIDE")
menu_additem(menu, text)
menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
menu_display(id, menu, 0, MENU_TIME)
}
public on_choice(id, menu, item)
{
menu_destroy(menu)
if (!g_choosing || id != g_winner)
return PLUGIN_HANDLED
if (item == 0)
{
take_first_pick(id)
return PLUGIN_HANDLED
}
if (item == 1)
{
new text[64]
formatex(text, charsmax(text), "%L", id, "CAPDUEL_SIDE_MENU")
new side = menu_create(text, "on_side")
formatex(text, charsmax(text), "%L", id, "CAPDUEL_T")
menu_additem(side, text)
formatex(text, charsmax(text), "%L", id, "CAPDUEL_CT")
menu_additem(side, text)
menu_setprop(side, MPROP_EXIT, MEXIT_NEVER)
menu_display(id, side, 0, MENU_TIME)
}
/* A timed-out or closed menu: choice_timeout decides. */
return PLUGIN_HANDLED
}
public on_side(id, menu, item)
{
menu_destroy(menu)
if (!g_choosing || id != g_winner || (item != 0 && item != 1))
return PLUGIN_HANDLED
remove_task(TASK_MENU)
g_choosing = false
new team = item + 1
olds_cap_set_side(id, team)
if (is_user_connected(g_loser))
olds_cap_set_first_pick(g_loser)
new w[MAX_NAME_LENGTH]
get_user_name(id, w, charsmax(w))
if (team == 1)
client_print(0, print_chat, "%L", LANG_PLAYER, "CAPDUEL_TOOK_T", w)
else
client_print(0, print_chat, "%L", LANG_PLAYER, "CAPDUEL_TOOK_CT", w)
finish_draft()
return PLUGIN_HANDLED
}
take_first_pick(id)
{
remove_task(TASK_MENU)
g_choosing = false
olds_cap_set_first_pick(id)
new w[MAX_NAME_LENGTH]
get_user_name(id, w, charsmax(w))
client_print(0, print_chat, "%L", LANG_PLAYER, "CAPDUEL_TOOK_PICK", w)
finish_draft()
}
public choice_timeout()
{
if (!g_choosing)
return
if (is_user_connected(g_winner))
{
show_menu(g_winner, 0, "^n", 1)
take_first_pick(g_winner)
}
else
{
g_choosing = false
finish_draft()
}
}
finish_draft()
{
g_dueling = false
g_choosing = false
remove_task(TASK_DUEL)
remove_task(TASK_MENU)
unfreeze_all()
olds_cap_release_draft()
}
/* ------------------------------------------------------------------ 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
}
new from[8]
copy(from, charsmax(from), "spawns")
if (g_haveSpot[0])
copy(from, charsmax(from), "ini")
console_print(id, "[CapDuel] enabled %d, core %d, dueling %d (#%d vs #%d), choosing %d (winner #%d, loser #%d), spots from %s",
g_enabled, g_noCore ? 0 : 1, g_dueling, g_cap[0], g_cap[1], g_choosing, g_winner, g_loser, from)
for (new p = 1; p <= MaxClients; p++)
{
if (!is_user_connected(p))
continue
new name[MAX_NAME_LENGTH]
get_user_name(p, name, charsmax(name))
console_print(id, "[CapDuel] #%d %s team %d alive %d health %d frozen %d", p, name, get_member(p, m_iTeam),
is_user_alive(p), get_user_health(p), (get_entvar(p, var_flags) & FL_FROZEN) != 0)
}
return PLUGIN_HANDLED
}