server/flying_vehicle_test.go (10740B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/hex" 7 "encoding/json" 8 "math" 9 "net/http/httptest" 10 "testing" 11 "time" 12 ) 13 14 func newFlyingVehicleWorld(t *testing.T) (*server, *combatTestConn) { 15 t.Helper() 16 t.Chdir(t.TempDir()) 17 world, conn := newCombatWorld(t) 18 player := world.newCharacter() 19 player.stats = testCharacterStats() 20 player.playfield, player.position = 800, [3]float32{600, 67, 700} 21 player.stats.Navigation.VehicleAir = 79 // Effective 81 with the fixture's abilities. 22 if err := world.saveUser(player); err != nil { 23 t.Fatal(err) 24 } 25 if granted, err := world.grantItem(player, 117322); err != nil || !granted { 26 t.Fatal(granted, err) 27 } 28 return world, conn 29 } 30 31 func TestFlyingVehicleEquipRemovalAndRestart(t *testing.T) { 32 world, conn := newFlyingVehicleWorld(t) 33 player, client := world.characters[1], world.clients[1] 34 observer := &combatTestConn{} 35 world.characters[2] = &character{id: 2, playfield: 800, health: 100} 36 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true} 37 started := time.Now() 38 39 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 40 t.Fatal(err) 41 } 42 if client.equipment == nil || client.equipment.readyAt.Before(started.Add(10*time.Second)) || 43 client.equipment.readyAt.After(time.Now().Add(10*time.Second)) { 44 t.Fatal("Yalmaha did not schedule its authored ten-second HUD1 equip") 45 } 46 world.advanceEquipment(client.equipment.readyAt.Add(-time.Nanosecond)) 47 if len(conn.packets) != 0 || player.inventory.Items[0].Slot != 64 { 48 t.Fatal("Yalmaha appeared before the equip delay expired") 49 } 50 world.advanceEquipment(client.equipment.readyAt) 51 restored := newTestInventoryServer(t).characters[1] 52 if !restored.flying() || restored.stats.Navigation.VehicleAir != 79 || restored.inventory.equipmentBonus(156) != 150 || 53 restored.inventory.Items[0] != (ownedItem{ID: 1, Type: 51022, Template: 117322, Quality: 30, Slot: 1, Count: 1}) || len(equippedItemPackets(restored)) != 1 { 54 t.Fatal("worn Yalmaha, flight effects or trained skill did not survive restart") 55 } 56 world.characters[1000000].playfield, world.characters[1000000].position = 800, player.position 57 if err := world.handleCombat(client, attackPacket(1, 1000000)[20:]); err != nil || player.fighting != 0 { 58 t.Fatal("a Yalmaha pilot started fighting") 59 } 60 player.stats.Navigation.VehicleAir = 0 61 if err := world.handleInventory(client, itemMovePacket(1, 1, 111)[20:]); err != nil { 62 t.Fatal(err) 63 } 64 if client.equipment == nil { 65 t.Fatal("removal incorrectly required Vehicle Air") 66 } 67 world.advanceEquipment(client.equipment.readyAt) 68 restored = newTestInventoryServer(t).characters[1] 69 70 if restored.flying() || restored.driving() || restored.inventory.equipmentBonus(156) != 0 || 71 restored.inventory.Items[0] != (ownedItem{ID: 1, Type: 51022, Template: 117322, Quality: 30, Slot: 64, Count: 1}) || len(equippedItemPackets(restored)) != 0 { 72 t.Fatal("removing the Yalmaha lost its identity or retained its effects") 73 } 74 if len(observer.packets) != 2 || binary.BigEndian.Uint32(observer.packets[0][45:49]) != 6 || 75 binary.BigEndian.Uint32(observer.packets[1][45:49]) != 7 || len(conn.packets) != 5 { 76 t.Fatal("owner and observer did not receive one native wear and removal effect") 77 } 78 } 79 80 func TestFlyingVehicleRejectsIneligibleEquipment(t *testing.T) { 81 for _, scenario := range []string{"skill 80", "ground skill only", "indoor", "Grid", "outgoing combat", "incoming combat", "wrong quality", "wrong slot"} { 82 t.Run(scenario, func(t *testing.T) { 83 world, conn := newFlyingVehicleWorld(t) 84 player, client := world.characters[1], world.clients[1] 85 request := inventoryMovePacket(1, 64, 1)[20:] 86 switch scenario { 87 case "skill 80": 88 player.stats.Navigation.VehicleAir = 78 89 case "ground skill only": 90 player.stats.Navigation.VehicleAir, player.stats.Navigation.VehicleGround = 5, 79 91 case "indoor": 92 player.playfield = 1183 93 case "Grid": 94 player.playfield = 152 95 case "outgoing combat": 96 player.fighting = 1000000 97 world.fighting[1] = player 98 case "incoming combat": 99 world.characters[1000000].fighting = 1 100 world.fighting[1000000] = world.characters[1000000] 101 case "wrong quality": 102 player.inventory.Items[0].Quality = 29 103 case "wrong slot": 104 binary.BigEndian.PutUint32(request[32:36], 2) 105 } 106 107 if err := world.handleInventory(client, request); err != nil { 108 t.Fatal(err) 109 } 110 111 if client.equipment != nil || player.inventory.Items[0].Slot != 64 || len(conn.packets) > 2 { 112 t.Fatal("ineligible Yalmaha changed equipment or published effects") 113 } 114 }) 115 } 116 } 117 118 func TestFlyingVehicleRechecksRequirementsAfterDelay(t *testing.T) { 119 for _, scenario := range []string{"skill", "playfield", "combat"} { 120 t.Run(scenario, func(t *testing.T) { 121 world, conn := newFlyingVehicleWorld(t) 122 player, client := world.characters[1], world.clients[1] 123 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 124 t.Fatal(err) 125 } 126 finish := client.equipment.readyAt 127 switch scenario { 128 case "skill": 129 player.stats.Navigation.VehicleAir = 78 130 case "playfield": 131 player.playfield = 1183 132 case "combat": 133 world.characters[1000000].fighting = 1 134 world.fighting[1000000] = world.characters[1000000] 135 } 136 137 world.advanceEquipment(finish) 138 139 if player.inventory.Items[0].Slot != 64 || player.flying() || len(conn.packets) != 2 { 140 t.Fatal("Yalmaha used stale eligibility from the start of its equip delay") 141 } 142 }) 143 } 144 } 145 146 func TestFlyingVehiclePredictsPitchedForwardAndReverse(t *testing.T) { 147 for _, scenario := range []struct { 148 name string 149 running bool 150 forward float32 151 y, z float32 152 }{ 153 {"forward", true, 1, 76.36917, 709.36917}, 154 {"reverse", true, -1, 57.63084, 690.63086}, 155 {"walking selected", false, 1, 76.36917, 709.36917}, 156 } { 157 t.Run(scenario.name, func(t *testing.T) { 158 world, _ := newFlyingVehicleWorld(t) 159 player, client := world.characters[1], world.clients[1] 160 player.inventory.Items[0].Slot = 1 161 player.running = scenario.running 162 player.rotation = [4]float32{-0.38268343, 0, 0, 0.9238795} 163 world.characters[client.id].movement = heldPlayerMovement(world.characters[client.id], scenario.forward, time.Now().Add(-time.Second)) 164 response := httptest.NewRecorder() 165 166 world.mapPlayers(response, httptest.NewRequest("GET", "/admin/map-players?offline=1", nil)) 167 var result struct{ Players []mapPlayer } 168 if err := json.Unmarshal(response.Body.Bytes(), &result); err != nil { 169 t.Fatal(err) 170 } 171 172 if len(result.Players) != 1 || math.Abs(float64(result.Players[0].Position[1]-scenario.y)) > 0.02 || math.Abs(float64(result.Players[0].Position[2]-scenario.z)) > 0.02 { 173 t.Fatalf("pitched flight prediction: %+v", result.Players) 174 } 175 }) 176 } 177 } 178 179 func TestFlyingVehicleSnapshotPreservesGroundMode(t *testing.T) { 180 for _, running := range []bool{false, true} { 181 t.Run(map[bool]string{false: "walking", true: "running"}[running], func(t *testing.T) { 182 player := &character{id: 1, name: "Pilot", appearance: 0x515, level: 1, running: running, 183 inventory: playerInventory{Items: []ownedItem{{ID: 1, Type: 51022, Template: 117322, Quality: 30, Slot: 1, Count: 1}}}} 184 185 packet := targetPacket(characterPacket(player)) 186 start := bytes.Index(packet, []byte("Pilot\x00")) + len("Pilot") + 40 187 vehicle := packet[start : start+28] 188 189 previous := uint32(2) 190 if running { 191 previous = 3 192 } 193 if vehicle[12] != 7 || binary.BigEndian.Uint32(vehicle[22:26]) != previous { 194 t.Fatalf("flight snapshot mode/previous mode: %x", vehicle) 195 } 196 }) 197 } 198 } 199 200 func TestFlyingVehicleBoundaryArrivalPreservesAltitude(t *testing.T) { 201 for _, scenario := range []struct { 202 name string 203 flying bool 204 altitude float32 205 want float32 206 }{ 207 {"airborne", true, 67, 67}, 208 {"terrain clearance", true, 10, 20.5}, 209 {"grounded", false, 67, 20.5}, 210 } { 211 t.Run(scenario.name, func(t *testing.T) { 212 world, _ := newFlyingVehicleWorld(t) 213 player, client := world.characters[1], world.clients[1] 214 player.position = [3]float32{600, scenario.altitude, 700} 215 if scenario.flying { 216 player.inventory.Items[0].Slot = 1 217 if err := world.saveInventory(player, player.inventory); err != nil { 218 t.Fatal(err) 219 } 220 } 221 world.zoneLinks = map[string]map[uint32]zoneLinks{"target": {800: {Walls: []zoneWall{{ 222 Start: [3]float32{590, 0, 700}, End: [3]float32{610, 0, 700}, Destination: 716, 223 ArrivalStart: [3]float32{100, 0, 100}, ArrivalEnd: [3]float32{120, 0, 100}, 224 }}}}} 225 world.terrain = map[uint32]*terrain{716: {width: 1, height: 1, boundsWidth: 1, boundsHeight: 1, spacing: 200, scale: 1, heights: []byte{20, 20, 20, 20}}} 226 request := npcMovementPacket(player, 2)[20:] 227 request = append(request[:16:16], encodeMovement1491(request[16:])...) 228 229 if err := world.handleMovement(client, request); err != nil { 230 t.Fatal(err) 231 } 232 if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil { 233 t.Fatal(err) 234 } 235 restored := newTestInventoryServer(t).characters[1] 236 237 if restored.playfield != 716 || restored.position != [3]float32{110, scenario.want, 108} || restored.flying() != scenario.flying || 238 player.rotation != [4]float32{0, 0, 0, 1} || len(restored.inventory.Items) != 1 { 239 t.Fatalf("flight boundary arrival: %+v", restored.userData()) 240 } 241 }) 242 } 243 } 244 245 func TestFlyingVehicleCapturedAltitudeReachesObserverAndRestart(t *testing.T) { 246 world, _ := newFlyingVehicleWorld(t) 247 player, client := world.characters[1], world.clients[1] 248 observer := &combatTestConn{} 249 world.characters[2] = &character{id: 2, playfield: 800, health: 100} 250 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true} 251 compact, err := hex.DecodeString("021c20041714efd6c60000000030f30000") 252 if err != nil { 253 t.Fatal(err) 254 } 255 request := npcMovementPacket(player, 2)[20:36] 256 request = append(request, compact...) 257 258 if err := world.handleMovement(client, request); err != nil { 259 t.Fatal(err) 260 } 261 if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil { 262 t.Fatal(err) 263 } 264 restored := newTestInventoryServer(t).characters[1] 265 266 if restored.position != [3]float32{900, 65.4375, 669.875} || math.Abs(float64(restored.rotation[0])+0.644166) > 0.0001 || 267 math.Abs(float64(restored.rotation[3])-0.764886) > 0.0001 || len(observer.packets) != 1 { 268 t.Fatalf("captured airborne position/pitch was lost: %+v", restored.userData()) 269 } 270 relay := observer.packets[0] 271 if binary.BigEndian.Uint32(relay[16:20]) != 0x54111123 || binary.BigEndian.Uint16(relay[32:34]) != 1047 { 272 t.Fatalf("observer did not receive the captured sixteenth-metre altitude: %x", relay) 273 } 274 }