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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//!
//! A text-based user interface implemented for Unix terminals.
//!
//! The abstract game logic is implemented in `game::Game`. This user interface references that
//! game engine but is otherwise independent in realizing a user experience around the game.

use std::{
    io::{Stdout,Write,stdout},
    rc::Rc,
    sync::mpsc::{
        channel,
        Receiver,
        Sender,
    },
    thread,
};

use crossterm::{
    cursor::{
        Hide,
        MoveTo,
    },
    event::{
        Event,
        KeyEvent,
        KeyCode,
        read as read_event,
    },
    queue,
    style::{
        Attribute,
        Print,
        SetAttribute,
        SetBackgroundColor,
    },
    terminal::{
        Clear,
        ClearType,
        EnterAlternateScreen,
        LeaveAlternateScreen,
        enable_raw_mode,
        disable_raw_mode,
        size as terminal_size,
    },
};

use crate::{
    color::{Colors,Palette},
    conf::{
        self,
        HEADER_HEIGHT,
    },
    game::{
        Game,
        combat::{CombatCapable,CombatOutcome,CombatParticipant},
        move_::{
            Move,
            ProposedMove,
        },
        obs::LocatedObs,
    },
    log::{LogTarget,Message,MessageSource},
    util::{Dims,Rect,Location,sleep_millis}
};

use self::{
    audio::{
        play_sounds,
        Sounds,
    },
    buf::RectBuffer,
    sym::Sym,
};

pub fn run(mut game: Game, use_alt_screen: bool, palette: Palette, unicode: bool, quiet: bool,
    confirm_turn_end: bool
) -> Result<(),String> {
    // {//This is here so screen drops completely when the game ends. That lets us print a farewell message to a clean console.

        

    //     let _alt_screen_maybe: Option<AlternateScreen> = if use_alt_screen {
    //         Some(
    //             AlternateScreen::to_alternate(true)
    //             .map_err(|err| format!("Error obtaining alternate screen in raw mode: {}", err))?
    //         )
    //     } else {
    //         None
    //     };

    // The input thread
    let (input_thread_tx, input_thread_rx) = channel();
    let _input_thread_handle = thread::Builder::new().name("input".to_string()).spawn(move || {
        // let _raw = RawScreen::into_raw_mode().unwrap();
        enable_raw_mode().unwrap();

        // let input = input();
        // let reader = input.read_sync();
        // for input_event in reader {
        //     match input_event {
        //         InputEvent::Keyboard(key_event) => {
        //             let will_return = key_event==KeyCode::Char(conf::KEY_QUIT);
        //             input_thread_tx.send(key_event).unwrap();

        //             if will_return {
        //                 // It's important to kill this thread upon quitting so the RawMode gets cleaned up promptly
        //                 return;
        //             }
        //         }
        //         InputEvent::Mouse(_mouse_event) => {
        //             // do nothing
        //         },
        //         InputEvent::Unsupported(_data) => {
        //             // do nothing
        //         },
        //         InputEvent::Unknown => {
        //             // do nothing
        //         }
        //     }
        // }

        loop {
            match read_event() {
                Ok(event) => {
                    match event {
                        Event::Key(key_event) => {
                            let will_return = key_event.code==KeyCode::Char(conf::KEY_QUIT);
                            input_thread_tx.send(key_event).unwrap();
        
                            if will_return {
                                disable_raw_mode().unwrap();
                                return;
                            }
                        },
                        Event::Mouse(_mouse_event) => {},
                        Event::Resize(_columns, _rows) => {
                            //TODO Handle resize events
                        },
                    }
                },
                Err(err) => {
                    eprintln!("Error reading event: {}", err);
                    break;
                },
            }

        }

        disable_raw_mode().unwrap();
    });

    // The audio thread (if applicable)
    let (audio_thread_handle, audio_thread_tx) = if !quiet {
        let (tx, rx) = channel();
        let handle = thread::Builder::new().name("audio".to_string()).spawn(move || {
            play_sounds(rx, Sounds::Silence).unwrap();
        });
        (Some(handle), Some(tx))
    } else {
        (None, None)
    };

    let mut stdout = stdout();

    if use_alt_screen {
        queue!(stdout, EnterAlternateScreen).unwrap();
    }

    let (width, height) = terminal_size().map_err(|error_kind| format!("{}", error_kind))?;
    let term_dims = Dims { width, height };

    let mut ui = TermUI::new(
        game.dims(),
        term_dims,
        stdout,
        palette,
        unicode,
        confirm_turn_end,
        audio_thread_tx,
        input_thread_rx,
    );

    let mut prev_mode: Option<Mode> = None;
    let mut mode = self::mode::Mode::TurnStart;

    while mode.run(&mut game, &mut ui, &mut prev_mode) {
        // nothing here
    }

    if use_alt_screen {
        queue!(ui.stdout, LeaveAlternateScreen).unwrap();
    }

    if audio_thread_handle.is_some() {
        ui.audio_thread_tx.unwrap().send(Sounds::Silence).unwrap();
    }

    println!("\n\n\tHe rules a moment: Chaos umpire sits,
\tAnd by decision more embroils the fray
\tBy which he reigns: next him, high arbiter,
\tChance governs all.

\t\t\t\tParadise Lost (2.907-910)\n"
    );

    Ok(())
}

pub trait MoveAnimator {
    #[deprecated = "Use `animate_proposed_move` instead. We want to animate based on the proposal and then actually take the action defined by the move so the game state doesn't reflect the move yet, since it's easier to work relative to the prior game state."]
    fn animate_move(&mut self, game: &Game, move_result: &Move);
    fn animate_proposed_move(&mut self, game: &mut Game, proposed_move: &ProposedMove);
}

pub trait UI : LogTarget + MoveAnimator {

}

pub struct DefaultUI;

impl LogTarget for DefaultUI {
    fn log_message<T>(&mut self, _message: T) where Message:From<T> {
        // do nothing
    }

    fn replace_message<T>(&mut self, _message: T) where Message:From<T> {
        // do nothing
    }
}

impl MoveAnimator for DefaultUI {
    fn animate_move(&mut self, _game: &Game, _move_result: &Move) {
        // do nothing
    }

    fn animate_proposed_move(&mut self, _game: &mut Game, _proposed_move: &ProposedMove) {
        // do nothing
    }
}

impl UI for DefaultUI {

}

trait Draw {
    fn draw(&mut self, game: &Game, stdout: &mut Stdout, palette: &Palette) {
        self.draw_no_flush(game, stdout, palette);
        stdout.flush().unwrap();
    }
    fn draw_no_flush(&mut self, game: &Game, stdout: &mut Stdout, palette: &Palette);
}

trait Component : Draw {
    fn set_rect(&mut self, rect: Rect);

    fn rect(&self) -> Rect;

    fn is_done(&self) -> bool;

    // fn goto(&self, x: u16, y: u16) -> termion::cursor::Goto {
    //     let rect = self.rect();
    //     goto(rect.left + x, rect.top + y)
    // }

    fn goto(&self, x: u16, y: u16) -> MoveTo {
        let rect = self.rect();
        MoveTo(rect.left + x, rect.top + y)
    }

    fn clear(&self, stdout: &mut Stdout) {
        let rect = self.rect();
        let blank_string = (0..rect.width).map(|_| " ").collect::<String>();
        for y in 0..rect.height {
            // write!(*stdout, "{}{}", self.goto(0, y), blank_string).unwrap();
            queue!(*stdout, self.goto(0, y), Print(blank_string.clone())).unwrap();//FIXME clear component without cloning a bunch of strings
        }
    }

    // fn draw_window_frame(&self, title: &str, stdout: &mut termion::raw::RawTerminal<StdoutLock>) {
    //
    // }
}

mod audio;
mod buf;
mod indicators;
mod log;
mod map;
mod mode;
mod scroll;
mod sym;

use self::scroll::Scroller;
use self::indicators::{CurrentPlayer,Turn};
use self::log::LogArea;
use self::map::Map;
use self::mode::Mode;

enum ViewportSize {
    REGULAR,
    THEATER,
    FULLSCREEN
}

impl ViewportSize {
    fn rect(&self, term_dims: Dims) -> Rect {
        match *self {
            ViewportSize::REGULAR => Rect {
                left: 0,
                top: HEADER_HEIGHT,
                width: (term_dims.width - V_SCROLLBAR_WIDTH) / 2,
                height: 25
            },
            ViewportSize::THEATER => Rect {
                left: 0,
                top: HEADER_HEIGHT,
                width: term_dims.width - V_SCROLLBAR_WIDTH,
                height: 25
            },
            ViewportSize::FULLSCREEN => Rect {
                left: 0,
                top: 0,
                width: term_dims.width - V_SCROLLBAR_WIDTH,
                height: term_dims.height - H_SCROLLBAR_HEIGHT - 1
            }
        }
    }
}

fn current_player_rect() -> Rect {
    Rect {
        left: 10,
        top: 0,
        width: 21,
        height: 1
    }
}

fn turn_rect(current_player_rect: Rect) -> Rect {
    Rect {
        left: current_player_rect.right() + 2,
        top: 0,
        width: 11,
        height: 1
    }
}

fn log_area_rect(viewport_rect: Rect, term_dims: Dims) -> Rect {
    Rect {
        left: 0,
        top: viewport_rect.bottom() + 2,
        width: viewport_rect.width,
        height: term_dims.height - viewport_rect.height - 10
    }
}

fn sidebar_rect(viewport_rect: Rect, term_dims: Dims) -> Rect {
    // Rect {
    //     left: viewport_rect.right() + 1,
    //     top: viewport_rect.top,
    //     width: term_dims.width - viewport_rect.width,
    //     height: term_dims.height
    // }
    Rect {
        left: viewport_rect.width + V_SCROLLBAR_WIDTH + 1,
        top: HEADER_HEIGHT + 1,
        width: term_dims.width - viewport_rect.width - 2,
        height: term_dims.height - HEADER_HEIGHT
    }
}

const H_SCROLLBAR_HEIGHT: u16 = 1;
const V_SCROLLBAR_WIDTH: u16 = 1;

/// The terminal-based user interface.
struct TermUI {
    stdout: Stdout,
    term_dims: Dims,
    viewport_size: ViewportSize,

    map_scroller: Scroller<Map>,
    log: LogArea,
    sidebar_buf: RectBuffer,
    current_player: CurrentPlayer,
    turn: Turn,
    first_draw: bool,
    palette: Rc<Palette>,
    unicode: bool,
    confirm_turn_end: bool,
    audio_thread_tx: Option<Sender<Sounds>>,
    input_thread_rx: Receiver<KeyEvent>,
}

impl TermUI {
    fn new(
        map_dims: Dims,
        term_dims: Dims,
        stdout: Stdout,
        palette: Palette,
        unicode: bool,
        confirm_turn_end: bool,
        audio_thread_tx: Option<Sender<Sounds>>,
        input_thread_rx: Receiver<KeyEvent>,
    ) -> Self {
        let viewport_size = ViewportSize::REGULAR;
        let viewport_rect = viewport_size.rect(term_dims);
        let sidebar_rect = sidebar_rect(viewport_rect, term_dims);

        let palette = Rc::new(palette);

        let map = Map::new(viewport_rect, map_dims, palette.clone(), unicode);

        let map_scroller_rect = Rect {
            left: viewport_rect.left,
            top: viewport_rect.top,
            width: viewport_rect.width + 1,
            height: viewport_rect.height + 1
        };
        let mut map_scroller = Scroller::new(map_scroller_rect, map);
        map_scroller.set_rect(viewport_rect);

        let log_rect = log_area_rect(viewport_rect, term_dims);
        let log = LogArea::new(log_rect);

        let cp_rect = current_player_rect();
        let current_player = CurrentPlayer::new(cp_rect);

        let mut ui = TermUI {
            stdout,
            term_dims,
            viewport_size,

            map_scroller,
            log,
            sidebar_buf: RectBuffer::new(sidebar_rect),
            current_player,

            turn: Turn::new(turn_rect(cp_rect)),

            first_draw: true,
            
            palette,

            unicode,

            confirm_turn_end,

            audio_thread_tx,
            input_thread_rx,
        };

        ui.clear();

        ui
    }

    fn clear(&mut self) {
        // write!(self.stdout, "{}", clear::All).unwrap();
        // self.stdout.queue(Clear(ClearType::All));
        queue!(self.stdout, Clear(ClearType::All), SetBackgroundColor(self.palette.get_single(Colors::Background))).unwrap();

        // for x in 0..self.term_dims.width {
        //     for y in 0..self.term_dims.height {
        //         // write!(self.stdout, "{}{} ", goto(x,y), Bg(Rgb(0,0,0))).unwrap();
        //         queue!(self.stdout, goto(x,y), Output(String::from(" "))).unwrap();
        //     }
        // }
    }

    fn set_viewport_size(&mut self, game: &Game, viewport_size: ViewportSize) {
        self.viewport_size = viewport_size;
        self.map_scroller.set_rect(self.viewport_size.rect(self.term_dims));
        self.draw(game);
    }

    pub fn rotate_viewport_size(&mut self, game: &Game) {
        let new_size = match self.viewport_size {
            ViewportSize::REGULAR => ViewportSize::THEATER,
            ViewportSize::THEATER => ViewportSize::FULLSCREEN,
            ViewportSize::FULLSCREEN => ViewportSize::REGULAR
        };

        self.set_viewport_size(game, new_size);
        self.draw(game);
    }

    fn draw(&mut self, game: &Game) {
        self.draw_no_flush(game);
        self.stdout.flush().unwrap();
    }

    fn draw_no_flush(&mut self, game: &Game) {
        if self.first_draw {
            // write!(self.stdout, "{}{}{}{}",
            //     // termion::clear::All,
            //     goto(0,0),
            //     termion::style::Underline,
            //     conf::APP_NAME,
            //     StrongReset::new(&self.palette)
            // ).unwrap();
            queue!(self.stdout,
                MoveTo(0, 0),
                SetAttribute(Attribute::Underlined),
                Print(conf::APP_NAME.to_string()),
                SetAttribute(Attribute::Reset),
                SetBackgroundColor(self.palette.get_single(Colors::Background))
            ).unwrap();

            self.first_draw = false;
        }

        self.log.draw_no_flush(game, &mut self.stdout, &self.palette);
        self.current_player.draw_no_flush(game, &mut self.stdout, &self.palette);
        self.map_scroller.draw_no_flush(game, &mut self.stdout, &self.palette);
        self.turn.draw_no_flush(game, &mut self.stdout, &self.palette);
        self.sidebar_buf.draw_no_flush(game, &mut self.stdout, &self.palette);

        // write!(self.stdout, "{}{}", StrongReset::new(&self.palette), termion::cursor::Hide).unwrap();
        queue!(self.stdout,
            SetAttribute(Attribute::Reset),
            SetBackgroundColor(self.palette.get_single(Colors::Background)),
            Hide
        ).unwrap();
    }

    fn draw_located_observations(&mut self, game: &Game, located_obs: &[LocatedObs]) {
        for located_obs in located_obs {
            if let Some(viewport_loc) = self.map_scroller.scrollable.map_to_viewport_coords(located_obs.loc) {
                // let (city,unit) = if let Obs::Observed{ref tile,..} = located_obs.item {
                //     (Some(tile.city.as_ref()), Some(tile.unit.as_ref()))
                // } else {
                //     (Some(None),Some(None))
                // };

                self.map_scroller.scrollable.draw_tile_no_flush(game, &mut self.stdout, viewport_loc, false, 
                    false, None, None, None, Some(&located_obs.item));
            }
        }
    }

    fn animate_combat<A:CombatCapable+Sym,D:CombatCapable+Sym>(
        &mut self,
        game: &Game,
        outcome: &CombatOutcome<A,D>,
        attacker_loc: Location,
        defender_loc: Location) {

        let map = &mut self.map_scroller.scrollable;

        let attacker_viewport_loc = map.map_to_viewport_coords(attacker_loc);
        let defender_viewport_loc = map.map_to_viewport_coords(defender_loc);
        let attacker_sym = outcome.attacker().sym(self.unicode);
        let defender_sym = outcome.defender().sym(self.unicode);

        for damage_recipient in outcome.received_damage_sequence() {
            let viewport_loc = match *damage_recipient {
                CombatParticipant::Attacker => attacker_viewport_loc,
                CombatParticipant::Defender => defender_viewport_loc
            };
            let sym = match *damage_recipient {
                CombatParticipant::Attacker => attacker_sym,
                CombatParticipant::Defender => defender_sym
            };

            if let Some(viewport_loc) = viewport_loc {
                map.draw_tile_and_flush(game, &mut self.stdout, viewport_loc, true, false, None, None, Some(sym), None);
                sleep_millis(100);
                map.draw_tile_and_flush(game, &mut self.stdout, viewport_loc, false, false, None, None, Some(sym), None);
            } else {
                sleep_millis(100);
            }
        }
    }

    fn viewport_rect(&self) -> Rect {
        self.viewport_size.rect(self.term_dims)
    }

    fn cursor_viewport_loc(&self, mode: &Mode, game: &Game) -> Option<Location> {
        let map = &self.map_scroller.scrollable;

        match *mode {
            Mode::SetProduction{city_loc} => map.map_to_viewport_coords(city_loc),
            Mode::GetUnitOrders{unit_id,..} => {
                let unit_loc = game.current_player_unit_loc(unit_id).unwrap();
                map.map_to_viewport_coords(unit_loc)
            },
            _ => None
        }
    }

    fn ensure_map_loc_visible(&mut self, map_loc: Location) {
        self.map_scroller.scrollable.center_viewport_if_not_visible(map_loc);
    }

    fn cursor_map_loc(&self, mode: &Mode, game: &Game) -> Option<Location> {
        match *mode {
            Mode::SetProduction{city_loc} => Some(city_loc),
            Mode::GetUnitOrders{unit_id,..} => {
                let unit_loc = game.current_player_unit_loc(unit_id).unwrap();
                Some(unit_loc)
            },
            _ => None
        }
    }

    fn play_sound(&self, sound: Sounds) {
        if let Some(tx) = self.audio_thread_tx.as_ref() {
            tx.send(sound).unwrap();
        }
    }

    /// Block until a key is pressed; return that key
    fn get_key(&self) -> KeyEvent {
        self.input_thread_rx.recv().unwrap()
    }

    /// Return Some(key) if a key from the input thread is waiting for us, otherwise return None
    fn try_get_key(&self) -> Option<KeyEvent> {
        self.input_thread_rx.try_recv().ok()
    }

    fn confirm_turn_end(&self) -> bool {
        self.confirm_turn_end
    }

    fn sidebar_buf_mut(&mut self) -> &mut RectBuffer {
        &mut self.sidebar_buf
    }

    fn map(&self) -> &Map {
        &self.map_scroller.scrollable
    }
}

impl LogTarget for TermUI {
    fn log_message<T>(&mut self, message: T) where Message:From<T> {
        self.log.log_message(message);
    }

    fn replace_message<T>(&mut self, message: T) where Message:From<T> {
        self.log.replace_message(message);
    }
}

impl MoveAnimator for TermUI {
    fn animate_move(&mut self, game: &Game, move_result: &Move) {
        let mut current_loc = move_result.starting_loc;

        self.ensure_map_loc_visible(current_loc);
        self.draw(game);

        for (move_idx, move_) in move_result.components.iter().enumerate() {
            let target_loc = move_.loc;
            self.ensure_map_loc_visible(current_loc);

            //FIXME This draw is revealing current game state when we really need to show the past few steps of game state involved with this move
            // self.draw_no_flush(game);

            let mut was_combat = false;
            if let Some(ref combat) = move_.unit_combat {
                self.animate_combat(game, combat, current_loc, target_loc);
                was_combat = true;
            }

            if let Some(ref combat) = move_.city_combat {
                self.animate_combat(game, combat, current_loc, target_loc);
                was_combat = true;
            }

            if move_.distance_moved() > 0 {
                self.log_message(Message {
                    text: format!("Unit {} {}", move_result.unit, if move_.moved_successfully() {
                        if was_combat {"victorious"} else {"moved successfully"}
                    } else {"destroyed"}),
                    mark: Some('*'),
                    fg_color: Some(Colors::Combat),
                    bg_color: None,
                    source: Some(MessageSource::UI)
                });
            }

            if move_.moved_successfully() {
                self.draw_located_observations(game, &move_.observations_after_move);
            }

            current_loc = target_loc;

            self.stdout.flush().unwrap();

            if move_idx < move_result.components.len() - 1 {
                sleep_millis(100);
            }
        }

        if move_result.unit.moves_remaining() == 0 {
            sleep_millis(250);
        }
    }

    fn animate_proposed_move(&mut self, game: &mut Game, proposed_move: &ProposedMove) {
        let mut current_loc = proposed_move.0.starting_loc;

        self.ensure_map_loc_visible(current_loc);
        self.draw(game);

        for (move_idx, move_) in proposed_move.0.components.iter().enumerate() {
            let target_loc = move_.loc;
            self.ensure_map_loc_visible(current_loc);

            //FIXME This draw is revealing current game state when we really need to show the past few steps of game state involved with this move
            // self.draw_no_flush(game);

            let mut was_combat = false;
            if let Some(ref combat) = move_.unit_combat {
                self.animate_combat(game, combat, current_loc, target_loc);
                was_combat = true;
            }

            if let Some(ref combat) = move_.city_combat {
                self.animate_combat(game, combat, current_loc, target_loc);
                was_combat = true;
            }

            if was_combat {
                self.log_message(Message {
                    text: format!("Unit {} was {}", proposed_move.0.unit, if move_.moved_successfully() {
                        "victorious"
                    } else {
                        "destroyed"
                    }),
                    mark: Some('*'),
                    fg_color: Some(Colors::Combat),
                    bg_color: None,
                    source: Some(MessageSource::UI)
                });
            }

            if move_.moved_successfully() {
                self.draw_located_observations(game, &move_.observations_after_move);
            }

            current_loc = target_loc;

            self.stdout.flush().unwrap();

            if move_idx < proposed_move.0.components.len() - 1 {
                sleep_millis(100);
            }
        }

        if proposed_move.0.moved_successfully() {
            self.log_message(Message {
                text: format!("Unit {} moved successfully", proposed_move.0.unit),
                mark: None,
                fg_color: Some(Colors::Combat),
                bg_color: None,
                source: Some(MessageSource::UI)
            });
        }

        if proposed_move.0.unit.moves_remaining() == 0 {
            sleep_millis(250);
        }
    }
}