/*
* Buddy Shield (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/>.
*
* Two humans agree to share one infection block (Zombie Plague 5.0): the
* first zombie attack on either of them is blocked in zp_fw_core_infect_pre,
* and the pair is used up.
*/
#include <amxmodx>
#include <amxmisc>
#define PLUGIN "Buddy Shield"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
/* Zombie Plague 5.0 API, declared here so the plugin compiles without ZP's includes. */
#pragma reqlib zp50_core
#pragma reqlib zp50_gamemodes
native zp_core_is_zombie(id)
native zp_gamemodes_get_name(game_mode_id, name[], len)
/* zp50_ammopacks is optional: only needed when olds_buddyshield_cost > 0. */
native zp_ammopacks_get(id)
native zp_ammopacks_set(id, amount)
#define ZP_PLUGIN_HANDLED 97
#define INVITE_TIME 20.0
new const BLOCK_SOUND[] = "items/gunpickup2.wav"
new g_cvCost
new g_partner[MAX_PLAYERS + 1]
new g_invitedBy[MAX_PLAYERS + 1]
new Float:g_inviteEnd[MAX_PLAYERS + 1]
new g_blocks
public plugin_natives()
{
set_native_filter("native_filter")
}
public native_filter(const name[], index, trap)
{
// Ammo packs come from an optional ZP module: run without it.
if (!trap && equal(name, "zp_ammopacks_", 13))
return PLUGIN_HANDLED
return PLUGIN_CONTINUE
}
public plugin_precache()
{
precache_sound(BLOCK_SOUND)
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_buddyshield.txt")
g_cvCost = create_cvar("olds_buddyshield_cost", "0", FCVAR_NONE, "Ammo packs the inviter pays when a pair forms (0 = free)", true, 0.0, true, 1000.0)
AutoExecConfig(true, "olds_buddyshield")
register_clcmd("say", "cmd_say")
register_clcmd("say_team", "cmd_say")
register_concmd("amx_buddyshield", "cmd_status", ADMIN_KICK, "- lists the shield pairs")
}
public client_disconnected(id)
{
break_pair(id, "BS_GONE")
clear_invites_of(id)
}
/* ---------- ZP forwards ---------- */
public zp_fw_core_infect_pre(id, attacker)
{
// Only a zombie's attack on a shielded human; mode-start picks are not attacks.
if (id < 1 || id > MaxClients || !g_partner[id])
return PLUGIN_CONTINUE
if (attacker < 1 || attacker > MaxClients || attacker == id || !is_user_connected(attacker) || !zp_core_is_zombie(attacker))
return PLUGIN_CONTINUE
new partner = g_partner[id]
g_partner[id] = 0
g_partner[partner] = 0
g_blocks++
new name[MAX_NAME_LENGTH], zname[MAX_NAME_LENGTH]
get_user_name(id, name, charsmax(name))
get_user_name(attacker, zname, charsmax(zname))
emit_sound(id, CHAN_ITEM, BLOCK_SOUND, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
set_dhudmessage(80, 200, 255, -1.0, 0.3, 0, 0.0, 2.5, 0.1, 0.4)
show_dhudmessage(id, "%L", id, "BS_DHUD")
client_print_color(id, print_team_default, "%L", id, "BS_BLOCKED_YOU", zname)
if (is_user_connected(partner))
client_print_color(partner, print_team_default, "%L", partner, "BS_BLOCKED_PARTNER", name)
log_amx("Buddy shield blocked %s's infection of %s; the pair is used up", zname, name)
return ZP_PLUGIN_HANDLED
}
public zp_fw_core_infect_post(id, attacker)
{
break_pair(id, "BS_ROLE")
clear_invites_of(id)
}
public zp_fw_core_cure_post(id, attacker)
{
break_pair(id, "BS_ROLE")
clear_invites_of(id)
}
public zp_fw_gamemodes_start(gameModeId)
{
new name[32]
zp_gamemodes_get_name(gameModeId, name, charsmax(name))
// Infection rounds keep the pairs; any special mode clears them.
if (containi(name, "Infection") != -1)
return
clear_all("BS_SPECIAL")
}
public zp_fw_gamemodes_end(gameModeId)
{
clear_all("")
}
/* ---------- pairs ---------- */
break_pair(id, const reason[])
{
new partner = g_partner[id]
if (!partner)
return
g_partner[id] = 0
g_partner[partner] = 0
if (reason[0])
{
if (is_user_connected(id))
client_print_color(id, print_team_default, "%L", id, reason)
if (is_user_connected(partner))
client_print_color(partner, print_team_default, "%L", partner, reason)
}
}
clear_invites_of(id)
{
g_invitedBy[id] = 0
for (new p = 1; p <= MaxClients; p++)
{
if (g_invitedBy[p] == id)
g_invitedBy[p] = 0
}
}
clear_all(const reason[])
{
for (new id = 1; id <= MaxClients; id++)
{
if (g_partner[id] && id < g_partner[id])
break_pair(id, reason)
g_invitedBy[id] = 0
}
}
bool:eligible(id)
{
return is_user_alive(id) && !zp_core_is_zombie(id)
}
/* ---------- /shield <name> ---------- */
public cmd_say(id)
{
new text[64]
read_args(text, charsmax(text))
remove_quotes(text)
trim(text)
if (!equali(text, "/shield", 7) || (text[7] != EOS && text[7] != ' '))
return PLUGIN_CONTINUE
new arg[32]
copy(arg, charsmax(arg), text[7])
trim(arg)
invite(id, arg)
return PLUGIN_HANDLED
}
bool:can_pay(id, cost)
{
if (!LibraryExists("zp50_ammopacks", LibType_Library))
return true
return zp_ammopacks_get(id) >= cost
}
invite(id, const arg[])
{
if (!eligible(id))
{
client_print_color(id, print_team_default, "%L", id, "BS_YOU_NOT_HUMAN")
return
}
if (g_partner[id])
{
client_print_color(id, print_team_default, "%L", id, "BS_YOU_PAIRED")
return
}
if (!arg[0])
{
client_print_color(id, print_team_default, "%L", id, "BS_USAGE")
return
}
new target = find_player_ex(FindPlayer_MatchNameSubstring | FindPlayer_CaseInsensitive, arg)
if (!target || target == id || !is_user_connected(target))
{
client_print_color(id, print_team_default, "%L", id, "BS_NO_PLAYER", arg)
return
}
new tname[MAX_NAME_LENGTH]
get_user_name(target, tname, charsmax(tname))
if (!is_user_alive(target) || zp_core_is_zombie(target))
{
client_print_color(id, print_team_default, "%L", id, "BS_NOT_HUMAN", tname)
return
}
if (g_partner[target])
{
client_print_color(id, print_team_default, "%L", id, "BS_TAKEN", tname)
return
}
new cost = get_pcvar_num(g_cvCost)
if (cost > 0 && !can_pay(id, cost))
{
client_print_color(id, print_team_default, "%L", id, "BS_NO_PACKS", cost)
return
}
g_invitedBy[target] = id
g_inviteEnd[target] = get_gametime() + INVITE_TIME
new name[MAX_NAME_LENGTH], title[128], item[64]
get_user_name(id, name, charsmax(name))
client_print_color(id, print_team_default, "%L", id, "BS_INVITED", tname)
formatex(title, charsmax(title), "%L", target, "BS_MENU_TITLE", name)
new menu = menu_create(title, "menu_answer")
formatex(item, charsmax(item), "%L", target, "BS_MENU_ACCEPT")
menu_additem(menu, item, "1")
formatex(item, charsmax(item), "%L", target, "BS_MENU_DECLINE")
menu_additem(menu, item, "0")
menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
menu_display(target, menu, 0, floatround(INVITE_TIME))
}
public menu_answer(id, menu, item)
{
new info[4], access, callback
if (item >= 0)
menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
menu_destroy(menu)
new inviter = g_invitedBy[id]
g_invitedBy[id] = 0
if (!inviter || !is_user_connected(inviter))
return PLUGIN_HANDLED
if (item < 0 || info[0] != '1')
{
client_print_color(inviter, print_team_default, "%L", inviter, "BS_DECLINED")
return PLUGIN_HANDLED
}
// Things may have changed while the menu was open.
if (get_gametime() > g_inviteEnd[id] || !eligible(id) || !eligible(inviter) || g_partner[id] || g_partner[inviter])
{
client_print_color(id, print_team_default, "%L", id, "BS_EXPIRED")
client_print_color(inviter, print_team_default, "%L", inviter, "BS_EXPIRED")
return PLUGIN_HANDLED
}
new cost = get_pcvar_num(g_cvCost)
if (cost > 0 && LibraryExists("zp50_ammopacks", LibType_Library))
{
new packs = zp_ammopacks_get(inviter)
if (packs < cost)
{
client_print_color(inviter, print_team_default, "%L", inviter, "BS_NO_PACKS", cost)
client_print_color(id, print_team_default, "%L", id, "BS_EXPIRED")
return PLUGIN_HANDLED
}
zp_ammopacks_set(inviter, packs - cost)
}
g_partner[id] = inviter
g_partner[inviter] = id
new a[MAX_NAME_LENGTH], b[MAX_NAME_LENGTH]
get_user_name(inviter, a, charsmax(a))
get_user_name(id, b, charsmax(b))
client_print_color(inviter, print_team_default, "%L", inviter, "BS_PAIRED", b)
client_print_color(id, print_team_default, "%L", id, "BS_PAIRED", a)
log_amx("Buddy shield pair: %s + %s (cost %d)", a, b, cost)
return PLUGIN_HANDLED
}
/* ---------- admin ---------- */
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
new pairs, a[MAX_NAME_LENGTH], b[MAX_NAME_LENGTH]
for (new p = 1; p <= MaxClients; p++)
{
if (!g_partner[p] || p > g_partner[p])
continue
get_user_name(p, a, charsmax(a))
get_user_name(g_partner[p], b, charsmax(b))
console_print(id, " %s + %s", a, b)
pairs++
}
console_print(id, "%L", id, "BS_STATUS", pairs, g_blocks)
return PLUGIN_HANDLED
}