sb-core: constants module (WI-2, issue #2)

This commit is contained in:
Jan-Peter von Hunnius 2026-10-03 11:58:02 +02:00
parent 337e9bcf2c
commit 093012af20
6 changed files with 223 additions and 45 deletions

View file

@ -1,3 +1,4 @@
use crate::constants;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -41,25 +42,24 @@ pub fn find_spell(id: &str) -> Option<&'static SpellDef> {
}
pub fn spell_learn_cost(spell_level: i32) -> i32 {
match spell_level {
1 => 0,
2 => 1000,
3 => 2000,
4 => 4000,
5 => 7000,
6 => 10000,
7 => 20000,
_ => 999999,
if (constants::MIN_SPELL_LEVEL..=constants::MAX_SPELL_LEVEL).contains(&spell_level) {
constants::SPELL_LEARN_COST_BY_LEVEL[(spell_level - 1) as usize]
} else {
constants::SPELL_LEARN_COST_UNKNOWN_LEVEL
}
}
pub fn caster_spell_level(character_level: i32) -> i32 {
(character_level / constants::CASTER_LEVELS_PER_SPELL_LEVEL) + 1
}
pub fn max_spell_points(class: &str, level: i32) -> i32 {
let spell_class = class_for_spells(class);
let spell_level = match spell_class {
"conjurer" | "magician" | "sorcerer" | "wizard" => (level / 3) + 1,
"conjurer" | "magician" | "sorcerer" | "wizard" => caster_spell_level(level),
_ => 0,
};
spell_level * 10
spell_level * constants::SPELL_POINTS_PER_SPELL_LEVEL
}
pub fn is_caster_class(class: &str) -> bool {
@ -75,3 +75,39 @@ pub fn caster_classes() -> &'static [&'static str] {
use std::sync::LazyLock;
static ALL_SPELLS: LazyLock<Vec<SpellDef>> = LazyLock::new(load_spells);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn caster_spell_level_spot_values() {
assert_eq!(caster_spell_level(1), 1);
assert_eq!(caster_spell_level(2), 1);
assert_eq!(caster_spell_level(3), 2);
assert_eq!(caster_spell_level(6), 3);
assert_eq!(caster_spell_level(100), 34);
}
#[test]
fn spell_learn_cost_guard_spot_checks() {
assert_eq!(
spell_learn_cost(0),
constants::SPELL_LEARN_COST_UNKNOWN_LEVEL
);
assert_eq!(
spell_learn_cost(-1),
constants::SPELL_LEARN_COST_UNKNOWN_LEVEL
);
assert_eq!(
spell_learn_cost(8),
constants::SPELL_LEARN_COST_UNKNOWN_LEVEL
);
for level in constants::MIN_SPELL_LEVEL..=constants::MAX_SPELL_LEVEL {
assert_eq!(
spell_learn_cost(level),
constants::SPELL_LEARN_COST_BY_LEVEL[(level - 1) as usize]
);
}
}
}