Skip to main content
Verticopolis engine

verticopolis_engine/economy/
films.rs

1//! A theater's film (#1932, #1950). Neither mode charges a theater each
2//! period. The player pays to show a film, the film ages until its draw is
3//! spent, and the mode's [`FilmRules`] say what each film costs, how long
4//! it draws, and what it earns.
5//!
6//! Classic follows SimTower: a new film or a cheaper classic, a run of six
7//! days at full draw and three fading, and a theater whose new film draws a
8//! full house fills the crowd's and the traffic model's blockbuster slots.
9//!
10//! Modern offers a revival, a feature or a blockbuster, each film draws for
11//! 28 days and fades for 14, its income follows its tier and its age, and a
12//! theater books its chosen film again the morning the old one is spent
13//! (`film_policy`, unless the player switched that off).
14
15use serde::Serialize;
16
17use super::screens::running_screens;
18use super::tickets::{TicketPrice, MODERN_TICKET_PRICES};
19use super::visitors::{
20    set_audience, FADING_FILM_DRAW, FILM_SPENT_DAYS, NEW_FILM_DRAW, NEW_FILM_FULL_DRAW_DAYS,
21    SPENT_FILM_DRAW,
22};
23use crate::clock::GameMode;
24use crate::facilities::Kind;
25use crate::tower::{Extras, Unit};
26
27/// What showing a new film costs in SimTower.
28pub const NEW_FILM_PRICE: f64 = 300_000.0;
29/// What showing a classic film costs in SimTower.
30pub const CLASSIC_FILM_PRICE: f64 = 150_000.0;
31/// What a Modern revival, feature and blockbuster cost (#1950).
32pub const MODERN_REVIVAL_PRICE: f64 = 60_000.0;
33pub const MODERN_FEATURE_PRICE: f64 = 120_000.0;
34pub const MODERN_BLOCKBUSTER_PRICE: f64 = 300_000.0;
35/// What a theater showing a blockbuster earns, against a feature's 1.
36pub const BLOCKBUSTER_INCOME_MULT: f64 = 2.2;
37/// What a Modern revival earns at full draw, against a feature's 1.
38pub const REVIVAL_INCOME_MULT: f64 = 0.6;
39/// The share of its full-draw income a Modern film earns while fading, and
40/// once spent.
41pub const FADING_INCOME_SHARE: f64 = 0.6;
42pub const SPENT_INCOME_SHARE: f64 = 0.25;
43/// A Modern film's days at full draw, and the age at which it is spent.
44pub const MODERN_FULL_DRAW_DAYS: i64 = 28;
45pub const MODERN_SPENT_DAYS: i64 = 42;
46
47/// A film a theater can show. Classic calls `New` a new film and `Classic` a
48/// classic; Modern calls them a feature and a revival, and adds `Blockbuster`.
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub enum FilmTier {
51    New,
52    Classic,
53    Blockbuster,
54}
55
56impl FilmTier {
57    /// Every tier, in the order the port numbers them.
58    pub const ALL: [FilmTier; 3] = [FilmTier::New, FilmTier::Classic, FilmTier::Blockbuster];
59
60    /// The tier's key in the catalog and the port.
61    pub const fn key(self) -> &'static str {
62        match self {
63            FilmTier::New => "new",
64            FilmTier::Classic => "classic",
65            FilmTier::Blockbuster => "blockbuster",
66        }
67    }
68
69    /// The film a theater's saved flags say it shows.
70    pub fn showing(extras: &Extras) -> FilmTier {
71        if extras.film_classic {
72            FilmTier::Classic
73        } else if extras.film_blockbuster {
74            FilmTier::Blockbuster
75        } else {
76            FilmTier::New
77        }
78    }
79}
80
81/// How far into its run a film is.
82#[derive(Clone, Copy, Debug, PartialEq, Eq)]
83pub enum FilmStage {
84    Full,
85    Fading,
86    Spent,
87}
88
89/// How long a film draws in a mode.
90#[derive(Clone, Copy, Debug, PartialEq, Eq)]
91pub struct FilmRun {
92    /// The days a film keeps its full draw.
93    pub full_draw_days: i64,
94    /// The age in days from which a film is spent.
95    pub spent_days: i64,
96}
97
98impl FilmRun {
99    /// The stage of a film that has shown for `days_shown` days.
100    pub fn stage(self, days_shown: i64) -> FilmStage {
101        if days_shown < self.full_draw_days {
102            FilmStage::Full
103        } else if days_shown < self.spent_days {
104            FilmStage::Fading
105        } else {
106            FilmStage::Spent
107        }
108    }
109
110    /// The census draw of a `tier` film that has shown for `days_shown`
111    /// days: 2, 1 and 0 through its run, and never past a fading film's for
112    /// a classic or revival.
113    pub fn draw(self, days_shown: i64, tier: FilmTier) -> i64 {
114        let draw = match self.stage(days_shown) {
115            FilmStage::Full => NEW_FILM_DRAW,
116            FilmStage::Fading => FADING_FILM_DRAW,
117            FilmStage::Spent => SPENT_FILM_DRAW,
118        };
119        if tier == FilmTier::Classic {
120            draw.min(FADING_FILM_DRAW)
121        } else {
122            draw
123        }
124    }
125}
126
127/// A film a mode lets the player show.
128#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
129#[serde(rename_all = "camelCase")]
130pub struct FilmOffer {
131    #[serde(skip)]
132    pub tier: FilmTier,
133    /// The tier's key, as the catalog publishes it.
134    pub key: &'static str,
135    /// What the card calls the film.
136    pub label: &'static str,
137    /// What showing it costs.
138    pub price: f64,
139    /// How the log opens the film ("A new film opens...").
140    #[serde(skip)]
141    pub opening: &'static str,
142    /// What it earns at full draw, against a feature's 1, where income
143    /// follows the film's age.
144    #[serde(skip)]
145    pub income_mult: f64,
146}
147
148/// A choice on a Modern theater's Auto-rebook button: the policy the save
149/// stores, and the film it books again once the theater's film is spent, or
150/// none where the player switched rebooking off (#1950).
151#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
152#[serde(rename_all = "camelCase")]
153pub struct FilmRebook {
154    /// The policy, as the save and `setFilm` name it.
155    pub policy: &'static str,
156    /// The film's key in `films`, or null for off.
157    pub film: Option<&'static str>,
158    #[serde(skip)]
159    pub tier: Option<FilmTier>,
160}
161
162/// A mode's film rules.
163#[derive(Debug)]
164pub struct FilmRules {
165    /// The films the player can show, in the order the card lists them.
166    pub offers: &'static [FilmOffer],
167    pub run: FilmRun,
168    /// The tier whose film fills the blockbuster slots at full draw.
169    pub blockbuster_tier: FilmTier,
170    /// The Auto-rebook choices, in the order the button cycles through
171    /// them, where a theater books its policy's film again once its film is
172    /// spent (Modern); empty where it never does (Classic).
173    pub rebooks: &'static [FilmRebook],
174    /// The choice a theater with no policy, or an unlisted one (an old
175    /// save's "auto"), rebooks by.
176    pub rebook_default: Option<&'static str>,
177    /// Whether a theater's income follows its film's tier and age (Modern).
178    /// Classic earns the blockbuster multiplier in a blockbuster slot only.
179    pub income_follows_film: bool,
180    /// The ticket price steps a theater can charge, cheapest first (Modern),
181    /// or none where the player sets no ticket price (Classic).
182    pub ticket_prices: &'static [TicketPrice],
183}
184
185impl FilmRules {
186    /// The offer for `tier`, or `None` where the mode has no such film.
187    pub fn offer(&self, tier: FilmTier) -> Option<&'static FilmOffer> {
188        self.offers.iter().find(|o| o.tier == tier)
189    }
190
191    /// The film a theater with `policy` books again once its film is spent,
192    /// or `None` where it books none: rebooking is off, or the mode never
193    /// rebooks.
194    pub fn rebooked(&self, policy: Option<&str>) -> Option<FilmTier> {
195        let listed = |p: &str| self.rebooks.iter().find(|r| r.policy == p);
196        policy
197            .and_then(listed)
198            .or_else(|| self.rebook_default.and_then(listed))
199            .and_then(|r| r.tier)
200    }
201}
202
203pub static CLASSIC_FILMS: FilmRules = FilmRules {
204    offers: &[
205        FilmOffer {
206            tier: FilmTier::New,
207            key: "new",
208            label: "New film",
209            price: NEW_FILM_PRICE,
210            opening: "A new film",
211            income_mult: 1.0,
212        },
213        FilmOffer {
214            tier: FilmTier::Classic,
215            key: "classic",
216            label: "Classic film",
217            price: CLASSIC_FILM_PRICE,
218            opening: "A classic film",
219            income_mult: 1.0,
220        },
221    ],
222    run: FilmRun {
223        full_draw_days: NEW_FILM_FULL_DRAW_DAYS,
224        spent_days: FILM_SPENT_DAYS,
225    },
226    blockbuster_tier: FilmTier::New,
227    rebooks: &[],
228    rebook_default: None,
229    income_follows_film: false,
230    ticket_prices: &[],
231};
232
233pub static MODERN_FILMS: FilmRules = FilmRules {
234    offers: &[
235        FilmOffer {
236            tier: FilmTier::Classic,
237            key: "classic",
238            label: "Revival",
239            price: MODERN_REVIVAL_PRICE,
240            opening: "A revival",
241            income_mult: REVIVAL_INCOME_MULT,
242        },
243        FilmOffer {
244            tier: FilmTier::New,
245            key: "new",
246            label: "Feature",
247            price: MODERN_FEATURE_PRICE,
248            opening: "A feature",
249            income_mult: 1.0,
250        },
251        FilmOffer {
252            tier: FilmTier::Blockbuster,
253            key: "blockbuster",
254            label: "Blockbuster",
255            price: MODERN_BLOCKBUSTER_PRICE,
256            opening: "A blockbuster",
257            income_mult: BLOCKBUSTER_INCOME_MULT,
258        },
259    ],
260    run: FilmRun {
261        full_draw_days: MODERN_FULL_DRAW_DAYS,
262        spent_days: MODERN_SPENT_DAYS,
263    },
264    blockbuster_tier: FilmTier::Blockbuster,
265    rebooks: &[
266        FilmRebook {
267            policy: "off",
268            film: None,
269            tier: None,
270        },
271        FilmRebook {
272            policy: "revival",
273            film: Some("classic"),
274            tier: Some(FilmTier::Classic),
275        },
276        FilmRebook {
277            policy: "feature",
278            film: Some("new"),
279            tier: Some(FilmTier::New),
280        },
281        FilmRebook {
282            policy: "blockbuster",
283            film: Some("blockbuster"),
284            tier: Some(FilmTier::Blockbuster),
285        },
286    ],
287    rebook_default: Some("feature"),
288    income_follows_film: true,
289    ticket_prices: &MODERN_TICKET_PRICES,
290};
291
292/// Why a theatre's film did not change.
293#[derive(Debug, Clone, Copy, PartialEq)]
294pub enum FilmRefusal {
295    /// The mode offers no such film (a blockbuster in Classic).
296    Unavailable,
297    /// The id names no theatre.
298    NotTheater,
299    /// The theatre is not open: under construction, burning or gutted.
300    Closed,
301    /// The theater runs no such screen (a second screen in Classic).
302    NoScreen,
303    /// The film would cost more than the tower holds.
304    Funds {
305        /// The price of the film the player asked for.
306        cost: f64,
307    },
308}
309
310impl FilmRefusal {
311    /// The refusal's reason, as the port reports it.
312    pub fn reason(&self) -> &'static str {
313        match self {
314            FilmRefusal::Unavailable => "unavailable",
315            FilmRefusal::NotTheater => "notTheater",
316            FilmRefusal::Closed => "closed",
317            FilmRefusal::NoScreen => "noScreen",
318            FilmRefusal::Funds { .. } => "funds",
319        }
320    }
321}
322
323/// Whether a theater's film fills a blockbuster slot tonight: the mode's
324/// blockbuster film at full draw on any of its screens.
325pub(crate) fn fills_blockbuster_slot(mode: GameMode, u: &Unit) -> bool {
326    let films = mode.films();
327    running_screens(mode, u).any(|film| {
328        film.tier == films.blockbuster_tier && films.run.stage(film.days_shown()) == FilmStage::Full
329    })
330}
331
332/// The audience a theater counts tonight: each screen's film draws its own
333/// house to half the seats, so the theater counts the screens' mean.
334pub(crate) fn theater_audience(mode: GameMode, u: &Unit) -> i64 {
335    let run = mode.films().run;
336    let (mut total, mut count) = (0, 0);
337    for film in running_screens(mode, u) {
338        let draw = run.draw(film.days_shown(), film.tier);
339        total += set_audience(Kind::Cinema, draw).unwrap_or(0);
340        count += 1;
341    }
342    if count == 0 {
343        0
344    } else {
345        total / count
346    }
347}
348
349/// What a theater's films multiply its ticket income by. `in_slot` is
350/// whether it holds a blockbuster slot, which is all Classic reads. Where
351/// income follows the film (Modern), each screen sells half the seats, so
352/// the theater earns the screens' mean.
353pub(crate) fn film_income_mult(mode: GameMode, u: &Unit, in_slot: bool) -> f64 {
354    let films = mode.films();
355    if !films.income_follows_film {
356        return if in_slot {
357            BLOCKBUSTER_INCOME_MULT
358        } else {
359            1.0
360        };
361    }
362    let (mut total, mut count) = (0.0, 0.0);
363    for film in running_screens(mode, u) {
364        let peak = films.offer(film.tier).map_or(1.0, |o| o.income_mult);
365        let share = match films.run.stage(film.days_shown()) {
366            FilmStage::Full => 1.0,
367            FilmStage::Fading => FADING_INCOME_SHARE,
368            FilmStage::Spent => SPENT_INCOME_SHARE,
369        };
370        total += peak * share;
371        count += 1.0;
372    }
373    if count == 0.0 {
374        1.0
375    } else {
376        total / count
377    }
378}
379
380#[cfg(test)]
381#[path = "films_tests.rs"]
382pub(crate) mod films_tests;
383
384#[cfg(test)]
385#[path = "films_modern_tests.rs"]
386mod films_modern_tests;