Skip to main content

Death & Respawn System

The death system handles player deaths with zone-aware mechanics, crash recovery, gravestone spawning, and secure item recovery. It uses database-first persistence with atomic operations to prevent item duplication and loss.
Death code lives in:
  • packages/shared/src/systems/shared/combat/PlayerDeathSystem.ts - Main death orchestrator (1,263 lines)
  • packages/shared/src/systems/shared/death/DeathStateManager.ts - Death persistence (368 lines)
  • packages/shared/src/systems/shared/death/SafeAreaDeathHandler.ts - Safe zone logic (322 lines)
  • packages/shared/src/systems/shared/death/WildernessDeathHandler.ts - PvP zone logic (130 lines)
  • packages/shared/src/systems/shared/death/ZoneDetectionSystem.ts - Zone detection (213 lines)
  • packages/server/src/database/repositories/DeathRepository.ts - Database operations

Death Zones


Crash Recovery System

The death system includes comprehensive crash recovery to prevent item loss during server restarts.

Database-First Persistence

Death locks are created in the database before clearing inventory, ensuring items are always recoverable:

Recovery on Startup

When the server starts, it automatically recovers unfinished deaths:
Crash recovery ensures that if the server crashes during death processing, items are never lost. The system recreates gravestones/ground items from the database on restart.

Death Lock System

To prevent item duplication on server restart/crash, deaths are tracked with dual persistence (memory + database):

Creating a Death Lock (Atomic Acquisition)

The system uses atomic acquisition to prevent race conditions:
Atomic acquisition prevents duplicate death processing when multiple requests arrive simultaneously (e.g., client retry + server timeout).

Crash Recovery

When the server restarts, the DeathStateManager automatically recovers unfinished deaths:
Recovery Scenarios:
  1. Entities exist: Restore death lock to memory (items already in world)
  2. Entities missing: Recreate gravestone/ground items from stored item data
  3. No items: Mark as recovered and clean up
Death locks persist until items are fully looted. This prevents item duplication if the server crashes before cleanup completes.

Death Flow

Safe Zone Death (OSRS-Accurate)

Wilderness Death


Gravestone System

Gravestones protect items in safe areas with OSRS-accurate timing.

Gravestone Timing (Updated in PR #566)

Ground item despawn time was increased from 3 minutes to 60 minutes to match OSRS mechanics. This gives players ample time to retrieve items after gravestone expiration.

Gravestone Entity

Gravestone Expiration

When gravestone expires:
  1. Items transition to ground items
  2. Ground items have 60-minute despawn timer
  3. Death lock updated with ground item IDs
  4. Items become lootable by anyone

Item Recovery

Looting from Gravestone

The loot system uses shadow state with transaction tracking for optimistic UI updates with automatic rollback on failure.

Shadow State Pattern (Client)

The client uses optimistic updates with automatic rollback:
The shadow state pattern ensures the client UI always stays in sync with the server, even if loot requests fail or time out.

Loot All

Players can loot all items at once with a single request:

Item Looting Tracking

When items are looted from gravestones or ground, the death lock is updated:

Clearing Death Lock

Death locks persist until all items are looted or despawn:
Death Lock Persistence: The death lock is NOT cleared on respawn. It persists until all items are recovered or despawn. This enables crash recovery.

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
  • Item loss when player disconnects during death

Player Disconnect Handling

When a player disconnects mid-death, the death lock is preserved:
Why This Matters:
  • Gravestone/ground items persist for other players to see
  • Player can recover items when they reconnect
  • Memory is freed for disconnected players
  • Database ensures no item duplication on reconnect

Respawn Validation

The respawn system validates that players are actually dead before allowing respawn:

Death Screen UI

The death screen includes:
  • Respawn button with spam prevention
  • Countdown timer showing time until items despawn
  • Timeout handling (10 second timeout with retry)
  • Input blocking during death animation

Movement Blocking

Players cannot move while dying or dead:
Death States: The system uses DeathState.DYING (during animation) and DeathState.DEAD (after animation) to block actions during the death sequence.

Combat State Cleanup

When a player dies or respawns, all combat-related states are cleaned up across multiple systems:

Death Event Handlers

Multi-Layer Cleanup

The system uses defense-in-depth with three layers of cleanup:
  1. Event-based cleanup: Death/respawn events trigger immediate cleanup
  2. Runtime guards: Health checks in aggro/combat loops
  3. Timeout cleanup: Stale states cleaned up after timeout

Death Events


Death Constants

Ground item despawn time was increased from 3 minutes to 60 minutes in PR #566 to match OSRS mechanics.

Security Features

Rate Limiting

Atomic Operations

All loot operations are queued to prevent concurrent access:

Death State Blocking

Players cannot loot while dying or dead:
OSRS Accuracy: Ground item despawn time was updated from 3 minutes to 60 minutes to match OSRS behavior.

Database Schema

Death locks are stored in the player_deaths table:
Key Fields:
  • items: Stores item data for crash recovery (recreate gravestone if entities lost)
  • killed_by: Displays on gravestone (“Here lies X, killed by Y”)
  • recovered: Prevents duplicate recovery on multiple restarts

Gravestone Display

Gravestones now show the player’s name instead of their ID:
Name Resolution:
  • Queries characters table for player’s username
  • Falls back to player ID if name not found
  • Sanitizes names with Unicode normalization (security)


Duel Arena Deaths

Deaths in the Duel Arena are handled differently:

No Item Loss

Death State Handling

The DuelSystem sets the death state but prevents normal death processing:

Health Restoration

After duel resolution, both players are restored to full health:
Duel deaths use the same death animation (8 ticks) as normal deaths for visual consistency.