flowfield

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 flowfield implements Flow Field pathfinding for efficient group movement. Instead of computing per-agent paths, it builds a vector field from the goal outward (one Dijkstra pass), then every agent looks up its direction in O(1).

Single target:

ff := flowfield.New(grid, goalX, goalY)
for _, a := range agents {
    dx, dy := ff.GetDirection(a.X, a.Y)
    a.X += dx; a.Y += dy
}

Multiple targets (each tile routes to the nearest goal):

ff := flowfield.NewMulti(grid, [][2]int{{3,5}, {9,7}})
ff.Reset(7, 3)                          // switch to single goal
ff.ResetMulti([][2]int{{1,1}, {9,9}})   // switch to multi goal

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field holds the precomputed integration and vector fields.

func New

func New(grid finder.Grid, goalX, goalY int, opts ...Option) *Field

New builds a flow field toward a single goal tile.

func NewMulti

func NewMulti(grid finder.Grid, goals [][2]int, opts ...Option) *Field

NewMulti builds a flow field toward multiple goals. Each tile routes to the nearest goal automatically.

func (*Field) FindPath

func (ff *Field) FindPath(startX, startY int) [][2]int

FindPath backtracks from (x, y) by following the direction chain to the nearest goal. Useful for debugging and visualization.

func (*Field) GetCost

func (ff *Field) GetCost(x, y int) float64

GetCost returns the integration cost (distance to nearest goal). Returns -1 for unreachable tiles.

func (*Field) GetDirection

func (ff *Field) GetDirection(x, y int) (int, int)

GetDirection returns the movement direction for tile (x, y). Returns (0,0) if the tile is blocked or unreachable.

func (*Field) Reset

func (ff *Field) Reset(goalX, goalY int)

Reset recalculates the field for a new single goal. Reuses memory.

func (*Field) ResetMulti

func (ff *Field) ResetMulti(goals [][2]int)

ResetMulti recalculates the field for multiple goals. Reuses memory.

type Option

type Option func(*Field)

Option configures the flow field.

func WithDiagonal

func WithDiagonal(d finder.DiagonalMovement) Option

WithDiagonal sets the diagonal movement rule. Defaults to DiagonalNever (cardinal-only).

Jump to

Keyboard shortcuts

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