Skip to content
View in the app

A better way to browse. Learn more.

Learn more

swap-preview/addons/amxmodx/scripting/olds_swapview.sma

olds_swapview.sma
179 lines · 5.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Swap Preview (olds.ro workshop card AMX-025)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details: <https://www.gnu.org/licenses/>.
 *
 * When the match manager schedules the halftime side change (forward
 * olds_match_halftime_scheduled, see olds_match.inc), every player on a team sees for
 * olds_swapview_secs seconds a DHUD line naming the side they play next, in that side's
 * colour (Terrorists read "next: CT", CTs read "next: T"). When the manager cancels or
 * restarts halftime (olds_match_halftime_cancelled) the preview is withdrawn. A player who
 * joins during the preview sees it once. The plugin only shows text: the manager swaps.
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>
#include <olds_match>

#define PLUGIN  "Swap Preview"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TASK_SLICE 2500
#define MAX_SEEN   64
#define KEY_LEN    40

new g_secs
new bool:g_active
new g_slicesLeft
new g_seen[MAX_SEEN][KEY_LEN], g_seenCount
new bool:g_watching[MAX_PLAYERS + 1]
new g_shows[MAX_PLAYERS + 1]

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_swapview.txt")
    bind_pcvar_num(create_cvar("olds_swapview_secs", "5", FCVAR_NONE, "Seconds the preview is shown", true, 1.0, true, 30.0), g_secs)
    register_concmd("amx_swapview_status", "cmd_status", ADMIN_KICK, "- shows the swap preview state")
}

/* ------------------------------------------------------------------ manager forwards */

public olds_match_halftime_scheduled(Float:delay)
{
    if (g_active)
        return
    g_active = true
    g_seenCount = 0
    g_slicesLeft = g_secs
    for (new id = 1; id <= MaxClients; id++)
    {
        g_watching[id] = false
        g_shows[id] = 0
        if (is_user_connected(id))
            start_watching(id)
    }
    log_amx("Halftime scheduled (in %.0f s): side preview shown for %d s", delay, g_secs)
    remove_task(TASK_SLICE)
    show_slice()
    if (g_secs > 1)
        set_task(1.0, "show_slice", TASK_SLICE, _, _, "a", g_secs - 1)
    set_task(float(g_secs), "end_preview", TASK_SLICE + 1)
}

public olds_match_halftime_cancelled()
{
    if (!g_active)
        return
    stop_preview()
    log_amx("Halftime cancelled: side preview withdrawn")
}

public end_preview()
{
    stop_preview()
}

stop_preview()
{
    g_active = false
    g_slicesLeft = 0
    remove_task(TASK_SLICE)
    remove_task(TASK_SLICE + 1)
    for (new id = 1; id <= MaxClients; id++)
        g_watching[id] = false
}

/* ------------------------------------------------------------------ who has seen it */

player_key(id, key[], len)
{
    get_user_authid(id, key, len)
    if (!equali(key, "STEAM_", 6) && !equali(key, "VALVE_", 6))
        get_user_name(id, key, len)
}

bool:has_seen(const key[])
{
    for (new i = 0; i < g_seenCount; i++)
        if (equal(g_seen[i], key))
            return true
    return false
}

/* Each identity sees one preview per scheduling, even after a reconnect. */
start_watching(id)
{
    if (is_user_hltv(id))
        return
    new key[KEY_LEN]
    player_key(id, key, charsmax(key))
    if (has_seen(key))
        return
    if (g_seenCount < MAX_SEEN)
        copy(g_seen[g_seenCount++], KEY_LEN - 1, key)
    g_watching[id] = true
    g_shows[id]++
}

public client_putinserver(id)
{
    g_watching[id] = false
    g_shows[id] = 0
    if (g_active)
        start_watching(id)
}

public client_disconnected(id)
{
    g_watching[id] = false
}

/* ------------------------------------------------------------------ the DHUD */

/* The preview is drawn in one-second slices, so a withdrawal clears it within a second. */
public show_slice()
{
    if (!g_active)
        return
    g_slicesLeft--
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!g_watching[id] || !is_user_connected(id))
            continue
        new TeamName:team = get_member(id, m_iTeam)
        if (team == TEAM_TERRORIST)
        {
            set_dhudmessage(80, 140, 255, -1.0, 0.25, 0, 0.0, 1.05, 0.0, 0.0)
            show_dhudmessage(id, "%L", id, "SV_NEXT_CT")
        }
        else if (team == TEAM_CT)
        {
            set_dhudmessage(255, 70, 60, -1.0, 0.25, 0, 0.0, 1.05, 0.0, 0.0)
            show_dhudmessage(id, "%L", id, "SV_NEXT_T")
        }
    }
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[SwapView] active %d, seconds left %d, identities shown %d", g_active, max(g_slicesLeft, 0), g_seenCount)
    new name[MAX_NAME_LENGTH]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        console_print(id, "[SwapView]   %s: team %d, watching %d, previews started %d", name, get_member(p, m_iTeam), g_watching[p], g_shows[p])
    }
    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.