Skip to main content
Verticopolis engine

verticopolis_engine/
events_fire.rs

1//! A fire outbreak and the paid-rescue offer it raises, split from
2//! `events.rs` for its size: `startFireOn` and `offerFireRescue` in
3//! `src/engine/EventSystem.ts`.
4
5use crate::events::PendingChoice;
6use crate::facilities::Kind;
7use crate::gameplay::GameplayEvent;
8use crate::services::with_thousands;
9use crate::sim::{LogKind, Simulation};
10use crate::tower::UnitState;
11
12/// Why [`Simulation::call_fire_rescue`] refused.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum FireRescueRefusal {
15    /// No fire is burning.
16    None,
17    /// An emergency choice is already waiting for the player's answer.
18    Pending,
19}
20
21impl FireRescueRefusal {
22    /// The port's `reason` string.
23    pub fn reason(&self) -> &'static str {
24        match self {
25            FireRescueRefusal::None => "none",
26            FireRescueRefusal::Pending => "pending",
27        }
28    }
29}
30
31#[doc(hidden)]
32impl Simulation {
33    /// `startFireOn`: set the room at index `i` ablaze as a new outbreak. A
34    /// blaze spreading to a neighbor is the same fire, so only this counts as
35    /// one started.
36    pub(super) fn ignite(&mut self, i: usize) {
37        let (u, at) = self.tower.marked(i);
38        u.set_state(at, UnitState::Fire);
39        u.set_occupants(at, 0);
40        let (id, name, floor) = (u.id, u.kind.facility().name, u.floor);
41        self.events.active.insert(id);
42        self.events.fires_started += 1;
43        self.gameplay.push(GameplayEvent::FireStarted);
44        let msg = format!(
45            "๐Ÿ”ฅ Fire broke out in {} on {}!",
46            name,
47            self.floor_label(floor)
48        );
49        self.emit(&msg, LogKind::Bad);
50    }
51
52    /// `offerFireRescue`: the paid-rescue choice a new outbreak raises, with
53    /// the free-defense tip for a tower that has no Security office.
54    pub(super) fn offer_fire_rescue(&mut self) {
55        let cost = self.fire_rescue_cost();
56        let message = format!("๐Ÿš’ Fire rescue available for ${}. Pay to stop the spread and save the tower now, or decline and fight it the slow way. Either way the rooms already ablaze burn down to gutted shells you'll rebuild; the fee just limits how far the fire spreads.", with_thousands(cost));
57        self.events.pending = Some(PendingChoice {
58            kind: "fireRescue",
59            cost,
60            message: message.clone(),
61        });
62        self.emit(&message, LogKind::Bad);
63        if !self.has_any(Kind::Security) {
64            self.emit(
65                "Tip: a Security office fights fires for free. Build one to defend your tower.",
66                LogKind::Bad,
67            );
68        }
69    }
70
71    /// Options > Call Fire Rescue (manual p.62): a player who declined the
72    /// offer can still call the rescue for as long as a fire burns. This puts
73    /// the same offer up again, at today's price, and returns that price;
74    /// [`Simulation::resolve_choice`] answers it as it answers the first one.
75    /// It takes no money and draws nothing from the random stream.
76    pub fn call_fire_rescue(&mut self) -> Result<f64, FireRescueRefusal> {
77        if self.events.pending.is_some() {
78            return Err(FireRescueRefusal::Pending);
79        }
80        if self.events.active.is_empty() {
81            return Err(FireRescueRefusal::None);
82        }
83        self.offer_fire_rescue();
84        Ok(self.fire_rescue_cost())
85    }
86
87    /// Tooling only: the daily roll's outbreak in room `id` instead of a
88    /// random one, the fire and the rescue offer both, so a test can set a
89    /// chosen room ablaze mid-run. It draws nothing from the random stream.
90    /// False, changing nothing, while a fire burns or a choice is pending
91    /// (the roll starts none then), or when `id` names no room that can
92    /// catch fire.
93    pub fn start_fire_at(&mut self, id: i64) -> bool {
94        if self.events.pending.is_some() || !self.events.active.is_empty() {
95            return false;
96        }
97        let at = self
98            .flammable_units()
99            .into_iter()
100            .find(|&i| self.tower.units[i].id == id);
101        let Some(i) = at else { return false };
102        self.ignite(i);
103        self.offer_fire_rescue();
104        true
105    }
106}