td_wavegen/
lib.rs

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
/// Simple type to differenciate mobs!
#[derive(Debug, Clone, PartialEq)]
pub struct MobType {
    /// Map the mob types in your game to different indexes!
    index: usize,
    /// The cost of each instance of this specific mob!
    cost: usize,
}

impl MobType {
    pub fn new(index: usize, cost: usize) -> Self {
        MobType {
            index: index,
            cost: cost,
        }
    }
}

#[derive(Debug, Clone)]
struct MobCostMap(Vec<(usize, usize)>);

impl From<&Vec<MobType>> for MobCostMap {
    fn from(mob_types: &Vec<MobType>) -> MobCostMap {
        let mut map_cost_map_contents = mob_types
            .iter()
            .map(|e| (e.cost, e.index))
            .collect::<Vec<(usize, usize)>>();
        map_cost_map_contents.sort_by(|a, b| a.0.cmp(&b.0));
        MobCostMap(map_cost_map_contents)
    }
}

/// A single wave, i.e. a set of mobs for a given max cost!
#[derive(Debug, PartialEq)]
pub struct Wave {
    /// Optional wave nr.!
    wave_number: Option<usize>,
    /// Total real cost of this wave. Might be a bit lower than the max cost if there is no mob to buy for the leftover budget!
    total_cost: usize,
    /// List of mobs for this wave!
    mobs: Vec<MobType>,
}

impl Wave {
    /// Generate a wave from a given maximum cost. This will "buy" mobs for this wave until it runs out of budget!
    pub fn gen_from_max_total_cost(mut max_total_cost: usize, mob_types: &Vec<MobType>) -> Wave {
        let mut mob_cost_map: MobCostMap = MobCostMap::from(mob_types);
        let mut total_cost: usize = 0;
        let mut mobs: Vec<MobType> = Vec::new();
        while max_total_cost > 0 || mob_cost_map.0.len() > 0 {
            if let Some(mob_cost) = mob_cost_map.0.last() {
                if max_total_cost >= mob_cost.0 {
                    max_total_cost -= mob_cost.0;
                    total_cost += mob_cost.0;
                    mobs.push(MobType {
                        index: mob_cost.1,
                        cost: mob_cost.0,
                    });
                } else {
                    mob_cost_map.0.remove(mob_cost_map.0.len() - 1);
                }
            } else {
                break;
            }
        }
        Wave {
            wave_number: None,
            total_cost: total_cost,
            mobs: mobs,
        }
    }
}

/// Main type to use. Creates multiple waves!
#[derive(Debug, Clone)]
pub struct Waves {
    /// Next wave number!
    next_wave: usize,
    /// Function that determines the budget of each wave!
    cost_function: fn(usize) -> usize,
    /// Registered mob types that the system can "buy" in a wave!
    mob_types: Vec<MobType>,
}

impl Waves {
    /// Waves builder. Best to use this!
    pub fn builder() -> WavesBuilder {
        WavesBuilder {
            next_wave: 1,
            mob_types: Vec::new(),
            cost_function: |wave_nr| wave_nr,
        }
    }
}

impl Iterator for Waves {
    type Item = Wave;
    fn next(&mut self) -> Option<Self::Item> {
        let max_total_cost: usize = (self.cost_function)(self.next_wave);
        let mut wave: Wave = Wave::gen_from_max_total_cost(max_total_cost, &self.mob_types);
        wave.wave_number = Some(self.next_wave);
        self.next_wave += 1;
        Some(wave)
    }
}

pub struct WavesBuilder {
    next_wave: usize,
    mob_types: Vec<MobType>,
    cost_function: fn(usize) -> usize,
}

impl WavesBuilder {
    /// Register a single mob type with the waves builder!
    pub fn add_mob_type(mut self, mob_type: MobType) -> WavesBuilder {
        self.mob_types.push(mob_type);
        self
    }
    /// Register a vec of mob types all at once!
    pub fn add_mob_types(mut self, mut mob_types: Vec<MobType>) -> WavesBuilder {
        self.mob_types.append(&mut mob_types);
        self
    }
    /// Use an already finished vec of mob types!
    pub fn with_mob_types(mut self, mob_types: Vec<MobType>) -> WavesBuilder {
        self.mob_types = mob_types;
        self
    }
    /// Specify cost function for the waves!
    pub fn with_cost_function(mut self, f: fn(usize) -> usize) -> WavesBuilder {
        self.cost_function = f;
        self
    }
    /// Usually 1, sets the starting wave!
    pub fn with_starting_wave(mut self, starting_wave: usize) -> WavesBuilder {
        self.next_wave = starting_wave;
        self
    }
    /// Creates a waves object and consumes the builder!
    pub fn build(self) -> Waves {
        Waves {
            next_wave: self.next_wave,
            cost_function: self.cost_function,
            mob_types: self.mob_types,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mob_cost_map_length() {
        let mob_type_a = MobType { index: 1, cost: 1 };
        let mob_type_b = MobType { index: 2, cost: 2 };
        let mob_type_c = MobType { index: 3, cost: 3 };
        let mob_types = vec![mob_type_a, mob_type_c, mob_type_b];
        let mob_cost_map = MobCostMap::from(&mob_types);
        println!("{:?}", mob_cost_map);
        assert_eq!(mob_cost_map.0.len(), 3);
    }

    #[test]
    fn gen_wave_from_max_total_cost_7_a() {
        let mob_type_a = MobType { index: 1, cost: 1 };
        let mob_type_b = MobType { index: 2, cost: 2 };
        let mob_type_c = MobType { index: 3, cost: 3 };
        let mob_types = vec![mob_type_a, mob_type_c, mob_type_b];
        let wave: Wave = Wave::gen_from_max_total_cost(7, &mob_types);
        println!("{:?}", wave);
        assert_eq!(wave.total_cost, 7);
        assert_eq!(wave.mobs.len(), 3);
    }

    #[test]
    fn gen_wave_from_max_total_cost_8() {
        let mob_type_a = MobType { index: 1, cost: 1 };
        let mob_type_b = MobType { index: 2, cost: 2 };
        let mob_type_c = MobType { index: 3, cost: 3 };
        let mob_types = vec![mob_type_a, mob_type_c, mob_type_b];
        let wave: Wave = Wave::gen_from_max_total_cost(8, &mob_types);
        println!("{:?}", wave);
        assert_eq!(wave.total_cost, 8);
        assert_eq!(wave.mobs.len(), 3);
    }

    #[test]
    fn gen_wave_from_max_total_cost_11() {
        let mob_type_a = MobType { index: 1, cost: 1 };
        let mob_type_b = MobType { index: 2, cost: 4 };
        let mob_types = vec![mob_type_b, mob_type_a];
        let wave: Wave = Wave::gen_from_max_total_cost(11, &mob_types);
        println!("{:?}", wave);
        assert_eq!(wave.total_cost, 11);
        assert_eq!(wave.mobs.len(), 5);
    }

    #[test]
    fn gen_wave_from_max_total_cost_7_b() {
        let mob_type_a = MobType { index: 1, cost: 2 };
        let mob_type_b = MobType { index: 2, cost: 4 };
        let mob_types = vec![mob_type_b, mob_type_a];
        let wave: Wave = Wave::gen_from_max_total_cost(7, &mob_types);
        println!("{:?}", wave);
        assert_eq!(wave.total_cost, 6);
        assert_eq!(wave.mobs.len(), 2);
    }

    #[test]
    fn roundtrip() {
        let mob_type_a = MobType { index: 1, cost: 1 };
        let mob_type_b = MobType { index: 2, cost: 3 };
        let mob_type_c = MobType { index: 3, cost: 5 };
        let mob_type_d = MobType { index: 4, cost: 7 };
        let mob_types = vec![mob_type_a, mob_type_c, mob_type_b, mob_type_d];

        let mob_cost_map = MobCostMap::from(&mob_types);
        assert_eq!(mob_cost_map.0, [(1, 1), (3, 2), (5, 3), (7, 4)]);
        println!("{:?}", mob_cost_map);

        let mut waves = Waves::builder()
            .with_mob_types(mob_types)
            .with_starting_wave(2)
            .with_cost_function(|wave| wave * 3)
            .build();
        println!("{:?}", waves);
        let wave_1 = waves.next();
        let wave_2 = waves.next();
        let wave_3 = waves.next();
        let wave_4 = waves.next();
        let wave_5 = waves.next();
        println!("{:?}", wave_1);
        println!("{:?}", wave_2);
        println!("{:?}", wave_3);
        println!("{:?}", wave_4);
        println!("{:?}", wave_5);
        let test_wave_5 = Some(Wave {
            wave_number: Some(6),
            total_cost: 18,
            mobs: vec![
                MobType { index: 4, cost: 7 },
                MobType { index: 4, cost: 7 },
                MobType { index: 2, cost: 3 },
                MobType { index: 1, cost: 1 },
            ],
        });
        assert_eq!(wave_5, test_wave_5);
    }
}