server/mission_encounters_test.go (10010B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "encoding/binary" 7 "fmt" 8 "math" 9 "reflect" 10 "testing" 11 "time" 12 ) 13 14 func newMissionEncounterOfferWorld(t *testing.T) (*server, *combatTestConn) { 15 t.Helper() 16 encounters := gamedata.MissionTemplates[3221228472].Encounters 17 world, output := newMissionOfferWorld(t) 18 definition := gamedata.MissionTemplates[3221228472] 19 definition.Encounters = encounters 20 gamedata.MissionTemplates[3221228472] = definition 21 world.nextNPC = 2000000 22 if err := world.handleMissionOffers(world.clients[1], missionOffersRequest()); err != nil { 23 t.Fatal(err) 24 } 25 world.clients[1].missionOffer.seed = 2 26 output.packets = nil 27 return world, output 28 } 29 30 func TestMissionEncounterAcceptanceCommitsConcretePopulationAndRestoresIt(t *testing.T) { 31 world, output := newMissionEncounterOfferWorld(t) 32 client := world.clients[1] 33 request := missionAcceptanceRequest(*client.missionOffer) 34 35 if err := world.handleMissionAcceptance(client, request); err != nil { 36 t.Fatal(err) 37 } 38 cold := reloadMissionEntranceWorld(t) 39 mission := world.characters[1].missions[0] 40 41 if world.nextNPC != 2000005 || cold.nextNPC != 2000005 || len(world.mobileNPCs[65536]) != 5 || 42 len(cold.mobileNPCs[65536]) != 5 || !reflect.DeepEqual(mission, cold.characters[1].missions[0]) || len(output.packets) != 2 { 43 t.Fatal("accepted population or contract changed on restart", world.nextNPC, cold.nextNPC, output.packets) 44 } 45 for index, z := range []float32{46, 43, 36, 28, 20} { 46 id := 2000000 + uint32(index) 47 npc, restored := world.characters[id], cold.characters[id] 48 if npc == nil || restored == nil || npc.id != id || npc.playfield != 65536 || npc.level != 1 || npc.monsterData != 17655 || 49 npc.name != "Leet" || npc.health != 20 || npc.position[0] != 15 || npc.position[2] != z || npc.combatProfile == nil || 50 npc.motion == nil || npc.motion.roaming || npc.motion.home != npc.position || 51 npc.npcDefinitionID != "950/district/2/group/0/authored_point" || npc.worldState(true, time.Time{}) != restored.worldState(true, time.Time{}) { 52 t.Fatal("selected encounter lost its admitted profile, placement or identity", id, npc, restored) 53 } 54 height := float32(.011) 55 if index == 4 { 56 height = 1.476895 57 } 58 if math.Abs(float64(npc.position[1]-height)) > .000001 { 59 t.Fatal("encounter was placed beneath its actual mesh floor", npc.position) 60 } 61 } 62 for _, packet := range output.packets { 63 if binary.BigEndian.Uint32(packet[16:20]) == 0x29304349 { 64 t.Fatal("private population leaked into the exterior") 65 } 66 } 67 } 68 69 func TestMissionEncounterInvalidSelectionCannotConsumeOfferOrKey(t *testing.T) { 70 for _, scenario := range []string{"unknown definition", "unsupported level", "empty positions", "outside", "nan", "identities"} { 71 t.Run(scenario, func(t *testing.T) { 72 world, output := newMissionEncounterOfferWorld(t) 73 client := world.clients[1] 74 definition := client.missionOffer.definition 75 definition.Encounters = []gamedata.MissionEncounter{{Definition: "950/district/2/group/0/authored_point", Level: 1, Positions: [][3]float32{{15, 0, 43}}}} 76 switch scenario { 77 case "unknown definition": 78 definition.Encounters[0].Definition = "missing" 79 case "unsupported level": 80 definition.Encounters[0].Level = 11 81 case "empty positions": 82 definition.Encounters[0].Positions = nil 83 case "outside": 84 definition.Encounters[0].Positions[0] = [3]float32{500, 0, 500} 85 case "nan": 86 definition.Encounters[0].Positions[0][0] = float32(math.NaN()) 87 case "identities": 88 world.nextNPC = math.MaxUint32 89 } 90 client.missionOffer.definition = definition 91 before, err := readTestItemStore(world) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 failure := world.handleMissionAcceptance(client, missionAcceptanceRequest(*client.missionOffer)) 97 after, err := readTestItemStore(world) 98 99 if failure == nil || err != nil || !bytes.Equal(before, after) || len(world.characters) != 2 || 100 client.missionOffer == nil || len(world.characters[1].missions) != 0 || len(output.packets) != 0 { 101 t.Fatal("unsupported encounter consumed the offer or published partial state", failure, err) 102 } 103 }) 104 } 105 } 106 107 func TestMissionEncounterAcceptanceRollsBackPopulationAndRetries(t *testing.T) { 108 for _, statement := range []string{ 109 "CREATE TRIGGER reject_population BEFORE INSERT ON characters WHEN NEW.id>=2000000 BEGIN SELECT RAISE(ABORT, 'actor'); END", 110 "CREATE TRIGGER reject_population BEFORE INSERT ON characters WHEN NEW.id>=2000000 BEGIN SELECT RAISE(IGNORE); END", 111 "CREATE TRIGGER reject_population BEFORE INSERT ON npcs WHEN NEW.id>=2000000 BEGIN SELECT RAISE(ABORT, 'npc'); END", 112 "CREATE TRIGGER reject_population BEFORE INSERT ON npcs WHEN NEW.id>=2000000 BEGIN SELECT RAISE(IGNORE); END", 113 "CREATE TRIGGER reject_population BEFORE UPDATE OF last_npc_id ON world BEGIN SELECT RAISE(IGNORE); END", 114 "CREATE TRIGGER reject_population AFTER UPDATE OF last_item_id ON world BEGIN UPDATE characters SET level=2 WHERE id=2000000; END", 115 "CREATE TRIGGER reject_population AFTER UPDATE OF last_item_id ON world BEGIN DELETE FROM npcs WHERE id=2000000; END", 116 "CREATE TABLE pending_population (id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_population AFTER INSERT ON npcs WHEN NEW.id>=2000000 BEGIN INSERT INTO pending_population VALUES (999999); END", 117 } { 118 t.Run(statement, func(t *testing.T) { 119 world, output := newMissionEncounterOfferWorld(t) 120 client := world.clients[1] 121 request := missionAcceptanceRequest(*client.missionOffer) 122 before, err := readTestItemStore(world) 123 if err != nil { 124 t.Fatal(err) 125 } 126 if _, err := world.db.Exec(statement); err != nil { 127 t.Fatal(err) 128 } 129 130 failure := world.handleMissionAcceptance(client, request) 131 cold := newTestInventoryServer(t) 132 after, err := readTestItemStore(world) 133 134 if failure == nil || err != nil || !bytes.Equal(before, after) || world.nextNPC != 2000000 || 135 len(world.characters) != 2 || len(cold.characters) != 2 || len(world.characters[1].missions) != 0 || 136 len(cold.characters[1].missions) != 0 || len(world.mobileNPCs) != 0 || len(output.packets) != 0 || client.missionOffer == nil { 137 t.Fatal("failed acceptance leaked encounter state, allocation or publication", failure, err) 138 } 139 140 if _, err := world.db.Exec("DROP TRIGGER reject_population"); err != nil { 141 t.Fatal(err) 142 } 143 if err := world.handleMissionAcceptance(client, request); err != nil { 144 t.Fatal(err) 145 } 146 cold = newTestInventoryServer(t) 147 148 if world.nextNPC != 2000010 || cold.nextNPC != 2000010 || len(world.characters) != 7 || len(cold.characters) != 7 || 149 len(output.packets) != 2 || len(world.mobileNPCs[65536]) != 5 { 150 t.Fatal("retry did not commit exactly one original population", world.nextNPC, cold.nextNPC) 151 } 152 }) 153 } 154 } 155 156 func enteredMissionEncounterWorld(t *testing.T) (*server, *worldClient, *combatTestConn) { 157 t.Helper() 158 world, output := newMissionEncounterOfferWorld(t) 159 client := world.clients[1] 160 if err := world.handleMissionAcceptance(client, missionAcceptanceRequest(*client.missionOffer)); err != nil { 161 t.Fatal(err) 162 } 163 player := world.characters[1] 164 player.geometry = world.geometry 165 player.position, player.rotation = [3]float32{89.30216, 115.75, 152.5}, [4]float32{0, 1, 0, 0} 166 if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil { 167 t.Fatal(err) 168 } 169 if player.playfield != 65536 { 170 t.Fatal("encounter fixture failed ordinary keyed entry", player.playfield) 171 } 172 world.clients[1], client.ready, client.loading = client, true, false 173 player.movement = playerMovement{} 174 world.updateNPCInterest(client, time.Now()) 175 output.packets = nil 176 return world, client, output 177 } 178 179 func TestMissionEncounterPursuitCrossesPortalAndReturnsWithoutDatabaseChurn(t *testing.T) { 180 for _, id := range []uint32{2000003, 2000004} { 181 t.Run(fmt.Sprint(id), func(t *testing.T) { 182 world, _, _ := enteredMissionEncounterWorld(t) 183 player, npc := world.characters[1], world.characters[id] 184 player.position = [3]float32{15, .011, 43} 185 npc.fighting = 1 186 world.fighting[npc.id] = npc 187 start := time.Now() 188 world.lastNPCUpdate = start 189 190 for tick := 1; tick <= 32; tick++ { 191 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 192 } 193 far := npc.position 194 player.health = 0 195 for tick := 33; tick <= 80; tick++ { 196 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 197 } 198 var storedZ float32 199 if err := world.db.QueryRow("SELECT position_z FROM characters WHERE id=?", id).Scan(&storedZ); err != nil { 200 t.Fatal(err) 201 } 202 203 if far[2] < 41 || far[2] > 43 || math.Abs(float64(far[1]-.011)) > .02 || npc.fighting != 0 || npc.motion.moving || npc.motion.returning || 204 math.Hypot(float64(npc.position[0]-npc.motion.home[0]), float64(npc.position[2]-npc.motion.home[2])) >= 1 || storedZ != npc.motion.home[2] { 205 t.Fatal("indoor pursuit escaped its floor, failed to return or persisted each tick", far, npc.position, npc.motion, storedZ) 206 } 207 }) 208 } 209 } 210 211 func TestMissionEncounterPursuitCannotCrossTheOriginalRoomWall(t *testing.T) { 212 for _, tickCount := range []int{1, 80} { 213 t.Run(fmt.Sprint(tickCount), func(t *testing.T) { 214 world, _, _ := enteredMissionEncounterWorld(t) 215 player, npc := world.characters[1], world.characters[2000003] 216 player.position = [3]float32{100, .011, 28} 217 npc.fighting = 1 218 world.fighting[npc.id] = npc 219 start := time.Now() 220 world.lastNPCUpdate = start 221 222 for tick := 1; tick <= tickCount; tick++ { 223 advanceNavigationForTest(t, world, start.Add(time.Duration(tick)*250*time.Millisecond), world.advanceNPCs) 224 } 225 226 if npc.position[0] <= 15 || npc.position[0] >= 18 || math.Abs(float64(npc.position[2]-28)) > 1 || 227 npc.position[1] < .001 || npc.position[1] > 1 || world.geometry.mission(65536).roomAt(npc.position) == nil { 228 t.Fatal("NPC pursuit crossed the original mesh wall or lost its floor", npc.position) 229 } 230 }) 231 } 232 }