Skip to content
View in the app

A better way to browse. Learn more.

Learn more

crown-tenure/addons/amxmodx/scripting/olds_crown.sma

olds_crown.sma
221 lines · 6.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Crown Tenure (olds.ro workshop card AMX-098)
 *
 * King of the Hill: times every reign (how long one player holds the crown
 * before losing it) and keeps the three longest of the map. /reigns shows
 * them at any time; they are posted to chat when the map ends (intermission)
 * and written to the AMXX log when the plugin ends.
 *     Longest reigns: 1. Alpha 90 s  2. Bravo 60 s  3. Charlie 45 s
 *
 *   say /reigns    the three longest reigns so far on this map
 *
 * Adapter: King of the Hill cores have no common API, so the core, or a
 * small bridge, reports the crown through two natives (library
 * "olds_crown", include/olds_crown.inc):
 *     olds_crown_assigned(id)   id now wears the crown (the previous king's
 *                               reign closes); a repeat for the current king
 *                               changes nothing
 *     olds_crown_reset()        nobody wears the crown (the reign closes)
 * A reign also closes when the king leaves the server. Each reign closes
 * exactly once; time is measured with the game clock, independent of kills.
 * Reigns shorter than olds_crown_minsec are never listed.
 *
 * Cvar: olds_crown_minsec 10 (0-600).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>

#define PLUGIN  "Crown Tenure"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const TOP = 3

new g_king                           // 0 = nobody
new Float:g_since
new g_topSeconds[TOP]
new g_topName[TOP][MAX_NAME_LENGTH]
new g_topAuth[TOP][MAX_AUTHID_LENGTH]
new bool:g_posted
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_minSec

public plugin_natives()
{
    register_library("olds_crown")
    register_native("olds_crown_assigned", "native_assigned")
    register_native("olds_crown_reset", "native_reset")
}

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

    bind_pcvar_num(create_cvar("olds_crown_minsec", "10", FCVAR_NONE,
        "Shortest reign in seconds that can be listed (0-600).", true, 0.0, true, 600.0), g_minSec)
    AutoExecConfig(true, "olds_crown")

    register_clcmd("say /reigns", "cmd_reigns")
    register_clcmd("say_team /reigns", "cmd_reigns")
    register_event("30", "on_intermission", "a")
}

/* ---------- natives for the core ---------- */

public native_assigned(plugin, params)
{
    new id = get_param(1)
    if (!(1 <= id <= MaxClients) || !is_user_connected(id))
    {
        log_error(AMX_ERR_NATIVE, "olds_crown_assigned: invalid player %d", id)
        return 0
    }
    if (id == g_king)
        return 1                     // the same king again: the reign goes on
    close_reign()
    g_king = id
    g_since = get_gametime()
    return 1
}

public native_reset(plugin, params)
{
    close_reign()
    return 1
}

/* ---------- reigns ---------- */

close_reign()
{
    if (!g_king)
        return
    new king = g_king
    g_king = 0
    new seconds = floatround(get_gametime() - g_since, floatround_floor)
    if (seconds < g_minSec || !is_user_connected(king))
        return
    new name[MAX_NAME_LENGTH], auth[MAX_AUTHID_LENGTH]
    get_user_name(king, name, charsmax(name))
    get_user_authid(king, auth, charsmax(auth))
    insert(seconds, name, auth)
}

/* Keeps the three longest, longest first. */
insert(seconds, const name[], const auth[])
{
    new place = TOP
    for (new i = 0; i < TOP; i++)
    {
        if (seconds > g_topSeconds[i])
        {
            place = i
            break
        }
    }
    if (place == TOP)
        return
    for (new i = TOP - 1; i > place; i--)
    {
        g_topSeconds[i] = g_topSeconds[i - 1]
        copy(g_topName[i], charsmax(g_topName[]), g_topName[i - 1])
        copy(g_topAuth[i], charsmax(g_topAuth[]), g_topAuth[i - 1])
    }
    g_topSeconds[place] = seconds
    copy(g_topName[place], charsmax(g_topName[]), name)
    copy(g_topAuth[place], charsmax(g_topAuth[]), auth)
}

/* The table including the reign still running, without closing it. */
table(output[], length, id)
{
    new seconds[TOP], names[TOP][MAX_NAME_LENGTH]
    for (new i = 0; i < TOP; i++)
    {
        seconds[i] = g_topSeconds[i]
        copy(names[i], charsmax(names[]), g_topName[i])
    }
    if (g_king && is_user_connected(g_king))
    {
        new running = floatround(get_gametime() - g_since, floatround_floor)
        if (running >= g_minSec)
        {
            for (new i = 0; i < TOP; i++)
            {
                if (running <= seconds[i])
                    continue
                for (new j = TOP - 1; j > i; j--)
                {
                    seconds[j] = seconds[j - 1]
                    copy(names[j], charsmax(names[]), names[j - 1])
                }
                seconds[i] = running
                get_user_name(g_king, names[i], charsmax(names[]))
                break
            }
        }
    }
    new written, count
    output[0] = EOS
    for (new i = 0; i < TOP; i++)
    {
        if (!seconds[i])
            break
        written += formatex(output[written], length - written, "%s%d. %s %d s", count ? "  " : "", i + 1, names[i],
            seconds[i])
        count++
    }
    if (!count)
        formatex(output, length, "%L", id, "CROWN_NONE")
}

/* ---------- output ---------- */

public cmd_reigns(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED
    g_nextUse[id] = now + 1.0
    new text[160]
    table(text, charsmax(text), id)
    client_print_color(id, print_team_default, "^4[Crown]^1 %L", id, "CROWN_TABLE", text)
    return PLUGIN_HANDLED
}

public on_intermission()
{
    if (g_posted)
        return
    g_posted = true
    close_reign()
    new text[160]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p) || is_user_bot(p))
            continue
        table(text, charsmax(text), p)
        client_print_color(p, print_team_default, "^4[Crown]^1 %L", p, "CROWN_TABLE", text)
    }
}

public client_disconnected(id)
{
    if (id == g_king)
        close_reign()
    g_nextUse[id] = 0.0
}

public plugin_end()
{
    close_reign()
    for (new i = 0; i < TOP && g_topSeconds[i]; i++)
        log_amx("Longest reign %d: %s <%s> %d s", i + 1, g_topName[i], g_topAuth[i], g_topSeconds[i])
}

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.