stats

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 9 Imported by: 3 modules, 2 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 stats holds gameplay stats: named base values changed by additive, multiplicative and override modifiers that come from sources such as equipment and buffs, with timed modifiers counted in fixed steps.

A Sheet keeps one entity's stats. Every change to a base value or a modifier recomputes the stats it touches at once, reports each value that changed through Sheet.OnChange, and leaves Sheet.Value a cached read. A stat's value is

clamp((base + sum of add values) * product of mul values)

or, while any override modifier is present, clamp(the override with the highest priority, the latest added among equal priorities). Adds and muls are applied in the order they were added; with float64 arithmetic that order can change the last digits of a result (0.1 + 0.2 + 0.3 is not exactly 0.3 + 0.1 + 0.2). A sum or product that overflows float64 saturates at the largest finite value of its sign, so values are always finite and within their bounds. Sheet.Refresh replaces a source's modifiers in a single change. Timed modifiers count calls of Sheet.Step, which a game makes once per fixed step of its game.Loop, so a modifier that lasts n steps is removed by exactly the n-th Step after it was added.

Stat definitions and named effects (groups of modifiers, such as an item's bonuses or a buff) are written in a versioned YAML file read with Parse and written with Encode.

Nothing in stats reads the wall clock, spawns goroutines or keeps package-level state, so sheets are deterministic and replay alike.

Index

Constants

View Source
const CurrentVersion = 1

CurrentVersion is the version of the stats file schema this package reads and writes.

Variables

View Source
var (
	// ErrUnknownStat is returned when a stat name is not defined on the sheet or in the file.
	ErrUnknownStat = errors.New("stats: unknown stat")
	// ErrDuplicateStat is returned when a stat is defined twice.
	ErrDuplicateStat = errors.New("stats: duplicate stat")
	// ErrInvalidOp is returned for an Op other than OpAdd, OpMul and OpOverride.
	ErrInvalidOp = errors.New("stats: invalid modifier op")
	// ErrInvalidValue is returned for a NaN or infinite number.
	ErrInvalidValue = errors.New("stats: value is not finite")
	// ErrInvalidSteps is returned for a negative step count.
	ErrInvalidSteps = errors.New("stats: negative steps")
)

Functions

func Encode

func Encode(f *File) ([]byte, error)

Encode validates f and writes it as deterministic YAML with 2-space indentation: fields in declaration order and lists in their given order, so Parse then Encode reproduces an encoded file byte for byte.

func Float

func Float(v float64) *float64

Float returns a pointer to v, for Def's optional Min and Max.

func Validate

func Validate(f *File) error

Validate checks f against the version 1 schema: the version, unique non-empty names, finite numbers, Min not above Max, non-negative Steps, known ops, and modifiers that name defined stats.

Types

type Active

type Active struct {
	// ID is the modifier's handle for Sheet.Remove.
	ID ID
	// Source is the tag the modifier was added with, such as "equip:weapon" or "buff:haste".
	Source string
	// Modifier is the modifier itself.
	Modifier Modifier
	// Remaining is how many more calls of Sheet.Step the modifier lasts, 0 for a modifier without a time limit.
	Remaining int
}

Active describes a modifier currently on a sheet.

type Change

type Change struct {
	// Stat is the stat's name.
	Stat string
	// Old and New are its value before and after the change.
	Old, New float64
}

Change reports a stat whose value changed.

type Def

type Def struct {
	// Name identifies the stat within a sheet; it must be non-empty and unique.
	Name string `yaml:"name"`
	// Base is the stat's value before modifiers.
	Base float64 `yaml:"base"`
	// Min, when set, is the lowest value the stat takes after modifiers.
	Min *float64 `yaml:"min,omitempty"`
	// Max, when set, is the highest value the stat takes after modifiers; it must not be below Min.
	Max *float64 `yaml:"max,omitempty"`
}

Def defines one stat: its name, base value and optional bounds.

type Effect

type Effect struct {
	// Name identifies the effect within a file; it must be non-empty and unique.
	Name string `yaml:"name"`
	// Steps is how many fixed steps the effect's modifiers last once applied; 0 keeps them until they are removed.
	Steps int `yaml:"steps,omitempty"`
	// Modifiers are the effect's modifiers, applied in order.
	Modifiers []Modifier `yaml:"modifiers"`
}

Effect is a named group of modifiers applied together from one source, such as an item's bonuses or a buff.

type File

type File struct {
	// Version is the schema version; it must equal CurrentVersion.
	Version int `yaml:"version"`
	// Stats are the stat definitions, in the order a sheet built from the file reports changes.
	Stats []Def `yaml:"stats"`
	// Effects are named groups of modifiers on those stats.
	Effects []Effect `yaml:"effects,omitempty"`
}

File is the root of a versioned YAML stats file: stat definitions and the effects that modify them.

func Parse

func Parse(data []byte) (*File, error)

Parse decodes a YAML stats file and validates it. Unknown fields and trailing YAML documents are refused.

func (*File) Effect

func (f *File) Effect(name string) (Effect, bool)

Effect returns the effect with the given name.

func (*File) NewSheet

func (f *File) NewSheet() (*Sheet, error)

NewSheet returns a sheet holding the file's stats at their base values, with no modifiers.

type ID

type ID uint64

ID identifies one modifier on a sheet. IDs start at 1 and are never reused by the same sheet, so the zero ID names no modifier.

type Modifier

type Modifier struct {
	// Stat names the stat the modifier changes.
	Stat string `yaml:"stat"`
	// Op is how the modifier changes it.
	Op Op `yaml:"op"`
	// Value is the amount added, the factor multiplied by, or the value an override sets; it must be finite.
	Value float64 `yaml:"value"`
	// Priority orders override modifiers on the same stat: the highest wins. Add and mul modifiers ignore it.
	Priority int `yaml:"priority,omitempty"`
}

Modifier changes one stat.

type Op

type Op string

Op is how a modifier changes a stat. Its YAML form is the constant's string.

const (
	// OpAdd adds the modifier's value to the stat's base, before any multiplication.
	OpAdd Op = "add"
	// OpMul multiplies the stat by the modifier's value after all additions; several multiply together.
	OpMul Op = "mul"
	// OpOverride replaces the computed value with the modifier's value, ignoring every add and mul; among several, the highest Priority wins and the latest added breaks ties.
	OpOverride Op = "override"
)

func (Op) Valid

func (o Op) Valid() bool

Valid reports whether o is OpAdd, OpMul or OpOverride.

type Sheet

type Sheet struct {
	// OnChange, when set, is called with each stat whose value changed.
	OnChange func(Change)
	// contains filtered or unexported fields
}

Sheet holds one entity's stats and the modifiers on them.

Every method that changes a base value or a modifier recomputes the stats it touched before returning and calls OnChange once for each one whose value changed, in definition order. OnChange may itself change the sheet; those changes are recomputed and reported before the outer call returns. A Sheet is not safe for concurrent use.

func NewSheet

func NewSheet(defs ...Def) (*Sheet, error)

NewSheet returns a sheet holding the given stats at their base values, in the given order.

func (*Sheet) Add

func (s *Sheet) Add(source string, m Modifier) (ID, error)

Add adds a modifier from source that lasts until it is removed, and returns its ID.

func (*Sheet) AddTimed

func (s *Sheet) AddTimed(source string, m Modifier, steps int) (ID, error)

AddTimed adds a modifier from source that lasts steps calls of Step and is removed by the last of them; 0 steps lasts until it is removed. It returns the modifier's ID.

func (*Sheet) Apply

func (s *Sheet) Apply(source string, e Effect) ([]ID, error)

Apply adds every modifier of e from source, lasting e.Steps, and returns their IDs in the effect's order. It adds none of them when any is invalid.

Applying an effect again stacks a second copy; to refresh a buff instead, use Refresh.

func (*Sheet) Base

func (s *Sheet) Base(name string) float64

Base returns the named stat's base value, or 0 when it is not defined.

func (*Sheet) Clear

func (s *Sheet) Clear()

Clear removes every modifier, leaving the stats at their base values.

func (*Sheet) Define

func (s *Sheet) Define(d Def) error

Define adds a stat at its base value after the stats already defined. Defining a name twice returns ErrDuplicateStat.

func (*Sheet) Has

func (s *Sheet) Has(name string) bool

Has reports whether the sheet defines the named stat.

func (*Sheet) Lookup

func (s *Sheet) Lookup(name string) (float64, bool)

Lookup returns the named stat's current value and whether it is defined.

func (*Sheet) Modifiers

func (s *Sheet) Modifiers() []Active

Modifiers returns the modifiers on the sheet in the order they were added.

func (*Sheet) Refresh

func (s *Sheet) Refresh(source string, e Effect) ([]ID, error)

Refresh removes every modifier added from source and applies e from source in their place, as one change: OnChange reports only each stat's value before the old modifiers left and after the new ones arrived, never the value in between. It returns the new modifiers' IDs in the effect's order. When any of e's modifiers is invalid it changes nothing.

Refresh restarts a buff's duration without the listeners seeing the buff end and begin again.

func (*Sheet) Remaining

func (s *Sheet) Remaining(id ID) (int, bool)

Remaining returns how many more calls of Step the modifier lasts (0 for one without a time limit) and whether it is on the sheet.

func (*Sheet) Remove

func (s *Sheet) Remove(id ID) bool

Remove removes the modifier with the given ID and reports whether it was on the sheet.

func (*Sheet) RemoveSource

func (s *Sheet) RemoveSource(source string) int

RemoveSource removes every modifier added from source, such as all of an unequipped item's bonuses, and returns how many it removed.

func (*Sheet) SetBase

func (s *Sheet) SetBase(name string, base float64) error

SetBase sets the named stat's base value and recomputes it.

func (*Sheet) Stats

func (s *Sheet) Stats() []string

Stats returns the sheet's stat names in definition order.

func (*Sheet) Step

func (s *Sheet) Step()

Step advances timed modifiers by one fixed step and removes those whose last step this was. Call it once from each game.Loop Update.

func (*Sheet) Steps

func (s *Sheet) Steps() uint64

Steps returns how many times Step has been called.

func (*Sheet) Value

func (s *Sheet) Value(name string) float64

Value returns the named stat's current value, or 0 when it is not defined.

Jump to

Keyboard shortcuts

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