Skip to content
View in the app

A better way to browse. Learn more.

Learn more

shelter-shift/addons/amxmodx/scripting/olds_shelter.sma

olds_shelter.sma
402 lines · 12.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Shelter Shift (olds.ro workshop card AMX-006)
 *
 * Times how long each named shelter on the map is held by survivors. A
 * shelter's streak runs while at least one living survivor is inside it and
 * ends when the last one leaves, dies or turns. At round end everyone sees
 * which shelter held out longest without a break (ties included), followed by
 * each shelter's longest stay.
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI. No particular infection core:
 *   - Survivor role: a living player on the survivor team (olds_shelter_team,
 *     CT by default). Zombie mods that turn a survivor move them to the other
 *     team (Zombie Swarm keeps zombies on T; Zombie Plague and Biohazard move
 *     an infected player to T), so a turned survivor stops counting at once.
 *   - Round callbacks: ReGameDLL's own. Timing starts when the freeze time ends
 *     (RG_CSGameRules_OnRoundFreezeEnd) and stops when the round is decided
 *     (RG_RoundEnd, or the game rules' m_bRoundTerminating flag for a round
 *     ended by a plugin through rg_round_end).
 *
 * Occupancy is re-evaluated every second by a repeating task, so stays are in
 * whole seconds: a stay is the number of consecutive checks that found the
 * shelter held. Stays shorter than olds_shelter_minsec never rank.
 *
 * Shelters: configs/olds_shelter/<map>.ini, one shelter per line:
 *   "Shelter name" x1 y1 z1 x2 y2 z2     a box given by two opposite corners
 *
 * Cvars: olds_shelter_minsec (0-600, default 10), olds_shelter_team (1 T, 2 CT; default 2).
 * Commands: olds_shelter_status and olds_shelter_where (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Shelter Shift"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_SHELTERS = 32
const NAME_LEN = 32
const TASK_TICK = 4127
const Float:TICK = 1.0
const RANKED_LINES = 5

// Shelters of the current map.
new g_name[MAX_SHELTERS][NAME_LEN]
new Float:g_min[MAX_SHELTERS][3]
new Float:g_max[MAX_SHELTERS][3]
new g_shelterCount
new g_file[PLATFORM_MAX_PATH]

// This round.
new bool:g_live
new g_occupants[MAX_SHELTERS]
new bool:g_held[MAX_SHELTERS]
new g_streakStart[MAX_SHELTERS]   // first check that found the shelter held
new g_lastHeld[MAX_SHELTERS]      // latest check that found it held
new g_best[MAX_SHELTERS]          // longest finished stay, in seconds
new g_second                      // seconds since the freeze time ended

new g_minSeconds
new g_survivorTeam
new g_map[32]
new g_hudSync

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

    bind_pcvar_num(create_cvar("olds_shelter_minsec", "10", FCVAR_NONE,
        "Stays shorter than this many seconds never rank.", true, 0.0, true, 600.0), g_minSeconds)
    bind_pcvar_num(create_cvar("olds_shelter_team", "2", FCVAR_NONE,
        "Survivor team: 1 terrorists, 2 counter-terrorists.", true, 1.0, true, 2.0), g_survivorTeam)
    AutoExecConfig(true, "olds_shelter")

    register_concmd("olds_shelter_status", "cmd_status", ADMIN_CFG, "- shelters, occupants and this round's stays")
    register_concmd("olds_shelter_where", "cmd_where", ADMIN_CFG, "- your position and the shelters you stand in")

    RegisterHookChain(RG_CSGameRules_OnRoundFreezeEnd, "on_freeze_end_post", true)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)

    g_hudSync = CreateHudSyncObj()
    get_mapname(g_map, charsmax(g_map))
    new dir[PLATFORM_MAX_PATH]
    get_configsdir(dir, charsmax(dir))
    formatex(g_file, charsmax(g_file), "%s/olds_shelter/%s.ini", dir, g_map)
}

public plugin_cfg()
{
    load_shelters()
    if (g_shelterCount)
        set_task(TICK, "task_tick", TASK_TICK, _, _, "b")
}

/* ---------- shelters ---------- */

load_shelters()
{
    g_shelterCount = 0
    new file = fopen(g_file, "rt")
    if (!file)
    {
        log_amx("No shelters for %s (%s): nothing is timed on this map.", g_map, g_file)
        return
    }

    new line[256], lineNumber, name[NAME_LEN + 8], coord[6][16]
    while (fgets(file, line, charsmax(line)))
    {
        lineNumber++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue

        if (parse(line, name, charsmax(name), coord[0], charsmax(coord[]), coord[1], charsmax(coord[]),
            coord[2], charsmax(coord[]), coord[3], charsmax(coord[]), coord[4], charsmax(coord[]),
            coord[5], charsmax(coord[])) != 7)
        {
            log_amx("%s line %d: expected a name and six numbers; skipped.", g_file, lineNumber)
            continue
        }
        if (!valid_name(name))
        {
            log_amx("%s line %d: the shelter name must be 1-%d printable characters; skipped.", g_file,
                lineNumber, NAME_LEN - 1)
            continue
        }
        new Float:value[6], bool:numbers = true
        for (new i = 0; i < 6 && numbers; i++)
        {
            numbers = is_number_text(coord[i])
            value[i] = str_to_float(coord[i])
        }
        if (!numbers)
        {
            log_amx("%s line %d: ^"%s^" has a corner that is not a number; skipped.", g_file, lineNumber, name)
            continue
        }
        if (find_shelter(name) != -1)
        {
            log_amx("%s line %d: shelter ^"%s^" is listed twice; skipped.", g_file, lineNumber, name)
            continue
        }
        if (g_shelterCount == MAX_SHELTERS)
        {
            log_amx("%s: more than %d shelters; the rest are ignored.", g_file, MAX_SHELTERS)
            break
        }

        new shelter = g_shelterCount++
        copy(g_name[shelter], charsmax(g_name[]), name)
        for (new axis = 0; axis < 3; axis++)
        {
            g_min[shelter][axis] = floatmin(value[axis], value[axis + 3])
            g_max[shelter][axis] = floatmax(value[axis], value[axis + 3])
        }
    }
    fclose(file)
    log_amx("%d shelter(s) loaded for %s.", g_shelterCount, g_map)
}

bool:valid_name(const name[])
{
    new length = strlen(name)
    if (length < 1 || length >= NAME_LEN)
        return false
    for (new i = 0; i < length; i++)
    {
        // No control characters and no chat colour codes.
        if ((name[i] & 0xFF) < ' ' || name[i] == '^^')
            return false
    }
    return true
}

bool:is_number_text(const text[])
{
    new i, digits, bool:point
    if (text[i] == '-' || text[i] == '+')
        i++
    for (; text[i]; i++)
    {
        if (isdigit(text[i]))
            digits++
        else if (text[i] == '.' && !point)
            point = true
        else
            return false
    }
    return digits > 0
}

find_shelter(const name[])
{
    for (new i = 0; i < g_shelterCount; i++)
    {
        if (equali(g_name[i], name))
            return i
    }
    return -1
}

bool:inside(shelter, const Float:origin[3])
{
    for (new axis = 0; axis < 3; axis++)
    {
        if (origin[axis] < g_min[shelter][axis] || origin[axis] > g_max[shelter][axis])
            return false
    }
    return true
}

/* ---------- the round ---------- */

public on_restart_round_pre()
{
    // A round ended without the RoundEnd hook still reports before the reset.
    finish_round()
    for (new i = 0; i < MAX_SHELTERS; i++)
    {
        g_occupants[i] = 0
        g_held[i] = false
        g_best[i] = 0
    }
    g_second = 0
    return HC_CONTINUE
}

public on_freeze_end_post()
{
    if (g_shelterCount)
        g_live = true
    return HC_CONTINUE
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    if (get_member_game(m_bRoundTerminating))
        finish_round()
    return HC_CONTINUE
}

public task_tick()
{
    if (!g_live)
        return
    if (get_member_game(m_bRoundTerminating))
    {
        finish_round()
        return
    }

    g_second++

    new players[MAX_PLAYERS], count
    get_players(players, count, "ae", g_survivorTeam == 1 ? "TERRORIST" : "CT")

    new Float:origin[3]
    arrayset(g_occupants, 0, sizeof g_occupants)
    for (new i = 0; i < count; i++)
    {
        get_entvar(players[i], var_origin, origin)
        for (new shelter = 0; shelter < g_shelterCount; shelter++)
        {
            if (inside(shelter, origin))
                g_occupants[shelter]++
        }
    }

    for (new shelter = 0; shelter < g_shelterCount; shelter++)
    {
        if (g_occupants[shelter])
        {
            if (!g_held[shelter])
            {
                g_held[shelter] = true
                g_streakStart[shelter] = g_second
            }
            g_lastHeld[shelter] = g_second
        }
        else if (g_held[shelter])
            end_streak(shelter)
    }
}

/* Ends a running streak at the last check that found the shelter held. */
end_streak(shelter)
{
    g_held[shelter] = false
    g_best[shelter] = max(g_best[shelter], streak_length(shelter))
}

streak_length(shelter)
{
    return g_lastHeld[shelter] - g_streakStart[shelter] + 1
}

finish_round()
{
    if (!g_live)
        return
    g_live = false

    for (new shelter = 0; shelter < g_shelterCount; shelter++)
    {
        if (g_held[shelter])
            end_streak(shelter) // still held when the round was decided
    }
    announce()
}

/* ---------- the round-end text ---------- */

announce()
{
    // Shelters that ranked, longest first (ties keep the config order).
    new order[MAX_SHELTERS], ranked
    for (new shelter = 0; shelter < g_shelterCount; shelter++)
    {
        if (g_best[shelter] < max(g_minSeconds, 1))
            continue
        new slot = ranked++
        while (slot > 0 && g_best[order[slot - 1]] < g_best[shelter])
        {
            order[slot] = order[slot - 1]
            slot--
        }
        order[slot] = shelter
    }

    if (!ranked)
    {
        client_print(0, print_chat, "[Shelter] %L", LANG_PLAYER, "SHELTER_NONE", max(g_minSeconds, 1))
        log_amx("Round on %s: no shelter held for %d s.", g_map, max(g_minSeconds, 1))
        return
    }

    // Winners: every shelter tied with the longest stay.
    new top = g_best[order[0]], names[MAX_SHELTERS * (NAME_LEN + 2)], length, winners
    for (new i = 0; i < ranked && g_best[order[i]] == top; i++)
    {
        if (winners++)
            length += copy(names[length], charsmax(names) - length, ", ")
        length += copy(names[length], charsmax(names) - length, g_name[order[i]])
    }

    client_print(0, print_chat, "[Shelter] %L", LANG_PLAYER, winners > 1 ? "SHELTER_WIN_TIE" : "SHELTER_WIN",
        names, top)
    set_hudmessage(80, 200, 120, -1.0, 0.25, 0, 0.0, 6.0, 0.2, 0.5, -1)
    ShowSyncHudMsg(0, g_hudSync, "%L", LANG_PLAYER, winners > 1 ? "SHELTER_WIN_TIE" : "SHELTER_WIN", names, top)

    for (new i = 0; i < min(ranked, RANKED_LINES); i++)
        client_print(0, print_chat, "[Shelter] %d. %s - %d s", i + 1, g_name[order[i]], g_best[order[i]])

    log_amx("Round on %s: longest shelter stay %d s by %s (%d ranked).", g_map, top, names, ranked)
}

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

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

    console_print(id, "[Shelter] %s: %d shelter(s); %s, second %d; minimum stay %d s; survivors are %s",
        g_map, g_shelterCount, g_live ? "timing" : "not timing", g_second, g_minSeconds,
        g_survivorTeam == 1 ? "terrorists" : "counter-terrorists")
    for (new shelter = 0; shelter < g_shelterCount; shelter++)
    {
        new running = g_held[shelter] ? streak_length(shelter) : 0
        console_print(id, "[Shelter] %s: %d inside, %s %d s, best %d s", g_name[shelter], g_occupants[shelter],
            g_held[shelter] ? "held for" : "empty,", running, g_best[shelter])
    }
    return PLUGIN_HANDLED
}

public cmd_where(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (!id || !is_user_alive(id))
    {
        console_print(id, "[Shelter] Only a living player in game has a position.")
        return PLUGIN_HANDLED
    }

    new Float:origin[3]
    get_entvar(id, var_origin, origin)
    console_print(id, "[Shelter] You are at %d %d %d", floatround(origin[0]), floatround(origin[1]),
        floatround(origin[2]))
    for (new shelter = 0; shelter < g_shelterCount; shelter++)
    {
        if (inside(shelter, origin))
            console_print(id, "[Shelter] inside: %s", g_name[shelter])
    }
    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.