summaryrefslogtreecommitdiff
path: root/src/engine/mod.rs
blob: 4ea63a35a4765cb4d05ea9d2aaa62a3410ef8fd0 (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
pub mod command;
pub mod geometry;
pub mod settings;

use self::command::{BuildingType, Command};
use self::geometry::Point;
use self::settings::GameSettings;

use std::ops::Fn;
use std::cmp;

#[derive(Debug, Clone)]
pub struct GameState {
    pub status: GameStatus,
    pub player: Player,
    pub opponent: Player,
    pub player_buildings: Vec<Building>,
    pub opponent_buildings: Vec<Building>,
    pub player_missiles: Vec<Missile>,
    pub opponent_missiles: Vec<Missile>
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GameStatus {
    Continue,
    PlayerWon,
    OpponentWon,
    Draw,
    InvalidMove
}

impl GameStatus {
    fn is_complete(&self) -> bool {
        *self != GameStatus::Continue
    }
}

#[derive(Debug, Clone)]
pub struct Player {
    pub energy: u16,
    pub health: u16
}

#[derive(Debug, Clone)]
pub struct Building {
    pub pos: Point,
    pub health: u16,
    pub construction_time_left: u8,
    pub weapon_damage: u16,
    pub weapon_speed: u8,
    pub weapon_cooldown_time_left: u8,
    pub weapon_cooldown_period: u8,
    pub energy_generated_per_turn: u16
}

impl Building {
    fn new(pos: Point, building: BuildingType) -> Building {
        match building {
            BuildingType::Defense => Building {
                pos: pos,
                health: 20,
                construction_time_left: 3,
                weapon_damage: 0,
                weapon_speed: 0,
                weapon_cooldown_time_left: 0,
                weapon_cooldown_period: 0,
                energy_generated_per_turn: 0
            },
            BuildingType::Attack => Building {
                pos: pos,
                health: 5,
                construction_time_left: 1,
                weapon_damage: 5,
                weapon_speed: 1,
                weapon_cooldown_time_left: 0,
                weapon_cooldown_period: 3,
                energy_generated_per_turn: 0
            },
            BuildingType::Energy => Building {
                pos: pos,
                health: 5,
                construction_time_left: 1,
                weapon_damage: 0,
                weapon_speed: 0,
                weapon_cooldown_time_left: 0,
                weapon_cooldown_period: 0,
                energy_generated_per_turn: 3
            }
        }
    }

    fn is_constructed(&self) -> bool {
        self.construction_time_left == 0
    }

    fn is_shooty(&self) -> bool {
        self.is_constructed() && self.weapon_damage > 0
    }
}

#[derive(Debug, Clone)]
pub struct Missile {
    pub pos: Point,
    pub damage: u16,
    pub speed: u8,
}

impl GameState {
    pub fn simulate(&self, settings: &GameSettings, player_command: Command, opponent_command: Command) -> GameState {
        if self.status.is_complete() {
            return self.clone();
        }
        
        let mut state = self.clone();
        let player_valid = GameState::perform_command(&mut state.player_buildings, player_command, &settings.size);
        let opponent_valid = GameState::perform_command(&mut state.opponent_buildings, opponent_command, &settings.size);

        if !player_valid || !opponent_valid {
            state.status = GameStatus::InvalidMove;
            return state;
        }

        GameState::update_construction(&mut state.player_buildings);
        GameState::update_construction(&mut state.opponent_buildings);

        GameState::add_missiles(&mut state.player_buildings, &mut state.player_missiles);
        GameState::add_missiles(&mut state.opponent_buildings, &mut state.opponent_missiles);

        GameState::move_missiles(&mut state.player_missiles, |p| p.move_right(&settings.size),
                                 &mut state.opponent_buildings, &mut state.opponent);
        GameState::move_missiles(&mut state.opponent_missiles, |p| p.move_left(),
                                 &mut state.player_buildings, &mut state.player);

        GameState::add_energy(&mut state.player, settings, &state.player_buildings);
        GameState::add_energy(&mut state.opponent, settings, &state.opponent_buildings);

        GameState::update_status(&mut state);
        state
    }

    fn perform_command(buildings: &mut Vec<Building>, command: Command, size: &Point) -> bool {
        match command {
            Command::Nothing => { true },
            Command::Build(p, b) => {
                let occupied = buildings.iter().any(|b| b.pos == p);
                let in_range = p.x < size.x && p.y < size.y;
                buildings.push(Building::new(p, b));
                !occupied && in_range
            },
        }
    }

    fn update_construction(buildings: &mut Vec<Building>) {
        for building in buildings.iter_mut().filter(|b| !b.is_constructed()) {
            building.construction_time_left -= 1;
        }
    }

    fn add_missiles(buildings: &mut Vec<Building>, missiles: &mut Vec<Missile>) {
        for building in buildings.iter_mut().filter(|b| b.is_shooty()) {
            if building.weapon_cooldown_time_left > 0 {
                building.weapon_cooldown_time_left -= 1;
            } else {
                missiles.push(Missile {
                    pos: building.pos,
                    speed: building.weapon_speed,
                    damage: building.weapon_damage,
                });
                building.weapon_cooldown_time_left = building.weapon_cooldown_period;
            }
        }
    }

    fn move_missiles<F>(missiles: &mut Vec<Missile>, move_fn: F, opponent_buildings: &mut Vec<Building>, opponent: &mut Player)
    where F: Fn(Point) -> Option<Point> {
        for missile in missiles.iter_mut() {
            for _ in 0..missile.speed {
                match move_fn(missile.pos) {
                    None => {
                        let damage = cmp::min(missile.damage, opponent.health);
                        opponent.health -= damage;
                        missile.speed = 0;
                    },
                    Some(point) => {
                        missile.pos = point;
                        for hit in opponent_buildings.iter_mut().filter(|b| b.is_constructed() && b.pos == point && b.health > 0) {
                            let damage = cmp::min(missile.damage, hit.health);
                            hit.health -= damage;
                            missile.speed = 0;                    
                        }
                    }
                }
                
                if missile.speed == 0 {
                    break;
                }
            }
        }
        missiles.retain(|m| m.speed > 0);
        opponent_buildings.retain(|b| b.health > 0);
    }

    fn add_energy(player: &mut Player, settings: &GameSettings, buildings: &Vec<Building>) {
        player.energy += settings.energy_income;
        player.energy += buildings.iter().map(|b| b.energy_generated_per_turn).sum::<u16>();
    }

    fn update_status(state: &mut GameState) {
        let player_dead = state.player.health == 0;
        let opponent_dead = state.player.health == 0;
        state.status = match (player_dead, opponent_dead) {
            (true, true) => GameStatus::Draw,
            (true, false) => GameStatus::PlayerWon,
            (false, true) => GameStatus::OpponentWon,
            (false, false) => GameStatus::Continue,
        };
    }
}