server/initiative_debuff_test.go (16628B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "slices" 7 "testing" 8 "time" 9 ) 10 11 func newIceFlechetteWorld(t *testing.T) (*server, *combatTestConn) { 12 t.Helper() 13 world, conn := newDirectDamageWorld(t, directDamageNanoFixture{Crystal: 28815, Profession: 11}) 14 if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:]); err != nil { 15 t.Fatal(err) 16 } 17 if !slices.Equal(world.characters[1].inventory.LearnedNanos, []uint32{28612}) || len(world.characters[1].inventory.Items) != 0 { 18 t.Fatal("starter crystal did not upload exactly its complete program") 19 } 20 conn.packets = nil 21 return world, conn 22 } 23 24 func castIceFlechette(t *testing.T, world *server) time.Time { 25 t.Helper() 26 client := world.clients[1] 27 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 28612})[20:]); err != nil { 28 t.Fatal(err) 29 } 30 if client.nanoCast == nil { 31 t.Fatal("qualified Ice Flechette was rejected") 32 } 33 at := client.nanoCast.readyAt.Add(time.Nanosecond) 34 world.advanceNanos(at) 35 return at 36 } 37 38 func TestPassiveDebuffCatalogAdmitsOnlyCompleteChildren(t *testing.T) { 39 var children, parents []uint32 40 41 for program := range gamedata.NanoTemplates { 42 if passiveDebuffNano(program) { 43 children = append(children, program) 44 } 45 if directDamageNano(program) != nil && damageNanoSecondary(program) != 0 { 46 parents = append(parents, program) 47 } 48 } 49 50 slices.Sort(children) 51 slices.Sort(parents) 52 if !slices.Equal(children, []uint32{144616, 144619, 144632}) || !slices.Equal(parents, []uint32{28612, 29114, 29298}) || nanoCrystalProgram(28815) != 28612 || nanoCrystalProgram(29193) != 29114 || 53 statBuffNano(144632) || directDamageNano(144632) != nil || nanoStat(144632, 75) != 144632 || nanoStat(144632, 8) != 6000 || nanoStat(144632, 54) != 1 { 54 t.Fatal("compound admission or passive child defaults changed", children, parents) 55 } 56 for _, stat := range []uint32{118, 119, 120, 149} { 57 if nanoProgramBonus(144632, stat) != -6 { 58 t.Fatal("original signed initiative modifier changed", stat) 59 } 60 } 61 if statBuffNano(144616) || directDamageNano(144616) != nil || nanoStat(144616, 75) != 221 || nanoStat(144616, 8) != 600 || nanoStat(144616, 54) != 1 { 62 t.Fatal("Mind Pain's passive child metadata changed") 63 } 64 } 65 66 func TestIceFlechetteUploadAndCastEnforceItsAuthoredRequirements(t *testing.T) { 67 for _, scenario := range []struct { 68 name string 69 profession uint16 70 skill uint16 71 accepted bool 72 }{ 73 {"skill equality", 11, 8, false}, 74 {"skill above", 11, 9, true}, 75 {"other profession", 8, 9, false}, 76 } { 77 t.Run(scenario.name, func(t *testing.T) { 78 world, _ := newDirectDamageWorld(t, directDamageNanoFixture{Crystal: 28815, Profession: 11}) 79 player, client := world.characters[1], world.clients[1] 80 player.profession, player.stats.NanoAiding.MatterCreation = scenario.profession, scenario.skill 81 82 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 83 t.Fatal(err) 84 } 85 learned := slices.Equal(player.inventory.LearnedNanos, []uint32{28612}) 86 // Possession of the learned entry cannot bypass the cast's own requirements. 87 player.inventory.LearnedNanos = []uint32{28612} 88 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 28612})[20:]); err != nil { 89 t.Fatal(err) 90 } 91 92 if learned != scenario.accepted || (client.nanoCast != nil) != scenario.accepted || 93 scenario.accepted && (len(player.inventory.Items) != 0 || player.nano != 49993 || player.nanoReserved != 7) || 94 !scenario.accepted && (len(player.inventory.Items) != 1 || player.nano != 50000 || player.nanoReserved != 0) || 95 world.characters[1000000].health != 100 || len(world.characters[1000000].activeNanos) != 0 { 96 t.Fatal("upload or casting ignored Ice Flechette's authored requirements") 97 } 98 }) 99 } 100 } 101 102 func TestIceFlechetteDamageRefreshExpiryAndColdRestore(t *testing.T) { 103 world, conn := newIceFlechetteWorld(t) 104 player, target := world.characters[1], world.characters[1000000] 105 target.nano = 10 106 107 at := castIceFlechette(t, world) 108 firstHealth := target.health 109 cold := newTestInventoryServer(t).characters[1000000] 110 firstDeadline := target.activeNanos[0].expiresAt 111 player.nanoRechargeUntil = time.Time{} 112 refreshed := castIceFlechette(t, world) 113 deadline := target.activeNanos[0].expiresAt 114 world.advanceNanos(deadline.Add(-time.Nanosecond)) 115 if len(target.activeNanos) != 1 || target.nanoBonus(119) != -6 { 116 t.Fatal("effect expired before its deadline or stacked") 117 } 118 before := len(conn.packets) 119 world.advanceNanos(deadline) 120 world.advanceNanos(deadline.Add(time.Second)) 121 restored := newTestInventoryServer(t).characters[1000000] 122 123 if at.IsZero() || firstHealth < 83 || firstHealth > 87 || firstHealth-target.health < 13 || firstHealth-target.health > 17 || 124 player.nano != 49986 || player.nanoReserved != 0 || len(cold.activeNanos) != 1 || cold.activeNanos[0].remaining != time.Minute || 125 cold.activeNanos[0].expiresAt.IsZero() || cold.nanoBonus(149) != -6 || cold.usedNCU() != 0 || 126 !deadline.Equal(refreshed.Add(time.Minute)) || !deadline.After(firstDeadline) || len(target.activeNanos) != 0 || len(restored.activeNanos) != 0 || 127 target.nano != 10 || restored.nano != 10 || target.nanoBonus(119) != 0 || len(conn.packets) != before+1 || 128 binary.BigEndian.Uint32(conn.packets[before][29:33]) != 65 || binary.BigEndian.Uint32(conn.packets[before][45:49]) != 144632 { 129 t.Fatal("damage, refresh, hostile NCU, explicit expiry or persistence changed") 130 } 131 } 132 133 func TestIceFlechetteSlowsTheNPCWeaponClockAndRestoresIt(t *testing.T) { 134 world, _ := newIceFlechetteWorld(t) 135 player, target := world.characters[1], world.characters[1000000] 136 healthBefore := player.health 137 138 at := castIceFlechette(t, world) 139 attack, recharge := target.attackDelays() 140 world.advanceCombat(at.Add(1760*time.Millisecond - time.Nanosecond)) 141 if player.health != healthBefore { 142 t.Fatal("NPC swing ignored the debuffed attack delay") 143 } 144 world.advanceCombat(at.Add(1760 * time.Millisecond)) 145 health := player.health 146 world.advanceNanos(at.Add(time.Minute)) 147 restoredAttack, restoredRecharge := target.attackDelays() 148 149 if attack != 1760*time.Millisecond || recharge != 1770*time.Millisecond || health >= healthBefore || 150 !target.rechargeUntil.Equal(at.Add(3530*time.Millisecond)) || !target.nextAttack.Equal(at.Add(5290*time.Millisecond)) || 151 restoredAttack != 1750*time.Millisecond || restoredRecharge != 1750*time.Millisecond || target.combatProfile.Initiative != 0 || 152 binary.BigEndian.Uint32(targetPacket(npcCombatPacket(target))[49:53]) != 0 { 153 t.Fatal("NPC effective initiative, weapon deadline or raw snapshot initiative changed", attack, recharge, target.rechargeUntil.Sub(at), target.nextAttack.Sub(at)) 154 } 155 } 156 157 func TestIceFlechetteReplaysOnceWhenAnObserverEnters(t *testing.T) { 158 world, _ := newIceFlechetteWorld(t) 159 at := castIceFlechette(t, world) 160 cold := newTestInventoryServer(t) 161 observer := &worldClient{id: 1, npcs: make(map[uint32]bool)} 162 163 packets := cold.updateNPCInterest(observer, cold.characters[1000000].activeNanos[0].expiresAt.Add(-30*time.Second)) 164 repeated := cold.updateNPCInterest(observer, at) 165 166 count := 0 167 for _, packet := range packets { 168 packet = targetPacket(packet) 169 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 { 170 count++ 171 if binary.BigEndian.Uint32(packet[24:28]) != 1000000 || binary.BigEndian.Uint32(packet[37:41]) != 144632 || 172 binary.BigEndian.Uint32(packet[41:45]) != 1 || binary.BigEndian.Uint32(packet[45:49]) != 3000 { 173 t.Fatal("replay lost child identity, caster or remaining duration") 174 } 175 } 176 } 177 if count != 1 || len(repeated) != 0 { 178 t.Fatal("observer replay repeated or omitted the passive child", count, len(repeated)) 179 } 180 } 181 182 func TestIceFlechetteTimerUsesNPCCheckpointsWithoutRewritingStillActors(t *testing.T) { 183 world, _ := newIceFlechetteWorld(t) 184 world.clockStarted = time.Now() 185 at := castIceFlechette(t, world) 186 if err := world.flushWorld(); err != nil { 187 t.Fatal(err) 188 } 189 saved := world.savedNPCs[1000000] 190 saved.NextSave = at.Add(20 * time.Second) 191 world.savedNPCs[1000000] = saved 192 world.activeNPCs = map[uint32]bool{1000000: true} 193 auditCheckpointWrites(t, world) 194 if _, err := world.db.Exec("CREATE TRIGGER audit_nano AFTER UPDATE ON active_nanos BEGIN INSERT INTO checkpoint_writes VALUES ('active_nanos', 'UPDATE'); END"); err != nil { 195 t.Fatal(err) 196 } 197 198 for second := 1; second < 20; second++ { 199 world.advanceNanos(at.Add(time.Duration(second) * time.Second)) 200 if err := world.checkpointWorld(at.Add(time.Duration(second) * time.Second)); err != nil { 201 t.Fatal(err) 202 } 203 } 204 if writes := checkpointWrites(t, world); len(writes) != 0 { 205 t.Fatal("routine effect ticks wrote state", writes) 206 } 207 if err := world.checkpointWorld(at.Add(20 * time.Second)); err != nil { 208 t.Fatal(err) 209 } 210 cold := newTestInventoryServer(t) 211 212 if cold.characters[1000000].activeNanos[0].remaining != 40*time.Second || cold.characters[1000000].activeNanos[0].expiresAt.IsZero() || 213 checkpointWrites(t, world)["active_nanos/UPDATE"] != 1 || checkpointWrites(t, world)["characters/UPDATE"] != 0 { 214 t.Fatal("NPC timer checkpoint or actor write cadence changed", checkpointWrites(t, world)) 215 } 216 } 217 218 func TestIceFlechetteFailuresCannotPublishPartialDamageOrModifiers(t *testing.T) { 219 for _, failure := range []struct{ name, operation, statement string }{ 220 {"add", "add", "CREATE TRIGGER fail_ice BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL, 'add'); END"}, 221 {"ignored add", "add", "CREATE TRIGGER fail_ice BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END"}, 222 {"refresh", "refresh", "CREATE TRIGGER fail_ice BEFORE UPDATE ON active_nanos BEGIN SELECT RAISE(FAIL, 'refresh'); END"}, 223 {"ignored refresh", "refresh", "CREATE TRIGGER fail_ice BEFORE UPDATE ON active_nanos BEGIN SELECT RAISE(IGNORE); END"}, 224 {"expiry", "expiry", "CREATE TRIGGER fail_ice BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'expiry'); END"}, 225 {"ignored expiry", "expiry", "CREATE TRIGGER fail_ice BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(IGNORE); END"}, 226 {"death", "death", "CREATE TRIGGER fail_ice BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'death'); END"}, 227 {"ignored death", "death", "CREATE TRIGGER fail_ice BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(IGNORE); END"}, 228 {"commit", "add", "CREATE TABLE pending_ice (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_ice AFTER INSERT ON active_nanos BEGIN INSERT INTO pending_ice VALUES (999999); END"}, 229 } { 230 t.Run(failure.name, func(t *testing.T) { 231 world, conn := newIceFlechetteWorld(t) 232 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 233 at := time.Now() 234 if failure.operation != "add" { 235 at = castIceFlechette(t, world) 236 target.activeNanos[0].expiresAt = at.Add(50 * time.Second) 237 if err := world.saveActiveNanos(target, at); err != nil { 238 t.Fatal(err) 239 } 240 if failure.operation == "death" { 241 target.health = 1 242 if err := world.flushWorld(); err != nil { 243 t.Fatal(err) 244 } 245 } 246 } 247 health, nano, effects := target.health, player.nano, len(target.activeNanos) 248 if failure.operation == "expiry" { 249 at = target.activeNanos[0].expiresAt 250 } else { 251 player.nanoRechargeUntil = time.Time{} 252 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, target.id, 53019, 28612})[20:]); err != nil { 253 t.Fatal(err) 254 } 255 at = client.nanoCast.readyAt.Add(time.Nanosecond) 256 } 257 if _, err := world.db.Exec(failure.statement); err != nil { 258 t.Fatal(err) 259 } 260 conn.packets = nil 261 262 world.advanceNanos(at) 263 cold := newTestInventoryServer(t) 264 265 if target.health != health || player.nano != nano || player.nanoReserved != 0 || len(target.activeNanos) != effects || 266 cold.characters[1000000].health != health || len(cold.characters[1000000].activeNanos) != effects || cold.characters[1].nano != nano { 267 t.Fatal("failed transaction published a partial damage, modifier, cost or death") 268 } 269 for _, packet := range conn.packets { 270 key := binary.BigEndian.Uint32(packet[16:20]) 271 if key != 0x2b333d6e && (key != 0x5e477770 || binary.BigEndian.Uint32(packet[29:33]) != 108) { 272 t.Fatalf("failed outcome published success %x", packet) 273 } 274 } 275 if _, err := world.db.Exec("DROP TRIGGER fail_ice"); err != nil { 276 t.Fatal(err) 277 } 278 if failure.operation == "expiry" { 279 world.advanceNanos(at.Add(time.Second)) 280 if len(target.activeNanos) != 0 { 281 t.Fatal("failed expiry did not retry") 282 } 283 } else { 284 castIceFlechette(t, world) 285 if target.health >= health || player.nano != nano-7 { 286 t.Fatal("cast could not retry exactly once after rollback") 287 } 288 } 289 }) 290 } 291 } 292 293 func TestIceFlechetteDeathRemovesTheChildAndRespawnsWithoutIt(t *testing.T) { 294 world, conn := newIceFlechetteWorld(t) 295 castIceFlechette(t, world) 296 target := world.characters[1000000] 297 target.health = 1 298 world.characters[1].nanoRechargeUntil = time.Time{} 299 conn.packets = nil 300 301 at := castIceFlechette(t, world) 302 cold := newTestInventoryServer(t) 303 world.advanceNPCRespawns(at.Add(time.Duration(target.combatProfile.RespawnSeconds) * time.Second)) 304 305 removed := 0 306 for _, packet := range conn.packets { 307 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 { 308 switch binary.BigEndian.Uint32(packet[29:33]) { 309 case 177: 310 t.Fatal("lethal hit installed a new debuff on the corpse") 311 case 65: 312 removed++ 313 } 314 } 315 } 316 if target.health != 0 || len(target.activeNanos) != 0 || len(cold.characters[1000000].activeNanos) != 0 || removed != 1 { 317 t.Fatal("death retained or failed to remove the native modifier") 318 } 319 respawn := world.characters[1000001] 320 if respawn == nil || respawn.health != 100 || len(respawn.activeNanos) != 0 || respawn.nanoBonus(119) != 0 { 321 t.Fatal("respawn did not create a clean new life") 322 } 323 } 324 325 func TestPassiveDebuffRejectsIncompleteSecondaryContracts(t *testing.T) { 326 for _, fixture := range []struct { 327 name string 328 child, parent, crystal uint32 329 }{ 330 {"Ice Flechette", 144632, 28612, 28815}, 331 {"Mind Pain", 144616, 29114, 29193}, 332 {"Mind Scream", 144619, 29298, 29395}, 333 } { 334 t.Run(fixture.name, func(t *testing.T) { 335 for _, scenario := range []string{"missing modifier", "duplicate stat", "other stat", "mixed set", "positive", "periodic", "conditional", "self", "area", "other function", "opaque", "other flags", "unknown section"} { 336 t.Run(scenario, func(t *testing.T) { 337 original := gamedata.NanoTemplates[fixture.child] 338 child := *original 339 child.Stats, child.Sections = slices.Clone(child.Stats), slices.Clone(child.Sections) 340 functions := slices.Clone(*child.Sections[2].Functions) 341 functions[0].Arguments = slices.Clone(functions[0].Arguments) 342 child.Sections[2].Functions = &functions 343 value := int32(0) 344 switch scenario { 345 case "missing modifier": 346 functions = functions[:len(functions)-1] 347 case "duplicate stat": 348 value = *functions[1].Arguments[0].Integer 349 functions[0].Arguments[0].Integer = &value 350 case "mixed set": 351 value = 130 352 if fixture.child != 144632 { 353 value = 118 354 } 355 functions[0].Arguments[0].Integer = &value 356 case "other stat": 357 value = 156 358 functions[0].Arguments[0].Integer = &value 359 case "positive": 360 value = 6 361 functions[0].Arguments[1].Integer = &value 362 case "periodic": 363 functions[0].TickCount = 2 364 case "conditional": 365 functions[0].Requirements = []gamedata.ItemRequirement{{Stat: 54, Value: 20, Operator: 2}} 366 case "self": 367 functions[0].Target = 2 368 case "area", "other flags": 369 for index, stat := range child.Stats { 370 if scenario == "area" && stat[0] == 405 || scenario == "other flags" && stat[0] == 0 { 371 child.Stats[index][1] = 4 372 } 373 } 374 case "other function": 375 functions[0].ID = 53051 376 case "opaque": 377 functions[0].Arguments[1].Integer = nil 378 case "unknown section": 379 child.Sections = append(child.Sections, gamedata.ItemSection{Type: 99}) 380 } 381 gamedata.NanoTemplates[fixture.child] = &child 382 t.Cleanup(func() { gamedata.NanoTemplates[fixture.child] = original }) 383 384 accepted, parent, crystal := passiveDebuffNano(fixture.child), directDamageNano(fixture.parent), nanoCrystalProgram(fixture.crystal) 385 386 if accepted || parent != nil || crystal != 0 { 387 t.Fatal("incomplete secondary allowed a partial upload or cast", scenario) 388 } 389 }) 390 } 391 }) 392 } 393 }