verticopolis_engine/economy/tickets.rs
1//! A Modern theater's ticket price (#1950). The player sets one of four
2//! steps per theater: a lower price earns less per seat and draws more of
3//! the crowd's cinema visits, a higher one earns more per seat and draws
4//! fewer. Seats are capped, so a theater that turns people away can raise
5//! its price without losing a seat until its demand falls below capacity.
6//!
7//! Classic has no ticket price: SimTower gives the player no such control,
8//! so its film rules list no steps and every theater earns and draws as
9//! Normal.
10
11use serde::Serialize;
12
13use crate::clock::GameMode;
14use crate::facilities::Kind;
15use crate::sim::Simulation;
16use crate::tower::Unit;
17
18/// One ticket price step.
19#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
20#[serde(rename_all = "camelCase")]
21pub struct TicketPrice {
22 /// The step's key, as the save and `setTicketPrice` name it.
23 pub key: &'static str,
24 /// The name the card shows.
25 pub label: &'static str,
26 /// What the step multiplies a theater's ticket income per seat by.
27 pub income_mult: f64,
28 /// What the step multiplies the share of the crowd's cinema visits a
29 /// theater draws by.
30 pub pick_mult: f64,
31}
32
33/// The step a theater with none set charges.
34pub const NORMAL_TICKET_PRICE: &str = "normal";
35
36/// Modern's steps, cheapest first, in the order the card's button cycles.
37pub static MODERN_TICKET_PRICES: [TicketPrice; 4] = [
38 TicketPrice {
39 key: "low",
40 label: "Low",
41 income_mult: 0.75,
42 pick_mult: 1.25,
43 },
44 TicketPrice {
45 key: NORMAL_TICKET_PRICE,
46 label: "Normal",
47 income_mult: 1.0,
48 pick_mult: 1.0,
49 },
50 TicketPrice {
51 key: "high",
52 label: "High",
53 income_mult: 1.3,
54 pick_mult: 0.75,
55 },
56 TicketPrice {
57 key: "premium",
58 label: "Premium",
59 income_mult: 1.6,
60 pick_mult: 0.55,
61 },
62];
63
64impl GameMode {
65 /// The ticket price steps a theater can charge, cheapest first, or an
66 /// empty list where the mode has no ticket price (Classic).
67 pub fn ticket_prices(self) -> &'static [TicketPrice] {
68 self.films().ticket_prices
69 }
70
71 /// The step a theater with none set charges, or `None` where the mode
72 /// has no ticket price.
73 pub fn ticket_price_default(self) -> Option<&'static str> {
74 (!self.ticket_prices().is_empty()).then_some(NORMAL_TICKET_PRICE)
75 }
76
77 /// The listed step named `key`, or `None` where the mode lists none by
78 /// that name.
79 pub fn ticket_price(self, key: &str) -> Option<&'static TicketPrice> {
80 self.ticket_prices().iter().find(|p| p.key == key)
81 }
82}
83
84/// The step a theater charges, or `None` at Normal (no step set, or a mode
85/// without ticket prices).
86pub(crate) fn charged(mode: GameMode, u: &Unit) -> Option<&'static TicketPrice> {
87 u.extras()
88 .ticket_price
89 .and_then(|key| mode.ticket_price(key))
90}
91
92/// The step a theater charges as the cards name it: its own, or Normal where
93/// it has none. `None` for any other room and in a mode without ticket
94/// prices.
95pub(crate) fn shown_ticket_price(mode: GameMode, u: &Unit) -> Option<&'static str> {
96 let normal = mode.ticket_price_default()?;
97 (u.kind == Kind::Cinema).then(|| charged(mode, u).map_or(normal, |p| p.key))
98}
99
100/// What a theater's ticket price multiplies its ticket income per seat by.
101pub(crate) fn ticket_income_mult(mode: GameMode, u: &Unit) -> f64 {
102 charged(mode, u).map_or(1.0, |p| p.income_mult)
103}
104
105/// What a theater's ticket price multiplies its share of the crowd's cinema
106/// visits by.
107pub(crate) fn ticket_pick_mult(mode: GameMode, u: &Unit) -> f64 {
108 charged(mode, u).map_or(1.0, |p| p.pick_mult)
109}
110
111impl Simulation {
112 /// Sets the ticket price cinema `id` charges (Modern): one of the mode's
113 /// steps (the [`catalog`](crate::catalog::catalog)'s `ticketPrices`).
114 /// Returns the step set, or `None` when `id` is not a cinema or the mode
115 /// lists no step named `key`. Normal is stored as no step at all, so a
116 /// theater back at Normal saves as it did before it had a price.
117 pub fn set_ticket_price(&mut self, id: i64, key: &str) -> Option<&'static str> {
118 let step = self.tower.mode.ticket_price(key)?.key;
119 let u = self.tower.get_unit_mut(id)?;
120 if u.kind != Kind::Cinema {
121 return None;
122 }
123 let stored = (step != NORMAL_TICKET_PRICE).then_some(step);
124 if u.extras().ticket_price != stored {
125 u.extras_mut().ticket_price = stored;
126 }
127 Some(step)
128 }
129}
130
131#[cfg(test)]
132#[path = "tickets_tests.rs"]
133mod tests;