server/clothing_test.go (10647B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "fmt" 8 "reflect" 9 "slices" 10 "testing" 11 ) 12 13 func TestStarterClothingWearRestrictionsAndColdRecovery(t *testing.T) { 14 for _, template := range []uint32{29426, 96358, 99719} { 15 for side := uint16(0); side <= 2; side++ { 16 t.Run(fmt.Sprintf("%d/side%d", template, side), func(t *testing.T) { 17 world, owner := newEquipmentWorld(t) 18 player, client := world.characters[1], world.clients[1] 19 appearance := unpackAppearance(player.appearance) 20 appearance.Side = side 21 player.appearance = appearance.packed() 22 player.inventory.Items[0] = ownedItem{ID: 1, Type: 51022, Template: template, Quality: 1, Slot: 64, Count: 1} 23 if err := world.saveUser(player); err != nil { 24 t.Fatal(err) 25 } 26 if err := world.saveInventory(player, player.inventory); err != nil { 27 t.Fatal(err) 28 } 29 before, _ := readTestItemStore(world) 30 observer := &combatTestConn{} 31 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 32 world.clients[2] = &worldClient{id: 2, conn: observer, target: true, ready: true, players: map[uint32]bool{1: true}} 33 slot, bonus := uint32(17), int32(0) 34 if template == 29426 { 35 slot, bonus = 21, 10 36 } 37 38 if err := world.handleInventory(client, inventoryMovePacket(1, 64, slot)[20:]); err != nil { 39 t.Fatal(err) 40 } 41 if template == 29426 && side != 2 { 42 after, _ := readTestItemStore(world) 43 if client.equipment != nil || !bytes.Equal(before, after) || player.inventory.Items[0].Slot != 64 || len(owner.packets) != 2 || len(observer.packets) != 0 { 44 t.Fatal("wrong-side shirt request changed ownership or equipment") 45 } 46 return 47 } 48 if client.equipment == nil { 49 t.Fatal("catalog clothing was rejected") 50 } 51 world.advanceEquipment(client.equipment.readyAt) 52 restored := newTestInventoryServer(t).characters[1] 53 if err := world.handleInventory(client, itemMovePacket(1, slot, 111)[20:]); err != nil { 54 t.Fatal(err) 55 } 56 world.advanceEquipment(client.equipment.readyAt) 57 removed := newTestInventoryServer(t).characters[1] 58 59 if !reflect.DeepEqual(restored.inventory.Items, []ownedItem{{ID: 1, Type: 51022, Template: template, Quality: 1, Slot: slot, Count: 1}}) || 60 !reflect.DeepEqual(removed.inventory.Items, []ownedItem{{ID: 1, Type: 51022, Template: template, Quality: 1, Slot: 64, Count: 1}}) || 61 restored.equipmentBonus(91) != bonus || removed.equipmentBonus(91) != 0 || player.equipmentBonus(91) != 0 { 62 t.Fatal("wear/removal lost identity, persisted the wrong slot or misapplied AC") 63 } 64 if len(owner.packets) != 4 || len(observer.packets) != 2 || 65 binary.BigEndian.Uint32(owner.packets[1][16:20]) != 0x35505644 || 66 !bytes.Equal(owner.packets[1][16:], observer.packets[0][16:]) || 67 binary.BigEndian.Uint32(observer.packets[1][45:49]) != 7 { 68 t.Fatal("owner and observer did not receive the same wear/removal effects") 69 } 70 }) 71 } 72 } 73 } 74 75 func TestCatalogClothingProfessionAlternativesAndStrictAbilityBounds(t *testing.T) { 76 for _, scenario := range []struct { 77 profession uint16 78 stamina uint16 79 accepted bool 80 }{{1, 10, true}, {9, 10, true}, {2, 10, false}, {1, 9, false}} { 81 t.Run(fmt.Sprintf("profession%d/stamina%d", scenario.profession, scenario.stamina), func(t *testing.T) { 82 world, _ := newEquipmentWorld(t) 83 player, client := world.characters[1], world.clients[1] 84 appearance := unpackAppearance(player.appearance) 85 appearance.Side = 1 86 player.appearance = appearance.packed() 87 player.profession, player.stats.Abilities.Stamina = scenario.profession, scenario.stamina 88 player.inventory.Items[0] = ownedItem{ID: 1, Type: 51022, Template: 201201, Quality: 1, Slot: 64, Count: 1} 89 90 err := world.handleInventory(client, inventoryMovePacket(1, 64, 27)[20:]) 91 if client.equipment != nil { 92 world.advanceEquipment(client.equipment.readyAt) 93 } 94 95 if err != nil || (player.inventory.Items[0].Slot == 27) != scenario.accepted { 96 t.Fatal("catalog AND/OR expression or strict stamina boundary was misinterpreted", err) 97 } 98 }) 99 } 100 } 101 102 func TestClothingRejectedEffectsAndPlacementPreserveItems(t *testing.T) { 103 for _, scenario := range []string{"wrong slot", "wrong quality", "wrong identity", "cannot wear", "unsupported effect", "client no-op", "conditional modifier", "wrong target", "repeated modifier", "modifier interval", "changed faction", "failed save"} { 104 t.Run(scenario, func(t *testing.T) { 105 world, owner := newEquipmentWorld(t) 106 player, client := world.characters[1], world.clients[1] 107 player.appearance = (appearanceFields{Race: 1, Breed: 1, Sex: 2, Side: 2}).packed() 108 player.inventory.Items[0] = ownedItem{ID: 1, Type: 51022, Template: 29426, Quality: 1, Slot: 64, Count: 1} 109 original := gamedata.ItemTemplates[29426] 110 definition := *original 111 definition.Stats = slices.Clone(original.Stats) 112 if scenario == "cannot wear" { 113 for index, stat := range definition.Stats { 114 if stat[0] == 30 { 115 definition.Stats[index][1] &^= 4 116 } 117 } 118 } 119 switch scenario { 120 case "unsupported effect", "client no-op", "conditional modifier", "wrong target", "repeated modifier", "modifier interval": 121 definition.Sections = slices.Clone(original.Sections) 122 functions := slices.Clone(*definition.Sections[0].Functions) 123 switch scenario { 124 case "unsupported effect": 125 functions[0].ID = 53002 126 case "client no-op": 127 functions[0].ID = 53051 128 case "conditional modifier": 129 functions[0].Requirements = []gamedata.ItemRequirement{{Stat: 33, Value: 2, Operator: 0}} 130 case "wrong target": 131 functions[0].Target = 3 132 case "repeated modifier": 133 functions[0].TickCount = 2 134 case "modifier interval": 135 functions[0].TickInterval = 1 136 } 137 definition.Sections[0].Functions = &functions 138 } 139 gamedata.ItemTemplates[29426] = &definition 140 t.Cleanup(func() { gamedata.ItemTemplates[29426] = original }) 141 destination := uint32(21) 142 if scenario == "wrong slot" { 143 destination = 18 144 } else if scenario == "wrong quality" { 145 player.inventory.Items[0].Quality = 2 146 } else if scenario == "wrong identity" { 147 player.inventory.Items[0].Type = 51018 148 } 149 if err := world.saveInventory(player, player.inventory); err != nil { 150 t.Fatal(err) 151 } 152 before, _ := readTestItemStore(world) 153 154 err := world.handleInventory(client, inventoryMovePacket(1, 64, destination)[20:]) 155 if scenario == "changed faction" || scenario == "failed save" { 156 if client.equipment == nil { 157 t.Fatal("valid initial wear did not start") 158 } 159 if scenario == "changed faction" { 160 player.appearance = (appearanceFields{Race: 1, Breed: 1, Sex: 2, Side: 1}).packed() 161 } else { 162 world.closeDatabase() 163 } 164 world.advanceEquipment(client.equipment.readyAt) 165 } 166 restored := newTestInventoryServer(t).characters[1] 167 168 if err != nil || client.equipment != nil || player.inventory.Items[0].Slot != 64 || 169 !reflect.DeepEqual(player.inventory.Items, restored.inventory.Items) || player.equipmentBonus(91) != 0 || len(owner.packets) != 2 { 170 t.Fatal("rejected/failed wear lost the item or published a bonus", err) 171 } 172 if scenario != "failed save" { 173 after, _ := readTestItemStore(world) 174 if !bytes.Equal(before, after) { 175 t.Fatal("rejected wear changed persisted equipment") 176 } 177 } 178 }) 179 } 180 } 181 182 func TestClothingAllAbilitiesDriveOverequipping(t *testing.T) { 183 for _, stat := range []int32{16, 17, 18, 19, 20, 21} { 184 t.Run(fmt.Sprint(stat), func(t *testing.T) { 185 world, _ := newEquipmentWorld(t) 186 player, client := world.characters[1], world.clients[1] 187 original := gamedata.ItemTemplates[29426] 188 definition := *original 189 actions := []gamedata.ItemAction{{Type: 6, Requirements: []gamedata.ItemRequirement{{Stat: stat, Value: 9, Operator: 2}}}} 190 definition.Sections = slices.Clone(original.Sections) 191 definition.Sections[1].Actions = &actions 192 gamedata.ItemTemplates[29426] = &definition 193 t.Cleanup(func() { gamedata.ItemTemplates[29426] = original }) 194 player.inventory.Items[0] = ownedItem{ID: 1, Type: 51022, Template: 29426, Quality: 1, Slot: 64, Count: 1} 195 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 21)[20:]); err != nil || client.equipment == nil { 196 t.Fatal("qualified clothing did not begin wear", err) 197 } 198 world.advanceEquipment(client.equipment.readyAt) 199 200 for _, scenario := range []struct { 201 ability uint16 202 bonus int32 203 }{{9, 10}, {7, 8}, {5, 5}, {3, 3}, {0, 0}, {10, 10}} { 204 player.stats.Abilities = characterAbilitiesStats{scenario.ability, scenario.ability, scenario.ability, scenario.ability, scenario.ability, scenario.ability} 205 bonus := player.equipmentBonus(91) 206 207 if bonus != scenario.bonus || player.inventory.Items[0].ID != 1 || player.inventory.Items[0].Slot != 21 { 208 t.Fatalf("ability %d: AC %d, want %d; equipment %+v", scenario.ability, bonus, scenario.bonus, player.inventory.Items) 209 } 210 } 211 }) 212 } 213 } 214 215 func TestClothingSkillEffectAndZeroOverequippingThreshold(t *testing.T) { 216 world, _ := newEquipmentWorld(t) 217 player, client := world.characters[1], world.clients[1] 218 original := gamedata.ItemTemplates[29426] 219 definition := *original 220 definition.Sections = slices.Clone(original.Sections) 221 functions := slices.Clone(*definition.Sections[0].Functions) 222 functions[0].ID = 53012 223 trailing := "00000000" 224 functions[0].Arguments = append(slices.Clone(functions[0].Arguments), gamedata.ItemArgument{BytesHex: &trailing}) 225 definition.Sections[0].Functions = &functions 226 actions := []gamedata.ItemAction{{Type: 6, Requirements: []gamedata.ItemRequirement{{Stat: 18, Value: 0, Operator: 2}}}} 227 definition.Sections[1].Actions = &actions 228 gamedata.ItemTemplates[29426] = &definition 229 t.Cleanup(func() { gamedata.ItemTemplates[29426] = original }) 230 player.inventory.Items[0] = ownedItem{ID: 1, Type: 51022, Template: 29426, Quality: 1, Slot: 64, Count: 1} 231 232 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 21)[20:]); err != nil || client.equipment == nil { 233 t.Fatal("qualified Skill modifier garment was rejected", err) 234 } 235 world.advanceEquipment(client.equipment.readyAt) 236 player.stats.Abilities.Stamina = 0 237 player.activeNanos = syntheticNanoModifiers(t, [][2]int32{{18, -10}}) 238 bonus := player.equipmentBonus(91) 239 world.handleInventory(client, itemMovePacket(1, 21, 111)[20:]) 240 world.advanceEquipment(client.equipment.readyAt) 241 242 if player.effectiveAbility(18) != -10 || bonus != 10 || player.equipmentBonus(91) != 0 || player.inventory.Items[0].Slot != 64 { 243 t.Fatal("Skill modifier or zero-denominator OE changed its native semantics") 244 } 245 }