From 0338b6752768ba5d60cf7e857d1a9aaac7fcd8de Mon Sep 17 00:00:00 2001 From: Oresavna Date: Sat, 22 May 2021 11:18:59 +0200 Subject: [PATCH] add frequency control for sim - once per second --- server/src/sys/windsim.rs | 24 +++++++++++++++--------- server/src/windsim/mod.rs | 4 ++++ server/src/windsim/wind_grid.rs | 4 ++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/server/src/sys/windsim.rs b/server/src/sys/windsim.rs index 83b54fcdc8..db9e07afca 100644 --- a/server/src/sys/windsim.rs +++ b/server/src/sys/windsim.rs @@ -1,13 +1,13 @@ #![allow(dead_code)] -use crate::windsim::WindSim; +use crate::windsim::{WindSim, MS_BETWEEN_TICKS}; use common::{ comp::{Pos, Vel}, resources::DeltaTime, }; use common_ecs::{Job, Origin, Phase, System}; +use rand::Rng; use specs::{Read, Write}; use vek::*; -use rand::Rng; /// This system updates the wind grid for the entire map #[derive(Default)] @@ -22,25 +22,31 @@ impl<'a> System<'a> for Sys { fn run(_job: &mut Job, (dt, mut windsim): Self::SystemData) { let mut rng = rand::thread_rng(); + // 1000 chunks let wind_sources: Vec<(Pos, Vel)> = (0..1000) - .map(|y| { + .map(|vertical_chunk| { let r1 = rng.gen_range(0.0..std::f32::consts::PI / 2.0); let r2 = rng.gen_range(0.0..std::f32::consts::PI / 2.0); let r = (r1 + r2) / 2.0; ( Pos(Vec3 { - x: inline_tweak::tweak!(5000.0), - y: y as f32 * 32.0, + x: inline_tweak::tweak!(1000.0), + y: vertical_chunk as f32 * 32.0, z: inline_tweak::tweak!(200.0), }), Vel(Vec3 { - x: inline_tweak::tweak!(30.0) * r.sin(), - y: inline_tweak::tweak!(30.0) * r.cos(), - z: inline_tweak::tweak!(30.0) * rng.gen_range(-0.25..0.25), + x: inline_tweak::tweak!(300.0) * r.sin(), + y: inline_tweak::tweak!(300.0) * r.cos(), + z: inline_tweak::tweak!(10.0) * rng.gen_range(-0.25..0.25), }), ) }) .collect::>(); - windsim.tick(wind_sources, &dt); + // If MS_BETWEEN_TICKS is 1000 it runs the sim once per second + if windsim.ms_since_update >= MS_BETWEEN_TICKS { + windsim.tick(wind_sources, &DeltaTime((MS_BETWEEN_TICKS / 1000) as f32)); + } else { + windsim.ms_since_update += (dt.0 * 1000.0) as u32; + } } } diff --git a/server/src/windsim/mod.rs b/server/src/windsim/mod.rs index 94e67cb6f4..dac8bb01c1 100644 --- a/server/src/windsim/mod.rs +++ b/server/src/windsim/mod.rs @@ -18,11 +18,13 @@ pub const GRID_SIZE: Vec3 = Vec3 { y: Y_SIZE, z: Z_SIZE, }; +pub const MS_BETWEEN_TICKS: u32 = 1000; #[derive(Default)] pub struct WindSim { grid: WindGrid, blocks_per_cell: Vec3, + pub ms_since_update: u32, } impl WindSim { @@ -30,6 +32,7 @@ impl WindSim { Self { grid: WindGrid::default(), blocks_per_cell: cell_size_in_blocks(world_size), + ms_since_update: MS_BETWEEN_TICKS, } } @@ -75,6 +78,7 @@ impl WindSim { 0.1, true, ); + self.ms_since_update = 0; } } diff --git a/server/src/windsim/wind_grid.rs b/server/src/windsim/wind_grid.rs index ecf9b8ad29..a70f99e0d8 100644 --- a/server/src/windsim/wind_grid.rs +++ b/server/src/windsim/wind_grid.rs @@ -1,8 +1,8 @@ #![allow(dead_code)] use vek::*; -pub const X_SIZE: usize = 96; -pub const Y_SIZE: usize = 96; +pub const X_SIZE: usize = 256; +pub const Y_SIZE: usize = 256; pub const Z_SIZE: usize = 4; pub const SIZE_1D: usize = (X_SIZE + 2) * (Y_SIZE + 2) * (Z_SIZE + 2);