verticopolis_engine/view/window.rs
1//! The people window: the part of the tower a view draws, so its frame
2//! carries only the people there (`ViewReader::set_people_window`). The 1994
3//! game draws every person in its scrolling window and none outside it; a
4//! view does the same here, so the frame's size follows the screen and stays
5//! flat as the crowd grows. The window is the host's camera and no part of
6//! the game: it is not saved and not hashed, and the simulation steps every
7//! person whatever it is.
8
9use crate::crowd::Person;
10use crate::tower::Transport;
11
12/// The floors and tiles a view shows: a person is in it when their floor
13/// position (`fy`) lies within `floor_lo..=floor_hi` and their tile (`x`)
14/// within `x_lo..=x_hi`, after the pads [`PeopleWindow::padded`] adds.
15#[derive(Clone, Copy, Debug, PartialEq)]
16pub struct PeopleWindow {
17 /// The lowest floor shown.
18 pub floor_lo: f64,
19 /// The highest floor shown.
20 pub floor_hi: f64,
21 /// The leftmost tile shown.
22 pub x_lo: f64,
23 /// The rightmost tile shown.
24 pub x_hi: f64,
25}
26
27/// The floors a window reaches past its edges, so a figure standing on the
28/// edge floor, or a car gliding between two floors there, keeps its people.
29pub const FLOOR_PAD: f64 = 1.0;
30
31impl PeopleWindow {
32 /// A window from a host's four numbers, or `None` (every person) when
33 /// one is not finite or a range is empty, so a host that sends nonsense
34 /// gets the whole crowd instead of an empty frame.
35 pub fn new(floor_lo: f64, floor_hi: f64, x_lo: f64, x_hi: f64) -> Option<PeopleWindow> {
36 let finite = [floor_lo, floor_hi, x_lo, x_hi]
37 .iter()
38 .all(|v| v.is_finite());
39 (finite && floor_lo <= floor_hi && x_lo <= x_hi).then_some(PeopleWindow {
40 floor_lo,
41 floor_hi,
42 x_lo,
43 x_hi,
44 })
45 }
46
47 /// The window as a frame tests people against: [`FLOOR_PAD`] floors
48 /// past each edge, and half the widest shaft plus one tile past each
49 /// side. A rider stands at their shaft's middle, so a car whose edge is
50 /// on screen has its middle at most half its width outside, and every
51 /// rider of a car the view shows is in the frame.
52 pub fn padded(self, transports: &[Transport]) -> PeopleWindow {
53 let widest = transports.iter().map(|t| t.width).max().unwrap_or(0);
54 let x_pad = widest as f64 / 2.0 + 1.0;
55 PeopleWindow {
56 floor_lo: self.floor_lo - FLOOR_PAD,
57 floor_hi: self.floor_hi + FLOOR_PAD,
58 x_lo: self.x_lo - x_pad,
59 x_hi: self.x_hi + x_pad,
60 }
61 }
62
63 /// Whether `p` is in the window as it stands (pads already added).
64 pub fn holds(&self, p: &Person) -> bool {
65 (self.floor_lo..=self.floor_hi).contains(&p.fy) && (self.x_lo..=self.x_hi).contains(&p.x)
66 }
67}
68
69#[cfg(test)]
70#[path = "window_tests.rs"]
71mod window_tests;