Skip to content
View in the app

A better way to browse. Learn more.

Learn more

jail-clock/addons/amxmodx/scripting/olds_jailclock.sma

olds_jailclock.sma
267 lines · 8.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Jail Clock (olds.ro workshop card AMX-048)
 *
 * Surf: on maps with a jail whose door opens on a fixed cycle, players inside
 * the jail see a private estimate of the next opening:
 *     Jail door in 12 s (+/- 2)
 * Nothing is shown outside the jail box, before the cycle has been seen once,
 * or on maps without a configuration.
 *
 * Configuration: addons/amxmodx/configs/olds_jailclock/<map>.ini
 *     door  "targetname"                 the jail door (func_door or func_door_rotating)
 *     cycle 30                           seconds from one opening to the next (5-600)
 *     box   x1 y1 z1  x2 y2 z2           the jail; more box lines add more boxes (up to 8)
 * A map without the file, or with a missing or invalid line, stays idle and
 * logs one notice.
 *
 * Timing: the plugin never guesses the first opening. Ham_Use on the door
 * (the map firing it) starts or resyncs the cycle: the next opening is
 * expected cycle seconds later. When the door fires the estimate is cleared
 * at once; if the door entity disappears, the clock stops until it is used
 * again. The shown estimate is rounded to whole seconds and widened by
 * olds_jailclock_slop seconds either way; it stops at 0.
 *
 * Cvar: olds_jailclock_slop 2 (0-10). Command: olds_jailclock_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <reapi>

#define PLUGIN  "Jail Clock"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_BOXES = 8
const MAX_DOORS = 16
const TASK_TICK = 4811

new g_doorName[64]
new g_cycle
new Float:g_min[MAX_BOXES][3]
new Float:g_max[MAX_BOXES][3]
new g_boxes
new bool:g_ready                  // a valid configuration was loaded
new g_door[MAX_DOORS]
new g_doors

new Float:g_nextOpen               // 0.0 = no cycle seen yet
new Float:g_lastUse
new g_slop

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

    bind_pcvar_num(create_cvar("olds_jailclock_slop", "2", FCVAR_NONE,
        "Seconds the door estimate is widened either way (0-10).", true, 0.0, true, 10.0), g_slop)
    AutoExecConfig(true, "olds_jailclock")

    register_concmd("olds_jailclock_status", "cmd_status", ADMIN_CFG, "- the jail door, cycle and box")

    load_config()
    if (!g_ready)
        return
    find_doors()
    RegisterHam(Ham_Use, "func_door", "on_door_use_post", true)
    RegisterHam(Ham_Use, "func_door_rotating", "on_door_use_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    set_task(1.0, "task_tick", TASK_TICK, _, _, "b")
}

/* ---------- configuration ---------- */

load_config()
{
    new map[64], path[160]
    get_mapname(map, charsmax(map))
    get_configsdir(path, charsmax(path))
    format(path, charsmax(path), "%s/olds_jailclock/%s.ini", path, map)

    new file = fopen(path, "rt")
    if (!file)
    {
        log_amx("Jail Clock: no %s, idle on this map", path)
        return
    }
    new line[256], word[16], value[7][64], number, bool:bad
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        new parts = parse(line, word, charsmax(word), value[0], charsmax(value[]), value[1], charsmax(value[]),
            value[2], charsmax(value[]), value[3], charsmax(value[]), value[4], charsmax(value[]),
            value[5], charsmax(value[]))
        if (equali(word, "door") && parts == 2 && value[0][0])
            copy(g_doorName, charsmax(g_doorName), value[0])
        else if (equali(word, "cycle") && parts == 2 && is_digits(value[0]))
            g_cycle = str_to_num(value[0])
        else if (equali(word, "box") && parts == 7 && g_boxes < MAX_BOXES && box_numbers(value))
        {
            for (new axis = 0; axis < 3; axis++)
            {
                new Float:a = str_to_float(value[axis]), Float:b = str_to_float(value[axis + 3])
                g_min[g_boxes][axis] = floatmin(a, b)
                g_max[g_boxes][axis] = floatmax(a, b)
            }
            g_boxes++
        }
        else
        {
            log_amx("Jail Clock: %s line %d not understood", path, number)
            bad = true
        }
    }
    fclose(file)

    if (bad || !g_doorName[0] || !(5 <= g_cycle <= 600) || !g_boxes)
    {
        log_amx("Jail Clock: %s needs door, cycle (5-600) and at least one box; idle on this map", path)
        return
    }
    g_ready = true
}

bool:is_digits(const text[])
{
    if (!text[0] || strlen(text) > 4)
        return false
    for (new i = 0; text[i]; i++)
        if (!isdigit(text[i]))
            return false
    return true
}

bool:box_numbers(const value[7][64])
{
    for (new v = 0; v < 6; v++)
    {
        new i = (value[v][0] == '-' || value[v][0] == '+') ? 1 : 0, digits
        for (; value[v][i]; i++)
        {
            if (isdigit(value[v][i]))
                digits++
            else if (value[v][i] != '.')
                return false
        }
        if (!digits)
            return false
    }
    return true
}

/* ---------- the door ---------- */

find_doors()
{
    g_doors = 0
    new entity = MaxClients
    while ((entity = rg_find_ent_by_class(entity, "func_door")) > 0 && g_doors < MAX_DOORS)
        if (is_our_door(entity))
            g_door[g_doors++] = entity
    entity = MaxClients
    while ((entity = rg_find_ent_by_class(entity, "func_door_rotating")) > 0 && g_doors < MAX_DOORS)
        if (is_our_door(entity))
            g_door[g_doors++] = entity
    if (!g_doors)
        log_amx("Jail Clock: no door named ^"%s^" on this map yet", g_doorName)
}

bool:is_our_door(entity)
{
    new name[64]
    get_entvar(entity, var_targetname, name, charsmax(name))
    return bool:equal(name, g_doorName)
}

bool:door_exists()
{
    for (new d = 0; d < g_doors; d++)
        if (!is_nullent(g_door[d]) && is_our_door(g_door[d]))
            return true
    return false
}

public on_door_use_post(entity, caller, activator, useType, Float:value)
{
    if (!g_ready || !is_our_door(entity))
        return HAM_IGNORED
    new Float:now = get_gametime()
    // Double doors fire together: one opening.
    if (now - g_lastUse < 0.5)
        return HAM_IGNORED
    g_lastUse = now
    if (!door_exists())
        find_doors()
    g_nextOpen = now + float(g_cycle)   // the old line fades within a second
    return HAM_IGNORED
}

public on_restart_round_pre()
{
    g_nextOpen = 0.0
    g_lastUse = 0.0
    return HC_CONTINUE
}

/* ---------- the estimate ---------- */

bool:in_jail(id)
{
    new Float:origin[3]
    get_entvar(id, var_origin, origin)
    for (new b = 0; b < g_boxes; b++)
        if (g_min[b][0] <= origin[0] <= g_max[b][0] && g_min[b][1] <= origin[1] <= g_max[b][1]
            && g_min[b][2] <= origin[2] <= g_max[b][2])
            return true
    return false
}

public task_tick()
{
    if (g_nextOpen == 0.0)
        return
    if (!door_exists())
    {
        g_nextOpen = 0.0          // the door is gone: stop until it is used again
        return
    }
    new left = max(floatround(g_nextOpen - get_gametime(), floatround_ceil), 0)
    new players[MAX_PLAYERS], count
    get_players_ex(players, count, GetPlayers_ExcludeDead | GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV)
    set_dhudmessage(255, 190, 80, -1.0, 0.80, 0, 0.0, 1.05, 0.0, 0.0)
    for (new i = 0; i < count; i++)
    {
        new id = players[i]
        if (!in_jail(id))
            continue
        if (g_slop)
            show_dhudmessage(id, "%L", id, "JAILCLOCK_HUD_SLOP", left, g_slop)
        else
            show_dhudmessage(id, "%L", id, "JAILCLOCK_HUD", left)
    }
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (!g_ready)
    {
        console_print(id, "[Jail Clock] idle: no valid configuration for this map")
        return PLUGIN_HANDLED
    }
    console_print(id, "[Jail Clock] door ^"%s^" (%d found, %s); cycle %d s; %d box(es); slop %d", g_doorName, g_doors,
        door_exists() ? "present" : "missing", g_cycle, g_boxes, g_slop)
    if (g_nextOpen == 0.0)
        console_print(id, "[Jail Clock] no cycle seen yet")
    else
        console_print(id, "[Jail Clock] next opening in %.1f s", floatmax(g_nextOpen - get_gametime(), 0.0))
    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.