/*
* Contested Hill Meter (olds.ro workshop card IDEA-86E191A05B0F3B87)
* 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/>.
*
* Every 0.5 s the living players of each team inside the hill are counted. The meter
* (-100 = Terrorists, +100 = CTs) moves olds_hillpush points per extra player toward the
* side with more players inside. A team alone on an uncontested hill scores as before.
* Once both teams stand on the hill, nobody scores until the meter reaches the end of the
* side that holds the hill (more players inside); then that side scores again. Points go
* through the core's olds_hill_add_score (olds_hill_api.inc) one per second of holding;
* without a core the plugin keeps its own score. The meter goes back to 0 every round.
*/
#include <amxmodx>
#include <amxmisc>
#include <reapi>
#include <olds_hill_api>
#define PLUGIN "Contested Hill Meter"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define TASK_TICK 86190
#define BAR_LEN 21
new g_push
new bool:g_noCore
new Float:g_boxMin[3], Float:g_boxMax[3], bool:g_haveBox
new g_meter
new bool:g_lock
new g_half[3] // half-seconds held, by side
new g_ownScore[3]
new g_inside[3]
new g_holder
new g_hud
public plugin_natives()
{
set_native_filter("native_filter")
}
public native_filter(const name[], index, trap)
{
if (!trap)
{
g_noCore = true
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_hillmeter.txt")
bind_pcvar_num(create_cvar("olds_hillpush", "4", FCVAR_NONE, "Meter points per extra player per 0.5 s", true, 1.0, true, 100.0), g_push)
AutoExecConfig(true, "olds_hillmeter")
register_concmd("amx_hillmeter_status", "cmd_status", ADMIN_KICK, "- shows the hill meter")
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_pre", false)
g_hud = CreateHudSyncObj()
}
public plugin_cfg()
{
if (g_noCore)
{
load_box()
log_amx("No King of the Hill core with olds_hill_add_score: the meter keeps its own score")
}
else
olds_hill_set_external(1)
set_task(0.5, "tick", TASK_TICK, _, _, "b")
}
public plugin_end()
{
if (!g_noCore)
olds_hill_set_external(0)
}
load_box()
{
new path[160]
get_configsdir(path, charsmax(path))
add(path, charsmax(path), "/olds_hill.ini")
new file = fopen(path, "rt")
if (!file)
{
log_amx("%s is missing: no hill box", path)
return
}
new map[32], line[160], part[7][16]
get_mapname(map, charsmax(map))
while (!feof(file) && !g_haveBox)
{
fgets(file, line, charsmax(line))
trim(line)
if (!line[0] || line[0] == ';' || line[0] == '/')
continue
/* "<map> x1 y1 z1 x2 y2 z2" */
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[]), part[6], charsmax(part[])) != 7)
{
log_amx("olds_hill.ini: expected ^"<map> x1 y1 z1 x2 y2 z2^", line skipped")
continue
}
if (!equali(part[0], map))
continue
new bool:ok = true
for (new k = 0; k < 3; k++)
{
if (!is_coord(part[1 + k]) || !is_coord(part[4 + k]))
{
ok = false
break
}
new Float:a = str_to_float(part[1 + k]), Float:b = str_to_float(part[4 + k])
g_boxMin[k] = floatmin(a, b)
g_boxMax[k] = floatmax(a, b)
}
if (ok)
g_haveBox = true
else
log_amx("olds_hill.ini: bad numbers for %s, line skipped", map)
}
fclose(file)
}
is_coord(const text[])
{
new i, digits
if (text[0] == '-' || text[0] == '+')
i = 1
for (; text[i]; i++)
{
if (isdigit(text[i]))
digits++
else if (text[i] != '.')
return 0
}
return digits > 0 && floatabs(str_to_float(text)) <= 16384.0
}
bool:get_zone(Float:mins[3], Float:maxs[3])
{
if (!g_noCore)
return olds_hill_get_zone(mins, maxs) != 0
if (!g_haveBox)
return false
mins = g_boxMin
maxs = g_boxMax
return true
}
public on_restart_pre()
{
g_meter = 0
g_lock = false
g_holder = 0
arrayset(g_half, 0, sizeof g_half)
arrayset(g_ownScore, 0, sizeof g_ownScore)
}
side_end(side)
{
if (side == OLDS_HILL_SIDE_CT)
return 100
return -100
}
public tick()
{
new Float:mins[3], Float:maxs[3]
g_inside[OLDS_HILL_SIDE_T] = 0
g_inside[OLDS_HILL_SIDE_CT] = 0
g_holder = 0
if (get_member_game(m_bFreezePeriod) || !get_zone(mins, maxs))
return
new Float:o[3]
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_alive(id))
continue
get_entvar(id, var_origin, o)
if (o[0] < mins[0] || o[0] > maxs[0] || o[1] < mins[1] || o[1] > maxs[1] || o[2] < mins[2] || o[2] > maxs[2])
continue
new TeamName:team = get_member(id, m_iTeam)
if (team == TEAM_TERRORIST)
g_inside[OLDS_HILL_SIDE_T]++
else if (team == TEAM_CT)
g_inside[OLDS_HILL_SIDE_CT]++
}
new t = g_inside[OLDS_HILL_SIDE_T], ct = g_inside[OLDS_HILL_SIDE_CT]
g_meter = clamp(g_meter + (ct - t) * g_push, -100, 100)
if (ct > t)
g_holder = OLDS_HILL_SIDE_CT
else if (t > ct)
g_holder = OLDS_HILL_SIDE_T
new bool:contested = t > 0 && ct > 0
if (g_holder && g_meter == side_end(g_holder))
g_lock = false
else if (contested)
g_lock = true
new bool:scores = g_holder != 0 && ((!g_lock && !contested) || g_meter == side_end(g_holder))
if (scores)
{
g_half[g_holder]++
if (g_half[g_holder] % 2 == 0)
give_point(g_holder)
}
show_meter(contested, scores)
}
give_point(side)
{
if (!g_noCore)
{
olds_hill_add_score(side, 1)
return
}
g_ownScore[side]++
}
show_meter(bool:contested, bool:scores)
{
new bar[BAR_LEN + 1], pos = (g_meter + 100) * (BAR_LEN - 1) / 200
for (new i = 0; i < BAR_LEN; i++)
bar[i] = '-'
bar[pos] = '|'
bar[BAR_LEN] = EOS
new red = 220, green = 220, blue = 220
if (g_meter <= -100)
{
green = 60
blue = 60
}
else if (g_meter >= 100)
{
red = 60
green = 120
}
set_hudmessage(red, green, blue, -1.0, 0.12, 0, 0.0, 0.6, 0.0, 0.0, -1)
new state_key[16]
if (contested && !scores)
copy(state_key, charsmax(state_key), "HM_CONTESTED")
else if (scores)
copy(state_key, charsmax(state_key), "HM_SCORING")
else
copy(state_key, charsmax(state_key), "HM_IDLE")
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id) || is_user_bot(id))
continue
if (g_noCore)
ShowSyncHudMsg(id, g_hud, "T [%s] CT^n%L^n%d : %d", bar, id, state_key, g_ownScore[OLDS_HILL_SIDE_T], g_ownScore[OLDS_HILL_SIDE_CT])
else
ShowSyncHudMsg(id, g_hud, "T [%s] CT^n%L", bar, id, state_key)
}
}
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
new Float:mins[3], Float:maxs[3]
new bool:zone = get_zone(mins, maxs)
console_print(id, "[HillMeter] core %d, zone %d, meter %d, locked %d, inside T %d CT %d, holder %d, own score T %d CT %d",
!g_noCore, zone, g_meter, g_lock, g_inside[OLDS_HILL_SIDE_T], g_inside[OLDS_HILL_SIDE_CT], g_holder,
g_ownScore[OLDS_HILL_SIDE_T], g_ownScore[OLDS_HILL_SIDE_CT])
return PLUGIN_HANDLED
}