util

package
v0.0.0-...-db92557 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package util provides utility functions and general-use types.

Index

Constants

View Source
const IndexInvalid uint16 = 0xFFFF

Variables

View Source
var DirectionOffsets = []Point{
	{0, -1},
	{1, -1},
	{1, 0},
	{1, 1},
	{0, 1},
	{-1, 1},
	{-1, 0},
	{-1, -1},
}

Offsets for each direction

View Source
var FacingOffsets = []Point{
	{0, -1},
	{1, 0},
	{0, 1},
	{-1, 0},
}

Offsets for each facing

Functions

func GetBool

func GetBool(r io.Reader) bool

GetBool returns the next boolean value in the data buffer.

func GetByte

func GetByte(r io.Reader) byte

GetByte is a convenience function that returns the next byte in the buffer.

func GetBytes

func GetBytes(r io.Reader) []byte

GetBytes returns the next byte slice in the data buffer.

func GetFloat

func GetFloat(r io.Reader) float64

GetFloat returns the next 64-bit floating-point value.

func GetString

func GetString(r io.Reader) string

GetString returns the next null-terminated string in the data buffer.

func GetTime

func GetTime(r io.Reader) time.Time

GetTime returns the next time.Time value in the data buffer.

func GetUint16

func GetUint16(r io.Reader) uint16

GetUint16 returns the next unsigned 16-bit integer in the data buffer.

func GetUint32

func GetUint32(r io.Reader) uint32

GetUint32 returns the next unsigned 32-bit integer in the data buffer.

func GetUint64

func GetUint64(r io.Reader) uint64

GetUint64 returns the next unsigned 64-bit integer in the data buffer.

func Log

func Log(fmt string, args ...any)

func Pad

func Pad(w io.Writer, l int)

Pad writes zero-padding

func PutBool

func PutBool(w io.Writer, v bool)

PutBool writes a boolean value

func PutByte

func PutByte(w io.Writer, v byte)

PutByte writes a single byte

func PutBytes

func PutBytes(w io.Writer, d []byte)

PutBytes puts a slice of bytes to the writer.

func PutDictionary

func PutDictionary(w io.Writer, d *Dictionary)

PutDictionary writes a Dictionary structure to the writer.

func PutFloat

func PutFloat(w io.Writer, v float64)

PutFloat writes a 64-bit floating-point value.

func PutPoint

func PutPoint(w io.Writer, p Point)

PutPoint writes a Point value to the writer.

func PutRect

func PutRect(w io.Writer, r Rect)

PutRect writes a Rect value to the writer.

func PutString

func PutString(w io.Writer, s string)

PutString writes a null-terminated string

func PutTime

func PutTime(w io.Writer, t time.Time)

PutTime writes a time.Time value to the writer.

func PutUint16

func PutUint16(w io.Writer, v uint16)

PutUint16 writes a 16-bit numeric value

func PutUint32

func PutUint32(w io.Writer, v uint32)

PutUint32 writes a 32-bit numeric value

func PutUint64

func PutUint64(w io.Writer, v uint64)

PutUint64 writes a 64-bit numeric value

func Random

func Random(min, max int) int

Random returns a random int within the half-open range [min-max).

func RandomBool

func RandomBool() bool

RandomBool returns a random boolean value.

func RandomF

func RandomF(min, max float64) float64

RandomF returns a random floating point value in the half-open range [min-max).

func RandomValue

func RandomValue[T any](s []T) T

RandomValue returns a random value from the slice.

Types

type Dictionary

type Dictionary struct {
	Indexes map[string]uint16 // Map of indexes for strings
	// contains filtered or unexported fields
}

Dictionary implements a string dictionary that can be persisted to disk.

func GetDictionary

func GetDictionary(r io.Reader) *Dictionary

GetDirectory returns the next Directory structure in the data buffer.

func NewDictionary

func NewDictionary() *Dictionary

NewDictionary returns a new Dictionary ready for use.

func (*Dictionary) Get

func (d *Dictionary) Get(s string) uint16

Get returns the index associated with the string, or IndexInvalid if the string is not in the dictionary.

func (*Dictionary) Lookup

func (d *Dictionary) Lookup(i uint16) string

Lookup returns the string associated with the index.

func (*Dictionary) Put

func (d *Dictionary) Put(s string) uint16

Put puts a string into the dictionary and returns the index allocated.

type Direction

type Direction byte

Direction represents one of the eight compass rose points.

const (
	DirectionNorth Direction = iota
	DirectionNorthEast
	DirectionEast
	DirectionSouthEast
	DirectionSouth
	DirectionSouthWest
	DirectionWest
	DirectionNorthWest
	DirectionInvalid Direction = 0xFF
)

func (Direction) Bound

func (d Direction) Bound() Direction

Bound returns a Direction value wrapped and bounded.

func (Direction) Facing

func (d Direction) Facing() Facing

Facing returns the equivalent facing for this direction.

func (Direction) IsDiagonal

func (d Direction) IsDiagonal() bool

IsDiagonal returns true if the direction is one of the diagonals.

func (Direction) RotateClockwise

func (d Direction) RotateClockwise(n int) Direction

RotateClockwise rotates the direction clockwise by n steps.

func (Direction) RotateCounterclockwise

func (d Direction) RotateCounterclockwise(n int) Direction

RotateCounterclockwise rotates the direction counterclockwise by n steps.

type Facing

type Facing uint8

Facing represents one of the four cardinal directions.

const (
	FacingNorth Facing = iota
	FacingEast
	FacingSouth
	FacingWest
	FacingInvalid
)

func (Facing) Bound

func (f Facing) Bound() Facing

Bound returns a Facing value wrapped and bounded.

func (Facing) Direction

func (f Facing) Direction() Direction

Direction returns the equivalent Direction value.

func (Facing) MarshalJSON

func (f Facing) MarshalJSON() ([]byte, error)

func (Facing) Rotate

func (f Facing) Rotate(a Facing) Facing

Rotate rotates the facing to the given facing, assuming the original facing was North.

func (*Facing) UnmarshalJSON

func (f *Facing) UnmarshalJSON(in []byte) error

type FloodFill

type FloodFill struct {
	Matches func(Point) bool // Returns true if the point matches the source
	Set     func(Point)      // Sets the point
}

FloodFill implements a flood-fill algorithm.

func (*FloodFill) Execute

func (f *FloodFill) Execute(p Point)

Execute executes the flood fill algorithm. Matches and Set must be non-nil.

type Point

type Point struct {
	X int // X component
	Y int // Y component
}

Point represents an integer point in 2D space.

func GetPoint

func GetPoint(r io.Reader) Point

GetPoint returns the next Point value in the data buffer.

func NewPoint

func NewPoint(x, y int) Point

NewPoint returns a new Point value.

func RandomPoint

func RandomPoint(b Rect) Point

RandomPoint returns a random point within the rect.

func Ray

func Ray(p1, p2 Point) []Point

Ray returns a slice of points along the ray, including both the starting and ending points. Subsequent calls to Ray reuse the same return slice.

func (Point) Add

func (p Point) Add(a Point) Point

Add returns the result of adding the two points X and Y values.

func (Point) DirectionTo

func (p Point) DirectionTo(a Point) Direction

DirectionTo returns the direction code that most closely matches the direction of the argument point.

func (Point) Distance

func (p Point) Distance(d Point) int

Distance returns the maximum distance from p to d along either the X or Y axis.

func (Point) Divide

func (p Point) Divide(a int) Point

Divide returns the result of dividing p by a.

func (Point) Multiply

func (p Point) Multiply(a int) Point

Multiply returns the result of multiplying a and p.

func (Point) Step

func (p Point) Step(d Direction) Point

Step returns the result of stepping in direction d from point p.

func (Point) StepFacing

func (p Point) StepFacing(f Facing) Point

StepFacing returns the result of stepping in facing f from point p.

func (Point) Sub

func (p Point) Sub(a Point) Point

Sub returns the result of subtracting a from p.

type Rect

type Rect struct {
	TL Point // Top-left point
	BR Point // Bottom-right point
}

Rect represents integer 2D bounds.

func GetRect

func GetRect(r io.Reader) Rect

GetRect returns the next Rect value in the data buffer.

func NewRect

func NewRect(a, b Point) Rect

NewRect creates a new Bounds object from two points regardless of order.

func NewRectFromExtents

func NewRectFromExtents(cp Point, left, right, up, down int) Rect

NewRectFromExtents creates a new rect centered on point p with the given extents.

func NewRectFromFacing

func NewRectFromFacing(p Point, w, h int, f Facing) Rect

NewRectFromFacing creates a new rect starting at point p with width w and height h and extends from that point as appropriate for facing f.

func NewRectFromPoints

func NewRectFromPoints(ps ...Point) Rect

NewRectFromPoints creates a new Rect that contains all of the given points.

func NewRectFromRadius

func NewRectFromRadius(p Point, r int) Rect

NewRectFromRadius creates a new rect centered on point p with radius r.

func NewRectWH

func NewRectWH(w, h int) Rect

NewRectWH creates a new Rect value with the given dimensions.

func NewRectXYWH

func NewRectXYWH(x, y, w, h int) Rect

NewRectXYWH creates a new Rect value with the given dimensions and offset.

func (Rect) Add

func (r Rect) Add(p Point) Rect

Add adds the point to both points of the rect.

func (Rect) Area

func (r Rect) Area() int

Area returns the width of the rect multiplied by its height.

func (Rect) Bound

func (r Rect) Bound(p Point) Point

Bound bounds a point to the rect, such that the point is forced inside the rect along the axis where necessary.

func (Rect) Center

func (r Rect) Center() Point

Center returns the centerpoint of the rect.

func (Rect) CenterRect

func (r Rect) CenterRect(w, h int) Rect

CenterRect returns the center a rect from the center of this rect with the given dimensions.

func (Rect) Contain

func (r Rect) Contain(b Rect) Rect

Contain returns the rect contained within this rect, that is moved along the axis so that b is contained within r. If any of the dimensions of b are larger than that dimension in r the results are undefined.

func (Rect) ContainingRect

func (r Rect) ContainingRect(a Rect) Rect

ContainingRect returns the rect that contains both r and a.

func (Rect) Contains

func (r Rect) Contains(p Point) bool

Contains returns true if the point is contained within the rect.

func (Rect) ContainsRect

func (r Rect) ContainsRect(b Rect) bool

ContainsRect returns true if r fully contains b.

func (Rect) Divide

func (r Rect) Divide(a int) Rect

Divide divides all of the points of the rect by a.

func (Rect) Extents

func (r Rect) Extents(cp Point) (left, right, top, bottom int)

Extents returns the extents of the rect from the given centerpoint. If the point lies outside the rect, the result is undefined.

func (Rect) Grow

func (r Rect) Grow(n int) Rect

Grow removes n tiles from all sides of the rect.

func (Rect) Height

func (r Rect) Height() int

Height returns the height of the rect.

func (Rect) Move

func (r Rect) Move(o Point) Rect

Move moves the rect without modifying the width or height.

func (Rect) MoveRelative

func (r Rect) MoveRelative(o Point) Rect

Move moves the rect relative to the current position without modifying the width or height.

func (Rect) Multiply

func (r Rect) Multiply(a int) Rect

Multiply multiplies all of the points of the rect by a.

func (Rect) Overlap

func (r Rect) Overlap(a Rect) Rect

Overlap returns the overlapping rect between r and a. If there is no overlap the zero value is returned.

func (Rect) Overlaps

func (r Rect) Overlaps(a Rect) bool

Overlaps returns true if r and a overlap.

func (Rect) RandomSubRect

func (r Rect) RandomSubRect(w, h int) Rect

RandomSubRect returns a random sub rectangle completely contained within this rectangle but in a random position. If the bounds given are larger than the bounds of this rect the result is undefined.

func (Rect) ReverseRotatePoint

func (r Rect) ReverseRotatePoint(p Point, f Facing) Point

ReverseRotatePoint rotates the given relative point within the rect as if the rect is rotated to the given facing and returns the point to a North facing.

func (Rect) Rotate

func (r Rect) Rotate(cp Point, f Facing) Rect

Rotate rotates the rect to the given facing about the given point, assuming the rect is currently facing North.

func (Rect) RotateInPlace

func (r Rect) RotateInPlace(f Facing) Rect

RotateInPlace rotates the rect to the given facing keeping the center tile in place when possible.

func (Rect) RotatePointRelative

func (r Rect) RotatePointRelative(p Point, f Facing) Point

RotatePointRelative rotates the given relative point within the rect to translate from the relative position at facing North to the facing given.

func (Rect) RotateRect

func (r Rect) RotateRect(a Rect, f Facing) Rect

RotateRect rotates the given rect within this rect assuming the given rect is in a North facing.

func (Rect) Shrink

func (r Rect) Shrink(n int) Rect

Shrink removes n tiles from all sides of the rect.

func (Rect) Width

func (r Rect) Width() int

Width returns the width of the rect.

Jump to

Keyboard shortcuts

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