Changed other bitwise stuff to use constants
[entelect-challenge-tower-defence.git] / tests / live_comparison.rs
1 extern crate zombot;
2
3 use zombot::input::json;
4 use zombot::engine::command::{Command, BuildingType};
5 use zombot::engine::geometry::Point;
6 use zombot::engine::settings::GameSettings;
7 use zombot::engine::GameState;
8
9 use std::fs::File;
10 use std::io::prelude::*;
11
12 #[test]
13 fn it_successfully_simulates_replay() {
14     test_from_replay("tests/after_200", 64);
15 }
16
17 #[test]
18 fn it_successfully_simulates_replay_with_teslas() {
19     test_from_replay("tests/after_203_teslas", 60);
20 }
21
22 fn test_from_replay(replay_folder: &str, length: usize) {
23     let (settings, mut state) = json::read_bitwise_state_from_file(&format!("{}/Round 000/state.json", replay_folder)).unwrap();
24     
25     for i in 0..length {
26         let player = read_player_command(&format!("{}/Round {:03}/PlayerCommand.txt", replay_folder, i));
27         let opponent = read_opponent_command(&format!("{}/Round {:03}/OpponentCommand.txt", replay_folder, i), &settings);
28         let (_, mut expected_state) = json::read_bitwise_state_from_file(&format!("{}/Round {:03}/state.json", replay_folder, i+1)).unwrap();
29         
30         state.simulate(&settings, player, opponent);
31         state.sort();
32         expected_state.sort();
33         assert_eq!(state, expected_state, "\nFailed on state {}\n", i+1);
34     }
35 }
36
37 fn read_player_command(filename: &str) -> Command {
38     let mut file = File::open(filename).unwrap();
39     let mut content = String::new();
40     file.read_to_string(&mut content).unwrap();
41     if content.trim() == "No Command" {
42         Command::Nothing
43     }
44     else {
45         let mut components = content.split(',');
46         let point = Point::new(components.next().unwrap().trim().parse().unwrap(),
47                                components.next().unwrap().trim().parse().unwrap());
48         let action_type = components.next().unwrap().trim().parse().unwrap();
49         if action_type == 3 {
50             Command::Deconstruct(point)
51         } else {
52             Command::Build(point, BuildingType::from_u8(action_type).unwrap())
53         }
54     }
55 }
56
57 fn read_opponent_command(filename: &str, settings: &GameSettings) -> Command {
58     match read_player_command(filename) {
59         Command::Nothing => Command::Nothing,
60         Command::Build(p, b) => Command::Build(Point::new(
61             settings.size.x - p.x - 1,
62             p.y
63         ), b),
64         Command::Deconstruct(p) => Command::Deconstruct(Point::new(
65             settings.size.x - p.x - 1,
66             p.y
67         )),
68     }
69     
70 }