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::*;

/// Cycle evaluation helpers for `TrafficLightPhase3D`.
impl TrafficLightPhase3D {
    /// Evaluates the active signal phase for elapsed simulation time `time_secs` and intersection `offset_secs`.
    ///
    /// # Arguments
    ///
    /// - `f32` - Continuous simulation time in seconds.
    /// - `f32` - Per-intersection cycle phase offset in seconds.
    ///
    /// # Returns
    ///
    /// - `Self` - Active `Green`, `Amber`, or `Red` phase.
    #[must_use]
    pub fn from_cycle_time(time_secs: f32, offset_secs: f32) -> Self {
        let wrapped: f32 = (time_secs + offset_secs).rem_euclid(TRAFFIC_CYCLE_TOTAL_SECS);
        if wrapped < TRAFFIC_PHASE_GREEN_SECS {
            Self::Green
        } else if wrapped < TRAFFIC_PHASE_GREEN_SECS + TRAFFIC_PHASE_AMBER_SECS {
            Self::Amber
        } else {
            Self::Red
        }
    }
}

/// Default empty state construction for `CityEcosystemState3D`.
impl Default for CityEcosystemState3D {
    /// Returns an unpopulated `CityEcosystemState3D` with a deterministic `BoidsFlockSolver`.
    ///
    /// # Returns
    ///
    /// - `Self` - Empty ecosystem state ready for `populate_city_ecosystem_and_furniture`.
    fn default() -> Self {
        let boids_cfg: BoidsConfig = BoidsConfig {
            perception_radius: 26.0,
            separation_radius: 7.5,
            separation_weight: 2.35,
            alignment_weight: 1.25,
            cohesion_weight: 1.05,
            obstacle_weight: 4.5,
            wander_weight: 0.45,
            goal_weight: 1.35,
        };
        Self {
            populated: false,
            traffic_signals: Vec::new(),
            clouds: Vec::new(),
            bird_flock: BoidsFlockSolver::new(boids_cfg, CITY_ECO_BOIDS_SEED),
            birds: Vec::new(),
            wind_sway_props: Vec::new(),
            river_water_objects: Vec::new(),
            summary: CityEcosystemSummary3D::default(),
        }
    }
}

/// Spatial clearance and collision-free placement queries for `CityOccupancyTracker3D`.
impl CityOccupancyTracker3D {
    /// Seeds a `CityOccupancyTracker3D` from all existing static solid objects in `world`.
    ///
    /// # Arguments
    ///
    /// - `&World3D` - Existing city world whose static solid AABBs are reserved.
    ///
    /// # Returns
    ///
    /// - `Self` - Populated occupancy tracker enforcing zero static overlap.
    #[must_use]
    pub(crate) fn from_world(world: &World3D) -> Self {
        let mut occupied_boxes: Vec<AxisAlignedBox3D> =
            Vec::with_capacity(world.get_objects().len() + 384);
        for obj in world.get_objects().iter() {
            if obj.get_solid_collider() && !obj.get_is_dynamic() {
                occupied_boxes.push(obj.get_world_bounds());
            }
        }
        let ground_bounds: (f32, f32, f32, f32) = world.ground_play_bounds().unwrap_or((
            CITY_GROUND_X_MIN,
            CITY_GROUND_X_MAX,
            CITY_GROUND_Z_MIN,
            CITY_GROUND_Z_MAX,
        ));
        Self {
            occupied_boxes,
            ground_bounds,
        }
    }

    /// Returns `true` when `candidate` fits cleanly inside world ground limits and does not overlap any reserved static AABB within `margin`.
    ///
    /// # Arguments
    ///
    /// - `AxisAlignedBox3D` - Candidate world-space bounding box.
    /// - `f32` - Safety separation clearance margin in meters.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when placement is collision-free and strictly in bounds.
    #[must_use]
    pub(crate) fn can_place_static(&self, candidate: AxisAlignedBox3D, margin: f32) -> bool {
        let (min_x, max_x, min_z, max_z): (f32, f32, f32, f32) = self.get_ground_bounds();
        let cmin: Vec3 = candidate.get_min();
        let cmax: Vec3 = candidate.get_max();
        if cmin.get_x() < min_x + 0.8
            || cmax.get_x() > max_x - 0.8
            || cmin.get_z() < min_z + 0.8
            || cmax.get_z() > max_z - 0.8
        {
            return false;
        }
        let pad: f32 = margin.max(0.02);
        for existing in self.get_occupied_boxes().iter() {
            let emin: Vec3 = existing.get_min();
            let emax: Vec3 = existing.get_max();
            let ox: f32 = cmax.get_x().min(emax.get_x()) - cmin.get_x().max(emin.get_x());
            let oy: f32 = cmax.get_y().min(emax.get_y()) - cmin.get_y().max(emin.get_y());
            let oz: f32 = cmax.get_z().min(emax.get_z()) - cmin.get_z().max(emin.get_z());
            if ox > -pad && oy > -pad && oz > -pad {
                return false;
            }
        }
        true
    }

    /// Records `bounds` as an occupied static volume so subsequent placements stay strictly disjoint.
    ///
    /// # Arguments
    ///
    /// - `AxisAlignedBox3D` - Newly placed static object's world-space AABB.
    pub(crate) fn reserve_static(&mut self, bounds: AxisAlignedBox3D) {
        self.get_mut_occupied_boxes().push(bounds);
    }
}