roguelike-game/src/random_table.rs

60 lines
1.3 KiB
Rust

use ::rltk::RandomNumberGenerator;
pub struct RandomEntry {
name: String,
weight: i32,
}
impl RandomEntry {
pub fn new<S: ToString>(name: S, weight: i32) -> RandomEntry {
RandomEntry {
name: name.to_string(),
weight,
}
}
}
#[derive(Default)]
pub struct RandomTable {
entries: Vec<RandomEntry>,
total_weight: i32,
}
impl RandomTable {
pub fn new() -> RandomTable {
RandomTable {
entries: Vec::new(),
total_weight: 0,
}
}
pub fn add<S: ToString>(mut self, name: S, weight: i32) -> Self {
if weight > 0 {
self.total_weight += weight;
self.entries.push(RandomEntry::new(name, weight));
}
self
}
pub fn roll(&self, rng: &mut RandomNumberGenerator) -> String {
if self.total_weight == 0 {
return "None".to_string();
}
let mut roll = rng.roll_dice(1, self.total_weight) - 1;
let mut index: usize = 0;
while roll > 0 {
if roll < self.entries[index].weight {
return self.entries[index].name.clone();
}
roll -= self.entries[index].weight;
index += 1;
}
"None".to_string()
}
}