server/omni_training_boss_test.go (7540B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "slices" 8 "testing" 9 "time" 10 ) 11 12 func TestOmniTrainingBossIsTheOnlyAdmittedAquaanPopulation(t *testing.T) { 13 spawns, err := readNPCSpawns() 14 if err != nil { 15 t.Fatal(err) 16 } 17 var admitted []string 18 for _, npc := range spawns { 19 if npc.monsterData == 30369 && npc.combatProfile != nil { 20 admitted = append(admitted, npc.npcDefinitionID) 21 } 22 } 23 if !slices.Equal(admitted, []string{"950/district/6/fixed/0"}) { 24 t.Fatal("Omni profile spread to another authored population", admitted) 25 } 26 data := testNPCData() 27 data.Playfields[0].Districts[0].Groups[0] = *gamedata.NPCGroups["950/district/6/fixed/0"] 28 useNPCData(t, data) 29 world := newTestServer(t) 30 if err := world.flushWorld(); err != nil { 31 t.Fatal(err) 32 } 33 34 cold := newTestInventoryServer(t) 35 36 for _, current := range []*server{world, cold} { 37 npc := current.characters[1000000] 38 if npc.name != "Aquaan Boss" || npc.level != 4 || npc.monsterData != 30369 || npc.health != 40 || 39 npc.combatProfile == nil || npc.attackRating() != 15 || itemStat(npc.attackWeapon(), 286) != 7 || itemStat(npc.attackWeapon(), 285) != 16 || 40 npc.motion == nil || npc.motion.home != npc.position || npc.motion.returning { 41 t.Fatal("authored Omni boss lost its explicit combat contract", npc) 42 } 43 packet := targetPacket(characterPacket(npc)) 44 end := int(binary.BigEndian.Uint16(packet[6:8])) 45 if binary.BigEndian.Uint32(packet[end-16:end-12]) != 120715 || binary.BigEndian.Uint32(packet[end-12:end-8]) != 120716 || 46 binary.BigEndian.Uint32(packet[end-4:end]) != 4 { 47 t.Fatal("native boss initialization lost its selected weapon endpoints or quality") 48 } 49 } 50 } 51 52 func TestOmniTrainingBossProfileRejectsOtherBindings(t *testing.T) { 53 for _, scenario := range []string{"other code", "placeholder", "other body", "level 3", "level 5", "outdoor guardian"} { 54 t.Run(scenario, func(t *testing.T) { 55 data := testNPCData() 56 group := &data.Playfields[0].Districts[0].Groups[0] 57 group.TemplateCode, group.MonsterData, group.Levels, group.AppearanceStatus = "BNMO", 30369, [2]uint16{4, 4}, "best_effort" 58 switch scenario { 59 case "other code": 60 group.TemplateCode = "AQ01" 61 case "placeholder": 62 group.AppearanceStatus = "placeholder_creature" 63 case "other body": 64 group.MonsterData = 30371 65 case "level 3": 66 group.Levels = [2]uint16{3, 3} 67 case "level 5": 68 group.Levels = [2]uint16{5, 5} 69 case "outdoor guardian": 70 *group = *gamedata.NPCGroups["716/district/60/fixed/0"] 71 } 72 useNPCData(t, data) 73 world := newTestServer(t) 74 if err := world.flushWorld(); err != nil { 75 t.Fatal(err) 76 } 77 78 cold := newTestInventoryServer(t) 79 80 if world.characters[1000000].combatProfile != nil || cold.characters[1000000].combatProfile != nil { 81 t.Fatal("an unreviewed Aquaan binding inherited training-boss combat or loot") 82 } 83 }) 84 } 85 } 86 87 func newOmniTrainingBossKillWorld(t *testing.T, attack string) *server { 88 t.Helper() 89 world, _ := newKillExperienceWorld(t, attack) 90 target := world.characters[1000000] 91 target.monsterData, target.level, target.npcDefinitionID = 30369, 4, "950/district/6/fixed/0" 92 // Isolate equipment quality/transaction scenarios from additional sale-only drops. 93 profile := *gamedata.NPCCombatProfiles[30369][4] 94 profile.Loot = slices.Clone(profile.Loot[:2]) 95 target.combatProfile = &profile 96 if err := world.flushWorld(); err != nil { 97 t.Fatal(err) 98 } 99 return world 100 } 101 102 func TestOmniTrainingBossDeathsSelectSupportedEquipmentQualities(t *testing.T) { 103 for _, attack := range []string{"unarmed", "axe", "pistol", "fling", "fast", "nano"} { 104 for quality := uint32(3); quality <= 5; quality++ { 105 t.Run(fmt.Sprintf("%s/QL%d", attack, quality), func(t *testing.T) { 106 world := newOmniTrainingBossKillWorld(t, attack) 107 world.combatRoll = func(limit uint32) uint32 { 108 if limit == 3 { 109 return quality - 3 110 } 111 return 0 112 } 113 before := world.nextItem 114 115 attackForExperience(t, world, attack) 116 world.advanceCombat(time.Now().Add(time.Hour)) 117 world.advanceNanos(time.Now().Add(time.Hour)) 118 cold := newTestInventoryServer(t) 119 120 want := []ownedItem{ 121 {ID: before + 1, Type: 51022, Template: 85596, Quality: quality, Count: 1}, 122 {ID: before + 2, Type: 51018, Template: 123742, Quality: quality, Count: 1, Slot: 1}, 123 } 124 for _, current := range []*server{world, cold} { 125 corpse := current.characters[1000000].corpse 126 if corpse == nil || !slices.Equal(corpse.items, want) || corpse.credits != 1 || current.characters[1].experience != 150 || current.nextItem != before+2 { 127 t.Fatal("Omni boss death changed selected qualities, omitted rewards or repeated allocation", corpse) 128 } 129 } 130 }) 131 } 132 } 133 } 134 135 func TestOmniTrainingBossCanRollIndependentOrEmptyLoot(t *testing.T) { 136 for _, scenario := range []struct { 137 roll uint32 138 count int 139 }{{0, 2}, {59, 2}, {60, 1}, {74, 1}, {75, 0}, {99, 0}} { 140 t.Run(fmt.Sprint(scenario.roll), func(t *testing.T) { 141 world := newOmniTrainingBossKillWorld(t, "unarmed") 142 world.combatRoll = func(limit uint32) uint32 { return min(scenario.roll, limit-1) } 143 before := world.nextItem 144 145 attackForExperience(t, world, "unarmed") 146 cold := newTestInventoryServer(t) 147 148 for _, current := range []*server{world, cold} { 149 corpse := current.characters[1000000].corpse 150 if corpse == nil || len(corpse.items) != scenario.count || current.nextItem != before+uint32(scenario.count) || current.characters[1].experience != 150 { 151 t.Fatal("membership boundary lost an empty result or changed the reward allocation", corpse) 152 } 153 } 154 }) 155 } 156 } 157 158 func TestOmniTrainingBossFailedSecondDropRetriesAtomically(t *testing.T) { 159 for _, failure := range []struct{ name, sql string }{ 160 {"insert", "CREATE TRIGGER fail_omni_boss BEFORE INSERT ON items WHEN NEW.template=123742 BEGIN SELECT RAISE(FAIL,'second drop'); END"}, 161 {"ignored placement", "CREATE TRIGGER fail_omni_boss BEFORE INSERT ON item_locations WHEN NEW.slot=1 BEGIN SELECT RAISE(IGNORE); END"}, 162 {"commit", "CREATE TABLE omni_boss_commit (id INTEGER REFERENCES items(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_omni_boss AFTER INSERT ON items WHEN NEW.template=123742 BEGIN INSERT INTO omni_boss_commit VALUES(999999); END"}, 163 } { 164 t.Run(failure.name, func(t *testing.T) { 165 world := newOmniTrainingBossKillWorld(t, "unarmed") 166 world.combatRoll = func(uint32) uint32 { return 0 } 167 before := world.nextItem 168 if _, err := world.db.Exec(failure.sql); err != nil { 169 t.Fatal(err) 170 } 171 172 attackForExperience(t, world, "unarmed") 173 cold := newTestInventoryServer(t) 174 175 for _, current := range []*server{world, cold} { 176 if current.characters[1000000].health != 1 || current.characters[1000000].corpse != nil || current.nextItem != before || current.characters[1].experience != 0 { 177 t.Fatal("failed second reward committed a partial death or reward") 178 } 179 } 180 181 // Failed reservations leave gaps; retry commits fresh item identities. 182 if _, err := world.db.Exec("DROP TRIGGER fail_omni_boss"); err != nil { 183 t.Fatal(err) 184 } 185 attackForExperience(t, world, "unarmed") 186 retried := newTestInventoryServer(t) 187 corpse := retried.characters[1000000].corpse 188 if corpse == nil || len(corpse.items) != 2 || corpse.items[0].ID != before+3 || corpse.items[1].ID != before+4 || 189 corpse.items[0].Quality != 3 || corpse.items[1].Quality != 3 || retried.characters[1].experience != 150 { 190 t.Fatal("retry lost selected qualities or repeated instance allocation", corpse) 191 } 192 }) 193 } 194 }