server/aggdef_nano_test.go (19503B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "slices" 8 "testing" 9 "time" 10 ) 11 12 type aggDefNanoFixture struct { 13 program, crystal, quality, duration, debit uint32 14 modifiers [][2]int32 15 attack, recharge time.Duration 16 } 17 18 func aggDefNanoFixtures() []aggDefNanoFixture { 19 return []aggDefNanoFixture{ 20 {149994, 150020, 107, 1800, 34, 21 [][2]int32{{51, 200}, {149, 1000}, {119, 1000}, {120, 1000}, {118, 1000}, {311, 100}, {280, 100}, {279, 100}}, 22 time.Second, time.Second}, 23 {152522, 152531, 143, 6000, 119, 24 [][2]int32{{282, 93}, {317, 93}, {281, 93}, {311, 93}, {316, 93}, {280, 93}, {278, 93}, {279, 93}, {51, 90}}, 25 2100 * time.Millisecond, 2100 * time.Millisecond}, 26 {152525, 152529, 143, 3000, 119, 27 [][2]int32{{120, 450}, {149, 450}, {119, 450}, {118, 450}, {282, 190}, {317, 190}, {281, 190}, {311, 190}, 28 {316, 190}, {280, 190}, {278, 190}, {279, 190}, {51, 120}}, 29 1050 * time.Millisecond, time.Second}, 30 } 31 } 32 33 func TestAggDefNanosUploadCastRestoreAndExpireCompleteEffects(t *testing.T) { 34 for _, fixture := range aggDefNanoFixtures() { 35 t.Run(fmt.Sprint(fixture.program), func(t *testing.T) { 36 world, conn := newCombatNanoWorld(t, combatNanoFixture{}) 37 player, client := world.characters[1], world.clients[1] 38 player.aggDef = -100 39 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 40 player.stats.Body.MartialArts = 0 41 player.stats.Speed.MeleeInitiative, player.stats.Speed.RangedInitiative = 0, 0 42 player.stats.Speed.PhysicalInitiative, player.stats.Speed.NanoInitiative = 0, 0 43 if err := world.saveUser(player); err != nil { 44 t.Fatal(err) 45 } 46 if granted, err := world.grantItem(player, fixture.crystal); err != nil || !granted { 47 t.Fatal(granted, err) 48 } 49 trained := player.baseStats() 50 observer := &combatTestConn{} 51 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 52 world.clients[2] = &worldClient{id: 2, conn: observer, ready: true, target: true, players: map[uint32]bool{1: true}} 53 54 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 55 t.Fatal(err) 56 } 57 if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[1][37:41]) != fixture.quality || 58 !slices.Equal(player.inventory.LearnedNanos, []uint32{fixture.program}) { 59 t.Fatal("complete original crystal did not upload its program once") 60 } 61 conn.packets = nil 62 completed := castTestBuff(t, world, 1, 1000000, fixture.program) 63 cold := newTestInventoryServer(t).characters[1] 64 65 if player.baseStats() != trained || cold.baseStats() != trained || player.aggDef != -100 || cold.aggDef != -100 || 66 player.nano != 50000-fixture.debit || cold.nano != player.nano || player.nanoReserved != 0 || 67 len(player.activeNanos) != 1 || len(cold.activeNanos) != 1 || player.usedNCU() != 0 || cold.usedNCU() != 0 || 68 player.activeNanos[0].expiresAt.Sub(completed) != time.Duration(fixture.duration)*10*time.Millisecond || 69 len(cold.inventory.Items) != 2 || !slices.Equal(cold.inventory.LearnedNanos, []uint32{fixture.program}) || 70 len(world.characters[1000000].activeNanos) != 0 || len(world.characters[2].activeNanos) != 0 { 71 t.Fatal("cast or cold restore changed the wrong target, foundation, item, duration or resource") 72 } 73 for _, modifier := range fixture.modifiers { 74 if player.nanoBonus(uint32(modifier[0])) != modifier[1] || cold.nanoBonus(uint32(modifier[0])) != modifier[1] { 75 t.Fatal("cast or cold restore lost an authored companion", modifier) 76 } 77 } 78 attack, recharge := player.attackDelays() 79 if attack != fixture.attack || recharge != fixture.recharge { 80 t.Fatal("complete initiative/AggDef effects did not reach shared weapon timing", attack, recharge) 81 } 82 for _, packets := range [][][]byte{conn.packets, observer.packets} { 83 applications := 0 84 for _, packet := range packets { 85 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 { 86 if !slices.Equal(packet[16:], targetPacket(characterActionPacket(1, [5]uint32{177, 53019, fixture.program, 1, fixture.duration}))[16:]) { 87 t.Fatal("native application changed the source, target or original program") 88 } 89 applications++ 90 } 91 } 92 if applications != 1 { 93 t.Fatal("owner or observer did not receive exactly one whole-program application", applications) 94 } 95 } 96 97 conn.packets, observer.packets = nil, nil 98 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, fixture.program})[20:]); err != nil { 99 t.Fatal(err) 100 } 101 102 if len(player.activeNanos) != 1 || len(newTestInventoryServer(t).characters[1].activeNanos) != 1 || len(conn.packets) != 0 || len(observer.packets) != 0 { 103 t.Fatal("a locally noncancelable effect accepted cancellation") 104 } 105 106 deadline := player.activeNanos[0].expiresAt 107 world.advanceNanos(deadline.Add(-time.Nanosecond)) 108 if len(player.activeNanos) != 1 { 109 t.Fatal("effect expired before its authored deadline") 110 } 111 world.advanceNanos(deadline) 112 world.advanceNanos(deadline.Add(time.Second)) 113 cold = newTestInventoryServer(t).characters[1] 114 115 if len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.usedNCU() != 0 || cold.usedNCU() != 0 || 116 player.baseStats() != trained || player.aggDef != -100 || cold.aggDef != -100 { 117 t.Fatal("authoritative expiry retained an effect or changed its foundations") 118 } 119 for _, modifier := range fixture.modifiers { 120 if player.nanoBonus(uint32(modifier[0])) != 0 || cold.nanoBonus(uint32(modifier[0])) != 0 { 121 t.Fatal("expiry retained an authored companion", modifier) 122 } 123 } 124 attack, recharge = player.attackDelays() 125 if attack != 3*time.Second || recharge != 3*time.Second { 126 t.Fatal("expiry did not restore the unmodified defensive timing", attack, recharge) 127 } 128 for _, packets := range [][][]byte{conn.packets, observer.packets} { 129 if len(packets) != 1 || !slices.Equal(packets[0][16:], targetPacket(characterActionPacket(1, [5]uint32{65, 0, 0, 1, fixture.program}))[16:]) { 130 t.Fatal("expiry did not publish exactly one whole-program removal", packets) 131 } 132 } 133 }) 134 } 135 } 136 137 func TestAggDefNanoCrystalAndCastEnforceEveryStrictSkillGate(t *testing.T) { 138 for _, fixture := range []struct { 139 program, crystal, stat uint32 140 threshold uint16 141 }{ 142 {149994, 150020, 129, 245}, 143 {152522, 152531, 130, 525}, {152522, 152531, 128, 525}, {152522, 152531, 131, 525}, 144 {152525, 152529, 130, 525}, {152525, 152529, 128, 525}, {152525, 152529, 131, 525}, 145 } { 146 for _, difference := range []int{-1, 0, 1} { 147 t.Run(fmt.Sprintf("%d/%d/%d", fixture.program, fixture.stat, difference), func(t *testing.T) { 148 world, _ := newCombatNanoWorld(t, combatNanoFixture{}) 149 player, client := world.characters[1], world.clients[1] 150 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 151 for _, field := range player.stats.trainingStats() { 152 if field.stat == fixture.stat { 153 *field.value = uint16(int(fixture.threshold) + difference) 154 } 155 } 156 if granted, err := world.grantItem(player, fixture.crystal); err != nil || !granted { 157 t.Fatal(granted, err) 158 } 159 160 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 161 t.Fatal(err) 162 } 163 learned := slices.Contains(player.inventory.LearnedNanos, fixture.program) 164 player.inventory.LearnedNanos = []uint32{fixture.program} 165 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, fixture.program})[20:]); err != nil { 166 t.Fatal(err) 167 } 168 169 if difference == 1 { 170 if !learned || client.nanoCast == nil || len(player.inventory.Items) != 2 { 171 t.Fatal("qualified original crystal or cast was rejected") 172 } 173 } else if learned || client.nanoCast != nil || player.nano != 50000 || player.nanoReserved != 0 || 174 len(player.inventory.Items) != 3 || len(player.activeNanos) != 0 { 175 t.Fatal("failed strict skill gate consumed a crystal, reserved nano or applied an effect") 176 } 177 }) 178 } 179 } 180 } 181 182 func TestAggDefNanosUseNoNCUAtFullCapacity(t *testing.T) { 183 for _, fixture := range aggDefNanoFixtures() { 184 t.Run(fmt.Sprint(fixture.program), func(t *testing.T) { 185 world, conn := newCombatNanoWorld(t, combatNanoFixture{Profession: 12}) 186 player, client := world.characters[1], world.clients[1] 187 player.inventory.LearnedNanos = []uint32{29303, 27169, fixture.program} 188 castTestBuff(t, world, 1, 1, 29303) 189 if player.usedNCU() != 52 || player.maximumNCU() != 52 { 190 t.Fatal("fixture did not fill the available NCU") 191 } 192 player.nanoRechargeUntil = time.Time{} 193 before := player.nano 194 195 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27169})[20:]); err != nil { 196 t.Fatal(err) 197 } 198 199 if client.nanoCast != nil || player.nano != before || len(player.activeNanos) != 1 { 200 t.Fatal("full NCU did not reject an ordinary buff without debit") 201 } 202 203 castTestBuff(t, world, 1, 1, fixture.program) 204 cold := newTestInventoryServer(t).characters[1] 205 206 if player.usedNCU() != 52 || cold.usedNCU() != 52 || len(player.activeNanos) != 2 || len(cold.activeNanos) != 2 || 207 player.nano != before-fixture.debit || player.nanoBonus(51) == 0 { 208 t.Fatal("NCU-exempt effect did not coexist with a full allocation") 209 } 210 211 conn.packets = nil 212 world.advanceNanos(player.activeNanos[1].expiresAt) 213 cold = newTestInventoryServer(t).characters[1] 214 215 if player.usedNCU() != 52 || cold.usedNCU() != 52 || len(player.activeNanos) != 1 || len(cold.activeNanos) != 1 || 216 player.activeNanos[0].program != 29303 || player.nanoBonus(129) != 140 || player.nanoBonus(51) != 0 || len(conn.packets) != 1 { 217 t.Fatal("expiry subtracted exempt NCU or removed an unrelated effect") 218 } 219 }) 220 } 221 } 222 223 func TestAggDefNanoDamageCompanionsReachOrdinaryHits(t *testing.T) { 224 for _, fixture := range []struct{ program, health uint32 }{{149994, 895}, {152522, 902}, {152525, 805}} { 225 t.Run(fmt.Sprint(fixture.program), func(t *testing.T) { 226 world, _ := newCombatNanoWorld(t, combatNanoFixture{}) 227 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 228 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 229 player.stats.Body.MartialArts = 1 230 player.inventory.LearnedNanos = []uint32{fixture.program} 231 target.health = 1000 232 castTestBuff(t, world, 1, 1, fixture.program) 233 234 if err := world.handleCombat(client, attackPacket(1, target.id)[20:]); err != nil { 235 t.Fatal(err) 236 } 237 world.advanceCombat(player.nextAttack) 238 239 if target.health != fixture.health { 240 t.Fatal("ordinary hit omitted the matching authored damage companion", target.health, fixture.health) 241 } 242 }) 243 } 244 } 245 246 func TestAggDefNanoStrainUpgradeRefreshAndDowngradePreserveSeparateContributions(t *testing.T) { 247 world, conn := newCombatNanoWorld(t, combatNanoFixture{}) 248 player, client := world.characters[1], world.clients[1] 249 player.inventory.LearnedNanos = []uint32{149994, 152522, 152525} 250 castTestBuff(t, world, 1, 1, 149994) 251 player.nanoRechargeUntil = time.Time{} 252 castTestBuff(t, world, 1, 1, 152522) 253 if player.nanoBonus(51) != 290 || player.nanoBonus(279) != 193 || player.usedNCU() != 0 { 254 t.Fatal("distinct strains did not retain separate complete contributions") 255 } 256 257 player.nanoRechargeUntil = time.Time{} 258 castTestBuff(t, world, 1, 1, 152525) 259 player.nanoRechargeUntil = time.Time{} 260 castTestBuff(t, world, 1, 1, 152525) 261 before := len(conn.packets) 262 player.nanoRechargeUntil = time.Time{} 263 err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 152522})[20:]) 264 cold := newTestInventoryServer(t).characters[1] 265 266 if err != nil || client.nanoCast != nil || player.nano != 49609 || cold.nano != 49609 || len(player.activeNanos) != 2 || len(cold.activeNanos) != 2 || 267 player.nanoBonus(51) != 320 || cold.nanoBonus(51) != 320 || player.nanoBonus(279) != 290 || cold.nanoBonus(279) != 290 || 268 player.nanoBonus(120) != 1450 || cold.nanoBonus(120) != 1450 || player.usedNCU() != 0 || cold.usedNCU() != 0 || len(conn.packets) != before+1 { 269 t.Fatal("NCU-exempt strain replacement stacked, lost another strain or charged a rejected downgrade", err) 270 } 271 for _, effect := range player.activeNanos { 272 if effect.program == 152522 { 273 t.Fatal("weaker strain survived replacement") 274 } 275 } 276 } 277 278 func TestAggDefNanoFailedReplacementAndExpiryRetainCompleteEffectsUntilRetry(t *testing.T) { 279 for _, operation := range []string{"replace", "expire"} { 280 t.Run(operation, func(t *testing.T) { 281 world, conn := newCombatNanoWorld(t, combatNanoFixture{}) 282 player, client := world.characters[1], world.clients[1] 283 player.inventory.LearnedNanos = []uint32{152522, 152525} 284 castTestBuff(t, world, 1, 1, 152522) 285 player.nanoRechargeUntil = time.Time{} 286 at := player.activeNanos[0].expiresAt 287 statement := "CREATE TRIGGER reject_effect BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'test expiry'); END" 288 if operation == "replace" { 289 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 152525})[20:]); err != nil || client.nanoCast == nil { 290 t.Fatal("replacement was not admitted", err) 291 } 292 at = client.nanoCast.readyAt.Add(time.Nanosecond) 293 statement = "CREATE TRIGGER reject_effect BEFORE INSERT ON active_nanos WHEN NEW.program_id=152525 BEGIN SELECT RAISE(FAIL, 'test replacement'); END" 294 } 295 if _, err := world.db.Exec(statement); err != nil { 296 t.Fatal(err) 297 } 298 conn.packets = nil 299 300 world.advanceNanos(at) 301 cold := newTestInventoryServer(t).characters[1] 302 303 if player.nano != 49881 || cold.nano != 49881 || player.nanoReserved != 0 || len(player.activeNanos) != 1 || len(cold.activeNanos) != 1 || 304 player.nanoBonus(51) != 90 || cold.nanoBonus(51) != 90 || player.nanoBonus(279) != 93 || cold.nanoBonus(279) != 93 || player.nanoBonus(120) != 0 || 305 player.usedNCU() != 0 || cold.usedNCU() != 0 || operation == "replace" && len(conn.packets) != 2 || operation == "expire" && len(conn.packets) != 0 { 306 t.Fatal("failed persistence published a partial replacement/removal or lost a refund") 307 } 308 if _, err := world.db.Exec("DROP TRIGGER reject_effect"); err != nil { 309 t.Fatal(err) 310 } 311 312 if operation == "replace" { 313 player.nanoRechargeUntil = time.Time{} 314 castTestBuff(t, world, 1, 1, 152525) 315 } else { 316 world.advanceNanos(at.Add(time.Second)) 317 } 318 cold = newTestInventoryServer(t).characters[1] 319 320 if operation == "replace" { 321 if len(player.activeNanos) != 1 || len(cold.activeNanos) != 1 || player.activeNanos[0].program != 152525 || 322 player.nanoBonus(51) != 120 || cold.nanoBonus(51) != 120 || player.nanoBonus(279) != 190 || player.nano != 49762 || cold.nano != 49762 { 323 t.Fatal("durable retry did not replace the complete strain exactly once") 324 } 325 } else if len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.nanoBonus(51) != 0 || cold.nanoBonus(279) != 0 || len(conn.packets) != 1 { 326 t.Fatal("expiry did not retry the complete durable removal") 327 } 328 }) 329 } 330 } 331 332 func TestAggDefNanoIncompleteContractsRejectWithoutPartialEffects(t *testing.T) { 333 for _, scenario := range []string{"foreign target", "mixed target", "unknown stat", "unknown function", "unknown flags", 334 "duplicate attack key", "missing attack", "empty weights", "unknown attack stat", "zero weight", "negative weight", 335 "duplicate getter", "missing attack getter", "unknown getter", "nonzero getter", "missing getters", 336 "missing text", "missing color", "extra text argument", "text header", "text argument header", "text only", 337 "conditional", "repeated", "interval"} { 338 t.Run(scenario, func(t *testing.T) { 339 original := gamedata.NanoTemplates[149994] 340 definition := *original 341 definition.Stats, definition.Sections = slices.Clone(original.Stats), slices.Clone(original.Sections) 342 entries := slices.Clone(*definition.Sections[0].Entries) 343 definition.Sections[0].Entries = &entries 344 functions := slices.Clone(*definition.Sections[2].Functions) 345 definition.Sections[2].Functions = &functions 346 textIndex := slices.IndexFunc(functions, func(function gamedata.ItemFunction) bool { return function.ID == 53057 }) 347 if textIndex < 0 { 348 t.Fatal("original Call on Insanity has no HeadText companion") 349 } 350 text := &functions[textIndex] 351 text.Arguments = slices.Clone(text.Arguments) 352 switch scenario { 353 case "foreign target": 354 for index := range functions { 355 functions[index].Target = 3 356 } 357 case "mixed target": 358 text.Target = 3 359 case "unknown stat": 360 functions[0].Arguments = []gamedata.ItemArgument{{Integer: new(int32(277))}, {Integer: new(int32(200))}} 361 case "unknown function": 362 functions[0].ID = 53153 363 case "unknown flags": 364 for index, stat := range definition.Stats { 365 if stat[0] == 0 { 366 definition.Stats[index][1] = 0x1c002 367 } 368 } 369 case "duplicate attack key": 370 entries[0] = entries[1] 371 case "missing attack": 372 entries = entries[:1] 373 case "empty weights", "unknown attack stat", "zero weight", "negative weight": 374 weights := [][2]uint32{{54, 1000}} 375 if scenario == "empty weights" { 376 weights = nil 377 } 378 if scenario == "unknown attack stat" { 379 weights[0][0] = 999 380 } 381 if scenario == "zero weight" { 382 weights[0][1] = 0 383 } 384 if scenario == "negative weight" { 385 weights[0][1] = 0xffffffff 386 } 387 entries[1].Stats = &weights 388 case "duplicate getter", "missing attack getter", "unknown getter", "nonzero getter", "missing getters": 389 getters := [][2]uint32{{13, 0}, {12, 0}} 390 if scenario == "duplicate getter" { 391 getters[0][0] = 12 392 } 393 if scenario == "missing attack getter" { 394 getters = getters[:1] 395 } 396 if scenario == "unknown getter" { 397 getters[0][0] = 14 398 } 399 if scenario == "nonzero getter" { 400 getters[1][1] = 2 401 } 402 definition.Sections[1].Stats = &getters 403 if scenario == "missing getters" { 404 definition.Sections[1].Stats = nil 405 } 406 case "missing text": 407 text.Arguments[0] = gamedata.ItemArgument{} 408 case "missing color": 409 text.Arguments[1] = gamedata.ItemArgument{} 410 case "extra text argument": 411 text.Arguments = append(text.Arguments, gamedata.ItemArgument{Integer: new(int32(1))}) 412 case "text header": 413 text.HeaderWords[0] = 1 414 case "text argument header": 415 text.ArgumentHeader = 8 416 case "text only": 417 functions = []gamedata.ItemFunction{*text} 418 case "conditional": 419 text.Requirements = []gamedata.ItemRequirement{{Stat: 54, Operator: 2, Value: 1}} 420 case "repeated": 421 text.TickCount = 2 422 case "interval": 423 text.TickInterval = 1 424 } 425 gamedata.NanoTemplates[149994] = &definition 426 t.Cleanup(func() { gamedata.NanoTemplates[149994] = original }) 427 world, conn := newCombatNanoWorld(t, combatNanoFixture{}) 428 player, client := world.characters[1], world.clients[1] 429 player.inventory.LearnedNanos = []uint32{149994} 430 431 err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 149994})[20:]) 432 433 if err != nil || client.nanoCast != nil || player.nano != 50000 || player.nanoReserved != 0 || len(player.activeNanos) != 0 || 434 len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 108 { 435 t.Fatal("unsupported contract started a cast, charged nano or applied partial effects", err) 436 } 437 }) 438 } 439 }