mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
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 veloren/veloren!840
This commit is contained in:
commit
a4eaeb1da6
@ -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)
|
||||
|
@ -1,5 +1,5 @@
|
||||
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,
|
||||
|
@ -10,6 +10,7 @@ pub mod events;
|
||||
pub mod input;
|
||||
pub mod metrics;
|
||||
pub mod settings;
|
||||
pub mod state_ext;
|
||||
pub mod sys;
|
||||
#[cfg(not(feature = "worldgen"))] mod test_world;
|
||||
|
||||
@ -21,25 +22,22 @@ use crate::{
|
||||
chunk_generator::ChunkGenerator,
|
||||
client::{Client, RegionSubscription},
|
||||
cmd::CHAT_COMMANDS,
|
||||
state_ext::StateExt,
|
||||
sys::sentinel::{DeletedEntities, TrackedComps},
|
||||
};
|
||||
use common::{
|
||||
assets, comp,
|
||||
effect::Effect,
|
||||
comp,
|
||||
event::{EventBus, ServerEvent},
|
||||
msg::{ClientMsg, ClientState, ServerInfo, ServerMsg},
|
||||
net::PostOffice,
|
||||
state::{State, TimeOfDay},
|
||||
sync::{Uid, WorldSyncExt},
|
||||
sync::WorldSyncExt,
|
||||
terrain::TerrainChunkSize,
|
||||
vol::{ReadVol, RectVolSize},
|
||||
};
|
||||
use log::{debug, error, warn};
|
||||
use log::{debug, error};
|
||||
use metrics::ServerMetrics;
|
||||
use specs::{
|
||||
join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity, RunNow,
|
||||
SystemData, WorldExt,
|
||||
};
|
||||
use specs::{join::Join, Builder, Entity as EcsEntity, RunNow, SystemData, WorldExt};
|
||||
use std::{
|
||||
i32,
|
||||
sync::Arc,
|
||||
@ -98,6 +96,7 @@ impl Server {
|
||||
state.ecs_mut().insert(sys::SubscriptionTimer::default());
|
||||
state.ecs_mut().insert(sys::TerrainSyncTimer::default());
|
||||
state.ecs_mut().insert(sys::TerrainTimer::default());
|
||||
state.ecs_mut().insert(sys::WaypointTimer::default());
|
||||
// Server-only components
|
||||
state.ecs_mut().register::<RegionSubscription>();
|
||||
state.ecs_mut().register::<Client>();
|
||||
@ -223,94 +222,6 @@ impl Server {
|
||||
/// Get a reference to the server's world.
|
||||
pub fn world(&self) -> &World { &self.world }
|
||||
|
||||
/// Build a static object entity
|
||||
pub fn create_object(
|
||||
&mut self,
|
||||
pos: comp::Pos,
|
||||
object: comp::object::Body,
|
||||
) -> EcsEntityBuilder {
|
||||
self.state
|
||||
.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
|
||||
pub fn create_projectile(
|
||||
state: &mut State,
|
||||
pos: comp::Pos,
|
||||
vel: comp::Vel,
|
||||
body: comp::Body,
|
||||
projectile: comp::Projectile,
|
||||
) -> EcsEntityBuilder {
|
||||
state
|
||||
.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)
|
||||
}
|
||||
|
||||
pub fn create_player_character(
|
||||
state: &mut State,
|
||||
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 = state.ecs().read_resource::<SpawnPoint>().0;
|
||||
|
||||
state.write_component(entity, body);
|
||||
state.write_component(entity, comp::Stats::new(name, body, main));
|
||||
state.write_component(entity, comp::Energy::new(1000));
|
||||
state.write_component(entity, comp::Controller::default());
|
||||
state.write_component(entity, comp::Pos(spawn_point));
|
||||
state.write_component(entity, comp::Vel(Vec3::zero()));
|
||||
state.write_component(entity, comp::Ori(Vec3::unit_y()));
|
||||
state.write_component(entity, comp::Gravity(1.0));
|
||||
state.write_component(entity, comp::CharacterState::default());
|
||||
state.write_component(entity, comp::Alignment::Owned(entity));
|
||||
state.write_component(entity, comp::Inventory::default());
|
||||
state.write_component(
|
||||
entity,
|
||||
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::default()),
|
||||
);
|
||||
// Make sure physics are accepted.
|
||||
state.write_component(entity, comp::ForceUpdate);
|
||||
|
||||
// Give the Admin component to the player if their name exists in admin list
|
||||
if server_settings.admins.contains(
|
||||
&state
|
||||
.ecs()
|
||||
.read_storage::<comp::Player>()
|
||||
.get(entity)
|
||||
.expect("Failed to fetch entity.")
|
||||
.alias,
|
||||
) {
|
||||
state.write_component(entity, comp::Admin);
|
||||
}
|
||||
// Tell the client its request was successful.
|
||||
if let Some(client) = state.ecs().write_storage::<Client>().get_mut(entity) {
|
||||
client.allow_state(ClientState::Character);
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle events coming through via the event bus
|
||||
|
||||
/// Execute a single server tick, handle input and update the game state by
|
||||
/// the given duration.
|
||||
pub fn tick(&mut self, _input: Input, dt: Duration) -> Result<Vec<Event>, Error> {
|
||||
@ -336,7 +247,6 @@ impl Server {
|
||||
// 8) Finish the tick, passing control of the main thread back
|
||||
// to the frontend
|
||||
|
||||
let before_tick_1 = Instant::now();
|
||||
// 1) Build up a list of events for this frame, to be passed to the frontend.
|
||||
let mut frontend_events = Vec::new();
|
||||
|
||||
@ -347,30 +257,49 @@ impl Server {
|
||||
|
||||
// 2)
|
||||
|
||||
let before_new_connections = Instant::now();
|
||||
|
||||
// 3) Handle inputs from clients
|
||||
frontend_events.append(&mut self.handle_new_connections()?);
|
||||
|
||||
let before_message_system = Instant::now();
|
||||
|
||||
// Run message recieving sys before the systems in common for decreased latency
|
||||
// (e.g. run before controller system)
|
||||
sys::message::Sys.run_now(&self.state.ecs());
|
||||
|
||||
let before_tick_4 = Instant::now();
|
||||
let before_state_tick = Instant::now();
|
||||
|
||||
// 4) Tick the server's LocalState.
|
||||
self.state.tick(dt, sys::add_server_systems);
|
||||
// 5) Fetch any generated `TerrainChunk`s and insert them into the terrain.
|
||||
// in sys/terrain.rs
|
||||
self.state.tick(dt, sys::add_server_systems, false);
|
||||
|
||||
let before_handle_events = Instant::now();
|
||||
|
||||
// Handle game events
|
||||
frontend_events.append(&mut self.handle_events());
|
||||
|
||||
let before_update_terrain_and_regions = Instant::now();
|
||||
|
||||
// Apply terrain changes and update the region map after processing server
|
||||
// events so that changes made by server events will be immediately
|
||||
// visble to client synchronization systems, minimizing the latency of
|
||||
// `ServerEvent` mediated effects
|
||||
self.state.update_region_map();
|
||||
self.state.apply_terrain_changes();
|
||||
|
||||
let before_sync = Instant::now();
|
||||
|
||||
// 6) Synchronise clients with the new state of the world.
|
||||
sys::run_sync_systems(self.state.ecs_mut());
|
||||
|
||||
let before_world_tick = Instant::now();
|
||||
|
||||
// Tick the world
|
||||
self.world.tick(dt);
|
||||
|
||||
// 5) Fetch any generated `TerrainChunk`s and insert them into the terrain.
|
||||
// in sys/terrain.rs
|
||||
|
||||
let before_tick_6 = Instant::now();
|
||||
// 6) Synchronise clients with the new state of the world.
|
||||
let before_entity_cleanup = Instant::now();
|
||||
|
||||
// Remove NPCs that are outside the view distances of all players
|
||||
// This is done by removing NPCs in unloaded chunks
|
||||
@ -392,8 +321,9 @@ impl Server {
|
||||
}
|
||||
}
|
||||
|
||||
let before_tick_7 = Instant::now();
|
||||
let end_of_server_tick = Instant::now();
|
||||
// 7) Update Metrics
|
||||
// Get system timing info
|
||||
let entity_sync_nanos = self
|
||||
.state
|
||||
.ecs()
|
||||
@ -412,31 +342,36 @@ impl Server {
|
||||
.read_resource::<sys::TerrainSyncTimer>()
|
||||
.nanos as i64;
|
||||
let terrain_nanos = self.state.ecs().read_resource::<sys::TerrainTimer>().nanos as i64;
|
||||
let total_sys_nanos = entity_sync_nanos
|
||||
+ message_nanos
|
||||
+ sentinel_nanos
|
||||
+ subscription_nanos
|
||||
+ terrain_sync_nanos
|
||||
+ terrain_nanos;
|
||||
let waypoint_nanos = self.state.ecs().read_resource::<sys::WaypointTimer>().nanos as i64;
|
||||
let total_sys_ran_in_dispatcher_nanos = terrain_nanos + waypoint_nanos;
|
||||
// Report timing info
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["input"])
|
||||
.set((before_tick_4 - before_tick_1).as_nanos() as i64 - message_nanos);
|
||||
.with_label_values(&["new connections"])
|
||||
.set((before_message_system - before_new_connections).as_nanos() as i64);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["state tick"])
|
||||
.set(
|
||||
(before_handle_events - before_tick_4).as_nanos() as i64
|
||||
- (total_sys_nanos - message_nanos),
|
||||
(before_handle_events - before_state_tick).as_nanos() as i64
|
||||
- total_sys_ran_in_dispatcher_nanos,
|
||||
);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["handle server events"])
|
||||
.set((before_tick_6 - before_handle_events).as_nanos() as i64);
|
||||
.set((before_update_terrain_and_regions - before_handle_events).as_nanos() as i64);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["entity deletion"])
|
||||
.set((before_tick_7 - before_tick_6).as_nanos() as i64);
|
||||
.with_label_values(&["update terrain and region map"])
|
||||
.set((before_sync - before_update_terrain_and_regions).as_nanos() as i64);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["world tick"])
|
||||
.set((before_entity_cleanup - before_world_tick).as_nanos() as i64);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["entity cleanup"])
|
||||
.set((end_of_server_tick - before_entity_cleanup).as_nanos() as i64);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["entity sync"])
|
||||
@ -445,6 +380,10 @@ impl Server {
|
||||
.tick_time
|
||||
.with_label_values(&["message"])
|
||||
.set(message_nanos);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["sentinel"])
|
||||
.set(sentinel_nanos);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["subscription"])
|
||||
@ -457,6 +396,11 @@ impl Server {
|
||||
.tick_time
|
||||
.with_label_values(&["terrain"])
|
||||
.set(terrain_nanos);
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["waypoint"])
|
||||
.set(waypoint_nanos);
|
||||
// Report other info
|
||||
self.metrics
|
||||
.player_online
|
||||
.set(self.state.ecs().read_storage::<Client>().join().count() as i64);
|
||||
@ -476,7 +420,7 @@ impl Server {
|
||||
self.metrics
|
||||
.tick_time
|
||||
.with_label_values(&["metrics"])
|
||||
.set(before_tick_7.elapsed().as_nanos() as i64);
|
||||
.set(end_of_server_tick.elapsed().as_nanos() as i64);
|
||||
|
||||
// 8) Finish the tick, pass control back to the frontend.
|
||||
|
||||
@ -588,118 +532,3 @@ impl Server {
|
||||
impl Drop for Server {
|
||||
fn drop(&mut self) { self.state.notify_registered_clients(ServerMsg::Shutdown); }
|
||||
}
|
||||
|
||||
trait StateExt {
|
||||
fn give_item(&mut self, entity: EcsEntity, item: comp::Item) -> bool;
|
||||
fn apply_effect(&mut self, entity: EcsEntity, effect: Effect);
|
||||
fn notify_registered_clients(&self, msg: ServerMsg);
|
||||
fn create_npc(
|
||||
&mut self,
|
||||
pos: comp::Pos,
|
||||
stats: comp::Stats,
|
||||
body: comp::Body,
|
||||
) -> EcsEntityBuilder;
|
||||
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())
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
222
server/src/state_ext.rs
Normal file
222
server/src/state_ext.rs
Normal file
@ -0,0 +1,222 @@
|
||||
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 = (
|
||||
|
@ -1,5 +1,6 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user