Skip to content

server.arena

Constants #

const max_volume = 256 * 256 * 256

max_volume caps how many blocks a single snapshot may capture so a bad min/max box can't allocate unbounded memory. 256256256 is generous for a minigame arena while still bounded.

fn capture #

fn capture(mut src BlockSource, box Box) !&Snapshot

capture reads every block in box from src into a fresh Snapshot. Returns an error if the box exceeds max_volume so a runaway box can't exhaust memory.

fn new_box #

fn new_box(x1 int, y1 int, z1 int, x2 int, y2 int, z2 int) Box

new_box normalizes the two corners so the caller need not order them.

interface BlockSink #

interface BlockSink {
mut:
	set_block_id(id int, x int, y int, z int)
}

BlockSink writes a block by network id and broadcasts the change to viewers. Restore goes through this so a reset looks identical to normal block edits.

interface BlockSource #

interface BlockSource {
mut:
	get_block(x int, y int, z int) int
}

BlockSource reads block network ids from a world by absolute coordinates. Hub satisfies it; tests use an in-memory grid.

struct Box #

struct Box {
pub:
	min_x int
	min_y int
	min_z int
	max_x int
	max_y int
	max_z int
}

Box is an axis-aligned block region, normalized so min <= max on every axis.

fn (Box) volume #

fn (b Box) volume() i64

volume is the number of blocks the box covers (inclusive on both corners). Computed in i64 so a huge box can't overflow int32 and slip past the cap.

struct Snapshot #

struct Snapshot {
pub:
	box Box
mut:
	ids []int
}

Snapshot holds the block ids of a Box captured at some point in time. It is a flat array so a full restore is a single linear pass. Memory cost is 4 bytes per block - a 64^3 arena is ~1 MB.

fn (Snapshot) restore #

fn (s &Snapshot) restore(mut sink BlockSink)

restore writes every captured block back through sink in the same order it was captured, so viewers see the arena reset to its saved state.

fn (Snapshot) len #

fn (s &Snapshot) len() int

len is the number of blocks stored in the snapshot.