aorecomp

Anarchy Online classic in the browser

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


server/npc_pursuit_test.go (9662B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"math"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func newNPCPursuitWorld(t *testing.T) (*server, *combatTestConn, time.Time) {
     13 	t.Helper()
     14 	world, conn := newCombatWorld(t)
     15 	start := time.Now()
     16 	world.characters[1].position = [3]float32{21, 0, 0}
     17 	npc := world.characters[1000000]
     18 	npc.monsterData, npc.level = 17655, 1
     19 	npc.combatProfile = gamedata.NPCCombatProfiles[17655][1]
     20 	npc.motion = &npcMotion{roaming: true, home: npc.position}
     21 	npc.fighting = 1
     22 	npc.nextAttack = start.Add(1750 * time.Millisecond)
     23 	world.fighting[npc.id] = npc
     24 	world.mobileNPCs = map[uint32][]*character{950: {npc}}
     25 	world.lastNPCUpdate = start
     26 	return world, conn, start
     27 }
     28 
     29 func TestNPCPursuitRetainsAttackDeadlineAndOnlyHitsInsideRange(t *testing.T) {
     30 	world, _, start := newNPCPursuitWorld(t)
     31 	deadline := world.characters[1000000].nextAttack
     32 
     33 	for tick := 1; tick <= 8; tick++ {
     34 		now := start.Add(time.Duration(tick) * 250 * time.Millisecond)
     35 		world.advanceNPCs(now)
     36 		world.advanceCombat(now)
     37 	}
     38 	if world.characters[1].health != 100 || world.characters[1000000].nextAttack != deadline || world.characters[1000000].fighting != 1 || world.characters[1000000].position[0] <= 1 || world.characters[1000000].position[0] >= 10 {
     39 		t.Fatal("pursuit hit from outside range or reset the attack deadline")
     40 	}
     41 	tick := 9
     42 	for ; tick <= 40 && world.characters[1].health == 100; tick++ {
     43 		now := start.Add(time.Duration(tick) * 250 * time.Millisecond)
     44 		world.advanceNPCs(now)
     45 		world.advanceCombat(now)
     46 	}
     47 	deadline = world.characters[1000000].nextAttack
     48 	for ; tick <= 50; tick++ {
     49 		world.advanceNPCs(start.Add(time.Duration(tick) * 250 * time.Millisecond))
     50 	}
     51 
     52 	if world.characters[1].health < 92 || world.characters[1].health > 96 || world.characters[1000000].position[0] <= 19 || world.characters[1000000].position[0] >= 21 || world.characters[1000000].motion.moving || world.characters[1000000].running || world.characters[1000000].fighting != 1 || world.characters[1000000].nextAttack != deadline {
     53 		t.Fatalf("arrival did not resume normal combat: player=%+v npc=%+v", world.characters[1], world.characters[1000000])
     54 	}
     55 }
     56 
     57 func TestNPCPursuitPublishesNativeWaypointRouteToBothObservers(t *testing.T) {
     58 	world, first, start := newNPCPursuitWorld(t)
     59 	second := &combatTestConn{}
     60 	world.characters[2] = &character{id: 2, playfield: 950}
     61 	world.clients[2] = &worldClient{id: 2, conn: second, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     62 
     63 	world.advanceNPCs(start.Add(250 * time.Millisecond))
     64 
     65 	if len(first.packets) != 1 || len(second.packets) != 1 || !bytes.Equal(first.packets[0][16:], second.packets[0][16:]) || binary.BigEndian.Uint32(first.packets[0][16:20]) != 0x260f3671 || binary.BigEndian.Uint32(first.packets[0][24:28]) != 1000000 || binary.BigEndian.Uint16(first.packets[0][6:8]) != 82 || !bytes.Equal(first.packets[0][29:42], []byte{0, 0, 0xc3, 0x50, 0, 0x0f, 0x42, 0x40, 0, 0, 0, 0, 23}) || binary.BigEndian.Uint32(first.packets[0][54:58]) != 2 || math.Float32frombits(binary.BigEndian.Uint32(first.packets[0][42:46])) != 1 || math.Float32frombits(binary.BigEndian.Uint32(first.packets[0][70:74])) != 21 {
     66 		t.Fatalf("native route or recipients: %x %x", first.packets, second.packets)
     67 	}
     68 }
     69 
     70 func TestNPCPursuitChangesDirectionForMovingTargetWithoutRestartingAttack(t *testing.T) {
     71 	world, conn, start := newNPCPursuitWorld(t)
     72 	deadline := world.characters[1000000].nextAttack
     73 	world.advanceNPCs(start.Add(250 * time.Millisecond))
     74 	world.characters[1].position = [3]float32{2.125, 0, 20}
     75 
     76 	world.advanceNPCs(start.Add(500 * time.Millisecond))
     77 
     78 	if world.characters[1000000].position[0] <= 1 || world.characters[1000000].position[0] >= 1.05 || world.characters[1000000].position[2] <= 0 || world.characters[1000000].position[2] >= .5 || world.characters[1000000].nextAttack != deadline || len(conn.packets) != 2 || world.characters[1000000].motion.destination != [3]float32{2.125, 0, 20} {
     79 		t.Fatalf("target direction was not refreshed: %+v packets=%x", world.characters[1000000], conn.packets)
     80 	}
     81 }
     82 
     83 func TestNPCPursuitReturnsHomeAfterTargetLossWithoutHealingOrTeleporting(t *testing.T) {
     84 	for _, scenario := range []string{"disconnect", "loading", "death", "zone"} {
     85 		t.Run(scenario, func(t *testing.T) {
     86 			world, conn, start := newNPCPursuitWorld(t)
     87 			for tick := 1; tick <= 4; tick++ {
     88 				world.advanceNPCs(start.Add(time.Duration(tick) * 250 * time.Millisecond))
     89 			}
     90 			world.characters[1000000].health = 50
     91 			position := world.characters[1000000].position
     92 			switch scenario {
     93 			case "disconnect":
     94 				delete(world.clients, 1)
     95 			case "loading":
     96 				world.clients[1].loading = true
     97 			case "death":
     98 				world.characters[1].health = 0
     99 			case "zone":
    100 				world.characters[1].playfield = 705
    101 			}
    102 			conn.packets = nil
    103 
    104 			world.advanceNPCs(start.Add(1250 * time.Millisecond))
    105 			if math.Abs(float64(world.characters[1000000].position[0]-position[0])) > 1.125 || !world.characters[1000000].motion.returning || world.characters[1000000].fighting != 0 || world.characters[1000000].health != 50 {
    106 				t.Fatal("target loss teleported, healed, or retained the old fight")
    107 			}
    108 			finished := start.Add(1250 * time.Millisecond)
    109 			for tick := 6; tick <= 40 && world.characters[1000000].motion.returning; tick++ {
    110 				finished = start.Add(time.Duration(tick) * 250 * time.Millisecond)
    111 				world.advanceNPCs(finished)
    112 			}
    113 			world.advanceRegeneration(finished)
    114 
    115 			if math.Abs(float64(world.characters[1000000].position[0]-1)) >= 1 || world.characters[1000000].motion.returning || world.characters[1000000].motion.moving || world.characters[1000000].running || world.characters[1000000].health != 50 || len(world.fighting) != 0 || !world.characters[1000000].motion.nextMove.After(finished) {
    116 				t.Fatalf("return did not finish cleanly: %+v", world.characters[1000000])
    117 			}
    118 		})
    119 	}
    120 }
    121 
    122 func TestNPCPursuitCanBeEngagedAgainDuringReturn(t *testing.T) {
    123 	world, conn, start := newNPCPursuitWorld(t)
    124 	world.advanceNPCs(start.Add(250 * time.Millisecond))
    125 	world.advanceNPCs(start.Add(500 * time.Millisecond))
    126 	world.clients[1].loading = true
    127 	world.advanceNPCs(start.Add(750 * time.Millisecond))
    128 	world.clients[1].loading = false
    129 	world.characters[1].position = [3]float32{3, 0, 0}
    130 	conn.packets = nil
    131 
    132 	err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
    133 	hit := world.characters[1].nextAttack
    134 	world.advanceCombat(hit)
    135 
    136 	if err != nil || world.characters[1000000].motion.returning || world.characters[1000000].motion.moving || world.characters[1000000].fighting != 1 || world.characters[1000000].health == 100 || len(world.fighting) != 2 {
    137 		t.Fatalf("new encounter retained a return route: %+v err=%v", world.characters[1000000], err)
    138 	}
    139 }
    140 
    141 func TestNPCPursuitDeathClearsRouteAndNeverReturnsCorpse(t *testing.T) {
    142 	for _, scenario := range []string{"pursuing", "returning"} {
    143 		t.Run(scenario, func(t *testing.T) {
    144 			world, conn, start := newNPCPursuitWorld(t)
    145 			world.advanceNPCs(start.Add(250 * time.Millisecond))
    146 			world.advanceNPCs(start.Add(500 * time.Millisecond))
    147 			if scenario == "returning" {
    148 				world.clients[1].loading = true
    149 				world.advanceNPCs(start.Add(750 * time.Millisecond))
    150 				world.clients[1].loading = false
    151 			}
    152 			world.characters[1000000].health = 1
    153 			world.characters[1].position = [3]float32{3, 0, 0}
    154 			world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
    155 			hit := world.characters[1].nextAttack
    156 
    157 			world.advanceCombat(hit)
    158 			position := world.characters[1000000].position
    159 			conn.packets = nil
    160 			world.advanceNPCs(hit.Add(time.Second))
    161 
    162 			if world.characters[1000000].health != 0 || world.characters[1000000].position != position || world.characters[1000000].motion.returning || world.characters[1000000].motion.moving || len(conn.packets) != 0 {
    163 				t.Fatalf("dead creature moved or retained pursuit: %+v packets=%x", world.characters[1000000], conn.packets)
    164 			}
    165 		})
    166 	}
    167 }
    168 
    169 func TestNPCPursuitJoiningObserverGetsSpawnBeforeRouteAndAttack(t *testing.T) {
    170 	world, _, start := newNPCPursuitWorld(t)
    171 	world.advanceNPCs(start.Add(250 * time.Millisecond))
    172 	observer := &combatTestConn{}
    173 	world.characters[2] = &character{id: 2, playfield: 950}
    174 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: make(map[uint32]bool)}
    175 
    176 	world.advanceNPCs(start.Add(500 * time.Millisecond))
    177 
    178 	if len(observer.packets) != 4 || binary.BigEndian.Uint32(observer.packets[0][16:20]) != 0x271b3a6b || binary.BigEndian.Uint32(observer.packets[1][16:20]) != 0x1d3c0f1c || binary.BigEndian.Uint32(observer.packets[2][16:20]) != 0x260f3671 || binary.BigEndian.Uint32(observer.packets[3][16:20]) != 0x28494070 || math.Abs(float64(math.Float32frombits(binary.BigEndian.Uint32(observer.packets[2][42:46]))-1.3)) > .0001 {
    179 		t.Fatalf("join order or current route: %x", observer.packets)
    180 	}
    181 }
    182 
    183 func TestNPCPursuitExcludesUnadmittedProfiles(t *testing.T) {
    184 	for _, scenario := range []string{"species", "profile"} {
    185 		t.Run(scenario, func(t *testing.T) {
    186 			world, _, start := newNPCPursuitWorld(t)
    187 			switch scenario {
    188 			case "species":
    189 				world.characters[1000000].monsterData = 17657
    190 				world.characters[1000000].combatProfile = nil
    191 			case "profile":
    192 				world.characters[1000000].combatProfile = nil
    193 			}
    194 
    195 			world.advanceNPCs(start.Add(250 * time.Millisecond))
    196 			world.advanceCombat(start.Add(250 * time.Millisecond))
    197 
    198 			if world.characters[1000000].position != [3]float32{1, 0, 0} || world.characters[1000000].fighting != 0 || world.characters[1000000].motion.moving {
    199 				t.Fatalf("unverified profile acquired pursuit: %+v", world.characters[1000000])
    200 			}
    201 		})
    202 	}
    203 }