summaryrefslogtreecommitdiff
path: root/2018-tower-defence/src/input/json.rs
blob: a71d49ea8d97438d5b3d24746e94ddd925a16861 (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
use std::fs::File;
use std::io::prelude::*;
use serde_json;
use std::error::Error;

use engine;
use engine::command;
use engine::bitwise_engine;
use engine::constants::*;

pub fn read_bitwise_state_from_file(filename: &str) -> Result<bitwise_engine::BitwiseGameState, Box<Error>> {
    let mut file = File::open(filename)?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;
    let state: State = serde_json::from_str(content.as_ref())?;

    let engine_state = state.to_bitwise_engine();
    Ok(engine_state)
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct State {
    game_details: GameDetails,
    players: Vec<Player>,
    game_map: Vec<Vec<GameCell>>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct GameDetails {
    round: u16
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Player {
    player_type: char,
    energy: u16,
    health: u8,
    iron_curtain_available: bool,
    active_iron_curtain_lifetime: i16
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct GameCell {
    x: u8,
    y: u8,
    buildings: Vec<BuildingState>,
    missiles: Vec<MissileState>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct BuildingState {
    health: u8,
    construction_time_left: i16,
    weapon_cooldown_time_left: u8,
    building_type: String,
    x: u8,
    y: u8,
    player_type: char
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct MissileState {
    player_type: char
}


impl State {
    fn to_bitwise_engine(&self) -> bitwise_engine::BitwiseGameState {
        let mut player = bitwise_engine::Player::empty();
        let mut opponent = bitwise_engine::Player::empty();

        self.player().map_onto_engine(&mut player);
        self.opponent().map_onto_engine(&mut opponent);
        
        for row in &self.game_map {
            for cell in row {
                let point = engine::geometry::Point::new(cell.x, cell.y);
                for building in &cell.buildings {
                    let building_type = building.convert_building_type();
                    
                    let mut bitwise_buildings = if building.player_type == 'A' {
                        &mut player
                    } else {
                        &mut opponent
                    };
                    let bitfield = point.to_bitfield();

                    bitwise_buildings.occupied |= bitfield;
                    if building.construction_time_left >= 0 {
                        bitwise_buildings.unconstructed.push(building.to_bitwise_engine_unconstructed());
                    } else {
                        for health_tier in 0..DEFENCE_HEALTH {
                            if building.health > health_tier as u8 * MISSILE_DAMAGE {
                                bitwise_buildings.buildings[health_tier] |= bitfield;
                            }
                        }
                        if building_type == command::BuildingType::Energy {
                            bitwise_buildings.energy_towers |= bitfield;
                        }
                        else if building_type == command::BuildingType::Attack {
                            for cooldown_tier in 0..MISSILE_COOLDOWN + 1 {
                                if building.weapon_cooldown_time_left == cooldown_tier as u8 {
                                    bitwise_buildings.missile_towers[cooldown_tier] |= bitfield;
                                }
                            }
                        }
                        else if building_type == command::BuildingType::Tesla {
                            bitwise_buildings.tesla_cooldowns.push(bitwise_engine::TeslaCooldown { 
                                pos: point,
                                cooldown: building.weapon_cooldown_time_left,
                                age: building.construction_time_left.abs() as u16
                            });
                        }
                    }
                }
                for missile in &cell.missiles {
                    let (mut left, mut right) = engine::geometry::Point::new_double_bitfield(cell.x, cell.y, missile.player_type == 'A');
                    let mut bitwise_buildings = if missile.player_type == 'A' {
                        &mut player
                    } else {
                        &mut opponent
                    };

                    for mut tier in &mut bitwise_buildings.missiles {
                        let setting = (!tier.0 & left, !tier.1 & right);
                        tier.0 |= setting.0;
                        tier.1 |= setting.1;
                        left &= !setting.0;
                        right &= !setting.1;
                    }
                }
            }
        }
            
        bitwise_engine::BitwiseGameState::new(
            player, opponent,
            self.game_details.round
        )
    }

    fn player(&self) -> &Player {
        self.players.iter()
            .find(|p| p.player_type == 'A')
            .expect("Player character did not appear in state.json")
    }

    fn opponent(&self) -> &Player {
        self.players.iter()
            .find(|p| p.player_type == 'B')
            .expect("Opponent character did not appear in state.json")
    }
}

impl BuildingState {
    fn to_bitwise_engine_unconstructed(&self) -> bitwise_engine::UnconstructedBuilding {
        bitwise_engine::UnconstructedBuilding {
            pos: engine::geometry::Point::new(self.x, self.y),
            construction_time_left: self.construction_time_left as u8, // > 0 check already happened
            building_type: self.convert_building_type()
        }
    }

    fn convert_building_type(&self) -> command::BuildingType {
        match self.building_type.as_ref() {
            "ATTACK" => command::BuildingType::Attack,
            "ENERGY" => command::BuildingType::Energy,
            "TESLA" => command::BuildingType::Tesla,
            _ => command::BuildingType::Defence,
        }
    }
}


impl Player {
    fn map_onto_engine(&self, engine_player: &mut bitwise_engine::Player) {
        engine_player.health = self.health;
        engine_player.energy = self.energy;
        engine_player.iron_curtain_available = self.iron_curtain_available;
        engine_player.iron_curtain_remaining = if self.active_iron_curtain_lifetime < 0 {
            0
        } else {
            self.active_iron_curtain_lifetime as u8
        };
    }
}