Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BTGraphCompiler.cpp
Go to the documentation of this file.
1/**
2 * @file BTGraphCompiler.cpp
3 * @brief Implementation of BTGraphCompiler
4 * @author Olympe Engine
5 * @date 2026-02-18
6 */
7
8#include "BTGraphCompiler.h"
9#include "BTGraphValidator.h"
10#include "BTNodeRegistry.h"
11#include "../../system/system_utils.h"
12
13namespace Olympe {
14namespace AI {
15
19 std::string& errorMsg)
20{
21 if (graph == nullptr) {
22 errorMsg = "Graph is null";
23 return false;
24 }
25
26 // Validate graph first
28 for (auto msgIt = validationMessages.begin(); msgIt != validationMessages.end(); ++msgIt) {
29 const auto& msg = *msgIt;
30 if (msg.severity == BTValidationSeverity::Error) {
31 errorMsg = msg.message;
32 return false;
33 }
34 }
35
36 // Clear asset
37 outAsset.nodes.clear();
38 outAsset.rootNodeId = graph->rootNodeId.value;
39
40 // Compile each node
41 for (auto nodeIt = graph->GetNodes().begin(); nodeIt != graph->GetNodes().end(); ++nodeIt) {
42 const auto& graphNode = *nodeIt;
43
46 errorMsg = "Failed to compile node " + std::to_string(graphNode.id.value);
47 return false;
48 }
49
50 outAsset.nodes.push_back(btNode);
51 }
52
53 SYSTEM_LOG << "[BTGraphCompiler] Compiled " << outAsset.nodes.size() << " nodes" << std::endl;
54 return true;
55}
56
60{
61 outNode.id = graphNode.id.value;
62 outNode.name = graphNode.name;
63
64 // Map type
65 outNode.type = MapNodeType(graphNode.type);
66
67 // Copy children
68 outNode.childIds.clear();
69 for (auto childIt = graphNode.children.begin(); childIt != graphNode.children.end(); ++childIt) {
70 outNode.childIds.push_back(childIt->value);
71 }
72
73 // Copy decorator child
74 outNode.decoratorChildId = graphNode.decoratorChild.value;
75
76 // Copy parameters to flexible parameter maps
77 for (auto paramIt = graphNode.parameters.begin(); paramIt != graphNode.parameters.end(); ++paramIt) {
78 const std::string& key = paramIt->first;
79 const std::string& value = paramIt->second;
80
81 // Store as string by default
82 outNode.stringParams[key] = value;
83
84 // Try to parse as int or float
85 try {
86 size_t pos = 0;
87 int intVal = std::stoi(value, &pos);
88 if (pos == value.length()) {
89 outNode.intParams[key] = intVal;
90 }
91 } catch (...) {
92 // Not an int
93 }
94
95 try {
96 size_t pos = 0;
97 float floatVal = std::stof(value, &pos);
98 if (pos == value.length()) {
99 outNode.floatParams[key] = floatVal;
100 }
101 } catch (...) {
102 // Not a float
103 }
104 }
105
106 // Set legacy fields for backward compatibility
107 if (outNode.type == BTNodeType::Action) {
108 // Try to map action type
109 if (graphNode.type == "BT_Wait") {
110 outNode.actionType = BTActionType::Idle;
111 outNode.actionParam1 = outNode.GetParameterFloat("duration", 1.0f);
112 } else if (graphNode.type == "BT_MoveToTarget") {
114 outNode.actionParam1 = outNode.GetParameterFloat("speed", 100.0f);
115 } else if (graphNode.type == "BT_AttackTarget") {
117 }
118 } else if (outNode.type == BTNodeType::Condition) {
119 // Try to map condition type
120 if (graphNode.type == "BT_HasTarget") {
122 } else if (graphNode.type == "BT_IsTargetInRange") {
124 outNode.conditionParam = outNode.GetParameterFloat("distance", 100.0f);
125 }
126 } else if (outNode.type == BTNodeType::Repeater) {
127 outNode.repeatCount = outNode.GetParameterInt("repeatCount", 1);
128 }
129
130 return true;
131}
132
134 // Composites
135 if (typeString == "BT_Selector") {
137 }
138 if (typeString == "BT_Sequence") {
140 }
141 if (typeString == "BT_Parallel") {
142 return BTNodeType::Selector; // Map to Selector for now (no Parallel in BTNodeType)
143 }
144
145 // Decorators
146 if (typeString == "BT_Inverter") {
148 }
149 if (typeString == "BT_Repeater" ||
150 typeString == "BT_UntilSuccess" ||
151 typeString == "BT_UntilFailure" ||
152 typeString == "BT_Cooldown") {
154 }
155
156 // Conditions
157 if (typeString == "BT_CheckBlackboardValue" ||
158 typeString == "BT_HasTarget" ||
159 typeString == "BT_IsTargetInRange" ||
160 typeString == "BT_CanSeeTarget") {
162 }
163
164 // Actions (everything else defaults to Action)
165 return BTNodeType::Action;
166}
167
168} // namespace AI
169} // namespace Olympe
Compiler from GraphDocument to BehaviorTreeAsset.
Validation system for Behavior Tree graph structure.
Registry of all Behavior Tree node types for AIGraphPlugin_BT.
@ SetMoveGoalToTarget
Move towards current target.
@ AttackIfClose
Attack if in range.
@ Idle
Do nothing.
BTNodeType
Behavior tree node types.
@ Action
Leaf node - performs an action.
@ Selector
OR node - succeeds if any child succeeds.
@ Sequence
AND node - succeeds if all children succeed.
@ Inverter
Decorator - inverts child result.
@ Condition
Leaf node - checks a condition.
@ Repeater
Decorator - repeats child N times.
@ TargetVisible
Can see target entity.
@ TargetInRange
Target within specified range.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static bool Compile(const NodeGraph::GraphDocument *graph, BehaviorTreeAsset &outAsset, std::string &errorMsg)
Compile GraphDocument to BehaviorTreeAsset.
static BTNodeType MapNodeType(const std::string &typeString)
Map graph node type string to BTNodeType enum.
static bool CompileNode(const NodeGraph::NodeData &graphNode, BTNode &outNode)
Compile a single node from graph to asset.
static std::vector< BTValidationMessage > ValidateGraph(const NodeGraph::GraphDocument *graph)
Validate a complete BT graph.
Main document class for a node graph.
@ Error
Error (blocking compilation)
< Provides AssetID and INVALID_ASSET_ID
Represents a single node in a behavior tree.
#define SYSTEM_LOG