|
|
@ -1,37 +1,37 @@ |
|
|
|
#![allow(dead_code)]
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use libpso::character::character::InventoryItem;
|
|
|
|
use std::collections::{HashMap, BTreeMap};
|
|
|
|
use libpso::character::character;//::InventoryItem;
|
|
|
|
use crate::entity::gateway::EntityGateway;
|
|
|
|
use crate::entity::character::CharacterEntity;
|
|
|
|
use crate::entity::item::{ItemEntity, ItemDetail, ItemLocation};
|
|
|
|
use crate::entity::item::{ItemEntityId, ItemEntity, ItemDetail, ItemLocation};
|
|
|
|
use crate::entity::item::{Meseta, NewItemEntity};
|
|
|
|
use crate::entity::item::tool::Tool;
|
|
|
|
use crate::ship::map::MapArea;
|
|
|
|
use crate::ship::drops::{ItemDrop, ItemDropType};
|
|
|
|
use crate::ship::ship::ShipError;
|
|
|
|
use crate::ship::ship::ClientState;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum ItemInstance {
|
|
|
|
Individual(ItemEntity),
|
|
|
|
Stacked(Vec<ItemEntity>),
|
|
|
|
Meseta(Meseta),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
|
|
|
pub struct ActiveItemId(pub u32);
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum ActiveItemEntityId {
|
|
|
|
Individual(ItemEntityId),
|
|
|
|
Stacked(Vec<ItemEntityId>),
|
|
|
|
Meseta(Meseta),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ActiveItem {
|
|
|
|
pub id: ActiveItemId,
|
|
|
|
item: ItemInstance,
|
|
|
|
enum HeldItemType {
|
|
|
|
Individual(ItemDetail),
|
|
|
|
Stacked(Tool, usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActiveItem {
|
|
|
|
impl HeldItemType {
|
|
|
|
pub fn as_client_bytes(&self) -> [u8; 16] {
|
|
|
|
match &self.item {
|
|
|
|
ItemInstance::Individual(i) => {
|
|
|
|
match &i.item {
|
|
|
|
match self {
|
|
|
|
HeldItemType::Individual(item) => {
|
|
|
|
match &item {
|
|
|
|
ItemDetail::Weapon(w) => w.as_bytes(),
|
|
|
|
ItemDetail::Armor(a) => a.as_bytes(),
|
|
|
|
ItemDetail::Shield(s) => s.as_bytes(),
|
|
|
@ -41,328 +41,278 @@ impl ActiveItem { |
|
|
|
ItemDetail::Mag(m) => m.as_bytes(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ItemInstance::Stacked(i) => {
|
|
|
|
let len = i.len();
|
|
|
|
match &i[0].item {
|
|
|
|
ItemDetail::Tool(t) => t.as_stacked_bytes(len),
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
HeldItemType::Stacked(tool, count) => {
|
|
|
|
tool.as_stacked_bytes(*count)
|
|
|
|
},
|
|
|
|
ItemInstance::Meseta(m) => {
|
|
|
|
m.as_bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ActiveInventory(Vec<ActiveItem>);
|
|
|
|
|
|
|
|
impl ActiveInventory {
|
|
|
|
pub fn as_client_inventory_items(&self) -> [InventoryItem; 30] {
|
|
|
|
self.0.iter()
|
|
|
|
.enumerate()
|
|
|
|
.fold([InventoryItem::default(); 30], |mut inventory, (index, item)| {
|
|
|
|
let bytes = item.as_client_bytes();
|
|
|
|
inventory[index].data1.copy_from_slice(&bytes[0..12]);
|
|
|
|
inventory[index].data2.copy_from_slice(&bytes[12..16]);
|
|
|
|
inventory[index].item_id = item.id.0;
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InventoryItem {
|
|
|
|
id: ActiveItemId,
|
|
|
|
item: HeldItemType,
|
|
|
|
//slot: usize,
|
|
|
|
equipped: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
// does this do anything?
|
|
|
|
inventory[index].equipped = match item.item {
|
|
|
|
ItemInstance::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 1,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
// because this actually equips the item
|
|
|
|
inventory[index].flags |= match item.item {
|
|
|
|
ItemInstance::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 8,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
inventory
|
|
|
|
})
|
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BankItem {
|
|
|
|
id: ActiveItemId,
|
|
|
|
item: HeldItemType,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FloorItemType {
|
|
|
|
Individual(ItemDetail),
|
|
|
|
Stacked(Tool, usize),
|
|
|
|
Meseta(Meseta),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inventory_item_index(item: &ItemInstance) -> usize {
|
|
|
|
match item {
|
|
|
|
ItemInstance::Individual(i) => {
|
|
|
|
match i.location {
|
|
|
|
ItemLocation::Inventory{index, ..} => index,
|
|
|
|
_ => panic!()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ItemInstance::Stacked(i) => {
|
|
|
|
match i[0].location {
|
|
|
|
ItemLocation::Inventory{index, ..} => index,
|
|
|
|
_ => panic!()
|
|
|
|
impl FloorItemType {
|
|
|
|
pub fn as_client_bytes(&self) -> [u8; 16] {
|
|
|
|
match self {
|
|
|
|
FloorItemType::Individual(item) => {
|
|
|
|
match &item {
|
|
|
|
ItemDetail::Weapon(w) => w.as_bytes(),
|
|
|
|
ItemDetail::Armor(a) => a.as_bytes(),
|
|
|
|
ItemDetail::Shield(s) => s.as_bytes(),
|
|
|
|
ItemDetail::Unit(u) => u.as_bytes(),
|
|
|
|
ItemDetail::Tool(t) => t.as_individual_bytes(),
|
|
|
|
ItemDetail::TechniqueDisk(d) => d.as_bytes(),
|
|
|
|
ItemDetail::Mag(m) => m.as_bytes(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
FloorItemType::Stacked(tool, count) => {
|
|
|
|
tool.as_stacked_bytes(*count)
|
|
|
|
},
|
|
|
|
FloorItemType::Meseta(m) => {
|
|
|
|
m.as_bytes()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ActiveItemOnFloor {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FloorItem {
|
|
|
|
pub id: ActiveItemId,
|
|
|
|
pub item: FloorItemType,
|
|
|
|
pub map_area: MapArea,
|
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
pub z: f32,
|
|
|
|
pub item: ActiveItem,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stack_items(items: Vec<ItemEntity>) -> Vec<ItemInstance> {
|
|
|
|
let mut stacks = HashMap::new();
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum InventoryError {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CharacterInventory(Vec<InventoryItem>);
|
|
|
|
|
|
|
|
impl CharacterInventory {
|
|
|
|
pub fn as_client_inventory_items(&self) -> [character::InventoryItem; 30] {
|
|
|
|
self.0.iter()
|
|
|
|
.enumerate()
|
|
|
|
.fold([character::InventoryItem::default(); 30], |mut inventory, (slot, item)| {
|
|
|
|
let bytes = item.item.as_client_bytes();
|
|
|
|
inventory[slot].data1.copy_from_slice(&bytes[0..12]);
|
|
|
|
inventory[slot].data2.copy_from_slice(&bytes[12..16]);
|
|
|
|
inventory[slot].item_id = item.id.0;
|
|
|
|
// does this do anything?
|
|
|
|
inventory[slot].equipped = if item.equipped { 1 } else { 0 };
|
|
|
|
// because this actually equips the item
|
|
|
|
inventory[slot].flags |= if item.equipped { 8 } else { 0 };
|
|
|
|
inventory
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for item in items {
|
|
|
|
stacks.entry(item.item.item_type()).or_insert(Vec::new()).push(item);
|
|
|
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<usize, InventoryError> {
|
|
|
|
self.0.push(item);
|
|
|
|
Ok(self.count() - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
stacks.into_iter()
|
|
|
|
.map(|(_, items)| {
|
|
|
|
match items[0].item.is_stackable() {
|
|
|
|
true => {
|
|
|
|
vec![ItemInstance::Stacked(items)]
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
items.into_iter().map(|i| {
|
|
|
|
ItemInstance::Individual(i)
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.collect()
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ActiveBank([Option<ActiveItemId>; 200]);
|
|
|
|
|
|
|
|
pub struct ActiveItemDatabase {
|
|
|
|
id: u32,
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ItemManagerError {
|
|
|
|
EntityGatewayError,
|
|
|
|
CouldNotAddToInventory(FloorItem),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ItemManager {
|
|
|
|
id: usize,
|
|
|
|
active_to_entity: HashMap<ActiveItemId, ActiveItemEntityId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActiveItemDatabase {
|
|
|
|
pub fn new() -> ActiveItemDatabase {
|
|
|
|
ActiveItemDatabase {
|
|
|
|
impl ItemManager {
|
|
|
|
pub fn new() -> ItemManager {
|
|
|
|
ItemManager {
|
|
|
|
id: 0,
|
|
|
|
active_to_entity: HashMap::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn activate_item(&mut self, item: ItemInstance) -> ActiveItem {
|
|
|
|
fn next_id(&mut self) -> ActiveItemId {
|
|
|
|
self.id += 1;
|
|
|
|
ActiveItem {
|
|
|
|
id: ActiveItemId(self.id),
|
|
|
|
item: item,
|
|
|
|
}
|
|
|
|
ActiveItemId(self.id as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: deactivate item
|
|
|
|
|
|
|
|
pub fn get_character_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> ActiveInventory {
|
|
|
|
pub fn get_character_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> CharacterInventory {
|
|
|
|
let items = entity_gateway.get_items_by_character(&character);
|
|
|
|
let inventory_items = items.into_iter()
|
|
|
|
.filter(|item| {
|
|
|
|
.filter_map(|item| {
|
|
|
|
match item.location {
|
|
|
|
ItemLocation::Inventory{..} => true,
|
|
|
|
_ => false,
|
|
|
|
ItemLocation::Inventory{slot, equipped, ..} => Some((item.id, item.item, slot, equipped)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fold(BTreeMap::new(), |mut acc, (id, item, slot, equipped)| {
|
|
|
|
if item.is_stackable() {
|
|
|
|
if let ItemDetail::Tool(tool) = item {
|
|
|
|
let stacked = acc.entry(slot).or_insert((HeldItemType::Stacked(tool, 0), ActiveItemEntityId::Stacked(Vec::new()), false));
|
|
|
|
if let HeldItemType::Stacked(_, ref mut item_count) = stacked.0 {
|
|
|
|
*item_count += 1;
|
|
|
|
}
|
|
|
|
if let ActiveItemEntityId::Stacked(ref mut id_list) = stacked.1 {
|
|
|
|
id_list.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.insert(slot, (HeldItemType::Individual(item), ActiveItemEntityId::Individual(id), equipped));
|
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
let mut stacked = stack_items(inventory_items);
|
|
|
|
stacked.sort_by(|a, b| {
|
|
|
|
inventory_item_index(a).partial_cmp(&inventory_item_index(b)).unwrap()
|
|
|
|
});
|
|
|
|
let activated = stacked.into_iter().map(|i| self.activate_item(i));
|
|
|
|
ActiveInventory(activated.take(30).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn activate_item_drop<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, item_drop: ItemDrop) -> Result<ActiveItemOnFloor, ShipError> {
|
|
|
|
let item_detail = match item_drop.item {
|
|
|
|
ItemDropType::Weapon(w) => Some(ItemDetail::Weapon(w)),
|
|
|
|
ItemDropType::Armor(w) => Some(ItemDetail::Armor(w)),
|
|
|
|
ItemDropType::Shield(w) => Some(ItemDetail::Shield(w)),
|
|
|
|
ItemDropType::Unit(w) => Some(ItemDetail::Unit(w)),
|
|
|
|
ItemDropType::Tool(w) => Some(ItemDetail::Tool(w)),
|
|
|
|
ItemDropType::TechniqueDisk(w) => Some(ItemDetail::TechniqueDisk(w)),
|
|
|
|
ItemDropType::Mag(w) => Some(ItemDetail::Mag(w)),
|
|
|
|
ItemDropType::Meseta(_) => None
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_slot, (held_item, entity_id, equipped))| {
|
|
|
|
let id = self.next_id();
|
|
|
|
self.active_to_entity.insert(id, entity_id);
|
|
|
|
InventoryItem {
|
|
|
|
id: id,
|
|
|
|
item: held_item,
|
|
|
|
equipped: equipped,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
CharacterInventory(inventory_items.take(30).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn drop_item_on_local_floor<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, item_drop: ItemDrop) -> Result<FloorItem, ItemManagerError> {
|
|
|
|
let item = match item_drop.item {
|
|
|
|
ItemDropType::Weapon(w) => FloorItemType::Individual(ItemDetail::Weapon(w)),
|
|
|
|
ItemDropType::Armor(w) => FloorItemType::Individual(ItemDetail::Armor(w)),
|
|
|
|
ItemDropType::Shield(w) => FloorItemType::Individual(ItemDetail::Shield(w)),
|
|
|
|
ItemDropType::Unit(w) => FloorItemType::Individual(ItemDetail::Unit(w)),
|
|
|
|
ItemDropType::TechniqueDisk(w) => FloorItemType::Individual(ItemDetail::TechniqueDisk(w)),
|
|
|
|
ItemDropType::Mag(w) => FloorItemType::Individual(ItemDetail::Mag(w)),
|
|
|
|
ItemDropType::Tool(w) => FloorItemType::Individual(ItemDetail::Tool(w)),
|
|
|
|
//ItemDropType::Tool(t) if t.is_stackable() => FloorItemType::Stacked(t, ),
|
|
|
|
//ItemDropType::Tool(t) if !t.is_stackable() => FloorItemType::Individual(ItemDetail::Tool(w)),
|
|
|
|
ItemDropType::Meseta(m) => FloorItemType::Meseta(Meseta(m))
|
|
|
|
};
|
|
|
|
let item_instance = match item_detail {
|
|
|
|
Some(item) => {
|
|
|
|
let item_entity = entity_gateway.create_item(NewItemEntity {
|
|
|
|
item: item,
|
|
|
|
location: ItemLocation::Floor {
|
|
|
|
|
|
|
|
let active_entity_ids = match &item {
|
|
|
|
FloorItemType::Individual(i) => {
|
|
|
|
let entity = entity_gateway.create_item(NewItemEntity {
|
|
|
|
item: i.clone(),
|
|
|
|
location: ItemLocation::LocalFloor {
|
|
|
|
character_id: character.id,
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}
|
|
|
|
}).unwrap();
|
|
|
|
stack_items(vec![item_entity]).pop().ok_or(ShipError::ItemError)?
|
|
|
|
}).ok_or(ItemManagerError::EntityGatewayError)?;
|
|
|
|
ActiveItemEntityId::Individual(entity.id)
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let meseta = match item_drop.item {
|
|
|
|
ItemDropType::Meseta(m) => m,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
ItemInstance::Meseta(Meseta(meseta))
|
|
|
|
}
|
|
|
|
FloorItemType::Stacked(tool, count) => {
|
|
|
|
let entities = (0..*count).map(|_| {
|
|
|
|
entity_gateway.create_item(NewItemEntity {
|
|
|
|
item: ItemDetail::Tool(*tool),
|
|
|
|
location: ItemLocation::LocalFloor {
|
|
|
|
character_id: character.id,
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}
|
|
|
|
})})
|
|
|
|
.map(|entity| -> Result<ItemEntityId, ItemManagerError> {
|
|
|
|
let e = entity.ok_or(ItemManagerError::EntityGatewayError)?;
|
|
|
|
Ok(e.id)
|
|
|
|
});
|
|
|
|
ActiveItemEntityId::Stacked(entities.collect::<Result<Vec<_>, _>>()?)
|
|
|
|
},
|
|
|
|
FloorItemType::Meseta(m) => ActiveItemEntityId::Meseta(m.clone()),
|
|
|
|
};
|
|
|
|
let active_item = self.activate_item(item_instance);
|
|
|
|
|
|
|
|
Ok(ActiveItemOnFloor {
|
|
|
|
let id = self.next_id();
|
|
|
|
self.active_to_entity.insert(id, active_entity_ids);
|
|
|
|
Ok(FloorItem {
|
|
|
|
id: id,
|
|
|
|
item: item,
|
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
item: active_item,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use crate::entity::character::CharacterEntityId;
|
|
|
|
use crate::entity::item;
|
|
|
|
use crate::entity::item::{ItemEntity, ItemDetail, ItemEntityId, ItemLocation};
|
|
|
|
use crate::entity::item::tool::Tool;
|
|
|
|
#[test]
|
|
|
|
fn test_stack_items() {
|
|
|
|
let item1 = ItemEntity {
|
|
|
|
id: ItemEntityId(1),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 0,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Weapon(item::weapon::Weapon {
|
|
|
|
weapon: item::weapon::WeaponType::Saber,
|
|
|
|
grind: 0,
|
|
|
|
special: None,
|
|
|
|
attrs: [None; 3],
|
|
|
|
tekked: true,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item2 = ItemEntity {
|
|
|
|
id: ItemEntityId(2),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 1,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Tool(Tool {
|
|
|
|
tool: item::tool::ToolType::Monofluid,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item3 = ItemEntity {
|
|
|
|
id: ItemEntityId(3),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 2,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Weapon(item::weapon::Weapon {
|
|
|
|
weapon: item::weapon::WeaponType::Handgun,
|
|
|
|
grind: 12,
|
|
|
|
special: None,
|
|
|
|
attrs: [None; 3],
|
|
|
|
tekked: true,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item4 = ItemEntity {
|
|
|
|
id: ItemEntityId(4),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 1,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Tool(Tool {
|
|
|
|
tool: item::tool::ToolType::Monofluid,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item5 = ItemEntity {
|
|
|
|
id: ItemEntityId(5),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 1,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Tool(Tool {
|
|
|
|
tool: item::tool::ToolType::Monofluid,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item6 = ItemEntity {
|
|
|
|
id: ItemEntityId(6),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 3,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Weapon(item::weapon::Weapon {
|
|
|
|
weapon: item::weapon::WeaponType::Handgun,
|
|
|
|
grind: 12,
|
|
|
|
special: None,
|
|
|
|
attrs: [None; 3],
|
|
|
|
tekked: true,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item7 = ItemEntity {
|
|
|
|
id: ItemEntityId(7),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 4,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Tool(Tool {
|
|
|
|
tool: item::tool::ToolType::Monomate,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item8 = ItemEntity {
|
|
|
|
id: ItemEntityId(8),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 4,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Tool(Tool {
|
|
|
|
tool: item::tool::ToolType::Monomate,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item9 = ItemEntity {
|
|
|
|
id: ItemEntityId(9),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: CharacterEntityId(0),
|
|
|
|
index: 4,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
item: ItemDetail::Tool(Tool {
|
|
|
|
tool: item::tool::ToolType::Monomate,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let item_vec = vec![item1.clone(), item2.clone(), item3.clone(), item4.clone(), item5.clone(), item6.clone(), item7.clone(), item8.clone(), item9.clone()];
|
|
|
|
|
|
|
|
let stacked = stack_items(item_vec);
|
|
|
|
|
|
|
|
assert!(stacked.len() == 5);
|
|
|
|
assert!(stacked.iter().filter(|k| {
|
|
|
|
**k == ItemInstance::Individual(item6.clone())
|
|
|
|
}).count() == 1);
|
|
|
|
|
|
|
|
assert!(stacked.iter().filter(|k| {
|
|
|
|
**k == ItemInstance::Individual(item3.clone())
|
|
|
|
}).count() == 1);
|
|
|
|
pub fn move_item_from_floor_to_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, client: &mut ClientState, floor_item: FloorItem) -> Result<(), ItemManagerError> {
|
|
|
|
match floor_item.item {
|
|
|
|
FloorItemType::Individual(item) => {
|
|
|
|
let inventory_item = InventoryItem {
|
|
|
|
id: floor_item.id,
|
|
|
|
item: HeldItemType::Individual(item.clone()),
|
|
|
|
equipped: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(stacked.iter().filter(|k| {
|
|
|
|
**k == ItemInstance::Individual(item1.clone())
|
|
|
|
}).count() == 1);
|
|
|
|
let item_entity_id = self.active_to_entity.get(&floor_item.id).unwrap(); // TODO: unwrap
|
|
|
|
if let ActiveItemEntityId::Individual(item_id) = item_entity_id {
|
|
|
|
let slot = client.inventory.add_item(inventory_item).unwrap(); // TODO: unwrap
|
|
|
|
entity_gateway.save_item(&ItemEntity {
|
|
|
|
id: *item_id,
|
|
|
|
item: item,
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: client.character.id,
|
|
|
|
slot: slot,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
}); // TODO: error check
|
|
|
|
} // else something went very wrong TODO: log it
|
|
|
|
},
|
|
|
|
FloorItemType::Stacked(tool, usize) => {
|
|
|
|
let inventory_item = InventoryItem {
|
|
|
|
id: floor_item.id,
|
|
|
|
item: HeldItemType::Stacked(tool, usize),
|
|
|
|
equipped: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(stacked.iter().filter(|k| {
|
|
|
|
**k == ItemInstance::Stacked(vec![item2.clone(), item4.clone(), item5.clone()])
|
|
|
|
}).count() == 1);
|
|
|
|
let item_entity_id = self.active_to_entity.get(&floor_item.id).unwrap(); // TODO: unwrap
|
|
|
|
if let ActiveItemEntityId::Stacked(item_ids) = item_entity_id {
|
|
|
|
let slot = client.inventory.add_item(inventory_item).unwrap(); // TODO: unwrap
|
|
|
|
for item_id in item_ids {
|
|
|
|
entity_gateway.save_item(&ItemEntity {
|
|
|
|
id: *item_id,
|
|
|
|
item: ItemDetail::Tool(tool),
|
|
|
|
location: ItemLocation::Inventory {
|
|
|
|
character_id: client.character.id,
|
|
|
|
slot: slot,
|
|
|
|
equipped: false,
|
|
|
|
},
|
|
|
|
}); // TODO: error check
|
|
|
|
}
|
|
|
|
} // else something went very wrong TODO: log it
|
|
|
|
},
|
|
|
|
FloorItemType::Meseta(meseta) => {
|
|
|
|
client.character.meseta += meseta.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(stacked.iter().filter(|k| {
|
|
|
|
**k == ItemInstance::Stacked(vec![item7.clone(), item8.clone(), item9.clone()])
|
|
|
|
}).count() == 1);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|