server/npc_motion_test.go (7767B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "math" 8 "testing" 9 "time" 10 ) 11 12 func TestNPCRoamingMatchesNativeAccelerationAndWaypointArrival(t *testing.T) { 13 for _, scenario := range []struct { 14 name string 15 destination float32 16 position float32 17 moving bool 18 }{ 19 {"accelerating", 11, 1.1, true}, 20 {"near waypoint", 1.125, 1, false}, 21 } { 22 t.Run(scenario.name, func(t *testing.T) { 23 world, _, start := newNPCPursuitWorld(t) 24 world.characters[1000000].fighting = 0 25 world.characters[1000000].motion.destination = [3]float32{scenario.destination, 0, 0} 26 world.characters[1000000].motion.moving = true 27 world.characters[1000000].motion.nextMove = start 28 29 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 30 31 if math.Abs(float64(world.characters[1000000].position[0]-scenario.position)) > .0001 || world.characters[1000000].motion.moving != scenario.moving { 32 t.Fatalf("native acceleration/arrival: position=%v moving=%t", world.characters[1000000].position, world.characters[1000000].motion.moving) 33 } 34 }) 35 } 36 } 37 38 func TestNPCRoamingPublishesTheDestinationFloorHeight(t *testing.T) { 39 // Keep every random endpoint on raised ground above the retained home height. 40 world, conn, start := newNPCPursuitWorld(t) 41 npc := world.characters[1000000] 42 npc.fighting = 0 43 npc.position, npc.motion.home = [3]float32{5, 2.01, 5}, [3]float32{5, 0, 5} 44 npc.motion.nextMove = start 45 npc.body = playerCollisionBody{mesh: 1, scale: 1, base: int32(npc.baseScale()), 46 sphere: collisionSphere{offset: [3]float32{0, .25, 0}, radius: .25}} 47 world.terrain = map[uint32]*terrain{950: {width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, 48 spacing: 5, scale: 1, heights: []byte{2, 2, 2, 2, 2, 2, 2, 2, 2}}} 49 world.geometry = &sceneGeometry{ 50 playfields: map[uint32]scenePlayfield{950: {cellsX: 1, cellsZ: 1, cellWidth: 10, cellDepth: 10, sizeX: 10, sizeZ: 10}}, 51 terrain: world.terrain, 52 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{950 << 16: nil}), 53 waters: sceneResourcesForTest(map[uint32]*sceneWater{950: {zones: []waterZone{{maxHeight: -1}}}}), 54 } 55 56 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 57 58 if npc.position != [3]float32{5, 2.01, 5} || npc.motion.home != [3]float32{5, 0, 5} || !npc.motion.moving { 59 t.Fatal("choosing a route changed the current pose or retained home", npc.position, npc.motion) 60 } 61 if npc.motion.destination[1] != 2.01 || len(conn.packets) != 1 || math.Float32frombits(binary.BigEndian.Uint32(conn.packets[0][74:78])) != 2.01 { 62 t.Fatalf("idle route advertised a waypoint below its floor: destination=%v packets=%x", npc.motion.destination, conn.packets) 63 } 64 } 65 66 func TestNPCRoamingDoesNotAdvertiseAnOccludedRoute(t *testing.T) { 67 // Every candidate around home lies across the wall from the current pose. 68 world, conn, start := newNPCPursuitWorld(t) 69 addCombatWall(world) 70 world.terrain = world.geometry.terrain 71 world.geometry.waters = sceneResourcesForTest(map[uint32]*sceneWater{950: {zones: []waterZone{{maxHeight: -1}}}}) 72 npc := world.characters[1000000] 73 npc.fighting = 0 74 npc.motion.home, npc.motion.nextMove = [3]float32{5, 0, 10}, start 75 76 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 77 78 if npc.motion.moving || npc.position != [3]float32{16, 0, 10} || len(conn.packets) != 0 { 79 t.Fatal("idle route crossed an obstruction hidden from the native follower", npc.position, npc.motion, conn.packets) 80 } 81 if !npc.motion.nextMove.After(start.Add(3 * time.Second)) { 82 t.Fatal("blocked idle route did not pause before another choice", npc.motion.nextMove) 83 } 84 } 85 86 func TestNPCPursuitStartsItsRouteBeforeAdvancing(t *testing.T) { 87 world, conn, start := newNPCPursuitWorld(t) 88 89 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 90 91 if world.characters[1000000].position != [3]float32{1, 0, 0} || !world.characters[1000000].motion.moving { 92 t.Fatalf("pursuit started with a position jump: %v", world.characters[1000000].position) 93 } 94 if len(conn.packets) != 1 || math.Float32frombits(binary.BigEndian.Uint32(conn.packets[0][42:46])) != 1 { 95 t.Fatalf("route does not begin at the published position: %x", conn.packets) 96 } 97 } 98 99 func TestNPCPursuitStopsAfterAdvancingTheCurrentInterval(t *testing.T) { 100 world, conn, start := newNPCPursuitWorld(t) 101 world.characters[1].position = [3]float32{3.5, 0, 0} 102 world.characters[1000000].motion.destination = [3]float32{3.5, 0, 0} 103 world.characters[1000000].motion.moving = true 104 world.characters[1000000].running = true 105 world.characters[1000000].movement.velocity = [3]float32{4.5, 0, 0} 106 107 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 108 109 if math.Abs(float64(world.characters[1000000].position[0]-2.125)) > .0001 || world.characters[1000000].motion.moving || world.characters[1000000].fighting != 1 { 110 t.Fatalf("pursuit arrival was deferred to an outdated pose: position=%v moving=%t fighting=%d", world.characters[1000000].position, world.characters[1000000].motion.moving, world.characters[1000000].fighting) 111 } 112 if len(conn.packets) != 2 || math.Abs(float64(math.Float32frombits(binary.BigEndian.Uint32(conn.packets[0][42:46]))-2.125)) > .0001 { 113 t.Fatalf("stop did not publish the current pose: %x", conn.packets) 114 } 115 } 116 117 func TestNPCPursuitRouteChangesPreserveNativePosition(t *testing.T) { 118 world, conn, start := newNPCPursuitWorld(t) 119 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 120 world.characters[1].position = [3]float32{21, 0, 1} 121 122 advanceNavigationForTest(t, world, start.Add(500 * time.Millisecond), world.advanceNPCs) 123 124 if len(conn.packets) != 2 || !bytes.Equal(conn.packets[1][42:54], make([]byte, 12)) { 125 t.Fatalf("changing direction relocated the native actor: %x", conn.packets) 126 } 127 if math.Float32frombits(binary.BigEndian.Uint32(conn.packets[1][70:74])) != 21 || math.Float32frombits(binary.BigEndian.Uint32(conn.packets[1][78:82])) != 1 { 128 t.Fatalf("route lost the new target position: %x", conn.packets[1]) 129 } 130 } 131 132 func TestNPCMotionStopsBeforeLeavingTheTerrainGrid(t *testing.T) { 133 for _, pursuing := range []bool{false, true} { 134 t.Run(fmt.Sprintf("pursuing=%t", pursuing), func(t *testing.T) { 135 world, _, start := newNPCPursuitWorld(t) 136 npc := world.characters[1000000] 137 world.terrain = map[uint32]*terrain{950: {width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 1, scale: 1, heights: make([]byte, 9)}} 138 npc.position = [3]float32{1.95, 0, 0} 139 npc.motion.destination = [3]float32{4, 0, 0} 140 npc.motion.moving = true 141 npc.motion.nextMove = start 142 npc.running = pursuing 143 npc.movement.velocity = [3]float32{4.5, 0, 0} 144 if !pursuing { 145 npc.fighting = 0 146 } 147 148 advanceNavigationForTest(t, world, start.Add(250 * time.Millisecond), world.advanceNPCs) 149 150 if npc.position != [3]float32{1.95, 0, 0} || npc.motion.moving || npc.movement.velocity != [3]float32{} { 151 t.Fatal("movement left the terrain grid", npc.position, npc.motion) 152 } 153 }) 154 } 155 } 156 157 func TestNPCMotionRetainsItsGroundTangentVelocity(t *testing.T) { 158 world, _, _ := enteredMissionEncounterWorld(t) 159 npc := world.characters[2000003] 160 npc.position = [3]float32{15, .011, 15} 161 npc.motion.home = [3]float32{12.5, .011, 34} 162 npc.motion.returning = true 163 start := time.Now() 164 world.lastNPCUpdate = start 165 166 for tick := 1; tick <= 7; tick++ { 167 advanceNavigationForTest(t, world, start.Add(time.Duration(tick) * 250 * time.Millisecond), world.advanceNPCs) 168 } 169 170 if math.Abs(float64(npc.movement.velocity[1])) <= 1 || npc.movement.velocity[2] >= 4.3 { 171 t.Fatal("climbing lost its vertical velocity before the next speed-cap calculation", npc.position, npc.movement.velocity) 172 } 173 }