Documentation
¶
Overview ¶
Package util provides utility functions and general-use types.
Index ¶
- Constants
- Variables
- func GetBool(r io.Reader) bool
- func GetByte(r io.Reader) byte
- func GetBytes(r io.Reader) []byte
- func GetFloat(r io.Reader) float64
- func GetString(r io.Reader) string
- func GetTime(r io.Reader) time.Time
- func GetUint16(r io.Reader) uint16
- func GetUint32(r io.Reader) uint32
- func GetUint64(r io.Reader) uint64
- func Log(fmt string, args ...any)
- func Pad(w io.Writer, l int)
- func PutBool(w io.Writer, v bool)
- func PutByte(w io.Writer, v byte)
- func PutBytes(w io.Writer, d []byte)
- func PutDictionary(w io.Writer, d *Dictionary)
- func PutFloat(w io.Writer, v float64)
- func PutPoint(w io.Writer, p Point)
- func PutRect(w io.Writer, r Rect)
- func PutString(w io.Writer, s string)
- func PutTime(w io.Writer, t time.Time)
- func PutUint16(w io.Writer, v uint16)
- func PutUint32(w io.Writer, v uint32)
- func PutUint64(w io.Writer, v uint64)
- func Random(min, max int) int
- func RandomBool() bool
- func RandomF(min, max float64) float64
- func RandomValue[T any](s []T) T
- type Dictionary
- type Direction
- type Facing
- type FloodFill
- type Point
- func (p Point) Add(a Point) Point
- func (p Point) DirectionTo(a Point) Direction
- func (p Point) Distance(d Point) int
- func (p Point) Divide(a int) Point
- func (p Point) Multiply(a int) Point
- func (p Point) Step(d Direction) Point
- func (p Point) StepFacing(f Facing) Point
- func (p Point) Sub(a Point) Point
- type Rect
- func GetRect(r io.Reader) Rect
- func NewRect(a, b Point) Rect
- func NewRectFromExtents(cp Point, left, right, up, down int) Rect
- func NewRectFromFacing(p Point, w, h int, f Facing) Rect
- func NewRectFromPoints(ps ...Point) Rect
- func NewRectFromRadius(p Point, r int) Rect
- func NewRectWH(w, h int) Rect
- func NewRectXYWH(x, y, w, h int) Rect
- func (r Rect) Add(p Point) Rect
- func (r Rect) Area() int
- func (r Rect) Bound(p Point) Point
- func (r Rect) Center() Point
- func (r Rect) CenterRect(w, h int) Rect
- func (r Rect) Contain(b Rect) Rect
- func (r Rect) ContainingRect(a Rect) Rect
- func (r Rect) Contains(p Point) bool
- func (r Rect) ContainsRect(b Rect) bool
- func (r Rect) Divide(a int) Rect
- func (r Rect) Extents(cp Point) (left, right, top, bottom int)
- func (r Rect) Grow(n int) Rect
- func (r Rect) Height() int
- func (r Rect) Move(o Point) Rect
- func (r Rect) MoveRelative(o Point) Rect
- func (r Rect) Multiply(a int) Rect
- func (r Rect) Overlap(a Rect) Rect
- func (r Rect) Overlaps(a Rect) bool
- func (r Rect) RandomSubRect(w, h int) Rect
- func (r Rect) ReverseRotatePoint(p Point, f Facing) Point
- func (r Rect) Rotate(cp Point, f Facing) Rect
- func (r Rect) RotateInPlace(f Facing) Rect
- func (r Rect) RotatePointRelative(p Point, f Facing) Point
- func (r Rect) RotateRect(a Rect, f Facing) Rect
- func (r Rect) Shrink(n int) Rect
- func (r Rect) Width() int
Constants ¶
const IndexInvalid uint16 = 0xFFFF
Variables ¶
var DirectionOffsets = []Point{
{0, -1},
{1, -1},
{1, 0},
{1, 1},
{0, 1},
{-1, 1},
{-1, 0},
{-1, -1},
}
Offsets for each direction
var FacingOffsets = []Point{
{0, -1},
{1, 0},
{0, 1},
{-1, 0},
}
Offsets for each facing
Functions ¶
func PutDictionary ¶
func PutDictionary(w io.Writer, d *Dictionary)
PutDictionary writes a Dictionary structure to the writer.
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.
func (Direction) IsDiagonal ¶
IsDiagonal returns true if the direction is one of the diagonals.
func (Direction) RotateClockwise ¶
RotateClockwise rotates the direction clockwise by n steps.
func (Direction) RotateCounterclockwise ¶
RotateCounterclockwise rotates the direction counterclockwise by n steps.
type Facing ¶
type Facing uint8
Facing represents one of the four cardinal directions.
func (Facing) MarshalJSON ¶
func (Facing) Rotate ¶
Rotate rotates the facing to the given facing, assuming the original facing was North.
func (*Facing) UnmarshalJSON ¶
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.
type Point ¶
Point represents an integer point in 2D space.
func RandomPoint ¶
RandomPoint returns a random point within the rect.
func Ray ¶
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) DirectionTo ¶
DirectionTo returns the direction code that most closely matches the direction of the argument point.
func (Point) Distance ¶
Distance returns the maximum distance from p to d along either the X or Y axis.
func (Point) StepFacing ¶
StepFacing returns the result of stepping in facing f from point p.
type Rect ¶
Rect represents integer 2D bounds.
func NewRectFromExtents ¶
NewRectFromExtents creates a new rect centered on point p with the given extents.
func NewRectFromFacing ¶
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 ¶
NewRectFromPoints creates a new Rect that contains all of the given points.
func NewRectFromRadius ¶
NewRectFromRadius creates a new rect centered on point p with radius r.
func NewRectXYWH ¶
NewRectXYWH creates a new Rect value with the given dimensions and offset.
func (Rect) Bound ¶
Bound bounds a point to the rect, such that the point is forced inside the rect along the axis where necessary.
func (Rect) CenterRect ¶
CenterRect returns the center a rect from the center of this rect with the given dimensions.
func (Rect) Contain ¶
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 ¶
ContainingRect returns the rect that contains both r and a.
func (Rect) ContainsRect ¶
ContainsRect returns true if r fully contains b.
func (Rect) Extents ¶
Extents returns the extents of the rect from the given centerpoint. If the point lies outside the rect, the result is undefined.
func (Rect) MoveRelative ¶
Move moves the rect relative to the current position without modifying the width or height.
func (Rect) Overlap ¶
Overlap returns the overlapping rect between r and a. If there is no overlap the zero value is returned.
func (Rect) RandomSubRect ¶
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 ¶
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 ¶
Rotate rotates the rect to the given facing about the given point, assuming the rect is currently facing North.
func (Rect) RotateInPlace ¶
RotateInPlace rotates the rect to the given facing keeping the center tile in place when possible.
func (Rect) RotatePointRelative ¶
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 ¶
RotateRect rotates the given rect within this rect assuming the given rect is in a North facing.