163 lines
6.5 KiB
Go
163 lines
6.5 KiB
Go
package content
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"slices"
|
|
)
|
|
|
|
var PrimaryStats = []string{"constitution", "intelligence", "dexterity", "strength", "wisdom", "charisma", "luck"}
|
|
var DerivedStats = []string{"healthPoints", "endurance", "resistance", "healthRecovery", "magicDamage", "knowledge", "spellPoints", "manaPoints", "moveSpeed", "rangedDamage", "criticalChance", "dodgeChance", "staminaRecovery", "meleeDamage", "heavyDamage", "carryCapacity", "defence", "skillPoints", "xpGain", "manaRecovery", "buffEffectiveness", "dropRate", "gatherRate", "stamina"}
|
|
var StatLinks = map[string][]string{
|
|
"constitution": {"healthPoints", "endurance", "resistance", "healthRecovery"},
|
|
"intelligence": {"magicDamage", "knowledge", "spellPoints", "manaPoints"},
|
|
"dexterity": {"moveSpeed", "rangedDamage", "criticalChance", "endurance", "dodgeChance", "staminaRecovery"},
|
|
"strength": {"meleeDamage", "heavyDamage", "carryCapacity", "endurance", "defence"},
|
|
"wisdom": {"knowledge", "resistance", "skillPoints", "xpGain", "manaRecovery"},
|
|
"charisma": {"buffEffectiveness"},
|
|
"luck": {"criticalChance", "dropRate", "gatherRate", "dodgeChance"},
|
|
}
|
|
|
|
type StatModifier struct {
|
|
Stat string `json:"stat" yaml:"stat"`
|
|
Flat float64 `json:"flat" yaml:"flat"`
|
|
Percent float64 `json:"percent" yaml:"percent"`
|
|
}
|
|
type StatsRules struct {
|
|
Version int `json:"version" yaml:"version"`
|
|
Base map[string]float64 `json:"base" yaml:"base"`
|
|
Coefficients map[string]map[string]float64 `json:"coefficients" yaml:"coefficients"`
|
|
}
|
|
type ItemRequirements struct {
|
|
Level int64 `json:"level" yaml:"level"`
|
|
Stats map[string]float64 `json:"stats" yaml:"stats"`
|
|
Titles []string `json:"titles" yaml:"titles"`
|
|
}
|
|
type ItemDefinition struct {
|
|
ID string `json:"id" yaml:"id"`
|
|
Name string `json:"name" yaml:"name"`
|
|
Description string `json:"description" yaml:"description"`
|
|
Slot string `json:"slot" yaml:"slot"`
|
|
Weight float64 `json:"weight" yaml:"weight"`
|
|
DropChance float64 `json:"dropChance" yaml:"dropChance"`
|
|
Requirements ItemRequirements `json:"requirements" yaml:"requirements"`
|
|
Modifiers []StatModifier `json:"modifiers" yaml:"modifiers"`
|
|
}
|
|
type Catalog struct {
|
|
Version int64 `json:"version"`
|
|
Titles []Title `json:"titles"`
|
|
Items []ItemDefinition `json:"items"`
|
|
Stats StatsRules `json:"stats"`
|
|
}
|
|
|
|
func KnownStat(stat string) bool {
|
|
return slices.Contains(PrimaryStats, stat) || slices.Contains(DerivedStats, stat)
|
|
}
|
|
func (s StatsRules) Validate() error {
|
|
if s.Version != 1 || len(s.Base) != len(PrimaryStats)+len(DerivedStats) || len(s.Coefficients) != len(PrimaryStats) {
|
|
return fmt.Errorf("invalid stats rule version or missing stat values")
|
|
}
|
|
for k, v := range s.Base {
|
|
if !KnownStat(k) || math.IsNaN(v) || math.IsInf(v, 0) || v < 0 || v > 1000000 {
|
|
return fmt.Errorf("invalid stat base %q", k)
|
|
}
|
|
}
|
|
if s.Base["healthPoints"] < 50 || s.Base["stamina"] < 1 {
|
|
return fmt.Errorf("base health must be at least 50 and stamina positive")
|
|
}
|
|
for primary, links := range StatLinks {
|
|
coefficients, ok := s.Coefficients[primary]
|
|
if !ok || len(coefficients) != len(links) {
|
|
return fmt.Errorf("missing coefficients for %s", primary)
|
|
}
|
|
for derived, v := range coefficients {
|
|
if !slices.Contains(links, derived) || math.IsNaN(v) || math.IsInf(v, 0) || v < 0 || v > 1000 {
|
|
return fmt.Errorf("invalid coefficient %s -> %s", primary, derived)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func ValidateModifiers(modifiers []StatModifier) error {
|
|
if len(modifiers) > 64 {
|
|
return fmt.Errorf("too many modifiers")
|
|
}
|
|
for _, m := range modifiers {
|
|
if !KnownStat(m.Stat) || math.IsNaN(m.Flat) || math.IsInf(m.Flat, 0) || math.Abs(m.Flat) > 1000000 || math.IsNaN(m.Percent) || math.IsInf(m.Percent, 0) || m.Percent < -100 || m.Percent > 10000 {
|
|
return fmt.Errorf("invalid modifier for %q", m.Stat)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func ValidItemSlot(slot string) bool {
|
|
return slices.Contains([]string{"helmet", "mask", "chest", "shoulders", "pants", "boots", "gloves", "earrings", "necklace", "ring", "weapon", "offhand", "cape", "bag"}, slot)
|
|
}
|
|
func (item ItemDefinition) Validate() error {
|
|
if slices.Contains([]string{"axe","flint_and_steel","fishing_pole","wood","ore","herb","bandage"},item.ID){return fmt.Errorf("equipment ID conflicts with a stackable item")}
|
|
if math.IsNaN(item.DropChance) || math.IsInf(item.DropChance, 0) || item.DropChance < 0 || item.DropChance > 100 {
|
|
return fmt.Errorf("invalid drop chance")
|
|
}
|
|
if !titleID.MatchString(item.ID) || len(item.Name) < 1 || len(item.Name) > 80 || len(item.Description) > 500 || !ValidItemSlot(item.Slot) || math.IsNaN(item.Weight) || math.IsInf(item.Weight, 0) || item.Weight < 0 || item.Weight > 10000 {
|
|
return fmt.Errorf("invalid equipment definition %q", item.ID)
|
|
}
|
|
if item.Requirements.Level < 1 || item.Requirements.Level > 10000 || len(item.Requirements.Stats) > len(PrimaryStats)+len(DerivedStats) || len(item.Requirements.Titles) > 64 {
|
|
return fmt.Errorf("invalid requirements for %q", item.ID)
|
|
}
|
|
for key, n := range item.Requirements.Stats {
|
|
if !KnownStat(key) || math.IsNaN(n) || math.IsInf(n, 0) || n < 0 || n > 1000000 {
|
|
return fmt.Errorf("invalid required stat %q", key)
|
|
}
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, id := range item.Requirements.Titles {
|
|
if !titleID.MatchString(id) || seen[id] {
|
|
return fmt.Errorf("invalid required title %q", id)
|
|
}
|
|
seen[id] = true
|
|
}
|
|
return ValidateModifiers(item.Modifiers)
|
|
}
|
|
func (c Catalog) Validate() error {
|
|
if c.Version < 1 || len(c.Items) > 512 {
|
|
return fmt.Errorf("invalid catalog version or item count")
|
|
}
|
|
if err := c.Stats.Validate(); err != nil {
|
|
return err
|
|
}
|
|
defaults, err := Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defaults.Titles = c.Titles
|
|
if err = defaults.validateProgression(); err != nil {
|
|
return err
|
|
}
|
|
titles := map[string]bool{}
|
|
for _, title := range c.Titles {
|
|
titles[title.ID] = true
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, item := range c.Items {
|
|
if seen[item.ID] {
|
|
return fmt.Errorf("duplicate item %q", item.ID)
|
|
}
|
|
seen[item.ID] = true
|
|
if err = item.Validate(); err != nil {
|
|
return err
|
|
}
|
|
for _, id := range item.Requirements.Titles {
|
|
if !titles[id] {
|
|
return fmt.Errorf("unknown required title %q", id)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func DefaultCatalog() (Catalog, error) {
|
|
b, err := Load()
|
|
if err != nil {
|
|
return Catalog{}, err
|
|
}
|
|
return Catalog{Version: 1, Titles: b.Titles, Items: b.Items, Stats: *b.Stats}, nil
|
|
}
|