GitHubLoslegen

Die API-Referenz stammt aus den Go-Doc-Kommentaren im Quellcode und ist auf Englisch verfasst.

package rpgdata

import "github.com/nsgo-dev/game/pkg/rpgdata"

Übersicht

Package rpgdata is experimental: its API and documented formats may change in a minor release. See the API stability policy. reads and writes the versioned YAML database an RPG's records live in: classes with level curves and learned skills, actors, skills, enemies and troops, with every ID and cross-reference checked on load. Class and Enemy look records up by ID, and Progression tracks a character's level and experience against a class's linear experience rule.

Index

Konstanten

const CurrentVersion = 1

CurrentVersion is the schema version Parse reads and Encode writes.

Funktionen

func Encode(d *Database) ([]byte, error)

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

Typen

type Actor struct {
	// ID identifies the actor among the database's actors.
	ID string `yaml:"id"`
	// Name is the display name or its localization key.
	Name string `yaml:"name"`
	// Class names the actor's class.
	Class string `yaml:"class"`
	// Level is the initial level, 1 when 0, and at most the class's MaxLevel when it has one.
	Level int `yaml:"level,omitempty"`
}

Actor is a playable character.

type Class struct {
	// ID identifies the class among the database's classes.
	ID string `yaml:"id"`
	// Name is the display name or its localization key.
	Name string `yaml:"name,omitempty"`
	// MaxLevel is the highest level, or 0 for no cap.
	MaxLevel int `yaml:"maxLevel,omitempty"`
	// LevelXP scales the experience rule: going from level n to n+1 takes n × LevelXP experience. 0 means the class never levels up from experience.
	LevelXP int `yaml:"levelXP,omitempty"`
	// Stats maps a stat name to its value at each level, the first entry being level 1; levels past the end keep the last value.
	Stats map[string][]int `yaml:"stats,omitempty"`
	// Skills are the skills the class learns and the level each is learned at.
	Skills []LearnedSkill `yaml:"skills,omitempty"`
}

Class is a class: its level cap, experience rule, stat curves and the skills it teaches.

func (c Class) SkillsLearnedAt(level int) []string

SkillsLearnedAt returns the IDs of the skills the class learns on reaching level, in the order the class lists them.

func (c Class) StatAt(name string, level int) int

StatAt returns the class's value of the named stat at level: the curve's entry for that level, its first entry below level 1, its last entry past its end, and 0 for a stat the class has no curve for.

type Database struct {
	// Version is the schema version, CurrentVersion.
	Version int `yaml:"version"`
	// Classes are the classes actors belong to.
	Classes []Class `yaml:"classes,omitempty"`
	// Actors are the playable characters.
	Actors []Actor `yaml:"actors,omitempty"`
	// Skills are the skills classes teach.
	Skills []Skill `yaml:"skills,omitempty"`
	// Enemies are the foes troops are made of.
	Enemies []Enemy `yaml:"enemies,omitempty"`
	// Troops are the groups of enemies a battle starts with.
	Troops []Troop `yaml:"troops,omitempty"`
}

Database is one RPG database file. IDs are unique within each category, and every reference names a record of the category it points at.

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

Parse decodes and validates one database document. It refuses unknown fields, a version other than CurrentVersion, a trailing document and any record Validate refuses, returning a *loaderr.Error with the field and line of the problem.

func (d *Database) Class(id string) *Class

Class returns the class with the id, or nil when d has none. The pointer points into d.Classes and is invalidated when d.Classes changes.

func (d *Database) Enemy(id string) *Enemy

Enemy returns the enemy with the id, or nil when d has none. The pointer points into d.Enemies and is invalidated when d.Enemies changes.

func (d *Database) Validate() error

Validate checks the version, that IDs are non-empty and unique within each category, that numbers are in range and that every class skill, actor class and troop member names a record that exists. Its errors are *loaderr.Error values naming the field at fault.

type Enemy struct {
	// ID identifies the enemy among the database's enemies.
	ID string `yaml:"id"`
	// Name is the display name or its localization key.
	Name string `yaml:"name"`
	// HP is the enemy's hit points, 1 or more.
	HP int `yaml:"hp"`
	// Attack is the enemy's attack stat, 0 or more.
	Attack int `yaml:"attack,omitempty"`
	// Defense is the enemy's defense stat, 0 or more.
	Defense int `yaml:"defense,omitempty"`
	// Experience is the experience defeating it gives, 0 or more.
	Experience int `yaml:"experience,omitempty"`
	// Gold is the gold defeating it gives, 0 or more.
	Gold int `yaml:"gold,omitempty"`
}

Enemy is a foe's record.

type LearnedSkill struct {
	// ID names a skill of the database.
	ID string `yaml:"id"`
	// Level is the level the skill is learned at, 1 or more.
	Level int `yaml:"level"`
}

LearnedSkill is a skill a class learns on reaching a level.

type Progression struct {
	// Level is the current level, 1 or more.
	Level int `yaml:"level"`
	// Experience is the experience gathered since reaching Level.
	Experience int `yaml:"experience"`
	// MaxLevel is the level cap, or 0 for none.
	MaxLevel int `yaml:"maxLevel"`
	// LevelXP scales the experience each level takes; 0 or less never levels up.
	LevelXP int `yaml:"levelXP"`
}

Progression is a character's level and the experience gathered toward the next one. Going from level n to n+1 takes n × LevelXP experience; at MaxLevel, when it is above 0, experience no longer levels up.

func NewProgression(level, maxLevel, levelXP int) Progression

NewProgression returns a progression at level, raised to 1 and lowered to maxLevel when it has one, with no experience gathered.

func (p *Progression) AddExperience(xp int) []int

AddExperience adds xp, levels up as many times as it pays for and returns each level reached, in order. Experience left over after the last level up stays toward the next one; at the level cap it keeps accumulating without effect. A nil progression or an xp of 0 or less changes nothing.

func (p Progression) NeedForNext() int

NeedForNext returns the experience the next level takes in total, or 0 when the progression cannot level up any more.

type Skill struct {
	// ID identifies the skill among the database's skills.
	ID string `yaml:"id"`
	// Name is the display name or its localization key.
	Name string `yaml:"name"`
	// Scope is who the skill targets, such as self, one_enemy or all_allies.
	Scope string `yaml:"scope"`
	// Formula is the damage or healing formula, such as a.attack * 2 - b.defense.
	Formula string `yaml:"formula,omitempty"`
	// Cost is what using the skill costs, 0 or more.
	Cost int `yaml:"cost,omitempty"`
}

Skill is a skill's record. Scope and Formula are free-form text for the game's battle rules to interpret.

type Troop struct {
	// ID identifies the troop among the database's troops.
	ID string `yaml:"id"`
	// Members are the troop's enemies and where they stand; a troop has at least one.
	Members []TroopMember `yaml:"members"`
}

Troop is a group of enemies a battle starts with.

type TroopMember struct {
	// Enemy names an enemy of the database.
	Enemy string `yaml:"enemy"`
	// X is the member's horizontal position.
	X int `yaml:"x"`
	// Y is the member's vertical position.
	Y int `yaml:"y"`
}

TroopMember is one enemy in a troop and its position.