2020-05-11 10:06:53 +00:00
|
|
|
use crate::{
|
2020-11-02 18:30:56 +00:00
|
|
|
client::Client, persistence::PersistedComponents, presence::Presence,
|
|
|
|
sys::sentinel::DeletedEntities, SpawnPoint,
|
2020-05-11 10:06:53 +00:00
|
|
|
};
|
2020-03-10 02:27:32 +00:00
|
|
|
use common::{
|
2020-09-17 23:02:14 +00:00
|
|
|
character::CharacterId,
|
2021-02-28 20:02:03 +00:00
|
|
|
combat,
|
2021-01-04 17:16:42 +00:00
|
|
|
comp::{
|
|
|
|
self,
|
|
|
|
skills::{GeneralSkill, Skill},
|
|
|
|
Inventory,
|
|
|
|
},
|
2020-03-10 02:27:32 +00:00
|
|
|
effect::Effect,
|
2021-03-15 22:50:26 +00:00
|
|
|
slowjob::SlowJobPool,
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::{Uid, UidAllocator},
|
2020-03-10 02:27:32 +00:00
|
|
|
};
|
2020-12-13 17:11:55 +00:00
|
|
|
use common_net::{
|
|
|
|
msg::{CharacterInfo, PlayerListUpdate, PresenceKind, ServerGeneral},
|
|
|
|
sync::WorldSyncExt,
|
|
|
|
};
|
2020-12-01 12:13:07 +00:00
|
|
|
use common_sys::state::State;
|
2020-10-14 21:02:48 +00:00
|
|
|
use rand::prelude::*;
|
2020-06-05 01:48:26 +00:00
|
|
|
use specs::{
|
|
|
|
saveload::MarkerAllocator, 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
|
|
|
/// Updates a component associated with the entity based on the `Effect`
|
2020-11-04 02:01:12 +00:00
|
|
|
fn apply_effect(&self, entity: EcsEntity, effect: Effect, source: Option<Uid>);
|
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,
|
2021-03-22 22:11:13 +00:00
|
|
|
health: comp::Health,
|
2020-12-05 18:23:45 +00:00
|
|
|
poise: comp::Poise,
|
2021-01-13 03:26:51 +00:00
|
|
|
inventory: comp::Inventory,
|
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;
|
2021-03-12 18:53:06 +00:00
|
|
|
fn create_ship(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
ship: comp::ship::Body,
|
|
|
|
level: u16,
|
2021-03-22 22:11:13 +00:00
|
|
|
mountable: bool,
|
2021-03-12 18:53:06 +00:00
|
|
|
) -> 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-08-08 22:22:21 +00:00
|
|
|
/// Build a shockwave entity
|
|
|
|
fn create_shockwave(
|
|
|
|
&mut self,
|
|
|
|
properties: comp::shockwave::Properties,
|
|
|
|
pos: comp::Pos,
|
|
|
|
ori: comp::Ori,
|
|
|
|
) -> EcsEntityBuilder;
|
2020-09-05 16:27:36 +00:00
|
|
|
/// Build a beam entity
|
|
|
|
fn create_beam(
|
|
|
|
&mut self,
|
|
|
|
properties: comp::beam::Properties,
|
|
|
|
pos: comp::Pos,
|
|
|
|
ori: comp::Ori,
|
|
|
|
) -> EcsEntityBuilder;
|
2021-03-13 06:48:30 +00:00
|
|
|
// NOTE: currently only used for testing
|
|
|
|
/// Queues chunk generation in the view distance of the persister, this
|
|
|
|
/// entity must be built before those chunks are received (the builder
|
|
|
|
/// borrows the ecs world so that is kind of impossible in practice)
|
|
|
|
fn create_persister(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
view_distance: u32,
|
|
|
|
world: &std::sync::Arc<world::World>,
|
|
|
|
index: &world::IndexOwned,
|
|
|
|
) -> EcsEntityBuilder;
|
2020-06-16 01:00:32 +00:00
|
|
|
/// Insert common/default components for a new character joining the server
|
2020-09-17 23:02:14 +00:00
|
|
|
fn initialize_character_data(&mut self, entity: EcsEntity, character_id: CharacterId);
|
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-07-12 20:18:57 +00:00
|
|
|
fn send_chat(&self, msg: comp::UnresolvedChatMsg);
|
2020-10-30 16:39:53 +00:00
|
|
|
fn notify_players(&self, msg: ServerGeneral);
|
2020-10-12 08:18:28 +00:00
|
|
|
fn notify_in_game_clients(&self, msg: ServerGeneral);
|
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 {
|
2020-10-27 21:27:19 +00:00
|
|
|
fn apply_effect(&self, entity: EcsEntity, effects: Effect, source: Option<Uid>) {
|
|
|
|
match effects {
|
2020-03-10 02:27:32 +00:00
|
|
|
Effect::Health(change) => {
|
|
|
|
self.ecs()
|
2020-10-31 22:34:08 +00:00
|
|
|
.write_storage::<comp::Health>()
|
2020-03-10 02:27:32 +00:00
|
|
|
.get_mut(entity)
|
2021-01-07 20:25:12 +00:00
|
|
|
.map(|mut health| health.change_by(change));
|
2020-03-10 02:27:32 +00:00
|
|
|
},
|
2020-11-04 02:01:12 +00:00
|
|
|
Effect::Damage(damage) => {
|
2021-01-08 19:12:09 +00:00
|
|
|
let inventories = self.ecs().read_storage::<Inventory>();
|
2021-02-28 20:02:03 +00:00
|
|
|
let stats = self.ecs().read_storage::<comp::Stats>();
|
2021-02-02 18:02:40 +00:00
|
|
|
let change = damage.calculate_health_change(
|
2021-02-28 20:02:03 +00:00
|
|
|
combat::Damage::compute_damage_reduction(
|
|
|
|
inventories.get(entity),
|
|
|
|
stats.get(entity),
|
|
|
|
),
|
2021-02-02 18:02:40 +00:00
|
|
|
source,
|
|
|
|
false,
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
);
|
2020-11-05 01:21:42 +00:00
|
|
|
self.ecs()
|
|
|
|
.write_storage::<comp::Health>()
|
|
|
|
.get_mut(entity)
|
2021-01-07 20:25:12 +00:00
|
|
|
.map(|mut health| health.change_by(change));
|
2020-11-04 02:01:12 +00:00
|
|
|
},
|
2020-12-16 23:30:33 +00:00
|
|
|
Effect::PoiseChange(poise_damage) => {
|
2021-01-13 03:26:51 +00:00
|
|
|
let inventories = self.ecs().read_storage::<Inventory>();
|
|
|
|
let change = poise_damage.modify_poise_damage(inventories.get(entity));
|
2020-12-17 00:14:14 +00:00
|
|
|
// Check to make sure the entity is not already stunned
|
|
|
|
if let Some(character_state) = self
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::CharacterState>()
|
|
|
|
.get(entity)
|
|
|
|
{
|
|
|
|
if !character_state.is_stunned() {
|
|
|
|
self.ecs()
|
|
|
|
.write_storage::<comp::Poise>()
|
|
|
|
.get_mut(entity)
|
2021-01-27 03:05:13 +00:00
|
|
|
.map(|mut poise| poise.change_by(change, Vec3::zero()));
|
2020-12-17 00:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-05 18:23:45 +00:00
|
|
|
},
|
2020-10-27 21:27:19 +00:00
|
|
|
Effect::Buff(buff) => {
|
|
|
|
self.ecs()
|
|
|
|
.write_storage::<comp::Buffs>()
|
|
|
|
.get_mut(entity)
|
2021-01-07 20:25:12 +00:00
|
|
|
.map(|mut buffs| {
|
2020-10-27 21:27:19 +00:00
|
|
|
buffs.insert(comp::Buff::new(
|
|
|
|
buff.kind,
|
|
|
|
buff.data,
|
|
|
|
buff.cat_ids,
|
|
|
|
comp::BuffSource::Item,
|
|
|
|
))
|
|
|
|
});
|
|
|
|
},
|
2020-03-10 02:27:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_npc(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
stats: comp::Stats,
|
2021-03-22 22:11:13 +00:00
|
|
|
health: comp::Health,
|
2020-12-05 18:23:45 +00:00
|
|
|
poise: comp::Poise,
|
2021-01-13 03:26:51 +00:00
|
|
|
inventory: comp::Inventory,
|
2020-03-10 02:27:32 +00:00
|
|
|
body: comp::Body,
|
|
|
|
) -> EcsEntityBuilder {
|
2021-03-22 22:11:13 +00:00
|
|
|
self.ecs_mut()
|
2020-03-10 02:27:32 +00:00
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(comp::Vel(Vec3::zero()))
|
2021-02-04 09:17:38 +00:00
|
|
|
.with(
|
|
|
|
comp::Ori::from_unnormalized_vec(Vec3::new(
|
2021-01-26 20:23:18 +00:00
|
|
|
thread_rng().gen_range(-1.0..1.0),
|
|
|
|
thread_rng().gen_range(-1.0..1.0),
|
2020-10-14 21:02:48 +00:00
|
|
|
0.0,
|
2021-02-04 09:17:38 +00:00
|
|
|
))
|
2020-10-14 21:02:48 +00:00
|
|
|
.unwrap_or_default(),
|
2021-02-04 09:17:38 +00:00
|
|
|
)
|
2021-03-12 18:53:06 +00:00
|
|
|
.with(match body {
|
|
|
|
comp::Body::Ship(ship) => comp::Collider::Voxel {
|
|
|
|
id: ship.manifest_entry().to_string(),
|
|
|
|
},
|
|
|
|
_ => comp::Collider::Box {
|
|
|
|
radius: body.radius(),
|
|
|
|
z_min: 0.0,
|
|
|
|
z_max: body.height(),
|
|
|
|
},
|
2020-04-26 14:37:13 +00:00
|
|
|
})
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Controller::default())
|
|
|
|
.with(body)
|
2021-01-16 17:01:57 +00:00
|
|
|
.with(comp::Energy::new(
|
|
|
|
body,
|
|
|
|
stats
|
|
|
|
.skill_set
|
|
|
|
.skill_level(Skill::General(GeneralSkill::EnergyIncrease))
|
|
|
|
.unwrap_or(None)
|
|
|
|
.unwrap_or(0),
|
|
|
|
))
|
2021-03-22 22:11:13 +00:00
|
|
|
.with(stats)
|
|
|
|
.with(health)
|
|
|
|
.with(poise)
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Alignment::Npc)
|
|
|
|
.with(comp::Gravity(1.0))
|
|
|
|
.with(comp::CharacterState::default())
|
2021-01-08 19:12:09 +00:00
|
|
|
.with(inventory)
|
2020-10-01 01:35:57 +00:00
|
|
|
.with(comp::Buffs::default())
|
2021-02-27 19:55:06 +00:00
|
|
|
.with(comp::Combo::default())
|
2021-03-01 20:44:29 +00:00
|
|
|
.with(comp::Auras::default())
|
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-07-05 12:39:28 +00:00
|
|
|
.with(comp::Mass(5.0))
|
2020-04-26 14:37:13 +00:00
|
|
|
.with(comp::Collider::Box {
|
2020-08-24 17:24:44 +00:00
|
|
|
radius: comp::Body::Object(object).radius(),
|
2020-04-26 14:37:13 +00:00
|
|
|
z_min: 0.0,
|
2020-08-24 17:24:44 +00:00
|
|
|
z_max: comp::Body::Object(object).height(),
|
2020-04-26 14:37:13 +00:00
|
|
|
})
|
2020-08-24 17:24:44 +00:00
|
|
|
.with(comp::Body::Object(object))
|
2020-03-10 02:27:32 +00:00
|
|
|
.with(comp::Gravity(1.0))
|
|
|
|
}
|
|
|
|
|
2021-03-12 18:53:06 +00:00
|
|
|
fn create_ship(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
ship: comp::ship::Body,
|
|
|
|
level: u16,
|
2021-03-22 22:11:13 +00:00
|
|
|
mountable: bool,
|
2021-03-12 18:53:06 +00:00
|
|
|
) -> EcsEntityBuilder {
|
|
|
|
let mut builder = self
|
|
|
|
.ecs_mut()
|
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(comp::Vel(Vec3::zero()))
|
|
|
|
.with(comp::Ori::default())
|
|
|
|
.with(comp::Mass(50.0))
|
|
|
|
.with(comp::Collider::Voxel {
|
|
|
|
id: ship.manifest_entry().to_string(),
|
|
|
|
})
|
|
|
|
.with(comp::Body::Ship(ship))
|
|
|
|
.with(comp::Gravity(1.0))
|
2021-03-22 22:11:13 +00:00
|
|
|
.with(comp::Scale(comp::ship::AIRSHIP_SCALE))
|
2021-03-12 18:53:06 +00:00
|
|
|
.with(comp::Controller::default())
|
|
|
|
.with(comp::inventory::Inventory::new_empty())
|
|
|
|
.with(comp::CharacterState::default())
|
2021-03-12 23:19:10 +00:00
|
|
|
// TODO: some of these are required in order for the character_behavior system to
|
|
|
|
// recognize a possesed airship; that system should be refactored to use `.maybe()`
|
2021-03-12 18:53:06 +00:00
|
|
|
.with(comp::Energy::new(ship.into(), level))
|
2021-03-22 22:11:13 +00:00
|
|
|
//.with(comp::Health::new(ship.into(), level))
|
2021-03-12 18:53:06 +00:00
|
|
|
.with(comp::Stats::new("Airship".to_string()))
|
|
|
|
.with(comp::Buffs::default())
|
|
|
|
.with(comp::Combo::default());
|
2021-03-22 22:11:13 +00:00
|
|
|
|
|
|
|
if mountable {
|
|
|
|
builder = builder.with(comp::MountState::Unmounted);
|
2021-03-12 02:58:57 +00:00
|
|
|
}
|
2021-03-12 18:53:06 +00:00
|
|
|
builder
|
2021-03-11 16:48:59 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
self.ecs_mut()
|
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(vel)
|
2021-02-04 09:17:38 +00:00
|
|
|
.with(comp::Ori::from_unnormalized_vec(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-08-08 22:22:21 +00:00
|
|
|
fn create_shockwave(
|
|
|
|
&mut self,
|
|
|
|
properties: comp::shockwave::Properties,
|
|
|
|
pos: comp::Pos,
|
|
|
|
ori: comp::Ori,
|
|
|
|
) -> EcsEntityBuilder {
|
|
|
|
self.ecs_mut()
|
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(ori)
|
|
|
|
.with(comp::Shockwave {
|
|
|
|
properties,
|
|
|
|
creation: None,
|
|
|
|
})
|
2020-10-08 00:32:30 +00:00
|
|
|
.with(comp::ShockwaveHitEntities {
|
|
|
|
hit_entities: Vec::<Uid>::new(),
|
|
|
|
})
|
2020-08-08 22:22:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 16:27:36 +00:00
|
|
|
fn create_beam(
|
|
|
|
&mut self,
|
|
|
|
properties: comp::beam::Properties,
|
|
|
|
pos: comp::Pos,
|
|
|
|
ori: comp::Ori,
|
|
|
|
) -> EcsEntityBuilder {
|
|
|
|
self.ecs_mut()
|
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(ori)
|
2020-09-24 02:02:30 +00:00
|
|
|
.with(comp::BeamSegment {
|
2020-09-05 16:27:36 +00:00
|
|
|
properties,
|
|
|
|
creation: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-13 06:48:30 +00:00
|
|
|
// NOTE: currently only used for testing
|
|
|
|
/// Queues chunk generation in the view distance of the persister, this
|
|
|
|
/// entity must be built before those chunks are received (the builder
|
|
|
|
/// borrows the ecs world so that is kind of impossible in practice)
|
|
|
|
fn create_persister(
|
|
|
|
&mut self,
|
|
|
|
pos: comp::Pos,
|
|
|
|
view_distance: u32,
|
|
|
|
world: &std::sync::Arc<world::World>,
|
|
|
|
index: &world::IndexOwned,
|
|
|
|
) -> EcsEntityBuilder {
|
|
|
|
use common::{terrain::TerrainChunkSize, vol::RectVolSize};
|
|
|
|
use std::sync::Arc;
|
|
|
|
// Request chunks
|
|
|
|
{
|
2021-03-15 22:50:26 +00:00
|
|
|
let ecs = self.ecs();
|
|
|
|
let slow_jobs = ecs.write_resource::<SlowJobPool>();
|
|
|
|
let mut chunk_generator =
|
|
|
|
ecs.write_resource::<crate::chunk_generator::ChunkGenerator>();
|
2021-03-13 06:48:30 +00:00
|
|
|
let chunk_pos = self.terrain().pos_key(pos.0.map(|e| e as i32));
|
|
|
|
(-(view_distance as i32)..view_distance as i32 + 1)
|
|
|
|
.flat_map(|x| {
|
|
|
|
(-(view_distance as i32)..view_distance as i32 + 1).map(move |y| Vec2::new(x, y))
|
|
|
|
})
|
|
|
|
.map(|offset| offset + chunk_pos)
|
|
|
|
// Filter chunks outside the view distance
|
|
|
|
// Note: calculation from client chunk request filtering
|
|
|
|
.filter(|chunk_key| {
|
|
|
|
pos.0.xy().map(|e| e as f64).distance(
|
|
|
|
chunk_key.map(|e| e as f64 + 0.5) * TerrainChunkSize::RECT_SIZE.map(|e| e as f64),
|
|
|
|
) < (view_distance as f64 - 1.0 + 2.5 * 2.0_f64.sqrt())
|
|
|
|
* TerrainChunkSize::RECT_SIZE.x as f64
|
|
|
|
})
|
|
|
|
.for_each(|chunk_key| {
|
2021-03-15 22:50:26 +00:00
|
|
|
chunk_generator.generate_chunk(None, chunk_key, &slow_jobs, Arc::clone(world), index.clone());
|
2021-03-13 06:48:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ecs_mut()
|
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(Presence::new(view_distance, PresenceKind::Spectator))
|
|
|
|
}
|
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
fn initialize_character_data(&mut self, entity: EcsEntity, character_id: CharacterId) {
|
2020-03-10 02:27:32 +00:00
|
|
|
let spawn_point = self.ecs().read_resource::<SpawnPoint>().0;
|
|
|
|
|
|
|
|
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());
|
2020-07-07 00:11:37 +00:00
|
|
|
self.write_component(
|
|
|
|
entity,
|
2020-08-23 20:29:40 +00:00
|
|
|
comp::Alignment::Owned(self.read_component_copied(entity).unwrap()),
|
2020-07-07 00:11:37 +00:00
|
|
|
);
|
2020-10-01 01:35:57 +00:00
|
|
|
self.write_component(entity, comp::Buffs::default());
|
2021-03-01 20:44:29 +00:00
|
|
|
self.write_component(entity, comp::Auras::default());
|
2021-02-27 19:55:06 +00:00
|
|
|
self.write_component(entity, comp::Combo::default());
|
2020-03-15 14:27:06 +00:00
|
|
|
|
2020-08-23 20:17:16 +00:00
|
|
|
// Make sure physics components are updated
|
|
|
|
self.write_component(entity, comp::ForceUpdate);
|
|
|
|
|
2020-10-30 16:39:53 +00:00
|
|
|
const INITIAL_VD: u32 = 5; //will be changed after login
|
|
|
|
self.write_component(
|
|
|
|
entity,
|
|
|
|
Presence::new(INITIAL_VD, PresenceKind::Character(character_id)),
|
|
|
|
);
|
2020-04-25 13:41:27 +00:00
|
|
|
|
2020-06-16 01:00:32 +00:00
|
|
|
// Tell the client its request was successful.
|
2020-11-02 18:30:56 +00:00
|
|
|
if let Some(client) = self.ecs().read_storage::<Client>().get(entity) {
|
|
|
|
client.send_fallible(ServerGeneral::CharacterSuccess);
|
2020-06-16 01:00:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_character_data(&mut self, entity: EcsEntity, components: PersistedComponents) {
|
2021-01-08 19:12:09 +00:00
|
|
|
let (body, stats, inventory, waypoint) = components;
|
2020-06-01 07:31:13 +00:00
|
|
|
|
2020-08-23 20:29:40 +00:00
|
|
|
if let Some(player_uid) = self.read_component_copied::<Uid>(entity) {
|
2020-08-23 20:17:16 +00:00
|
|
|
// Notify clients of a player list update
|
2020-10-30 16:39:53 +00:00
|
|
|
self.notify_players(ServerGeneral::PlayerListUpdate(
|
2020-08-23 20:17:16 +00:00
|
|
|
PlayerListUpdate::SelectedCharacter(player_uid, CharacterInfo {
|
|
|
|
name: String::from(&stats.name),
|
|
|
|
}),
|
|
|
|
));
|
2020-06-16 01:00:32 +00:00
|
|
|
|
2020-08-23 20:17:16 +00:00
|
|
|
self.write_component(entity, comp::Collider::Box {
|
|
|
|
radius: body.radius(),
|
|
|
|
z_min: 0.0,
|
|
|
|
z_max: body.height(),
|
|
|
|
});
|
|
|
|
self.write_component(entity, body);
|
2021-01-04 17:16:42 +00:00
|
|
|
let (health_level, energy_level) = (
|
|
|
|
stats
|
|
|
|
.skill_set
|
2021-01-16 17:01:57 +00:00
|
|
|
.skill_level(Skill::General(GeneralSkill::HealthIncrease))
|
|
|
|
.unwrap_or(None)
|
2021-01-04 17:16:42 +00:00
|
|
|
.unwrap_or(0),
|
|
|
|
stats
|
|
|
|
.skill_set
|
2021-01-16 17:01:57 +00:00
|
|
|
.skill_level(Skill::General(GeneralSkill::EnergyIncrease))
|
|
|
|
.unwrap_or(None)
|
2021-01-04 17:16:42 +00:00
|
|
|
.unwrap_or(0),
|
2020-11-02 00:26:01 +00:00
|
|
|
);
|
2021-01-22 21:12:16 +00:00
|
|
|
self.write_component(entity, comp::Health::new(body, health_level));
|
|
|
|
self.write_component(entity, comp::Energy::new(body, energy_level));
|
2020-12-05 18:23:45 +00:00
|
|
|
self.write_component(entity, comp::Poise::new(body));
|
2020-08-23 20:17:16 +00:00
|
|
|
self.write_component(entity, stats);
|
|
|
|
self.write_component(entity, inventory);
|
|
|
|
self.write_component(
|
|
|
|
entity,
|
|
|
|
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::default()),
|
|
|
|
);
|
2020-11-03 00:12:49 +00:00
|
|
|
|
|
|
|
if let Some(waypoint) = waypoint {
|
|
|
|
self.write_component(entity, waypoint);
|
|
|
|
self.write_component(entity, comp::Pos(waypoint.get_pos()));
|
|
|
|
self.write_component(entity, comp::Vel(Vec3::zero()));
|
|
|
|
self.write_component(entity, comp::ForceUpdate);
|
|
|
|
}
|
2020-08-23 20:17:16 +00:00
|
|
|
}
|
2020-03-10 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 01:48:26 +00:00
|
|
|
/// Send the chat message to the proper players. Say and region are limited
|
|
|
|
/// by location. Faction and group are limited by component.
|
2020-07-12 20:18:57 +00:00
|
|
|
fn send_chat(&self, msg: comp::UnresolvedChatMsg) {
|
2020-06-05 01:48:26 +00:00
|
|
|
let ecs = self.ecs();
|
2020-06-11 05:16:42 +00:00
|
|
|
let is_within =
|
|
|
|
|target, a: &comp::Pos, b: &comp::Pos| a.0.distance_squared(b.0) < target * target;
|
2020-07-12 20:18:57 +00:00
|
|
|
|
|
|
|
let group_manager = ecs.read_resource::<comp::group::GroupManager>();
|
|
|
|
let resolved_msg = msg.clone().map_group(|group_id| {
|
|
|
|
group_manager
|
|
|
|
.group_info(group_id)
|
|
|
|
.map_or_else(|| "???".into(), |i| i.name.clone())
|
|
|
|
});
|
|
|
|
|
2020-06-05 01:48:26 +00:00
|
|
|
match &msg.chat_type {
|
2020-10-18 09:03:02 +00:00
|
|
|
comp::ChatType::Offline(_)
|
2020-06-12 07:43:20 +00:00
|
|
|
| comp::ChatType::CommandInfo
|
|
|
|
| comp::ChatType::CommandError
|
2020-07-01 19:05:44 +00:00
|
|
|
| comp::ChatType::Loot
|
2020-09-06 19:42:32 +00:00
|
|
|
| comp::ChatType::Kill(_, _)
|
2020-06-28 17:10:01 +00:00
|
|
|
| comp::ChatType::Meta
|
2020-10-30 16:39:53 +00:00
|
|
|
| comp::ChatType::World(_) => self.notify_players(ServerGeneral::ChatMsg(resolved_msg)),
|
2020-10-18 09:03:02 +00:00
|
|
|
comp::ChatType::Online(u) => {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, uid) in
|
|
|
|
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
2020-10-18 09:03:02 +00:00
|
|
|
{
|
|
|
|
if uid != u {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
2020-10-18 09:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-05 01:48:26 +00:00
|
|
|
comp::ChatType::Tell(u, t) => {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, uid) in
|
|
|
|
(&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
|
2020-06-05 01:48:26 +00:00
|
|
|
{
|
|
|
|
if uid == u || uid == t {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
2020-06-05 01:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-11 05:16:42 +00:00
|
|
|
comp::ChatType::Say(uid) => {
|
|
|
|
let entity_opt =
|
|
|
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
|
|
let positions = ecs.read_storage::<comp::Pos>();
|
|
|
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
2020-06-11 05:16:42 +00:00
|
|
|
if is_within(comp::ChatMsg::SAY_DISTANCE, pos, speaker_pos) {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
2020-06-11 05:16:42 +00:00
|
|
|
}
|
2020-06-05 01:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-11 05:16:42 +00:00
|
|
|
comp::ChatType::Region(uid) => {
|
|
|
|
let entity_opt =
|
|
|
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
|
|
let positions = ecs.read_storage::<comp::Pos>();
|
|
|
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
2020-06-11 05:16:42 +00:00
|
|
|
if is_within(comp::ChatMsg::REGION_DISTANCE, pos, speaker_pos) {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
2020-06-11 05:16:42 +00:00
|
|
|
}
|
2020-06-05 01:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-11 05:16:42 +00:00
|
|
|
comp::ChatType::Npc(uid, _r) => {
|
|
|
|
let entity_opt =
|
|
|
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
|
|
let positions = ecs.read_storage::<comp::Pos>();
|
|
|
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
2020-06-11 05:16:42 +00:00
|
|
|
if is_within(comp::ChatMsg::NPC_DISTANCE, pos, speaker_pos) {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
2020-06-11 05:16:42 +00:00
|
|
|
}
|
2020-06-05 01:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-03-16 01:30:35 +00:00
|
|
|
comp::ChatType::NpcSay(uid, _r) => {
|
|
|
|
let entity_opt =
|
|
|
|
(*ecs.read_resource::<UidAllocator>()).retrieve_entity_internal(uid.0);
|
|
|
|
let positions = ecs.read_storage::<comp::Pos>();
|
|
|
|
if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
|
|
|
|
for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
|
|
|
|
if is_within(comp::ChatMsg::NPC_SAY_DISTANCE, pos, speaker_pos) {
|
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-12 17:44:29 +00:00
|
|
|
comp::ChatType::FactionMeta(s) | comp::ChatType::Faction(_, s) => {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, faction) in (
|
|
|
|
&ecs.read_storage::<Client>(),
|
2020-06-05 01:48:26 +00:00
|
|
|
&ecs.read_storage::<comp::Faction>(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
if s == &faction.0 {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
2020-06-05 01:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-07-12 20:18:57 +00:00
|
|
|
comp::ChatType::GroupMeta(g) | comp::ChatType::Group(_, g) => {
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, group) in (
|
|
|
|
&ecs.read_storage::<Client>(),
|
2020-07-12 20:18:57 +00:00
|
|
|
&ecs.read_storage::<comp::Group>(),
|
2020-06-05 01:48:26 +00:00
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
2020-07-12 20:18:57 +00:00
|
|
|
if g == group {
|
2020-11-02 18:30:56 +00:00
|
|
|
client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
|
2020-06-05 01:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends the message to all connected clients
|
2020-10-30 16:39:53 +00:00
|
|
|
fn notify_players(&self, msg: ServerGeneral) {
|
2020-11-02 18:30:56 +00:00
|
|
|
let mut msg = Some(msg);
|
2020-10-18 23:53:19 +00:00
|
|
|
let mut lazy_msg = None;
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, _) in (
|
|
|
|
&self.ecs().read_storage::<Client>(),
|
2020-10-30 16:39:53 +00:00
|
|
|
&self.ecs().read_storage::<comp::Player>(),
|
2020-10-16 19:37:28 +00:00
|
|
|
)
|
2020-03-10 02:27:32 +00:00
|
|
|
.join()
|
|
|
|
{
|
2020-10-18 23:53:19 +00:00
|
|
|
if lazy_msg.is_none() {
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg = Some(client.prepare(msg.take().unwrap()));
|
2020-10-18 23:53:19 +00:00
|
|
|
}
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg.as_ref().map(|ref msg| client.send_prepared(&msg));
|
2020-03-10 02:27:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 10:31:49 +00:00
|
|
|
/// Sends the message to all clients playing in game
|
2020-10-12 08:18:28 +00:00
|
|
|
fn notify_in_game_clients(&self, msg: ServerGeneral) {
|
2020-11-02 18:30:56 +00:00
|
|
|
let mut msg = Some(msg);
|
2020-10-18 23:53:19 +00:00
|
|
|
let mut lazy_msg = None;
|
2020-11-02 18:30:56 +00:00
|
|
|
for (client, _) in (
|
|
|
|
&mut self.ecs().write_storage::<Client>(),
|
2020-10-30 16:39:53 +00:00
|
|
|
&self.ecs().read_storage::<Presence>(),
|
2020-10-16 19:37:28 +00:00
|
|
|
)
|
2020-10-07 10:31:49 +00:00
|
|
|
.join()
|
|
|
|
{
|
2020-10-18 23:53:19 +00:00
|
|
|
if lazy_msg.is_none() {
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg = Some(client.prepare(msg.take().unwrap()));
|
2020-10-18 23:53:19 +00:00
|
|
|
}
|
2020-11-02 18:30:56 +00:00
|
|
|
lazy_msg.as_ref().map(|ref msg| client.send_prepared(&msg));
|
2020-10-07 10:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:27:32 +00:00
|
|
|
fn delete_entity_recorded(
|
|
|
|
&mut self,
|
|
|
|
entity: EcsEntity,
|
|
|
|
) -> Result<(), specs::error::WrongGeneration> {
|
2020-04-26 17:03:19 +00:00
|
|
|
// Remove entity from a group if they are in one
|
|
|
|
{
|
2020-11-02 18:30:56 +00:00
|
|
|
let clients = self.ecs().read_storage::<Client>();
|
2020-04-26 17:03:19 +00:00
|
|
|
let uids = self.ecs().read_storage::<Uid>();
|
|
|
|
let mut group_manager = self.ecs().write_resource::<comp::group::GroupManager>();
|
2020-07-12 03:12:03 +00:00
|
|
|
group_manager.entity_deleted(
|
2020-04-26 17:03:19 +00:00
|
|
|
entity,
|
|
|
|
&mut self.ecs().write_storage(),
|
|
|
|
&self.ecs().read_storage(),
|
|
|
|
&uids,
|
|
|
|
&self.ecs().entities(),
|
|
|
|
&mut |entity, group_change| {
|
2020-11-02 18:30:56 +00:00
|
|
|
clients
|
|
|
|
.get(entity)
|
|
|
|
.and_then(|c| {
|
2020-04-26 17:03:19 +00:00
|
|
|
group_change
|
|
|
|
.try_map(|e| uids.get(e).copied())
|
2020-11-02 18:30:56 +00:00
|
|
|
.map(|g| (g, c))
|
2020-04-26 17:03:19 +00:00
|
|
|
})
|
2020-11-02 18:30:56 +00:00
|
|
|
.map(|(g, c)| c.send(ServerGeneral::GroupUpdate(g)));
|
2020-04-26 17:03:19 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:27:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|