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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! This is the official Rust Bot SDK for [cellwars](https://cellwars.io)

use std::sync::{
    Arc,
    Mutex,
};
use std::collections::HashMap;
use std::fmt;
use std::io;
use std::io::{
    BufRead,
    Write,
    Stdin,
    Stdout,
};
use std::mem::take;
use std::str::FromStr;
use std::error::Error;

#[derive(Debug)]
struct ParseCommandError {
    details: &'static str,
}

impl ParseCommandError {
    fn new(details: &'static str) -> Self {
        Self {
            details,
        }
    }
}

impl fmt::Display for ParseCommandError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Failed to parse command: {}", self.details)
    }
}

impl Error for ParseCommandError {

}

#[derive(Debug, Eq, PartialEq)]
enum Command {
    Initialize{ width: u32, height: u32, team_id: u32, my_column: u32, enemy_column: u32 },
    Spawn{ cell_id: u32, x: u32, y: u32, health: u32, team_id: u32, age: u32 },
    Die{ cell_id: u32 },
    SetCellProperties{ cell_id: u32, x: u32, y: u32, health: u32, age: u32 },
    ConflictingActions{ x: u32, y: u32 },
    RunRound,
    EndGame,
}

#[derive(Debug, Eq, PartialEq)]
enum Action {
    Move{ cell_id: u32, x: u32, y: u32 },
    Attack{ cell_id: u32, x: u32, y: u32 },
    Explode{ cell_id: u32 },
    Initialized,
    RoundEnd,
}

impl fmt::Display for Action {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Action::Move{ cell_id, x, y } =>
                write!(f, "MOVE {} {} {}", cell_id, x, y ),
            Action::Attack{ cell_id, x, y } =>
                write!(f, "ATTACK {} {} {}", cell_id, x, y ),
            Action::Explode{ cell_id } =>
                write!(f, "EXPLODE {}", cell_id ),
            Action::Initialized => write!(f, "INITIALIZED"),
            Action::RoundEnd => write!(f, "ROUND_END"),
        }
    }
}

impl FromStr for Command {
    type Err = ParseCommandError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let tokens : Vec<&str> = s.trim().split(' ').collect();
        if tokens.len() == 0 {
            return Err(ParseCommandError::new("no tokens in command"));
        }
        let mut parameters = Vec::new();
        for token in tokens.iter().skip(1) {
            let token = match token.parse::<u32>() {
                Ok(token) => token,
                Err(_) => return Err(ParseCommandError::new("non int parameter found")),
            };
            parameters.push(token);
        }
        match (tokens[0], parameters.len()) {
            ("INITIALIZE", 5) => Ok(Command::Initialize{
                width: parameters[0],
                height: parameters[1],
                team_id: parameters[2],
                my_column: parameters[3],
                enemy_column: parameters[4],
            }),
            ("SPAWN", 6) => Ok(Command::Spawn{
                cell_id: parameters[0],
                x: parameters[1],
                y: parameters[2],
                health: parameters[3],
                team_id: parameters[4],
                age: parameters[5],
            }),
            ("DIE", 1) => Ok(Command::Die{
                cell_id: parameters[0],
            }),
            ("SET_CELL_PROPERTIES", 5) => Ok(Command::SetCellProperties{
                cell_id: parameters[0],
                x: parameters[1],
                y: parameters[2],
                health: parameters[3],
                age: parameters[4],
            }),
            ("CONFLICTING_ACTIONS", 2) => Ok(Command::ConflictingActions{
                x: parameters[0],
                y: parameters[1],
            }),
            ("RUN_ROUND", 0) => Ok(Command::RunRound),
            ("END_GAME", 0) => Ok(Command::EndGame),
            _ => Err(ParseCommandError::new("Unknown command")),
        }
    }
}

struct CommunicatorDetails {
    input: io::BufReader<Stdin>,
    output: io::BufWriter<Stdout>,
    pending_actions: Vec<Action>,
}

#[doc(hidden)]
pub struct Communicator {
    details: Mutex<CommunicatorDetails>,
}

impl Communicator {
    pub fn new(input: Stdin, output: Stdout) -> Self {
        Self {
            details: Mutex::new(CommunicatorDetails{
                input: io::BufReader::new(input),
                output: io::BufWriter::new(output),
                pending_actions: Vec::new(),
            })
        }
    }

    fn add_action(&self, action: Action) {
        self.details.lock().unwrap().pending_actions.push(action);
    }

    fn send_action(
        action: Action,
        details: &mut CommunicatorDetails,
    ) -> io::Result<()>
    {
        details.output.write_all(format!("{}\n", action).as_bytes())?;
        Ok(())
    }

    fn flush(details: &mut CommunicatorDetails) -> io::Result<()> {
        details.output.flush()?;
        Ok(())
    }

    fn flush_action(&self, action: Action) -> io::Result<()> {
        let mut details = self.details.lock().unwrap();
        Self::send_action(action, &mut details)?;
        Self::flush(&mut details)?;
        Ok(())
    }

    fn end_round(&self) -> io::Result<()> {
        let mut details = self.details.lock().unwrap();
        let mut pending_actions = take(&mut details.pending_actions);
        pending_actions.push(Action::RoundEnd);
        for action in pending_actions.into_iter() {
            Self::send_action(action, &mut details)?;
        }
        Self::flush(&mut details)?;
        Ok(())
    }

    fn read_command(&self) -> Result<Command, Box<dyn Error>> {
        let mut line = String::new();
        self.details.lock().unwrap().input.read_line(&mut line)?;
        let command: Command = line.parse()?;
        Ok(command)
    }
}

/// A position, defined by a tuple of x and y coordinates.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Position {
    x: i32,
    y: i32,
}

impl From<(i32, i32)> for Position {
    fn from((x, y): (i32, i32)) -> Position {
        Position {
            x,
            y,
        }
    }
}

impl Position {
    /// The x coordinate of this position.
    pub fn x(&self) -> i32 {
        self.x
    }

    /// The y coordinate of this position.
    pub fn y(&self) -> i32 {
        self.y
    }

    /// Translate this position by an offset.
    ///
    /// # Arguments
    ///
    /// * `x` - the x offset.
    /// * `y` - the y offset.
    pub fn translated_by_offset(&self, x: i32, y: i32) -> Position {
        Position {
            x: self.x + x,
            y: self.y + y,
        }
    }

    /// Translates this position by the given direction.
    ///
    /// # Arguments
    ///
    /// * `direction` - the direction to translate this position by.
    pub fn translated_by_direction(&self, direction: &Direction) -> Position {
        let offset = direction.as_position_offset();
        self.translated_by_offset(offset.0, offset.1)
    }

    /// The distance between this position and another one.
    ///
    /// Note that this is going to be the Manhattan distance between the two positions.
    ///
    /// # Arguments
    ///
    /// * `position` - the position to calculate the distance against.
    pub fn distance(&self, position: &Position) -> u64 {
        ((self.x - position.x).abs() + (self.y - position.y).abs()) as u64
    }
}

/// A direction.
pub enum Direction {
    /// North (up)
    North,
    /// South (down)
    South,
    /// East (right)
    East,
    /// West (left)
    West,
}

impl Direction {
    fn as_position_offset(&self) -> (i32, i32) {
        match self {
            Direction::North => (0, -1),
            Direction::South => (0, 1),
            Direction::East => (1, 0),
            Direction::West => (-1, 0),
        }
    }
}

/// A cell in the game
pub struct Cell {
    cell_id: u32,
    position: Position,
    health: u32,
    team_id: u32,
    age: u32,
    is_enemy: bool,
    communicator: Arc<Communicator>,
    world_properties: WorldProperties,
}

impl Cell {
    /// Gets an unique identifier for this cell. The identifier will not change for a given cell for the
    /// duration of each match.
    pub fn cell_id(&self) -> u32 {
        self.cell_id
    }

    /// Gets the position of this cell.
    pub fn position(&self) -> &Position {
        &self.position
    }

    /// Gets the health of this cell. This is typically a number between 1 and 100.
    pub fn health(&self) -> u32 {
        self.health
    }

    /// Gets this cell's team identifier.
    pub fn team_id(&self) -> u32 {
        self.team_id
    }

    /// Gets the cell's age. Every time a new chunk of cells spawn, all new cells will have an age of 0
    /// and all of the existing cell's ages will be incremented by 1.
    pub fn age(&self) -> u32 {
        self.age
    }

    /// Indicates if this cell belongs to the enemy. This is a shorthand for checking if the team_id is
    /// different from yours.
    pub fn is_enemy(&self) -> bool {
        self.is_enemy
    }

    fn is_in_bounds(&self, position: &Position) -> bool {
        position.x() >= 0 &&
            position.y() >= 0 &&
            position.x() < self.world_properties.width as i32 &&
            position.y() < self.world_properties.height as i32
    }

    /// Indicates if the cell can move into the given position.
    ///
    /// # Arguments
    ///
    /// * `position` - the target position.
    pub fn can_move_to_position(&self, position: &Position) -> bool {
        self.is_in_bounds(position) && self.position.distance(position) == 1
    }

    /// Indicates if the cell can move in the given direction.
    ///
    /// # Arguments
    ///
    /// * `direction` - the direction.
    pub fn can_move_in_direction(&self, direction: &Direction) -> bool {
        self.can_move_to_position(&self.position.translated_by_direction(direction))
    }

    /// Indicates if the cell can attack the given position.
    ///
    /// # Arguments
    ///
    /// * `position` - the target position.
    pub fn can_attack_position(&self, position: &Position) -> bool {
        self.is_in_bounds(position) &&
            (position.x() - self.position.x()).abs() <= 1 &&
            (position.y() - self.position.y()).abs() <= 1
    }

    /// Indicates if the cell can attack the given cell.
    ///
    /// Note that this is purely a proximity check. e.g. this does not take into account whether the cell is an enemy.
    ///
    /// # Arguments
    ///
    /// * `cell` - the target cell.
    pub fn can_attack_cell(&self, cell: &Cell) -> bool {
        self.can_attack_position(&cell.position)
    }

    /// Instructs this cell to attack the given cell.
    ///
    /// See the documentation on the restrictions on attacking too-far-away positions.
    ///
    /// # Arguments
    ///
    /// * `cell` - the cell to be attacked.
    pub fn attack_cell(&self, cell: &Cell) {
        self.attack_position(&cell.position);
    }

    /// Instructs this cell to attack the given position.
    ///
    /// See the documentation on the restrictions on attacking too-far-away positions.
    ///
    /// # Arguments
    ///
    /// * `position` - the position to be attacked.
    pub fn attack_position(&self, position: &Position) {
        if self.can_attack_position(position) {
            self.communicator.add_action(Action::Attack{
                cell_id: self.cell_id,
                x: position.x() as u32,
                y: position.y() as u32,
            });
        }
    }

    /// Instructs this cell to move into the given position.
    ///
    /// See the documentation on the restrictions on moving into too-far-away positions and movement conflicts.
    ///
    /// # Arguments
    ///
    /// * `position` - the position to move into.
    pub fn move_to_position(&self, position: &Position) {
        if self.can_move_to_position(position) {
            self.communicator.add_action(Action::Move{
                cell_id: self.cell_id,
                x: position.x() as u32,
                y: position.y() as u32,
            });
        }
    }

    /// Instructs this cell to move into the given direction.
    ///
    /// See the documentation on movement conflicts.
    ///
    /// # Arguments
    ///
    /// * `direction` - the direction to move into.
    pub fn move_in_direction(&self, direction: &Direction) {
        if self.can_move_in_direction(direction) {
            self.move_to_position(&self.position.translated_by_direction(&direction));
        }
    }

    /// Instructs this cell to explode.
    ///
    /// This will cause your cell to die and cause damage to *all* surrounding cells.
    pub fn explode(&self) {
        self.communicator.add_action(Action::Explode{cell_id: self.cell_id});
    }
}

impl fmt::Debug for Cell {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Cell(cell_id: {}, position: {:?}, team_id: {}, health: {})",
            self.cell_id,
            &self.position,
            self.team_id,
            self.health
        )
    }
}

#[derive(Default, Clone)]
struct WorldProperties {
    width: u32,
    height: u32,
    my_team_id: u32,
    my_column: u32,
    enemy_column: u32,
}

/// The state of the game's world.
#[derive(Default)]
pub struct WorldState {
    cells: HashMap<u32, Cell>,
    properties: WorldProperties,
}

impl WorldState {
    /// The width of the world, in number of squares.
    pub fn width(&self) -> u32 {
        self.properties.width
    }

    /// The height of the world, in number of squares.
    pub fn height(&self) -> u32 {
        self.properties.height
    }

    /// The identifier of your bot's team. Use this to differentiate between your cells and your enemy's.
    pub fn my_team_id(&self) -> u32 {
        self.properties.my_team_id
    }

    /// The index of the column in which your cells spawn. This will typically be either 0 or width - 1.
    pub fn my_starting_column(&self) -> u32 {
        self.properties.my_column
    }

    /// The index of the column in which your enemy's cells spawn. This will typically be either 0 or width - 1.
    pub fn enemy_starting_column(&self) -> u32 {
        self.properties.enemy_column
    }

    /// Gets all of the cells you currently control
    pub fn my_cells(&self) -> Vec<&Cell> {
        self.cells.values().filter(|cell| cell.team_id == self.my_team_id()).collect()
    }

    /// Gets all of the cells the enemy currently controls
    pub fn enemy_cells(&self) -> Vec<&Cell> {
        self.cells.values().filter(|cell| cell.team_id != self.my_team_id()).collect()
    }
}

#[doc(hidden)]
pub struct GameCoordinator {
    communicator: Arc<Communicator>,
}

impl GameCoordinator {
    pub fn new(communicator: Communicator) -> Self {
        Self {
            communicator: Arc::new(communicator),
        }
    }

    fn apply_initialize(
        width: u32,
        height: u32,
        my_team_id: u32,
        my_column: u32,
        enemy_column: u32,
    ) -> WorldState
    {
        WorldState {
            properties: WorldProperties{
                width,
                height,
                my_team_id,
                my_column,
                enemy_column,
            },
            ..Default::default()
        }
    }

    fn apply_spawn(
        &self,
        mut world_state: WorldState,
        cell_id: u32,
        x: u32,
        y: u32,
        health: u32,
        team_id: u32,
        age: u32,
    ) -> WorldState
    {
        let is_enemy = team_id != world_state.my_team_id();
        world_state.cells.insert(cell_id, Cell{
            cell_id,
            position: Position{x: x as i32, y: y as i32},
            health,
            team_id,
            age,
            is_enemy,
            communicator: self.communicator.clone(),
            world_properties: world_state.properties.clone(),
        });
        world_state
    }

    fn apply_set_cell_properties(
        mut world_state: WorldState,
        cell_id: u32,
        x: u32,
        y: u32,
        health: u32,
        age: u32,
    ) -> WorldState
    {
        let cell = world_state.cells.get_mut(&cell_id).expect("Invalid cell id");
        cell.position = Position{x: x as i32, y: y as i32};
        cell.health = health;
        cell.age = age;
        world_state
    }

    fn apply_die(mut world_state: WorldState, cell_id: u32) -> WorldState {
        world_state.cells.remove(&cell_id);
        world_state
    }

    fn advertise_initialization(&self) -> io::Result<()> {
        self.communicator.flush_action(Action::Initialized)?;
        Ok(())
    }

    fn apply_command(
        &self,
        command: Command,
        world_state: WorldState,
    ) -> WorldState
    {
        let world_state = match command {
            Command::Initialize{ width, height, team_id, my_column, enemy_column } =>
                Self::apply_initialize(width, height, team_id, my_column, enemy_column),
            Command::Spawn{ cell_id, x, y, health, team_id, age} =>
                self.apply_spawn(world_state, cell_id, x, y, health, team_id, age),
            Command::Die{ cell_id } =>
                Self::apply_die(world_state, cell_id),
            Command::SetCellProperties{ cell_id, x, y, health, age } =>
                Self::apply_set_cell_properties(world_state, cell_id, x, y, health, age),
            _ => world_state,
        };
        world_state
    }

    pub fn run_loop<B>(&self, mut bot: B) -> Result<(), Box<dyn Error>>
    where
        B: UserBot,
    {
        let mut world_state = WorldState::default();
        self.advertise_initialization()?;
        loop {
            let mut command = self.communicator.read_command()?;
            if command == Command::EndGame {
                break;
            }
            while command != Command::RunRound {
                world_state = self.apply_command(command, world_state);
                command = self.communicator.read_command()?;
            }
            bot.run_round(&world_state);
            self.communicator.end_round()?;
        }
        Ok(())
    }
}

/// A bot defined by the user
///
/// This trait should be implemented by users and should contain all of their bot's logic
pub trait UserBot {
    /// Run a particular round of the game.
    ///
    /// The implementation of this method must guarantee that at most one action is emitted per
    /// cell.
    ///
    /// # Arguments
    ///
    /// * `world_state` - the current state of the world
    fn run_round(&mut self, world_state: &WorldState);
}