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 }