@holoscript/framework 6.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ALL-test-results.json +1 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/ROADMAP.md +175 -0
- package/dist/AgentManifest-CB4xM-Ma.d.cts +704 -0
- package/dist/AgentManifest-CB4xM-Ma.d.ts +704 -0
- package/dist/BehaviorTree-BrBFECv5.d.cts +103 -0
- package/dist/BehaviorTree-BrBFECv5.d.ts +103 -0
- package/dist/InvisibleWallet-BB6tFvRA.d.cts +1732 -0
- package/dist/InvisibleWallet-rtRrBOA8.d.ts +1732 -0
- package/dist/OrchestratorAgent-BvWgf9uw.d.cts +798 -0
- package/dist/OrchestratorAgent-Q_CbVTmO.d.ts +798 -0
- package/dist/agents/index.cjs +4790 -0
- package/dist/agents/index.d.cts +1788 -0
- package/dist/agents/index.d.ts +1788 -0
- package/dist/agents/index.js +4695 -0
- package/dist/ai/index.cjs +5347 -0
- package/dist/ai/index.d.cts +1753 -0
- package/dist/ai/index.d.ts +1753 -0
- package/dist/ai/index.js +5244 -0
- package/dist/behavior.cjs +449 -0
- package/dist/behavior.d.cts +130 -0
- package/dist/behavior.d.ts +130 -0
- package/dist/behavior.js +407 -0
- package/dist/economy/index.cjs +3659 -0
- package/dist/economy/index.d.cts +747 -0
- package/dist/economy/index.d.ts +747 -0
- package/dist/economy/index.js +3617 -0
- package/dist/implementations-D9T3un9D.d.cts +236 -0
- package/dist/implementations-D9T3un9D.d.ts +236 -0
- package/dist/index.cjs +24550 -0
- package/dist/index.d.cts +1729 -0
- package/dist/index.d.ts +1729 -0
- package/dist/index.js +24277 -0
- package/dist/learning/index.cjs +219 -0
- package/dist/learning/index.d.cts +104 -0
- package/dist/learning/index.d.ts +104 -0
- package/dist/learning/index.js +189 -0
- package/dist/negotiation/index.cjs +970 -0
- package/dist/negotiation/index.d.cts +610 -0
- package/dist/negotiation/index.d.ts +610 -0
- package/dist/negotiation/index.js +931 -0
- package/dist/skills/index.cjs +1118 -0
- package/dist/skills/index.d.cts +289 -0
- package/dist/skills/index.d.ts +289 -0
- package/dist/skills/index.js +1079 -0
- package/dist/swarm/index.cjs +5268 -0
- package/dist/swarm/index.d.cts +2433 -0
- package/dist/swarm/index.d.ts +2433 -0
- package/dist/swarm/index.js +5221 -0
- package/dist/training/index.cjs +2745 -0
- package/dist/training/index.d.cts +1734 -0
- package/dist/training/index.d.ts +1734 -0
- package/dist/training/index.js +2687 -0
- package/extract-failures.js +10 -0
- package/package.json +82 -0
- package/src/__tests__/bounty-marketplace.test.ts +374 -0
- package/src/__tests__/delegation.test.ts +144 -0
- package/src/__tests__/distributed-claimer.test.ts +147 -0
- package/src/__tests__/done-log-audit.test.ts +342 -0
- package/src/__tests__/framework.test.ts +865 -0
- package/src/__tests__/goal-synthesizer.test.ts +236 -0
- package/src/__tests__/presence.test.ts +223 -0
- package/src/__tests__/protocol-agent.test.ts +254 -0
- package/src/__tests__/revenue-splitter.test.ts +114 -0
- package/src/__tests__/scenario-driven-todo.test.ts +197 -0
- package/src/__tests__/self-improve.test.ts +349 -0
- package/src/__tests__/service-lifecycle.test.ts +237 -0
- package/src/__tests__/skill-router.test.ts +121 -0
- package/src/agents/AgentManifest.ts +493 -0
- package/src/agents/AgentRegistry.ts +475 -0
- package/src/agents/AgentTypes.ts +585 -0
- package/src/agents/AgentWalletRegistry.ts +83 -0
- package/src/agents/AuthenticatedCRDT.ts +388 -0
- package/src/agents/CapabilityMatcher.ts +453 -0
- package/src/agents/CrossRealityHandoff.ts +305 -0
- package/src/agents/CulturalMemory.ts +454 -0
- package/src/agents/FederatedRegistryAdapter.ts +429 -0
- package/src/agents/NormEngine.ts +450 -0
- package/src/agents/OrchestratorAgent.ts +414 -0
- package/src/agents/SkillWorkflowEngine.ts +472 -0
- package/src/agents/TaskDelegationService.ts +551 -0
- package/src/agents/__tests__/AgentManifest.prod.test.ts +134 -0
- package/src/agents/__tests__/AgentManifest.test.ts +182 -0
- package/src/agents/__tests__/AgentModule.test.ts +864 -0
- package/src/agents/__tests__/AgentRegistry.prod.test.ts +125 -0
- package/src/agents/__tests__/AgentRegistry.test.ts +148 -0
- package/src/agents/__tests__/AgentTypes.test.ts +534 -0
- package/src/agents/__tests__/AgentWalletRegistry.test.ts +152 -0
- package/src/agents/__tests__/AuthenticatedCRDT.test.ts +558 -0
- package/src/agents/__tests__/CapabilityMatcher.prod.test.ts +117 -0
- package/src/agents/__tests__/CapabilityMatcher.test.ts +178 -0
- package/src/agents/__tests__/CrossRealityHandoff.test.ts +402 -0
- package/src/agents/__tests__/CulturalMemory.test.ts +200 -0
- package/src/agents/__tests__/FederatedRegistryAdapter.test.ts +409 -0
- package/src/agents/__tests__/NormEngine.test.ts +276 -0
- package/src/agents/__tests__/OrchestratorAgent.test.ts +182 -0
- package/src/agents/__tests__/SkillWorkflowEngine.test.ts +357 -0
- package/src/agents/__tests__/TaskDelegationService.test.ts +446 -0
- package/src/agents/index.ts +107 -0
- package/src/agents/spatial-comms/Layer1RealTime.ts +621 -0
- package/src/agents/spatial-comms/Layer2A2A.ts +661 -0
- package/src/agents/spatial-comms/Layer3MCP.ts +651 -0
- package/src/agents/spatial-comms/ProtocolTypes.ts +543 -0
- package/src/agents/spatial-comms/SpatialCommClient.ts +483 -0
- package/src/agents/spatial-comms/__tests__/performance-benchmark.test.ts +465 -0
- package/src/agents/spatial-comms/examples/multi-agent-world-creation.ts +409 -0
- package/src/agents/spatial-comms/index.ts +66 -0
- package/src/ai/AIAdapter.ts +313 -0
- package/src/ai/AICopilot.ts +331 -0
- package/src/ai/AIOutputValidator.ts +203 -0
- package/src/ai/BTNodes.ts +239 -0
- package/src/ai/BehaviorSelector.ts +135 -0
- package/src/ai/BehaviorTree.ts +153 -0
- package/src/ai/Blackboard.ts +165 -0
- package/src/ai/GenerationAnalytics.ts +461 -0
- package/src/ai/GenerationCache.ts +265 -0
- package/src/ai/GoalPlanner.ts +165 -0
- package/src/ai/HoloScriptGenerator.ts +580 -0
- package/src/ai/InfluenceMap.ts +180 -0
- package/src/ai/NavMesh.ts +168 -0
- package/src/ai/PerceptionSystem.ts +178 -0
- package/src/ai/PromptTemplates.ts +453 -0
- package/src/ai/SemanticSearchService.ts +80 -0
- package/src/ai/StateMachine.ts +196 -0
- package/src/ai/SteeringBehavior.ts +150 -0
- package/src/ai/SteeringBehaviors.ts +244 -0
- package/src/ai/TrainingDataGenerator.ts +1082 -0
- package/src/ai/UtilityAI.ts +145 -0
- package/src/ai/__tests__/AIAdapter.prod.test.ts +259 -0
- package/src/ai/__tests__/AIAdapter.test.ts +109 -0
- package/src/ai/__tests__/AICopilot.prod.test.ts +341 -0
- package/src/ai/__tests__/AICopilot.test.ts +178 -0
- package/src/ai/__tests__/AIOutputValidator.prod.test.ts +226 -0
- package/src/ai/__tests__/AIOutputValidator.test.ts +138 -0
- package/src/ai/__tests__/BTNodes.prod.test.ts +391 -0
- package/src/ai/__tests__/BTNodes.test.ts +263 -0
- package/src/ai/__tests__/BehaviorSelector.prod.test.ts +129 -0
- package/src/ai/__tests__/BehaviorSelector.test.ts +132 -0
- package/src/ai/__tests__/BehaviorTree.prod.test.ts +266 -0
- package/src/ai/__tests__/BehaviorTree.test.ts +216 -0
- package/src/ai/__tests__/Blackboard.prod.test.ts +339 -0
- package/src/ai/__tests__/Blackboard.test.ts +183 -0
- package/src/ai/__tests__/GenerationAnalytics.prod.test.ts +141 -0
- package/src/ai/__tests__/GenerationAnalytics.test.ts +165 -0
- package/src/ai/__tests__/GenerationCache.prod.test.ts +144 -0
- package/src/ai/__tests__/GenerationCache.test.ts +171 -0
- package/src/ai/__tests__/GoalPlanner.prod.test.ts +189 -0
- package/src/ai/__tests__/GoalPlanner.test.ts +137 -0
- package/src/ai/__tests__/GoalPlannerDepth.prod.test.ts +217 -0
- package/src/ai/__tests__/HoloScriptGenerator.test.ts +125 -0
- package/src/ai/__tests__/InfluenceMap.prod.test.ts +146 -0
- package/src/ai/__tests__/InfluenceMap.test.ts +149 -0
- package/src/ai/__tests__/NavMesh.prod.test.ts +141 -0
- package/src/ai/__tests__/NavMesh.test.ts +159 -0
- package/src/ai/__tests__/PerceptionSystem.prod.test.ts +135 -0
- package/src/ai/__tests__/PerceptionSystem.test.ts +250 -0
- package/src/ai/__tests__/PromptTemplates.prod.test.ts +313 -0
- package/src/ai/__tests__/PromptTemplates.test.ts +146 -0
- package/src/ai/__tests__/SemanticSearch.test.ts +37 -0
- package/src/ai/__tests__/StateMachine.prod.test.ts +162 -0
- package/src/ai/__tests__/StateMachine.test.ts +163 -0
- package/src/ai/__tests__/SteeringBehavior.prod.test.ts +251 -0
- package/src/ai/__tests__/SteeringBehavior.test.ts +135 -0
- package/src/ai/__tests__/SteeringBehaviors.prod.test.ts +133 -0
- package/src/ai/__tests__/SteeringBehaviors.test.ts +151 -0
- package/src/ai/__tests__/TrainingDataGenerator.prod.test.ts +286 -0
- package/src/ai/__tests__/TrainingDataGenerator.test.ts +286 -0
- package/src/ai/__tests__/UtilityAI.prod.test.ts +207 -0
- package/src/ai/__tests__/UtilityAI.test.ts +155 -0
- package/src/ai/__tests__/adapters.prod.test.ts +263 -0
- package/src/ai/__tests__/adapters.test.ts +320 -0
- package/src/ai/adapters.ts +1585 -0
- package/src/ai/index.ts +130 -0
- package/src/behavior/BehaviorPresets.ts +140 -0
- package/src/behavior/BehaviorTree.ts +236 -0
- package/src/behavior/StateMachine.ts +176 -0
- package/src/behavior/StateTrait.ts +67 -0
- package/src/behavior/index.ts +8 -0
- package/src/behavior.ts +8 -0
- package/src/board/audit.ts +284 -0
- package/src/board/board-ops.ts +336 -0
- package/src/board/board-types.ts +302 -0
- package/src/board/index.ts +69 -0
- package/src/define-agent.ts +46 -0
- package/src/define-team.ts +33 -0
- package/src/delegation.ts +265 -0
- package/src/distributed-claimer.ts +228 -0
- package/src/economy/AgentBudgetEnforcer.ts +464 -0
- package/src/economy/BountyManager.ts +185 -0
- package/src/economy/CreatorRevenueAggregator.ts +460 -0
- package/src/economy/InvisibleWallet.ts +82 -0
- package/src/economy/KnowledgeMarketplace.ts +193 -0
- package/src/economy/PaymentWebhookService.ts +512 -0
- package/src/economy/RevenueSplitter.ts +156 -0
- package/src/economy/SubscriptionManager.ts +546 -0
- package/src/economy/UnifiedBudgetOptimizer.ts +635 -0
- package/src/economy/UsageMeter.ts +440 -0
- package/src/economy/_core-stubs.ts +219 -0
- package/src/economy/index.ts +100 -0
- package/src/economy/x402-facilitator.ts +1978 -0
- package/src/index.ts +348 -0
- package/src/knowledge/__tests__/knowledge-consolidator.test.ts +444 -0
- package/src/knowledge/__tests__/knowledge-store-vector.test.ts +291 -0
- package/src/knowledge/brain.ts +167 -0
- package/src/knowledge/consolidation.ts +581 -0
- package/src/knowledge/knowledge-consolidator.ts +510 -0
- package/src/knowledge/knowledge-store.ts +616 -0
- package/src/learning/MemoryConsolidator.ts +102 -0
- package/src/learning/MemoryScorer.ts +69 -0
- package/src/learning/ProceduralCompiler.ts +45 -0
- package/src/learning/SemanticClusterer.ts +66 -0
- package/src/learning/index.ts +8 -0
- package/src/llm/llm-adapter.ts +159 -0
- package/src/mesh/index.ts +309 -0
- package/src/negotiation/NegotiationProtocol.ts +694 -0
- package/src/negotiation/NegotiationTypes.ts +473 -0
- package/src/negotiation/VotingMechanisms.ts +691 -0
- package/src/negotiation/index.ts +49 -0
- package/src/protocol/goal-synthesizer.ts +317 -0
- package/src/protocol/implementations.ts +474 -0
- package/src/protocol/micro-phase-decomposer.ts +299 -0
- package/src/protocol/micro-step-decomposer.test.ts +306 -0
- package/src/protocol-agent.test.ts +353 -0
- package/src/protocol-agent.ts +670 -0
- package/src/self-improve/absorb-scanner.ts +252 -0
- package/src/self-improve/evolution-engine.ts +149 -0
- package/src/self-improve/framework-absorber.ts +214 -0
- package/src/self-improve/index.ts +50 -0
- package/src/self-improve/prompt-optimizer.ts +212 -0
- package/src/self-improve/test-generator.ts +175 -0
- package/src/skill-router.ts +186 -0
- package/src/skills/index.ts +5 -0
- package/src/skills/skill-md-bridge.ts +1699 -0
- package/src/swarm/ACOEngine.ts +261 -0
- package/src/swarm/CollectiveIntelligence.ts +383 -0
- package/src/swarm/ContributionSynthesizer.ts +481 -0
- package/src/swarm/LeaderElection.ts +393 -0
- package/src/swarm/PSOEngine.ts +206 -0
- package/src/swarm/QuorumPolicy.ts +173 -0
- package/src/swarm/SwarmCoordinator.ts +335 -0
- package/src/swarm/SwarmManager.ts +442 -0
- package/src/swarm/SwarmMembership.ts +456 -0
- package/src/swarm/VotingRound.ts +255 -0
- package/src/swarm/__tests__/ACOEngine.prod.test.ts +164 -0
- package/src/swarm/__tests__/ACOEngine.test.ts +117 -0
- package/src/swarm/__tests__/CollectiveIntelligence.prod.test.ts +296 -0
- package/src/swarm/__tests__/CollectiveIntelligence.test.ts +457 -0
- package/src/swarm/__tests__/ContributionSynthesizer.prod.test.ts +269 -0
- package/src/swarm/__tests__/ContributionSynthesizer.test.ts +254 -0
- package/src/swarm/__tests__/LeaderElection.prod.test.ts +196 -0
- package/src/swarm/__tests__/LeaderElection.test.ts +151 -0
- package/src/swarm/__tests__/PSOEngine.prod.test.ts +162 -0
- package/src/swarm/__tests__/PSOEngine.test.ts +106 -0
- package/src/swarm/__tests__/QuorumPolicy.prod.test.ts +216 -0
- package/src/swarm/__tests__/QuorumPolicy.test.ts +177 -0
- package/src/swarm/__tests__/SwarmCoordinator.prod.test.ts +186 -0
- package/src/swarm/__tests__/SwarmCoordinator.test.ts +167 -0
- package/src/swarm/__tests__/SwarmManager.prod.test.ts +308 -0
- package/src/swarm/__tests__/SwarmManager.test.ts +373 -0
- package/src/swarm/__tests__/SwarmMembership.prod.test.ts +273 -0
- package/src/swarm/__tests__/SwarmMembership.test.ts +264 -0
- package/src/swarm/__tests__/VotingRound.prod.test.ts +233 -0
- package/src/swarm/__tests__/VotingRound.test.ts +174 -0
- package/src/swarm/analytics/SwarmInspector.ts +476 -0
- package/src/swarm/analytics/SwarmMetrics.ts +449 -0
- package/src/swarm/analytics/__tests__/SwarmInspector.prod.test.ts +366 -0
- package/src/swarm/analytics/__tests__/SwarmInspector.test.ts +454 -0
- package/src/swarm/analytics/__tests__/SwarmMetrics.prod.test.ts +254 -0
- package/src/swarm/analytics/__tests__/SwarmMetrics.test.ts +370 -0
- package/src/swarm/analytics/index.ts +7 -0
- package/src/swarm/index.ts +69 -0
- package/src/swarm/messaging/BroadcastChannel.ts +509 -0
- package/src/swarm/messaging/GossipProtocol.ts +565 -0
- package/src/swarm/messaging/SwarmEventBus.ts +443 -0
- package/src/swarm/messaging/__tests__/BroadcastChannel.prod.test.ts +331 -0
- package/src/swarm/messaging/__tests__/BroadcastChannel.test.ts +333 -0
- package/src/swarm/messaging/__tests__/GossipProtocol.prod.test.ts +356 -0
- package/src/swarm/messaging/__tests__/GossipProtocol.test.ts +437 -0
- package/src/swarm/messaging/__tests__/SwarmEventBus.prod.test.ts +191 -0
- package/src/swarm/messaging/__tests__/SwarmEventBus.test.ts +247 -0
- package/src/swarm/messaging/index.ts +8 -0
- package/src/swarm/spatial/FlockingBehavior.ts +462 -0
- package/src/swarm/spatial/FormationController.ts +500 -0
- package/src/swarm/spatial/Vector3.ts +170 -0
- package/src/swarm/spatial/ZoneClaiming.ts +509 -0
- package/src/swarm/spatial/__tests__/FlockingBehavior.prod.test.ts +239 -0
- package/src/swarm/spatial/__tests__/FlockingBehavior.test.ts +298 -0
- package/src/swarm/spatial/__tests__/FormationController.prod.test.ts +240 -0
- package/src/swarm/spatial/__tests__/FormationController.test.ts +297 -0
- package/src/swarm/spatial/__tests__/Vector3.prod.test.ts +283 -0
- package/src/swarm/spatial/__tests__/Vector3.test.ts +224 -0
- package/src/swarm/spatial/__tests__/ZoneClaiming.prod.test.ts +246 -0
- package/src/swarm/spatial/__tests__/ZoneClaiming.test.ts +374 -0
- package/src/swarm/spatial/index.ts +28 -0
- package/src/team.ts +1245 -0
- package/src/training/LRScheduler.ts +377 -0
- package/src/training/QualityScoringPipeline.ts +139 -0
- package/src/training/SoftDedup.ts +461 -0
- package/src/training/SparsityMonitor.ts +685 -0
- package/src/training/SparsityMonitorTypes.ts +209 -0
- package/src/training/SpatialTrainingDataGenerator.ts +1526 -0
- package/src/training/SpatialTrainingDataTypes.ts +216 -0
- package/src/training/TrainingPipelineConfig.ts +215 -0
- package/src/training/constants.ts +94 -0
- package/src/training/index.ts +138 -0
- package/src/training/schema.ts +147 -0
- package/src/training/scripts/generate-novel-use-cases-dataset.ts +272 -0
- package/src/training/scripts/generate-spatial-dataset.ts +521 -0
- package/src/training/training/data/novel-use-cases.jsonl +153 -0
- package/src/training/training/data/spatial-reasoning-10k.jsonl +9354 -0
- package/src/training/trainingmonkey/TrainingMonkeyIntegration.ts +477 -0
- package/src/training/trainingmonkey/TrainingMonkeyTypes.ts +230 -0
- package/src/training/trainingmonkey/index.ts +26 -0
- package/src/training/trait-mappings.ts +157 -0
- package/src/types/core-stubs.d.ts +113 -0
- package/src/types.ts +304 -0
- package/test-output.txt +0 -0
- package/test-result.json +1 -0
- package/tsc-errors.txt +4 -0
- package/tsc_output.txt +0 -0
- package/tsconfig.json +14 -0
- package/tsup-learning-esm.config.ts +12 -0
- package/tsup.config.ts +21 -0
- package/typescript-errors-2.txt +0 -0
- package/typescript-errors.txt +22 -0
- package/vitest-log-utf8.txt +268 -0
- package/vitest-log.txt +0 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StateMachine.ts
|
|
3
|
+
*
|
|
4
|
+
* Hierarchical state machine: states with enter/exit/update hooks,
|
|
5
|
+
* transitions with guards, sub-state machines, and event dispatch.
|
|
6
|
+
*
|
|
7
|
+
* @module ai
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// TYPES
|
|
12
|
+
// =============================================================================
|
|
13
|
+
|
|
14
|
+
export type StateAction = (context: Record<string, unknown>) => void;
|
|
15
|
+
export type GuardFn = (context: Record<string, unknown>) => boolean;
|
|
16
|
+
|
|
17
|
+
export interface StateConfig {
|
|
18
|
+
id: string;
|
|
19
|
+
onEnter?: StateAction;
|
|
20
|
+
onExit?: StateAction;
|
|
21
|
+
onUpdate?: StateAction;
|
|
22
|
+
parent?: string; // Parent state ID for hierarchy
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface TransitionConfig {
|
|
26
|
+
from: string;
|
|
27
|
+
to: string;
|
|
28
|
+
event: string;
|
|
29
|
+
guard?: GuardFn;
|
|
30
|
+
action?: StateAction;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// =============================================================================
|
|
34
|
+
// STATE MACHINE
|
|
35
|
+
// =============================================================================
|
|
36
|
+
|
|
37
|
+
export class StateMachine {
|
|
38
|
+
private states: Map<string, StateConfig> = new Map();
|
|
39
|
+
private transitions: TransitionConfig[] = [];
|
|
40
|
+
private currentStateId: string | null = null;
|
|
41
|
+
private context: Record<string, unknown> = {};
|
|
42
|
+
private history: string[] = [];
|
|
43
|
+
private maxHistory = 50;
|
|
44
|
+
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// State Management
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
addState(config: StateConfig): void {
|
|
50
|
+
this.states.set(config.id, config);
|
|
51
|
+
}
|
|
52
|
+
removeState(id: string): void {
|
|
53
|
+
this.states.delete(id);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
setInitialState(id: string): void {
|
|
57
|
+
this.currentStateId = id;
|
|
58
|
+
const chain = this.getStateChain(id);
|
|
59
|
+
for (const stateId of chain) {
|
|
60
|
+
const state = this.states.get(stateId);
|
|
61
|
+
if (state?.onEnter) state.onEnter(this.context);
|
|
62
|
+
}
|
|
63
|
+
this.history.push(id);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getCurrentState(): string | null {
|
|
67
|
+
return this.currentStateId;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// Transitions
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
addTransition(config: TransitionConfig): void {
|
|
75
|
+
this.transitions.push(config);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
send(event: string): boolean {
|
|
79
|
+
if (!this.currentStateId) return false;
|
|
80
|
+
|
|
81
|
+
// Check transitions from current state (and parent states)
|
|
82
|
+
const candidates = this.getTransitionsFor(this.currentStateId, event);
|
|
83
|
+
|
|
84
|
+
for (const transition of candidates) {
|
|
85
|
+
if (transition.guard && !transition.guard(this.context)) continue;
|
|
86
|
+
|
|
87
|
+
const fromChain = this.getStateChain(this.currentStateId);
|
|
88
|
+
const toChain = this.getStateChain(transition.to);
|
|
89
|
+
|
|
90
|
+
let lcaIndex = 0;
|
|
91
|
+
while (
|
|
92
|
+
lcaIndex < fromChain.length &&
|
|
93
|
+
lcaIndex < toChain.length &&
|
|
94
|
+
fromChain[lcaIndex] === toChain[lcaIndex]
|
|
95
|
+
) {
|
|
96
|
+
lcaIndex++;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Exit states up to Lowest Common Ancestor
|
|
100
|
+
for (let i = fromChain.length - 1; i >= lcaIndex; i--) {
|
|
101
|
+
const state = this.states.get(fromChain[i]);
|
|
102
|
+
if (state?.onExit) state.onExit(this.context);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Run transition action
|
|
106
|
+
if (transition.action) transition.action(this.context);
|
|
107
|
+
|
|
108
|
+
// Enter new states from LCA down to target
|
|
109
|
+
for (let i = lcaIndex; i < toChain.length; i++) {
|
|
110
|
+
const state = this.states.get(toChain[i]);
|
|
111
|
+
if (state?.onEnter) state.onEnter(this.context);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this.currentStateId = transition.to;
|
|
115
|
+
|
|
116
|
+
this.history.push(transition.to);
|
|
117
|
+
if (this.history.length > this.maxHistory) this.history.shift();
|
|
118
|
+
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private getTransitionsFor(stateId: string, event: string): TransitionConfig[] {
|
|
126
|
+
const result: TransitionConfig[] = [];
|
|
127
|
+
|
|
128
|
+
// Direct transitions
|
|
129
|
+
result.push(...this.transitions.filter((t) => t.from === stateId && t.event === event));
|
|
130
|
+
|
|
131
|
+
// Check parent state transitions (hierarchy)
|
|
132
|
+
const state = this.states.get(stateId);
|
|
133
|
+
if (state?.parent) {
|
|
134
|
+
result.push(...this.getTransitionsFor(state.parent, event));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
// Enter/Exit with Hierarchy (Chained)
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
|
|
144
|
+
private getStateChain(id: string): string[] {
|
|
145
|
+
const chain: string[] = [];
|
|
146
|
+
let currentId: string | undefined = id;
|
|
147
|
+
while (currentId) {
|
|
148
|
+
chain.unshift(currentId);
|
|
149
|
+
currentId = this.states.get(currentId)?.parent;
|
|
150
|
+
}
|
|
151
|
+
return chain;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// Update
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
update(): void {
|
|
159
|
+
if (!this.currentStateId) return;
|
|
160
|
+
const state = this.states.get(this.currentStateId);
|
|
161
|
+
if (state?.onUpdate) state.onUpdate(this.context);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
// Context
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
setContext(key: string, value: unknown): void {
|
|
169
|
+
this.context[key] = value;
|
|
170
|
+
}
|
|
171
|
+
getContext(key: string): unknown {
|
|
172
|
+
return this.context[key];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---------------------------------------------------------------------------
|
|
176
|
+
// Queries
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
|
|
179
|
+
getStateCount(): number {
|
|
180
|
+
return this.states.size;
|
|
181
|
+
}
|
|
182
|
+
getHistory(): string[] {
|
|
183
|
+
return [...this.history];
|
|
184
|
+
}
|
|
185
|
+
isInState(id: string): boolean {
|
|
186
|
+
return this.currentStateId === id;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
getChildStates(parentId: string): string[] {
|
|
190
|
+
const children: string[] = [];
|
|
191
|
+
for (const [id, state] of this.states) {
|
|
192
|
+
if (state.parent === parentId) children.push(id);
|
|
193
|
+
}
|
|
194
|
+
return children;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SteeringBehavior — Seek/flee/arrive/wander/avoid with weighted blending
|
|
3
|
+
*
|
|
4
|
+
* @version 1.0.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface Vec2 {
|
|
8
|
+
x: number;
|
|
9
|
+
z: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type SteeringType = 'seek' | 'flee' | 'arrive' | 'wander' | 'avoid' | 'pursue' | 'evade';
|
|
13
|
+
|
|
14
|
+
export interface SteeringAgent {
|
|
15
|
+
position: Vec2;
|
|
16
|
+
velocity: Vec2;
|
|
17
|
+
maxSpeed: number;
|
|
18
|
+
maxForce: number;
|
|
19
|
+
mass: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SteeringOutput {
|
|
23
|
+
force: Vec2;
|
|
24
|
+
type: SteeringType;
|
|
25
|
+
weight: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class SteeringBehavior {
|
|
29
|
+
/**
|
|
30
|
+
* Seek — steer toward a target
|
|
31
|
+
*/
|
|
32
|
+
static seek(agent: SteeringAgent, target: Vec2): Vec2 {
|
|
33
|
+
const desired = { x: target.x - agent.position.x, z: target.z - agent.position.z };
|
|
34
|
+
const mag = Math.sqrt(desired.x ** 2 + desired.z ** 2);
|
|
35
|
+
if (mag === 0) return { x: 0, z: 0 };
|
|
36
|
+
desired.x = (desired.x / mag) * agent.maxSpeed;
|
|
37
|
+
desired.z = (desired.z / mag) * agent.maxSpeed;
|
|
38
|
+
return {
|
|
39
|
+
x: desired.x - agent.velocity.x,
|
|
40
|
+
z: desired.z - agent.velocity.z,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Flee — steer away from a target
|
|
46
|
+
*/
|
|
47
|
+
static flee(agent: SteeringAgent, target: Vec2): Vec2 {
|
|
48
|
+
const force = this.seek(agent, target);
|
|
49
|
+
return { x: -force.x, z: -force.z };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Arrive — seek with deceleration near target
|
|
54
|
+
*/
|
|
55
|
+
static arrive(agent: SteeringAgent, target: Vec2, slowRadius: number = 5): Vec2 {
|
|
56
|
+
const toTarget = { x: target.x - agent.position.x, z: target.z - agent.position.z };
|
|
57
|
+
const dist = Math.sqrt(toTarget.x ** 2 + toTarget.z ** 2);
|
|
58
|
+
if (dist === 0) return { x: 0, z: 0 };
|
|
59
|
+
|
|
60
|
+
const speed = dist < slowRadius ? agent.maxSpeed * (dist / slowRadius) : agent.maxSpeed;
|
|
61
|
+
|
|
62
|
+
const desired = {
|
|
63
|
+
x: (toTarget.x / dist) * speed,
|
|
64
|
+
z: (toTarget.z / dist) * speed,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
x: desired.x - agent.velocity.x,
|
|
69
|
+
z: desired.z - agent.velocity.z,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Wander — random jitter-based steering
|
|
75
|
+
*/
|
|
76
|
+
static wander(
|
|
77
|
+
agent: SteeringAgent,
|
|
78
|
+
wanderRadius: number = 2,
|
|
79
|
+
wanderDistance: number = 4,
|
|
80
|
+
jitter: number = 0.5
|
|
81
|
+
): Vec2 {
|
|
82
|
+
const angle = Math.random() * Math.PI * 2;
|
|
83
|
+
const wanderTarget = {
|
|
84
|
+
x: agent.position.x + Math.cos(angle) * wanderRadius * jitter,
|
|
85
|
+
z: agent.position.z + Math.sin(angle) * wanderRadius * jitter,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const velMag = Math.sqrt(agent.velocity.x ** 2 + agent.velocity.z ** 2);
|
|
89
|
+
const forward =
|
|
90
|
+
velMag > 0
|
|
91
|
+
? {
|
|
92
|
+
x: (agent.velocity.x / velMag) * wanderDistance,
|
|
93
|
+
z: (agent.velocity.z / velMag) * wanderDistance,
|
|
94
|
+
}
|
|
95
|
+
: { x: wanderDistance, z: 0 };
|
|
96
|
+
|
|
97
|
+
const circleCenter = {
|
|
98
|
+
x: agent.position.x + forward.x,
|
|
99
|
+
z: agent.position.z + forward.z,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return this.seek(agent, {
|
|
103
|
+
x: circleCenter.x + wanderTarget.x - agent.position.x,
|
|
104
|
+
z: circleCenter.z + wanderTarget.z - agent.position.z,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Obstacle avoidance
|
|
110
|
+
*/
|
|
111
|
+
static avoid(
|
|
112
|
+
agent: SteeringAgent,
|
|
113
|
+
obstacles: { position: Vec2; radius: number }[],
|
|
114
|
+
lookAhead: number = 5
|
|
115
|
+
): Vec2 {
|
|
116
|
+
const force: Vec2 = { x: 0, z: 0 };
|
|
117
|
+
|
|
118
|
+
for (const obs of obstacles) {
|
|
119
|
+
const toObs = { x: obs.position.x - agent.position.x, z: obs.position.z - agent.position.z };
|
|
120
|
+
const dist = Math.sqrt(toObs.x ** 2 + toObs.z ** 2);
|
|
121
|
+
|
|
122
|
+
if (dist < lookAhead + obs.radius) {
|
|
123
|
+
const pushStrength = (lookAhead + obs.radius - dist) / (lookAhead + obs.radius);
|
|
124
|
+
force.x -= (toObs.x / dist) * pushStrength * agent.maxForce;
|
|
125
|
+
force.z -= (toObs.z / dist) * pushStrength * agent.maxForce;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return force;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Blend multiple steering outputs by weight
|
|
134
|
+
*/
|
|
135
|
+
static blend(outputs: SteeringOutput[], maxForce: number): Vec2 {
|
|
136
|
+
const result: Vec2 = { x: 0, z: 0 };
|
|
137
|
+
for (const out of outputs) {
|
|
138
|
+
result.x += out.force.x * out.weight;
|
|
139
|
+
result.z += out.force.z * out.weight;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const mag = Math.sqrt(result.x ** 2 + result.z ** 2);
|
|
143
|
+
if (mag > maxForce) {
|
|
144
|
+
result.x = (result.x / mag) * maxForce;
|
|
145
|
+
result.z = (result.z / mag) * maxForce;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SteeringBehaviors.ts
|
|
3
|
+
*
|
|
4
|
+
* Autonomous agent steering: seek, flee, arrive, wander,
|
|
5
|
+
* flock (separation/alignment/cohesion), and obstacle avoidance.
|
|
6
|
+
*
|
|
7
|
+
* @module ai
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// TYPES
|
|
12
|
+
// =============================================================================
|
|
13
|
+
|
|
14
|
+
export interface SteeringAgent {
|
|
15
|
+
position: { x: number; y: number; z: number };
|
|
16
|
+
velocity: { x: number; y: number; z: number };
|
|
17
|
+
maxSpeed: number;
|
|
18
|
+
maxForce: number;
|
|
19
|
+
mass: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface FlockConfig {
|
|
23
|
+
separationWeight: number;
|
|
24
|
+
alignmentWeight: number;
|
|
25
|
+
cohesionWeight: number;
|
|
26
|
+
neighborRadius: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ObstacleCircle {
|
|
30
|
+
center: { x: number; y: number; z: number };
|
|
31
|
+
radius: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type Vec3 = { x: number; y: number; z: number };
|
|
35
|
+
|
|
36
|
+
// =============================================================================
|
|
37
|
+
// STEERING BEHAVIORS
|
|
38
|
+
// =============================================================================
|
|
39
|
+
|
|
40
|
+
export class SteeringBehaviors {
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Basic Behaviors
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
static seek(agent: SteeringAgent, target: Vec3): Vec3 {
|
|
46
|
+
const desired = SteeringBehaviors.sub(target, agent.position);
|
|
47
|
+
const norm = SteeringBehaviors.normalize(desired);
|
|
48
|
+
const scaled = SteeringBehaviors.scale(norm, agent.maxSpeed);
|
|
49
|
+
return SteeringBehaviors.truncate(
|
|
50
|
+
SteeringBehaviors.sub(scaled, agent.velocity),
|
|
51
|
+
agent.maxForce
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static flee(agent: SteeringAgent, threat: Vec3): Vec3 {
|
|
56
|
+
const desired = SteeringBehaviors.sub(agent.position, threat);
|
|
57
|
+
const norm = SteeringBehaviors.normalize(desired);
|
|
58
|
+
const scaled = SteeringBehaviors.scale(norm, agent.maxSpeed);
|
|
59
|
+
return SteeringBehaviors.truncate(
|
|
60
|
+
SteeringBehaviors.sub(scaled, agent.velocity),
|
|
61
|
+
agent.maxForce
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static arrive(agent: SteeringAgent, target: Vec3, slowRadius: number): Vec3 {
|
|
66
|
+
const toTarget = SteeringBehaviors.sub(target, agent.position);
|
|
67
|
+
const dist = SteeringBehaviors.vecLength(toTarget);
|
|
68
|
+
if (dist < 0.001) return { x: 0, y: 0, z: 0 };
|
|
69
|
+
|
|
70
|
+
const speed = dist < slowRadius ? agent.maxSpeed * (dist / slowRadius) : agent.maxSpeed;
|
|
71
|
+
const desired = SteeringBehaviors.scale(SteeringBehaviors.normalize(toTarget), speed);
|
|
72
|
+
return SteeringBehaviors.truncate(
|
|
73
|
+
SteeringBehaviors.sub(desired, agent.velocity),
|
|
74
|
+
agent.maxForce
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static wander(
|
|
79
|
+
agent: SteeringAgent,
|
|
80
|
+
circleDistance: number,
|
|
81
|
+
circleRadius: number,
|
|
82
|
+
angleJitter: number,
|
|
83
|
+
currentAngle: number
|
|
84
|
+
): { force: Vec3; newAngle: number } {
|
|
85
|
+
const angle = currentAngle + (Math.random() - 0.5) * angleJitter;
|
|
86
|
+
const vel = SteeringBehaviors.normalize(agent.velocity);
|
|
87
|
+
const circleCenter = SteeringBehaviors.add(
|
|
88
|
+
agent.position,
|
|
89
|
+
SteeringBehaviors.scale(vel, circleDistance)
|
|
90
|
+
);
|
|
91
|
+
const offset = { x: Math.cos(angle) * circleRadius, y: 0, z: Math.sin(angle) * circleRadius };
|
|
92
|
+
const target = SteeringBehaviors.add(circleCenter, offset);
|
|
93
|
+
return { force: SteeringBehaviors.seek(agent, target), newAngle: angle };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
// Flock
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
static flock(agent: SteeringAgent, neighbors: SteeringAgent[], config: FlockConfig): Vec3 {
|
|
101
|
+
const nearby = neighbors.filter((n) => {
|
|
102
|
+
if (n === agent) return false;
|
|
103
|
+
return SteeringBehaviors.distance(agent.position, n.position) < config.neighborRadius;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
if (nearby.length === 0) return { x: 0, y: 0, z: 0 };
|
|
107
|
+
|
|
108
|
+
const sep = SteeringBehaviors.separation(agent, nearby);
|
|
109
|
+
const ali = SteeringBehaviors.alignment(agent, nearby);
|
|
110
|
+
const coh = SteeringBehaviors.cohesion(agent, nearby);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
x:
|
|
114
|
+
sep.x * config.separationWeight +
|
|
115
|
+
ali.x * config.alignmentWeight +
|
|
116
|
+
coh.x * config.cohesionWeight,
|
|
117
|
+
y:
|
|
118
|
+
sep.y * config.separationWeight +
|
|
119
|
+
ali.y * config.alignmentWeight +
|
|
120
|
+
coh.y * config.cohesionWeight,
|
|
121
|
+
z:
|
|
122
|
+
sep.z * config.separationWeight +
|
|
123
|
+
ali.z * config.alignmentWeight +
|
|
124
|
+
coh.z * config.cohesionWeight,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private static separation(agent: SteeringAgent, neighbors: SteeringAgent[]): Vec3 {
|
|
129
|
+
let fx = 0,
|
|
130
|
+
fy = 0,
|
|
131
|
+
fz = 0;
|
|
132
|
+
for (const n of neighbors) {
|
|
133
|
+
const d = SteeringBehaviors.sub(agent.position, n.position);
|
|
134
|
+
const dist = SteeringBehaviors.vecLength(d) || 0.001;
|
|
135
|
+
fx += d.x / (dist * dist);
|
|
136
|
+
fy += d.y / (dist * dist);
|
|
137
|
+
fz += d.z / (dist * dist);
|
|
138
|
+
}
|
|
139
|
+
return SteeringBehaviors.normalize({ x: fx, y: fy, z: fz });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private static alignment(_agent: SteeringAgent, neighbors: SteeringAgent[]): Vec3 {
|
|
143
|
+
let vx = 0,
|
|
144
|
+
vy = 0,
|
|
145
|
+
vz = 0;
|
|
146
|
+
for (const n of neighbors) {
|
|
147
|
+
vx += n.velocity.x;
|
|
148
|
+
vy += n.velocity.y;
|
|
149
|
+
vz += n.velocity.z;
|
|
150
|
+
}
|
|
151
|
+
const avg = { x: vx / neighbors.length, y: vy / neighbors.length, z: vz / neighbors.length };
|
|
152
|
+
return SteeringBehaviors.normalize(avg);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private static cohesion(agent: SteeringAgent, neighbors: SteeringAgent[]): Vec3 {
|
|
156
|
+
let cx = 0,
|
|
157
|
+
cy = 0,
|
|
158
|
+
cz = 0;
|
|
159
|
+
for (const n of neighbors) {
|
|
160
|
+
cx += n.position.x;
|
|
161
|
+
cy += n.position.y;
|
|
162
|
+
cz += n.position.z;
|
|
163
|
+
}
|
|
164
|
+
const center = { x: cx / neighbors.length, y: cy / neighbors.length, z: cz / neighbors.length };
|
|
165
|
+
return SteeringBehaviors.normalize(SteeringBehaviors.sub(center, agent.position));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ---------------------------------------------------------------------------
|
|
169
|
+
// Obstacle Avoidance
|
|
170
|
+
// ---------------------------------------------------------------------------
|
|
171
|
+
|
|
172
|
+
static obstacleAvoidance(
|
|
173
|
+
agent: SteeringAgent,
|
|
174
|
+
obstacles: ObstacleCircle[],
|
|
175
|
+
lookAhead: number
|
|
176
|
+
): Vec3 {
|
|
177
|
+
const ahead = SteeringBehaviors.add(
|
|
178
|
+
agent.position,
|
|
179
|
+
SteeringBehaviors.scale(SteeringBehaviors.normalize(agent.velocity), lookAhead)
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
let nearest: ObstacleCircle | null = null;
|
|
183
|
+
let nearestDist = Infinity;
|
|
184
|
+
|
|
185
|
+
for (const obs of obstacles) {
|
|
186
|
+
const dist = SteeringBehaviors.distance(ahead, obs.center);
|
|
187
|
+
if (dist < obs.radius && dist < nearestDist) {
|
|
188
|
+
nearest = obs;
|
|
189
|
+
nearestDist = dist;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (!nearest) return { x: 0, y: 0, z: 0 };
|
|
194
|
+
|
|
195
|
+
const avoidance = SteeringBehaviors.sub(ahead, nearest.center);
|
|
196
|
+
return SteeringBehaviors.truncate(SteeringBehaviors.normalize(avoidance), agent.maxForce);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// Integration
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
static applyForce(agent: SteeringAgent, force: Vec3, dt: number): void {
|
|
204
|
+
const ax = force.x / agent.mass,
|
|
205
|
+
ay = force.y / agent.mass,
|
|
206
|
+
az = force.z / agent.mass;
|
|
207
|
+
agent.velocity.x += ax * dt;
|
|
208
|
+
agent.velocity.y += ay * dt;
|
|
209
|
+
agent.velocity.z += az * dt;
|
|
210
|
+
agent.velocity = SteeringBehaviors.truncate(agent.velocity, agent.maxSpeed);
|
|
211
|
+
agent.position.x += agent.velocity.x * dt;
|
|
212
|
+
agent.position.y += agent.velocity.y * dt;
|
|
213
|
+
agent.position.z += agent.velocity.z * dt;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---------------------------------------------------------------------------
|
|
217
|
+
// Vec3 Helpers
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
|
|
220
|
+
private static sub(a: Vec3, b: Vec3): Vec3 {
|
|
221
|
+
return { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
|
|
222
|
+
}
|
|
223
|
+
private static add(a: Vec3, b: Vec3): Vec3 {
|
|
224
|
+
return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
|
|
225
|
+
}
|
|
226
|
+
private static scale(v: Vec3, s: number): Vec3 {
|
|
227
|
+
return { x: v.x * s, y: v.y * s, z: v.z * s };
|
|
228
|
+
}
|
|
229
|
+
private static vecLength(v: Vec3): number {
|
|
230
|
+
return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
|
|
231
|
+
}
|
|
232
|
+
private static distance(a: Vec3, b: Vec3): number {
|
|
233
|
+
return SteeringBehaviors.vecLength(SteeringBehaviors.sub(a, b));
|
|
234
|
+
}
|
|
235
|
+
private static normalize(v: Vec3): Vec3 {
|
|
236
|
+
const len = SteeringBehaviors.vecLength(v) || 1;
|
|
237
|
+
return { x: v.x / len, y: v.y / len, z: v.z / len };
|
|
238
|
+
}
|
|
239
|
+
private static truncate(v: Vec3, max: number): Vec3 {
|
|
240
|
+
const len = SteeringBehaviors.vecLength(v);
|
|
241
|
+
if (len <= max) return v;
|
|
242
|
+
return SteeringBehaviors.scale(SteeringBehaviors.normalize(v), max);
|
|
243
|
+
}
|
|
244
|
+
}
|