Skip to main content
Verticopolis engine

verticopolis_engine/
brush.rs

1//! The floor and lobby brush: [`Simulation::paint_brush`] lays a run of
2//! floor or lobby tiles in one command.
3
4use crate::build::substrate_cost;
5use crate::charges::INVALID_REQUEST;
6use crate::facilities::Kind;
7use crate::sim::Simulation;
8
9/// A structural tile and the bridge laid with it, as `build` lays them.
10pub(crate) struct BridgedTile {
11    /// How many stories the tile itself rises.
12    pub stories: i64,
13    /// The price of every bridged substrate tile.
14    pub bridge_cost: f64,
15}
16
17/// The most tiles one brush may carry.
18pub const BRUSH_TILE_LIMIT: usize = 32;
19
20/// What a brush laid.
21#[derive(Clone, Debug, PartialEq)]
22pub struct BrushResult {
23    /// How many tiles it laid.
24    pub placed: i64,
25    /// The last refusal one of its tiles got, if any did.
26    pub reason: Option<String>,
27}
28
29impl BrushResult {
30    #[doc(hidden)]
31    pub fn invalid() -> BrushResult {
32        BrushResult {
33            placed: 0,
34            reason: Some(INVALID_REQUEST.into()),
35        }
36    }
37}
38
39impl Simulation {
40    /// How many stories a structural tile of `kind` laid at `(floor, x)`
41    /// covers: the height a lobby tile there gets ([`Self::with_lobby_height`]
42    /// and the hall it is laid against), 1 for a floor tile.
43    pub(crate) fn tile_stories(&self, kind: Kind, floor: i64, x: i64) -> i64 {
44        if kind == Kind::Lobby {
45            self.tower
46                .placement_lobby_height_at(floor, x, kind.facility().width)
47        } else {
48            1
49        }
50    }
51
52    /// How many stories a structural tile of `kind` at `(floor, x)` rises
53    /// once `plan`, its bridge, is laid, and that bridge's price.
54    /// `fill_bridge` lays each side of a bridge out from the structure it
55    /// reaches, so in Modern a lobby bridge takes the height of the hall on
56    /// its side, and the tile joins the left side's (#2068): it is laid last,
57    /// against the left bridge, or against the right one when only that side
58    /// bridges and nothing stands beside it. In Classic, and for any tile but
59    /// a lobby, every tile takes [`Self::tile_stories`]. `build` charges the
60    /// tiles a bridge did lay at the height each one took.
61    pub(crate) fn bridged_tile(
62        &self,
63        kind: Kind,
64        floor: i64,
65        x: i64,
66        plan: &[(i64, i64)],
67    ) -> BridgedTile {
68        let own = self.tile_stories(kind, floor, x);
69        let per_hall = kind == Kind::Lobby && self.mode.lobby_height_per_hall();
70        let w = kind.facility().width;
71        let hall = |col: i64| {
72            self.tower
73                .hall_at(floor, col)
74                .map(|u| self.tower.unit_stories(u))
75        };
76        // The height of the hall a side's bridge reaches, past its far end.
77        let reach = |left: bool| -> Option<i64> {
78            let cols = plan
79                .iter()
80                .filter(|&&(fl, g)| fl == floor && (g < x) == left)
81                .map(|&(_, g)| g);
82            let end = if left {
83                cols.min()? - 1
84            } else {
85                cols.max()? + 1
86            };
87            per_hall.then(|| hall(end)).flatten()
88        };
89        let (left, right) = (reach(true), reach(false));
90        let beside = hall(x - 1).is_some() || hall(x + w).is_some();
91        let stories = left.or(if beside { None } else { right }).unwrap_or(own);
92        let side = |fl: i64, g: i64| {
93            if fl != floor {
94                None
95            } else if g < x {
96                left
97            } else {
98                right
99            }
100        };
101        let bridge_cost = plan
102            .iter()
103            .map(|&(fl, g)| substrate_cost(kind, side(fl, g).unwrap_or(own)))
104            .sum();
105        BridgedTile {
106            stories,
107            bridge_cost,
108        }
109    }
110
111    /// How many stories a lobby tile laid at `(floor, x)` now rises, with
112    /// the bridge the engine would lay for it when bridging is on: what
113    /// [`Self::can_build`] prices it at, and what `lobbyRise` reports.
114    pub fn lobby_rise_at(&self, floor: i64, x: i64) -> i64 {
115        let plan = if self.auto_bridge {
116            self.tower.bridge_fill_plan(Kind::Lobby, floor, x, 1, 1)
117        } else {
118            Vec::new()
119        };
120        self.bridged_tile(Kind::Lobby, floor, x, &plan).stories
121    }
122
123    /// Runs `run` with `height` as the height a floor's first lobby
124    /// placement asks for (#308), so a lobby built, quoted or brushed inside
125    /// it on a floor that holds no lobby yet rises that many stories, where
126    /// the mode lets that floor's lobby rise. A floor that holds a lobby
127    /// keeps its height whatever is asked. Outside it, a first lobby is one
128    /// story. The height is clamped to 1 through
129    /// [`MAX_LOBBY_HEIGHT`](crate::facilities::MAX_LOBBY_HEIGHT).
130    ///
131    /// ```
132    /// use verticopolis_engine::clock::GameMode;
133    /// use verticopolis_engine::facilities::Kind;
134    /// use verticopolis_engine::sim::Simulation;
135    ///
136    /// let mut sim = Simulation::new_game(1, GameMode::Classic);
137    /// let tiles: Vec<i64> = (100..104).collect();
138    /// sim.with_lobby_height(2, |sim| sim.paint_brush(Kind::Lobby, 1, &tiles));
139    /// assert_eq!(sim.tower.lobby_height(1), 2);
140    /// ```
141    pub fn with_lobby_height<T>(
142        &mut self,
143        height: i64,
144        run: impl FnOnce(&mut Simulation) -> T,
145    ) -> T {
146        let before = self.tower.requested_lobby_height;
147        self.set_lobby_height(height);
148        let out = run(self);
149        self.tower.requested_lobby_height = before;
150        out
151    }
152
153    /// Sets the height a floor's first lobby placement asks for until it is
154    /// set again, as a frontend's lobby tool holds its choice between
155    /// clicks; [`Self::with_lobby_height`] is the scoped form. Clamped to 1
156    /// through [`MAX_LOBBY_HEIGHT`](crate::facilities::MAX_LOBBY_HEIGHT).
157    /// Tool state only: never saved, and a loaded tower starts at 1.
158    pub fn set_lobby_height(&mut self, height: i64) {
159        self.tower.requested_lobby_height = height.clamp(1, crate::facilities::MAX_LOBBY_HEIGHT);
160    }
161
162    /// Lays `kind` (floor or lobby) on each tile column in `tiles` on
163    /// `floor`, charging as that many [`build`](Simulation::build) calls
164    /// would, in one command. It skips a tile that already carries `kind`,
165    /// and tries a refused tile again once a neighbor it needed is laid, so
166    /// the order of `tiles` does not matter.
167    ///
168    /// A lobby brush also turns plain floor into lobby (a sky lobby). Any
169    /// other kind, or more than [`BRUSH_TILE_LIMIT`] tiles, lays nothing and
170    /// answers `INVALID_REQUEST`.
171    ///
172    /// ```
173    /// use verticopolis_engine::clock::GameMode;
174    /// use verticopolis_engine::facilities::Kind;
175    /// use verticopolis_engine::sim::Simulation;
176    ///
177    /// let mut sim = Simulation::new_game(1, GameMode::Classic);
178    /// let tiles: Vec<i64> = (100..124).collect();
179    /// let laid = sim.paint_brush(Kind::Lobby, 1, &tiles);
180    /// assert_eq!(laid.placed, 24);
181    /// ```
182    pub fn paint_brush(&mut self, kind: Kind, floor: i64, tiles: &[i64]) -> BrushResult {
183        if !matches!(kind, Kind::Floor | Kind::Lobby) || tiles.len() > BRUSH_TILE_LIMIT {
184            return BrushResult::invalid();
185        }
186        self.with_lobby_upgrades(|sim| {
187            let mut placed = 0;
188            let mut reason = None;
189            let mut progress = true;
190            while progress {
191                progress = false;
192                for &tx in tiles {
193                    let existing = sim.tower.structure_kind_at(floor, tx);
194                    if existing == Some(kind) || (existing.is_some() && kind != Kind::Lobby) {
195                        continue;
196                    }
197                    let r = sim.build(kind, floor, tx);
198                    if r.ok {
199                        placed += 1;
200                        progress = true;
201                    } else {
202                        reason = r.reason;
203                    }
204                }
205            }
206            BrushResult { placed, reason }
207        })
208    }
209}