element

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: 4 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 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

View Source
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

func Dual(primary, secondary Element) (Affinity, error)

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 Single

func Single(member Element) (Affinity, error)

Single builds a one-element affinity.

func (Affinity) Elements

func (a Affinity) Elements() []Element

Elements returns the one or two elements, primary first.

func (Affinity) Has

func (a Affinity) Has(member Element) bool

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) IsDual

func (a Affinity) IsDual() bool

IsDual reports whether the affinity carries two elements.

func (Affinity) MarshalJSON

func (a Affinity) MarshalJSON() ([]byte, error)

MarshalJSON writes a single affinity as a name and a dual affinity as an array, matching what UnmarshalJSON accepts.

func (Affinity) Primary

func (a Affinity) Primary() Element

Primary returns the first element, which is also the sole element of a single affinity.

func (Affinity) Secondary

func (a Affinity) Secondary() (Element, bool)

Secondary returns the second element and whether there is one.

func (Affinity) String

func (a Affinity) String() string

func (*Affinity) UnmarshalJSON

func (a *Affinity) UnmarshalJSON(raw []byte) error

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

func ParseChart(raw []byte) (*Chart, error)

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

func (c *Chart) Cycles() []Cycle

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

func (c *Chart) Inert() []Element

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

func (c *Chart) LegalPairs() []Affinity

LegalPairs returns every dual affinity the chart allows, ordered by element declaration order.

func (*Chart) Multiplier

func (c *Chart) Multiplier(attacker, defender Element) int

Multiplier returns the affinity multiplier in parts per thousand for an attack of one element landing on a defender of another.

func (*Chart) MultiplierAgainst

func (c *Chart) MultiplierAgainst(attacker Element, defender Affinity) int

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

func (c *Chart) MutualPairs() [][2]Element

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) Related

func (c *Chart) Related(left, right Element) bool

Related reports whether either element beats the other.

func (*Chart) Scale

func (c *Chart) Scale(damage int64, attacker, defender Element) int64

Scale applies the affinity multiplier to a damage figure using integer division, so the result is identical on every platform.

func (*Chart) ScaleAgainst

func (c *Chart) ScaleAgainst(damage int64, attacker Element, defender Affinity) int64

ScaleAgainst applies MultiplierAgainst to a damage figure.

func (*Chart) Strengths

func (c *Chart) Strengths(member Element) []Element

Strengths returns the elements the given element is strong against, in declaration order.

func (*Chart) Validate

func (c *Chart) Validate() error

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

func (c *Chart) ValidateAffinity(affinity Affinity) error

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

func (c *Chart) Weaknesses(member Element) []Element

Weaknesses returns the elements the given element is weak to, in declaration order.

type Cycle

type Cycle struct {
	Name  string
	Chain []Element
}

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.

const (
	Neutral Element = iota
	Water
	Fire
	Grass
	Ground
	Ice
	Metal
	Wind
	Electric
	Light
	Dark
)

func All

func All() []Element

All returns every declared element in declaration order.

func Parse

func Parse(name string) (Element, error)

Parse resolves an element name as written in the data files.

func (Element) String

func (e Element) String() string

func (Element) Valid

func (e Element) Valid() bool

Valid reports whether the value is one of the declared elements.

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.

Jump to

Keyboard shortcuts

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