aorecomp

Anarchy Online classic in the browser

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


server/player_movement_test.go (18132B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math"
      8 	"net"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func playerMovementRequest(player *character, action uint32) []byte {
     14 	packet := npcMovementPacket(player, action)
     15 	request := append([]byte(nil), packet[20:36]...)
     16 	return append(request, encodeMovement1491(packet[36:76])...)
     17 }
     18 
     19 func TestHeldStrafeAdvancesAuthoritativePoseBeyondTwentySeconds(t *testing.T) {
     20 	world, _ := newEquipmentWorld(t)
     21 	player, client := world.characters[1], world.clients[1]
     22 	player.position, player.rotation = [3]float32{0, 14, 0}, [4]float32{0, 0, 0, 1}
     23 	player.inventory, player.running = playerInventory{}, false
     24 	player.stats.Speed.RunSpeed = 0
     25 	if err := world.handleMovement(client, playerMovementRequest(player, 5)); err != nil {
     26 		t.Fatal(err)
     27 	}
     28 	started := world.characters[client.id].movement.updatedAt
     29 
     30 	for frame := 1; frame <= 250; frame++ {
     31 		world.advancePlayerMovement(started.Add(time.Duration(frame) * 100 * time.Millisecond))
     32 	}
     33 
     34 	if math.Abs(float64(player.position[0])-50) > 0.0002 || player.position[1] != 14 || player.position[2] != 0 {
     35 		t.Fatal("held strafe left authoritative pose stale", player.position)
     36 	}
     37 }
     38 
     39 func TestHeldForwardUsesNativeForceAcceleration(t *testing.T) {
     40 	world, _ := newEquipmentWorld(t)
     41 	player, client := world.characters[1], world.clients[1]
     42 	player.position, player.rotation = [3]float32{0, 14, 0}, [4]float32{0, 0, 0, 1}
     43 	player.inventory, player.running, player.appearance = playerInventory{}, false, 0x315
     44 	if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
     45 		t.Fatal(err)
     46 	}
     47 	started := world.characters[client.id].movement.updatedAt
     48 
     49 	world.advancePlayerMovement(started.Add(100 * time.Millisecond))
     50 
     51 	if math.Abs(float64(player.position[2])-.03) > .000001 {
     52 		t.Fatal("forward input skipped native acceleration", player.position)
     53 	}
     54 }
     55 
     56 func TestPlayerMovementIdleClockResumesWithoutAccumulatedMotion(t *testing.T) {
     57 	world, _ := newEquipmentWorld(t)
     58 	player, client := world.characters[1], world.clients[1]
     59 	player.position, player.rotation = [3]float32{0, 14, 0}, [4]float32{0, 0, 0, 1}
     60 	player.inventory, player.running, player.appearance = playerInventory{}, false, 0x315
     61 	started := time.Now().Add(-time.Second)
     62 	player.movement.updatedAt = started
     63 
     64 	for frame := 1; frame <= 10; frame++ {
     65 		world.advancePlayerMovement(started.Add(time.Duration(frame) * 100 * time.Millisecond))
     66 	}
     67 	if player.position != [3]float32{0, 14, 0} || player.movement.updatedAt != started.Add(time.Second) {
     68 		t.Fatal("idle movement changed position or retained an old clock", player.position, player.movement.updatedAt)
     69 	}
     70 	if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
     71 		t.Fatal(err)
     72 	}
     73 	world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
     74 	if math.Abs(float64(player.position[2])-.03) > .000001 {
     75 		t.Fatal("resumed movement accumulated the idle interval", player.position)
     76 	}
     77 }
     78 
     79 func heldPlayerMovement(player *character, forward float32, at time.Time) playerMovement {
     80 	movement := playerMovement{updatedAt: at}
     81 	movement.prepare(player, at)
     82 	action := uint32(1)
     83 	if forward < 0 {
     84 		action = 3
     85 	}
     86 	movement.action(player, action)
     87 	speed := float32(1.5)
     88 	if player.running || player.flying() {
     89 		speed = player.runningMovementSpeed(forward < 0)
     90 	}
     91 	facing, _ := bodyAxes(player.rotation)
     92 	for axis := range facing {
     93 		movement.velocity[axis] = facing[axis] * speed * forward
     94 	}
     95 	return movement
     96 }
     97 
     98 func TestPlayerMovementComponentTransitionsUseNativeGates(t *testing.T) {
     99 	for _, scenario := range []struct {
    100 		name                  string
    101 		actions               []uint32
    102 		forward, strafe, turn float32
    103 	}{
    104 		{"wrong reverse release", []uint32{1, 4}, 1, 0, 0},
    105 		{"wrong forward release", []uint32{3, 2}, -1, 0, 0},
    106 		{"wrong left release", []uint32{5, 8}, 0, 2, 0},
    107 		{"wrong right release", []uint32{7, 6}, 0, -2, 0},
    108 		{"opposite strafe", []uint32{5, 7}, 0, -2, 0},
    109 		{"repeated strafe", []uint32{5, 5}, 0, 2, 0},
    110 		{"wrong negative turn release", []uint32{9, 12}, 0, 0, 3.5},
    111 		{"wrong positive turn release", []uint32{11, 10}, 0, 0, -3.5},
    112 		{"forward refreshes turn", []uint32{9, 1}, 1, 0, 1.5},
    113 		{"forward stop refreshes turn", []uint32{9, 1, 2}, 0, 0, 3.5},
    114 		{"reverse retains turn", []uint32{9, 3}, -1, 0, 3.5},
    115 		{"reverse stop retains turn", []uint32{1, 9, 3, 4}, 0, 0, 1.5},
    116 	} {
    117 		t.Run(scenario.name, func(t *testing.T) {
    118 			world, _ := newEquipmentWorld(t)
    119 			player, client := world.characters[1], world.clients[1]
    120 			player.inventory = playerInventory{}
    121 			player.stats.Speed.RunSpeed = 0
    122 			player.rotation = [4]float32{0, 0, 0, 1}
    123 
    124 			for _, action := range scenario.actions {
    125 				if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil {
    126 					t.Fatal(err)
    127 				}
    128 			}
    129 
    130 			if player.movement.forward != scenario.forward || player.movement.strafe != scenario.strafe || player.movement.turn != scenario.turn {
    131 				t.Fatal("native input gate/cache mismatch", player.movement)
    132 			}
    133 		})
    134 	}
    135 }
    136 
    137 func TestPlayerMovementStrafeCacheChangesOnlyOnAdmittedStart(t *testing.T) {
    138 	world, _ := newEquipmentWorld(t)
    139 	player, client := world.characters[1], world.clients[1]
    140 	player.inventory = playerInventory{}
    141 	player.stats.Speed.RunSpeed = 0
    142 	if err := world.handleMovement(client, playerMovementRequest(player, 5)); err != nil {
    143 		t.Fatal(err)
    144 	}
    145 	player.stats.Speed.RunSpeed = 100
    146 
    147 	if err := world.handleMovement(client, playerMovementRequest(player, 5)); err != nil {
    148 		t.Fatal(err)
    149 	}
    150 	cached := player.movement.strafe
    151 	if err := world.handleMovement(client, playerMovementRequest(player, 7)); err != nil {
    152 		t.Fatal(err)
    153 	}
    154 
    155 	if cached != 2 || player.movement.strafe != -2.25 {
    156 		t.Fatal("strafe cache repriced without a transition", cached, player.movement.strafe)
    157 	}
    158 }
    159 
    160 func TestPlayerMovementReverseHaltsBeforeApplyingNewForce(t *testing.T) {
    161 	world, _ := newEquipmentWorld(t)
    162 	player, client := world.characters[1], world.clients[1]
    163 	player.inventory, player.running = playerInventory{}, false
    164 	player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1}
    165 	if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
    166 		t.Fatal(err)
    167 	}
    168 	world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
    169 
    170 	if err := world.handleMovement(client, playerMovementRequest(player, 3)); err != nil {
    171 		t.Fatal(err)
    172 	}
    173 	stopped := player.movement.velocity
    174 	before := player.position
    175 	world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
    176 
    177 	if stopped != [3]float32{} || math.Abs(float64(player.position[2]-before[2])+.03) > .000001 || player.rotation != [4]float32{0, 0, 0, 1} {
    178 		t.Fatal("reversal retained velocity or turned the body backward", stopped, before, player.position, player.rotation)
    179 	}
    180 }
    181 
    182 func TestPlayerMovementCheckpointsPoseWithoutTickDatabaseWrites(t *testing.T) {
    183 	world := newCheckpointWorld(t)
    184 	player := world.characters[1]
    185 	client := &worldClient{id: 1, ready: true, target: true, conn: &combatTestConn{}}
    186 	world.clients[1] = client
    187 	player.inventory = playerInventory{}
    188 	player.stats.Speed.RunSpeed = 0
    189 	if err := world.handleMovement(client, playerMovementRequest(player, 5)); err != nil {
    190 		t.Fatal(err)
    191 	}
    192 	started := player.movement.updatedAt
    193 	player.nextPositionSave = started.Add(20 * time.Second)
    194 	auditCheckpointWrites(t, world)
    195 
    196 	for frame := 1; frame <= 200; frame++ {
    197 		world.advancePlayerMovement(started.Add(time.Duration(frame) * 100 * time.Millisecond))
    198 	}
    199 	if writes := checkpointWrites(t, world); len(writes) != 0 {
    200 		t.Fatal("motion churned SQLite", writes)
    201 	}
    202 	if err := world.flushPositions(started.Add(20 * time.Second)); err != nil {
    203 		t.Fatal(err)
    204 	}
    205 	restored := newTestInventoryServer(t).characters[1]
    206 
    207 	if restored.position != player.position || math.Abs(float64(player.position[0])-92) > .001 || checkpointWrites(t, world)["characters/UPDATE"] != 1 || restored.movement.active() {
    208 		t.Fatal("movement checkpoint lost pose or persisted held controls", player.position, restored.position, checkpointWrites(t, world), restored.movement)
    209 	}
    210 }
    211 
    212 func TestPlayerMovementRejectsStalledFrames(t *testing.T) {
    213 	for _, duration := range []time.Duration{-time.Second, 0, 4*time.Second + time.Nanosecond} {
    214 		t.Run(duration.String(), func(t *testing.T) {
    215 			world, _ := newEquipmentWorld(t)
    216 			player, client := world.characters[1], world.clients[1]
    217 			player.inventory = playerInventory{}
    218 			if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
    219 				t.Fatal(err)
    220 			}
    221 			before := player.position
    222 			at := player.movement.updatedAt.Add(duration)
    223 
    224 			world.advancePlayerMovement(at)
    225 
    226 			if player.position != before || player.movement.updatedAt != at {
    227 				t.Fatal("native rejected-frame boundary", player.position, player.movement.updatedAt, at)
    228 			}
    229 		})
    230 	}
    231 }
    232 
    233 func TestPlayerMovementReverseFollowRetainsDirectionThroughArrival(t *testing.T) {
    234 	world, _ := newEquipmentWorld(t)
    235 	player, client := world.characters[1], world.clients[1]
    236 	player.inventory, player.running = playerInventory{}, false
    237 	player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1}
    238 	target := &character{id: 1000000, npc: true, health: 100, playfield: player.playfield, position: [3]float32{0, 0, 30}}
    239 	world.characters[target.id] = target
    240 	client.npcs = map[uint32]bool{target.id: true}
    241 	if err := world.handleMovement(client, playerMovementRequest(player, 3)); err != nil {
    242 		t.Fatal(err)
    243 	}
    244 	if err := world.handlePlayerFollow(client, playerFollowRequest(target.id)); err != nil {
    245 		t.Fatal(err)
    246 	}
    247 	started := player.movement.updatedAt
    248 
    249 	for frame := 1; frame <= 10; frame++ {
    250 		world.advancePlayerMovement(started.Add(time.Duration(frame) * 100 * time.Millisecond))
    251 	}
    252 	moving := player.movement
    253 	bodyZ := 1 - 2*player.rotation[0]*player.rotation[0] - 2*player.rotation[1]*player.rotation[1]
    254 	for frame := 11; frame <= 240; frame++ {
    255 		world.advancePlayerMovement(started.Add(time.Duration(frame) * 100 * time.Millisecond))
    256 	}
    257 
    258 	if moving.forward != -1 || moving.direction != -1 || moving.velocity[2] <= 0 || bodyZ > -.99 || player.movement.forward != 0 || player.movement.direction != 1 || player.followTarget != target.id {
    259 		t.Fatal("reverse follow lost native direction or arrival transition", moving, bodyZ, player.movement, player.followTarget)
    260 	}
    261 }
    262 
    263 func TestPlayerMovementPoseCorrectionRealignsVelocity(t *testing.T) {
    264 	for _, action := range []uint32{1, 3} {
    265 		t.Run(fmt.Sprint(action), func(t *testing.T) {
    266 			world, _ := newEquipmentWorld(t)
    267 			player, client := world.characters[1], world.clients[1]
    268 			player.inventory, player.running = playerInventory{}, false
    269 			player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1}
    270 			if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil {
    271 				t.Fatal(err)
    272 			}
    273 			world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
    274 
    275 			err := world.handleMovement(client, mouseMovementRequest())
    276 
    277 			sign := float32(1)
    278 			if action == 3 {
    279 				sign = -1
    280 			}
    281 			if err != nil || math.Abs(float64(vectorLength(player.movement.velocity))-.3) > .00001 || sign*player.movement.velocity[0] < .24 || sign*player.movement.velocity[2] < .16 {
    282 				t.Fatal("pose correction lost speed or retained the old velocity heading", err, player.movement.velocity)
    283 			}
    284 		})
    285 	}
    286 }
    287 
    288 func TestPlayerMovementSnapshotRetainsCachedControlsAndComponentHistory(t *testing.T) {
    289 	world, _ := newEquipmentWorld(t)
    290 	player, client := world.characters[1], world.clients[1]
    291 	player.inventory, player.running = playerInventory{}, false
    292 	player.stats.Speed.RunSpeed = 0
    293 	for _, action := range []uint32{9, 3, 5, 23} {
    294 		if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil {
    295 			t.Fatal(err)
    296 		}
    297 	}
    298 
    299 	packet := characterPacket(player)
    300 
    301 	offset := 71 + len(player.name) + 20 + 1 + 14
    302 	if binary.BigEndian.Uint32(packet[offset:]) != 42 || !bytes.Equal(packet[offset+16:offset+26], []byte{3, 3, 2, 3, 4, 3, 0, 5, 4, 1}) {
    303 		t.Fatal("snapshot lost movement component history", packet[offset:offset+46])
    304 	}
    305 	for index, word := range []uint32{0xbf800000, 0x40000000, 0x40600000, 0} {
    306 		if binary.BigEndian.Uint32(packet[offset+30+index*4:]) != word {
    307 			t.Fatal("snapshot recomputed a cached control", index, packet[offset:offset+46])
    308 		}
    309 	}
    310 }
    311 
    312 func TestLateReverseFollowInitializesDirectionBeforeController(t *testing.T) {
    313 	world := newCheckpointWorld(t)
    314 	player := world.characters[1]
    315 	player.movement = heldPlayerMovement(player, -1, time.Now())
    316 	player.followTarget = 1000000
    317 	world.characters[1000000] = &character{id: 1000000, npc: true, health: 100, playfield: player.playfield}
    318 	world.clients[1] = &worldClient{id: 1, ready: true, target: true, conn: &combatTestConn{}, npcs: map[uint32]bool{1000000: true}}
    319 	world.newCharacter().playfield = player.playfield
    320 	connection := &combatTestConn{}
    321 	observer := &worldClient{id: 2, players: map[uint32]bool{1: true}, ready: true, loading: true, target: true, conn: connection, npcs: map[uint32]bool{1000000: true}}
    322 	world.clients[2] = observer
    323 
    324 	if err := world.handleCharacterReady(observer); err != nil {
    325 		t.Fatal(err)
    326 	}
    327 
    328 	var actions []uint32
    329 	for _, packet := range connection.packets {
    330 		if binary.BigEndian.Uint32(packet[24:28]) != 1 {
    331 			continue
    332 		}
    333 		switch binary.BigEndian.Uint32(packet[16:20]) {
    334 		case 0x54111123:
    335 			actions = append(actions, binary.BigEndian.Uint32(decodeMovement1491(packet[29:])))
    336 		case 0x260f3671:
    337 			actions = append(actions, 1000000)
    338 		}
    339 	}
    340 	if fmt.Sprint(actions) != "[4 3 20 1000000]" || player.movement.direction != -1 || player.followTarget != 1000000 {
    341 		t.Fatal("reverse direction replay must precede follow without changing authoritative state", actions, player.movement, player.followTarget)
    342 	}
    343 }
    344 
    345 func TestHeldLongitudinalMovementContinuesBeyondTwentySeconds(t *testing.T) {
    346 	for _, scenario := range []struct {
    347 		action uint32
    348 		z      float32
    349 	}{{1, 52.2}, {3, -52.2}} {
    350 		t.Run(fmt.Sprint(scenario.action), func(t *testing.T) {
    351 			world, _ := newEquipmentWorld(t)
    352 			player, client := world.characters[1], world.clients[1]
    353 			player.inventory, player.running = playerInventory{}, false
    354 			player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1}
    355 			if err := world.handleMovement(client, playerMovementRequest(player, scenario.action)); err != nil {
    356 				t.Fatal(err)
    357 			}
    358 			started := player.movement.updatedAt
    359 
    360 			for frame := 1; frame <= 350; frame++ {
    361 				world.advancePlayerMovement(started.Add(time.Duration(frame) * 100 * time.Millisecond))
    362 			}
    363 
    364 			if math.Abs(float64(player.position[2]-scenario.z)) > .001 || player.position[0] != 0 || player.rotation != [4]float32{0, 0, 0, 1} {
    365 				t.Fatal("held input expired or skipped acceleration", player.position, player.rotation)
    366 			}
    367 		})
    368 	}
    369 }
    370 
    371 func TestCombinedHeldMovementKeepsIndependentLateralDisplacement(t *testing.T) {
    372 	world, _ := newEquipmentWorld(t)
    373 	player, client := world.characters[1], world.clients[1]
    374 	player.inventory, player.running = playerInventory{}, false
    375 	player.stats.Speed.RunSpeed = 0
    376 	player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1}
    377 	// Begin forward acceleration at the measured interval, after the lateral input.
    378 	for _, action := range []uint32{5, 1} {
    379 		if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil {
    380 			t.Fatal(err)
    381 		}
    382 	}
    383 	started := player.movement.updatedAt
    384 
    385 	for frame := 1; frame <= 10; frame++ {
    386 		world.advancePlayerMovement(started.Add(time.Duration(frame) * 100 * time.Millisecond))
    387 	}
    388 
    389 	if math.Abs(float64(player.position[0])-2) > .001 || math.Abs(float64(player.position[2])-1.2) > .001 || math.Abs(float64(vectorLength(player.movement.velocity))-1.5) > .00001 {
    390 		t.Fatal("lateral motion was folded into capped longitudinal velocity", player.position, player.movement.velocity)
    391 	}
    392 }
    393 
    394 func TestHeldStationaryTurnAdvancesAuthoritativeHeading(t *testing.T) {
    395 	world, _ := newEquipmentWorld(t)
    396 	player, client := world.characters[1], world.clients[1]
    397 	player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1}
    398 	if err := world.handleMovement(client, playerMovementRequest(player, 9)); err != nil {
    399 		t.Fatal(err)
    400 	}
    401 
    402 	world.advancePlayerMovement(player.movement.updatedAt.Add(time.Second))
    403 
    404 	if player.position != [3]float32{} || math.Abs(float64(player.rotation[1])-.98398596) > .000001 || math.Abs(float64(player.rotation[3])+.17824606) > .000001 {
    405 		t.Fatal("stationary turn did not rotate by 3.5 radians", player.position, player.rotation)
    406 	}
    407 }
    408 
    409 func TestLateObserverSnapshotAdvancesHeadingWithPose(t *testing.T) {
    410 	world := newCheckpointWorld(t)
    411 	player := world.characters[1]
    412 	player.rotation = [4]float32{0, 0, 0, 1}
    413 	player.movement.prepare(player, time.Now().Add(-100*time.Millisecond))
    414 	player.movement.turn = 3.5
    415 	player.movement.components[7], player.movement.components[8] = 5, 4
    416 	world.clients[1] = &worldClient{id: 1, ready: true, target: true, conn: &combatTestConn{}}
    417 	world.newCharacter().playfield = player.playfield
    418 	cookie := systemPacket(0x1b, []byte{0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0})
    419 	binary.BigEndian.PutUint16(cookie, 0xdfdf)
    420 	address, peer := net.Pipe()
    421 	defer address.Close()
    422 	defer peer.Close()
    423 	connection := &combatInterestConn{combatTestConn: combatTestConn{Conn: address}, input: bytes.NewReader(cookie)}
    424 
    425 	if err := serveWorldClient(world, &worldClient{conn: connection, loading: true}); err != nil {
    426 		t.Fatal(err)
    427 	}
    428 
    429 	var snapshots int
    430 	for _, packet := range connection.packets {
    431 		if binary.BigEndian.Uint32(packet[16:20]) != 0x271b3a6b || binary.BigEndian.Uint32(packet[24:28]) != 1 {
    432 			continue
    433 		}
    434 		snapshots++
    435 		// SCFU's fixed prefix ends with compact position then body quaternion.
    436 		y := math.Float32frombits(binary.BigEndian.Uint32(packet[48:52]))
    437 		w := math.Float32frombits(binary.BigEndian.Uint32(packet[56:60]))
    438 		if y < .17 || y > .5 || w < .8 || w > .986 {
    439 			t.Fatal("late actor snapshot retained the old heading", y, w)
    440 		}
    441 	}
    442 	if snapshots != 1 || player.rotation != [4]float32{0, 0, 0, 1} {
    443 		t.Fatal("snapshot mutated authoritative motion or omitted the actor", snapshots, player.rotation)
    444 	}
    445 }