2019-03-05 18:39:18 +00:00
|
|
|
#![feature(label_break_value)]
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
pub mod error;
|
|
|
|
pub mod input;
|
|
|
|
|
|
|
|
// Reexports
|
2019-04-23 09:53:45 +00:00
|
|
|
pub use crate::{error::Error, input::Input};
|
2019-04-19 19:32:47 +00:00
|
|
|
pub use specs::join::Join;
|
2019-04-23 09:53:45 +00:00
|
|
|
pub use specs::Entity as EcsEntity;
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
use common::{
|
2019-03-06 13:48:44 +00:00
|
|
|
comp,
|
2019-04-23 09:53:45 +00:00
|
|
|
msg::{ClientMsg, ClientState, ServerMsg},
|
|
|
|
net::PostBox,
|
2019-03-03 22:02:38 +00:00
|
|
|
state::State,
|
|
|
|
terrain::TerrainChunk,
|
|
|
|
};
|
2019-04-23 09:53:45 +00:00
|
|
|
use specs::Builder;
|
|
|
|
use std::{collections::HashSet, net::SocketAddr, time::Duration};
|
|
|
|
use threadpool::ThreadPool;
|
|
|
|
use vek::*;
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-04-17 14:46:04 +00:00
|
|
|
const SERVER_TIMEOUT: f64 = 20.0; // Seconds
|
2019-03-05 18:39:18 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
pub enum Event {
|
|
|
|
Chat(String),
|
2019-04-23 12:01:16 +00:00
|
|
|
Disconnect,
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Client {
|
2019-04-23 12:01:16 +00:00
|
|
|
client_state: Option<ClientState>,
|
2019-04-10 23:16:29 +00:00
|
|
|
thread_pool: ThreadPool,
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
last_ping: f64,
|
2019-04-19 19:32:47 +00:00
|
|
|
pub postbox: PostBox<ClientMsg, ServerMsg>,
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
tick: u64,
|
|
|
|
state: State,
|
2019-04-19 19:32:47 +00:00
|
|
|
entity: EcsEntity,
|
2019-04-10 23:16:29 +00:00
|
|
|
view_distance: u64,
|
2019-04-11 22:26:43 +00:00
|
|
|
|
|
|
|
pending_chunks: HashSet<Vec3<i32>>,
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2019-01-12 15:57:19 +00:00
|
|
|
/// Create a new `Client`.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-23 09:53:45 +00:00
|
|
|
pub fn new<A: Into<SocketAddr>>(addr: A, view_distance: u64) -> Result<Self, Error> {
|
2019-04-23 12:01:16 +00:00
|
|
|
let mut client_state = Some(ClientState::Connected);
|
2019-04-11 22:26:43 +00:00
|
|
|
let mut postbox = PostBox::to(addr)?;
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-04-19 19:32:47 +00:00
|
|
|
// Wait for initial sync
|
2019-04-22 00:38:29 +00:00
|
|
|
let (state, entity) = match postbox.next_message() {
|
2019-04-23 09:53:45 +00:00
|
|
|
Some(ServerMsg::InitialSync {
|
|
|
|
ecs_state,
|
|
|
|
entity_uid,
|
|
|
|
}) => {
|
2019-04-10 17:23:27 +00:00
|
|
|
let mut state = State::from_state_package(ecs_state);
|
2019-04-23 09:53:45 +00:00
|
|
|
let entity = state
|
|
|
|
.ecs()
|
|
|
|
.entity_from_uid(entity_uid)
|
|
|
|
.ok_or(Error::ServerWentMad)?;
|
2019-04-22 00:38:29 +00:00
|
|
|
(state, entity)
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
2019-04-10 17:23:27 +00:00
|
|
|
_ => return Err(Error::ServerWentMad),
|
|
|
|
};
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
Ok(Self {
|
2019-04-19 19:32:47 +00:00
|
|
|
client_state,
|
2019-01-23 20:01:58 +00:00
|
|
|
thread_pool: threadpool::Builder::new()
|
|
|
|
.thread_name("veloren-worker".into())
|
|
|
|
.build(),
|
2019-01-15 15:13:11 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
last_ping: state.get_time(),
|
|
|
|
postbox,
|
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
tick: 0,
|
2019-03-03 22:02:38 +00:00
|
|
|
state,
|
2019-04-22 00:38:29 +00:00
|
|
|
entity,
|
2019-04-10 23:16:29 +00:00
|
|
|
view_distance,
|
2019-04-11 22:26:43 +00:00
|
|
|
|
|
|
|
pending_chunks: HashSet::new(),
|
2019-03-03 22:02:38 +00:00
|
|
|
})
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 18:12:29 +00:00
|
|
|
pub fn register(&mut self, player: comp::Player) {
|
|
|
|
self.postbox.send_message(ClientMsg::Register { player });
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
/// Get a reference to the client's worker thread pool. This pool should be used for any
|
|
|
|
/// computationally expensive operations that run outside of the main thread (i.e: threads that
|
|
|
|
/// block on I/O operations are exempt).
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-23 09:53:45 +00:00
|
|
|
pub fn thread_pool(&self) -> &threadpool::ThreadPool {
|
|
|
|
&self.thread_pool
|
|
|
|
}
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-01-12 15:57:19 +00:00
|
|
|
/// Get a reference to the client's game state.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-23 09:53:45 +00:00
|
|
|
pub fn state(&self) -> &State {
|
|
|
|
&self.state
|
|
|
|
}
|
2019-01-12 15:57:19 +00:00
|
|
|
|
|
|
|
/// Get a mutable reference to the client's game state.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-23 09:53:45 +00:00
|
|
|
pub fn state_mut(&mut self) -> &mut State {
|
|
|
|
&mut self.state
|
|
|
|
}
|
2019-01-12 15:57:19 +00:00
|
|
|
|
2019-04-19 19:32:47 +00:00
|
|
|
/// Get the player's entity
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-04-19 19:32:47 +00:00
|
|
|
pub fn entity(&self) -> EcsEntity {
|
|
|
|
self.entity
|
2019-03-02 03:48:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
/// Get the current tick number.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-23 20:01:58 +00:00
|
|
|
pub fn get_tick(&self) -> u64 {
|
|
|
|
self.tick
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
/// Send a chat message to the server
|
|
|
|
#[allow(dead_code)]
|
2019-03-05 18:39:18 +00:00
|
|
|
pub fn send_chat(&mut self, msg: String) {
|
2019-04-11 22:26:43 +00:00
|
|
|
self.postbox.send_message(ClientMsg::Chat(msg))
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
/// Execute a single client 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 client-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) Perform a single LocalState tick (i.e: update the world and entities in the world)
|
|
|
|
// 4) Go through the terrain update queue and apply all changes to the terrain
|
|
|
|
// 5) 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();
|
|
|
|
|
|
|
|
// Handle new messages from the server
|
|
|
|
frontend_events.append(&mut self.handle_new_messages()?);
|
|
|
|
|
2019-04-12 09:09:14 +00:00
|
|
|
self.state.terrain().iter().for_each(|(k, _)| {
|
2019-04-23 14:49:14 +00:00
|
|
|
//println!("Chunk at {:?}", k);
|
2019-04-12 09:09:14 +00:00
|
|
|
});
|
|
|
|
|
2019-04-23 09:53:45 +00:00
|
|
|
self.state.write_component(
|
|
|
|
self.entity,
|
|
|
|
comp::Control {
|
|
|
|
move_dir: input.move_dir,
|
|
|
|
},
|
|
|
|
);
|
2019-03-02 03:48:30 +00:00
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
// Tick the client's LocalState (step 3)
|
|
|
|
self.state.tick(dt);
|
|
|
|
|
2019-03-05 18:39:18 +00:00
|
|
|
// Update the server about the player's physics attributes
|
2019-04-14 20:30:27 +00:00
|
|
|
match (
|
2019-04-19 19:32:47 +00:00
|
|
|
self.state.read_storage().get(self.entity).cloned(),
|
|
|
|
self.state.read_storage().get(self.entity).cloned(),
|
|
|
|
self.state.read_storage().get(self.entity).cloned(),
|
2019-04-14 20:30:27 +00:00
|
|
|
) {
|
|
|
|
(Some(pos), Some(vel), Some(dir)) => {
|
2019-04-23 09:53:45 +00:00
|
|
|
self.postbox
|
|
|
|
.send_message(ClientMsg::PlayerPhysics { pos, vel, dir });
|
|
|
|
}
|
|
|
|
_ => {}
|
2019-03-05 18:39:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 17:41:20 +00:00
|
|
|
// Update the server about the player's currently playing animation and the previous one
|
2019-04-23 09:53:45 +00:00
|
|
|
if let Some(animation_history) = self
|
|
|
|
.state
|
|
|
|
.read_storage::<comp::AnimationHistory>()
|
|
|
|
.get(self.entity)
|
|
|
|
.cloned()
|
|
|
|
{
|
2019-04-17 20:32:36 +00:00
|
|
|
if Some(animation_history.current) != animation_history.last {
|
2019-04-23 09:53:45 +00:00
|
|
|
self.postbox
|
|
|
|
.send_message(ClientMsg::PlayerAnimation(animation_history));
|
2019-04-17 17:32:29 +00:00
|
|
|
}
|
2019-04-16 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 22:26:43 +00:00
|
|
|
// Request chunks from the server
|
2019-04-23 09:53:45 +00:00
|
|
|
if let Some(pos) = self
|
|
|
|
.state
|
|
|
|
.read_storage::<comp::phys::Pos>()
|
|
|
|
.get(self.entity)
|
|
|
|
{
|
2019-04-14 20:30:27 +00:00
|
|
|
let chunk_pos = self.state.terrain().pos_key(pos.0.map(|e| e as i32));
|
|
|
|
|
2019-04-23 14:49:14 +00:00
|
|
|
for i in chunk_pos.x - 2..chunk_pos.x + 2 {
|
|
|
|
for j in chunk_pos.y - 2..chunk_pos.y + 2 {
|
|
|
|
for k in 0..1 {
|
2019-04-14 20:30:27 +00:00
|
|
|
let key = chunk_pos + Vec3::new(i, j, k);
|
2019-04-23 09:53:45 +00:00
|
|
|
if self.state.terrain().get_key(key).is_none()
|
|
|
|
&& !self.pending_chunks.contains(&key)
|
|
|
|
{
|
|
|
|
self.postbox
|
|
|
|
.send_message(ClientMsg::TerrainChunkRequest { key });
|
2019-04-14 20:30:27 +00:00
|
|
|
self.pending_chunks.insert(key);
|
2019-04-11 22:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:23:31 +00:00
|
|
|
// Finish the tick, pass control back to the frontend (step 6)
|
2019-01-23 20:01:58 +00:00
|
|
|
self.tick += 1;
|
2019-03-03 22:02:38 +00:00
|
|
|
Ok(frontend_events)
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
2019-01-23 20:01:58 +00:00
|
|
|
|
|
|
|
/// Clean up the client after a tick
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-23 20:01:58 +00:00
|
|
|
pub fn cleanup(&mut self) {
|
|
|
|
// Cleanup the local state
|
|
|
|
self.state.cleanup();
|
|
|
|
}
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
/// Handle new server messages
|
|
|
|
fn handle_new_messages(&mut self) -> Result<Vec<Event>, Error> {
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
|
|
|
// Step 1
|
|
|
|
let new_msgs = self.postbox.new_messages();
|
|
|
|
|
|
|
|
if new_msgs.len() > 0 {
|
|
|
|
self.last_ping = self.state.get_time();
|
|
|
|
|
|
|
|
for msg in new_msgs {
|
|
|
|
match msg {
|
2019-04-19 19:32:47 +00:00
|
|
|
ServerMsg::InitialSync { .. } => return Err(Error::ServerWentMad),
|
2019-03-03 22:02:38 +00:00
|
|
|
ServerMsg::Shutdown => return Err(Error::ServerShutdown),
|
2019-04-11 22:26:43 +00:00
|
|
|
ServerMsg::Ping => self.postbox.send_message(ClientMsg::Pong),
|
2019-04-23 09:53:45 +00:00
|
|
|
ServerMsg::Pong => {}
|
2019-03-04 19:50:26 +00:00
|
|
|
ServerMsg::Chat(msg) => frontend_events.push(Event::Chat(msg)),
|
2019-04-19 19:32:47 +00:00
|
|
|
ServerMsg::SetPlayerEntity(uid) => self.entity = self.state.ecs().entity_from_uid(uid).unwrap(), // TODO: Don't unwrap here!
|
2019-04-10 21:51:14 +00:00
|
|
|
ServerMsg::EcsSync(sync_package) => self.state.ecs_mut().sync_with_package(sync_package),
|
2019-04-14 20:30:27 +00:00
|
|
|
ServerMsg::EntityPhysics { entity, pos, vel, dir } => match self.state.ecs().entity_from_uid(entity) {
|
|
|
|
Some(entity) => {
|
|
|
|
self.state.write_component(entity, pos);
|
|
|
|
self.state.write_component(entity, vel);
|
|
|
|
self.state.write_component(entity, dir);
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
|
|
|
None => {}
|
2019-04-14 20:30:27 +00:00
|
|
|
},
|
2019-04-17 20:32:36 +00:00
|
|
|
ServerMsg::EntityAnimation { entity, animation_history } => match self.state.ecs().entity_from_uid(entity) {
|
2019-04-16 14:29:44 +00:00
|
|
|
Some(entity) => {
|
2019-04-17 20:32:36 +00:00
|
|
|
self.state.write_component(entity, animation_history);
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
|
|
|
None => {}
|
2019-04-16 14:29:44 +00:00
|
|
|
},
|
2019-04-11 22:26:43 +00:00
|
|
|
ServerMsg::TerrainChunkUpdate { key, chunk } => {
|
|
|
|
self.state.insert_chunk(key, *chunk);
|
|
|
|
self.pending_chunks.remove(&key);
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
2019-04-19 19:32:47 +00:00
|
|
|
ServerMsg::StateAnswer(Ok(state)) => {
|
2019-04-23 12:01:16 +00:00
|
|
|
self.client_state = Some(state);
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
2019-04-19 19:32:47 +00:00
|
|
|
ServerMsg::StateAnswer(Err((error, state))) => {
|
2019-04-23 12:01:16 +00:00
|
|
|
self.client_state = Some(state);
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
2019-04-20 17:54:37 +00:00
|
|
|
ServerMsg::ForceState(state) => {
|
2019-04-23 12:01:16 +00:00
|
|
|
self.client_state = Some(state);
|
|
|
|
}
|
|
|
|
ServerMsg::Disconnect => {
|
|
|
|
self.client_state = None;
|
|
|
|
frontend_events.push(Event::Disconnect);
|
2019-04-23 09:53:45 +00:00
|
|
|
}
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 18:39:18 +00:00
|
|
|
} else if let Some(err) = self.postbox.error() {
|
2019-03-04 19:50:26 +00:00
|
|
|
return Err(err.into());
|
2019-03-05 18:39:18 +00:00
|
|
|
} else if self.state.get_time() - self.last_ping > SERVER_TIMEOUT {
|
|
|
|
return Err(Error::ServerTimeout);
|
2019-03-06 13:51:01 +00:00
|
|
|
} else if self.state.get_time() - self.last_ping > SERVER_TIMEOUT * 0.5 {
|
|
|
|
// Try pinging the server if the timeout is nearing
|
2019-04-11 22:26:43 +00:00
|
|
|
self.postbox.send_message(ClientMsg::Ping);
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(frontend_events)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Client {
|
|
|
|
fn drop(&mut self) {
|
2019-04-11 22:26:43 +00:00
|
|
|
self.postbox.send_message(ClientMsg::Disconnect);
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|