API 参考取自源码中的 Go 文档注释,以英文书写。
package loot
import "github.com/nsgo-dev/game/pkg/loot"概述
Package loot rolls nested weighted drop tables with guaranteed drops and pity counters on the deterministic streams of package random.
Tables
A Document is the versioned YAML text a game's author writes: a list of named tables. Each Table holds weighted Entries, of which it picks Rolls times, and Guaranteed entries it drops every time it is rolled. An Entry drops an item, rolls another table (nesting), or drops nothing. Parse decodes and validates a Document, refusing unknown fields, unsupported versions, unknown or cyclic table references and runaway sizes; Encode writes the same Document back as deterministic YAML that parses to an equal value.
Rolling
New compiles a Document into an immutable Library. A Roller rolls a Library's tables on one random.Stream, so a seed and a sequence of rolls always produce the same drops on every platform. A Library can be shared by many Rollers; each Roller is used by one goroutine.
Pity
A table's Pity rules raise the chance of one of its entries after a run of picks without it. A hard rule (GuaranteeAt) forces the entry on the GuaranteeAt-th pick since it was last picked; a soft rule (SoftFrom, SoftStep) adds SoftStep to the entry's weight for every pick from the SoftFrom-th one on. A Roller keeps a miss counter per rule; State and SetState copy the counters out and back in for save slots, beside the stream's own random.State.
索引
- 常量
- 变量
- func Encode(doc *Document) ([]byte, error)
- func Validate(doc *Document) error
- type Document
- type Drop
- type Entry
- type Library
- type Pity
- type Range
- type Roller
- func NewRoller(lib *Library, stream *random.Stream) *Roller
- func (r *Roller) AppendRoll(dst []Drop, table string) ([]Drop, error)
- func (r *Roller) Library() *Library
- func (r *Roller) Misses(table, rule string) (int, bool)
- func (r *Roller) ResetPity()
- func (r *Roller) Roll(table string) ([]Drop, error)
- func (r *Roller) SetState(st State) error
- func (r *Roller) State() State
- func (r *Roller) Stream() *random.Stream
- type State
- type Table
常量
const (
// MaxRolls is the largest number of weighted picks one roll of a table may make.
MaxRolls = 1024
// MaxCount is the largest quantity of an item entry, or the largest number of times a table entry rolls its table.
MaxCount = 1 << 30
// MaxWeight is the largest entry weight and the largest SoftStep.
MaxWeight = 1e15
// MaxGuaranteeAt is the largest GuaranteeAt and SoftFrom of a pity rule.
MaxGuaranteeAt = 1 << 20
// MaxRollSteps bounds the work of one Roller.Roll: the worst case of picks, sub-table rolls and drops a table can produce, which Validate computes from Rolls and Count maxima, must not exceed it. A weighted pick counts as many steps as its table has entries and pity rules, because it scans all of them.
MaxRollSteps = 1 << 16
)Limits that keep every roll bounded. Validate refuses documents that exceed them.
const CurrentVersion = 1CurrentVersion is the loot document schema version that Parse accepts and Encode writes.
const MaxMisses = 1 << 30MaxMisses is the largest value a pity miss counter holds; counting saturates there, and SetState refuses larger values.
变量
var ErrUnknownTable = errors.New("loot: unknown table")ErrUnknownTable is returned by Roller.Roll for a table name the Library does not hold.
函数
func Encode(doc *Document) ([]byte, error)Encode validates doc and writes it as deterministic 2-space YAML that Parse decodes to an equal Document.
func Validate(doc *Document) errorValidate checks doc against the version 1 schema: names, entry kinds, weights, ranges, pity rules, table references without cycles, and the MaxRollSteps bound on every table.
类型
type Document struct {
// Version is the schema version; it must equal CurrentVersion (1).
Version int `yaml:"version"`
// Tables are the document's tables. Their names are unique, and entries refer to them by name.
Tables []Table `yaml:"tables"`
}Document is the root of a versioned YAML loot document.
func Parse(data []byte) (*Document, error)Parse decodes a YAML loot document and validates it. Unknown fields, duplicate keys and trailing YAML documents are rejected. Empty lists decode as nil, so the result equals the Document that its Encode text parses to.
type Drop struct {
Item string `json:"item" yaml:"item"`
Count int `json:"count" yaml:"count"`
}Drop is one item a roll produced, with its quantity (always at least 1).
type Entry struct {
// Item names the item this entry drops.
Item string `yaml:"item,omitempty"`
// Table names another table of the document that this entry rolls.
Table string `yaml:"table,omitempty"`
// Nothing marks an empty pick, which drops nothing; its weight sets the chance of a miss.
Nothing bool `yaml:"nothing,omitempty"`
// Weight is the entry's relative chance among the table's Entries. It must be positive there and 0 in Guaranteed.
Weight float64 `yaml:"weight,omitempty"`
// Count is the item's quantity, or how many times the table entry rolls its table, drawn uniformly from the range; nil means exactly one. A Nothing entry has no Count.
Count *Range `yaml:"count,omitempty"`
}Entry is one possible drop of a table. Exactly one of Item, Table and Nothing is set.
type Library struct {
// contains filtered or unexported fields
}Library is a compiled, immutable set of tables that any number of Rollers can roll concurrently.
func New(doc *Document) (*Library, error)New validates doc and compiles it into a Library. The Library does not keep doc, so later changes to doc do not affect it.
func (l *Library) Chances(table string) []float64Chances returns the probability of each of the named table's Entries on one pick with no pity ramp applied, in document order. It returns nil for an unknown table or one without entries.
func (l *Library) Has(name string) boolHas reports whether the Library holds a table named name.
func (l *Library) Tables() []stringTables returns the Library's table names in document order.
type Pity struct {
// Name identifies the rule's miss counter in State; it is unique within its table.
Name string `yaml:"name"`
// Item names the target entry by its Item.
Item string `yaml:"item,omitempty"`
// Table names the target entry by its Table.
Table string `yaml:"table,omitempty"`
// GuaranteeAt, when positive, forces the target on the GuaranteeAt-th pick since it was last picked. When several rules of a table force a pick at once, the first one in order wins and the others keep counting.
GuaranteeAt int `yaml:"guaranteeAt,omitempty"`
// SoftFrom, when positive, starts the soft ramp: on the n-th pick since the target was last picked, with n >= SoftFrom, the target's weight grows by (n-SoftFrom+1)*SoftStep. It must be below GuaranteeAt when both are set.
SoftFrom int `yaml:"softFrom,omitempty"`
// SoftStep is the weight the soft ramp adds per pick. It is positive exactly when SoftFrom is.
SoftStep float64 `yaml:"softStep,omitempty"`
}Pity is a rule that raises the chance of one entry of its table after a run of picks without it. Exactly one of Item and Table names the target entry, and at least one of GuaranteeAt and SoftFrom is set.
type Range struct {
Min int
Max int
}Range is an inclusive integer range [Min, Max]. In YAML it is written as a single integer when Min equals Max and as a mapping with min and max otherwise.
func (r Range) MarshalYAML() (any, error)MarshalYAML writes r as an integer when Min equals Max and as a min/max mapping otherwise.
func (r *Range) UnmarshalYAML(value *yaml.Node) errorUnmarshalYAML reads an integer or a mapping with exactly the keys min and max.
type Roller struct {
// contains filtered or unexported fields
}Roller rolls a Library's tables on one random.Stream and keeps the pity miss counters of one player or save. A Roller is not safe for concurrent use; give each goroutine its own Roller and stream.
func NewRoller(lib *Library, stream *random.Stream) *RollerNewRoller returns a Roller with zero pity counters that rolls lib's tables on stream, typically a stream forked for loot such as streams.Stream("loot"). It panics if lib or stream is nil.
func (r *Roller) AppendRoll(dst []Drop, table string) ([]Drop, error)AppendRoll is like Roll but appends the drops to dst, so a caller that reuses dst rolls without allocating. On an unknown table it returns dst unchanged and an error wrapping ErrUnknownTable, without drawing from the stream or touching the counters.
func (r *Roller) Misses(table, rule string) (int, bool)Misses returns how many picks of table have passed since the target of its pity rule was last picked, and false when the table or rule does not exist. The next pick is pick number Misses+1, which is what GuaranteeAt and SoftFrom count.
func (r *Roller) ResetPity()ResetPity sets every pity counter of r back to zero.
func (r *Roller) Roll(table string) ([]Drop, error)Roll rolls the named table once and returns its drops in the order they were produced: guaranteed entries first, then each weighted pick, with nested tables expanded in place.
func (r *Roller) SetState(st State) errorSetState replaces r's pity counters with st's. Rules missing from st start at zero, and counters for tables or rules the Library no longer has are ignored, so a save outlives edits to the loot document. It refuses a counter below 0 or above MaxMisses and then leaves r unchanged.
func (r *Roller) Stream() *random.StreamStream returns the stream r draws from; save its State beside r.State to resume the same drops.
type State struct {
Pity map[string]map[string]int `json:"pity,omitempty" yaml:"pity,omitempty"`
}State is a Roller's pity counters as plain data for save slots, keyed by table name and then by pity rule name. Only non-zero counters are listed. Its map keys marshal in sorted order with encoding/json and YAML, so its text is deterministic.
type Table struct {
// Name identifies the table to Roller.Roll and to entries of other tables.
Name string `yaml:"name"`
// Rolls is how many weighted picks one roll of the table makes, drawn uniformly from the range; nil means exactly one. It must be nil when Entries is empty.
Rolls *Range `yaml:"rolls,omitempty"`
// Guaranteed entries drop every time the table is rolled, before its weighted picks. Their Weight must be 0 and they cannot be Nothing.
Guaranteed []Entry `yaml:"guaranteed,omitempty"`
// Entries are the weighted choices of each pick. Every Weight must be positive, and no two entries may name the same item or the same table.
Entries []Entry `yaml:"entries,omitempty"`
// Pity rules raise the chance of one entry of Entries after a run of picks without it.
Pity []Pity `yaml:"pity,omitempty"`
}Table is one named drop table.