From 7a03c689f65000c72114075663b80d7f82c37d62 Mon Sep 17 00:00:00 2001 From: juliancoffee Date: Sat, 19 Jun 2021 23:19:43 +0300 Subject: [PATCH] Add config file for dungeon distribution - Now you can specify probability of "spawn" of dungeon with specific difficulty --- .../world/dungeon/difficulty_distribution.ron | 20 +++++++++++ world/src/site/dungeon/mod.rs | 35 ++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 assets/world/dungeon/difficulty_distribution.ron diff --git a/assets/world/dungeon/difficulty_distribution.ron b/assets/world/dungeon/difficulty_distribution.ron new file mode 100644 index 0000000000..09cd410377 --- /dev/null +++ b/assets/world/dungeon/difficulty_distribution.ron @@ -0,0 +1,20 @@ +/// Distribution of different dungeon levels. +/// +/// first number is dungeon level, integer +/// second number is probability, any normal positive float (not a NaN, for example) +/// +/// Values are relative to each other, +/// lesser probability means there will be less dungeons. +/// +/// General rules: +/// 1) Probability should not be less or equal to zero +/// 2) Keep it synced with number of dungeon levels +/// 3) Keep these pairs sorted from lowest to highest tier +([ + (0, 1.0), + (1, 1.0), + (2, 1.0), + (3, 1.0), + (4, 1.0), + (5, 1.0), +]) diff --git a/world/src/site/dungeon/mod.rs b/world/src/site/dungeon/mod.rs index 72f9763664..981e3f5650 100644 --- a/world/src/site/dungeon/mod.rs +++ b/world/src/site/dungeon/mod.rs @@ -9,7 +9,7 @@ use crate::{ }; use common::{ - assets::AssetHandle, + assets::{self, AssetExt, AssetHandle}, astar::Astar, comp::{self}, generation::{ChunkSupplement, EntityInfo}, @@ -20,7 +20,7 @@ use common::{ use core::{f32, hash::BuildHasherDefault}; use fxhash::FxHasher64; use lazy_static::lazy_static; -use rand::prelude::*; +use rand::{prelude::*, seq::SliceRandom}; use serde::Deserialize; use vek::*; @@ -47,13 +47,34 @@ pub struct Colors { const ALT_OFFSET: i32 = -2; +#[derive(Deserialize)] +struct DungeonDistribution(Vec<(u32, f32)>); +impl assets::Asset for DungeonDistribution { + type Loader = assets::RonLoader; + + const EXTENSION: &'static str = "ron"; +} + +lazy_static! { + static ref DUNGEON_DISTRIBUTION: Vec<(u32, f32)> = + DungeonDistribution::load_expect("world.dungeon.difficulty_distribution") + .read() + .0 + .clone(); +} + impl Dungeon { - #[allow(clippy::let_and_return)] // TODO: Pending review in #587 pub fn generate(wpos: Vec2, sim: Option<&WorldSim>, rng: &mut impl Rng) -> Self { let mut ctx = GenCtx { sim, rng }; - let difficulty = ctx.rng.gen_range(0..6); + + let difficulty = DUNGEON_DISTRIBUTION + .choose_weighted(&mut ctx.rng, |pair| pair.1) + .map(|(difficulty, _)| *difficulty) + .expect("this can never fail"); + let floors = 3 + difficulty / 2; - let this = Self { + + Self { name: { let name = NameGen::location(ctx.rng).generate(); match ctx.rng.gen_range(0..5) { @@ -81,9 +102,7 @@ impl Dungeon { }) .collect(), difficulty, - }; - - this + } } pub fn name(&self) -> &str { &self.name }