grid

package
v0.0.0-...-883f3e4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TakePosition

func TakePosition(pos Position, taker SpaceTaker) actions.Action

TakePosition builds an action that tries to immediately take the given position with the given taker. If the position is taken, the action fails, interrupted.

Types

type HeadedPlacement

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

A HeadedPlacement is either:

  • nowhere
  • at a point
  • moving from one point to another

func (*HeadedPlacement) AtPoint

func (hp *HeadedPlacement) AtPoint() Point

AtPoint the current point of current the placement. As a special case it returns the zero value when the placement is not placed anywhere. Call Placed to distinguish that from a placement at that position.

func (*HeadedPlacement) FollowPath

func (hp *HeadedPlacement) FollowPath(path Path, stepDt time.Duration) actions.Action

FollowPath creates an action that will move the heading along the path. Each step will take stepDt.

When first run, the action fails immediately if the heading is not at the initial position of the path.

func (*HeadedPlacement) Headed

func (hp *HeadedPlacement) Headed() bool

Headed says whether the placement is headed somewhere. Nothing else is able to occupy the position where the placement is headed at the same time.

func (*HeadedPlacement) Heading

func (hp *HeadedPlacement) Heading() Point

Heading is the point where the placement is headed. As a special case it returns the zero value when the placement is headed nowhere. Call Headed to distinguish that from a placement headed there.

func (*HeadedPlacement) MoveTo

func (hp *HeadedPlacement) MoveTo(dst Position, dt time.Duration) actions.Action

MoveTo creates an action that will try to move the placement to dst over a duration of dt. The action gets interrupted if the dst position is taken as it starts.

func (*HeadedPlacement) Place

func (hp *HeadedPlacement) Place(pos Position) bool

Place tries to put the placement at the given position. The return value says whether this has succeded.

func (*HeadedPlacement) Placed

func (hp *HeadedPlacement) Placed() bool

Placed says whether the placement takes up some position. Nothing else is be able to occupy the same position at the same time.

func (*HeadedPlacement) Progress

func (hp *HeadedPlacement) Progress() float64

Progress says how far along the placement is in the move from the start position to the heading. This is 1 when the placement is not headed anywhere.

type OnePosTaker

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

A OnePosTaker is a space taker that occupies at most one grid position at a time.

func (*OnePosTaker) AtPoint

func (taker *OnePosTaker) AtPoint() Point

AtPoint is the point at which the taken position is.

The zero value of the type is returned when the taker is not placed. Do not use that to check if the taker is placed. A taker might be taking the position at the zero point.

func (*OnePosTaker) ForceOff

func (taker *OnePosTaker) ForceOff(pos Position)

ForceOff is part of the SpaceTaker interface.

func (*OnePosTaker) Leave

func (taker *OnePosTaker) Leave()

Leave makes the taker leave a position if it has one taken.

func (*OnePosTaker) LetOnto

func (taker *OnePosTaker) LetOnto(pos Position)

LetOnto is part of the SpaceTaker interface.

func (*OnePosTaker) Placed

func (taker *OnePosTaker) Placed() bool

Placed says whether the space taker is taking a position.

type Path

type Path []Position

A Path is a sequence of positions.

type PathFinder

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

A PathFinder finds paths from one point to another.

func NewPathFinder

func NewPathFinder(space *Space) PathFinder

NewPathFinder creates a path finder that searches for path through the specified space.

The path finder will be sensitive to what positions do and do not exist in the space. The space can be modified after the path finder is created - it will be aware of the updates.

func (PathFinder) FindPath

func (pf PathFinder) FindPath(src, dst Position) (path Path, exists bool)

FindPath searches for a path from src to dst.

When a path cannot be found it reports so and returns a path containing exactly src. Otherwise it returns a viable path.

func (PathFinder) IsViable

func (pf PathFinder) IsViable(path Path) bool

IsViable validates a path.

It will be false if the path contains positions from a different space. It will be false if the path contains positions that cannot be occupied. It will be false if the path contains two consecutive positions that are not neighbours.

type Point

type Point struct {
	Row, Column int
}

A Point is a point on a 2D grid.

func P

func P(row, column int) Point

P creates a position at the said row and column.

type Position

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

A Position within some space.

func (Position) AtPoint

func (pos Position) AtPoint() Point

AtPoint is the point at which the position is in the space that contains it.7

func (Position) Create

func (pos Position) Create() bool

Create ensures that a position within a space exists. It fails when the position exists.

func (Position) Destroy

func (pos Position) Destroy() bool

Destroy ensures that a position within a space does not exist. It fails when the position does not exist or is taken.

func (Position) Exists

func (pos Position) Exists() bool

Exists says whether the position within the space exists.

func (Position) Free

func (pos Position) Free() bool

Free tries to mark the position as no longer taken. It fails if the position is not taken.

func (Position) Take

func (pos Position) Take(taker SpaceTaker) bool

Take tries to mark the position as taken. It fails if the position does not exist or is free.

func (Position) Taken

func (pos Position) Taken() bool

Taken says whether the position is currently taken.

type Space

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

A Space where things can exist and interact.

It is a subspace of a 2D grid. Which positions on the grid exist can change dynamically.

func NewSpace

func NewSpace() *Space

NewSpace creates a new, empty space.

func (*Space) At

func (space *Space) At(at Point) Position

At returns the position at the given point in the space.

Two position values returned for the same point in the space will be equal.

func (*Space) Max

func (space *Space) Max() Point

Max is the point with the column of the rightmost position and row of the top one.

func (*Space) Min

func (space *Space) Min() Point

Min is the point with the column of the leftmost position and row of the bottom one.

type SpaceTaker

type SpaceTaker interface {
	// LetOnto tells the space taker that it is now taking up pos.
	LetOnto(pos Position)
	// ForceOff tells the space taker that it is no longer taking up pos.
	ForceOff(pos Position)
}

A SpaceTaker is the thing that takes up a taken position.

A space taker can take up multiple positions at once.

func DummyTaker

func DummyTaker() SpaceTaker

DummyTaker returns a space taker that only takes up the position it takes. It has no additional behaviour.

Jump to

Keyboard shortcuts

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