Skip to content
View in the app

A better way to browse. Learn more.

Learn more

block-undo-history/addons/amxmodx/scripting/olds_bmundo.sma

olds_bmundo.sma
410 lines · 10.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Block Undo History (olds.ro workshop card IDEA-F47A03552368622E)
 *
 * Blockmaker add-on: every builder can undo and redo their own last twenty block
 * actions (place, move, turn, delete) with two binds:
 *   bind "[" olds_bm_undo
 *   bind "]" olds_bm_redo
 *   olds_bm_history            your two stacks in the console (from the server
 *                              console: every builder's)
 *
 * The block maker core's forwards (olds_blockmaker.inc) push each action onto
 * the builder's undo stack (20 deep: the 21st action drops the oldest); a new
 * action empties the redo stack. Undoing a delete creates the block again
 * through the core with the same type, origin, angles and size. Blocks are
 * followed by an id of their own, so a block that was deleted and brought
 * back is still the same block for every stack. A builder's stacks hold only
 * their own actions, so nobody can undo another builder's work; a step whose
 * block someone else removed is dropped with a message.
 * Stacks are cleared when the builder leaves and on map change.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <fakemeta>
#include <olds_blockmaker>

#define PLUGIN  "Block Undo History"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const STACK = 20
const EDICTS = 2049

enum
{
    K_NONE = 0,
    K_PLACE,
    K_MOVE,
    K_DELETE
}
new const KIND_KEY[][] = { "", "BMU_K_PLACE", "BMU_K_MOVE", "BMU_K_DELETE" }
new const KIND_WORD[][] = { "", "place", "move", "delete" }

enum
{
    S_UNDO = 0,
    S_REDO
}

// Floats kept per action.
enum
{
    F_OLD_O = 0,
    F_OLD_A = 3,
    F_NEW_O = 6,
    F_NEW_A = 9,
    F_SIZE = 12,
    F_COUNT = 15
}

const SLOTS = (MAX_PLAYERS + 1) * 2 * STACK

new g_kind[SLOTS]
new g_blockUid[SLOTS]
new Float:g_f[SLOTS][F_COUNT]
new g_type[SLOTS][OLDS_BM_TYPE_LEN]
new g_count[MAX_PLAYERS + 1][2]

new g_uid[EDICTS]                        // entity -> block id (0 = none)
new g_nextUid
new bool:g_replaying

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_bmundo.txt")
    register_clcmd("olds_bm_undo", "cmd_undo")
    register_clcmd("olds_bm_redo", "cmd_redo")
    register_concmd("olds_bm_history", "cmd_history")
}

public client_disconnected(id)
{
    g_count[id][S_UNDO] = 0
    g_count[id][S_REDO] = 0
}

/* ---------- stacks ---------- */

at(id, stack, pos)
{
    return (id * 2 + stack) * STACK + pos
}

copy_action(dst, src)
{
    g_kind[dst] = g_kind[src]
    g_blockUid[dst] = g_blockUid[src]
    for (new i = 0; i < F_COUNT; i++)
        g_f[dst][i] = g_f[src][i]
    copy(g_type[dst], charsmax(g_type[]), g_type[src])
}

// A free slot on top of the stack; a full stack drops its oldest action.
push_slot(id, stack)
{
    if (g_count[id][stack] >= STACK)
    {
        for (new pos = 1; pos < STACK; pos++)
            copy_action(at(id, stack, pos - 1), at(id, stack, pos))
        g_count[id][stack] = STACK - 1
    }
    new slot = at(id, stack, g_count[id][stack])
    g_count[id][stack]++
    return slot
}

// Reads the block into the action's "old" or "new" origin and angles, and its type and size.
bool:read_block(ent, slot, base)
{
    new Float:origin[3], Float:angles[3], Float:size[3]
    if (!olds_bm_get_block(ent, g_type[slot], charsmax(g_type[]), origin, angles, size))
        return false
    for (new axis = 0; axis < 3; axis++)
    {
        g_f[slot][base + axis] = origin[axis]
        g_f[slot][base + 3 + axis] = angles[axis]
        g_f[slot][F_SIZE + axis] = size[axis]
    }
    return true
}

record(builder, kind, ent)
{
    if (!olds_bm_is_block(ent))
        return -1
    new slot = push_slot(builder, S_UNDO)
    g_kind[slot] = kind
    g_blockUid[slot] = g_uid[ent]
    if (!read_block(ent, slot, F_NEW_O))
    {
        g_count[builder][S_UNDO]--
        return -1
    }
    for (new i = 0; i < 6; i++)
        g_f[slot][F_OLD_O + i] = g_f[slot][F_NEW_O + i]
    g_count[builder][S_REDO] = 0
    return slot
}

bool:is_builder(id)
{
    return 1 <= id <= MaxClients && is_user_connected(id) != 0
}

bool:in_range(ent)
{
    return 0 < ent < EDICTS
}

/* ---------- core forwards ---------- */

public olds_bm_block_created(builder, ent)
{
    if (!in_range(ent))
        return
    // A step being replayed: the caller gives the block its old id.
    if (g_replaying)
    {
        g_uid[ent] = 0
        return
    }
    g_uid[ent] = ++g_nextUid
    if (is_builder(builder))
        record(builder, K_PLACE, ent)
}

public olds_bm_block_moved(builder, ent, const Float:oldOrigin[3], const Float:oldAngles[3])
{
    if (g_replaying || !in_range(ent) || !is_builder(builder))
        return
    if (!g_uid[ent])
        g_uid[ent] = ++g_nextUid
    new slot = record(builder, K_MOVE, ent)
    if (slot < 0)
        return
    for (new axis = 0; axis < 3; axis++)
    {
        g_f[slot][F_OLD_O + axis] = oldOrigin[axis]
        g_f[slot][F_OLD_A + axis] = oldAngles[axis]
    }
}

public olds_bm_block_deleted(builder, ent)
{
    if (!in_range(ent))
        return
    if (!g_replaying && is_builder(builder))
    {
        if (!g_uid[ent])
            g_uid[ent] = ++g_nextUid
        record(builder, K_DELETE, ent)
    }
    g_uid[ent] = 0
}

/* ---------- undo and redo ---------- */

find_block(uid)
{
    if (!uid)
        return 0
    for (new ent = 1; ent < EDICTS; ent++)
    {
        if (g_uid[ent] != uid)
            continue
        if (pev_valid(ent) && olds_bm_is_block(ent))
            return ent
        g_uid[ent] = 0
    }
    return 0
}

// Creates the block again from the action's old or new state, with its old id.
bool:recreate(id, slot, base)
{
    new Float:origin[3], Float:angles[3], Float:size[3]
    for (new axis = 0; axis < 3; axis++)
    {
        origin[axis] = g_f[slot][base + axis]
        angles[axis] = g_f[slot][base + 3 + axis]
        size[axis] = g_f[slot][F_SIZE + axis]
    }
    g_replaying = true
    new ent = olds_bm_create_block(id, g_type[slot], origin, angles, size)
    g_replaying = false
    if (!in_range(ent))
        return false
    g_uid[ent] = g_blockUid[slot]
    return true
}

bool:transform(id, ent, slot, base)
{
    new Float:origin[3], Float:angles[3]
    for (new axis = 0; axis < 3; axis++)
    {
        origin[axis] = g_f[slot][base + axis]
        angles[axis] = g_f[slot][base + 3 + axis]
    }
    g_replaying = true
    new done = olds_bm_move_block(id, ent, origin, angles)
    g_replaying = false
    return done != 0
}

bool:drop_block(id, ent, slot, base)
{
    read_block(ent, slot, base)
    g_replaying = true
    new done = olds_bm_delete_block(id, ent)
    g_replaying = false
    if (done)
        g_uid[ent] = 0
    return done != 0
}

// 1 done, 0 the block is gone, -1 the core refused.
play(id, slot, bool:undo)
{
    new kind = g_kind[slot]
    // Undo a place / redo a delete removes the block; undo a delete / redo a place creates it.
    new bool:removes = (kind == K_PLACE && undo) || (kind == K_DELETE && !undo)
    if (kind == K_MOVE || removes)
    {
        new ent = find_block(g_blockUid[slot])
        if (!ent)
            return 0
        new bool:done
        if (kind == K_MOVE)
        {
            if (undo)
                done = transform(id, ent, slot, F_OLD_O)
            else
                done = transform(id, ent, slot, F_NEW_O)
        }
        else if (kind == K_PLACE)
            done = drop_block(id, ent, slot, F_NEW_O)
        else
            done = drop_block(id, ent, slot, F_OLD_O)
        return done ? 1 : -1
    }
    if (find_block(g_blockUid[slot]))
        return -1
    new bool:made
    if (kind == K_DELETE)
        made = recreate(id, slot, F_OLD_O)
    else
        made = recreate(id, slot, F_NEW_O)
    return made ? 1 : -1
}

step(id, from)
{
    new to = S_REDO
    if (from == S_REDO)
        to = S_UNDO
    if (!g_count[id][from])
    {
        if (from == S_UNDO)
            say_to(id, "BMU_NO_UNDO")
        else
            say_to(id, "BMU_NO_REDO")
        return
    }
    g_count[id][from]--
    new slot = at(id, from, g_count[id][from])
    new result = play(id, slot, from == S_UNDO)
    if (result <= 0)
    {
        if (result == 0)
            say_to(id, "BMU_GONE")
        else
            say_to(id, "BMU_REFUSED")
        return
    }
    copy_action(push_slot(id, to), slot)
    new what[64]
    formatex(what, charsmax(what), "%L", id, KIND_KEY[g_kind[slot]], g_type[slot])
    if (from == S_UNDO)
        say_to(id, "BMU_UNDONE", what, g_count[id][S_UNDO])
    else
        say_to(id, "BMU_REDONE", what, g_count[id][S_REDO])
}

public cmd_undo(id)
{
    if (is_builder(id))
        step(id, S_UNDO)
    return PLUGIN_HANDLED
}

public cmd_redo(id)
{
    if (is_builder(id))
        step(id, S_REDO)
    return PLUGIN_HANDLED
}

/* ---------- history ---------- */

public cmd_history(id)
{
    if (id)
        print_history(id, id)
    else
    {
        new shown
        for (new p = 1; p <= MaxClients; p++)
        {
            if (is_user_connected(p) && (g_count[p][S_UNDO] || g_count[p][S_REDO]))
            {
                print_history(0, p)
                shown++
            }
        }
        if (!shown)
            console_print(0, "[Undo] no builder has a history")
    }
    return PLUGIN_HANDLED
}

print_history(to, id)
{
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    console_print(to, "[Undo] %s: %d to undo, %d to redo", name, g_count[id][S_UNDO], g_count[id][S_REDO])
    for (new stack = S_UNDO; stack <= S_REDO; stack++)
    {
        for (new pos = g_count[id][stack] - 1; pos >= 0; pos--)
        {
            new slot = at(id, stack, pos)
            new Float:origin[3]
            for (new axis = 0; axis < 3; axis++)
                origin[axis] = g_f[slot][F_NEW_O + axis]
            if (g_kind[slot] == K_DELETE)
            {
                for (new axis = 0; axis < 3; axis++)
                    origin[axis] = g_f[slot][F_OLD_O + axis]
            }
            new label[8]
            if (stack == S_UNDO)
                copy(label, charsmax(label), "undo")
            else
                copy(label, charsmax(label), "redo")
            console_print(to, "[Undo]   %s %d: %s %s block #%d at %.0f %.0f %.0f", label, g_count[id][stack] - pos,
                KIND_WORD[g_kind[slot]], g_type[slot], g_blockUid[slot], origin[0], origin[1], origin[2])
        }
    }
}

say_to(id, const key[], any:...)
{
    new pattern[160], message[192]
    formatex(pattern, charsmax(pattern), "%L", id, key)
    vformat(message, charsmax(message), pattern, 3)
    client_print_color(id, print_team_default, "^4[Undo]^1 %s", message)
    console_print(id, "[Undo] %s", message)
}

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.