import { type Vec3 } from 'mathcat'; import { type NavMesh, type NodeRef, type QueryFilter, type SlicedNodePathQuery, type StraightPathPoint } from 'navcat'; import * as localBoundary from './local-boundary.js'; import * as obstacleAvoidance from './obstacle-avoidance.js'; import * as pathCorridor from './path-corridor.js'; export declare enum AgentState { INVALID = 0, WALKING = 1, OFFMESH = 2 } export declare enum AgentTargetState { NONE = 0, FAILED = 1, VALID = 2, REQUESTING = 3, WAITING_FOR_QUEUE = 4, WAITING_FOR_PATH = 5, VELOCITY = 6 } export declare enum CrowdUpdateFlags { ANTICIPATE_TURNS = 1, OBSTACLE_AVOIDANCE = 2, SEPARATION = 4, OPTIMIZE_VIS = 8, OPTIMIZE_TOPO = 16 } export type AgentParams = { /** Agent radius (in world units), used for collision detection and avoidance calculations */ radius: number; /** Agent height (in world units), used for visualization and spatial queries */ height: number; /** Maximum acceleration (in world units per second squared), controls how quickly an agent can change velocity. */ maxAcceleration: number; /** Maximum speed (in world units per second), the agent will not exceed this speed. */ maxSpeed: number; /** Collision query range (in world units), determines how far to look for neighboring agents and obstacles. */ collisionQueryRange: number; /** * Path visibility optimization range (in world units), the maximum distance to raycast for path shortcuts. * @default radius * 30.0 */ pathOptimizationRange?: number; /** Separation weight, controls the strength of separation behavior from other agents, only applied when the flag CrowdUpdateFlags.SEPARATION is given */ separationWeight: number; /** * Flags that control agent behavior * @see CrowdUpdateFlags */ updateFlags: number; /** Query filter used for navmesh queries, determines which polygons the agent can traverse and the cost of traversal */ queryFilter: QueryFilter; /** * Obstacle avoidance parameters, configures the adaptive sampling algorithm for velocity planning * @default @see DEFAULT_OBSTACLE_AVOIDANCE_PARAMS **/ obstacleAvoidance?: obstacleAvoidance.ObstacleAvoidanceParams; /** * If true, agents will automatically traverse off-mesh connections with a linear interpolation. * If false, the agent will enter OFFMESH state and populate offMeshAnimation data, * but the animation must be implemented externally, and completeOffMeshConnection must be called when done. * @default true */ autoTraverseOffMeshConnections?: boolean; /** * Whether to collect debug data for obstacle avoidance visualization. * @default false */ debugObstacleAvoidance?: boolean; }; /** Sensible default obstacle avoidance parameters */ export declare const DEFAULT_OBSTACLE_AVOIDANCE_PARAMS: obstacleAvoidance.ObstacleAvoidanceParams; export type Agent = { /** The agent radius */ radius: number; /** The agent height */ height: number; /** The agent maximum acceleration */ maxAcceleration: number; /** The agent maximum speed */ maxSpeed: number; /** The agent collision query range */ collisionQueryRange: number; /** The agent path visibility optimization range */ pathOptimizationRange: number; /** The agent separation weight */ separationWeight: number; /** The agent update flags */ updateFlags: number; /** The agent query filter */ queryFilter: QueryFilter; /** The agent obstacle avoidance parameters */ obstacleAvoidance: obstacleAvoidance.ObstacleAvoidanceParams; /** Whether the agent automatically traverses off-mesh connections */ autoTraverseOffMeshConnections: boolean; /** The current state of the agent */ state: AgentState; /** Path corridor for navigation */ corridor: pathCorridor.PathCorridor; /** Local boundary for obstacle avoidance */ boundary: localBoundary.LocalBoundary; /** Sliced pathfinding query state */ slicedQuery: SlicedNodePathQuery; /** Obstacle avoidance query for velocity planning */ obstacleAvoidanceQuery: obstacleAvoidance.ObstacleAvoidanceQuery; /** Debug data for obstacle avoidance visualization */ obstacleAvoidanceDebugData: obstacleAvoidance.ObstacleAvoidanceDebugData | undefined; /** Neighboring agents within collision query range */ neis: Array<{ agentId: string; dist: number; }>; /** Steering corners extracted from the corridor */ corners: StraightPathPoint[]; /** Current position in world space */ position: Vec3; /** Desired speed magnitude */ desiredSpeed: number; /** Desired velocity from steering */ desiredVelocity: Vec3; /** Planned velocity after obstacle avoidance */ newVelocity: Vec3; /** Current velocity */ velocity: Vec3; /** Displacement vector for collision resolution */ displacement: Vec3; /** @see AgentTargetState */ targetState: number; /** Target polygon reference */ targetRef: NodeRef | null; /** Target position */ targetPosition: Vec3; /** Whether the agent needs to replan its path to the target */ targetReplan: boolean; /** Time spent pathfinding to current target */ targetPathfindingTime: number; /** True if pathfinding returned a partial result (best-effort path) */ targetPathIsPartial: boolean; /** Time since last topology optimization (seconds) */ topologyOptTime: number; /** Off-mesh connection animation state (null when not traversing an off-mesh connection) */ offMeshAnimation: { t: number; startPosition: Vec3; endPosition: Vec3; nodeRef: NodeRef; duration: number; } | null; }; export type Crowd = { agents: Record; agentIdCounter: number; maxAgentRadius: number; agentPlacementHalfExtents: Vec3; /** * Maximum pathfinding iterations distributed across all agents per update. * Higher values allow more agents to complete pathfinding faster but increase CPU cost. * @default 600 */ maxIterationsPerUpdate: number; /** * Maximum pathfinding iterations per agent per update. * Limits how much CPU time a single agent can consume. * @default 200 */ maxIterationsPerAgent: number; /** * Initial quick search iterations when pathfinding request starts. * Helps find short paths immediately, and is used as the initial partial corridor to * start moving towards the target while the full path is still being computed. * @default 20 */ quickSearchIterations: number; }; /** * Creates a new crowd * @param maxAgentRadius the maximum agent radius in the crowd * @returns the created crowd */ export declare const create: (maxAgentRadius: number) => Crowd; /** * Adds an agent to the crowd. * @param crowd the crowd * @param position the initial position of the agent * @param agentParams the parameters for the agent * @returns the ID of the added agent */ export declare const addAgent: (crowd: Crowd, navMesh: NavMesh, position: Vec3, agentParams: AgentParams) => string; /** * Removes an agent from the crowd. * @param crowd the crowd * @param agentId the ID of the agent * @returns true if the agent was removed, false otherwise */ export declare const removeAgent: (crowd: Crowd, agentId: string) => boolean; /** * Requests a move target for an agent. * @param crowd the crowd * @param agentId the ID of the agent * @param targetRef the target reference * @param targetPos the target position * @returns true if the move target was set, false otherwise */ export declare const requestMoveTarget: (crowd: Crowd, agentId: string, targetRef: NodeRef, targetPos: Vec3) => boolean; /** * Request a move velocity for an agent. * @param crowd the crowd * @param agentId the ID of the agent * @param velocity the desired velocity * @returns true if the move velocity was set, false otherwise */ export declare const requestMoveVelocity: (crowd: Crowd, agentId: string, velocity: Vec3) => boolean; /** * Reset the move target for an agent. * @param crowd the crowd * @param agentId the ID of the agent * @returns true if the move target was reset, false otherwise */ export declare const resetMoveTarget: (crowd: Crowd, agentId: string) => boolean; /** * Manually completes an off-mesh connection for an agent. * This should be called after custom off-mesh animation is complete. * @param crowd the crowd * @param agentId the agent id * @returns true if the off-mesh connection was completed successfully, false otherwise */ export declare const completeOffMeshConnection: (crowd: Crowd, agentId: string) => boolean; /** * Update the crowd simulation. * @param crowd the crowd * @param navMesh the navigation mesh * @param deltaTime the time since the last update */ export declare const update: (crowd: Crowd, navMesh: NavMesh, deltaTime: number) => void; /** * Check if an agent is at or near the end of their corridor. * Works for both complete and partial paths - if the path is partial, * this checks if the agent reached the best-effort position. * * @param crowd the crowd * @param agentId the agent id * @param threshold distance threshold to consider "at target" * @returns true if the agent is at the end of their path */ export declare const isAgentAtTarget: (crowd: Crowd, agentId: string, threshold: number) => boolean;