Skip to content

Pathfind

Auto-generated from src/stdlib/pathfind.mcrs — do not edit manually.

API


pf_pack v1.0.0

Encode a (x, z) grid coordinate pair as a single packed integer.

redscript
fn pf_pack(x: int, z: int): int

Parameters

ParameterDescription
xGrid X coordinate [0, 15]
zGrid Z coordinate [0, 15]

Returns: x * 16 + z (unique index in [0, 255])

Example

redscript
let p: int = pf_pack(3, 7)  // result: 55

pf_unpack_x v1.0.0

Extract the X coordinate from a packed grid index.

redscript
fn pf_unpack_x(packed: int): int

Parameters

ParameterDescription
packedPacked coordinate from pf_pack

Returns: X component: packed / 16

Example

redscript
let x: int = pf_unpack_x(55)  // result: 3

pf_unpack_z v1.0.0

Extract the Z coordinate from a packed grid index.

redscript
fn pf_unpack_z(packed: int): int

Parameters

ParameterDescription
packedPacked coordinate from pf_pack

Returns: Z component: packed % 16

Example

redscript
let z: int = pf_unpack_z(55)  // result: 7

pf_new_map v1.0.0

Allocate a new 16×16 grid obstacle map with all cells passable.

redscript
fn pf_new_map(): int[]

Returns: int[] of 256 zeros; index = pf_pack(x, z), 0=passable, 1=blocked

Example

redscript
let map: int[] = pf_new_map()

pf_set_blocked v1.0.0

Mark a grid cell as impassable (blocked).

redscript
fn pf_set_blocked(map: int[], x: int, z: int)

Parameters

ParameterDescription
mapObstacle map from pf_new_map
xCell X coordinate [0, 15]
zCell Z coordinate [0, 15]

Returns: void — sets map[pf_pack(x,z)] = 1

Example

redscript
pf_set_blocked(map, 5, 3)  // block cell (5, 3)

pf_set_open v1.0.0

Mark a grid cell as passable (open).

redscript
fn pf_set_open(map: int[], x: int, z: int)

Parameters

ParameterDescription
mapObstacle map from pf_new_map
xCell X coordinate [0, 15]
zCell Z coordinate [0, 15]

Returns: void — sets map[pf_pack(x,z)] = 0

Example

redscript
pf_set_open(map, 5, 3)  // re-open previously blocked cell (5, 3)

pf_is_blocked v1.0.0

Check whether a grid cell is blocked or out of bounds.

redscript
fn pf_is_blocked(map: int[], x: int, z: int): int

Parameters

ParameterDescription
mapObstacle map from pf_new_map
xCell X coordinate
zCell Z coordinate

Returns: 1 if out of bounds (x or z outside [0,15]) or cell is blocked, 0 if passable

Example

redscript
let blocked: int = pf_is_blocked(map, 5, 3)

pf_heuristic v1.0.0

Compute Manhattan distance between two grid cells, scaled ×10000.

redscript
fn pf_heuristic(x1: int, z1: int, x2: int, z2: int): int

Parameters

ParameterDescription
x1First cell X
z1First cell Z
x2Second cell X
z2Second cell Z

Returns: (|x1-x2| + |z1-z2|) * 10000 — useful as A* heuristic

Example

redscript
let h: int = pf_heuristic(0, 0, 3, 4)  // result: 70000 (7 Manhattan steps × 10000)

pathfind_bfs v1.0.0

Find the shortest path between two cells on a 16×16 grid using BFS.

redscript
fn pathfind_bfs(map: int[], sx: int, sz: int, gx: int, gz: int): int[]

Parameters

ParameterDescription
mapObstacle map from pf_new_map (with pf_set_blocked applied)
sxStart cell X [0, 15]
szStart cell Z [0, 15]
gxGoal cell X [0, 15]
gzGoal cell Z [0, 15]

Returns: int[] of packed coords (pf_pack) from start to goal inclusive, or [] if no path

Example

redscript
let map: int[] = pf_new_map()
pf_set_blocked(map, 3, 5)
let path: int[] = pathfind_bfs(map, 0, 0, 7, 7)
let x0: int = pf_unpack_x(path[0])

pf_noop v1.0.0

Default no-op onDone callback for pathfind_bfs_coro. Replace with your own handler.

redscript
fn pf_noop()

Released under the MIT License.