Files

128 lines
4.0 KiB
Go

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")
}
}