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
+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)