2021-01-26 02:50:16 +00:00
|
|
|
use crate::apply_attack;
|
2020-12-01 00:28:00 +00:00
|
|
|
use common::{
|
2021-01-26 00:31:06 +00:00
|
|
|
comp::{buff, group, Body, CharacterState, Health, Inventory, MeleeAttack, Ori, Pos, Scale},
|
2020-03-25 14:24:55 +00:00
|
|
|
event::{EventBus, LocalEvent, ServerEvent},
|
2020-09-14 12:56:05 +00:00
|
|
|
metrics::SysMetrics,
|
2020-08-26 08:11:31 +00:00
|
|
|
span,
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::Uid,
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2020-11-02 00:26:01 +00:00
|
|
|
GroupTarget,
|
2019-06-11 19:28:25 +00:00
|
|
|
};
|
2020-10-19 03:00:35 +00:00
|
|
|
use rand::{thread_rng, Rng};
|
2020-09-14 12:56:05 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadExpect, ReadStorage, System, WriteStorage};
|
2020-10-01 00:40:46 +00:00
|
|
|
use std::time::Duration;
|
2019-08-24 20:41:34 +00:00
|
|
|
use vek::*;
|
2019-06-11 19:28:25 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// This system is responsible for handling accepted inputs like moving or
|
|
|
|
/// attacking
|
2019-06-11 19:28:25 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-06-11 19:28:25 +00:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2019-09-25 21:31:25 +00:00
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
2020-03-25 14:24:55 +00:00
|
|
|
Read<'a, EventBus<LocalEvent>>,
|
2020-09-14 12:56:05 +00:00
|
|
|
ReadExpect<'a, SysMetrics>,
|
2019-09-25 21:31:25 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-06-11 19:28:25 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
|
|
|
ReadStorage<'a, Ori>,
|
2019-12-01 23:40:05 +00:00
|
|
|
ReadStorage<'a, Scale>,
|
2019-12-20 03:31:41 +00:00
|
|
|
ReadStorage<'a, Body>,
|
2020-10-31 22:34:08 +00:00
|
|
|
ReadStorage<'a, Health>,
|
2021-01-08 19:12:09 +00:00
|
|
|
ReadStorage<'a, Inventory>,
|
2020-04-26 17:03:19 +00:00
|
|
|
ReadStorage<'a, group::Group>,
|
2021-01-26 00:31:06 +00:00
|
|
|
WriteStorage<'a, MeleeAttack>,
|
2020-11-05 22:48:04 +00:00
|
|
|
ReadStorage<'a, CharacterState>,
|
2019-06-11 19:28:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(
|
|
|
|
entities,
|
2019-09-25 21:31:25 +00:00
|
|
|
server_bus,
|
2020-03-25 14:24:55 +00:00
|
|
|
local_bus,
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics,
|
2019-09-25 21:31:25 +00:00
|
|
|
uids,
|
2019-06-11 19:28:25 +00:00
|
|
|
positions,
|
|
|
|
orientations,
|
2019-12-01 23:40:05 +00:00
|
|
|
scales,
|
2019-12-20 03:31:41 +00:00
|
|
|
bodies,
|
2020-10-31 22:34:08 +00:00
|
|
|
healths,
|
2021-01-08 19:12:09 +00:00
|
|
|
inventories,
|
2020-04-26 17:03:19 +00:00
|
|
|
groups,
|
2020-08-08 22:22:21 +00:00
|
|
|
mut attacking_storage,
|
2020-11-05 22:48:04 +00:00
|
|
|
char_states,
|
2019-06-11 19:28:25 +00:00
|
|
|
): Self::SystemData,
|
|
|
|
) {
|
2020-09-14 12:56:05 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2020-10-17 22:42:43 +00:00
|
|
|
span!(_guard, "run", "melee::Sys::run");
|
2020-03-22 04:49:32 +00:00
|
|
|
let mut server_emitter = server_bus.emitter();
|
2020-11-16 23:32:23 +00:00
|
|
|
let _local_emitter = local_bus.emitter();
|
2019-06-11 19:28:25 +00:00
|
|
|
// Attacks
|
2021-01-24 04:00:57 +00:00
|
|
|
for (entity, uid, pos, ori, scale_maybe, attack, body) in (
|
2019-09-29 11:41:04 +00:00
|
|
|
&entities,
|
|
|
|
&uids,
|
|
|
|
&positions,
|
|
|
|
&orientations,
|
2019-12-01 23:40:05 +00:00
|
|
|
scales.maybe(),
|
2020-02-11 15:42:17 +00:00
|
|
|
&mut attacking_storage,
|
2021-01-24 04:00:57 +00:00
|
|
|
&bodies,
|
2019-09-29 11:41:04 +00:00
|
|
|
)
|
|
|
|
.join()
|
2019-08-24 19:12:54 +00:00
|
|
|
{
|
2020-03-10 17:54:59 +00:00
|
|
|
if attack.applied {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attack.applied = true;
|
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Go through all other entities
|
2021-01-26 02:50:16 +00:00
|
|
|
for (b, pos_b, scale_b_maybe, health_b, body_b, char_state_b_maybe, inventory_b_maybe) in (
|
2020-11-05 22:48:04 +00:00
|
|
|
&entities,
|
|
|
|
&positions,
|
|
|
|
scales.maybe(),
|
|
|
|
&healths,
|
|
|
|
&bodies,
|
|
|
|
char_states.maybe(),
|
2021-01-26 02:50:16 +00:00
|
|
|
inventories.maybe(),
|
2020-11-05 22:48:04 +00:00
|
|
|
)
|
|
|
|
.join()
|
2020-09-05 23:45:36 +00:00
|
|
|
{
|
2020-09-05 16:41:11 +00:00
|
|
|
// 2D versions
|
|
|
|
let pos2 = Vec2::from(pos.0);
|
|
|
|
let pos_b2 = Vec2::<f32>::from(pos_b.0);
|
|
|
|
let ori2 = Vec2::from(*ori.0);
|
|
|
|
|
2020-02-24 13:32:12 +00:00
|
|
|
// Scales
|
|
|
|
let scale = scale_maybe.map_or(1.0, |s| s.0);
|
|
|
|
let scale_b = scale_b_maybe.map_or(1.0, |s| s.0);
|
2021-01-24 04:00:57 +00:00
|
|
|
let rad = body.radius() * scale;
|
2020-02-24 13:32:12 +00:00
|
|
|
let rad_b = body_b.radius() * scale_b;
|
2019-08-24 19:12:54 +00:00
|
|
|
|
2020-11-06 01:22:26 +00:00
|
|
|
// Check if entity is dodging
|
2020-12-31 18:37:25 +00:00
|
|
|
let is_dodge = char_state_b_maybe.map_or(false, |c_s| c_s.is_melee_dodge());
|
2020-11-05 22:48:04 +00:00
|
|
|
|
2020-09-30 00:47:11 +00:00
|
|
|
// Check if it is a hit
|
2020-02-24 13:32:12 +00:00
|
|
|
if entity != b
|
2020-10-31 22:34:08 +00:00
|
|
|
&& !health_b.is_dead
|
2020-09-05 16:41:11 +00:00
|
|
|
// Spherical wedge shaped attack field
|
2021-01-24 04:00:57 +00:00
|
|
|
&& pos.0.distance_squared(pos_b.0) < (rad + rad_b + scale * attack.range).powi(2)
|
2020-09-05 16:41:11 +00:00
|
|
|
&& ori2.angle_between(pos_b2 - pos2) < attack.max_angle + (rad_b / pos2.distance(pos_b2)).atan()
|
2020-02-24 13:32:12 +00:00
|
|
|
{
|
2020-04-26 17:03:19 +00:00
|
|
|
// See if entities are in the same group
|
|
|
|
let same_group = groups
|
|
|
|
.get(entity)
|
|
|
|
.map(|group_a| Some(group_a) == groups.get(b))
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
2020-11-02 00:26:01 +00:00
|
|
|
let target_group = if same_group {
|
|
|
|
GroupTarget::InGroup
|
|
|
|
} else {
|
|
|
|
GroupTarget::OutOfGroup
|
|
|
|
};
|
|
|
|
|
2021-01-26 02:50:16 +00:00
|
|
|
let dir = Dir::new((pos_b.0 - pos.0).try_normalized().unwrap_or(*ori.0));
|
|
|
|
|
|
|
|
let server_events = apply_attack(
|
|
|
|
&attack.attack,
|
|
|
|
target_group,
|
|
|
|
b,
|
|
|
|
inventory_b_maybe,
|
|
|
|
*uid,
|
|
|
|
dir,
|
|
|
|
);
|
|
|
|
|
|
|
|
for event in server_events {
|
|
|
|
server_emitter.emit(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*for (target, damage) in attack.damages.iter() {
|
2020-11-04 02:01:12 +00:00
|
|
|
if let Some(target) = target {
|
2020-11-05 22:48:04 +00:00
|
|
|
if *target != target_group
|
2020-12-16 23:30:33 +00:00
|
|
|
|| (!matches!(target, GroupTarget::InGroup) && is_dodge)
|
2020-11-05 22:48:04 +00:00
|
|
|
{
|
2020-11-04 02:01:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 10:11:37 +00:00
|
|
|
|
2021-01-13 03:26:51 +00:00
|
|
|
let change = damage.modify_damage(inventories.get(b), Some(*uid));
|
2020-12-16 23:30:33 +00:00
|
|
|
|
2020-11-02 00:26:01 +00:00
|
|
|
server_emitter.emit(ServerEvent::Damage { entity: b, change });
|
2021-01-27 03:05:13 +00:00
|
|
|
|
2020-10-19 03:00:35 +00:00
|
|
|
// Apply bleeding buff on melee hits with 10% chance
|
|
|
|
// TODO: Don't have buff uniformly applied on all melee attacks
|
2020-12-06 02:29:46 +00:00
|
|
|
if change.amount < 0 && thread_rng().gen::<f32>() < 0.1 {
|
2020-10-25 01:20:03 +00:00
|
|
|
use buff::*;
|
2020-10-19 03:00:35 +00:00
|
|
|
server_emitter.emit(ServerEvent::Buff {
|
2020-10-25 01:20:03 +00:00
|
|
|
entity: b,
|
2020-10-24 23:07:38 +00:00
|
|
|
buff_change: BuffChange::Add(Buff::new(
|
|
|
|
BuffKind::Bleeding,
|
|
|
|
BuffData {
|
2020-12-06 02:29:46 +00:00
|
|
|
strength: -change.amount as f32 / 10.0,
|
2020-10-19 03:00:35 +00:00
|
|
|
duration: Some(Duration::from_secs(10)),
|
|
|
|
},
|
2020-10-24 23:07:38 +00:00
|
|
|
vec![BuffCategory::Physical],
|
|
|
|
BuffSource::Character { by: *uid },
|
2020-10-19 03:00:35 +00:00
|
|
|
)),
|
|
|
|
});
|
|
|
|
}
|
2021-01-27 03:05:13 +00:00
|
|
|
|
2020-09-13 01:33:46 +00:00
|
|
|
let kb_dir = Dir::new((pos_b.0 - pos.0).try_normalized().unwrap_or(*ori.0));
|
2020-10-27 22:16:17 +00:00
|
|
|
let impulse = attack.knockback.calculate_impulse(kb_dir);
|
|
|
|
if !impulse.is_approx_zero() {
|
|
|
|
server_emitter.emit(ServerEvent::Knockback { entity: b, impulse });
|
|
|
|
}
|
2020-11-04 02:01:12 +00:00
|
|
|
|
2021-01-27 03:05:13 +00:00
|
|
|
let poise_change = poise_change.modify_poise_damage(inventories.get(b));
|
|
|
|
if poise_change.amount.abs() > 0 {
|
|
|
|
server_emitter.emit(ServerEvent::PoiseChange {
|
|
|
|
entity: b,
|
|
|
|
change: poise_change,
|
|
|
|
kb_dir: *kb_dir,
|
|
|
|
});
|
|
|
|
}
|
2020-12-10 00:32:24 +00:00
|
|
|
|
2020-11-04 02:01:12 +00:00
|
|
|
attack.hit_count += 1;
|
2021-01-26 02:50:16 +00:00
|
|
|
}*/
|
2020-02-11 15:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
sys_metrics.melee_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-11 19:28:25 +00:00
|
|
|
}
|
2020-09-05 23:45:36 +00:00
|
|
|
}
|