commit 59009d7bb3d63fa47f9cacb0963a9b1c089e406f
parent a35d2946d6e5b5d6df04c1676bdc6f3dd9837af7
Author: anon <anon@example.com>
Date: Thu, 22 Oct 2026 08:52:00 +0000
Respawn profiled Leets as fresh independent creatures
Diffstat:
12 files changed, 340 insertions(+), 8 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -129,7 +129,7 @@ recorded in [inventory behavior](docs/inventory.md).
## Death and recovery
-- [ ] Respawn one creature. Check: after the profile's respawn interval, exactly
+- [x] Respawn one creature. Check: after the profile's respawn interval, exactly
one living replacement appears with fresh resources and no old fight state.
- [ ] Resurrect a player through the native flow. Check: death leads to a valid
resurrection location and usable character without reloading the browser.
@@ -140,6 +140,9 @@ recorded in [inventory behavior](docs/inventory.md).
- [ ] Apply the classic insurance/XP-loss policy. Check: recorded before/after
XP and save-point cases match the documented 14.9.1 rule.
+The Leet's replacement lifecycle and explicitly provisional delay are recorded
+in [creature respawn](docs/npc-respawn.md).
+
## Loot and credits
- [x] Open a corpse containing one fixed item. Check: the native loot window
diff --git a/docs/combat-client-reconstruction.md b/docs/combat-client-reconstruction.md
@@ -84,7 +84,9 @@ unknowns for each area:
| Line of sight | Client checks cannot establish original server validation | Not implemented on the server |
| Dual wield, ammunition/reload, specials and nanos | Scheduling, core effect streams and skill-lock contract recovered; full effect semantics remain partial | Not implemented |
| Resource regeneration | Native prediction formulas and injured-client probes | Authoritative player recovery; absolute quiet corrections reconcile prediction |
-| XP, loot, reclaim and respawn | Separate rules and state required | Not implemented |
+| Corpse loot | Native handlers and real combat/loot encounters | Fixed-item transfer persists; production drop tables remain unimplemented |
+| Creature respawn | Explicit provisional level-1 Leet delay; historical district units unresolved | One fresh replacement after 100 seconds; see [respawn](npc-respawn.md) |
+| XP, reclaim and player resurrection | Separate rules and state required | Not implemented |
## Implementation order and acceptance checks
diff --git a/docs/combat-npc-balance.md b/docs/combat-npc-balance.md
@@ -70,6 +70,7 @@ and AggDef outside -100..100.
| Initiative | 0 | Provisional actor value |
| AggDef | 75 | Provisional actor value |
| Attack/recharge phases | 1.75s / 1.75s | Derived from the shipped 175 cs delays and the selected initiative/AggDef using the recovered timing formula |
+| Respawn delay from death | 100 seconds | Provisional; independent of unresolved district time units |
The first bite lands after the attack phase; subsequent bites are 3.5 seconds
apart before world-tick quantization. Range still uses the existing four-metre
@@ -78,6 +79,9 @@ Health, hit distribution, effective ratings, armor and original spawn binding
remain gaps in canonical balance; the profile does not make those values
historically verified.
+The replacement lifecycle and its timing limits are described in
+[creature respawn](npc-respawn.md).
+
The native `SimpleCharFullUpdate` includes the selected weapon endpoints and
QL in its initial weapon list. `SpecialAttackWeapon` (`0x1d3c0f1c`) follows the
spawn, carrying the same list and the five initiative/AggDef values. Native
diff --git a/docs/combat-rules.md b/docs/combat-rules.md
@@ -43,8 +43,10 @@ and their verified/provisional status are recorded in [NPC balance](combat-npc-b
The uniform normal-damage roll is a reconstruction choice; the item establishes
the endpoints, not the historical server's random distribution. The four-meter
range approximates physical extents, and there is no server line-of-sight
-test. Other equipment, nanos, specials, aggression/taunt AI, reclaim, loot, XP and
-respawn are not implemented. Reload revival is a local test convenience.
+test. Other equipment, nanos, specials, aggression/taunt AI, reclaim, XP and
+player resurrection are not implemented. Fixed corpse loot and the provisional
+level-1 Leet [respawn lifecycle](npc-respawn.md) are supported. Reload revival
+is a local test convenience.
These limitations prevent a claim of canonical combat balance.
The [NPC balance search](combat-npc-balance.md) identifies authentic Leet Bite
weapon endpoints, but does not establish historical creature HP or final damage.
diff --git a/docs/npc-respawn.md b/docs/npc-respawn.md
@@ -0,0 +1,70 @@
+# Creature respawn
+
+The exact-level Leet profile (MonsterData 17655, level 1) respawns 100 seconds
+after death. This delay is provisional. It is an explicit reconstruction value,
+not a verified 14.9.1 Leet timer or an interpretation of district time units.
+Unlisted creatures have no respawn policy.
+
+## Evidence and limits
+
+The original district resources contain respawn chance and time fields, as
+described in [game data](game-data.md#version-5-district-layout). District and
+fixed-spawn time fields have different widths. Their units and selection rules
+remain unresolved; the runtime does not convert either field into a timer.
+
+A bounded online search for backyard/Leet respawn observations did not establish
+a 14.9.1 delay. The [January 2004 spawn discussion](https://forums-archive.anarchy-online.com/showthread.php?222825-Maximum-spawn-times-for-chance-bosses=)
+distinguishes fixed and probabilistic named-creature schedules. It concerns
+other creatures after the target version and cannot establish this Leet's
+delay, chance or clock origin. A dated capture identifying the creature,
+death/despawn times and subsequent spawn is still needed.
+
+The current policy is deterministic: one replacement, with the delay measured
+from the authoritative killing blow. It does not implement chance rolls,
+district population refill, alternate species or a timer starting at corpse
+removal. The 250 ms world tick may delay publication until its next visit.
+
+## Lifecycle and identities
+
+The death transition queues one pending respawn when the profile has a nonzero
+`respawn_seconds`. Repeated combat ticks cannot queue another death for that
+life. The pending queue is protected by the normal update/state lock order.
+It advances even without connected observers and is processed before roaming
+and combat in the world tick.
+
+Each replacement receives a fresh character identity, the same definition and
+combat profile, full profile health, and empty combat/recharge state. A roaming
+creature returns to its authored home with a fresh roaming state. Its entry in
+the roaming roster is replaced, so successive deaths do not add roaming workers.
+Fixed creatures remain at their fixed post. Normal interest handling sends the
+new actor before its native weapon profile, only to eligible nearby observers.
+
+The previous corpse retains its original identity and contents. An open loot
+window remains attached to that corpse; requests using the dead actor cannot
+attack the replacement. Taking an old item uses the existing durable inventory
+transfer, and repeating that request cannot manufacture a new drop. Corpse
+expiration remains a separate TODO, so old corpses can currently accumulate.
+
+NPC lives, corpses and pending timers remain runtime state. Restart reconstructs
+exactly the authored living population with full resources; it does not persist
+elapsed NPC timers or resurrect additional copies from the old queue. Player
+inventory obtained before restart remains durable.
+
+## Verification
+
+`server/respawn_test.go` covers the exact deadline, repeated and concurrent
+ticks, two successive kills, old loot after replacement, fresh home/resources
+and combat state, identical owner/observer spawn packets, observer-free
+respawning, interest reentry, restart and exclusion of unprofiled creatures.
+
+Two published WASM clients exercised two full-health kills with the existing
+QL 1 Kinetic Axe and two timed replacements. The live identities progressed
+from 1000000 to 1068609 to 1068610; each replacement started at 100 HP without
+attacking until a new request. Both observers saw the new living creature and
+the old remains. The first corpse's window stayed open across respawn, and
+its fixture item transferred once afterward. That item remained in the native
+inventory after page reconnect and server restart. Restart produced one living
+authored creature with no old corpses or pending timers. There were no page
+errors; connection-refused logs occurred during the intentional restart.
+Screenshots, per-step states and the isolated fixture are retained in
+`local/respawn-smoke/`.
diff --git a/server/combat.go b/server/combat.go
@@ -257,6 +257,11 @@ func (server *server) advanceCombat(now time.Time) {
target.corpse.looter = attacker.id
server.queueCombat_locked(updates, target, target, corpsePacket(target))
}
+ if target.npc && target.combatProfile != nil && target.combatProfile.RespawnSeconds != 0 {
+ server.npcRespawns = append(server.npcRespawns, npcRespawn{
+ npc: target, at: now.Add(time.Duration(target.combatProfile.RespawnSeconds) * time.Second),
+ })
+ }
} else if target.npc && target.fighting == 0 {
server.startAttack_locked(target, attacker, now, updates)
}
diff --git a/server/gamedata/npc_combat.yaml b/server/gamedata/npc_combat.yaml
@@ -9,3 +9,5 @@
# Provisional actor settings. Together they retain the weapon's base delays.
initiative: 0
agg_def: 75
+ # Provisional delay from death, independent of unresolved district time units.
+ respawn_seconds: 100
diff --git a/server/gamedata/types.go b/server/gamedata/types.go
@@ -15,10 +15,11 @@ type NPCData struct {
}
type NPCCombatProfile struct {
- Health uint32 `yaml:"health"`
- Weapon uint32 `yaml:"weapon"`
- Initiative int32 `yaml:"initiative"`
- AggDef int32 `yaml:"agg_def"`
+ Health uint32 `yaml:"health"`
+ Weapon uint32 `yaml:"weapon"`
+ Initiative int32 `yaml:"initiative"`
+ AggDef int32 `yaml:"agg_def"`
+ RespawnSeconds uint32 `yaml:"respawn_seconds"`
}
type NPCPlayfield struct {
diff --git a/server/respawn.go b/server/respawn.go
@@ -0,0 +1,58 @@
+package main
+
+import "time"
+
+type npcRespawn struct {
+ npc *character
+ at time.Time
+}
+
+func (server *server) advanceNPCRespawns(now time.Time) {
+ server.updateMutex.Lock()
+ defer server.updateMutex.Unlock()
+ server.mutex.Lock()
+ pending := server.npcRespawns
+ server.npcRespawns = nil
+ spawned := false
+ for _, respawn := range pending {
+ if now.Before(respawn.at) {
+ server.npcRespawns = append(server.npcRespawns, respawn)
+ continue
+ }
+
+ // A new identity keeps the remains and stale requests separate from this life.
+ npc := &character{
+ id: server.nextNPC, name: respawn.npc.name,
+ playfield: respawn.npc.playfield, position: respawn.npc.position,
+ rotation: [4]float32{0, 0, 0, 1}, appearance: respawn.npc.appearance,
+ monsterData: respawn.npc.monsterData, level: respawn.npc.level, npc: true,
+ combatProfile: respawn.npc.combatProfile, npcDefinitionID: respawn.npc.npcDefinitionID,
+ }
+ server.nextNPC++
+ npc.health = npc.maximumHealth()
+ if respawn.npc.roaming != nil {
+ npc.position = respawn.npc.roaming.home
+ npc.roaming = &npcRoaming{home: npc.position}
+ for index, other := range server.roamingNPCs[npc.playfield] {
+ if other == respawn.npc {
+ server.roamingNPCs[npc.playfield][index] = npc
+ break
+ }
+ }
+ }
+ server.characters[npc.id] = npc
+ spawned = true
+ }
+
+ // Publish through normal interest handling, including the native combat profile.
+ updates := make(map[*worldClient][][]byte)
+ if spawned {
+ for _, client := range server.clients {
+ if client.ready && !client.loading {
+ updates[client] = server.updateNPCInterest_locked(client, now)
+ }
+ }
+ }
+ server.mutex.Unlock()
+ server.sendCombatUpdates(updates)
+}
diff --git a/server/respawn_test.go b/server/respawn_test.go
@@ -0,0 +1,181 @@
+package main
+
+import (
+ "bytes"
+ "encoding/binary"
+ "sync"
+ "testing"
+ "time"
+)
+
+func newRespawnWorld(t *testing.T) (*server, *combatTestConn) {
+ t.Helper()
+ data := testNPCData()
+ data.Playfields[0].Districts[0].Groups[0].Level = 1
+ data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point"
+ useNPCData(t, data)
+ world := newTestServer(t)
+ player := world.newCharacter()
+ player.position = [3]float32{52, 14, 56}
+ conn := &combatTestConn{}
+ world.clients[player.id] = &worldClient{id: player.id, conn: conn, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
+ world.characters[1000000].health = 1
+ return world, conn
+}
+
+func TestNPCRespawnCreatesOneFreshLifeAtHomeForBothObservers(t *testing.T) {
+ world, first := newRespawnWorld(t)
+ observer := world.newCharacter()
+ second := &combatTestConn{}
+ world.clients[observer.id] = &worldClient{id: observer.id, conn: second, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
+ npc := world.characters[1000000]
+ npc.position[0]++
+ npc.rotation = [4]float32{0, 1, 0, 0}
+ npc.roaming.moving = true
+ npc.roaming.destination = [3]float32{55, 14, 56}
+ npc.rechargeUntil = time.Now().Add(time.Hour)
+
+ world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:])
+ killed := world.characters[1].nextAttack
+ world.advanceCombat(killed)
+ if err := world.handleLootUse(world.clients[1], lootUseRequest(1)); err != nil {
+ t.Fatal(err)
+ }
+ first.packets, second.packets = nil, nil
+ world.advanceNPCRespawns(killed.Add(100*time.Second - time.Nanosecond))
+ if world.characters[1000001] != nil || len(first.packets) != 0 || len(world.npcRespawns) != 1 {
+ t.Fatal("replacement appeared before the profile's respawn interval")
+ }
+ world.advanceNPCRespawns(killed.Add(100 * time.Second))
+ world.advanceNPCRespawns(killed.Add(time.Hour))
+
+ replacement := world.characters[1000001]
+ if replacement == nil || len(world.characters) != 4 || len(world.npcRespawns) != 0 || world.nextNPC != 1000002 {
+ t.Fatal("respawn did not create exactly one replacement")
+ }
+ if replacement.health != 100 || replacement.corpse != nil || replacement.fighting != 0 ||
+ !replacement.nextAttack.IsZero() || !replacement.rechargeUntil.IsZero() ||
+ replacement.position != [3]float32{52, 14, 56} || replacement.rotation != [4]float32{0, 0, 0, 1} ||
+ replacement.roaming.home != [3]float32{52, 14, 56} || replacement.roaming.moving || !replacement.roaming.nextMove.IsZero() ||
+ replacement.combatProfile != npc.combatProfile || replacement.npcDefinitionID != "test/npc" {
+ t.Fatalf("replacement retained dead-life state: %+v", replacement)
+ }
+ if len(world.roamingNPCs[950]) != 1 || world.roamingNPCs[950][0] != replacement ||
+ npc.health != 0 || npc.corpse == nil || npc.fighting != 0 || len(world.fighting) != 0 || world.clients[1].lootCorpse != npc.id {
+ t.Fatal("respawn changed the corpse, active loot window or roaming population")
+ }
+ if len(first.packets) != 2 || len(second.packets) != 2 ||
+ binary.BigEndian.Uint32(first.packets[0][16:20]) != 0x271b3a6b ||
+ binary.BigEndian.Uint32(first.packets[0][24:28]) != 1000001 ||
+ binary.BigEndian.Uint32(first.packets[1][16:20]) != 0x1d3c0f1c ||
+ !bytes.Equal(first.packets[0][16:], second.packets[0][16:]) || !bytes.Equal(first.packets[1][16:], second.packets[1][16:]) {
+ t.Fatal("observers did not receive the same new actor and combat profile")
+ }
+}
+
+func TestNPCRespawnWithoutObserversThenReentryAndRestart(t *testing.T) {
+ world, conn := newRespawnWorld(t)
+ world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
+ killed := world.characters[1].nextAttack
+ world.advanceCombat(killed)
+ conn.packets = nil
+ world.clients[1].ready = false
+
+ world.advanceNPCRespawns(killed.Add(100 * time.Second))
+ if len(conn.packets) != 0 || world.characters[1000001] == nil {
+ t.Fatal("offline population did not respawn independently of observers")
+ }
+ world.clients[1].ready = true
+ world.clients[1].npcs = make(map[uint32]bool)
+ world.advanceNPCs(killed.Add(101 * time.Second))
+ restarted := newServer()
+
+ spawns, corpses, profiles := 0, 0, 0
+ for _, packet := range conn.packets {
+ switch binary.BigEndian.Uint32(packet[16:20]) {
+ case 0x271b3a6b:
+ spawns++
+ case 0x4f474e05:
+ corpses++
+ case 0x1d3c0f1c:
+ profiles++
+ }
+ }
+ if spawns != 1 || corpses != 1 || profiles != 1 {
+ t.Fatalf("reentry lost the living replacement or old corpse: %d/%d/%d", spawns, corpses, profiles)
+ }
+ if len(restarted.characters) != 1 || restarted.characters[1000000].health != 100 ||
+ restarted.characters[1000000].corpse != nil || len(restarted.npcRespawns) != 0 || len(restarted.roamingNPCs[950]) != 1 {
+ t.Fatal("restart duplicated the authored population or retained a dead life")
+ }
+}
+
+func TestNPCRespawnConcurrentTicksAndStaleCombatKeepLifeSeparate(t *testing.T) {
+ world, _ := newRespawnWorld(t)
+ world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
+ killed := world.characters[1].nextAttack
+ world.advanceCombat(killed)
+ var workers sync.WaitGroup
+
+ for range 8 {
+ workers.Go(func() { world.advanceNPCRespawns(killed.Add(100 * time.Second)) })
+ }
+ workers.Wait()
+ world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
+ world.advanceCombat(killed.Add(time.Hour))
+
+ if len(world.characters) != 3 || world.characters[1000001].health != 100 ||
+ world.characters[1].fighting != 0 || len(world.fighting) != 0 || len(world.npcRespawns) != 0 || world.nextNPC != 1000002 {
+ t.Fatal("concurrent timers or a stale request duplicated or attacked the new life")
+ }
+}
+
+func TestNPCWithoutRespawnProfileStaysDead(t *testing.T) {
+ world, _ := newRespawnWorld(t)
+ world.characters[1000000].combatProfile = nil
+
+ world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
+ killed := world.characters[1].nextAttack
+ world.advanceCombat(killed)
+ world.advanceNPCRespawns(killed.Add(time.Hour))
+
+ if len(world.characters) != 2 || world.characters[1000000].health != 0 || len(world.npcRespawns) != 0 {
+ t.Fatal("an unlisted creature received an invented respawn policy")
+ }
+}
+
+func TestNPCRespawnKeepsOldLootSeparateAcrossTwoKills(t *testing.T) {
+ world, _ := newRespawnWorld(t)
+ if err := world.saveUser_locked(world.characters[1]); err != nil {
+ t.Fatal(err)
+ }
+ world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
+ killed := world.characters[1].nextAttack
+ world.advanceCombat(killed)
+ world.characters[1000000].corpse.items = []ownedItem{{ID: 17, Type: 51005, Template: 42640, Quality: 1}}
+ world.handleLootUse(world.clients[1], lootUseRequest(1))
+
+ world.advanceNPCRespawns(killed.Add(100 * time.Second))
+ if err := world.handleInventory(world.clients[1], lootTakeRequest()); err != nil {
+ t.Fatal(err)
+ }
+ world.handleInventory(world.clients[1], lootTakeRequest())
+ world.characters[1000001].health = 1
+ world.characters[1].rechargeUntil = killed.Add(101 * time.Second)
+ world.handleCombat(world.clients[1], attackPacket(1, 1000001)[20:])
+ secondKill := world.characters[1].nextAttack
+ world.advanceCombat(secondKill)
+ world.advanceNPCRespawns(secondKill.Add(100 * time.Second))
+ world.advanceNPCRespawns(secondKill.Add(time.Hour))
+
+ if len(world.characters) != 4 || world.characters[1000002].health != 100 || world.characters[1000002].corpse != nil ||
+ len(world.npcRespawns) != 0 || len(world.roamingNPCs[950]) != 1 || world.roamingNPCs[950][0].id != 1000002 {
+ t.Fatal("a second death did not produce exactly one fresh living replacement")
+ }
+ if world.characters[1000000].corpse == world.characters[1000001].corpse ||
+ len(world.characters[1000000].corpse.items) != 0 || len(world.characters[1000001].corpse.items) != 0 ||
+ len(world.characters[1].inventory.Items) != 1 || world.characters[1].inventory.Items[0].ID != 17 ||
+ world.clients[1].lootCorpse != 1000000 {
+ t.Fatal("respawn or repeated pickup duplicated old loot or rebound the open container")
+ }
+}
diff --git a/server/roaming.go b/server/roaming.go
@@ -19,6 +19,7 @@ func (server *server) roamNPCs() {
ticker := time.NewTicker(250 * time.Millisecond)
defer ticker.Stop()
for now := range ticker.C {
+ server.advanceNPCRespawns(now)
server.advanceNPCs(now)
server.advanceEquipment(now)
server.advanceCombat(now)
diff --git a/server/state.go b/server/state.go
@@ -83,9 +83,11 @@ type server struct {
// Order entity-state packet batches; acquire before mutex.
updateMutex sync.Mutex
nextCharacter uint32
+ nextNPC uint32
nextItem uint32
characters map[uint32]*character
roamingNPCs map[uint32][]*character
+ npcRespawns []npcRespawn
clients map[uint32]*worldClient
fighting map[uint32]*character
chats map[uint32]*chatClient
@@ -126,6 +128,7 @@ func newServer() *server {
}
for _, spawn := range spawns {
server.characters[spawn.id] = &spawn
+ server.nextNPC = max(server.nextNPC, spawn.id+1)
if spawn.roaming != nil {
server.roamingNPCs[spawn.playfield] = append(server.roamingNPCs[spawn.playfield], &spawn)
}