Skip to content
View in the app

A better way to browse. Learn more.

Learn more

team-king-of-the-hill/addons/amxmodx/scripting/olds_hill.sma

olds_hill.sma
547 lines · 15.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Team King of the Hill (olds.ro)
 *
 * Teams fight over one marked box. Every olds_hill_tick seconds that only one
 * team has living players in the box, that team scores a point; the first
 * team to olds_hill_target points wins the round, and the hill scores start
 * again from zero.
 *
 * Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
 *
 * 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. You should have received a copy of the GNU General Public
 * License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Team King of the Hill"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_HILLS 8
#define TASK_TICK 9180
#define TASK_OUTLINE 9181

#define SIDE_NONE 0
#define SIDE_T 1
#define SIDE_CT 2

new const OUTLINE_SPRITE[] = "sprites/laserbeam.spr"
new const LEAD_SOUND[] = "events/task_complete"     // stock sound/events/task_complete.wav, played with spk

new Float:g_min[MAX_HILLS][3]
new Float:g_max[MAX_HILLS][3]
new g_hills
new g_active                      // index of the hill in play

new g_score[3]                    // by SIDE_*
new g_leader = SIDE_NONE
new g_halfDone[3]                 // highest 50 % step already announced, by side
new g_holder = SIDE_NONE          // who scored on the last tick, for the outline colour
new bool:g_contested
new bool:g_roundOver
new g_sprite
new g_hud

new g_target
new Float:g_tick
new g_shiftmove

public plugin_precache()
{
    g_sprite = precache_model(OUTLINE_SPRITE)
}

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

    bind_pcvar_num(create_cvar("olds_hill_target", "300", FCVAR_NONE,
        "Points a team needs to win", true, 5.0, true, 100000.0), g_target)
    new tick = create_cvar("olds_hill_tick", "1", FCVAR_NONE,
        "Seconds between scoring checks; a team alone on the hill scores 1 point each check", true, 0.2, true, 30.0)
    bind_pcvar_float(tick, g_tick)
    hook_cvar_change(tick, "on_tick_change")
    bind_pcvar_num(create_cvar("olds_hill_shiftmove", "0", FCVAR_NONE,
        "1 = the hill moves to the next saved box every round, 0 = always the first box", true, 0.0, true, 1.0), g_shiftmove)
    AutoExecConfig(true, "olds_hill")

    register_concmd("amx_hillset", "cmd_hillset", ADMIN_CFG, "[add] [half-size 32-1024] | <x1 y1 z1 x2 y2 z2> | clear - save the hill")
    register_concmd("amx_hill", "cmd_hill", ADMIN_CFG, "[reset] - hill boxes and scores")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    g_hud = CreateHudSyncObj()
}

public plugin_cfg()
{
    load_hills()
    start_tick(g_tick)
    set_task(1.0, "task_outline", TASK_OUTLINE, _, _, "b")
}

public on_tick_change(pcvar, const old[], const value[])
{
    start_tick(str_to_float(value))
}

start_tick(Float:seconds)
{
    remove_task(TASK_TICK)
    set_task(floatclamp(seconds, 0.2, 30.0), "task_tick", TASK_TICK, _, _, "b")
}

/* ---------- storage: configs/hill/<map>.ini, one box per line ---------- */

hill_path(path[], len)
{
    new map[64]
    get_configsdir(path, len)
    get_mapname(map, charsmax(map))
    format(path, len, "%s/hill/%s.ini", path, map)
}

load_hills()
{
    g_hills = 0
    g_active = 0
    new path[192]
    hill_path(path, charsmax(path))
    new file = fopen(path, "rt")
    if (!file)
    {
        server_print("[Hill] No hill for this map yet (%s): an admin sets one with amx_hillset.", path)
        return
    }
    new line[192], part[6][16], lineNo, Float:box[6]
    while (!feof(file) && fgets(file, line, charsmax(line)))
    {
        lineNo++
        trim(line)
        if (!line[0] || line[0] == ';' || line[0] == '#')
            continue
        if (parse(line, part[0], charsmax(part[]), part[1], charsmax(part[]), part[2], charsmax(part[]),
            part[3], charsmax(part[]), part[4], charsmax(part[]), part[5], charsmax(part[])) != 6
            || !numbers_to_box(part, box))
        {
            log_amx("[Hill] %s line %d skipped: expected x1 y1 z1 x2 y2 z2", path, lineNo)
            continue
        }
        if (g_hills >= MAX_HILLS)
        {
            log_amx("[Hill] %s: more than %d boxes, the rest are skipped", path, MAX_HILLS)
            break
        }
        store_box(g_hills++, box)
    }
    fclose(file)
    server_print("[Hill] %d hill box(es) loaded.", g_hills)
}

bool:numbers_to_box(const part[][], Float:box[6])
{
    for (new i = 0; i < 6; i++)
    {
        if (!is_number(part[i]))
            return false
        box[i] = str_to_float(part[i])
        if (floatabs(box[i]) > 16384.0)
            return false
    }
    return true
}

store_box(index, const Float:box[6])
{
    for (new axis = 0; axis < 3; axis++)
    {
        g_min[index][axis] = floatmin(box[axis], box[axis + 3])
        g_max[index][axis] = floatmax(box[axis], box[axis + 3])
    }
}

bool:save_hills()
{
    new path[192]
    get_configsdir(path, charsmax(path))
    add(path, charsmax(path), "/hill")
    if (!dir_exists(path) && mkdir(path) != 0)
        return false
    hill_path(path, charsmax(path))
    new file = fopen(path, "wt")
    if (!file)
        return false
    fprintf(file, "; Team King of the Hill boxes: x1 y1 z1 x2 y2 z2 (saved by amx_hillset)^n")
    for (new i = 0; i < g_hills; i++)
        fprintf(file, "%.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(file)
    return true
}

bool:is_number(const text[])
{
    new i, digits, dots
    if (text[0] == '-' || text[0] == '+')
        i++
    for (; text[i]; i++)
    {
        if (text[i] == '.')
        {
            if (++dots > 1)
                return false
        }
        else if ('0' <= text[i] <= '9')
            digits++
        else
            return false
    }
    return digits > 0
}

/* ---------- rounds ---------- */

public on_restart_round_post()
{
    g_roundOver = false
    if (get_member_game(m_bCompleteReset))
        reset_scores()
    if (g_shiftmove && g_hills > 1)
        g_active = (g_active + 1) % g_hills
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    g_roundOver = true
    return HC_CONTINUE
}

reset_scores()
{
    g_score[SIDE_T] = 0
    g_score[SIDE_CT] = 0
    g_halfDone[SIDE_T] = 0
    g_halfDone[SIDE_CT] = 0
    g_leader = SIDE_NONE
    g_holder = SIDE_NONE
    g_contested = false
}

/* ---------- scoring ---------- */

bool:in_hill(id)
{
    new Float:lo[3], Float:hi[3]
    get_entvar(id, var_absmin, lo)
    get_entvar(id, var_absmax, hi)
    for (new axis = 0; axis < 3; axis++)
    {
        if (hi[axis] < g_min[g_active][axis] || lo[axis] > g_max[g_active][axis])
            return false
    }
    return true
}

public task_tick()
{
    g_holder = SIDE_NONE
    g_contested = false
    if (!g_hills || g_roundOver || get_member_game(m_bFreezePeriod))
        return

    new players[MAX_PLAYERS], count, inside[3]
    get_players(players, count, "a")
    for (new i = 0; i < count; i++)
    {
        new id = players[i]
        if (!in_hill(id))
            continue
        switch (get_member(id, m_iTeam))
        {
            case TEAM_TERRORIST: inside[SIDE_T]++
            case TEAM_CT: inside[SIDE_CT]++
        }
    }

    if (inside[SIDE_T] && inside[SIDE_CT])
        g_contested = true
    else if (inside[SIDE_T])
        add_point(SIDE_T)
    else if (inside[SIDE_CT])
        add_point(SIDE_CT)
    show_scores()
}

add_point(side)
{
    g_holder = side
    g_score[side]++
    new other = side == SIDE_T ? SIDE_CT : SIDE_T

    if (g_score[side] > g_score[other] && g_leader != side)
    {
        g_leader = side
        tell_all("HILL_LEAD", side, g_score[side], g_score[other])
        play_lead_sound()
    }

    if (g_score[side] >= g_target)
    {
        win(side)
        return
    }
    new step = g_score[side] * 2 / g_target     // 1 once half the target is reached
    if (step > g_halfDone[side])
    {
        g_halfDone[side] = step
        tell_all("HILL_HALF", side, g_score[side], g_target)
    }
}

win(side)
{
    new other = side == SIDE_T ? SIDE_CT : SIDE_T
    tell_all("HILL_WIN", side, g_score[side], g_score[other])
    g_roundOver = true
    if (side == SIDE_T)
        rg_round_end(5.0, WINSTATUS_TERRORISTS, ROUND_TERRORISTS_WIN, "Terrorists hold the hill!", "default", true)
    else
        rg_round_end(5.0, WINSTATUS_CTS, ROUND_CTS_WIN, "Counter-Terrorists hold the hill!", "default", true)
    reset_scores()
}

team_name(side, out[], len)
{
    if (side == SIDE_T)
        copy(out, len, "Terrorists")
    else
        copy(out, len, "Counter-Terrorists")
}

tell_all(const key[], side, a, b)
{
    new team[24]
    team_name(side, team, charsmax(team))
    new players[MAX_PLAYERS], count
    get_players(players, count, "ch")
    for (new i = 0; i < count; i++)
        client_print_color(players[i], side == SIDE_T ? print_team_red : print_team_blue, "^4[Hill]^1 %L", players[i], key, team, a, b)
    server_print("[Hill] %L", LANG_SERVER, key, team, a, b)
}

play_lead_sound()
{
    new players[MAX_PLAYERS], count
    get_players(players, count, "ch")
    for (new i = 0; i < count; i++)
        client_cmd(players[i], "spk %s", LEAD_SOUND)
}

show_scores()
{
    new players[MAX_PLAYERS], count
    get_players(players, count, "ch")
    if (!count)
        return
    set_hudmessage(255, 255, 255, -1.0, 0.04, 0, 0.0, floatmax(g_tick, 1.0) + 0.2, 0.0, 0.0, -1)
    for (new i = 0; i < count; i++)
    {
        new id = players[i]
        if (g_contested)
            ShowSyncHudMsg(id, g_hud, "%L", id, "HILL_HUD_CONTESTED", g_score[SIDE_T], g_score[SIDE_CT], g_target)
        else
            ShowSyncHudMsg(id, g_hud, "%L", id, "HILL_HUD", g_score[SIDE_T], g_score[SIDE_CT], g_target)
    }
}

/* ---------- outline ---------- */

public task_outline()
{
    if (!g_hills)
        return
    new red = 230, green = 230, blue = 230
    if (g_contested)
        blue = 40
    else if (g_holder == SIDE_T)
    {
        green = 50
        blue = 50
    }
    else if (g_holder == SIDE_CT)
    {
        red = 50
        green = 110
    }
    new Float:lo[3], Float:hi[3]
    lo = g_min[g_active]
    hi = g_max[g_active]
    // bottom and top rectangles, then the four uprights
    for (new level = 0; level < 2; level++)
    {
        new Float:z = level ? hi[2] : lo[2] + 2.0
        beam(lo[0], lo[1], z, hi[0], lo[1], z, red, green, blue)
        beam(hi[0], lo[1], z, hi[0], hi[1], z, red, green, blue)
        beam(hi[0], hi[1], z, lo[0], hi[1], z, red, green, blue)
        beam(lo[0], hi[1], z, lo[0], lo[1], z, red, green, blue)
    }
    beam(lo[0], lo[1], lo[2], lo[0], lo[1], hi[2], red, green, blue)
    beam(hi[0], lo[1], lo[2], hi[0], lo[1], hi[2], red, green, blue)
    beam(hi[0], hi[1], lo[2], hi[0], hi[1], hi[2], red, green, blue)
    beam(lo[0], hi[1], lo[2], lo[0], hi[1], hi[2], red, green, blue)
}

beam(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2, red, green, blue)
{
    message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
    write_byte(TE_BEAMPOINTS)
    write_coord_f(x1)
    write_coord_f(y1)
    write_coord_f(z1)
    write_coord_f(x2)
    write_coord_f(y2)
    write_coord_f(z2)
    write_short(g_sprite)
    write_byte(0)       // start frame
    write_byte(0)       // frame rate
    write_byte(11)      // life, tenths of a second
    write_byte(6)       // width
    write_byte(0)       // noise
    write_byte(red)
    write_byte(green)
    write_byte(blue)
    write_byte(180)     // brightness
    write_byte(0)       // scroll speed
    message_end()
}

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

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

    new arg[16], argc = read_argc(), first = 1
    read_argv(1, arg, charsmax(arg))
    if (equali(arg, "clear"))
    {
        g_hills = 0
        g_active = 0
        new path[192]
        hill_path(path, charsmax(path))
        if (file_exists(path))
            delete_file(path)
        console_print(id, "[Hill] All hill boxes for this map removed.")
        return PLUGIN_HANDLED
    }
    new bool:append = false
    if (equali(arg, "add"))
    {
        append = true
        first = 2
    }

    new Float:box[6]
    if (argc - first == 6)
    {
        new part[6][16]
        for (new i = 0; i < 6; i++)
            read_argv(first + i, part[i], charsmax(part[]))
        if (!numbers_to_box(part, box))
        {
            console_print(id, "[Hill] Coordinates must be six numbers: x1 y1 z1 x2 y2 z2")
            return PLUGIN_HANDLED
        }
    }
    else if (argc - first <= 1)
    {
        if (!id || !is_user_alive(id))
        {
            console_print(id, "[Hill] Stand where the hill goes (alive), or give x1 y1 z1 x2 y2 z2.")
            return PLUGIN_HANDLED
        }
        new Float:half = 128.0
        if (argc - first == 1)
        {
            read_argv(first, arg, charsmax(arg))
            if (!is_number(arg))
            {
                console_print(id, "[Hill] The half-size must be a number from 32 to 1024.")
                return PLUGIN_HANDLED
            }
            half = floatclamp(str_to_float(arg), 32.0, 1024.0)
        }
        new Float:origin[3]
        get_entvar(id, var_origin, origin)
        box[0] = origin[0] - half
        box[1] = origin[1] - half
        box[2] = origin[2] - 40.0
        box[3] = origin[0] + half
        box[4] = origin[1] + half
        box[5] = origin[2] + 160.0
    }
    else
    {
        console_print(id, "[Hill] Usage: amx_hillset [add] [half-size] | amx_hillset [add] x1 y1 z1 x2 y2 z2 | amx_hillset clear")
        return PLUGIN_HANDLED
    }

    if (append)
    {
        if (g_hills >= MAX_HILLS)
        {
            console_print(id, "[Hill] This map already has %d boxes.", MAX_HILLS)
            return PLUGIN_HANDLED
        }
        store_box(g_hills++, box)
    }
    else
    {
        g_hills = 1
        g_active = 0
        store_box(0, box)
    }
    if (!save_hills())
    {
        console_print(id, "[Hill] Could not write configs/hill/; the box is used until the map changes.")
        return PLUGIN_HANDLED
    }
    new name[32]
    get_user_name(id, name, charsmax(name))
    log_amx("[Hill] %s saved hill box %d: %.0f %.0f %.0f %.0f %.0f %.0f", name, append ? g_hills : 1,
        box[0], box[1], box[2], box[3], box[4], box[5])
    console_print(id, "[Hill] Saved: %d hill box(es) on this map.", g_hills)
    return PLUGIN_HANDLED
}

public cmd_hill(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new arg[16]
    read_argv(1, arg, charsmax(arg))
    if (equali(arg, "reset"))
    {
        reset_scores()
        console_print(id, "[Hill] Scores reset.")
        return PLUGIN_HANDLED
    }
    console_print(id, "[Hill] %d box(es), active %d, target %d, tick %.1f s, shiftmove %d", g_hills, g_hills ? g_active + 1 : 0, g_target, g_tick, g_shiftmove)
    for (new i = 0; i < g_hills; 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, "  Score T %d, CT %d; leader %d; contested %d; round wins T %d, CT %d", g_score[SIDE_T], g_score[SIDE_CT], g_leader, g_contested,
        get_member_game(m_iNumTerroristWins), get_member_game(m_iNumCTWins))
    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.