server/critical_hit_test.go (12664B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "fmt" 8 "slices" 9 "testing" 10 "time" 11 ) 12 13 func TestCriticalDamageRatingArmorOverequippingAndAddedDamage(t *testing.T) { 14 for _, scenario := range []struct { 15 name string 16 skill uint16 17 armored bool 18 fling uint16 19 backpack bool 20 want uint32 21 }{ 22 {"base pistol", 5, false, 5, false, 24}, 23 {"AR 400", 398, false, 5, false, 48}, 24 {"AR 800", 798, false, 5, false, 72}, 25 {"AR 1200", 1198, false, 5, false, 96}, 26 {"critical AC floor", 398, true, 5, false, 30}, 27 {"critical AC floor AR 800", 798, true, 5, false, 45}, 28 {"75 percent OE", 5, false, 2, false, 18}, 29 {"50 percent OE", 5, false, 1, false, 12}, 30 {"damage addition after scaling", 398, false, 5, true, 108}, 31 {"damage addition after armor", 398, true, 5, true, 90}, 32 {"damage addition after OE", 5, false, 1, true, 72}, 33 } { 34 t.Run(scenario.name, func(t *testing.T) { 35 world, _ := newRangedWorld(t) 36 player, target := world.characters[1], world.characters[1000000] 37 player.stats.Ranged.Pistol, player.stats.Ranged.FlingShot = scenario.skill, scenario.fling 38 player.level, player.stats.Body.Adventuring = 120, 1000 39 if scenario.armored { 40 target.inventory.Items = []ownedItem{{ID: 4, Type: 51022, Template: 85685, Quality: 200, Slot: 21, Count: 1}} 41 } 42 if scenario.backpack { 43 player.inventory.Items = append(player.inventory.Items, ownedItem{ID: 3, Type: 51017, Template: 158790, Quality: 1, Slot: 19, Count: 1}) 44 } 45 world.combatRoll = func(limit uint32) uint32 { 46 if limit == 10000 { 47 return 0 48 } 49 if limit != 100 { 50 t.Fatal("a critical must not roll the ordinary damage range", limit) 51 } 52 return 0 53 } 54 55 if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil { 56 t.Fatal(err) 57 } 58 world.advanceCombat(player.nextAttack) 59 60 if target.health != 1000-scenario.want || player.inventory.Items[0].Ammo != 2 { 61 t.Fatalf("critical damage: got %d want %d", 1000-target.health, scenario.want) 62 } 63 }) 64 } 65 } 66 67 func TestCriticalChanceBoundariesUseSignedWornModifiers(t *testing.T) { 68 for _, scenario := range []struct { 69 bonus int32 70 roll uint32 71 critical bool 72 }{ 73 {-100, 0, false}, {-3, 0, false}, {-2, 0, true}, {-2, 1, false}, 74 {0, 0, true}, {0, 2, true}, {0, 3, false}, {0, 99, false}, 75 {8, 10, true}, {8, 11, false}, {97, 99, true}, {2147483647, 99, true}, 76 } { 77 t.Run(fmt.Sprintf("%d/%d", scenario.bonus, scenario.roll), func(t *testing.T) { 78 world, conn := newRangedWorld(t) 79 player, target := world.characters[1], world.characters[1000000] 80 original := gamedata.ItemTemplates[158790] 81 backpack := *original 82 backpack.Sections = slices.Clone(original.Sections) 83 stat := int32(379) 84 functions := []gamedata.ItemFunction{{ID: 53045, Arguments: []gamedata.ItemArgument{{Integer: &stat}, {Integer: &scenario.bonus}}}} 85 for index, section := range backpack.Sections { 86 if section.Type == 2 && section.Subtype == 14 { 87 backpack.Sections[index].Functions = &functions 88 } 89 } 90 gamedata.ItemTemplates[158790] = &backpack 91 t.Cleanup(func() { gamedata.ItemTemplates[158790] = original }) 92 player.level, player.stats.Body.Adventuring = 120, 1000 93 player.inventory.Items = append(player.inventory.Items, ownedItem{ID: 3, Type: 51017, Template: 158790, Quality: 1, Slot: 19, Count: 1}) 94 var bounds []uint32 95 world.combatRoll = func(limit uint32) uint32 { 96 if limit == 10000 { 97 return 0 98 } 99 bounds = append(bounds, limit) 100 if len(bounds) == 1 { 101 return scenario.roll 102 } 103 return limit - 1 104 } 105 106 world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]) 107 now := player.nextAttack 108 world.advanceCombat(now) 109 world.advanceCombat(now) 110 111 wantDamage, wantResult := uint32(12), uint32(4) 112 wantBounds := []uint32{100, 10} 113 if scenario.critical { 114 wantDamage, wantResult, wantBounds = 24, 5, []uint32{100} 115 } 116 if !slices.Equal(bounds, wantBounds) || target.health != 1000-wantDamage || player.inventory.Items[0].Ammo != 2 || 117 binary.BigEndian.Uint32(conn.packets[1][29:33]) != wantDamage || binary.BigEndian.Uint32(conn.packets[1][53:57]) != wantResult { 118 t.Fatal("chance threshold, draw count or native result mismatch", bounds, target.health, conn.packets) 119 } 120 }) 121 } 122 } 123 124 func TestCriticalFistsAndNPCWeaponUseTheirOwnCatalogBonus(t *testing.T) { 125 for _, scenario := range []struct { 126 name string 127 npc, weapon bool 128 want uint32 129 }{ 130 {"player fists", false, false, 8}, {"Leet Bite", true, true, 12}, {"prototype NPC", true, false, 5}, 131 } { 132 t.Run(scenario.name, func(t *testing.T) { 133 world, conn := newCombatWorld(t) 134 attacker, target := world.characters[1], world.characters[1000000] 135 if scenario.npc { 136 attacker, target = target, attacker 137 if scenario.weapon { 138 attacker.combatProfile = gamedata.NPCCombatProfiles[17655][1] 139 } 140 } 141 world.combatRoll = func(uint32) uint32 { return 0 } 142 attacker.fighting, attacker.nextAttack = target.id, time.Now() 143 world.fighting[attacker.id] = attacker 144 145 world.advanceCombat(attacker.nextAttack) 146 147 if target.health != 100-scenario.want || binary.BigEndian.Uint32(conn.packets[0][29:33]) != scenario.want || 148 binary.BigEndian.Uint32(conn.packets[0][53:57]) != 5 { 149 t.Fatal("critical selected another weapon's bonus", target.health, conn.packets) 150 } 151 }) 152 } 153 } 154 155 func TestCriticalWeaponSpecialsPersistDamageLockAndUnchangedAmmunition(t *testing.T) { 156 for _, skill := range []uint32{147, 150} { 157 for _, killing := range []bool{false, true} { 158 t.Run(fmt.Sprintf("%d/kill=%t", skill, killing), func(t *testing.T) { 159 var world *server 160 var owner *combatTestConn 161 request, want := flingShotRequest(1, 1000000), uint32(24) 162 if skill == 147 { 163 world, owner = newFastAttackWorld(t) 164 request, want = fastAttackRequest(1, 1000000), 25 165 } else { 166 world, owner = newRangedWorld(t) 167 } 168 player, target := world.characters[1], world.characters[1000000] 169 target.monsterData, target.level = 17655, 1 170 target.combatProfile = gamedata.NPCCombatProfiles[17655][1] 171 if killing { 172 target.health, want = 7, 7 173 } 174 initial := target.health 175 world.combatRoll = func(uint32) uint32 { return 0 } 176 observer := &combatTestConn{} 177 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 178 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 179 180 if err := world.handleWeaponSpecial(world.clients[1], request[20:]); err != nil { 181 t.Fatal(err) 182 } 183 world.handleWeaponSpecial(world.clients[1], request[20:]) 184 cold := newTestInventoryServer(t) 185 186 if target.health != initial-want || cold.characters[target.id].health != target.health || 187 !cold.characters[1].skillLocks[skill].Equal(player.skillLocks[skill]) || 188 !bytes.Equal(owner.packets[0][16:], observer.packets[0][16:]) || 189 binary.BigEndian.Uint32(owner.packets[0][29:33]) != 6 || binary.BigEndian.Uint32(owner.packets[0][33:37]) != want { 190 t.Fatal("critical special did not commit one matching outcome for both observers", target.health, owner.packets) 191 } 192 if skill == 150 && (player.inventory.Items[0].Ammo != 3 || cold.characters[1].inventory.Items[0].Ammo != 3) { 193 t.Fatal("critical Fling consumed pre-15.5.5 ammunition") 194 } 195 if killing && (cold.characters[target.id].corpse == nil || binary.BigEndian.Uint32(owner.packets[0][53:57]) != 4) { 196 t.Fatal("critical special lost death routing") 197 } 198 }) 199 } 200 } 201 } 202 203 func TestCriticalFailedTransactionsPublishNoDamageResult(t *testing.T) { 204 for _, skill := range []uint32{0, 147, 150} { 205 t.Run(fmt.Sprint(skill), func(t *testing.T) { 206 var world *server 207 var conn *combatTestConn 208 if skill == 147 { 209 world, conn = newFastAttackWorld(t) 210 } else { 211 world, conn = newRangedWorld(t) 212 } 213 world.combatRoll = func(uint32) uint32 { return 0 } 214 target := world.characters[1000000] 215 if _, err := world.saveDamageOutcome(target, time.Now()); err != nil { 216 t.Fatal(err) 217 } 218 statement := "CREATE TRIGGER fail_critical BEFORE INSERT ON skill_locks BEGIN SELECT RAISE(FAIL, 'critical lock failure'); END" 219 if skill == 0 { 220 statement = "CREATE TRIGGER fail_critical BEFORE UPDATE OF ammo ON items BEGIN SELECT RAISE(FAIL, 'critical ammo failure'); END" 221 world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]) 222 } 223 if _, err := world.db.Exec(statement); err != nil { 224 t.Fatal(err) 225 } 226 conn.packets = nil 227 228 if skill == 0 { 229 world.advanceCombat(world.characters[1].nextAttack) 230 } else { 231 request := flingShotRequest(1, target.id) 232 if skill == 147 { 233 request = fastAttackRequest(1, target.id) 234 } 235 if err := world.handleWeaponSpecial(world.clients[1], request[20:]); err == nil { 236 t.Fatal("failed critical transaction was accepted") 237 } 238 } 239 cold := newTestInventoryServer(t) 240 241 if target.health != 1000 || cold.characters[target.id].health != 1000 || 242 len(world.characters[1].skillLocks) != 0 || len(cold.characters[1].skillLocks) != 0 || 243 len(target.damageContributors) != 0 || len(cold.characters[target.id].damageContributors) != 0 { 244 t.Fatal("failed critical mutated live or durable damage state") 245 } 246 for _, packet := range conn.packets { 247 key := binary.BigEndian.Uint32(packet[16:20]) 248 if key == 0x46002f16 || key == 0x754f1115 { 249 t.Fatal("failed critical published a damage result") 250 } 251 } 252 if skill != 147 && (world.characters[1].inventory.Items[0].Ammo != 3 || cold.characters[1].inventory.Items[0].Ammo != 3) { 253 t.Fatal("failed critical consumed ammunition") 254 } 255 }) 256 } 257 } 258 259 func TestCriticalOriginalHelmetWearRemovalAndColdRecovery(t *testing.T) { 260 world, _ := newRangedWorld(t) 261 player, client := world.characters[1], world.clients[1] 262 player.profession = 5 263 if err := world.saveUser(player); err != nil { 264 t.Fatal(err) 265 } 266 if granted, err := world.grantItem(player, 160568); err != nil || !granted { 267 t.Fatal(granted, err) 268 } 269 270 if err := world.handleInventory(client, inventoryMovePacket(1, 65, 18)[20:]); err != nil { 271 t.Fatal(err) 272 } 273 if client.equipment == nil { 274 t.Fatal("original Agent helmet was rejected") 275 } 276 world.advanceEquipment(client.equipment.readyAt) 277 worn := player.equipmentBonus(379) 278 cold := newTestInventoryServer(t).characters[1] 279 if err := world.handleInventory(client, itemMovePacket(1, 18, 111)[20:]); err != nil { 280 t.Fatal(err) 281 } 282 world.advanceEquipment(client.equipment.readyAt) 283 removed := newTestInventoryServer(t).characters[1] 284 285 if worn != 1 || cold.equipmentBonus(379) != 1 || removed.equipmentBonus(379) != 0 || player.equipmentBonus(379) != 0 || 286 player.inventory.Items[2].ID != cold.inventory.Items[2].ID || removed.inventory.Items[2].ID != cold.inventory.Items[2].ID || 287 cold.inventory.Items[2].Slot != 18 || removed.inventory.Items[2].Slot != 65 { 288 t.Fatal("critical modifier did not follow the same original item across wear, removal and restart") 289 } 290 } 291 292 func TestCriticalKillingShotCommitsAmmoCorpseAndExperienceTogether(t *testing.T) { 293 world, owner := newRangedWorld(t) 294 player, target := world.characters[1], world.characters[1000000] 295 player.experience = 1400 296 if err := world.saveUser(player); err != nil { 297 t.Fatal(err) 298 } 299 target.health, target.monsterData, target.level = 7, 17655, 1 300 target.combatProfile = gamedata.NPCCombatProfiles[17655][1] 301 observer := &combatTestConn{} 302 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 303 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 304 world.combatRoll = func(uint32) uint32 { return 0 } 305 306 world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]) 307 now := player.nextAttack 308 world.advanceCombat(now) 309 world.advanceCombat(now.Add(time.Minute)) 310 cold := newTestInventoryServer(t) 311 312 if target.health != 0 || cold.characters[target.id].health != 0 || cold.characters[target.id].corpse.looter != 1 || 313 player.inventory.Items[0].Ammo != 2 || cold.characters[1].inventory.Items[0].Ammo != 2 || 314 player.level != 2 || player.experience != 50 || cold.characters[1].level != 2 || cold.characters[1].experience != 50 || 315 !bytes.Equal(owner.packets[1][16:], observer.packets[1][16:]) || 316 binary.BigEndian.Uint32(owner.packets[1][29:33]) != 7 || binary.BigEndian.Uint32(owner.packets[1][49:53]) != 4 || 317 binary.BigEndian.Uint32(owner.packets[1][53:57]) != 5 { 318 t.Fatal("critical overkill separated damage, death, ammunition or the single XP award", target.health, owner.packets) 319 } 320 }