2020-05-11 10:06:53 +00:00
|
|
|
use crate::{
|
2020-06-16 01:00:32 +00:00
|
|
|
client::Client, persistence::character::PersistedComponents, settings::ServerSettings,
|
|
|
|
sys::sentinel::DeletedEntities, SpawnPoint,
|
2020-05-11 10:06:53 +00:00
|
|
|
};
|
2020-03-10 02:27:32 +00:00
|
|
|
use common::{
|
2020-06-04 11:44:33 +00:00
|
|
|
comp,
|
2020-03-10 02:27:32 +00:00
|
|
|
effect::Effect,
|
2020-06-16 01:00:32 +00:00
|
|
|
msg::{CharacterInfo, ClientState, PlayerListUpdate, ServerMsg},
|
2020-03-10 02:27:32 +00:00
|
|
|
state::State,
|
|
|
|
sync::{Uid, WorldSyncExt},
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2020-03-10 02:27:32 +00:00
|
|
|
};
|
|
|
|
use specs::{Builder, Entity as EcsEntity, EntityBuilder as EcsEntityBuilder, Join, WorldExt};
|
2020-06-21 14:26:06 +00:00
|
|
|
use tracing::warn;
|
2020-03-10 02:27:32 +00:00
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
pub trait StateExt {
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Push an item into the provided entity's inventory
|
2020-03-10 02:27:32 +00:00
|
|
|
fn give_item(&mut self, entity: EcsEntity, item: comp::Item) -> bool;
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Updates a component associated with the entity based on the `Effect`
|
2020-03-10 02:27:32 +00:00
|
|
|
fn apply_effect(&mut self, entity: EcsEntity, effect: Effect);
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Build a non-player character
|
2020-03-10 02:27:32 +00:00
|
|
|
fn create_npc(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
stats: comp::Stats,
|
2020-02-26 17:04:43 +00:00
|
|
|
loadout: comp::Loadout,
|
2020-03-10 02:27:32 +00:00
|
|
|
body: comp::Body,
|
|
|
|
) -> EcsEntityBuilder;
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Build a static object entity
|
2020-03-10 02:27:32 +00:00
|
|
|
fn create_object(&mut self, pos: comp::Pos, object: comp::object::Body) -> EcsEntityBuilder;
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Build a projectile
|
2020-03-10 02:27:32 +00:00
|
|
|
fn create_projectile(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
vel: comp::Vel,
|
|
|
|
body: comp::Body,
|
|
|
|
projectile: comp::Projectile,
|
|
|
|
) -> EcsEntityBuilder;
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Insert common/default components for a new character joining the server
|
2020-06-25 18:50:04 +00:00
|
|
|
fn initialize_character_data(&mut self, entity: EcsEntity, character_id: i32);
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Update the components associated with the entity's current character.
|
|
|
|
/// Performed after loading component data from the database
|
|
|
|
fn update_character_data(&mut self, entity: EcsEntity, components: PersistedComponents);
|
|
|
|
/// Iterates over registered clients and send each `ServerMsg`
|
2020-03-10 02:27:32 +00:00
|
|
|
fn notify_registered_clients(&self, msg: ServerMsg);
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Delete an entity, recording the deletion in [`DeletedEntities`]
|
2020-03-10 02:27:32 +00:00
|
|
|
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));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_npc(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
stats: comp::Stats,
|
2020-02-26 17:04:43 +00:00
|
|
|
loadout: comp::Loadout,
|
2020-03-10 02:27:32 +00:00
|
|
|
body: comp::Body,
|
|
|
|
) -> EcsEntityBuilder {
|
|
|
|
self.ecs_mut()
|
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(comp::Vel(Vec3::zero()))
|
2020-03-28 01:31:22 +00:00
|
|
|
.with(comp::Ori::default())
|
2020-04-26 14:37:13 +00:00
|
|
|
.with(comp::Collider::Box {
|
|
|
|
radius: 0.4,
|
|
|
|
z_min: 0.0,
|
|
|
|
z_max: 1.75,
|
|
|
|
})
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Controller::default())
|
|
|
|
.with(body)
|
|
|
|
.with(stats)
|
|
|
|
.with(comp::Alignment::Npc)
|
|
|
|
.with(comp::Energy::new(500))
|
2020-04-26 14:37:13 +00:00
|
|
|
.with(comp::Collider::Box {
|
|
|
|
radius: 0.4,
|
|
|
|
z_min: 0.0,
|
|
|
|
z_max: 1.75,
|
|
|
|
})
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Gravity(1.0))
|
|
|
|
.with(comp::CharacterState::default())
|
2020-02-26 17:04:43 +00:00
|
|
|
.with(loadout)
|
2020-03-10 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()))
|
2020-03-28 01:31:22 +00:00
|
|
|
.with(comp::Ori::default())
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Body::Object(object))
|
|
|
|
.with(comp::Mass(100.0))
|
2020-04-26 14:37:13 +00:00
|
|
|
.with(comp::Collider::Box {
|
|
|
|
radius: 0.4,
|
|
|
|
z_min: 0.0,
|
|
|
|
z_max: 0.9,
|
|
|
|
})
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Gravity(1.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-03-28 01:31:22 +00:00
|
|
|
.with(comp::Ori(Dir::from_unnormalized(vel.0).unwrap_or_default()))
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Mass(0.0))
|
2020-04-26 14:37:13 +00:00
|
|
|
.with(comp::Collider::Point)
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(body)
|
|
|
|
.with(projectile)
|
|
|
|
.with(comp::Sticky)
|
|
|
|
}
|
|
|
|
|
2020-06-25 18:50:04 +00:00
|
|
|
fn initialize_character_data(&mut self, entity: EcsEntity, character_id: i32) {
|
2020-03-10 02:27:32 +00:00
|
|
|
let spawn_point = self.ecs().read_resource::<SpawnPoint>().0;
|
|
|
|
|
|
|
|
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()));
|
2020-03-28 01:31:22 +00:00
|
|
|
self.write_component(entity, comp::Ori::default());
|
2020-04-26 14:37:13 +00:00
|
|
|
self.write_component(entity, comp::Collider::Box {
|
|
|
|
radius: 0.4,
|
|
|
|
z_min: 0.0,
|
|
|
|
z_max: 1.75,
|
|
|
|
});
|
2020-03-10 02:27:32 +00:00
|
|
|
self.write_component(entity, comp::Gravity(1.0));
|
|
|
|
self.write_component(entity, comp::CharacterState::default());
|
|
|
|
self.write_component(entity, comp::Alignment::Owned(entity));
|
2020-03-15 14:27:06 +00:00
|
|
|
|
2020-04-25 13:41:27 +00:00
|
|
|
// Set the character id for the player
|
|
|
|
// TODO this results in a warning in the console: "Error modifying synced
|
|
|
|
// component, it doesn't seem to exist"
|
|
|
|
// It appears to be caused by the player not yet existing on the client at this
|
|
|
|
// point, despite being able to write the data on the server
|
2020-06-16 01:00:32 +00:00
|
|
|
self.ecs()
|
2020-04-25 13:41:27 +00:00
|
|
|
.write_storage::<comp::Player>()
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|player| {
|
|
|
|
player.character_id = Some(character_id);
|
|
|
|
});
|
|
|
|
|
2020-03-10 02:27:32 +00:00
|
|
|
// Give the Admin component to the player if their name exists in admin list
|
2020-06-25 11:20:09 +00:00
|
|
|
if self.ecs().fetch::<ServerSettings>().admins.contains(
|
2020-03-10 02:27:32 +00:00
|
|
|
&self
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Player>()
|
|
|
|
.get(entity)
|
|
|
|
.expect("Failed to fetch entity.")
|
|
|
|
.alias,
|
|
|
|
) {
|
|
|
|
self.write_component(entity, comp::Admin);
|
|
|
|
}
|
2020-05-11 10:06:53 +00:00
|
|
|
|
2020-06-16 01:00:32 +00:00
|
|
|
// 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 update_character_data(&mut self, entity: EcsEntity, components: PersistedComponents) {
|
|
|
|
let (body, stats, inventory, loadout) = components;
|
2020-06-01 07:31:13 +00:00
|
|
|
|
2020-06-16 01:00:32 +00:00
|
|
|
// Notify clients of a player list update
|
|
|
|
let client_uid = self
|
|
|
|
.read_component_cloned::<Uid>(entity)
|
|
|
|
.map(|u| u.into())
|
|
|
|
.expect("Client doesn't have a Uid!!!");
|
2020-06-01 07:31:13 +00:00
|
|
|
|
|
|
|
self.notify_registered_clients(ServerMsg::PlayerListUpdate(
|
2020-06-16 01:00:32 +00:00
|
|
|
PlayerListUpdate::SelectedCharacter(client_uid, CharacterInfo {
|
|
|
|
name: String::from(&stats.name),
|
|
|
|
level: stats.level.level(),
|
2020-06-01 07:31:13 +00:00
|
|
|
}),
|
|
|
|
));
|
|
|
|
|
2020-06-16 01:00:32 +00:00
|
|
|
self.write_component(entity, body);
|
|
|
|
self.write_component(entity, stats);
|
|
|
|
self.write_component(entity, inventory);
|
|
|
|
self.write_component(entity, loadout);
|
|
|
|
|
|
|
|
self.write_component(
|
|
|
|
entity,
|
|
|
|
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::default()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure physics are accepted.
|
|
|
|
self.write_component(entity, comp::ForceUpdate);
|
2020-03-10 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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!(
|
2020-06-21 21:47:49 +00:00
|
|
|
?uid,
|
|
|
|
?pos,
|
2020-03-10 02:27:32 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
}
|