hex

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hex implements the battlefield geometry for the arena.

The battlefield is one shared odd-q offset hex grid, 6 columns by 3 rows. Columns run left to right from the ally backline to the enemy backline; odd columns sit half a cell lower than even ones, which is what gives a frontline cell contact with two cells of the next column.

 c0  c1  c2  c3  c4  c5      c0-c2 = ally  (c2 = frontline)
 BK  MD  FR  FR  MD  BK      c3-c5 = enemy (c3 = frontline)

 __    __    __
/00\__/20\__/40\__
\__/10\__/30\__/50\
/01\__/21\__/41\__/
\__/11\__/31\__/51\
/02\__/22\__/42\__/
\__/12\__/32\__/52\
   \__/  \__/  \__/

Offset coordinates exist only for authoring formations and for rendering. Every distance and area calculation runs on cube coordinates, where the hex metric is a plain max-of-absolute-differences and there is no diagonal case to get wrong.

Everything here is a pure function of its integer arguments: no floating point, no map iteration, no randomness, no clock. Results are therefore stable across runs and platforms, which is what lets the battle engine be replayed from a seed.

Index

Constants

View Source
const (
	Cols = 6
	Rows = 3

	AllyBackCol   = 0
	AllyFrontCol  = 2
	EnemyFrontCol = 3
	EnemyBackCol  = 5

	// FormationCols is the width of a single team's authoring grid: every
	// team authors its formation as 3 columns of 3 rows regardless of side.
	FormationCols = 3
	// FormationRows mirrors Rows and exists for symmetry at call sites.
	FormationRows = Rows
	// BoardSlots is how many units one side may have standing at once: every
	// formation slot, which is what the board itself admits.
	//
	// Derived from the grid rather than written out, because it is a claim
	// ABOUT the grid — a literal here could drift from the formation it says it
	// is the whole of, and nothing would notice.
	//
	// It is deliberately NOT MaxSquadSize. This is the board's bound; that one
	// is the roster's, and the difference between the two is the room a summon
	// stands in (→ ENG-013).
	BoardSlots = FormationCols * FormationRows
	// MaxSquadSize is the most units a side is ever fielded with: the largest
	// squad a builder may save, a draft may pick, and a roster may bring.
	//
	// It is deliberately NOT BoardSlots. That one is the BOARD's bound — every
	// formation slot — and the difference between the two is the room a summon
	// stands in. A single constant doing both jobs is what made every summoning
	// skill a dead slot at the largest board (→ ENG-013).
	//
	// This package does not know what a format is and must not learn: the
	// formats are declared in internal/wire, which imports this package, and
	// TestTheLargestFormatIsTheSquadCap holds the two in step from that side.
	MaxSquadSize = 5
)

Battlefield dimensions. Columns run ally backline to enemy backline.

Variables

View Source
var Directions = [6]Cube{
	{+1, -1, 0},
	{+1, 0, -1},
	{0, +1, -1},
	{-1, +1, 0},
	{-1, 0, +1},
	{0, -1, +1},
}

Directions holds the six cube neighbour offsets in a fixed order, so every list derived from them — neighbours, rings, disks — has a stable ordering.

Functions

func Distance

func Distance(a, b Cube) int

Distance returns the hex distance between two cube coordinates.

func Ranks

func Ranks(side Side) [][]Offset

Ranks returns a side's three columns ordered from its own frontline backwards, which is the order an attack arrives in.

It exists because "how deep into that side is this cell" is not a column number: the ally side counts down from 2 to 0 and the enemy side counts up from 3 to 5, so the same depth is a different column on each half. Reading the column directly is the mistake this replaces — it works for whichever side the author happened to be thinking about and is backwards for the other.

func Render

func Render(label func(Offset) string) string

Render draws the battlefield as interlocking ASCII hexes, which is how the terminal client shows a formation. label is called once per cell and may return up to two characters; anything shorter is padded, longer is cut.

Types

type Cell

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

Cell is a place on the board that may be no place at all, which an Offset cannot be.

Offset's zero value is already spoken for: {0,0} is the ally back corner, a real cell somebody stands in, so there is no coordinate left over to mean "nowhere". That is what made a cell on a battle event dishonest. `omitempty` does nothing to a struct field, so every event with no cell to report wrote the back corner into the log regardless — and only a handful of the kinds ever meant one, while `omitzero` on the offset would have gone wrong the other way and dropped the corner from the events that did. Neither tag can tell the two apart, because the coordinate carries no room to say. So the absence lives in a second field instead, where it is a fact of its own rather than a coordinate pressed into doubling as one, and the zero value means absent — an omitted field therefore reads back as the Cell it was written from.

It is a value and not a pointer because a log is verified by comparing whole events with ==, and two pointers satisfy that by address: a re-run would differ from the battle it had just reproduced exactly, and nothing would fail to compile on the way there.

func At

func At(offset Offset) Cell

At is the cell at a coordinate. The zero Cell, which this never returns, is the one that is nowhere.

func (Cell) MarshalJSON

func (c Cell) MarshalJSON() ([]byte, error)

MarshalJSON writes the bare coordinate, so a cell reads on the wire exactly as an offset always did, and null when there is no cell.

func (Cell) Offset

func (c Cell) Offset() (Offset, bool)

Offset reports the coordinate and whether there is one, in the comma-ok shape a caller cannot read past without answering the second question.

func (Cell) String

func (c Cell) String() string

func (*Cell) UnmarshalJSON

func (c *Cell) UnmarshalJSON(raw []byte) error

UnmarshalJSON reads a coordinate, taking null — and, by never being called at all, an absent field — as no cell.

type Cube

type Cube struct {
	X, Y, Z int
}

Cube is a cube hex coordinate; the three axes always sum to zero.

func Disk

func Disk(center Cube, radius int) []Cube

Disk returns every cell within radius steps of center, center first, then ring by ring outward.

func Ring

func Ring(center Cube, radius int) []Cube

Ring returns the cells exactly radius steps from center, walking the six directions from a fixed start so the order is reproducible. A radius of zero yields the center itself.

func (Cube) Add

func (c Cube) Add(d Cube) Cube

Add returns the coordinate translated by d.

func (Cube) Offset

func (c Cube) Offset() Offset

Offset converts back to odd-q offset coordinates.

func (Cube) Scale

func (c Cube) Scale(n int) Cube

Scale returns the coordinate multiplied by n, used to step n cells along a direction.

func (Cube) String

func (c Cube) String() string

type Offset

type Offset struct {
	Col int `json:"col"`
	Row int `json:"row"`
}

Offset is an odd-q offset coordinate: odd columns sit half a cell lower than even ones.

func Cells

func Cells() []Offset

Cells returns every battlefield coordinate, column-major.

func InRange

func InRange(origin Offset, radius int) []Offset

InRange returns the on-board cells within radius of origin, excluding origin itself. This is the candidate set a skill with that range may target before any side or occupancy filter is applied.

func Place

func Place(side Side, author Offset) Offset

Place maps a formation-authoring slot onto the shared battlefield.

Both teams author their formation the same way — column 0 is their own backline, column 2 their frontline — and the enemy formation is rotated 180 degrees about the centre of the board. Without that rotation the two halves would not mirror each other: odd columns are pushed downward, so an ally frontline slot and the enemy slot authored identically would have different distance profiles and the matchup would be silently unbalanced.

SideNone is placed as an ally would be. There is nothing better to do with a side that is not one, and refusing it here would put an error in the geometry: whoever builds a roster is where a unit on no side is caught.

func SideCells

func SideCells(side Side) []Offset

SideCells returns the nine battlefield coordinates belonging to one side, column-major.

func (Offset) Cube

func (o Offset) Cube() Cube

Cube converts to cube coordinates, where all distance math happens.

func (Offset) DistanceTo

func (o Offset) DistanceTo(p Offset) int

DistanceTo returns the hex distance between two offset coordinates. This is the number an attack range is compared against.

func (Offset) Neighbors

func (o Offset) Neighbors() []Offset

Neighbors returns the six adjacent coordinates in Directions order, including any that fall outside the battlefield.

func (Offset) NeighborsOnBoard

func (o Offset) NeighborsOnBoard() []Offset

NeighborsOnBoard returns only the adjacent coordinates inside the battlefield. Edge cells have fewer than six.

func (Offset) OnBoard

func (o Offset) OnBoard() bool

OnBoard reports whether the coordinate lies inside the battlefield.

func (Offset) Side

func (o Offset) Side() Side

Side reports which half of the battlefield the coordinate sits in. It is only meaningful for on-board coordinates.

func (Offset) String

func (o Offset) String() string

type Side

type Side uint8

Side identifies which half of the battlefield a coordinate belongs to.

const (
	// SideNone is no side at all, and it is the zero value on purpose.
	//
	// A side is written to a battle log with `omitempty`, so whichever side is
	// zero is the one that never appears on the wire — and a reader would then
	// be recovering it from the absence of a field rather than from the log.
	// That is exactly the trap a draw with no stated reason sets: the fact is
	// right by coincidence until the day somebody reorders a constant. With
	// nothing at zero, an absent side means what it says, and a battle whose
	// winner is undecided writes no winner rather than quietly writing the
	// first one declared.
	SideNone Side = iota
	SideAlly
	SideEnemy
)

func (Side) Fights

func (s Side) Fights() bool

Fights reports whether the side is one a unit can be on. SideNone is not.

func (Side) MarshalJSON

func (s Side) MarshalJSON() ([]byte, error)

MarshalJSON writes the side by name.

A number would tie a saved battle log to the order these constants happen to be declared in, so inserting a side later would silently reinterpret every log already written.

func (Side) Opposing

func (s Side) Opposing() Side

Opposing is the other half of the battlefield, and SideNone for anything that is not fighting — a draw has no other side to name.

func (Side) String

func (s Side) String() string

func (*Side) UnmarshalJSON

func (s *Side) UnmarshalJSON(raw []byte) error

UnmarshalJSON reads a side written by name.

Jump to

Keyboard shortcuts

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