/*
* Reset Score (olds.ro workshop card LIB-2142)
*
* A player types /rs or /resetscore in chat to set their own kills and deaths
* back to zero. Nothing else about the player changes: weapons, the bomb,
* money and team stay as they are.
*
* Chat commands (chat and team chat, any letter case, slash optional):
* /rs rs
* /resetscore resetscore
*
* Cvars (saved with their descriptions in configs/plugins/olds_resetscore.cfg
* the first time the plugin starts):
* olds_rs_cooldown 60 Seconds a player waits between two resets, 0 = no wait.
* olds_rs_confirm 1 1 = ask with a Yes/No menu first, 0 = reset at once.
* olds_rs_alive 1 1 = living players may reset, 0 = only dead players
* and spectators.
*
* Requires AMX Mod X 1.9 or newer with the cstrike and fun modules.
* ReAPI is not used.
*
* Copyright (C) 2026 alexit (olds.ro workshop)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* 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/>.
*/
#include <amxmodx>
#include <cstrike>
#include <fun>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Reset Score";
new const PLUGIN_VERSION[] = "1.1.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
// Chat lines that ask for a reset, compared without regard to letter case.
new const RESET_COMMANDS[][] = { "/rs", "/resetscore", "rs", "resetscore" };
// Seconds the Yes/No menu stays on screen before it closes by itself.
const CONFIRM_MENU_TIME = 10;
// Value of g_lastReset[] for a player who has not reset since joining.
const Float:NEVER = -1.0;
// Positions of the two answers in the confirmation menu.
enum
{
ANSWER_YES,
ANSWER_NO
};
new g_cooldown; // olds_rs_cooldown
new g_confirm; // olds_rs_confirm
new g_allowAlive; // olds_rs_alive
new g_msgScoreInfo;
new g_confirmMenu;
// Game time of each player's last reset, used for the cooldown.
new Float:g_lastReset[MAX_PLAYERS + 1] = { NEVER, ... };
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
g_msgScoreInfo = get_user_msgid("ScoreInfo");
if (!g_msgScoreInfo)
set_fail_state("The ScoreInfo message does not exist. This plugin needs Counter-Strike.");
register_clcmd("say", "cmd_say");
register_clcmd("say_team", "cmd_say");
bind_pcvar_num(create_cvar("olds_rs_cooldown", "60", FCVAR_NONE,
"Seconds a player waits between two score resets (0 = no wait)",
true, 0.0, true, 86400.0), g_cooldown);
bind_pcvar_num(create_cvar("olds_rs_confirm", "1", FCVAR_NONE,
"1 = ask with a Yes/No menu before the reset, 0 = reset at once",
true, 0.0, true, 1.0), g_confirm);
bind_pcvar_num(create_cvar("olds_rs_alive", "1", FCVAR_NONE,
"1 = living players may reset, 0 = only dead players and spectators",
true, 0.0, true, 1.0), g_allowAlive);
AutoExecConfig(true, "olds_resetscore");
// One menu serves every player and lives until plugin_end, so there is
// no per-request handle that could be left behind.
g_confirmMenu = menu_create("Reset your score to 0 / 0?", "menu_confirm");
menu_additem(g_confirmMenu, "Yes");
menu_additem(g_confirmMenu, "No");
menu_setprop(g_confirmMenu, MPROP_EXIT, MEXIT_NEVER);
}
public plugin_end()
{
menu_destroy(g_confirmMenu);
}
public client_disconnected(id)
{
// The slot may go to someone else. Rejoining clears the score anyway,
// so the cooldown does not need to outlive the connection.
g_lastReset[id] = NEVER;
}
/**
* Chat hook. Only a message that is exactly one of the commands counts, so
* "/rsx", "rs please" or "/rs please" are left alone as ordinary chat.
*/
public cmd_say(id)
{
new text[32];
// Chat sends the whole line as one argument. A line that fills the
// buffer was cut short, so it is longer than any command.
if (read_argc() != 2 || read_argv(1, text, charsmax(text)) >= charsmax(text))
return PLUGIN_CONTINUE;
trim(text);
if (!is_reset_command(text))
return PLUGIN_CONTINUE;
if (is_user_connected(id))
request_reset(id);
// The command itself is not shown in chat.
return PLUGIN_HANDLED;
}
bool:is_reset_command(const text[])
{
for (new i = 0; i < sizeof(RESET_COMMANDS); i++)
{
if (equali(text, RESET_COMMANDS[i]))
return true;
}
return false;
}
/**
* Answer from the confirmation menu. item is negative when the menu timed
* out or AMXX closed it (another menu opened, the player left, the map ended).
*/
public menu_confirm(id, menu, item)
{
// The answer can arrive several seconds after the request, so the rules
// are checked again before anything changes.
if (item == ANSWER_YES && is_user_connected(id) && can_reset(id))
reset_score(id);
// The menu is shared, so it is never destroyed here.
return PLUGIN_HANDLED;
}
request_reset(id)
{
if (!can_reset(id))
return;
if (g_confirm)
menu_display(id, g_confirmMenu, 0, CONFIRM_MENU_TIME);
else
reset_score(id);
}
/**
* Tells whether the player may reset right now. When the answer is no, the
* reason is printed to that player only.
*/
bool:can_reset(id)
{
if (!g_allowAlive && is_user_alive(id))
{
client_print_color(id, print_team_default, "You can reset your score only while ^4dead^1.");
return false;
}
// Measured from the last reset with the current cvar value, so a changed
// cooldown applies at once.
if (g_lastReset[id] != NEVER)
{
new Float:wait = g_lastReset[id] + float(g_cooldown) - get_gametime();
if (wait > 0.0)
{
new seconds = floatround(wait, floatround_ceil);
client_print_color(id, print_team_default, "You can reset your score again in ^4%d^1 second%s.",
seconds, seconds == 1 ? "" : "s");
return false;
}
}
if (get_user_frags(id) == 0 && cs_get_user_deaths(id) == 0)
{
// "^40" would be read as one escape (character 40), so the digits
// go in through the format string.
client_print_color(id, print_team_default, "Your score is already ^4%s^1.", "0 / 0");
return false;
}
return true;
}
reset_score(id)
{
set_user_frags(id, 0);
// The module would send ScoreInfo by itself here. It is switched off so
// the scoreboard gets one update, sent below.
cs_set_user_deaths(id, 0, false);
send_score_info(id);
g_lastReset[id] = get_gametime();
client_print_color(id, print_team_default, "^4Score reset");
}
/**
* Sends the player's scoreboard line to every client. The "e" natives pass
* the message through message hooks, so plugins that follow ScoreInfo see
* the reset as well.
*/
send_score_info(id)
{
emessage_begin(MSG_ALL, g_msgScoreInfo);
ewrite_byte(id);
ewrite_short(get_user_frags(id));
ewrite_short(cs_get_user_deaths(id));
ewrite_short(0); // class id, not used by Counter-Strike
ewrite_short(_:cs_get_user_team(id));
emessage_end();
}