Skip to content
View in the app

A better way to browse. Learn more.

Learn more

field-medic/addons/amxmodx/scripting/olds_fieldmedic.sma

olds_fieldmedic.sma
241 lines · 7 KB · Pawn (AMX Mod X)
Raw
/*
 * Field Medic (olds.ro)
 * 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: <https://www.gnu.org/licenses/>.
 *
 * Capture the Flag (jCTF): a teammate says /medic to spend olds_medic_cost adrenaline and
 * heal the team's flag carrier by olds_medic_hp, capped at 100 HP. One heal per possession:
 * each time the flag is stolen or picked up a new possession starts. Nothing is charged
 * unless the heal raises the carrier's health.
 *
 * Cvars: olds_medic_hp 30, olds_medic_cost 15
 * Admin: amx_medic_status (ADMIN_KICK)
 */

#include <amxmodx>
#include <amxmisc>
#include <fun>

#pragma semicolon 0

#define PLUGIN  "Field Medic"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define HEALTH_CAP 100

/* jCTF API, declared here so the plugin builds without jctf.inc; the native filter lets the
   plugin load (and refuse /medic) when jCTF is not running. */
native jctf_get_team(id)
native jctf_get_flagcarrier(id)
native jctf_get_adrenaline(id)
native jctf_add_adrenaline(id, add, const reason[])

enum
{
    FLAG_STOLEN = 0,
    FLAG_PICKED,
    FLAG_DROPPED,
    FLAG_MANUALDROP,
    FLAG_RETURNED,
    FLAG_CAPTURED,
    FLAG_AUTORETURN,
    FLAG_ADMINRETURN
}

new const HEAL_SOUND[] = "items/smallmedkit1.wav"

new g_heal
new g_cost
new bool:g_api

new g_serial
new g_possession[MAX_PLAYERS + 1]
new g_healedIn[MAX_PLAYERS + 1]
new g_heals

public plugin_natives()
{
    set_native_filter("native_filter")
}

public native_filter(const name[], index, trap)
{
    if (equal(name, "jctf_", 5))
        return PLUGIN_HANDLED
    return PLUGIN_CONTINUE
}

public plugin_precache()
{
    precache_sound(HEAL_SOUND)
}

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

    bind_pcvar_num(create_cvar("olds_medic_hp", "30", FCVAR_NONE,
        "Health a /medic heal gives the flag carrier", true, 1.0, true, 100.0), g_heal)
    bind_pcvar_num(create_cvar("olds_medic_cost", "15", FCVAR_NONE,
        "Adrenaline the volunteer pays for a heal", true, 0.0, true, 100.0), g_cost)
    AutoExecConfig(true, "olds_fieldmedic")

    register_clcmd("say /medic", "cmd_medic")
    register_clcmd("say_team /medic", "cmd_medic")
    register_concmd("amx_medic_status", "cmd_status", ADMIN_KICK, "- shows the carriers' heal allowance")

    register_event("HLTV", "on_new_round", "a", "1=0", "2=0")
}

public plugin_cfg()
{
    g_api = LibraryExists("jctf", LibType_Library) != 0
    if (!g_api)
        log_amx("jCTF is not running: /medic is turned off on this map")
}

/* ------------------------------------------------------------ possessions */

public jctf_flag(event, id)
{
    if (id < 1 || id > MaxClients)
        return
    switch (event)
    {
        case FLAG_STOLEN, FLAG_PICKED:
        {
            g_possession[id] = ++g_serial
            g_healedIn[id] = 0
        }
        case FLAG_DROPPED, FLAG_MANUALDROP, FLAG_CAPTURED:
        {
            g_possession[id] = 0
            g_healedIn[id] = 0
        }
    }
}

public on_new_round()
{
    arrayset(g_possession, 0, sizeof g_possession)
    arrayset(g_healedIn, 0, sizeof g_healedIn)
}

public client_disconnected(id)
{
    g_possession[id] = 0
    g_healedIn[id] = 0
}

find_carrier(team)
{
    for (new i = 1; i <= MaxClients; i++)
    {
        if (is_user_alive(i) && jctf_get_team(i) == team && jctf_get_flagcarrier(i))
            return i
    }
    return 0
}

/* ------------------------------------------------------------ /medic */

public cmd_medic(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    if (!g_api)
    {
        client_print_color(id, print_team_default, "%L", id, "MEDIC_NO_CTF")
        return PLUGIN_HANDLED
    }
    if (!is_user_alive(id))
    {
        client_print_color(id, print_team_default, "%L", id, "MEDIC_DEAD")
        return PLUGIN_HANDLED
    }
    new team = jctf_get_team(id)
    new carrier = find_carrier(team)
    if (!carrier)
    {
        client_print_color(id, print_team_default, "%L", id, "MEDIC_NO_CARRIER")
        return PLUGIN_HANDLED
    }
    if (carrier == id)
    {
        client_print_color(id, print_team_default, "%L", id, "MEDIC_SELF")
        return PLUGIN_HANDLED
    }
    /* A carrier seen without a pickup event (plugin reloaded mid-carry) gets a possession now. */
    if (!g_possession[carrier])
        g_possession[carrier] = ++g_serial

    new name[MAX_NAME_LENGTH], vname[MAX_NAME_LENGTH]
    get_user_name(carrier, name, charsmax(name))
    get_user_name(id, vname, charsmax(vname))

    if (g_healedIn[carrier] == g_possession[carrier])
    {
        client_print_color(id, print_team_default, "%L", id, "MEDIC_USED", name)
        return PLUGIN_HANDLED
    }
    new health = get_user_health(carrier)
    if (health >= HEALTH_CAP)
    {
        client_print_color(id, print_team_default, "%L", id, "MEDIC_FULL", name)
        return PLUGIN_HANDLED
    }
    new cost = clamp(g_cost, 0, 100)
    new have = jctf_get_adrenaline(id)
    if (have < cost)
    {
        client_print_color(id, print_team_default, "%L", id, "MEDIC_POOR", cost, have)
        return PLUGIN_HANDLED
    }

    new after = min(health + clamp(g_heal, 1, 100), HEALTH_CAP)
    set_user_health(carrier, after)
    if (cost > 0)
        jctf_add_adrenaline(id, -cost, "Field Medic")
    g_healedIn[carrier] = g_possession[carrier]
    g_heals++

    emit_sound(carrier, CHAN_ITEM, HEAL_SOUND, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
    client_print_color(carrier, print_team_default, "%L", carrier, "MEDIC_HEALED_YOU", vname, after)
    client_print_color(id, print_team_default, "%L", id, "MEDIC_HEALED_BY", name, after, cost)
    log_amx("%s healed flag carrier %s from %d to %d HP for %d adrenaline (possession %d)",
        vname, name, health, after, cost, g_possession[carrier])
    return PLUGIN_HANDLED
}

/* ------------------------------------------------------------ admin */

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "Field Medic: jCTF %d, heal %d, cost %d, heals done %d", g_api ? 1 : 0, g_heal, g_cost, g_heals)
    new name[MAX_NAME_LENGTH], shown
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!is_user_connected(i))
            continue
        if (!g_api || !jctf_get_flagcarrier(i))
            continue
        get_user_name(i, name, charsmax(name))
        console_print(id, "  carrier %s: %d HP, possession %d, healed %d", name, get_user_health(i),
            g_possession[i], (g_possession[i] && g_healedIn[i] == g_possession[i]) ? 1 : 0)
        shown++
    }
    if (!shown)
        console_print(id, "  no flag carriers")
    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.