Skip to content
View in the app

A better way to browse. Learn more.

Learn more

warden-s-agenda/addons/amxmodx/scripting/olds_agenda.sma

olds_agenda.sma
334 lines · 9.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Warden's Agenda (olds.ro workshop card AMX-011)
 *
 * The warden writes up to three short lines for the round ("stay in cells
 * until 1:30", "no camping in the armoury"). Anyone can read them with
 * /agenda, and a player who joins mid-round sees them once on their first
 * spawn, so late joiners get the same instructions as everyone else.
 *
 *   say /agenda <text>    warden only: add a line (up to three, each cut at olds_agenda_len characters)
 *   say /agenda clear     warden only: remove every line
 *   say /agenda           read the agenda
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and the Jailbreak core
 * "[JB] Core" (library "jailbreak"; github.com/al1k24/JailBreak-ReAPI,
 * Jb_Core.sma). The warden is whoever jb_get_simon_id() names at that moment
 * (0 when there is none); this plugin keeps no warden of its own.
 *   - Warden transfer: checked every second; when a different player is
 *     warden than the one who wrote the agenda, the agenda is cleared.
 *   - Round reset: RG_CSGameRules_RestartRound clears the agenda.
 *
 * Late joiners: a player who connects gets the agenda once, 1.5 s after their
 * first spawn in that round (chat, a DHUD block and the stock
 * buttons/button3.wav). Later spawns (respawns) show nothing; a new round
 * starts over.
 *
 * Cvar: olds_agenda_len (10-100, default 70), the longest line in characters
 * (a line never exceeds 120 bytes).
 * Command: olds_agenda_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  "Warden's Agenda"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/*
 * Jailbreak core API used here (jb/core.inc). Declared in this file so it
 * compiles without the core's includes; the library is still required.
 */
#pragma reqlib "jailbreak"
native jb_get_simon_id()

const MAX_LINES = 3
const MAX_CHARS = 100
const LINE_BYTES = 120
const TASK_WATCH = 6211
const TASK_SHOW = 6300
const Float:SHOW_DELAY = 1.5
const Float:AGENDA_COOLDOWN = 1.0

new const SAY_COMMAND[] = "/agenda"
new const CUE_SOUND[] = "buttons/button3"

new g_line[MAX_LINES][LINE_BYTES + 1]
new g_lineCount
new g_authorUserId            // user id of the warden who wrote the agenda

new bool:g_lateJoin[MAX_PLAYERS + 1]   // connected this round, first spawn not seen yet
new bool:g_shown[MAX_PLAYERS + 1]      // got the late-join agenda this round
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_maxChars

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_agenda.txt")

    bind_pcvar_num(create_cvar("olds_agenda_len", "70", FCVAR_NONE,
        "Longest agenda line, in characters (10-100).", true, 10.0, true, float(MAX_CHARS)), g_maxChars)
    AutoExecConfig(true, "olds_agenda")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_agenda_status", "cmd_status", ADMIN_CFG, "- the agenda, its author and the current warden")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_post", true)

    set_task(1.0, "task_watch_warden", TASK_WATCH, _, _, "b")
}

/* ---------- the warden ---------- */

current_warden()
{
    new warden = jb_get_simon_id()
    return (1 <= warden <= MaxClients && is_user_connected(warden)) ? warden : 0
}

public task_watch_warden()
{
    if (!g_lineCount)
        return
    // A different player holds the warden role: the old agenda is not theirs.
    new warden = current_warden()
    if (warden && get_user_userid(warden) != g_authorUserId)
        clear_agenda()
}

clear_agenda()
{
    g_lineCount = 0
    g_authorUserId = 0
}

/* ---------- rounds and late joiners ---------- */

public on_restart_round_pre()
{
    // A player who joined but has not spawned yet keeps the late-join flag,
    // so their first spawn in the new round still shows that round's agenda.
    clear_agenda()
    for (new id = 1; id <= MaxClients; id++)
    {
        g_shown[id] = false
        remove_task(TASK_SHOW + id)
    }
    return HC_CONTINUE
}

public client_putinserver(id)
{
    g_lateJoin[id] = true
    g_shown[id] = false
}

public client_disconnected(id)
{
    g_lateJoin[id] = false
    g_shown[id] = false
    g_nextUse[id] = 0.0
    remove_task(TASK_SHOW + id)
}

public on_spawn_post(id)
{
    if (!(1 <= id <= MaxClients) || !g_lateJoin[id] || !is_user_alive(id))
        return HC_CONTINUE

    // The first spawn after joining: show it once, a moment later.
    g_lateJoin[id] = false
    if (!g_shown[id])
        set_task(SHOW_DELAY, "task_show_late", TASK_SHOW + id)
    return HC_CONTINUE
}

public task_show_late(taskId)
{
    new id = taskId - TASK_SHOW
    if (!is_user_connected(id) || g_shown[id] || !g_lineCount)
        return
    g_shown[id] = true

    print_agenda(id)
    set_dhudmessage(255, 220, 120, 0.02, 0.30, 0, 0.0, 8.0, 0.2, 0.5)
    new block[MAX_LINES * (LINE_BYTES + 4) + 64], length
    length = formatex(block, charsmax(block), "%L", id, "AGENDA_HUD_TITLE")
    for (new i = 0; i < g_lineCount; i++)
        length += formatex(block[length], charsmax(block) - length, "^n%d. %s", i + 1, g_line[i])
    show_dhudmessage(id, "%s", block)
    client_cmd(id, "spk %s", CUE_SOUND)
}

/* ---------- /agenda ---------- */

public on_say(id)
{
    if (!is_user_connected(id))
        return PLUGIN_CONTINUE

    new text[192]
    read_args(text, charsmax(text))
    remove_quotes(text)
    trim(text)

    new length = strlen(SAY_COMMAND)
    if (!equali(text, SAY_COMMAND, length) || (text[length] && text[length] != ' '))
        return PLUGIN_CONTINUE

    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED
    g_nextUse[id] = now + AGENDA_COOLDOWN

    new start = length
    while (text[start] == ' ')
        start++

    if (!text[start])
        print_agenda(id)
    else
        set_agenda(id, text[start])
    return PLUGIN_HANDLED
}

set_agenda(id, const argument[])
{
    new warden = current_warden()
    if (id != warden)
    {
        client_print_color(id, print_team_default, "^4[Agenda]^1 %L", id, "AGENDA_WARDEN_ONLY")
        return
    }

    // A new warden starts a new agenda.
    if (g_lineCount && get_user_userid(id) != g_authorUserId)
        clear_agenda()

    if (equali(argument, "clear"))
    {
        clear_agenda()
        client_print_color(id, print_team_default, "^4[Agenda]^1 %L", id, "AGENDA_CLEARED")
        return
    }
    if (g_lineCount == MAX_LINES)
    {
        client_print_color(id, print_team_default, "^4[Agenda]^1 %L", id, "AGENDA_FULL", MAX_LINES)
        return
    }

    new line[LINE_BYTES + 1]
    new bool:cut = clean_line(argument, line, g_maxChars)
    if (!line[0])
    {
        print_agenda(id)
        return
    }

    copy(g_line[g_lineCount], charsmax(g_line[]), line)
    g_lineCount++
    g_authorUserId = get_user_userid(id)

    client_print_color(id, print_team_default, "^4[Agenda]^1 %L", id, cut ? "AGENDA_ADDED_CUT" : "AGENDA_ADDED",
        g_lineCount, MAX_LINES, g_maxChars)
}

print_agenda(id)
{
    if (!g_lineCount)
    {
        client_print_color(id, print_team_default, "^4[Agenda]^1 %L", id, "AGENDA_EMPTY")
        return
    }
    new name[MAX_NAME_LENGTH]
    get_author_name(name, charsmax(name))
    client_print_color(id, print_team_default, "^4[Agenda]^1 %L", id, "AGENDA_TITLE", name)
    for (new i = 0; i < g_lineCount; i++)
        client_print_color(id, print_team_default, "^4[Agenda]^1 %d. ^3%s", i + 1, g_line[i])
}

/* The author's current name, or "-" when they are gone. */
get_author_name(name[], len)
{
    copy(name, len, "-")
    for (new id = 1; id <= MaxClients; id++)
    {
        if (is_user_connected(id) && get_user_userid(id) == g_authorUserId)
        {
            get_user_name(id, name, len)
            return
        }
    }
}

/*
 * Copies printable characters, at most maxChars characters and LINE_BYTES
 * bytes, never ending inside a UTF-8 character. Returns true when cut.
 */
bool:clean_line(const raw[], line[], maxChars)
{
    new length, chars, bool:cut
    for (new i = 0; raw[i]; i++)
    {
        new byte = raw[i] & 0xFF
        if (byte < ' ' || byte == 0x7F || byte == '^^')
            continue

        if ((byte & 0xC0) != 0x80)
        {
            new size = byte >= 0xF0 ? 4 : (byte >= 0xE0 ? 3 : (byte >= 0xC0 ? 2 : 1))
            if (chars == maxChars || length + size > LINE_BYTES)
            {
                cut = true
                break
            }
            chars++
        }
        else if (length == LINE_BYTES)
        {
            cut = true
            break
        }
        line[length++] = byte
    }
    line[length] = EOS
    trim(line)
    return cut
}

/* ---------- admin ---------- */

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED

    new warden = current_warden(), wardenName[MAX_NAME_LENGTH], author[MAX_NAME_LENGTH]
    if (warden)
        get_user_name(warden, wardenName, charsmax(wardenName))
    else
        copy(wardenName, charsmax(wardenName), "none")
    get_author_name(author, charsmax(author))

    console_print(id, "[Agenda] warden: %s; %d line(s) by %s; lines up to %d characters", wardenName,
        g_lineCount, g_lineCount ? author : "-", g_maxChars)
    for (new i = 0; i < g_lineCount; i++)
        console_print(id, "[Agenda] %d. %s", i + 1, g_line[i])

    new players[MAX_PLAYERS], count, name[MAX_NAME_LENGTH]
    get_players(players, count, "h")
    for (new i = 0; i < count; i++)
    {
        get_user_name(players[i], name, charsmax(name))
        console_print(id, "[Agenda] %s: late join pending %d, shown %d", name, g_lateJoin[players[i]],
            g_shown[players[i]])
    }
    return PLUGIN_HANDLED
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.