package content import ( "encoding/json" "fmt" "math" "slices" ) type CraftMaterial struct { ID string `json:"id" yaml:"id"` Name string `json:"name" yaml:"name"` Description string `json:"description" yaml:"description"` } type CraftStat struct { Stat string `json:"stat" yaml:"stat"` Min float64 `json:"min" yaml:"min"` Max float64 `json:"max" yaml:"max"` Percent bool `json:"percent" yaml:"percent"` Weight float64 `json:"weight" yaml:"weight"` } type CraftRecipe struct { ID string `json:"id" yaml:"id"` Revision int64 `json:"revision" yaml:"revision"` Name string `json:"name" yaml:"name"` Description string `json:"description" yaml:"description"` Profession string `json:"profession" yaml:"profession"` Level int64 `json:"level" yaml:"level"` XP int64 `json:"xp" yaml:"xp"` Inputs map[string]int64 `json:"inputs" yaml:"inputs"` Outputs map[string]int64 `json:"outputs" yaml:"outputs"` Stats map[string]float64 `json:"stats" yaml:"stats"` Titles []string `json:"titles" yaml:"titles"` } type CraftRules struct { BaseFailure float64 `json:"baseFailure" yaml:"baseFailure"` MinFailure float64 `json:"minFailure" yaml:"minFailure"` MaxFailure float64 `json:"maxFailure" yaml:"maxFailure"` OverLevelDecay float64 `json:"overLevelDecay" yaml:"overLevelDecay"` UnderLevelGrowth float64 `json:"underLevelGrowth" yaml:"underLevelGrowth"` BaseLoss float64 `json:"baseLoss" yaml:"baseLoss"` MinLoss float64 `json:"minLoss" yaml:"minLoss"` LossDecay float64 `json:"lossDecay" yaml:"lossDecay"` QualityPerLevel float64 `json:"qualityPerLevel" yaml:"qualityPerLevel"` MaxQualityBias float64 `json:"maxQualityBias" yaml:"maxQualityBias"` Cooldown float64 `json:"cooldown" yaml:"cooldown"` } type CraftingContent struct { RecipeHistory []CraftRecipe `json:"recipeHistory,omitempty" yaml:"recipeHistory"` Professions []GatheringSkill `json:"professions" yaml:"professions"` Materials []CraftMaterial `json:"materials" yaml:"materials"` Recipes []CraftRecipe `json:"recipes" yaml:"recipes"` Rules CraftRules `json:"rules" yaml:"rules"` } func (r CraftRules) Validate() error { if !finiteRange(r.BaseFailure, 0, 100) || !finiteRange(r.MinFailure, 0, r.BaseFailure) || !finiteRange(r.MaxFailure, r.BaseFailure, 100) || !finiteRange(r.OverLevelDecay, .001, 2) || !finiteRange(r.UnderLevelGrowth, .001, 2) || !finiteRange(r.BaseLoss, 1, 100) || !finiteRange(r.MinLoss, 1, r.BaseLoss) || !finiteRange(r.LossDecay, .001, 2) || !finiteRange(r.QualityPerLevel, 0, 1) || !finiteRange(r.MaxQualityBias, 1, 20) || !finiteRange(r.Cooldown, .1, 60) { return fmt.Errorf("invalid crafting probability, loss, quality or cooldown tuning") } return nil } func (r CraftRules) Failure(level, required int64) float64 { gap := float64(level - required) chance := r.BaseFailure * math.Exp(-math.Max(0, gap)*r.OverLevelDecay) if gap < 0 { chance = 100 - (100-r.BaseFailure)*math.Exp(gap*r.UnderLevelGrowth) } return math.Max(r.MinFailure, math.Min(r.MaxFailure, chance)) } func (r CraftRules) Loss(level int64) float64 { return math.Max(r.MinLoss, r.BaseLoss*math.Exp(-float64(max(0, level-1))*r.LossDecay)) } func SkillLevel(s GatheringSkill, xp int64) (int64, int64) { level := int64(1) for level < s.MaxLevel { need := XPForLevel(level + 1) if xp < need { return level, need - xp } xp -= need level++ } return level, 0 } func (c Catalog) StackIDs() map[string]bool { ids := map[string]bool{} for id := range c.BuildingItems() { ids[id] = true } for id := range c.PowerItems() { ids[id] = true } for _, id := range []string{"axe", "flint_and_steel", "fishing_pole", "wood", "ore", "herb", "bandage", "escape_stone"} { ids[id] = true } if c.Gathering != nil { for _, r := range c.Gathering.Resources { if r.Output == "self" { ids[r.ID] = true } } for _, v := range c.Gathering.Consumables { ids[v.ID] = true } } if c.Crafting != nil { for _, m := range c.Crafting.Materials { ids[m.ID] = true } } return ids } func ValidateCraftPool(item ItemDefinition) error { if item.CraftRolls < 0 || item.CraftRolls > 8 || item.CraftRolls > int64(len(item.CraftPool)) || len(item.CraftPool) > 32 { return fmt.Errorf("invalid number of crafted stat rolls") } seen := map[string]bool{} for _, s := range item.CraftPool { key := fmt.Sprintf("%s:%t", s.Stat, s.Percent) if seen[key] || !KnownStat(s.Stat) || !finiteRange(s.Weight, .01, 1000) || !finiteRange(s.Min, -1000000, 1000000) || !finiteRange(s.Max, s.Min, 1000000) || (s.Percent && (s.Min < -100 || s.Max > 10000)) { return fmt.Errorf("invalid or duplicate crafted stat pool entry") } seen[key] = true } if len(item.Modifiers)+int(item.CraftRolls) > 64 { return fmt.Errorf("crafted result has too many modifiers") } return nil } func (c Catalog) validateCrafting() error { if c.Crafting == nil { return nil } v := c.Crafting if len(v.Professions) < 1 || len(v.Professions) > 64 || len(v.Materials) > 256 || len(v.Recipes) > 512 || len(v.RecipeHistory) > 4096 { return fmt.Errorf("invalid crafting content limits") } if err := v.Rules.Validate(); err != nil { return err } professions := map[string]bool{} for _, s := range v.Professions { if !validSetIdentity(s.ID, s.Name, s.Description) || professions[s.ID] || s.BaseXP < 1 || s.BaseXP > 1000000 || s.XPPerLevel < 1 || s.XPPerLevel > 1000000 || s.MaxLevel < 1 || s.MaxLevel > 1000 { return fmt.Errorf("invalid crafting profession") } professions[s.ID] = true } base := c base.Crafting = nil stacks := base.StackIDs() outputs := base.StackIDs() for _, i := range c.Items { outputs[i.ID] = true } for _, m := range v.Materials { if !validSetIdentity(m.ID, m.Name, m.Description) || outputs[m.ID] { return fmt.Errorf("invalid or colliding crafting material") } outputs[m.ID] = true stacks[m.ID] = true } titles := map[string]bool{} for _, t := range c.Titles { titles[t.ID] = true } ids, combinations := map[string]bool{}, map[string]bool{} history := map[string]bool{} for _, r := range v.RecipeHistory { key := fmt.Sprintf("%s:%d", r.ID, r.Revision) if history[key] { return fmt.Errorf("duplicate archived recipe revision") } history[key] = true for _, current := range v.Recipes { if current.ID == r.ID && current.Revision <= r.Revision { return fmt.Errorf("archived recipe must precede current revision") } } copy := *v copy.Recipes = []CraftRecipe{r} copy.RecipeHistory = nil catalog := c catalog.Crafting = © if err := catalog.validateCrafting(); err != nil { return err } } for _, r := range v.Recipes { if !validSetIdentity(r.ID, r.Name, r.Description) || ids[r.ID] || r.Revision < 1 || !professions[r.Profession] || r.Level < 1 || r.Level > 1000 || r.XP < 1 || r.XP > 1000000 || len(r.Inputs) < 1 || len(r.Inputs) > 16 || len(r.Outputs) < 1 || len(r.Outputs) > 8 || len(r.Stats) > 32 || len(r.Titles) > 16 { return fmt.Errorf("invalid crafting recipe %q", r.Name) } ids[r.ID] = true for id, n := range r.Inputs { if !stacks[id] || n < 1 || n > 1000 { return fmt.Errorf("recipe inputs require stack materials and quantities 1-1000") } } count := int64(0) for id, n := range r.Outputs { if !outputs[id] || n < 1 || n > 100 { return fmt.Errorf("invalid recipe output") } count += n } if count > 100 { return fmt.Errorf("at most 100 output units per craft") } raw, _ := json.Marshal(r.Inputs) key := r.Profession + ":" + string(raw) if combinations[key] { return fmt.Errorf("ambiguous ingredient combination in profession") } combinations[key] = true for stat, n := range r.Stats { if !KnownStat(stat) || !finiteRange(n, 0, 1000000) { return fmt.Errorf("invalid crafting stat requirement") } } seen := map[string]bool{} for _, id := range r.Titles { if !titles[id] || seen[id] { return fmt.Errorf("unknown or repeated crafting title requirement") } seen[id] = true } } return nil } // Old worlds/catalogs receive the new crafting system once. Owned items keep // their copied modifiers; only future crafts use the seed stat pools. func (c *Catalog) RestoreCrafting(defaults Balance) { if c.Crafting != nil { return } c.Crafting = defaults.Crafting c.Items = slices.Clone(c.Items) for i := range c.Items { for _, seed := range defaults.Items { if seed.ID == c.Items[i].ID { c.Items[i].CraftPool = seed.CraftPool c.Items[i].CraftRolls = seed.CraftRolls } } } }