Skip to main content

elevator_core/
events.rs

1//! Simulation event bus and typed event channels.
2
3use crate::components::CallDirection;
4use crate::entity::EntityId;
5use crate::error::{RejectionContext, RejectionReason};
6use crate::ids::GroupId;
7use ordered_float::OrderedFloat;
8use serde::{Deserialize, Serialize};
9
10/// Events emitted by the simulation during ticks.
11///
12/// All entity references use `EntityId`. Games can look up additional
13/// component data on the referenced entity if needed.
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15#[non_exhaustive]
16pub enum Event {
17    // -- Elevator events --
18    /// An elevator departed from a stop.
19    ElevatorDeparted {
20        /// The elevator that departed.
21        elevator: EntityId,
22        /// The stop it departed from.
23        from_stop: EntityId,
24        /// The tick when departure occurred.
25        tick: u64,
26    },
27    /// An elevator arrived at a stop.
28    ElevatorArrived {
29        /// The elevator that arrived.
30        elevator: EntityId,
31        /// The stop it arrived at.
32        at_stop: EntityId,
33        /// The tick when arrival occurred.
34        tick: u64,
35    },
36    /// An elevator's doors finished opening.
37    DoorOpened {
38        /// The elevator whose doors opened.
39        elevator: EntityId,
40        /// The tick when the doors opened.
41        tick: u64,
42    },
43    /// An elevator's doors finished closing.
44    DoorClosed {
45        /// The elevator whose doors closed.
46        elevator: EntityId,
47        /// The tick when the doors closed.
48        tick: u64,
49    },
50    /// Emitted when an elevator passes a stop without stopping.
51    /// Games/dispatch can use this to decide whether to add stops mid-travel.
52    PassingFloor {
53        /// The elevator passing by.
54        elevator: EntityId,
55        /// The stop being passed.
56        stop: EntityId,
57        /// Direction: true = moving up, false = moving down.
58        moving_up: bool,
59        /// The tick when the pass occurred.
60        tick: u64,
61    },
62    /// An in-flight movement was aborted by
63    /// [`Simulation::abort_movement`](crate::sim::Simulation::abort_movement).
64    ///
65    /// The car brakes along its normal deceleration profile, re-targets to
66    /// the nearest reachable stop, and arrives there without opening doors
67    /// (onboard riders stay aboard). The pending destination queue is also
68    /// cleared as part of the abort.
69    ///
70    /// Emitted at abort time — the car is still in flight, decelerating
71    /// toward `brake_target`. A normal [`Event::ElevatorArrived`] fires
72    /// once the car actually reaches the stop.
73    MovementAborted {
74        /// The elevator whose trip was aborted.
75        elevator: EntityId,
76        /// The stop the car will brake to and park at.
77        brake_target: EntityId,
78        /// The tick when the abort was requested.
79        tick: u64,
80    },
81
82    // -- Rider events (unified: passengers, cargo, any rideable entity) --
83    /// A new rider appeared at a stop and wants to travel.
84    RiderSpawned {
85        /// The spawned rider entity.
86        rider: EntityId,
87        /// The stop where the rider appeared.
88        origin: EntityId,
89        /// The stop the rider wants to reach.
90        destination: EntityId,
91        /// Opaque consumer tag attached to the rider at emit time. `0`
92        /// when the rider is untagged. See [`Simulation::set_rider_tag`]
93        /// for the back-pointer pattern this enables.
94        ///
95        /// [`Simulation::set_rider_tag`]: crate::sim::Simulation::set_rider_tag
96        #[serde(default)]
97        tag: u64,
98        /// The tick when the rider spawned.
99        tick: u64,
100    },
101    /// A rider boarded an elevator.
102    RiderBoarded {
103        /// The rider that boarded.
104        rider: EntityId,
105        /// The elevator the rider boarded.
106        elevator: EntityId,
107        /// Opaque consumer tag attached to the rider at emit time. `0`
108        /// when the rider is untagged.
109        #[serde(default)]
110        tag: u64,
111        /// The tick when boarding occurred.
112        tick: u64,
113    },
114    /// A rider exited an elevator at a stop.
115    #[serde(alias = "RiderAlighted")]
116    RiderExited {
117        /// The rider that exited.
118        rider: EntityId,
119        /// The elevator the rider exited.
120        elevator: EntityId,
121        /// The stop where the rider exited.
122        stop: EntityId,
123        /// Opaque consumer tag attached to the rider at emit time. `0`
124        /// when the rider is untagged. Sampled before the rider is freed
125        /// so consumers can correlate the exit with external state even
126        /// after the [`RiderId`](crate::entity::RiderId) becomes stale.
127        #[serde(default)]
128        tag: u64,
129        /// The tick when exiting occurred.
130        tick: u64,
131    },
132    /// A rider was rejected from boarding (e.g., over capacity).
133    RiderRejected {
134        /// The rider that was rejected.
135        rider: EntityId,
136        /// The elevator that rejected the rider.
137        elevator: EntityId,
138        /// The reason for rejection.
139        reason: RejectionReason,
140        /// Additional numeric context for the rejection.
141        context: Option<RejectionContext>,
142        /// Opaque consumer tag attached to the rider at emit time. `0`
143        /// when the rider is untagged.
144        #[serde(default)]
145        tag: u64,
146        /// The tick when rejection occurred.
147        tick: u64,
148    },
149    /// A rider gave up waiting and left the stop.
150    RiderAbandoned {
151        /// The rider that abandoned.
152        rider: EntityId,
153        /// The stop the rider left.
154        stop: EntityId,
155        /// Opaque consumer tag attached to the rider at emit time. `0`
156        /// when the rider is untagged. Sampled before the rider is freed.
157        #[serde(default)]
158        tag: u64,
159        /// The tick when abandonment occurred.
160        tick: u64,
161    },
162
163    /// A rider was ejected from an elevator (due to disable or despawn).
164    ///
165    /// The rider is moved to `Waiting` phase at the nearest stop.
166    RiderEjected {
167        /// The rider that was ejected.
168        rider: EntityId,
169        /// The elevator the rider was ejected from.
170        elevator: EntityId,
171        /// The stop the rider was placed at.
172        stop: EntityId,
173        /// Opaque consumer tag attached to the rider at emit time. `0`
174        /// when the rider is untagged.
175        #[serde(default)]
176        tag: u64,
177        /// The tick when ejection occurred.
178        tick: u64,
179    },
180
181    // -- Dispatch events --
182    /// An elevator was assigned to serve a stop by the dispatcher.
183    ElevatorAssigned {
184        /// The elevator that was assigned.
185        elevator: EntityId,
186        /// The stop it was assigned to serve.
187        stop: EntityId,
188        /// The tick when the assignment occurred.
189        tick: u64,
190    },
191
192    // -- Topology lifecycle events --
193    /// A new stop was added to the simulation.
194    StopAdded {
195        /// The new stop entity.
196        stop: EntityId,
197        /// The line the stop was added to.
198        line: EntityId,
199        /// The group the stop was added to.
200        group: GroupId,
201        /// The tick when the stop was added.
202        tick: u64,
203    },
204    /// A new elevator was added to the simulation.
205    ElevatorAdded {
206        /// The new elevator entity.
207        elevator: EntityId,
208        /// The line the elevator was added to.
209        line: EntityId,
210        /// The group the elevator was added to.
211        group: GroupId,
212        /// The tick when the elevator was added.
213        tick: u64,
214    },
215    /// An entity was disabled.
216    EntityDisabled {
217        /// The entity that was disabled.
218        entity: EntityId,
219        /// The tick when it was disabled.
220        tick: u64,
221    },
222    /// An entity was re-enabled.
223    EntityEnabled {
224        /// The entity that was re-enabled.
225        entity: EntityId,
226        /// The tick when it was enabled.
227        tick: u64,
228    },
229    /// A rider's route was invalidated due to topology change.
230    ///
231    /// Emitted when a stop on a rider's route is disabled or removed.
232    /// If no alternative is found, the rider will abandon after a grace period.
233    RouteInvalidated {
234        /// The affected rider.
235        rider: EntityId,
236        /// The stop that caused the invalidation.
237        affected_stop: EntityId,
238        /// Why the route was invalidated.
239        reason: RouteInvalidReason,
240        /// Opaque consumer tag attached to the rider at emit time. `0`
241        /// when the rider is untagged.
242        #[serde(default)]
243        tag: u64,
244        /// The tick when invalidation occurred.
245        tick: u64,
246    },
247    /// A rider was manually rerouted via `sim.reroute()`.
248    RiderRerouted {
249        /// The rerouted rider.
250        rider: EntityId,
251        /// The new destination stop.
252        new_destination: EntityId,
253        /// Opaque consumer tag attached to the rider at emit time. `0`
254        /// when the rider is untagged.
255        #[serde(default)]
256        tag: u64,
257        /// The tick when rerouting occurred.
258        tick: u64,
259    },
260
261    /// A rider settled at a stop, becoming a resident.
262    RiderSettled {
263        /// The rider that settled.
264        rider: EntityId,
265        /// The stop where the rider settled.
266        stop: EntityId,
267        /// Opaque consumer tag attached to the rider at emit time. `0`
268        /// when the rider is untagged.
269        #[serde(default)]
270        tag: u64,
271        /// The tick when settlement occurred.
272        tick: u64,
273    },
274    /// A rider was removed from the simulation.
275    RiderDespawned {
276        /// The rider that was removed.
277        rider: EntityId,
278        /// Opaque consumer tag attached to the rider at emit time. `0`
279        /// when the rider is untagged. Sampled before the rider is freed.
280        #[serde(default)]
281        tag: u64,
282        /// The tick when despawn occurred.
283        tick: u64,
284    },
285
286    // -- Line lifecycle events --
287    /// A line was added to the simulation.
288    LineAdded {
289        /// The new line entity.
290        line: EntityId,
291        /// The group the line was added to.
292        group: GroupId,
293        /// The tick when the line was added.
294        tick: u64,
295    },
296    /// A line was removed from the simulation.
297    LineRemoved {
298        /// The removed line entity.
299        line: EntityId,
300        /// The group the line belonged to.
301        group: GroupId,
302        /// The tick when the line was removed.
303        tick: u64,
304    },
305    /// A line was reassigned to a different group.
306    LineReassigned {
307        /// The line entity that was reassigned.
308        line: EntityId,
309        /// The group the line was previously in.
310        old_group: GroupId,
311        /// The group the line was moved to.
312        new_group: GroupId,
313        /// The tick when reassignment occurred.
314        tick: u64,
315    },
316    /// An elevator was reassigned to a different line.
317    ElevatorReassigned {
318        /// The elevator that was reassigned.
319        elevator: EntityId,
320        /// The line the elevator was previously on.
321        old_line: EntityId,
322        /// The line the elevator was moved to.
323        new_line: EntityId,
324        /// The tick when reassignment occurred.
325        tick: u64,
326    },
327
328    // -- Repositioning events --
329    /// An elevator is being repositioned to improve coverage.
330    ///
331    /// Emitted when an idle elevator begins moving to a new position
332    /// as decided by the [`RepositionStrategy`](crate::dispatch::RepositionStrategy).
333    ElevatorRepositioning {
334        /// The elevator being repositioned.
335        elevator: EntityId,
336        /// The stop it is being sent to.
337        to_stop: EntityId,
338        /// The tick when repositioning began.
339        tick: u64,
340    },
341    /// An elevator completed repositioning and arrived at its target.
342    ///
343    /// Note: this is detected by the movement system — the elevator
344    /// arrives just like any other movement. Games can distinguish
345    /// repositioning arrivals from dispatch arrivals by tracking
346    /// which elevators received `ElevatorRepositioning` events.
347    ElevatorRepositioned {
348        /// The elevator that completed repositioning.
349        elevator: EntityId,
350        /// The stop it arrived at.
351        at_stop: EntityId,
352        /// The tick when it arrived.
353        tick: u64,
354    },
355
356    /// An elevator was recalled to a specific stop via
357    /// [`Simulation::recall_to`](crate::sim::Simulation::recall_to).
358    ///
359    /// The car's destination queue has been replaced with the recall
360    /// target. If the car was mid-flight, it continues to its current
361    /// target, then proceeds to the recall stop. If idle, it departs
362    /// on the next tick.
363    ElevatorRecalled {
364        /// The elevator that was recalled.
365        elevator: EntityId,
366        /// The stop the elevator is being sent to.
367        to_stop: EntityId,
368        /// The tick when the recall was issued.
369        tick: u64,
370    },
371
372    /// An elevator's service mode was changed.
373    ServiceModeChanged {
374        /// The elevator whose mode changed.
375        elevator: EntityId,
376        /// The previous service mode.
377        from: crate::components::ServiceMode,
378        /// The new service mode.
379        to: crate::components::ServiceMode,
380        /// The tick when the change occurred.
381        tick: u64,
382    },
383
384    // -- Observability events --
385    /// Energy consumed/regenerated by an elevator this tick.
386    ///
387    /// Requires the `energy` feature.
388    #[cfg(feature = "energy")]
389    EnergyConsumed {
390        /// The elevator that consumed energy.
391        elevator: EntityId,
392        /// Energy consumed this tick.
393        consumed: OrderedFloat<f64>,
394        /// Energy regenerated this tick.
395        regenerated: OrderedFloat<f64>,
396        /// The tick when energy was recorded.
397        tick: u64,
398    },
399
400    /// An elevator's load changed (rider boarded or exited).
401    ///
402    /// Emitted immediately after [`RiderBoarded`](Self::RiderBoarded) or
403    /// [`RiderExited`](Self::RiderExited). Useful for real-time capacity
404    /// bar displays in game UIs.
405    ///
406    /// Load values use [`OrderedFloat`] for
407    /// `Eq` compatibility. Dereference to get the inner `f64`:
408    ///
409    /// ```rust,no_run
410    /// use elevator_core::events::Event;
411    /// # fn handle(event: Event) {
412    /// if let Event::CapacityChanged { current_load, capacity, .. } = event {
413    ///     let pct = *current_load / *capacity * 100.0;
414    ///     println!("Elevator at {pct:.0}% capacity");
415    /// }
416    /// # }
417    /// ```
418    CapacityChanged {
419        /// The elevator whose load changed.
420        elevator: EntityId,
421        /// Current total weight aboard after the change.
422        current_load: OrderedFloat<f64>,
423        /// Maximum weight capacity of the elevator.
424        capacity: OrderedFloat<f64>,
425        /// The tick when the change occurred.
426        tick: u64,
427    },
428
429    /// An elevator became idle (no more assignments or repositioning).
430    ElevatorIdle {
431        /// The elevator that became idle.
432        elevator: EntityId,
433        /// The stop where it became idle (if at a stop).
434        at_stop: Option<EntityId>,
435        /// The tick when it became idle.
436        tick: u64,
437    },
438
439    /// An elevator's direction indicator lamps changed.
440    ///
441    /// Emitted by the dispatch phase when the pair
442    /// `(going_up, going_down)` transitions to a new value — e.g. when
443    /// a car is assigned a target above it (up-only), below it (down-only),
444    /// or returns to idle (both lamps lit).
445    ///
446    /// Games can use this for UI (lighting up-arrow / down-arrow indicators
447    /// on a car) without polling the elevator each tick.
448    DirectionIndicatorChanged {
449        /// The elevator whose indicator lamps changed.
450        elevator: EntityId,
451        /// New state of the up-direction lamp.
452        going_up: bool,
453        /// New state of the down-direction lamp.
454        going_down: bool,
455        /// The tick when the change occurred.
456        tick: u64,
457    },
458
459    /// An elevator was permanently removed from the simulation.
460    ///
461    /// Distinct from [`Event::EntityDisabled`] — a disabled elevator can be
462    /// re-enabled, but a removed elevator is despawned.
463    ElevatorRemoved {
464        /// The elevator that was removed.
465        elevator: EntityId,
466        /// The line it belonged to.
467        line: EntityId,
468        /// The group it belonged to.
469        group: GroupId,
470        /// The tick when removal occurred.
471        tick: u64,
472    },
473
474    /// A stop was queued as a destination for an elevator.
475    ///
476    /// Emitted by [`Simulation::push_destination`](crate::sim::Simulation::push_destination),
477    /// [`Simulation::push_destination_front`](crate::sim::Simulation::push_destination_front),
478    /// and the built-in dispatch phase whenever it actually appends a stop
479    /// to an elevator's [`DestinationQueue`](crate::components::DestinationQueue).
480    /// Adjacent-duplicate pushes (that are deduplicated) do not emit.
481    DestinationQueued {
482        /// The elevator whose queue was updated.
483        elevator: EntityId,
484        /// The stop that was queued.
485        stop: EntityId,
486        /// The tick when the push occurred.
487        tick: u64,
488    },
489
490    /// A stop was permanently removed from the simulation.
491    ///
492    /// Distinct from [`Event::EntityDisabled`] — a disabled stop can be
493    /// re-enabled, but a removed stop is despawned.
494    StopRemoved {
495        /// The stop that was removed.
496        stop: EntityId,
497        /// The tick when removal occurred.
498        tick: u64,
499    },
500
501    /// A manual door-control command was received and either applied
502    /// immediately or stored for later.
503    ///
504    /// Emitted by
505    /// [`Simulation::open_door`](crate::sim::Simulation::open_door),
506    /// [`Simulation::close_door`](crate::sim::Simulation::close_door),
507    /// [`Simulation::hold_door`](crate::sim::Simulation::hold_door),
508    /// and [`Simulation::cancel_door_hold`](crate::sim::Simulation::cancel_door_hold)
509    /// when the command is accepted. Paired with
510    /// [`Event::DoorCommandApplied`] when the command eventually takes effect.
511    DoorCommandQueued {
512        /// The elevator targeted by the command.
513        elevator: EntityId,
514        /// The command that was queued.
515        command: crate::door::DoorCommand,
516        /// The tick when the command was submitted.
517        tick: u64,
518    },
519    /// A queued door-control command actually took effect — doors began
520    /// opening/closing or a hold was applied.
521    DoorCommandApplied {
522        /// The elevator the command applied to.
523        elevator: EntityId,
524        /// The command that was applied.
525        command: crate::door::DoorCommand,
526        /// The tick when the command was applied.
527        tick: u64,
528    },
529
530    /// An elevator parameter was mutated at runtime via one of the
531    /// `Simulation::set_*` upgrade setters (e.g. buying a speed upgrade
532    /// in an RPG, or a scripted event changing capacity mid-game).
533    ///
534    /// Emitted immediately when the setter succeeds. Games can use this
535    /// to trigger score popups, SFX, or UI updates.
536    ElevatorUpgraded {
537        /// The elevator whose parameter changed.
538        elevator: EntityId,
539        /// Which field was changed.
540        field: UpgradeField,
541        /// Previous value of the field.
542        old: UpgradeValue,
543        /// New value of the field.
544        new: UpgradeValue,
545        /// The tick when the upgrade was applied.
546        tick: u64,
547    },
548
549    /// A velocity command was submitted to an elevator running in
550    /// [`ServiceMode::Manual`](crate::components::ServiceMode::Manual).
551    ///
552    /// Emitted by
553    /// [`Simulation::set_target_velocity`](crate::sim::Simulation::set_target_velocity)
554    /// and [`Simulation::emergency_stop`](crate::sim::Simulation::emergency_stop).
555    ManualVelocityCommanded {
556        /// The elevator targeted by the command.
557        elevator: EntityId,
558        /// The new target velocity (clamped to `[-max_speed, max_speed]`),
559        /// or `None` when the command clears the target.
560        target_velocity: Option<ordered_float::OrderedFloat<f64>>,
561        /// The tick when the command was submitted.
562        tick: u64,
563    },
564
565    // -- Hall & car call events --
566    /// A hall button was pressed. Fires immediately, before any ack
567    /// latency delay. Games use this for button-light animations or SFX.
568    HallButtonPressed {
569        /// Stop where the press occurred.
570        stop: EntityId,
571        /// Direction the button requests service in.
572        direction: CallDirection,
573        /// Tick of the press.
574        tick: u64,
575    },
576    /// A hall call has elapsed its ack latency and is now visible to
577    /// dispatch. Useful for UI confirmations ("your call has been
578    /// received"). In groups with `ack_latency_ticks = 0` this fires on
579    /// the same tick as `HallButtonPressed`.
580    HallCallAcknowledged {
581        /// Stop the call was placed at.
582        stop: EntityId,
583        /// Direction of the call.
584        direction: CallDirection,
585        /// Tick acknowledgement completed.
586        tick: u64,
587    },
588    /// A hall call was cleared when an assigned car arrived at the stop
589    /// with matching direction indicators. Corresponds to the real-world
590    /// button-light turning off.
591    HallCallCleared {
592        /// Stop whose call was cleared.
593        stop: EntityId,
594        /// Direction of the cleared call.
595        direction: CallDirection,
596        /// Car that cleared the call by arriving.
597        car: EntityId,
598        /// Tick the call was cleared.
599        tick: u64,
600    },
601    /// A rider inside a car pressed a floor button (Classic mode only).
602    /// In Destination mode riders reveal destinations at the hall
603    /// kiosk, so no car buttons are pressed.
604    CarButtonPressed {
605        /// Elevator the button was pressed inside.
606        car: EntityId,
607        /// Floor the rider requested.
608        floor: EntityId,
609        /// Rider who pressed the button. `None` when the press is
610        /// synthetic — e.g. issued via
611        /// [`Simulation::press_car_button`](crate::sim::Simulation::press_car_button)
612        /// for scripted events, player input, or cutscene cues with no
613        /// associated rider entity.
614        rider: Option<EntityId>,
615        /// Opaque consumer tag attached to the pressing rider, mirroring
616        /// the optionality of [`rider`](Self::CarButtonPressed::rider).
617        /// `Some(0)` for a present-but-untagged rider; `None` for a
618        /// synthetic press with no rider entity.
619        #[serde(default)]
620        tag: Option<u64>,
621        /// Tick of the press.
622        tick: u64,
623    },
624    /// A rider skipped boarding a car they considered too crowded
625    /// (their preferences filtered the car out). The rider remains
626    /// Waiting and may board a later car.
627    #[serde(alias = "RiderBalked")]
628    RiderSkipped {
629        /// Rider who skipped.
630        rider: EntityId,
631        /// Elevator they declined to board.
632        elevator: EntityId,
633        /// Stop where the skip happened.
634        at_stop: EntityId,
635        /// Opaque consumer tag attached to the rider at emit time. `0`
636        /// when the rider is untagged.
637        #[serde(default)]
638        tag: u64,
639        /// Tick of the skip.
640        tick: u64,
641    },
642    /// A snapshot restore encountered an entity reference that could not
643    /// be remapped. The original (now-invalid) ID was kept. This signals
644    /// a corrupted or hand-edited snapshot.
645    SnapshotDanglingReference {
646        /// The entity ID from the snapshot that had no mapping.
647        stale_id: EntityId,
648        /// Tick from the snapshot at restore time.
649        tick: u64,
650    },
651    /// A snapshot restore could not re-instantiate the reposition strategy
652    /// for a group (e.g. custom strategy not registered). Idle elevator
653    /// positioning will not work for this group until the game calls
654    /// [`Simulation::set_reposition`](crate::sim::Simulation::set_reposition).
655    RepositionStrategyNotRestored {
656        /// The group whose strategy was lost.
657        group: GroupId,
658    },
659    /// A snapshot restore instantiated the dispatch strategy for a
660    /// group but couldn't replay its tunable configuration (the
661    /// serialized form in the snapshot didn't parse). The strategy
662    /// runs with its default weights — identical to a legacy snapshot
663    /// that never carried dispatch config at all.
664    DispatchConfigNotRestored {
665        /// The group whose dispatcher ran with defaults.
666        group: GroupId,
667        /// The parse error from
668        /// [`DispatchStrategy::restore_config`](crate::dispatch::DispatchStrategy::restore_config).
669        reason: String,
670    },
671    /// A stop was removed while resident riders were present.
672    /// The game must relocate or despawn these riders.
673    ResidentsAtRemovedStop {
674        /// The removed stop.
675        stop: EntityId,
676        /// Riders that were resident at the stop.
677        residents: Vec<EntityId>,
678    },
679}
680
681/// Identifies which elevator parameter was changed in an
682/// [`Event::ElevatorUpgraded`].
683#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
684#[non_exhaustive]
685pub enum UpgradeField {
686    /// Maximum travel speed (distance/tick).
687    MaxSpeed,
688    /// Acceleration rate (distance/tick^2).
689    Acceleration,
690    /// Deceleration rate (distance/tick^2).
691    Deceleration,
692    /// Maximum weight the car can carry.
693    WeightCapacity,
694    /// Ticks for a door open/close transition.
695    DoorTransitionTicks,
696    /// Ticks the door stays fully open.
697    DoorOpenTicks,
698}
699
700impl std::fmt::Display for UpgradeField {
701    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
702        match self {
703            Self::MaxSpeed => write!(f, "max_speed"),
704            Self::Acceleration => write!(f, "acceleration"),
705            Self::Deceleration => write!(f, "deceleration"),
706            Self::WeightCapacity => write!(f, "weight_capacity"),
707            Self::DoorTransitionTicks => write!(f, "door_transition_ticks"),
708            Self::DoorOpenTicks => write!(f, "door_open_ticks"),
709        }
710    }
711}
712
713/// Old-or-new value carried by [`Event::ElevatorUpgraded`].
714///
715/// Uses [`OrderedFloat`] for the float variant so the event enum
716/// remains `Eq`-comparable alongside the other observability events.
717#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
718#[non_exhaustive]
719pub enum UpgradeValue {
720    /// A floating-point parameter value (speed, accel, decel, capacity).
721    Float(OrderedFloat<f64>),
722    /// An integral tick-count parameter value (door timings).
723    Ticks(u32),
724}
725
726impl UpgradeValue {
727    /// Construct a float-valued upgrade payload.
728    #[must_use]
729    pub const fn float(v: f64) -> Self {
730        Self::Float(OrderedFloat(v))
731    }
732
733    /// Construct a tick-valued upgrade payload.
734    #[must_use]
735    pub const fn ticks(v: u32) -> Self {
736        Self::Ticks(v)
737    }
738}
739
740impl std::fmt::Display for UpgradeValue {
741    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
742        match self {
743            Self::Float(v) => write!(f, "{}", **v),
744            Self::Ticks(v) => write!(f, "{v}"),
745        }
746    }
747}
748
749/// Reason a rider's route was invalidated.
750#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
751#[non_exhaustive]
752pub enum RouteInvalidReason {
753    /// A stop on the route was disabled (transient — the stop entity
754    /// still exists).
755    StopDisabled,
756    /// Reserved. No code currently emits this — the "no alternative
757    /// found" signal is conveyed by the accompanying `RiderAbandoned`
758    /// event so this enum stays dedicated to *why* the route was
759    /// invalidated rather than *what the rerouter could do about it*.
760    NoAlternative,
761    /// A stop on the route was permanently removed (despawned).
762    /// Distinguishes the removal path from a transient disable.
763    StopRemoved,
764}
765
766/// Coarse-grained classification of an [`Event`].
767///
768/// Exposes the same grouping that the `Event` variants are already
769/// commented under, so consumers can filter a drained event stream with
770/// one match arm per category rather than enumerating ~25 variants.
771#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
772#[non_exhaustive]
773pub enum EventCategory {
774    /// Elevator motion, arrival/departure, and door state.
775    Elevator,
776    /// Rider lifecycle: spawn, board, exit, reject, abandon, despawn, settle, reroute.
777    Rider,
778    /// Dispatch decisions and bookkeeping.
779    Dispatch,
780    /// Runtime topology mutations (entities/lines/groups added, removed, reassigned).
781    Topology,
782    /// Idle-elevator repositioning activity.
783    Reposition,
784    /// Direction indicator lamp state changes.
785    Direction,
786    /// Observability and misc signals (capacity, service mode, energy, etc.).
787    Observability,
788}
789
790impl Event {
791    /// Classify this event into a coarse-grained [`EventCategory`].
792    ///
793    /// Useful when a consumer only cares about, say, rider activity and
794    /// wants to skip elevator-motion and topology chatter without
795    /// enumerating every variant. The exhaustive match inside guarantees
796    /// the method stays in sync when new variants are added.
797    #[must_use]
798    pub const fn category(&self) -> EventCategory {
799        match self {
800            Self::ElevatorDeparted { .. }
801            | Self::ElevatorArrived { .. }
802            | Self::DoorOpened { .. }
803            | Self::DoorClosed { .. }
804            | Self::DoorCommandQueued { .. }
805            | Self::DoorCommandApplied { .. }
806            | Self::PassingFloor { .. }
807            | Self::MovementAborted { .. }
808            | Self::ElevatorIdle { .. } => EventCategory::Elevator,
809            Self::RiderSpawned { .. }
810            | Self::RiderBoarded { .. }
811            | Self::RiderExited { .. }
812            | Self::RiderRejected { .. }
813            | Self::RiderAbandoned { .. }
814            | Self::RiderEjected { .. }
815            | Self::RouteInvalidated { .. }
816            | Self::RiderRerouted { .. }
817            | Self::RiderSettled { .. }
818            | Self::RiderDespawned { .. } => EventCategory::Rider,
819            Self::ElevatorAssigned { .. } | Self::DestinationQueued { .. } => {
820                EventCategory::Dispatch
821            }
822            Self::StopAdded { .. }
823            | Self::StopRemoved { .. }
824            | Self::ElevatorAdded { .. }
825            | Self::ElevatorRemoved { .. }
826            | Self::EntityDisabled { .. }
827            | Self::EntityEnabled { .. }
828            | Self::LineAdded { .. }
829            | Self::LineRemoved { .. }
830            | Self::LineReassigned { .. }
831            | Self::ElevatorReassigned { .. } => EventCategory::Topology,
832            Self::ElevatorRepositioning { .. } | Self::ElevatorRepositioned { .. } => {
833                EventCategory::Reposition
834            }
835            Self::ElevatorRecalled { .. } => EventCategory::Elevator,
836            Self::DirectionIndicatorChanged { .. } => EventCategory::Direction,
837            Self::ServiceModeChanged { .. }
838            | Self::CapacityChanged { .. }
839            | Self::ElevatorUpgraded { .. }
840            | Self::ManualVelocityCommanded { .. } => EventCategory::Observability,
841            #[cfg(feature = "energy")]
842            Self::EnergyConsumed { .. } => EventCategory::Observability,
843            Self::HallButtonPressed { .. }
844            | Self::HallCallAcknowledged { .. }
845            | Self::HallCallCleared { .. }
846            | Self::CarButtonPressed { .. } => EventCategory::Dispatch,
847            Self::RiderSkipped { .. } => EventCategory::Rider,
848            Self::SnapshotDanglingReference { .. }
849            | Self::RepositionStrategyNotRestored { .. }
850            | Self::DispatchConfigNotRestored { .. } => EventCategory::Observability,
851            Self::ResidentsAtRemovedStop { .. } => EventCategory::Topology,
852        }
853    }
854}
855
856/// Collects simulation events for consumers to drain.
857#[derive(Debug, Default)]
858pub struct EventBus {
859    /// The pending events not yet consumed.
860    events: Vec<Event>,
861}
862
863impl EventBus {
864    /// Pushes a new event onto the bus.
865    pub fn emit(&mut self, event: Event) {
866        self.events.push(event);
867    }
868
869    /// Returns and clears all pending events.
870    pub fn drain(&mut self) -> Vec<Event> {
871        std::mem::take(&mut self.events)
872    }
873
874    /// Returns a slice of all pending events without clearing them.
875    #[must_use]
876    pub fn peek(&self) -> &[Event] {
877        &self.events
878    }
879}
880
881/// A typed event channel for game-specific events.
882///
883/// Games insert this as a global resource on `World`:
884///
885/// ```
886/// use elevator_core::world::World;
887/// use elevator_core::events::EventChannel;
888///
889/// #[derive(Debug)]
890/// enum MyGameEvent { Foo, Bar }
891///
892/// let mut world = World::new();
893/// world.insert_resource(EventChannel::<MyGameEvent>::new());
894/// // Later:
895/// world.resource_mut::<EventChannel<MyGameEvent>>().unwrap().emit(MyGameEvent::Foo);
896/// ```
897#[derive(Debug)]
898pub struct EventChannel<T> {
899    /// Pending events not yet consumed.
900    events: Vec<T>,
901}
902
903impl<T> EventChannel<T> {
904    /// Create an empty event channel.
905    #[must_use]
906    pub const fn new() -> Self {
907        Self { events: Vec::new() }
908    }
909
910    /// Emit an event into the channel.
911    pub fn emit(&mut self, event: T) {
912        self.events.push(event);
913    }
914
915    /// Drain and return all pending events.
916    pub fn drain(&mut self) -> Vec<T> {
917        std::mem::take(&mut self.events)
918    }
919
920    /// Peek at pending events without clearing.
921    #[must_use]
922    pub fn peek(&self) -> &[T] {
923        &self.events
924    }
925
926    /// Check if the channel has no pending events.
927    #[must_use]
928    pub const fn is_empty(&self) -> bool {
929        self.events.is_empty()
930    }
931
932    /// Number of pending events.
933    #[must_use]
934    pub const fn len(&self) -> usize {
935        self.events.len()
936    }
937}
938
939impl<T> Default for EventChannel<T> {
940    fn default() -> Self {
941        Self::new()
942    }
943}