Skip to main content

euv_core/game/engine3d/city_eco/
impl.rs

1use super::*;
2
3/// Cycle evaluation helpers for `TrafficLightPhase3D`.
4impl TrafficLightPhase3D {
5    /// Evaluates the active signal phase for elapsed simulation time `time_secs` and intersection `offset_secs`.
6    ///
7    /// # Arguments
8    ///
9    /// - `f32` - Continuous simulation time in seconds.
10    /// - `f32` - Per-intersection cycle phase offset in seconds.
11    ///
12    /// # Returns
13    ///
14    /// - `Self` - Active `Green`, `Amber`, or `Red` phase.
15    #[must_use]
16    pub fn from_cycle_time(time_secs: f32, offset_secs: f32) -> Self {
17        let wrapped: f32 = (time_secs + offset_secs).rem_euclid(TRAFFIC_CYCLE_TOTAL_SECS);
18        if wrapped < TRAFFIC_PHASE_GREEN_SECS {
19            Self::Green
20        } else if wrapped < TRAFFIC_PHASE_GREEN_SECS + TRAFFIC_PHASE_AMBER_SECS {
21            Self::Amber
22        } else {
23            Self::Red
24        }
25    }
26}
27
28/// Default empty state construction for `CityEcosystemState3D`.
29impl Default for CityEcosystemState3D {
30    /// Returns an unpopulated `CityEcosystemState3D` with a deterministic `BoidsFlockSolver`.
31    ///
32    /// # Returns
33    ///
34    /// - `Self` - Empty ecosystem state ready for `populate_city_ecosystem_and_furniture`.
35    fn default() -> Self {
36        let boids_cfg: BoidsConfig = BoidsConfig {
37            perception_radius: 26.0,
38            separation_radius: 7.5,
39            separation_weight: 2.35,
40            alignment_weight: 1.25,
41            cohesion_weight: 1.05,
42            obstacle_weight: 4.5,
43            wander_weight: 0.45,
44            goal_weight: 1.35,
45        };
46        Self {
47            populated: false,
48            traffic_signals: Vec::new(),
49            clouds: Vec::new(),
50            bird_flock: BoidsFlockSolver::new(boids_cfg, CITY_ECO_BOIDS_SEED),
51            birds: Vec::new(),
52            wind_sway_props: Vec::new(),
53            river_water_objects: Vec::new(),
54            summary: CityEcosystemSummary3D::default(),
55        }
56    }
57}
58
59/// Spatial clearance and collision-free placement queries for `CityOccupancyTracker3D`.
60impl CityOccupancyTracker3D {
61    /// Seeds a `CityOccupancyTracker3D` from all existing static solid objects in `world`.
62    ///
63    /// # Arguments
64    ///
65    /// - `&World3D` - Existing city world whose static solid AABBs are reserved.
66    ///
67    /// # Returns
68    ///
69    /// - `Self` - Populated occupancy tracker enforcing zero static overlap.
70    #[must_use]
71    pub(crate) fn from_world(world: &World3D) -> Self {
72        let mut occupied_boxes: Vec<AxisAlignedBox3D> =
73            Vec::with_capacity(world.get_objects().len() + 384);
74        for obj in world.get_objects().iter() {
75            if obj.get_solid_collider() && !obj.get_is_dynamic() {
76                occupied_boxes.push(obj.get_world_bounds());
77            }
78        }
79        let ground_bounds: (f32, f32, f32, f32) = world.ground_play_bounds().unwrap_or((
80            CITY_GROUND_X_MIN,
81            CITY_GROUND_X_MAX,
82            CITY_GROUND_Z_MIN,
83            CITY_GROUND_Z_MAX,
84        ));
85        Self {
86            occupied_boxes,
87            ground_bounds,
88        }
89    }
90
91    /// Returns `true` when `candidate` fits cleanly inside world ground limits and does not overlap any reserved static AABB within `margin`.
92    ///
93    /// # Arguments
94    ///
95    /// - `AxisAlignedBox3D` - Candidate world-space bounding box.
96    /// - `f32` - Safety separation clearance margin in meters.
97    ///
98    /// # Returns
99    ///
100    /// - `bool` - `true` when placement is collision-free and strictly in bounds.
101    #[must_use]
102    pub(crate) fn can_place_static(&self, candidate: AxisAlignedBox3D, margin: f32) -> bool {
103        let (min_x, max_x, min_z, max_z): (f32, f32, f32, f32) = self.get_ground_bounds();
104        let cmin: Vec3 = candidate.get_min();
105        let cmax: Vec3 = candidate.get_max();
106        if cmin.get_x() < min_x + 0.8
107            || cmax.get_x() > max_x - 0.8
108            || cmin.get_z() < min_z + 0.8
109            || cmax.get_z() > max_z - 0.8
110        {
111            return false;
112        }
113        let pad: f32 = margin.max(0.02);
114        for existing in self.get_occupied_boxes().iter() {
115            let emin: Vec3 = existing.get_min();
116            let emax: Vec3 = existing.get_max();
117            let ox: f32 = cmax.get_x().min(emax.get_x()) - cmin.get_x().max(emin.get_x());
118            let oy: f32 = cmax.get_y().min(emax.get_y()) - cmin.get_y().max(emin.get_y());
119            let oz: f32 = cmax.get_z().min(emax.get_z()) - cmin.get_z().max(emin.get_z());
120            if ox > -pad && oy > -pad && oz > -pad {
121                return false;
122            }
123        }
124        true
125    }
126
127    /// Records `bounds` as an occupied static volume so subsequent placements stay strictly disjoint.
128    ///
129    /// # Arguments
130    ///
131    /// - `AxisAlignedBox3D` - Newly placed static object's world-space AABB.
132    pub(crate) fn reserve_static(&mut self, bounds: AxisAlignedBox3D) {
133        self.get_mut_occupied_boxes().push(bounds);
134    }
135}