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 orchestratorcombat/DeathUtils.ts- Pure utility functions (sanitization, keep-3, validation)combat/DeathTypes.ts- Type definitionsdeath/DeathStateManager.ts- Death lock persistence and crash recoverydeath/SafeAreaDeathHandler.ts- Safe zone death logic (gravestone system)death/WildernessDeathHandler.ts- Wilderness death logic (immediate ground drop)death/ZoneDetectionSystem.ts- Zone type detection
Death Zones
- 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.itemsfield stores dropped items for gravestone recoverykeptItemsfield stores kept items for respawn recoverykilledByfield stores sanitized killer name (XSS/injection protected)
Creating a Death Lock
Death Flow (Updated March 2026)
Safe Zone Death (OSRS Keep-3)
- Player HP reaches 0
- 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
- After transaction:
- Persist equipment clear to DB (retry on failure)
- Persist inventory clear to DB (retry on failure)
- Emit
PLAYER_SET_DEADevent
- Respawn (tick-based, deterministic):
- Return kept items to inventory
- Spawn gravestone with dropped items (5 minute timer)
- Teleport to spawn town
- Clear death lock
- Player can return to gravestone to reclaim dropped items
Wilderness Death
- Player HP reaches 0
- Transaction starts:
- Clear equipment in-memory
- Clear inventory in-memory
- All items marked for ground drop (no keep-3)
- Create death lock
- Commit transaction
- After transaction:
- Persist clears to DB
- Drop all items to ground immediately (no gravestone)
- Items despawn after 2 minutes
- Respawn:
- Teleport to spawn town
- No items returned
- Clear death lock after ground items despawn
Duel Arena Death
- Player HP reaches 0 in duel arena
- No item drops (inventory/equipment preserved)
- Death animation plays
- DuelSystem handles respawn and stakes
- No death lock created
Gravestone System
Gravestone Expiration
When gravestone expires:- Items transition to ground items
- Ground items have additional despawn timer
onGravestoneExpired()updates death lock
Item Recovery
Looting from Gravestone
Clearing Death Lock
Reconnect Validation
When a player reconnects, the system checks for active deaths:- 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):- Tag each item with its manifest value
- Sort descending by value (most valuable first)
- Greedily assign keep-count without expanding stacks
- Split into kept/dropped lists
Two-Phase Persist Pattern (New in PR #1094)
Problem: Death transaction calledclearEquipmentAndReturn() and clearInventoryImmediate() which each opened nested DB transactions, causing SQLite to deadlock.
Solution: In-memory clear inside transaction, DB persist after transaction.
AUDIT_LOG on retry failure.
Gravestone Privacy (New in PR #1094)
Gravestone loot items are hidden from network broadcast (OSRS-accurate):Security Features (New in PR #1094)
Duel Escape Prevention
handleRespawnRequest()- Blocks manual respawn buttoninitiateRespawn()- Defense-in-depth guard
Position Validation
Killer Name Sanitization
- 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)
Related Documentation
- Combat System
- Ground Items & Loot
- Database Schema
- DeathUtils API - Troubleshooting guide
- OSRS Wiki - Death - OSRS mechanics reference