Skip to content
View in the app

A better way to browse. Learn more.

Learn more

checkpoint-postcard/addons/amxmodx/scripting/olds_cpnote.sma

olds_cpnote.sma
355 lines · 11.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Checkpoint Postcard (olds.ro workshop card AMX-008)
 *
 * Lets a Kreedz player pin a short private note to their current checkpoint
 * ("crouch-jump off the left edge"). When they later restore that checkpoint
 * the note shows on their screen as one DHUD line for 4 seconds.
 *
 *   say /cpnote <text>    attach the text to your current checkpoint (cut at olds_cpnote_len characters)
 *   say /cpnote clear     remove the note from your current checkpoint
 *   say /cpnote           show your current checkpoint's note in chat
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and the Kreedz timer by Theggv
 * (kz_core.sma, kreedz_api.inc; checked against github.com/Theggv/Kreedz
 * commit 6baf707). How this plugin follows that timer's checkpoints:
 *   - The core keeps the latest 20 checkpoints in a ring; gocheck (/tp, /gc)
 *     restores the newest, /stuck steps back one and restores it, and the
 *     next save overwrites the slot after the restored one. This plugin keeps
 *     the same ring of 20, so a checkpoint's id is its slot in that ring, and
 *     a note lives exactly as long as its checkpoint: overwriting the slot
 *     (a new save, including one after /stuck) removes the old note.
 *   - kz_cp_pre fires before the core decides whether to save (it refuses in
 *     the air), and kz_cp_post is never fired by that core, so a save is
 *     confirmed on the next frame by kz_get_cp_num() having gone up, and its
 *     position read with kz_get_last_cp().
 *   - On kz_tp_post the restored checkpoint is the one at kz_get_last_cp();
 *     if that is an older slot (after /stuck) the ring position follows it.
 *     A position this plugin never saw (another plugin replaced the core's
 *     checkpoints, e.g. a saved-run restore) drops every note.
 *   - A new run (kz_timer_start_post) resets the core's checkpoints, so all
 *     notes go too. Paused-mode checkpoints are a separate practice ring and
 *     never carry notes.
 *   - The core keeps a player's checkpoints when they reconnect to the same
 *     slot with the same Steam id. Notes do the same, but only for a real
 *     Steam id: LAN ids and bots share one id, so their notes are dropped on
 *     every new connection and can never reach another player.
 * Notes are private (only the owner ever sees them), kept in memory only and
 * cleared on map change.
 *
 * Cvar: olds_cpnote_len (1-60, default 60), the longest note in characters.
 * Command: olds_cpnote_status (ADMIN_CFG) lists each player's checkpoint ring
 * and note lengths, never the note text.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Checkpoint Postcard"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/*
 * Kreedz API used here (kreedz_api.inc). Declared in this file so it compiles
 * without the timer's include. The core registers no library, so a server
 * without it fails to load this plugin with a "native not found" error.
 */
enum TimerState
{
    TIMER_DISABLED,
    TIMER_PAUSED,
    TIMER_ENABLED
}
native TimerState:kz_get_timer_state(id)
native kz_get_cp_num(id)
native kz_get_last_cp(id, Float:value[3])

const RING = 20                 // the core's MAX_CACHE: checkpoints kept per player
const MAX_CHARS = 60
const NOTE_BYTES = 127          // a DHUD line holds 127 bytes
const AUTH_LEN = 35
const Float:SHOW_TIME = 4.0

new const SAY_COMMAND[] = "/cpnote"

// Per player: a mirror of the core's checkpoint ring, with a note per slot.
new Float:g_cpPos[MAX_PLAYERS + 1][RING][3]
new bool:g_cpValid[MAX_PLAYERS + 1][RING]
new g_note[MAX_PLAYERS + 1][RING][NOTE_BYTES + 1]
new g_next[MAX_PLAYERS + 1]               // slot of the next save (the core's CheckIndex)
new g_owner[MAX_PLAYERS + 1][AUTH_LEN]    // Steam id the ring belongs to
new g_pendingCount[MAX_PLAYERS + 1]       // kz_get_cp_num() when kz_cp_pre fired
new g_pendingUserId[MAX_PLAYERS + 1]
new g_shown[MAX_PLAYERS + 1]              // last restore: slot whose note was shown, or -1

new g_maxChars

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

    bind_pcvar_num(create_cvar("olds_cpnote_len", "60", FCVAR_NONE,
        "Longest checkpoint note, in characters (1-60).", true, 1.0, true, float(MAX_CHARS)), g_maxChars)
    AutoExecConfig(true, "olds_cpnote")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_cpnote_status", "cmd_status", ADMIN_CFG, "- every player's checkpoints and note lengths")
}

/* ---------- following the core's checkpoints ---------- */

clear_ring(id)
{
    for (new slot = 0; slot < RING; slot++)
    {
        g_cpValid[id][slot] = false
        g_note[id][slot][0] = EOS
    }
    g_next[id] = 0
    g_shown[id] = -1
}

public client_putinserver(id)
{
    // The core keeps checkpoints for the same Steam id in the same slot only.
    new auth[AUTH_LEN]
    get_user_authid(id, auth, charsmax(auth))
    if (!equal(auth, g_owner[id]) || !is_unique_auth(auth))
    {
        copy(g_owner[id], charsmax(g_owner[]), auth)
        clear_ring(id)
    }
    g_pendingUserId[id] = 0
}

/* STEAM_0:x:y / STEAM_1:x:y belong to one person; LAN ids, BOT and pending ids do not. */
bool:is_unique_auth(const auth[])
{
    return (equal(auth, "STEAM_0:", 8) || equal(auth, "STEAM_1:", 8)) && auth[8] && auth[9] == ':'
}

public client_disconnected(id)
{
    g_pendingUserId[id] = 0
}

public kz_timer_start_post(id)
{
    // A new run resets the core's checkpoints.
    if (1 <= id <= MaxClients)
        clear_ring(id)
}

public kz_cp_pre(id)
{
    if (!(1 <= id <= MaxClients) || !is_user_connected(id) || kz_get_timer_state(id) == TIMER_PAUSED)
        return
    // The core may still refuse the save (in the air, or another plugin blocks it): check next frame.
    g_pendingCount[id] = kz_get_cp_num(id)
    g_pendingUserId[id] = get_user_userid(id)
    RequestFrame("confirm_save", id)
}

public confirm_save(id)
{
    if (!g_pendingUserId[id] || !is_user_connected(id) || get_user_userid(id) != g_pendingUserId[id])
        return
    g_pendingUserId[id] = 0
    if (kz_get_cp_num(id) != g_pendingCount[id] + 1)
        return

    // Saved into the next slot of the ring: whatever was there is replaced, note included.
    new slot = g_next[id]
    kz_get_last_cp(id, g_cpPos[id][slot])
    g_cpValid[id][slot] = true
    g_note[id][slot][0] = EOS
    g_next[id] = (slot + 1) % RING
}

/*
 * The slot of the core's current checkpoint (the one gocheck restores), or -1.
 * Follows /stuck to an older slot, and drops every note when the core holds a
 * checkpoint this plugin never saw.
 */
current_slot(id)
{
    if (kz_get_timer_state(id) == TIMER_PAUSED || kz_get_cp_num(id) <= 0)
        return -1

    new Float:pos[3]
    kz_get_last_cp(id, pos)

    new expected = (g_next[id] - 1 + RING) % RING
    if (g_cpValid[id][expected] && same_pos(g_cpPos[id][expected], pos))
        return expected

    // Newest first, so a repeated position resolves to the latest save.
    for (new back = 2; back <= RING; back++)
    {
        new slot = (g_next[id] - back + RING) % RING
        if (g_cpValid[id][slot] && same_pos(g_cpPos[id][slot], pos))
        {
            g_next[id] = (slot + 1) % RING
            return slot
        }
    }

    clear_ring(id)
    return -1
}

bool:same_pos(const Float:a[3], const Float:b[3])
{
    return a[0] == b[0] && a[1] == b[1] && a[2] == b[2]
}

public kz_tp_post(id)
{
    if (!(1 <= id <= MaxClients) || !is_user_connected(id))
        return

    new slot = current_slot(id)
    g_shown[id] = -1
    if (slot == -1 || !g_note[id][slot][0])
        return // no note: nothing on screen

    g_shown[id] = slot
    set_dhudmessage(255, 210, 90, -1.0, 0.72, 0, 0.0, SHOW_TIME, 0.1, 0.3)
    show_dhudmessage(id, "%s", g_note[id][slot])
}

/* ---------- /cpnote ---------- */

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 start = length
    while (text[start] == ' ')
        start++
    handle_note(id, text[start])
    return PLUGIN_HANDLED // notes are private
}

handle_note(id, const argument[])
{
    new slot = current_slot(id)
    if (slot == -1)
    {
        client_print_color(id, print_team_default, "^4[CP note]^1 %L", id,
            kz_get_timer_state(id) == TIMER_PAUSED ? "CPNOTE_PAUSED" : "CPNOTE_NO_CP")
        return
    }

    if (!argument[0])
    {
        if (g_note[id][slot][0])
            client_print_color(id, print_team_default, "^4[CP note]^1 %L ^3%s", id, "CPNOTE_CURRENT", g_note[id][slot])
        else
            client_print_color(id, print_team_default, "^4[CP note]^1 %L", id, "CPNOTE_USAGE", g_maxChars)
        return
    }

    if (equali(argument, "clear"))
    {
        g_note[id][slot][0] = EOS
        client_print_color(id, print_team_default, "^4[CP note]^1 %L", id, "CPNOTE_CLEARED")
        return
    }

    new bool:cut = clean_note(argument, g_note[id][slot], g_maxChars)
    if (!g_note[id][slot][0])
    {
        client_print_color(id, print_team_default, "^4[CP note]^1 %L", id, "CPNOTE_USAGE", g_maxChars)
        return
    }
    client_print_color(id, print_team_default, "^4[CP note]^1 %L", id, cut ? "CPNOTE_SAVED_CUT" : "CPNOTE_SAVED",
        g_maxChars)
}

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

        new bool:lead = (byte & 0xC0) != 0x80
        if (lead)
        {
            // The size of the character this byte starts.
            new size = 1
            if (byte >= 0xF0)
                size = 4
            else if (byte >= 0xE0)
                size = 3
            else if (byte >= 0xC0)
                size = 2
            if (chars == maxChars || length + size > NOTE_BYTES)
            {
                cut = true
                break
            }
            chars++
        }
        else if (length == NOTE_BYTES)
        {
            cut = true
            break
        }
        note[length++] = byte
    }
    note[length] = EOS
    trim(note)
    return cut
}

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

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

    console_print(id, "[CP note] notes up to %d characters", g_maxChars)
    new players[MAX_PLAYERS], count, name[MAX_NAME_LENGTH], line[192]
    get_players(players, count, "h")
    for (new i = 0; i < count; i++)
    {
        new player = players[i], saved, length
        for (new slot = 0; slot < RING; slot++)
        {
            if (!g_cpValid[player][slot])
                continue
            saved++
            if (g_note[player][slot][0])
                length += formatex(line[length], charsmax(line) - length, " %d:%d", slot,
                    strlen(g_note[player][slot]))
        }
        get_user_name(player, name, charsmax(name))
        new slot = current_slot(player)
        console_print(id, "[CP note] %s: %d checkpoint(s), core count %d, current slot %d, last restore showed %d; notes (slot:bytes):%s",
            name, saved, kz_get_cp_num(player), slot, g_shown[player], length ? line : " none")
    }
    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.