mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Remove allocation in SpriteKind::collectible_id
.
This commit is contained in:
parent
b4c2bc14df
commit
836f10ae63
@ -239,7 +239,7 @@ fn loot_table(loot_table: &str) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let loot_table = "common.loot_tables.".to_owned() + loot_table;
|
||||
|
||||
let loot_table = Lottery::<LootSpec>::load_expect(&loot_table).read();
|
||||
let loot_table = Lottery::<LootSpec<String>>::load_expect(&loot_table).read();
|
||||
|
||||
for (i, (chance, item)) in loot_table.iter().enumerate() {
|
||||
let chance = if let Some((next_chance, _)) = loot_table.iter().nth(i + 1) {
|
||||
|
@ -418,7 +418,7 @@ fn loot_table(loot_table: &str) -> Result<(), Box<dyn Error>> {
|
||||
.map(|(i, x)| (x.to_string(), i))
|
||||
.collect();
|
||||
|
||||
let mut items = Vec::<(f32, LootSpec)>::new();
|
||||
let mut items = Vec::<(f32, LootSpec<String>)>::new();
|
||||
|
||||
for ref record in rdr.records().flatten() {
|
||||
let item = match record.get(headers["Kind"]).expect("No loot specifier") {
|
||||
|
@ -84,14 +84,14 @@ struct ProbabilityFile {
|
||||
}
|
||||
|
||||
impl assets::Asset for ProbabilityFile {
|
||||
type Loader = assets::LoadFrom<Vec<(f32, LootSpec)>, assets::RonLoader>;
|
||||
type Loader = assets::LoadFrom<Vec<(f32, LootSpec<String>)>, assets::RonLoader>;
|
||||
|
||||
const EXTENSION: &'static str = "ron";
|
||||
}
|
||||
|
||||
impl From<Vec<(f32, LootSpec)>> for ProbabilityFile {
|
||||
impl From<Vec<(f32, LootSpec<String>)>> for ProbabilityFile {
|
||||
#[allow(clippy::cast_precision_loss)]
|
||||
fn from(content: Vec<(f32, LootSpec)>) -> Self {
|
||||
fn from(content: Vec<(f32, LootSpec<String>)>) -> Self {
|
||||
Self {
|
||||
content: content
|
||||
.into_iter()
|
||||
@ -101,7 +101,7 @@ impl From<Vec<(f32, LootSpec)>> for ProbabilityFile {
|
||||
vec![(p0 * (a + b) as f32 / 2.0, asset)].into_iter()
|
||||
},
|
||||
LootSpec::LootTable(table_asset) => {
|
||||
let total = Lottery::<LootSpec>::load_expect(&table_asset)
|
||||
let total = Lottery::<LootSpec<String>>::load_expect(&table_asset)
|
||||
.read()
|
||||
.total();
|
||||
Self::load_expect_cloned(&table_asset)
|
||||
|
@ -131,7 +131,7 @@ impl EntityInfo {
|
||||
self = self.with_loot_drop(Item::new_from_asset_expect(&asset));
|
||||
},
|
||||
LootKind::LootTable(asset) => {
|
||||
let table = Lottery::<LootSpec>::load_expect(&asset);
|
||||
let table = Lottery::<LootSpec<String>>::load_expect(&asset);
|
||||
let drop = table.read().choose().to_item();
|
||||
self = self.with_loot_drop(drop);
|
||||
},
|
||||
@ -352,7 +352,7 @@ mod tests {
|
||||
LootKind::LootTable(asset) => {
|
||||
// we need to just load it check if it exists,
|
||||
// because all loot tables are tested in Lottery module
|
||||
let _ = Lottery::<LootSpec>::load_expect(&asset);
|
||||
let _ = Lottery::<LootSpec<String>>::load_expect(&asset);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -77,30 +77,30 @@ impl<T> Lottery<T> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
pub enum LootSpec {
|
||||
pub enum LootSpec<T: AsRef<str>> {
|
||||
/// Asset specifier
|
||||
Item(String),
|
||||
Item(T),
|
||||
/// Asset specifier, lower range, upper range
|
||||
ItemQuantity(String, u32, u32),
|
||||
ItemQuantity(T, u32, u32),
|
||||
/// Loot table
|
||||
LootTable(String),
|
||||
LootTable(T),
|
||||
}
|
||||
|
||||
impl LootSpec {
|
||||
impl<T: AsRef<str>> LootSpec<T> {
|
||||
pub fn to_item(&self) -> Item {
|
||||
match self {
|
||||
Self::Item(item) => Item::new_from_asset_expect(&item),
|
||||
Self::Item(item) => Item::new_from_asset_expect(item.as_ref()),
|
||||
Self::ItemQuantity(item, lower, upper) => {
|
||||
let range = *lower..=*upper;
|
||||
let quantity = thread_rng().gen_range(range);
|
||||
let mut item = Item::new_from_asset_expect(&item);
|
||||
let mut item = Item::new_from_asset_expect(item.as_ref());
|
||||
// TODO: Handle multiple of an item that is unstackable
|
||||
if item.set_amount(quantity).is_err() {
|
||||
warn!("Tried to set quantity on non stackable item");
|
||||
}
|
||||
item
|
||||
},
|
||||
Self::LootTable(table) => Lottery::<LootSpec>::load_expect(&table)
|
||||
Self::LootTable(table) => Lottery::<LootSpec<String>>::load_expect(table.as_ref())
|
||||
.read()
|
||||
.choose()
|
||||
.to_item(),
|
||||
@ -115,7 +115,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_loot_tables() {
|
||||
fn validate_table_contents(table: Lottery<LootSpec>) {
|
||||
fn validate_table_contents(table: Lottery<LootSpec<String>>) {
|
||||
for (_, item) in table.iter() {
|
||||
match item {
|
||||
LootSpec::Item(item) => {
|
||||
@ -137,14 +137,16 @@ mod tests {
|
||||
Item::new_from_asset_expect(&item);
|
||||
},
|
||||
LootSpec::LootTable(loot_table) => {
|
||||
let loot_table = Lottery::<LootSpec>::load_expect_cloned(&loot_table);
|
||||
let loot_table =
|
||||
Lottery::<LootSpec<String>>::load_expect_cloned(&loot_table);
|
||||
validate_table_contents(loot_table);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let loot_tables = assets::load_expect_dir::<Lottery<LootSpec>>("common.loot_tables", true);
|
||||
let loot_tables =
|
||||
assets::load_expect_dir::<Lottery<LootSpec<String>>>("common.loot_tables", true);
|
||||
for loot_table in loot_tables.iter() {
|
||||
validate_table_contents(loot_table.cloned());
|
||||
}
|
||||
|
@ -263,9 +263,9 @@ impl SpriteKind {
|
||||
}
|
||||
|
||||
/// What loot table does collecting this sprite draw from?
|
||||
pub fn collectible_id(&self) -> Option<LootSpec> {
|
||||
let item = |id: &str| LootSpec::Item(id.to_string());
|
||||
let table = |id: &str| LootSpec::LootTable(id.to_string());
|
||||
pub fn collectible_id(&self) -> Option<LootSpec<&'static str>> {
|
||||
let item = |id: &'static str| LootSpec::Item(id);
|
||||
let table = |id: &'static str| LootSpec::LootTable(id);
|
||||
Some(match self {
|
||||
SpriteKind::Apple => item("common.items.food.apple"),
|
||||
SpriteKind::Mushroom => item("common.items.food.mushroom"),
|
||||
|
@ -374,7 +374,7 @@ pub fn handle_destroy(server: &mut Server, entity: EcsEntity, cause: HealthSourc
|
||||
// Decide for a loot drop before turning into a lootbag
|
||||
let old_body = state.ecs().write_storage::<Body>().remove(entity);
|
||||
let lottery = || {
|
||||
Lottery::<LootSpec>::load_expect(match old_body {
|
||||
Lottery::<LootSpec<String>>::load_expect(match old_body {
|
||||
Some(common::comp::Body::Humanoid(_)) => "common.loot_tables.creature.humanoid",
|
||||
Some(common::comp::Body::QuadrupedSmall(quadruped_small)) => {
|
||||
match quadruped_small.species {
|
||||
|
Loading…
Reference in New Issue
Block a user