gridutil

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package gridutil provides utility functions for grid-based pathfinding. All functions work with any grid type that satisfies the minimal interfaces defined here (GridLOS, GridWorld). No grid/finder package imports needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BresenhamLine added in v0.8.0

func BresenhamLine(x0, y0, x1, y1 int) [][2]int

BresenhamLine returns all tiles on the Bresenham line from (x0, y0) to (x1, y1), including both endpoints.

func CompressPath added in v0.8.0

func CompressPath(path [][2]int) [][2]int

CompressPath removes collinear waypoints from a tile path without any line-of-sight checking. Only points where the direction changes are kept. This is guaranteed to never cut corners since it only removes truly redundant waypoints. Writes the result in-place over the input buffer.

func HasLineOfSightBresenham added in v0.8.0

func HasLineOfSightBresenham(g GridLOS, x1, y1, x2, y2 int) bool

HasLineOfSightBresenham reports whether two tiles can see each other — every tile on the Bresenham line between them is walkable. Only suitable for orthogonal grids. For hex and staggered grids, use HasLineOfSightDense instead.

func HasLineOfSightDense added in v0.8.0

func HasLineOfSightDense(g GridWorld, x1, y1, x2, y2 int) bool

HasLineOfSightDense reports whether two tiles can see each other using world-space sub-tile sampling. Suitable for all grid types. For orthogonal grids, HasLineOfSightBresenham is more efficient.

func HasLineOfSightDenseWorld added in v0.8.0

func HasLineOfSightDenseWorld(g GridWorld, wx1, wy1, wx2, wy2 float32) bool

HasLineOfSightDenseWorld is like HasLineOfSightDense but takes world coordinates.

func RandomInRing

func RandomInRing(g GridLOS, cx, cy, innerR, outerR int) (int, int, bool)

RandomInRing returns a random walkable tile with Chebyshev distance between innerR and outerR from (cx, cy).

func RandomInRingWorld

func RandomInRingWorld(g GridWorld, wx, wy float32, innerR, outerR int) (float32, float32, bool)

RandomInRingWorld returns the world center of a random walkable tile with Chebyshev distance between innerR and outerR from (wx, wy).

func RandomWalkable

func RandomWalkable(g GridLOS) (int, int, bool)

RandomWalkable returns a random walkable tile coordinate.

func RandomWalkableInRadius

func RandomWalkableInRadius(g GridLOS, cx, cy, radius int) (int, int, bool)

RandomWalkableInRadius returns a random walkable tile within radius tiles of (cx, cy).

func RandomWalkableInRadiusWorld

func RandomWalkableInRadiusWorld(g GridWorld, wx, wy float32, radius int) (float32, float32, bool)

RandomWalkableInRadiusWorld returns the world center of a random walkable tile within radius tiles of (wx, wy).

func RandomWalkableWorld

func RandomWalkableWorld(g GridWorld) (float32, float32, bool)

RandomWalkableWorld returns the world center of a random walkable tile.

func SmoothenBresenham added in v0.8.0

func SmoothenBresenham(g GridLOS, path [][2]int) [][2]int

SmoothenBresenham removes unnecessary waypoints from a tile path using Bresenham line-of-sight (greedy string-pulling). Only suitable for orthogonal grids. For hex and staggered grids, use SmoothenDense instead. Writes the result in-place over the input buffer (the finder's cached pathBuf).

func SmoothenBresenhamStrict added in v0.8.0

func SmoothenBresenhamStrict(g GridLOS, path [][2]int) [][2]int

SmoothenBresenhamStrict is like SmoothenBresenham but also checks corner cells at each diagonal step, preventing the smoothed path from cutting through the corner of a blocked tile. Only suitable for orthogonal grids.

func SmoothenDense added in v0.8.0

func SmoothenDense(g GridWorld, path [][2]int) [][2]int

SmoothenDense removes unnecessary waypoints from a tile path using world-space line-of-sight sampling. Suitable for all grid types. Writes the result in-place over the input buffer.

func TileDistance

func TileDistance(x1, y1, x2, y2 int) int

TileDistance returns the Chebyshev distance between two tiles: max(|dx|, |dy|). This is the number of steps needed for 8-directional movement.

func TilesInRadius

func TilesInRadius(cx, cy, r int) [][2]int

TilesInRadius returns all tiles within Chebyshev distance r from (cx, cy).

Types

type GridLOS added in v0.8.0

type GridLOS interface {
	Width() int
	Height() int
	IsWalkableAt(x, y int) bool
}

GridLOS is the interface for tile-space operations (LOS, smoothing). All grid types (*OrthogonalGrid, *HexGrid, *StaggeredGrid) satisfy it.

type GridWorld added in v0.8.0

type GridWorld interface {
	GridLOS
	TileToWorld(tx, ty int) (float32, float32)
	WorldToTile(wx, wy float32) (int, int)
	TileWidth() int
	TileHeight() int
}

GridWorld extends GridLOS with world coordinate conversion. Needed by SmoothenPathDense and other world-space utilities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL