Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ECS_Components_AI.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 - 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6This file is part of Olympe Engine V2.
7
8AI Components purpose: Define all AI-related components for the ECS architecture.
9
10*/
11
12#pragma once
13
14#include "ECS_Entity.h"
15#include "vector.h"
16#include <cstdint>
17#include "ECS_Systems_AI.h"
18#include "ComponentRegistry.h"
19
20// --- AI Blackboard Component ---
21// Typed blackboard with explicit fields for performance (no std::map/std::string keys in hot path)
23{
24 // Mode tracking (exposed to BT for condition checks)
25 int AIMode = 1; // 1=Idle, 2=Patrol, 3=Combat, 4=Flee, 5=Investigate
26
27 // Target tracking
30 float timeSinceTargetSeen = 0.0f;
31 bool hasTarget = false;
32
33 // Perception state
34 float distanceToTarget = 0.0f;
35 bool targetVisible = false;
36 bool targetInRange = false;
37
38 // Movement goals
40 bool hasMoveGoal = false;
41
42 // Patrol state
45 Vector patrolPoints[8]; // Fixed size for performance
47 bool hasPatrolPath = false;
48
49 // Combat state
50 float lastAttackTime = 0.0f;
51 float attackCooldown = 1.0f;
52 bool canAttack = true;
53
54 // Stimulus tracking
55 float lastDamageTaken = 0.0f;
56 float damageAmount = 0.0f;
58 bool heardNoise = false;
59 float noiseCooldown = 0.0f;
60
61 // NEW: Wander behavior state
62 float wanderWaitTimer = 0.0f; // Current timer
63 float wanderTargetWaitTime = 0.0f; // Target wait time
64 Vector wanderDestination; // Chosen destination point
65 bool hasWanderDestination = false; // Destination chosen?
66 float wanderSearchRadius = 500.0f; // Default search radius
67 int wanderMaxSearchAttempts = 10; // Default max attempts
68};
70
71// --- AI Senses Component ---
72// Perception parameters for the AI entity
74{
75 float visionRadius = 300.0f; // How far the entity can see
76 float visionAngle = 180.0f; // Field of view in degrees (360 = omnidirectional)
77 float hearingRadius = 500.0f; // How far the entity can hear sounds
78
79 // Timeslicing parameters for performance
80 float perceptionHz = 5.0f; // Perception updates per second (default: 5 Hz = every 0.2s)
81 float thinkHz = 10.0f; // Decision/BT updates per second (default: 10 Hz = every 0.1s)
82
83 // Internal timers (updated by systems)
84 float nextPerceptionTime = 0.0f;
85 float nextThinkTime = 0.0f;
86
87 // Constructors
88 AISenses_data() = default;
91};
93
94// --- AI State Component (HFSM) ---
95// Hierarchical Finite State Machine mode/state
96/**
97 * @enum AIMode
98 * @brief AI behavior modes for NPCs
99 * @note Values start at 1 to match BT condition checks (AIMode == 1 for Idle, 2 for Patrol, 3 for Combat)
100 */
101enum class AIMode : uint8_t
102{
103 Idle = 1, // Default wander/idle behavior
104 Patrol = 2, // Following patrol route
105 Combat = 3, // Engaging enemies
106 Flee = 4, // Running away from threats
107 Investigate = 5, // Checking suspicious activity
108 Dead = 6 // Entity is dead
109};
110
112{
115 float timeInCurrentMode = 0.0f;
116
117 // State transition thresholds
118 float combatEngageDistance = 250.0f;
119 float fleeHealthThreshold = 0.2f; // Flee when health below 20%
120 float investigateTimeout = 5.0f; // Time to investigate before returning to patrol
121};
123
124// --- Behavior Tree Runtime Component ---
125// Per-entity behavior tree execution state
127{
128 // Tree identification (RENAMED: Added AI prefix for clarity)
129 uint32_t AITreeAssetId = 0; // ID of the behavior tree asset to execute
130 std::string AITreePath = ""; // Path to the tree asset
131
132 // Execution state (RENAMED: Added AI prefix for clarity)
133 uint32_t AICurrentNodeIndex = 0; // Index of the currently executing node
134 uint8_t lastStatus = 0; // Last node execution status (0=Running, 1=Success, 2=Failure)
135
136 // Timeslicing state
137 float nextThinkTime = 0.0f; // When to next tick the behavior tree
138
139 // Tree execution control
140 bool isActive = true; // Enable/disable tree execution
141 bool needsRestart = false; // Flag to restart tree from root
142
143 // Constructors
147 {
148 SYSTEM_LOG << "[BehaviorTreeRuntime_data] Default constructor called: AITreeAssetId="
149 << AITreeAssetId << std::endl;
150 }
151
153 : AITreeAssetId(treeId), isActive(active),
155 {
156 SYSTEM_LOG << "[BehaviorTreeRuntime_data] Parameterized constructor called: AITreeAssetId="
157 << treeId << ", active=" << active << std::endl;
158 }
159};
161
162// --- Move Intent Component ---
163// Movement intent that will be converted to Movement_data by AIMotionSystem
165{
166 Vector targetPosition; // Where to move
167 float desiredSpeed = 100.0f; // Speed multiplier (0.0 to 1.0, can exceed 1.0 for sprint)
168 bool hasIntent = false; // Whether this intent is active
169 float arrivalThreshold = 5.0f; // Distance at which we consider arrived
170
171 // Pathfinding flags (for future expansion)
172 bool usePathfinding = false;
173 bool avoidObstacles = false;
174};
176
177// --- Attack Intent Component ---
178// Attack intent for combat actions
180{
182 Vector targetPosition; // Attack position if no specific entity
183 float damage = 10.0f; // Damage to deal
184 float range = 50.0f; // Attack range
185 bool hasIntent = false; // Whether this intent is active
186 float cooldown = 1.0f; // Time between attacks
187
188 // Attack type (can be extended)
189 enum class AttackType : uint8_t
190 {
191 Melee = 0,
192 Ranged,
193 Area
194 };
196};
#define AUTO_REGISTER_COMPONENT(ComponentType)
Macro for auto-registering a component at program startup Place this immediately after the struct def...
AIMode
AI behavior modes for NPCs.
@ Investigate
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
const EntityID INVALID_ENTITY_ID
Definition ECS_Entity.h:23
AISenses_data(float vision, float hearing)
AISenses_data()=default
BehaviorTreeRuntime_data(uint32_t treeId, bool active)
#define SYSTEM_LOG