server/nano_armor_test.go (18533B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 _ "embed" 6 "encoding/binary" 7 "encoding/json" 8 "fmt" 9 "reflect" 10 "slices" 11 "testing" 12 "time" 13 ) 14 15 //go:embed testdata/armor-nanos.json 16 var armorNanoData []byte 17 18 type armorNanoFixture struct { 19 Program, Crystal, Quality, Profession uint32 20 Modifiers [][2]int32 21 NCU, Duration, Strain, Flags, Can uint32 22 Target, Debit uint32 23 NanoCost uint32 `json:"nano_cost"` 24 AttackDelay uint32 `json:"attack_delay"` 25 RechargeDelay uint32 `json:"recharge_delay"` 26 Range uint32 27 Actions []gamedata.ItemAction 28 CrystalActions []gamedata.ItemAction `json:"crystal_actions"` 29 Weights []gamedata.ItemEntry 30 Getter [][2]uint32 31 } 32 33 func armorNanoFixtures(t *testing.T) []armorNanoFixture { 34 t.Helper() 35 var fixtures []armorNanoFixture 36 if err := json.Unmarshal(armorNanoData, &fixtures); err != nil { 37 t.Fatal(err) 38 } 39 return fixtures 40 } 41 42 func newArmorNanoWorld(t *testing.T, fixture armorNanoFixture) (*server, *combatTestConn) { 43 t.Helper() 44 world, conn := newStatBuffWorld(t) 45 player := world.characters[1] 46 for _, field := range player.stats.trainingStats() { 47 *field.value = 900 48 } 49 player.stats.Body.NanoPool, player.nano = 30000, 50000 50 if fixture.Profession != 0 { 51 player.profession = uint16(fixture.Profession) 52 } 53 if err := world.saveUser(player); err != nil { 54 t.Fatal(err) 55 } 56 // This original belt supplies enough NCU without adding AC under test. 57 if granted, err := world.grantItem(player, 204601); err != nil || !granted { 58 t.Fatal(granted, err) 59 } 60 inventory := player.inventory 61 inventory.Items = slices.Clone(inventory.Items) 62 inventory.Items[1].Slot = 7 63 if err := world.saveInventory(player, inventory); err != nil { 64 t.Fatal(err) 65 } 66 if fixture.Crystal != 0 { 67 if granted, err := world.grantItem(player, fixture.Crystal); err != nil || !granted { 68 t.Fatal(granted, err) 69 } 70 } 71 conn.packets = nil 72 return world, conn 73 } 74 75 func TestArmorNanoCatalogMatchesCompleteOriginalContracts(t *testing.T) { 76 var programs, crystals, expectedPrograms, expectedCrystals []uint32 77 fixtures := armorNanoFixtures(t) 78 for _, fixture := range fixtures { 79 expectedPrograms = append(expectedPrograms, fixture.Program) 80 if fixture.Crystal != 0 { 81 expectedCrystals = append(expectedCrystals, fixture.Crystal) 82 } 83 } 84 for _, fixture := range combatNanoFixtures(t) { 85 for _, modifier := range fixture.Modifiers { 86 if modifier[0] >= 90 && modifier[0] <= 97 { 87 expectedPrograms = append(expectedPrograms, fixture.Program) 88 expectedCrystals = append(expectedCrystals, fixture.Crystal) 89 break 90 } 91 } 92 } 93 94 for program := range gamedata.NanoTemplates { 95 if !statBuffNano(program) { 96 continue 97 } 98 for stat := uint32(90); stat <= 97; stat++ { 99 if nanoProgramBonus(program, stat) > 0 { 100 programs = append(programs, program) 101 break 102 } 103 } 104 } 105 for crystal := range gamedata.ItemTemplates { 106 if slices.Contains(programs, nanoCrystalProgram(crystal)) { 107 crystals = append(crystals, crystal) 108 } 109 } 110 for _, values := range [][]uint32{programs, crystals, expectedPrograms, expectedCrystals} { 111 slices.Sort(values) 112 } 113 114 if len(programs) != 92 || len(crystals) != 91 || !slices.Equal(programs, expectedPrograms) || !slices.Equal(crystals, expectedCrystals) { 115 t.Fatal("AC admission differs from independent complete originals", programs, crystals) 116 } 117 for _, fixture := range fixtures { 118 for _, field := range [][2]uint32{{0, fixture.Flags}, {30, fixture.Can}, {8, fixture.Duration}, {54, fixture.NCU}, {75, fixture.Strain}, 119 {210, fixture.RechargeDelay}, {287, fixture.Range}, {294, fixture.AttackDelay}, {407, fixture.NanoCost}} { 120 if nanoStat(fixture.Program, field[0]) != field[1] { 121 t.Fatal("authored casting metadata differs", fixture.Program, field) 122 } 123 } 124 definition := gamedata.NanoTemplates[fixture.Program] 125 var modifiers [][2]int32 126 var actions []gamedata.ItemAction 127 var weights []gamedata.ItemEntry 128 var getter [][2]uint32 129 for _, section := range definition.Sections { 130 if section.Actions != nil { 131 actions = append(actions, *section.Actions...) 132 } 133 if section.Entries != nil { 134 weights = append(weights, *section.Entries...) 135 } 136 if section.Stats != nil { 137 getter = append(getter, *section.Stats...) 138 } 139 if section.Functions != nil { 140 for _, function := range *section.Functions { 141 if function.ID != 53045 || function.HeaderWords != [2]uint32{0, 4} || function.ArgumentHeader != 9 || 142 function.TickCount != 1 || function.TickInterval != 0 || function.Target != fixture.Target || len(function.Requirements) != 0 || len(function.Arguments) != 2 { 143 t.Fatal("complete original effect changed", fixture.Program) 144 } 145 modifiers = append(modifiers, [2]int32{*function.Arguments[0].Integer, *function.Arguments[1].Integer}) 146 } 147 } 148 } 149 if !slices.Equal(modifiers, fixture.Modifiers) || !reflect.DeepEqual(actions, fixture.Actions) || 150 !reflect.DeepEqual(weights, fixture.Weights) || !slices.Equal(getter, fixture.Getter) { 151 t.Fatal("original modifiers, criteria or dictionaries changed", fixture.Program) 152 } 153 if fixture.Crystal != 0 { 154 crystal := gamedata.ItemTemplates[fixture.Crystal] 155 var criteria []gamedata.ItemAction 156 for _, section := range crystal.Sections { 157 if section.Actions != nil { 158 criteria = append(criteria, *section.Actions...) 159 } 160 } 161 if *crystal.Quality != fixture.Quality || !reflect.DeepEqual(criteria, fixture.CrystalActions) { 162 t.Fatal("original crystal quality or criteria changed", fixture.Crystal) 163 } 164 } 165 } 166 } 167 168 func TestArmorNanosUploadCastRestoreAndCancelAllEffects(t *testing.T) { 169 for _, fixture := range armorNanoFixtures(t) { 170 t.Run(fmt.Sprint(fixture.Program), func(t *testing.T) { 171 world, conn := newArmorNanoWorld(t, fixture) 172 player, client := world.characters[1], world.clients[1] 173 trained, health, maximum := player.baseStats(), player.health, player.maximumHealth() 174 if fixture.Crystal != 0 { 175 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 176 t.Fatal(err) 177 } 178 if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[1][37:41]) != fixture.Quality { 179 t.Fatal("original crystal did not upload at its authored quality") 180 } 181 } else { 182 // Melee Impact Shield has no original uploader; acquisition remains open. 183 player.inventory.LearnedNanos = []uint32{fixture.Program} 184 if err := world.saveInventory(player, player.inventory); err != nil { 185 t.Fatal(err) 186 } 187 } 188 189 at := castTestBuff(t, world, 1, 1, fixture.Program) 190 cold := newTestInventoryServer(t).characters[1] 191 192 if player.usedNCU() != fixture.NCU || cold.usedNCU() != fixture.NCU || player.nano != 50000-fixture.Debit || cold.nano != player.nano || 193 player.baseStats() != trained || cold.baseStats() != trained || player.health != health || cold.health != health || 194 len(player.activeNanos) != 1 || player.activeNanos[0].expiresAt.Sub(at) != time.Duration(fixture.Duration)*10*time.Millisecond || 195 !slices.Equal(cold.inventory.LearnedNanos, []uint32{fixture.Program}) || len(cold.inventory.Items) != 2 { 196 t.Fatal("AC cast lost its complete effect, durable state or resource lifecycle") 197 } 198 for _, modifier := range fixture.Modifiers { 199 if player.nanoBonus(uint32(modifier[0])) != modifier[1] || cold.nanoBonus(uint32(modifier[0])) != modifier[1] { 200 t.Fatal("AC contract lost a companion modifier", modifier) 201 } 202 } 203 if player.maximumHealth() != maximum+uint32(player.nanoBonus(1)) { 204 t.Fatal("complete mixed AC/HP program lost its health bonus") 205 } 206 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, fixture.Program})[20:]); err != nil { 207 t.Fatal(err) 208 } 209 cold = newTestInventoryServer(t).characters[1] 210 if player.usedNCU() != 0 || len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.maximumHealth() != maximum || player.health != health { 211 t.Fatal("AC removal retained its NCU, protection or a companion") 212 } 213 }) 214 } 215 } 216 217 func TestArmorNanoProtectsWithItsAuthoredDamageTypeAmounts(t *testing.T) { 218 world, _ := newArmorNanoWorld(t, armorNanoFixture{Profession: 2}) 219 player := world.characters[1] 220 player.inventory.LearnedNanos = []uint32{28905} 221 attacker := &character{npc: true, combatProfile: &gamedata.NPCCombatProfile{Weapon: [2]uint32{120911, 120911}}} 222 before := attacker.weaponDamage(player, false, func(uint32) uint32 { return 145 }) 223 224 castTestBuff(t, world, 1, 1, 28905) 225 after := attacker.weaponDamage(player, false, func(uint32) uint32 { return 145 }) 226 227 if player.nanoBonus(90) != 80 || player.nanoBonus(91) != 95 || before != 200 || after != 191 { 228 t.Fatal("complete AC nano did not protect with its selected damage type", before, after, player.nanoBonus(90), player.nanoBonus(91)) 229 } 230 } 231 232 func TestArmorNanoRequirementsGateBothUploadAndCast(t *testing.T) { 233 for _, fixture := range armorNanoFixtures(t) { 234 for _, requirement := range fixture.Actions[0].Requirements { 235 if requirement.Operator != 2 && requirement.Stat != 368 { 236 continue 237 } 238 for _, passed := range []bool{false, true} { 239 t.Run(fmt.Sprintf("%d/%d/pass=%t", fixture.Program, requirement.Stat, passed), func(t *testing.T) { 240 world, _ := newArmorNanoWorld(t, fixture) 241 player, client := world.characters[1], world.clients[1] 242 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 243 if requirement.Stat == 368 { 244 if !passed { 245 player.profession = uint16(fixture.Profession%12 + 1) 246 } 247 } else { 248 for _, field := range player.stats.trainingStats() { 249 if field.stat == uint32(requirement.Stat) { 250 *field.value = uint16(requirement.Value) 251 if passed { 252 *field.value++ 253 } 254 } 255 } 256 } 257 258 if fixture.Crystal != 0 { 259 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 260 t.Fatal(err) 261 } 262 if (len(player.inventory.LearnedNanos) == 1) != passed || !passed && len(player.inventory.Items) != 3 { 263 t.Fatal("original crystal ignored an authored requirement") 264 } 265 } 266 player.inventory.LearnedNanos = []uint32{fixture.Program} 267 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, fixture.Program})[20:]); err != nil { 268 t.Fatal(err) 269 } 270 271 if (client.nanoCast != nil) != passed || !passed && player.nano != 50000 || len(player.activeNanos) != 0 { 272 t.Fatal("cast ignored its criterion or rejected cast changed resources") 273 } 274 }) 275 } 276 } 277 } 278 } 279 280 func TestArmorNanosDoNotAdmitUnimplementedCompanions(t *testing.T) { 281 for _, program := range []uint32{25994, 28879, 29232, 29642, 29763, 29782, 31593, 91809, 154544, 157129, 162357} { 282 t.Run(fmt.Sprint(program), func(t *testing.T) { 283 world, _ := newSkillNanoWorld(t) 284 player, client := world.characters[1], world.clients[1] 285 player.inventory.LearnedNanos = []uint32{program} 286 287 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, program})[20:]); err != nil { 288 t.Fatal(err) 289 } 290 291 if statBuffNano(program) || client.nanoCast != nil || player.nano != 50000 || len(player.activeNanos) != 0 { 292 t.Fatal("unimplemented complete contract was reduced to its AC fraction") 293 } 294 }) 295 } 296 } 297 298 func TestArmorNanoProtectionAddsAfterEquipmentOverequipping(t *testing.T) { 299 for _, scenario := range []struct { 300 strength uint16 301 equipment int32 302 protected, unprotected uint32 303 }{{461, 875, 103, 113}, {367, 657, 125, 135}, {275, 438, 147, 157}, {183, 219, 169, 179}, {91, 0, 191, 200}} { 304 t.Run(fmt.Sprint(scenario.strength), func(t *testing.T) { 305 world, _ := newArmorNanoWorld(t, armorNanoFixture{Profession: 2}) 306 player, client := world.characters[1], world.clients[1] 307 player.stats.Abilities.Strength, player.stats.Abilities.Agility = 461, 511 308 player.inventory.LearnedNanos = []uint32{28905} 309 if granted, err := world.grantItem(player, 85685); err != nil || !granted { 310 t.Fatal(granted, err) 311 } 312 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 21)[20:]); err != nil || client.equipment == nil { 313 t.Fatal("qualified chest equip failed", err) 314 } 315 world.advanceEquipment(client.equipment.readyAt) 316 player.stats.Abilities.Strength = scenario.strength 317 if err := world.saveUser(player); err != nil { 318 t.Fatal(err) 319 } 320 attacker := &character{npc: true, combatProfile: &gamedata.NPCCombatProfile{Weapon: [2]uint32{120911, 120911}}} 321 322 castTestBuff(t, world, 1, 1, 28905) 323 cold := newTestInventoryServer(t).characters[1] 324 damage := attacker.weaponDamage(player, false, func(uint32) uint32 { return 145 }) 325 restored := attacker.weaponDamage(cold, false, func(uint32) uint32 { return 145 }) 326 327 if player.equipmentBonus(91) != scenario.equipment || player.nanoBonus(91) != 95 || damage != scenario.protected || restored != scenario.protected { 328 t.Fatal("nano protection was OE-scaled or equipment was counted twice", player.equipmentBonus(91), player.nanoBonus(91), damage, restored) 329 } 330 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 28905})[20:]); err != nil { 331 t.Fatal(err) 332 } 333 if player.equipmentBonus(91) != scenario.equipment || attacker.weaponDamage(player, false, func(uint32) uint32 { return 145 }) != scenario.unprotected { 334 t.Fatal("nano removal changed the independent equipment contribution") 335 } 336 }) 337 } 338 } 339 340 func TestArmorNanoDamageTypesUseIndependentProtectionAndTheWeaponFloor(t *testing.T) { 341 for _, scenario := range []struct { 342 kind, damage uint32 343 }{{90, 192}, {91, 191}, {92, 192}, {93, 192}, {94, 192}, {95, 192}, {96, 192}, {97, 192}} { 344 t.Run(fmt.Sprint(scenario.kind), func(t *testing.T) { 345 world, _ := newArmorNanoWorld(t, armorNanoFixture{Profession: 2}) 346 player := world.characters[1] 347 player.inventory.LearnedNanos = []uint32{28905} 348 original := gamedata.ItemTemplates[120911] 349 weapon := *original 350 weapon.Stats = slices.Clone(original.Stats) 351 for index, stat := range weapon.Stats { 352 if stat[0] == 436 { 353 weapon.Stats[index][1] = scenario.kind 354 } 355 } 356 gamedata.ItemTemplates[120911] = &weapon 357 t.Cleanup(func() { gamedata.ItemTemplates[120911] = original }) 358 attacker := &character{npc: true, combatProfile: &gamedata.NPCCombatProfile{Weapon: [2]uint32{120911, 120911}}} 359 castTestBuff(t, world, 1, 1, 28905) 360 361 damage := attacker.weaponDamage(player, false, func(uint32) uint32 { return 145 }) 362 minimum := attacker.weaponDamage(player, false, func(uint32) uint32 { return 0 }) 363 364 if damage != scenario.damage || minimum != 55 { 365 t.Fatal("damage selected another protection or crossed the weapon minimum", damage, minimum) 366 } 367 }) 368 } 369 } 370 371 func TestArmorNanoReplacementAndIndependentStrainsExpireSeparately(t *testing.T) { 372 world, _ := newSkillNanoWorld(t) 373 player, client := world.characters[1], world.clients[1] 374 player.profession, player.inventory.LearnedNanos = 3, []uint32{29743, 70548, 27142} 375 castTestBuff(t, world, 1, 1, 29743) 376 player.nanoRechargeUntil = time.Time{} 377 castTestBuff(t, world, 1, 1, 27142) 378 player.nanoRechargeUntil = time.Time{} 379 380 castTestBuff(t, world, 1, 1, 70548) 381 382 if player.nanoBonus(90) != 23 || player.nanoBonus(94) != 29 || player.usedNCU() != 3 || len(player.activeNanos) != 2 { 383 t.Fatal("same-strain AC stacked or a separate radiation contribution was lost") 384 } 385 player.nanoRechargeUntil = time.Time{} 386 before := player.nano 387 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 29743})[20:]); err != nil { 388 t.Fatal(err) 389 } 390 if client.nanoCast != nil || player.nano != before || player.nanoBonus(90) != 23 { 391 t.Fatal("weaker armor replaced a stronger effect or charged a rejected cast") 392 } 393 // Expire just the radiation effect, then the replacement's own deadline. 394 radiationExpiry := player.activeNanos[0].expiresAt 395 world.advanceNanos(radiationExpiry) 396 if player.nanoBonus(94) != 23 || player.usedNCU() != 2 || len(player.activeNanos) != 1 { 397 t.Fatal("independent expiry removed the wrong contribution") 398 } 399 world.advanceNanos(player.activeNanos[0].expiresAt) 400 if player.nanoBonus(90) != 0 || player.nanoBonus(94) != 0 || player.usedNCU() != 0 || len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 401 t.Fatal("armor expiry retained protection or durable NCU") 402 } 403 } 404 405 func TestFailedArmorCastCannotPublishProtectionOrCompanionStats(t *testing.T) { 406 for _, failure := range []struct{ name, statement string }{ 407 {"effect", "CREATE TRIGGER fail_ac BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL, 'test AC'); END"}, 408 {"ignored effect", "CREATE TRIGGER fail_ac BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END"}, 409 {"commit", "CREATE TABLE pending_ac (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_ac AFTER UPDATE OF nano ON characters WHEN NEW.id=1 BEGIN INSERT INTO pending_ac VALUES (999999); END"}, 410 } { 411 t.Run(failure.name, func(t *testing.T) { 412 world, _ := newArmorNanoWorld(t, armorNanoFixture{Profession: 11}) 413 player, client := world.characters[1], world.clients[1] 414 player.inventory.LearnedNanos = []uint32{150620} 415 maximum := player.maximumHealth() 416 if err := world.saveInventory(player, player.inventory); err != nil { 417 t.Fatal(err) 418 } 419 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 150620})[20:]); err != nil { 420 t.Fatal(err) 421 } 422 at := client.nanoCast.readyAt.Add(time.Nanosecond) 423 if _, err := world.db.Exec(failure.statement); err != nil { 424 t.Fatal(err) 425 } 426 427 world.advanceNanos(at) 428 if _, err := world.db.Exec("DROP TRIGGER fail_ac"); err != nil { 429 t.Fatal(err) 430 } 431 world.advanceNanos(at.Add(time.Second + time.Nanosecond)) 432 cold := newTestInventoryServer(t).characters[1] 433 434 if player.nano != 50000 || cold.nano != 50000 || player.maximumHealth() != maximum || cold.maximumHealth() != maximum || 435 player.health != 20 || player.nanoBonus(90) != 0 || cold.nanoBonus(90) != 0 || player.nanoBonus(168) != 0 || 436 len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || client.nanoCast != nil { 437 t.Fatal("failed complete AC/HP/NR cast leaked protection, companions or nano cost") 438 } 439 }) 440 } 441 }