server/pet_wait_test.go (3771B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "testing" 6 "time" 7 ) 8 9 func TestPetWaitHoldsItsCommandedPositionUntilFollow(t *testing.T) { 10 for _, scenario := range []string{"idle", "moving"} { 11 t.Run(scenario, func(t *testing.T) { 12 world, output := newPetSummonWorld(t) 13 player, client := world.characters[1], world.clients[1] 14 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:]) 15 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 16 pet := world.characters[1000001] 17 player.position = [3]float32{20, 0, 0} 18 now := time.Now() 19 if scenario == "moving" { 20 world.handlePetCommand(client, petFollowRequest(1)[20:]) 21 advanceNavigationForTest(t, world, now.Add(time.Second), world.advancePlayerMovement) 22 if pet.movement.speed == 0 { 23 t.Fatal("fixture pet must be moving before wait") 24 } 25 } 26 start := pet.position 27 request := petFollowRequest(1)[20:] 28 binary.BigEndian.PutUint32(request[16:], 4) 29 output.packets = nil 30 auditCheckpointWrites(t, world) 31 32 err := world.handlePetCommand(client, request) 33 player.position = [3]float32{30, 0, 0} 34 for tick := 1; tick <= 100; tick++ { 35 advanceNavigationForTest(t, world, now.Add(time.Second+time.Duration(tick)*100*time.Millisecond), world.advancePlayerMovement) 36 } 37 38 if err != nil || pet.followTarget != 0 || pet.motion != nil || pet.position != start || 39 pet.movement.velocity != [3]float32{} || pet.movement.speed != 0 || player.nano != 25 || 40 len(checkpointWrites(t, world)) != 0 || len(output.packets) != 1 { 41 t.Fatal("wait did not hold without another cost or tick writes", err, pet.followTarget, start, pet.position, len(output.packets)) 42 } 43 packet := output.packets[0] 44 if binary.BigEndian.Uint32(packet[16:20]) != 0x260f3671 || binary.BigEndian.Uint32(packet[24:28]) != pet.id || 45 binary.BigEndian.Uint32(packet[29:33]) != 0 || binary.BigEndian.Uint32(packet[33:37]) != 0 || packet[41] != 19 { 46 t.Fatalf("wait did not clear the native target and halt: %x", packet) 47 } 48 if err := world.handlePetCommand(client, petFollowRequest(1)[20:]); err != nil { 49 t.Fatal(err) 50 } 51 advanceNavigationForTest(t, world, now.Add(12*time.Second), world.advancePlayerMovement) 52 if pet.followTarget != 1 || pet.position == start || player.nano != 25 { 53 t.Fatal("valid follow did not replace wait", pet.followTarget, pet.position) 54 } 55 }) 56 } 57 } 58 59 func TestPetOwnerReadyReplaysMembershipAfterActorLoading(t *testing.T) { 60 world, output := newPetSummonWorld(t) 61 client := world.clients[1] 62 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:]) 63 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 64 observer := &combatTestConn{} 65 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 66 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: observer, npcs: map[uint32]bool{1000001: true}} 67 client.loading = true 68 output.packets = nil 69 70 err := world.handleCharacterReady(client) 71 72 if err != nil { 73 t.Fatal(err) 74 } 75 for _, packets := range [][][]byte{output.packets, observer.packets} { 76 ready, linked := false, false 77 for _, packet := range packets { 78 key := binary.BigEndian.Uint32(packet[16:20]) 79 if key == 0x570c2039 { 80 ready = true 81 } 82 if key == 0x194e4f76 { 83 if !ready || binary.BigEndian.Uint32(packet[24:28]) != 1 || binary.BigEndian.Uint32(packet[33:37]) != 1000001 { 84 t.Fatalf("pet membership did not follow actor readiness: %x", packet) 85 } 86 linked = true 87 } else if linked { 88 t.Fatalf("actor replay continued after final pet membership: %x", packet) 89 } 90 } 91 if !linked { 92 t.Fatal("ready actor did not replay its pet membership") 93 } 94 } 95 }