Documentation
¶
Overview ¶
Package element implements the elemental affinity chart.
The chart is declared as data, not code: a set of cycles where each entry beats the next and the last beats the first, plus mutually-strong pairs and inert elements. Three short chains express every relation:
organic water > fire > grass > ground > (water) industrial ice > metal > wind > electric > (ice) cross water > metal > grass > wind > fire > ice > ground > electric > (water)
That layout gives each cycled element exactly two strengths and two weaknesses, so no element is structurally better than another. Validate enforces the property, which is what stops a later element being added in a way that quietly breaks the balance.
An attacker and a defender each carry one element, so exactly one relation ever applies and multipliers never stack.
Multipliers are integers in parts per thousand and damage scaling is integer division. There is no floating point anywhere in this package, so a battle replayed from the same seed produces the same numbers on every platform.
Index ¶
- Constants
- type Affinity
- func (a Affinity) Elements() []Element
- func (a Affinity) Has(member Element) bool
- func (a Affinity) IsDual() bool
- func (a Affinity) MarshalJSON() ([]byte, error)
- func (a Affinity) Primary() Element
- func (a Affinity) Secondary() (Element, bool)
- func (a Affinity) String() string
- func (a *Affinity) UnmarshalJSON(raw []byte) error
- type Chart
- func (c *Chart) Cycles() []Cycle
- func (c *Chart) Inert() []Element
- func (c *Chart) LegalPairs() []Affinity
- func (c *Chart) Multiplier(attacker, defender Element) int
- func (c *Chart) MultiplierAgainst(attacker Element, defender Affinity) int
- func (c *Chart) Multipliers() Multipliers
- func (c *Chart) MutualPairs() [][2]Element
- func (c *Chart) Related(left, right Element) bool
- func (c *Chart) Scale(damage int64, attacker, defender Element) int64
- func (c *Chart) ScaleAgainst(damage int64, attacker Element, defender Affinity) int64
- func (c *Chart) Strengths(member Element) []Element
- func (c *Chart) Validate() error
- func (c *Chart) ValidateAffinity(affinity Affinity) error
- func (c *Chart) Weaknesses(member Element) []Element
- type Cycle
- type Element
- type Multipliers
Constants ¶
const Count = int(Dark) + 1
Count is the number of declared elements.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Affinity ¶
type Affinity struct {
// contains filtered or unexported fields
}
Affinity is a unit's elemental makeup: one element, or two.
An attack always carries a single element — the one its skill declares — so a dual affinity is not a free offensive upgrade. What it changes is the defending side, where the two multipliers stack, and the set of skills the unit is allowed to learn.
The zero value is a single neutral affinity, which is the right default for a unit whose theme has no element.
func Dual ¶
Dual builds a two-element affinity. It rejects a repeated element and any pairing involving Neutral, which is inert and so adds nothing as a second element. Whether the two elements are allowed to sit together also depends on the chart; see Chart.ValidateAffinity.
func (Affinity) Has ¶
Has reports whether the affinity includes the given element. This is the check that decides whether a unit may learn a skill of that element.
func (Affinity) MarshalJSON ¶
MarshalJSON writes a single affinity as a name and a dual affinity as an array, matching what UnmarshalJSON accepts.
func (Affinity) Primary ¶
Primary returns the first element, which is also the sole element of a single affinity.
func (*Affinity) UnmarshalJSON ¶
UnmarshalJSON accepts either an element name or an array of one or two names, so seed data can write "fire" or ["fire", "wind"].
type Chart ¶
type Chart struct {
// contains filtered or unexported fields
}
Chart is a resolved affinity chart. Build one with ParseChart; the zero value is not usable.
func ParseChart ¶
ParseChart reads a chart declaration and resolves it into a lookup table. It never touches the filesystem; the caller supplies the bytes.
func (*Chart) Cycles ¶
Cycles returns the declared rings, in declaration order, as copies.
Copies because a caller holding the chart's own slices could sort or overwrite them and change what every later reader sees — the chart is loaded once and shared, and a reference screen sorting a ring for display would silently reorder the ring the game itself was declared with.
func (*Chart) Inert ¶
Inert returns the elements in no cycle and no pair, in element order.
Read off the resolved flags rather than off the declaration, because being inert is the absence of edges rather than a thing that was written: an element left out of every cycle by accident is inert in play whatever the "inert" list says, and a reference that reported the list would describe a chart the game is not using.
func (*Chart) LegalPairs ¶
LegalPairs returns every dual affinity the chart allows, ordered by element declaration order.
func (*Chart) Multiplier ¶
Multiplier returns the affinity multiplier in parts per thousand for an attack of one element landing on a defender of another.
func (*Chart) MultiplierAgainst ¶
MultiplierAgainst returns the affinity multiplier in parts per thousand for an attack of one element landing on a defender's affinity. A dual defender stacks the two multipliers, so a hit that both elements are weak to lands at roughly 2.25x and one that both resist at roughly 0.44x, while a weakness and a resistance cancel back to neutral.
func (*Chart) Multipliers ¶
func (c *Chart) Multipliers() Multipliers
Multipliers returns the three configured outcomes.
func (*Chart) MutualPairs ¶
MutualPairs returns the declared two-way pairs, in declaration order.
A pair is not a cycle of two: both members beat each other, so neither is safe from the other, and rendering one as a two-link ring would draw it as though one of the arrows were the way round it was declared. light and dark are the only pair today and they are each other's whole matchup.
func (*Chart) Scale ¶
Scale applies the affinity multiplier to a damage figure using integer division, so the result is identical on every platform.
func (*Chart) ScaleAgainst ¶
ScaleAgainst applies MultiplierAgainst to a damage figure.
func (*Chart) Strengths ¶
Strengths returns the elements the given element is strong against, in declaration order.
func (*Chart) Validate ¶
Validate enforces the structural invariants the chart's balance rests on: every element is classified exactly once, a pair is only strong in both directions when it was declared mutual, inert elements have no relations, members of a mutual pair have exactly one relation each way, and every cycled element has the same number of strengths as weaknesses.
func (*Chart) ValidateAffinity ¶
ValidateAffinity rejects a dual affinity whose two elements have a relation with each other.
A unit that is both the counter and the victim of its own second element is incoherent, and the pairings that survive the rule turn out to be exactly the ones with a natural reading — water with ice, fire with metal, ground with wind. The rule still leaves plenty of duals with a stacked weakness, so it removes the nonsense without removing the risk.
func (*Chart) Weaknesses ¶
Weaknesses returns the elements the given element is weak to, in declaration order.
type Cycle ¶
Cycle is one declared ring: each member beats the next, and the last beats the first.
The name is the author's own word for the grouping ("organic", "cross"). It carries no rule — nothing in battle reads it — and exists so a reference can say which ring an edge came out of rather than listing edges nobody can group.
type Element ¶
type Element uint8
Element identifies one elemental affinity. The zero value is Neutral, which is inert in both directions and is the right default for a unit whose theme has no element.
type Multipliers ¶
type Multipliers struct {
Advantage int `json:"advantage"`
Neutral int `json:"neutral"`
Disadvantage int `json:"disadvantage"`
}
Multipliers holds the three affinity outcomes in parts per thousand.