server/nano_casting_test.go (13647B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "slices" 7 "sync" 8 "testing" 9 "time" 10 ) 11 12 func newNanoCastingWorld(t *testing.T) (*server, *combatTestConn) { 13 t.Helper() 14 t.Chdir(t.TempDir()) 15 world, conn := newCombatWorld(t) 16 world.nextCharacter = 1 17 player := world.characters[1] 18 player.name = "Nano Caster" 19 player.profession, player.nano = 8, 30 20 player.appearance, player.level = 0x315, 1 21 player.rotation[3] = 1 22 player.stats = testCharacterStats() 23 player.stats.NanoAiding.PsychologicalModification, player.stats.NanoAiding.MatterCreation = 7, 6 24 player.health = player.maximumHealth() 25 player.inventory.LearnedNanos = []uint32{29618} 26 if err := world.saveUser(player); err != nil { 27 t.Fatal(err) 28 } 29 return world, conn 30 } 31 32 func TestWinterBiteTimesOneResultAndRecharge(t *testing.T) { 33 world, conn := newNanoCastingWorld(t) 34 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 35 request := characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 29618})[20:] 36 observer := &combatTestConn{} 37 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 38 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true, target: true, npcs: map[uint32]bool{npc.id: true}} 39 40 if err := world.handleNanoCast(client, request); err != nil { 41 t.Fatal(err) 42 } 43 deadline := client.nanoCast.readyAt 44 world.handleNanoCast(client, request) 45 world.advanceNanos(deadline) 46 if player.nano != 21 || npc.health != 100 || len(conn.packets) != 2 || len(observer.packets) != 1 { 47 t.Fatal("duplicate or early result", player.nano, npc.health) 48 } 49 world.advanceNanos(deadline.Add(time.Nanosecond)) 50 health := npc.health 51 world.advanceNanos(deadline.Add(500 * time.Millisecond)) 52 world.handleNanoCast(client, request) 53 54 if player.nano != 21 || npc.health != health || health < 77 || health > 90 || client.nanoCast != nil || player.fighting != 0 || npc.fighting != player.id { 55 t.Fatalf("cast state: nano=%d health=%d pending=%+v fights=%d/%d", player.nano, npc.health, client.nanoCast, player.fighting, npc.fighting) 56 } 57 if player.nanoRechargeUntil != deadline.Add(999999978*time.Nanosecond) || len(conn.packets) != 5 || len(observer.packets) != 4 { 58 t.Fatal("recharge or duplicate packets", player.nanoRechargeUntil.Sub(deadline), len(conn.packets), len(observer.packets)) 59 } 60 if binary.BigEndian.Uint32(conn.packets[0][41:45]) != 0 || binary.BigEndian.Uint32(conn.packets[0][45:49]) != 29618 || binary.BigEndian.Uint32(conn.packets[2][29:33]) != 107 || binary.BigEndian.Uint32(conn.packets[3][16:20]) != 0x4727213e || int32(binary.BigEndian.Uint32(conn.packets[3][29:33])) != int32(health)-100 || binary.BigEndian.Uint32(conn.packets[3][33:37]) != 95 { 61 t.Fatalf("native nano packets: %x", conn.packets) 62 } 63 if len(npc.damageContributors) != 1 || npc.damageContributors[damageContributor{player: 1}] != 100-health { 64 t.Fatal("missing damage contribution", npc.damageContributors) 65 } 66 } 67 68 func TestWinterBiteRejectsInvalidAdmission(t *testing.T) { 69 for _, scenario := range []string{"unlearned", "profession", "psychology", "creation", "nano", "sitting", "forward", "strafe", "turn", "equipment", "hidden", "dead target", "player target", "empty identity", "item identity", "wrong zone", "range", "range vertical", "wrong program", "wrong resource", "foreign owner", "loading", "not ready", "stale", "dead caster"} { 70 t.Run(scenario, func(t *testing.T) { 71 world, _ := newNanoCastingWorld(t) 72 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 73 fields := [5]uint32{19, 50000, npc.id, 53019, 29618} 74 owner := uint32(1) 75 switch scenario { 76 case "unlearned": 77 player.inventory.LearnedNanos = nil 78 case "profession": 79 player.profession = 12 80 case "psychology": 81 player.stats.NanoAiding.PsychologicalModification = 6 82 case "creation": 83 player.stats.NanoAiding.MatterCreation = 5 84 case "nano": 85 player.nano = 8 86 case "sitting": 87 player.sitting = true 88 case "forward": 89 world.characters[client.id].movement.forward = 1 90 case "strafe": 91 world.characters[client.id].movement.strafe = 1 92 case "turn": 93 world.characters[client.id].movement.turn = 1 94 case "equipment": 95 client.equipment = &equipmentMove{} 96 case "hidden": 97 delete(client.npcs, npc.id) 98 case "dead target": 99 npc.health = 0 100 case "player target": 101 npc.npc = false 102 case "empty identity": 103 fields[1], fields[2] = 0, 0 104 case "item identity": 105 fields[1] = 51018 106 case "wrong zone": 107 npc.playfield = 705 108 case "range": 109 npc.position = [3]float32{25.001, 0, 0} 110 case "range vertical": 111 npc.position = [3]float32{0, 25.001, 0} 112 case "wrong program": 113 fields[4] = 43324 114 case "wrong resource": 115 fields[3] = 1040005 116 case "foreign owner": 117 owner = 2 118 case "loading": 119 client.loading = true 120 case "not ready": 121 client.ready = false 122 case "stale": 123 world.clients[1] = &worldClient{id: 1} 124 case "dead caster": 125 player.health = 0 126 } 127 before := player.nano 128 err := world.handleNanoCast(client, characterActionPacket(owner, fields)[20:]) 129 world.advanceNanos(time.Now().Add(time.Hour)) 130 if err != nil || client.nanoCast != nil || player.nano != before || npc.health != 100 && scenario != "dead target" { 131 t.Fatal("invalid cast changed state", err, player.nano, npc.health) 132 } 133 }) 134 } 135 } 136 137 func TestWinterBiteNativeTimingAndRangeBoundary(t *testing.T) { 138 for _, scenario := range []struct { 139 agg int32 140 delay time.Duration 141 }{{0, 2575000047}, {100, 1574999928}, {-100, 3575000047}} { 142 t.Run(time.Duration(scenario.agg).String(), func(t *testing.T) { 143 world, _ := newNanoCastingWorld(t) 144 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 145 player.aggDef = scenario.agg 146 npc.position = [3]float32{15, 20, 0} 147 before := time.Now() 148 err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 29618})[20:]) 149 after := time.Now() 150 if err != nil || client.nanoCast == nil || client.nanoCast.readyAt.Before(before.Add(scenario.delay)) || client.nanoCast.readyAt.After(after.Add(scenario.delay)) { 151 t.Fatal("native timing/range", err, client.nanoCast, player.nanoCastDelay(29618)) 152 } 153 }) 154 } 155 } 156 157 func TestWinterBiteCancellationAndInvalidatedTargetRefundOnce(t *testing.T) { 158 for _, scenario := range []string{"request", "target moved", "target dead", "hidden", "lifecycle"} { 159 t.Run(scenario, func(t *testing.T) { 160 world, conn := newNanoCastingWorld(t) 161 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 162 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 29618})[20:]) 163 deadline := client.nanoCast.readyAt 164 switch scenario { 165 case "request": 166 for range 2 { 167 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{108})[20:]); err != nil { 168 t.Fatal(err) 169 } 170 } 171 case "target moved": 172 npc.position[0] = 26 173 case "target dead": 174 npc.health = 0 175 case "hidden": 176 delete(client.npcs, npc.id) 177 case "lifecycle": 178 updates := make(map[*worldClient][][]byte) 179 world.cancelCombat(1, updates) 180 world.sendCombatUpdates(updates) 181 } 182 world.advanceNanos(deadline.Add(time.Second)) 183 world.advanceNanos(deadline.Add(time.Hour)) 184 if client.nanoCast != nil || player.nano != 30 || player.nanoRechargeUntil != (time.Time{}) || npc.health != 100 && scenario != "target dead" || len(conn.packets) != 4 { 185 t.Fatal("cancel did not refund exactly once", player.nano, npc.health, len(conn.packets)) 186 } 187 }) 188 } 189 } 190 191 func TestWinterBiteDeathUsesSharedLootAndStopsPendingWeapons(t *testing.T) { 192 t.Chdir(t.TempDir()) 193 world, conn := newNanoCastingWorld(t) 194 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 195 npc.health, npc.monsterData = 1, 17655 196 npc.combatProfile = &gamedata.NPCCombatProfile{CorpseSeconds: 60, RespawnSeconds: 90} 197 npc.damageContributors = map[damageContributor]uint32{{player: 2}: 10} 198 npc.leadingContributor = damageContributor{player: 2} 199 world.handleCombat(client, attackPacket(1, npc.id)[20:]) 200 player.rechargeUntil = time.Now().Add(time.Second) 201 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 29618})[20:]) 202 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 203 if npc.health != 0 || npc.corpse == nil || npc.corpse.looter != 2 || len(world.corpseExpiry) != 1 || len(world.npcRespawns) != 1 || len(world.fighting) != 0 || player.fighting != 0 { 204 t.Fatalf("nano death lost shared lifecycle: %+v", npc) 205 } 206 last := conn.packets[len(conn.packets)-1] 207 if binary.BigEndian.Uint32(last[29:33]) != 153 || binary.BigEndian.Uint32(last[45:49]) != 5 { 208 t.Fatalf("final death reset must follow queued weapon drain: %x", conn.packets) 209 } 210 } 211 212 func TestWinterBiteConcurrentRequestsReserveOneCost(t *testing.T) { 213 world, conn := newNanoCastingWorld(t) 214 client := world.clients[1] 215 request := characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29618})[20:] 216 var workers sync.WaitGroup 217 zone, stop := startTestZone(t, world) 218 219 for range 8 { 220 workers.Go(func() { 221 if err := zone.call(zonePacket{client, request}); err != nil { 222 t.Error(err) 223 } 224 }) 225 } 226 workers.Wait() 227 if err := stop(); err != nil { 228 t.Fatal(err) 229 } 230 if client.nanoCast == nil || world.characters[1].nano != 21 || len(conn.packets) != 2 { 231 t.Fatal("concurrent cast duplicated admission") 232 } 233 } 234 235 func TestWinterBiteCrystalPersistsLearnedProgram(t *testing.T) { 236 world, _ := newNanoLearningWorld(t) 237 player := world.characters[1] 238 player.profession = 8 239 player.stats.NanoAiding.PsychologicalModification = 7 240 player.inventory.Items = nil 241 if ok, err := world.grantItem(player, 29625); !ok || err != nil { 242 t.Fatal(ok, err) 243 } 244 err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 2)[20:]) 245 restored := newTestInventoryServer(t).characters[1] 246 if err != nil || !slices.Equal(restored.inventory.LearnedNanos, []uint32{29618}) || len(restored.inventory.Items) != 0 { 247 t.Fatal("winter crystal did not persist consumption and learning", err, restored.inventory) 248 } 249 } 250 251 func TestWinterBiteMovementCancelsBeforeDelayedResult(t *testing.T) { 252 for _, action := range []uint32{1, 3, 5, 7, 13} { 253 t.Run(time.Duration(action).String(), func(t *testing.T) { 254 t.Chdir(t.TempDir()) 255 world, _ := newNanoCastingWorld(t) 256 player, client := world.characters[1], world.clients[1] 257 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29618})[20:]) 258 deadline := client.nanoCast.readyAt 259 body := encodeMovement1491(npcMovementPacket(player, action)[36:]) 260 err := world.handleMovement(client, append(make([]byte, 16), body...)) 261 world.advanceNanos(deadline.Add(time.Second)) 262 if err != nil || client.nanoCast != nil || player.nano != 30 || world.characters[1000000].health != 100 { 263 t.Fatal("movement left delayed damage", err, player.nano) 264 } 265 }) 266 } 267 } 268 269 func TestWinterBiteWeaponRechargeCannotAccelerateAnAttack(t *testing.T) { 270 world, _ := newNanoCastingWorld(t) 271 player, client, npc := world.characters[1], world.clients[1], world.characters[1000000] 272 world.handleCombat(client, attackPacket(1, npc.id)[20:]) 273 player.rechargeUntil = time.Now().Add(time.Second) 274 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, npc.id, 53019, 29618})[20:]) 275 deadline := client.nanoCast.readyAt 276 next := player.nextAttack 277 world.advanceCombat(deadline) 278 world.handleCombat(client, stopFightPacket(1)[20:]) 279 world.handleCombat(client, attackPacket(1, npc.id)[20:]) 280 if npc.health != 100 || player.nextAttack != next || !next.After(deadline) { 281 t.Fatal("cast/start/stop bypassed weapon timing", next, player.nextAttack, deadline, npc.health) 282 } 283 } 284 285 func TestWinterBiteProfileChangeRefundsPendingCast(t *testing.T) { 286 t.Chdir(t.TempDir()) 287 world, _ := newNanoCastingWorld(t) 288 player, client := world.characters[1], world.clients[1] 289 if err := world.saveUser(player); err != nil { 290 t.Fatal(err) 291 } 292 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29618})[20:]) 293 deadline := client.nanoCast.readyAt 294 destination := player.userData() 295 destination.Playfield = 705 296 err := world.updateUser(destination) 297 world.advanceNanos(deadline.Add(time.Second)) 298 if err != nil || client.nanoCast != nil || player.nano != 30 || world.characters[1000000].health != 100 || player.playfield != 705 { 299 t.Fatal("profile update lost refund or kept delayed cast", err, player.nano, player.playfield) 300 } 301 } 302 303 func TestWinterBiteCrystalRejectsNativeRequirementBoundaries(t *testing.T) { 304 for _, scenario := range []string{"psychology 8", "creation 7", "profession"} { 305 t.Run(scenario, func(t *testing.T) { 306 world, _ := newNanoLearningWorld(t) 307 player := world.characters[1] 308 player.profession = 8 309 player.stats.NanoAiding.PsychologicalModification = 7 310 player.inventory.Items = nil 311 if ok, err := world.grantItem(player, 29625); !ok || err != nil { 312 t.Fatal(ok, err) 313 } 314 switch scenario { 315 case "psychology 8": 316 player.stats.NanoAiding.PsychologicalModification = 6 317 case "creation 7": 318 player.stats.NanoAiding.MatterCreation = 5 319 case "profession": 320 player.profession = 12 321 } 322 err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 2)[20:]) 323 if err != nil || len(player.inventory.LearnedNanos) != 0 || len(player.inventory.Items) != 1 || player.inventory.Items[0].Template != 29625 { 324 t.Fatal("invalid winter upload consumed crystal", err, player.inventory) 325 } 326 }) 327 } 328 }