Files

177 lines
7.7 KiB
Go

package content
import (
"fmt"
"math"
"slices"
)
// A skill is data, not an enum. All gathering professions share this curve.
type GatheringSkill struct {
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
BaseXP int64 `json:"baseXp" yaml:"baseXp"`
XPPerLevel int64 `json:"xpPerLevel" yaml:"xpPerLevel"`
MaxLevel int64 `json:"maxLevel" yaml:"maxLevel"`
}
type ResourceDefinition struct {
Appearance *Appearance `json:"appearance,omitempty" yaml:"appearance,omitempty"`
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
SkillID string `json:"skillId" yaml:"skillId"`
Model string `json:"model" yaml:"model"`
Output string `json:"output" yaml:"output"`
RequiredLevel int64 `json:"requiredLevel" yaml:"requiredLevel"`
Amount int64 `json:"amount" yaml:"amount"`
XP int64 `json:"xp" yaml:"xp"`
Seconds float64 `json:"seconds" yaml:"seconds"`
RespawnSeconds float64 `json:"respawnSeconds" yaml:"respawnSeconds"`
Density int64 `json:"density" yaml:"density"`
Biomes []string `json:"biomes" yaml:"biomes"`
}
type BiomeDefinition struct {
Revision int64 `json:"revision,omitempty" yaml:"revision"`
Resources []string `json:"resources" yaml:"resources"`
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
Color string `json:"color" yaml:"color"`
Weight int64 `json:"weight" yaml:"weight"`
}
type GatheringConsumable struct {
UseEffect *ItemUseEffect `json:"useEffect,omitempty" yaml:"useEffect"`
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
Seconds float64 `json:"seconds" yaml:"seconds"`
Modifiers []StatModifier `json:"modifiers" yaml:"modifiers"`
}
type GatheringContent struct {
BiomeHistory []BiomeDefinition `json:"biomeHistory,omitempty" yaml:"biomeHistory"`
Skills []GatheringSkill `json:"skills" yaml:"skills"`
Resources []ResourceDefinition `json:"resources" yaml:"resources"`
Biomes []BiomeDefinition `json:"biomes" yaml:"biomes"`
Consumables []GatheringConsumable `json:"consumables" yaml:"consumables"`
}
type GatheringRules struct {
FailureBase float64 `json:"failureBase" yaml:"failureBase"`
FailurePerLevel float64 `json:"failurePerLevel" yaml:"failurePerLevel"`
FailureMax float64 `json:"failureMax" yaml:"failureMax"`
DestroyBase float64 `json:"destroyBase" yaml:"destroyBase"`
DestroyPerLevel float64 `json:"destroyPerLevel" yaml:"destroyPerLevel"`
NegativeChance float64 `json:"negativeChance" yaml:"negativeChance"`
DamagePerLevel float64 `json:"damagePerLevel" yaml:"damagePerLevel"`
FatigueSeconds float64 `json:"fatigueSeconds" yaml:"fatigueSeconds"`
FatiguePercent float64 `json:"fatiguePercent" yaml:"fatiguePercent"`
JumpSpeed float64 `json:"jumpSpeed" yaml:"jumpSpeed"`
Gravity float64 `json:"gravity" yaml:"gravity"`
HillHeight float64 `json:"hillHeight" yaml:"hillHeight"`
DetailHeight float64 `json:"detailHeight" yaml:"detailHeight"`
}
var GatheringStats = []string{"gatheringLevel", "gatheringYield", "gatheringSpeed", "gatheringXP"}
func GatheringStat(key string) bool { return slices.Contains(GatheringStats, key) }
func finiteRange(v, low, high float64) bool {
return !math.IsNaN(v) && !math.IsInf(v, 0) && v >= low && v <= high
}
func (r GatheringRules) Validate() error {
for _, v := range []float64{r.FailureBase, r.FailurePerLevel, r.FailureMax, r.DestroyBase, r.DestroyPerLevel, r.NegativeChance, r.FatiguePercent} {
if !finiteRange(v, 0, 100) {
return fmt.Errorf("invalid gathering probability or fatigue")
}
}
if r.FailureMax < r.FailureBase || !finiteRange(r.DamagePerLevel, 0, 100) || !finiteRange(r.FatigueSeconds, 1, 3600) || !finiteRange(r.JumpSpeed, 1, 30) || !finiteRange(r.Gravity, 1, 100) || !finiteRange(r.HillHeight, 1, 100) || !finiteRange(r.DetailHeight, 0, 20) {
return fmt.Errorf("invalid gathering/movement rules")
}
return nil
}
func (g GatheringContent) Validate() error {
if len(g.BiomeHistory) > 1024 {
return fmt.Errorf("biome revision history limit reached")
}
if len(g.Skills) < 1 || len(g.Skills) > 64 || len(g.Resources) > 256 || len(g.Biomes) < 1 || len(g.Biomes) > 32 || len(g.Consumables) > 256 {
return fmt.Errorf("invalid gathering content counts")
}
ids := map[string]bool{}
identity := func(id, name, desc string) bool {
if ids[id] || !validSetIdentity(id, name, desc) {
return false
}
ids[id] = true
return true
}
skills, biomes := map[string]bool{}, map[string]bool{}
for _, s := range g.Skills {
if !identity(s.ID, s.Name, s.Description) || s.BaseXP < 1 || s.BaseXP > 1000000 || s.XPPerLevel < 0 || s.XPPerLevel > 1000000 || s.MaxLevel < 2 || s.MaxLevel > 10000 {
return fmt.Errorf("invalid gathering skill %q", s.ID)
}
skills[s.ID] = true
}
for _, b := range g.Biomes {
if !identity(b.ID, b.Name, b.Description) || b.Weight < 1 || b.Weight > 100 || len(b.Color) != 7 || b.Color[0] != '#' {
return fmt.Errorf("invalid biome %q", b.ID)
}
for _, c := range b.Color[1:] {
if !slices.Contains([]rune("0123456789abcdefABCDEF"), c) {
return fmt.Errorf("invalid biome color")
}
}
biomes[b.ID] = true
}
for _, r := range g.Resources {
if err := ValidateAppearance(r.Appearance, map[string]string{"tree": "tree", "ore": "rock", "herb": "plant"}[r.Model]); err != nil {
return err
}
if !identity(r.ID, r.Name, r.Description) || !skills[r.SkillID] || !slices.Contains([]string{"tree", "ore", "herb"}, r.Model) || !slices.Contains([]string{"wood", "ore", "herb", "self"}, r.Output) || r.RequiredLevel < 1 || r.RequiredLevel > 10000 || r.Amount < 1 || r.Amount > 1000 || r.XP < 0 || r.XP > 1000000 || !finiteRange(r.Seconds, 0.1, 3600) || !finiteRange(r.RespawnSeconds, 1, 604800) || r.Density < 0 || r.Density > 32 || len(r.Biomes) < 1 || len(r.Biomes) > 32 {
return fmt.Errorf("invalid resource %q", r.ID)
}
seen := map[string]bool{}
for _, id := range r.Biomes {
if !biomes[id] || seen[id] {
return fmt.Errorf("unknown/duplicate resource biome %q", id)
}
seen[id] = true
}
}
for _, b := range append(slices.Clone(g.Biomes), g.BiomeHistory...) {
if b.Revision < 0 || len(b.Resources) > 256 {
return fmt.Errorf("invalid biome revision")
}
seen := map[string]bool{}
for _, id := range b.Resources {
if seen[id] || !slices.ContainsFunc(g.Resources, func(r ResourceDefinition) bool { return r.ID == id }) {
return fmt.Errorf("unknown/duplicate biome resource %q", id)
}
seen[id] = true
}
}
for _, c := range g.Consumables {
if !identity(c.ID, c.Name, c.Description) || !finiteRange(c.Seconds, 1, 86400) || (c.UseEffect == nil && len(c.Modifiers) < 1) {
return fmt.Errorf("invalid consumable %q", c.ID)
}
if c.UseEffect != nil && (len(c.Modifiers) != 0 || !c.UseEffect.Valid()) {
return fmt.Errorf("invalid consumable use effect")
}
if err := ValidateModifiers(c.Modifiers); err != nil {
return err
}
for _, m := range c.Modifiers {
if !GatheringStat(m.Stat) {
return fmt.Errorf("gathering consumables require gathering modifiers")
}
}
}
return nil
}
func (g GatheringContent) ValidateScopes(mods []StatModifier) error {
for _, m := range mods {
if m.Skill != "" && !slices.ContainsFunc(g.Skills, func(s GatheringSkill) bool { return s.ID == m.Skill }) {
return fmt.Errorf("unknown gathering skill %q", m.Skill)
}
}
return nil
}