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 ¶
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 ¶
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 Ranks ¶
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.
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 ¶
At is the cell at a coordinate. The zero Cell, which this never returns, is the one that is nowhere.
func (Cell) MarshalJSON ¶
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 ¶
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) UnmarshalJSON ¶
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 ¶
Disk returns every cell within radius steps of center, center first, then ring by ring outward.
func Ring ¶
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.
type Offset ¶
Offset is an odd-q offset coordinate: odd columns sit half a cell lower than even ones.
func InRange ¶
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 ¶
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 ¶
SideCells returns the nine battlefield coordinates belonging to one side, column-major.
func (Offset) DistanceTo ¶
DistanceTo returns the hex distance between two offset coordinates. This is the number an attack range is compared against.
func (Offset) Neighbors ¶
Neighbors returns the six adjacent coordinates in Directions order, including any that fall outside the battlefield.
func (Offset) NeighborsOnBoard ¶
NeighborsOnBoard returns only the adjacent coordinates inside the battlefield. Edge cells have fewer than six.
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) MarshalJSON ¶
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 ¶
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) UnmarshalJSON ¶
UnmarshalJSON reads a side written by name.