aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit bf4d3000bc3fd03793cfe041dbaa52d757b3ca80
parent eeb81fe88f851c646d8f8a530097923dc784588a
Author: anon <anon@example.com>
Date:   Fri, 23 Oct 2026 00:46:05 +0000

Implement native Leet health recovery

Diffstat:
MTODO.md | 7++++++-
Mdocs/combat-regeneration.md | 4++--
Adocs/npc-regeneration.md | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/gamedata/npc_combat.yaml | 1+
Aserver/npc_regeneration.go | 32++++++++++++++++++++++++++++++++
Aserver/npc_regeneration_test.go | 199+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/regeneration.go | 37+++++++++++++++++++++----------------
Mserver/state.go | 2++
8 files changed, 362 insertions(+), 19 deletions(-)

diff --git a/TODO.md b/TODO.md @@ -154,9 +154,14 @@ its boundary checks and original-server evidence limits are recorded in selects the expected target and removes disconnected targets. - [ ] Navigate around one simple obstacle. Check: a pursuing creature takes a valid route around a wall rather than walking through it. -- [ ] Recover NPC resources outside combat. Check: one profile follows an +- [x] Recover NPC resources outside combat. Check: one profile follows an evidenced recovery/reset policy without healing twice on disengagement. +The supported Leet uses the recovered native NPC health branch, with one +retained recovery clock across combat transitions and quiet observer corrections. +Its evidence, automated/native checks and original-server policy limits are in +[NPC health recovery](docs/npc-regeneration.md). NPC nano recovery remains open. + ## Death and recovery - [x] Respawn one creature. Check: after the profile's respawn interval, exactly diff --git a/docs/combat-regeneration.md b/docs/combat-regeneration.md @@ -110,8 +110,8 @@ A new connection resets the prediction counter and posture; stopping an attack or sitting does not. Late scheduler visits advance one native-style check, without replaying an offline/stalled backlog. The two-second deadline stays anchored to its original phase, so small scheduling delays do not accumulate. -Historical NPC regeneration is a -separate unresolved rule. +The supported Leet's separate recovery rule and its server-policy limits are +recorded in [NPC health recovery](npc-regeneration.md). Absolute health is sent only when neither the player nor an enemy targeting that player is attacking. StopFight flushes queued weapon outcomes before the diff --git a/docs/npc-regeneration.md b/docs/npc-regeneration.md @@ -0,0 +1,99 @@ +# Leet health recovery + +The supported MonsterData 17655, level-1 Leet now recovers health on the +server. It uses the shipped 14.9.1 client's NPC prediction branch. The +unavailable original server's numeric recovery policy and scheduler are not +established by this client code. The profile's 100 maximum HP remains +provisional; see [NPC combat profiles](combat-npc-balance.md). + +## Recovered client behavior + +Original `kkr.dll` SHA-256 +`a37bc1c15053fdcc5572772309f7074c4f83baffe2faa164df40b059b7c480c0`, +`FUN_1006ea50`, checks recovery every two seconds and advances a counter by +two. Its NPC byte at actor offset `0x1b4` selects the health branch at +`1006f4b9`. With weapon-controller state 1 (idle) and stat 519 equal to zero: + +```text +health_interval = 1 +health_delta = max(1, trunc(maximum_health / 120)) +``` + +Otherwise it doubles HealInterval342 and retains HealDelta343. On an eligible +counter, it adds `trunc(effective_BodyDevelopment152 / RestModifier425)` to +that delta and clamps health to its maximum. Actor flag `0x10` at `+0x11c` +disables recovery. The separate player interval calculation in `FUN_10074560` +does not run for NPCs; applying the player's stamina formula gives the wrong +combat interval here. + +A read-only probe of the published WASM client verified that the Leet spawn +sets the NPC byte and retains interval 30, delta 3, base Body Development 5, +RestModifier 100 and stat 519 equal to zero. The small ability contribution +does not reach the next integer quotient of Body Development / 100. Its +combat branch therefore gains 3 HP at counters divisible by 60. The idle +branch gains 1 HP each check for the supported maximum of 100. There is no +instantaneous full-health reset on stopping combat. + +The controlled baseline omitted server recovery. After a nano hit, the native +Leet gained 3 HP at combat counter 60. After moving out of attack range, its +weapon controller became idle; HP rose from 53 to 59 as its counter advanced +from 84 to 96, while server health stayed fixed. This demonstrates client +prediction, not original-server authority. + +REA record +`ev_9df66de8d0f05217a69046147043bfd7e0d2bd2483793a59a51bb25b74be2ca0` +retains the update and player interval decompilation. Original instruction +addresses and baseline snapshots are in `local/npc-regeneration-research/`. + +## Server policy and delivery + +Only this exact species/level with an explicit combat profile receives this +recovery. Other NPCs and NPC nano recovery remain outside the implementation. +Its retained native inputs are fixed above; no configurable recovery system +or new stat-519 behavior is introduced. + +The server keeps one two-second phase and counter per living Leet, independent +of observers. Starting or stopping an attack changes the applicable branch +without resetting that phase or granting an additional heal. A late scheduler +visit performs one check and skips the stalled backlog, following the existing +player recovery policy. A fresh respawn gets full health and a new clock; +dead remains cannot heal. Current NPC health is not persisted across restart. + +Quiet health corrections use the existing zero-feedback HealthDamage packet. +They reach only ready observers that already know the NPC in its playfield. +Corrections wait while the NPC or any weapon attacker targeting it is fighting, +so queued animation damage cannot subtract twice. StopFight precedes the next +quiet correction. Combat prediction phases can differ between clients and +the server; the subsequent quiet correction reconciles the display. + +A contemporary [March 2003 mission-boss discussion, post 16](https://forums-archive.anarchy-online.com/showthread.php?112679-Finding-GA-Guide-P=) +reports substantially faster boss recovery after combat ends. It provides no +numeric rate, Leet measurement, or original-server capture. Later guides and +the local Fandom pages likewise do not establish a 14.9.1 Leet server formula; +they are not substitutes for that missing evidence. + +## Verification + +Focused tests cover idle/combat rates, the native maximum-health division +boundaries, clamping, scheduler jitter/backlogs, repeated visits, +disengagement ordering, incoming weapon queues, observer interest/readiness, +recovery without clients, dead/unverified-profile exclusions and fresh respawn +state. Player regeneration tests also exercise the shared quiet correction. + +`go test ./server/...` passed (server 107.905 seconds). The focused NPC and +player regeneration tests passed five repetitions under the race detector +(94.593 seconds). + +Two published WASM clients verified a real nano hit from 100 to 89 HP, +3-point server combat recovery at counter 120, and recovery after leaving +attack range. A 40-sample idle capture followed server HP from 94 to 100: +each two-counter step added exactly one HP, with native HP at most one +prediction tick ahead. Both native clients and the server ended at 100 HP. +A second encounter began from that recovered maximum and inflicted 19 damage. +The separate killed Leet stayed dead throughout. Screenshots showed ordinary +melee feedback without extra recovery messages. + +The 120 captured samples and 20 checked assertions are retained in +`local/npc-regeneration-smoke/verification.json`. The agent-owned game pages +were unloaded after verification. The temporary server on HTTP port 18108 +remains available at the user's explicit request for manual testing. diff --git a/server/gamedata/npc_combat.yaml b/server/gamedata/npc_combat.yaml @@ -3,6 +3,7 @@ 1: # Provisional health; no historical level-1 Leet HP measurement recovered. health: 100 + # Health recovery uses the checked native NPC branch; see docs/npc-regeneration.md. # Shipped QL 1 LeetBite1_001. Choosing this variant for this spawn is provisional. # Base damage 4..8 and attack/recharge 175 cs are verified item data. weapon: 120910 diff --git a/server/npc_regeneration.go b/server/npc_regeneration.go @@ -0,0 +1,32 @@ +package main + +import "time" + +func (server *server) advanceNPCRegeneration_locked(now time.Time, updates map[*worldClient][][]byte) { + for _, npc := range server.characters { + // Only the supported level-1 Leet profile has checked recovery inputs. + if !npc.npc || npc.monsterData != 17655 || npc.level != 1 || npc.combatProfile == nil || npc.health == 0 { + continue + } + if npc.regenerationAt.IsZero() { + npc.regenerationAt = now + continue + } + if now.Sub(npc.regenerationAt) < 2*time.Second { + continue + } + // Preserve the phase across combat transitions and skip scheduler backlogs. + intervals := now.Sub(npc.regenerationAt) / (2 * time.Second) + npc.regenerationAt = npc.regenerationAt.Add(intervals * 2 * time.Second) + npc.regenerationSeconds += 2 + + // kkr.dll FUN_1006ea50: NPC idle recovery replaces delta and interval. + // This profile retains native defaults: BodyDevelopment < 100, stat 519 = 0. + if npc.fighting == 0 { + npc.health = min(npc.maximumHealth(), npc.health+max(1, npc.maximumHealth()/120)) + } else if npc.regenerationSeconds%60 == 0 { + npc.health = min(npc.maximumHealth(), npc.health+3) + } + server.queueQuietHealth_locked(updates, npc) + } +} diff --git a/server/npc_regeneration_test.go b/server/npc_regeneration_test.go @@ -0,0 +1,199 @@ +package main + +import ( + "ao/server/gamedata" + "bytes" + "encoding/binary" + "testing" + "time" +) + +func newNPCRegenerationWorld() (*server, *combatTestConn, time.Time) { + world, conn := newCombatWorld() + world.characters[1000000].monsterData = 17655 + world.characters[1000000].level = 1 + world.characters[1000000].combatProfile = gamedata.NPCCombatProfiles[17655][1] + world.characters[1000000].health = 20 + start := time.Unix(100, 0) + // Keep player recovery out of the NPC packet assertions. + world.clients[1].regenerationAt = start.Add(time.Hour) + return world, conn, start +} + +func TestNPCRegenerationUsesOneIdleCheckDespiteJitterAndBacklogs(t *testing.T) { + world, _, start := newNPCRegenerationWorld() + + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(2*time.Second - time.Nanosecond)) + if world.characters[1000000].health != 20 { + t.Fatal("recovered before the two-second check") + } + world.advanceRegeneration(start.Add(2200 * time.Millisecond)) + world.advanceRegeneration(start.Add(2200 * time.Millisecond)) + world.advanceRegeneration(start.Add(4 * time.Second)) + world.advanceRegeneration(start.Add(time.Hour)) + world.advanceRegeneration(start.Add(time.Hour)) + + if world.characters[1000000].health != 23 || world.characters[1000000].regenerationSeconds != 6 || world.characters[1000000].regenerationAt != start.Add(time.Hour) { + t.Fatalf("replayed or shifted recovery: %+v", world.characters[1000000]) + } +} + +func TestNPCRegenerationCombatUsesNativeDeltaAndCounter(t *testing.T) { + world, conn, start := newNPCRegenerationWorld() + world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) + world.characters[1000000].fighting = 1 + world.fighting[1000000] = world.characters[1000000] + conn.packets = nil + + world.advanceRegeneration(start) + for second := 2; second <= 58; second += 2 { + world.advanceRegeneration(start.Add(time.Duration(second) * time.Second)) + } + if world.characters[1000000].health != 20 { + t.Fatal("combat health recovered before counter 60") + } + world.advanceRegeneration(start.Add(60 * time.Second)) + + if world.characters[1000000].health != 23 || len(conn.packets) != 0 { + t.Fatalf("combat recovery or premature correction: hp=%d packets=%x", world.characters[1000000].health, conn.packets) + } +} + +func TestNPCRegenerationDisengagementDoesNotHealTwiceOrResetPhase(t *testing.T) { + world, conn, start := newNPCRegenerationWorld() + world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) + world.characters[1000000].fighting = 1 + world.fighting[1000000] = world.characters[1000000] + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(2 * time.Second)) + world.characters[1].position = [3]float32{100, 0, 0} + conn.packets = nil + + world.advanceCombat(start.Add(3 * time.Second)) + world.advanceRegeneration(start.Add(3 * time.Second)) + if world.characters[1000000].health != 20 || world.characters[1000000].fighting != 0 || len(conn.packets) != 2 { + t.Fatal("disengagement healed immediately or failed to stop both actors") + } + world.advanceRegeneration(start.Add(4 * time.Second)) + world.advanceRegeneration(start.Add(4 * time.Second)) + + if world.characters[1000000].health != 21 || world.characters[1000000].regenerationSeconds != 4 || len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[2][16:20]) != 0x3710256c { + t.Fatalf("disengagement recovery: hp=%d packets=%x", world.characters[1000000].health, conn.packets) + } +} + +func TestNPCRegenerationSendsQuietHealthOnlyToReadyObserversWithInterest(t *testing.T) { + world, first, start := newNPCRegenerationWorld() + observers := make([]*combatTestConn, 5) + for i := range observers { + id := uint32(i + 2) + observers[i] = &combatTestConn{} + world.characters[id] = &character{id: id, playfield: 950} + world.clients[id] = &worldClient{id: id, conn: observers[i], target: true, ready: true, npcs: map[uint32]bool{1000000: true}, regenerationAt: start.Add(time.Hour)} + } + world.clients[3].loading = true + world.clients[4].ready = false + world.characters[5].playfield = 705 + delete(world.clients[6].npcs, 1000000) + + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(2 * time.Second)) + + if len(first.packets) != 1 || len(observers[0].packets) != 1 || !bytes.Equal(first.packets[0][16:], observers[0].packets[0][16:]) || !bytes.Equal(first.packets[0][29:45], []byte{0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) || binary.BigEndian.Uint32(first.packets[0][24:28]) != 1000000 { + t.Fatalf("quiet correction layout: %x", first.packets) + } + for _, conn := range observers[1:] { + if len(conn.packets) != 0 { + t.Fatalf("correction leaked outside interest: %x", conn.packets) + } + } +} + +func TestNPCRegenerationDefersCorrectionForIncomingWeaponQueue(t *testing.T) { + world, conn, start := newNPCRegenerationWorld() + world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) + conn.packets = nil + + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(2 * time.Second)) + + if world.characters[1000000].health != 21 || len(conn.packets) != 0 { + t.Fatalf("incoming-only recovery changed native queue: hp=%d packets=%x", world.characters[1000000].health, conn.packets) + } +} + +func TestNPCRegenerationClampsNativeIdleGainBoundaries(t *testing.T) { + for _, scenario := range []struct{ maximum, initial, want uint32 }{ + {100, 99, 100}, {120, 100, 101}, {121, 100, 101}, {239, 100, 101}, {240, 100, 102}, {480, 478, 480}, + } { + world, _, start := newNPCRegenerationWorld() + world.characters[1000000].combatProfile = &gamedata.NPCCombatProfile{Health: scenario.maximum} + world.characters[1000000].health = scenario.initial + + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(2 * time.Second)) + + if world.characters[1000000].health != scenario.want { + t.Fatalf("max %d: got %d want %d", scenario.maximum, world.characters[1000000].health, scenario.want) + } + } +} + +func TestNPCRegenerationSkipsDeadAndUnverifiedProfiles(t *testing.T) { + for _, scenario := range []string{"dead", "unprofiled", "species", "level", "player"} { + t.Run(scenario, func(t *testing.T) { + world, conn, start := newNPCRegenerationWorld() + switch scenario { + case "dead": + world.characters[1000000].health = 0 + case "unprofiled": + world.characters[1000000].combatProfile = nil + case "species": + world.characters[1000000].monsterData = 1234 + case "level": + world.characters[1000000].level = 2 + case "player": + world.characters[1000000].npc = false + } + initial := world.characters[1000000].health + + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(60 * time.Second)) + + if world.characters[1000000].health != initial || world.characters[1000000].nano != 0 || len(conn.packets) != 0 { + t.Fatalf("unexpected recovery: %+v packets=%x", world.characters[1000000], conn.packets) + } + }) + } +} + +func TestNPCRegenerationContinuesWithoutObservers(t *testing.T) { + world, conn, start := newNPCRegenerationWorld() + delete(world.clients, 1) + + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(2 * time.Second)) + + if world.characters[1000000].health != 21 || len(conn.packets) != 0 { + t.Fatal("NPC recovery depended on an observer") + } +} + +func TestNPCRegenerationRespawnStartsWithFreshResourcesAndClock(t *testing.T) { + world, _, start := newNPCRegenerationWorld() + delete(world.clients, 1) + world.advanceRegeneration(start) + world.advanceRegeneration(start.Add(2 * time.Second)) + world.characters[1000000].health = 0 + world.npcRespawns = []npcRespawn{{npc: world.characters[1000000], at: start.Add(4 * time.Second)}} + world.nextNPC = 1000001 + + world.advanceNPCRespawns(start.Add(4 * time.Second)) + world.advanceRegeneration(start.Add(4 * time.Second)) + + if world.characters[1000000].health != 0 || world.characters[1000001].health != 100 || + world.characters[1000001].regenerationSeconds != 0 || world.characters[1000001].regenerationAt != start.Add(4*time.Second) { + t.Fatal("replacement inherited the dead life's resources or recovery phase") + } +} diff --git a/server/regeneration.go b/server/regeneration.go @@ -11,6 +11,7 @@ func (server *server) advanceRegeneration(now time.Time) { defer server.updateMutex.Unlock() server.mutex.Lock() updates := make(map[*worldClient][][]byte) + server.advanceNPCRegeneration_locked(now, updates) for id, client := range server.clients { if !client.ready || client.loading { continue @@ -57,22 +58,7 @@ func (server *server) advanceRegeneration(now time.Time) { player.nano = min(player.maximumNano(), player.nano+gain) } - // Absolute HP must wait until weapon queues have been flushed by StopFight. - quiet := player.fighting == 0 - for _, attacker := range server.fighting { - if attacker.fighting == player.id { - quiet = false - break - } - } - if quiet { - // HealthDamage assigns absolute HP; a zero change suppresses feedback. - var body []byte - for _, value := range []uint32{player.health, 0, 0, 0} { - body = binary.BigEndian.AppendUint32(body, value) - } - server.queueCombat_locked(updates, player, player, n3Packet(0x3710256c, 50000, id, body)) - } + server.queueQuietHealth_locked(updates, player) body := binary.BigEndian.AppendUint32(nil, 214) body = binary.BigEndian.AppendUint32(body, player.nano) updates[client] = append(updates[client], n3Packet(0x2b333d6e, 50000, id, body)) @@ -80,3 +66,22 @@ func (server *server) advanceRegeneration(now time.Time) { server.mutex.Unlock() server.sendCombatUpdates(updates) } + +func (server *server) queueQuietHealth_locked(updates map[*worldClient][][]byte, actor *character) { + // Absolute HP must wait until weapon queues have been flushed by StopFight. + if actor.fighting != 0 { + return + } + for _, attacker := range server.fighting { + if attacker.fighting == actor.id { + return + } + } + + // HealthDamage assigns absolute HP; a zero change suppresses feedback. + var body []byte + for _, value := range []uint32{actor.health, 0, 0, 0} { + body = binary.BigEndian.AppendUint32(body, value) + } + server.queueCombat_locked(updates, actor, actor, n3Packet(0x3710256c, 50000, actor.id, body)) +} diff --git a/server/state.go b/server/state.go @@ -46,6 +46,8 @@ type character struct { implantChangesUntil time.Time treatmentLockedUntil time.Time nanoRechargeUntil time.Time + regenerationAt time.Time + regenerationSeconds uint32 savedProfile playerProfile savedPosition playerPosition nextPositionSave time.Time