diff --git a/client/src/lib.rs b/client/src/lib.rs index 3328a81ade..04672cd8b7 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -802,6 +802,7 @@ impl Client { | ClientGeneral::RequestSiteInfo(_) | ClientGeneral::UnlockSkillGroup(_) | ClientGeneral::RequestPlayerPhysics { .. } + | ClientGeneral::WindRequest(_) | ClientGeneral::RequestLossyTerrainCompression { .. } => { &mut self.in_game_stream }, diff --git a/common/net/src/msg/client.rs b/common/net/src/msg/client.rs index 0cc811ceb9..5648baaf04 100644 --- a/common/net/src/msg/client.rs +++ b/common/net/src/msg/client.rs @@ -88,6 +88,7 @@ pub enum ClientGeneral { RequestLossyTerrainCompression { lossy_terrain_compression: bool, }, + WindRequest(comp::Pos), } impl ClientMsg { @@ -126,9 +127,11 @@ impl ClientMsg { | ClientGeneral::RequestSiteInfo(_) | ClientGeneral::UnlockSkillGroup(_) | ClientGeneral::RequestPlayerPhysics { .. } + | ClientGeneral::WindRequest(_) | ClientGeneral::RequestLossyTerrainCompression { .. } => { c_type == ClientType::Game && presence.is_some() }, + //Always possible ClientGeneral::ChatMsg(_) | ClientGeneral::Command(_, _) diff --git a/common/net/src/msg/server.rs b/common/net/src/msg/server.rs index 05450e4db9..9a657c9d43 100644 --- a/common/net/src/msg/server.rs +++ b/common/net/src/msg/server.rs @@ -160,6 +160,8 @@ pub enum ServerGeneral { chunk: Result, }, TerrainBlockUpdates(CompressedData, Block>>), + // Windupdates from the WindGrid on server + WindUpdate(Vec3), // Always possible PlayerListUpdate(PlayerListUpdate), /// A message to go into the client chat box. The client is responsible for @@ -282,6 +284,7 @@ impl ServerMsg { | ServerGeneral::InventoryUpdate(_, _) | ServerGeneral::TerrainChunkUpdate { .. } | ServerGeneral::TerrainBlockUpdates(_) + | ServerGeneral::WindUpdate(_) | ServerGeneral::SetViewDistance(_) | ServerGeneral::Outcomes(_) | ServerGeneral::Knockback(_) diff --git a/server/src/client.rs b/server/src/client.rs index e856e6a82f..d7d1f1528f 100644 --- a/server/src/client.rs +++ b/server/src/client.rs @@ -109,6 +109,7 @@ impl Client { | ServerGeneral::Outcomes(_) | ServerGeneral::Knockback(_) | ServerGeneral::UpdatePendingTrade(_, _, _) + | ServerGeneral::WindUpdate(_) | ServerGeneral::FinishedTrade(_) => { self.in_game_stream.lock().unwrap().send(g) }, @@ -180,6 +181,7 @@ impl Client { | ServerGeneral::Knockback(_) | ServerGeneral::SiteEconomy(_) | ServerGeneral::UpdatePendingTrade(_, _, _) + | ServerGeneral::WindUpdate(_) | ServerGeneral::FinishedTrade(_) => { PreparedMsg::new(2, &g, &self.in_game_stream_params) }, diff --git a/server/src/sys/msg/in_game.rs b/server/src/sys/msg/in_game.rs index 9aa4e3ee40..52867b718f 100644 --- a/server/src/sys/msg/in_game.rs +++ b/server/src/sys/msg/in_game.rs @@ -1,4 +1,4 @@ -use crate::{client::Client, presence::Presence, Settings}; +use crate::{client::Client, presence::Presence, windsim::WindSim, Settings}; use common::{ comp::{ Admin, CanBuild, ControlEvent, Controller, ForceUpdate, Health, Ori, Player, Pos, SkillSet, @@ -38,6 +38,7 @@ impl Sys { player_physics_settings: &mut Write<'_, PlayerPhysicsSettings>, maybe_player: &Option<&Player>, maybe_admin: &Option<&Admin>, + wind_sim: &Read<'_, WindSim>, msg: ClientGeneral, ) -> Result<(), crate::error::Error> { let presence = match maybe_presence { @@ -258,6 +259,9 @@ impl Sys { } => { presence.lossy_terrain_compression = lossy_terrain_compression; }, + ClientGeneral::WindRequest(pos) => { + client.send(ServerGeneral::WindUpdate(wind_sim.get_velocity(pos)))?; + }, _ => tracing::error!("not a client_in_game msg"), } Ok(()) @@ -289,6 +293,7 @@ impl<'a> System<'a> for Sys { Write<'a, PlayerPhysicsSettings>, ReadStorage<'a, Player>, ReadStorage<'a, Admin>, + Read<'a, WindSim>, ); const NAME: &'static str = "msg::in_game"; @@ -317,6 +322,7 @@ impl<'a> System<'a> for Sys { mut player_physics_settings, players, admins, + wind_sim, ): Self::SystemData, ) { let mut server_emitter = server_event_bus.emitter(); @@ -351,6 +357,7 @@ impl<'a> System<'a> for Sys { &mut player_physics_settings, &player, &maybe_admin, + &wind_sim, msg, ) }); diff --git a/server/src/windsim/fluid.rs b/server/src/windsim/fluid.rs index af1a392f51..5f232aa560 100644 --- a/server/src/windsim/fluid.rs +++ b/server/src/windsim/fluid.rs @@ -1,10 +1,10 @@ -use super::types::*; +use super::wind_grid::*; /// Macro for indexing into a 1D array using 3D coordinates. macro_rules! IX { ( $x: expr, $y: expr, $z: expr ) => {{ $x as usize + (X_SIZE + 2) * ($y as usize + (Y_SIZE + 2) * $z as usize) }}; } -/// +/// /// * `b` - The type of border. 1 for vertical vel walls, 2 for side vel walls, /// 3 for back-front walls, 0 for dens. fn set_borders(grid: &mut Box<[f32]>, b: u8) { diff --git a/server/src/windsim/mod.rs b/server/src/windsim/mod.rs index 7bb4244101..96eec7d886 100644 --- a/server/src/windsim/mod.rs +++ b/server/src/windsim/mod.rs @@ -1,10 +1,10 @@ #![allow(dead_code)] pub mod fluid; -mod types; +mod wind_grid; use common::{terrain::TerrainChunkSize, vol::RectVolSize}; pub use fluid::step_fluid; -use types::{WindGrid, X_SIZE, Y_SIZE, Z_SIZE}; use vek::*; +use wind_grid::{WindGrid, X_SIZE, Y_SIZE, Z_SIZE}; use common::{ comp::{Pos, Vel}, @@ -12,6 +12,8 @@ use common::{ }; //use common_state::State; +pub const DEFAULT_POS: Vec3 = Vec3 { x: 0, y: 0, z: 0 }; + #[derive(Default)] pub struct WindSim { grid: WindGrid, @@ -45,9 +47,17 @@ impl WindSim { } } + // Takes a world position and returns a world velocity + pub fn get_velocity(&self, pos: Pos) -> Vec3 { + let cell_pos = self.world_to_grid(pos).unwrap_or(DEFAULT_POS); + let cell_vel = self.grid.get_velocity(cell_pos); + cell_vel.map2(self.blocks_per_cell, |vi, si| vi * si as f32) + } + + // Abstraction for running the simulation pub fn tick(&mut self, sources: Vec<(Pos, Vel)>, dt: &DeltaTime) { for (pos, vel) in sources { - let cell_pos = self.world_to_grid(pos).unwrap_or(Vec3{x:0, y:0, z:0}); + let cell_pos = self.world_to_grid(pos).unwrap_or(DEFAULT_POS); 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) } diff --git a/server/src/windsim/types.rs b/server/src/windsim/wind_grid.rs similarity index 95% rename from server/src/windsim/types.rs rename to server/src/windsim/wind_grid.rs index 3fbb884bb9..ecf9b8ad29 100644 --- a/server/src/windsim/types.rs +++ b/server/src/windsim/wind_grid.rs @@ -43,7 +43,7 @@ impl WindGrid { x + (X_SIZE + 2) * (y + (Y_SIZE + 2) * z) } - // Takes 3D grid position (not a world position) + // Takes 3D grid position (not a world position) returns grid velocity pub fn get_velocity(&self, pos: Vec3) -> Vec3 { let index = Self::get_index(pos.x, pos.y, pos.z); let x = self.x_vel[index] as f32;