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
|
|
|
|
pub use specs::Entity as EcsEntity;
|
|
|
|
pub use crate::{
|
|
|
|
error::Error,
|
|
|
|
input::Input,
|
|
|
|
};
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
time::Duration,
|
|
|
|
net::SocketAddr,
|
|
|
|
};
|
2019-01-15 15:13:11 +00:00
|
|
|
use vek::*;
|
2019-01-23 20:01:58 +00:00
|
|
|
use threadpool;
|
2019-03-06 13:48:44 +00:00
|
|
|
use specs::Builder;
|
2019-03-03 22:02:38 +00:00
|
|
|
use common::{
|
2019-03-06 13:48:44 +00:00
|
|
|
comp,
|
2019-03-03 22:02:38 +00:00
|
|
|
state::State,
|
|
|
|
terrain::TerrainChunk,
|
|
|
|
net::PostBox,
|
|
|
|
msg::{ClientMsg, ServerMsg},
|
|
|
|
};
|
2019-01-15 15:13:11 +00:00
|
|
|
use world::World;
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-05 18:39:18 +00:00
|
|
|
const SERVER_TIMEOUT: f64 = 5.0; // Seconds
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
pub enum Event {
|
|
|
|
Chat(String),
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Client {
|
2019-01-23 20:01:58 +00:00
|
|
|
thread_pool: threadpool::ThreadPool,
|
2019-01-02 17:23:31 +00:00
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
last_ping: f64,
|
|
|
|
postbox: PostBox<ClientMsg, ServerMsg>,
|
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
tick: u64,
|
|
|
|
state: State,
|
2019-03-05 18:39:18 +00:00
|
|
|
player: Option<EcsEntity>,
|
2019-01-15 15:13:11 +00:00
|
|
|
|
|
|
|
// Testing
|
|
|
|
world: World,
|
|
|
|
pub chunk: Option<TerrainChunk>,
|
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)]
|
|
|
|
pub fn new<A: Into<SocketAddr>>(addr: A) -> Result<Self, Error> {
|
|
|
|
let state = State::new();
|
|
|
|
|
|
|
|
let mut postbox = PostBox::to_server(addr)?;
|
|
|
|
|
|
|
|
Ok(Self {
|
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-01-15 15:13:11 +00:00
|
|
|
player: None,
|
|
|
|
|
|
|
|
// Testing
|
|
|
|
world: World::new(),
|
|
|
|
chunk: None,
|
2019-03-03 22:02:38 +00:00
|
|
|
})
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|
|
|
|
|
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-01-23 20:01:58 +00:00
|
|
|
pub fn thread_pool(&self) -> &threadpool::ThreadPool { &self.thread_pool }
|
|
|
|
|
|
|
|
// TODO: Get rid of this
|
2019-01-15 15:13:11 +00:00
|
|
|
pub fn with_test_state(mut self) -> Self {
|
|
|
|
self.chunk = Some(self.world.generate_chunk(Vec3::zero()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
// TODO: Get rid of this
|
|
|
|
pub fn load_chunk(&mut self, pos: Vec3<i32>) {
|
|
|
|
self.state.terrain_mut().insert(pos, self.world.generate_chunk(pos));
|
|
|
|
self.state.changes_mut().new_chunks.push(pos);
|
|
|
|
}
|
|
|
|
|
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-01-12 15:57:19 +00:00
|
|
|
pub fn state(&self) -> &State { &self.state }
|
|
|
|
|
|
|
|
/// Get a mutable reference to the client's game state.
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-01-12 15:57:19 +00:00
|
|
|
pub fn state_mut(&mut self) -> &mut State { &mut self.state }
|
|
|
|
|
2019-03-02 03:48:30 +00:00
|
|
|
/// Get the player entity
|
2019-03-03 22:02:38 +00:00
|
|
|
#[allow(dead_code)]
|
2019-03-05 18:39:18 +00:00
|
|
|
pub fn player(&self) -> Option<EcsEntity> {
|
2019-03-02 03:48:30 +00:00
|
|
|
self.player
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
self.postbox.send(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-03-17 05:26:51 +00:00
|
|
|
// Step 1
|
2019-03-05 18:39:18 +00:00
|
|
|
if let Some(ecs_entity) = self.player {
|
2019-03-02 19:43:51 +00:00
|
|
|
// TODO: remove this
|
|
|
|
const PLAYER_VELOCITY: f32 = 100.0;
|
|
|
|
// TODO: Set acceleration instead
|
2019-03-05 00:00:11 +00:00
|
|
|
self.state.write_component(ecs_entity, comp::phys::Vel(Vec3::from(input.move_dir * PLAYER_VELOCITY)));
|
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
|
|
|
|
if let Some(ecs_entity) = self.player {
|
|
|
|
match (
|
|
|
|
self.state.read_storage().get(ecs_entity).cloned(),
|
|
|
|
self.state.read_storage().get(ecs_entity).cloned(),
|
|
|
|
self.state.read_storage().get(ecs_entity).cloned(),
|
|
|
|
) {
|
|
|
|
(Some(pos), Some(vel), Some(dir)) => {
|
|
|
|
self.postbox.send(ClientMsg::PlayerPhysics { pos, vel, dir });
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
ServerMsg::Shutdown => return Err(Error::ServerShutdown),
|
2019-03-05 18:39:18 +00:00
|
|
|
ServerMsg::Ping => self.postbox.send(ClientMsg::Pong),
|
|
|
|
ServerMsg::Pong => {},
|
2019-03-04 19:50:26 +00:00
|
|
|
ServerMsg::Chat(msg) => frontend_events.push(Event::Chat(msg)),
|
2019-03-05 18:39:18 +00:00
|
|
|
ServerMsg::SetPlayerEntity(uid) => {
|
2019-03-06 13:48:44 +00:00
|
|
|
let ecs_entity = self.state
|
|
|
|
.get_entity(uid)
|
|
|
|
.unwrap_or_else(|| self.state.build_uid_entity_with_uid(uid).build());
|
|
|
|
|
2019-03-05 18:39:18 +00:00
|
|
|
self.player = Some(ecs_entity);
|
|
|
|
},
|
2019-03-04 19:50:26 +00:00
|
|
|
ServerMsg::EntityPhysics { uid, pos, vel, dir } => {
|
2019-03-06 13:48:44 +00:00
|
|
|
let ecs_entity = self.state
|
|
|
|
.get_entity(uid)
|
|
|
|
.unwrap_or_else(|| self.state.build_uid_entity_with_uid(uid).build());
|
|
|
|
|
2019-03-04 19:50:26 +00:00
|
|
|
self.state.write_component(ecs_entity, pos);
|
|
|
|
self.state.write_component(ecs_entity, vel);
|
|
|
|
self.state.write_component(ecs_entity, dir);
|
|
|
|
},
|
2019-03-05 00:00:11 +00:00
|
|
|
ServerMsg::EntityDeleted(uid) => {
|
|
|
|
self.state.delete_entity(uid);
|
|
|
|
},
|
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
|
|
|
|
self.postbox.send(ClientMsg::Ping);
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(frontend_events)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Client {
|
|
|
|
fn drop(&mut self) {
|
2019-03-05 18:39:18 +00:00
|
|
|
self.postbox.send(ClientMsg::Disconnect);
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
2019-01-02 17:23:31 +00:00
|
|
|
}
|