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;
|
|
|
|
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-04-16 13:06:30 +00:00
|
|
|
use crate::client::{Client, ClientState, Clients};
|
2019-03-03 22:02:38 +00:00
|
|
|
use common::{
|
2019-03-04 19:50:26 +00:00
|
|
|
comp,
|
2019-04-16 13:06:30 +00:00
|
|
|
msg::{ClientMsg, ServerMsg},
|
2019-03-03 22:02:38 +00:00
|
|
|
net::PostOffice,
|
2019-04-16 13:06:30 +00:00
|
|
|
state::State,
|
2019-04-10 23:16:29 +00:00
|
|
|
terrain::TerrainChunk,
|
2019-03-03 22:02:38 +00:00
|
|
|
};
|
2019-04-16 13:06:30 +00:00
|
|
|
use specs::{
|
|
|
|
join::Join, saveload::MarkedBuilder, world::EntityBuilder as EcsEntityBuilder, Builder,
|
|
|
|
Entity as EcsEntity,
|
2019-03-04 19:50:26 +00:00
|
|
|
};
|
2019-04-16 13:06:30 +00:00
|
|
|
use std::{collections::HashSet, net::SocketAddr, sync::mpsc, time::Duration};
|
|
|
|
use threadpool::ThreadPool;
|
|
|
|
use vek::*;
|
|
|
|
use world::World;
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use scan_fmt::scan_fmt;
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
const CLIENT_TIMEOUT: f64 = 5.0; // Seconds
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-04-16 13:06:30 +00:00
|
|
|
struct ChatCommand {
|
|
|
|
keyword: &'static str,
|
|
|
|
arg_fmt: &'static str,
|
|
|
|
help_string: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChatCommand {
|
|
|
|
pub fn new(keyword: &'static str, arg_fmt: &'static str, help_string: &'static str) -> Self {
|
|
|
|
Self {
|
|
|
|
keyword,
|
|
|
|
arg_fmt,
|
|
|
|
help_string,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref CHAT_COMMANDS: Vec<ChatCommand> = vec![
|
|
|
|
ChatCommand::new(
|
|
|
|
"jump",
|
|
|
|
"{d} {d} {d}",
|
|
|
|
"jump: offset your current position by a vector\n
|
|
|
|
Usage: /jump [x] [y] [z]"
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"goto",
|
|
|
|
"{d} {d} {d}",
|
|
|
|
"goto: teleport to a given position\n
|
|
|
|
Usage: /goto [x] [y] [z]"
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"alias",
|
|
|
|
"{}",
|
|
|
|
"alias: change your player name (cannot contain spaces)\n
|
|
|
|
Usage: /alias [name]"
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"tp",
|
|
|
|
"{}",
|
|
|
|
"tp: teleport to a named player\n
|
|
|
|
Usage: /tp [name]"
|
|
|
|
),
|
|
|
|
ChatCommand::new("help", "", "help: display this message")
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Server {
|
2019-01-02 19:22:01 +00:00
|
|
|
state: State,
|
2019-01-15 15:13:11 +00:00
|
|
|
world: 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,
|
|
|
|
chunk_tx: mpsc::Sender<(Vec3<i32>, TerrainChunk)>,
|
|
|
|
chunk_rx: mpsc::Receiver<(Vec3<i32>, TerrainChunk)>,
|
2019-04-10 23:41:37 +00:00
|
|
|
pending_chunks: HashSet<Vec3<i32>>,
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2019-01-12 15:57:19 +00:00
|
|
|
/// Create a new `Server`.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn new() -> Result<Self, Error> {
|
2019-04-10 23:16:29 +00:00
|
|
|
let (chunk_tx, chunk_rx) = mpsc::channel();
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
Ok(Self {
|
2019-04-16 13:06:30 +00:00
|
|
|
state: State::new(),
|
2019-01-15 15:13:11 +00:00
|
|
|
world: World::new(),
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-03-05 18:39:18 +00:00
|
|
|
postoffice: PostOffice::bind(SocketAddr::from(([0; 4], 59003)))?,
|
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-03-03 22:02:38 +00:00
|
|
|
})
|
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
|
|
|
/// Get a mutable 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_mut(&mut self) -> &mut World {
|
|
|
|
&mut self.world
|
|
|
|
}
|
2019-01-15 15:13:11 +00:00
|
|
|
|
2019-01-02 17:23:31 +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-03-03 22:02:38 +00:00
|
|
|
// Build up a list of events for this frame, to be passed to the frontend
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle new client connections (step 2)
|
|
|
|
frontend_events.append(&mut self.handle_new_connections()?);
|
|
|
|
|
|
|
|
// Handle new messages from clients
|
|
|
|
frontend_events.append(&mut self.handle_new_messages()?);
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
// Tick the client's LocalState (step 3)
|
|
|
|
self.state.tick(dt);
|
|
|
|
|
2019-04-10 23:16:29 +00:00
|
|
|
// Fetch any generated `TerrainChunk`s and insert them into the terrain
|
|
|
|
// Also, send the chunk data to anybody that is close by
|
|
|
|
for (key, chunk) in self.chunk_rx.try_iter() {
|
|
|
|
// Send the chunk to all nearby players
|
|
|
|
for (entity, player, pos) in (
|
|
|
|
&self.state.ecs().internal().entities(),
|
|
|
|
&self.state.ecs().internal().read_storage::<comp::Player>(),
|
2019-04-16 13:06:30 +00:00
|
|
|
&self
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.internal()
|
|
|
|
.read_storage::<comp::phys::Pos>(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2019-04-10 23:16:29 +00:00
|
|
|
// TODO: Distance check
|
|
|
|
// if self.state.terrain().key_pos(key)
|
|
|
|
|
2019-04-12 09:10:01 +00:00
|
|
|
/*
|
2019-04-10 23:16:29 +00:00
|
|
|
self.clients.notify(entity, ServerMsg::TerrainChunkUpdate {
|
|
|
|
key,
|
2019-04-11 22:26:43 +00:00
|
|
|
chunk: Box::new(chunk.clone()),
|
2019-04-10 23:16:29 +00:00
|
|
|
});
|
2019-04-12 09:10:01 +00:00
|
|
|
*/
|
2019-04-10 23:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.state.insert_chunk(key, chunk);
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
// Synchronise clients with the new state of the world
|
|
|
|
self.sync_clients();
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
// Finish the tick, pass control back to the frontend (step 6)
|
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
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
|
|
/// Handle new client connections
|
|
|
|
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-03-05 00:00:11 +00:00
|
|
|
|
2019-04-16 13:06:30 +00:00
|
|
|
self.clients.add(
|
2019-04-10 17:23:27 +00:00
|
|
|
entity,
|
2019-04-16 13:06:30 +00:00
|
|
|
Client {
|
|
|
|
state: ClientState::Connecting,
|
|
|
|
postbox,
|
|
|
|
last_ping: self.state.get_time(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
frontend_events.push(Event::ClientConnected { entity });
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(frontend_events)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle new client messages
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Update client ping
|
|
|
|
if new_msgs.len() > 0 {
|
|
|
|
client.last_ping = state.get_time();
|
|
|
|
|
|
|
|
// Process incoming messages
|
|
|
|
for msg in new_msgs {
|
2019-04-10 17:23:27 +00:00
|
|
|
match client.state {
|
|
|
|
ClientState::Connecting => match msg {
|
|
|
|
ClientMsg::Connect { player, character } => {
|
|
|
|
|
|
|
|
// Write client components
|
2019-04-10 23:16:29 +00:00
|
|
|
state.write_component(entity, player);
|
|
|
|
state.write_component(entity, comp::phys::Pos(Vec3::zero()));
|
|
|
|
state.write_component(entity, comp::phys::Vel(Vec3::zero()));
|
|
|
|
state.write_component(entity, comp::phys::Dir(Vec3::unit_y()));
|
2019-04-10 17:23:27 +00:00
|
|
|
if let Some(character) = character {
|
2019-04-10 23:16:29 +00:00
|
|
|
state.write_component(entity, character);
|
2019-04-16 14:29:44 +00:00
|
|
|
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.state = ClientState::Connected;
|
|
|
|
|
|
|
|
// Return a handshake with the state of the current world
|
|
|
|
client.notify(ServerMsg::Handshake {
|
|
|
|
ecs_state: state.ecs().gen_state_package(),
|
|
|
|
player_entity: state
|
|
|
|
.ecs()
|
2019-04-10 23:16:29 +00:00
|
|
|
.uid_from_entity(entity)
|
2019-04-10 17:23:27 +00:00
|
|
|
.unwrap()
|
|
|
|
.into(),
|
|
|
|
});
|
2019-04-16 13:06:30 +00:00
|
|
|
}
|
2019-04-10 17:23:27 +00:00
|
|
|
_ => disconnect = true,
|
|
|
|
},
|
|
|
|
ClientState::Connected => match msg {
|
|
|
|
ClientMsg::Connect { .. } => disconnect = true, // Not allowed when already connected
|
2019-04-10 23:41:37 +00:00
|
|
|
ClientMsg::Disconnect => disconnect = true,
|
2019-04-11 22:26:43 +00:00
|
|
|
ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong),
|
2019-04-16 13:06:30 +00:00
|
|
|
ClientMsg::Pong => {}
|
2019-04-10 23:16:29 +00:00
|
|
|
ClientMsg::Chat(msg) => new_chat_msgs.push((entity, msg)),
|
2019-04-16 14:29:44 +00:00
|
|
|
ClientMsg::PlayerAnimation(animation) => state.write_component(entity, animation),
|
2019-04-10 17:23:27 +00:00
|
|
|
ClientMsg::PlayerPhysics { pos, vel, dir } => {
|
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-16 13:06:30 +00:00
|
|
|
}
|
|
|
|
ClientMsg::TerrainChunkRequest { key } => {
|
|
|
|
match state.terrain().get_key(key) {
|
|
|
|
Some(chunk) => {} /*client.postbox.send_message(ServerMsg::TerrainChunkUpdate {
|
2019-04-10 23:41:37 +00:00
|
|
|
key,
|
2019-04-11 22:26:43 +00:00
|
|
|
chunk: Box::new(chunk.clone()),
|
2019-04-16 13:06:30 +00:00
|
|
|
}),*/
|
|
|
|
None => requested_chunks.push(key),
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 18:39:18 +00:00
|
|
|
},
|
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 {
|
|
|
|
// 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-03-03 22:02:38 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle new chat messages
|
2019-04-10 17:23:27 +00:00
|
|
|
for (entity, msg) in new_chat_msgs {
|
2019-04-16 13:06:30 +00:00
|
|
|
// Handle chat commands
|
|
|
|
if msg.starts_with("/") && msg.len() > 1 {
|
|
|
|
let argv = String::from(&msg[1..]);
|
|
|
|
self.process_chat_cmd(entity, argv);
|
|
|
|
} else {
|
|
|
|
self.clients.notify_connected(ServerMsg::Chat(
|
|
|
|
match self
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.internal()
|
|
|
|
.read_storage::<comp::Player>()
|
|
|
|
.get(entity)
|
|
|
|
{
|
|
|
|
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-03-05 00:00:11 +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-04-10 23:41:37 +00:00
|
|
|
// Generate requested chunks
|
|
|
|
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
|
|
|
|
|
|
|
/// Sync client states with the most up to date information
|
|
|
|
fn sync_clients(&mut self) {
|
2019-04-14 20:30:27 +00:00
|
|
|
// Sync 'logical' state using Sphynx
|
2019-04-10 17:23:27 +00:00
|
|
|
self.clients.notify_connected(ServerMsg::EcsSync(self.state.ecs_mut().next_sync_package()));
|
2019-04-14 20:30:27 +00:00
|
|
|
|
|
|
|
// Sync 'physical' state
|
|
|
|
for (entity, &uid, &pos, &vel, &dir, force_update) in (
|
|
|
|
&self.state.ecs().internal().entities(),
|
|
|
|
&self.state.ecs().internal().read_storage::<Uid>(),
|
|
|
|
&self.state.ecs().internal().read_storage::<comp::phys::Pos>(),
|
|
|
|
&self.state.ecs().internal().read_storage::<comp::phys::Vel>(),
|
|
|
|
&self.state.ecs().internal().read_storage::<comp::phys::Dir>(),
|
|
|
|
self.state.ecs().internal().read_storage::<comp::phys::ForceUpdate>().maybe(),
|
|
|
|
).join() {
|
|
|
|
let msg = ServerMsg::EntityPhysics {
|
|
|
|
entity: uid.into(),
|
|
|
|
pos,
|
|
|
|
vel,
|
|
|
|
dir,
|
|
|
|
};
|
|
|
|
|
|
|
|
match force_update {
|
2019-04-16 14:29:44 +00:00
|
|
|
|
|
|
|
|
2019-04-14 20:30:27 +00:00
|
|
|
Some(_) => self.clients.notify_connected(msg),
|
|
|
|
None => self.clients.notify_connected_except(entity, msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:29:44 +00:00
|
|
|
// Sync animation states
|
|
|
|
for (entity, &uid, &animation) in (
|
|
|
|
&self.state.ecs().internal().entities(),
|
|
|
|
&self.state.ecs().internal().read_storage::<Uid>(),
|
|
|
|
&self.state.ecs().internal().read_storage::<comp::Animation>(),
|
|
|
|
).join() {
|
|
|
|
self.clients.notify_connected_except(entity, ServerMsg::EntityAnimation {
|
|
|
|
entity: uid.into(),
|
|
|
|
animation,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-14 20:30:27 +00:00
|
|
|
// Remove all force flags
|
|
|
|
self.state.ecs_mut().internal_mut().write_storage::<comp::phys::ForceUpdate>().clear();
|
2019-03-04 19:50:26 +00:00
|
|
|
}
|
2019-04-10 23:41:37 +00:00
|
|
|
|
|
|
|
pub fn generate_chunk(&mut self, key: Vec3<i32>) {
|
|
|
|
if self.pending_chunks.insert(key) {
|
|
|
|
let chunk_tx = self.chunk_tx.clone();
|
2019-04-16 13:06:30 +00:00
|
|
|
self.thread_pool
|
|
|
|
.execute(move || chunk_tx.send((key, World::generate_chunk(key))).unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_chat_cmd<'a>(&mut self, entity: EcsEntity, cmd: String) {
|
|
|
|
let sep = cmd.find(' ');
|
|
|
|
let (kwd, args) = match sep {
|
|
|
|
Some(i) => (cmd[..i].to_string(), cmd[(i + 1)..].to_string()),
|
|
|
|
None => (cmd, "".to_string()),
|
|
|
|
};
|
|
|
|
let action_opt = CHAT_COMMANDS.iter().find(|x| x.keyword == kwd);
|
|
|
|
match action_opt {
|
|
|
|
Some(action) => match action.keyword {
|
|
|
|
"jump" => {
|
|
|
|
let (opt_x, opt_y, opt_z) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32);
|
|
|
|
match (opt_x, opt_y, opt_z) {
|
|
|
|
(Some(x), Some(y), Some(z)) => {
|
|
|
|
if let Some(current_pos) =
|
|
|
|
self.state.read_component_cloned::<comp::phys::Pos>(entity)
|
|
|
|
{
|
|
|
|
self.state.write_component(
|
|
|
|
entity,
|
|
|
|
comp::phys::Pos(current_pos.0 + Vec3::new(x, y, z)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
self.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(String::from(
|
|
|
|
"Command 'jump' invalid in current state",
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
_ => self
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"goto" => {
|
|
|
|
let (opt_x, opt_y, opt_z) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32);
|
|
|
|
match (opt_x, opt_y, opt_z) {
|
|
|
|
(Some(x), Some(y), Some(z)) => self
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::phys::Pos(Vec3::new(x, y, z))),
|
|
|
|
_ => self
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"alias" => {
|
|
|
|
let opt_alias = scan_fmt!(&args, action.arg_fmt, String);
|
|
|
|
match opt_alias {
|
|
|
|
Some(alias) => self
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::player::Player { alias }),
|
|
|
|
None => self
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"tp" => {
|
|
|
|
let opt_alias = scan_fmt!(&args, action.arg_fmt, String);
|
|
|
|
match opt_alias {
|
|
|
|
Some(alias) => {
|
|
|
|
let ecs = self.state.ecs().internal();
|
|
|
|
let opt_player =
|
|
|
|
(&ecs.entities(), &ecs.read_storage::<comp::player::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
|
|
|
.map(|(entity, _)| entity);
|
|
|
|
match opt_player {
|
|
|
|
Some(player) => {
|
|
|
|
if let Some(pos) =
|
|
|
|
self.state.read_component_cloned::<comp::phys::Pos>(player)
|
|
|
|
{
|
|
|
|
self.state.write_component(entity, pos);
|
|
|
|
} else {
|
|
|
|
self.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!(
|
|
|
|
"Unable to teleport to player '{}'",
|
|
|
|
alias
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
|
|
|
self.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(String::from(action.help_string)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => self
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"help" => {
|
|
|
|
for cmd in CHAT_COMMANDS.iter() {
|
|
|
|
self.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(cmd.help_string)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
// unknown command
|
|
|
|
None => {
|
|
|
|
self.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!(
|
|
|
|
"Unrecognised command: '/{}'\nAvailable commands:",
|
|
|
|
kwd
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
for cmd in CHAT_COMMANDS.iter() {
|
|
|
|
self.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(cmd.keyword)));
|
|
|
|
}
|
|
|
|
}
|
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-10 17:23:27 +00:00
|
|
|
self.clients.notify_connected(ServerMsg::Shutdown);
|
2019-03-05 18:39:18 +00:00
|
|
|
}
|
|
|
|
}
|