Commit a4eaeb1d authored by Imbris's avatar Imbris

Merge branch 'imbris/se-latency' into 'master'

Rearrange some operations in the server tick to reduce clientside latency of ServerEvent mediated effects

See merge request !840
parents 406767ae 14f0b018
Pipeline #125115492 failed with stages
in 61 minutes and 24 seconds
......@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed highlighting of non-collectible sprites
- Fixed /give_exp ignoring player argument
- Extend run sfx to small animals to prevent sneak attacks by geese.
- Decreased clientside latency of ServerEvent mediated effects (e.g. projectiles, inventory operations, etc)
### Removed
......
......@@ -407,7 +407,7 @@ impl Client {
// 3) Update client local data
// 4) Tick the client's LocalState
self.state.tick(dt, add_foreign_systems);
self.state.tick(dt, add_foreign_systems, true);
// 5) Terrain
let pos = self
......
......@@ -286,8 +286,35 @@ impl State {
}
}
// Run RegionMap tick to update entity region occupancy
pub fn update_region_map(&self) {
self.ecs.write_resource::<RegionMap>().tick(
self.ecs.read_storage::<comp::Pos>(),
self.ecs.read_storage::<comp::Vel>(),
self.ecs.entities(),
);
}
// Apply terrain changes
pub fn apply_terrain_changes(&self) {
let mut terrain = self.ecs.write_resource::<TerrainGrid>();
let mut modified_blocks = std::mem::replace(
&mut self.ecs.write_resource::<BlockChange>().blocks,
Default::default(),
);
// Apply block modifications
// Only include in `TerrainChanges` if successful
modified_blocks.retain(|pos, block| terrain.set(*pos, *block).is_ok());
self.ecs.write_resource::<TerrainChanges>().modified_blocks = modified_blocks;
}
/// Execute a single tick, simulating the game state by the given duration.
pub fn tick(&mut self, dt: Duration, add_foreign_systems: impl Fn(&mut DispatcherBuilder)) {
pub fn tick(
&mut self,
dt: Duration,
add_foreign_systems: impl Fn(&mut DispatcherBuilder),
update_terrain_and_regions: bool,
) {
// Change the time accordingly.
self.ecs.write_resource::<TimeOfDay>().0 += dt.as_secs_f64() * DAY_CYCLE_FACTOR;
self.ecs.write_resource::<Time>().0 += dt.as_secs_f64();
......@@ -297,12 +324,9 @@ impl State {
// important physics events.
self.ecs.write_resource::<DeltaTime>().0 = dt.as_secs_f32().min(MAX_DELTA_TIME);
// Run RegionMap tick to update entity region occupancy
self.ecs.write_resource::<RegionMap>().tick(
self.ecs.read_storage::<comp::Pos>(),
self.ecs.read_storage::<comp::Vel>(),
self.ecs.entities(),
);
if update_terrain_and_regions {
self.update_region_map();
}
// Run systems to update the world.
// Create and run a dispatcher for ecs systems.
......@@ -315,16 +339,9 @@ impl State {
self.ecs.maintain();
// Apply terrain changes
let mut terrain = self.ecs.write_resource::<TerrainGrid>();
let mut modified_blocks = std::mem::replace(
&mut self.ecs.write_resource::<BlockChange>().blocks,
Default::default(),
);
// Apply block modifications
// Only include in `TerrainChanges` if successful
modified_blocks.retain(|pos, block| terrain.set(*pos, *block).is_ok());
self.ecs.write_resource::<TerrainChanges>().modified_blocks = modified_blocks;
if update_terrain_and_regions {
self.apply_terrain_changes();
}
// Process local events
let events = self.ecs.read_resource::<EventBus<LocalEvent>>().recv_all();
......
......@@ -643,7 +643,7 @@ fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action:
.read_storage::<comp::Ori>()
.get(entity)
.copied();
/*let builder = server
/*let builder = server.state
.create_object(pos, ori, obj_type)
.with(ori);*/
if let (Some(pos), Some(ori)) = (pos, ori) {
......@@ -705,6 +705,7 @@ fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action:
},
};
server
.state
.create_object(pos, obj_type)
.with(comp::Ori(
// converts player orientation into a 90° rotation for the object by using the axis
......
......@@ -16,7 +16,7 @@ pub fn handle_create_character(
let state = &mut server.state;
let server_settings = &server.server_settings;
Server::create_player_character(state, entity, name, body, main, server_settings);
state.create_player_character(entity, name, body, main, server_settings);
sys::subscription::initialize_region_subscription(state.ecs(), entity);
}
......@@ -59,8 +59,7 @@ pub fn handle_shoot(
// TODO: Player height
pos.z += 1.2;
let mut builder =
Server::create_projectile(state, Pos(pos), Vel(dir * 100.0), body, projectile);
let mut builder = state.create_projectile(Pos(pos), Vel(dir * 100.0), body, projectile);
if let Some(light) = light {
builder = builder.with(light)
}
......@@ -73,6 +72,7 @@ pub fn handle_shoot(
pub fn handle_create_waypoint(server: &mut Server, pos: Vec3<f32>) {
server
.state
.create_object(Pos(pos), comp::object::Body::CampfireLit)
.with(LightEmitter {
offset: Vec3::unit_z() * 0.5,
......
......@@ -227,7 +227,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv
+ Vec3::unit_z() * 10.0
+ Vec3::<f32>::zero().map(|_| rand::thread_rng().gen::<f32>() - 0.5) * 4.0;
server
state
.create_object(Default::default(), comp::object::Body::Pouch)
.with(comp::Pos(pos.0 + Vec3::unit_z() * 0.25))
.with(item)
......
use super::Event;
use crate::{auth_provider::AuthProvider, client::Client, Server, StateExt};
use crate::{auth_provider::AuthProvider, client::Client, state_ext::StateExt, Server};
use common::{
comp,
comp::Player,
......
This diff is collapsed.
use crate::{client::Client, settings::ServerSettings, sys::sentinel::DeletedEntities, SpawnPoint};
use common::{
assets, comp,
effect::Effect,
msg::{ClientState, ServerMsg},
state::State,
sync::{Uid, WorldSyncExt},
};
use log::warn;
use specs::{Builder, Entity as EcsEntity, EntityBuilder as EcsEntityBuilder, Join, WorldExt};
use vek::*;
pub trait StateExt {
fn give_item(&mut self, entity: EcsEntity, item: comp::Item) -> bool;
fn apply_effect(&mut self, entity: EcsEntity, effect: Effect);
fn create_npc(
&mut self,
pos: comp::Pos,
stats: comp::Stats,
body: comp::Body,
) -> EcsEntityBuilder;
fn create_object(&mut self, pos: comp::Pos, object: comp::object::Body) -> EcsEntityBuilder;
fn create_projectile(
&mut self,
pos: comp::Pos,
vel: comp::Vel,
body: comp::Body,
projectile: comp::Projectile,
) -> EcsEntityBuilder;
fn create_player_character(
&mut self,
entity: EcsEntity,
name: String,
body: comp::Body,
main: Option<String>,
server_settings: &ServerSettings,
);
fn notify_registered_clients(&self, msg: ServerMsg);
fn delete_entity_recorded(
&mut self,
entity: EcsEntity,
) -> Result<(), specs::error::WrongGeneration>;
}
impl StateExt for State {
fn give_item(&mut self, entity: EcsEntity, item: comp::Item) -> bool {
let success = self
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(entity)
.map(|inv| inv.push(item).is_none())
.unwrap_or(false);
if success {
self.write_component(
entity,
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::Collected),
);
}
success
}
fn apply_effect(&mut self, entity: EcsEntity, effect: Effect) {
match effect {
Effect::Health(change) => {
self.ecs()
.write_storage::<comp::Stats>()
.get_mut(entity)
.map(|stats| stats.health.change_by(change));
},
Effect::Xp(xp) => {
self.ecs()
.write_storage::<comp::Stats>()
.get_mut(entity)
.map(|stats| stats.exp.change_by(xp));
},
}
}
/// Build a non-player character.
fn create_npc(
&mut self,
pos: comp::Pos,
stats: comp::Stats,
body: comp::Body,
) -> EcsEntityBuilder {
self.ecs_mut()
.create_entity_synced()
.with(pos)
.with(comp::Vel(Vec3::zero()))
.with(comp::Ori(Vec3::unit_y()))
.with(comp::Controller::default())
.with(body)
.with(stats)
.with(comp::Alignment::Npc)
.with(comp::Energy::new(500))
.with(comp::Gravity(1.0))
.with(comp::CharacterState::default())
}
/// Build a static object entity
fn create_object(&mut self, pos: comp::Pos, object: comp::object::Body) -> EcsEntityBuilder {
self.ecs_mut()
.create_entity_synced()
.with(pos)
.with(comp::Vel(Vec3::zero()))
.with(comp::Ori(Vec3::unit_y()))
.with(comp::Body::Object(object))
.with(comp::Mass(100.0))
.with(comp::Gravity(1.0))
//.with(comp::LightEmitter::default())
}
/// Build a projectile
fn create_projectile(
&mut self,
pos: comp::Pos,
vel: comp::Vel,
body: comp::Body,
projectile: comp::Projectile,
) -> EcsEntityBuilder {
self.ecs_mut()
.create_entity_synced()
.with(pos)
.with(vel)
.with(comp::Ori(vel.0.normalized()))
.with(comp::Mass(0.0))
.with(body)
.with(projectile)
.with(comp::Sticky)
}
fn create_player_character(
&mut self,
entity: EcsEntity,
name: String,
body: comp::Body,
main: Option<String>,
server_settings: &ServerSettings,
) {
// Give no item when an invalid specifier is given
let main = main.and_then(|specifier| assets::load_cloned(&specifier).ok());
let spawn_point = self.ecs().read_resource::<SpawnPoint>().0;
self.write_component(entity, body);
self.write_component(entity, comp::Stats::new(name, body, main));
self.write_component(entity, comp::Energy::new(1000));
self.write_component(entity, comp::Controller::default());
self.write_component(entity, comp::Pos(spawn_point));
self.write_component(entity, comp::Vel(Vec3::zero()));
self.write_component(entity, comp::Ori(Vec3::unit_y()));
self.write_component(entity, comp::Gravity(1.0));
self.write_component(entity, comp::CharacterState::default());
self.write_component(entity, comp::Alignment::Owned(entity));
self.write_component(entity, comp::Inventory::default());
self.write_component(
entity,
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::default()),
);
// Make sure physics are accepted.
self.write_component(entity, comp::ForceUpdate);
// Give the Admin component to the player if their name exists in admin list
if server_settings.admins.contains(
&self
.ecs()
.read_storage::<comp::Player>()
.get(entity)
.expect("Failed to fetch entity.")
.alias,
) {
self.write_component(entity, comp::Admin);
}
// Tell the client its request was successful.
if let Some(client) = self.ecs().write_storage::<Client>().get_mut(entity) {
client.allow_state(ClientState::Character);
}
}
fn notify_registered_clients(&self, msg: ServerMsg) {
for client in (&mut self.ecs().write_storage::<Client>())
.join()
.filter(|c| c.is_registered())
{
client.notify(msg.clone())
}
}
fn delete_entity_recorded(
&mut self,
entity: EcsEntity,
) -> Result<(), specs::error::WrongGeneration> {
let (maybe_uid, maybe_pos) = (
self.ecs().read_storage::<Uid>().get(entity).copied(),
self.ecs().read_storage::<comp::Pos>().get(entity).copied(),
);
let res = self.ecs_mut().delete_entity(entity);
if res.is_ok() {
if let (Some(uid), Some(pos)) = (maybe_uid, maybe_pos) {
if let Some(region_key) = self
.ecs()
.read_resource::<common::region::RegionMap>()
.find_region(entity, pos.0)
{
self.ecs()
.write_resource::<DeletedEntities>()
.record_deleted_entity(uid, region_key);
} else {
// Don't panic if the entity wasn't found in a region maybe it was just created
// and then deleted before the region manager had a chance to assign it a
// region
warn!(
"Failed to find region containing entity during entity deletion, assuming \
it wasn't sent to any clients and so deletion doesn't need to be \
recorded for sync purposes"
);
}
}
}
res
}
}
......@@ -15,30 +15,35 @@ pub type SentinelTimer = SysTimer<sentinel::Sys>;
pub type SubscriptionTimer = SysTimer<subscription::Sys>;
pub type TerrainTimer = SysTimer<terrain::Sys>;
pub type TerrainSyncTimer = SysTimer<terrain_sync::Sys>;
pub type WaypointTimer = SysTimer<waypoint::Sys>;
// System names
const ENTITY_SYNC_SYS: &str = "server_entity_sync_sys";
const SENTINEL_SYS: &str = "sentinel_sys";
const SUBSCRIPTION_SYS: &str = "server_subscription_sys";
const TERRAIN_SYNC_SYS: &str = "server_terrain_sync_sys";
// Note: commented names may be useful in the future
//const ENTITY_SYNC_SYS: &str = "server_entity_sync_sys";
//const SENTINEL_SYS: &str = "sentinel_sys";
//const SUBSCRIPTION_SYS: &str = "server_subscription_sys";
//const TERRAIN_SYNC_SYS: &str = "server_terrain_sync_sys";
const TERRAIN_SYS: &str = "server_terrain_sys";
const WAYPOINT_SYS: &str = "waypoint_sys";
pub fn add_server_systems(dispatch_builder: &mut DispatcherBuilder) {
// TODO: makes some of these dependent on systems in common like the phys system
dispatch_builder.add(sentinel::Sys, SENTINEL_SYS, &[common::sys::PHYS_SYS]);
dispatch_builder.add(subscription::Sys, SUBSCRIPTION_SYS, &[
common::sys::PHYS_SYS,
]);
dispatch_builder.add(entity_sync::Sys, ENTITY_SYNC_SYS, &[
SUBSCRIPTION_SYS,
SENTINEL_SYS,
]);
dispatch_builder.add(terrain_sync::Sys, TERRAIN_SYS, &[]);
dispatch_builder.add(terrain::Sys, TERRAIN_SYNC_SYS, &[TERRAIN_SYS]);
dispatch_builder.add(terrain::Sys, TERRAIN_SYS, &[]);
dispatch_builder.add(waypoint::Sys, WAYPOINT_SYS, &[]);
}
pub fn run_sync_systems(ecs: &mut specs::World) {
use specs::RunNow;
// Setup for entity sync
// If I'm not mistaken, these two could be ran in parallel
sentinel::Sys.run_now(ecs);
subscription::Sys.run_now(ecs);
// Sync
terrain_sync::Sys.run_now(ecs);
entity_sync::Sys.run_now(ecs);
}
/// Used to keep track of how much time each system takes
pub struct SysTimer<S> {
pub nanos: u64,
......
......@@ -8,12 +8,8 @@ use common::{
};
use specs::{Join, Read, ReadExpect, ReadStorage, System, Write, WriteStorage};
/// This system will handle loading generated chunks and unloading
/// uneeded chunks.
/// 1. Inserts newly generated chunks into the TerrainGrid
/// 2. Sends new chunks to neaby clients
/// 3. Handles the chunk's supplement (e.g. npcs)
/// 4. Removes chunks outside the range of players
/// This systems sends new chunks to clients as well as changes to existing
/// chunks
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
......
use super::SysTimer;
use common::comp::{Player, Pos, Waypoint, WaypointArea};
use specs::{Entities, Join, ReadStorage, System, WriteStorage};
use specs::{Entities, Join, ReadStorage, System, Write, WriteStorage};
/// This system updates player waypoints
/// TODO: Make this faster by only considering local waypoints
......@@ -11,12 +12,15 @@ impl<'a> System<'a> for Sys {
ReadStorage<'a, Player>,
ReadStorage<'a, WaypointArea>,
WriteStorage<'a, Waypoint>,
Write<'a, SysTimer<Self>>,
);
fn run(
&mut self,
(entities, positions, players, waypoint_areas, mut waypoints): Self::SystemData,
(entities, positions, players, waypoint_areas, mut waypoints, mut timer): Self::SystemData,
) {
timer.start();
for (entity, player_pos, _) in (&entities, &positions, &players).join() {
for (waypoint_pos, waypoint_area) in (&positions, &waypoint_areas).join() {
if player_pos.0.distance_squared(waypoint_pos.0) < waypoint_area.radius().powf(2.0)
......@@ -25,5 +29,7 @@ impl<'a> System<'a> for Sys {
}
}
}
timer.end();
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment