server/npc_outdoor_navigation_test.go (4783B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "fmt" 6 "testing" 7 "time" 8 ) 9 10 func newOutdoorNavigationWorld(t *testing.T) (*server, time.Time) { 11 t.Helper() 12 world := newPlayerCollisionWorld(t) 13 player, npc := world.characters[1], world.characters[1000000] 14 player.playfield, player.position = 950, [3]float32{42, 14.01, 62} 15 npc.playfield, npc.position, npc.geometry = 950, [3]float32{42, 14.01, 58}, world.geometry 16 npc.monsterData, npc.level, npc.combatProfile = 17655, 1, gamedata.NPCCombatProfiles[17655][1] 17 npc.motion = &npcMotion{home: npc.position} 18 npc.fighting, world.fighting[npc.id] = player.id, npc 19 world.mobileNPCs = map[uint32][]*character{950: {npc}} 20 start := time.Now() 21 world.lastNPCUpdate = start 22 npc.nextAttack = start.Add(1750 * time.Millisecond) 23 return world, start 24 } 25 26 func TestOutdoorNPCDetoursAroundOriginalTrainingScenery(t *testing.T) { 27 for _, row := range []struct { 28 monster uint32 29 level uint16 30 start [3]float32 31 end [3]float32 32 }{ 33 {17655, 1, [3]float32{42, 14.01, 58}, [3]float32{42, 14.01, 62}}, 34 {17655, 1, [3]float32{46, 14.01, 58}, [3]float32{50, 14.01, 58}}, 35 {17655, 1, [3]float32{46, 14.01, 58}, [3]float32{50.0453, 14.01, 69.887344}}, 36 {30354, 4, [3]float32{46, 14.01, 58}, [3]float32{50, 14.01, 58}}, 37 } { 38 t.Run(fmt.Sprint(row.monster, "/", row.start[0], "/", row.end[2]), func(t *testing.T) { 39 world, start := newOutdoorNavigationWorld(t) 40 player, npc := world.characters[1], world.characters[1000000] 41 player.position, npc.position, npc.motion.home = row.end, row.start, row.start 42 npc.monsterData, npc.level, npc.combatProfile = row.monster, row.level, gamedata.NPCCombatProfiles[row.monster][row.level] 43 deadline := npc.nextAttack 44 if world.combatVisible(npc, player, start) { 45 t.Fatal("original scenery no longer separates the fixture actors") 46 } 47 48 for tick := 1; tick <= 96; tick++ { 49 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 50 } 51 52 if !npc.pursuitArrived(player.position) || !world.combatVisible(npc, player, start.Add(24*time.Second)) || 53 npc.motion.moving || npc.fighting != player.id || npc.nextAttack != deadline { 54 t.Fatal("outdoor pursuit stopped on the wrong side of original scenery", npc.position, npc.motion.route) 55 } 56 }) 57 } 58 } 59 60 func TestOutdoorNPCDetourReturnsHomeAfterTargetLoss(t *testing.T) { 61 world, start := newOutdoorNavigationWorld(t) 62 player, npc := world.characters[1], world.characters[1000000] 63 player.position, npc.position = [3]float32{50, 14.01, 58}, [3]float32{46, 14.01, 58} 64 npc.motion.home = npc.position 65 for tick := 1; tick <= 96; tick++ { 66 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 67 } 68 if !npc.pursuitArrived(player.position) { 69 t.Fatal("fixture did not reach the far side of the original wall", npc.position) 70 } 71 72 player.health = 0 73 for tick := 97; tick <= 192; tick++ { 74 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 75 } 76 77 if npc.motion.returning || npc.motion.moving || npc.fighting != 0 || surfaceDistanceSquared(npc.position, npc.motion.home) >= 1.01 { 78 t.Fatal("outdoor return did not detour back to the retained home", npc.position, npc.motion) 79 } 80 } 81 82 func TestOutdoorNPCDoesNotDeclareArrivalThroughOriginalWall(t *testing.T) { 83 world, start := newOutdoorNavigationWorld(t) 84 player, npc := world.characters[1], world.characters[1000000] 85 npc.position, player.position = [3]float32{47.4, 14.01, 58}, [3]float32{49.3, 14.01, 58} 86 if world.combatVisible(npc, player, start) { 87 t.Fatal("fixture no longer places the close target behind the original wall") 88 } 89 90 advanceNavigationForTest(t, world, start.Add(250*time.Millisecond), world.advanceNPCs) 91 92 if !npc.motion.moving || len(npc.motion.route) < 2 || npc.pursuitArrived(player.position) { 93 t.Fatal("nearby target across a wall was mistaken for a clear arrival", npc.position, npc.motion) 94 } 95 } 96 97 func TestOutdoorNPCRouteFollowsAChangingTarget(t *testing.T) { 98 world, start := newOutdoorNavigationWorld(t) 99 player, npc := world.characters[1], world.characters[1000000] 100 player.position, npc.position = [3]float32{50, 14.01, 58}, [3]float32{46, 14.01, 58} 101 for tick := 1; tick <= 4; tick++ { 102 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 103 } 104 105 player.position = [3]float32{60.5, 14.01, 70} 106 for tick := 5; tick <= 60; tick++ { 107 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 108 } 109 110 if !npc.pursuitArrived(player.position) || npc.motion.moving || npc.fighting != player.id { 111 t.Fatal("outdoor route retained a stale target behind the original wall", npc.position, npc.motion) 112 } 113 }