Merge branch 'Animals_one_slot_only' into 'master'

Change animal species to have only one inventory slot

See merge request veloren/veloren!3405
This commit is contained in:
Marcel 2022-05-29 23:53:00 +00:00
commit 43c17ce312
13 changed files with 55 additions and 36 deletions

View File

@ -8,6 +8,7 @@ use vek::Vec3;
use crate::{
comp::{
body::Body,
inventory::{
item::{tool::AbilityMap, ItemDef, ItemKind, MaterialStatManifest, TagExampleInfo},
loadout::Loadout,
@ -80,9 +81,19 @@ impl InventorySortOrder {
/// that contains items etc) must first ensure items are unloaded from the item.
/// This is handled in `inventory\slot.rs`
impl Inventory {
pub fn new_empty() -> Inventory { Self::new_with_loadout(LoadoutBuilder::empty().build()) }
pub fn with_empty() -> Inventory {
Self::with_loadout_humanoid(LoadoutBuilder::empty().build())
}
pub fn new_with_loadout(loadout: Loadout) -> Inventory {
pub fn with_loadout(loadout: Loadout, body: Body) -> Inventory {
if let Body::Humanoid(_) = body {
Self::with_loadout_humanoid(loadout)
} else {
Self::with_loadout_animal(loadout)
}
}
pub fn with_loadout_humanoid(loadout: Loadout) -> Inventory {
Inventory {
next_sort_order: InventorySortOrder::Name,
loadout,
@ -90,6 +101,14 @@ impl Inventory {
}
}
pub fn with_loadout_animal(loadout: Loadout) -> Inventory {
Inventory {
next_sort_order: InventorySortOrder::Name,
loadout,
slots: vec![None; 1],
}
}
/// Total number of slots in in the inventory.
pub fn capacity(&self) -> usize { self.slots().count() }

View File

@ -124,7 +124,7 @@ fn push_all_unique_empty() {
fn free_slots_minus_equipped_item_items_only_present_in_equipped_bag_slots() {
let msm = &MaterialStatManifest::load().read();
let ability_map = &AbilityMap::load().read();
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(18);
let bag1_slot = EquipSlot::Armor(ArmorSlot::Bag1);
@ -144,7 +144,7 @@ fn free_slots_minus_equipped_item_items_only_present_in_equipped_bag_slots() {
fn free_slots_minus_equipped_item() {
let msm = &MaterialStatManifest::load().read();
let ability_map = &AbilityMap::load().read();
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(18);
let bag1_slot = EquipSlot::Armor(ArmorSlot::Bag1);
@ -166,7 +166,7 @@ fn free_slots_minus_equipped_item() {
#[test]
fn get_slot_range_for_equip_slot() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(18);
let bag1_slot = EquipSlot::Armor(ArmorSlot::Bag1);
inv.loadout.swap(bag1_slot, Some(bag));
@ -196,7 +196,7 @@ fn can_swap_equipped_bag_into_empty_inv_slot(
inv_slot_id: InvSlotId,
expected_result: bool,
) {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
inv.replace_loadout_item(EquipSlot::Armor(ArmorSlot::Bag1), Some(get_test_bag(18)));
@ -209,7 +209,7 @@ fn can_swap_equipped_bag_into_empty_inv_slot(
#[test]
fn can_swap_equipped_bag_into_only_empty_slot_provided_by_itself_should_return_true() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
inv.replace_loadout_item(EquipSlot::Armor(ArmorSlot::Bag1), Some(get_test_bag(18)));
@ -224,7 +224,7 @@ fn can_swap_equipped_bag_into_only_empty_slot_provided_by_itself_should_return_t
fn unequip_items_both_hands() {
let msm = &MaterialStatManifest::load().read();
let ability_map = &AbilityMap::load().read();
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let sword = Item::new_from_asset_expect("common.items.weapons.sword.starter");
@ -267,7 +267,7 @@ fn equip_replace_already_equipped_item() {
"common.items.armor.misc.foot.sandals",
));
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
inv.push(boots.duplicate(ability_map, msm)).unwrap();
inv.replace_loadout_item(
EquipSlot::Armor(ArmorSlot::Feet),
@ -294,7 +294,7 @@ fn equip_replace_already_equipped_item() {
/// after equipping it (because the equipped bag is larger)
#[test]
fn equip_equipping_smaller_bag_from_last_slot_of_big_bag() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
const LARGE_BAG_ID: &str = "common.items.testing.test_bag_18_slot";
let small_bag = get_test_bag(9);
@ -322,7 +322,7 @@ fn equip_equipping_smaller_bag_from_last_slot_of_big_bag() {
#[test]
fn unequip_unequipping_bag_into_its_own_slot_with_no_other_free_slots_returns_one_item() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(9);
assert!(
@ -350,7 +350,7 @@ fn unequip_unequipping_bag_into_its_own_slot_with_no_other_free_slots_returns_on
fn equip_one_bag_equipped_equip_second_bag() {
let msm = &MaterialStatManifest::load().read();
let ability_map = &AbilityMap::load().read();
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(9);
assert!(
@ -371,7 +371,7 @@ fn equip_one_bag_equipped_equip_second_bag() {
#[test]
fn free_after_swap_equipped_item_has_more_slots() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(18);
assert!(
@ -394,7 +394,7 @@ fn free_after_swap_equipped_item_has_more_slots() {
#[test]
fn free_after_swap_equipped_item_has_less_slots() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(9);
assert!(
@ -417,7 +417,7 @@ fn free_after_swap_equipped_item_has_less_slots() {
#[test]
fn free_after_swap_equipped_item_with_slots_swapped_with_empty_inv_slot() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let bag = get_test_bag(9);
assert!(
@ -437,7 +437,7 @@ fn free_after_swap_equipped_item_with_slots_swapped_with_empty_inv_slot() {
#[test]
fn free_after_swap_inv_item_with_slots_swapped_with_empty_equip_slot() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
inv.push(get_test_bag(9)).unwrap();
@ -452,7 +452,7 @@ fn free_after_swap_inv_item_with_slots_swapped_with_empty_equip_slot() {
#[test]
fn free_after_swap_inv_item_without_slots_swapped_with_empty_equip_slot() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let boots = Item::new_from_asset_expect("common.items.testing.test_boots");
inv.push(boots).unwrap();
@ -471,7 +471,7 @@ fn free_after_swap_inv_item_without_slots_swapped_with_empty_equip_slot() {
// provide slots.
#[test]
fn backpack_crash() {
let mut inv = Inventory::new_empty();
let mut inv = Inventory::with_empty();
let backpack = Item::new_from_asset_expect("common.items.armor.misc.back.backpack");
inv.loadout

View File

@ -179,7 +179,7 @@ impl CharacterBehavior for Data {
skill_set,
health,
poise: comp::Poise::new(body),
inventory: comp::Inventory::new_with_loadout(loadout),
inventory: comp::Inventory::with_loadout(loadout, body),
body,
agent: Some(
comp::Agent::from_body(&body)

View File

@ -50,7 +50,7 @@ pub fn create_character(
.active_mainhand(character_mainhand.map(|x| Item::new_from_asset_expect(&x)))
.active_offhand(character_offhand.map(|x| Item::new_from_asset_expect(&x)))
.build();
let mut inventory = Inventory::new_with_loadout(loadout);
let mut inventory = Inventory::with_loadout_humanoid(loadout);
let stats = Stats::new(character_alias.to_string());
let skill_set = SkillSet::default();
// Default items for new characters

View File

@ -1201,7 +1201,7 @@ fn handle_spawn(
let body = body();
let loadout = LoadoutBuilder::from_default(&body).build();
let inventory = Inventory::new_with_loadout(loadout);
let inventory = Inventory::with_loadout(loadout, body);
let mut entity_base = server
.state
@ -1293,7 +1293,7 @@ fn handle_spawn_training_dummy(
skill_set,
Some(health),
poise,
Inventory::new_empty(),
Inventory::with_empty(),
body,
)
.with(comp::Vel(vel))

View File

@ -447,7 +447,7 @@ pub fn handle_possess(server: &mut Server, possessor_uid: Uid, possessee_uid: Ui
let mut inventory = inventories
.entry(possessee)
.expect("Nobody has &mut World, so there's no way to delete an entity.")
.or_insert(Inventory::new_empty());
.or_insert(Inventory::with_empty());
let debug_item = comp::Item::new_from_asset_expect("common.items.debug.admin_stick");
if let item::ItemKind::Tool(_) = &*debug_item.kind() {

View File

@ -428,12 +428,12 @@ mod tests {
let player: EcsEntity = mockworld
.create_entity()
.with(Inventory::new_empty())
.with(Inventory::with_empty())
.build();
let merchant: EcsEntity = mockworld
.create_entity()
.with(Inventory::new_empty())
.with(Inventory::with_empty())
.build();
{

View File

@ -339,7 +339,7 @@ pub fn load_character_list(player_uuid_: &str, connection: &Connection) -> Chara
Ok(CharacterItem {
character: char,
body: char_body,
inventory: Inventory::new_with_loadout(loadout),
inventory: Inventory::with_loadout_humanoid(loadout),
})
})
.collect()

View File

@ -349,7 +349,7 @@ pub fn convert_inventory_from_database_items(
// inventory at the correct position.
//
let loadout = convert_loadout_from_database_items(loadout_container_id, loadout_items)?;
let mut inventory = Inventory::new_with_loadout(loadout);
let mut inventory = Inventory::with_loadout_humanoid(loadout);
let mut item_indices = HashMap::new();
// In order to items with components to properly load, it is important that this

View File

@ -306,7 +306,7 @@ impl StateExt for State {
.with(body)
.with(comp::Scale(comp::ship::AIRSHIP_SCALE))
.with(comp::Controller::default())
.with(comp::inventory::Inventory::new_empty())
.with(comp::inventory::Inventory::with_empty())
.with(comp::CharacterState::default())
// TODO: some of these are required in order for the character_behavior system to
// recognize a possesed airship; that system should be refactored to use `.maybe()`
@ -597,7 +597,7 @@ impl StateExt for State {
comp::SkillSet::default(),
Some(comp::Health::new(body, DEFAULT_PET_HEALTH_LEVEL)),
Poise::new(body),
Inventory::new_empty(),
Inventory::with_empty(),
body,
)
.with(comp::Scale(1.0))

View File

@ -389,7 +389,7 @@ impl NpcData {
loadout_builder = loadout_builder.with_creator(make_loadout, economy.as_ref());
}
let loadout = loadout_builder.build();
let mut inventory = comp::inventory::Inventory::new_with_loadout(loadout);
let mut inventory = comp::inventory::Inventory::with_loadout(loadout, body);
for (num, mut item) in items {
if let Err(e) = item.set_amount(num) {
tracing::warn!(

View File

@ -17,7 +17,7 @@ fn maps_wield_while_equipping() {
"common.items.weapons.axe.starter_axe",
)))
.build();
let inventory = Inventory::new_with_loadout(loadout);
let inventory = Inventory::with_loadout_humanoid(loadout);
let result = CombatEventMapper::map_event(
&CharacterState::Equipping(states::equipping::Data {
@ -45,7 +45,7 @@ fn maps_unwield() {
"common.items.weapons.bow.starter",
)))
.build();
let inventory = Inventory::new_with_loadout(loadout);
let inventory = Inventory::with_loadout_humanoid(loadout);
let result = CombatEventMapper::map_event(
&CharacterState::default(),
@ -67,7 +67,7 @@ fn maps_basic_melee() {
"common.items.weapons.axe.starter_axe",
)))
.build();
let inventory = Inventory::new_with_loadout(loadout);
let inventory = Inventory::with_loadout_humanoid(loadout);
let result = CombatEventMapper::map_event(
&CharacterState::BasicMelee(states::basic_melee::Data {
@ -118,7 +118,7 @@ fn matches_ability_stage() {
"common.items.weapons.sword.starter",
)))
.build();
let inventory = Inventory::new_with_loadout(loadout);
let inventory = Inventory::with_loadout_humanoid(loadout);
let result = CombatEventMapper::map_event(
&CharacterState::ComboMelee(states::combo_melee::Data {
@ -180,7 +180,7 @@ fn ignores_different_ability_stage() {
"common.items.weapons.axe.starter_axe",
)))
.build();
let inventory = Inventory::new_with_loadout(loadout);
let inventory = Inventory::with_loadout_humanoid(loadout);
let result = CombatEventMapper::map_event(
&CharacterState::ComboMelee(states::combo_melee::Data {

View File

@ -202,7 +202,7 @@ impl Mode {
.active_offhand(offhand.map(Item::new_from_asset_expect))
.build();
let inventory = Box::new(Inventory::new_with_loadout(loadout));
let inventory = Box::new(Inventory::with_loadout_humanoid(loadout));
Self::CreateOrEdit {
name,