summaryrefslogtreecommitdiff
path: root/src/ships.rs
blob: 104e986e3e46d4e7c888a8c713169ed975ce588e (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
use std::fmt;
use std::str;

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum Weapon {
    SingleShot,
    DoubleShotVertical,
    DoubleShotHorizontal,
    CornerShot,
    CrossShotDiagonal,
    CrossShotHorizontal,
    SeekerMissle
}

impl fmt::Display for Weapon {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Weapon::*;
        
        f.write_str(
            match self {
                &SingleShot => "1",
                &DoubleShotVertical => "2",
                &DoubleShotHorizontal => "3",
                &CornerShot => "4",
                &CrossShotDiagonal => "5",
                &CrossShotHorizontal => "6",
                &SeekerMissle => "7"
            }
        )
    }
}

impl Weapon {
    pub fn energy_per_round(map_size: u16) -> u16 {
        if map_size < 10 {
            2
        }
        else if map_size < 14 {
            3
        }
        else {
            4
        }
    }
    pub fn energy_cost(&self, map_size: u16) -> u16 {
        use Weapon::*;
        let epr = Weapon::energy_per_round(map_size);
        match self {
            &SingleShot => 1,
            &DoubleShotVertical | &DoubleShotHorizontal => 8*epr,
            &CornerShot => 10*epr,
            &CrossShotDiagonal => 12*epr,
            &CrossShotHorizontal => 14*epr,
            &SeekerMissle => 10*epr
        }
    }
    pub fn single_shot_rounds_to_ready(&self, current_energy: u16, map_size: u16) -> u16 {
        let single_shot_cost = Weapon::SingleShot.energy_cost(map_size);
        let energy_per_round = Weapon::energy_per_round(map_size) - single_shot_cost;
        let required_energy = self.energy_cost(map_size) - current_energy;
        //weird plus is to make the integer rounding up instead of down
        (required_energy + energy_per_round - 1) / energy_per_round
    }
}


#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum Ship {
    Battleship,
    Carrier,
    Cruiser,
    Destroyer,
    Submarine
}

impl fmt::Display for Ship {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Ship::*;
        
        f.write_str(
            match self {
                &Battleship => "Battleship",
                &Carrier => "Carrier",
                &Cruiser => "Cruiser",
                &Destroyer => "Destroyer",
                &Submarine => "Submarine"
            }
        )
    }
}

impl str::FromStr for Ship {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use Ship::*;
        
        match s {
            "Battleship" => Ok(Battleship),
            "Carrier" => Ok(Carrier),
            "Cruiser" => Ok(Cruiser),
            "Destroyer" => Ok(Destroyer),
            "Submarine" => Ok(Submarine),
            _ => Err(String::from("ship type is not known"))
        }
    }
}

impl Ship {
    pub fn length(&self) -> u16 {
        use Ship::*;
        
        match self {
            &Battleship => 4,
            &Carrier => 5,
            &Cruiser => 3,
            &Destroyer => 2,
            &Submarine => 3
        }
    }

    pub fn weapons(&self) -> Vec<Weapon> {
        use Ship::*;
        use Weapon::*;
        
        match self {
            &Battleship => vec!(SingleShot, CrossShotDiagonal),
            &Carrier => vec!(SingleShot, CornerShot),
            &Cruiser => vec!(SingleShot, CrossShotHorizontal),
            &Destroyer => vec!(SingleShot, DoubleShotVertical, DoubleShotHorizontal),
            &Submarine => vec!(SingleShot, SeekerMissle)
        }
    }

    pub fn all_types() -> Vec<Ship> {
        use Ship::*;
        
        vec!(
            Battleship,
            Carrier,
            Cruiser,
            Destroyer,
            Submarine
        )
    }
}