/*
* Small-Server Rebound (olds.ro workshop card AMX-088)
*
* Map voting: while a server runs low (small maps), real players may come
* back. Once more than olds_rebound_players real players have been on the
* server for olds_rebound_secs seconds in a row, one normal-pool map that
* fits the new player count is nominated for the next map vote, and the
* players are told.
*
* Needs: AMX Mod X 1.10 and Mistrick's Map Manager (library
* "map_manager_core", github.com/Mistrick/MapManagerModular):
* - pool: the core's map list (mapm_maplist_loaded): every map with its
* min and max players; eligible = fits the current real-player count,
* is not the current map and exists on the server;
* - nomination: in the upcoming-vote callback (mapm_prepare_votelist) the
* map is checked again and pushed with mapm_push_map_to_votelist; the
* core refuses a map already in the vote, so it is never there twice;
* - a vote in progress (is_vote_started) stops the count.
*
* Real players: no bots, no HLTV. The count starts over when the number
* drops to the threshold or below, or a vote starts; a pending nomination is
* dropped if the number drops before the vote. One rebound nomination per
* map.
*
* Cvars: olds_rebound_players 12 (1-31), olds_rebound_secs 120 (5-1800),
* olds_rebound_count_bots 0 (1 counts bots too: only for trying the plugin
* on an empty test server). Command: olds_rebound_status (ADMIN_MAP).
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <amxmisc>
#define PLUGIN "Small-Server Rebound"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
/* Map Manager core API (map_manager.inc), declared here so the plugin compiles on its own. */
#pragma reqlib "map_manager_core"
native mapm_get_map_index(const map[])
native mapm_push_map_to_votelist(map[], type = 1, ignore_check = 0)
native bool:is_vote_started()
const MAPNAME_LENGTH = 32
const MAX_POOL = 512
const PUSH_BY_NATIVE = 1
const PUSH_SUCCESS = 1
const TASK_COUNT = 8811
/* MapStruct of map_manager_consts.inc: name, min players, max players, priority. */
enum _:PoolMap
{
PoolName[MAPNAME_LENGTH],
PoolMin,
PoolMax,
PoolPriority
}
new g_pool[MAX_POOL][MAPNAME_LENGTH]
new g_min[MAX_POOL]
new g_max[MAX_POOL]
new g_poolSize
new g_above // seconds above the threshold in a row
new g_pending[MAPNAME_LENGTH] // nominated, waiting for the vote
new bool:g_done // this map's rebound is used
new g_currentMap[MAPNAME_LENGTH]
new g_threshold
new g_secs
new g_countBots
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_rebound.txt")
bind_pcvar_num(create_cvar("olds_rebound_players", "12", FCVAR_NONE,
"Real players the server must exceed (1-31).", true, 1.0, true, 31.0), g_threshold)
bind_pcvar_num(create_cvar("olds_rebound_secs", "120", FCVAR_NONE,
"Seconds above the threshold in a row before the nomination (5-1800).", true, 5.0, true, 1800.0), g_secs)
bind_pcvar_num(create_cvar("olds_rebound_count_bots", "0", FCVAR_NONE,
"1 = count bots as players too (only for testing on an empty server).", true, 0.0, true, 1.0), g_countBots)
AutoExecConfig(true, "olds_rebound")
register_concmd("olds_rebound_status", "cmd_status", ADMIN_MAP, "- player count, timer and pending nomination")
get_mapname(g_currentMap, charsmax(g_currentMap))
set_task(1.0, "task_count", TASK_COUNT, _, _, "b")
}
/* ---------- the core ---------- */
/* The core loaded its map list: keep our own copy of the pool. */
public mapm_maplist_loaded(Array:maplist, const nextmap[])
{
g_poolSize = 0
if (maplist == Invalid_Array)
return
new size = ArraySize(maplist), info[PoolMap]
for (new i = 0; i < size && g_poolSize < MAX_POOL; i++)
{
ArrayGetArray(maplist, i, info)
copy(g_pool[g_poolSize], charsmax(g_pool[]), info[PoolName])
g_min[g_poolSize] = info[PoolMin]
g_max[g_poolSize] = info[PoolMax]
g_poolSize++
}
}
public mapm_maplist_unloaded()
{
g_poolSize = 0
g_pending[0] = EOS
}
/* The upcoming vote: push the pending map, checked once more. */
public mapm_prepare_votelist(type)
{
if (!g_pending[0])
return PLUGIN_CONTINUE
new map[MAPNAME_LENGTH]
copy(map, charsmax(map), g_pending)
g_pending[0] = EOS
new players = real_players()
if (players <= g_threshold || !eligible(map, players))
{
log_amx("rebound nomination %s dropped before the vote (%d real players)", map, players)
return PLUGIN_CONTINUE
}
new result = mapm_push_map_to_votelist(map, PUSH_BY_NATIVE)
if (result == PUSH_SUCCESS)
log_amx("rebound nomination %s added to the vote", map)
else
log_amx("rebound nomination %s not added (the core answered %d: full, already in the vote or blocked)", map,
result)
return PLUGIN_CONTINUE
}
public mapm_vote_started(type)
{
g_above = 0
}
/* ---------- counting ---------- */
real_players()
{
if (g_countBots)
return get_playersnum_ex(GetPlayers_ExcludeHLTV)
return get_playersnum_ex(GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV)
}
bool:eligible(const map[], players)
{
if (equali(map, g_currentMap) || !is_map_valid(map) || mapm_get_map_index(map) < 0)
return false
for (new i = 0; i < g_poolSize; i++)
if (equali(g_pool[i], map))
return g_min[i] <= players <= g_max[i]
return false
}
public task_count()
{
new players = real_players()
if (players <= g_threshold || is_vote_started())
{
g_above = 0
if (g_pending[0] && players <= g_threshold)
{
log_amx("rebound nomination %s cancelled: %d real players", g_pending, players)
announce("REBOUND_CANCELLED", g_pending)
g_pending[0] = EOS
}
return
}
if (g_done || g_pending[0])
return
if (++g_above < g_secs)
return
g_above = 0
new candidates[MAX_POOL], count
for (new i = 0; i < g_poolSize; i++)
if (eligible(g_pool[i], players))
candidates[count++] = i
if (!count)
{
log_amx("rebound: %d real players for %d s, but no normal-pool map fits", players, g_secs)
g_done = true
return
}
copy(g_pending, charsmax(g_pending), g_pool[candidates[random(count)]])
g_done = true
log_amx("rebound: %d real players for %d s, %s nominated for the next vote", players, g_secs, g_pending)
announce("REBOUND_NOMINATED", g_pending, players)
}
announce(const key[], any:...)
{
new pattern[160], message[192]
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id) || is_user_bot(id))
continue
formatex(pattern, charsmax(pattern), "%L", id, key)
vformat(message, charsmax(message), pattern, 2)
client_print_color(id, print_team_default, "^4[Maps]^1 %s", message)
}
}
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
console_print(id, "[Rebound] %d real player(s) (threshold %d%s); %d/%d s above; pending %s; used this map %s; pool %d map(s)",
real_players(), g_threshold, g_countBots ? ", bots counted" : "", g_above, g_secs, g_pending[0] ? g_pending : "-",
g_done ? "yes" : "no", g_poolSize)
return PLUGIN_HANDLED
}