Persist TimeOfDay with rtsim

This commit is contained in:
Joshua Barretto 2022-08-14 15:20:22 +01:00
parent 64c56f544d
commit e204789ce9
8 changed files with 22 additions and 15 deletions

View File

@ -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;

View File

@ -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 {}

View File

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

View File

@ -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);
}
}

View File

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

View File

@ -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));

View File

@ -44,7 +44,7 @@ impl RtSim {
pub fn new(index: IndexRef, world: &World, data_dir: PathBuf) -> Result<Self, ron::Error> {
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) {

View File

@ -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<ServerEvent>>,
WriteExpect<'a, RtSim>,
ReadExpect<'a, Arc<world::World>>,
@ -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