We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rand::distributions::Weighted
https://docs.rs/rand/0.6.5/rand/distributions/struct.Weighted.html
We use it here:
https://tomassedovic.github.io/roguelike-tutorial/part-12-monster-item-progression.html
Reported via email by Jordan Selanders [email protected] who also sent the following fix:
fn place_objects(room: Rect, map: &Map, objects: &mut Vec<Object>) { let num_monsters = rand::thread_rng().gen_range(0, MAX_ROOM_MONSTERS + 1); let monster_chances = ["orc", "troll"]; let monster_weights = [2, 1]; let monster_dist = WeightedIndex::new(&monster_weights).unwrap(); let mut rng = rand::thread_rng(); for _ in 0..num_monsters { let x = rand::thread_rng().gen_range(room.x1 + 1, room.x2); let y = rand::thread_rng().gen_range(room.y1 + 1, room.y2); let mut monster = match monster_chances[monster_dist.sample(&mut rng)] { "orc" => { let mut orc = Object::new(x, y, 'o', "orc", colors::DESATURATED_GREEN, true); orc.fighter = Some(Fighter { max_hp: 10, hp: 10, defense: 0, power: 3, on_death: DeathCallback::Monster, xp: 10 }); orc.ai = Some(Ai::Basic); orc } "troll" => { let mut troll = Object::new(x, y, 'T', "troll", colors::DARKER_GREEN, true); troll.fighter = Some(Fighter { max_hp: 16, hp: 16, defense: 1, power: 4, on_death: DeathCallback::Monster, xp: 20 }); troll.ai = Some(Ai::Basic); troll } _ => unreachable!(), }; monster.alive = true; objects.push(monster); } let num_items = rand::thread_rng().gen_range(0, MAX_ROOM_ITEMS + 1); let item_chances = [Item::Heal, Item::Lightning, Item::Fireball, Item::Confuse]; let item_weights = [7, 1, 1, 1]; let item_dist = WeightedIndex::new(&item_weights).unwrap(); for _ in 0..num_items { let x = rand::thread_rng().gen_range(room.x1 + 1, room.x2); let y = rand::thread_rng().gen_range(room.y1 + 1, room.y2); if !is_blocked(x, y, map, objects) { let mut item = match item_chances[item_dist.sample(&mut rng)] { Item::Heal => { let mut object = Object::new(x, y, '!', "healing potion", colors::VIOLET, false); object.item = Some(Item::Heal); object } Item::Lightning => { let mut object = Object::new(x, y, '#', "scroll of lightning bolt", colors::LIGHT_YELLOW, false); object.item = Some(Item::Lightning); object } Item::Fireball => { let mut object = Object::new(x, y, '#', "scroll of fireball", colors::LIGHT_YELLOW, false); object.item = Some(Item::Fireball); object } Item::Confuse => { let mut object = Object::new(x, y, '#', "scroll of confusion", colors::LIGHT_YELLOW, false); object.item = Some(Item::Confuse); object } }; item.always_visible = true; objects.push(item); } } }
The text was updated successfully, but these errors were encountered:
Here is the solution I came up with using WeightedIndex when I hit that issue while following the tutorial.
WeightedIndex
Sorry, something went wrong.
No branches or pull requests
https://docs.rs/rand/0.6.5/rand/distributions/struct.Weighted.html
We use it here:
https://tomassedovic.github.io/roguelike-tutorial/part-12-monster-item-progression.html
Reported via email by Jordan Selanders [email protected] who also sent the following fix:
The text was updated successfully, but these errors were encountered: