2020-07-04 23:55:13 +00:00
|
|
|
use common::{
|
2020-12-06 02:29:46 +00:00
|
|
|
comp::{HealthSource, Object, PhysicsState, PoiseChange, PoiseSource, Pos, Vel},
|
2020-11-01 17:15:46 +00:00
|
|
|
effect::Effect,
|
2020-07-04 23:55:13 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-12-01 00:28:00 +00:00
|
|
|
resources::DeltaTime,
|
2021-05-06 18:50:16 +00:00
|
|
|
Damage, DamageKind, DamageSource, Explosion, RadiusEffect,
|
2020-07-04 23:55:13 +00:00
|
|
|
};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_ecs::{Job, Origin, Phase, System};
|
2021-03-04 14:00:16 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadStorage, WriteStorage};
|
2020-07-04 23:55:13 +00:00
|
|
|
|
2020-07-05 12:39:28 +00:00
|
|
|
/// This system is responsible for handling misc object behaviours
|
2021-03-04 14:00:16 +00:00
|
|
|
#[derive(Default)]
|
2020-07-04 23:55:13 +00:00
|
|
|
pub struct Sys;
|
2021-03-08 11:13:59 +00:00
|
|
|
impl<'a> System<'a> for Sys {
|
2020-07-04 23:55:13 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
|
|
|
Read<'a, DeltaTime>,
|
|
|
|
Read<'a, EventBus<ServerEvent>>,
|
|
|
|
ReadStorage<'a, Pos>,
|
2020-08-11 11:52:15 +00:00
|
|
|
ReadStorage<'a, Vel>,
|
2020-07-04 23:55:13 +00:00
|
|
|
ReadStorage<'a, PhysicsState>,
|
|
|
|
WriteStorage<'a, Object>,
|
|
|
|
);
|
|
|
|
|
2021-03-04 14:00:16 +00:00
|
|
|
const NAME: &'static str = "object";
|
|
|
|
const ORIGIN: Origin = Origin::Server;
|
|
|
|
const PHASE: Phase = Phase::Create;
|
|
|
|
|
2020-07-04 23:55:13 +00:00
|
|
|
fn run(
|
2021-03-08 11:13:59 +00:00
|
|
|
_job: &mut Job<Self>,
|
2020-08-11 11:52:15 +00:00
|
|
|
(entities, _dt, server_bus, positions, velocities, physics_states, mut objects): Self::SystemData,
|
2020-07-04 23:55:13 +00:00
|
|
|
) {
|
|
|
|
let mut server_emitter = server_bus.emitter();
|
|
|
|
|
|
|
|
// Objects
|
2020-08-11 14:05:34 +00:00
|
|
|
for (entity, pos, vel, physics, object) in (
|
|
|
|
&entities,
|
|
|
|
&positions,
|
|
|
|
&velocities,
|
|
|
|
&physics_states,
|
|
|
|
&mut objects,
|
|
|
|
)
|
|
|
|
.join()
|
2020-07-04 23:55:13 +00:00
|
|
|
{
|
|
|
|
match object {
|
2020-07-05 14:06:01 +00:00
|
|
|
Object::Bomb { owner } => {
|
|
|
|
if physics.on_surface().is_some() {
|
2020-07-04 23:55:13 +00:00
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::Suicide,
|
|
|
|
});
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
2020-10-15 00:43:53 +00:00
|
|
|
explosion: Explosion {
|
2020-10-30 20:41:21 +00:00
|
|
|
effects: vec![
|
2021-01-30 22:35:00 +00:00
|
|
|
RadiusEffect::Entity(Effect::Damage(Damage {
|
|
|
|
source: DamageSource::Explosion,
|
2021-05-06 18:50:16 +00:00
|
|
|
kind: DamageKind::Energy,
|
2021-03-15 04:36:07 +00:00
|
|
|
value: 400.0,
|
2021-01-30 22:35:00 +00:00
|
|
|
})),
|
|
|
|
RadiusEffect::Entity(Effect::PoiseChange(PoiseChange {
|
|
|
|
source: PoiseSource::Explosion,
|
|
|
|
amount: -100,
|
|
|
|
})),
|
2020-10-30 20:41:21 +00:00
|
|
|
RadiusEffect::TerrainDestruction(4.0),
|
|
|
|
],
|
2020-10-06 02:19:41 +00:00
|
|
|
radius: 12.0,
|
2021-02-16 05:18:05 +00:00
|
|
|
reagent: None,
|
2020-10-06 02:19:41 +00:00
|
|
|
},
|
2020-07-04 23:55:13 +00:00
|
|
|
owner: *owner,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-08-11 14:05:34 +00:00
|
|
|
Object::Firework { owner, reagent } => {
|
|
|
|
if vel.0.z < 0.0 {
|
2021-03-06 19:03:21 +00:00
|
|
|
const ENABLE_RECURSIVE_FIREWORKS: bool = true;
|
|
|
|
if ENABLE_RECURSIVE_FIREWORKS {
|
|
|
|
use common::{
|
2021-03-23 09:51:53 +00:00
|
|
|
comp::{object, Body, LightEmitter, Projectile},
|
2021-03-06 19:03:21 +00:00
|
|
|
util::Dir,
|
|
|
|
};
|
|
|
|
use rand::Rng;
|
|
|
|
use std::{f32::consts::PI, time::Duration};
|
|
|
|
use vek::{Rgb, Vec3};
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
// Note that if the expected fireworks per firework is > 1, this will
|
|
|
|
// eventually cause enough server lag that more players can't log in.
|
2021-03-06 20:11:19 +00:00
|
|
|
let thresholds: &[(f32, usize)] = &[(0.25, 2), (0.7, 1)];
|
|
|
|
let expected = {
|
|
|
|
let mut total = 0.0;
|
|
|
|
let mut cumulative_probability = 0.0;
|
|
|
|
for (p, n) in thresholds {
|
|
|
|
total += (p - cumulative_probability) * *n as f32;
|
|
|
|
cumulative_probability += p;
|
|
|
|
}
|
|
|
|
total
|
2021-03-06 19:03:21 +00:00
|
|
|
};
|
2021-03-06 20:11:19 +00:00
|
|
|
assert!(expected < 1.0);
|
|
|
|
let num_fireworks = (|| {
|
|
|
|
let x = rng.gen_range(0.0..1.0);
|
|
|
|
for (p, n) in thresholds {
|
|
|
|
if x < *p {
|
|
|
|
return *n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
0
|
|
|
|
})();
|
2021-03-06 19:03:21 +00:00
|
|
|
for _ in 0..num_fireworks {
|
|
|
|
let speed: f32 = rng.gen_range(40.0..80.0);
|
|
|
|
let theta: f32 = rng.gen_range(0.0..2.0 * PI);
|
|
|
|
let phi: f32 = rng.gen_range(0.25 * PI..0.5 * PI);
|
|
|
|
let dir = Dir::from_unnormalized(Vec3::new(
|
|
|
|
theta.cos(),
|
|
|
|
theta.sin(),
|
|
|
|
phi.sin(),
|
|
|
|
))
|
|
|
|
.expect("nonzero vector should normalize");
|
|
|
|
server_emitter.emit(ServerEvent::Shoot {
|
|
|
|
entity,
|
|
|
|
dir,
|
|
|
|
body: Body::Object(object::Body::for_firework(*reagent)),
|
|
|
|
light: Some(LightEmitter {
|
|
|
|
animated: true,
|
|
|
|
flicker: 2.0,
|
|
|
|
strength: 2.0,
|
|
|
|
col: Rgb::new(1.0, 1.0, 0.0),
|
|
|
|
}),
|
|
|
|
projectile: Projectile {
|
|
|
|
hit_solid: Vec::new(),
|
|
|
|
hit_entity: Vec::new(),
|
|
|
|
time_left: Duration::from_secs(60),
|
|
|
|
owner: *owner,
|
|
|
|
ignore_group: true,
|
2021-06-01 01:51:47 +00:00
|
|
|
is_sticky: true,
|
|
|
|
is_point: true,
|
2021-03-06 19:03:21 +00:00
|
|
|
},
|
|
|
|
speed,
|
|
|
|
object: Some(Object::Firework {
|
|
|
|
owner: *owner,
|
|
|
|
reagent: *reagent,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 11:52:15 +00:00
|
|
|
server_emitter.emit(ServerEvent::Destroy {
|
|
|
|
entity,
|
|
|
|
cause: HealthSource::Suicide,
|
|
|
|
});
|
|
|
|
server_emitter.emit(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
2020-10-15 00:43:53 +00:00
|
|
|
explosion: Explosion {
|
2020-10-30 20:41:21 +00:00
|
|
|
effects: vec![
|
2021-01-30 22:35:00 +00:00
|
|
|
RadiusEffect::Entity(Effect::Damage(Damage {
|
|
|
|
source: DamageSource::Explosion,
|
2021-05-06 18:50:16 +00:00
|
|
|
kind: DamageKind::Energy,
|
2021-01-30 22:35:00 +00:00
|
|
|
value: 50.0,
|
|
|
|
})),
|
|
|
|
RadiusEffect::Entity(Effect::PoiseChange(PoiseChange {
|
|
|
|
source: PoiseSource::Explosion,
|
|
|
|
amount: -40,
|
|
|
|
})),
|
2020-10-30 20:41:21 +00:00
|
|
|
RadiusEffect::TerrainDestruction(4.0),
|
|
|
|
],
|
2020-10-06 02:19:41 +00:00
|
|
|
radius: 12.0,
|
2021-02-16 05:18:05 +00:00
|
|
|
reagent: Some(*reagent),
|
2020-10-06 02:19:41 +00:00
|
|
|
},
|
2020-08-11 11:52:15 +00:00
|
|
|
owner: *owner,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-07-04 23:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|