Skip to content
View in the app

A better way to browse. Learn more.

Learn more

round-bookmark/addons/amxmodx/scripting/olds_mark.sma

olds_mark.sma
236 lines · 7.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Round Bookmark (olds.ro workshop card AMX-015)
 *
 * Lets a spectator or a dead player tag the current round for later demo
 * review. Each bookmark keeps the map, the round number and the elapsed map
 * time, plus a short note; the list is private and printed to the player's
 * console only.
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI.
 *
 *   say /bookmark [note]    (spectators and dead players; note up to 48 characters)
 *   bookmarks               console: your latest bookmarks, newest last
 *
 * The round number counts every round start on this map
 * (RG_CSGameRules_RestartRound, including restarts); a bookmark taken before
 * the first round starts is round 0. Map time is the server's map clock
 * (get_gametime). Each player keeps the latest olds_mark_max bookmarks
 * (1-20, default 5); the oldest is dropped first, and lowering olds_mark_max
 * drops the older ones for good. Lists are cleared on disconnect and on map
 * change. A note longer than 48 bytes is cut at a whole UTF-8 character.
 *
 * Commands: olds_mark_status (ADMIN_CFG) prints every player's list.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Round Bookmark"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_MARKS = 20
const NOTE_LEN = 48
const Float:MARK_COOLDOWN = 1.0

new const SAY_PREFIX[] = "/bookmark"

// Per player: a ring of the latest bookmarks.
new g_note[MAX_PLAYERS + 1][MAX_MARKS][NOTE_LEN + 1]
new g_round[MAX_PLAYERS + 1][MAX_MARKS]
new Float:g_time[MAX_PLAYERS + 1][MAX_MARKS]
new g_next[MAX_PLAYERS + 1]      // slot for the next bookmark
new g_count[MAX_PLAYERS + 1]     // bookmarks kept, up to MAX_MARKS
new Float:g_nextMark[MAX_PLAYERS + 1]

new g_map[32]
new g_roundNumber
new g_max

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

    new cvar = create_cvar("olds_mark_max", "5", FCVAR_NONE,
        "Bookmarks kept and listed per player (1-20).", true, 1.0, true, float(MAX_MARKS))
    bind_pcvar_num(cvar, g_max)
    hook_cvar_change(cvar, "on_max_changed")
    AutoExecConfig(true, "olds_mark")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_clcmd("bookmarks", "cmd_bookmarks")
    register_concmd("olds_mark_status", "cmd_status", ADMIN_CFG, "- every player's bookmarks")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    get_mapname(g_map, charsmax(g_map))
}

public on_max_changed(pcvar, const oldValue[], const newValue[])
{
    // Bookmarks beyond a lower limit are dropped, so raising it again cannot bring them back.
    new limit = clamp(str_to_num(newValue), 1, MAX_MARKS)
    for (new id = 1; id <= MaxClients; id++)
        g_count[id] = min(g_count[id], limit)
}

public on_restart_round_pre()
{
    g_roundNumber++
    return HC_CONTINUE
}

/* ---------- adding ---------- */

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)

    // "/bookmark" alone or followed by a space and the note; anything else is ordinary chat.
    new prefixLength = strlen(SAY_PREFIX)
    if (!equali(text, SAY_PREFIX, prefixLength) || (text[prefixLength] && text[prefixLength] != ' '))
        return PLUGIN_CONTINUE

    add_bookmark(id, text[prefixLength])
    return PLUGIN_HANDLED // keep the note private
}

add_bookmark(id, const rawNote[])
{
    new Float:now = get_gametime()
    if (now < g_nextMark[id])
        return
    g_nextMark[id] = now + MARK_COOLDOWN

    if (is_user_alive(id))
    {
        client_print_color(id, print_team_default, "^4[Bookmark]^1 %L", id, "MARK_DEAD_ONLY")
        return
    }

    new note[NOTE_LEN + 1]
    new bool:cut = clean_note(rawNote, note, charsmax(note))

    new slot = g_next[id]
    copy(g_note[id][slot], charsmax(g_note[][]), note)
    g_round[id][slot] = g_roundNumber
    g_time[id][slot] = now
    g_next[id] = (slot + 1) % MAX_MARKS
    // Only the latest olds_mark_max are kept: older ones count as dropped.
    g_count[id] = min(g_count[id] + 1, g_max)

    new clock[16]
    format_clock(now, clock, charsmax(clock))
    client_print_color(id, print_team_default, "^4[Bookmark]^1 %L", id, cut ? "MARK_SAVED_CUT" : "MARK_SAVED",
        g_roundNumber, clock, NOTE_LEN)
}

/*
 * Copies printable characters only (no control or colour codes), skips the
 * leading spaces and keeps up to len bytes, never ending inside a UTF-8
 * character. Returns true when the note was cut.
 */
bool:clean_note(const raw[], note[], len)
{
    new length, bool:cut
    new i = 0
    while (raw[i] == ' ')
        i++
    for (; raw[i]; i++)
    {
        new byte = raw[i] & 0xFF
        if (byte < ' ' || byte == 0x7F || byte == '^^')
            continue
        if (length == len)
        {
            cut = true
            // The next byte continues a character: drop that character's start too.
            if ((byte & 0xC0) == 0x80)
            {
                while (length > 0 && (note[length - 1] & 0xC0) == 0x80)
                    length--
                if (length > 0 && (note[length - 1] & 0xC0) == 0xC0)
                    length--
            }
            break
        }
        note[length++] = byte
    }
    note[length] = EOS
    trim(note)
    return cut
}

format_clock(Float:seconds, clock[], len)
{
    new total = floatround(seconds, floatround_floor)
    formatex(clock, len, "%d:%02d", total / 60, total % 60)
}

/* ---------- listing ---------- */

public cmd_bookmarks(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED

    print_bookmarks(id, id)
    return PLUGIN_HANDLED
}

/* Prints owner's latest olds_mark_max bookmarks, oldest first, to viewer's console. */
print_bookmarks(viewer, owner)
{
    new shown = min(g_count[owner], g_max)
    if (!shown)
    {
        console_print(viewer, "[Bookmark] %L", viewer ? viewer : LANG_SERVER, "MARK_NONE")
        return
    }

    new clock[16], noNote[NOTE_LEN + 1]
    formatex(noNote, charsmax(noNote), "%L", viewer ? viewer : LANG_SERVER, "MARK_NO_NOTE")
    for (new i = shown; i >= 1; i--)
    {
        new slot = (g_next[owner] - i + MAX_MARKS) % MAX_MARKS
        format_clock(g_time[owner][slot], clock, charsmax(clock))
        console_print(viewer, "[Bookmark] %s  round %d  %s  %s", g_map, g_round[owner][slot], clock,
            g_note[owner][slot][0] ? g_note[owner][slot] : noNote)
    }
}

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

    console_print(id, "[Bookmark] %s, round %d, keeping %d per player", g_map, g_roundNumber, g_max)
    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, "[Bookmark] -- %s (%d stored)", name, g_count[players[i]])
        print_bookmarks(id, players[i])
    }
    return PLUGIN_HANDLED
}

/* ---------- players ---------- */

public client_disconnected(id)
{
    g_next[id] = 0
    g_count[id] = 0
    g_nextMark[id] = 0.0
}

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.