You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.8 KiB

use once_cell::sync::Lazy;
use rand::{distributions::WeightedIndex, prelude::Distribution, Rng};
use valence::prelude::*;
pub struct LootTable {
items: Vec<ItemKind>,
index: WeightedIndex<f64>,
}
impl LootTable {
pub fn new(table: Vec<(ItemKind, f64)>) -> LootTable {
let items = table.iter().map(|item| item.0);
let weights = table.iter().map(|item| item.1);
LootTable {
items: items.collect(),
index: WeightedIndex::new(weights).unwrap(),
}
}
pub fn get_random_item(&self) -> ItemKind {
let mut rng = rand::thread_rng();
self.items[self.index.sample(&mut rng)]
}
pub fn get_random_inventory(&self, min: i64, max: i64) -> Inventory {
let mut rng = rand::thread_rng();
let mut inventory = Inventory::new(InventoryKind::Generic9x3);
for _ in 0..rng.gen_range(min..max) {
let mut random_slot_idx = rng.gen_range(0..27);
while let Some(_) = inventory.slot(random_slot_idx) {
random_slot_idx = rng.gen_range(0..27)
}
inventory.set_slot(
random_slot_idx,
ItemStack::new(self.get_random_item(), 1, None),
)
}
inventory
}
}
pub static EARLY_GAME: Lazy<LootTable> = Lazy::new(|| {
let table = vec![
// Armor
(ItemKind::LeatherHelmet, 20.0),
(ItemKind::LeatherChestplate, 16.5),
(ItemKind::LeatherLeggings, 16.0),
(ItemKind::LeatherBoots, 25.0),
// Attacking items (swords/axes)
(ItemKind::WoodenSword, 20.0),
(ItemKind::WoodenAxe, 10.0),
(ItemKind::StoneSword, 15.0),
(ItemKind::StoneAxe, 7.5),
(ItemKind::IronSword, 5.0),
(ItemKind::IronAxe, 2.5),
];
LootTable::new(table)
});