Skip to main content

rm2k/
setup.rs

1//! `RPG_RT`'s post-load fixups, as an explicit opt-in.
2//!
3//! Three `Actor` fields (see the [schema docs](crate::schema) for why the schema stores `-1`
4//! for them rather than a real value) get resolved to the engine's actual default by
5//! [`Setup::setup`], which then also pads the actor's six stat curves out to the engine's
6//! maximum level.
7//!
8//! Not automatic: padding the curves **changes the file**. `Parameters` is `persist`, so it is
9//! always written back at whatever length padding left it. Running this on load would make
10//! load/save non-idempotent.
11//!
12//! Byte-exactness is this crate's bar, so the fixup is separate. Call it for values a game engine
13//! can run on; skip it when editing files and writing back exactly what you read.
14//!
15//! ```
16//! # use rm2k::{rpg::Database, setup::Setup, Engine, LcfStruct};
17//! # let mut db = Database::defaults(Engine::R2K);
18//! // Reading a game: resolve the sentinels first.
19//! db.setup(Engine::R2K);
20//! ```
21
22use crate::{
23    engine::Engine,
24    rpg::{Actor, Database},
25};
26
27/// Apply `RPG_RT`'s post-load fixups.
28pub trait Setup {
29    /// Resolve sentinel defaults and pad derived data for `engine`.
30    fn setup(&mut self, engine: Engine);
31}
32
33/// The highest level an actor can reach, per engine.
34///
35/// It is both the substitute for a `-1` final level and the length the stat curves
36/// are padded to - note the curves use *this*, not the actor's own final level,
37/// even when the actor's is lower.
38#[must_use]
39#[inline]
40pub const fn max_final_level(engine: Engine) -> i32 {
41    engine.pick(50, 99)
42}
43
44/// The default experience curve constants, per engine.
45#[must_use]
46#[inline]
47const fn default_exp(engine: Engine) -> i32 {
48    engine.pick(30, 300)
49}
50
51impl Setup for Actor<'_> {
52    fn setup(&mut self, engine: Engine) {
53        let max_level = max_final_level(engine);
54
55        if self.final_level == -1 {
56            self.final_level = max_level;
57        }
58        if self.exp_base == -1 {
59            self.exp_base = default_exp(engine);
60        }
61        if self.exp_inflation == -1 {
62            self.exp_inflation = default_exp(engine);
63        }
64
65        pad_parameters(&mut self.parameters, max_level);
66    }
67}
68
69/// Pad an actor's six stat curves out to `levels` entries.
70///
71/// Only ever grows, and each curve has its own fill value. A curve that
72/// is already long enough is untouched, so this is idempotent.
73fn pad_parameters(parameters: &mut crate::raw::Parameters, levels: i32) {
74    let levels = if levels > 0 { levels as usize } else { 0 };
75    for (curve, fill) in [
76        (&mut parameters.maxhp, 1i16),
77        (&mut parameters.maxsp, 0),
78        (&mut parameters.attack, 1),
79        (&mut parameters.defense, 1),
80        (&mut parameters.spirit, 1),
81        (&mut parameters.agility, 1),
82    ] {
83        if curve.0.len() < levels {
84            curve.0.resize(levels, fill);
85        }
86    }
87}
88
89impl Setup for Database<'_> {
90    /// Run [`Actor::setup`](Setup::setup) over every actor.
91    fn setup(&mut self, engine: Engine) {
92        for actor in self.actors.iter_mut() {
93            actor.setup(engine);
94        }
95    }
96}