Skip to main content

unbanished_game/
chance.rs

1//! The chance of an event that happens on average once in a span of years: an injury at work, a
2//! tornado while it rains, a fire that starts by itself.
3
4use crate::random::Random;
5use crate::scheduler::seconds;
6
7/// Whether an event that happens on average once in `years × odds` game years happens now, given
8/// the time on its clock (game ticks since the last check). The clock restarts. With odds of 0
9/// or less nothing is drawn.
10///
11/// Measured: `tools/emu/once_in_years.py` (the original on 2,000 cases).
12///
13/// Original: `FUN_1400ae760`.
14pub fn once_in_years(timer: &mut i64, years: f32, odds: f32, rng: &mut Random) -> bool {
15    // param_2[1] <= 0: the clock clears, no draw.
16    if odds <= 0.0 {
17        *timer = 0;
18        return false;
19    }
20    // DAT_1401788b8 (12, months) and DAT_140178934 (300 s, a month), multiplied in that order.
21    let span = years * 12.0 * 300.0;
22    let since = seconds(*timer);
23    *timer = 0;
24    rng.rand_float() < since / (span * odds)
25}