aorecomp

Anarchy Online classic in the browser

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


server/movement_yaw_test.go (5738B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"math"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestMovementYawOffsetUsesNativeLeftMultiplicationAndWrap(t *testing.T) {
     12 	for _, scenario := range []struct {
     13 		name               string
     14 		offset             float32
     15 		rotation, expected [4]float32
     16 	}{
     17 		{"zero", 0, [4]float32{0, 0, 0, 1}, [4]float32{0, 0, 0, 1}},
     18 		{"positive", 1, [4]float32{0, 0, 0, 1}, [4]float32{0, .47942555, 0, .87758255}},
     19 		{"captured camera offset", .1875, [4]float32{0, 0, 0, 1}, [4]float32{0, .09361273, 0, .9956087}},
     20 		{"negative wrap", -1, [4]float32{0, 0, 0, 1}, [4]float32{0, .47942546, 0, -.8775826}},
     21 		{"positive wrap", 7, [4]float32{0, 0, 0, 1}, [4]float32{0, .3507832, 0, .9364567}},
     22 		{"tilted body", 1, [4]float32{1, 0, 0, 0}, [4]float32{.87758255, 0, -.47942555, 0}},
     23 	} {
     24 		t.Run(scenario.name, func(t *testing.T) {
     25 			world, _ := newEquipmentWorld(t)
     26 			player, client := world.characters[1], world.clients[1]
     27 			player.rotation = scenario.rotation
     28 			request := playerMovementRequest(player, 20)
     29 			binary.BigEndian.PutUint16(request[len(request)-2:], uint16(int16(scenario.offset*2048)))
     30 
     31 			if err := world.handleMovement(client, request); err != nil {
     32 				t.Fatal(err)
     33 			}
     34 
     35 			for axis, expected := range scenario.expected {
     36 				if math.Abs(float64(player.rotation[axis]-expected)) > .000001 {
     37 					t.Fatal("movement yaw lost native composition or quaternion sign", player.rotation, scenario.expected)
     38 				}
     39 			}
     40 		})
     41 	}
     42 }
     43 
     44 func TestMovementYawOffsetPreservesSpeedAndReverseDirection(t *testing.T) {
     45 	for _, action := range []uint32{1, 3} {
     46 		t.Run(fmt.Sprint(action), func(t *testing.T) {
     47 			world, _ := newEquipmentWorld(t)
     48 			player, client := world.characters[1], world.clients[1]
     49 			player.inventory, player.rotation, player.running = playerInventory{}, [4]float32{0, 0, 0, 1}, false
     50 			if err := world.handleMovement(client, playerMovementRequest(player, action)); err != nil {
     51 				t.Fatal(err)
     52 			}
     53 			world.advancePlayerMovement(player.movement.updatedAt.Add(100 * time.Millisecond))
     54 			request := playerMovementRequest(player, 20)
     55 			binary.BigEndian.PutUint16(request[len(request)-2:], 2048)
     56 
     57 			if err := world.handleMovement(client, request); err != nil {
     58 				t.Fatal(err)
     59 			}
     60 
     61 			sign := float32(1)
     62 			if action == 3 {
     63 				sign = -1
     64 			}
     65 			if player.movement.direction != int32(sign) || math.Abs(float64(player.movement.speed)-.3) > .00001 || math.Abs(float64(player.movement.velocity[0]-sign*.2524413)) > .00001 || math.Abs(float64(player.movement.velocity[2]-sign*.1620907)) > .00001 {
     66 				t.Fatal("yaw placement must reorient velocity without changing retained speed or direction", player.movement)
     67 			}
     68 		})
     69 	}
     70 }
     71 
     72 func TestMovementYawOffsetRelayKeepsTheOriginalPoseAndScalar(t *testing.T) {
     73 	world, owner := newEquipmentWorld(t)
     74 	player, client := world.characters[1], world.clients[1]
     75 	player.rotation = [4]float32{0, 0, 0, 1}
     76 	observer := &combatTestConn{}
     77 	world.characters[2] = &character{id: 2, playfield: player.playfield, health: 100}
     78 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer}
     79 	request := playerMovementRequest(player, 20)
     80 	binary.BigEndian.PutUint16(request[len(request)-2:], 2048)
     81 
     82 	if err := world.handleMovement(client, request); err != nil {
     83 		t.Fatal(err)
     84 	}
     85 
     86 	if len(owner.packets) != 0 || len(observer.packets) != 1 {
     87 		t.Fatal("pose must relay only to the observer", len(owner.packets), len(observer.packets))
     88 	}
     89 	packet := observer.packets[0]
     90 	body := decodeMovement1491(packet[29:])
     91 	if binary.BigEndian.Uint32(packet[16:20]) != 0x54111123 || packet[28] != 0 || math.Float32frombits(binary.BigEndian.Uint32(body[32:36])) != 1 || math.Float32frombits(binary.BigEndian.Uint32(body[28:32])) != 1 || binary.BigEndian.Uint64(body[16:24]) != 0 || binary.BigEndian.Uint32(body[24:28]) != 0 {
     92 		t.Fatal("relay must preserve the raw quaternion and scalar so observers compose yaw once", packet)
     93 	}
     94 }
     95 
     96 func TestMovementPoseRecordsIncomingHeightBeforeStartingToFall(t *testing.T) {
     97 	world := newPlayerCollisionWorld(t)
     98 	player, client := world.characters[1], world.clients[1]
     99 	pose := *player
    100 	pose.position[1] = 30
    101 	request := playerMovementRequest(&pose, 20)
    102 
    103 	if err := world.handleMovement(client, request); err != nil {
    104 		t.Fatal(err)
    105 	}
    106 
    107 	if !player.movement.falling || player.movement.fallHeight != 30 || player.movement.previousPosition[1] != 30 {
    108 		t.Fatal("placement must record the incoming position before the falling callback", player.position, player.movement)
    109 	}
    110 }
    111 
    112 func TestMouseMovementDoesNotInterpretItsOpaqueFloatsAsYaw(t *testing.T) {
    113 	world, _ := newEquipmentWorld(t)
    114 	player, client := world.characters[1], world.clients[1]
    115 	request := mouseMovementRequest()
    116 	binary.BigEndian.PutUint32(request[31:35], math.Float32bits(1))
    117 
    118 	if err := world.handleMovement(client, request); err != nil {
    119 		t.Fatal(err)
    120 	}
    121 
    122 	if math.Abs(float64(player.rotation[1])-.47255) > .0001 || math.Abs(float64(player.rotation[3])-.8813) > .0001 {
    123 		t.Fatal("MouseMove must pass zero yaw independently of its opaque fields", player.rotation)
    124 	}
    125 }
    126 
    127 func TestMovementPoseReorientsVelocityAfterPositionOnlyLanding(t *testing.T) {
    128 	world := newPlayerCollisionWorld(t)
    129 	player, client := world.characters[1], world.clients[1]
    130 	player.rotation = [4]float32{0, 0, 0, 1}
    131 	player.movement.velocity = [3]float32{0, -1, 1}
    132 	player.movement.falling = true
    133 	request := playerMovementRequest(player, 20)
    134 
    135 	if err := world.handleMovement(client, request); err != nil {
    136 		t.Fatal(err)
    137 	}
    138 
    139 	if player.movement.velocity != [3]float32{0, 0, 1} || player.movement.speed != 1 || player.movement.falling {
    140 		t.Fatal("position-only placement must land before reorienting the remaining horizontal velocity", player.movement)
    141 	}
    142 }