2019-03-03 22:02:38 +00:00
|
|
|
#![feature(drain_filter)]
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
pub mod client;
|
2019-04-23 09:53:45 +00:00
|
|
|
pub mod cmd;
|
2019-03-03 22:02:38 +00:00
|
|
|
pub mod error;
|
|
|
|
pub mod input;
|
|
|
|
|
|
|
|
// Reexports
|
2019-04-16 13:06:30 +00:00
|
|
|
pub use crate::{error::Error, input::Input};
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-05-09 17:58:16 +00:00
|
|
|
use crate::{
|
|
|
|
client::{Client, Clients},
|
|
|
|
cmd::CHAT_COMMANDS,
|
2019-05-06 14:26:10 +00:00
|
|
|
};
|
2019-03-03 22:02:38 +00:00
|
|
|
use common::{
|
2019-03-04 19:50:26 +00:00
|
|
|
comp,
|
2019-04-23 09:53:45 +00:00
|
|
|
msg::{ClientMsg, ClientState, RequestStateError, ServerMsg},
|
2019-03-03 22:02:38 +00:00
|
|
|
net::PostOffice,
|
2019-04-16 21:06:33 +00:00
|
|
|
state::{State, Uid},
|
2019-04-10 23:16:29 +00:00
|
|
|
terrain::TerrainChunk,
|
2019-03-03 22:02:38 +00:00
|
|
|
};
|
2019-05-09 17:58:16 +00:00
|
|
|
use specs::{
|
|
|
|
join::Join, saveload::MarkedBuilder, world::EntityBuilder as EcsEntityBuilder, Builder,
|
|
|
|
Entity as EcsEntity,
|
2019-05-06 14:26:10 +00:00
|
|
|
};
|
2019-05-21 22:31:38 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashSet,
|
|
|
|
i32,
|
|
|
|
net::SocketAddr,
|
|
|
|
sync::{mpsc, Arc},
|
|
|
|
time::Duration,
|
|
|
|
};
|
2019-05-09 17:58:16 +00:00
|
|
|
use threadpool::ThreadPool;
|
|
|
|
use vek::*;
|
|
|
|
use world::World;
|
2019-04-16 13:06:30 +00:00
|
|
|
|
2019-04-17 14:46:04 +00:00
|
|
|
const CLIENT_TIMEOUT: f64 = 20.0; // Seconds
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
const DEFAULT_WORLD_SEED: u32 = 1337;
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
pub enum Event {
|
2019-04-16 13:06:30 +00:00
|
|
|
ClientConnected { entity: EcsEntity },
|
|
|
|
ClientDisconnected { entity: EcsEntity },
|
|
|
|
Chat { entity: EcsEntity, msg: String },
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 22:04:39 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct SpawnPoint(Vec3<f32>);
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
pub struct Server {
|
2019-01-02 19:22:01 +00:00
|
|
|
state: State,
|
2019-05-16 17:40:32 +00:00
|
|
|
world: Arc<World>,
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
postoffice: PostOffice<ServerMsg, ClientMsg>,
|
2019-03-04 19:50:26 +00:00
|
|
|
clients: Clients,
|
2019-04-10 23:16:29 +00:00
|
|
|
|
|
|
|
thread_pool: ThreadPool,
|
2019-05-17 17:44:30 +00:00
|
|
|
chunk_tx: mpsc::Sender<(Vec2<i32>, TerrainChunk)>,
|
|
|
|
chunk_rx: mpsc::Receiver<(Vec2<i32>, TerrainChunk)>,
|
|
|
|
pending_chunks: HashSet<Vec2<i32>>,
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2019-05-06 14:26:10 +00:00
|
|
|
/// Create a new `Server` bound to the default socket.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn new() -> Result<Self, Error> {
|
2019-05-06 14:26:10 +00:00
|
|
|
Self::bind(SocketAddr::from(([0; 4], 59003)))
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Create a new server bound to the given socket.
|
2019-05-06 14:26:10 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn bind<A: Into<SocketAddr>>(addrs: A) -> Result<Self, Error> {
|
2019-04-10 23:16:29 +00:00
|
|
|
let (chunk_tx, chunk_rx) = mpsc::channel();
|
|
|
|
|
2019-04-16 21:06:33 +00:00
|
|
|
let mut state = State::new();
|
2019-05-21 22:31:38 +00:00
|
|
|
state
|
|
|
|
.ecs_mut()
|
2019-05-25 05:42:26 +00:00
|
|
|
.add_resource(SpawnPoint(Vec3::new(16_384.0, 16_384.0, 280.0)));
|
2019-04-16 21:06:33 +00:00
|
|
|
|
|
|
|
let mut this = Self {
|
|
|
|
state,
|
2019-05-16 17:40:32 +00:00
|
|
|
world: Arc::new(World::generate(DEFAULT_WORLD_SEED)),
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-05-06 14:26:10 +00:00
|
|
|
postoffice: PostOffice::bind(addrs.into())?,
|
2019-03-04 19:50:26 +00:00
|
|
|
clients: Clients::empty(),
|
2019-04-10 23:16:29 +00:00
|
|
|
|
|
|
|
thread_pool: threadpool::Builder::new()
|
|
|
|
.thread_name("veloren-worker".into())
|
|
|
|
.build(),
|
|
|
|
chunk_tx,
|
|
|
|
chunk_rx,
|
2019-04-10 23:41:37 +00:00
|
|
|
pending_chunks: HashSet::new(),
|
2019-04-16 21:06:33 +00:00
|
|
|
};
|
|
|
|
|
2019-05-21 22:04:39 +00:00
|
|
|
/*
|
2019-04-16 21:06:33 +00:00
|
|
|
for i in 0..4 {
|
2019-05-12 21:34:20 +00:00
|
|
|
this.create_npc(
|
|
|
|
"Tobermory".to_owned(),
|
|
|
|
comp::Body::Humanoid(comp::HumanoidBody::random()),
|
|
|
|
)
|
2019-05-22 20:53:24 +00:00
|
|
|
.with(comp::Actions::default())
|
2019-05-12 21:34:20 +00:00
|
|
|
.with(comp::Agent::Wanderer(Vec2::zero()))
|
|
|
|
.build();
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
2019-05-21 22:04:39 +00:00
|
|
|
*/
|
2019-04-16 21:06:33 +00:00
|
|
|
|
|
|
|
Ok(this)
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 15:13:11 +00:00
|
|
|
/// Get a reference to the server's game state.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-16 13:06:30 +00:00
|
|
|
pub fn state(&self) -> &State {
|
|
|
|
&self.state
|
|
|
|
}
|
2019-01-15 15:13:11 +00:00
|
|
|
/// Get a mutable reference to the server's game state.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-16 13:06:30 +00:00
|
|
|
pub fn state_mut(&mut self) -> &mut State {
|
|
|
|
&mut self.state
|
|
|
|
}
|
2019-01-12 15:57:19 +00:00
|
|
|
|
2019-01-15 15:13:11 +00:00
|
|
|
/// Get a reference to the server's world.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-16 13:06:30 +00:00
|
|
|
pub fn world(&self) -> &World {
|
|
|
|
&self.world
|
|
|
|
}
|
2019-01-15 15:13:11 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Build a non-player character.
|
2019-04-16 21:06:33 +00:00
|
|
|
#[allow(dead_code)]
|
2019-05-12 21:21:18 +00:00
|
|
|
pub fn create_npc(&mut self, name: String, body: comp::Body) -> EcsEntityBuilder {
|
2019-04-16 21:06:33 +00:00
|
|
|
self.state
|
|
|
|
.ecs_mut()
|
|
|
|
.create_entity_synced()
|
2019-04-23 22:48:31 +00:00
|
|
|
.with(comp::phys::Pos(Vec3::new(0.0, 0.0, 64.0)))
|
2019-04-16 21:06:33 +00:00
|
|
|
.with(comp::phys::Vel(Vec3::zero()))
|
2019-04-17 08:59:38 +00:00
|
|
|
.with(comp::phys::Dir(Vec3::unit_y()))
|
2019-05-22 20:53:24 +00:00
|
|
|
.with(comp::Inputs::default())
|
|
|
|
.with(comp::Actions::default())
|
2019-05-12 21:34:20 +00:00
|
|
|
.with(comp::Actor::Character { name, body })
|
2019-05-13 13:58:01 +00:00
|
|
|
.with(comp::Stats::default())
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 09:53:45 +00:00
|
|
|
pub fn create_player_character(
|
|
|
|
state: &mut State,
|
|
|
|
entity: EcsEntity,
|
|
|
|
client: &mut Client,
|
2019-05-12 21:21:18 +00:00
|
|
|
name: String,
|
2019-05-15 16:06:58 +00:00
|
|
|
body: comp::Body,
|
2019-04-23 09:53:45 +00:00
|
|
|
) {
|
2019-05-21 22:04:39 +00:00
|
|
|
let spawn_point = state.ecs().read_resource::<SpawnPoint>().0;
|
|
|
|
|
2019-05-15 16:06:58 +00:00
|
|
|
state.write_component(entity, comp::Actor::Character { name, body });
|
2019-05-19 15:48:56 +00:00
|
|
|
state.write_component(entity, comp::Stats::default());
|
2019-05-22 20:53:24 +00:00
|
|
|
state.write_component(entity, comp::Inputs::default());
|
|
|
|
state.write_component(entity, comp::Actions::default());
|
|
|
|
state.write_component(entity, comp::AnimationInfo::new());
|
2019-05-21 22:04:39 +00:00
|
|
|
state.write_component(entity, comp::phys::Pos(spawn_point));
|
2019-04-19 07:35:23 +00:00
|
|
|
state.write_component(entity, comp::phys::Vel(Vec3::zero()));
|
|
|
|
state.write_component(entity, comp::phys::Dir(Vec3::unit_y()));
|
2019-05-22 20:53:24 +00:00
|
|
|
// Make sure physics are accepted.
|
2019-04-19 07:35:23 +00:00
|
|
|
state.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Tell the client its request was successful.
|
2019-05-19 22:20:25 +00:00
|
|
|
client.allow_state(ClientState::Character);
|
2019-04-19 07:35:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Execute a single server tick, handle input and update the game state by the given duration.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn tick(&mut self, input: Input, dt: Duration) -> Result<Vec<Event>, Error> {
|
2019-01-02 17:23:31 +00:00
|
|
|
// This tick function is the centre of the Veloren universe. Most server-side things are
|
|
|
|
// managed from here, and as such it's important that it stays organised. Please consult
|
|
|
|
// the core developers before making significant changes to this code. Here is the
|
|
|
|
// approximate order of things. Please update it as this code changes.
|
|
|
|
//
|
|
|
|
// 1) Collect input from the frontend, apply input effects to the state of the game
|
|
|
|
// 2) Go through any events (timer-driven or otherwise) that need handling and apply them
|
|
|
|
// to the state of the game
|
|
|
|
// 3) Go through all incoming client network communications, apply them to the game state
|
|
|
|
// 4) Perform a single LocalState tick (i.e: update the world and entities in the world)
|
|
|
|
// 5) Go through the terrain update queue and apply all changes to the terrain
|
|
|
|
// 6) Send relevant state updates to all clients
|
|
|
|
// 7) Finish the tick, passing control of the main thread back to the frontend
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// 1) Build up a list of events for this frame, to be passed to the frontend.
|
2019-03-03 22:02:38 +00:00
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// If networking has problems, handle them.
|
2019-03-05 18:39:18 +00:00
|
|
|
if let Some(err) = self.postoffice.error() {
|
2019-03-03 22:02:38 +00:00
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// 2)
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// 3) Handle inputs from clients
|
|
|
|
frontend_events.append(&mut self.handle_new_connections()?);
|
2019-03-03 22:02:38 +00:00
|
|
|
frontend_events.append(&mut self.handle_new_messages()?);
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// 4) Tick the client's LocalState.
|
2019-01-02 17:23:31 +00:00
|
|
|
self.state.tick(dt);
|
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
// Tick the world
|
|
|
|
self.world.tick(dt);
|
|
|
|
|
2019-05-23 15:14:39 +00:00
|
|
|
// Sync deaths.
|
|
|
|
let todo_kill = (
|
|
|
|
&self.state.ecs().entities(),
|
|
|
|
&self.state.ecs().read_storage::<comp::Dying>(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
.map(|(entity, _)| entity)
|
|
|
|
.collect::<Vec<EcsEntity>>();
|
|
|
|
|
|
|
|
for entity in todo_kill {
|
|
|
|
self.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Dying>()
|
|
|
|
.remove(entity);
|
|
|
|
self.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Actions>()
|
|
|
|
.remove(entity);
|
|
|
|
if let Some(client) = self.clients.get_mut(&entity) {
|
|
|
|
client.force_state(ClientState::Dead);
|
|
|
|
} else {
|
|
|
|
//self.state.ecs_mut().delete_entity_synced(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle respawns
|
|
|
|
let todo_respawn = (
|
|
|
|
&self.state.ecs().entities(),
|
|
|
|
&self.state.ecs().read_storage::<comp::Respawn>(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
.map(|(entity, _)| entity)
|
|
|
|
.collect::<Vec<EcsEntity>>();
|
|
|
|
|
|
|
|
for entity in todo_respawn {
|
|
|
|
if let Some(client) = self.clients.get_mut(&entity) {
|
|
|
|
client.allow_state(ClientState::Character);
|
|
|
|
self.state.ecs_mut().write_storage::<comp::Respawn>().remove(entity);
|
|
|
|
self.state.write_component(entity, comp::Stats::default());
|
|
|
|
self.state.write_component(entity, comp::Actions::default());
|
2019-05-23 15:54:15 +00:00
|
|
|
self.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::phys::Pos>()
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|pos| pos.0.z += 100.0);
|
2019-05-23 15:14:39 +00:00
|
|
|
self.state.write_component(entity, comp::phys::Vel(Vec3::zero()));
|
|
|
|
self.state.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// 5) Fetch any generated `TerrainChunk`s and insert them into the terrain.
|
2019-05-17 09:22:32 +00:00
|
|
|
// Also, send the chunk data to anybody that is close by.
|
2019-05-06 12:11:03 +00:00
|
|
|
if let Ok((key, chunk)) = self.chunk_rx.try_recv() {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Send the chunk to all nearby players.
|
2019-04-10 23:16:29 +00:00
|
|
|
for (entity, player, pos) in (
|
2019-04-22 08:20:25 +00:00
|
|
|
&self.state.ecs().entities(),
|
|
|
|
&self.state.ecs().read_storage::<comp::Player>(),
|
2019-04-19 19:32:47 +00:00
|
|
|
&self.state.ecs().read_storage::<comp::phys::Pos>(),
|
2019-04-29 20:37:19 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-04-25 19:25:22 +00:00
|
|
|
let chunk_pos = self.state.terrain().pos_key(pos.0.map(|e| e as i32));
|
2019-05-09 17:58:16 +00:00
|
|
|
let dist = (Vec2::from(chunk_pos) - Vec2::from(key))
|
|
|
|
.map(|e: i32| e.abs())
|
2019-05-19 00:45:02 +00:00
|
|
|
.reduce_max() as u32;
|
2019-04-25 19:25:22 +00:00
|
|
|
|
2019-05-19 00:45:02 +00:00
|
|
|
if player.view_distance.map(|vd| dist < vd).unwrap_or(false) {
|
2019-04-29 20:37:19 +00:00
|
|
|
self.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::TerrainChunkUpdate {
|
|
|
|
key,
|
|
|
|
chunk: Box::new(chunk.clone()),
|
|
|
|
},
|
|
|
|
);
|
2019-04-25 19:25:22 +00:00
|
|
|
}
|
2019-04-10 23:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.state.insert_chunk(key, chunk);
|
2019-04-25 19:08:26 +00:00
|
|
|
self.pending_chunks.remove(&key);
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Remove chunks that are too far from players.
|
2019-04-25 19:08:26 +00:00
|
|
|
let mut chunks_to_remove = Vec::new();
|
2019-04-25 19:25:22 +00:00
|
|
|
self.state.terrain().iter().for_each(|(key, _)| {
|
2019-05-19 00:45:02 +00:00
|
|
|
let mut should_drop = true;
|
2019-04-25 19:08:26 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// For each player with a position, calculate the distance.
|
2019-05-19 00:45:02 +00:00
|
|
|
for (player, pos) in (
|
2019-04-25 19:08:26 +00:00
|
|
|
&self.state.ecs().read_storage::<comp::Player>(),
|
|
|
|
&self.state.ecs().read_storage::<comp::phys::Pos>(),
|
2019-04-29 20:37:19 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-04-25 19:08:26 +00:00
|
|
|
let chunk_pos = self.state.terrain().pos_key(pos.0.map(|e| e as i32));
|
2019-05-12 19:57:39 +00:00
|
|
|
let dist = Vec2::from(chunk_pos - key)
|
2019-05-19 00:45:02 +00:00
|
|
|
.map(|e: i32| e.abs() as u32)
|
2019-05-12 19:57:39 +00:00
|
|
|
.reduce_max();
|
2019-05-19 00:45:02 +00:00
|
|
|
|
|
|
|
if player.view_distance.map(|vd| dist <= vd).unwrap_or(false) {
|
|
|
|
should_drop = false;
|
|
|
|
}
|
2019-04-25 19:08:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 00:45:02 +00:00
|
|
|
if should_drop {
|
2019-04-25 19:25:22 +00:00
|
|
|
chunks_to_remove.push(key);
|
2019-04-25 19:08:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
for key in chunks_to_remove {
|
|
|
|
self.state.remove_chunk(key);
|
2019-04-10 23:16:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// 6) Synchronise clients with the new state of the world.
|
2019-03-04 19:50:26 +00:00
|
|
|
self.sync_clients();
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// 7) Finish the tick, pass control back to the frontend.
|
2019-03-03 22:02:38 +00:00
|
|
|
Ok(frontend_events)
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Clean up the server after a tick.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-30 12:11:34 +00:00
|
|
|
pub fn cleanup(&mut self) {
|
|
|
|
// Cleanup the local state
|
|
|
|
self.state.cleanup();
|
|
|
|
}
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Handle new client connections.
|
2019-03-03 22:02:38 +00:00
|
|
|
fn handle_new_connections(&mut self) -> Result<Vec<Event>, Error> {
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
2019-04-11 22:26:43 +00:00
|
|
|
for mut postbox in self.postoffice.new_postboxes() {
|
2019-04-16 13:06:30 +00:00
|
|
|
let entity = self.state.ecs_mut().create_entity_synced().build();
|
2019-04-21 18:12:29 +00:00
|
|
|
let mut client = Client {
|
|
|
|
client_state: ClientState::Connected,
|
|
|
|
postbox,
|
|
|
|
last_ping: self.state.get_time(),
|
|
|
|
};
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Return the state of the current world (all of the components that Sphynx tracks).
|
2019-04-21 18:12:29 +00:00
|
|
|
client.notify(ServerMsg::InitialSync {
|
|
|
|
ecs_state: self.state.ecs().gen_state_package(),
|
2019-05-17 09:22:32 +00:00
|
|
|
entity_uid: self.state.ecs().uid_from_entity(entity).unwrap().into(), // Can't fail.
|
2019-04-21 18:12:29 +00:00
|
|
|
});
|
2019-03-05 00:00:11 +00:00
|
|
|
|
2019-04-23 09:53:45 +00:00
|
|
|
self.clients.add(entity, client);
|
2019-04-16 13:06:30 +00:00
|
|
|
|
|
|
|
frontend_events.push(Event::ClientConnected { entity });
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(frontend_events)
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Handle new client messages.
|
2019-03-03 22:02:38 +00:00
|
|
|
fn handle_new_messages(&mut self) -> Result<Vec<Event>, Error> {
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
|
|
|
let state = &mut self.state;
|
|
|
|
let mut new_chat_msgs = Vec::new();
|
2019-03-05 00:00:11 +00:00
|
|
|
let mut disconnected_clients = Vec::new();
|
2019-04-10 23:41:37 +00:00
|
|
|
let mut requested_chunks = Vec::new();
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-04-10 23:16:29 +00:00
|
|
|
self.clients.remove_if(|entity, client| {
|
2019-04-10 17:23:27 +00:00
|
|
|
let mut disconnect = false;
|
2019-03-03 22:02:38 +00:00
|
|
|
let new_msgs = client.postbox.new_messages();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Update client ping.
|
2019-03-03 22:02:38 +00:00
|
|
|
if new_msgs.len() > 0 {
|
|
|
|
client.last_ping = state.get_time();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Process incoming messages.
|
2019-03-03 22:02:38 +00:00
|
|
|
for msg in new_msgs {
|
2019-04-19 19:32:47 +00:00
|
|
|
match msg {
|
|
|
|
ClientMsg::RequestState(requested_state) => match requested_state {
|
2019-04-21 18:12:29 +00:00
|
|
|
ClientState::Connected => disconnect = true, // Default state
|
2019-04-21 15:52:15 +00:00
|
|
|
ClientState::Registered => match client.client_state {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Use ClientMsg::Register instead.
|
2019-04-29 20:37:19 +00:00
|
|
|
ClientState::Connected => {
|
|
|
|
client.error_state(RequestStateError::WrongMessage)
|
|
|
|
}
|
|
|
|
ClientState::Registered => {
|
|
|
|
client.error_state(RequestStateError::Already)
|
|
|
|
}
|
2019-05-23 15:14:39 +00:00
|
|
|
ClientState::Spectator
|
|
|
|
| ClientState::Character
|
|
|
|
| ClientState::Dead => client.allow_state(ClientState::Registered),
|
2019-04-20 17:54:37 +00:00
|
|
|
},
|
|
|
|
ClientState::Spectator => match requested_state {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Become Registered first.
|
2019-04-29 20:37:19 +00:00
|
|
|
ClientState::Connected => {
|
|
|
|
client.error_state(RequestStateError::Impossible)
|
|
|
|
}
|
|
|
|
ClientState::Spectator => {
|
|
|
|
client.error_state(RequestStateError::Already)
|
|
|
|
}
|
2019-05-23 15:14:39 +00:00
|
|
|
ClientState::Registered
|
|
|
|
| ClientState::Character
|
|
|
|
| ClientState::Dead => client.allow_state(ClientState::Spectator),
|
2019-04-20 17:54:37 +00:00
|
|
|
},
|
2019-05-17 09:22:32 +00:00
|
|
|
// Use ClientMsg::Character instead.
|
2019-04-29 20:37:19 +00:00
|
|
|
ClientState::Character => {
|
|
|
|
client.error_state(RequestStateError::WrongMessage)
|
|
|
|
}
|
2019-05-23 15:14:39 +00:00
|
|
|
ClientState::Dead => client.error_state(RequestStateError::Impossible),
|
2019-04-19 19:32:47 +00:00
|
|
|
},
|
2019-04-21 18:12:29 +00:00
|
|
|
ClientMsg::Register { player } => match client.client_state {
|
2019-04-29 20:37:19 +00:00
|
|
|
ClientState::Connected => {
|
|
|
|
Self::initialize_player(state, entity, client, player)
|
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// Use RequestState instead (No need to send `player` again).
|
2019-04-22 14:25:37 +00:00
|
|
|
_ => client.error_state(RequestStateError::Impossible),
|
2019-04-19 19:32:47 +00:00
|
|
|
},
|
2019-05-19 00:45:02 +00:00
|
|
|
ClientMsg::SetViewDistance(view_distance) => match client.client_state {
|
|
|
|
ClientState::Character { .. } => {
|
|
|
|
state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Player>()
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|player| player.view_distance = Some(view_distance));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2019-05-12 21:21:18 +00:00
|
|
|
ClientMsg::Character { name, body } => match client.client_state {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Become Registered first.
|
2019-04-29 20:37:19 +00:00
|
|
|
ClientState::Connected => {
|
|
|
|
client.error_state(RequestStateError::Impossible)
|
|
|
|
}
|
2019-05-23 15:14:39 +00:00
|
|
|
ClientState::Registered
|
|
|
|
| ClientState::Spectator
|
|
|
|
| ClientState::Dead => {
|
2019-05-12 21:21:18 +00:00
|
|
|
Self::create_player_character(state, entity, client, name, body)
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
|
|
|
ClientState::Character => {
|
|
|
|
client.error_state(RequestStateError::Already)
|
|
|
|
}
|
2019-04-19 19:32:47 +00:00
|
|
|
},
|
|
|
|
ClientMsg::Chat(msg) => match client.client_state {
|
2019-04-29 20:37:19 +00:00
|
|
|
ClientState::Connected => {
|
|
|
|
client.error_state(RequestStateError::Impossible)
|
|
|
|
}
|
2019-04-23 09:53:45 +00:00
|
|
|
ClientState::Registered
|
|
|
|
| ClientState::Spectator
|
2019-05-23 15:14:39 +00:00
|
|
|
| ClientState::Dead
|
2019-04-23 09:53:45 +00:00
|
|
|
| ClientState::Character => new_chat_msgs.push((entity, msg)),
|
2019-04-19 19:32:47 +00:00
|
|
|
},
|
2019-05-23 15:14:39 +00:00
|
|
|
ClientMsg::PlayerInputs(mut inputs) => match client.client_state {
|
|
|
|
ClientState::Character | ClientState::Dead => {
|
|
|
|
state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Inputs>()
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|s| {
|
|
|
|
s.events.append(&mut inputs.events);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => client.error_state(RequestStateError::Impossible),
|
|
|
|
},
|
2019-05-17 20:47:58 +00:00
|
|
|
ClientMsg::PlayerAnimation(animation_info) => {
|
2019-04-29 20:37:19 +00:00
|
|
|
match client.client_state {
|
|
|
|
ClientState::Character => {
|
2019-05-17 20:47:58 +00:00
|
|
|
state.write_component(entity, animation_info)
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// Only characters can send animations.
|
2019-04-29 20:37:19 +00:00
|
|
|
_ => client.error_state(RequestStateError::Impossible),
|
|
|
|
}
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
2019-04-19 19:32:47 +00:00
|
|
|
ClientMsg::PlayerPhysics { pos, vel, dir } => match client.client_state {
|
|
|
|
ClientState::Character => {
|
2019-04-10 23:16:29 +00:00
|
|
|
state.write_component(entity, pos);
|
|
|
|
state.write_component(entity, vel);
|
|
|
|
state.write_component(entity, dir);
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// Only characters can send positions.
|
2019-04-22 14:25:37 +00:00
|
|
|
_ => client.error_state(RequestStateError::Impossible),
|
2019-04-19 19:32:47 +00:00
|
|
|
},
|
|
|
|
ClientMsg::TerrainChunkRequest { key } => match client.client_state {
|
2019-05-23 15:14:39 +00:00
|
|
|
ClientState::Connected
|
|
|
|
| ClientState::Registered
|
|
|
|
| ClientState::Dead => {
|
2019-04-22 14:25:37 +00:00
|
|
|
client.error_state(RequestStateError::Impossible);
|
|
|
|
}
|
2019-04-19 19:32:47 +00:00
|
|
|
ClientState::Spectator | ClientState::Character => {
|
2019-04-16 13:06:30 +00:00
|
|
|
match state.terrain().get_key(key) {
|
2019-04-29 20:37:19 +00:00
|
|
|
Some(chunk) => {
|
|
|
|
client.postbox.send_message(ServerMsg::TerrainChunkUpdate {
|
|
|
|
key,
|
|
|
|
chunk: Box::new(chunk.clone()),
|
|
|
|
})
|
|
|
|
}
|
2019-04-16 13:06:30 +00:00
|
|
|
None => requested_chunks.push(key),
|
|
|
|
}
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
|
|
|
},
|
2019-05-17 09:22:32 +00:00
|
|
|
// Always possible.
|
2019-04-20 17:54:37 +00:00
|
|
|
ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong),
|
2019-04-23 09:53:45 +00:00
|
|
|
ClientMsg::Pong => {}
|
2019-04-20 17:54:37 +00:00
|
|
|
ClientMsg::Disconnect => disconnect = true,
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 13:06:30 +00:00
|
|
|
} else if state.get_time() - client.last_ping > CLIENT_TIMEOUT || // Timeout
|
|
|
|
client.postbox.error().is_some()
|
|
|
|
// Postbox error
|
2019-03-03 22:02:38 +00:00
|
|
|
{
|
2019-04-10 17:23:27 +00:00
|
|
|
disconnect = true;
|
2019-03-06 13:51:01 +00:00
|
|
|
} else if state.get_time() - client.last_ping > CLIENT_TIMEOUT * 0.5 {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Try pinging the client if the timeout is nearing.
|
2019-04-11 22:26:43 +00:00
|
|
|
client.postbox.send_message(ServerMsg::Ping);
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:23:27 +00:00
|
|
|
if disconnect {
|
2019-04-10 23:16:29 +00:00
|
|
|
disconnected_clients.push(entity);
|
2019-04-23 12:01:16 +00:00
|
|
|
client.postbox.send_message(ServerMsg::Disconnect);
|
2019-03-03 22:02:38 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Handle new chat messages.
|
2019-04-10 17:23:27 +00:00
|
|
|
for (entity, msg) in new_chat_msgs {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Handle chat commands.
|
2019-04-16 13:06:30 +00:00
|
|
|
if msg.starts_with("/") && msg.len() > 1 {
|
|
|
|
let argv = String::from(&msg[1..]);
|
|
|
|
self.process_chat_cmd(entity, argv);
|
|
|
|
} else {
|
2019-04-22 00:38:29 +00:00
|
|
|
self.clients.notify_registered(ServerMsg::Chat(
|
2019-04-23 09:53:45 +00:00
|
|
|
match self.state.ecs().read_storage::<comp::Player>().get(entity) {
|
2019-04-16 13:06:30 +00:00
|
|
|
Some(player) => format!("[{}] {}", &player.alias, msg),
|
|
|
|
None => format!("[<anon>] {}", msg),
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
|
|
|
frontend_events.push(Event::Chat { entity, msg });
|
|
|
|
}
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Handle client disconnects.
|
2019-04-10 17:23:27 +00:00
|
|
|
for entity in disconnected_clients {
|
2019-04-16 13:06:30 +00:00
|
|
|
self.state.ecs_mut().delete_entity_synced(entity);
|
2019-03-05 00:00:11 +00:00
|
|
|
|
2019-04-16 13:06:30 +00:00
|
|
|
frontend_events.push(Event::ClientDisconnected { entity });
|
2019-03-05 00:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Generate requested chunks.
|
2019-04-10 23:41:37 +00:00
|
|
|
for key in requested_chunks {
|
|
|
|
self.generate_chunk(key);
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
Ok(frontend_events)
|
|
|
|
}
|
2019-03-04 19:50:26 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Initialize a new client states with important information.
|
2019-04-21 18:12:29 +00:00
|
|
|
fn initialize_player(
|
2019-04-17 19:22:34 +00:00
|
|
|
state: &mut State,
|
|
|
|
entity: specs::Entity,
|
|
|
|
client: &mut Client,
|
|
|
|
player: comp::Player,
|
|
|
|
) {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Save player metadata (for example the username).
|
2019-04-17 19:22:34 +00:00
|
|
|
state.write_component(entity, player);
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Sync logical information other players have authority over, not the server.
|
2019-05-17 20:47:58 +00:00
|
|
|
for (other_entity, &uid, &animation_info) in (
|
2019-04-22 08:20:25 +00:00
|
|
|
&state.ecs().entities(),
|
|
|
|
&state.ecs().read_storage::<common::state::Uid>(),
|
2019-05-17 20:47:58 +00:00
|
|
|
&state.ecs().read_storage::<comp::AnimationInfo>(),
|
2019-04-23 09:53:45 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-05-17 20:47:58 +00:00
|
|
|
// Animation
|
2019-05-19 18:08:29 +00:00
|
|
|
client.notify(ServerMsg::EntityAnimation {
|
2019-04-17 20:26:11 +00:00
|
|
|
entity: uid.into(),
|
2019-05-17 20:47:58 +00:00
|
|
|
animation_info: animation_info,
|
2019-04-17 20:26:11 +00:00
|
|
|
});
|
|
|
|
}
|
2019-04-19 19:32:47 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Tell the client its request was successful.
|
2019-04-21 15:52:15 +00:00
|
|
|
client.allow_state(ClientState::Registered);
|
2019-04-17 19:22:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Sync client states with the most up to date information.
|
2019-03-04 19:50:26 +00:00
|
|
|
fn sync_clients(&mut self) {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Sync 'logical' state using Sphynx.
|
2019-04-23 09:53:45 +00:00
|
|
|
self.clients
|
|
|
|
.notify_registered(ServerMsg::EcsSync(self.state.ecs_mut().next_sync_package()));
|
2019-04-14 20:30:27 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Sync 'physical' state.
|
2019-04-14 20:30:27 +00:00
|
|
|
for (entity, &uid, &pos, &vel, &dir, force_update) in (
|
2019-04-22 08:20:25 +00:00
|
|
|
&self.state.ecs().entities(),
|
|
|
|
&self.state.ecs().read_storage::<Uid>(),
|
|
|
|
&self.state.ecs().read_storage::<comp::phys::Pos>(),
|
|
|
|
&self.state.ecs().read_storage::<comp::phys::Vel>(),
|
|
|
|
&self.state.ecs().read_storage::<comp::phys::Dir>(),
|
2019-04-29 20:37:19 +00:00
|
|
|
self.state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::phys::ForceUpdate>()
|
|
|
|
.maybe(),
|
2019-04-23 09:53:45 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-04-14 20:30:27 +00:00
|
|
|
let msg = ServerMsg::EntityPhysics {
|
|
|
|
entity: uid.into(),
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
dir,
|
|
|
|
};
|
|
|
|
|
|
|
|
match force_update {
|
2019-04-22 00:38:29 +00:00
|
|
|
Some(_) => self.clients.notify_ingame(msg),
|
|
|
|
None => self.clients.notify_ingame_except(entity, msg),
|
2019-04-14 20:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 20:47:58 +00:00
|
|
|
// Sync animation.
|
|
|
|
for (entity, &uid, &animation_info) in (
|
2019-04-22 08:20:25 +00:00
|
|
|
&self.state.ecs().entities(),
|
|
|
|
&self.state.ecs().read_storage::<Uid>(),
|
2019-05-17 20:47:58 +00:00
|
|
|
&self.state.ecs().read_storage::<comp::AnimationInfo>(),
|
2019-04-23 09:53:45 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-05-17 20:47:58 +00:00
|
|
|
if animation_info.changed {
|
|
|
|
self.clients.notify_ingame_except(
|
|
|
|
entity,
|
|
|
|
ServerMsg::EntityAnimation {
|
|
|
|
entity: uid.into(),
|
|
|
|
animation_info: animation_info.clone(),
|
|
|
|
},
|
|
|
|
);
|
2019-04-17 17:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Remove all force flags.
|
2019-04-29 20:37:19 +00:00
|
|
|
self.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::phys::ForceUpdate>()
|
|
|
|
.clear();
|
2019-05-23 15:14:39 +00:00
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
2019-04-10 23:41:37 +00:00
|
|
|
|
2019-05-17 17:44:30 +00:00
|
|
|
pub fn generate_chunk(&mut self, key: Vec2<i32>) {
|
2019-04-10 23:41:37 +00:00
|
|
|
if self.pending_chunks.insert(key) {
|
|
|
|
let chunk_tx = self.chunk_tx.clone();
|
2019-05-16 17:40:32 +00:00
|
|
|
let world = self.world.clone();
|
2019-05-18 16:35:53 +00:00
|
|
|
self.thread_pool.execute(move || {
|
2019-05-18 17:36:45 +00:00
|
|
|
let _ = chunk_tx.send((key, world.generate_chunk(key))).unwrap();
|
2019-05-18 16:35:53 +00:00
|
|
|
});
|
2019-04-16 13:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:38:01 +00:00
|
|
|
fn process_chat_cmd(&mut self, entity: EcsEntity, cmd: String) {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Separate string into keyword and arguments.
|
2019-04-16 13:06:30 +00:00
|
|
|
let sep = cmd.find(' ');
|
|
|
|
let (kwd, args) = match sep {
|
|
|
|
Some(i) => (cmd[..i].to_string(), cmd[(i + 1)..].to_string()),
|
|
|
|
None => (cmd, "".to_string()),
|
|
|
|
};
|
2019-04-16 15:38:01 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Find the command object and run its handler.
|
2019-04-16 13:06:30 +00:00
|
|
|
let action_opt = CHAT_COMMANDS.iter().find(|x| x.keyword == kwd);
|
|
|
|
match action_opt {
|
2019-04-16 15:38:01 +00:00
|
|
|
Some(action) => action.execute(self, entity, args),
|
2019-05-17 09:22:32 +00:00
|
|
|
// Unknown command
|
2019-04-16 13:06:30 +00:00
|
|
|
None => {
|
|
|
|
self.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!(
|
2019-04-16 15:14:43 +00:00
|
|
|
"Unrecognised command: '/{}'\ntype '/help' for a list of available commands",
|
2019-04-16 13:06:30 +00:00
|
|
|
kwd
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
2019-04-10 23:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
2019-03-05 18:39:18 +00:00
|
|
|
|
|
|
|
impl Drop for Server {
|
|
|
|
fn drop(&mut self) {
|
2019-04-22 00:38:29 +00:00
|
|
|
self.clients.notify_registered(ServerMsg::Shutdown);
|
2019-03-05 18:39:18 +00:00
|
|
|
}
|
|
|
|
}
|