Skip to content
View in the app

A better way to browse. Learn more.

Learn more

dusk-rehearsal/addons/amxmodx/scripting/olds_dusk.sma

olds_dusk.sma
316 lines · 8.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Dusk Rehearsal (olds.ro workshop card AMX-089)
 *
 * Practice: an admin darkens the map with a lighting preset for the rest of
 * the round, to rehearse it at dusk or at night. The map's own lighting comes
 * back at round end, when the admin cancels, and when the plugin is paused or
 * the map changes.
 *
 *   amx_dusk <preset>   switch to a preset (ADMIN_MAP), for this round only
 *   amx_dusk off        cancel and restore the map's lighting
 *   amx_dusk            list the presets and what is active
 *
 * The map's lighting: lightstyle 0 as the map sets it while loading, caught
 * with the EngFunc_LightStyle forward in plugin_precache. If it was not seen
 * (the pattern is unknown), the plugin refuses every preset on this map, so
 * it never changes lighting it cannot put back. Presets are applied with
 * set_lights() from the engine module; restoring calls set_lights("#OFF"),
 * which sends the map's own pattern again.
 *
 * Presets: addons/amxmodx/configs/olds_dusk.ini, one per line:
 *     name  pattern
 * name: letters, digits, '-' or '_', up to 15 characters; pattern: letters a
 * (darkest) to z (brightest), up to 32 of them (more than one letter
 * flickers). Invalid lines are logged and skipped.
 *
 * Cvar: olds_dusk_enable 1 (0 turns the command off and restores at once).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <engine>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Dusk Rehearsal"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_PRESETS = 16
const PRESET_NAME = 16
const PATTERN_LENGTH = 33
const TASK_WATCH = 8911

new g_preset[MAX_PRESETS][PRESET_NAME]
new g_pattern[MAX_PRESETS][PATTERN_LENGTH]
new g_count

new g_original[PATTERN_LENGTH]     // the map's lightstyle 0, empty = unknown
new g_lightForward
new g_active = -1                  // preset in use, -1 = the map's own lighting
new bool:g_roundOpen

new g_enable

public plugin_precache()
{
    g_lightForward = register_forward(FM_LightStyle, "on_light_style", false)
}

public on_light_style(style, const pattern[])
{
    if (style == 0 && !g_original[0] && pattern[0])
        copy(g_original, charsmax(g_original), pattern)
    return FMRES_IGNORED
}

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

    if (g_lightForward)
    {
        unregister_forward(FM_LightStyle, g_lightForward, false)
        g_lightForward = 0
    }

    new pcvar = create_cvar("olds_dusk_enable", "1", FCVAR_NONE,
        "1 = admins may use amx_dusk; 0 = off (and the map's lighting is restored).", true, 0.0, true, 1.0)
    bind_pcvar_num(pcvar, g_enable)
    hook_cvar_change(pcvar, "on_enable_change")
    AutoExecConfig(true, "olds_dusk")

    register_concmd("amx_dusk", "cmd_dusk", ADMIN_MAP, "<preset|off> - lighting preset for this practice round")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    set_task(1.0, "task_watch_round_end", TASK_WATCH, _, _, "b")

    load_presets()
    if (!g_original[0])
    {
        new map[64]
        get_mapname(map, charsmax(map))
        log_amx("%s: the map's lighting was not seen while loading, presets are off on this map", map)
    }
}

/* ---------- presets ---------- */

load_presets()
{
    new path[128]
    get_configsdir(path, charsmax(path))
    add(path, charsmax(path), "/olds_dusk.ini")
    new file = fopen(path, "rt")
    if (!file)
    {
        log_amx("%s not found: no presets", path)
        return
    }
    new line[128], name[32], pattern[64], number
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        if (g_count == MAX_PRESETS)
        {
            log_amx("%s: more than %d presets, the rest are ignored", path, MAX_PRESETS)
            break
        }
        if (parse(line, name, charsmax(name), pattern, charsmax(pattern)) != 2 || !valid_name(name)
            || !valid_pattern(pattern) || equali(name, "off") || find_preset(name) >= 0)
        {
            log_amx("%s line %d: expected ^"name pattern^" (name up to %d characters, pattern a-z up to %d), skipped",
                path, number, PRESET_NAME - 1, PATTERN_LENGTH - 1)
            continue
        }
        strtolower(pattern)
        copy(g_preset[g_count], charsmax(g_preset[]), name)
        copy(g_pattern[g_count], charsmax(g_pattern[]), pattern)
        g_count++
    }
    fclose(file)
}

bool:valid_name(const name[])
{
    new length = strlen(name)
    if (!length || length >= PRESET_NAME)
        return false
    for (new i = 0; i < length; i++)
        if (!isalnum(name[i]) && name[i] != '-' && name[i] != '_')
            return false
    return true
}

bool:valid_pattern(const pattern[])
{
    new length = strlen(pattern)
    if (!length || length >= PATTERN_LENGTH)
        return false
    for (new i = 0; i < length; i++)
    {
        new c = pattern[i] & 0xFF
        if (!('a' <= c <= 'z') && !('A' <= c <= 'Z'))
            return false
    }
    return true
}

find_preset(const name[])
{
    for (new p = 0; p < g_count; p++)
        if (equali(g_preset[p], name))
            return p
    return -1
}

/* ---------- applying and restoring ---------- */

apply(preset)
{
    g_active = preset
    set_lights(g_pattern[preset])
}

bool:restore()
{
    if (g_active < 0)
        return false
    g_active = -1
    set_lights("#OFF")
    return true
}

/* ---------- amx_dusk ---------- */

public cmd_dusk(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED

    new arg[32]
    read_argv(1, arg, charsmax(arg))
    trim(arg)

    if (!arg[0])
    {
        list_presets(id)
        return PLUGIN_HANDLED
    }
    if (equali(arg, "off"))
    {
        if (restore())
            announce(id, "DUSK_RESTORED_BY", "")
        else
            reply(id, "DUSK_NOTHING_ACTIVE")
        return PLUGIN_HANDLED
    }
    if (!g_enable)
    {
        reply(id, "DUSK_DISABLED")
        return PLUGIN_HANDLED
    }
    if (!g_original[0])
    {
        reply(id, "DUSK_UNKNOWN_MAP")
        return PLUGIN_HANDLED
    }
    new preset = find_preset(arg)
    if (preset < 0)
    {
        reply(id, "DUSK_NO_PRESET")
        list_presets(id)
        return PLUGIN_HANDLED
    }
    if (!g_roundOpen || get_member_game(m_bRoundTerminating))
    {
        reply(id, "DUSK_ROUND_OVER")
        return PLUGIN_HANDLED
    }
    apply(preset)
    announce(id, "DUSK_SET_BY", g_preset[preset])
    return PLUGIN_HANDLED
}

list_presets(id)
{
    console_print(id, "[Dusk] map lighting %s%s%s; active: %s; enabled %d", g_original[0] ? "^"" : "",
        g_original[0] ? g_original : "unknown (presets off)", g_original[0] ? "^"" : "",
        g_active >= 0 ? g_preset[g_active] : "none", g_enable)
    for (new p = 0; p < g_count; p++)
        console_print(id, "[Dusk]   %s ^"%s^"", g_preset[p], g_pattern[p])
    if (!g_count)
        console_print(id, "[Dusk]   no presets loaded")
}

reply(id, const key[])
{
    if (id)
        client_print_color(id, print_team_default, "^4[Dusk]^1 %L", id, key)
    else
        server_print("[Dusk] %L", LANG_SERVER, key)
}

announce(id, const key[], const preset[])
{
    new name[MAX_NAME_LENGTH]
    if (id)
        get_user_name(id, name, charsmax(name))
    else
        copy(name, charsmax(name), "Server")
    client_print_color(0, print_team_default, "^4[Dusk]^1 %L", LANG_PLAYER, key, name, preset)
    log_amx("%s: %s %s", name, key, preset)
}

/* ---------- round end, cvar and pause ---------- */

public on_restart_round_pre()
{
    g_roundOpen = true
    if (restore())
        client_print_color(0, print_team_default, "^4[Dusk]^1 %L", LANG_PLAYER, "DUSK_RESTORED_ROUND")
    return HC_CONTINUE
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    round_over()
    return HC_CONTINUE
}

/* A round closed by rg_round_end() from another plugin skips RG_RoundEnd. */
public task_watch_round_end()
{
    if (g_roundOpen && get_member_game(m_bRoundTerminating))
        round_over()
}

round_over()
{
    g_roundOpen = false
    if (restore())
        client_print_color(0, print_team_default, "^4[Dusk]^1 %L", LANG_PLAYER, "DUSK_RESTORED_ROUND")
}

public on_enable_change(pcvar, const oldValue[], const newValue[])
{
    if (!str_to_num(newValue))
        restore()
}

public plugin_pause()
{
    restore()
}

public plugin_end()
{
    restore()
}

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.