elevator-core 8.3.0

Engine-agnostic elevator simulation library with pluggable dispatch strategies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
//! Central entity/component storage (struct-of-arrays ECS).

use std::any::{Any, TypeId};
use std::collections::HashMap;

use slotmap::{SecondaryMap, SlotMap};

use crate::components::{
    AccessControl, CallDirection, CarCall, DestinationQueue, Elevator, HallCall, Line, Patience,
    Position, Preferences, Rider, Route, ServiceMode, Stop, Velocity,
};
#[cfg(feature = "energy")]
use crate::energy::{EnergyMetrics, EnergyProfile};
use crate::entity::EntityId;
use crate::query::storage::AnyExtMap;

/// Central storage for all simulation entities and their components.
///
/// Uses separate `SecondaryMap` per component type (struct-of-arrays pattern)
/// to enable independent mutable borrows of different component storages
/// within the same system function.
///
/// Built-in components are accessed via typed methods. Games can attach
/// custom data via the extension storage (`insert_ext` / `get_ext`).
/// The query builder (`world.query::<...>()`) provides ECS-style iteration.
pub struct World {
    /// Primary key storage. An entity exists iff its key is here.
    pub(crate) alive: SlotMap<EntityId, ()>,

    // -- Built-in component storages (crate-internal) --
    /// Shaft-axis positions.
    pub(crate) positions: SecondaryMap<EntityId, Position>,
    /// Snapshot of `positions` taken at the start of the current tick.
    /// Enables sub-tick interpolation for smooth rendering between steps.
    pub(crate) prev_positions: SecondaryMap<EntityId, Position>,
    /// Shaft-axis velocities.
    pub(crate) velocities: SecondaryMap<EntityId, Velocity>,
    /// Elevator components.
    pub(crate) elevators: SecondaryMap<EntityId, Elevator>,
    /// Stop (floor/station) data.
    pub(crate) stops: SecondaryMap<EntityId, Stop>,
    /// Rider core data.
    pub(crate) riders: SecondaryMap<EntityId, Rider>,
    /// Multi-leg routes.
    pub(crate) routes: SecondaryMap<EntityId, Route>,
    /// Line (physical path) components.
    pub(crate) lines: SecondaryMap<EntityId, Line>,
    /// Patience tracking.
    pub(crate) patience: SecondaryMap<EntityId, Patience>,
    /// Boarding preferences.
    pub(crate) preferences: SecondaryMap<EntityId, Preferences>,
    /// Per-rider access control (allowed stops).
    pub(crate) access_controls: SecondaryMap<EntityId, AccessControl>,

    /// Per-elevator energy cost profiles.
    #[cfg(feature = "energy")]
    pub(crate) energy_profiles: SecondaryMap<EntityId, EnergyProfile>,
    /// Per-elevator accumulated energy metrics.
    #[cfg(feature = "energy")]
    pub(crate) energy_metrics: SecondaryMap<EntityId, EnergyMetrics>,
    /// Elevator service modes.
    pub(crate) service_modes: SecondaryMap<EntityId, ServiceMode>,
    /// Per-elevator destination queues.
    pub(crate) destination_queues: SecondaryMap<EntityId, DestinationQueue>,
    /// Up/down hall call buttons per stop. At most two per stop.
    pub(crate) hall_calls: SecondaryMap<EntityId, StopCalls>,
    /// Floor buttons pressed inside each car (Classic mode).
    pub(crate) car_calls: SecondaryMap<EntityId, Vec<CarCall>>,

    /// Disabled marker (entities skipped by all systems).
    pub(crate) disabled: SecondaryMap<EntityId, ()>,

    // -- Extension storage for game-specific components --
    /// Type-erased per-entity maps for custom components.
    extensions: HashMap<TypeId, Box<dyn AnyExtMap>>,
    /// `TypeId` → name mapping for extension serialization.
    ext_names: HashMap<TypeId, String>,

    // -- Global resources (singletons not attached to any entity) --
    /// Type-erased global resources for game-specific state.
    resources: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
}

impl World {
    /// Create an empty world with no entities.
    #[must_use]
    pub fn new() -> Self {
        Self {
            alive: SlotMap::with_key(),
            positions: SecondaryMap::new(),
            prev_positions: SecondaryMap::new(),
            velocities: SecondaryMap::new(),
            elevators: SecondaryMap::new(),
            stops: SecondaryMap::new(),
            riders: SecondaryMap::new(),
            routes: SecondaryMap::new(),
            lines: SecondaryMap::new(),
            patience: SecondaryMap::new(),
            preferences: SecondaryMap::new(),
            access_controls: SecondaryMap::new(),
            #[cfg(feature = "energy")]
            energy_profiles: SecondaryMap::new(),
            #[cfg(feature = "energy")]
            energy_metrics: SecondaryMap::new(),
            service_modes: SecondaryMap::new(),
            destination_queues: SecondaryMap::new(),
            hall_calls: SecondaryMap::new(),
            car_calls: SecondaryMap::new(),
            disabled: SecondaryMap::new(),
            extensions: HashMap::new(),
            ext_names: HashMap::new(),
            resources: HashMap::new(),
        }
    }

    /// Allocate a new entity. Returns its id. No components attached yet.
    pub fn spawn(&mut self) -> EntityId {
        self.alive.insert(())
    }

    /// Remove an entity and all its components (built-in and extensions).
    ///
    /// Cross-references are cleaned up automatically:
    /// - If the entity is a rider aboard an elevator, it is removed from the
    ///   elevator's rider list and `current_load` is adjusted.
    /// - If the entity is an elevator, its riders' phases are reset to `Waiting`.
    pub fn despawn(&mut self, id: EntityId) {
        // Clean up rider → elevator cross-references.
        if let Some(rider) = self.riders.get(id) {
            let weight = rider.weight;
            // If this rider is aboard an elevator, remove from its riders list.
            match rider.phase {
                crate::components::RiderPhase::Boarding(elev)
                | crate::components::RiderPhase::Riding(elev)
                | crate::components::RiderPhase::Exiting(elev) => {
                    if let Some(car) = self.elevators.get_mut(elev) {
                        car.riders.retain(|r| *r != id);
                        car.current_load = (car.current_load - weight).max(0.0);
                    }
                }
                _ => {}
            }
        }

        // Clean up elevator → rider cross-references.
        if let Some(car) = self.elevators.get(id) {
            let rider_ids: Vec<EntityId> = car.riders.clone();
            let elev_pos = self.positions.get(id).map(|p| p.value);
            let nearest_stop = elev_pos.and_then(|p| self.find_nearest_stop(p));
            for rid in rider_ids {
                if let Some(rider) = self.riders.get_mut(rid) {
                    rider.phase = crate::components::RiderPhase::Waiting;
                    rider.current_stop = nearest_stop;
                }
            }
        }

        self.alive.remove(id);
        self.positions.remove(id);
        self.prev_positions.remove(id);
        self.velocities.remove(id);
        self.elevators.remove(id);
        self.stops.remove(id);
        self.riders.remove(id);
        self.routes.remove(id);
        self.lines.remove(id);
        self.patience.remove(id);
        self.preferences.remove(id);
        self.access_controls.remove(id);
        #[cfg(feature = "energy")]
        self.energy_profiles.remove(id);
        #[cfg(feature = "energy")]
        self.energy_metrics.remove(id);
        self.service_modes.remove(id);
        self.destination_queues.remove(id);
        self.disabled.remove(id);

        for ext in self.extensions.values_mut() {
            ext.remove(id);
        }
    }

    /// Check if an entity is alive.
    #[must_use]
    pub fn is_alive(&self, id: EntityId) -> bool {
        self.alive.contains_key(id)
    }

    /// Number of live entities.
    #[must_use]
    pub fn entity_count(&self) -> usize {
        self.alive.len()
    }

    /// Iterate all alive entity keys (used by the query builder).
    pub(crate) fn alive_keys(&self) -> slotmap::basic::Keys<'_, EntityId, ()> {
        self.alive.keys()
    }

    // ── Position accessors ───────────────────────────────────────────

    /// Get an entity's position.
    #[must_use]
    pub fn position(&self, id: EntityId) -> Option<&Position> {
        self.positions.get(id)
    }

    /// Get an entity's position mutably.
    pub fn position_mut(&mut self, id: EntityId) -> Option<&mut Position> {
        self.positions.get_mut(id)
    }

    /// Set an entity's position.
    pub fn set_position(&mut self, id: EntityId, pos: Position) {
        self.positions.insert(id, pos);
    }

    /// Snapshot of an entity's position at the start of the current tick.
    ///
    /// Pairs with [`position`](Self::position) to support sub-tick interpolation
    /// (see [`Simulation::position_at`](crate::sim::Simulation::position_at)).
    #[must_use]
    pub fn prev_position(&self, id: EntityId) -> Option<&Position> {
        self.prev_positions.get(id)
    }

    /// Snapshot all current positions into `prev_positions`.
    ///
    /// Called at the start of each tick by
    /// [`Simulation::step`](crate::sim::Simulation::step) before any phase
    /// mutates positions.
    pub(crate) fn snapshot_prev_positions(&mut self) {
        self.prev_positions.clear();
        for (id, pos) in &self.positions {
            self.prev_positions.insert(id, *pos);
        }
    }

    // ── Velocity accessors ───────────────────────────────────────────

    /// Get an entity's velocity.
    #[must_use]
    pub fn velocity(&self, id: EntityId) -> Option<&Velocity> {
        self.velocities.get(id)
    }

    /// Get an entity's velocity mutably.
    pub fn velocity_mut(&mut self, id: EntityId) -> Option<&mut Velocity> {
        self.velocities.get_mut(id)
    }

    /// Set an entity's velocity.
    pub fn set_velocity(&mut self, id: EntityId, vel: Velocity) {
        self.velocities.insert(id, vel);
    }

    // ── Elevator accessors ───────────────────────────────────────────

    /// Get an entity's elevator component.
    #[must_use]
    pub fn elevator(&self, id: EntityId) -> Option<&Elevator> {
        self.elevators.get(id)
    }

    /// Get an entity's elevator component mutably.
    pub fn elevator_mut(&mut self, id: EntityId) -> Option<&mut Elevator> {
        self.elevators.get_mut(id)
    }

    /// Set an entity's elevator component.
    pub fn set_elevator(&mut self, id: EntityId, elev: Elevator) {
        self.elevators.insert(id, elev);
    }

    // ── Rider accessors ──────────────────────────────────────────────

    /// Get an entity's rider component.
    #[must_use]
    pub fn rider(&self, id: EntityId) -> Option<&Rider> {
        self.riders.get(id)
    }

    /// Get an entity's rider component mutably.
    pub fn rider_mut(&mut self, id: EntityId) -> Option<&mut Rider> {
        self.riders.get_mut(id)
    }

    /// Set an entity's rider component.
    pub fn set_rider(&mut self, id: EntityId, rider: Rider) {
        self.riders.insert(id, rider);
    }

    // ── Stop accessors ───────────────────────────────────────────────

    /// Get an entity's stop component.
    #[must_use]
    pub fn stop(&self, id: EntityId) -> Option<&Stop> {
        self.stops.get(id)
    }

    /// Get an entity's stop component mutably.
    pub fn stop_mut(&mut self, id: EntityId) -> Option<&mut Stop> {
        self.stops.get_mut(id)
    }

    /// Set an entity's stop component.
    pub fn set_stop(&mut self, id: EntityId, stop: Stop) {
        self.stops.insert(id, stop);
    }

    // ── Route accessors ──────────────────────────────────────────────

    /// Get an entity's route.
    #[must_use]
    pub fn route(&self, id: EntityId) -> Option<&Route> {
        self.routes.get(id)
    }

    /// Get an entity's route mutably.
    pub fn route_mut(&mut self, id: EntityId) -> Option<&mut Route> {
        self.routes.get_mut(id)
    }

    /// Set an entity's route.
    pub fn set_route(&mut self, id: EntityId, route: Route) {
        self.routes.insert(id, route);
    }

    // ── Line accessors ─────────────────────────────────────────────��──

    /// Get an entity's line component.
    #[must_use]
    pub fn line(&self, id: EntityId) -> Option<&Line> {
        self.lines.get(id)
    }

    /// Get an entity's line component mutably.
    pub fn line_mut(&mut self, id: EntityId) -> Option<&mut Line> {
        self.lines.get_mut(id)
    }

    /// Set an entity's line component.
    pub fn set_line(&mut self, id: EntityId, line: Line) {
        self.lines.insert(id, line);
    }

    /// Remove an entity's line component.
    pub fn remove_line(&mut self, id: EntityId) -> Option<Line> {
        self.lines.remove(id)
    }

    /// Iterate all line entities.
    pub fn iter_lines(&self) -> impl Iterator<Item = (EntityId, &Line)> {
        self.lines.iter()
    }

    // ── Patience accessors ───────────────────────────────────────────

    /// Get an entity's patience.
    #[must_use]
    pub fn patience(&self, id: EntityId) -> Option<&Patience> {
        self.patience.get(id)
    }

    /// Get an entity's patience mutably.
    pub fn patience_mut(&mut self, id: EntityId) -> Option<&mut Patience> {
        self.patience.get_mut(id)
    }

    /// Set an entity's patience.
    pub fn set_patience(&mut self, id: EntityId, patience: Patience) {
        self.patience.insert(id, patience);
    }

    // ── Preferences accessors ────────────────────────────────────────

    /// Get an entity's preferences.
    #[must_use]
    pub fn preferences(&self, id: EntityId) -> Option<&Preferences> {
        self.preferences.get(id)
    }

    /// Set an entity's preferences.
    pub fn set_preferences(&mut self, id: EntityId, prefs: Preferences) {
        self.preferences.insert(id, prefs);
    }

    // ── Access control accessors ────────────────────────────────────

    /// Get an entity's access control.
    #[must_use]
    pub fn access_control(&self, id: EntityId) -> Option<&AccessControl> {
        self.access_controls.get(id)
    }

    /// Get an entity's access control mutably.
    pub fn access_control_mut(&mut self, id: EntityId) -> Option<&mut AccessControl> {
        self.access_controls.get_mut(id)
    }

    /// Set an entity's access control.
    pub fn set_access_control(&mut self, id: EntityId, ac: AccessControl) {
        self.access_controls.insert(id, ac);
    }

    // ── Energy accessors (feature-gated) ────────────────────────────

    #[cfg(feature = "energy")]
    /// Get an entity's energy profile.
    #[must_use]
    pub fn energy_profile(&self, id: EntityId) -> Option<&EnergyProfile> {
        self.energy_profiles.get(id)
    }

    #[cfg(feature = "energy")]
    /// Get an entity's energy metrics.
    #[must_use]
    pub fn energy_metrics(&self, id: EntityId) -> Option<&EnergyMetrics> {
        self.energy_metrics.get(id)
    }

    #[cfg(feature = "energy")]
    /// Get an entity's energy metrics mutably.
    pub fn energy_metrics_mut(&mut self, id: EntityId) -> Option<&mut EnergyMetrics> {
        self.energy_metrics.get_mut(id)
    }

    #[cfg(feature = "energy")]
    /// Set an entity's energy profile.
    pub fn set_energy_profile(&mut self, id: EntityId, profile: EnergyProfile) {
        self.energy_profiles.insert(id, profile);
    }

    #[cfg(feature = "energy")]
    /// Set an entity's energy metrics.
    pub fn set_energy_metrics(&mut self, id: EntityId, metrics: EnergyMetrics) {
        self.energy_metrics.insert(id, metrics);
    }

    // ── Service mode accessors ──────────────────────────────────────

    /// Get an entity's service mode.
    #[must_use]
    pub fn service_mode(&self, id: EntityId) -> Option<&ServiceMode> {
        self.service_modes.get(id)
    }

    /// Set an entity's service mode.
    pub fn set_service_mode(&mut self, id: EntityId, mode: ServiceMode) {
        self.service_modes.insert(id, mode);
    }

    // ── Destination queue accessors ─────────────────────────────────

    /// Get an entity's destination queue.
    #[must_use]
    pub fn destination_queue(&self, id: EntityId) -> Option<&DestinationQueue> {
        self.destination_queues.get(id)
    }

    /// Get an entity's destination queue mutably (crate-internal — games
    /// mutate via the [`Simulation`](crate::sim::Simulation) helpers).
    pub(crate) fn destination_queue_mut(&mut self, id: EntityId) -> Option<&mut DestinationQueue> {
        self.destination_queues.get_mut(id)
    }

    /// Set an entity's destination queue.
    pub fn set_destination_queue(&mut self, id: EntityId, queue: DestinationQueue) {
        self.destination_queues.insert(id, queue);
    }

    // ── Hall call / car call accessors ──────────────────────────────
    //
    // Phase wiring in follow-up commits consumes the mutators. Until
    // that lands, `#[allow(dead_code)]` suppresses warnings that would
    // otherwise block the build under the workspace's `deny(warnings)`.

    /// Get the `(up, down)` hall call pair at a stop, if any exist.
    #[must_use]
    pub fn stop_calls(&self, stop: EntityId) -> Option<&StopCalls> {
        self.hall_calls.get(stop)
    }

    /// Get a specific directional hall call at a stop.
    #[must_use]
    pub fn hall_call(&self, stop: EntityId, direction: CallDirection) -> Option<&HallCall> {
        self.hall_calls.get(stop).and_then(|c| c.get(direction))
    }

    /// Mutable access to a directional hall call (crate-internal).
    #[allow(dead_code)]
    pub(crate) fn hall_call_mut(
        &mut self,
        stop: EntityId,
        direction: CallDirection,
    ) -> Option<&mut HallCall> {
        self.hall_calls
            .get_mut(stop)
            .and_then(|c| c.get_mut(direction))
    }

    /// Insert (or replace) a hall call at `stop` in `direction`.
    /// Returns `false` if the stop entity no longer exists in the world.
    #[allow(dead_code)]
    pub(crate) fn set_hall_call(&mut self, call: HallCall) -> bool {
        let Some(entry) = self.hall_calls.entry(call.stop) else {
            return false;
        };
        let slot = entry.or_default();
        match call.direction {
            CallDirection::Up => slot.up = Some(call),
            CallDirection::Down => slot.down = Some(call),
        }
        true
    }

    /// Remove and return the hall call at `(stop, direction)`, if any.
    #[allow(dead_code)]
    pub(crate) fn remove_hall_call(
        &mut self,
        stop: EntityId,
        direction: CallDirection,
    ) -> Option<HallCall> {
        let entry = self.hall_calls.get_mut(stop)?;
        match direction {
            CallDirection::Up => entry.up.take(),
            CallDirection::Down => entry.down.take(),
        }
    }

    /// Iterate every active hall call across the world.
    pub fn iter_hall_calls(&self) -> impl Iterator<Item = &HallCall> {
        self.hall_calls.values().flat_map(StopCalls::iter)
    }

    /// Mutable iteration over every active hall call (crate-internal).
    #[allow(dead_code)]
    pub(crate) fn iter_hall_calls_mut(&mut self) -> impl Iterator<Item = &mut HallCall> {
        self.hall_calls.values_mut().flat_map(StopCalls::iter_mut)
    }

    /// Car calls currently registered inside `car`.
    #[must_use]
    pub fn car_calls(&self, car: EntityId) -> &[CarCall] {
        self.car_calls.get(car).map_or(&[], Vec::as_slice)
    }

    /// Mutable access to the car-call list (crate-internal). Returns
    /// `None` if the car entity no longer exists.
    #[allow(dead_code)]
    pub(crate) fn car_calls_mut(&mut self, car: EntityId) -> Option<&mut Vec<CarCall>> {
        Some(self.car_calls.entry(car)?.or_default())
    }

    // ── Typed query helpers ──────────────────────────────────────────

    /// Iterate all elevator entities (have `Elevator` + `Position`).
    pub fn iter_elevators(&self) -> impl Iterator<Item = (EntityId, &Position, &Elevator)> {
        self.elevators
            .iter()
            .filter_map(|(id, car)| self.positions.get(id).map(|pos| (id, pos, car)))
    }

    /// Iterate all elevator entity IDs (allocates).
    #[must_use]
    pub fn elevator_ids(&self) -> Vec<EntityId> {
        self.elevators.keys().collect()
    }

    /// Fill the buffer with all elevator entity IDs, clearing it first.
    pub fn elevator_ids_into(&self, buf: &mut Vec<EntityId>) {
        buf.clear();
        buf.extend(self.elevators.keys());
    }

    /// Iterate all rider entities.
    pub fn iter_riders(&self) -> impl Iterator<Item = (EntityId, &Rider)> {
        self.riders.iter()
    }

    /// Iterate all rider entities mutably.
    pub fn iter_riders_mut(&mut self) -> impl Iterator<Item = (EntityId, &mut Rider)> {
        self.riders.iter_mut()
    }

    /// Iterate all rider entity IDs (allocates).
    #[must_use]
    pub fn rider_ids(&self) -> Vec<EntityId> {
        self.riders.keys().collect()
    }

    /// Iterate all stop entities.
    pub fn iter_stops(&self) -> impl Iterator<Item = (EntityId, &Stop)> {
        self.stops.iter()
    }

    /// Iterate all stop entity IDs (allocates).
    #[must_use]
    pub fn stop_ids(&self) -> Vec<EntityId> {
        self.stops.keys().collect()
    }

    /// Iterate elevators in `Idle` phase (not disabled).
    pub fn iter_idle_elevators(&self) -> impl Iterator<Item = (EntityId, &Position, &Elevator)> {
        use crate::components::ElevatorPhase;
        self.iter_elevators()
            .filter(|(id, _, car)| car.phase == ElevatorPhase::Idle && !self.is_disabled(*id))
    }

    /// Iterate elevators that are currently moving — either on a dispatched
    /// trip (`MovingToStop`) or a repositioning trip (`Repositioning`).
    /// Excludes disabled elevators.
    pub fn iter_moving_elevators(&self) -> impl Iterator<Item = (EntityId, &Position, &Elevator)> {
        self.iter_elevators()
            .filter(|(id, _, car)| car.phase.is_moving() && !self.is_disabled(*id))
    }

    /// Iterate riders in `Waiting` phase (not disabled).
    pub fn iter_waiting_riders(&self) -> impl Iterator<Item = (EntityId, &Rider)> {
        use crate::components::RiderPhase;
        self.iter_riders()
            .filter(|(id, r)| r.phase == RiderPhase::Waiting && !self.is_disabled(*id))
    }

    /// Find the stop entity at a given position (within epsilon).
    #[must_use]
    pub fn find_stop_at_position(&self, position: f64) -> Option<EntityId> {
        const EPSILON: f64 = 1e-6;
        self.stops.iter().find_map(|(id, stop)| {
            if (stop.position - position).abs() < EPSILON {
                Some(id)
            } else {
                None
            }
        })
    }

    /// Find the stop entity nearest to a given position.
    ///
    /// Unlike [`find_stop_at_position`](Self::find_stop_at_position), this finds
    /// the closest stop by minimum distance rather than requiring an exact match.
    /// Used when ejecting riders from a disabled/despawned elevator mid-transit.
    #[must_use]
    pub fn find_nearest_stop(&self, position: f64) -> Option<EntityId> {
        self.stops
            .iter()
            .min_by(|(_, a), (_, b)| {
                (a.position - position)
                    .abs()
                    .total_cmp(&(b.position - position).abs())
            })
            .map(|(id, _)| id)
    }

    /// Get a stop's position by entity id.
    #[must_use]
    pub fn stop_position(&self, id: EntityId) -> Option<f64> {
        self.stops.get(id).map(|s| s.position)
    }

    // ── Extension (custom component) storage ─────────────────────────

    /// Insert a custom component for an entity.
    ///
    /// Games use this to attach their own typed data to simulation entities.
    /// Extension components must be `Serialize + DeserializeOwned` to support
    /// snapshot save/load. A `name` string is required for serialization roundtrips.
    /// Extension components are automatically cleaned up on `despawn()`.
    ///
    /// ```
    /// use elevator_core::world::World;
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(Debug, Clone, Serialize, Deserialize)]
    /// struct VipTag { level: u32 }
    ///
    /// let mut world = World::new();
    /// let entity = world.spawn();
    /// world.insert_ext(entity, VipTag { level: 3 }, "vip_tag");
    /// ```
    pub fn insert_ext<T: 'static + Send + Sync + serde::Serialize + serde::de::DeserializeOwned>(
        &mut self,
        id: EntityId,
        value: T,
        name: &str,
    ) {
        let type_id = TypeId::of::<T>();
        let map = self
            .extensions
            .entry(type_id)
            .or_insert_with(|| Box::new(SecondaryMap::<EntityId, T>::new()));
        if let Some(m) = map.as_any_mut().downcast_mut::<SecondaryMap<EntityId, T>>() {
            m.insert(id, value);
        }
        self.ext_names.insert(type_id, name.to_owned());
    }

    /// Get a clone of a custom component for an entity.
    #[must_use]
    pub fn get_ext<T: 'static + Send + Sync + Clone>(&self, id: EntityId) -> Option<T> {
        self.ext_map::<T>()?.get(id).cloned()
    }

    /// Get a mutable reference to a custom component for an entity.
    pub fn get_ext_mut<T: 'static + Send + Sync>(&mut self, id: EntityId) -> Option<&mut T> {
        self.ext_map_mut::<T>()?.get_mut(id)
    }

    /// Remove a custom component for an entity.
    pub fn remove_ext<T: 'static + Send + Sync>(&mut self, id: EntityId) -> Option<T> {
        self.ext_map_mut::<T>()?.remove(id)
    }

    /// Downcast extension storage to a typed `SecondaryMap` (shared).
    pub(crate) fn ext_map<T: 'static + Send + Sync>(&self) -> Option<&SecondaryMap<EntityId, T>> {
        self.extensions
            .get(&TypeId::of::<T>())?
            .as_any()
            .downcast_ref::<SecondaryMap<EntityId, T>>()
    }

    /// Downcast extension storage to a typed `SecondaryMap` (mutable).
    fn ext_map_mut<T: 'static + Send + Sync>(&mut self) -> Option<&mut SecondaryMap<EntityId, T>> {
        self.extensions
            .get_mut(&TypeId::of::<T>())?
            .as_any_mut()
            .downcast_mut::<SecondaryMap<EntityId, T>>()
    }

    /// Serialize all extension component data for snapshot.
    /// Returns name → (`EntityId` → RON string) mapping.
    pub(crate) fn serialize_extensions(&self) -> HashMap<String, HashMap<EntityId, String>> {
        let mut result = HashMap::new();
        for (type_id, map) in &self.extensions {
            if let Some(name) = self.ext_names.get(type_id) {
                result.insert(name.clone(), map.serialize_entries());
            }
        }
        result
    }

    /// Deserialize extension data from snapshot. Requires that extension types
    /// have been registered (via `register_ext_deserializer`) before calling.
    pub(crate) fn deserialize_extensions(
        &mut self,
        data: &HashMap<String, HashMap<EntityId, String>>,
    ) {
        for (name, entries) in data {
            // Find the TypeId by name.
            if let Some((&type_id, _)) = self.ext_names.iter().find(|(_, n)| *n == name)
                && let Some(map) = self.extensions.get_mut(&type_id)
            {
                map.deserialize_entries(entries);
            }
        }
    }

    /// Register an extension type for deserialization (creates empty storage).
    ///
    /// Must be called before `restore()` for each extension type that was
    /// present in the original simulation.
    pub fn register_ext<
        T: 'static + Send + Sync + serde::Serialize + serde::de::DeserializeOwned,
    >(
        &mut self,
        name: &str,
    ) {
        let type_id = TypeId::of::<T>();
        self.extensions
            .entry(type_id)
            .or_insert_with(|| Box::new(SecondaryMap::<EntityId, T>::new()));
        self.ext_names.insert(type_id, name.to_owned());
    }

    // ── Disabled entity management ──────────────────────────────────

    /// Mark an entity as disabled. Disabled entities are skipped by all systems.
    pub fn disable(&mut self, id: EntityId) {
        self.disabled.insert(id, ());
    }

    /// Re-enable a disabled entity.
    pub fn enable(&mut self, id: EntityId) {
        self.disabled.remove(id);
    }

    /// Check if an entity is disabled.
    #[must_use]
    pub fn is_disabled(&self, id: EntityId) -> bool {
        self.disabled.contains_key(id)
    }

    // ── Global resources (singletons) ───────────────────────────────

    /// Insert a global resource. Replaces any existing resource of the same type.
    ///
    /// Resources are singletons not attached to any entity. Games use them
    /// for event channels, score trackers, or any global state.
    ///
    /// ```
    /// use elevator_core::world::World;
    /// use elevator_core::events::EventChannel;
    ///
    /// #[derive(Debug)]
    /// enum MyEvent { Score(u32) }
    ///
    /// let mut world = World::new();
    /// world.insert_resource(EventChannel::<MyEvent>::new());
    /// ```
    pub fn insert_resource<T: 'static + Send + Sync>(&mut self, value: T) {
        self.resources.insert(TypeId::of::<T>(), Box::new(value));
    }

    /// Get a shared reference to a global resource.
    #[must_use]
    pub fn resource<T: 'static + Send + Sync>(&self) -> Option<&T> {
        self.resources.get(&TypeId::of::<T>())?.downcast_ref()
    }

    /// Get a mutable reference to a global resource.
    pub fn resource_mut<T: 'static + Send + Sync>(&mut self) -> Option<&mut T> {
        self.resources.get_mut(&TypeId::of::<T>())?.downcast_mut()
    }

    /// Remove a global resource, returning it if it existed.
    pub fn remove_resource<T: 'static + Send + Sync>(&mut self) -> Option<T> {
        self.resources
            .remove(&TypeId::of::<T>())
            .and_then(|b| b.downcast().ok())
            .map(|b| *b)
    }

    // ── Query builder ───────────────────────────────────────────────

    /// Create a query builder for iterating entities by component composition.
    ///
    /// ```
    /// use elevator_core::prelude::*;
    ///
    /// let mut sim = SimulationBuilder::demo().build().unwrap();
    /// sim.spawn_rider_by_stop_id(StopId(0), StopId(1), 75.0).unwrap();
    ///
    /// let world = sim.world();
    /// for (id, rider, pos) in world.query::<(EntityId, &Rider, &Position)>().iter() {
    ///     println!("{id:?}: {:?} at {}", rider.phase(), pos.value());
    /// }
    /// ```
    #[must_use]
    pub const fn query<Q: crate::query::WorldQuery>(&self) -> crate::query::QueryBuilder<'_, Q> {
        crate::query::QueryBuilder::new(self)
    }

    /// Create a mutable extension query builder.
    ///
    /// Uses the keys-snapshot pattern: collects matching entity IDs upfront
    /// into an owned `Vec`, then iterates with mutable access via
    /// [`for_each_mut`](crate::query::ExtQueryMut::for_each_mut).
    ///
    /// # Example
    ///
    /// ```ignore
    /// world.query_ext_mut::<VipTag>().for_each_mut(|id, tag| {
    ///     tag.level += 1;
    /// });
    /// ```
    pub fn query_ext_mut<T: 'static + Send + Sync>(&mut self) -> crate::query::ExtQueryMut<'_, T> {
        crate::query::ExtQueryMut::new(self)
    }
}

impl Default for World {
    fn default() -> Self {
        Self::new()
    }
}

/// Stops sorted by position for efficient range queries (binary search).
///
/// Used by the movement system to detect `PassingFloor` events in O(log n)
/// instead of O(n) per moving elevator per tick.
pub(crate) struct SortedStops(pub(crate) Vec<(f64, EntityId)>);

/// The up/down hall call pair at a single stop.
///
/// At most two calls coexist at a stop (one per [`CallDirection`]);
/// this struct owns the slots. Stored in [`World::hall_calls`] keyed by
/// the stop's entity id.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct StopCalls {
    /// Pending upward call, if the up button is pressed.
    pub up: Option<HallCall>,
    /// Pending downward call, if the down button is pressed.
    pub down: Option<HallCall>,
}

impl StopCalls {
    /// Borrow the call for a specific direction.
    #[must_use]
    pub const fn get(&self, direction: CallDirection) -> Option<&HallCall> {
        match direction {
            CallDirection::Up => self.up.as_ref(),
            CallDirection::Down => self.down.as_ref(),
        }
    }

    /// Mutable borrow of the call for a direction.
    pub const fn get_mut(&mut self, direction: CallDirection) -> Option<&mut HallCall> {
        match direction {
            CallDirection::Up => self.up.as_mut(),
            CallDirection::Down => self.down.as_mut(),
        }
    }

    /// Iterate both calls in (Up, Down) order, skipping empty slots.
    pub fn iter(&self) -> impl Iterator<Item = &HallCall> {
        self.up.iter().chain(self.down.iter())
    }

    /// Mutable iteration over both calls.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut HallCall> {
        self.up.iter_mut().chain(self.down.iter_mut())
    }
}