From c7fcd301e2f0cd2b3c754754722de2ce45e5880b Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Mon, 21 Aug 2023 09:01:58 +0100 Subject: [PATCH] character creation starting inventories --- src/gamesystem.rs | 1 + src/gui/race_selection.rs | 40 +++++++++++++++----------- src/map/interval_spawning_system.rs | 2 +- src/map_builders/prefab_builder/mod.rs | 10 +++---- src/particle_system.rs | 11 ------- src/raws/rawmaster.rs | 12 ++++++-- src/spawner.rs | 30 +++++++------------ 7 files changed, 50 insertions(+), 56 deletions(-) diff --git a/src/gamesystem.rs b/src/gamesystem.rs index f9e42fa..05e31f9 100644 --- a/src/gamesystem.rs +++ b/src/gamesystem.rs @@ -60,6 +60,7 @@ pub fn skill_bonus(skill: Skill, skills: &Skills) -> i32 { } /// Roll 4d6 and drop the lowest, for rolling d20-style stats +#[allow(unused)] pub fn roll_4d6(rng: &mut rltk::RandomNumberGenerator) -> i32 { let mut rolls: Vec = Vec::new(); for _i in 0..4 { diff --git a/src/gui/race_selection.rs b/src/gui/race_selection.rs index da197a5..5b13b6e 100644 --- a/src/gui/race_selection.rs +++ b/src/gui/race_selection.rs @@ -287,39 +287,45 @@ pub fn setup_player_class(ecs: &mut World, class: Classes) { fn get_starting_inventory(class: Classes, rng: &mut RandomNumberGenerator) -> (Vec, Vec) { let mut equipped: Vec = Vec::new(); let mut carried: Vec = Vec::new(); + let mut starting_food: &str = "1"; match class { Classes::Fighter => { + starting_food = "1d2+1"; equipped = vec![ "equip_shortsword".to_string(), "equip_body_ringmail".to_string(), "equip_mediumshield".to_string(), ]; - carried = vec!["food_rations".to_string()]; } Classes::Rogue => { + starting_food = "1d2+2"; equipped = vec!["equip_rapier".to_string(), "equip_body_weakleather".to_string()]; - carried = vec![ - "equip_dagger".to_string(), - "equip_dagger".to_string(), - "food_rations".to_string(), - "food_apple".to_string(), - ]; + carried = vec!["equip_dagger".to_string(), "equip_dagger".to_string()]; } Classes::Wizard => { + starting_food = "1d2+1"; equipped = vec!["equip_dagger".to_string(), "equip_back_protection".to_string()]; - carried = vec!["food_rations".to_string()]; - for _i in 0..rng.roll_dice(1, 3) { - carried.push(scroll_table(3).roll(rng)); - } - for _i in 0..rng.roll_dice(1, 2) - 1 { - carried.push(potion_table(3).roll(rng)); - } + pick_random_table_item(rng, &mut carried, "scrolls", "1d3", Some(3)); + pick_random_table_item(rng, &mut carried, "potions", "1d3-1", Some(3)); } Classes::Villager => { - let rolled_weapon = raws::table_by_name(&raws::RAWS.lock().unwrap(), "villager_equipment", 1).roll(rng); - equipped.push(rolled_weapon); - carried = vec!["food_rations".to_string(), "food_apple".to_string(), "food_apple".to_string()]; + starting_food = "1d3+2"; + pick_random_table_item(rng, &mut equipped, "villager_equipment", "1", None); } } + pick_random_table_item(rng, &mut carried, "food", starting_food, None); return (equipped, carried); } + +fn pick_random_table_item( + rng: &mut RandomNumberGenerator, + push_to: &mut Vec, + table: &'static str, + dice: &'static str, + difficulty: Option, +) { + let (n, d, m) = raws::parse_dice_string(dice); + for _i in 0..rng.roll_dice(n, d) + m { + push_to.push(raws::table_by_name(&raws::RAWS.lock().unwrap(), table, difficulty).roll(rng)); + } +} diff --git a/src/map/interval_spawning_system.rs b/src/map/interval_spawning_system.rs index 9cc83e5..e4f5a9b 100644 --- a/src/map/interval_spawning_system.rs +++ b/src/map/interval_spawning_system.rs @@ -43,7 +43,7 @@ fn spawn_random_mob_in_free_nonvisible_tile(ecs: &mut World) { } let mut spawn_locations: Vec<(i32, i32)> = Vec::new(); let mut rng = ecs.write_resource::(); - let key = spawner::mob_table(difficulty).roll(&mut rng); + let key = spawner::mob_table(Some(difficulty)).roll(&mut rng); let spawn_type = raws::get_mob_spawn_type(&raws::RAWS.lock().unwrap(), &key); let roll = raws::get_mob_spawn_amount(&mut rng, &spawn_type, player_level); for _i in 0..roll { diff --git a/src/map_builders/prefab_builder/mod.rs b/src/map_builders/prefab_builder/mod.rs index c97b1f1..74dd5b2 100644 --- a/src/map_builders/prefab_builder/mod.rs +++ b/src/map_builders/prefab_builder/mod.rs @@ -103,23 +103,23 @@ impl PrefabBuilder { } '%' => { build_data.map.tiles[idx] = TileType::Floor; - build_data.spawn_list.push((idx, spawner::food_table(difficulty).roll(rng))); + build_data.spawn_list.push((idx, spawner::food_table(Some(difficulty)).roll(rng))); } '!' => { build_data.map.tiles[idx] = TileType::Floor; - build_data.spawn_list.push((idx, spawner::potion_table(difficulty).roll(rng))); + build_data.spawn_list.push((idx, spawner::potion_table(Some(difficulty)).roll(rng))); } '/' => { build_data.map.tiles[idx] = TileType::Floor; - build_data.spawn_list.push((idx, spawner::wand_table(difficulty).roll(rng))); + build_data.spawn_list.push((idx, spawner::wand_table(Some(difficulty)).roll(rng))); } '?' => { build_data.map.tiles[idx] = TileType::Floor; - build_data.spawn_list.push((idx, spawner::scroll_table(difficulty).roll(rng))); + build_data.spawn_list.push((idx, spawner::scroll_table(Some(difficulty)).roll(rng))); } ')' => { build_data.map.tiles[idx] = TileType::Floor; - build_data.spawn_list.push((idx, spawner::equipment_table(difficulty).roll(rng))); + build_data.spawn_list.push((idx, spawner::equipment_table(Some(difficulty)).roll(rng))); } _ => { rltk::console::log(format!("Unknown glyph '{}' when loading prefab", (ch as u8) as char)); diff --git a/src/particle_system.rs b/src/particle_system.rs index 83af51c..558fc0d 100644 --- a/src/particle_system.rs +++ b/src/particle_system.rs @@ -139,17 +139,6 @@ impl ParticleBuilder { ); } - pub fn trap_triggered(&mut self, x: i32, y: i32) { - self.request( - x, - y, - rltk::RGB::named(rltk::RED), - rltk::RGB::named(rltk::RED), - rltk::to_cp437('‼'), - DEFAULT_PARTICLE_LIFETIME, - ); - } - pub fn kick(&mut self, x: i32, y: i32) { self.request( x, diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index c2e66b7..f0df572 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -593,9 +593,15 @@ fn get_renderable_component(renderable: &super::item_structs::Renderable) -> cra } } -pub fn table_by_name(raws: &RawMaster, key: &str, difficulty: i32) -> RandomTable { - let upper_bound = difficulty; - let lower_bound = if key != "mobs" { 0 } else { difficulty / 6 }; +pub fn table_by_name(raws: &RawMaster, key: &str, optional_difficulty: Option) -> RandomTable { + let (difficulty, upper_bound, lower_bound); + if optional_difficulty.is_some() { + difficulty = optional_difficulty.unwrap(); + upper_bound = difficulty; + lower_bound = if key != "mobs" { 0 } else { difficulty / 6 }; + } else { + difficulty = -1; + } if raws.table_index.contains_key(key) { let spawn_table = &raws.raws.spawn_tables[raws.table_index[key]]; diff --git a/src/spawner.rs b/src/spawner.rs index 04f9f84..0791602 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -53,14 +53,6 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { .marked::>() .build(); - raws::spawn_named_entity( - &raws::RAWS.lock().unwrap(), - ecs, - "food_rations", - raws::SpawnType::Carried { by: player }, - 0, - ); - return player; } @@ -118,7 +110,7 @@ pub fn spawn_region( }; // Roll on each table, getting an entity + spawn point if spawn_mob { - let key = mob_table(difficulty).roll(rng); + let key = mob_table(Some(difficulty)).roll(rng); let spawn_type = raws::get_mob_spawn_type(&raws::RAWS.lock().unwrap(), &key); let roll = raws::get_mob_spawn_amount(rng, &spawn_type, player_level); for _i in 0..roll { @@ -126,13 +118,13 @@ pub fn spawn_region( } } for _i in 0..num_traps { - let key = trap_table(difficulty).roll(rng); + let key = trap_table(Some(difficulty)).roll(rng); entity_to_spawn_list(rng, &mut areas, key, &mut spawn_points); } for _i in 0..num_items { // Player level isn't taken into account for item spawning, to encourage // delving deeper to gear up more quickly. - let key = get_random_item_category(rng, map.difficulty).roll(rng); + let key = get_random_item_category(rng, Some(map.difficulty)).roll(rng); entity_to_spawn_list(rng, &mut areas, key, &mut spawn_points); } // Push entities and their spawn points to map's spawn list @@ -189,7 +181,7 @@ fn debug_table() -> RandomTable { return RandomTable::new().add("debug", 1); } -fn get_random_item_category(rng: &mut RandomNumberGenerator, difficulty: i32) -> RandomTable { +fn get_random_item_category(rng: &mut RandomNumberGenerator, difficulty: Option) -> RandomTable { let item_category = item_category_table().roll(rng); match item_category.as_ref() { "equipment" => return equipment_table(difficulty), @@ -201,31 +193,31 @@ fn get_random_item_category(rng: &mut RandomNumberGenerator, difficulty: i32) -> }; } -pub fn equipment_table(difficulty: i32) -> RandomTable { +pub fn equipment_table(difficulty: Option) -> RandomTable { raws::table_by_name(&raws::RAWS.lock().unwrap(), "equipment", difficulty) } -pub fn potion_table(difficulty: i32) -> RandomTable { +pub fn potion_table(difficulty: Option) -> RandomTable { raws::table_by_name(&raws::RAWS.lock().unwrap(), "potions", difficulty) } -pub fn scroll_table(difficulty: i32) -> RandomTable { +pub fn scroll_table(difficulty: Option) -> RandomTable { raws::table_by_name(&raws::RAWS.lock().unwrap(), "scrolls", difficulty) } -pub fn wand_table(difficulty: i32) -> RandomTable { +pub fn wand_table(difficulty: Option) -> RandomTable { raws::table_by_name(&raws::RAWS.lock().unwrap(), "wands", difficulty) } -pub fn food_table(difficulty: i32) -> RandomTable { +pub fn food_table(difficulty: Option) -> RandomTable { raws::table_by_name(&raws::RAWS.lock().unwrap(), "food", difficulty) } /// Locks RAWS, and provides access to master list of all mobs. -pub fn mob_table(difficulty: i32) -> RandomTable { +pub fn mob_table(difficulty: Option) -> RandomTable { raws::table_by_name(&raws::RAWS.lock().unwrap(), "mobs", difficulty) } -pub fn trap_table(difficulty: i32) -> RandomTable { +pub fn trap_table(difficulty: Option) -> RandomTable { raws::table_by_name(&raws::RAWS.lock().unwrap(), "traps", difficulty) }