Files

282 lines
9.5 KiB
Go

package game
import (
"aeternalis/server/content"
p "aeternalis/server/internal/protocol"
"encoding/json"
"testing"
"time"
)
func combatFixture(t *testing.T) (*World, *Character, *Mob) {
t.Helper()
w, c := fixture(t)
w.now = w.CreatedAt.Add(time.Minute)
d := *w.mobDefinition("ash_guard")
d.Behavior = "passive"
d.HP = 200
d.ScalePerDay = 0
m := w.newMob("combat-target", d, w.terrain.Position(20, 0), 60)
w.Mobs[m.Entity.ID] = m
a := protocolAttack(w.Balance.Combat.Attacks[0])
c.View.Items = append(c.View.Items, p.ItemInstance{ID: "bow", DefinitionID: "bow", Name: "Bow", Slot: "weapon", Requirements: p.ItemRequirements{Level: 1}, Attack: &a})
c.View.Equipment["weapon"] = "bow"
w.RefreshStats(c)
return w, c, m
}
func advanceProjectiles(w *World, steps int) {
for i := 0; i < steps; i++ {
w.now = w.now.Add(50 * time.Millisecond)
w.updateCombat(.05)
}
}
func TestProjectileHitMissCostsAndWeaponSwap(t *testing.T) {
w, c, m := combatFixture(t)
stamina := c.View.Stamina
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Target: m.Entity.ID, Seq: 1}); err != nil {
t.Fatal(err)
}
if m.Entity.HP != m.Entity.MaxHP || len(w.Projectiles) != 1 || c.View.Stamina != stamina-2 {
t.Fatal("projectile applied instant damage or wrong cost")
}
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "attack", Target: m.Entity.ID, Seq: 1}); err == nil {
t.Fatal("sequence replay accepted")
}
if err := w.unequip(c, "weapon"); err != nil {
t.Fatal(err)
}
if err := w.attack(c, m.Entity.ID); err == nil {
t.Fatal("weapon swap bypassed cooldown")
}
advanceProjectiles(w, 20)
if m.Entity.HP >= m.Entity.MaxHP || len(w.Projectiles) != 0 {
t.Fatal("projectile failed to hit")
}
w, c, m = combatFixture(t)
if err := w.attack(c, m.Entity.ID); err != nil {
t.Fatal(err)
}
m.Entity.Position = w.terrain.Position(20, 12)
advanceProjectiles(w, 25)
if m.Entity.HP != m.Entity.MaxHP {
t.Fatal("straight projectile homed on moving target")
}
w, c, m = combatFixture(t)
c.View.Stamina = 0
if err := w.attack(c, m.Entity.ID); err == nil || len(w.Projectiles) != 0 {
t.Fatal("insufficient stamina spawned projectile")
}
}
func TestCrossChunkPursuitRetainsIdentityAndHealth(t *testing.T) {
w, c, m := combatFixture(t)
m.Definition.Behavior = "aggressive"
m.HomeX = 0
m.HomeZ = 0
home := w.terrain.Position(125, 10)
m.Home = &home
m.Entity.Position = home
m.Entity.HP = 123
c.View.Entity.Position = w.terrain.Position(141, 10)
for i := 0; i < 8; i++ {
w.updateMobs(map[string]bool{"0:0": true, "1:0": true}, .25)
}
if m.Entity.Position.X <= 128 || m.Entity.HP != 123 || w.Mobs[m.Entity.ID] != m {
t.Fatal("chunk edge blocked/reset pursuit")
}
x := m.Entity.Position.X
w.updateMobs(map[string]bool{"1:0": true}, .25)
if m.Entity.Position.X <= x {
t.Fatal("mob froze when only destination chunk active")
}
raw, _ := json.Marshal(w)
var restored World
_ = json.Unmarshal(raw, &restored)
if err := restored.Restore(w.now); err != nil {
t.Fatal(err)
}
if restored.Mobs[m.Entity.ID].Entity.Position.X != m.Entity.Position.X || restored.Mobs[m.Entity.ID].Entity.HP != 123 {
t.Fatal("restart reset traveling mob")
}
}
func TestAreaHealingBoundariesAndSourceDeath(t *testing.T) {
w, c, m := combatFixture(t)
c.View.Entity.Position = w.terrain.Position(127, 10)
m.Entity.Position = w.terrain.Position(129, 10)
shot := Projectile{Source: c.View.Entity.ID, Player: true, Position: body(c.View.Entity.Position), Power: 10, Attack: p.CombatAttack{ID: "test_area", Name: "Area", Delivery: "melee", Effect: "damage", Stat: "rangedDamage", Multiplier: 1, Range: 6, Cooldown: 1, Speed: 1, Radius: 6, Duration: 4, Interval: 1}}
w.impact(shot, "")
if m.Entity.HP != 190 || len(w.Areas) != 1 {
t.Fatal("AoE clipped chunk boundary")
}
other, err := w.Join("other", "Other")
if err != nil {
t.Fatal(err)
}
other.Online = true
other.View.Entity.Position = w.terrain.Position(128, 10)
other.View.Entity.HP = 30
c.View.Entity.HP = 40
shot.Attack.Effect = "heal"
w.impact(shot, "")
if other.View.Entity.HP != 40 || c.View.Entity.HP != 50 {
t.Fatal("area did not heal living allies")
}
w.kill(other, "test death")
w.areaPulse(shot)
if other.View.Entity.HP != 0 || other.View.Entity.State != "dead" {
t.Fatal("area resurrected a corpse")
}
w.kill(c, "source died")
hp := m.Entity.HP
advanceProjectiles(w, 30)
if m.Entity.HP != hp || len(w.Areas) != 0 {
t.Fatal("dead source area continued damaging")
}
}
func TestProjectileAndAreaRestartDoesNotRepeatDamage(t *testing.T) {
w, c, m := combatFixture(t)
a := w.playerAttack(c)
a.Radius = 4
a.Duration = 2
a.Interval = 1
c.View.Items[len(c.View.Items)-1].Attack = &a
if err := w.attack(c, m.Entity.ID); err != nil {
t.Fatal(err)
}
advanceProjectiles(w, 4)
raw, _ := json.Marshal(w)
var restored World
_ = json.Unmarshal(raw, &restored)
if err := restored.Restore(w.now); err != nil {
t.Fatal(err)
}
advanceProjectiles(&restored, 12)
rm := restored.Mobs[m.Entity.ID]
hp := rm.Entity.HP
if hp >= 200 || len(restored.Areas) != 1 {
t.Fatal("saved projectile did not complete")
}
raw, _ = json.Marshal(&restored)
var again World
_ = json.Unmarshal(raw, &again)
if err := again.Restore(restored.now); err != nil {
t.Fatal(err)
}
again.updateCombat(.01)
if again.Mobs[m.Entity.ID].Entity.HP != hp {
t.Fatal("restore repeated impact or early area tick")
}
advanceProjectiles(&again, 60)
if len(again.Areas) != 0 || len(again.Projectiles) != 0 {
t.Fatal("effects outlived their deadlines")
}
}
func TestMobLootOnceAndRespawn(t *testing.T) {
w, c, m := combatFixture(t)
m.Loot.Entries = []content.LootEntry{{Item: "herb", Amount: 2, Chance: 100, Weight: 1}, {Item: "wood", Amount: 1, Chance: 100, Weight: 1, Group: "one"}, {Item: "ore", Amount: 1, Chance: 100, Weight: 2, Group: "one"}}
shot := Projectile{Source: c.View.Entity.ID, Player: true, Attack: p.CombatAttack{Effect: "damage"}, Power: 1000}
w.combatHit(shot, m.Entity.ID)
w.combatHit(shot, m.Entity.ID)
if len(w.Corpses) != 1 || c.View.Kills != 1 {
t.Fatal("duplicate loot or kill credit")
}
for _, corpse := range w.Corpses {
if corpse.Inventory["herb"] != 2 || corpse.Inventory["wood"]+corpse.Inventory["ore"] != 1 {
t.Fatal("guaranteed/weighted loot wrong")
}
}
raw, _ := json.Marshal(w)
var restored World
_ = json.Unmarshal(raw, &restored)
if err := restored.Restore(w.now); err != nil {
t.Fatal(err)
}
restored.now = restored.now.Add(61 * time.Second)
restored.updateMobs(map[string]bool{Key(m.HomeX, m.HomeZ): true}, .05)
if restored.Mobs[m.Entity.ID].Entity.HP <= 0 || len(restored.Corpses) != 1 {
t.Fatal("respawn rerolled old loot")
}
}
func TestTerrainObstructionAndCombatLimits(t *testing.T) {
w, c, m := combatFixture(t)
if _, blocked := w.terrainHit(p.Vec3{X: 0, Y: -2}, p.Vec3{X: 20, Y: -2}); !blocked {
t.Fatal("terrain penetration allowed")
}
w.Balance.CombatRules.MaxProjectiles = 1
if err := w.attack(c, m.Entity.ID); err != nil {
t.Fatal(err)
}
w.now = w.now.Add(2 * time.Second)
stamina := c.View.Stamina
if err := w.attack(c, m.Entity.ID); err == nil || c.View.Stamina != stamina {
t.Fatal("work limit spent resources")
}
w.kill(c, "permanent death")
advanceProjectiles(w, 30)
if m.Entity.HP != m.Entity.MaxHP {
t.Fatal("dead character's projectile hit")
}
if err := w.SpawnMobs("ash_guard", "33,0,0"); err == nil {
t.Fatal("spawn limit ignored")
}
if _, _, _, err := ParseMobSpawn("1,NaN,0"); err == nil {
t.Fatal("nonfinite spawn accepted")
}
}
func TestRegionalSpawnsRespectExploredBiomeAndPlayerDistance(t *testing.T) {
w, c, _ := combatFixture(t)
w.Mobs = map[string]*Mob{}
w.ResourceLayouts["0:0"] = ResourceLayout{BiomeID: "forest", Biome: "Forest"}
w.Balance.Combat.Spawns = []content.MobSpawn{{ID: "test_region", MobID: "ash_guard", X: 20, Z: 0, Count: 1, Respawn: 5, MinPlayerDistance: 10, Biomes: []string{"meadow"}}}
active := map[string]bool{"0:0": true}
w.updateSpawns(active)
if len(w.Mobs) != 0 {
t.Fatal("spawn ignored explored biome")
}
w.Balance.Combat.Spawns[0].Biomes = []string{"forest"}
c.View.Entity.Position = w.terrain.Position(20, 0)
w.updateSpawns(active)
if len(w.Mobs) != 0 {
t.Fatal("spawn ignored player distance")
}
c.View.Entity.Position = w.terrain.Position(0, 0)
w.updateSpawns(map[string]bool{})
if len(w.Mobs) != 0 {
t.Fatal("spawn activated an unsubscribed chunk")
}
w.updateSpawns(active)
w.updateSpawns(active)
if len(w.Mobs) != 1 || w.Mobs["spawn:test_region:0"] == nil {
t.Fatal("regional slot was missing or duplicated")
}
}
func TestProjectileSpeedSampleAndShortFlightEvents(t *testing.T) {
w, c, m := combatFixture(t)
dex, str := float64(2), float64(1)
weapon := &c.View.Items[len(c.View.Items)-1]
weapon.Attack.DexteritySpeed = &dex
weapon.Attack.StrengthSpeed = &str
c.View.Stats["dexterity"] = p.StatValue{Final: 5}
c.View.Stats["strength"] = p.StatValue{Final: 3}
m.Entity.Position = w.terrain.Position(2, 0)
if err := w.attack(c, m.Entity.ID); err != nil {
t.Fatal(err)
}
if len(w.ProjectileVisuals) != 1 || w.ProjectileVisuals[0].Phase != "launch" || w.ProjectileVisuals[0].Speed != 48 {
t.Fatal("wrong launch speed/event")
}
c.View.Stats["dexterity"] = p.StatValue{Final: 100}
w.updateCombat(.1)
if len(w.Projectiles) != 0 || len(w.ProjectileVisuals) != 2 || w.ProjectileVisuals[1].Outcome != "hit" {
t.Fatal("short flight lost its hit")
}
if w.ProjectileVisuals[1].Speed != 48 {
t.Fatal("stat change altered in-flight speed")
}
snapshot := w.Snapshot(c.View.Entity.ID, map[string]bool{})
if len(snapshot.ProjectileVisuals) != 2 {
t.Fatal("flight between snapshots became invisible")
}
}