euv-core 0.57.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Typed key-value blackboard shared across `BehaviorTree` condition and action nodes.
#[derive(Clone, Data, Debug, Default, PartialEq)]
pub struct Blackboard {
    /// Stored typed entries indexed by string key.
    pub(crate) entries: HashMap<String, BtValue>,
}

/// Per-node runtime memory stored cleanly outside tree topology for zero-allocation ticks.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct BtNodeState {
    /// Active child offset resumed on the next tick for memory-aware composites.
    pub(crate) running_child: usize,
    /// Completed repetitions counter for `Repeater` decorators.
    pub(crate) repeat_count: usize,
    /// Remaining cooldown timer in seconds for `Cooldown` decorators.
    pub(crate) cooldown_remaining: f32,
}

/// Data-driven arena-indexed Behavior Tree supporting composites, decorators, conditions, and actions.
#[derive(Clone, Data, Debug, Default)]
pub struct BehaviorTree {
    /// Arena of behavior tree nodes.
    pub(crate) nodes: Vec<BtNodeKind>,
    /// Root node index.
    #[get(type(copy))]
    pub(crate) root: usize,
    /// Per-node execution state aligned 1:1 with `nodes`.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) states: Vec<BtNodeState>,
}

/// State definition inside a `HierarchicalFsm` with optional parent and default child.
#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
pub struct HfsmStateDef {
    /// Unique state identifier.
    #[get(type(copy))]
    pub(crate) id: u32,
    /// Optional parent state identifier (`None` for top-level root states).
    #[get(skip)]
    pub(crate) parent: Option<u32>,
    /// Optional default initial sub-state entered automatically when entering this composite state.
    #[get(skip)]
    pub(crate) initial_child: Option<u32>,
}

/// Guarded transition rule between states in a `HierarchicalFsm`.
#[derive(Clone, Copy, Data, Debug, Eq, Hash, PartialEq)]
pub struct HfsmTransition {
    /// Source state identifier (also matches when active as an ancestor via parent fallback).
    #[get(type(copy))]
    pub(crate) from_state: u32,
    /// Destination state identifier.
    #[get(type(copy))]
    pub(crate) to_state: u32,
    /// Optional event trigger (`None` evaluates automatically on every `update`).
    #[get(skip)]
    pub(crate) event_id: Option<u32>,
    /// Optional guard identifier evaluated via callback before taking the transition.
    #[get(skip)]
    pub(crate) guard_id: Option<u32>,
    /// Priority tie-breaker (higher priority evaluated first).
    #[get(type(copy))]
    pub(crate) priority: i32,
}

/// Hierarchical Finite State Machine with LCA exit/enter ordering, guarded transitions, and parent fallback.
#[derive(Clone, Data, Debug, Default)]
pub struct HierarchicalFsm {
    /// Registered state definitions.
    pub(crate) states: Vec<HfsmStateDef>,
    /// Registered guarded transitions.
    pub(crate) transitions: Vec<HfsmTransition>,
    /// Active nested state chain ordered from top-level ancestor to leaf state.
    pub(crate) active_chain: Vec<u32>,
    /// Reusable target ancestor chain scratch buffer avoiding per-transition allocations.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) target_chain_scratch: Vec<u32>,
}

/// Single normalized input axis and response curve contributing to a `UtilityActionDef` score.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct UtilityConsideration {
    /// Caller-defined input signal identifier.
    #[get(type(copy))]
    pub(crate) input_id: u32,
    /// Input value mapping to normalized `0.0`.
    #[get(type(copy))]
    pub(crate) min_input: f32,
    /// Input value mapping to normalized `1.0`.
    #[get(type(copy))]
    pub(crate) max_input: f32,
    /// Response curve shaping normalized input into `[0.0, 1.0]`.
    #[get(type(copy))]
    pub(crate) curve: UtilityCurveKind,
    /// Positive exponent weight scaling this consideration's influence.
    #[get(type(copy))]
    pub(crate) weight: f32,
}

/// Candidate action evaluated by `UtilityAiBrain`.
#[derive(Clone, Data, Debug, New, PartialEq)]
pub struct UtilityActionDef {
    /// Unique action identifier.
    #[get(type(copy))]
    pub(crate) action_id: u32,
    /// Base priority multiplier for this action.
    #[get(type(copy))]
    pub(crate) base_weight: f32,
    /// Considerations combined with compensation factor to compute utility.
    pub(crate) considerations: Vec<UtilityConsideration>,
}

/// Multi-consideration Utility AI selector with response curves and hysteresis anti-oscillation.
#[derive(Clone, Data, Debug)]
pub struct UtilityAiBrain {
    /// Candidate actions evaluated each tick.
    pub(crate) actions: Vec<UtilityActionDef>,
    /// Additive inertia bonus applied to the currently active action to suppress score chatter.
    #[get(type(copy))]
    pub(crate) inertia_bonus: f32,
    /// Minimum dwell duration in seconds before switching away from a viable active action.
    #[get(type(copy))]
    pub(crate) min_dwell_time: f32,
    /// Currently selected action identifier when `has_active` is `true`.
    #[get(type(copy))]
    pub(crate) current_action: u32,
    /// Whether an active action has been selected.
    #[get(type(copy))]
    pub(crate) has_active: bool,
    /// Elapsed dwell time in the currently active action.
    #[get(type(copy))]
    pub(crate) dwell_elapsed: f32,
    /// Reusable buffer recording the latest `(action_id, utility_score)` results.
    pub(crate) last_scores: Vec<(u32, f32)>,
}

/// Individual 3D Reynolds steering and flocking agent.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct BoidsAgent3D {
    /// World-space 3D position.
    #[get(type(copy))]
    pub(crate) position: Vec3,
    /// World-space 3D velocity vector.
    #[get(type(copy))]
    pub(crate) velocity: Vec3,
    /// Persistent wander phase angle in radians.
    #[get(type(copy))]
    pub(crate) wander_angle: f32,
    /// Maximum agent speed.
    #[get(type(copy))]
    pub(crate) max_speed: f32,
    /// Maximum steering acceleration magnitude.
    #[get(type(copy))]
    pub(crate) max_force: f32,
}

/// Spherical obstacle avoided by `BoidsFlockSolver`.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct BoidsObstacle3D {
    /// Obstacle center in world space.
    #[get(type(copy))]
    pub(crate) center: Vec3,
    /// Obstacle solid radius.
    #[get(type(copy))]
    pub(crate) radius: f32,
}

/// Wander circle geometry parameters for Reynolds `BoidsFlockSolver::steer_wander`.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct BoidsWanderParams {
    /// Forward distance from the agent to the wander circle center.
    #[get(type(copy))]
    pub(crate) circle_dist: f32,
    /// Radius of the wander target circle.
    #[get(type(copy))]
    pub(crate) circle_radius: f32,
    /// Maximum angular jitter in radians applied per step.
    #[get(type(copy))]
    pub(crate) jitter: f32,
}

/// Flocking weights and neighborhood radii for `BoidsFlockSolver`.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct BoidsConfig {
    /// Neighborhood radius for alignment and cohesion.
    #[get(type(copy))]
    pub(crate) perception_radius: f32,
    /// Close-range repulsion radius for separation.
    #[get(type(copy))]
    pub(crate) separation_radius: f32,
    /// Weight applied to separation steering force.
    #[get(type(copy))]
    pub(crate) separation_weight: f32,
    /// Weight applied to alignment steering force.
    #[get(type(copy))]
    pub(crate) alignment_weight: f32,
    /// Weight applied to cohesion steering force.
    #[get(type(copy))]
    pub(crate) cohesion_weight: f32,
    /// Weight applied to spherical obstacle avoidance steering.
    #[get(type(copy))]
    pub(crate) obstacle_weight: f32,
    /// Weight applied to gentle wander noise steering.
    #[get(type(copy))]
    pub(crate) wander_weight: f32,
    /// Weight applied to optional global goal attraction (`has_goal`).
    #[get(type(copy))]
    pub(crate) goal_weight: f32,
}

/// Reusable scratch state accelerating `BoidsFlockSolver` via `SpatialHashGrid3D`.
#[derive(Clone, Debug)]
pub(crate) struct BoidsScratch {
    /// Uniform-cell 3D spatial hash rebuilt each step with zero steady-state allocations.
    pub(crate) spatial_grid: SpatialHashGrid3D,
    /// Reusable neighbor identifier query buffer.
    pub(crate) neighbor_ids: Vec<u64>,
    /// Staged steering accelerations applied simultaneously across the flock.
    pub(crate) accelerations: Vec<Vec3>,
    /// Staged updated wander angles applied simultaneously across the flock.
    pub(crate) next_angles: Vec<f32>,
}

/// Reusable 3D Reynolds Steering and Flocking solver accelerated by `SpatialHashGrid3D`.
#[derive(Clone, Data, Debug)]
pub struct BoidsFlockSolver {
    /// Flocking weights and radii.
    #[get(type(copy))]
    pub(crate) config: BoidsConfig,
    /// Flock agent states.
    pub(crate) agents: Vec<BoidsAgent3D>,
    /// Spherical obstacles avoided by the flock.
    pub(crate) obstacles: Vec<BoidsObstacle3D>,
    /// Global target position when `has_goal` is `true`.
    #[get(type(copy))]
    pub(crate) goal_target: Vec3,
    /// Whether agents steer toward `goal_target`.
    #[get(type(copy))]
    pub(crate) has_goal: bool,
    /// Deterministic PRNG driving wander steering.
    #[get(type(copy))]
    pub(crate) rng: DeterministicRng,
    /// Reusable spatial hash and scratch buffers.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) scratch: BoidsScratch,
}