Olympe Engine
2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Source
Runtime
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
═══════════════════════════════════════════════════════════════════════════════
11
PHASE 4: RUNTIME CONDITION EVALUATION - QUICK REFERENCE
12
═══════════════════════════════════════════════════════════════════════════════
13
14
This file provides a quick reference for using the Phase 4 condition evaluation
15
API. For detailed documentation, see PHASE4_CONDITION_EVALUATION.md
16
17
═══════════════════════════════════════════════════════════════════════════════
18
1. 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
27
using namespace Olympe;
28
29
═══════════════════════════════════════════════════════════════════════════════
30
2. EVALUATE SINGLE CONDITION
31
═══════════════════════════════════════════════════════════════════════════════
32
33
// Create environment
34
RuntimeEnvironment env;
35
env.SetBlackboardVariable("mHealth", 45.0f);
36
37
// Create condition: mHealth <= 50
38
ConditionPreset preset("my_preset",
39
Operand::CreateVariable("mHealth"),
40
ComparisonOp::LessEqual,
41
Operand::CreateConst(50.0f));
42
43
// Evaluate
44
std::string error;
45
bool result = ConditionPresetEvaluator::Evaluate(preset, env, error);
46
// result = true, error = ""
47
48
═══════════════════════════════════════════════════════════════════════════════
49
3. EVALUATE CONDITION CHAIN (AND/OR)
50
═══════════════════════════════════════════════════════════════════════════════
51
52
// Setup
53
std::vector<NodeConditionRef> conditions;
54
conditions.push_back(NodeConditionRef("preset_1", LogicalOp::Start)); // First
55
conditions.push_back(NodeConditionRef("preset_2", LogicalOp::And)); // AND
56
conditions.push_back(NodeConditionRef("preset_3", LogicalOp::Or)); // OR
57
58
RuntimeEnvironment env;
59
env.SetBlackboardVariable("var1", 10.0f);
60
env.SetBlackboardVariable("var2", 20.0f);
61
env.SetBlackboardVariable("var3", 30.0f);
62
63
// Evaluate
64
std::string error;
65
bool 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
═══════════════════════════════════════════════════════════════════════════════
74
4. OPERAND MODES
75
═══════════════════════════════════════════════════════════════════════════════
76
77
// Variable mode - lookup from blackboard
78
Operand var = Operand::CreateVariable("mHealth");
79
env.SetBlackboardVariable("mHealth", 45.0f);
80
81
// Const mode - literal value
82
Operand const_val = Operand::CreateConst(50.0f);
83
84
// Pin mode - dynamic pin value
85
Operand pin = Operand::CreatePin("pin_uuid");
86
env.SetDynamicPinValue("pin_uuid", 0.75f);
87
88
═══════════════════════════════════════════════════════════════════════════════
89
5. COMPARISON OPERATORS
90
═══════════════════════════════════════════════════════════════════════════════
91
92
ComparisonOp::Equal; // ==
93
ComparisonOp::NotEqual; // !=
94
ComparisonOp::Less; // <
95
ComparisonOp::LessEqual; // <=
96
ComparisonOp::Greater; // >
97
ComparisonOp::GreaterEqual; // >=
98
99
═══════════════════════════════════════════════════════════════════════════════
100
6. LOGICAL OPERATORS
101
═══════════════════════════════════════════════════════════════════════════════
102
103
LogicalOp::Start; // First condition (no combinator)
104
LogicalOp::And; // AND with previous
105
LogicalOp::Or; // OR with previous
106
107
// Example: (A) AND (B) AND (C)
108
conditions.push_back(NodeConditionRef("a_id", LogicalOp::Start));
109
conditions.push_back(NodeConditionRef("b_id", LogicalOp::And));
110
conditions.push_back(NodeConditionRef("c_id", LogicalOp::And));
111
112
// Example: (X) OR (Y) OR (Z)
113
conditions.push_back(NodeConditionRef("x_id", LogicalOp::Start));
114
conditions.push_back(NodeConditionRef("y_id", LogicalOp::Or));
115
conditions.push_back(NodeConditionRef("z_id", LogicalOp::Or));
116
117
═══════════════════════════════════════════════════════════════════════════════
118
7. ERROR HANDLING
119
═══════════════════════════════════════════════════════════════════════════════
120
121
std::string error;
122
bool result = ConditionPresetEvaluator::Evaluate(preset, env, error);
123
124
if (!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
═══════════════════════════════════════════════════════════════════════════════
134
8. BRANCH NODE PATTERN
135
═══════════════════════════════════════════════════════════════════════════════
136
137
void 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
═══════════════════════════════════════════════════════════════════════════════
170
9. SHORT-CIRCUIT BEHAVIOR
171
═══════════════════════════════════════════════════════════════════════════════
172
173
// AND chains: stops at first FALSE
174
Condition A = false; // Evaluated
175
Condition B = true; // NOT evaluated (short-circuit)
176
Condition C = true; // NOT evaluated
177
Result: false (stops after A)
178
179
// OR chains: stops at first TRUE
180
Condition X = false; // Evaluated
181
Condition Y = true; // Evaluated (not first TRUE)
182
Condition Z = true; // NOT evaluated (stops after Y)
183
Result: true (stops after Y)
184
185
// Performance: 1-100% faster depending on condition structure
186
187
═══════════════════════════════════════════════════════════════════════════════
188
10. COMMON PATTERNS
189
═══════════════════════════════════════════════════════════════════════════════
190
191
// Pattern 1: Simple health check
192
if (EvaluateCondition(healthPreset, env, error)) {
193
TriggerLowHealthEvent();
194
}
195
196
// Pattern 2: Complex decision
197
bool shouldAttack = EvaluateConditionChain(
198
{lowHealthCond, highFatigueCond},
199
registry, env, error);
200
201
// Pattern 3: Multi-branch
202
if (EvaluateConditionChain(path1Conditions, ...)) {
203
ExecutePath1();
204
} else if (EvaluateConditionChain(path2Conditions, ...)) {
205
ExecutePath2();
206
} else {
207
ExecuteDefaultPath();
208
}
209
210
═══════════════════════════════════════════════════════════════════════════════
211
11. PERFORMANCE TIPS
212
═══════════════════════════════════════════════════════════════════════════════
213
214
1. Place fast conditions first in AND chains
215
- If first condition is usually false, no other evaluations needed
216
217
2. Place likely-true conditions first in OR chains
218
- If first condition is usually true, no other evaluations needed
219
220
3. Avoid complex conditions as first in chain
221
- Short-circuit optimization most effective with cheap first tests
222
223
4. Reuse RuntimeEnvironment when possible
224
- Avoid creating new environment for each frame
225
- Update values instead of creating new instance
226
227
═══════════════════════════════════════════════════════════════════════════════
228
12. DEBUGGING
229
═══════════════════════════════════════════════════════════════════════════════
230
231
// Debug: Print condition evaluation
232
std::string error;
233
bool result = ConditionPresetEvaluator::Evaluate(preset, env, error);
234
std::cout << "Condition result: " << (result ? "TRUE" : "FALSE") << std::endl;
235
if (!error.empty()) {
236
std::cout << "Error: " << error << std::endl;
237
}
238
239
// Debug: Check environment state
240
float val;
241
if (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
248
for (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
═══════════════════════════════════════════════════════════════════════════════
254
13. API SUMMARY
255
═══════════════════════════════════════════════════════════════════════════════
256
257
ConditionPresetEvaluator::Evaluate(
258
const ConditionPreset& preset,
259
RuntimeEnvironment& env,
260
std::string& outErrorMsg)
261
-> bool result
262
263
ConditionPresetEvaluator::EvaluateConditionChain(
264
const std::vector<NodeConditionRef>& conditions,
265
const ConditionPresetRegistry& registry,
266
RuntimeEnvironment& env,
267
std::string& outErrorMsg)
268
-> bool result
269
270
RuntimeEnvironment::SetBlackboardVariable(key, value)
271
RuntimeEnvironment::GetBlackboardVariable(key, outValue) -> bool
272
RuntimeEnvironment::SetDynamicPinValue(pinID, value)
273
RuntimeEnvironment::GetDynamicPinValue(pinID, outValue) -> bool
274
RuntimeEnvironment::Clear()
275
276
═══════════════════════════════════════════════════════════════════════════════
277
14. 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
═══════════════════════════════════════════════════════════════════════════════
296
15. 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
═══════════════════════════════════════════════════════════════════════════════
304
STATUS: ✅ PHASE 4 COMPLETE - PRODUCTION READY
305
BUILD: ✅ Génération réussie (0 errors)
306
═══════════════════════════════════════════════════════════════════════════════
307
*/
Generated on Mon Apr 13 2026 08:15:20 for Olympe Engine by
1.9.8