server/nano_combat_buffs_test.go (20273B)
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/combat-buff-nanos.json 16 var combatNanoData []byte 17 18 type combatNanoFixture 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 TestDamageNanoContributesToOrdinaryHits(t *testing.T) { 34 world, conn := newCombatNanoWorld(t, combatNanoFixture{}) 35 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 36 player.stats.Abilities = characterAbilitiesStats{10, 10, 10, 10, 10, 10} 37 player.stats.Body.MartialArts = 5 38 player.inventory.LearnedNanos = []uint32{27188} 39 target.health = 1000 40 castTestBuff(t, world, 1, 1, 27188) 41 conn.packets = nil 42 43 if err := world.handleCombat(client, attackPacket(1, target.id)[20:]); err != nil { 44 t.Fatal(err) 45 } 46 world.advanceCombat(player.nextAttack) 47 48 if player.nanoBonus(279) != 2 || target.health != 993 { 49 t.Fatal("ordinary weapon outcome omitted the complete nano damage contribution", target.health, player.nanoBonus(279)) 50 } 51 } 52 53 func combatNanoFixtures(t *testing.T) []combatNanoFixture { 54 t.Helper() 55 var fixtures []combatNanoFixture 56 if err := json.Unmarshal(combatNanoData, &fixtures); err != nil { 57 t.Fatal(err) 58 } 59 return fixtures 60 } 61 62 func newCombatNanoWorld(t *testing.T, fixture combatNanoFixture) (*server, *combatTestConn) { 63 t.Helper() 64 world, conn := newSkillNanoWorld(t) 65 player := world.characters[1] 66 player.level = 200 67 for _, field := range player.stats.trainingStats() { 68 *field.value = 1000 69 } 70 player.stats.Body.NanoPool = 30000 71 if fixture.Profession != 0 { 72 player.profession = uint16(fixture.Profession) 73 } 74 if err := world.saveUser(player); err != nil { 75 t.Fatal(err) 76 } 77 if fixture.Crystal != 0 { 78 if granted, err := world.grantItem(player, fixture.Crystal); err != nil || !granted { 79 t.Fatal(granted, err) 80 } 81 } 82 conn.packets = nil 83 return world, conn 84 } 85 86 func TestCombatNanoContractsMatchCompleteOriginalRecords(t *testing.T) { 87 fixtures := combatNanoFixtures(t) 88 89 if len(fixtures) != 83 { 90 t.Fatal("incomplete independent combat nano census", len(fixtures)) 91 } 92 for _, fixture := range fixtures { 93 if !statBuffNano(fixture.Program) || nanoCrystalProgram(fixture.Crystal) != fixture.Program { 94 t.Fatal("complete original contract or uploader rejected", fixture.Program, fixture.Crystal) 95 } 96 for _, field := range [][2]uint32{{0, fixture.Flags}, {30, fixture.Can}, {8, fixture.Duration}, {54, fixture.NCU}, {75, fixture.Strain}, 97 {210, fixture.RechargeDelay}, {287, fixture.Range}, {294, fixture.AttackDelay}, {407, fixture.NanoCost}} { 98 if nanoStat(fixture.Program, field[0]) != field[1] { 99 t.Fatal("authored casting metadata differs", fixture.Program, field) 100 } 101 } 102 definition := gamedata.NanoTemplates[fixture.Program] 103 var modifiers [][2]int32 104 var actions []gamedata.ItemAction 105 var weights []gamedata.ItemEntry 106 var getter [][2]uint32 107 for _, section := range definition.Sections { 108 if section.Actions != nil { 109 actions = append(actions, *section.Actions...) 110 } 111 if section.Entries != nil { 112 weights = append(weights, *section.Entries...) 113 } 114 if section.Stats != nil { 115 getter = append(getter, *section.Stats...) 116 } 117 if section.Functions != nil { 118 for _, function := range *section.Functions { 119 if (function.ID != 53045 && function.ID != 53012) || function.HeaderWords != [2]uint32{0, 4} || function.ArgumentHeader != 9 || 120 function.TickCount != 1 || function.TickInterval != 0 || function.Target != fixture.Target || len(function.Requirements) != 0 || (function.ID == 53045 && len(function.Arguments) != 2 || function.ID == 53012 && (len(function.Arguments) != 3 || function.Arguments[2].BytesHex == nil || *function.Arguments[2].BytesHex != "00000000")) { 121 t.Fatal("complete original effect changed", fixture.Program) 122 } 123 modifiers = append(modifiers, [2]int32{*function.Arguments[0].Integer, *function.Arguments[1].Integer}) 124 } 125 } 126 } 127 if !slices.Equal(modifiers, fixture.Modifiers) || !reflect.DeepEqual(actions, fixture.Actions) || 128 !reflect.DeepEqual(weights, fixture.Weights) || !slices.Equal(getter, fixture.Getter) { 129 t.Fatal("original modifiers, criteria or dictionaries changed", fixture.Program) 130 } 131 if fixture.Crystal != 0 { 132 crystal := gamedata.ItemTemplates[fixture.Crystal] 133 var criteria []gamedata.ItemAction 134 for _, section := range crystal.Sections { 135 if section.Actions != nil { 136 criteria = append(criteria, *section.Actions...) 137 } 138 } 139 if *crystal.Quality != fixture.Quality || !reflect.DeepEqual(criteria, fixture.CrystalActions) { 140 t.Fatal("original crystal quality or criteria changed", fixture.Crystal) 141 } 142 } 143 } 144 } 145 146 func TestCombatNanosUploadCastRestoreAndCancelAllEffects(t *testing.T) { 147 for _, fixture := range combatNanoFixtures(t) { 148 t.Run(fmt.Sprint(fixture.Program), func(t *testing.T) { 149 world, conn := newCombatNanoWorld(t, fixture) 150 player, client := world.characters[1], world.clients[1] 151 trained, health, maximum := player.baseStats(), player.health, player.maximumHealth() 152 if fixture.Crystal != 0 { 153 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 154 t.Fatal(err) 155 } 156 if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[1][37:41]) != fixture.Quality { 157 t.Fatal("original crystal did not upload at its authored quality") 158 } 159 } 160 161 at := castTestBuff(t, world, 1, 1, fixture.Program) 162 cold := newTestInventoryServer(t).characters[1] 163 164 if player.usedNCU() != fixture.NCU || cold.usedNCU() != fixture.NCU || player.nano != 50000-fixture.Debit || cold.nano != player.nano || 165 player.baseStats() != trained || cold.baseStats() != trained || player.health != health || cold.health != health || 166 len(player.activeNanos) != 1 || player.activeNanos[0].expiresAt.Sub(at) != time.Duration(fixture.Duration)*10*time.Millisecond || 167 !slices.Equal(cold.inventory.LearnedNanos, []uint32{fixture.Program}) || len(cold.inventory.Items) != 2 { 168 t.Fatal("combat cast lost its complete effect, durable state or resource lifecycle") 169 } 170 for _, modifier := range fixture.Modifiers { 171 if player.nanoBonus(uint32(modifier[0])) != modifier[1] || cold.nanoBonus(uint32(modifier[0])) != modifier[1] { 172 t.Fatal("combat contract lost a companion modifier", modifier) 173 } 174 } 175 if player.maximumHealth() != maximum+uint32(player.nanoBonus(1)) { 176 t.Fatal("complete mixed combat/HP program lost its health bonus") 177 } 178 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, fixture.Program})[20:]); err != nil { 179 t.Fatal(err) 180 } 181 cold = newTestInventoryServer(t).characters[1] 182 if player.usedNCU() != 0 || len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.maximumHealth() != maximum || player.health != health { 183 t.Fatal("combat removal retained its NCU, contribution or a companion") 184 } 185 }) 186 } 187 } 188 189 func TestCombatNanoRequirementsGateBothUploadAndCast(t *testing.T) { 190 for _, fixture := range combatNanoFixtures(t) { 191 for _, requirement := range fixture.Actions[0].Requirements { 192 if requirement.Operator != 2 && requirement.Stat != 368 && requirement.Stat != 60 { 193 continue 194 } 195 for _, passed := range []bool{false, true} { 196 t.Run(fmt.Sprintf("%d/%d/pass=%t", fixture.Program, requirement.Stat, passed), func(t *testing.T) { 197 world, _ := newCombatNanoWorld(t, fixture) 198 player, client := world.characters[1], world.clients[1] 199 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 200 if requirement.Stat == 368 || requirement.Stat == 60 { 201 if !passed { 202 player.profession = uint16(fixture.Profession%12 + 1) 203 } 204 } else if requirement.Stat == 54 { 205 player.level = uint16(requirement.Value) 206 if passed { 207 player.level++ 208 } 209 } else { 210 for _, field := range player.stats.trainingStats() { 211 if field.stat == uint32(requirement.Stat) { 212 *field.value = uint16(requirement.Value) 213 if passed { 214 *field.value++ 215 } 216 } 217 } 218 } 219 220 if fixture.Crystal != 0 { 221 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 222 t.Fatal(err) 223 } 224 if (len(player.inventory.LearnedNanos) == 1) != passed || !passed && len(player.inventory.Items) != 3 { 225 t.Fatal("original crystal ignored an authored requirement") 226 } 227 } 228 player.inventory.LearnedNanos = []uint32{fixture.Program} 229 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, fixture.Program})[20:]); err != nil { 230 t.Fatal(err) 231 } 232 233 if (client.nanoCast != nil) != passed || !passed && player.nano != 50000 || len(player.activeNanos) != 0 { 234 t.Fatal("cast ignored its criterion or rejected cast changed resources") 235 } 236 }) 237 } 238 } 239 } 240 } 241 242 func TestCombatNanosRejectUnsupportedSubjectsAndCompanionEffects(t *testing.T) { 243 for _, program := range []uint32{28892, 116812, 116813, 116814, 116815, 116816, 116817, 116821, 244 151824, 151825, 151826, 151827, 151828, 151829, 151830, 151831, 168889, 28876, 25994, 30746} { 245 t.Run(fmt.Sprint(program), func(t *testing.T) { 246 world, _ := newCombatNanoWorld(t, combatNanoFixture{}) 247 player, client := world.characters[1], world.clients[1] 248 player.inventory.LearnedNanos = []uint32{program} 249 250 err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, program})[20:]) 251 252 if err != nil || statBuffNano(program) || client.nanoCast != nil || player.nano != 50000 || len(player.activeNanos) != 0 { 253 t.Fatal("unsupported subject or companion admitted a partial combat buff", program, err) 254 } 255 }) 256 } 257 } 258 259 func TestDamageNanoReplacementAndIndependentCompanionsExpire(t *testing.T) { 260 world, _ := newCombatNanoWorld(t, combatNanoFixture{Profession: 2}) 261 player, client := world.characters[1], world.clients[1] 262 player.inventory.LearnedNanos = []uint32{27188, 27189, 28899} 263 castTestBuff(t, world, 1, 1, 27188) 264 player.nanoRechargeUntil = time.Time{} 265 castTestBuff(t, world, 1, 1, 28899) 266 player.nanoRechargeUntil = time.Time{} 267 268 castTestBuff(t, world, 1, 1, 27189) 269 270 if player.nanoBonus(278) != 5 || player.nanoBonus(279) != 6 || player.nanoBonus(16) != 12 || player.usedNCU() != 13 || len(player.activeNanos) != 2 { 271 t.Fatal("damage replacement stacked or lost a complete independent companion") 272 } 273 player.nanoRechargeUntil = time.Time{} 274 before := player.nano 275 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27188})[20:]); err != nil { 276 t.Fatal(err) 277 } 278 if client.nanoCast != nil || player.nano != before || player.nanoBonus(278) != 5 { 279 t.Fatal("weaker damage nano replaced a stronger effect or consumed resources") 280 } 281 world.advanceNanos(player.activeNanos[1].expiresAt) 282 if player.nanoBonus(278) != 0 || player.nanoBonus(279) != 1 || player.nanoBonus(16) != 12 || player.usedNCU() != 7 { 283 t.Fatal("damage expiry removed the independent strength/melee contribution") 284 } 285 world.advanceNanos(player.activeNanos[0].expiresAt) 286 if player.nanoBonus(279) != 0 || player.nanoBonus(16) != 0 || player.usedNCU() != 0 || len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 287 t.Fatal("expiry retained damage, strength or durable NCU") 288 } 289 } 290 291 func TestFailedCombatNanoCastCannotPublishDamageOrStrength(t *testing.T) { 292 for _, failure := range []struct{ name, statement string }{ 293 {"effect", "CREATE TRIGGER fail_damage BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL, 'test damage'); END"}, 294 {"ignored effect", "CREATE TRIGGER fail_damage BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END"}, 295 {"commit", "CREATE TABLE pending_damage (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_damage AFTER UPDATE OF nano ON characters WHEN NEW.id=1 BEGIN INSERT INTO pending_damage VALUES (999999); END"}, 296 } { 297 t.Run(failure.name, func(t *testing.T) { 298 world, _ := newCombatNanoWorld(t, combatNanoFixture{Profession: 2}) 299 player, client := world.characters[1], world.clients[1] 300 player.inventory.LearnedNanos = []uint32{28899} 301 if err := world.saveInventory(player, player.inventory); err != nil { 302 t.Fatal(err) 303 } 304 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 28899})[20:]); err != nil { 305 t.Fatal(err) 306 } 307 at := client.nanoCast.readyAt.Add(time.Nanosecond) 308 if _, err := world.db.Exec(failure.statement); err != nil { 309 t.Fatal(err) 310 } 311 312 world.advanceNanos(at) 313 if _, err := world.db.Exec("DROP TRIGGER fail_damage"); err != nil { 314 t.Fatal(err) 315 } 316 world.advanceNanos(at.Add(time.Second + time.Nanosecond)) 317 cold := newTestInventoryServer(t).characters[1] 318 319 if player.nano != 50000 || cold.nano != 50000 || player.health != 20 || player.nanoBonus(279) != 0 || cold.nanoBonus(279) != 0 || 320 player.nanoBonus(16) != 0 || len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || client.nanoCast != nil { 321 t.Fatal("failed cast leaked damage/strength, lost resources or retained its reservation") 322 } 323 }) 324 } 325 } 326 327 func TestCombatNanoDeathRemovesDamageAndStrengthDurably(t *testing.T) { 328 world, _ := newCombatNanoWorld(t, combatNanoFixture{Profession: 2}) 329 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 330 player.inventory.LearnedNanos = []uint32{28899} 331 castTestBuff(t, world, 1, 1, 28899) 332 player.health = 1 333 if err := world.handleCombat(client, attackPacket(1, npc.id)[20:]); err != nil { 334 t.Fatal(err) 335 } 336 world.advanceCombat(player.nextAttack) 337 338 world.advanceCombat(npc.nextAttack) 339 cold := newTestInventoryServer(t).characters[1] 340 341 if player.health != 0 || cold.health != 0 || player.nanoBonus(279) != 0 || player.nanoBonus(16) != 0 || 342 len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 { 343 t.Fatal("lethal combat retained damage/strength or durable active effects") 344 } 345 } 346 347 func TestDamageNanosReachOrdinaryFastAttackAndFlingOutcomes(t *testing.T) { 348 for _, attack := range []struct { 349 name string 350 template, damage uint32 351 }{{"ordinary", 0, 19}, {"fast", 209283, 72}, {"fling", 123742, 44}} { 352 t.Run(attack.name, func(t *testing.T) { 353 world, _ := newCombatNanoWorld(t, combatNanoFixture{}) 354 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 355 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 356 player.inventory.LearnedNanos = []uint32{27188} 357 npc.health = 1000 358 if attack.template != 0 { 359 if granted, err := world.grantItem(player, attack.template); err != nil || !granted { 360 t.Fatal(granted, err) 361 } 362 if attack.name == "fling" { 363 inventory := player.inventory 364 inventory.Items = slices.Clone(inventory.Items) 365 inventory.Items[2].Ammo = 3 366 if err := world.saveInventory(player, inventory); err != nil { 367 t.Fatal(err) 368 } 369 } 370 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil || client.equipment == nil { 371 t.Fatal("original weapon did not equip", err) 372 } 373 world.advanceEquipment(client.equipment.readyAt) 374 } 375 castTestBuff(t, world, 1, 1, 27188) 376 377 switch attack.name { 378 case "ordinary": 379 if err := world.handleCombat(client, attackPacket(1, npc.id)[20:]); err != nil { 380 t.Fatal(err) 381 } 382 world.advanceCombat(player.nextAttack) 383 case "fast": 384 if err := world.handleWeaponSpecial(client, fastAttackRequest(1, npc.id)[20:]); err != nil { 385 t.Fatal(err) 386 } 387 case "fling": 388 if err := world.handleWeaponSpecial(client, flingShotRequest(1, npc.id)[20:]); err != nil { 389 t.Fatal(err) 390 } 391 } 392 393 if npc.health != 1000-attack.damage || npc.damageContributors[damageContributor{player: 1}] != attack.damage { 394 t.Fatal("weapon outcome or damage credit omitted the shared nano addition", npc.health, npc.damageContributors) 395 } 396 }) 397 } 398 } 399 400 func newDamageWeaponWorld(t *testing.T) (*server, *combatTestConn) { 401 t.Helper() 402 world, conn := newRangedWorld(t) 403 player := world.characters[1] 404 player.level, player.stats.Body.Adventuring = 120, 1000 405 player.stats.NanoAiding.MatterMetamorphosis, player.stats.NanoAiding.MatterCreation = 150, 150 406 player.stats.Body.NanoPool, player.nano = 1000, 1000 407 player.inventory.LearnedNanos = []uint32{27189} 408 if err := world.saveUser(player); err != nil { 409 t.Fatal(err) 410 } 411 if granted, err := world.grantItem(player, 158790); err != nil || !granted { 412 t.Fatal(granted, err) 413 } 414 if err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 65, 19)[20:]); err != nil || world.clients[1].equipment == nil { 415 t.Fatal("Robust did not equip", err) 416 } 417 world.advanceEquipment(world.clients[1].equipment.readyAt) 418 return world, conn 419 } 420 421 func TestDamageNanosSelectEveryTypeAfterRatingEquipmentAndArmor(t *testing.T) { 422 for _, scenario := range []struct{ kind, normal, critical, removed uint32 }{ 423 {90, 71, 95, 66}, {91, 11, 35, 6}, {92, 71, 95, 66}, {93, 11, 35, 6}, {94, 74, 98, 69}, 424 {95, 14, 38, 9}, {96, 14, 38, 9}, {97, 74, 98, 69}, {168, 24, 48, 24}, {0, 71, 95, 66}, 425 } { 426 t.Run(fmt.Sprint(scenario.kind), func(t *testing.T) { 427 world, _ := newDamageWeaponWorld(t) 428 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 429 player.stats.Ranged.Pistol = 398 430 target.inventory.Items = []ownedItem{{ID: 4, Type: 51022, Template: 85685, Quality: 200, Slot: 21, Count: 1}} 431 original := gamedata.ItemTemplates[123742] 432 weapon := *original 433 weapon.Stats = slices.Clone(original.Stats) 434 for index, stat := range weapon.Stats { 435 if stat[0] == 436 { 436 weapon.Stats[index][1] = scenario.kind 437 } 438 } 439 gamedata.ItemTemplates[123742] = &weapon 440 t.Cleanup(func() { gamedata.ItemTemplates[123742] = original }) 441 442 castTestBuff(t, world, 1, 1, 27189) 443 normal := player.weaponDamage(target, false, func(limit uint32) uint32 { return limit - 1 }) 444 critical := player.weaponDamage(target, true, func(limit uint32) uint32 { return limit - 1 }) 445 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 27189})[20:]); err != nil { 446 t.Fatal(err) 447 } 448 removed := player.weaponDamage(target, false, func(limit uint32) uint32 { return limit - 1 }) 449 450 if normal != scenario.normal || critical != scenario.critical || removed != scenario.removed { 451 t.Fatal("damage type, armor floor, critical reconstruction or independent removal differs", normal, critical, removed) 452 } 453 }) 454 } 455 } 456 457 func TestDamageNanosRemainFullWhenWornDamageBecomesOverequipped(t *testing.T) { 458 for _, scenario := range []struct { 459 level uint16 460 bonus int32 461 damage uint32 462 }{{119, 60, 68}, {95, 45, 53}, {71, 30, 38}, {47, 15, 23}, {23, 0, 8}} { 463 t.Run(fmt.Sprint(scenario.level), func(t *testing.T) { 464 world, _ := newDamageWeaponWorld(t) 465 player, target := world.characters[1], world.characters[1000000] 466 castTestBuff(t, world, 1, 1, 27189) 467 468 player.level = scenario.level 469 if err := world.saveUser(player); err != nil { 470 t.Fatal(err) 471 } 472 cold := newTestInventoryServer(t).characters[1] 473 damage := player.weaponDamage(target, false, func(uint32) uint32 { return 0 }) 474 restored := cold.weaponDamage(target, false, func(uint32) uint32 { return 0 }) 475 476 if player.equipmentBonus(278) != scenario.bonus || player.nanoBonus(278) != 5 || damage != scenario.damage || restored != scenario.damage { 477 t.Fatal("nano addition was OE-scaled or lost on cold recovery", player.equipmentBonus(278), player.nanoBonus(278), damage, restored) 478 } 479 }) 480 } 481 }