79 lines
2.7 KiB
Go
79 lines
2.7 KiB
Go
package content
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
)
|
|
|
|
type BuildingDefinition struct {
|
|
ID string `json:"id" yaml:"id"`
|
|
Name string `json:"name" yaml:"name"`
|
|
Description string `json:"description" yaml:"description"`
|
|
Model string `json:"model" yaml:"model"`
|
|
Width float64 `json:"width" yaml:"width"`
|
|
Depth float64 `json:"depth" yaml:"depth"`
|
|
Height float64 `json:"height" yaml:"height"`
|
|
HP float64 `json:"hp" yaml:"hp"`
|
|
Profession string `json:"profession" yaml:"profession"`
|
|
Level int64 `json:"level" yaml:"level"`
|
|
}
|
|
type BuildingContent struct {
|
|
Definitions []BuildingDefinition `json:"definitions" yaml:"definitions"`
|
|
MaxStructures int `json:"maxStructures" yaml:"maxStructures"`
|
|
Range float64 `json:"range" yaml:"range"`
|
|
ProtectedRadius float64 `json:"protectedRadius" yaml:"protectedRadius"`
|
|
MaxSlope float64 `json:"maxSlope" yaml:"maxSlope"`
|
|
}
|
|
|
|
func (d BuildingDefinition) Validate() error {
|
|
if !validSetIdentity(d.ID, d.Name, d.Description) || !slices.Contains([]string{"wall", "camp", "fort"}, d.Model) || !finiteRange(d.Width, .5, 8) || !finiteRange(d.Depth, .5, 8) || !finiteRange(d.Height, .5, 6) || !finiteRange(d.HP, 1, 1000000) || d.Level < 1 || d.Level > 1000 {
|
|
return fmt.Errorf("invalid structure definition %q", d.Name)
|
|
}
|
|
return nil
|
|
}
|
|
func (c Catalog) BuildingItems() map[string]string {
|
|
out := map[string]string{}
|
|
if c.Building != nil {
|
|
for _, d := range c.Building.Definitions {
|
|
out["kit_"+d.ID] = d.Name + " kit"
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
func (c Catalog) validateBuilding() error {
|
|
if c.Building == nil {
|
|
return nil
|
|
}
|
|
b := c.Building
|
|
if len(b.Definitions) > 128 || b.MaxStructures < 1 || b.MaxStructures > 512 || !finiteRange(b.Range, 2, 12) || !finiteRange(b.ProtectedRadius, 12, 100) || !finiteRange(b.MaxSlope, .1, 3) {
|
|
return fmt.Errorf("invalid building limits")
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, d := range b.Definitions {
|
|
if e := d.Validate(); e != nil {
|
|
return e
|
|
}
|
|
if seen[d.ID] {
|
|
return fmt.Errorf("duplicate building identity")
|
|
}
|
|
seen[d.ID] = true
|
|
if c.Crafting == nil || !slices.ContainsFunc(c.Crafting.Professions, func(p GatheringSkill) bool { return p.ID == d.Profession }) {
|
|
return fmt.Errorf("unknown building profession")
|
|
}
|
|
for _, i := range c.Items {
|
|
if i.ID == "kit_"+d.ID {
|
|
return fmt.Errorf("equipment conflicts with structure kit")
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Existing catalogs gain the reusable structure definitions, while recipes remain
|
|
// authored separately. No publication rewrites already placed structures.
|
|
func (c *Catalog) RestoreBuilding(b Balance) {
|
|
if c.Building == nil {
|
|
c.Building = b.Building
|
|
}
|
|
}
|