Skip to main content

Mob AI & NPC Behavior

The mob AI system implements OSRS-accurate behavior using a state machine pattern. Mobs have distinct behavioral states and transition based on game events like player proximity, combat, and leash distance.
AI code lives in:
  • packages/shared/src/entities/managers/AIStateMachine.ts - State machine
  • packages/shared/src/systems/shared/combat/AggroSystem.ts - Aggression detection
  • packages/shared/src/entities/npc/MobEntity.ts - Mob entity class

AI State Machine

Mobs use a clean state machine with 5 core states:

State Behavior Details

IDLE State

  • Mob stands still watching for players
  • Random idle duration: 3-8 seconds
  • Checks for nearby players each frame
  • Transitions to WANDER after idle time expires (if movement type allows)
  • Transitions to CHASE if player detected within aggro range

WANDER State

  • Generate random target within wander radius
  • Move toward target tile-by-tile
  • Uses tile-based distance checks to prevent infinite loops
  • Transitions to CHASE if player detected
  • Transitions to IDLE when target reached

CHASE State

  • Mob pursues its current target
  • Transitions to ATTACK when in melee range
  • Transitions to RETURN if target escapes or mob exceeds leash distance
  • Handles same-tile step-out (OSRS-accurate)

ATTACK State

  • Mob performs attacks on tick intervals
  • Uses canAttack(currentTick) for OSRS-accurate timing
  • Transitions to CHASE if target moves out of range
  • Transitions to RETURN if target dies or escapes

RETURN State

  • Mob walks back to spawn point
  • Uses tile-based pathfinding
  • Clears combat state and target
  • Transitions to IDLE when at spawn

Aggression System

The AggroSystem handles mob aggression detection following OSRS rules.

Level-Based Aggression

OSRS rule: Aggressive mobs only attack players whose combat level is less than double the mob’s level + 1.
Examples:
  • Level 2 Goblin → Ignores players level 5+
  • Level 14 Dark Wizard → Ignores players level 29+
  • Level 89 Abyssal Demon → Ignores players level 179+ (never ignores)

Tolerance Timer (10-Minute Immunity)

In OSRS, players become immune to aggression after staying in a 21×21 tile region for 10 minutes:

Detection Ranges


Leash System

Mobs have a maximum chase distance from their spawn point:
When a mob exceeds its leash range:
  1. Combat state is cleared
  2. Target is cleared
  3. Mob transitions to RETURN state
  4. Health regenerates while returning (optional)

Movement Types

Mobs have configurable movement behaviors defined in their manifest:

Same-Tile Step-Out

OSRS-accurate behavior when mob is on the same tile as its target:
“In RS, they pick a random cardinal direction and try to move the NPC towards that by 1 tile, if it can. If not, the NPC does nothing that cycle.”

Mob Entity Configuration


AI Events