Add config file for dungeon distribution

- Now you can specify probability of "spawn" of dungeon with specific
difficulty
This commit is contained in:
juliancoffee 2021-06-19 23:19:43 +03:00
parent e7f54d6306
commit e5c63b2ca7
2 changed files with 47 additions and 8 deletions

View File

@ -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),
])

View File

@ -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<i32>, 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 }