From 4b4f725c74520c3a374849df99a1a6595e1e260a Mon Sep 17 00:00:00 2001 From: David Gay Date: Wed, 31 Jan 2024 02:05:14 -0500 Subject: random npc command --- src/cli.rs | 12 +++++++++++- src/main.rs | 32 ++++++++++++++++++++++++++++++++ src/rules/npcs.rs | 2 +- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 4fdef22..bc6c3a8 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -24,7 +24,17 @@ pub fn cli() -> Command { .help("Output in CSV format"), ), ) - .subcommand(Command::new("magic").about("Generates a magic item")), + .subcommand(Command::new("magic").about("Generates a magic item")) + .subcommand( + Command::new("npc") + .about("Generates a random NPC with a class and magic item loadout") + .arg( + Arg::new("class") + .short('c') + .long("class") + .help("The class of the NPC, e.g. fighter"), + ), + ), ) .subcommand( Command::new("roll") diff --git a/src/main.rs b/src/main.rs index bc9e200..44c162c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,38 @@ fn main() { Some(("random", sub_matches)) => { let random_command = sub_matches.subcommand().unwrap(); match random_command { + ("npc", npc_matches) => { + let class_name = npc_matches.get_one::("class").unwrap_or_else(|| { + eprintln!("Error: Class is required."); + std::process::exit(1); + }); + let class_ref = CLASSES.get(&*class_name.to_lowercase()).unwrap_or_else(|| { + eprintln!("Class '{}' not found.", &*class_name); + std::process::exit(1); + }); + + let mut npc = Npc::new( + Some(RANDOM_TABLES.roll_table("npc_alignment")), + None, + Some(class_ref), + None, + None, + Vec::new(), + ); + + npc.add_random_magic_items(); + + println!( + "{} {}. {}", + npc.alignment.unwrap(), + npc.class.unwrap().name, + npc.magic_items + .iter() + .map(|item| item.name.clone()) + .collect::>() + .join(", "), + ); + } ("henchman", henchman_matches) => { let class_name = RANDOM_TABLES.roll_table("henchman_class").to_string(); let race_name = RANDOM_TABLES.roll_table("henchman_race").to_string(); diff --git a/src/rules/npcs.rs b/src/rules/npcs.rs index ab111de..b228454 100644 --- a/src/rules/npcs.rs +++ b/src/rules/npcs.rs @@ -1,7 +1,7 @@ use crate::dice::roll_formula; use crate::random_tables::RANDOM_TABLES; use crate::rules::ability_scores::{AbilityScore, AbilityScoreCollection}; -use crate::rules::classes::{Class, CLASSES}; +use crate::rules::classes::Class; use crate::rules::magic_items::{MagicItem, MAGIC_ITEMS}; use crate::rules::races::Race; use log::debug; -- cgit v1.2.3