server/player_flight_modes_test.go (10493B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "math" 7 "net" 8 "testing" 9 "time" 10 ) 11 12 func TestPlayerFlightWalkAndRunPreserveUnrelatedMotionAndEquipment(t *testing.T) { 13 for _, action := range []uint32{22, 23} { 14 for _, running := range []bool{false, true} { 15 t.Run(fmt.Sprintf("action%d/preferredRun%t", action, running), func(t *testing.T) { 16 world := newPlayerFormWorld(t, 117322) 17 player, client := world.characters[1], world.clients[1] 18 player.running = running 19 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 20 t.Fatal(err) 21 } 22 world.advanceEquipment(client.equipment.readyAt) 23 player.position = [3]float32{961.625, 60, 510.75} 24 player.movement.velocity, player.movement.speed = [3]float32{3, 4, 0}, 5 25 player.movement.forward, player.movement.strafe, player.movement.turn, player.movement.vertical = 1, 2, 1.5, 6 26 player.movement.components = [10]byte{7, 3, 1, 3, 4, 3, 5, 5, 4, 4} 27 body, item := player.body, player.inventory.Items[0] 28 observer := &combatTestConn{} 29 world.characters[2] = &character{id: 2, playfield: 705, health: 100} 30 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, ready: true, target: true, conn: observer} 31 32 if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil { 33 t.Fatal(err) 34 } 35 36 mode, orientation := byte(2), uint32(0) 37 if action == 23 { 38 mode, orientation = 3, 4 39 } 40 if player.flying() || player.running != (action == 23) || player.movement.components[0] != mode || player.movement.orientation != orientation || binary.BigEndian.Uint32(player.vehicleState()[22:26]) != uint32(mode) { 41 t.Fatal("walk/run did not leave flight with its distinct orientation and preferred mode", player.movement, player.running) 42 } 43 if player.position != [3]float32{961.625, 60.1, 510.75} || player.movement.previousPosition != player.position || player.movement.velocity != [3]float32{3, 4, 0} || player.movement.speed != 5 || !player.movement.falling || player.movement.fallHeight != 60.1 { 44 t.Fatal("walk/run exit lost position-only placement or falling admission", player.position, player.movement) 45 } 46 if player.movement.forward != 1 || player.movement.strafe != 2 || player.movement.turn != 1.5 || player.movement.vertical != 0 || player.movement.components != [10]byte{mode, 3, 1, 3, 4, 3, 5, 5, 4, 4} { 47 t.Fatal("walk/run exit changed unrelated controls or helper state", player.movement) 48 } 49 if player.body != body || player.inventory.Items[0] != item || player.height != 150 { 50 t.Fatal("a movement mode request changed the worn vehicle or its body", player.body, player.inventory) 51 } 52 if len(observer.packets) != 1 || observer.packets[0][29] != byte(action) || binary.BigEndian.Uint16(observer.packets[0][32:34]) != 960 { 53 t.Fatal("observer must receive the original pose and action before its own native lift", observer.packets) 54 } 55 }) 56 } 57 } 58 } 59 60 func TestPlayerFlightWalkAndRunPreserveExistingGravity(t *testing.T) { 61 for _, action := range []uint32{22, 23} { 62 t.Run(fmt.Sprint(action), func(t *testing.T) { 63 world := newPlayerFormWorld(t, 117322) 64 player, client := world.characters[1], world.clients[1] 65 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 66 t.Fatal(err) 67 } 68 world.advanceEquipment(client.equipment.readyAt) 69 player.position[1] = 60 70 player.movement.falling, player.movement.verticalVelocity, player.movement.fallHeight = true, -12, 1000 71 72 if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil { 73 t.Fatal(err) 74 } 75 exit, impulse := player.position, player.movement.verticalVelocity 76 world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond)) 77 78 if exit != [3]float32{961.625, 60.1, 510.75} || impulse != -12 || player.movement.verticalVelocity != -14 || player.movement.fallHeight != 1000 || !player.movement.falling || math.Abs(float64(player.position[1])-58.7) > .00001 { 79 t.Fatal("leaving flight reset existing gravity or fall history", exit, impulse, player.position, player.movement) 80 } 81 }) 82 } 83 } 84 85 func TestPlayerFlightWalkLevelsFacingButRunRetainsFlightOrientation(t *testing.T) { 86 for _, action := range []uint32{22, 23} { 87 t.Run(fmt.Sprint(action), func(t *testing.T) { 88 world := newPlayerFormWorld(t, 117322) 89 player, client := world.characters[1], world.clients[1] 90 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 91 t.Fatal(err) 92 } 93 world.advanceEquipment(client.equipment.readyAt) 94 player.position[1] = 60 95 player.rotation = [4]float32{-.38268343, 0, 0, .9238795} 96 97 if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil { 98 t.Fatal(err) 99 } 100 exit, _ := bodyAxes(player.rotation) 101 world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond)) 102 103 forward, _ := bodyAxes(player.rotation) 104 if exit[1] < .7 || !player.movement.falling || player.flying() { 105 t.Fatal("exit must place in the previous orientation before restoring ground physics", exit, player.movement) 106 } 107 if action == 22 && forward != [3]float32{0, 0, 1} || action == 23 && forward[1] < .7 { 108 t.Fatal("walk and run must have distinct orientation restoration", forward, player.rotation, player.movement) 109 } 110 }) 111 } 112 } 113 114 func TestPlayerFlightEquipmentRemovalStillAppliesAfterWalkOrRun(t *testing.T) { 115 for _, action := range []uint32{22, 23} { 116 t.Run(fmt.Sprint(action), func(t *testing.T) { 117 world := newPlayerFormWorld(t, 117322) 118 player, client := world.characters[1], world.clients[1] 119 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 120 t.Fatal(err) 121 } 122 world.advanceEquipment(client.equipment.readyAt) 123 player.position[1] = 60 124 if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil { 125 t.Fatal(err) 126 } 127 item := player.inventory.Items[0] 128 if err := world.handleInventory(client, itemMovePacket(1, 1, 111)[20:]); err != nil || client.equipment == nil { 129 t.Fatal("removal was not scheduled", err) 130 } 131 finish := client.equipment.readyAt 132 player.movement.updatedAt = finish 133 before := player.position 134 135 world.advanceEquipment(finish) 136 137 if math.Abs(float64(player.position[1]-before[1])-.1) > .00001 || player.movement.orientation != 0 || player.flying() || player.inventory.Items[0].ID != item.ID || player.inventory.Items[0].Slot != 64 || !player.movement.falling { 138 t.Fatal("equipment removal lost its own exit effect or the original item", player.position, player.movement, player.inventory) 139 } 140 }) 141 } 142 } 143 144 func TestPlayerFlightWalkAndRunPlacementCanEnterSwimming(t *testing.T) { 145 for _, action := range []uint32{22, 23} { 146 t.Run(fmt.Sprint(action), func(t *testing.T) { 147 world := newPlayerFormWorld(t, 117322) 148 player, client := world.characters[1], world.clients[1] 149 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 150 t.Fatal(err) 151 } 152 world.advanceEquipment(client.equipment.readyAt) 153 player.position = [3]float32{100, 6.5, 25} 154 player.movement.strafe, player.movement.vertical = 2, 6 155 player.movement.components[3], player.movement.components[4] = 3, 4 156 157 if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil { 158 t.Fatal(err) 159 } 160 161 if player.flying() || player.running != (action == 23) || !player.movement.deepWater || player.movement.components[0] != 4 || player.movement.strafe != 0 || player.movement.vertical != 0 || player.movement.components[3] != 3 || player.position[0] != 100 || player.position[2] != 25 || player.inventory.Items[0].Slot != 1 { 162 t.Fatal("flight exit overwrote water admission or retained its lateral control", player.position, player.movement, player.inventory) 163 } 164 }) 165 } 166 } 167 168 func TestPlayerFlightGroundModeReplayedAfterWearForNewObserver(t *testing.T) { 169 for _, action := range []uint32{22, 23} { 170 for _, following := range []bool{false, true} { 171 t.Run(fmt.Sprintf("action%d/following%t", action, following), func(t *testing.T) { 172 world := newPlayerFormWorld(t, 117322) 173 player, client := world.characters[1], world.clients[1] 174 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil { 175 t.Fatal(err) 176 } 177 world.advanceEquipment(client.equipment.readyAt) 178 player.position[1] = 60 179 if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil { 180 t.Fatal(err) 181 } 182 world.advancePlayerMovement(player.movement.updatedAt.Add(4 * time.Second)) 183 if player.flying() || player.movement.active() { 184 t.Fatal("fixture must settle in a ground mode with its flight item still worn", player.position, player.movement) 185 } 186 world.nextCharacter = 1 187 observer := world.newCharacter() 188 observer.playfield, observer.position = 705, player.position 189 if following { 190 player.followTarget = observer.id 191 } 192 startTestService(t, world) 193 connection, host := net.Pipe() 194 t.Cleanup(func() { connection.Close() }) 195 connection.SetDeadline(time.Now().Add(5 * time.Second)) 196 go func() { defer host.Close(); world.serveWorldConnection(host) }() 197 cookie := systemPacket(0x1b, append(binary.BigEndian.AppendUint32(nil, observer.id), make([]byte, 8)...)) 198 binary.BigEndian.PutUint16(cookie, 0xdfdf) 199 200 if _, err := connection.Write(cookie); err != nil { 201 t.Fatal(err) 202 } 203 wear, restore, anchor := -1, -1, -1 204 for index := 0; ; index++ { 205 packet, err := readPacket(connection) 206 if err != nil { 207 t.Fatal(err) 208 } 209 key := binary.BigEndian.Uint32(packet[16:20]) 210 if key == 0x5f52412e { 211 break 212 } 213 if key != 0x35505644 && key != 0x575f6628 && key != 0x54111123 { 214 continue 215 } 216 if binary.BigEndian.Uint32(packet[24:28]) != 1 { 217 continue 218 } 219 if key == 0x35505644 && binary.BigEndian.Uint32(packet[45:49]) == 6 { 220 wear = index 221 } 222 if key == 0x575f6628 && binary.BigEndian.Uint32(packet[29:33]) == action { 223 restore = index 224 } 225 if key == 0x54111123 && packet[29] == 0x94 { 226 anchor = index 227 } 228 } 229 230 if wear < 0 || restore <= wear || anchor <= restore { 231 t.Fatal("new observer must restore the ground mode after worn effects, then anchor the native lift", wear, restore, anchor) 232 } 233 }) 234 } 235 } 236 }