server/regeneration_test.go (12800B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "testing" 8 "time" 9 ) 10 11 func newRegenerationWorld(t *testing.T) (*server, *combatTestConn, time.Time) { 12 t.Helper() 13 world, conn := newCombatWorld(t) 14 world.characters[1].appearance = 1 << 4 15 world.characters[1].level = 1 16 world.characters[1].health = 20 17 start := time.Unix(100, 0) 18 world.clients[1].regenerationAt = start 19 return world, conn, start 20 } 21 22 func TestRegenerationStandingUsesNativeCounterAndNanoInterval(t *testing.T) { 23 world, _, start := newRegenerationWorld(t) 24 25 for second := 2; second <= 56; second += 2 { 26 world.advanceRegeneration(start.Add(time.Duration(second) * time.Second)) 27 } 28 world.advanceRegeneration(start.Add(58*time.Second - time.Nanosecond)) 29 if world.characters[1].health != 20 || world.characters[1].nano != 6 { 30 t.Fatalf("before health tick: %+v", world.characters[1]) 31 } 32 world.advanceRegeneration(start.Add(58 * time.Second)) 33 34 if world.characters[1].health != 23 || world.characters[1].nano != 6 { 35 t.Fatalf("standing recovery: %+v", world.characters[1]) 36 } 37 } 38 39 func TestRegenerationSittingHalvesHealthInterval(t *testing.T) { 40 world, _, start := newRegenerationWorld(t) 41 world.characters[1].sitting = true 42 43 for second := 2; second <= 14; second += 2 { 44 world.advanceRegeneration(start.Add(time.Duration(second) * time.Second)) 45 } 46 47 if world.characters[1].health != 23 || world.characters[1].nano != 0 { 48 t.Fatalf("sitting recovery: %+v", world.characters[1]) 49 } 50 } 51 52 func TestRegenerationCombatPublishesOnlyActualHealthChanges(t *testing.T) { 53 world, conn, start := newRegenerationWorld(t) 54 if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil { 55 t.Fatal(err) 56 } 57 conn.packets = nil 58 59 for second := 2; second <= 116; second += 2 { 60 world.advanceRegeneration(start.Add(time.Duration(second) * time.Second)) 61 } 62 if world.characters[1].health != 20 || world.characters[1].nano != 6 { 63 t.Fatalf("before combat health tick: %+v", world.characters[1]) 64 } 65 world.advanceRegeneration(start.Add(118 * time.Second)) 66 67 if world.characters[1].health != 23 || world.characters[1].nano != 6 { 68 t.Fatalf("combat recovery: %+v", world.characters[1]) 69 } 70 healthSnapshots := 0 71 for _, packet := range conn.packets { 72 if binary.BigEndian.Uint32(packet[16:20]) == 0x3710256c { 73 healthSnapshots++ 74 if !bytes.Equal(packet[29:45], []byte{0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) { 75 t.Fatal("combat recovery did not publish its exact zero-feedback health", packet) 76 } 77 } else if binary.BigEndian.Uint32(packet[16:20]) != 0x2b333d6e || binary.BigEndian.Uint32(packet[29:33]) != 214 { 78 t.Fatalf("combat recovery emitted an unexpected packet: %x", packet) 79 } 80 } 81 if healthSnapshots != 1 { 82 t.Fatal("actual combat recovery was withheld or unchanged health was published repeatedly", healthSnapshots) 83 } 84 } 85 86 func TestRegenerationIncomingCombatPublishesRecoveryWithoutChangingAttack(t *testing.T) { 87 for _, bodyDevelopment := range []uint16{10, 80} { 88 t.Run(fmt.Sprint(bodyDevelopment), func(t *testing.T) { 89 world, conn, start := newRegenerationWorld(t) 90 player, npc := world.characters[1], world.characters[1000000] 91 player.level = 20 92 player.stats.Body.BodyDevelopment = bodyDevelopment 93 counter := uint32(58) 94 if bodyDevelopment == 80 { 95 player.stats.Abilities.Stamina = 41 96 counter = 56 97 } 98 if uint32(bodyDevelopment) > player.trainingMaximum(152) || uint32(player.stats.Abilities.Stamina) > player.trainingMaximum(18) { 99 t.Fatal("recovery fixture exceeds the recovered training caps") 100 } 101 npc.regenerationAt = start.Add(time.Hour) 102 peer := &combatTestConn{} 103 world.characters[2] = &character{id: 2, playfield: 950, health: 100} 104 world.clients[2] = &worldClient{id: 2, conn: peer, target: true, ready: true, players: map[uint32]bool{1: true}, regenerationAt: start.Add(time.Hour)} 105 updates := make(map[*worldClient][][]byte) 106 world.startAttack(npc, player, start, updates) 107 world.sendCombatUpdates(updates) 108 clocks := [2]time.Time{npc.nextAttack, npc.rechargeUntil} 109 world.clients[1].regenerationSeconds = counter - 2 110 conn.packets, peer.packets = nil, nil 111 112 world.advanceRegeneration(start.Add(2 * time.Second)) 113 world.advanceRegeneration(start.Add(2 * time.Second)) 114 115 if player.health != 23 || world.clients[1].regenerationSeconds != counter || player.fighting != 0 || npc.fighting != 1 || 116 clocks != [2]time.Time{npc.nextAttack, npc.rechargeUntil} { 117 t.Fatal("recovery changed its amount, phase or incoming attacker") 118 } 119 if len(conn.packets) != 2 || len(peer.packets) != 1 || 120 binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3710256c || 121 !bytes.Equal(conn.packets[0][29:45], []byte{0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) || 122 !bytes.Equal(conn.packets[0][16:], peer.packets[0][16:]) { 123 t.Fatal("incoming recovery did not publish one zero-feedback health snapshot to both clients") 124 } 125 }) 126 } 127 } 128 129 func TestRegenerationDoesNotCorrectHealthWhileOnlyTheEnemyAttacks(t *testing.T) { 130 world, conn, start := newRegenerationWorld(t) 131 world.characters[1000000].fighting = 1 132 world.fighting[1000000] = world.characters[1000000] 133 134 world.advanceRegeneration(start.Add(2 * time.Second)) 135 136 if len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 214 { 137 t.Fatalf("absolute HP interrupted an incoming hit: %x", conn.packets) 138 } 139 } 140 141 func TestRegenerationCorrectsAbsoluteIdleResourcesForOwnerAndObserver(t *testing.T) { 142 world, conn, start := newRegenerationWorld(t) 143 observer := &combatTestConn{} 144 world.characters[2] = &character{id: 2, playfield: 950} 145 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true} 146 147 world.advanceRegeneration(start.Add(2 * time.Second)) 148 149 if len(conn.packets) != 2 || len(observer.packets) != 1 { 150 t.Fatalf("resource recipients: owner=%x observer=%x", conn.packets, observer.packets) 151 } 152 if binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3710256c || 153 !bytes.Equal(conn.packets[0][29:45], []byte{0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) || 154 !bytes.Equal(conn.packets[0][16:], observer.packets[0][16:]) || 155 binary.BigEndian.Uint32(conn.packets[1][29:33]) != 214 || binary.BigEndian.Uint32(conn.packets[1][33:37]) != 0 { 156 t.Fatalf("absolute resource correction: %x", conn.packets) 157 } 158 } 159 160 func TestRegenerationFullHealthReconcilesEveryTenSeconds(t *testing.T) { 161 world, conn, start := newRegenerationWorld(t) 162 world.characters[1000000].regenerationAt = start.Add(time.Hour) 163 world.characters[1].health = world.characters[1].maximumHealth() 164 observer := &combatTestConn{} 165 world.characters[2] = &character{id: 2, playfield: 950} 166 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, regenerationAt: start.Add(time.Hour)} 167 168 for second := 2; second <= 8; second += 2 { 169 world.advanceRegeneration(start.Add(time.Duration(second) * time.Second)) 170 } 171 if len(observer.packets) != 0 || len(conn.packets) != 4 { 172 t.Fatal("stable full health was broadcast or owner nano corrections were lost") 173 } 174 world.advanceRegeneration(start.Add(10 * time.Second)) 175 176 if len(observer.packets) != 1 || binary.BigEndian.Uint32(observer.packets[0][16:20]) != 0x3710256c || 177 binary.BigEndian.Uint32(observer.packets[0][29:33]) != 37 { 178 t.Fatal("full health did not receive its periodic reconciliation", observer.packets) 179 } 180 } 181 182 func TestRegenerationInjuryResumesQuietCorrections(t *testing.T) { 183 world, conn, start := newRegenerationWorld(t) 184 world.characters[1000000].regenerationAt = start.Add(time.Hour) 185 world.characters[1].health = world.characters[1].maximumHealth() 186 world.advanceRegeneration(start.Add(2 * time.Second)) 187 world.characters[1].health = 20 188 conn.packets = nil 189 190 world.advanceRegeneration(start.Add(4 * time.Second)) 191 192 if len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3710256c || 193 binary.BigEndian.Uint32(conn.packets[0][29:33]) != 20 { 194 t.Fatal("injury waited for the full-health reconciliation interval", conn.packets) 195 } 196 } 197 198 func TestRegenerationClampsHealthAndNanoToNativeMaximums(t *testing.T) { 199 world, conn, start := newRegenerationWorld(t) 200 world.characters[1].health = 36 201 world.characters[1].nano = 34 202 world.clients[1].regenerationSeconds = 810 203 204 world.advanceRegeneration(start.Add(2 * time.Second)) 205 206 if world.characters[1].health != 37 || world.characters[1].nano != 35 { 207 t.Fatalf("resource overflow: %+v", world.characters[1]) 208 } 209 if len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3710256c || 210 binary.BigEndian.Uint32(conn.packets[0][29:33]) != 37 { 211 t.Fatal("actual healing to full health waited for periodic reconciliation", conn.packets) 212 } 213 } 214 215 func TestRegenerationSkipsDeadLoadingOfflineAndNPCCharacters(t *testing.T) { 216 for _, scenario := range []string{"dead", "loading", "not ready", "offline", "NPC"} { 217 t.Run(scenario, func(t *testing.T) { 218 world, conn, start := newRegenerationWorld(t) 219 world.clients[1].regenerationSeconds = 810 220 world.characters[1].resurrectionShock = 5000 221 switch scenario { 222 case "dead": 223 world.characters[1].health = 0 224 case "loading": 225 world.clients[1].loading = true 226 case "not ready": 227 world.clients[1].ready = false 228 case "offline": 229 delete(world.clients, 1) 230 case "NPC": 231 world.characters[1].npc = true 232 } 233 health := world.characters[1].health 234 235 world.advanceRegeneration(start.Add(2 * time.Second)) 236 237 if world.characters[1].health != health || world.characters[1].nano != 0 || world.characters[1].resurrectionShock != 5000 || len(conn.packets) != 0 { 238 t.Fatalf("%s regenerated: %+v packets=%x", scenario, world.characters[1], conn.packets) 239 } 240 }) 241 } 242 } 243 244 func TestRegenerationLateUpdateDoesNotReplayMissedTicks(t *testing.T) { 245 world, _, start := newRegenerationWorld(t) 246 247 world.advanceRegeneration(start.Add(time.Hour)) 248 world.advanceRegeneration(start.Add(time.Hour)) 249 250 if world.characters[1].health != 20 || world.characters[1].nano != 0 || world.clients[1].regenerationSeconds != 2 { 251 t.Fatalf("late update replayed recovery: %+v", world.characters[1]) 252 } 253 } 254 255 func TestRegenerationStoppingAttackPreservesCounterPhase(t *testing.T) { 256 world, _, start := newRegenerationWorld(t) 257 if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil { 258 t.Fatal(err) 259 } 260 for second := 2; second <= 56; second += 2 { 261 world.advanceRegeneration(start.Add(time.Duration(second) * time.Second)) 262 } 263 264 if err := world.handleCombat(world.clients[1], stopFightPacket(1)[20:]); err != nil { 265 t.Fatal(err) 266 } 267 world.advanceRegeneration(start.Add(58 * time.Second)) 268 269 if world.characters[1].health != 23 || world.clients[1].regenerationSeconds != 58 { 270 t.Fatalf("attack stop restarted resource recovery: %+v", world.characters[1]) 271 } 272 } 273 274 func TestRegenerationNativeSitAndStandRequestsChangeRecovery(t *testing.T) { 275 useNPCData(t, testNPCData()) 276 world := newTestServer(t) 277 world.newCharacter().health = 20 278 startTestService(t, world) 279 client := connectZoningClient(t, world, true) 280 defer client.Close() 281 282 for _, action := range []uint32{86, 87} { 283 body := binary.BigEndian.AppendUint32(nil, action) 284 body = append(body, make([]byte, 16)...) 285 if _, err := client.Write(targetPacket(n3Packet(0x5e477770, 50000, 1, body))); err != nil { 286 t.Fatal(err) 287 } 288 if _, err := readPacket(client); err != nil { 289 t.Fatal(err) 290 } 291 var sitting bool 292 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 293 sitting = state.characters[1].sitting 294 return nil 295 })) 296 if sitting != (action == 86) { 297 t.Fatalf("native posture action %d: sitting=%t", action, sitting) 298 } 299 } 300 301 var sitting bool 302 var health uint32 303 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 304 sitting, health = state.characters[1].sitting, state.characters[1].health 305 return nil 306 })) 307 if sitting || health != 20 { 308 t.Fatal("posture requests changed resources or failed to stand") 309 } 310 } 311 312 func TestRegenerationFullCharacterPreservesNanoAndBreedMaximum(t *testing.T) { 313 for _, scenario := range []struct { 314 breed uint16 315 nano uint32 316 }{ 317 {0, 18}, {1, 35}, {2, 34}, {3, 48}, {4, 24}, {5, 59}, {6, 18}, {7, 18}, 318 } { 319 player := (&userData{ID: 1, Appearance: scenario.breed << 4, Level: 1, Stats: testCharacterStats()}).character() 320 player.nano = 11 321 322 packet := fullCharacterPacket(player) 323 stats := make(map[uint32]uint32) 324 count := binary.BigEndian.Uint32(packet[64:68])/1009 - 1 325 for offset := 68; offset < 68+int(count)*8; offset += 8 { 326 stats[binary.BigEndian.Uint32(packet[offset:offset+4])] = binary.BigEndian.Uint32(packet[offset+4 : offset+8]) 327 } 328 329 if stats[214] != 11 || stats[221] != scenario.nano { 330 t.Fatalf("breed %d: nano=%d max=%d", scenario.breed, stats[214], stats[221]) 331 } 332 } 333 }