aorecomp

Anarchy Online classic in the browser

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


server/pet_follow_test.go (6517B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"testing"
      6 	"time"
      7 )
      8 
      9 func petFollowRequest(owner uint32) []byte {
     10 	var body []byte
     11 	for _, value := range []uint32{1, 0, 1009, 0, 0} {
     12 		body = binary.BigEndian.AppendUint32(body, value)
     13 	}
     14 	return n3Packet(0x6b333303, 50000, owner, body)
     15 }
     16 
     17 func TestPetFollowMovesItsOwnedEntityWithoutRoutinePersistence(t *testing.T) {
     18 	world, output := newPetSummonWorld(t)
     19 	player, client := world.characters[1], world.clients[1]
     20 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
     21 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     22 	pet := world.characters[1000001]
     23 	start := pet.position
     24 	player.position = [3]float32{10, 0, 0}
     25 	output.packets = nil
     26 	auditCheckpointWrites(t, world)
     27 
     28 	err := world.handlePetCommand(client, petFollowRequest(1)[20:])
     29 	now := time.Now()
     30 	for step := 1; step <= 600; step++ {
     31 		advanceNavigationForTest(t, world, now.Add(time.Duration(step)*time.Second/60), world.advancePlayerMovement)
     32 	}
     33 
     34 	if err != nil || pet.followTarget != 1 || pet.position[0] < start[0]+3 || surfaceDistanceSquared(pet.position, player.position) >= 5 ||
     35 		player.nano != 25 || len(checkpointWrites(t, world)) != 0 {
     36 		t.Fatal("owned pet did not follow without routine persistence", err, pet.followTarget, pet.position)
     37 	}
     38 	follows := 0
     39 	for _, packet := range output.packets {
     40 		if binary.BigEndian.Uint32(packet[16:20]) == 0x260f3671 && binary.BigEndian.Uint32(packet[24:28]) == pet.id {
     41 			follows++
     42 			if binary.BigEndian.Uint32(packet[29:33]) != 50000 || binary.BigEndian.Uint32(packet[33:37]) != 1 {
     43 				t.Fatalf("pet received an unrelated follow target: %x", packet)
     44 			}
     45 		}
     46 	}
     47 	if follows != 1 {
     48 		t.Fatal("follow did not install exactly one native owner target", follows)
     49 	}
     50 }
     51 
     52 func TestPetFollowRejectsUnsupportedOrUnavailableRequests(t *testing.T) {
     53 	for _, scenario := range []string{"foreign actor", "stale client", "loading", "not ready", "owner dead", "pet dead", "zone", "expired", "command", "argument", "selectors", "boolean", "name"} {
     54 		t.Run(scenario, func(t *testing.T) {
     55 			world, output := newPetSummonWorld(t)
     56 			player, client := world.characters[1], world.clients[1]
     57 			world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
     58 			world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     59 			pet := world.characters[1000001]
     60 			request := petFollowRequest(1)[20:]
     61 			switch scenario {
     62 			case "foreign actor":
     63 				binary.BigEndian.PutUint32(request[8:], 2)
     64 			case "stale client":
     65 				world.clients[1] = &worldClient{id: 1}
     66 			case "loading":
     67 				client.loading = true
     68 			case "not ready":
     69 				client.ready = false
     70 			case "owner dead":
     71 				player.health = 0
     72 			case "pet dead":
     73 				pet.health = 0
     74 			case "zone":
     75 				pet.playfield++
     76 			case "expired":
     77 				pet.pet.expiresAt = time.Now().Add(-time.Second)
     78 			case "command":
     79 				binary.BigEndian.PutUint32(request[16:], 14)
     80 			case "argument":
     81 				binary.BigEndian.PutUint32(request[20:], 1000001)
     82 			case "selectors":
     83 				binary.BigEndian.PutUint32(request[24:], 1010)
     84 			case "boolean":
     85 				binary.BigEndian.PutUint32(request[28:], 1)
     86 			case "name":
     87 				binary.BigEndian.PutUint32(request[32:], 1)
     88 				request = append(request, 'x')
     89 			}
     90 			output.packets = nil
     91 
     92 			err := world.handlePetCommand(client, request)
     93 
     94 			if err != nil || pet.followTarget != 0 || pet.motion != nil || len(output.packets) != 0 || player.nano != 25 {
     95 				t.Fatal("rejected command changed the pet or owner", err, pet.followTarget, len(output.packets), player.nano)
     96 			}
     97 		})
     98 	}
     99 }
    100 
    101 func TestPetFollowSuspendsAndResumesWithItsOwner(t *testing.T) {
    102 	world, output := newPetSummonWorld(t)
    103 	player, client := world.characters[1], world.clients[1]
    104 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
    105 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    106 	pet := world.characters[1000001]
    107 	world.handlePetCommand(client, petFollowRequest(1)[20:])
    108 	player.position = [3]float32{10, 0, 0}
    109 	now := pet.movement.updatedAt
    110 	advanceNavigationForTest(t, world, now.Add(250*time.Millisecond), world.advancePlayerMovement)
    111 	start := pet.position
    112 	client.loading = true
    113 	output.packets = nil
    114 
    115 	advanceNavigationForTest(t, world, now.Add(500*time.Millisecond), world.advancePlayerMovement)
    116 	advanceNavigationForTest(t, world, now.Add(time.Second), world.advancePlayerMovement)
    117 	if pet.position != start || !pet.pet.paused || len(output.packets) != 1 ||
    118 		binary.BigEndian.Uint32(output.packets[0][29:33]) != 0 {
    119 		t.Fatal("unavailable owner did not halt the native pet exactly once", pet.position, start, len(output.packets))
    120 	}
    121 	client.loading = false
    122 	advanceNavigationForTest(t, world, now.Add(1250*time.Millisecond), world.advancePlayerMovement)
    123 	pet.pet.expiresAt = now.Add(1500 * time.Millisecond)
    124 	advanceNavigationForTest(t, world, pet.pet.expiresAt, world.advancePlayerMovement)
    125 	stopped := pet.position
    126 	advanceNavigationForTest(t, world, now.Add(2*time.Second), world.advancePlayerMovement)
    127 
    128 	if start == stopped || pet.position != stopped || !pet.pet.paused || pet.followTarget != 1 ||
    129 		pet.movement.velocity != [3]float32{} {
    130 		t.Fatal("pet failed to resume or moved after its deadline", start, stopped, pet.position)
    131 	}
    132 }
    133 
    134 func TestPetFollowInterestReplaysOwnerTargetAfterSnapshot(t *testing.T) {
    135 	world, output := newPetSummonWorld(t)
    136 	client := world.clients[1]
    137 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
    138 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    139 	world.handlePetCommand(client, petFollowRequest(1)[20:])
    140 	delete(client.npcs, 1000001)
    141 	output.packets = nil
    142 
    143 	packets := world.updateNPCInterest(client, time.Now())
    144 	for _, packet := range packets {
    145 		if err := client.send(packet); err != nil {
    146 			t.Fatal(err)
    147 		}
    148 	}
    149 
    150 	spawned, followed := false, 0
    151 	for _, packet := range output.packets {
    152 		if binary.BigEndian.Uint32(packet[24:28]) != 1000001 {
    153 			continue
    154 		}
    155 		if binary.BigEndian.Uint32(packet[16:20]) == 0x271b3a6b {
    156 			spawned = true
    157 		}
    158 		if binary.BigEndian.Uint32(packet[16:20]) == 0x260f3671 {
    159 			followed++
    160 			if !spawned || binary.BigEndian.Uint32(packet[33:37]) != 1 || binary.BigEndian.Uint16(packet[6:8]) != 58 ||
    161 				packet[41] != 23 || binary.BigEndian.Uint32(packet[54:58]) != 0 {
    162 				t.Fatalf("replay did not follow the owner after spawn: %x", packet)
    163 			}
    164 		}
    165 	}
    166 	if followed != 1 {
    167 		t.Fatal("pet interest did not replay exactly one owner controller", followed)
    168 	}
    169 }