Skip to content
View in the app

A better way to browse. Learn more.

Learn more

tower-upgrade-tiers/addons/amxmodx/scripting/olds_tdup.sma

olds_tdup.sma
335 lines · 10.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Tower Upgrade Tiers (olds.ro workshop idea IDEA-906B12E980D548F2)
 * 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/>.
 *
 * An add-on for the olds.ro Tower Defence core (towers: info_target "olds_td_tower", owner in
 * var_owner, the core's mark 22752 in var_iuser4). The owner looks at a tower within 128 units
 * and says /tdup: a menu upgrades it (tier 1: olds_tdup_cost1, tier 2: olds_tdup_cost2) or
 * sells it for olds_tdup_refund of everything spent. Tier 1 multiplies range and fire rate by
 * 1.25, tier 2 by 1.5; each tier has its own glow colour. The tier is kept in var_iuser3,
 * because the core keeps its tower mark in var_iuser4.
 */
#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <hamsandwich>
#include <reapi>

#define PLUGIN  "Tower Upgrade Tiers"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TOWER_MARK  22752
#define NPC_MARK    22751
#define AIM_RANGE   128
#define MAX_TIER    2

new const TOWER_CLASS[] = "olds_td_tower"
new const NPC_CLASS[] = "olds_td_npc"
new const Float:MULT[MAX_TIER + 1] = { 1.0, 1.25, 1.5 }
new const TIER_COLOR[MAX_TIER + 1][3] = { { 0, 0, 0 }, { 60, 160, 255 }, { 255, 190, 40 } }

new g_cost1, g_cost2, Float:g_refund, Float:g_baseRate, Float:g_baseRange, Float:g_damage
new g_cvBaseCost
new g_beam
new g_menuTower[MAX_PLAYERS + 1]

public plugin_precache()
{
    g_beam = precache_model("sprites/laserbeam.spr")
}

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

    bind_pcvar_num(create_cvar("olds_tdup_cost1", "600", FCVAR_NONE, "Price of tier 1", true, 0.0, true, 16000.0), g_cost1)
    bind_pcvar_num(create_cvar("olds_tdup_cost2", "1200", FCVAR_NONE, "Price of tier 2", true, 0.0, true, 16000.0), g_cost2)
    bind_pcvar_float(create_cvar("olds_tdup_refund", "0.5", FCVAR_NONE, "Share of everything spent paid back on selling", true, 0.0, true, 1.0), g_refund)
    bind_pcvar_float(create_cvar("olds_tdup_base_rate", "0.5", FCVAR_NONE, "Seconds between shots of a tier 0 tower (as in the core)", true, 0.05, true, 10.0), g_baseRate)
    bind_pcvar_float(create_cvar("olds_tdup_base_range", "700", FCVAR_NONE, "Range of a tier 0 tower (as in the core)", true, 50.0, true, 4000.0), g_baseRange)
    bind_pcvar_float(create_cvar("olds_tdup_damage", "20", FCVAR_NONE, "Damage of a shot the add-on fires past the core's range (as in the core)", true, 0.0, true, 1000.0), g_damage)
    AutoExecConfig(true, "olds_tdup")

    register_clcmd("say /tdup", "cmd_menu")
    register_clcmd("say_team /tdup", "cmd_menu")
    register_clcmd("tdup", "cmd_menu")
    register_concmd("amx_tdup_status", "cmd_status", ADMIN_RCON, "- lists the towers and their tiers")
    RegisterHam(Ham_Think, "info_target", "on_think_post", 1)
}

public plugin_cfg()
{
    g_cvBaseCost = get_cvar_pointer("olds_td_towercost")
}

bool:is_tower(ent)
{
    if (ent <= MaxClients || is_nullent(ent) || get_entvar(ent, var_iuser4) != TOWER_MARK)
        return false
    new classname[32]
    get_entvar(ent, var_classname, classname, charsmax(classname))
    return bool:equal(classname, TOWER_CLASS)
}

tier_of(ent)
{
    return clamp(get_entvar(ent, var_iuser3), 0, MAX_TIER)
}

/* ------------------------------------------------------------------ the tower's think */

Float:entity_range_from(const Float:eye[3], npc)
{
    new Float:o[3]
    get_entvar(npc, var_origin, o)
    o[2] += 30.0
    return get_distance_f(eye, o)
}

public on_think_post(ent)
{
    if (!is_tower(ent))
        return HAM_IGNORED
    new tier = tier_of(ent)
    if (!tier)
        return HAM_IGNORED
    /* The core has had its shot this think within its own range; past it, up to the tier's
       range, this add-on fires the same way. */
    new Float:eye[3]
    get_entvar(ent, var_origin, eye)
    eye[2] += 40.0
    new Float:far = g_baseRange * MULT[tier]
    new nearest = nearest_npc(ent, eye, far)
    if (nearest && entity_range_from(eye, nearest) >= g_baseRange)
        fire(ent, eye, nearest)
    set_entvar(ent, var_nextthink, get_gametime() + g_baseRate / MULT[tier])
    return HAM_IGNORED
}

/* The nearest marked NPC the tower can see, within `range`. */
nearest_npc(tower, const Float:eye[3], Float:range)
{
    new best, Float:bestDist = range
    new npc = MaxClients
    while ((npc = engfunc(EngFunc_FindEntityByString, npc, "classname", NPC_CLASS)) > 0)
    {
        if (get_entvar(npc, var_iuser4) != NPC_MARK)
            continue
        new Float:o[3]
        get_entvar(npc, var_origin, o)
        o[2] += 30.0
        new Float:dist = get_distance_f(eye, o)
        if (dist >= bestDist)
            continue
        new tr = create_tr2()
        engfunc(EngFunc_TraceLine, eye, o, IGNORE_MONSTERS, tower, tr)
        new Float:fraction
        get_tr2(tr, TR_flFraction, fraction)
        free_tr2(tr)
        if (fraction < 1.0)
            continue
        best = npc
        bestDist = dist
    }
    return best
}

fire(tower, const Float:eye[3], npc)
{
    new Float:o[3], Float:dir[3], Float:angles[3]
    get_entvar(npc, var_origin, o)
    o[2] += 30.0
    xs_sub(o, eye, dir)
    vector_to_angle(dir, angles)
    angles[0] = 0.0
    set_entvar(tower, var_angles, angles)
    new tier = tier_of(tower)
    message_begin_f(MSG_PVS, SVC_TEMPENTITY, eye)
    write_byte(TE_BEAMPOINTS)
    write_coord_f(eye[0])
    write_coord_f(eye[1])
    write_coord_f(eye[2])
    write_coord_f(o[0])
    write_coord_f(o[1])
    write_coord_f(o[2])
    write_short(g_beam)
    write_byte(0)
    write_byte(0)
    write_byte(2)
    write_byte(12)
    write_byte(0)
    write_byte(TIER_COLOR[tier][0])
    write_byte(TIER_COLOR[tier][1])
    write_byte(TIER_COLOR[tier][2])
    write_byte(200)
    write_byte(0)
    message_end()
    new owner = get_entvar(tower, var_owner)
    if (owner < 1 || owner > MaxClients || !is_user_connected(owner))
        owner = tower
    ExecuteHamB(Ham_TakeDamage, npc, tower, owner, g_damage, DMG_BULLET)
}

xs_sub(const Float:a[3], const Float:b[3], Float:out[3])
{
    out[0] = a[0] - b[0]
    out[1] = a[1] - b[1]
    out[2] = a[2] - b[2]
}

/* ------------------------------------------------------------------ the menu */

aimed_tower(id)
{
    new ent, body
    get_user_aiming(id, ent, body, AIM_RANGE)
    if (is_tower(ent))
        return ent
    return 0
}

upgrade_cost(tier)
{
    if (tier == 0)
        return g_cost1
    if (tier == 1)
        return g_cost2
    return 0
}

/* Everything spent on the tower: the core's price and the upgrades bought. */
spent_on(ent)
{
    new total = g_cvBaseCost ? get_pcvar_num(g_cvBaseCost) : 0
    new tier = tier_of(ent)
    if (tier >= 1)
        total += g_cost1
    if (tier >= 2)
        total += g_cost2
    return total
}

public cmd_menu(id)
{
    if (!is_user_alive(id))
        return PLUGIN_HANDLED
    new ent = aimed_tower(id)
    if (!ent)
    {
        client_print(id, print_chat, "%L", id, "TDUP_LOOK")
        return PLUGIN_HANDLED
    }
    if (get_entvar(ent, var_owner) != id)
    {
        client_print(id, print_chat, "%L", id, "TDUP_NOT_OWNER")
        return PLUGIN_HANDLED
    }
    g_menuTower[id] = ent
    new tier = tier_of(ent)
    new text[96]
    formatex(text, charsmax(text), "%L", id, "TDUP_TITLE", tier)
    new menu = menu_create(text, "on_menu")
    if (tier < MAX_TIER)
        formatex(text, charsmax(text), "%L", id, "TDUP_UPGRADE", tier + 1, upgrade_cost(tier))
    else
        formatex(text, charsmax(text), "%L", id, "TDUP_MAXED")
    menu_additem(menu, text)
    formatex(text, charsmax(text), "%L", id, "TDUP_SELL", floatround(float(spent_on(ent)) * g_refund, floatround_floor))
    menu_additem(menu, text)
    menu_display(id, menu)
    return PLUGIN_HANDLED
}

public on_menu(id, menu, item)
{
    menu_destroy(menu)
    new ent = g_menuTower[id]
    g_menuTower[id] = 0
    if (item < 0 || !is_user_alive(id))
        return PLUGIN_HANDLED
    /* Checked again: the tower may be gone or the player may have walked away. */
    if (!is_tower(ent) || get_entvar(ent, var_owner) != id)
    {
        client_print(id, print_chat, "%L", id, "TDUP_NOT_OWNER")
        return PLUGIN_HANDLED
    }
    if (item == 0)
        upgrade(id, ent)
    else if (item == 1)
        sell(id, ent)
    return PLUGIN_HANDLED
}

upgrade(id, ent)
{
    new tier = tier_of(ent)
    if (tier >= MAX_TIER)
    {
        client_print(id, print_chat, "%L", id, "TDUP_MAXED")
        return
    }
    new cost = upgrade_cost(tier)
    new money = get_member(id, m_iAccount)
    if (money < cost)
    {
        client_print(id, print_chat, "%L", id, "TDUP_NO_MONEY", cost, money)
        return
    }
    rg_add_account(id, -cost, AS_ADD)
    tier++
    set_entvar(ent, var_iuser3, tier)
    set_entvar(ent, var_renderfx, kRenderFxGlowShell)
    new Float:color[3]
    color[0] = float(TIER_COLOR[tier][0])
    color[1] = float(TIER_COLOR[tier][1])
    color[2] = float(TIER_COLOR[tier][2])
    set_entvar(ent, var_rendercolor, color)
    set_entvar(ent, var_rendermode, kRenderNormal)
    set_entvar(ent, var_renderamt, 16.0)
    set_entvar(ent, var_nextthink, get_gametime() + 0.1)
    client_print(id, print_chat, "%L", id, "TDUP_DONE", tier, cost)
    log_amx("Tower %d upgraded to tier %d for $%d", ent, tier, cost)
}

sell(id, ent)
{
    new refund = floatround(float(spent_on(ent)) * g_refund, floatround_floor)
    rg_add_account(id, refund, AS_ADD)
    set_entvar(ent, var_iuser4, 0)
    set_entvar(ent, var_flags, FL_KILLME)
    client_print(id, print_chat, "%L", id, "TDUP_SOLD", refund)
    log_amx("Tower %d sold for $%d", ent, refund)
}

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

public cmd_status(id, level, cid)
{
    if (id && !(get_user_flags(id) & level))
    {
        console_print(id, "You have no access to that command.")
        return PLUGIN_HANDLED
    }
    new ent = MaxClients, count
    while ((ent = engfunc(EngFunc_FindEntityByString, ent, "classname", TOWER_CLASS)) > 0)
    {
        if (!is_tower(ent))
            continue
        new Float:color[3]
        get_entvar(ent, var_rendercolor, color)
        console_print(id, "[TDUp] tower %d owner #%d tier %d glow %.0f %.0f %.0f, spent %d", ent, get_entvar(ent, var_owner),
            tier_of(ent), color[0], color[1], color[2], spent_on(ent))
        count++
    }
    console_print(id, "[TDUp] %d tower(s)", count)
    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.