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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use common::{
    combat::{self, AttackOptions, AttackSource, AttackerInfo, TargetInfo},
    comp::{
        ability::Dodgeable,
        agent::{Sound, SoundKind},
        aura::EnteredAuras,
        melee::MultiTarget,
        Alignment, Body, Buffs, CharacterState, Combo, Energy, Group, Health, Inventory, Mass,
        Melee, Ori, PhysicsState, Player, Pos, Scale, Stats,
    },
    event::{self, EmitExt, EventBus},
    event_emitters,
    outcome::Outcome,
    resources::Time,
    terrain::TerrainGrid,
    uid::{IdMaps, Uid},
    util::{find_dist::Cylinder, Dir},
    vol::ReadVol,
    GroupTarget,
};
use common_ecs::{Job, Origin, Phase, System};
use itertools::Itertools;
use specs::{
    shred, Entities, Join, LendJoin, Read, ReadExpect, ReadStorage, SystemData, WriteStorage,
};
use vek::*;

event_emitters! {
    struct ReadAttackEvents[AttackEmitters] {
        health_change: event::HealthChangeEvent,
        energy_change: event::EnergyChangeEvent,
        poise_change: event::PoiseChangeEvent,
        sound: event::SoundEvent,
        mine_block: event::MineBlockEvent,
        parry_hook: event::ParryHookEvent,
        kockback: event::KnockbackEvent,
        entity_attack_hook: event::EntityAttackedHookEvent,
        combo_change: event::ComboChangeEvent,
        buff: event::BuffEvent,
    }
}

#[derive(SystemData)]
pub struct ReadData<'a> {
    time: Read<'a, Time>,
    terrain: ReadExpect<'a, TerrainGrid>,
    id_maps: Read<'a, IdMaps>,
    entities: Entities<'a>,
    players: ReadStorage<'a, Player>,
    uids: ReadStorage<'a, Uid>,
    positions: ReadStorage<'a, Pos>,
    orientations: ReadStorage<'a, Ori>,
    alignments: ReadStorage<'a, Alignment>,
    scales: ReadStorage<'a, Scale>,
    bodies: ReadStorage<'a, Body>,
    healths: ReadStorage<'a, Health>,
    energies: ReadStorage<'a, Energy>,
    inventories: ReadStorage<'a, Inventory>,
    groups: ReadStorage<'a, Group>,
    char_states: ReadStorage<'a, CharacterState>,
    physic_states: ReadStorage<'a, PhysicsState>,
    stats: ReadStorage<'a, Stats>,
    combos: ReadStorage<'a, Combo>,
    buffs: ReadStorage<'a, Buffs>,
    entered_auras: ReadStorage<'a, EnteredAuras>,
    events: ReadAttackEvents<'a>,
    masses: ReadStorage<'a, Mass>,
}

/// This system is responsible for handling accepted inputs like moving or
/// attacking
#[derive(Default)]
pub struct Sys;

impl<'a> System<'a> for Sys {
    type SystemData = (
        ReadData<'a>,
        WriteStorage<'a, Melee>,
        Read<'a, EventBus<Outcome>>,
    );

    const NAME: &'static str = "melee";
    const ORIGIN: Origin = Origin::Common;
    const PHASE: Phase = Phase::Create;

    fn run(_job: &mut Job<Self>, (read_data, mut melee_attacks, outcomes): Self::SystemData) {
        let mut emitters = read_data.events.get_emitters();
        let mut outcomes_emitter = outcomes.emitter();
        let mut rng = rand::thread_rng();

        // Attacks
        for (attacker, uid, pos, ori, melee_attack, body, scale) in (
            &read_data.entities,
            &read_data.uids,
            &read_data.positions,
            &read_data.orientations,
            &mut melee_attacks,
            &read_data.bodies,
            read_data.scales.maybe(),
        )
            .join()
        {
            if melee_attack.applied {
                continue;
            }
            emitters.emit(event::SoundEvent {
                sound: Sound::new(SoundKind::Melee, pos.0, 2.0, read_data.time.0),
            });
            melee_attack.applied = true;

            // Scales
            let eye_pos = pos.0 + Vec3::unit_z() * body.eye_height(scale.map_or(1.0, |s| s.0));
            let scale = read_data.scales.get(attacker).map_or(1.0, |s| s.0);
            let height = body.height() * scale;
            // TODO: use Capsule Prisms instead of Cylinders
            let rad = body.max_radius() * scale;

            let melee_z = pos.0.z + 0.5 * body.height();
            let melee_z_range = (melee_z - melee_attack.range)..(melee_z + melee_attack.range);

            // Mine blocks broken by the attack
            if let Some((block_pos, tool)) = melee_attack.break_block {
                // Check distance to block
                if eye_pos.distance_squared(block_pos.map(|e| e as f32 + 0.5))
                    < (rad + scale * melee_attack.range).powi(2)
                {
                    emitters.emit(event::MineBlockEvent {
                        entity: attacker,
                        pos: block_pos,
                        tool,
                    });
                }
            }

            // Go through all other entities
            for (target, pos_b, health_b, body_b, uid_b) in (
                &read_data.entities,
                &read_data.positions,
                &read_data.healths,
                &read_data.bodies,
                &read_data.uids,
            )
                .join()
                .sorted_by_key(|(_, pos_b, _, _, _)| pos_b.0.distance_squared(pos.0) as u32)
            {
                // Unless the melee attack can hit multiple targets, stop the attack if it has
                // already hit 1 target
                if melee_attack.multi_target.is_none() && melee_attack.hit_count > 0 {
                    break;
                }

                let look_dir = *ori.look_dir();

                // 2D versions
                let pos2 = Vec2::from(pos.0);
                let pos_b2 = Vec2::<f32>::from(pos_b.0);
                let ori2 = Vec2::from(look_dir);

                // Scales
                let scale_b = read_data.scales.get(target).map_or(1.0, |s| s.0);
                let height_b = body_b.height() * scale_b;
                let rad_b = body_b.max_radius() * scale_b;

                // Check if entity is dodging
                let target_dodging = match melee_attack.dodgeable {
                    Dodgeable::Roll => read_data
                        .char_states
                        .get(target)
                        .and_then(|cs| cs.roll_attack_immunities())
                        .map_or(false, |i| i.melee),
                    Dodgeable::Jump => read_data
                        .physic_states
                        .get(target)
                        .map_or(false, |ps| ps.on_ground.is_none()),
                    Dodgeable::No => false,
                };

                // Check if it is a hit
                let hit = attacker != target
                    && !health_b.is_dead
                    // Spherical wedge shaped attack field
                    && pos.0.distance_squared(pos_b.0) < (rad + rad_b + scale * melee_attack.range).powi(2)
                    && (melee_z_range.contains(&pos_b.0.z) || melee_z_range.contains(&(pos_b.0.z + body_b.height())) || (pos_b.0.z..(pos_b.0.z + body_b.height())).contains(&melee_z))
                    && ori2.angle_between(pos_b2 - pos2) < melee_attack.max_angle + (rad_b / pos2.distance(pos_b2)).atan();

                //Check if target is behind a wall
                let attacker_cylinder = Cylinder {
                    center: pos.0 + (0.5 * height * Vec3::unit_z()),
                    radius: rad,
                    height,
                };
                let target_cylinder = Cylinder {
                    center: pos_b.0 + (0.5 * height_b * Vec3::unit_z()),
                    radius: rad_b,
                    height: height_b,
                };
                let hit = hit
                    && !is_blocked_by_wall(&read_data.terrain, attacker_cylinder, target_cylinder);

                if hit {
                    let allow_friendly_fire =
                        combat::allow_friendly_fire(&read_data.entered_auras, attacker, target);
                    // See if entities are in the same group
                    let same_group = read_data
                        .groups
                        .get(attacker)
                        .map(|group_a| Some(group_a) == read_data.groups.get(target))
                        .unwrap_or(false);

                    let target_group = if same_group {
                        GroupTarget::InGroup
                    } else {
                        GroupTarget::OutOfGroup
                    };

                    let dir = Dir::new((pos_b.0 - pos.0).try_normalized().unwrap_or(look_dir));

                    let attacker_info = Some(AttackerInfo {
                        entity: attacker,
                        uid: *uid,
                        group: read_data.groups.get(attacker),
                        energy: read_data.energies.get(attacker),
                        combo: read_data.combos.get(attacker),
                        inventory: read_data.inventories.get(attacker),
                        stats: read_data.stats.get(attacker),
                        mass: read_data.masses.get(attacker),
                    });

                    let target_ori = read_data.orientations.get(target);
                    let target_char_state = read_data.char_states.get(target);
                    let target_info = TargetInfo {
                        entity: target,
                        uid: *uid_b,
                        inventory: read_data.inventories.get(target),
                        stats: read_data.stats.get(target),
                        health: read_data.healths.get(target),
                        pos: pos_b.0,
                        ori: target_ori,
                        char_state: target_char_state,
                        energy: read_data.energies.get(target),
                        buffs: read_data.buffs.get(target),
                        mass: read_data.masses.get(target),
                    };

                    // PvP check
                    let permit_pvp = combat::permit_pvp(
                        &read_data.alignments,
                        &read_data.players,
                        &read_data.entered_auras,
                        &read_data.id_maps,
                        Some(attacker),
                        target,
                    );

                    // Note: Don't use ori.look_vec() here, it leads to incorrect results for wide
                    // angle melee attacks
                    let precision_from_flank = combat::precision_mult_from_flank(
                        (pos_b.0 - pos.0)
                            .with_z(0.0)
                            .try_normalized()
                            .unwrap_or(ori.look_vec()),
                        target_ori,
                        melee_attack.precision_flank_multipliers,
                        melee_attack.precision_flank_invert,
                    );

                    let precision_from_poise = {
                        if let Some(CharacterState::Stunned(data)) = target_char_state {
                            Some(
                                combat::MAX_MELEE_POISE_PRECISION
                                    * data.static_data.poise_state.damage_multiplier(),
                            )
                        } else {
                            None
                        }
                    };

                    let precision_mult = match (precision_from_flank, precision_from_poise) {
                        (Some(a), Some(b)) => Some(a.max(b)),
                        (Some(a), None) | (None, Some(a)) => Some(a),
                        (None, None) => None,
                    };

                    let attack_options = AttackOptions {
                        target_dodging,
                        permit_pvp,
                        allow_friendly_fire,
                        target_group,
                        precision_mult,
                    };

                    let strength =
                        if let Some(MultiTarget::Scaling(scaling)) = melee_attack.multi_target {
                            1.0 + melee_attack.hit_count as f32 * scaling
                        } else {
                            1.0
                        };

                    let mut is_applied = false;
                    for offset in 0..melee_attack.simultaneous_hits {
                        is_applied = melee_attack.attack.apply_attack(
                            attacker_info,
                            &target_info,
                            dir,
                            attack_options,
                            strength,
                            AttackSource::Melee,
                            *read_data.time,
                            &mut emitters,
                            |o| outcomes_emitter.emit(o),
                            &mut rng,
                            offset as u64,
                        ) || is_applied;
                    }

                    if is_applied {
                        melee_attack.hit_count += melee_attack.simultaneous_hits;
                    }
                }
            }
        }
    }
}

// Cast rays from the corners of an Axis Aligned Box centered at the attacker to
// one centered at the target. We use multiple rays to ensure that target is at
// least almost completly behind a wall.
fn is_blocked_by_wall(terrain: &TerrainGrid, attacker: Cylinder, target: Cylinder) -> bool {
    let attacker_v = Vec3::new(
        attacker.radius / f32::sqrt(2.),
        attacker.radius / f32::sqrt(2.),
        attacker.height / 2.0,
    );
    let attacker_aabb = Aabb {
        min: attacker.center - attacker_v,
        max: attacker.center + attacker_v,
    };

    let target_v = Vec3::new(
        target.radius / f32::sqrt(2.),
        target.radius / f32::sqrt(2.),
        target.height / 2.0,
    );
    let target_aabb = Aabb {
        min: target.center - target_v,
        max: target.center + target_v,
    };

    let mut segments = Vec::with_capacity(9);
    segments.push(LineSegment3 {
        start: attacker.center,
        end: target.center,
    });
    for i in 0..2 {
        for j in 0..2 {
            for l in 0..2 {
                let (x1, x2) = if i == 0 {
                    (attacker_aabb.min.x, target_aabb.min.x)
                } else {
                    (attacker_aabb.max.x, target_aabb.max.x)
                };

                let (y1, y2) = if j == 0 {
                    (attacker_aabb.min.y, target_aabb.min.y)
                } else {
                    (attacker_aabb.max.y, target_aabb.max.y)
                };

                let (z1, z2) = if l == 0 {
                    (attacker_aabb.min.z, target_aabb.min.z)
                } else {
                    (attacker_aabb.max.z, target_aabb.max.z)
                };

                segments.push(LineSegment3 {
                    start: Vec3::new(x1, y1, z1),
                    end: Vec3::new(x2, y2, z2),
                });
            }
        }
    }

    for &segment in segments.iter() {
        let ray_dist = terrain
            .ray(segment.start, segment.end)
            .until(|b| b.is_filled())
            .cast()
            .0;
        if let Some(ray_direction) = (segment.end - segment.start).try_normalized() {
            let ray_end = segment.start + ray_dist * ray_direction;
            let ray = LineSegment3 {
                start: segment.start,
                end: ray_end,
            };
            let proj = ray.projected_point(target_aabb.center());

            if target_aabb.contains_point(proj) {
                return false;
            }
        } else {
            return false;
        }
    }
    true
}