server/npc_shared_pursuit_test.go (8257B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "fmt" 6 "math" 7 "testing" 8 "time" 9 ) 10 11 func TestAdmittedNPCProfilesSharePursuitAndReturn(t *testing.T) { 12 for _, profile := range []struct { 13 monster uint32 14 level uint16 15 }{{17655, 1}, {17655, 2}, {30365, 1}, {30365, 2}, {17708, 2}, {17708, 3}, {17708, 4}, {30354, 4}, 16 {17687, 1}, {17687, 2}, {30252, 1}, {30252, 2}, {30252, 3}, {30252, 4}, {17660, 1}, {17660, 2}, {17660, 3}, {17660, 4}, 17 {22794, 1}, {22794, 2}, {22794, 3}, {22794, 4}, {17687, 3}, {17687, 4}, {40075, 1}, {40075, 2}, {40075, 3}, 18 {30365, 3}, {30365, 4}, {17649, 1}, {17649, 2}, {17649, 3}, {17649, 4}, {30258, 1}, {30258, 2}, {30258, 3}, {30258, 4}, 19 {38387, 1}, {38387, 2}, {38387, 3}, {17653, 2}, {17653, 3}, {17653, 4}, {45873, 4}} { 20 for _, roaming := range []bool{false, true} { 21 t.Run(fmt.Sprintf("%d/%d/roaming=%t", profile.monster, profile.level, roaming), func(t *testing.T) { 22 world, _, start := newNPCPursuitWorld(t) 23 npc := world.characters[1000000] 24 npc.monsterData, npc.level = profile.monster, profile.level 25 npc.combatProfile = gamedata.NPCCombatProfiles[profile.monster][profile.level] 26 npc.motion.roaming = roaming 27 deadline := npc.nextAttack 28 29 for tick := 1; tick <= 4; tick++ { 30 world.advanceNPCs(start.Add(time.Duration(tick) * 250 * time.Millisecond)) 31 } 32 33 if npc.position[0] <= 2 || npc.position[0] >= 4 || !npc.running || !npc.motion.moving || npc.fighting != 1 || npc.nextAttack != deadline { 34 t.Fatal("admitted profile did not pursue through the shared motion path", npc.position, npc.fighting, npc.nextAttack) 35 } 36 37 world.characters[1].health = 0 38 for tick := 5; tick <= 40 && (npc.fighting != 0 || npc.motion.returning); tick++ { 39 world.advanceNPCs(start.Add(time.Duration(tick) * 250 * time.Millisecond)) 40 } 41 42 if math.Abs(float64(npc.position[0]-1)) >= 1 || npc.motion.home != [3]float32{1, 0, 0} || npc.running || npc.motion.moving || npc.motion.returning || npc.fighting != 0 || !npc.nextAttack.IsZero() { 43 t.Fatal("admitted profile did not stop and return to its retained home", npc.position, npc.motion, npc.fighting) 44 } 45 46 position := npc.position 47 world.advanceNPCs(start.Add(time.Minute)) 48 49 if npc.position != position || npc.motion.moving != roaming || npc.running { 50 t.Fatal("return did not preserve its idle placement policy") 51 } 52 }) 53 } 54 } 55 } 56 57 func newFixedBossPursuitWorld(t *testing.T) (*server, *combatTestConn) { 58 t.Helper() 59 world, conn := newControlPursuitWorld(t) 60 npc := world.characters[1000000] 61 npc.monsterData, npc.level, npc.npcDefinitionID = 30354, 4, "952/district/5/fixed/0" 62 npc.combatProfile = gamedata.NPCCombatProfiles[30354][4] 63 npc.motion.roaming = false 64 npc.playfield, world.characters[1].playfield = 952, 952 65 world.mobileNPCs = map[uint32][]*character{952: {npc}} 66 return world, conn 67 } 68 69 func TestFixedNPCPursuitUsesRemainingThreatAfterTargetLoss(t *testing.T) { 70 world, _, start := newNPCPursuitWorld(t) 71 npc := world.characters[1000000] 72 npc.motion.roaming = false 73 npc.threat = map[uint32]int64{1: 20, 2: 10} 74 world.characters[2] = &character{id: 2, playfield: 950, position: [3]float32{2.125, 0, 20}, health: 100} 75 world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 76 deadline := npc.nextAttack 77 world.advanceNPCs(start.Add(250 * time.Millisecond)) 78 world.characters[1].health = 0 79 80 world.advanceNPCs(start.Add(500 * time.Millisecond)) 81 world.advanceNPCs(start.Add(750 * time.Millisecond)) 82 83 if npc.position[0] <= 1 || npc.position[0] >= 1.05 || npc.position[2] <= 0 || npc.position[2] >= .5 || npc.fighting != 2 || npc.nextAttack != deadline || npc.motion.returning || !npc.running { 84 t.Fatal("motion discarded remaining threat or restarted its attack clock", npc.position, npc.fighting, npc.nextAttack) 85 } 86 } 87 88 func TestFixedNPCPursuitRespectsRootAndSnare(t *testing.T) { 89 for _, program := range []uint32{42110, 82518} { 90 t.Run(fmt.Sprint(program), func(t *testing.T) { 91 world, _ := newFixedBossPursuitWorld(t) 92 if program == 82518 { 93 world.characters[1].profession = 4 94 } 95 npc := world.characters[1000000] 96 at := castTestBuff(t, world, 1, npc.id, program) 97 world.lastNPCUpdate = at 98 99 world.advanceNPCs(at.Add(250 * time.Millisecond)) 100 101 want := float32(1.2666667) 102 if program == 42110 { 103 want = 1 104 } 105 if math.Abs(float64(npc.position[0]-want)) > .0001 || npc.position[1] != 0 || npc.position[2] != 0 || npc.motion.moving != (program != 42110) || npc.fighting != 1 { 106 t.Fatal("fixed encounter bypassed the shared movement control", npc.position, npc.motion) 107 } 108 if program == 42110 { 109 deadline := npc.activeNanos[0].expiresAt 110 world.advanceNanos(deadline) 111 world.lastNPCUpdate = deadline 112 world.advanceNPCs(deadline.Add(250 * time.Millisecond)) 113 world.advanceNPCs(deadline.Add(500 * time.Millisecond)) 114 115 if math.Abs(float64(npc.position[0]-1.3)) > .0001 || !npc.motion.moving || !npc.running { 116 t.Fatal("root expiry did not resume fixed-encounter pursuit", npc.position, npc.motion) 117 } 118 } 119 }) 120 } 121 } 122 123 func TestFixedNPCColdLoadRestoresHomeAndHealthWithoutCombat(t *testing.T) { 124 for _, state := range []string{"pursuit", "return", "rooted", "home"} { 125 t.Run(state, func(t *testing.T) { 126 world, _ := newFixedBossPursuitWorld(t) 127 npc := world.characters[1000000] 128 npc.health = 50 129 if state != "home" { 130 npc.position = [3]float32{5.5, 0, 0} 131 } 132 npc.motion.returning = state == "return" 133 if state == "return" || state == "home" { 134 npc.fighting = 0 135 delete(world.fighting, npc.id) 136 } 137 if state == "rooted" { 138 castTestBuff(t, world, 1, npc.id, 42110) 139 } 140 if err := world.flushWorld(); err != nil { 141 t.Fatal(err) 142 } 143 144 cold := newTestInventoryServer(t) 145 cold.characters[1].playfield = npc.playfield 146 cold.clients[1] = &worldClient{id: 1, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)} 147 cold.lastNPCUpdate = time.Now() 148 cold.advanceNPCs(cold.lastNPCUpdate.Add(250 * time.Millisecond)) 149 cold.advanceNPCs(cold.lastNPCUpdate.Add(250 * time.Millisecond)) 150 151 restored := cold.characters[npc.id] 152 if restored.motion == nil || restored.motion.roaming || restored.motion.home != [3]float32{1, 0, 0} || restored.fighting != 0 || 153 restored.position != [3]float32{1, 0, 0} || restored.health != 50 || restored.motion.returning || restored.motion.moving || restored.movementRestricted() != (state == "rooted") || len(cold.mobileNPCs[952]) != 1 { 154 t.Fatal("cold fixed encounter lost home, health or control state, or resumed stale combat", restored.position, restored.motion, restored.fighting) 155 } 156 }) 157 } 158 } 159 160 func TestFixedNPCRespawnUsesHomeAndKeepsIdlePolicy(t *testing.T) { 161 world, _ := newFixedBossPursuitWorld(t) 162 world.nextNPC = 1000001 163 npc, player := world.characters[1000000], world.characters[1] 164 npc.position, npc.health = [3]float32{3, 0, 0}, 1 165 player.position = npc.position 166 if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil { 167 t.Fatal(err) 168 } 169 world.advanceCombat(player.nextAttack) 170 if len(world.npcRespawns) != 1 { 171 t.Fatal("moved fixed encounter did not die") 172 } 173 deadline := world.npcRespawns[0].at 174 175 world.advanceNPCRespawns(deadline) 176 cold := newTestInventoryServer(t) 177 cold.lastNPCUpdate = deadline 178 cold.advanceNPCs(deadline.Add(time.Minute)) 179 180 replacement := cold.characters[world.nextNPC-1] 181 if replacement == nil || replacement.id == npc.id || replacement.position != [3]float32{1, 0, 0} || replacement.motion.roaming || 182 replacement.motion.moving || replacement.motion.returning || replacement.health != 20 || len(cold.mobileNPCs[952]) != 1 || cold.characters[npc.id].health != 0 { 183 t.Fatal("fixed respawn reused corpse position, wandered or lost its separate life", replacement) 184 } 185 } 186 187 func TestAuthoredFixedBossHasCombatMotion(t *testing.T) { 188 spawns, err := readNPCSpawns() 189 if err != nil { 190 t.Fatal(err) 191 } 192 193 for _, npc := range spawns { 194 if npc.npcDefinitionID != "952/district/5/fixed/0" { 195 continue 196 } 197 if npc.motion == nil || npc.motion.home != npc.position || npc.motion.roaming { 198 t.Fatal("fixed boss has no retained home and combat motion") 199 } 200 return 201 } 202 t.Fatal("authored fixed boss missing") 203 }