/*
* Block Grid Snap (olds.ro workshop card IDEA-1FF6EB95975C2247)
*
* Blockmaker add-on: while a builder places or moves a block, its origin snaps
* to a grid and to the edges of nearby blocks, so courses line up without
* pixel-pushing. Each builder turns it on or off for themselves:
* say /snap toggle (the start value is olds_bmsnap_default)
*
* Cvars
* olds_bmsnap_grid 8 grid size in units (0 = no grid)
* olds_bmsnap_edge 6 an edge this close to another block's edge
* snaps to it (0 = no edge snap)
* olds_bmsnap_default 0 snap is on for builders who never used /snap
*
* The core's olds_bm_block_positioning forward (olds_blockmaker.inc) gives the
* new origin; on each axis the block goes flush against, or lines up with, the
* nearest edge of a nearby block within olds_bmsnap_edge, else to the nearest
* grid line. Only the origin changes: the rotation is never touched. When the
* block is let go, the builder reads the snapped coordinates in chat; while
* dragging they show in the centre of the screen.
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <fakemeta>
#include <olds_blockmaker>
#define PLUGIN "Block Grid Snap"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
const Float:CENTER_EVERY = 0.25
new Float:g_grid, Float:g_edge, g_default
new bool:g_set[MAX_PLAYERS + 1] // used /snap this connection
new bool:g_on[MAX_PLAYERS + 1]
new Float:g_nextCenter[MAX_PLAYERS + 1]
new g_maxEnts
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_bmsnap.txt")
bind_pcvar_float(create_cvar("olds_bmsnap_grid", "8", FCVAR_NONE,
"Grid size in units (0 = no grid).", true, 0.0, true, 256.0), g_grid)
bind_pcvar_float(create_cvar("olds_bmsnap_edge", "6", FCVAR_NONE,
"Snap to another block's edge within this many units (0 = off).", true, 0.0, true, 64.0), g_edge)
bind_pcvar_num(create_cvar("olds_bmsnap_default", "0", FCVAR_NONE,
"1 = snap is on for builders who never used /snap.", true, 0.0, true, 1.0), g_default)
AutoExecConfig(true, "olds_bmsnap")
register_clcmd("say /snap", "cmd_snap")
register_clcmd("say_team /snap", "cmd_snap")
g_maxEnts = global_get(glb_maxEntities)
}
public client_disconnected(id)
{
g_set[id] = false
g_on[id] = false
g_nextCenter[id] = 0.0
}
bool:snapping(id)
{
if (g_set[id])
return g_on[id]
return g_default != 0
}
public cmd_snap(id)
{
g_on[id] = !snapping(id)
g_set[id] = true
if (g_on[id])
client_print_color(id, print_team_default, "^4[Snap]^1 %L", id, "BMSNAP_ON", g_grid, g_edge)
else
client_print_color(id, print_team_default, "^4[Snap]^1 %L", id, "BMSNAP_OFF")
return PLUGIN_HANDLED
}
public olds_bm_block_positioning(builder, ent, Float:origin[3], const Float:size[3], release)
{
if (!(1 <= builder <= MaxClients) || !is_user_connected(builder) || !snapping(builder))
return
new Float:half[3], Float:best[3], bool:edged[3]
for (new axis = 0; axis < 3; axis++)
{
half[axis] = floatabs(size[axis]) / 2.0
best[axis] = g_edge + 0.001
}
new Float:target[3]
if (g_edge > 0.0)
edge_targets(ent, origin, half, best, target, edged)
for (new axis = 0; axis < 3; axis++)
{
if (edged[axis])
origin[axis] = target[axis]
else if (g_grid > 0.0)
origin[axis] = float(floatround(origin[axis] / g_grid)) * g_grid
}
show(builder, origin, edged, release != 0)
}
// For each axis, the closest edge position of a nearby block within olds_bmsnap_edge.
edge_targets(self, const Float:origin[3], const Float:half[3], Float:best[3], Float:target[3], bool:edged[3])
{
new type[OLDS_BM_TYPE_LEN], Float:other[3], Float:angles[3], Float:size[3]
new Float:lo[3], Float:hi[3], Float:myLo[3], Float:myHi[3]
for (new axis = 0; axis < 3; axis++)
{
myLo[axis] = origin[axis] - half[axis]
myHi[axis] = origin[axis] + half[axis]
}
for (new ent = MaxClients + 1; ent < g_maxEnts; ent++)
{
if (ent == self || !olds_bm_is_block(ent) || !olds_bm_get_block(ent, type, charsmax(type), other, angles, size))
continue
// Nearby: the boxes are no further apart than olds_bmsnap_edge on every axis.
new bool:near = true
for (new axis = 0; axis < 3 && near; axis++)
{
lo[axis] = other[axis] - floatabs(size[axis]) / 2.0
hi[axis] = other[axis] + floatabs(size[axis]) / 2.0
if (myLo[axis] > hi[axis] + g_edge || myHi[axis] < lo[axis] - g_edge)
near = false
}
if (!near)
continue
for (new axis = 0; axis < 3; axis++)
{
// Flush on either side, or the same edge lined up.
try_target(axis, origin, hi[axis] + half[axis], best, target, edged)
try_target(axis, origin, lo[axis] - half[axis], best, target, edged)
try_target(axis, origin, lo[axis] + half[axis], best, target, edged)
try_target(axis, origin, hi[axis] - half[axis], best, target, edged)
}
}
}
try_target(axis, const Float:origin[3], Float:value, Float:best[3], Float:target[3], bool:edged[3])
{
new Float:distance = floatabs(value - origin[axis])
if (distance < best[axis])
{
best[axis] = distance
target[axis] = value
edged[axis] = true
}
}
show(id, const Float:origin[3], const bool:edged[3], bool:release)
{
new marks[3][4]
for (new axis = 0; axis < 3; axis++)
{
if (edged[axis])
copy(marks[axis], charsmax(marks[]), "|")
else
copy(marks[axis], charsmax(marks[]), "")
}
if (release)
{
client_print_color(id, print_team_default, "^4[Snap]^1 %L", id, "BMSNAP_AT", origin[0], marks[0], origin[1],
marks[1], origin[2], marks[2])
console_print(id, "[Snap] %.1f%s %.1f%s %.1f%s", origin[0], marks[0], origin[1], marks[1], origin[2], marks[2])
return
}
new Float:now = get_gametime()
if (now < g_nextCenter[id])
return
g_nextCenter[id] = now + CENTER_EVERY
client_print(id, print_center, "%.0f%s %.0f%s %.0f%s", origin[0], marks[0], origin[1], marks[1], origin[2], marks[2])
}