Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
PHASE4_QUICK_REFERENCE.h
Go to the documentation of this file.
1/**
2 * @file PHASE4_QUICK_REFERENCE.h
3 * @brief Quick reference guide for Phase 4 API
4 * @author Olympe Engine
5 */
6
7#pragma once
8
9/*
10═══════════════════════════════════════════════════════════════════════════════
11PHASE 4: RUNTIME CONDITION EVALUATION - QUICK REFERENCE
12═══════════════════════════════════════════════════════════════════════════════
13
14This file provides a quick reference for using the Phase 4 condition evaluation
15API. For detailed documentation, see PHASE4_CONDITION_EVALUATION.md
16
17═══════════════════════════════════════════════════════════════════════════════
181. BASIC IMPORTS
19═══════════════════════════════════════════════════════════════════════════════
20
21#include "Runtime/ConditionPresetEvaluator.h"
22#include "Runtime/RuntimeEnvironment.h"
23#include "Editor/ConditionPreset/ConditionPreset.h"
24#include "Editor/ConditionPreset/NodeConditionRef.h"
25#include "Editor/ConditionPreset/Operand.h"
26
27using namespace Olympe;
28
29═══════════════════════════════════════════════════════════════════════════════
302. EVALUATE SINGLE CONDITION
31═══════════════════════════════════════════════════════════════════════════════
32
33// Create environment
34RuntimeEnvironment env;
35env.SetBlackboardVariable("mHealth", 45.0f);
36
37// Create condition: mHealth <= 50
38ConditionPreset preset("my_preset",
39 Operand::CreateVariable("mHealth"),
40 ComparisonOp::LessEqual,
41 Operand::CreateConst(50.0f));
42
43// Evaluate
44std::string error;
45bool result = ConditionPresetEvaluator::Evaluate(preset, env, error);
46// result = true, error = ""
47
48═══════════════════════════════════════════════════════════════════════════════
493. EVALUATE CONDITION CHAIN (AND/OR)
50═══════════════════════════════════════════════════════════════════════════════
51
52// Setup
53std::vector<NodeConditionRef> conditions;
54conditions.push_back(NodeConditionRef("preset_1", LogicalOp::Start)); // First
55conditions.push_back(NodeConditionRef("preset_2", LogicalOp::And)); // AND
56conditions.push_back(NodeConditionRef("preset_3", LogicalOp::Or)); // OR
57
58RuntimeEnvironment env;
59env.SetBlackboardVariable("var1", 10.0f);
60env.SetBlackboardVariable("var2", 20.0f);
61env.SetBlackboardVariable("var3", 30.0f);
62
63// Evaluate
64std::string error;
65bool result = ConditionPresetEvaluator::EvaluateConditionChain(
66 conditions, // Vector of NodeConditionRef
67 registry, // ConditionPresetRegistry
68 env,
69 error);
70
71// Evaluation: (preset_1) AND (preset_2) OR (preset_3)
72
73═══════════════════════════════════════════════════════════════════════════════
744. OPERAND MODES
75═══════════════════════════════════════════════════════════════════════════════
76
77// Variable mode - lookup from blackboard
78Operand var = Operand::CreateVariable("mHealth");
79env.SetBlackboardVariable("mHealth", 45.0f);
80
81// Const mode - literal value
82Operand const_val = Operand::CreateConst(50.0f);
83
84// Pin mode - dynamic pin value
85Operand pin = Operand::CreatePin("pin_uuid");
86env.SetDynamicPinValue("pin_uuid", 0.75f);
87
88═══════════════════════════════════════════════════════════════════════════════
895. COMPARISON OPERATORS
90═══════════════════════════════════════════════════════════════════════════════
91
92ComparisonOp::Equal; // ==
93ComparisonOp::NotEqual; // !=
94ComparisonOp::Less; // <
95ComparisonOp::LessEqual; // <=
96ComparisonOp::Greater; // >
97ComparisonOp::GreaterEqual; // >=
98
99═══════════════════════════════════════════════════════════════════════════════
1006. LOGICAL OPERATORS
101═══════════════════════════════════════════════════════════════════════════════
102
103LogicalOp::Start; // First condition (no combinator)
104LogicalOp::And; // AND with previous
105LogicalOp::Or; // OR with previous
106
107// Example: (A) AND (B) AND (C)
108conditions.push_back(NodeConditionRef("a_id", LogicalOp::Start));
109conditions.push_back(NodeConditionRef("b_id", LogicalOp::And));
110conditions.push_back(NodeConditionRef("c_id", LogicalOp::And));
111
112// Example: (X) OR (Y) OR (Z)
113conditions.push_back(NodeConditionRef("x_id", LogicalOp::Start));
114conditions.push_back(NodeConditionRef("y_id", LogicalOp::Or));
115conditions.push_back(NodeConditionRef("z_id", LogicalOp::Or));
116
117═══════════════════════════════════════════════════════════════════════════════
1187. ERROR HANDLING
119═══════════════════════════════════════════════════════════════════════════════
120
121std::string error;
122bool result = ConditionPresetEvaluator::Evaluate(preset, env, error);
123
124if (!error.empty()) {
125 std::cerr << "Condition evaluation failed: " << error << std::endl;
126 // Example errors:
127 // "Blackboard variable not found: 'mHealth'"
128 // "Preset not found in registry: 'preset_999'"
129 // "Dynamic pin value not found for pin: 'pin_xyz'"
130 return false;
131}
132
133═══════════════════════════════════════════════════════════════════════════════
1348. BRANCH NODE PATTERN
135═══════════════════════════════════════════════════════════════════════════════
136
137void ExecuteBranchNode(const BranchNodeData& node)
138{
139 // 1. Create and populate environment
140 RuntimeEnvironment env;
141 env.SetBlackboardVariable("mHealth", blackboard.GetHealth());
142 env.SetBlackboardVariable("mFatigue", blackboard.GetFatigue());
143
144 // 2. Set dynamic pin values if applicable
145 for (const auto& pin : node.dynamicPins) {
146 env.SetDynamicPinValue(pin.id, pin.value);
147 }
148
149 // 3. Evaluate conditions
150 std::string error;
151 bool conditionMet = ConditionPresetEvaluator::EvaluateConditionChain(
152 node.conditions, registry, env, error);
153
154 // 4. Check for errors
155 if (!error.empty()) {
156 LOG_ERROR("Condition failed: " << error);
157 ExecuteElseBranch(node); // Safe fallback
158 return;
159 }
160
161 // 5. Route to branch
162 if (conditionMet) {
163 ExecuteThenBranch(node);
164 } else {
165 ExecuteElseBranch(node);
166 }
167}
168
169═══════════════════════════════════════════════════════════════════════════════
1709. SHORT-CIRCUIT BEHAVIOR
171═══════════════════════════════════════════════════════════════════════════════
172
173// AND chains: stops at first FALSE
174Condition A = false; // Evaluated
175Condition B = true; // NOT evaluated (short-circuit)
176Condition C = true; // NOT evaluated
177Result: false (stops after A)
178
179// OR chains: stops at first TRUE
180Condition X = false; // Evaluated
181Condition Y = true; // Evaluated (not first TRUE)
182Condition Z = true; // NOT evaluated (stops after Y)
183Result: true (stops after Y)
184
185// Performance: 1-100% faster depending on condition structure
186
187═══════════════════════════════════════════════════════════════════════════════
18810. COMMON PATTERNS
189═══════════════════════════════════════════════════════════════════════════════
190
191// Pattern 1: Simple health check
192if (EvaluateCondition(healthPreset, env, error)) {
193 TriggerLowHealthEvent();
194}
195
196// Pattern 2: Complex decision
197bool shouldAttack = EvaluateConditionChain(
198 {lowHealthCond, highFatigueCond},
199 registry, env, error);
200
201// Pattern 3: Multi-branch
202if (EvaluateConditionChain(path1Conditions, ...)) {
203 ExecutePath1();
204} else if (EvaluateConditionChain(path2Conditions, ...)) {
205 ExecutePath2();
206} else {
207 ExecuteDefaultPath();
208}
209
210═══════════════════════════════════════════════════════════════════════════════
21111. PERFORMANCE TIPS
212═══════════════════════════════════════════════════════════════════════════════
213
2141. Place fast conditions first in AND chains
215 - If first condition is usually false, no other evaluations needed
216
2172. Place likely-true conditions first in OR chains
218 - If first condition is usually true, no other evaluations needed
219
2203. Avoid complex conditions as first in chain
221 - Short-circuit optimization most effective with cheap first tests
222
2234. Reuse RuntimeEnvironment when possible
224 - Avoid creating new environment for each frame
225 - Update values instead of creating new instance
226
227═══════════════════════════════════════════════════════════════════════════════
22812. DEBUGGING
229═══════════════════════════════════════════════════════════════════════════════
230
231// Debug: Print condition evaluation
232std::string error;
233bool result = ConditionPresetEvaluator::Evaluate(preset, env, error);
234std::cout << "Condition result: " << (result ? "TRUE" : "FALSE") << std::endl;
235if (!error.empty()) {
236 std::cout << "Error: " << error << std::endl;
237}
238
239// Debug: Check environment state
240float val;
241if (env.GetBlackboardVariable("mHealth", val)) {
242 std::cout << "mHealth = " << val << std::endl;
243} else {
244 std::cout << "mHealth not in environment" << std::endl;
245}
246
247// Debug: Chain evaluation step-by-step
248for (size_t i = 0; i < conditions.size(); ++i) {
249 const auto& cond = conditions[i];
250 std::cout << "Condition " << i << ": " << cond.presetID << std::endl;
251}
252
253═══════════════════════════════════════════════════════════════════════════════
25413. API SUMMARY
255═══════════════════════════════════════════════════════════════════════════════
256
257ConditionPresetEvaluator::Evaluate(
258 const ConditionPreset& preset,
259 RuntimeEnvironment& env,
260 std::string& outErrorMsg)
261 -> bool result
262
263ConditionPresetEvaluator::EvaluateConditionChain(
264 const std::vector<NodeConditionRef>& conditions,
265 const ConditionPresetRegistry& registry,
266 RuntimeEnvironment& env,
267 std::string& outErrorMsg)
268 -> bool result
269
270RuntimeEnvironment::SetBlackboardVariable(key, value)
271RuntimeEnvironment::GetBlackboardVariable(key, outValue) -> bool
272RuntimeEnvironment::SetDynamicPinValue(pinID, value)
273RuntimeEnvironment::GetDynamicPinValue(pinID, outValue) -> bool
274RuntimeEnvironment::Clear()
275
276═══════════════════════════════════════════════════════════════════════════════
27714. BEST PRACTICES
278═══════════════════════════════════════════════════════════════════════════════
279
280✓ DO:
281 - Check error messages on evaluation failure
282 - Populate RuntimeEnvironment completely before evaluation
283 - Structure AND chains with likely-false conditions first
284 - Structure OR chains with likely-true conditions first
285 - Reuse RuntimeEnvironment across multiple evaluations
286 - Comment complex condition chains
287
288✗ DON'T:
289 - Ignore error messages
290 - Create new RuntimeEnvironment for each frame
291 - Put expensive computations in operands
292 - Leave uninitialized blackboard variables
293 - Mix too many conditions without grouping (if possible)
294
295═══════════════════════════════════════════════════════════════════════════════
29615. FURTHER READING
297═══════════════════════════════════════════════════════════════════════════════
298
299- PHASE4_CONDITION_EVALUATION.md - Full documentation
300- PHASE4_COMPLETION_REPORT.md - Implementation details
301- Phase4_ConditionEvaluation_Examples.cpp - 7 working examples
302
303═══════════════════════════════════════════════════════════════════════════════
304STATUS: ✅ PHASE 4 COMPLETE - PRODUCTION READY
305BUILD: ✅ Génération réussie (0 errors)
306═══════════════════════════════════════════════════════════════════════════════
307*/