2020-05-02 22:08:37 -03:00
|
|
|
#![allow(dead_code)]
|
2020-05-05 21:53:04 -06:00
|
|
|
use std::collections::{HashMap, BTreeMap};
|
|
|
|
use libpso::character::character;//::InventoryItem;
|
2020-03-21 17:47:28 -07:00
|
|
|
use crate::entity::gateway::EntityGateway;
|
2020-03-29 12:43:20 -07:00
|
|
|
use crate::entity::character::CharacterEntity;
|
2020-05-05 21:53:04 -06:00
|
|
|
use crate::entity::item::{ItemEntityId, ItemEntity, ItemDetail, ItemLocation};
|
2020-04-26 22:01:05 -06:00
|
|
|
use crate::entity::item::{Meseta, NewItemEntity};
|
2020-05-05 21:53:04 -06:00
|
|
|
use crate::entity::item::tool::Tool;
|
2020-04-26 22:01:05 -06:00
|
|
|
use crate::ship::map::MapArea;
|
|
|
|
use crate::ship::drops::{ItemDrop, ItemDropType};
|
2020-05-05 21:53:04 -06:00
|
|
|
use crate::ship::ship::ClientState;
|
2020-01-18 23:38:42 -08:00
|
|
|
|
|
|
|
|
2020-03-21 17:47:28 -07:00
|
|
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
2020-04-26 22:01:05 -06:00
|
|
|
pub struct ActiveItemId(pub u32);
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum ActiveItemEntityId {
|
|
|
|
Individual(ItemEntityId),
|
|
|
|
Stacked(Vec<ItemEntityId>),
|
|
|
|
Meseta(Meseta),
|
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-03-21 17:47:28 -07:00
|
|
|
#[derive(Debug)]
|
2020-05-05 21:53:04 -06:00
|
|
|
enum HeldItemType {
|
|
|
|
Individual(ItemDetail),
|
|
|
|
Stacked(Tool, usize),
|
2020-03-21 17:47:28 -07:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
impl HeldItemType {
|
2020-03-21 17:47:28 -07:00
|
|
|
pub fn as_client_bytes(&self) -> [u8; 16] {
|
2020-05-05 21:53:04 -06:00
|
|
|
match self {
|
|
|
|
HeldItemType::Individual(item) => {
|
|
|
|
match &item {
|
2020-03-21 17:47:28 -07:00
|
|
|
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(),
|
2020-03-21 21:46:52 -07:00
|
|
|
ItemDetail::Mag(m) => m.as_bytes(),
|
2020-03-21 17:47:28 -07:00
|
|
|
}
|
|
|
|
},
|
2020-05-05 21:53:04 -06:00
|
|
|
HeldItemType::Stacked(tool, count) => {
|
|
|
|
tool.as_stacked_bytes(*count)
|
2020-04-03 22:56:14 -07:00
|
|
|
},
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InventoryItem {
|
|
|
|
id: ActiveItemId,
|
|
|
|
item: HeldItemType,
|
|
|
|
//slot: usize,
|
|
|
|
equipped: bool,
|
|
|
|
}
|
2020-03-21 17:47:28 -07:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BankItem {
|
|
|
|
id: ActiveItemId,
|
|
|
|
item: HeldItemType,
|
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FloorItemType {
|
|
|
|
Individual(ItemDetail),
|
|
|
|
Stacked(Tool, usize),
|
|
|
|
Meseta(Meseta),
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
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()
|
2020-03-21 17:47:28 -07:00
|
|
|
}
|
2020-05-05 21:53:04 -06:00
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FloorItem {
|
|
|
|
pub id: ActiveItemId,
|
|
|
|
pub item: FloorItemType,
|
2020-04-26 22:01:05 -06:00
|
|
|
pub map_area: MapArea,
|
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
pub z: f32,
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
#[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
|
|
|
|
})
|
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<usize, InventoryError> {
|
|
|
|
self.0.push(item);
|
|
|
|
Ok(self.count() - 1)
|
2020-03-21 17:47:28 -07:00
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ItemManagerError {
|
|
|
|
EntityGatewayError,
|
|
|
|
CouldNotAddToInventory(FloorItem),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ItemManager {
|
|
|
|
id: usize,
|
|
|
|
active_to_entity: HashMap<ActiveItemId, ActiveItemEntityId>,
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
impl ItemManager {
|
|
|
|
pub fn new() -> ItemManager {
|
|
|
|
ItemManager {
|
2020-03-21 17:47:28 -07:00
|
|
|
id: 0,
|
2020-05-05 21:53:04 -06:00
|
|
|
active_to_entity: HashMap::new()
|
2020-03-21 17:47:28 -07:00
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
fn next_id(&mut self) -> ActiveItemId {
|
2020-03-21 17:47:28 -07:00
|
|
|
self.id += 1;
|
2020-05-05 21:53:04 -06:00
|
|
|
ActiveItemId(self.id as u32)
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
pub fn get_character_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> CharacterInventory {
|
2020-03-21 17:47:28 -07:00
|
|
|
let items = entity_gateway.get_items_by_character(&character);
|
|
|
|
let inventory_items = items.into_iter()
|
2020-05-05 21:53:04 -06:00
|
|
|
.filter_map(|item| {
|
2020-03-21 17:47:28 -07:00
|
|
|
match item.location {
|
2020-05-05 21:53:04 -06:00
|
|
|
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));
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
2020-04-26 22:01:05 -06:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
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))
|
2020-04-26 22:01:05 -06:00
|
|
|
};
|
2020-05-05 21:53:04 -06:00
|
|
|
|
|
|
|
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,
|
2020-04-26 22:01:05 -06:00
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
|
|
|
}
|
2020-05-05 21:53:04 -06:00
|
|
|
}).ok_or(ItemManagerError::EntityGatewayError)?;
|
|
|
|
ActiveItemEntityId::Individual(entity.id)
|
2020-04-26 22:01:05 -06:00
|
|
|
},
|
2020-05-05 21:53:04 -06:00
|
|
|
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()),
|
2020-04-26 22:01:05 -06:00
|
|
|
};
|
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
let id = self.next_id();
|
|
|
|
self.active_to_entity.insert(id, active_entity_ids);
|
|
|
|
Ok(FloorItem {
|
|
|
|
id: id,
|
|
|
|
item: item,
|
2020-04-26 22:01:05 -06:00
|
|
|
map_area: item_drop.map_area,
|
|
|
|
x: item_drop.x,
|
|
|
|
y: item_drop.y,
|
|
|
|
z: item_drop.z,
|
2020-04-26 22:23:59 -06:00
|
|
|
})
|
2020-04-26 22:01:05 -06:00
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
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,
|
|
|
|
};
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
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,
|
|
|
|
};
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
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;
|
2020-05-06 07:51:03 -06:00
|
|
|
entity_gateway.save_character(&client.character);
|
2020-05-05 21:53:04 -06:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 23:38:42 -08:00
|
|
|
|
2020-05-05 21:53:04 -06:00
|
|
|
Ok(())
|
2020-01-18 23:38:42 -08:00
|
|
|
}
|
|
|
|
}
|