From 3449e96574a4b9703ca90d953b8369e1c9d66b46 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sat, 14 Oct 2023 21:09:30 -0400 Subject: Add NPC-specific class ability score modifiers when generating henchmen --- src/rules/npcs.rs | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/rules/npcs.rs b/src/rules/npcs.rs index e190952..dc3d751 100644 --- a/src/rules/npcs.rs +++ b/src/rules/npcs.rs @@ -1,4 +1,4 @@ -use crate::dice::{roll_formula, RollResult}; +use crate::dice::roll_formula; use crate::rules::ability_scores::{AbilityScore, AbilityScoreCollection}; use crate::rules::classes::Class; use std::collections::HashMap; @@ -28,7 +28,7 @@ impl Npc { pub fn roll_henchman_ability_scores(&mut self) { rand::thread_rng(); - let mut ability_score_rolls: HashMap = HashMap::new(); + let mut ability_score_rolls: HashMap = HashMap::new(); for &ability in &[ AbilityScore::Strength, @@ -48,20 +48,28 @@ impl Npc { roll_result.increase_sides_below_max(6, 1); } - ability_score_rolls.insert(ability, roll_result); + // At this point we don't need the individual dice anymore. + let mut total = roll_result.total(); + + // Add NPC-specific class ability score modifiers. + total += class_ref + .npc_ability_score_modifiers + .get(&ability) + .copied() + .unwrap_or(0) as u32; + + ability_score_rolls.insert(ability, total); } // Create an AbilityScoreCollection for the Npc, using the above results. let mut score_collection = AbilityScoreCollection::new(); - for (ability, roll_result) in &ability_score_rolls { - let total_score = roll_result.total(); - score_collection.add_score(*ability, total_score); + for (ability, value) in &ability_score_rolls { + score_collection.add_score(*ability, *value); } self.ability_scores = Some(score_collection); // TODO: Modify results for race. - // TODO: Modify results for henchmen-specific class bonuses. // TODO: Apply racial minimums and maximums. // TODO: Verify legality of class based on alignment. // TODO: Verify legality of class based on race. @@ -84,3 +92,29 @@ impl Npc { // write!(f, "{}", formatted_string) // } // } + +#[cfg(test)] +mod tests { + use crate::rules::classes::CLASSES; + use super::*; + + #[test] + #[ignore] + #[should_panic(expected = "Ability score generation isn't testable yet.")] + fn test_roll_henchman_ability_scores() { + let class_ref = CLASSES.get("fighter").unwrap(); + let mut npc = Npc { + alignment: Some(String::from("Lawful Good")), + race: Some(String::from("Dwarf")), + class: Some(class_ref), + ability_scores: None, + }; + + // Roll ability scores for the Npc. + npc.roll_henchman_ability_scores(); + + // TODO: Need to actually test this process. + // Check if ability scores are modified correctly based on class requirements and modifiers. + // let ability_scores = npc.ability_scores.unwrap(); + } +} -- cgit v1.2.3