2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2021-02-08 17:31:17 +00:00
|
|
|
comp::{BuffChange, ControlEvent, Controller},
|
2021-02-22 19:33:54 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-09-14 12:56:05 +00:00
|
|
|
metrics::SysMetrics,
|
2020-08-26 08:11:31 +00:00
|
|
|
span,
|
2021-02-22 19:33:54 +00:00
|
|
|
uid::UidAllocator,
|
2019-06-09 14:20:20 +00:00
|
|
|
};
|
2019-09-09 19:11:40 +00:00
|
|
|
use specs::{
|
|
|
|
saveload::{Marker, MarkerAllocator},
|
2021-02-22 19:33:54 +00:00
|
|
|
shred::ResourceId,
|
|
|
|
Entities, Join, Read, ReadExpect, System, SystemData, World, WriteStorage,
|
2019-09-09 19:11:40 +00:00
|
|
|
};
|
2020-11-03 22:46:07 +00:00
|
|
|
use vek::*;
|
2019-06-09 14:20:20 +00:00
|
|
|
|
2021-02-22 19:33:54 +00:00
|
|
|
#[derive(SystemData)]
|
2021-02-22 21:02:37 +00:00
|
|
|
pub struct ReadData<'a> {
|
2021-02-22 19:33:54 +00:00
|
|
|
entities: Entities<'a>,
|
|
|
|
uid_allocator: Read<'a, UidAllocator>,
|
|
|
|
server_bus: Read<'a, EventBus<ServerEvent>>,
|
|
|
|
metrics: ReadExpect<'a, SysMetrics>,
|
|
|
|
}
|
2019-11-22 00:53:28 +00:00
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
pub struct Sys;
|
2019-12-03 06:30:08 +00:00
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2021-02-22 21:02:37 +00:00
|
|
|
type SystemData = (ReadData<'a>, WriteStorage<'a, Controller>);
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2021-02-22 21:02:37 +00:00
|
|
|
fn run(&mut self, (read_data, mut controllers): Self::SystemData) {
|
2020-09-14 12:56:05 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "controller::Sys::run");
|
2021-02-22 21:02:37 +00:00
|
|
|
let mut server_emitter = read_data.server_bus.emitter();
|
2020-03-23 17:13:44 +00:00
|
|
|
|
2021-02-22 21:02:37 +00:00
|
|
|
for (entity, controller) in (&read_data.entities, &mut controllers).join() {
|
2020-03-24 07:38:16 +00:00
|
|
|
let mut inputs = &mut controller.inputs;
|
|
|
|
|
|
|
|
// Note(imbris): I avoided incrementing the duration with inputs.tick() because
|
|
|
|
// this is being done manually in voxygen right now so it would double up on
|
|
|
|
// speed of time.
|
|
|
|
// Perhaphs the duration aspects of inputs could be
|
|
|
|
// calculated exclusively on the server (since the client can't be
|
|
|
|
// trusted anyway). It needs to be considered if these calculations
|
|
|
|
// being on the client are critical for responsiveness/client-side prediction.
|
|
|
|
inputs.tick_freshness();
|
2019-10-15 04:06:14 +00:00
|
|
|
|
2019-12-20 13:30:37 +00:00
|
|
|
// Update `inputs.move_dir`.
|
|
|
|
inputs.move_dir = if inputs.move_dir.magnitude_squared() > 1.0 {
|
|
|
|
// Cap move_dir to 1
|
|
|
|
inputs.move_dir.normalized()
|
|
|
|
} else {
|
|
|
|
inputs.move_dir
|
2019-12-03 06:30:08 +00:00
|
|
|
};
|
2020-11-03 22:46:07 +00:00
|
|
|
inputs.move_z = inputs.move_z.clamped(-1.0, 1.0);
|
2019-06-09 14:20:20 +00:00
|
|
|
|
2019-12-03 06:30:08 +00:00
|
|
|
// Process other controller events
|
2019-10-21 00:59:53 +00:00
|
|
|
for event in controller.events.drain(..) {
|
2019-09-09 19:11:40 +00:00
|
|
|
match event {
|
|
|
|
ControlEvent::Mount(mountee_uid) => {
|
2021-02-22 21:02:37 +00:00
|
|
|
if let Some(mountee_entity) = read_data
|
2021-02-22 19:33:54 +00:00
|
|
|
.uid_allocator
|
|
|
|
.retrieve_entity_internal(mountee_uid.id())
|
2019-09-09 19:11:40 +00:00
|
|
|
{
|
|
|
|
server_emitter.emit(ServerEvent::Mount(entity, mountee_entity));
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-10-16 06:08:45 +00:00
|
|
|
ControlEvent::RemoveBuff(buff_id) => {
|
|
|
|
server_emitter.emit(ServerEvent::Buff {
|
2020-10-25 01:20:03 +00:00
|
|
|
entity,
|
2020-10-24 20:12:37 +00:00
|
|
|
buff_change: BuffChange::RemoveFromController(buff_id),
|
2020-10-16 06:08:45 +00:00
|
|
|
});
|
|
|
|
},
|
2019-09-09 19:11:40 +00:00
|
|
|
ControlEvent::Unmount => server_emitter.emit(ServerEvent::Unmount(entity)),
|
2020-10-07 02:23:20 +00:00
|
|
|
ControlEvent::EnableLantern => {
|
|
|
|
server_emitter.emit(ServerEvent::EnableLantern(entity))
|
|
|
|
},
|
|
|
|
ControlEvent::DisableLantern => {
|
|
|
|
server_emitter.emit(ServerEvent::DisableLantern(entity))
|
2020-05-04 15:15:31 +00:00
|
|
|
},
|
2021-01-31 20:29:50 +00:00
|
|
|
ControlEvent::Interact(npc_uid) => {
|
2021-02-22 21:02:37 +00:00
|
|
|
if let Some(npc_entity) = read_data
|
2021-02-22 19:33:54 +00:00
|
|
|
.uid_allocator
|
|
|
|
.retrieve_entity_internal(npc_uid.id())
|
2021-01-31 20:29:50 +00:00
|
|
|
{
|
|
|
|
server_emitter.emit(ServerEvent::NpcInteract(entity, npc_entity));
|
|
|
|
}
|
|
|
|
},
|
2021-02-13 23:32:55 +00:00
|
|
|
ControlEvent::InitiateInvite(inviter_uid, kind) => {
|
|
|
|
server_emitter.emit(ServerEvent::InitiateInvite(entity, inviter_uid, kind));
|
|
|
|
},
|
|
|
|
ControlEvent::InviteResponse(response) => {
|
|
|
|
server_emitter.emit(ServerEvent::InviteResponse(entity, response));
|
|
|
|
},
|
|
|
|
ControlEvent::PerformTradeAction(trade_id, action) => {
|
|
|
|
server_emitter
|
|
|
|
.emit(ServerEvent::ProcessTradeAction(entity, trade_id, action));
|
2021-02-11 00:59:14 +00:00
|
|
|
},
|
2021-03-01 22:40:42 +00:00
|
|
|
ControlEvent::InventoryEvent(event) => {
|
|
|
|
server_emitter.emit(ServerEvent::InventoryManip(entity, event.into()));
|
2020-03-24 07:38:16 +00:00
|
|
|
},
|
2020-04-26 17:03:19 +00:00
|
|
|
ControlEvent::GroupManip(manip) => {
|
|
|
|
server_emitter.emit(ServerEvent::GroupManip(entity, manip))
|
|
|
|
},
|
2020-03-24 07:38:16 +00:00
|
|
|
ControlEvent::Respawn => server_emitter.emit(ServerEvent::Respawn(entity)),
|
2019-09-09 19:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
2021-02-22 21:02:37 +00:00
|
|
|
read_data.metrics.controller_ns.store(
|
2020-12-15 23:51:07 +00:00
|
|
|
start_time.elapsed().as_nanos() as u64,
|
2020-09-14 12:56:05 +00:00
|
|
|
std::sync::atomic::Ordering::Relaxed,
|
|
|
|
);
|
2019-06-09 14:20:20 +00:00
|
|
|
}
|
|
|
|
}
|