/** * @file BehaviorTree.ts * @description Behavior Tree System with Graphical Editor Support * PRO-1.4: Professional Editor Features * * Features: * - Full behavior tree implementation * - Composite nodes (Selector, Sequence, Parallel) * - Decorator nodes (Inverter, Repeater, Succeeder, etc.) * - Leaf nodes (Actions, Conditions) * - Real-time preview and debugging * - Blackboard system for shared data * - Subtree support * - Serialization support */ import type { EntityId, Vec2 } from '../core/types'; /** Status returned by behavior tree nodes */ export type BTStatus = 'success' | 'failure' | 'running'; /** Visual status for editor display */ export type BTVisualStatus = BTStatus | 'inactive' | 'aborted'; /** Type-safe blackboard key */ export interface BTBlackboardKey { name: string; type: string; defaultValue?: T; } /** Blackboard for sharing data between nodes */ export declare class BTBlackboard { private data; private types; private listeners; /** Set a value in the blackboard */ set(key: BTBlackboardKey | string, value: T): void; /** Get a value from the blackboard */ get(key: BTBlackboardKey | string): T | undefined; /** Check if key exists */ has(key: BTBlackboardKey | string): boolean; /** Remove a key */ delete(key: BTBlackboardKey | string): void; /** Clear all data */ clear(): void; /** Subscribe to key changes */ subscribe(key: BTBlackboardKey | string, callback: (value: T) => void): () => void; /** Get all keys */ keys(): string[]; /** Clone the blackboard */ clone(): BTBlackboard; /** Export to JSON */ toJSON(): Record; /** Import from JSON */ fromJSON(data: Record): void; } export interface BTContext { /** The entity this tree is controlling */ entityId: EntityId; /** Shared blackboard */ blackboard: BTBlackboard; /** Delta time since last tick */ dt: number; /** Total time elapsed */ time: number; /** World reference */ world: unknown; /** Event emitter for external communication */ emit: (event: string, data?: unknown) => void; /** Cancel current execution */ abort: () => void; /** Is execution aborted? */ isAborted: boolean; } export type BTNodeType = 'root' | 'composite' | 'decorator' | 'action' | 'condition' | 'subtree'; export interface BTNodeDefinition { type: string; category: BTNodeType; name: string; description: string; icon?: string; color?: string; /** Properties that can be configured in editor */ properties?: BTPropertyDef[]; /** Create a node instance */ create: (properties?: Record) => BTNode; } export interface BTPropertyDef { name: string; type: 'boolean' | 'number' | 'string' | 'enum' | 'blackboardKey' | 'entity'; default?: unknown; enumValues?: string[]; description?: string; min?: number; max?: number; } /** Base class for all behavior tree nodes */ export declare abstract class BTNode { id: string; name: string; type: string; category: BTNodeType; /** Position in editor */ position: Vec2; /** Custom properties */ properties: Record; /** Comment for documentation */ comment?: string; /** Current execution status (for debugging) */ lastStatus: BTVisualStatus; /** Last execution time (for profiling) */ lastExecutionTime: number; constructor(type: string, category: BTNodeType, name: string); /** Initialize the node */ initialize(context: BTContext): void; /** Tick the node */ abstract tick(context: BTContext): BTStatus; /** Reset the node state */ reset(): void; /** Called when execution is aborted */ abort(): void; /** Clone the node */ abstract clone(): BTNode; /** Serialize to JSON */ toJSON(): BTNodeJSON; } export interface BTNodeJSON { id: string; type: string; category: BTNodeType; name: string; position: Vec2; properties: Record; comment?: string; children?: BTNodeJSON[]; } /** Base class for composite nodes (have children) */ export declare abstract class BTComposite extends BTNode { children: BTNode[]; protected currentChild: number; constructor(type: string, name: string); addChild(child: BTNode): void; removeChild(child: BTNode): void; reset(): void; abort(): void; toJSON(): BTNodeJSON; } /** Sequence: Runs children in order until one fails */ export declare class BTSequence extends BTComposite { constructor(name?: string); tick(context: BTContext): BTStatus; clone(): BTSequence; } /** Selector: Runs children until one succeeds */ export declare class BTSelector extends BTComposite { constructor(name?: string); tick(context: BTContext): BTStatus; clone(): BTSelector; } /** Parallel: Runs all children simultaneously */ export declare class BTParallel extends BTComposite { /** Policy for determining success */ successPolicy: 'all' | 'one'; /** Policy for determining failure */ failurePolicy: 'all' | 'one'; private childStatuses; constructor(name?: string); initialize(context: BTContext): void; tick(context: BTContext): BTStatus; reset(): void; clone(): BTParallel; } /** Random Selector: Selects children in random order */ export declare class BTRandomSelector extends BTComposite { private shuffledOrder; constructor(name?: string); initialize(context: BTContext): void; private shuffle; tick(context: BTContext): BTStatus; clone(): BTRandomSelector; } /** Random Sequence: Runs children in random order */ export declare class BTRandomSequence extends BTComposite { private shuffledOrder; constructor(name?: string); initialize(context: BTContext): void; private shuffle; tick(context: BTContext): BTStatus; clone(): BTRandomSequence; } /** Base class for decorator nodes (single child) */ export declare abstract class BTDecorator extends BTNode { child: BTNode | null; constructor(type: string, name: string); setChild(child: BTNode): void; reset(): void; abort(): void; toJSON(): BTNodeJSON; } /** Inverter: Inverts child's result */ export declare class BTInverter extends BTDecorator { constructor(name?: string); tick(context: BTContext): BTStatus; clone(): BTInverter; } /** Succeeder: Always returns success */ export declare class BTSucceeder extends BTDecorator { constructor(name?: string); tick(context: BTContext): BTStatus; clone(): BTSucceeder; } /** Failer: Always returns failure */ export declare class BTFailer extends BTDecorator { constructor(name?: string); tick(context: BTContext): BTStatus; clone(): BTFailer; } /** Repeater: Repeats child N times */ export declare class BTRepeater extends BTDecorator { times: number; private currentCount; constructor(name?: string, times?: number); tick(context: BTContext): BTStatus; reset(): void; clone(): BTRepeater; } /** RepeatUntilFail: Repeats until child fails */ export declare class BTRepeatUntilFail extends BTDecorator { constructor(name?: string); tick(context: BTContext): BTStatus; clone(): BTRepeatUntilFail; } /** RepeatUntilSuccess: Repeats until child succeeds */ export declare class BTRepeatUntilSuccess extends BTDecorator { constructor(name?: string); tick(context: BTContext): BTStatus; clone(): BTRepeatUntilSuccess; } /** Timeout: Fails if child takes too long */ export declare class BTTimeout extends BTDecorator { timeoutSeconds: number; private elapsed; private started; constructor(name?: string, seconds?: number); tick(context: BTContext): BTStatus; reset(): void; clone(): BTTimeout; } /** Cooldown: Rate limits child execution */ export declare class BTCooldown extends BTDecorator { cooldownSeconds: number; private lastExecution; constructor(name?: string, seconds?: number); tick(context: BTContext): BTStatus; reset(): void; clone(): BTCooldown; } /** Condition Guard: Only runs child if condition is true */ export declare class BTConditionGuard extends BTDecorator { condition: (context: BTContext) => boolean; constructor(name?: string, condition?: (context: BTContext) => boolean); tick(context: BTContext): BTStatus; clone(): BTConditionGuard; } /** Action Node: Performs an action */ export declare class BTAction extends BTNode { action: (context: BTContext) => BTStatus; constructor(name: string, action: (context: BTContext) => BTStatus); tick(context: BTContext): BTStatus; clone(): BTAction; } /** Condition Node: Tests a condition */ export declare class BTCondition extends BTNode { condition: (context: BTContext) => boolean; constructor(name: string, condition: (context: BTContext) => boolean); tick(context: BTContext): BTStatus; clone(): BTCondition; } /** Wait Action: Waits for a duration */ export declare class BTWait extends BTNode { seconds: number; private elapsed; private started; constructor(name?: string, seconds?: number); tick(context: BTContext): BTStatus; reset(): void; clone(): BTWait; } /** Log Action: Logs a message */ export declare class BTLog extends BTNode { message: string; level: 'info' | 'warn' | 'error'; constructor(name?: string, message?: string, level?: 'info' | 'warn' | 'error'); tick(context: BTContext): BTStatus; clone(): BTLog; } export declare class BTSubtree extends BTNode { subtreeId: string; private subtree; constructor(name: string, subtreeId: string); setSubtree(tree: BehaviorTree): void; tick(context: BTContext): BTStatus; reset(): void; clone(): BTSubtree; } export declare class BTRoot extends BTNode { child: BTNode | null; constructor(name?: string); setChild(child: BTNode): void; tick(context: BTContext): BTStatus; reset(): void; toJSON(): BTNodeJSON; clone(): BTRoot; } export declare class BehaviorTree { id: string; name: string; root: BTRoot; blackboard: BTBlackboard; /** Execution statistics */ stats: { tickCount: number; lastTickTime: number; averageTickTime: number; }; /** Debugging */ debugMode: boolean; breakpoints: Set; private isPaused; constructor(name?: string); /** Tick the tree */ tick(context: BTContext): BTStatus; /** Reset the tree */ reset(): void; /** Pause execution */ pause(): void; /** Resume execution */ resume(): void; /** Add breakpoint */ addBreakpoint(nodeId: string): void; /** Remove breakpoint */ removeBreakpoint(nodeId: string): void; /** Toggle breakpoint */ toggleBreakpoint(nodeId: string): void; /** Find node by ID */ findNode(nodeId: string): BTNode | null; private findNodeRecursive; /** Get all nodes */ getAllNodes(): BTNode[]; private collectNodes; /** Clone the tree */ clone(): BehaviorTree; /** Serialize to JSON */ toJSON(): { id: string; name: string; root: BTNodeJSON; blackboard: Record; }; } export declare class BTNodeRegistry { private definitions; register(definition: BTNodeDefinition): void; get(type: string): BTNodeDefinition | undefined; getAll(): BTNodeDefinition[]; getByCategory(category: BTNodeType): BTNodeDefinition[]; createNode(type: string, properties?: Record): BTNode | null; } export declare function createBehaviorTree(name?: string): BehaviorTree; export declare function createBlackboard(): BTBlackboard; export declare function createNodeRegistry(): BTNodeRegistry; /** Create execution context */ export declare function createBTContext(params: { entityId: EntityId; dt: number; time: number; world: unknown; blackboard?: BTBlackboard; }): BTContext;