summaryrefslogtreecommitdiffstats
path: root/src/game_state.rs
blob: 057c529f9137673ea9fd45cb910062b76df37263 (plain)
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
// -*- coding: utf-8 -*-
//
// Copyright 2021 Michael Buesch <m@bues.ch>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

mod recorder;
mod serialize;

use anyhow as ah;
use crate::board::{
    BOARD_HEIGHT,
    BOARD_WIDTH,
    BoardIterator,
    BoardPosIterator,
    PosType,
    coord_is_on_board,
    is_on_main_diag,
};
use crate::net::{
    client::Client,
    consts::{
        MAX_PLAYERS,
        MAX_ROOMS,
    },
    protocol::{
        MSG_MOVE_ACTION_ABORT,
        MSG_MOVE_ACTION_MOVE,
        MSG_MOVE_ACTION_PICK,
        MSG_MOVE_ACTION_PUT,
        MSG_MOVE_TOKEN_CURRENT,
        MSG_MOVE_TOKEN_SHEEP,
        MSG_MOVE_TOKEN_WOLF,
        Message,
        MsgGameState,
        MsgMove,
        MsgPlayerList,
        MsgRoomList,
        MsgSay,
        MsgType,
    },
};
use crate::coord::{
    Coord,
    CoordAxis,
};
use crate::coord;
use crate::game_state::recorder::{
    RecordedMove,
    Recorder,
};
use crate::player::{
    Player,
    PlayerList,
    PlayerMode,
    num_to_player_mode,
};
use crate::print::Print;
use crate::random::random_alphanum;
use std::collections::VecDeque;
use std::fmt;

const SAY_DEQUE_MAX_LEN: usize = 0x1000;

/// All possible relative move offsets that could lead to a capture.
const CAPTURE_OFFSETS: [Coord; 12] = [
    // horizontal
    coord!(-2, 0),
    coord!(2, 0),
    // vertical
    coord!(0, -2),
    coord!(0, 2),
    // diagonal
    coord!(-2, -2),
    coord!(-2, 2),
    coord!(2, -2),
    coord!(2, 2),
    // left barn corner
    coord!(1, -2),
    coord!(-1, 2),
    // right barn corner
    coord!(-1, -2),
    coord!(1, 2),
];

/// All possible relative non-capture move offsets.
const MOVE_OFFSETS: [Coord; 8] = [
    // horizontal
    coord!(1, 0),
    coord!(-1, 0),
    // vertical
    coord!(0, 1),
    coord!(0, -1),
    // diagonal
    coord!(-1, -1),
    coord!(-1, 1),
    coord!(1, -1),
    coord!(1, 1),
];

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FieldState {
    Unused,
    Empty,
    Wolf,
    Sheep,
}

const fn field_state_to_num(field_state: FieldState) -> u32 {
    match field_state {
        FieldState::Unused => 0,
        FieldState::Empty =>  1,
        FieldState::Wolf =>   2,
        FieldState::Sheep =>  3,
    }
}

fn num_to_field_state(field_state: u32) -> ah::Result<FieldState> {
    match field_state {
        0 => Ok(FieldState::Unused),
        1 => Ok(FieldState::Empty),
        2 => Ok(FieldState::Wolf),
        3 => Ok(FieldState::Sheep),
        s => Err(ah::format_err!("Unknown field state value: {}", s)),
    }
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum WinState {
    Undecided,
    Wolf,
    Sheep,
}

impl fmt::Display for WinState {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            WinState::Undecided => "undecided",
            WinState::Wolf => "wolf",
            WinState::Sheep => "sheep",
        })
    }
}

macro_rules! unused { () => { FieldState::Unused } }
macro_rules! empty { () => { FieldState::Empty } }
macro_rules! wolf { () => { FieldState::Wolf } }
macro_rules! sheep { () => { FieldState::Sheep } }

const INITIAL_STATE: [[FieldState; BOARD_WIDTH as usize]; BOARD_HEIGHT as usize] = [
    [unused!(), unused!(), empty!(), unused!(), unused!(), ],
    [unused!(), empty!(),  empty!(), empty!(),  unused!(), ],
    [empty!(),  wolf!(),   empty!(), wolf!(),   empty!(),  ],
    [empty!(),  empty!(),  empty!(), empty!(),  empty!(),  ],
    [sheep!(),  sheep!(),  sheep!(), sheep!(),  sheep!(),  ],
    [sheep!(),  sheep!(),  sheep!(), sheep!(),  sheep!(),  ],
    [sheep!(),  sheep!(),  sheep!(), sheep!(),  sheep!(),  ],
];

pub fn is_opposite_token(a: FieldState, b: FieldState) -> bool {
    (a == FieldState::Sheep && b == FieldState::Wolf) ||
    (a == FieldState::Wolf  && b == FieldState::Sheep)
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum MoveState {
    NoMove,
    Wolf(Coord),
    Sheep(Coord),
}

const fn move_state_to_num(move_state: &MoveState) -> (u32, u32, u32) {
    match move_state {
        MoveState::NoMove =>        (0, 0, 0),
        MoveState::Wolf(coord) =>   (1, coord.x as u32, coord.y as u32),
        MoveState::Sheep(coord) =>  (2, coord.x as u32, coord.y as u32),
    }
}

fn num_to_move_state(move_state: (u32, u32, u32)) -> ah::Result<MoveState> {
    match move_state {
        (0, _, _) => Ok(MoveState::NoMove),
        (1, x, y) => Ok(MoveState::Wolf(coord!(x as i16, y as i16))),
        (2, x, y) => Ok(MoveState::Sheep(coord!(x as i16, y as i16))),
        (a, b, c) => Err(ah::format_err!("Unknown move state values: {} {} {}",
                                         a, b, c)),
    }
}

#[derive(Copy, Clone, PartialEq, Debug)]
enum Turn {
    Sheep,
    Wolf,
}

const fn turn_to_num(turn: &Turn) -> u32 {
    match turn {
        Turn::Sheep => 0,
        Turn::Wolf => 1,
    }
}

fn num_to_turn(turn: u32) -> ah::Result<Turn> {
    match turn {
        0 => Ok(Turn::Sheep),
        1 => Ok(Turn::Wolf),
        2 => Ok(Turn::Wolf), // backward compat: was WolfchainOrSheep
        turn => Err(ah::format_err!("Unknown turn value: {}", turn)),
    }
}

#[derive(PartialEq, Debug)]
enum ValidationResult {
    Invalid,
    Valid,
    ValidCapture(Coord),
}

#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Stats {
    pub wolves:         u8,
    pub sheep:          u8,
    pub sheep_captured: u8,
}

pub struct GameState {
    player_mode:        PlayerMode,
    player_name:        String,
    room_player_list:   PlayerList,
    room_list:          Vec<String>,

    fields:             [[FieldState; BOARD_WIDTH as usize]; BOARD_HEIGHT as usize],
    moving:             MoveState,
    i_am_moving:        bool,
    stats:              Stats,
    turn:               Turn,
    just_captured:      Option<Coord>,
    orig_sheep_count:   u8,
    recorder:           Recorder,

    client:             Option<Client>,
    client_addr:        Option<String>,
    joined_room:        Option<String>,
    say_deque:          VecDeque<String>,
}

impl GameState {
    /// Construct a new game state.
    pub fn new(player_mode:         PlayerMode,
               player_name:         Option<String>)
               -> ah::Result<GameState> {

        let fields = [[FieldState::Unused; BOARD_WIDTH as usize]; BOARD_HEIGHT as usize];
        let stats = Stats {
            wolves:         0,
            sheep:          0,
            sheep_captured: 0,
        };
        let room_player_list = PlayerList::new(vec![
            Player::new("Player".to_string(),
                        player_mode,
                        true)]);
        let player_name = match player_name {
            Some(n) => n,
            None => format!("Player-{}", random_alphanum(5)),
        };
        let mut game = GameState {
            player_mode,
            player_name,
            room_player_list,
            room_list:          vec![],
            fields,
            moving:             MoveState::NoMove,
            i_am_moving:        false,
            stats,
            turn:               Turn::Sheep,
            just_captured:      None,
            orig_sheep_count:   0,
            recorder:           Recorder::new(),
            client:             None,
            client_addr:        None,
            joined_room:        None,
            say_deque:          VecDeque::new(),
        };
        game.reset_game(true);
        game.print_turn();
        Ok(game)
    }

    pub fn get_recorder(&mut self) -> &Recorder {
        if let Err(e) = self.client_update_recorder() {
            Print::error(&format!("Failed to fetch recorder state from server: {}", e));
        }
        &self.recorder
    }

    pub fn reset_game(&mut self, force: bool) {
        if self.player_mode == PlayerMode::Spectator && !force {
            Print::error("reset_game: Player is spectator. Not allowed to reset the game.");
            return;
        }

        self.orig_sheep_count = 0;
        for coord in BoardIterator::new() {
            let x = coord.x as usize;
            let y = coord.y as usize;
            self.fields[y][x] = INITIAL_STATE[y][x];
            match self.fields[y][x] {
                FieldState::Sheep =>
                    self.orig_sheep_count += 1,
                FieldState::Wolf | FieldState::Unused | FieldState::Empty =>
                    (),
            }
        }

        self.moving = MoveState::NoMove;
        self.i_am_moving = false;
        self.turn = Turn::Sheep;
        self.just_captured = None;

        self.recorder.reset();
        self.recalc_stats();
        self.client_send_reset_game();
    }

    pub fn set_player_mode(&mut self, player_mode: PlayerMode) -> ah::Result<()> {
        if self.player_mode != player_mode {
            self.do_join_room(None, None, Some(player_mode))?;
        }
        Ok(())
    }

    pub fn set_player_name(&mut self, player_name: &str) -> ah::Result<()> {
        if self.player_name != player_name {
            self.do_join_room(None, Some(player_name), None)?;
        }
        Ok(())
    }

    pub fn set_room_player_list(&mut self, room_player_list: PlayerList) {
        self.room_player_list = room_player_list;
    }

    pub fn get_room_player_list(&self) -> &PlayerList {
        &self.room_player_list
    }

    pub fn get_room_list(&self) -> &Vec<String> {
        &self.room_list
    }

    fn recalc_stats(&mut self) {
        self.stats.wolves = 0;
        self.stats.sheep = 0;
        for coord in BoardIterator::new() {
            let x = coord.x as usize;
            let y = coord.y as usize;
            match self.fields[y][x] {
                FieldState::Wolf =>
                    self.stats.wolves += 1,
                FieldState::Sheep =>
                    self.stats.sheep += 1,
                FieldState::Unused | FieldState::Empty =>
                    (),
            }
        }
        self.stats.sheep_captured = self.orig_sheep_count - self.stats.sheep;
    }

    /// Get statistics.
    pub fn get_stats(&self) -> Stats {
        self.stats
    }

    fn wolves_are_stuck(&self) -> bool {
        if self.turn != Turn::Wolf {
            return false;
        }

        let mut can_move = false;
        for coord in BoardIterator::new() {
            match self.fields[coord.y as usize][coord.x as usize] {
                FieldState::Wolf => {
                    // Check if this wolf can do a valid move.
                    for offset in &MOVE_OFFSETS {
                        let to_pos = coord + *offset;
                        match self.validate_move(coord, to_pos) {
                            ValidationResult::Valid => can_move = true,
                            ValidationResult::Invalid | ValidationResult::ValidCapture(_) => (),
                        }
                    }
                    // Check if this wolf can do a valid capture.
                    for offset in &CAPTURE_OFFSETS {
                        let to_pos = coord + *offset;
                        match self.validate_move(coord, to_pos) {
                            ValidationResult::ValidCapture(_) => can_move = true,
                            ValidationResult::Invalid | ValidationResult::Valid => (),
                        }
                    }
                },
                FieldState::Sheep | FieldState::Unused | FieldState::Empty => {
                },
            }
            if can_move {
                break;
            }
        }
        !can_move
    }

    pub fn get_win_state(&self) -> WinState {
        if self.get_stats().sheep < 7 {
            WinState::Wolf
        } else {
            let mut sheep_win = true;
            for (coord, pos_type) in BoardPosIterator::new() {
                let x = coord.x as usize;
                let y = coord.y as usize;
                match pos_type {
                    PosType::Invalid => (),
                    PosType::Field => (),
                    PosType::Barn => {
                        if self.fields[y][x] != FieldState::Sheep {
                            sheep_win = false;
                        }
                    },
                }
            }
            if sheep_win {
                WinState::Sheep
            } else if self.wolves_are_stuck() {
                WinState::Sheep
            } else {
                WinState::Undecided
            }
        }
    }

    /// Set the state of a board field.
    fn set_field_state(&mut self, pos: Coord, state: FieldState) {
        if coord_is_on_board(pos) {
            self.fields[pos.y as usize][pos.x as usize] = state;
        }
    }

    /// Get the current state of a board field.
    pub fn get_field_state(&self, pos: Coord) -> FieldState {
        if coord_is_on_board(pos) {
            self.fields[pos.y as usize][pos.x as usize]
        } else {
            FieldState::Unused
        }
    }

    /// Get the moving state of a position.
    pub fn get_field_moving(&self, pos: Coord) -> bool {
        match self.get_move_state() {
            MoveState::NoMove => false,
            MoveState::Wolf(p) => p == pos,
            MoveState::Sheep(p) => p == pos,
        }
    }

    /// Get the global move status.
    pub fn get_move_state(&self) -> MoveState {
        self.moving
    }

    /// Capture one token at pos.
    fn capture(&mut self, _from_pos: Coord, to_pos: Coord, capture_pos: Coord) {
        match self.get_field_state(capture_pos) {
            FieldState::Unused | FieldState::Empty =>
                Print::error("Internal error: Cannot capture empty fields."),
            FieldState::Wolf =>
                Print::error("Internal error: Cannot capture wolves."),
            FieldState::Sheep => {
                self.stats.sheep -= 1;
                self.stats.sheep_captured += 1;
                self.just_captured = Some(to_pos);
                self.set_field_state(capture_pos, FieldState::Empty);
                Print::debug(&format!("Captured sheep at {}", capture_pos));
            },
        }
    }

    /// Check if a move from from_pos to to_pos is valid.
    fn validate_move(&self, from_pos: Coord, to_pos: Coord) -> ValidationResult {
        // Check if positions are on the board.
        if !coord_is_on_board(from_pos) || !coord_is_on_board(to_pos) {
            return ValidationResult::Invalid;
        }

        // Check if from position has a token.
        let from_state = self.get_field_state(from_pos);
        match from_state {
            FieldState::Unused | FieldState::Empty =>
                return ValidationResult::Invalid,
            FieldState::Wolf | FieldState::Sheep =>
                (),
        }
        // Check if to position has no token.
        let to_state = self.get_field_state(to_pos);
        match to_state {
            FieldState::Unused | FieldState::Wolf | FieldState::Sheep =>
                return ValidationResult::Invalid,
            FieldState::Empty =>
                (),
        }

        // Check if the player is allowed to move this token.
        match self.player_mode {
            PlayerMode::Spectator =>
                return ValidationResult::Invalid,
            PlayerMode::Both =>
                (),
            PlayerMode::Wolf => {
                if from_state != FieldState::Wolf {
                    return ValidationResult::Invalid;
                }
            },
            PlayerMode::Sheep => {
                if from_state != FieldState::Sheep {
                    return ValidationResult::Invalid;
                }
            },
        }

        // Check if this is our turn.
        match self.turn {
            Turn::Sheep => {
                if from_state != FieldState::Sheep {
                    return ValidationResult::Invalid;
                }
            },
            Turn::Wolf => {
                if from_state != FieldState::Wolf {
                    return ValidationResult::Invalid;
                }
            },
        }

        let distx = to_pos.x as isize - from_pos.x as isize;
        let centerx = from_pos.x as isize + (distx / 2);
        let disty = to_pos.y as isize - from_pos.y as isize;
        let centery = from_pos.y as isize + (disty / 2);

        let center_pos = coord!(centerx as CoordAxis, centery as CoordAxis);
        let center_state = self.get_field_state(center_pos);

        let mut result = ValidationResult::Invalid;

        if from_state == FieldState::Sheep &&
           to_pos.y > from_pos.y {
            // Invalid sheep backward move.
        } else if from_pos.x != to_pos.x && from_pos.y != to_pos.y {
            // Diagonal move.
            if from_state == FieldState::Wolf {
                if is_on_main_diag(from_pos) && is_on_main_diag(to_pos) {
                    // Wolf diagonal move.
                    if distx.abs() == 1 && disty.abs() == 1 {
                        // Diagonal move by one field.
                        result = ValidationResult::Valid;
                    } else if distx.abs() == 2 && disty.abs() == 2 {
                        if is_opposite_token(from_state, center_state) {
                            // Captured.
                            result = ValidationResult::ValidCapture(center_pos);
                        }
                    }
                } else if (from_pos == coord!(1, 1) && to_pos == coord!(2, 0)) ||
                          (from_pos == coord!(3, 1) && to_pos == coord!(2, 0)) ||
                          (from_pos == coord!(2, 0) && to_pos == coord!(1, 1)) ||
                          (from_pos == coord!(2, 0) && to_pos == coord!(3, 1)) {
                    // Wolf move to/from barn top.
                    result = ValidationResult::Valid;
                } else if (from_pos == coord!(1, 2) && to_pos == coord!(2, 0)) ||
                          (from_pos == coord!(2, 0) && to_pos == coord!(1, 2)) {
                    if self.get_field_state(coord!(1, 1)) == FieldState::Sheep {
                        // Captured at top-left corner of barn.
                        result = ValidationResult::ValidCapture(coord!(1, 1));
                    }
                } else if (from_pos == coord!(3, 2) && to_pos == coord!(2, 0)) ||
                          (from_pos == coord!(2, 0) && to_pos == coord!(3, 2)) {
                    if self.get_field_state(coord!(3, 1)) == FieldState::Sheep {
                        // Captured at top-right corner of barn.
                        result = ValidationResult::ValidCapture(coord!(3, 1));
                    }
                }
            } else if from_state == FieldState::Sheep &&
                      (from_pos == coord!(1, 1) || from_pos == coord!(3, 1)) {
                // Sheep move to barn top.
                result = ValidationResult::Valid;
            }
        } else if from_pos.x != to_pos.x && from_pos.y == to_pos.y {
            // Horizontal move.
            if distx.abs() == 1 {
                result = ValidationResult::Valid;
            } else if distx.abs() == 2 {
                if from_state == FieldState::Wolf &&
                   is_opposite_token(from_state, center_state) {
                    // Captured.
                    result = ValidationResult::ValidCapture(center_pos);
                }
            }
        } else if from_pos.x == to_pos.x && from_pos.y != to_pos.y {
            // Vertical move.
            if disty.abs() == 1 {
                result = ValidationResult::Valid;
            } else if disty.abs() == 2 {
                if from_state == FieldState::Wolf &&
                   is_opposite_token(from_state, center_state) {
                    // Captured.
                    result = ValidationResult::ValidCapture(center_pos);
                }
            }
        } else { // Can never happen.
            Print::error("Internal error: validate_move() invalid state.");
        }

        result
    }

    fn print_turn(&self) {
        if self.get_win_state() == WinState::Undecided {
            Print::debug(&format!("Next turn is: {:?}", self.turn));
        }
    }

    fn next_turn(&mut self) {
        match self.turn {
            Turn::Sheep => {
                self.turn = Turn::Wolf;
            },
            Turn::Wolf => {
                // The next turn is sheep, except if a wolf has just captured a sheep
                // and it can capture another one.
                let mut more = false;
                if let Some(wolf_pos) = self.just_captured {
                    for offset in &CAPTURE_OFFSETS {
                        let to_pos = wolf_pos + *offset;
                        match self.validate_move(wolf_pos, to_pos) {
                            ValidationResult::ValidCapture(_) => {
                                Print::debug("Wolf can capture more sheep.");
                                more = true;
                            },
                            ValidationResult::Invalid | ValidationResult::Valid =>
                                (),
                        }
                    }
                }
                self.turn = if more { Turn::Wolf } else { Turn::Sheep };
            },
        }
        self.just_captured = None;
        self.print_turn();
    }

    /// Start a move operation.
    pub fn move_pick(&mut self, pos: Coord) -> ah::Result<()> {
        if pos.x >= BOARD_WIDTH || pos.y >= BOARD_HEIGHT {
            return Err(ah::format_err!("move_pick: Coordinates ({}) out of bounds.", pos));
        }
        if self.moving != MoveState::NoMove {
            return Err(ah::format_err!("move_pick: Already moving."));
        }
        if self.player_mode == PlayerMode::Spectator {
            return Err(ah::format_err!("move_pick: Player is spectator. Not allowed to move."));
        }
        let win_state = self.get_win_state();
        if win_state != WinState::Undecided {
            return Err(ah::format_err!("move_pick: Already decided: {}", win_state));
        }

        // Try to pick the token. This might fail.
        let result = match self.get_field_state(pos) {
            FieldState::Unused | FieldState::Empty => {
                Err(ah::format_err!("move_pick: Move from empty field."))
            },
            FieldState::Wolf => {
                self.client_send_move_pick(pos, MSG_MOVE_TOKEN_WOLF)?;
                self.moving = MoveState::Wolf(pos);
                self.set_field_state(pos, FieldState::Wolf);
                Ok(())
            },
            FieldState::Sheep => {
                self.client_send_move_pick(pos, MSG_MOVE_TOKEN_SHEEP)?;
                self.moving = MoveState::Sheep(pos);
                self.set_field_state(pos, FieldState::Sheep);
                Ok(())
            },
        };
        self.i_am_moving = result.is_ok();
        result
    }

    /// Actually commit the move-put.
    fn do_move_put(&mut self, to_pos: Coord, captured: bool) {
        match self.moving {
            MoveState::NoMove =>
                Print::error("Internal error: Invalid move source."),
            MoveState::Wolf(from_pos) => {
                self.set_field_state(to_pos, FieldState::Wolf);
                self.set_field_state(from_pos, FieldState::Empty);
            },
            MoveState::Sheep(from_pos) => {
                self.set_field_state(to_pos, FieldState::Sheep);
                self.set_field_state(from_pos, FieldState::Empty);
            },
        }
        self.recalc_stats();
        self.next_turn();
        let recorded_move = RecordedMove {
            move_state: self.moving,
            to_pos,
            captured,
            win_state: self.get_win_state(),
        };
        self.recorder.record_move(&recorded_move);
        self.moving = MoveState::NoMove;
    }

    /// End a move operation.
    pub fn move_put(&mut self, pos: Coord) -> ah::Result<()> {
        if pos.x >= BOARD_WIDTH || pos.y >= BOARD_HEIGHT {
            return Err(ah::format_err!("move_put: Coordinates out of bounds."));
        }
        if self.player_mode == PlayerMode::Spectator {
            return Err(ah::format_err!("move_put: Player is spectator. Not allowed to move."));
        }

        let (from_pos, token_id) = match self.moving {
            MoveState::NoMove =>
                return Err(ah::format_err!("move_put: Not moving.")),
            MoveState::Wolf(p) => (p, MSG_MOVE_TOKEN_WOLF),
            MoveState::Sheep(p) => (p, MSG_MOVE_TOKEN_SHEEP),
        };

        // Try to put the token. This might fail.
        let result = match self.get_field_state(pos) {
            FieldState::Unused |
            FieldState::Wolf |
            FieldState::Sheep => {
                Err(ah::format_err!("move_put: Field occupied."))
            },
            FieldState::Empty => {
                match self.validate_move(from_pos, pos) {
                    ValidationResult::Invalid =>
                        Err(ah::format_err!("move_put: Invalid move.")),
                    ValidationResult::Valid => {
                        self.client_send_move_put(pos, token_id)?;
                        self.do_move_put(pos, false);
                        Ok(())
                    },
                    ValidationResult::ValidCapture(capture_pos) => {
                        self.client_send_move_put(pos, token_id)?;
                        self.capture(from_pos, pos, capture_pos);
                        self.do_move_put(pos, true);
                        Ok(())
                    },
                }
            },
        };
        self.i_am_moving = !result.is_ok();
        result
    }

    /// Abort a move operation.
    pub fn move_abort(&mut self) {
        if self.player_mode == PlayerMode::Spectator {
            Print::error("move_abort: Player is spectator. Not allowed to move.");
            return;
        }

        match self.moving {
            MoveState::NoMove => {
                self.i_am_moving = false;
            },
            MoveState::Wolf(coord) |
            MoveState::Sheep(coord) => {
                self.moving = MoveState::NoMove;
                self.i_am_moving = false;
                self.client_send_move_abort(coord).ok();
            },
        }
    }

    pub fn make_state_message(&self) -> MsgGameState {
        let mut fields = [[field_state_to_num(FieldState::Unused);
                           BOARD_WIDTH as usize];
                          BOARD_HEIGHT as usize];
        for coord in BoardIterator::new() {
            let x = coord.x as usize;
            let y = coord.y as usize;
            fields[y][x] = field_state_to_num(self.fields[y][x]);
        }
        let (moving_state, moving_x, moving_y) = move_state_to_num(&self.moving);
        let turn = turn_to_num(&self.turn);
        MsgGameState::new(fields, moving_state, moving_x, moving_y, turn)
    }

    pub fn read_state_message(&mut self,
                              msg: &MsgGameState,
                              force: bool) -> ah::Result<bool> {
        if !force && self.player_mode == PlayerMode::Spectator {
            return Err(ah::format_err!("Player is spectator. Not allowed to load game state."));
        }

        let mut changed = false;
        if !self.i_am_moving {
            for coord in BoardIterator::new() {
                let x = coord.x as usize;
                let y = coord.y as usize;
                let field = match num_to_field_state(msg.get_fields()[y][x]) {
                    Ok(field) => field,
                    Err(e) => {
                        Print::error(&format!("Received invalid field state: {}", e));
                        self.fields[y][x]
                    },
                };
                if field != self.fields[y][x] {
                    self.fields[y][x] = field;
                    changed = true;
                }
            }

            let moving = match num_to_move_state(msg.get_moving()) {
                Ok(moving) => moving,
                Err(e) => {
                    Print::error(&format!("Received invalid moving state: {}", e));
                    self.moving
                },
            };
            if moving != self.moving {
                self.moving = moving;
                changed = true;
            }

            let turn = match num_to_turn(msg.get_turn()) {
                Ok(turn) => turn,
                Err(e) => {
                    Print::error(&format!("Received invalid turn state: {}", e));
                    self.turn
                },
            };
            if turn != self.turn {
                self.turn = turn;
                changed = true;
            }

            if changed {
                self.recorder.reset();
                self.recalc_stats();
            }
        }
        Ok(changed)
    }
}

impl Drop for GameState {
    fn drop(&mut self) {
        self.client_disconnect();
    }
}

//////////////////////////////////////////////////////////////////////////////
// Client interface.
//////////////////////////////////////////////////////////////////////////////

impl GameState {
    fn client_handle_rx_msg_gamestate(&mut self, msg: &MsgGameState) -> bool {
        match self.read_state_message(msg, true) {
            Ok(changed) => changed,
            Err(_) => false,
        }
    }

    fn client_handle_rx_msg_roomlist(&mut self, msg: &MsgRoomList) {
        let total_count = msg.get_total_count();
        if total_count > MAX_ROOMS as u32 {
            Print::error(&format!("Received RoomList with too many rooms: {}",
                                  total_count));
            return;
        }

        self.room_list.resize_with(total_count as usize,
                                   || "".to_string());

        let room_name = match msg.get_room_name() {
            Ok(n) => n,
            Err(e) => {
                Print::error(&format!("Received RoomList with invalid room name: {}", e));
                return;
            },
        };

        let index = msg.get_index() as usize;
        if index >= self.room_list.len() {
            Print::error("Received RoomList with invalid index.");
            return;
        }

        self.room_list[index] = room_name;
    }

    fn client_handle_rx_msg_playerlist(&mut self, msg: &MsgPlayerList) {
        let total_count = msg.get_total_count();
        if total_count > MAX_PLAYERS as u32 {
            Print::error(&format!("Received PlayerList with too many players: {}",
                                  total_count));
            return;
        }

        self.room_player_list.resize(total_count as usize,
                                     || Player::new("<unknown>".to_string(),
                                                    PlayerMode::Spectator,
                                                    false));

        let player_name = match msg.get_player_name() {
            Ok(n) => n,
            Err(e) => {
                Print::error(&format!("Received PlayerList with invalid player name: {}", e));
                return;
            }
        };
        let player_mode = match num_to_player_mode(msg.get_player_mode()) {
            Ok(m) => m,
            Err(e) => {
                Print::error(&format!("Received PlayerList with invalid player mode '{}': {}",
                                      msg.get_player_mode(), e));
                return;
            },
        };
        let is_self = player_name == self.player_name;

        let index = msg.get_index() as usize;
        if index >= self.room_player_list.count() {
            Print::error("Received PlayerList with invalid index.");
            return;
        }

        self.room_player_list.set_player(index,
                                         Player::new(player_name,
                                                     player_mode,
                                                     is_self));
    }

    fn client_handle_rx_msg_say(&mut self, msg: &MsgSay) {
        let text = format!("[{}]: {}",
                           msg.get_player_name(),
                           msg.get_text());
        let text = text.replace("\n", "\n\t").replace("\r", "");
        self.say_deque.push_back(text);
        if self.say_deque.len() > SAY_DEQUE_MAX_LEN {
            self.say_deque.pop_front();
        }
    }

    fn client_handle_rx_messages(&mut self, messages: Vec<Box<dyn Message>>) -> bool {
        let mut redraw = false;
        for message in &messages {
            let message = message.get_message();

            match message {
                MsgType::Nop(_) |
                MsgType::Result(_) |
                MsgType::Ping(_) |
                MsgType::Pong(_) |
                MsgType::Join(_) |
                MsgType::Leave(_) |
                MsgType::Reset(_) |
                MsgType::ReqGameState(_) |
                MsgType::ReqRoomList(_) |
                MsgType::ReqPlayerList(_) |
                MsgType::ReqRecord(_) |
                MsgType::Record(_) |
                MsgType::Move(_) => {
                    // Ignore.
                },
                MsgType::GameState(msg) => {
                    if self.joined_room.is_some() {
                        if self.client_handle_rx_msg_gamestate(msg) {
                            redraw = true;
                        }
                    }
                },
                MsgType::RoomList(msg) => {
                    self.client_handle_rx_msg_roomlist(msg);
                },
                MsgType::PlayerList(msg) => {
                    if self.joined_room.is_some() {
                        self.client_handle_rx_msg_playerlist(msg);
                    }
                },
                MsgType::Say(msg) => {
                    self.client_handle_rx_msg_say(msg);
                },
            }
        }

        redraw
    }

    /// Poll the game server state.
    pub fn poll_server(&mut self) -> bool {
        let mut redraw = false;
        loop {
            if let Some(client) = self.client.as_mut() {
                if let Some(messages) = client.poll() {
                    redraw |= self.client_handle_rx_messages(messages);
                    continue;
                }
            }
            break;
        }
        redraw
    }

    /// Connect to a game server.
    pub fn client_connect(&mut self, addr: &str) -> ah::Result<()> {
        self.client_disconnect();
        Print::info(&format!("Connecting to server {} ...", addr));
        let mut client = Client::new(addr)?;
        client.send_ping()?;
        client.send_nop()?;
        self.client = Some(client);
        self.client_addr = Some(addr.to_string());
        Ok(())
    }

    fn do_join_room(&mut self,
                    room_name: Option<&str>,
                    player_name: Option<&str>,
                    player_mode: Option<PlayerMode>) -> ah::Result<()> {
        let room_name = match room_name {
            Some(room_name) =>
                Some(room_name.to_string()),
            None =>
                match self.joined_room.as_ref() {
                    Some(joined_room) => Some(joined_room.to_string()),
                    None => None,
                },
        };

        let player_name = match player_name {
            Some(player_name) => player_name,
            None => &self.player_name,
        };

        let player_mode = match player_mode {
            Some(player_mode) => player_mode,
            None => self.player_mode,
        };

        let old_joined_room = self.joined_room.take();

        if let Some(client) = self.client.as_mut() {
            match room_name {
                Some(room_name) => {
                    match client.send_join(&room_name,
                                           player_name,
                                           player_mode) {
                        Ok(_) => {
                            self.joined_room = Some(room_name);
                        },
                        Err(e) => {
                            self.joined_room = old_joined_room;
                            return Err(e);
                        },
                    }
                },
                None => (),
            }
        }

        self.player_name = player_name.to_string();
        self.player_mode = player_mode;

        Ok(())
    }

    /// Join a room on the server.
    pub fn client_join_room(&mut self, room_name: &str) -> ah::Result<()> {
        if self.client.is_none() {
            return Err(ah::format_err!("Cannot join room. Not connected to a server."));
        }
        Print::info(&format!("Joining room '{}' ...", room_name));
        self.do_join_room(Some(room_name), None, None)?;
        if let Some(client) = self.client.as_mut() {
            client.send_request_gamestate()?;
            self.fields = [[FieldState::Unused; BOARD_WIDTH as usize]; BOARD_HEIGHT as usize];
        }
        Ok(())
    }

    /// Disconnect from a game server.
    pub fn client_disconnect(&mut self) {
        if let Some(client) = self.client.take() {
            client.disconnect();
            self.client_addr = None;
            Print::info("Disconnected from server.");
        }
        self.joined_room = None;
        self.room_list.clear();
    }

    pub fn client_is_connected(&self) -> bool {
        self.client.is_some()
    }

    /// Get the address of the connected server, if any.
    pub fn client_get_addr(&self) -> Option<&str> {
        match &self.client_addr {
            None => None,
            Some(a) => Some(a),
        }
    }

    /// Get the name of the joined room, if any.
    pub fn client_get_joined_room(&self) -> Option<&str> {
        match &self.joined_room {
            None => None,
            Some(r) => Some(r),
        }
    }

    fn client_send_reset_game(&mut self) {
        if let Some(client) = self.client.as_mut() {
            if let Err(e) = client.send_reset() {
                Print::error(&format!("Failed to game-reset: {}", e));
            }
        }
    }

    /// Send the move-pick to the server.
    fn client_send_move_pick(&mut self, pos: Coord, token_id: u32) -> ah::Result<()> {
        if let Some(client) = self.client.as_mut() {
            if let Err(e) = client.send_move_token(MSG_MOVE_ACTION_PICK,
                                                   token_id,
                                                   pos.x as u32,
                                                   pos.y as u32) {
                let msg = format!("Move-pick failed on server: {}", e);
                Print::error(&msg);
                return Err(ah::format_err!("{}", msg));
            }
        }
        Ok(())
    }

    /// Send the move-put to the server.
    fn client_send_move_put(&mut self, pos: Coord, token_id: u32) -> ah::Result<()> {
        if let Some(client) = self.client.as_mut() {
            if let Err(e) = client.send_move_token(MSG_MOVE_ACTION_PUT,
                                                   token_id,
                                                   pos.x as u32,
                                                   pos.y as u32) {
                let msg = format!("Move failed on server: {}", e);
                Print::error(&msg);
                return Err(ah::format_err!("{}", msg));
            }
        }
        Ok(())
    }

    /// Send the move-abort to the server.
    fn client_send_move_abort(&mut self, pos: Coord) -> ah::Result<()> {
        if let Some(client) = self.client.as_mut() {
            if let Err(e) = client.send_move_token(MSG_MOVE_ACTION_ABORT,
                                                   MSG_MOVE_TOKEN_CURRENT,
                                                   pos.x as u32,
                                                   pos.y as u32) {
                Print::error(&format!("Move-abort failed on server: {}", e));
            }
        }
        Ok(())
    }

    fn client_send_full_gamestate(&mut self) -> ah::Result<()> {
        let mut game_state_msg = self.make_state_message();
        if let Some(client) = self.client.as_mut() {
            client.send_msg_wait_for_ok("GameState", 3.0, &mut game_state_msg)?;
        }
        Ok(())
    }

    pub fn client_get_chat_messages(&mut self) -> Vec<String> {
        let mut ret = Vec::with_capacity(self.say_deque.len());
        for text in &self.say_deque {
            ret.push(text.to_string());
        }
        self.say_deque.clear();
        ret
    }

    pub fn client_send_chat_message(&mut self, text: &str) -> ah::Result<()> {
        if let Some(client) = self.client.as_mut() {
            client.send_chat_message(text)?;
        }
        Ok(())
    }

    fn client_update_recorder(&mut self) -> ah::Result<()> {
        if let Some(client) = self.client.as_mut() {
            let record = client.fetch_record()?;
            self.recorder.parse_text(&record)?;
        }
        Ok(())
    }
}

//////////////////////////////////////////////////////////////////////////////
// Server interface.
//////////////////////////////////////////////////////////////////////////////

impl GameState {
    pub fn server_handle_rx_msg_move(&mut self, msg: &MsgMove) -> ah::Result<()> {
        match msg.get_action() {
            (MSG_MOVE_ACTION_PICK, x, y) => {
                self.move_pick(coord!(x as i16, y as i16))?;
            },
            (MSG_MOVE_ACTION_MOVE, _x, _y) => {
                //TODO
            },
            (MSG_MOVE_ACTION_PUT, x, y) => {
                self.move_put(coord!(x as i16, y as i16))?;
            },
            (MSG_MOVE_ACTION_ABORT, _x, _y) => {
                self.move_abort();
            },
            (action, _, _) => {
                Print::error(&format!("Received invalid move action: {}", action));
            },
        }
        Ok(())
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface