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
use glam::Vec2;
use thiserror::Error;

use crate::{
    content::{MapContent, MapContentValidationError, Object},
    hash::{MapHash, MapHasher},
    meta::{MapMetadata, MapMetadataValidationError},
    placement::Placement,
};

pub struct Map {
    metadata: MapMetadata,
    content: MapContent,
}

impl Map {
    /// Creates a new empty map (i.e. with no objects place on it).
    pub fn empty(metadata: MapMetadata) -> Self {
        Self::new(metadata, MapContent::empty())
    }

    pub(crate) fn new(metadata: MapMetadata, content: MapContent) -> Self {
        Self { metadata, content }
    }

    /// Compute deterministic hash of the map.
    pub fn compute_hash(&self) -> MapHash {
        let mut hasher = MapHasher::new();
        self.metadata.update_hash(&mut hasher);
        self.content.update_hash(&mut hasher);
        hasher.finalize()
    }

    pub fn metadata(&self) -> &MapMetadata {
        &self.metadata
    }

    pub fn content(&self) -> &MapContent {
        &self.content
    }

    /// Insert an object to the map.
    ///
    /// # Panics
    ///
    /// Panics if the object is placed out of the map bounds, has an invalid
    /// player or is otherwise invalid.
    pub fn insert_object(&mut self, object: Object) {
        object
            .validate(self.metadata.bounds(), self.metadata.max_player())
            .unwrap();
        self.content.insert_object(object);
    }

    /// Creates a new placement on the map.
    ///
    /// # Arguments
    ///
    /// * `position` - (x, y) coordinates of the object relative to (0, 0) (as
    ///   opposed to map bounds origin).
    ///
    /// * `heading` - (counter clockwise) rotation in radians of the object
    ///   around y axis (facing upwards).
    ///
    /// # Panics
    ///
    /// Panics if position is out of bounds of the map or if heading is not a
    /// number between 0 (inclusive) and 2π (exclusive).
    pub fn new_placement(&self, position: Vec2, heading: f32) -> Placement {
        let placement = Placement::new(position, heading);
        placement.validate(self.metadata.bounds()).unwrap();
        placement
    }

    pub(crate) fn validate(&self) -> Result<(), MapValidationError> {
        if let Err(error) = self.metadata.validate() {
            return Err(MapValidationError::Metadata { source: error });
        }
        if let Err(error) = self.content.validate(&self.metadata) {
            return Err(MapValidationError::Content { source: error });
        }
        Ok(())
    }
}

#[derive(Error, Debug)]
pub enum MapValidationError {
    #[error("invalid map metadata")]
    Metadata { source: MapMetadataValidationError },
    #[error("invalid map content")]
    Content { source: MapContentValidationError },
}

#[cfg(test)]
mod test {
    use std::error::Error;

    use de_types::{
        objects::{ActiveObjectType, InactiveObjectType, UnitType},
        player::Player,
    };
    use glam::Vec2;

    use super::*;
    use crate::{
        content::{ActiveObject, InactiveObject, InnerObject},
        placement::Placement,
        size::MapBounds,
    };

    #[test]
    fn test_map() {
        let mut map = Map::empty(MapMetadata::new(
            "Test Map".into(),
            MapBounds::new(Vec2::new(1000., 1000.)),
            Player::Player3,
        ));
        let object_a = Object::new(
            map.new_placement(Vec2::new(20., 25.), 0.),
            InnerObject::Active(ActiveObject::new(
                ActiveObjectType::Unit(UnitType::Attacker),
                Player::Player1,
            )),
        );
        map.insert_object(object_a);

        map.validate().unwrap();
        assert_eq!(
            map.metadata().bounds(),
            MapBounds::new(Vec2::new(1000., 1000.))
        );
        assert_eq!(map.metadata().max_player(), Player::Player3);
    }

    #[test]
    fn test_map_validation() {
        let mut content = MapContent::empty();
        content.insert_object(Object::new(
            Placement::new(Vec2::new(1., 1.), 0.),
            InnerObject::Active(ActiveObject::new(
                ActiveObjectType::Unit(UnitType::Attacker),
                Player::Player2,
            )),
        ));
        content.insert_object(Object::new(
            Placement::new(Vec2::new(100., 0.), 0.),
            InnerObject::Active(ActiveObject::new(
                ActiveObjectType::Unit(UnitType::Attacker),
                Player::Player1,
            )),
        ));

        let map = Map::new(
            MapMetadata::new(
                "Test Map".into(),
                MapBounds::new(Vec2::new(5., 5.)),
                Player::Player4,
            ),
            content,
        );

        let result = map.validate();
        match result {
            Ok(()) => unreachable!(),
            Err(error) => {
                let mut chain = Vec::new();

                let mut error: Option<&(dyn Error)> = Some(&error);
                while let Some(inner) = error {
                    chain.push(format!("{inner}"));
                    error = inner.source();
                }

                assert_eq!(chain.len(), 4);
                assert_eq!(chain[0], "invalid map content");
                assert_eq!(chain[1], "invalid objects[1]");
                assert_eq!(chain[2], "invalid object placement");
                assert_eq!(chain[3], "position (100, 0) is out of map bounds");
            }
        }
    }

    #[test]
    fn test_map_hash() {
        let mut map = Map::empty(MapMetadata::new(
            "Test Map".into(),
            MapBounds::new(Vec2::new(1000., 1000.)),
            Player::Player3,
        ));
        let object_a = Object::new(
            map.new_placement(Vec2::new(20., 25.), 0.),
            InnerObject::Active(ActiveObject::new(
                ActiveObjectType::Unit(UnitType::Attacker),
                Player::Player1,
            )),
        );
        let object_b = Object::new(
            map.new_placement(Vec2::new(40.1, 25.), 0.),
            InnerObject::Inactive(InactiveObject::new(InactiveObjectType::Tree)),
        );

        map.insert_object(object_a);
        let hash_a = map.compute_hash();
        map.insert_object(object_b);
        let hash_b = map.compute_hash();

        assert_eq!(
            hash_a,
            MapHash::from_hex("f06b6879c4dfe01324de5e91cdf17ab7c33a8e3a9acc936f439e5013da73713c")
                .unwrap()
        );
        assert_ne!(hash_a, hash_b);
    }
}