Skip to content
View in the app

A better way to browse. Learn more.

Learn more

round-reset-petition/addons/amxmodx/scripting/olds_resetpet.sma

olds_resetpet.sma
383 lines · 10.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Round Reset Petition (olds.ro)
 * 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. It is distributed WITHOUT ANY WARRANTY; see
 * <https://www.gnu.org/licenses/>.
 *
 * Surfers stuck in a jail region type /stuck. When enough of them ask
 * (olds_resetpet_pct of the living players), the round manager is asked once
 * to reset the round, but only when nobody has a timed run going.
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Round Reset Petition"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_BOXES  16
#define TASK_COUNT 5810

new g_cvPct, g_cvFallback
new g_fwRequest
new Float:g_min[MAX_BOXES][3], Float:g_max[MAX_BOXES][3], g_boxes
new g_file[128]
new bool:g_vote[MAX_PLAYERS + 1]
new bool:g_timer[MAX_PLAYERS + 1]
new bool:g_pending, bool:g_sent

public plugin_natives()
{
    register_library("olds_resetpet")
    register_native("olds_resetpet_timer", "native_timer")
    register_native("olds_resetpet_add_jail", "native_add_jail")
}

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

    g_cvPct = create_cvar("olds_resetpet_pct", "60", FCVAR_NONE, "Percent of the living players that must ask from inside a jail", true, 1.0, true, 100.0)
    g_cvFallback = create_cvar("olds_resetpet_fallback", "1", FCVAR_NONE, "1 = end the round as a draw when no round manager takes the request", true, 0.0, true, 1.0)
    AutoExecConfig(true, "olds_resetpet")

    register_clcmd("say /stuck", "cmd_stuck")
    register_clcmd("say_team /stuck", "cmd_stuck")
    register_concmd("amx_resetpet_jail", "cmd_jail", ADMIN_CFG, "add <x1 y1 z1 x2 y2 z2> | clear | list - jail boxes of this map")
    register_concmd("amx_resetpet_status", "cmd_status", ADMIN_KICK, "- petition state")

    g_fwRequest = CreateMultiForward("olds_resetpet_request", ET_STOP, FP_CELL, FP_CELL)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)

    new map[64]
    get_mapname(map, charsmax(map))
    get_configsdir(g_file, charsmax(g_file))
    format(g_file, charsmax(g_file), "%s/olds_resetpet/%s.ini", g_file, map)
    load_boxes()

    set_task(1.0, "task_count", TASK_COUNT, _, _, "b")
}

public plugin_end()
{
    DestroyForward(g_fwRequest)
}

public client_putinserver(id)
{
    g_vote[id] = false
    g_timer[id] = false
}

public client_disconnected(id)
{
    g_vote[id] = false
    g_timer[id] = false
}

public on_restart_round_pre()
{
    for (new id = 1; id <= MaxClients; id++)
        g_vote[id] = false
    g_pending = false
    g_sent = false
    return HC_CONTINUE
}

/* ---------- jail boxes ---------- */

load_boxes()
{
    g_boxes = 0
    new fp = fopen(g_file, "rt")
    if (!fp)
        return
    new line[160], parts[6][16], Float:a[3], Float:b[3]
    while (!feof(fp) && g_boxes < MAX_BOXES)
    {
        fgets(fp, line, charsmax(line))
        trim(line)
        if (!line[0] || line[0] == ';' || line[0] == '#')
            continue
        if (parse(line, parts[0], 15, parts[1], 15, parts[2], 15, parts[3], 15, parts[4], 15, parts[5], 15) != 6)
        {
            log_amx("Skipped a bad jail line in %s: %s", g_file, line)
            continue
        }
        for (new i = 0; i < 3; i++)
        {
            a[i] = str_to_float(parts[i])
            b[i] = str_to_float(parts[i + 3])
        }
        add_box(a, b)
    }
    fclose(fp)
}

bool:save_boxes()
{
    new dir[128]
    get_configsdir(dir, charsmax(dir))
    add(dir, charsmax(dir), "/olds_resetpet")
    if (!dir_exists(dir))
        mkdir(dir)
    new fp = fopen(g_file, "wt")
    if (!fp)
        return false
    fputs(fp, "; Round Reset Petition jail boxes: x1 y1 z1 x2 y2 z2 (two opposite corners)^n")
    for (new i = 0; i < g_boxes; i++)
        fprintf(fp, "%.0f %.0f %.0f %.0f %.0f %.0f^n", g_min[i][0], g_min[i][1], g_min[i][2], g_max[i][0], g_max[i][1], g_max[i][2])
    fclose(fp)
    return true
}

add_box(const Float:a[3], const Float:b[3])
{
    if (g_boxes >= MAX_BOXES)
        return 0
    for (new i = 0; i < 3; i++)
    {
        g_min[g_boxes][i] = floatmin(a[i], b[i])
        g_max[g_boxes][i] = floatmax(a[i], b[i])
    }
    return ++g_boxes
}

bool:is_trapped(id)
{
    if (!is_user_alive(id))
        return false
    new TeamName:team = get_member(id, m_iTeam)
    if (team != TEAM_TERRORIST && team != TEAM_CT)
        return false
    new Float:o[3]
    get_entvar(id, var_origin, o)
    for (new i = 0; i < g_boxes; i++)
    {
        if (o[0] >= g_min[i][0] && o[0] <= g_max[i][0] && o[1] >= g_min[i][1] && o[1] <= g_max[i][1]
            && o[2] >= g_min[i][2] && o[2] <= g_max[i][2])
            return true
    }
    return false
}

/* ---------- counting ---------- */

living_players()
{
    new count
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_alive(id))
            continue
        new TeamName:team = get_member(id, m_iTeam)
        if (team == TEAM_TERRORIST || team == TEAM_CT)
            count++
    }
    return count
}

needed_votes(living)
{
    // Round up: 60 percent of 5 is 3, of 4 is 3 (2.4 up).
    new needed = (living * get_pcvar_num(g_cvPct) + 99) / 100
    return max(needed, 1)
}

/* Drops the votes of players who left the jail or died, then counts. */
count_votes()
{
    new votes
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!g_vote[id])
            continue
        if (!is_user_connected(id) || !is_trapped(id))
        {
            g_vote[id] = false
            continue
        }
        votes++
    }
    return votes
}

running_timer()
{
    for (new id = 1; id <= MaxClients; id++)
    {
        if (g_timer[id] && is_user_connected(id))
            return id
    }
    return 0
}

public task_count()
{
    if (g_sent || !g_boxes)
        return
    evaluate(false)
}

evaluate(bool:announce)
{
    new votes = count_votes()
    new needed = needed_votes(living_players())
    if (announce)
        client_print_color(0, print_team_default, "%L", LANG_PLAYER, "RP_PROGRESS", votes, needed)
    if (votes < needed)
    {
        if (g_pending)
        {
            g_pending = false
            client_print_color(0, print_team_default, "%L", LANG_PLAYER, "RP_DROPPED", votes, needed)
        }
        return
    }
    new runner = running_timer()
    if (runner)
    {
        if (!g_pending)
        {
            g_pending = true
            new name[MAX_NAME_LENGTH]
            get_user_name(runner, name, charsmax(name))
            client_print_color(0, print_team_default, "%L", LANG_PLAYER, "RP_HELD", name)
        }
        return
    }
    send_request(votes, needed)
}

send_request(votes, needed)
{
    g_sent = true
    g_pending = false
    log_amx("Round reset petition accepted (%d/%d)", votes, needed)
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "RP_ACCEPTED", votes, needed)
    new result = PLUGIN_CONTINUE
    ExecuteForward(g_fwRequest, result, votes, needed)
    if (result >= PLUGIN_HANDLED)
        return
    if (get_pcvar_num(g_cvFallback) == 1 && !get_member_game(m_bRoundTerminating))
        rg_round_end(3.0, WINSTATUS_DRAW, ROUND_END_DRAW, "Round reset by petition", "rounddraw", false)
}

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

public cmd_stuck(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    if (g_sent)
    {
        client_print_color(id, print_team_default, "%L", id, "RP_ALREADY")
        return PLUGIN_HANDLED
    }
    if (!is_trapped(id))
    {
        client_print_color(id, print_team_default, "%L", id, "RP_NOT_TRAPPED")
        return PLUGIN_HANDLED
    }
    if (g_vote[id])
    {
        client_print_color(id, print_team_default, "%L", id, "RP_VOTED")
        return PLUGIN_HANDLED
    }
    g_vote[id] = true
    evaluate(true)
    return PLUGIN_HANDLED
}

public cmd_jail(id, level, cid)
{
    if (!cmd_access(id, level, cid, 2))
        return PLUGIN_HANDLED
    new action[8]
    read_argv(1, action, charsmax(action))
    if (equali(action, "add"))
    {
        if (read_argc() != 8)
        {
            console_print(id, "%L", id, "RP_JAIL_USAGE")
            return PLUGIN_HANDLED
        }
        new Float:a[3], Float:b[3]
        for (new i = 0; i < 3; i++)
        {
            a[i] = read_argv_float(2 + i)
            b[i] = read_argv_float(5 + i)
        }
        if (!add_box(a, b))
        {
            console_print(id, "%L", id, "RP_JAIL_FULL", MAX_BOXES)
            return PLUGIN_HANDLED
        }
        if (!save_boxes())
            console_print(id, "%L", id, "RP_JAIL_NOSAVE", g_file)
        console_print(id, "%L", id, "RP_JAIL_ADDED", g_boxes)
    }
    else if (equali(action, "clear"))
    {
        g_boxes = 0
        if (!save_boxes())
            console_print(id, "%L", id, "RP_JAIL_NOSAVE", g_file)
        console_print(id, "%L", id, "RP_JAIL_ADDED", g_boxes)
    }
    else if (equali(action, "list"))
    {
        for (new i = 0; i < g_boxes; i++)
            console_print(id, "#%d %.0f %.0f %.0f  %.0f %.0f %.0f", i + 1, g_min[i][0], g_min[i][1], g_min[i][2], g_max[i][0], g_max[i][1], g_max[i][2])
        console_print(id, "%L", id, "RP_JAIL_ADDED", g_boxes)
    }
    else
        console_print(id, "%L", id, "RP_JAIL_USAGE")
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new living = living_players(), trapped
    for (new p = 1; p <= MaxClients; p++)
    {
        if (is_trapped(p))
            trapped++
    }
    console_print(id, "%L", id, "RP_STATUS", living, trapped, count_votes(), needed_votes(living), running_timer(), g_pending, g_sent)
    return PLUGIN_HANDLED
}

/* ---------- natives ---------- */

public native_timer(plugin, params)
{
    if (params < 2)
        return 0
    new id = get_param(1)
    if (id < 1 || id > MaxClients || !is_user_connected(id))
    {
        log_error(AMX_ERR_NATIVE, "olds_resetpet_timer: invalid player %d", id)
        return 0
    }
    g_timer[id] = get_param(2) != 0
    return 1
}

public native_add_jail(plugin, params)
{
    if (params < 2)
        return 0
    new Float:a[3], Float:b[3]
    get_array_f(1, a, 3)
    get_array_f(2, b, 3)
    return add_box(a, b)
}

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.