summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <eapoems@riseup.net>2024-01-31 02:05:14 -0500
committerDavid Gay <eapoems@riseup.net>2024-01-31 02:05:14 -0500
commit4b4f725c74520c3a374849df99a1a6595e1e260a (patch)
tree89f11094dd47d538137d472e4a986a7c90548b74
parent7470832b07c458cddfae1bfa7c348b6dcc02d3c0 (diff)
random npc command
-rw-r--r--src/cli.rs12
-rw-r--r--src/main.rs32
-rw-r--r--src/rules/npcs.rs2
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::<String>("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::<Vec<String>>()
+ .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;