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
use std::collections::HashSet;

#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate eventsourcing_derive;
pub extern crate eventsourcing;

pub use radar::RadarPing;

pub mod commands;
pub mod events;
pub mod leaderboard;
mod radar;
pub mod state;

pub(crate) const DOMAIN_VERSION: &str = "1.0";

const DEFAULT_BOARD_HEIGHT: u32 = 100;
const DEFAULT_BOARD_WIDTH: u32 = 100;

/// The primary accumulator register
pub const EAX: u32 = 0;
/// The count register
pub const ECX: u32 = 1;
/// The base register
pub const EBX: u32 = 2;

/// A 2-dimensional coordinate within an arena
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Point {
    pub x: i32,
    pub y: i32,
}

impl Point {
    pub fn new(x: i32, y: i32) -> Point {
        Point { x, y }
    }

    pub fn bearing(&self, target: &Point) -> GridDirection {
        let dx = (target.x - self.x) as f64;
        let dy = (target.y - self.y) as f64;
        let mut angle = 90.0 - dy.atan2(dx).to_degrees();

        if angle < 0.0 {
            angle = angle + 360.0;
        }

        println!("Angle: {}", angle);
        let idx = angle.trunc() as usize / 45;
        ALL_DIRECTIONS[idx]
    }

    pub fn adjacent_points(&self, board: &GameBoard) -> Vec<Point> {
        let mut points = Vec::new();
        for direction in &ALL_DIRECTIONS {
            if let Some(target) = self.relative_point(board, direction, 1) {
                points.push(target)
            }
        }
        points
    }

    pub fn gather_points(
        &self,
        board: &GameBoard,
        direction: &GridDirection,
        count: usize,
    ) -> Vec<(Point, usize)> {
        let mut points = Vec::new();
        let mut p = Self::relative_point(&self, board, direction, 1);
        for i in 0..count {
            match p {
                Some(point) => {
                    points.push((point.clone(), i + 1));
                    p = Self::relative_point(&point, board, direction, 1);
                }
                None => break,
            }
        }
        points
    }

    /// Returns a point 1 unit away in the direction indicated. Grid origin is the most Southwest point
    pub fn relative_point(
        &self,
        board: &GameBoard,
        direction: &GridDirection,
        length: i32,
    ) -> Option<Point> {
        let destination = match direction {
            GridDirection::North => Point {
                x: self.x,
                y: self.y + length,
            },
            GridDirection::NorthEast => Point {
                x: self.x + length,
                y: self.y + length,
            },
            GridDirection::East => Point {
                x: self.x + length,
                y: self.y,
            },
            GridDirection::SouthEast => Point {
                x: self.x + length,
                y: self.y - length,
            },
            GridDirection::South => Point {
                x: self.x,
                y: self.y - length,
            },
            GridDirection::SouthWest => Point {
                x: self.x - length,
                y: self.y - length,
            },
            GridDirection::West => Point {
                x: self.x - length,
                y: self.y,
            },
            GridDirection::NorthWest => Point {
                x: self.x - length,
                y: self.y + length,
            },
        };
        if !destination.is_on_board(board) {
            None
        } else {
            Some(destination)
        }
    }

    pub fn is_on_board(&self, board: &GameBoard) -> bool {
        self.x <= board.width as _ && self.y <= board.height as _ && self.x >= 0 && self.y >= 0
    }
}

/// Represents the dimensions and other metadata for a game board. All game boards
/// have an origin of (0,0) that starts in the bottom left (southwest) corner of
/// the scene.
#[derive(Debug, Clone, Serialize, Deserialize, Copy)]
pub struct GameBoard {
    pub width: u32,
    pub height: u32,
}

impl Default for GameBoard {
    fn default() -> Self {
        GameBoard {
            width: DEFAULT_BOARD_WIDTH,
            height: DEFAULT_BOARD_HEIGHT,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum WeaponType {
    Primary = 0,
    Secondary = 1,
}

/// Represents a value contained within a register. Note that not all registers
/// support all value types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RegisterValue {
    Number(u64),
    Text(String),
}

/// An operation performed on a register
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RegisterOperation {
    Accumulate(u64),
    Decrement(u64),
    Set(RegisterValue),
}

/// Represents all possible directions at the game engine resolution
#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq)]
pub enum GridDirection {
    North = 0,
    NorthEast = 1,
    East = 2,
    SouthEast = 3,
    South = 4,
    SouthWest = 5,
    West = 6,
    NorthWest = 7,
}

static ALL_DIRECTIONS: [GridDirection; 8] = [
    GridDirection::North,
    GridDirection::NorthEast,
    GridDirection::East,
    GridDirection::SouthEast,
    GridDirection::South,
    GridDirection::SouthWest,
    GridDirection::West,
    GridDirection::NorthWest,
];

/// The set of initial parameters for a match
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MatchParameters {
    pub match_id: String,
    pub width: u32,
    pub height: u32,
    pub actors: Vec<String>,
    pub max_turns: u32,
    pub aps_per_turn: u32,
}

impl MatchParameters {
    pub fn new(
        match_id: String,
        width: u32,
        height: u32,
        max_turns: u32,
        aps_per_turn: u32,
        actors: Vec<String>,
    ) -> Self {
        MatchParameters {
            match_id,
            width,
            height,
            actors,
            max_turns,
            aps_per_turn,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TurnStatus {
    pub current: u32,
    pub taken: HashSet<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DamageSource {
    Wall,
    MechWeapon(String),
    MechCollision(String),
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn gather_points() {
        let board = GameBoard::default();
        let origin = Point::new(6, 6);

        let points = origin
            .gather_points(&board, &GridDirection::NorthEast, 6)
            .into_iter()
            .map(|(p, _d)| p)
            .collect::<Vec<_>>();
        assert_eq!(
            points,
            vec![
                Point::new(7, 7),
                Point::new(8, 8),
                Point::new(9, 9),
                Point::new(10, 10),
                Point::new(11, 11),
                Point::new(12, 12)
            ]
        );

        let truncated_points = origin
            .gather_points(&board, &GridDirection::SouthWest, 20)
            .into_iter()
            .map(|(p, _d)| p)
            .collect::<Vec<_>>();
        assert_eq!(
            truncated_points,
            vec![
                Point::new(5, 5),
                Point::new(4, 4),
                Point::new(3, 3),
                Point::new(2, 2),
                Point::new(1, 1),
                Point::new(0, 0),
            ]
        );
    }

    #[test]
    fn adjacent_points() {
        let board = GameBoard::default();
        let origin = Point::new(10, 5);
        let points = origin.adjacent_points(&board);
        assert_eq!(
            points,
            vec![
                Point::new(10, 6), // North
                Point::new(11, 6),
                Point::new(11, 5), // East
                Point::new(11, 4),
                Point::new(10, 4), // South
                Point::new(9, 4),
                Point::new(9, 5), // West
                Point::new(9, 6),
            ]
        )
    }

    #[test]
    fn compute_bearing() {
        // this should be a 45 degree bearing, or NorthEast
        let me = Point::new(0, 0);
        let them = Point::new(5, 5);
        assert_eq!(me.bearing(&them), GridDirection::NorthEast);

        assert_eq!(
            Point::new(0, 0).bearing(&Point::new(5, 0)),
            GridDirection::East
        );
        assert_eq!(
            Point::new(0, 0).bearing(&Point::new(0, -5)),
            GridDirection::South
        );
        assert_eq!(
            Point::new(1, 1).bearing(&Point::new(-1, -1)),
            GridDirection::SouthWest
        );
        assert_eq!(
            Point::new(5, 10).bearing(&Point::new(1, 6)),
            GridDirection::SouthWest
        );
        assert_eq!(
            Point::new(0, 0).bearing(&Point::new(0, 5)),
            GridDirection::North
        );
        assert_eq!(
            Point::new(6, 8).bearing(&Point::new(10, 4)),
            GridDirection::SouthEast
        );
        assert_eq!(
            Point::new(9, 11).bearing(&Point::new(4, 11)),
            GridDirection::West
        );
    }
}