package content import ( "fmt" "slices" ) type CombatAttack struct { DexteritySpeed float64 `json:"dexteritySpeed,omitempty" yaml:"dexteritySpeed"` StrengthSpeed float64 `json:"strengthSpeed,omitempty" yaml:"strengthSpeed"` ID string `json:"id" yaml:"id"` Name string `json:"name" yaml:"name"` Description string `json:"description" yaml:"description"` Delivery string `json:"delivery" yaml:"delivery"` Effect string `json:"effect" yaml:"effect"` Stat string `json:"stat" yaml:"stat"` Multiplier float64 `json:"multiplier" yaml:"multiplier"` Range float64 `json:"range" yaml:"range"` Cooldown float64 `json:"cooldown" yaml:"cooldown"` Stamina float64 `json:"stamina" yaml:"stamina"` Speed float64 `json:"speed" yaml:"speed"` Radius float64 `json:"radius" yaml:"radius"` Duration float64 `json:"duration" yaml:"duration"` Interval float64 `json:"interval" yaml:"interval"` } func (a CombatAttack) Validate() error { if !finiteRange(a.DexteritySpeed, 0, 5) || !finiteRange(a.StrengthSpeed, 0, 5) { return fmt.Errorf("invalid projectile speed coefficients") } if !validSetIdentity(a.ID, a.Name, a.Description) || !slices.Contains([]string{"melee", "projectile"}, a.Delivery) || !slices.Contains([]string{"damage", "heal"}, a.Effect) || !slices.Contains([]string{"meleeDamage", "rangedDamage", "magicDamage"}, a.Stat) || !finiteRange(a.Multiplier, .01, 100) || !finiteRange(a.Range, 1, 128) || !finiteRange(a.Cooldown, .1, 120) || !finiteRange(a.Stamina, 0, 10000) || !finiteRange(a.Speed, 1, 100) || !finiteRange(a.Radius, 0, 24) || !finiteRange(a.Duration, 0, 60) || !finiteRange(a.Interval, .2, 60) { return fmt.Errorf("invalid attack %q", a.ID) } if a.Duration > 0 && (a.Radius <= 0 || a.Interval > a.Duration) { return fmt.Errorf("timed areas need radius and interval <= duration") } return nil } type LootEntry struct { Item string `json:"item" yaml:"item"` Amount int64 `json:"amount" yaml:"amount"` Chance float64 `json:"chance" yaml:"chance"` Group string `json:"group" yaml:"group"` Weight int64 `json:"weight" yaml:"weight"` } type LootTable struct { ID string `json:"id" yaml:"id"` Name string `json:"name" yaml:"name"` Description string `json:"description" yaml:"description"` Entries []LootEntry `json:"entries" yaml:"entries"` } type MobDefinition 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"` Model string `json:"model" yaml:"model"` Behavior string `json:"behavior" yaml:"behavior"` Level int64 `json:"level" yaml:"level"` HP float64 `json:"hp" yaml:"hp"` Damage float64 `json:"damage" yaml:"damage"` Defence float64 `json:"defence" yaml:"defence"` Speed float64 `json:"speed" yaml:"speed"` AggroRange float64 `json:"aggroRange" yaml:"aggroRange"` LeashRange float64 `json:"leashRange" yaml:"leashRange"` ScalePerDay float64 `json:"scalePerDay" yaml:"scalePerDay"` XP int64 `json:"xp" yaml:"xp"` AttackID string `json:"attackId" yaml:"attackId"` LootID string `json:"lootId" yaml:"lootId"` } type MobSpawn struct { ID string `json:"id" yaml:"id"` Name string `json:"name" yaml:"name"` Description string `json:"description" yaml:"description"` MobID string `json:"mobId" yaml:"mobId"` X float64 `json:"x" yaml:"x"` Z float64 `json:"z" yaml:"z"` Radius float64 `json:"radius" yaml:"radius"` Count int64 `json:"count" yaml:"count"` Respawn float64 `json:"respawn" yaml:"respawn"` MinPlayerDistance float64 `json:"minPlayerDistance" yaml:"minPlayerDistance"` Biomes []string `json:"biomes" yaml:"biomes"` } type CombatContent struct { Attacks []CombatAttack `json:"attacks" yaml:"attacks"` Mobs []MobDefinition `json:"mobs" yaml:"mobs"` Loot []LootTable `json:"loot" yaml:"loot"` Spawns []MobSpawn `json:"spawns" yaml:"spawns"` } type CombatRules struct { MaxMobs int `yaml:"maxMobs"` MaxProjectiles int `yaml:"maxProjectiles"` MaxAreas int `yaml:"maxAreas"` MaxTargets int `yaml:"maxTargets"` MaxSlope float64 `yaml:"maxSlope"` HitRadius float64 `yaml:"hitRadius"` } func (r CombatRules) Validate() error { if r.MaxMobs < 1 || r.MaxMobs > 2048 || r.MaxProjectiles < 1 || r.MaxProjectiles > 512 || r.MaxAreas < 1 || r.MaxAreas > 128 || r.MaxTargets < 1 || r.MaxTargets > 128 || !finiteRange(r.MaxSlope, .1, 4) || !finiteRange(r.HitRadius, .1, 3) { return fmt.Errorf("invalid combat work limits") } return nil } func (c Catalog) validateCombat() error { if c.Combat == nil { return nil } v := c.Combat if len(v.Attacks) > 256 || len(v.Mobs) > 256 || len(v.Loot) > 256 || len(v.Spawns) > 256 { return fmt.Errorf("too much combat content") } 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 } attacks, loot, mobs := map[string]bool{}, map[string]bool{}, map[string]bool{} for _, a := range v.Attacks { if !identity(a.ID, a.Name, a.Description) { return fmt.Errorf("duplicate attack") } if err := a.Validate(); err != nil { return err } attacks[a.ID] = true } items := map[string]bool{} for id := range c.BuildingItems() { items[id] = true } for id := range c.PowerItems() { items[id] = true } if c.Crafting != nil { for _, m := range c.Crafting.Materials { items[m.ID] = true } } for _, id := range []string{"axe", "flint_and_steel", "fishing_pole", "wood", "ore", "herb", "bandage", "escape_stone"} { items[id] = true } for _, i := range c.Items { items[i.ID] = true } if c.Gathering != nil { for _, r := range c.Gathering.Resources { if r.Output == "self" { items[r.ID] = true } } for _, i := range c.Gathering.Consumables { items[i.ID] = true } } for _, l := range v.Loot { if !identity(l.ID, l.Name, l.Description) || len(l.Entries) > 32 { return fmt.Errorf("invalid loot table") } loot[l.ID] = true for _, e := range l.Entries { if !items[e.Item] || e.Amount < 1 || e.Amount > 100 || !finiteRange(e.Chance, 0, 100) || len(e.Group) > 40 || e.Weight < 1 || e.Weight > 10000 { return fmt.Errorf("invalid loot entry") } } } for _, m := range v.Mobs { if err := ValidateAppearance(m.Appearance, "creature", "human"); err != nil { return err } if !identity(m.ID, m.Name, m.Description) || !slices.Contains([]string{"sentinel", "boss"}, m.Model) || !slices.Contains([]string{"aggressive", "guard", "passive"}, m.Behavior) || m.Level < 1 || m.Level > 10000 || !finiteRange(m.HP, 1, 1000000) || !finiteRange(m.Damage, 0, 100000) || !finiteRange(m.Defence, 0, 100000) || !finiteRange(m.Speed, .1, 30) || !finiteRange(m.AggroRange, 1, 128) || !finiteRange(m.LeashRange, m.AggroRange, 512) || !finiteRange(m.ScalePerDay, 0, 10) || m.XP < 0 || m.XP > 1000000000 || !attacks[m.AttackID] || (m.LootID != "" && !loot[m.LootID]) { return fmt.Errorf("invalid mob %q or missing attack/loot reference", m.ID) } mobs[m.ID] = true } for _, s := range v.Spawns { if !identity(s.ID, s.Name, s.Description) || !mobs[s.MobID] || !finiteRange(s.X, -100000, 100000) || !finiteRange(s.Z, -100000, 100000) || !finiteRange(s.Radius, 0, 512) || s.Count < 1 || s.Count > 32 || !finiteRange(s.Respawn, 1, 604800) || !finiteRange(s.MinPlayerDistance, 0, 128) || len(s.Biomes) > 32 { return fmt.Errorf("invalid spawn") } for _, id := range s.Biomes { if c.Gathering == nil || !slices.ContainsFunc(c.Gathering.Biomes, func(b BiomeDefinition) bool { return b.ID == id }) { return fmt.Errorf("unknown spawn biome") } } } for _, i := range c.Items { if i.AttackID != "" && (!attacks[i.AttackID] || i.Slot != "weapon") { return fmt.Errorf("weapon references unknown attack") } } return nil }