Add persistent destructible world structures and building kits

This commit is contained in:
dingo
2026-09-09 22:46:06 -04:00
parent c2bef2a45f
commit e80064976a
36 changed files with 932 additions and 43 deletions
+11
View File
@@ -247,3 +247,14 @@ crafting:
# Milestone 7 provisional progression and resurrection tuning.
powers: {"weapons": [{"id": "blades", "name": "Blades", "description": "Blades proficiency", "baseXp": 100, "xpPerLevel": 50, "maxLevel": 1000}, {"id": "polearms", "name": "Polearms", "description": "Polearms proficiency", "baseXp": 100, "xpPerLevel": 50, "maxLevel": 1000}], "powers": [{"id": "arc_bolt", "name": "Arc bolt", "description": "Arc bolt", "kind": "spell", "attackId": "arc_bolt", "targeting": "target", "resurrection": false, "weapon": "", "weaponLevel": 1, "requirements": {"level": 1, "stats": {}, "titles": []}, "parents": {}, "book": true, "mana": 5, "castSeconds": 0.5, "cooldown": 2, "rankScaling": 0.03, "baseXp": 100, "xpPerLevel": 50, "maxLevel": 1000, "basePower": 10}, {"id": "mend", "name": "Mend", "description": "Mend", "kind": "spell", "attackId": "mend", "targeting": "target", "resurrection": false, "weapon": "", "weaponLevel": 1, "requirements": {"level": 1, "stats": {}, "titles": []}, "parents": {}, "book": true, "mana": 5, "castSeconds": 0, "cooldown": 2, "rankScaling": 0.03, "baseXp": 100, "xpPerLevel": 50, "maxLevel": 1000, "basePower": 10}, {"id": "renewal", "name": "Renewal aura", "description": "Renewal aura", "kind": "spell", "attackId": "renewal_aura", "targeting": "aura", "resurrection": false, "weapon": "", "weaponLevel": 1, "requirements": {"level": 1, "stats": {}, "titles": []}, "parents": {"mend": 2}, "book": false, "mana": 10, "castSeconds": 0, "cooldown": 10, "rankScaling": 0.03, "baseXp": 100, "xpPerLevel": 50, "maxLevel": 1000, "basePower": 10}, {"id": "revive", "name": "Resurrection", "description": "Resurrection", "kind": "spell", "attackId": "", "targeting": "target", "resurrection": true, "weapon": "", "weaponLevel": 1, "requirements": {"level": 1, "stats": {}, "titles": []}, "parents": {}, "book": true, "mana": 20, "castSeconds": 0, "cooldown": 30, "rankScaling": 0.03, "baseXp": 100, "xpPerLevel": 50, "maxLevel": 1000, "basePower": 0}, {"id": "cleave", "name": "Cleave", "description": "Cleave", "kind": "ability", "attackId": "cleave", "targeting": "target", "resurrection": false, "weapon": "blades", "weaponLevel": 2, "requirements": {"level": 1, "stats": {"strength": 2}, "titles": []}, "parents": {}, "book": false, "mana": 0, "castSeconds": 0, "cooldown": 2, "rankScaling": 0.03, "baseXp": 100, "xpPerLevel": 50, "maxLevel": 1, "basePower": 0}], "weaponXp": 10, "spellXp": 10, "revivalRange": 3, "revivalHealth": 0.25}
# Milestone 8a provisional structure limits and blueprints. Recipes are authored in Control.
building:
maxStructures: 256
range: 8
protectedRadius: 18
maxSlope: 1
definitions:
- {"id": "timber_wall", "name": "Timber wall", "description": "A persistent, destructible timber wall.", "model": "wall", "width": 4, "depth": 1, "height": 2, "hp": 250, "profession": "builder", "level": 1}
- {"id": "camp_platform", "name": "Camp platform", "description": "A persistent, destructible camp platform.", "model": "camp", "width": 4, "depth": 4, "height": 0.75, "hp": 300, "profession": "builder", "level": 1}
- {"id": "fort_block", "name": "Fort block", "description": "A persistent, destructible fort block.", "model": "fort", "width": 6, "depth": 3, "height": 3, "hp": 800, "profession": "builder", "level": 5}
+78
View File
@@ -0,0 +1,78 @@
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
}
}
+26
View File
@@ -0,0 +1,26 @@
package content
import "testing"
func TestBuildingKitsAndRequirements(t *testing.T) {
c, e := DefaultCatalog()
if e != nil {
t.Fatal(e)
}
c.Crafting.Recipes = append(c.Crafting.Recipes, CraftRecipe{ID: "wall_kit_recipe", Name: "Wall kit", Revision: 1, Profession: "builder", Level: 1, XP: 20, Inputs: map[string]int64{"wood": 6, "ore": 2}, Outputs: map[string]int64{"kit_timber_wall": 1}})
if e = c.Validate(); e != nil {
t.Fatal("could not author kit recipe", e)
}
d := c.Building.Definitions[0]
d.Height = -1
c.Building.Definitions[0] = d
if e = c.Validate(); e == nil {
t.Fatal("negative height allowed")
}
d.Height = 2
d.Profession = "missing"
c.Building.Definitions[0] = d
if e = c.Validate(); e == nil {
t.Fatal("missing profession allowed")
}
}
+3
View File
@@ -129,6 +129,9 @@ func (c Catalog) validateCombat() error {
attacks[a.ID] = true
}
items := map[string]bool{}
for id := range c.BuildingItems() {
items[id] = true
}
for id := range c.PowerItems() {
items[id] = true
}
+7 -3
View File
@@ -19,6 +19,7 @@ type Recipe struct {
}
type Balance struct {
Building *BuildingContent `yaml:"building"`
Powers *PowerContent `yaml:"powers"`
Crafting *CraftingContent `yaml:"crafting"`
Combat *CombatContent `yaml:"combat"`
@@ -82,10 +83,13 @@ func Parse(data []byte) (Balance, error) {
return b, b.Validate()
}
func (b Balance) Validate() error {
if err := (Catalog{Powers: b.Powers, Combat: b.Combat, Items: b.Items, Titles: b.Titles}).validatePowers(); err != nil {
if e := (Catalog{Building: b.Building, Crafting: b.Crafting, Items: b.Items}).validateBuilding(); e != nil {
return e
}
if err := (Catalog{Building: b.Building, Powers: b.Powers, Combat: b.Combat, Items: b.Items, Titles: b.Titles}).validatePowers(); err != nil {
return err
}
if err := (Catalog{Powers: b.Powers, Crafting: b.Crafting, Items: b.Items, Titles: b.Titles, Gathering: b.Gathering}).validateCrafting(); err != nil {
if err := (Catalog{Building: b.Building, Powers: b.Powers, Crafting: b.Crafting, Items: b.Items, Titles: b.Titles, Gathering: b.Gathering}).validateCrafting(); err != nil {
return err
}
if b.CombatRules != nil {
@@ -93,7 +97,7 @@ func (b Balance) Validate() error {
return err
}
}
if err := (Catalog{Powers: b.Powers, Crafting: b.Crafting, Combat: b.Combat, Items: b.Items, Gathering: b.Gathering}).validateCombat(); err != nil {
if err := (Catalog{Building: b.Building, Powers: b.Powers, Crafting: b.Crafting, Combat: b.Combat, Items: b.Items, Gathering: b.Gathering}).validateCombat(); err != nil {
return err
}
if b.Gathering != nil {
+3
View File
@@ -83,6 +83,9 @@ func SkillLevel(s GatheringSkill, xp int64) (int64, int64) {
}
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
}
+5 -1
View File
@@ -49,6 +49,7 @@ type ItemDefinition struct {
Modifiers []StatModifier `json:"modifiers" yaml:"modifiers"`
}
type Catalog struct {
Building *BuildingContent `json:"building,omitempty"`
Powers *PowerContent `json:"powers,omitempty"`
Crafting *CraftingContent `json:"crafting,omitempty"`
Combat *CombatContent `json:"combat,omitempty"`
@@ -134,6 +135,9 @@ func (item ItemDefinition) Validate() error {
return ValidateModifiers(item.Modifiers)
}
func (c Catalog) Validate() error {
if e := c.validateBuilding(); e != nil {
return e
}
if err := c.validatePowers(); err != nil {
return err
}
@@ -215,5 +219,5 @@ func DefaultCatalog() (Catalog, error) {
if err != nil {
return Catalog{}, err
}
return Catalog{Powers: b.Powers, Crafting: b.Crafting, Combat: b.Combat, Gathering: b.Gathering, Version: 1, Titles: b.Titles, Items: b.Items, Stats: *b.Stats}, nil
return Catalog{Building: b.Building, Powers: b.Powers, Crafting: b.Crafting, Combat: b.Combat, Gathering: b.Gathering, Version: 1, Titles: b.Titles, Items: b.Items, Stats: *b.Stats}, nil
}
+5
View File
@@ -31,6 +31,11 @@ func (w *World) AdminItems(c *Character, op, value string) error {
}
if op == "grant-item" {
known := false
for key := range (content.Catalog{Building: w.Balance.Building}).BuildingItems() {
if key == id {
known = true
}
}
for key := range (content.Catalog{Powers: w.Balance.Powers}).PowerItems() {
if key == id {
known = true
+259
View File
@@ -0,0 +1,259 @@
package game
import (
"aeternalis/server/content"
p "aeternalis/server/internal/protocol"
"errors"
"math"
"sort"
)
type Structure struct {
Entity p.Entity `json:"entity"`
Definition content.BuildingDefinition `json:"definition"`
Builder string `json:"builder"`
BuiltAt int64 `json:"builtAt"`
Provenance []p.CraftedStack `json:"provenance,omitempty"`
}
func (w *World) restoreBuilding() error {
if w.Balance.Building == nil {
b, e := content.Load()
if e != nil {
return e
}
w.Balance.Building = b.Building
}
if w.Structures == nil {
w.Structures = map[string]*Structure{}
}
if len(w.Structures) > 512 {
return errors.New("too many saved structures")
}
for id, s := range w.Structures {
if s == nil || id != s.Entity.ID || s.Entity.Kind != "structure" || s.Definition.Validate() != nil || !finiteCraft(s.Entity.HP) || s.Entity.HP <= 0 || s.Entity.HP > s.Definition.HP || s.Entity.MaxHP != s.Definition.HP || s.Entity.Width == nil || s.Entity.Depth == nil || s.Entity.Height == nil {
return errors.New("invalid saved structure")
}
pos := s.Entity.Position
if math.IsNaN(pos.X) || math.IsNaN(pos.Y) || math.IsNaN(pos.Z) || math.IsInf(pos.X, 0) || math.IsInf(pos.Y, 0) || math.IsInf(pos.Z, 0) || math.Hypot(pos.X, pos.Z) > w.Balance.WorldRadius || *s.Entity.Height != s.Definition.Height || !((*s.Entity.Width == s.Definition.Width && *s.Entity.Depth == s.Definition.Depth) || (*s.Entity.Width == s.Definition.Depth && *s.Entity.Depth == s.Definition.Width)) {
return errors.New("invalid saved structure bounds")
}
}
return nil
}
func (w *World) Build(c *Character, id string, dx, dz float64, rotation string) error {
if c == nil || !c.Online || c.Sheltered || c.View.Entity.State != "alive" || c.View.RemainingSeconds <= 0 || !w.AllowsXP() || c.PendingCast != nil {
return errors.New("cannot build here")
}
if w.Balance.Building == nil {
return errors.New("building unavailable")
}
b := w.Balance.Building
if math.IsNaN(dx) || math.IsNaN(dz) || math.IsInf(dx, 0) || math.IsInf(dz, 0) || math.Hypot(dx, dz) > 1 || (rotation != "0" && rotation != "1") {
return errors.New("invalid placement")
}
var d *content.BuildingDefinition
for i := range b.Definitions {
if b.Definitions[i].ID == id {
d = &b.Definitions[i]
break
}
}
if d == nil || c.View.Inventory["kit_"+id] < 1 {
return errors.New("structure kit required")
}
if c.View.Crafting[d.Profession].Level < d.Level {
return errors.New("building profession level too low")
}
if len(w.Structures) >= b.MaxStructures {
return errors.New("world structure limit reached")
}
pos := w.terrain.Position(c.View.Entity.Position.X+dx*b.Range, c.View.Entity.Position.Z+dz*b.Range)
width, depth, height := d.Width, d.Depth, d.Height
if rotation == "1" {
width, depth = depth, width
}
radius := math.Hypot(width, depth) / 2
if math.Hypot(pos.X, pos.Z)-radius < b.ProtectedRadius || math.Hypot(pos.X, pos.Z)+radius >= w.SafeRadius() {
return errors.New("keep structures away from the arrival clearing, portal, and world boundary")
}
if _, blocked := w.combatLine(body(c.View.Entity.Position), body(pos), ""); blocked {
return errors.New("placement is obstructed")
}
low, high := pos.Y, pos.Y
for _, x := range []float64{pos.X - width/2, pos.X + width/2} {
for _, z := range []float64{pos.Z - depth/2, pos.Z + depth/2} {
h := w.terrain.Height(x, z)
low = math.Min(low, h)
high = math.Max(high, h)
}
}
if high-low > math.Min(b.MaxSlope, height/2) {
return errors.New("choose flatter ground")
}
pos.Y = low
entity := p.Entity{ID: "structure:" + ID(), Kind: "structure", Name: d.Name, Position: pos, HP: d.HP, MaxHP: d.HP, State: d.Model, Width: &width, Depth: &depth, Height: &height}
for _, s := range w.Structures {
if boxesOverlap(entity, s.Entity, .2) {
return errors.New("structures overlap")
}
}
occupied := func(e p.Entity) bool {
return math.Abs(e.Position.X-pos.X) < width/2+1 && math.Abs(e.Position.Z-pos.Z) < depth/2+1
}
for _, other := range w.Characters {
if other.ReturnPosition != nil && occupied(p.Entity{Position: *other.ReturnPosition}) {
return errors.New("keep keystone return locations accessible")
}
if other.View.Entity.State != "dead" && occupied(other.View.Entity) {
return errors.New("a player occupies that space")
}
}
for _, m := range w.Mobs {
if m.Entity.HP > 0 && occupied(m.Entity) {
return errors.New("a mob occupies that space")
}
}
for _, corpse := range w.Corpses {
if occupied(corpse.Entity) {
return errors.New("keep corpses accessible")
}
}
for _, ch := range w.neighbors(pos) {
for _, resource := range ch.Resources {
if occupied(resource) {
return errors.New("keep resources accessible")
}
}
}
provenance := TakeStack(c, "kit_"+id, 1)
if w.Structures == nil {
w.Structures = map[string]*Structure{}
}
w.Structures[entity.ID] = &Structure{Entity: entity, Definition: *d, Builder: c.View.Entity.ID, BuiltAt: w.now.Unix(), Provenance: provenance}
c.Message = "Built " + d.Name
w.dirty = true
return nil
}
func boxesOverlap(a, b p.Entity, padding float64) bool {
return math.Abs(a.Position.X-b.Position.X) < (*a.Width+*b.Width)/2+padding && math.Abs(a.Position.Z-b.Position.Z) < (*a.Depth+*b.Depth)/2+padding
}
// Swept segment/AABB intersection also catches thin walls between simulation ticks.
func boxFraction(from, to p.Vec3, e p.Entity, padding, below float64) (float64, bool) {
lo, hi := 0.0, 1.0
starts := []float64{from.X, from.Y, from.Z}
ends := []float64{to.X, to.Y, to.Z}
mins := []float64{e.Position.X - *e.Width/2 - padding, e.Position.Y - below, e.Position.Z - *e.Depth/2 - padding}
maxs := []float64{e.Position.X + *e.Width/2 + padding, e.Position.Y + *e.Height - .001, e.Position.Z + *e.Depth/2 + padding}
for i, a := range starts {
d := ends[i] - a
if math.Abs(d) < 1e-9 {
if a < mins[i] || a > maxs[i] {
return 0, false
}
continue
}
u, v := (mins[i]-a)/d, (maxs[i]-a)/d
if u > v {
u, v = v, u
}
lo = math.Max(lo, u)
hi = math.Min(hi, v)
if lo > hi {
return 0, false
}
}
return lo, true
}
func (w *World) structureIDs() []string {
ids := make([]string, 0, len(w.Structures))
for id := range w.Structures {
ids = append(ids, id)
}
sort.Strings(ids)
return ids
}
func (w *World) combatLine(from, to p.Vec3, ignore string) (p.Vec3, bool) {
point, blocked := w.terrainHit(from, to)
best := math.Inf(1)
if blocked {
best = length3(from, point)
}
for _, id := range w.structureIDs() {
if id == ignore {
continue
}
s := w.Structures[id]
if f, ok := boxFraction(from, to, s.Entity, 0, 0); ok && f*length3(from, to) < best {
point = lerp3(from, to, f)
best = length3(from, point)
blocked = true
}
}
return point, blocked
}
func (w *World) structureBlocked(from, to p.Vec3) bool {
for _, s := range w.Structures {
if _, ok := boxFraction(from, to, s.Entity, .35, 1.7); ok {
return true
}
}
return false
}
func (w *World) floorAt(x, z, feet float64) float64 {
h := w.terrain.Height(x, z)
for _, s := range w.Structures {
e := s.Entity
top := e.Position.Y + *e.Height
if top <= feet+.05 && math.Abs(x-e.Position.X) <= *e.Width/2+.3 && math.Abs(z-e.Position.Z) <= *e.Depth/2+.3 {
h = math.Max(h, top)
}
}
return h
}
func (w *World) damageStructure(id string, power float64) {
s := w.Structures[id]
if s == nil || power <= 0 {
return
}
s.Entity.HP = math.Max(0, s.Entity.HP-power)
if s.Entity.HP == 0 {
delete(w.Structures, id)
}
w.dirty = true
}
// Aggressive/guard mobs can destroy nearby structures, including ones concealing
// their target. Passive and healing mobs do not attack buildings.
func (w *World) attackNearbyStructure(m *Mob, a p.CombatAttack, power, aggro, leash float64) bool {
if a.Effect != "damage" {
return false
}
for _, id := range w.structureIDs() {
e := w.Structures[id].Entity
near := a.Range + math.Max(*e.Width, *e.Depth)/2
if distance(m.Entity.Position, e.Position) > math.Min(aggro, near) || distance(e.Position, *m.Home) > leash {
continue
}
if _, blocked := w.combatLine(body(m.Entity.Position), body(e.Position), id); blocked {
continue
}
if w.Age()-m.LastAttack >= a.Cooldown {
if a.Delivery == "projectile" {
if len(w.Projectiles) >= w.Balance.CombatRules.MaxProjectiles || (a.Duration > 0 && len(w.Areas)+w.pendingAreas() >= w.Balance.CombatRules.MaxAreas) {
return true
}
shot := &Projectile{ID: w.combatID("projectile"), Source: m.Entity.ID, Player: false, Position: body(m.Entity.Position), Destination: body(e.Position), Target: id, Attack: a, Power: power * a.Multiplier}
w.Projectiles[shot.ID] = shot
w.projectileVisual(*shot, "launch", "")
} else {
w.damageStructure(id, power*a.Multiplier)
}
m.LastAttack = w.Age()
w.dirty = true
}
return true
}
return false
}
+127
View File
@@ -0,0 +1,127 @@
package game
import (
p "aeternalis/server/internal/protocol"
"encoding/json"
"math"
"testing"
"time"
)
func builtFixture(t *testing.T) (*World, *Character, *Structure) {
t.Helper()
w, c := fixture(t)
c.View.Inventory["kit_timber_wall"] = 2
for x := 30.; x < 100; x += 5 {
c.View.Entity.Position = w.terrain.Position(x, 0)
if e := w.Build(c, "timber_wall", 0, .75, "0"); e == nil {
for _, s := range w.Structures {
return w, c, s
}
}
}
t.Fatal("could not place fixture structure")
return nil, nil, nil
}
func TestStructurePlacementOwnershipAndPersistence(t *testing.T) {
w, c, s := builtFixture(t)
if c.View.Inventory["kit_timber_wall"] != 1 {
t.Fatal("kit not consumed once")
}
if e := w.Build(c, "timber_wall", 0, .75, "0"); e == nil || c.View.Inventory["kit_timber_wall"] != 1 {
t.Fatal("overlap spent kit")
}
if e := w.Build(c, "timber_wall", math.NaN(), 0, "0"); e == nil {
t.Fatal("nonfinite placement accepted")
}
w.Kind = "hub"
if e := w.Build(c, "timber_wall", .75, 0, "0"); e == nil {
t.Fatal("hub construction accepted")
}
w.Kind = "survival"
w.damageStructure(s.Entity.ID, 20)
hp := s.Entity.HP
w.kill(c, "building persistence check")
raw, _ := json.Marshal(w)
var restored World
if e := json.Unmarshal(raw, &restored); e != nil {
t.Fatal(e)
}
if e := restored.Restore(w.now.Add(time.Second)); e != nil {
t.Fatal(e)
}
if restored.Structures[s.Entity.ID] == nil || restored.Structures[s.Entity.ID].Entity.HP != hp {
t.Fatal("death/restart lost or healed structure")
}
w.damageStructure(s.Entity.ID, 1000000)
if len(w.Structures) != 0 {
t.Fatal("destroyed structure retained collision")
}
}
func TestStructureCollisionProjectileAndMobDamage(t *testing.T) {
w, c, s := builtFixture(t)
e := s.Entity
c.View.Entity.Position = w.terrain.Position(e.Position.X, e.Position.Z-2)
c.MoveZ = 1
for i := 0; i < 20; i++ {
w.moveCharacter(c, .05)
}
if c.View.Entity.Position.Z >= e.Position.Z-*e.Depth/2 {
t.Fatal("walked through wall")
}
from := body(w.terrain.Position(e.Position.X, e.Position.Z-4))
to := body(w.terrain.Position(e.Position.X, e.Position.Z+4))
if _, blocked := w.combatLine(from, to, ""); !blocked {
t.Fatal("wall did not block line of sight")
}
if _, blocked := w.combatLine(from, body(e.Position), e.ID); blocked {
t.Fatal("wall prevented its own targeting")
}
w.now = w.now.Add(time.Minute)
a := protocolAttack(w.Balance.Combat.Attacks[0])
a.Effect = "damage"
a.Delivery = "projectile"
a.Speed = 100
a.Radius = 0
a.Duration = 0
w.Projectiles["crossing"] = &Projectile{ID: "crossing", Source: c.View.Entity.ID, Player: true, Position: from, Destination: to, Attack: a, Power: 10}
hp := s.Entity.HP
w.updateCombat(.1)
if len(w.Projectiles) != 0 || s.Entity.HP != hp-10 {
t.Fatal("fast projectile tunneled through structure", s.Entity.HP)
}
home := w.terrain.Position(e.Position.X, e.Position.Z-2)
m := &Mob{Entity: p.Entity{ID: "attacker", Position: home, HP: 10}, Home: &home}
a.Effect = "damage"
a.Delivery = "melee"
a.Range = 3
a.Multiplier = 1
a.Cooldown = 1
hp = s.Entity.HP
if !w.attackNearbyStructure(m, a, 12, 10, 100) || s.Entity.HP != hp-12 {
t.Fatal("mob did not damage structure")
}
hp = s.Entity.HP
w.attackNearbyStructure(m, a, 12, 10, 100)
if s.Entity.HP != hp {
t.Fatal("mob ignored cooldown")
}
// Roof support, jumping from a roof, and leaving the footprint use real height.
c.View.Entity.Position = p.Vec3{X: e.Position.X, Y: e.Position.Y + *e.Height, Z: e.Position.Z}
c.MoveZ = 0
c.JumpVelocity = 0
if err := w.jump(c); err != nil {
t.Fatal("could not jump from structure", err)
}
w.moveCharacter(c, .05)
if c.View.Entity.Position.Y <= e.Position.Y+*e.Height {
t.Fatal("jump did not rise above structure")
}
c.JumpVelocity = 0
c.View.Entity.Position = p.Vec3{X: e.Position.X, Y: e.Position.Y + *e.Height, Z: e.Position.Z + *e.Depth/2 + .29}
c.MoveZ = 1
w.moveCharacter(c, .05)
if c.View.Entity.Position.Y <= w.terrain.Height(c.View.Entity.Position.X, c.View.Entity.Position.Z) {
t.Fatal("walking off roof teleported to ground")
}
}
+26 -2
View File
@@ -90,6 +90,11 @@ func (w *World) combatTargets(player bool, effect string) []p.Entity {
out = append(out, p.Entity{ID: "boss", Position: w.terrain.Position(0, 0), HP: w.BossHP})
}
}
if effect == "damage" {
for _, id := range w.structureIDs() {
out = append(out, w.Structures[id].Entity)
}
}
return out
}
func (w *World) terrainHit(a, b p.Vec3) (p.Vec3, bool) {
@@ -129,13 +134,16 @@ func (w *World) attack(c *Character, target string) error {
}
from, to := body(c.View.Entity.Position), body(e.Position)
extra := float64(0)
if e.Kind == "structure" {
extra = math.Max(*e.Width, *e.Depth) / 2
}
if target == "boss" {
extra = 4
}
if length3(from, to) > a.Range+extra {
return errors.New("target out of reach")
}
if _, blocked := w.terrainHit(from, to); blocked {
if _, blocked := w.combatLine(from, to, target); blocked {
return errors.New("terrain blocks the attack")
}
if a.Delivery == "projectile" && len(w.Projectiles) >= w.Balance.CombatRules.MaxProjectiles {
@@ -206,7 +214,7 @@ func (w *World) areaPulse(shot Projectile) {
if length3(body(e.Position), shot.Position) > shot.Attack.Radius {
continue
}
if _, blocked := w.terrainHit(shot.Position, body(e.Position)); blocked {
if _, blocked := w.combatLine(shot.Position, body(e.Position), e.ID); blocked {
continue
}
w.combatHit(shot, e.ID)
@@ -218,6 +226,12 @@ func (w *World) combatHit(shot Projectile, target string) {
return
}
healing := shot.Attack.Effect == "heal"
if w.Structures[target] != nil {
if !healing {
w.damageStructure(target, shot.Power)
}
return
}
if c := w.Characters[target]; c != nil {
if c.View.Entity.State != "alive" || !c.Online || c.Sheltered {
return
@@ -301,7 +315,17 @@ func (w *World) updateCombat(dt float64) {
}
hit := ""
hitPoint := obstacle
for _, id := range w.structureIDs() {
if f, ok := boxFraction(shot.Position, end, w.Structures[id].Entity, 0, 0); ok && f*length3(shot.Position, end) < best {
best = f * length3(shot.Position, end)
hit = id
hitPoint = lerp3(shot.Position, end, f)
}
}
for _, e := range w.combatTargets(shot.Player, shot.Attack.Effect) {
if e.Kind == "structure" {
continue
}
if e.ID == shot.Source && shot.Target != shot.Source {
continue
}
+5
View File
@@ -13,6 +13,11 @@ import (
)
func (w *World) refreshCrafting(c *Character) {
for id, name := range (content.Catalog{Building: w.Balance.Building}).BuildingItems() {
if c.View.Inventory[id] > 0 {
c.View.StackNames[id] = name
}
}
if c.View.Crafting == nil {
c.View.Crafting = map[string]p.CraftProgress{}
}
+3
View File
@@ -325,6 +325,9 @@ func (w *World) lootCorpse(c *Character, target string) error {
if corpse == nil || distance(c.View.Entity.Position, corpse.Entity.Position) > w.Balance.GatherRange {
return errors.New("corpse out of reach")
}
if _, blocked := w.combatLine(body(c.View.Entity.Position), body(corpse.Entity.Position), ""); blocked {
return errors.New("corpse obstructed")
}
seen := map[string]bool{}
if err := validateOwnedItems(c.View.Items, seen); err != nil {
return err
+19 -11
View File
@@ -176,6 +176,9 @@ func (w *World) gather(c *Character, target string) error {
return errors.New("resource out of reach")
}
id := stringValue(e.ResourceID)
if _, blocked := w.combatLine(body(c.View.Entity.Position), body(e.Position), ""); blocked {
return errors.New("resource obstructed")
}
if id == "" {
id = e.Kind
}
@@ -271,7 +274,7 @@ func (w *World) consumeGathering(c *Character, id string) error {
return errors.New("unknown gathering consumable")
}
func (w *World) jump(c *Character) error {
ground := w.terrain.Height(c.View.Entity.Position.X, c.View.Entity.Position.Z)
ground := w.floorAt(c.View.Entity.Position.X, c.View.Entity.Position.Z, c.View.Entity.Position.Y)
if c.JumpVelocity != 0 || c.View.Entity.Position.Y > ground+0.05 {
return errors.New("already airborne")
}
@@ -286,16 +289,21 @@ func (w *World) moveCharacter(c *Character, dt float64) {
if math.Hypot(x, z) >= w.Balance.WorldRadius {
x, z = pos.X, pos.Z
}
ground := w.terrain.Height(x, z)
if c.JumpVelocity != 0 || pos.Y > w.terrain.Height(pos.X, pos.Z)+0.05 {
y := pos.Y + c.JumpVelocity*dt - w.Balance.GatheringRules.Gravity*dt*dt/2
y := pos.Y
airborne := c.JumpVelocity != 0 || pos.Y > w.floorAt(pos.X, pos.Z, pos.Y)+.05 || (len(w.Structures) > 0 && pos.Y > w.floorAt(x, z, pos.Y)+.05)
if airborne {
y += c.JumpVelocity*dt - w.Balance.GatheringRules.Gravity*dt*dt/2
c.JumpVelocity -= w.Balance.GatheringRules.Gravity * dt
if y <= ground {
y = ground
c.JumpVelocity = 0
}
c.View.Entity.Position = p.Vec3{X: x, Y: y, Z: z}
} else {
c.View.Entity.Position = w.terrain.Position(x, z)
}
ground := w.floorAt(x, z, math.Max(pos.Y, y))
next := p.Vec3{X: x, Y: math.Max(pos.Y, math.Max(y, ground)), Z: z}
if w.structureBlocked(pos, next) {
x, z = pos.X, pos.Z
ground = w.floorAt(x, z, math.Max(pos.Y, y))
}
if y <= ground || !airborne {
y = ground
c.JumpVelocity = 0
}
c.View.Entity.Position = p.Vec3{X: x, Y: y, Z: z}
}
+15 -3
View File
@@ -83,6 +83,9 @@ func (w *World) SpawnMobs(definition, value string) error {
return errors.New("spawn would leave the surviving world")
}
positions = append(positions, w.terrain.Position(px, pz))
if w.structureBlocked(positions[len(positions)-1], positions[len(positions)-1]) {
return errors.New("spawn intersects a structure")
}
}
for _, pos := range positions {
id := w.combatID("mob")
@@ -102,6 +105,9 @@ func (w *World) ensureMob(ch p.Chunk) {
}
hp := w.Balance.MobHp * (1 + w.Age()/86400*w.Balance.MobScalePerDay)
home := w.terrain.Position(x, z)
if w.structureBlocked(home, home) {
return
}
w.Mobs[id] = &Mob{Entity: p.Entity{ID: id, Kind: "sentinel", Name: "Ash sentinel", Position: home, HP: hp, MaxHP: hp, State: "alive"}, HomeX: int(ch.X), HomeZ: int(ch.Z), Home: &home, RespawnDelay: w.Balance.MobRespawnSeconds}
w.dirty = true
}
@@ -131,6 +137,9 @@ func (w *World) updateSpawns(active map[string]bool) {
continue
}
pos := w.terrain.Position(x, z)
if w.structureBlocked(pos, pos) {
continue
}
near := false
for _, c := range w.Characters {
if c.Online && c.View.Entity.State == "alive" && distance(pos, c.View.Entity.Position) < rule.MinPlayerDistance {
@@ -181,7 +190,7 @@ func (w *World) mobStep(m *Mob, to p.Vec3, speed, dt float64) {
continue
}
dest := w.terrain.Position(x, z)
if math.Abs(dest.Y-from.Y) > w.Balance.CombatRules.MaxSlope*step {
if w.structureBlocked(from, dest) || math.Abs(dest.Y-from.Y) > w.Balance.CombatRules.MaxSlope*step {
continue
}
m.Entity.Position = dest
@@ -256,6 +265,9 @@ func (w *World) updateMobs(active map[string]bool, dt float64) {
}
continue
}
if behavior != "passive" && w.attackNearbyStructure(m, attack, power, aggro, leash) {
continue
}
var target *Character
if c := w.Characters[m.Target]; c != nil && c.Online && !c.Sheltered && c.View.Entity.State == "alive" && distance(c.View.Entity.Position, *m.Home) <= leash {
target = c
@@ -270,7 +282,7 @@ func (w *World) updateMobs(active map[string]bool, dt float64) {
}
d := distance(c.View.Entity.Position, m.Entity.Position)
if d < nearest && distance(c.View.Entity.Position, *m.Home) <= leash {
if _, blocked := w.terrainHit(body(m.Entity.Position), body(c.View.Entity.Position)); blocked {
if _, blocked := w.combatLine(body(m.Entity.Position), body(c.View.Entity.Position), ""); blocked {
continue
}
target = c
@@ -301,7 +313,7 @@ func (w *World) updateMobs(active map[string]bool, dt float64) {
if w.Age()-m.LastAttack < attack.Cooldown {
continue
}
if _, blocked := w.terrainHit(body(m.Entity.Position), body(aim)); blocked {
if _, blocked := w.combatLine(body(m.Entity.Position), body(aim), targetID); blocked {
w.mobStep(m, aim, speed, dt)
continue
}
+4 -1
View File
@@ -278,6 +278,9 @@ func (w *World) checkPower(c *Character, v p.LearnedPower, target string) error
for _, e := range w.combatTargets(true, v.Attack.Effect) {
if e.ID == target {
if length3(body(c.View.Entity.Position), body(e.Position)) > v.Attack.Range+func() float64 {
if e.Kind == "structure" {
return math.Max(*e.Width, *e.Depth) / 2
}
if target == "boss" {
return 4
}
@@ -285,7 +288,7 @@ func (w *World) checkPower(c *Character, v p.LearnedPower, target string) error
}() {
return errors.New("power target out of range")
}
if _, blocked := w.terrainHit(body(c.View.Entity.Position), body(e.Position)); blocked {
if _, blocked := w.combatLine(body(c.View.Entity.Position), body(e.Position), e.ID); blocked {
return errors.New("terrain blocks power")
}
found = true
+1 -1
View File
@@ -19,7 +19,7 @@ func (w *World) Revive(caster *Character, corpseID, power string) (*Character, e
if length3(caster.View.Entity.Position, corpse.Entity.Position) > w.Balance.Powers.RevivalRange {
return nil, errors.New("stand next to the corpse")
}
if _, blocked := w.terrainHit(body(caster.View.Entity.Position), body(corpse.Entity.Position)); blocked {
if _, blocked := w.combatLine(body(caster.View.Entity.Position), body(corpse.Entity.Position), ""); blocked {
return nil, errors.New("terrain blocks the corpse")
}
if caster.PendingCast != nil {
+3
View File
@@ -173,6 +173,9 @@ func (w *World) ApplyCatalog(catalog content.Catalog) error {
if catalog.Combat != nil {
w.Balance.Combat = catalog.Combat
}
if catalog.Building != nil {
w.Balance.Building = catalog.Building
}
if catalog.Powers != nil {
w.Balance.Powers = catalog.Powers
}
+12 -1
View File
@@ -76,6 +76,7 @@ type ResourceLayout struct {
Resources []p.Entity `json:"resources"`
}
type World struct {
Structures map[string]*Structure `json:"structures,omitempty"`
Kind string `json:"kind,omitempty"`
ProjectileVisuals []p.ProjectileVisual `json:"projectileVisuals,omitempty"`
Projectiles map[string]*Projectile `json:"projectiles,omitempty"`
@@ -189,6 +190,9 @@ func (w *World) Restore(now time.Time) error {
}
w.dirty = true
}
if e := w.restoreBuilding(); e != nil {
return e
}
if w.ResourceLayouts == nil {
w.ResourceLayouts = map[string]ResourceLayout{}
}
@@ -321,7 +325,7 @@ func (w *World) spawn(exclude string) (p.Vec3, error) {
if radius > math.Max(0, math.Min(w.Balance.WorldRadius-32, w.SafeRadius()-8)) {
break
}
clear := true
clear := !w.structureBlocked(pos, pos)
for id, c := range w.Characters {
if id != exclude && c.View.Entity.State != "dead" && distance(pos, c.View.Entity.Position) < w.Balance.SpawnSeparation {
clear = false
@@ -546,6 +550,8 @@ func (w *World) Intent(id string, i p.Intent) error {
return w.consumeGathering(c, i.Target)
case "gather":
return w.gather(c, i.Target)
case "build":
return w.Build(c, i.Target, i.X, i.Z, i.Slot)
case "learn":
return w.LearnPower(c, i.Target)
case "cast":
@@ -895,6 +901,11 @@ func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
s.Entities = append(s.Entities, m.Entity)
}
}
for _, structure := range w.Structures {
if visible(structure.Entity.Position) {
s.Entities = append(s.Entities, structure.Entity)
}
}
for _, corpse := range w.Corpses {
if visible(corpse.Entity.Position) {
s.Entities = append(s.Entities, corpse.Entity)
+39
View File
@@ -13,6 +13,7 @@ type Protocol struct {
}
type ContentCatalog struct {
Building *BuildingContent `json:"building,omitempty"`
Combat *CombatContent `json:"combat,omitempty"`
Crafting *CraftingContent `json:"crafting,omitempty"`
EffectSets []EffectSet `json:"effectSets,omitempty"`
@@ -25,6 +26,27 @@ type ContentCatalog struct {
Version int64 `json:"version"`
}
type BuildingContent struct {
Definitions []BuildingDefinition `json:"definitions"`
MaxSlope float64 `json:"maxSlope"`
MaxStructures int64 `json:"maxStructures"`
ProtectedRadius float64 `json:"protectedRadius"`
Range float64 `json:"range"`
}
type BuildingDefinition struct {
Depth float64 `json:"depth"`
Description string `json:"description"`
HP float64 `json:"hp"`
Height float64 `json:"height"`
ID string `json:"id"`
Level int64 `json:"level"`
Model string `json:"model"`
Name string `json:"name"`
Profession string `json:"profession"`
Width float64 `json:"width"`
}
type CombatContent struct {
Attacks []CombatAttack `json:"attacks"`
Loot []LootTable `json:"loot"`
@@ -302,6 +324,7 @@ type TitleEffects struct {
type ContentChange struct {
Attack *CombatAttackDraft `json:"attack,omitempty"`
Biome *BiomeDefinitionDraft `json:"biome,omitempty"`
Building *BuildingDraft `json:"building,omitempty"`
Consumable *GatheringConsumableDraft `json:"consumable,omitempty"`
CraftMaterial *CraftMaterialDraft `json:"craftMaterial,omitempty"`
CraftProfession *GatheringSkillDraft `json:"craftProfession,omitempty"`
@@ -357,6 +380,19 @@ type BiomeDefinitionDraft struct {
Weight int64 `json:"weight"`
}
type BuildingDraft struct {
Depth float64 `json:"depth"`
Description string `json:"description"`
HP float64 `json:"hp"`
Height float64 `json:"height"`
ID *string `json:"id,omitempty"`
Level int64 `json:"level"`
Model string `json:"model"`
Name string `json:"name"`
Profession string `json:"profession"`
Width float64 `json:"width"`
}
type GatheringConsumableDraft struct {
Description string `json:"description"`
ID *string `json:"id,omitempty"`
@@ -581,7 +617,9 @@ type Chunk struct {
}
type Entity struct {
Depth *float64 `json:"depth,omitempty"`
HP float64 `json:"hp"`
Height *float64 `json:"height,omitempty"`
ID string `json:"id"`
Kind string `json:"kind"`
MaxHP float64 `json:"maxHp"`
@@ -593,6 +631,7 @@ type Entity struct {
ResourceID *string `json:"resourceId,omitempty"`
SkillID *string `json:"skillId,omitempty"`
State string `json:"state"`
Width *float64 `json:"width,omitempty"`
}
type Vec3 struct {
+12 -2
View File
@@ -17,6 +17,7 @@ var ErrCatalogInvalid = errors.New("invalid catalog change")
var catalogRequestID = regexp.MustCompile(`^[a-f0-9-]{32,36}$`)
type CatalogChange struct {
Building *content.BuildingDefinition `json:"building,omitempty"`
Power *content.PowerDefinition `json:"power,omitempty"`
Weapon *content.GatheringSkill `json:"weapon,omitempty"`
CraftRecipe *content.CraftRecipe `json:"craftRecipe,omitempty"`
@@ -53,7 +54,7 @@ type CatalogResult struct {
// Explicit definition IDs are reserved for trusted seed/import code. Browser
// publications omit them; the server assigns an ID after resolving request replay.
func (c CatalogChange) HasDefinitionID() bool {
return (c.Power != nil && c.Power.ID != "") || (c.Weapon != nil && c.Weapon.ID != "") || (c.CraftRecipe != nil && c.CraftRecipe.ID != "") || (c.CraftProfession != nil && c.CraftProfession.ID != "") || (c.CraftMaterial != nil && c.CraftMaterial.ID != "") || (c.Attack != nil && c.Attack.ID != "") || (c.Mob != nil && c.Mob.ID != "") || (c.Loot != nil && c.Loot.ID != "") || (c.Spawn != nil && c.Spawn.ID != "") || (c.GatheringSkill != nil && c.GatheringSkill.ID != "") || (c.Resource != nil && c.Resource.ID != "") || (c.Biome != nil && c.Biome.ID != "") || (c.Consumable != nil && c.Consumable.ID != "") || (c.Title != nil && c.Title.ID != "") || (c.Item != nil && c.Item.ID != "") || (c.RequirementSet != nil && c.RequirementSet.ID != "") || (c.EffectSet != nil && c.EffectSet.ID != "")
return (c.Building != nil && c.Building.ID != "") || (c.Power != nil && c.Power.ID != "") || (c.Weapon != nil && c.Weapon.ID != "") || (c.CraftRecipe != nil && c.CraftRecipe.ID != "") || (c.CraftProfession != nil && c.CraftProfession.ID != "") || (c.CraftMaterial != nil && c.CraftMaterial.ID != "") || (c.Attack != nil && c.Attack.ID != "") || (c.Mob != nil && c.Mob.ID != "") || (c.Loot != nil && c.Loot.ID != "") || (c.Spawn != nil && c.Spawn.ID != "") || (c.GatheringSkill != nil && c.GatheringSkill.ID != "") || (c.Resource != nil && c.Resource.ID != "") || (c.Biome != nil && c.Biome.ID != "") || (c.Consumable != nil && c.Consumable.ID != "") || (c.Title != nil && c.Title.ID != "") || (c.Item != nil && c.Item.ID != "") || (c.RequirementSet != nil && c.RequirementSet.ID != "") || (c.EffectSet != nil && c.EffectSet.ID != "")
}
type CatalogAudit struct {
@@ -138,6 +139,13 @@ func decodeCatalog(version int64, raw string) (content.Catalog, error) {
}
catalog.RestorePowers(b)
}
if catalog.Building == nil {
b, e := content.Load()
if e != nil {
return catalog, e
}
catalog.RestoreBuilding(b)
}
return catalog, catalog.Validate()
}
func (d *DB) Catalog(ctx context.Context) (content.Catalog, error) {
@@ -173,12 +181,14 @@ func (c CatalogChange) validate() error {
}
valid := false
count := 0
for _, present := range []bool{c.Power != nil, c.Weapon != nil, c.CraftRecipe != nil, c.CraftProfession != nil, c.CraftMaterial != nil, c.CraftRules != nil, c.Attack != nil, c.Mob != nil, c.Loot != nil, c.Spawn != nil, c.GatheringSkill != nil, c.Resource != nil, c.Biome != nil, c.Consumable != nil, c.Title != nil, c.Item != nil, c.Stats != nil, c.RequirementSet != nil, c.EffectSet != nil} {
for _, present := range []bool{c.Building != nil, c.Power != nil, c.Weapon != nil, c.CraftRecipe != nil, c.CraftProfession != nil, c.CraftMaterial != nil, c.CraftRules != nil, c.Attack != nil, c.Mob != nil, c.Loot != nil, c.Spawn != nil, c.GatheringSkill != nil, c.Resource != nil, c.Biome != nil, c.Consumable != nil, c.Title != nil, c.Item != nil, c.Stats != nil, c.RequirementSet != nil, c.EffectSet != nil} {
if present {
count++
}
}
switch c.Op {
case "create-building":
valid = c.Building != nil
case "create-power":
valid = c.Power != nil
case "create-weapon":
+2 -1
View File
@@ -26,6 +26,7 @@ func TestServerAssignedCatalogIDsSurviveReplayAndRestart(t *testing.T) {
var last CatalogChange
var lastResult CatalogResult
for _, draft := range []CatalogChange{
{Op: "create-building", Building: &content.BuildingDefinition{Name: "Same name", Model: "wall", Width: 4, Depth: 1, Height: 2, HP: 250, Profession: "builder", Level: 1}},
{Op: "create-requirement-set", RequirementSet: &content.RequirementSet{Name: "Same name", Requirements: []content.TitleRequirement{{Metric: "level", AtLeast: 1}}}},
{Op: "create-effect-set", EffectSet: &content.EffectSet{Name: "Same name", Modifiers: []content.StatModifier{{Stat: "defence", Flat: 2}}}},
{Op: "create-title", Title: &content.Title{Name: "Same name", Scope: "personal", Requirements: []content.TitleRequirement{{Metric: "level", AtLeast: 2}}}},
@@ -86,7 +87,7 @@ func TestServerAssignedCatalogIDsSurviveReplayAndRestart(t *testing.T) {
t.Fatal("restart changed replay identity", err)
}
audits, err := reopened.CatalogChanges(ctx)
if err != nil || len(audits) != 8 || audits[0].Target != lastResult.ID {
if err != nil || len(audits) != len(seen) || audits[0].Target != lastResult.ID {
t.Fatal("generated ID not persisted in audit", err)
}
}
+20
View File
@@ -35,6 +35,9 @@ func (d *DB) PreviewCatalog(ctx context.Context, change CatalogChange) (CatalogP
}
// Preview validates a provisional identity but does not reserve or expose it.
if !change.HasDefinitionID() {
if resolved.Building != nil {
resolved.Building.ID = ""
}
if resolved.Power != nil {
resolved.Power.ID = ""
}
@@ -139,7 +142,24 @@ func buildCatalog(c content.Catalog, change CatalogChange) (content.Catalog, str
}
powers := *c.Powers
c.Powers = &powers
if c.Building == nil {
b, e := content.Load()
if e != nil {
return invalid(e)
}
c.RestoreBuilding(b)
}
building := *c.Building
c.Building = &building
switch change.Op {
case "create-building":
v := *change.Building
if v.ID == "" {
v.ID = "building_" + game.ID()
}
c.Building.Definitions = append(slices.Clone(c.Building.Definitions), v)
resolved.Building = &v
target = v.ID
case "create-power":
v := *change.Power
if v.ID == "" {
+9
View File
@@ -9,6 +9,15 @@ import (
)
func (h *HTTP) powerRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/building", func(w http.ResponseWriter, r *http.Request) {
if _, e := h.auth.account(r); e != nil {
fail(w, 401, "sign in required")
return
}
h.s.mu.Lock()
defer h.s.mu.Unlock()
respond(w, 200, h.s.world.Balance.Building)
})
mux.HandleFunc("GET /api/powers", func(w http.ResponseWriter, r *http.Request) {
account, e := h.auth.account(r)
if e != nil {