1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use common::{
    comp::{object, Body, Object, PhysicsState, Pos, Teleporting, Vel},
    consts::TELEPORTER_RADIUS,
    effect::Effect,
    event::{ChangeBodyEvent, DeleteEvent, EmitExt, EventBus, ExplosionEvent, ShootEvent},
    event_emitters,
    outcome::Outcome,
    resources::{DeltaTime, Time},
    CachedSpatialGrid, Damage, DamageKind, DamageSource, Explosion, RadiusEffect,
};
use common_ecs::{Job, Origin, Phase, System};
use specs::{Entities, Join, LendJoin, Read, ReadStorage};
use vek::Rgb;

event_emitters! {
    struct Events[Emitters] {
        delete: DeleteEvent,
        explosion: ExplosionEvent,
        shoot: ShootEvent,
        change_body: ChangeBodyEvent,
    }
}

/// This system is responsible for handling misc object behaviours
#[derive(Default)]
pub struct Sys;
impl<'a> System<'a> for Sys {
    type SystemData = (
        Entities<'a>,
        Events<'a>,
        Read<'a, DeltaTime>,
        Read<'a, Time>,
        Read<'a, EventBus<Outcome>>,
        Read<'a, CachedSpatialGrid>,
        ReadStorage<'a, Pos>,
        ReadStorage<'a, Vel>,
        ReadStorage<'a, PhysicsState>,
        ReadStorage<'a, Object>,
        ReadStorage<'a, Body>,
        ReadStorage<'a, Teleporting>,
    );

    const NAME: &'static str = "object";
    const ORIGIN: Origin = Origin::Server;
    const PHASE: Phase = Phase::Create;

    fn run(
        _job: &mut Job<Self>,
        (
            entities,
            events,
            _dt,
            time,
            outcome_bus,
            spatial_grid,
            positions,
            velocities,
            physics_states,
            objects,
            bodies,
            teleporting,
        ): Self::SystemData,
    ) {
        let mut emitters = events.get_emitters();

        // Objects
        for (entity, pos, vel, physics, object, body) in (
            &entities,
            &positions,
            &velocities,
            physics_states.maybe(),
            &objects,
            bodies.maybe(),
        )
            .join()
        {
            match object {
                Object::Bomb { owner } => {
                    if physics.is_some_and(|physics| physics.on_surface().is_some()) {
                        emitters.emit(DeleteEvent(entity));
                        emitters.emit(ExplosionEvent {
                            pos: pos.0,
                            explosion: Explosion {
                                effects: vec![
                                    RadiusEffect::Entity(Effect::Damage(Damage {
                                        source: DamageSource::Explosion,
                                        kind: DamageKind::Energy,
                                        value: 40.0,
                                    })),
                                    RadiusEffect::Entity(Effect::Poise(-100.0)),
                                    RadiusEffect::TerrainDestruction(4.0, Rgb::black()),
                                ],
                                radius: 12.0,
                                reagent: None,
                                min_falloff: 0.75,
                            },
                            owner: *owner,
                        });
                    }
                },
                Object::SurpriseEgg { .. } => {
                    if physics.is_some_and(|physics| physics.on_surface().is_some()) {
                        emitters.emit(DeleteEvent(entity));
                        outcome_bus.emit_now(Outcome::SurpriseEgg { pos: pos.0 });
                    }
                },
                Object::Firework { owner, reagent } => {
                    if vel.0.z < 0.0 {
                        const ENABLE_RECURSIVE_FIREWORKS: bool = true;
                        if ENABLE_RECURSIVE_FIREWORKS {
                            use common::{
                                comp::{LightEmitter, Projectile},
                                util::Dir,
                            };
                            use rand::Rng;
                            use std::{f32::consts::PI, time::Duration};
                            use vek::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.
                            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
                            };
                            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
                            })();
                            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");
                                emitters.emit(ShootEvent {
                                    entity,
                                    pos: *pos,
                                    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,
                                        is_sticky: true,
                                        is_point: true,
                                    },
                                    speed,
                                    object: Some(Object::Firework {
                                        owner: *owner,
                                        reagent: *reagent,
                                    }),
                                });
                            }
                        }
                        emitters.emit(DeleteEvent(entity));
                        emitters.emit(ExplosionEvent {
                            pos: pos.0,
                            explosion: Explosion {
                                effects: vec![
                                    RadiusEffect::Entity(Effect::Damage(Damage {
                                        source: DamageSource::Explosion,
                                        kind: DamageKind::Energy,
                                        value: 5.0,
                                    })),
                                    RadiusEffect::Entity(Effect::Poise(-40.0)),
                                    RadiusEffect::TerrainDestruction(4.0, Rgb::black()),
                                ],
                                radius: 12.0,
                                reagent: Some(*reagent),
                                min_falloff: 0.0,
                            },
                            owner: *owner,
                        });
                    }
                },
                Object::DeleteAfter {
                    spawned_at,
                    timeout,
                } => {
                    if (time.0 - spawned_at.0).max(0.0) > timeout.as_secs_f64() {
                        emitters.emit(DeleteEvent(entity));
                    }
                },
                Object::Portal { .. } => {
                    let is_active = spatial_grid
                        .0
                        .in_circle_aabr(pos.0.xy(), TELEPORTER_RADIUS)
                        .any(|entity| {
                            (&positions, &teleporting)
                                .lend_join()
                                .get(entity, &entities)
                                .map_or(false, |(teleporter_pos, _)| {
                                    pos.0.distance_squared(teleporter_pos.0)
                                        <= TELEPORTER_RADIUS.powi(2)
                                })
                        });

                    if body.is_some_and(|body| {
                        (*body == Body::Object(object::Body::PortalActive)) != is_active
                    }) {
                        emitters.emit(ChangeBodyEvent {
                            entity,
                            new_body: Body::Object(if is_active {
                                outcome_bus.emit_now(Outcome::PortalActivated { pos: pos.0 });
                                object::Body::PortalActive
                            } else {
                                object::Body::Portal
                            }),
                        });
                    }
                },
            }
        }
    }
}