elevator-core 18.0.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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
//! Central entity/component storage (struct-of-arrays ECS).

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

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;

/// Typed handle for extension component storage.
///
/// Constructed via [`ExtKey::new`] with an explicit name, or
/// [`ExtKey::from_type_name`] which uses `std::any::type_name::<T>()`.
#[derive(Debug)]
pub struct ExtKey<T> {
    /// Human-readable storage name, used for serialization roundtrips.
    name: &'static str,
    /// Binds this key to the extension component type `T`.
    _marker: PhantomData<T>,
}

impl<T> Clone for ExtKey<T> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<T> Copy for ExtKey<T> {}

impl<T> ExtKey<T> {
    /// Create a key with an explicit storage name.
    #[must_use]
    pub const fn new(name: &'static str) -> Self {
        Self {
            name,
            _marker: PhantomData,
        }
    }

    /// Create a key using `std::any::type_name::<T>()` as the storage name.
    #[must_use]
    pub fn from_type_name() -> Self {
        Self {
            name: std::any::type_name::<T>(),
            _marker: PhantomData,
        }
    }

    /// The storage name for this key.
    #[must_use]
    pub const fn name(&self) -> &'static str {
        self.name
    }
}

impl<T> Default for ExtKey<T> {
    fn default() -> Self {
        Self::from_type_name()
    }
}

/// 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` / `ext`).
/// The query builder (`world.query::<...>()`) provides ECS-style iteration.
///
/// # Component-accessor semantics
///
/// All `<component>(&self, id)` accessors return `Option`. They return `None`
/// when the entity is dead **or** when it is alive but lacks that component —
/// slotmap semantics make these two cases indistinguishable through this
/// surface. Use [`World::is_alive`] (or check the typed component you expect
/// to be present, e.g. `elevator()` for an elevator entity) when the
/// distinction matters.
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).
    ///
    /// `World::despawn` is a low-level operation: it removes the entity's
    /// arena entries and performs the cross-references that `World` can
    /// safely maintain on its own. It does **not** perform rider lifecycle
    /// transitions (which require `RiderIndex`, owned by `Simulation`).
    ///
    /// Cross-references handled here:
    /// - If the entity is a rider aboard an elevator, it is removed from
    ///   the elevator's rider list and `current_load` is adjusted.
    ///
    /// **Despawning an elevator with aboard riders is the caller's
    /// responsibility to clean up.** Use
    /// [`Simulation::remove_elevator`](crate::sim::Simulation::remove_elevator)
    /// (which calls [`Simulation::disable`](crate::sim::Simulation::disable)
    /// first to transition aboard riders to `Waiting` via the transition
    /// gateway). Calling this method directly on a populated elevator leaves
    /// aboard riders pointing at a now-dead `EntityId` in their `phase` —
    /// a footgun this method no longer attempts to paper over, since any
    /// reset it produced would silently desync `RiderIndex`.
    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 -= weight;
                    }
                }
                _ => {}
            }
        }

        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);
        self.hall_calls.remove(id);
        self.car_calls.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.
    ///
    /// # Warning
    ///
    /// This does **not** update the [`RiderIndex`](crate::rider_index::RiderIndex).
    /// Call [`RiderIndex::rebuild`](crate::rider_index::RiderIndex::rebuild) afterward
    /// if the phase or `current_stop` changed.
    pub(crate) 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 ──────────────────────────────

    /// 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).
    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.
    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.
    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).
    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.
    pub(crate) fn car_calls_mut(&mut self, car: EntityId) -> Option<&mut Vec<CarCall>> {
        Some(self.car_calls.entry(car)?.or_default())
    }

    /// Remove `rider` from every hall- and car-call's `pending_riders`
    /// list, and drop car calls whose list becomes empty as a result.
    ///
    /// Call on despawn or abandonment so stale rider IDs don't hold
    /// calls open past the rider's life. Mirrors the per-exit cleanup
    /// in `systems::loading` (for `Exit`-time car-call pruning). Hall
    /// calls stay alive after the list empties: they may still represent
    /// a script-driven press with no associated rider, and are cleared
    /// the usual way when an eligible car opens doors.
    pub(crate) fn scrub_rider_from_pending_calls(&mut self, rider: EntityId) {
        for call in self.iter_hall_calls_mut() {
            call.pending_riders.retain(|r| *r != rider);
        }
        for calls in self.car_calls.values_mut() {
            for c in calls.iter_mut() {
                c.pending_riders.retain(|r| *r != rider);
            }
            calls.retain(|c| !c.pending_riders.is_empty());
        }
    }

    // ── 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
    /// [`STOP_POSITION_EPSILON`](Self::STOP_POSITION_EPSILON)).
    ///
    /// Global lookup — does not filter by line. When two stops on
    /// different lines share the same physical position the result is
    /// whichever wins the linear scan, which is rarely what the
    /// caller actually wants. Prefer
    /// [`find_stop_at_position_in`](Self::find_stop_at_position_in)
    /// when the caller knows which line's stops to consider.
    #[must_use]
    pub fn find_stop_at_position(&self, position: f64) -> Option<EntityId> {
        self.stops.iter().find_map(|(id, stop)| {
            if (stop.position - position).abs() < Self::STOP_POSITION_EPSILON {
                Some(id)
            } else {
                None
            }
        })
    }

    /// Find the stop at a given position from within `candidates`.
    ///
    /// `candidates` is typically the `serves` list of a particular
    /// [`LineInfo`](crate::dispatch::LineInfo) — i.e. the stops a
    /// specific line can reach. Use this when a car arrives at a
    /// position and you need *its* line's stop entity, not whichever
    /// stop on any line happens to share the position. (Two parallel
    /// shafts at the same physical floor, or a sky-lobby served by
    /// both a low and high bank, both produce position collisions
    /// the global lookup can't disambiguate.)
    ///
    /// O(n) over `candidates`, which is typically small.
    #[must_use]
    pub fn find_stop_at_position_in(
        &self,
        position: f64,
        candidates: &[EntityId],
    ) -> Option<EntityId> {
        candidates.iter().copied().find(|&id| {
            self.stops
                .get(id)
                .is_some_and(|stop| (stop.position - position).abs() < Self::STOP_POSITION_EPSILON)
        })
    }

    /// Tolerance for [`find_stop_at_position`](Self::find_stop_at_position)
    /// and [`find_stop_at_position_in`](Self::find_stop_at_position_in).
    /// Sub-micrometre — small enough that no two distinct floors should
    /// land within it, large enough to absorb floating-point noise from
    /// trapezoidal-velocity arrival math.
    pub const STOP_POSITION_EPSILON: f64 = 1e-6;

    /// 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. An [`ExtKey`] is required for serialization roundtrips.
    /// Extension components are automatically cleaned up on `despawn()`.
    ///
    /// ```
    /// use elevator_core::world::{ExtKey, 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 }, ExtKey::from_type_name());
    /// ```
    pub fn insert_ext<T: 'static + Send + Sync + serde::Serialize + serde::de::DeserializeOwned>(
        &mut self,
        id: EntityId,
        value: T,
        key: ExtKey<T>,
    ) {
        let type_id = TypeId::of::<T>();
        Self::assert_ext_name_unique(&self.ext_names, type_id, key.name());
        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, key.name().to_owned());
    }

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

    /// Shared reference to a custom component for an entity.
    ///
    /// Zero-copy alternative to [`ext`](Self::ext): prefer this when
    /// `T` is large or expensive to clone, or when the caller only needs a
    /// borrow. Unlike `ext`, `T` does not need to implement `Clone`.
    #[must_use]
    pub fn ext_ref<T: 'static + Send + Sync>(&self, id: EntityId) -> Option<&T> {
        self.ext_map::<T>()?.get(id)
    }

    /// Mutable reference to a custom component for an entity.
    pub fn 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. `BTreeMap` for
    /// deterministic snapshot bytes across processes (#254).
    pub(crate) fn serialize_extensions(&self) -> BTreeMap<String, BTreeMap<EntityId, String>> {
        let mut result = BTreeMap::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: &BTreeMap<String, BTreeMap<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);
            }
        }
    }

    /// Return names from `snapshot_names` that have no registered extension type.
    pub(crate) fn unregistered_ext_names<'a>(
        &self,
        snapshot_names: impl Iterator<Item = &'a String>,
    ) -> Vec<String> {
        let registered: std::collections::HashSet<&str> =
            self.ext_names.values().map(String::as_str).collect();
        snapshot_names
            .filter(|name| !registered.contains(name.as_str()))
            .cloned()
            .collect()
    }

    /// 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. Returns the key for convenience.
    pub fn register_ext<
        T: 'static + Send + Sync + serde::Serialize + serde::de::DeserializeOwned,
    >(
        &mut self,
        key: ExtKey<T>,
    ) -> ExtKey<T> {
        let type_id = TypeId::of::<T>();
        Self::assert_ext_name_unique(&self.ext_names, type_id, key.name());
        self.extensions
            .entry(type_id)
            .or_insert_with(|| Box::new(SecondaryMap::<EntityId, T>::new()));
        self.ext_names.insert(type_id, key.name().to_owned());
        key
    }

    /// Panic if `name` is already registered to a different `TypeId`.
    ///
    /// Two extension types sharing one `ExtKey` name silently corrupts
    /// snapshot serde: `serialize_extensions` collapses both types' data
    /// into one slot in the result map, and `deserialize_extensions`
    /// routes the data to whichever `TypeId` `HashMap::iter().find` returns
    /// first — a non-deterministic choice. Failing fast here prevents
    /// the corruption from ever being written.
    ///
    /// Panic chosen over `Result` because [`register_ext`](Self::register_ext)
    /// and [`insert_ext`](Self::insert_ext) are non-fallible by design and
    /// changing their signatures would break every consumer. Name collisions
    /// are programmer errors discoverable at startup, not runtime conditions
    /// to recover from.
    #[allow(clippy::panic)]
    fn assert_ext_name_unique(ext_names: &HashMap<TypeId, String>, type_id: TypeId, name: &str) {
        if let Some((existing_tid, _)) = ext_names
            .iter()
            .find(|(tid, n)| **tid != type_id && n.as_str() == name)
        {
            panic!(
                "ExtKey name {name:?} is already registered to a different type \
                 ({existing_tid:?}); each extension type needs a unique key name. \
                 If renaming is impractical, use ExtKey::from_type_name() so the \
                 name embeds the fully-qualified Rust type name."
            );
        }
    }

    // ── 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::components::{Position, Rider};
    /// use elevator_core::prelude::*;
    ///
    /// let mut sim = SimulationBuilder::demo().build().unwrap();
    /// sim.spawn_rider(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
    ///
    /// ```no_run
    /// # use elevator_core::prelude::*;
    /// # use serde::{Serialize, Deserialize};
    /// # #[derive(Clone, Serialize, Deserialize)] struct VipTag { level: u32 }
    /// # fn run(world: &mut World) {
    /// 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())
    }
}