Skip to main content

euv_core/game/algorithms/ai/
struct.rs

1use super::*;
2
3/// Typed key-value blackboard shared across `BehaviorTree` condition and action nodes.
4#[derive(Clone, Data, Debug, Default, PartialEq)]
5pub struct Blackboard {
6    /// Stored typed entries indexed by string key.
7    pub(crate) entries: HashMap<String, BtValue>,
8}
9
10/// Per-node runtime memory stored cleanly outside tree topology for zero-allocation ticks.
11#[derive(Clone, Copy, Debug, Default, PartialEq)]
12pub(crate) struct BtNodeState {
13    /// Active child offset resumed on the next tick for memory-aware composites.
14    pub(crate) running_child: usize,
15    /// Completed repetitions counter for `Repeater` decorators.
16    pub(crate) repeat_count: usize,
17    /// Remaining cooldown timer in seconds for `Cooldown` decorators.
18    pub(crate) cooldown_remaining: f32,
19}
20
21/// Data-driven arena-indexed Behavior Tree supporting composites, decorators, conditions, and actions.
22#[derive(Clone, Data, Debug, Default)]
23pub struct BehaviorTree {
24    /// Arena of behavior tree nodes.
25    pub(crate) nodes: Vec<BtNodeKind>,
26    /// Root node index.
27    #[get(type(copy))]
28    pub(crate) root: usize,
29    /// Per-node execution state aligned 1:1 with `nodes`.
30    #[get(pub(crate))]
31    #[get_mut(pub(crate))]
32    #[set(pub(crate))]
33    pub(crate) states: Vec<BtNodeState>,
34}
35
36/// State definition inside a `HierarchicalFsm` with optional parent and default child.
37#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
38pub struct HfsmStateDef {
39    /// Unique state identifier.
40    #[get(type(copy))]
41    pub(crate) id: u32,
42    /// Optional parent state identifier (`None` for top-level root states).
43    #[get(skip)]
44    pub(crate) parent: Option<u32>,
45    /// Optional default initial sub-state entered automatically when entering this composite state.
46    #[get(skip)]
47    pub(crate) initial_child: Option<u32>,
48}
49
50/// Guarded transition rule between states in a `HierarchicalFsm`.
51#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
52pub struct HfsmTransition {
53    /// Source state identifier (also matches when active as an ancestor via parent fallback).
54    #[get(type(copy))]
55    pub(crate) from_state: u32,
56    /// Destination state identifier.
57    #[get(type(copy))]
58    pub(crate) to_state: u32,
59    /// Optional event trigger (`None` evaluates automatically on every `update`).
60    #[get(skip)]
61    pub(crate) event_id: Option<u32>,
62    /// Optional guard identifier evaluated via callback before taking the transition.
63    #[get(skip)]
64    pub(crate) guard_id: Option<u32>,
65    /// Priority tie-breaker (higher priority evaluated first).
66    #[get(type(copy))]
67    pub(crate) priority: i32,
68}
69
70/// Hierarchical Finite State Machine with LCA exit/enter ordering, guarded transitions, and parent fallback.
71#[derive(Clone, Data, Debug, Default)]
72pub struct HierarchicalFsm {
73    /// Registered state definitions.
74    pub(crate) states: Vec<HfsmStateDef>,
75    /// Registered guarded transitions.
76    pub(crate) transitions: Vec<HfsmTransition>,
77    /// Active nested state chain ordered from top-level ancestor to leaf state.
78    pub(crate) active_chain: Vec<u32>,
79    /// Reusable target ancestor chain scratch buffer avoiding per-transition allocations.
80    #[get(pub(crate))]
81    #[get_mut(pub(crate))]
82    #[set(pub(crate))]
83    pub(crate) target_chain_scratch: Vec<u32>,
84}
85
86/// Single normalized input axis and response curve contributing to a `UtilityActionDef` score.
87#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
88pub struct UtilityConsideration {
89    /// Caller-defined input signal identifier.
90    #[get(type(copy))]
91    pub(crate) input_id: u32,
92    /// Input value mapping to normalized `0.0`.
93    #[get(type(copy))]
94    pub(crate) min_input: f32,
95    /// Input value mapping to normalized `1.0`.
96    #[get(type(copy))]
97    pub(crate) max_input: f32,
98    /// Response curve shaping normalized input into `[0.0, 1.0]`.
99    #[get(type(copy))]
100    pub(crate) curve: UtilityCurveKind,
101    /// Positive exponent weight scaling this consideration's influence.
102    #[get(type(copy))]
103    pub(crate) weight: f32,
104}
105
106/// Candidate action evaluated by `UtilityAiBrain`.
107#[derive(Clone, Data, Debug, New, PartialEq)]
108pub struct UtilityActionDef {
109    /// Unique action identifier.
110    #[get(type(copy))]
111    pub(crate) action_id: u32,
112    /// Base priority multiplier for this action.
113    #[get(type(copy))]
114    pub(crate) base_weight: f32,
115    /// Considerations combined with compensation factor to compute utility.
116    pub(crate) considerations: Vec<UtilityConsideration>,
117}
118
119/// Multi-consideration Utility AI selector with response curves and hysteresis anti-oscillation.
120#[derive(Clone, Data, Debug)]
121pub struct UtilityAiBrain {
122    /// Candidate actions evaluated each tick.
123    pub(crate) actions: Vec<UtilityActionDef>,
124    /// Additive inertia bonus applied to the currently active action to suppress score chatter.
125    #[get(type(copy))]
126    pub(crate) inertia_bonus: f32,
127    /// Minimum dwell duration in seconds before switching away from a viable active action.
128    #[get(type(copy))]
129    pub(crate) min_dwell_time: f32,
130    /// Currently selected action identifier when `has_active` is `true`.
131    #[get(type(copy))]
132    pub(crate) current_action: u32,
133    /// Whether an active action has been selected.
134    #[get(type(copy))]
135    pub(crate) has_active: bool,
136    /// Elapsed dwell time in the currently active action.
137    #[get(type(copy))]
138    pub(crate) dwell_elapsed: f32,
139    /// Reusable buffer recording the latest `(action_id, utility_score)` results.
140    pub(crate) last_scores: Vec<(u32, f32)>,
141}
142
143/// Individual 3D Reynolds steering and flocking agent.
144#[derive(Clone, Copy, Data, Debug, PartialEq)]
145pub struct BoidsAgent3D {
146    /// World-space 3D position.
147    #[get(type(copy))]
148    pub(crate) position: Vec3,
149    /// World-space 3D velocity vector.
150    #[get(type(copy))]
151    pub(crate) velocity: Vec3,
152    /// Persistent wander phase angle in radians.
153    #[get(type(copy))]
154    pub(crate) wander_angle: f32,
155    /// Maximum agent speed.
156    #[get(type(copy))]
157    pub(crate) max_speed: f32,
158    /// Maximum steering acceleration magnitude.
159    #[get(type(copy))]
160    pub(crate) max_force: f32,
161}
162
163/// Spherical obstacle avoided by `BoidsFlockSolver`.
164#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
165pub struct BoidsObstacle3D {
166    /// Obstacle center in world space.
167    #[get(type(copy))]
168    pub(crate) center: Vec3,
169    /// Obstacle solid radius.
170    #[get(type(copy))]
171    pub(crate) radius: f32,
172}
173
174/// Wander circle geometry parameters for Reynolds `BoidsFlockSolver::steer_wander`.
175#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
176pub struct BoidsWanderParams {
177    /// Forward distance from the agent to the wander circle center.
178    #[get(type(copy))]
179    pub(crate) circle_dist: f32,
180    /// Radius of the wander target circle.
181    #[get(type(copy))]
182    pub(crate) circle_radius: f32,
183    /// Maximum angular jitter in radians applied per step.
184    #[get(type(copy))]
185    pub(crate) jitter: f32,
186}
187
188/// Flocking weights and neighborhood radii for `BoidsFlockSolver`.
189#[derive(Clone, Copy, Data, Debug, PartialEq)]
190pub struct BoidsConfig {
191    /// Neighborhood radius for alignment and cohesion.
192    #[get(type(copy))]
193    pub(crate) perception_radius: f32,
194    /// Close-range repulsion radius for separation.
195    #[get(type(copy))]
196    pub(crate) separation_radius: f32,
197    /// Weight applied to separation steering force.
198    #[get(type(copy))]
199    pub(crate) separation_weight: f32,
200    /// Weight applied to alignment steering force.
201    #[get(type(copy))]
202    pub(crate) alignment_weight: f32,
203    /// Weight applied to cohesion steering force.
204    #[get(type(copy))]
205    pub(crate) cohesion_weight: f32,
206    /// Weight applied to spherical obstacle avoidance steering.
207    #[get(type(copy))]
208    pub(crate) obstacle_weight: f32,
209    /// Weight applied to gentle wander noise steering.
210    #[get(type(copy))]
211    pub(crate) wander_weight: f32,
212    /// Weight applied to optional global goal attraction (`has_goal`).
213    #[get(type(copy))]
214    pub(crate) goal_weight: f32,
215}
216
217/// Reusable scratch state accelerating `BoidsFlockSolver` via `SpatialHashGrid3D`.
218#[derive(Clone, Debug)]
219pub(crate) struct BoidsScratch {
220    /// Uniform-cell 3D spatial hash rebuilt each step with zero steady-state allocations.
221    pub(crate) spatial_grid: SpatialHashGrid3D,
222    /// Reusable neighbor identifier query buffer.
223    pub(crate) neighbor_ids: Vec<u64>,
224    /// Staged steering accelerations applied simultaneously across the flock.
225    pub(crate) accelerations: Vec<Vec3>,
226    /// Staged updated wander angles applied simultaneously across the flock.
227    pub(crate) next_angles: Vec<f32>,
228}
229
230/// Reusable 3D Reynolds Steering and Flocking solver accelerated by `SpatialHashGrid3D`.
231#[derive(Clone, Data, Debug)]
232pub struct BoidsFlockSolver {
233    /// Flocking weights and radii.
234    #[get(type(copy))]
235    pub(crate) config: BoidsConfig,
236    /// Flock agent states.
237    pub(crate) agents: Vec<BoidsAgent3D>,
238    /// Spherical obstacles avoided by the flock.
239    pub(crate) obstacles: Vec<BoidsObstacle3D>,
240    /// Global target position when `has_goal` is `true`.
241    #[get(type(copy))]
242    pub(crate) goal_target: Vec3,
243    /// Whether agents steer toward `goal_target`.
244    #[get(type(copy))]
245    pub(crate) has_goal: bool,
246    /// Deterministic PRNG driving wander steering.
247    #[get(type(copy))]
248    pub(crate) rng: DeterministicRng,
249    /// Reusable spatial hash and scratch buffers.
250    #[get(pub(crate))]
251    #[get_mut(pub(crate))]
252    #[set(pub(crate))]
253    pub(crate) scratch: BoidsScratch,
254}