Add WindUpdate and WindRequest messages

This commit is contained in:
Oresavna 2021-05-11 12:43:50 +02:00 committed by Ludvig Böklin
parent b971e46753
commit c885aa3607
8 changed files with 33 additions and 7 deletions

View File

@ -802,6 +802,7 @@ impl Client {
| ClientGeneral::RequestSiteInfo(_)
| ClientGeneral::UnlockSkillGroup(_)
| ClientGeneral::RequestPlayerPhysics { .. }
| ClientGeneral::WindRequest(_)
| ClientGeneral::RequestLossyTerrainCompression { .. } => {
&mut self.in_game_stream
},

View File

@ -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(_, _)

View File

@ -160,6 +160,8 @@ pub enum ServerGeneral {
chunk: Result<SerializedTerrainChunk, ()>,
},
TerrainBlockUpdates(CompressedData<HashMap<Vec3<i32>, Block>>),
// Windupdates from the WindGrid on server
WindUpdate(Vec3<f32>),
// 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(_)

View File

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

View File

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

View File

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

View File

@ -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<usize> = 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<f32> {
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)
}

View File

@ -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<usize>) -> Vec3<f32> {
let index = Self::get_index(pos.x, pos.y, pos.z);
let x = self.x_vel[index] as f32;