From b971e4675393b288169de20f4ac30a0add010340 Mon Sep 17 00:00:00 2001 From: Oresavna Date: Sat, 8 May 2021 11:20:14 +0200 Subject: [PATCH] Add world to grid conversions to WindSim --- server/src/lib.rs | 6 ++++++ server/src/windsim/mod.rs | 27 +++++++-------------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/server/src/lib.rs b/server/src/lib.rs index 61b20c5247..cfc5f32ed9 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -377,6 +377,12 @@ impl Server { state.ecs_mut().insert(Arc::clone(&world)); state.ecs_mut().insert(index.clone()); + // begin wind simulation + state + .ecs_mut() + .insert(windsim::WindSim::new(&world.sim().get_size())); + tracing::info!("Initiated wind simulation"); + // Set starting time for the server. state.ecs_mut().write_resource::().0 = settings.start_time; diff --git a/server/src/windsim/mod.rs b/server/src/windsim/mod.rs index a279e97fe4..7bb4244101 100644 --- a/server/src/windsim/mod.rs +++ b/server/src/windsim/mod.rs @@ -5,7 +5,6 @@ use common::{terrain::TerrainChunkSize, vol::RectVolSize}; pub use fluid::step_fluid; use types::{WindGrid, X_SIZE, Y_SIZE, Z_SIZE}; use vek::*; -use world::sim::WorldSim; use common::{ comp::{Pos, Vel}, @@ -20,10 +19,10 @@ pub struct WindSim { } impl WindSim { - pub fn new(world_sim: &WorldSim) -> Self { + pub fn new(world_size: &Vec2) -> Self { Self { grid: WindGrid::default(), - blocks_per_cell: cell_size_in_blocks(world_sim), + blocks_per_cell: cell_size_in_blocks(world_size), } } @@ -48,8 +47,9 @@ impl WindSim { pub fn tick(&mut self, sources: Vec<(Pos, Vel)>, dt: &DeltaTime) { for (pos, vel) in sources { - self.grid - .add_velocity_source(pos.0.map(|e| e as usize), vel.0) + let cell_pos = self.world_to_grid(pos).unwrap_or(Vec3{x:0, y:0, z:0}); + let cell_vel = vel.0.map2(self.blocks_per_cell, |vi, si| vi / si as f32); + self.grid.add_velocity_source(cell_pos, cell_vel) } step_fluid( &mut self.grid.density, @@ -63,10 +63,9 @@ impl WindSim { } } -fn cell_size_in_blocks(world_sim: &WorldSim) -> Vec3 { - let world_chunks: Vec2 = world_sim.get_size(); +fn cell_size_in_blocks(world_chunks: &Vec2) -> Vec3 { + // world_blocks = world_chunks / blocks_per_chunk let blocks_per_chunk: Vec2 = TerrainChunkSize::RECT_SIZE; - let world_blocks: Vec2 = world_chunks.map2(blocks_per_chunk, |ai, bi| ai * bi); let grid_size = Vec3 { @@ -81,15 +80,3 @@ fn cell_size_in_blocks(world_sim: &WorldSim) -> Vec3 { z: 500, } } - -// pub fn init(state: &mut State) { -// let mut grid = WindGrid { -// x_vel: vec![0_f32; SIZE_1D].into_boxed_slice(), -// y_vel: vec![0_f32; SIZE_1D].into_boxed_slice(), -// z_vel: vec![0_f32; SIZE_1D].into_boxed_slice(), -// density: vec![0_f32; SIZE_1D].into_boxed_slice(), -// }; -// -// state.ecs_mut().insert(grid); -// -// }