server/stat_buff_catalog_test.go (15416B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "reflect" 8 "slices" 9 "testing" 10 "time" 11 ) 12 13 func newStatBuffWorld(t *testing.T) (*server, *combatTestConn) { 14 t.Helper() 15 world, conn := newNanoBuffWorld(t) 16 player := world.characters[1] 17 player.level, player.health, player.nano = 100, 20, 1000 18 player.stats.Body.NanoPool = 400 19 player.stats.NanoAiding.MatterMetamorphosis = 900 20 player.stats.NanoAiding.BiologicalMetamorphosis = 900 21 player.stats.NanoAiding.PsychologicalModification = 900 22 player.stats.NanoAiding.MatterCreation = 900 23 player.inventory.LearnedNanos = nil 24 if err := world.saveUser(player); err != nil { 25 t.Fatal(err) 26 } 27 28 // A GM-placed original Ear implant supplies 47 total NCU for these cast tests. 29 if granted, err := world.grantItem(player, 173155); err != nil || !granted { 30 t.Fatal(granted, err) 31 } 32 inventory := player.inventory 33 inventory.Items = slices.Clone(inventory.Items) 34 inventory.Items[0].Slot = 35 35 if err := world.saveInventory(player, inventory); err != nil { 36 t.Fatal(err) 37 } 38 conn.packets = nil 39 return world, conn 40 } 41 42 func TestStatBuffCatalogAdmitsOnlyCompleteOrdinaryContracts(t *testing.T) { 43 var programs, crystals []uint32 44 expectedPrograms := []uint32{25988, 25989, 26342, 26355, 26356, 26357, 26358, 26359, 26360, 27085, 27086, 45 27118, 27134, 27144, 27154, 27168, 27169, 28651, 28663, 29091, 29200, 29254, 29652, 30095, 95693, 95694, 95695, 95696, 95697} 46 expectedCrystals := []uint32{26206, 26207, 26469, 26482, 26483, 26484, 26485, 26486, 26487, 27216, 27217, 47 27253, 27270, 27280, 27290, 27304, 27305, 28764, 28776, 29092, 29202, 29366, 29814, 30140, 95723, 95724, 95725, 95726, 95727} 48 for _, fixture := range ordinarySkillNanoFixtures(t) { 49 expectedPrograms = append(expectedPrograms, fixture.Program) 50 if fixture.Crystal != 0 { 51 expectedCrystals = append(expectedCrystals, fixture.Crystal) 52 } 53 } 54 for _, fixture := range selfAndCriticalNanoFixtures(t) { 55 expectedPrograms = append(expectedPrograms, fixture.Program) 56 expectedCrystals = append(expectedCrystals, fixture.Crystal) 57 } 58 for _, fixture := range armorNanoFixtures(t) { 59 expectedPrograms = append(expectedPrograms, fixture.Program) 60 if fixture.Crystal != 0 { 61 expectedCrystals = append(expectedCrystals, fixture.Crystal) 62 } 63 } 64 for _, fixture := range combatNanoFixtures(t) { 65 expectedPrograms = append(expectedPrograms, fixture.Program) 66 expectedCrystals = append(expectedCrystals, fixture.Crystal) 67 } 68 for _, fixture := range nanoCostFixtures(t) { 69 expectedPrograms = append(expectedPrograms, fixture.Program) 70 expectedCrystals = append(expectedCrystals, fixture.Crystal) 71 } 72 for _, fixture := range nanoRangeFixtures(t) { 73 if !slices.Contains(expectedPrograms, fixture.Program) { 74 expectedPrograms = append(expectedPrograms, fixture.Program) 75 expectedCrystals = append(expectedCrystals, fixture.Crystal) 76 } 77 } 78 for _, fixture := range aggDefNanoFixtures() { 79 expectedPrograms = append(expectedPrograms, fixture.program) 80 expectedCrystals = append(expectedCrystals, fixture.crystal) 81 } 82 slices.Sort(expectedPrograms) 83 slices.Sort(expectedCrystals) 84 85 for program := range gamedata.NanoTemplates { 86 if statBuffNano(program) { 87 programs = append(programs, program) 88 } 89 } 90 for crystal := range gamedata.ItemTemplates { 91 if statBuffNano(nanoCrystalProgram(crystal)) { 92 crystals = append(crystals, crystal) 93 } 94 } 95 slices.Sort(programs) 96 slices.Sort(crystals) 97 98 // Exact IDs come from the independent complete-original-record catalog audit. 99 if len(programs) != 470 || !slices.Equal(programs, expectedPrograms) { 100 t.Fatal("buff admission differs from the complete effect/metadata audit", programs) 101 } 102 if len(crystals) != 468 || !slices.Equal(crystals, expectedCrystals) { 103 t.Fatal("crystal admission differs from the complete upload audit", crystals) 104 } 105 } 106 107 func TestStatBuffCrystalsLearnCastRestoreAndCancelTheirCompleteEffects(t *testing.T) { 108 for _, fixture := range []struct { 109 crystal, program, quality uint32 110 profession uint16 111 stat uint32 112 bonus int32 113 ncu, debit uint32 114 }{ 115 {26206, 25989, 53, 5, 17, 25, 17, 176}, {26207, 25988, 27, 5, 20, 15, 9, 99}, 116 {26469, 26342, 7, 1, 17, 12, 3, 44}, {26482, 26355, 4, 1, 17, 6, 2, 24}, 117 {26483, 26356, 4, 1, 19, 6, 2, 24}, {26484, 26357, 4, 1, 21, 6, 2, 24}, 118 {26485, 26358, 4, 1, 20, 6, 2, 24}, {26486, 26359, 4, 1, 18, 6, 2, 24}, 119 {26487, 26360, 4, 1, 16, 6, 2, 24}, {27217, 27086, 7, 1, 19, 12, 3, 44}, 120 {27253, 27118, 10, 1, 364, 2, 4, 24}, {27270, 27134, 7, 1, 21, 12, 3, 44}, 121 {27280, 27144, 14, 1, 343, 3, 5, 24}, {27290, 27154, 7, 1, 20, 12, 3, 44}, 122 {27304, 27168, 7, 1, 18, 12, 3, 44}, {27305, 27169, 7, 1, 16, 12, 3, 44}, 123 {28764, 28651, 146, 10, 343, 14, 45, 478}, {28776, 28663, 76, 10, 343, 8, 24, 228}, 124 {29202, 29200, 37, 8, 364, 6, 12, 112}, {29814, 29652, 136, 9, 16, 40, 42, 548}, 125 {30140, 30095, 90, 8, 364, 12, 28, 313}, 126 } { 127 t.Run(fmt.Sprint(fixture.program), func(t *testing.T) { 128 world, conn := newStatBuffWorld(t) 129 player, client := world.characters[1], world.clients[1] 130 player.profession = fixture.profession 131 if err := world.saveUser(player); err != nil { 132 t.Fatal(err) 133 } 134 if granted, err := world.grantItem(player, fixture.crystal); err != nil || !granted { 135 t.Fatal(granted, err) 136 } 137 138 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 139 t.Fatal(err) 140 } 141 if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[1][37:41]) != fixture.quality { 142 t.Fatal("upload used the wrong original crystal quality", conn.packets) 143 } 144 castTestBuff(t, world, 1, 1, fixture.program) 145 cold := newTestInventoryServer(t).characters[1] 146 147 if player.nanoBonus(fixture.stat) != fixture.bonus || cold.nanoBonus(fixture.stat) != fixture.bonus || 148 player.usedNCU() != fixture.ncu || cold.usedNCU() != fixture.ncu || player.nano != 1000-fixture.debit || cold.nano != player.nano || 149 len(cold.inventory.Items) != 1 || cold.inventory.Items[0].Template != 173155 || 150 !slices.Equal(cold.inventory.LearnedNanos, []uint32{fixture.program}) { 151 t.Fatal("upload/cast/restore changed the wrong effect, resource, NCU or item", cold.inventory, player.activeNanos, player.nano, cold.nano) 152 } 153 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, fixture.program})[20:]); err != nil { 154 t.Fatal(err) 155 } 156 if player.nanoBonus(fixture.stat) != 0 || player.usedNCU() != 0 || len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 157 t.Fatal("cancellation retained the effect or its NCU") 158 } 159 }) 160 } 161 } 162 163 func TestRegenerationNanosExpireAtTheirAuthoredDeadline(t *testing.T) { 164 for _, fixture := range []struct { 165 program, stat uint32 166 bonus int32 167 minutes time.Duration 168 }{ 169 {27118, 364, 2, 20}, {27144, 343, 3, 20}, {28651, 343, 14, 60}, 170 {28663, 343, 8, 30}, {29200, 364, 6, 30}, {30095, 364, 12, 45}, 171 } { 172 t.Run(fmt.Sprint(fixture.program), func(t *testing.T) { 173 world, _ := newStatBuffWorld(t) 174 player, client := world.characters[1], world.clients[1] 175 player.profession = 1 176 if fixture.program == 28651 || fixture.program == 28663 { 177 player.profession = 10 178 } else if fixture.program == 29200 || fixture.program == 30095 { 179 player.profession = 8 180 } 181 player.inventory.LearnedNanos = []uint32{fixture.program} 182 castTestBuff(t, world, 1, 1, fixture.program) 183 deadline := player.activeNanos[0].expiresAt 184 beforeNano := player.nano 185 start := time.Unix(100, 0) 186 client.regenerationAt, client.regenerationSeconds = start, 810 187 188 world.advanceRegeneration(start.Add(2 * time.Second)) 189 if fixture.stat == 343 && (player.health != uint32(23+fixture.bonus) || player.nano != beforeNano+3) || 190 fixture.stat == 364 && (player.health != 23 || player.nano != beforeNano+uint32(3+fixture.bonus)) { 191 t.Fatal("active delta did not affect the eligible recovery check", player.health, player.nano) 192 } 193 world.advanceNanos(deadline.Add(-time.Nanosecond)) 194 if len(player.activeNanos) != 1 || player.activeNanos[0].remaining != fixture.minutes*time.Minute { 195 t.Fatal("effect expired early or used the wrong authored duration", player.activeNanos) 196 } 197 world.advanceNanos(deadline) 198 health, nano := player.health, player.nano 199 client.regenerationSeconds = 1622 200 world.advanceRegeneration(start.Add(4 * time.Second)) 201 202 if player.health != health+3 || player.nano != nano+3 || len(player.activeNanos) != 0 || player.usedNCU() != 0 || 203 len(newTestInventoryServer(t).characters[1].activeNanos) != 0 { 204 t.Fatal("expired delta survived into another tick or cold load", player.activeNanos, player.health, player.nano) 205 } 206 }) 207 } 208 } 209 210 func TestStatBuffCrystalAndCastEnforceAuthoredSkillAndProfessionCriteria(t *testing.T) { 211 for _, fixture := range []struct { 212 crystal, program uint32 213 profession uint16 214 field string 215 base uint16 216 }{ 217 {26207, 25988, 5, "PM", 131}, {26206, 25989, 5, "PM", 249}, 218 {27253, 27118, 0, "MM", 55}, {27280, 27144, 0, "BM", 58}, 219 {28764, 28651, 10, "MC", 617}, {28776, 28663, 10, "MC", 324}, 220 {29202, 29200, 8, "PM", 176}, {30140, 30095, 8, "PM", 423}, 221 {29814, 29652, 9, "MM", 632}, 222 {27216, 27085, 0, "BM", 36}, {27216, 27085, 0, "MM", 44}, 223 {29092, 29091, 1, "BM", 6}, {29092, 29091, 1, "MC", 4}, 224 {29366, 29254, 1, "BM", 530}, {29366, 29254, 1, "MC", 512}, 225 {95724, 95693, 1, "BM", 653}, {95724, 95693, 1, "MC", 618}, 226 {95725, 95694, 1, "BM", 297}, {95725, 95694, 1, "MC", 271}, 227 {95723, 95695, 1, "BM", 172}, {95723, 95695, 1, "MC", 157}, 228 {95727, 95696, 1, "BM", 93}, {95727, 95696, 1, "MC", 88}, 229 {95726, 95697, 1, "BM", 727}, {95726, 95697, 1, "MC", 648}, 230 } { 231 for _, scenario := range []string{"skill equality", "skill below", "one above", "profession mismatch"} { 232 if scenario == "profession mismatch" && fixture.profession == 0 { 233 continue 234 } 235 t.Run(fmt.Sprintf("%d/%s/%s", fixture.program, fixture.field, scenario), func(t *testing.T) { 236 world, conn := newStatBuffWorld(t) 237 player, client := world.characters[1], world.clients[1] 238 player.profession = max(1, fixture.profession) 239 if scenario == "profession mismatch" { 240 player.profession = 2 241 } else { 242 base := fixture.base 243 if scenario == "one above" { 244 base++ 245 } else if scenario == "skill below" { 246 base-- 247 } 248 switch fixture.field { 249 case "PM": 250 player.stats.NanoAiding.PsychologicalModification = base 251 case "MC": 252 player.stats.NanoAiding.MatterCreation = base 253 case "MM": 254 player.stats.NanoAiding.MatterMetamorphosis = base 255 case "BM": 256 player.stats.NanoAiding.BiologicalMetamorphosis = base 257 } 258 } 259 if granted, err := world.grantItem(player, fixture.crystal); err != nil || !granted { 260 t.Fatal(granted, err) 261 } 262 before := slices.Clone(player.inventory.Items) 263 264 if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil { 265 t.Fatal(err) 266 } 267 player.inventory.LearnedNanos = []uint32{fixture.program} 268 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, fixture.program})[20:]); err != nil { 269 t.Fatal(err) 270 } 271 272 if scenario == "one above" { 273 if client.nanoCast == nil { 274 t.Fatal("criterion one point above the strict threshold rejected a cast") 275 } 276 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 277 if len(player.activeNanos) != 1 || player.activeNanos[0].program != fixture.program || len(player.inventory.Items) != 1 || 278 !slices.Equal(newTestInventoryServer(t).characters[1].inventory.LearnedNanos, []uint32{fixture.program}) { 279 t.Fatal("criterion one point above the strict threshold failed upload or completion") 280 } 281 return 282 } 283 if client.nanoCast != nil || player.nano != 1000 || len(player.activeNanos) != 0 || !reflect.DeepEqual(player.inventory.Items, before) || 284 len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 2 || binary.BigEndian.Uint32(conn.packets[1][29:33]) != 108 { 285 t.Fatal("skill/profession rejection consumed a crystal or reserved nano", conn.packets, player.inventory) 286 } 287 }) 288 } 289 } 290 } 291 292 func TestStatBuffCastRejectsIncompleteOrSpecialContractsWithoutDebit(t *testing.T) { 293 for _, scenario := range []string{"extra effect", "unsupported stat", "negative ability", "negative health", "negative skill", "conditional effect", "repeated effect", "interval", 294 "wrong target", "unknown section", "wrong mode", "bad weights", "special flags", "zero duration", "missing effect", "wrong arguments"} { 295 t.Run(scenario, func(t *testing.T) { 296 original := gamedata.NanoTemplates[27144] 297 definition := *original 298 definition.Sections, definition.Stats = slices.Clone(original.Sections), slices.Clone(original.Stats) 299 functions := slices.Clone(*definition.Sections[2].Functions) 300 functions[0].Arguments = slices.Clone(functions[0].Arguments) 301 definition.Sections[2].Functions = &functions 302 switch scenario { 303 case "extra effect": 304 functions = append(functions, gamedata.ItemFunction{ID: 53002}) 305 case "unsupported stat": 306 functions[0].Arguments[0].Integer = new(int32(221)) 307 case "negative ability": 308 functions[0].Arguments[0].Integer, functions[0].Arguments[1].Integer = new(int32(16)), new(int32(-3)) 309 case "negative health": 310 functions[0].Arguments[0].Integer, functions[0].Arguments[1].Integer = new(int32(1)), new(int32(-3)) 311 case "negative skill": 312 functions[0].Arguments[0].Integer, functions[0].Arguments[1].Integer = new(int32(100)), new(int32(-3)) 313 case "conditional effect": 314 functions[0].Requirements = []gamedata.ItemRequirement{{Stat: 54, Operator: 2, Value: 1}} 315 case "repeated effect": 316 functions[0].TickCount = 2 317 case "interval": 318 functions[0].TickInterval = 100 319 case "wrong target": 320 functions[0].Target = 4 321 case "unknown section": 322 definition.Sections = append(definition.Sections, gamedata.ItemSection{Type: 20}) 323 case "wrong mode": 324 modes := [][2]uint32{{12, 3}} 325 definition.Sections[1].Stats = &modes 326 case "bad weights": 327 weights := [][2]uint32{{999, 100}} 328 entries := slices.Clone(*definition.Sections[0].Entries) 329 entries[0].Stats = &weights 330 definition.Sections[0].Entries = &entries 331 case "special flags", "zero duration": 332 for index, stat := range definition.Stats { 333 if scenario == "special flags" && stat[0] == 0 || scenario == "zero duration" && stat[0] == 8 { 334 definition.Stats[index][1] = 0 335 } 336 } 337 case "missing effect": 338 functions = nil 339 case "wrong arguments": 340 functions[0].Arguments = nil 341 } 342 gamedata.NanoTemplates[27144] = &definition 343 t.Cleanup(func() { gamedata.NanoTemplates[27144] = original }) 344 world, conn := newStatBuffWorld(t) 345 player, client := world.characters[1], world.clients[1] 346 player.inventory.LearnedNanos = []uint32{27144} 347 348 err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27144})[20:]) 349 350 if err != nil || client.nanoCast != nil || player.nano != 1000 || player.nanoReserved != 0 || len(player.activeNanos) != 0 || 351 len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 108 { 352 t.Fatal("an incomplete buff reserved nano or applied partial effects", err, conn.packets) 353 } 354 }) 355 } 356 }