Web Engine Docs
Preparing documentation
Use the search bar to quickly find any topic
Preparing documentation
Use the search bar to quickly find any topic
Comprehensive AI system for creating intelligent game agents with behavior trees, state machines, perception, navigation, and steering behaviors.
Web Engine provides a comprehensive AI system for creating intelligent game agents with behavior trees, state machines, perception, navigation, and steering behaviors.
The AI system is built on a modular ECS architecture with several interconnected subsystems:
Hierarchical decision-making with composite nodes (Selector, Sequence, Parallel), decorator nodes (Inverter, Repeater, Timeout), and action/condition nodes.
Simple FSM and Hierarchical FSM with nested states, transition guards, event-driven transitions, and history states (shallow and deep).
Score-based decision making with considerations (health, distance, enemy count) and response curves for dynamic action selection.
import {AIAgent,Perception,Perceivable,initAIAgent,initPerception,initPerceivable,AIControllerType,AIAffiliation,} from '@web-engine-dev/core/engine/ai';// Create an AI agent with perceptionaddComponent(world, AIAgent, enemyEid);addComponent(world, Perception, enemyEid);// Initialize with behavior tree controllerinitAIAgent(enemyEid, AIControllerType.BehaviorTree, AIAffiliation.Hostile);initPerception(enemyEid);// Make player perceivableaddComponent(world, Perceivable, playerEid);initPerceivable(playerEid, AIAffiliation.Friendly);
import {registerBehaviorTree,assignBehaviorTree,BTSelector,BTSequence,BTCondition,BTAction,BTStatus,} from '@web-engine-dev/core/engine/ai';// Register a behavior treeregisterBehaviorTree('GuardAI', () => {return new BTSelector([// Combat behaviornew BTSequence([new BTCondition('hasTarget', (ctx) =>ctx.blackboard.has('targetEid')),new BTCondition('inRange', (ctx) => {const dist = ctx.blackboard.get('distanceToTarget') ?? Infinity;return dist < 10;}),new BTAction('attack', (ctx) => {// Attack logicreturn BTStatus.SUCCESS;}),]),// Patrol behaviornew BTSequence([new BTCondition('hasPatrol', (ctx) =>ctx.blackboard.has('patrolWaypoints')),new BTAction('patrol', (ctx) => {// Patrol logicreturn BTStatus.RUNNING;}),]),// Idle fallbacknew BTAction('idle', (ctx) => {return BTStatus.SUCCESS;}),]);});// Assign to entityassignBehaviorTree(enemyEid, 'GuardAI');
AI agents have high-level operational states that control behavior execution:
| State | Description |
|---|---|
| Disabled | Agent is inactive/disabled |
| Idle | Agent is idle, not executing behavior |
| Active | Agent is actively executing behavior |
| Paused | Agent behavior is paused (can be resumed) |
| Alert | Agent is in alert state (detected something) |
| Combat | Agent is in combat/hostile state |
| Fleeing | Agent is fleeing/retreating |
| Dead | Agent is dead/incapacitated |
The blackboard is a shared memory system for AI decision-making. Each agent has its own blackboard for storing and sharing data between behavior nodes:
import { getBlackboard, setBlackboardValue, getBlackboardValue } from '@web-engine-dev/core/engine/ai';// Get agent's blackboard IDconst blackboardId = AIAgent.blackboardId[eid];// Set valuessetBlackboardValue(blackboardId, 'targetEid', enemyEntity);setBlackboardValue(blackboardId, 'health', 100);setBlackboardValue(blackboardId, 'alertLevel', 0.5);// Get valuesconst target = getBlackboardValue<number>(blackboardId, 'targetEid');const health = getBlackboardValue<number>(blackboardId, 'health');// In behavior tree nodes, access via contextconst hasTarget = (ctx) => ctx.blackboard.has('targetEid');const isHealthLow = (ctx) => (ctx.blackboard.get('health') ?? 100) < 20;
AI agents can perceive their environment through vision and hearing:
import { emitSound, StimulusType } from '@web-engine-dev/core/engine/ai';// Emit a sound stimulus that AI agents can hearemitSound(sourceEid, // Entity making the soundposition, // World position [x, y, z]intensity: 0.8, // Loudness (0-1)radius: 15.0, // Hearing radiusduration: 2.0 // How long it persists);
The AI module includes several ECS systems that should be added to your game loop:
The AI system is designed for zero garbage collection in hot paths:
Perception LOD (Level of Detail) reduces AI update frequency based on distance from focus point:
import {setPerceptionLODEnabled,setLODFocusPosition,setPerceptionLODConfig,} from '@web-engine-dev/core/engine/ai';// Enable perception LODsetPerceptionLODEnabled(true);// Set focus position (usually camera/player)setLODFocusPosition(cameraPosition);// Configure LOD distancessetPerceptionLODConfig({lodDistances: [20, 50, 100], // Distance thresholdsupdateIntervals: [0.1, 0.25, 0.5, 1.0], // Update rates per LODmaxTrackedPerLOD: [32, 16, 8, 4], // Entity tracking limits});
The AI system uses a spatial index for efficient neighbor queries:
import { getAISpatialIndex } from '@web-engine-dev/core/engine/ai';const spatialIndex = getAISpatialIndex();// Query nearby entitiesconst neighbors = spatialIndex.queryRadius(position, // Query centerradius, // Search radiusmaxResults // Maximum results);
The AI system includes debug rendering capabilities:
import { getAIDebugRenderer } from '@web-engine-dev/core/engine/ai';const debugRenderer = getAIDebugRenderer();// Configure debug optionsdebugRenderer.setOptions({showVisionCones: true,showHearingRadii: true,showTrackedEntities: true,showPaths: true,showSteeringForces: true,showFormations: true,showBehaviorTreeStatus: true,showStateNames: true,});// Render debug visualizationdebugRenderer.render(world, scene, camera);
Properly clean up AI resources when entities are destroyed:
import {cleanupAIAgent,cleanupPerception,cleanupAIEntity,resetAllAISystems,} from '@web-engine-dev/core/engine/ai';// Clean up individual entitycleanupAIAgent(eid); // Releases blackboardcleanupPerception(eid); // Releases perception storagecleanupAIEntity(eid); // Comprehensive cleanup// Reset all AI systems (scene unload)resetAllAISystems();