/*
* Appeal Slip (olds.ro)
* Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
*
* 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/>.
*
* Jailbreak: a prisoner (T) killed by a guard (CT) may send that guard one private appeal,
* "/appeal <text>", asking for the reason. Only that guard sees it, and only that guard can
* reply with "/answer <text>" (one answer per appeal). The slip belongs to the prisoner's most
* recent guard elimination this round; it expires at round end or when either player leaves.
*
* Cvar: olds_appeal_len 80 (10-150) longest appeal or answer, in bytes
*/
#include <amxmodx>
#include <reapi>
#pragma semicolon 0
#define PLUGIN "Appeal Slip"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define MAX_TEXT 160
new g_maxLen
/* Per prisoner: the guard who eliminated them (0 = no slip) and whether the appeal was sent. */
new g_guard[MAX_PLAYERS + 1]
new g_guardUserid[MAX_PLAYERS + 1]
new bool:g_sent[MAX_PLAYERS + 1]
/* Per guard: the prisoner whose appeal waits for an answer (0 = none). */
new g_waiting[MAX_PLAYERS + 1]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
bind_pcvar_num(create_cvar("olds_appeal_len", "80", FCVAR_NONE,
"Longest appeal or answer in bytes", true, 10.0, true, 150.0), g_maxLen)
RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_pre", false)
register_logevent("on_round_end", 2, "1=Round_End")
register_clcmd("say", "hook_say")
register_clcmd("say_team", "hook_say")
register_dictionary("olds_appeal.txt")
AutoExecConfig(true, "olds_appeal")
}
clear_slip(prisoner)
{
new guard = g_guard[prisoner]
if (guard && g_waiting[guard] == prisoner)
g_waiting[guard] = 0
g_guard[prisoner] = 0
g_guardUserid[prisoner] = 0
g_sent[prisoner] = false
}
clear_all()
{
for (new i = 1; i <= MaxClients; i++)
{
g_guard[i] = 0
g_guardUserid[i] = 0
g_sent[i] = false
g_waiting[i] = 0
}
}
public on_restart_pre()
{
clear_all()
}
public on_round_end()
{
clear_all()
}
public client_disconnected(id)
{
clear_slip(id)
/* A guard leaving takes every slip addressed to them. */
for (new p = 1; p <= MaxClients; p++)
{
if (g_guard[p] == id)
clear_slip(p)
}
g_waiting[id] = 0
}
public on_killed_post(victim, attacker, gib)
{
if (victim < 1 || victim > MaxClients)
return HC_CONTINUE
/* A new death replaces any older slip. */
clear_slip(victim)
if (attacker < 1 || attacker > MaxClients || attacker == victim || !is_user_connected(attacker))
return HC_CONTINUE
if (TeamName:get_member(victim, m_iTeam) != TEAM_TERRORIST || TeamName:get_member(attacker, m_iTeam) != TEAM_CT)
return HC_CONTINUE
g_guard[victim] = attacker
g_guardUserid[victim] = get_user_userid(attacker)
client_print_color(victim, print_team_default, "%L", victim, "APPEAL_OFFER")
return HC_CONTINUE
}
/* Copies the text after the command, without control characters, cut to the length cvar
* (never inside a UTF-8 character). Returns its length. */
clean_text(const src[], dest[], destLen)
{
new limit = clamp(g_maxLen, 10, 150)
if (limit > destLen)
limit = destLen
new len
for (new i = 0; src[i] && len < limit; i++)
{
if (src[i] < 32 && src[i] >= 0)
continue
dest[len++] = src[i]
}
dest[len] = EOS
/* Cut back to the start of a UTF-8 character if the limit split one. */
if (len >= limit && src[0])
{
new back = len
while (back > 0 && (dest[back - 1] & 0xC0) == 0x80)
back--
if (back > 0 && (dest[back - 1] & 0xC0) == 0xC0)
{
new lead = dest[back - 1], need = 1
if ((lead & 0xE0) == 0xC0)
need = 2
else if ((lead & 0xF0) == 0xE0)
need = 3
else if ((lead & 0xF8) == 0xF0)
need = 4
if (len - (back - 1) < need)
{
len = back - 1
dest[len] = EOS
}
}
}
trim(dest)
return strlen(dest)
}
public hook_say(id)
{
if (!is_user_connected(id))
return PLUGIN_CONTINUE
new said[192]
read_args(said, charsmax(said))
remove_quotes(said)
trim(said)
if (equali(said, "/appeal", 7) && (said[7] == EOS || said[7] == ' '))
{
do_appeal(id, said[7])
return PLUGIN_HANDLED
}
if (equali(said, "/answer", 7) && (said[7] == EOS || said[7] == ' '))
{
do_answer(id, said[7])
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
do_appeal(id, const rest[])
{
new guard = g_guard[id]
if (!guard || is_user_alive(id))
{
client_print_color(id, print_team_default, "%L", id, "APPEAL_NONE")
return
}
if (!is_user_connected(guard) || get_user_userid(guard) != g_guardUserid[id])
{
clear_slip(id)
client_print_color(id, print_team_default, "%L", id, "APPEAL_GUARD_GONE")
return
}
if (g_sent[id])
{
client_print_color(id, print_team_default, "%L", id, "APPEAL_ALREADY")
return
}
new text[MAX_TEXT]
if (!clean_text(rest, text, charsmax(text)))
{
client_print_color(id, print_team_default, "%L", id, "APPEAL_USAGE", g_maxLen)
return
}
g_sent[id] = true
g_waiting[guard] = id
new pname[32], gname[32]
get_user_name(id, pname, charsmax(pname))
get_user_name(guard, gname, charsmax(gname))
client_print_color(guard, id, "%L", guard, "APPEAL_TO_GUARD", pname, text)
client_print_color(id, print_team_default, "%L", id, "APPEAL_SENT", gname)
log_amx("Appeal from ^"%s^" to ^"%s^": %s", pname, gname, text)
}
do_answer(id, const rest[])
{
new prisoner = g_waiting[id]
if (!prisoner || g_guard[prisoner] != id || !is_user_connected(prisoner))
{
g_waiting[id] = 0
client_print_color(id, print_team_default, "%L", id, "ANSWER_NONE")
return
}
new text[MAX_TEXT]
if (!clean_text(rest, text, charsmax(text)))
{
client_print_color(id, print_team_default, "%L", id, "ANSWER_USAGE", g_maxLen)
return
}
/* One answer per appeal: the slip is used up. */
g_waiting[id] = 0
g_guard[prisoner] = 0
g_guardUserid[prisoner] = 0
new pname[32], gname[32]
get_user_name(prisoner, pname, charsmax(pname))
get_user_name(id, gname, charsmax(gname))
client_print_color(prisoner, id, "%L", prisoner, "ANSWER_TO_PRISONER", gname, text)
client_print_color(id, print_team_default, "%L", id, "ANSWER_SENT", pname)
log_amx("Answer from ^"%s^" to ^"%s^": %s", gname, pname, text)
}