server/armor_damage_test.go (7096B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "reflect" 8 "slices" 9 "testing" 10 ) 11 12 func newArmorDamageWorld(t *testing.T) (*server, *combatTestConn) { 13 t.Helper() 14 world, conn := newArmorWorld(t) 15 player := world.characters[1] 16 player.stats.Abilities.Strength, player.stats.Abilities.Agility = 461, 511 17 if err := world.saveUser(player); err != nil { 18 t.Fatal(err) 19 } 20 if granted, err := world.grantItem(player, 85685); err != nil || !granted { 21 t.Fatalf("grant QL 200 chest: %v %v", granted, err) 22 } 23 world.handleInventory(world.clients[1], inventoryMovePacket(1, 67, 21)[20:]) 24 world.advanceEquipment(world.clients[1].equipment.readyAt) 25 return world, conn 26 } 27 28 func TestArmorDamageUsesTheAttacksProtectionType(t *testing.T) { 29 for _, attack := range []struct { 30 name string 31 kind uint32 32 damage uint32 33 }{ 34 {"projectile", 90, 100}, {"melee", 91, 113}, {"energy", 92, 100}, 35 {"chemical", 93, 125}, {"radiation", 94, 185}, {"cold", 95, 185}, 36 {"disease", 96, 185}, {"fire", 97, 185}, {"nano", 168, 200}, {"invalid", 0, 100}, 37 } { 38 t.Run(attack.name, func(t *testing.T) { 39 world, _ := newArmorDamageWorld(t) 40 original := gamedata.ItemTemplates[120911] 41 weapon := *original 42 weapon.Stats = slices.Clone(original.Stats) 43 for index, stat := range weapon.Stats { 44 if stat[0] == 436 { 45 weapon.Stats[index][1] = attack.kind 46 } 47 } 48 gamedata.ItemTemplates[120911] = &weapon 49 t.Cleanup(func() { gamedata.ItemTemplates[120911] = original }) 50 npc := world.characters[1000000] 51 npc.combatProfile = &gamedata.NPCCombatProfile{Weapon: [2]uint32{120911, 120911}} 52 53 damage := npc.weaponDamage(world.characters[1], false, func(limit uint32) uint32 { 54 if limit != 352 { 55 t.Fatalf("unexpected QL 400 Leet Bite roll bound: %d", limit) 56 } 57 return 145 58 }) 59 critical := npc.weaponDamage(world.characters[1], true, func(uint32) uint32 { 60 t.Fatal("critical damage used the ordinary range roll") 61 return 0 62 }) 63 64 if damage != attack.damage { 65 t.Fatalf("damage type %d: got %d want %d", attack.kind, damage, attack.damage) 66 } 67 if critical != attack.damage+300 { 68 t.Fatalf("critical protection type %d: got %d want %d", attack.kind, critical, attack.damage+300) 69 } 70 }) 71 } 72 } 73 74 func TestArmorDamageCannotCrossWeaponMinimum(t *testing.T) { 75 world, _ := newArmorDamageWorld(t) 76 npc := world.characters[1000000] 77 npc.combatProfile = &gamedata.NPCCombatProfile{Weapon: [2]uint32{120910, 120910}} 78 var hits []uint32 79 80 for _, roll := range []uint32{0, 1, 2, 3, 4} { 81 hits = append(hits, npc.weaponDamage(world.characters[1], false, func(limit uint32) uint32 { 82 if limit != 5 { 83 t.Fatalf("unexpected QL 1 Leet Bite roll bound: %d", limit) 84 } 85 return roll 86 })) 87 } 88 89 if !reflect.DeepEqual(hits, []uint32{4, 4, 4, 4, 4}) { 90 t.Fatalf("AC reduced weapon minimum: %v", hits) 91 } 92 } 93 94 func TestArmorDamageBonusesFollowOwnershipAcrossRemovalAndRestart(t *testing.T) { 95 for _, scenario := range []struct { 96 name string 97 slot uint32 98 armor [3]uint32 99 }{ 100 {"remove chest", 21, [3]uint32{1, 1, 1}}, 101 {"remove boots", 30, [3]uint32{1000, 875, 150}}, 102 } { 103 t.Run(scenario.name, func(t *testing.T) { 104 world, _ := newArmorDamageWorld(t) 105 player := world.characters[1] 106 world.handleInventory(world.clients[1], inventoryMovePacket(1, 66, 30)[20:]) 107 world.advanceEquipment(world.clients[1].equipment.readyAt) 108 before := [3]uint32{uint32(player.inventory.equipmentBonus(90)), uint32(player.inventory.equipmentBonus(91)), uint32(player.inventory.equipmentBonus(94))} 109 110 world.handleInventory(world.clients[1], itemMovePacket(1, scenario.slot, 111)[20:]) 111 world.advanceEquipment(world.clients[1].equipment.readyAt) 112 world.handleInventory(world.clients[1], itemMovePacket(1, scenario.slot, 111)[20:]) 113 restarted := newTestInventoryServer(t).characters[1] 114 after := [3]uint32{uint32(restarted.inventory.equipmentBonus(90)), uint32(restarted.inventory.equipmentBonus(91)), uint32(restarted.inventory.equipmentBonus(94))} 115 116 if before != [3]uint32{1001, 876, 151} || after != scenario.armor || len(restarted.inventory.Items) != 4 { 117 t.Fatalf("armor contributions were duplicated or lost: before%v after%v items%+v", before, after, restarted.inventory.Items) 118 } 119 120 // Removing the remaining piece restores the unarmored baseline. 121 remaining := uint32(30) 122 if scenario.slot == 30 { 123 remaining = 21 124 } 125 world.handleInventory(world.clients[1], itemMovePacket(1, remaining, 111)[20:]) 126 world.advanceEquipment(world.clients[1].equipment.readyAt) 127 if player.inventory.equipmentBonus(90) != 0 || player.inventory.equipmentBonus(91) != 0 || player.inventory.equipmentBonus(94) != 0 { 128 t.Fatalf("unequipping retained armor bonuses: %+v", player.inventory) 129 } 130 }) 131 } 132 } 133 134 func TestArmorDamageNativeCombatMatchesBothObservers(t *testing.T) { 135 world, owner := newArmorDamageWorld(t) 136 player, npc := world.characters[1], world.characters[1000000] 137 player.health = 37 138 npc.combatProfile = &gamedata.NPCCombatProfile{Weapon: [2]uint32{120910, 120910}} 139 observer := &combatTestConn{} 140 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 141 world.clients[2] = &worldClient{id: 2, conn: observer, target: true, ready: true, players: map[uint32]bool{1: true}, npcs: map[uint32]bool{1000000: true}} 142 owner.packets = nil 143 144 world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]) 145 world.advanceCombat(player.nextAttack) 146 world.advanceCombat(npc.nextAttack) 147 world.advanceCombat(npc.nextAttack) 148 var hits []uint32 149 for index, packet := range owner.packets { 150 if len(observer.packets) <= index || !bytes.Equal(packet[16:], observer.packets[index][16:]) { 151 t.Fatal("observers disagree on armored combat") 152 } 153 if binary.BigEndian.Uint32(packet[16:20]) == 0x46002f16 && binary.BigEndian.Uint32(packet[24:28]) == npc.id { 154 hits = append(hits, binary.BigEndian.Uint32(packet[29:33])) 155 } 156 } 157 158 if !reflect.DeepEqual(hits, []uint32{4, 4}) || player.health != 29 || len(owner.packets) != len(observer.packets) { 159 t.Fatalf("native armored hits or authoritative HP: hits%v HP%d", hits, player.health) 160 } 161 } 162 163 func TestArmorDamageHighQualityChestRequirements(t *testing.T) { 164 for _, scenario := range []struct { 165 name string 166 strength, agility uint16 167 slot uint32 168 }{ 169 {"strength below", 460, 511, 67}, {"agility below", 461, 510, 67}, {"both qualify", 461, 511, 21}, 170 } { 171 t.Run(scenario.name, func(t *testing.T) { 172 world, _ := newArmorWorld(t) 173 player := world.characters[1] 174 player.stats.Abilities.Strength, player.stats.Abilities.Agility = scenario.strength, scenario.agility 175 if granted, err := world.grantItem(player, 85685); err != nil || !granted { 176 t.Fatalf("grant: %v %v", granted, err) 177 } 178 179 world.handleInventory(world.clients[1], inventoryMovePacket(1, 67, 21)[20:]) 180 if world.clients[1].equipment != nil { 181 world.advanceEquipment(world.clients[1].equipment.readyAt) 182 } 183 184 if player.inventory.Items[3].Slot != scenario.slot { 185 t.Fatalf("QL 200 chest ignored exact requirements: %+v", player.inventory.Items[3]) 186 } 187 }) 188 } 189 }