API リファレンスはソースコード内の Go ドキュメントコメントから生成されており、英語で記述されています。
package battle
import "github.com/nsgo-dev/game/pkg/battle"概要
Package battle is experimental: its API and documented formats may change in a minor release. See the API stability policy. resolves deterministic, presentation-independent turn-based battles between a party and a troop.
Turns
New copies the party's and the troop's Fighters into a Runner. Each call to Resolve is one turn: the game supplies a Command per acting fighter, from either side, and the Runner orders them by Agility, highest first, breaking ties by side (the party before the troop) and then by the fighter's index, so the order never depends on the order of the command list. Commands whose actor is out of range or already defeated, including one defeated earlier in the same turn, are skipped.
Actions
An Action's Scope picks its targets relative to the actor's side. OneEnemy and AllEnemies deal Power plus the actor's Attack minus the target's Defense, at least 1, to living opponents, each target rolling for a hit against Accuracy and then for a critical against Rules.CriticalChance; a critical doubles the damage. Self, OneAlly and AllAllies heal Power HP on living allies without a roll, never above MaxHP. An Action with a Cost spends that much MP before it resolves, and an actor without enough MP does nothing.
Events and outcome
Resolve returns the Events of the turn in order, for the presentation layer to animate in its own time. When the last living troop member falls the battle is won, and when the last living party member falls it is lost; Resolve stops at that point and returns nil from then on.
Determinism and snapshots
Every roll is drawn from the Runner's random.Stream, so a seed and the same commands replay the same events on every platform. Snapshot copies the fighters, the turn counter, the outcome and the stream's state; Restore puts them back, so a battle can be saved mid-fight or rolled back and replayed. A Snapshot marshals to JSON for save slots.
目次
関数
func EvaluateFormula(formula string, a, b Fighter) (int, error)EvaluateFormula evaluates the small deterministic battle damage vocabulary against attacker a and defender b, truncating the result toward zero. It returns an error for an unknown value, a syntax error, a division by zero and a result that is not a finite number in the int32 range.
型
type Action struct {
// ID identifies the action to the game.
ID string
// Scope picks the action's targets.
Scope Scope
// Power is the action's base damage for hostile scopes and its healing otherwise.
Power int
// Cost is the MP the actor spends to use the action.
Cost int
// Accuracy is the chance in (0, 1] that a hostile action hits each target; 0 means it always hits.
Accuracy float64
// AddState applies a copy of this state to each living hostile target after a successful hit.
AddState *State
// Formula optionally replaces Power for hostile damage. It accepts numeric literals, a.attack, a.defense, b.attack, b.defense, +, -, *, / and parentheses.
Formula string
}Action is a skill, an attack or an item use.
type Command struct {
// Actor is the fighter using the action.
Actor Ref
// Action is the action used.
Action Action
// Target is the index of the target on the side the scope selects, for OneEnemy and OneAlly; the other scopes ignore it.
Target int
}Command is one fighter's choice for a turn.
type Event struct {
// Kind is what happened.
Kind EventKind
// Actor is the fighter whose command caused the event; it is the zero Ref for Won and Lost.
Actor Ref
// Target is the fighter affected; it is the zero Ref for NoMP, Won and Lost.
Target Ref
// Amount is the HP lost or gained, for Damaged and Healed.
Amount int
// Critical reports whether a Damaged event was a critical hit.
Critical bool
// State names the state involved in a state event.
State string
}Event is one thing that happened while a turn resolved.
type EventKind stringEventKind names what happened in an Event.
const (
// Missed is a hostile action missing its target.
Missed EventKind = "missed"
// Damaged is a target losing Amount HP; Critical marks a critical hit.
Damaged EventKind = "damaged"
// Healed is a target gaining Amount HP.
Healed EventKind = "healed"
// Defeated is a target falling to 0 HP.
Defeated EventKind = "defeated"
// NoMP is an actor lacking the MP its action costs; the action does nothing.
NoMP EventKind = "no_mp"
// StateDamage is damage applied by a state at turn start.
StateDamage EventKind = "state_damage"
// StateAdded reports a state applied by an action.
StateAdded EventKind = "state_added"
// StateExpired reports a state whose turns reached zero.
StateExpired EventKind = "state_expired"
// StateBlocked reports a living actor that could not act because of a state.
StateBlocked EventKind = "state_blocked"
// Won is the last living troop member falling.
Won EventKind = "won"
// Lost is the last living party member falling.
Lost EventKind = "lost"
)type Fighter struct {
// ID identifies the fighter to the game.
ID string `json:"id"`
// Name is the fighter's display name.
Name string `json:"name,omitempty"`
// HP is the fighter's current hit points; a fighter starts the battle alive when HP is above 0.
HP int `json:"hp"`
// MaxHP caps healing; 0 means healing is not capped.
MaxHP int `json:"max_hp,omitempty"`
// MP is the fighter's current magic points, spent by an Action's Cost.
MP int `json:"mp,omitempty"`
// Attack is added to the Power of the fighter's hostile actions.
Attack int `json:"attack,omitempty"`
// Defense is subtracted from the damage the fighter takes.
Defense int `json:"defense,omitempty"`
// Agility orders the fighter's command within a turn, highest first.
Agility int `json:"agility,omitempty"`
// Alive reports whether the fighter can still act and be targeted.
Alive bool `json:"alive"`
// States are the ordered status effects currently affecting the fighter.
States []State `json:"states,omitempty"`
}Fighter is one battler's stats and current state.
type Ref struct {
// Side is the fighter's side.
Side Side
// Index is the fighter's index in its side's slice.
Index int
}Ref names one fighter by side and index.
type Rules struct {
// CriticalChance is the chance in [0, 1] that a hit is critical and deals double damage.
CriticalChance float64
}Rules are the battle-wide tuning values.
type Runner struct {
// Party is the player's fighters.
Party []Fighter
// Troop is the enemy fighters.
Troop []Fighter
// Rules are the battle's tuning values.
Rules Rules
// RNG is the stream every roll is drawn from.
RNG *random.Stream
// Turn counts the turns resolved so far.
Turn int
// Won reports whether the troop has been defeated.
Won bool
// Lost reports whether the party has been defeated.
Lost bool
}Runner holds a battle's state and resolves its turns.
func New(party, troop []Fighter, rules Rules, rng *random.Stream) *RunnerNew returns a Runner over copies of party and troop, marking each fighter alive when its HP is above 0. A nil rng is replaced with random.New(0).
func (r *Runner) Order(commands []Command) []CommandOrder returns the commands whose actor exists, sorted into the order Resolve runs them: by the actor's Agility, highest first, then the party before the troop, then by the actor's index.
func (r *Runner) Resolve(commands []Command) []EventResolve runs one turn of commands and returns its events. It returns nil once the battle is won or lost.
func (r *Runner) Restore(s Snapshot) errorRestore replaces r's mutable state with a copy of s. It returns an error for a nil Runner or a Runner without a stream.
func (r *Runner) Snapshot() SnapshotSnapshot returns a copy of r's mutable state.
type Scope stringScope is which fighters an Action affects, relative to its actor's side.
const (
// Self targets the actor.
Self Scope = "self"
// OneEnemy targets the living opponent at Command.Target.
OneEnemy Scope = "one_enemy"
// AllEnemies targets every living opponent.
AllEnemies Scope = "all_enemies"
// OneAlly targets the living ally at Command.Target, which may be the actor.
OneAlly Scope = "one_ally"
// AllAllies targets every living ally, the actor included.
AllAllies Scope = "all_allies"
)type Side intSide names one of the two sides of a battle.
const (
// PartySide is the player's party.
PartySide Side = iota
// TroopSide is the enemy troop.
TroopSide
)type Snapshot struct {
// Party is the party's fighters.
Party []Fighter `json:"party"`
// Troop is the troop's fighters.
Troop []Fighter `json:"troop"`
// Turn is the number of turns resolved.
Turn int `json:"turn"`
// Won reports whether the battle was won.
Won bool `json:"won,omitempty"`
// Lost reports whether the battle was lost.
Lost bool `json:"lost,omitempty"`
// RNG is the state of the battle's stream.
RNG random.State `json:"rng"`
}Snapshot is a copy of a battle's mutable state, from Runner.Snapshot.
type State struct {
ID string `json:"id"`
Turns int `json:"turns"`
Damage int `json:"damage,omitempty"`
CannotAct bool `json:"cannot_act,omitempty"`
}State is a deterministic status effect. Turns counts remaining turns, Damage is applied at the start of each turn, and CannotAct skips the fighter's command while the state is active.