From e204789ce9ce2eebd74aa337f80cf1215bb4e366 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Sun, 14 Aug 2022 15:20:22 +0100 Subject: [PATCH] Persist TimeOfDay with rtsim --- rtsim/src/data/mod.rs | 3 ++- rtsim/src/event.rs | 5 +++-- rtsim/src/gen/mod.rs | 4 ++-- rtsim/src/lib.rs | 7 ++++--- rtsim/src/rule/npc_ai.rs | 4 ++-- server/src/lib.rs | 6 ++++-- server/src/rtsim2/mod.rs | 2 +- server/src/rtsim2/tick.rs | 6 ++++-- 8 files changed, 22 insertions(+), 15 deletions(-) diff --git a/rtsim/src/data/mod.rs b/rtsim/src/data/mod.rs index ed6bf14210..84fc342ec5 100644 --- a/rtsim/src/data/mod.rs +++ b/rtsim/src/data/mod.rs @@ -8,6 +8,7 @@ pub use self::{ nature::Nature, }; +use common::resources::TimeOfDay; use enum_map::{EnumMap, EnumArray, enum_map}; use serde::{Serialize, Deserialize, ser, de}; use std::{ @@ -29,7 +30,7 @@ pub struct Data { pub npcs: Npcs, pub sites: Sites, - pub time: f64, + pub time_of_day: TimeOfDay, } pub type ReadError = rmp_serde::decode::Error; diff --git a/rtsim/src/event.rs b/rtsim/src/event.rs index b60b9825f4..72932e9668 100644 --- a/rtsim/src/event.rs +++ b/rtsim/src/event.rs @@ -1,4 +1,4 @@ -use common::resources::Time; +use common::resources::{Time, TimeOfDay}; use world::{World, IndexRef}; use super::{Rule, RtState}; @@ -18,7 +18,8 @@ impl Event for OnSetup {} #[derive(Clone)] pub struct OnTick { + pub time_of_day: TimeOfDay, + pub time: Time, pub dt: f32, - pub time: f64, } impl Event for OnTick {} diff --git a/rtsim/src/gen/mod.rs b/rtsim/src/gen/mod.rs index 4ecc42ee14..97fe5b730e 100644 --- a/rtsim/src/gen/mod.rs +++ b/rtsim/src/gen/mod.rs @@ -26,7 +26,7 @@ impl Data { npcs: Npcs { npcs: Default::default() }, sites: Sites { sites: Default::default() }, - time: 0.0, + time_of_day: Default::default(), }; // Register sites with rtsim @@ -47,7 +47,7 @@ impl Data { .with_z(world.sim().get_alt_approx(wpos2d).unwrap_or(0.0)) }; for _ in 0..10 { - + this.npcs.create(Npc::new(rng.gen(), rand_wpos(&mut rng)).with_home(site_id).with_profession(match rng.gen_range(0..10) { 0 => Profession::Hunter, 1..=4 => Profession::Farmer, diff --git a/rtsim/src/lib.rs b/rtsim/src/lib.rs index 9f1ed63bc6..9152bc9d11 100644 --- a/rtsim/src/lib.rs +++ b/rtsim/src/lib.rs @@ -10,6 +10,7 @@ pub use self::{ event::{Event, EventCtx, OnTick}, rule::{Rule, RuleError}, }; +use common::resources::{Time, TimeOfDay}; use world::{World, IndexRef}; use anymap2::SendSyncAnyMap; use tracing::{info, error}; @@ -110,9 +111,9 @@ impl RtState { .for_each(|f| f(self, world, index, &e))); } - pub fn tick(&mut self, world: &World, index: IndexRef, dt: f32) { - self.data_mut().time += dt as f64; - let event = OnTick { dt, time: self.data().time }; + pub fn tick(&mut self, world: &World, index: IndexRef, time_of_day: TimeOfDay, time: Time, dt: f32) { + self.data_mut().time_of_day = time_of_day; + let event = OnTick { time_of_day, time, dt }; self.emit(event, world, index); } } diff --git a/rtsim/src/rule/npc_ai.rs b/rtsim/src/rule/npc_ai.rs index 2d7678375f..8579bba26e 100644 --- a/rtsim/src/rule/npc_ai.rs +++ b/rtsim/src/rule/npc_ai.rs @@ -53,8 +53,8 @@ impl Rule for NpcAi { npc.target = Some(( npc.wpos + Vec3::new( - ctx.event.time.sin() as f32 * 16.0, - ctx.event.time.cos() as f32 * 16.0, + ctx.event.time.0.sin() as f32 * 16.0, + ctx.event.time.0.cos() as f32 * 16.0, 0.0, ), 1.0, diff --git a/server/src/lib.rs b/server/src/lib.rs index 03c5ef4fa3..de44b7edc1 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -66,7 +66,6 @@ use crate::{ login_provider::LoginProvider, persistence::PersistedComponents, presence::{Presence, RegionSubscription, RepositionOnChunkLoad}, - // rtsim::RtSim, state_ext::StateExt, sys::sentinel::DeletedEntities, }; @@ -567,7 +566,10 @@ impl Server { #[cfg(feature = "worldgen")] { match rtsim2::RtSim::new(index.as_index_ref(), &world, data_dir.to_owned()) { - Ok(rtsim) => state.ecs_mut().insert(rtsim), + Ok(rtsim) => { + state.ecs_mut().insert(rtsim.state().data().time_of_day); + state.ecs_mut().insert(rtsim); + }, Err(err) => { error!("Failed to load rtsim: {}", err); return Err(Error::RtsimError(err)); diff --git a/server/src/rtsim2/mod.rs b/server/src/rtsim2/mod.rs index 748b99b054..6f2faf4357 100644 --- a/server/src/rtsim2/mod.rs +++ b/server/src/rtsim2/mod.rs @@ -44,7 +44,7 @@ impl RtSim { pub fn new(index: IndexRef, world: &World, data_dir: PathBuf) -> Result { let file_path = Self::get_file_path(data_dir); - info!("Looking for rtsim data in {}...", file_path.display()); + info!("Looking for rtsim data at {}...", file_path.display()); let data = 'load: { if std::env::var("RTSIM_NOLOAD").map_or(true, |v| v != "1") { match File::open(&file_path) { diff --git a/server/src/rtsim2/tick.rs b/server/src/rtsim2/tick.rs index 457a136fda..8566d9d653 100644 --- a/server/src/rtsim2/tick.rs +++ b/server/src/rtsim2/tick.rs @@ -6,7 +6,7 @@ use common::{ comp::{self, inventory::loadout::Loadout, skillset::skills}, event::{EventBus, ServerEvent}, generation::{BodyBuilder, EntityConfig, EntityInfo}, - resources::{DeltaTime, Time}, + resources::{DeltaTime, Time, TimeOfDay}, rtsim::{RtSimController, RtSimEntity}, slowjob::SlowJobPool, LoadoutBuilder, @@ -24,6 +24,7 @@ impl<'a> System<'a> for Sys { type SystemData = ( Read<'a, DeltaTime>, Read<'a, Time>, + Read<'a, TimeOfDay>, Read<'a, EventBus>, WriteExpect<'a, RtSim>, ReadExpect<'a, Arc>, @@ -43,6 +44,7 @@ impl<'a> System<'a> for Sys { ( dt, time, + time_of_day, mut server_event_bus, mut rtsim, world, @@ -56,7 +58,7 @@ impl<'a> System<'a> for Sys { let mut emitter = server_event_bus.emitter(); let rtsim = &mut *rtsim; - rtsim.state.tick(&world, index.as_index_ref(), dt.0); + rtsim.state.tick(&world, index.as_index_ref(), *time_of_day, *time, dt.0); if rtsim .last_saved