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
use crate::{
    state::{MatchState, MechState},
    GridDirection, Point,
};

const RADAR_GRID: [(GridDirection, i32); 8] = [
    (GridDirection::NorthWest, 3),
    (GridDirection::North, 4),
    (GridDirection::NorthEast, 3),
    (GridDirection::West, 4),
    (GridDirection::East, 4),
    (GridDirection::SouthWest, 3),
    (GridDirection::South, 4),
    (GridDirection::SouthEast, 3),
];

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RadarPing {
    pub name: String,
    pub avatar: String,
    pub foe: bool,
    pub location: Point,
    pub distance: usize,
}

pub(crate) fn collect_targets(state: &MatchState, origin: &Point) -> Vec<(MechState, usize)> {
    RADAR_GRID
        .iter()
        .flat_map(|(dir, length)| origin.gather_points(&state.game_board, dir, *length as usize))
        .filter_map(|(p, d)| MatchState::mech_at(state, &p).map(|m| (m, d)))
        .collect::<Vec<_>>()
}

pub(crate) fn radar_ping(
    state: &MatchState,
    origin: &Point,
    scanning_team: &str,
) -> Vec<RadarPing> {
    collect_targets(state, origin)
        .into_iter()
        .map(|(t, d)| mech_to_ping(t, scanning_team, d))
        .collect()
}

pub(crate) fn mech_to_ping(mech: MechState, scanning_team: &str, distance: usize) -> RadarPing {
    RadarPing {
        name: mech.name,
        avatar: mech.avatar,
        location: mech.position,
        distance,
        foe: mech.team.to_lowercase() != scanning_team.to_lowercase(),
    }
}