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
//! Phase 5: board and exit riders at stops with open doors.
use crate::components::{ElevatorPhase, Line, RiderPhase, Route, TransportMode};
use crate::entity::EntityId;
use crate::error::{RejectionContext, RejectionReason};
use crate::events::{Event, EventBus};
use crate::ids::GroupId;
use crate::rider_index::RiderIndex;
use crate::world::World;
use ordered_float::OrderedFloat;
use super::PhaseContext;
/// Intermediate action collected in the read-only pass, applied in the mutation pass.
enum LoadAction {
/// A rider exits the elevator at a stop.
Exit {
/// Rider entity leaving.
rider: EntityId,
/// Elevator entity being exited.
elevator: EntityId,
/// Stop entity where exiting occurs.
stop: EntityId,
},
/// A rider enters the elevator.
Board {
/// Rider entity boarding.
rider: EntityId,
/// Elevator entity being boarded.
elevator: EntityId,
/// Weight the rider adds.
weight: f64,
},
/// A rider is rejected from boarding.
Reject {
/// Rider entity rejected.
rider: EntityId,
/// Elevator entity that rejected the rider.
elevator: EntityId,
/// Why the rider was rejected.
reason: RejectionReason,
/// Numeric details of the rejection.
context: Option<RejectionContext>,
},
}
/// Read-only pass: inspect world state and collect one `LoadAction` per elevator.
#[allow(clippy::too_many_lines)]
fn collect_actions(world: &World, elevator_ids: &[EntityId]) -> Vec<LoadAction> {
let mut actions: Vec<LoadAction> = Vec::new();
for &eid in elevator_ids {
if world.is_disabled(eid) {
continue;
}
let Some(car) = world.elevator(eid) else {
continue;
};
if car.phase != ElevatorPhase::Loading {
continue;
}
let pos = world.position(eid).map_or(0.0, |p| p.value);
let Some(current_stop) = world.find_stop_at_position(pos) else {
continue;
};
// Try to exit one rider whose route destination matches the current stop.
let exit_rider = car
.riders
.iter()
.find(|rid| {
world.route(**rid).and_then(Route::current_destination) == Some(current_stop)
})
.copied();
if let Some(rid) = exit_rider {
actions.push(LoadAction::Exit {
rider: rid,
elevator: eid,
stop: current_stop,
});
continue;
}
// Derive this elevator's group from its line component.
let elev_line = car.line();
let elev_group: Option<GroupId> = world.line(elev_line).map(Line::group);
// Single pass: find a boardable rider (fits by weight) or a rejectable one (doesn't fit).
let remaining_capacity = car.weight_capacity - car.current_load;
let load_ratio = if car.weight_capacity > 0.0 {
car.current_load / car.weight_capacity
} else {
1.0
};
let car_restricted_stops = &car.restricted_stops;
let mut rejected_candidate: Option<EntityId> = None;
let mut preference_rejected: Option<EntityId> = None;
let mut access_rejected: Option<EntityId> = None;
let board_rider = world.iter_riders().find_map(|(rid, rider)| {
if world.is_disabled(rid) {
return None;
}
if rider.phase != RiderPhase::Waiting || rider.current_stop != Some(current_stop) {
return None;
}
// Must want to depart from this stop (check route leg origin).
let route_ok = world
.route(rid)
.is_none_or(|route| route.current().is_none_or(|leg| leg.from == current_stop));
if !route_ok {
return None;
}
// Group/line match: rider must want this elevator's group (or specific line).
if let Some(route) = world.route(rid) {
if let Some(leg) = route.current() {
match leg.via {
TransportMode::Group(g) => {
if elev_group != Some(g) {
return None;
}
}
TransportMode::Line(l) => {
if elev_line != l {
return None;
}
}
TransportMode::Walk => {
return None; // Walking riders don't board elevators.
}
}
}
}
// Access control: check rider can reach destination via this elevator.
if let Some(dest) = world.route(rid).and_then(Route::current_destination) {
if car_restricted_stops.contains(&dest) {
if access_rejected.is_none() {
access_rejected = Some(rid);
}
return None;
}
if let Some(ac) = world.access_control(rid) {
if !ac.can_access(dest) {
if access_rejected.is_none() {
access_rejected = Some(rid);
}
return None;
}
}
// Direction indicator filter: rider must be going in a direction
// this car will serve. A filtered rider silently stays waiting —
// no rejection event — so a later car in the right direction can
// pick them up.
let cur_pos = world.position(current_stop).map(|p| p.value);
let dest_pos = world.position(dest).map(|p| p.value);
if let (Some(cp), Some(dp)) = (cur_pos, dest_pos) {
if dp > cp && !car.going_up {
return None;
}
if dp < cp && !car.going_down {
return None;
}
}
}
// Rider preferences: skip crowded elevators.
if let Some(prefs) = world.preferences(rid)
&& prefs.skip_full_elevator
&& load_ratio > prefs.max_crowding_factor
{
if preference_rejected.is_none() {
preference_rejected = Some(rid);
}
return None;
}
if rider.weight <= remaining_capacity {
Some((rid, rider.weight))
} else {
if rejected_candidate.is_none() {
rejected_candidate = Some(rid);
}
None
}
});
if let Some((rid, weight)) = board_rider {
actions.push(LoadAction::Board {
rider: rid,
elevator: eid,
weight,
});
continue;
}
if let Some(rid) = access_rejected {
actions.push(LoadAction::Reject {
rider: rid,
elevator: eid,
reason: RejectionReason::AccessDenied,
context: None,
});
} else if let Some(rid) = rejected_candidate {
actions.push(LoadAction::Reject {
rider: rid,
elevator: eid,
reason: RejectionReason::OverCapacity,
context: Some(RejectionContext {
attempted_weight: world.rider(rid).map_or(0.0, |r| r.weight).into(),
current_load: car.current_load.into(),
capacity: car.weight_capacity.into(),
}),
});
} else if let Some(rid) = preference_rejected {
actions.push(LoadAction::Reject {
rider: rid,
elevator: eid,
reason: RejectionReason::PreferenceBased,
context: Some(RejectionContext {
attempted_weight: world.rider(rid).map_or(0.0, |r| r.weight).into(),
current_load: car.current_load.into(),
capacity: car.weight_capacity.into(),
}),
});
}
}
actions
}
/// Mutation pass: apply collected actions to the world and emit events.
fn apply_actions(
actions: Vec<LoadAction>,
world: &mut World,
events: &mut EventBus,
ctx: &PhaseContext,
rider_index: &mut RiderIndex,
) {
for action in actions {
match action {
LoadAction::Exit {
rider,
elevator,
stop,
} => {
// Guard: skip if rider is no longer Riding this elevator (another
// elevator may have already exited them in an earlier action).
if world
.rider(rider)
.is_none_or(|r| r.phase != RiderPhase::Riding(elevator))
{
continue;
}
let rider_weight = world.rider(rider).map_or(0.0, |rd| rd.weight);
if let Some(car) = world.elevator_mut(elevator) {
car.riders.retain(|r| *r != rider);
car.current_load = (car.current_load - rider_weight).max(0.0);
}
if let Some(rd) = world.rider_mut(rider) {
rd.phase = RiderPhase::Exiting(elevator);
rd.current_stop = Some(stop);
}
events.emit(Event::RiderExited {
rider,
elevator,
stop,
tick: ctx.tick,
});
if let Some(car) = world.elevator(elevator) {
events.emit(Event::CapacityChanged {
elevator,
current_load: OrderedFloat(car.current_load),
capacity: OrderedFloat(car.weight_capacity),
tick: ctx.tick,
});
}
}
LoadAction::Board {
rider,
elevator,
weight,
} => {
// Guard: skip if rider is no longer Waiting (another elevator at
// the same stop may have already boarded them in an earlier action).
let boarding_stop = world.rider(rider).and_then(|r| {
if r.phase == RiderPhase::Waiting {
r.current_stop
} else {
None
}
});
let Some(stop) = boarding_stop else {
continue;
};
rider_index.remove_waiting(stop, rider);
if let Some(car) = world.elevator_mut(elevator) {
car.current_load += weight;
car.riders.push(rider);
}
if let Some(rd) = world.rider_mut(rider) {
rd.phase = RiderPhase::Boarding(elevator);
rd.board_tick = Some(ctx.tick);
rd.current_stop = None;
}
events.emit(Event::RiderBoarded {
rider,
elevator,
tick: ctx.tick,
});
if let Some(car) = world.elevator(elevator) {
events.emit(Event::CapacityChanged {
elevator,
current_load: OrderedFloat(car.current_load),
capacity: OrderedFloat(car.weight_capacity),
tick: ctx.tick,
});
}
}
LoadAction::Reject {
rider,
elevator,
reason,
context,
} => {
events.emit(Event::RiderRejected {
rider,
elevator,
reason,
context,
tick: ctx.tick,
});
}
}
}
}
/// One rider boards or exits per tick per elevator.
pub fn run(
world: &mut World,
events: &mut EventBus,
ctx: &PhaseContext,
elevator_ids: &[EntityId],
rider_index: &mut RiderIndex,
) {
let actions = collect_actions(world, elevator_ids);
apply_actions(actions, world, events, ctx, rider_index);
}