aorecomp

Anarchy Online classic in the browser

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


server/interest_test.go (5860B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"math"
      7 	"net"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func TestLoginIncludesNearbyNPCsButNotDistantPopulation(t *testing.T) {
     13 	for _, target := range []bool{false, true} {
     14 		t.Run(map[bool]string{false: "original", true: "1491"}[target], func(t *testing.T) {
     15 			data := testNPCData()
     16 			data.Playfields[0].Districts[0].Groups[0].Positions = [][3]float32{{1170, 1000, 1000}, {1180, 0, 1000}, {1181, 0, 1000}}
     17 			useNPCData(t, data)
     18 			world := newTestServer(t)
     19 			player := world.newCharacter()
     20 			player.position = [3]float32{1000, 0, 1000}
     21 			startTestService(t, world)
     22 			client, connection := net.Pipe()
     23 			defer client.Close()
     24 			client.SetDeadline(time.Now().Add(5 * time.Second))
     25 			go func() { defer connection.Close(); world.serveWorldConnection(connection) }()
     26 			cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
     27 			offset := 20
     28 			if target {
     29 				binary.BigEndian.PutUint16(cookie, 0xdfdf)
     30 				offset = 16
     31 			}
     32 
     33 			client.Write(cookie)
     34 			seen := make(map[uint32]bool)
     35 			for {
     36 				packet, err := readPacket(client)
     37 				if err != nil {
     38 					t.Fatal(err)
     39 				}
     40 				if binary.BigEndian.Uint32(packet[offset:]) == 0x271b3a6b {
     41 					seen[binary.BigEndian.Uint32(packet[offset+8:])] = true
     42 				}
     43 				if binary.BigEndian.Uint32(packet[offset:]) == 0x5f52412e {
     44 					break
     45 				}
     46 			}
     47 
     48 			if len(seen) != 3 || !seen[1] || !seen[1000000] || !seen[1000001] {
     49 				t.Fatalf("spawned identities: %v", seen)
     50 			}
     51 			counts := &zoneAdminCounts{}
     52 			callTestZone(t, world, 1, counts)
     53 			if counts.players != 1 || counts.npcs != 3 {
     54 				t.Fatal("distant NPC was removed from the world")
     55 			}
     56 		})
     57 	}
     58 }
     59 
     60 func TestHeldMovementStreamsNPCWithinInterestRange(t *testing.T) {
     61 	data := testNPCData()
     62 	data.Playfields[0].Districts[0].Groups[0].Positions = [][3]float32{{1000, 0, 1181}}
     63 	useNPCData(t, data)
     64 	world := newTestServer(t)
     65 	player := world.newCharacter()
     66 	player.position = [3]float32{1000, 0, 1000}
     67 	movement := targetPacket(npcMovementPacket(player, 1))
     68 	startTestService(t, world)
     69 	client := connectZoningClient(t, world, true)
     70 	client.Write(movement)
     71 	ping := systemPacket(1, nil)
     72 	binary.BigEndian.PutUint16(ping[2:4], 11)
     73 	client.Write(ping)
     74 	if _, err := readPacket(client); err != nil {
     75 		t.Fatal(err)
     76 	}
     77 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
     78 		player := state.characters[1]
     79 		position := player.position
     80 		started := player.movement.updatedAt
     81 		state.advanceNPCs(started.Add(time.Second))
     82 		state.advanceNPCs(started.Add(2 * time.Second))
     83 		if player.position != position {
     84 			return fmt.Errorf("interest prediction changed the authoritative player position")
     85 		}
     86 		return nil
     87 	}))
     88 	packet, err := readPacket(client)
     89 	if err != nil {
     90 		t.Fatal(err)
     91 	}
     92 
     93 	if binary.BigEndian.Uint32(packet[16:]) != 0x271b3a6b || binary.BigEndian.Uint32(packet[24:]) != 1000000 {
     94 		t.Fatalf("entering NPC spawn: %x", packet)
     95 	}
     96 }
     97 
     98 func TestNPCInterestRetainsBoundaryMarginAndRespawns(t *testing.T) {
     99 	data := testNPCData()
    100 	data.Playfields[0].Districts[0].Groups[0].Positions = [][3]float32{{1180, 0, 1000}}
    101 	useNPCData(t, data)
    102 	world := newTestServer(t)
    103 	player := world.newCharacter()
    104 	player.position = [3]float32{1000, 0, 1000}
    105 	startTestService(t, world)
    106 	client := connectZoningClient(t, world, false)
    107 	now := time.Now()
    108 
    109 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
    110 		state.characters[1].position[0] = 980
    111 		state.advanceNPCs(now)
    112 		state.characters[1].position[0] = 979
    113 		state.advanceNPCs(now)
    114 		return nil
    115 	}))
    116 	removed, err := readPacket(client)
    117 	if err != nil {
    118 		t.Fatal(err)
    119 	}
    120 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
    121 		state.characters[1].position[0] = 999
    122 		state.advanceNPCs(now)
    123 		state.characters[1].position[0] = 1000
    124 		state.advanceNPCs(now)
    125 		return nil
    126 	}))
    127 	spawn, err := readPacket(client)
    128 	if err != nil {
    129 		t.Fatal(err)
    130 	}
    131 
    132 	if binary.BigEndian.Uint32(removed[20:]) != 0x36510078 || binary.BigEndian.Uint32(removed[28:]) != 1000000 {
    133 		t.Fatalf("departing NPC removal: %x", removed)
    134 	}
    135 	if binary.BigEndian.Uint32(spawn[20:]) != 0x271b3a6b || binary.BigEndian.Uint32(spawn[28:]) != 1000000 {
    136 		t.Fatalf("returning NPC spawn: %x", spawn)
    137 	}
    138 }
    139 
    140 func TestRoamingNPCStreamsSpawnBeforeCurrentMotion(t *testing.T) {
    141 	data := testNPCData()
    142 	data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point"
    143 	data.Playfields[0].Districts[0].Groups[0].Positions = [][3]float32{{1180.25, 0, 1000}, {2000, 0, 1000}}
    144 	useNPCData(t, data)
    145 	world := newTestServer(t)
    146 	player := world.newCharacter()
    147 	player.position = [3]float32{1000, 0, 1000}
    148 	startTestService(t, world)
    149 	client := connectZoningClient(t, world, false)
    150 	now := time.Now()
    151 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
    152 		state.lastNPCUpdate = now
    153 		for _, npc := range state.mobileNPCs[950] {
    154 			npc.motion.moving = true
    155 			npc.motion.nextMove = now
    156 			npc.motion.destination = npc.position
    157 			npc.motion.destination[0] -= 3
    158 		}
    159 
    160 		state.advanceNPCs(now.Add(250 * time.Millisecond))
    161 		state.advanceNPCs(now.Add(500 * time.Millisecond))
    162 		state.advanceNPCs(now.Add(750 * time.Millisecond))
    163 		if state.characters[1000001].position != [3]float32{2000, 0, 1000} {
    164 			return fmt.Errorf("unobserved NPC advanced its position")
    165 		}
    166 		return nil
    167 	}))
    168 	spawn, err := readPacket(client)
    169 	if err != nil {
    170 		t.Fatal(err)
    171 	}
    172 	motion, err := readPacket(client)
    173 	if err != nil {
    174 		t.Fatal(err)
    175 	}
    176 
    177 	if binary.BigEndian.Uint32(spawn[20:]) != 0x271b3a6b || binary.BigEndian.Uint32(spawn[28:]) != 1000000 {
    178 		t.Fatalf("entering NPC spawn: %x", spawn)
    179 	}
    180 	if binary.BigEndian.Uint32(motion[20:]) != 0x260f3671 || binary.BigEndian.Uint32(motion[28:]) != 1000000 || motion[48] != 22 || math.Abs(float64(math.Float32frombits(binary.BigEndian.Uint32(motion[49:]))-1179.887)) > .001 {
    181 		t.Fatalf("entering NPC motion: %x", motion)
    182 	}
    183 }