aorecomp

Anarchy Online classic in the browser

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


server/zoning_test.go (9757B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"math"
      8 	"net"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func connectZoningClient(t *testing.T, world *server, target bool) net.Conn {
     14 	t.Helper()
     15 	client, connection := net.Pipe()
     16 	t.Cleanup(func() { client.Close() })
     17 	client.SetDeadline(time.Now().Add(5 * time.Second))
     18 	go func() { defer connection.Close(); world.serveWorldConnection(connection) }()
     19 	cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
     20 	if target {
     21 		binary.BigEndian.PutUint16(cookie, 0xdfdf)
     22 	}
     23 	client.Write(cookie)
     24 	for {
     25 		packet, err := readPacket(client)
     26 		if err != nil {
     27 			t.Fatal(err)
     28 		}
     29 		offset := 20
     30 		if target {
     31 			offset = 16
     32 		}
     33 		if binary.BigEndian.Uint32(packet[offset:]) == 0x5f52412e {
     34 			break
     35 		}
     36 	}
     37 	ready := n3Packet(0x570c2039, 50000, 1, nil)
     38 	if target {
     39 		ready = targetPacket(ready)
     40 	}
     41 	client.Write(ready)
     42 	if _, err := readPacket(client); err != nil {
     43 		t.Fatal(err)
     44 	}
     45 	// A ping ensures the CharacterReady notification has been processed.
     46 	ping := systemPacket(1, nil)
     47 	binary.BigEndian.PutUint16(ping[2:4], 11)
     48 	client.Write(ping)
     49 	if _, err := readPacket(client); err != nil {
     50 		t.Fatal(err)
     51 	}
     52 	return client
     53 }
     54 
     55 func TestShopDoorReturnsToItsOriginalEntrance(t *testing.T) {
     56 	for _, target := range []bool{false, true} {
     57 		t.Run(map[bool]string{false: "11.0", true: "14.9.1"}[target], func(t *testing.T) {
     58 			world := newTestServer(t)
     59 			world.characters = make(map[uint32]*character)
     60 			player := world.newCharacter()
     61 			player.playfield = 705
     62 			player.position = [3]float32{765.0848, 19.1349, 516.5}
     63 			movement := npcMovementPacket(player, 1)
     64 			shutdown := startTestService(t, world)
     65 			client := connectZoningClient(t, world, target)
     66 			if target {
     67 				movement = targetPacket(movement)
     68 			}
     69 			client.Write(movement)
     70 			teleport, err := readPacket(client)
     71 			if err != nil {
     72 				t.Fatal(err)
     73 			}
     74 			redirect, err := readPacket(client)
     75 			if err != nil {
     76 				t.Fatal(err)
     77 			}
     78 			restarted := newTestInventoryServer(t)
     79 			arrived := restarted.characters[1].userData()
     80 
     81 			offset := 20
     82 			if target {
     83 				offset = 16
     84 			}
     85 			if binary.BigEndian.Uint32(teleport[offset:]) != 0x43197d22 || binary.BigEndian.Uint32(redirect[16:]) != 0x3c {
     86 				t.Fatalf("missing native zone sequence: %x %x", teleport, redirect)
     87 			}
     88 			if arrived.Playfield != 1183 || math.Abs(float64(arrived.Position[2]-118.50997)) > 0.001 || arrived.ReturnDoor == nil || *arrived.ReturnDoor != (doorOrigin{705, 0xc01102c1}) {
     89 				t.Fatalf("wrong shop arrival or persisted entrance: %+v", arrived)
     90 			}
     91 
     92 			client.Close()
     93 			client = connectZoningClient(t, world, target)
     94 			movementPlayer := testPlayerData(t, world, 1).character()
     95 			movementPlayer.position = [3]float32{187.0007, 5, 116.01}
     96 			movement = npcMovementPacket(movementPlayer, 1)
     97 			if target {
     98 				movement = targetPacket(movement)
     99 			}
    100 			client.Write(movement)
    101 			readPacket(client)
    102 			readPacket(client)
    103 			if err := shutdown(); err != nil {
    104 				t.Fatal(err)
    105 			}
    106 			restarted = newTestInventoryServer(t)
    107 			returned := restarted.characters[1].userData()
    108 
    109 			if returned.Playfield != 705 || returned.ReturnDoor != nil || math.Abs(float64(returned.Position[2]-514.007)) > 0.001 {
    110 				t.Fatalf("exit did not return to the same entrance: %+v", returned)
    111 			}
    112 		})
    113 	}
    114 }
    115 
    116 func TestHeldWalkingCrossesDoorWithoutAnotherMovementPacket(t *testing.T) {
    117 	world := newTestServer(t)
    118 	world.characters = make(map[uint32]*character)
    119 	player := world.newCharacter()
    120 	player.playfield = 705
    121 	player.position = [3]float32{765.0848, 19.1349, 511.5}
    122 	player.rotation = [4]float32{0, 0, 0, 1}
    123 	movement := targetPacket(npcMovementPacket(player, 1))
    124 	startTestService(t, world)
    125 	client := connectZoningClient(t, world, true)
    126 	client.Write(movement)
    127 	ping := systemPacket(1, nil)
    128 	binary.BigEndian.PutUint16(ping[2:4], 11)
    129 	client.Write(ping)
    130 	if _, err := readPacket(client); err != nil {
    131 		t.Fatal(err)
    132 	}
    133 	opening, err := readPacket(client)
    134 	if err != nil || !bytes.Equal(opening[16:], targetPacket(doorStatusPacket(0xc01102c1, false, true, true))[16:]) {
    135 		t.Fatal("approach did not open the authored shop door before zoning", err)
    136 	}
    137 	// Observe the transition among ordinary simulation updates while walking.
    138 	var transition []uint32
    139 	for len(transition) < 2 {
    140 		packet, err := readPacket(client)
    141 		if err != nil {
    142 			t.Fatal(err, "door pose", testPlayerData(t, world, 1).Position, "transition", transition)
    143 		}
    144 		key := binary.BigEndian.Uint32(packet[16:])
    145 		if key == 0x43197d22 || key == 0x3c {
    146 			transition = append(transition, key)
    147 		}
    148 	}
    149 	arrived := testPlayerData(t, world, 1)
    150 	if transition[0] != 0x43197d22 || transition[1] != 0x3c || arrived.Playfield != 1183 {
    151 		t.Fatal("held forward movement did not enter the mapped shop", transition, arrived)
    152 	}
    153 }
    154 
    155 func TestOmniCityPortalMapsToItsBoundaryExit(t *testing.T) {
    156 	world := newTestServer(t)
    157 	world.characters = make(map[uint32]*character)
    158 	player := world.newCharacter()
    159 	player.playfield = 705
    160 	player.position = [3]float32{1025, 16.4, 509}
    161 	movement := targetPacket(npcMovementPacket(player, 20))
    162 	startTestService(t, world)
    163 	client := connectZoningClient(t, world, true)
    164 
    165 	client.Write(movement)
    166 	teleport, err := readPacket(client)
    167 	if err != nil {
    168 		t.Fatal(err)
    169 	}
    170 	readPacket(client)
    171 
    172 	arrived := testPlayerData(t, world, 1)
    173 	if binary.BigEndian.Uint32(teleport[16:]) != 0x43197d22 || arrived.Playfield != 716 || arrived.Position[0] == gamedata.Playfields[716].Spawn[0] {
    174 		t.Fatal("city portal did not use the paired boundary destination")
    175 	}
    176 }
    177 
    178 func TestOmniBoundaryArrivalClearsTheGround(t *testing.T) {
    179 	for _, target := range []bool{false, true} {
    180 		for _, crossing := range []struct {
    181 			name        string
    182 			playfield   uint32
    183 			position    [3]float32
    184 			destination uint32
    185 			height      float32
    186 		}{
    187 			{"city-to-forest", 705, [3]float32{1025, 16.9, 509}, 716, 10.3},
    188 			{"forest-to-city", 716, [3]float32{209.625, 10.3, 2306}, 705, 16.9},
    189 		} {
    190 			t.Run(map[bool]string{false: "11.0", true: "14.9.1"}[target]+"/"+crossing.name, func(t *testing.T) {
    191 				world := newTestServer(t)
    192 				world.characters = make(map[uint32]*character)
    193 				player := world.newCharacter()
    194 				player.playfield, player.position = crossing.playfield, crossing.position
    195 				movement := npcMovementPacket(player, 20)
    196 				startTestService(t, world)
    197 				client := connectZoningClient(t, world, target)
    198 				if target {
    199 					movement = targetPacket(movement)
    200 				}
    201 
    202 				client.Write(movement)
    203 				if _, err := readPacket(client); err != nil {
    204 					t.Fatal(err)
    205 				}
    206 				if _, err := readPacket(client); err != nil {
    207 					t.Fatal(err)
    208 				}
    209 				arrived := testPlayerData(t, world, 1)
    210 
    211 				if arrived.Playfield != crossing.destination || math.Abs(float64(arrived.Position[1]-crossing.height)) > 0.001 {
    212 					t.Fatalf("boundary arrival intersects terrain: playfield %d position %v", arrived.Playfield, arrived.Position)
    213 				}
    214 			})
    215 		}
    216 	}
    217 }
    218 
    219 func TestLookingAtDoorAndUnassignedMissionDoNotZone(t *testing.T) {
    220 	world := newTestServer(t)
    221 	world.characters = make(map[uint32]*character)
    222 	player := world.newCharacter()
    223 	player.playfield = 705
    224 	player.position = [3]float32{765.0848, 19.1349, 516.5}
    225 	startTestService(t, world)
    226 	client := connectZoningClient(t, world, true)
    227 	look := binary.BigEndian.AppendUint32(nil, 51016)
    228 	look = binary.BigEndian.AppendUint32(look, 0xc01102c1)
    229 	look = binary.BigEndian.AppendUint32(look, 0)
    230 	ping := systemPacket(1, nil)
    231 	binary.BigEndian.PutUint16(ping[2:4], 11)
    232 
    233 	client.Write(targetPacket(n3Packet(0x2252445f, 50000, 1, look)))
    234 	client.Write(ping)
    235 	reply, err := readPacket(client)
    236 	if err != nil {
    237 		t.Fatal(err)
    238 	}
    239 
    240 	if binary.BigEndian.Uint16(reply[2:]) != 11 || testPlayerData(t, world, 1).Playfield != 705 {
    241 		t.Fatal("selecting a door incorrectly triggered zoning")
    242 	}
    243 
    244 	movementPlayer := testPlayerData(t, world, 1).character()
    245 	for _, door := range world.zoneLinks["target"][705].Doors {
    246 		if door.ID == 0xc00402c1 {
    247 			movementPlayer.position = door.Position
    248 		}
    249 	}
    250 
    251 	client.Write(targetPacket(npcMovementPacket(movementPlayer, 2)))
    252 	client.Write(ping)
    253 	reply, err = readPacket(client)
    254 	if err != nil {
    255 		t.Fatal(err)
    256 	}
    257 
    258 	if binary.BigEndian.Uint16(reply[2:]) != 11 || testPlayerData(t, world, 1).Playfield != 705 {
    259 		t.Fatal("unassigned mission entrance incorrectly triggered zoning")
    260 	}
    261 }
    262 
    263 func TestMovementBeforeCharacterReadyPreservesZoneArrival(t *testing.T) {
    264 	world := newTestServer(t)
    265 	world.characters = make(map[uint32]*character)
    266 	player := world.newCharacter()
    267 	player.playfield = 716
    268 	player.position = [3]float32{726, 9.8, 2304}
    269 	stale := *player
    270 	shutdown := startTestService(t, world)
    271 	client, connection := net.Pipe()
    272 	t.Cleanup(func() { client.Close() })
    273 	client.SetDeadline(time.Now().Add(5 * time.Second))
    274 	go func() { defer connection.Close(); world.serveWorldConnection(connection) }()
    275 	client.Write(systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}))
    276 	readWorldUpdates(t, client)
    277 	stale.position = [3]float32{1025, 16.4, 509}
    278 	ping := systemPacket(1, nil)
    279 	binary.BigEndian.PutUint16(ping[2:4], 11)
    280 
    281 	client.Write(npcMovementPacket(&stale, 20))
    282 	client.Write(ping)
    283 	readPacket(client)
    284 
    285 	arrival := testPlayerData(t, world, 1).Position
    286 	if arrival != [3]float32{726, 9.8, 2304} {
    287 		t.Fatalf("loading movement overwrote arrival: %v", arrival)
    288 	}
    289 
    290 	client.Write(n3Packet(0x570c2039, 50000, 1, nil))
    291 	if _, err := readPacket(client); err != nil {
    292 		t.Fatal(err)
    293 	}
    294 	stale.position = [3]float32{727, 9.8, 2304}
    295 	client.Write(npcMovementPacket(&stale, 2))
    296 	client.Write(ping)
    297 	readPacket(client)
    298 	if err := shutdown(); err != nil {
    299 		t.Fatal(err)
    300 	}
    301 	restarted := newTestInventoryServer(t)
    302 
    303 	if restarted.characters[1].position != [3]float32{727, 9.8, 2304} {
    304 		t.Fatal("movement after CharacterReady was not persisted")
    305 	}
    306 }