veloren/rtsim/src/gen/mod.rs

70 lines
2.1 KiB
Rust
Raw Normal View History

2022-08-11 14:44:57 +00:00
pub mod site;
use crate::data::{
2022-08-13 16:58:10 +00:00
npc::{Npcs, Npc, Profession},
2022-08-11 14:44:57 +00:00
site::{Sites, Site},
Data,
Nature,
};
2022-05-16 19:11:49 +00:00
use hashbrown::HashMap;
use rand::prelude::*;
2022-08-11 14:44:57 +00:00
use tracing::info;
2022-08-14 15:38:31 +00:00
use common::{
rtsim::WorldSettings,
resources::TimeOfDay,
};
use world::{
site::SiteKind,
IndexRef,
World,
};
2022-05-16 19:11:49 +00:00
impl Data {
2022-08-14 15:38:31 +00:00
pub fn generate(settings: &WorldSettings, world: &World, index: IndexRef) -> Self {
let mut seed = [0; 32];
seed.iter_mut().zip(&mut index.seed.to_le_bytes()).for_each(|(dst, src)| *dst = *src);
let mut rng = SmallRng::from_seed(seed);
let mut this = Self {
nature: Nature::generate(world),
npcs: Npcs { npcs: Default::default() },
2022-08-11 14:44:57 +00:00
sites: Sites { sites: Default::default() },
2022-08-14 15:38:31 +00:00
time_of_day: TimeOfDay(settings.start_time),
};
2022-08-11 14:44:57 +00:00
// Register sites with rtsim
for (world_site_id, _) in index
.sites
.iter()
{
2022-08-11 14:44:57 +00:00
let site = Site::generate(world_site_id, world, index);
this.sites.create(site);
}
info!("Registering {} rtsim sites from world sites.", this.sites.len());
// Spawn some test entities at the sites
for (site_id, site) in this.sites.iter() {
2022-08-13 16:58:10 +00:00
let rand_wpos = |rng: &mut SmallRng| {
let wpos2d = site.wpos.map(|e| e + rng.gen_range(-10..10));
2022-08-13 16:58:10 +00:00
wpos2d.map(|e| e as f32 + 0.5)
.with_z(world.sim().get_alt_approx(wpos2d).unwrap_or(0.0))
};
2022-08-14 15:18:47 +00:00
for _ in 0..20 {
2022-08-14 14:20:22 +00:00
2022-08-15 10:02:38 +00:00
this.npcs.create(Npc::new(rng.gen(), rand_wpos(&mut rng)).with_home(site_id).with_profession(match rng.gen_range(0..15) {
2022-08-13 16:58:10 +00:00
0 => Profession::Hunter,
2022-08-14 15:18:47 +00:00
1 => Profession::Blacksmith,
2022-08-15 10:02:38 +00:00
2 => Profession::Chef,
3 => Profession::Alchemist,
5..=10 => Profession::Farmer,
2022-08-13 16:58:10 +00:00
_ => Profession::Guard,
}));
}
2022-08-13 16:58:10 +00:00
this.npcs.create(Npc::new(rng.gen(), rand_wpos(&mut rng)).with_home(site_id).with_profession(Profession::Merchant));
2022-05-16 19:11:49 +00:00
}
this
2022-05-16 19:11:49 +00:00
}
}