Merge branch 'juliancoffee/hot_reload_dungeon_difficulty' into 'master'

Add config file for dungeon distribution

See merge request veloren/veloren!2481
This commit is contained in:
Samuel Keiffer 2021-06-20 04:07:26 +00:00
commit b37106e71f
2 changed files with 57 additions and 8 deletions

View File

@ -0,0 +1,25 @@
/// Distribution of different dungeon levels.
///
/// first number is dungeon level, integer
/// second number is weight, any normal positive float (not a NaN, for example)
///
/// Values are relative to each other,
/// lesser weight means there will be less dungeons of that tier.
///
/// General rules:
/// 1) Weight should not be less then zero
/// 2) At least some of weights shouldn't be a zero
/// 3) Keep it synced with number of dungeon levels
/// 4) Keep these pairs sorted from lowest to highest tier
///
/// Tips:
/// 1) Set every probability to 0.0 and left one with any other number
/// and you will have map full of dungeons of same level
([
(0, 5.0),
(1, 4.0),
(2, 4.0),
(3, 2.0),
(4, 2.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,39 @@ 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)
.unwrap_or_else(|err| {
panic!(
"Failed to choose difficulty (check instruction in config). Error: {}",
err
)
});
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 +107,7 @@ impl Dungeon {
})
.collect(),
difficulty,
};
this
}
}
pub fn name(&self) -> &str { &self.name }