Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BehaviorTreeGraphAdapter.h
Go to the documentation of this file.
1/**
2 * @file BehaviorTreeGraphAdapter.h
3 * @brief Adapter layer for converting BehaviorTree graphs to generic TaskGraphTemplate format
4 * @author Olympe Engine
5 * @date 2026-03-24
6 *
7 * @details
8 * Implements the Adapter pattern to convert hierarchical BehaviorTree structures into
9 * a flat graph representation compatible with the generic GraphExecutionSimulator framework.
10 * This eliminates code duplication and enables both VisualScript and BehaviorTree to share
11 * the same simulation infrastructure.
12 *
13 * Phase: BT-SIM-001 (BehaviorTree Graph Simulation - Framework Generic)
14 * C++14 compliant - no C++17/20 features.
15 */
16
17#pragma once
18
19#include <string>
20#include <vector>
21#include <map>
22#include <memory>
23#include <cstdint>
24#include "../AI/BehaviorTree.h"
25#include "../TaskSystem/TaskGraphTemplate.h"
26#include "../BlueprintEditor/GraphExecutionTracer.h"
27
28namespace Olympe {
29
30/**
31 * @class BehaviorTreeGraphAdapter
32 * @brief Converts BehaviorTree structures to generic TaskGraphTemplate format for simulation.
33 *
34 * @details
35 * This adapter layer enables the reuse of the generic GraphExecutionSimulator framework
36 * for BehaviorTree graphs without code duplication. It performs bidirectional conversion:
37 *
38 * Forward (BehaviorTree → Graph):
39 * - Converts hierarchical BTNode array into flat TaskGraphTemplate
40 * - Maps BTNode.childIds relationships to explicit ExecPinConnection entries
41 * - Preserves BehaviorTree-specific metadata where applicable
42 * - Validates tree structure before conversion
43 *
44 * Reverse (Trace → BehaviorTree Format):
45 * - Post-processes generic ExecutionEvent trace from GraphExecutionSimulator
46 * - Adds BehaviorTree-specific formatting (status symbols, keywords, indentation)
47 * - Enhances trace with hierarchical context from original BTNode structure
48 *
49 * Example Usage:
50 * @code
51 * BehaviorTreeAsset myTree = ...;
52 *
53 * // Convert to graph format
54 * auto taskGraph = BehaviorTreeGraphAdapter::AdaptToTaskGraph(myTree);
55 *
56 * // Simulate using generic framework
57 * GraphExecutionSimulator simulator;
58 * GraphExecutionTracer tracer;
59 * SimulationOptions options;
60 * simulator.SimulateExecution(*taskGraph, options, tracer);
61 *
62 * // Format trace back to BehaviorTree context
63 * std::string btTrace = BehaviorTreeGraphAdapter::FormatTraceForBehaviorTree(
64 * tracer, myTree);
65 *
66 * // Display via existing panel
67 * executionPanel->DisplayResults(tracer, btTrace);
68 * @endcode
69 */
71public:
72
73 /**
74 * @brief Converts a BehaviorTreeAsset to TaskGraphTemplate format.
75 *
76 * @param btAsset Source BehaviorTree asset containing hierarchical BTNode structure.
77 * @return Unique pointer to TaskGraphTemplate suitable for GraphExecutionSimulator.
78 * Returns nullptr if validation fails.
79 *
80 * @details
81 * Performs the following transformations:
82 * 1. Validates tree structure (no orphaned nodes, valid cycles, etc.)
83 * 2. Iterates BTNode[] and creates TaskNodeDefinition for each
84 * 3. Maps BTNode.type enum to appropriate TaskNodeType:
85 * - Selector → Selector (BT type 0)
86 * - Sequence → Sequence (BT type 1)
87 * - Condition → AtomicTask with condition ID
88 * - Action → AtomicTask with action ID
89 * - Inverter/Repeater → Decorator
90 * 4. Converts hierarchical childIds relationships to explicit ExecPinConnection entries:
91 * - Each child becomes a connection with SourceNodeID=parent, TargetNodeID=child
92 * - Pin names: "Control" (output) → "In" (input)
93 * 5. Returns fully populated TaskGraphTemplate ready for simulation
94 *
95 * @note
96 * - The conversion is one-way: BT → Graph (graph is flattened, hierarchy inferred at runtime)
97 * - All BTNode properties are preserved; nothing is lost
98 * - BT-specific details (condition type, action type) stored in AtomicTaskID field
99 */
100 static std::unique_ptr<TaskGraphTemplate> AdaptToTaskGraph(
102
103 /**
104 * @brief Formats generic execution trace back to BehaviorTree-specific context.
105 *
106 * @param tracer GraphExecutionTracer containing generic ExecutionEvent trace.
107 * @param btAsset Original BehaviorTreeAsset for context and hierarchy information.
108 * @return Human-readable string with BehaviorTree-specific formatting.
109 *
110 * @details
111 * Post-processes the generic execution trace from GraphExecutionSimulator with
112 * BehaviorTree-specific enhancements:
113 *
114 * 1. Status Symbol Mapping (from event message keywords):
115 * - SUCCESS → "✓ " (check mark)
116 * - FAILURE → "✗ " (cross mark)
117 * - RUNNING → "⊙ " (circle - in-progress)
118 * - Other → (blank) - informational message
119 *
120 * 2. Type Name Replacement:
121 * - Maps node types back to BT-specific names
122 *
123 * 3. Hierarchical Indentation:
124 * - Calculates node depth in original BT hierarchy
125 * - Indents output with (depth * 2) spaces for tree-like visualization
126 *
127 * @note
128 * - Called AFTER simulation completes (post-processing layer)
129 * - Does not modify tracer or btAsset; both remain const
130 * - Output is human-readable string (for logging, UI display, etc.)
131 */
132 static std::string FormatTraceForBehaviorTree(
135
136 /**
137 * @brief Validates BehaviorTree structure before conversion.
138 *
139 * @param btAsset BehaviorTreeAsset to validate.
140 * @return true if valid for conversion; false otherwise.
141 *
142 * @details
143 * Performs structural validation to catch issues before conversion:
144 * - Node ID uniqueness
145 * - Child references validity
146 * - Cycle detection
147 * - Node type consistency
148 */
149 static bool ValidateTreeStructure(
151
152private:
153
154 /**
155 * @brief Helper: Recursively adds a node and its children to the graph.
156 */
157 static void AddNodeToGraph(
158 const BTNode& btNode,
161 std::map<uint32_t, int32_t>& btToGraphIdMap);
162
163 /**
164 * @brief Helper: Calculates depth of a node in BT hierarchy.
165 */
167 uint32_t nodeId,
169
170 /**
171 * @brief Helper: Finds parent node ID for a given child node.
172 */
176
177 /**
178 * @brief Helper: Gets BehaviorTree node type name.
179 */
180 static std::string GetBTNodeTypeName(uint8_t type);
181
182};
183
184} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Converts BehaviorTree structures to generic TaskGraphTemplate format for simulation.
static void AddNodeToGraph(const BTNode &btNode, const BehaviorTreeAsset &btAsset, TaskGraphTemplate &outGraph, std::map< uint32_t, int32_t > &btToGraphIdMap)
Helper: Recursively adds a node and its children to the graph.
static uint32_t FindParentNodeId(uint32_t childNodeId, const BehaviorTreeAsset &btAsset)
Helper: Finds parent node ID for a given child node.
static bool ValidateTreeStructure(const BehaviorTreeAsset &btAsset)
Validates BehaviorTree structure before conversion.
static int32_t CalculateNodeDepth(uint32_t nodeId, const BehaviorTreeAsset &btAsset)
Helper: Calculates depth of a node in BT hierarchy.
static std::string GetBTNodeTypeName(uint8_t type)
Helper: Gets BehaviorTree node type name.
static std::unique_ptr< TaskGraphTemplate > AdaptToTaskGraph(const BehaviorTreeAsset &btAsset)
Converts a BehaviorTreeAsset to TaskGraphTemplate format.
static std::string FormatTraceForBehaviorTree(const GraphExecutionTracer &tracer, const BehaviorTreeAsset &btAsset)
Formats generic execution trace back to BehaviorTree-specific context.
Records execution trace during graph simulation.
Immutable, shareable task graph asset.
< Provides AssetID and INVALID_ASSET_ID
Represents a single node in a behavior tree.