Skip to main content

Death & Respawn System

The death system handles player deaths with zone-aware mechanics, gravestone spawning, and secure item recovery. Completely rewritten in PR #1094 (March 2026) to fix SQLite deadlock, equipment duplication, and implement OSRS-style “keep 3 most valuable items” for safe zone deaths.
Death code lives in packages/shared/src/systems/shared/ with:
  • combat/PlayerDeathSystem.ts - Main death orchestrator
  • combat/DeathUtils.ts - Pure utility functions (sanitization, keep-3, validation)
  • combat/DeathTypes.ts - Type definitions
  • death/DeathStateManager.ts - Death lock persistence and crash recovery
  • death/SafeAreaDeathHandler.ts - Safe zone death logic (gravestone system)
  • death/WildernessDeathHandler.ts - Wilderness death logic (immediate ground drop)
  • death/ZoneDetectionSystem.ts - Zone type detection
Breaking Change (March 2026): PLAYER_DIED event is deprecated. Use PLAYER_SET_DEAD for client death UI, or ENTITY_DEATH with type filter for server-side death processing.

Death Zones

OSRS-Accurate Mechanics:
  • Safe Zone: Keep 3 most valuable items (by manifest value), rest goes to gravestone
  • Wilderness: All items drop immediately to ground (no gravestone)
  • Duel Arena: No item drops (inventory/equipment preserved)

Death Lock System

To prevent item duplication on server restart/crash, deaths are tracked with database persistence. Updated in PR #1094 to include kept items for crash recovery.
New in PR #1094:
  • items field stores dropped items for gravestone recovery
  • keptItems field stores kept items for respawn recovery
  • killedBy field stores sanitized killer name (XSS/injection protected)

Creating a Death Lock


Death Flow (Updated March 2026)

Safe Zone Death (OSRS Keep-3)

  1. Player HP reaches 0
  2. Transaction starts:
    • Clear equipment in-memory (skip DB persist)
    • Clear inventory in-memory (skip DB persist)
    • Split items: keep 3 most valuable, drop rest
    • Create death lock with kept items
    • Commit transaction
  3. After transaction:
    • Persist equipment clear to DB (retry on failure)
    • Persist inventory clear to DB (retry on failure)
    • Emit PLAYER_SET_DEAD event
  4. Respawn (tick-based, deterministic):
    • Return kept items to inventory
    • Spawn gravestone with dropped items (5 minute timer)
    • Teleport to spawn town
    • Clear death lock
  5. Player can return to gravestone to reclaim dropped items
Two-Phase Persist Pattern: In-memory clear inside transaction, DB persist after transaction. Prevents SQLite deadlock from nested transactions.

Wilderness Death

  1. Player HP reaches 0
  2. Transaction starts:
    • Clear equipment in-memory
    • Clear inventory in-memory
    • All items marked for ground drop (no keep-3)
    • Create death lock
    • Commit transaction
  3. After transaction:
    • Persist clears to DB
    • Drop all items to ground immediately (no gravestone)
    • Items despawn after 2 minutes
  4. Respawn:
    • Teleport to spawn town
    • No items returned
    • Clear death lock after ground items despawn

Duel Arena Death

  1. Player HP reaches 0 in duel arena
  2. No item drops (inventory/equipment preserved)
  3. Death animation plays
  4. DuelSystem handles respawn and stakes
  5. No death lock created

Gravestone System

Gravestone Expiration

When gravestone expires:
  1. Items transition to ground items
  2. Ground items have additional despawn timer
  3. onGravestoneExpired() updates death lock

Item Recovery

Looting from Gravestone

Clearing Death Lock


Reconnect Validation

When a player reconnects, the system checks for active deaths:
This prevents:
  • Item duplication if server crashes mid-death
  • Double death processing on reconnect
  • Gravestone re-creation exploits

Death Events

Event Migration (PR #1094):

Death Constants


OSRS Keep-3 System (New in PR #1094)

Safe zone deaths keep the 3 most valuable items (by manifest value):
Algorithm:
  1. Tag each item with its manifest value
  2. Sort descending by value (most valuable first)
  3. Greedily assign keep-count without expanding stacks
  4. Split into kept/dropped lists
Example:
Reference: OSRS Wiki - Items Kept on Death

Two-Phase Persist Pattern (New in PR #1094)

Problem: Death transaction called clearEquipmentAndReturn() and clearInventoryImmediate() which each opened nested DB transactions, causing SQLite to deadlock. Solution: In-memory clear inside transaction, DB persist after transaction.
Crash Recovery: If server crashes between transaction commit and DB persist, death lock prevents reconnect inventory load. Items are not restored to player. Persist Retry Queue: Single-retry queue (bounded to 100 entries) handles transient DB failures. Emits AUDIT_LOG on retry failure.

Gravestone Privacy (New in PR #1094)

Gravestone loot items are hidden from network broadcast (OSRS-accurate):
Impact: Other players cannot see gravestone contents until interaction.

Security Features (New in PR #1094)

Duel Escape Prevention

Guards:
  • handleRespawnRequest() - Blocks manual respawn button
  • initiateRespawn() - Defense-in-depth guard

Position Validation

Killer Name Sanitization

Attack Vectors Prevented:
  • Homograph attacks (Cyrillic ‘а’ vs Latin ‘a’)
  • Zero-width characters (invisible manipulation)
  • BiDi overrides (text reversal)
  • XSS injection (script tags)
  • Buffer overflow (length capped at 64 chars)