aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/player_flight_test.go (14641B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"math"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestPlayerFlightWearAndRemovalLiftOnlyAfterTheCommittedDeadline(t *testing.T) {
     12 	world := newPlayerFormWorld(t, 117322)
     13 	player, client := world.characters[1], world.clients[1]
     14 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
     15 		t.Fatal(err)
     16 	}
     17 	finish := client.equipment.readyAt
     18 
     19 	world.advanceEquipment(finish.Add(-time.Nanosecond))
     20 	pending := player.position
     21 	world.advanceEquipment(finish)
     22 	entry, entryHistory := player.position, player.movement.previousPosition
     23 	if err := world.handleInventory(client, itemMovePacket(1, 1, 111)[20:]); err != nil || client.equipment == nil {
     24 		t.Fatal("removal was not scheduled", err)
     25 	}
     26 	world.advanceEquipment(client.equipment.readyAt)
     27 
     28 	if pending != [3]float32{961.625, 16.41, 510.75} || entry != [3]float32{961.625, 16.91, 510.75} || entryHistory != entry {
     29 		t.Fatal("flight entry must lift once through position-only placement", pending, entry, entryHistory)
     30 	}
     31 	if math.Abs(float64(player.position[1])-16.960006713867188) > .000001 || player.movement.previousPosition != [3]float32{961.625, 17.01, 510.75} || player.movement.falling {
     32 		t.Fatal("flight exit must align the lifted position with the resized vehicle before restoring the human body", player.position, player.movement)
     33 	}
     34 	if player.flying() || player.inventory.Items[0].Slot != 64 || player.height != 150 || player.body.mesh != 17909 {
     35 		t.Fatal("flight removal lost the item or saved human form", player.inventory, player.body)
     36 	}
     37 }
     38 
     39 func TestPlayerFlightEntryPreservesAnExistingFallAndPositionOnlyVelocity(t *testing.T) {
     40 	world := newPlayerFormWorld(t, 117322)
     41 	player, client := world.characters[1], world.clients[1]
     42 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
     43 		t.Fatal(err)
     44 	}
     45 	finish := client.equipment.readyAt
     46 	player.position = [3]float32{961.625, 90, 510.75}
     47 	player.movement.updatedAt = finish
     48 	player.movement.falling, player.movement.verticalVelocity = true, -12
     49 	player.movement.velocity, player.movement.speed = [3]float32{3, 4, 0}, 5
     50 	player.movement.fallHeight = 1000
     51 
     52 	world.advanceEquipment(finish)
     53 	entry, velocity, falling, impulse := player.position, player.movement.velocity, player.movement.falling, player.movement.verticalVelocity
     54 	world.advancePlayerMovement(finish.Add(100 * time.Millisecond))
     55 
     56 	if entry != [3]float32{961.625, 90.5, 510.75} || velocity != [3]float32{3, 4, 0} || !falling || impulse != -12 {
     57 		t.Fatal("flight entry cancelled falling or redirected the existing vector", entry, velocity, falling, impulse)
     58 	}
     59 	if !player.movement.falling || player.movement.verticalVelocity != -14 || player.movement.fallHeight != 1000 || player.position[1] >= 90 {
     60 		t.Fatal("disabling new falls must preserve gravity during an existing fall", player.position, player.movement)
     61 	}
     62 }
     63 
     64 func TestPlayerFlightExitPreservesVectorVelocityAndClearsOnlyVerticalControl(t *testing.T) {
     65 	world := newPlayerFormWorld(t, 117322)
     66 	player, client := world.characters[1], world.clients[1]
     67 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
     68 		t.Fatal(err)
     69 	}
     70 	world.advanceEquipment(client.equipment.readyAt)
     71 	if err := world.handleInventory(client, itemMovePacket(1, 1, 111)[20:]); err != nil {
     72 		t.Fatal(err)
     73 	}
     74 	finish := client.equipment.readyAt
     75 	player.position = [3]float32{961.625, 60, 510.75}
     76 	player.movement.updatedAt = finish
     77 	player.movement.velocity, player.movement.speed = [3]float32{3, 4, 0}, 5
     78 	player.movement.forward, player.movement.strafe, player.movement.turn, player.movement.vertical = 1, 2, 1.5, 6
     79 
     80 	world.advanceEquipment(finish)
     81 
     82 	if player.position != [3]float32{961.625, 60.1, 510.75} || player.movement.velocity != [3]float32{3, 4, 0} || player.movement.speed != 5 {
     83 		t.Fatal("exit placement redirected or stopped vector motion", player.position, player.movement)
     84 	}
     85 	if player.movement.forward != 1 || player.movement.strafe != 2 || player.movement.turn != 1.5 || player.movement.vertical != 0 || !player.movement.falling || player.movement.fallHeight != 60.1 {
     86 		t.Fatal("exit cleared unrelated controls or failed to admit a fall", player.movement)
     87 	}
     88 	forward, _ := bodyAxes(player.rotation)
     89 	if math.Abs(float64(forward[0])-.6) > .000001 || math.Abs(float64(forward[1])-.8) > .000001 || math.Abs(float64(forward[2])) > .000001 {
     90 		t.Fatal("exit must align in the previous flight orientation mode before restoring mode0", forward, player.rotation)
     91 	}
     92 }
     93 
     94 func TestPlayerFlightFailedCommitCannotPublishMovementOrBodyChanges(t *testing.T) {
     95 	for _, removal := range []bool{false, true} {
     96 		t.Run(fmt.Sprint(removal), func(t *testing.T) {
     97 			world := newPlayerFormWorld(t, 117322)
     98 			player, client := world.characters[1], world.clients[1]
     99 			if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
    100 				t.Fatal(err)
    101 			}
    102 			if removal {
    103 				world.advanceEquipment(client.equipment.readyAt)
    104 				if err := world.handleInventory(client, itemMovePacket(1, 1, 111)[20:]); err != nil {
    105 					t.Fatal(err)
    106 				}
    107 			}
    108 			before := *player
    109 			finish := client.equipment.readyAt
    110 			failDatabaseWrites(t, world)
    111 
    112 			world.advanceEquipment(finish)
    113 
    114 			if player.position != before.position || player.rotation != before.rotation || player.movement != before.movement || player.body != before.body || player.inventory.Items[0] != before.inventory.Items[0] {
    115 				t.Fatal("failed equipment commit published flight effects", before.movement, player.movement)
    116 			}
    117 		})
    118 	}
    119 }
    120 
    121 func TestPlayerFlightRemovalLevelsIdleFacingOnTheNextFallFrame(t *testing.T) {
    122 	world := newPlayerFormWorld(t, 117322)
    123 	player, client := world.characters[1], world.clients[1]
    124 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
    125 		t.Fatal(err)
    126 	}
    127 	world.advanceEquipment(client.equipment.readyAt)
    128 	player.position[1] = 60
    129 	player.rotation = [4]float32{-.38268343, 0, 0, .9238795}
    130 	if err := world.handleInventory(client, itemMovePacket(1, 1, 111)[20:]); err != nil {
    131 		t.Fatal(err)
    132 	}
    133 	finish := client.equipment.readyAt
    134 	player.movement.updatedAt = finish
    135 
    136 	world.advanceEquipment(finish)
    137 	exitForward, _ := bodyAxes(player.rotation)
    138 	world.advancePlayerMovement(finish.Add(100 * time.Millisecond))
    139 
    140 	forward, _ := bodyAxes(player.rotation)
    141 	if exitForward[1] < .7 || forward != [3]float32{0, 0, 1} || player.rotation != [4]float32{0, 0, 0, 1} || !player.movement.falling {
    142 		t.Fatal("exit must preserve pitch until the next ground-mode alignment, then level idle facing", exitForward, forward, player.rotation, player.movement)
    143 	}
    144 }
    145 
    146 func TestPlayerFlightReadyAnchorsOwnerAndObserversWithoutAnotherLift(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, player.movement = [3]float32{961.625, 60, 510.75}, playerMovement{}
    154 	observer := &combatTestConn{}
    155 	world.characters[2] = &character{id: 2, playfield: 705, health: 100}
    156 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true}
    157 	owner := client.conn.(*combatTestConn)
    158 	client.target = false
    159 	owner.packets = nil
    160 	client.loading = true
    161 
    162 	if err := world.handleCharacterReady(client); err != nil {
    163 		t.Fatal(err)
    164 	}
    165 	firstOwner, firstObserver := len(owner.packets), len(observer.packets)
    166 	if err := world.handleCharacterReady(client); err != nil {
    167 		t.Fatal(err)
    168 	}
    169 
    170 	if player.position != [3]float32{961.625, 60, 510.75} || player.movement.active() || !player.flying() {
    171 		t.Fatal("restoring worn flight must not apply another live entry lift", player.position, player.movement)
    172 	}
    173 	for _, packets := range [][][]byte{owner.packets[:firstOwner], observer.packets[:firstObserver]} {
    174 		last := packets[len(packets)-1]
    175 		if binary.BigEndian.Uint32(last[20:24]) != 0x54111123 || binary.BigEndian.Uint32(last[36:40]) != 0x94 || math.Float32frombits(binary.BigEndian.Uint32(last[44:48])) != 60 {
    176 			t.Fatalf("OnWear initialization must end with native position correction: %x", last)
    177 		}
    178 	}
    179 	if len(owner.packets) != firstOwner+1 || len(observer.packets) != firstObserver+1 {
    180 		t.Fatal("repeated ready replays flight effects or corrections", len(owner.packets), len(observer.packets))
    181 	}
    182 }
    183 
    184 func TestPlayerFlightOrientationUsesFullVectorMotionAndRetainedDirection(t *testing.T) {
    185 	for _, scenario := range []struct {
    186 		velocity  [3]float32
    187 		direction int32
    188 		forward   [3]float32
    189 	}{
    190 		{[3]float32{0, 3, 4}, 1, [3]float32{0, .6, .8}},
    191 		{[3]float32{0, 3, 4}, -1, [3]float32{0, -.6, -.8}},
    192 		{[3]float32{0, 0, -5}, 1, [3]float32{0, 0, -1}},
    193 		{[3]float32{0, 5, 0}, 1, [3]float32{0, 1, 0}},
    194 		{[3]float32{0, -5, 0}, 1, [3]float32{0, -1, 0}},
    195 		{[3]float32{-3, -4, 0}, -1, [3]float32{.6, .8, 0}},
    196 	} {
    197 		t.Run(fmt.Sprint(scenario.velocity, scenario.direction), func(t *testing.T) {
    198 			world := newPlayerFormWorld(t, 117322)
    199 			player, client := world.characters[1], world.clients[1]
    200 			if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
    201 				t.Fatal(err)
    202 			}
    203 			world.advanceEquipment(client.equipment.readyAt)
    204 			player.position[1] = 100
    205 			player.movement.velocity, player.movement.speed, player.movement.direction = scenario.velocity, 5, scenario.direction
    206 
    207 			world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
    208 
    209 			forward, _ := bodyAxes(player.rotation)
    210 			for axis, expected := range scenario.forward {
    211 				if math.Abs(float64(forward[axis]-expected)) > .000001 || math.IsNaN(float64(forward[axis])) {
    212 					t.Fatal("flight orientation flattened 3D motion or lost reverse facing", forward, scenario.forward, player.rotation)
    213 				}
    214 			}
    215 			if player.movement.velocity != scenario.velocity || player.movement.falling {
    216 				t.Fatal("orientation refresh must preserve vector motion and suppress new falls", player.movement)
    217 			}
    218 		})
    219 	}
    220 }
    221 
    222 func TestPlayerFlightHeldTurnRedirectsThreeDimensionalFacing(t *testing.T) {
    223 	world := newPlayerFormWorld(t, 117322)
    224 	player, client := world.characters[1], world.clients[1]
    225 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
    226 		t.Fatal(err)
    227 	}
    228 	world.advanceEquipment(client.equipment.readyAt)
    229 	player.position[1] = 100
    230 	player.movement.velocity, player.movement.speed, player.movement.turn = [3]float32{0, 3, 4}, 5, 1.5
    231 
    232 	world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
    233 
    234 	forward, _ := bodyAxes(player.rotation)
    235 	if math.Abs(float64(forward[0])-.11955051) > .000001 || math.Abs(float64(forward[1])-.6) > .000001 || math.Abs(float64(forward[2])-.7910169) > .000001 {
    236 		t.Fatal("held flight turn must rotate the 3D vector, then rebuild body facing", forward, player.movement)
    237 	}
    238 }
    239 
    240 func TestPlayerFlightSwimmingRejectsTheAuthoredFlightTransition(t *testing.T) {
    241 	world := newPlayerFormWorld(t, 117322)
    242 	player, client := world.characters[1], world.clients[1]
    243 	player.position = [3]float32{100, 6.5, 25}
    244 	if err := world.handleMovement(client, playerMovementRequest(player, 9)); err != nil {
    245 		t.Fatal(err)
    246 	}
    247 	world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
    248 	if player.movement.components[0] != 4 {
    249 		t.Fatal("fixture must enter actual deep water", player.position, player.movement)
    250 	}
    251 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil || client.equipment == nil {
    252 		t.Fatal("vehicle wear was not scheduled", err)
    253 	}
    254 	finish := client.equipment.readyAt
    255 	player.movement.updatedAt = finish
    256 	before := player.position
    257 
    258 	world.advanceEquipment(finish)
    259 
    260 	if player.position != before || player.flying() || player.movement.components[0] != 4 || !player.movement.deepWater || player.inventory.Items[0].Slot != 1 {
    261 		t.Fatal("worn flight gear must not override a rejected swimming transition", before, player.position, player.movement, player.inventory)
    262 	}
    263 }
    264 
    265 func TestPlayerFlightMovementPacketsCannotInvokeEquipmentTransitions(t *testing.T) {
    266 	world := newPlayerFormWorld(t, 117322)
    267 	player, client := world.characters[1], world.clients[1]
    268 
    269 	for _, action := range []uint32{27, 35, 27} {
    270 		if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil {
    271 			t.Fatal(err)
    272 		}
    273 	}
    274 
    275 	if player.flying() || player.position[1] != 16.41 || player.inventory.Items[0].Slot != 64 || player.movement.components[0] != 2 {
    276 		t.Fatal("untrusted movement events activated an unworn vehicle", player.position, player.movement, player.inventory)
    277 	}
    278 }
    279 
    280 func TestPlayerFlightOwnerCorrectionKeepsTheNativeCompactFlag(t *testing.T) {
    281 	world := newPlayerFormWorld(t, 117322)
    282 	player, client := world.characters[1], world.clients[1]
    283 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
    284 		t.Fatal(err)
    285 	}
    286 	world.advanceEquipment(client.equipment.readyAt)
    287 	player.position[1], player.movement = 60, playerMovement{}
    288 	client.loading = true
    289 	connection := client.conn.(*combatTestConn)
    290 	connection.packets = nil
    291 
    292 	if err := world.handleCharacterReady(client); err != nil {
    293 		t.Fatal(err)
    294 	}
    295 
    296 	packet := connection.packets[len(connection.packets)-1]
    297 	if binary.BigEndian.Uint32(packet[16:20]) != 0x54111123 || packet[28] != 0 || packet[29] != 0x94 || binary.BigEndian.Uint16(packet[32:34]) != 960 {
    298 		t.Fatalf("compact owner correction lost its flag, pass-on value or authoritative altitude: %x", packet)
    299 	}
    300 }
    301 
    302 func TestPlayerFlightLateObserverReceivesAnIdlePositionAnchor(t *testing.T) {
    303 	world := newPlayerFormWorld(t, 117322)
    304 	client := world.clients[1]
    305 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 1)[20:]); err != nil {
    306 		t.Fatal(err)
    307 	}
    308 	world.advanceEquipment(client.equipment.readyAt)
    309 	world.characters[2] = &character{id: 2, playfield: 705, health: 100}
    310 	connection := &combatTestConn{}
    311 	world.clients[2] = &worldClient{id: 2, conn: connection, ready: true, loading: true, players: map[uint32]bool{1: true}}
    312 
    313 	if err := world.handleCharacterReady(world.clients[2]); err != nil {
    314 		t.Fatal(err)
    315 	}
    316 
    317 	var anchors int
    318 	for _, packet := range connection.packets {
    319 		if binary.BigEndian.Uint32(packet[20:24]) == 0x54111123 && binary.BigEndian.Uint32(packet[28:32]) == 1 {
    320 			anchors++
    321 			if binary.BigEndian.Uint32(packet[36:40]) != 20 || math.Float32frombits(binary.BigEndian.Uint32(packet[44:48])) != 16.91 {
    322 				t.Fatalf("idle flight replay carried the wrong pose: %x", packet)
    323 			}
    324 		}
    325 	}
    326 	if anchors != 1 {
    327 		t.Fatal("idle flight initialization lacks its post-OnWear pose", anchors)
    328 	}
    329 }