item structure overhaul
also, item pickup
This commit is contained in:
parent
cb758b3b85
commit
9a721e0980
@ -247,6 +247,7 @@ pub struct NewCharacterEntity {
|
|||||||
pub guildcard: CharacterGuildCard,
|
pub guildcard: CharacterGuildCard,
|
||||||
|
|
||||||
pub tech_menu: CharacterTechMenu,
|
pub tech_menu: CharacterTechMenu,
|
||||||
|
pub meseta: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NewCharacterEntity {
|
impl NewCharacterEntity {
|
||||||
@ -264,6 +265,7 @@ impl NewCharacterEntity {
|
|||||||
info_board: CharacterInfoboard::new(),
|
info_board: CharacterInfoboard::new(),
|
||||||
guildcard: CharacterGuildCard::default(),
|
guildcard: CharacterGuildCard::default(),
|
||||||
tech_menu: CharacterTechMenu::new(),
|
tech_menu: CharacterTechMenu::new(),
|
||||||
|
meseta: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -287,4 +289,5 @@ pub struct CharacterEntity {
|
|||||||
pub guildcard: CharacterGuildCard,
|
pub guildcard: CharacterGuildCard,
|
||||||
|
|
||||||
pub tech_menu: CharacterTechMenu,
|
pub tech_menu: CharacterTechMenu,
|
||||||
|
pub meseta: u32,
|
||||||
}
|
}
|
||||||
|
@ -120,6 +120,7 @@ impl EntityGateway for InMemoryGateway {
|
|||||||
info_board: character.info_board,
|
info_board: character.info_board,
|
||||||
guildcard: character.guildcard,
|
guildcard: character.guildcard,
|
||||||
tech_menu: character.tech_menu,
|
tech_menu: character.tech_menu,
|
||||||
|
meseta: character.meseta,
|
||||||
};
|
};
|
||||||
characters.insert(new_character.id, new_character.clone());
|
characters.insert(new_character.id, new_character.clone());
|
||||||
Some(new_character)
|
Some(new_character)
|
||||||
|
@ -21,14 +21,21 @@ pub struct BankName(String);
|
|||||||
pub enum ItemLocation {
|
pub enum ItemLocation {
|
||||||
Inventory {
|
Inventory {
|
||||||
character_id: CharacterEntityId,
|
character_id: CharacterEntityId,
|
||||||
index: usize,
|
slot: usize,
|
||||||
equipped: bool,
|
equipped: bool,
|
||||||
},
|
},
|
||||||
Bank {
|
Bank {
|
||||||
character_id: CharacterEntityId,
|
character_id: CharacterEntityId,
|
||||||
slot: BankName,
|
slot: BankName,
|
||||||
},
|
},
|
||||||
Floor {
|
LocalFloor {
|
||||||
|
character_id: CharacterEntityId,
|
||||||
|
map_area: MapArea,
|
||||||
|
x: f32,
|
||||||
|
y: f32,
|
||||||
|
z: f32,
|
||||||
|
},
|
||||||
|
SharedFloor {
|
||||||
map_area: MapArea,
|
map_area: MapArea,
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
@ -44,7 +51,7 @@ pub enum ItemLocation {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Meseta(pub u32);
|
pub struct Meseta(pub u32);
|
||||||
|
|
||||||
impl Meseta {
|
impl Meseta {
|
||||||
|
@ -220,7 +220,7 @@ fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAccountE
|
|||||||
}),
|
}),
|
||||||
location: ItemLocation::Inventory {
|
location: ItemLocation::Inventory {
|
||||||
character_id: character.id,
|
character_id: character.id,
|
||||||
index: 0,
|
slot: 0,
|
||||||
equipped: true,
|
equipped: true,
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@ -235,7 +235,7 @@ fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAccountE
|
|||||||
}),
|
}),
|
||||||
location: ItemLocation::Inventory {
|
location: ItemLocation::Inventory {
|
||||||
character_id: character.id,
|
character_id: character.id,
|
||||||
index: 1,
|
slot: 1,
|
||||||
equipped: true,
|
equipped: true,
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@ -255,7 +255,7 @@ fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAccountE
|
|||||||
}),
|
}),
|
||||||
location: ItemLocation::Inventory {
|
location: ItemLocation::Inventory {
|
||||||
character_id: character.id,
|
character_id: character.id,
|
||||||
index: 2,
|
slot: 2,
|
||||||
equipped: true,
|
equipped: true,
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@ -268,7 +268,7 @@ fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAccountE
|
|||||||
}),
|
}),
|
||||||
location: ItemLocation::Inventory {
|
location: ItemLocation::Inventory {
|
||||||
character_id: character.id,
|
character_id: character.id,
|
||||||
index: 3,
|
slot: 3,
|
||||||
equipped: false,
|
equipped: false,
|
||||||
}});
|
}});
|
||||||
entity_gateway.create_item(
|
entity_gateway.create_item(
|
||||||
@ -279,7 +279,7 @@ fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAccountE
|
|||||||
}),
|
}),
|
||||||
location: ItemLocation::Inventory {
|
location: ItemLocation::Inventory {
|
||||||
character_id: character.id,
|
character_id: character.id,
|
||||||
index: 4,
|
slot: 4,
|
||||||
equipped: false,
|
equipped: false,
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ fn main() {
|
|||||||
entity_gateway.create_character(character);
|
entity_gateway.create_character(character);
|
||||||
let mut character = NewCharacterEntity::new(fake_user.id);
|
let mut character = NewCharacterEntity::new(fake_user.id);
|
||||||
character.slot = 2;
|
character.slot = 2;
|
||||||
character.name = "\tE12345678".into();
|
character.name = "no progress".into();
|
||||||
character.exp = 80000000;
|
character.exp = 80000000;
|
||||||
let character = entity_gateway.create_character(character).unwrap();
|
let character = entity_gateway.create_character(character).unwrap();
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ fn main() {
|
|||||||
),
|
),
|
||||||
location: ItemLocation::Inventory {
|
location: ItemLocation::Inventory {
|
||||||
character_id: character.id,
|
character_id: character.id,
|
||||||
index: 0,
|
slot: 0,
|
||||||
equipped: true,
|
equipped: true,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use libpso::character::character;
|
use libpso::character::character;
|
||||||
use crate::common::leveltable::CharacterStats;
|
use crate::common::leveltable::CharacterStats;
|
||||||
use crate::entity::character::CharacterEntity;
|
use crate::entity::character::CharacterEntity;
|
||||||
use crate::ship::items::ActiveInventory;
|
use crate::ship::items::CharacterInventory;
|
||||||
|
|
||||||
// TODO: exp
|
// TODO: exp
|
||||||
pub struct CharacterBytesBuilder<'a> {
|
pub struct CharacterBytesBuilder<'a> {
|
||||||
@ -79,7 +79,7 @@ pub struct FullCharacterBytesBuilder<'a> {
|
|||||||
character: Option<&'a CharacterEntity>,
|
character: Option<&'a CharacterEntity>,
|
||||||
stats: Option<&'a CharacterStats>,
|
stats: Option<&'a CharacterStats>,
|
||||||
level: Option<u32>,
|
level: Option<u32>,
|
||||||
inventory: Option<&'a ActiveInventory>,
|
inventory: Option<&'a CharacterInventory>,
|
||||||
key_config: Option<&'a [u8; 0x16C]>,
|
key_config: Option<&'a [u8; 0x16C]>,
|
||||||
joystick_config: Option<&'a [u8; 0x38]>,
|
joystick_config: Option<&'a [u8; 0x38]>,
|
||||||
symbol_chat: Option<&'a [u8; 1248]>,
|
symbol_chat: Option<&'a [u8; 1248]>,
|
||||||
@ -122,7 +122,7 @@ impl<'a> FullCharacterBytesBuilder<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inventory(self, inventory: &'a ActiveInventory) -> FullCharacterBytesBuilder<'a> {
|
pub fn inventory(self, inventory: &'a CharacterInventory) -> FullCharacterBytesBuilder<'a> {
|
||||||
FullCharacterBytesBuilder {
|
FullCharacterBytesBuilder {
|
||||||
inventory: Some(inventory),
|
inventory: Some(inventory),
|
||||||
..self
|
..self
|
||||||
|
@ -1,37 +1,37 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, BTreeMap};
|
||||||
use libpso::character::character::InventoryItem;
|
use libpso::character::character;//::InventoryItem;
|
||||||
use crate::entity::gateway::EntityGateway;
|
use crate::entity::gateway::EntityGateway;
|
||||||
use crate::entity::character::CharacterEntity;
|
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::{Meseta, NewItemEntity};
|
||||||
|
use crate::entity::item::tool::Tool;
|
||||||
use crate::ship::map::MapArea;
|
use crate::ship::map::MapArea;
|
||||||
use crate::ship::drops::{ItemDrop, ItemDropType};
|
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)]
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
||||||
pub struct ActiveItemId(pub u32);
|
pub struct ActiveItemId(pub u32);
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ActiveItem {
|
enum ActiveItemEntityId {
|
||||||
pub id: ActiveItemId,
|
Individual(ItemEntityId),
|
||||||
item: ItemInstance,
|
Stacked(Vec<ItemEntityId>),
|
||||||
|
Meseta(Meseta),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ActiveItem {
|
#[derive(Debug)]
|
||||||
|
enum HeldItemType {
|
||||||
|
Individual(ItemDetail),
|
||||||
|
Stacked(Tool, usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HeldItemType {
|
||||||
pub fn as_client_bytes(&self) -> [u8; 16] {
|
pub fn as_client_bytes(&self) -> [u8; 16] {
|
||||||
match &self.item {
|
match self {
|
||||||
ItemInstance::Individual(i) => {
|
HeldItemType::Individual(item) => {
|
||||||
match &i.item {
|
match &item {
|
||||||
ItemDetail::Weapon(w) => w.as_bytes(),
|
ItemDetail::Weapon(w) => w.as_bytes(),
|
||||||
ItemDetail::Armor(a) => a.as_bytes(),
|
ItemDetail::Armor(a) => a.as_bytes(),
|
||||||
ItemDetail::Shield(s) => s.as_bytes(),
|
ItemDetail::Shield(s) => s.as_bytes(),
|
||||||
@ -41,328 +41,278 @@ impl ActiveItem {
|
|||||||
ItemDetail::Mag(m) => m.as_bytes(),
|
ItemDetail::Mag(m) => m.as_bytes(),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ItemInstance::Stacked(i) => {
|
HeldItemType::Stacked(tool, count) => {
|
||||||
let len = i.len();
|
tool.as_stacked_bytes(*count)
|
||||||
match &i[0].item {
|
},
|
||||||
ItemDetail::Tool(t) => t.as_stacked_bytes(len),
|
}
|
||||||
_ => panic!(),
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct InventoryItem {
|
||||||
|
id: ActiveItemId,
|
||||||
|
item: HeldItemType,
|
||||||
|
//slot: usize,
|
||||||
|
equipped: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct BankItem {
|
||||||
|
id: ActiveItemId,
|
||||||
|
item: HeldItemType,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum FloorItemType {
|
||||||
|
Individual(ItemDetail),
|
||||||
|
Stacked(Tool, usize),
|
||||||
|
Meseta(Meseta),
|
||||||
|
}
|
||||||
|
|
||||||
|
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(),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ItemInstance::Meseta(m) => {
|
FloorItemType::Stacked(tool, count) => {
|
||||||
|
tool.as_stacked_bytes(*count)
|
||||||
|
},
|
||||||
|
FloorItemType::Meseta(m) => {
|
||||||
m.as_bytes()
|
m.as_bytes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ActiveInventory(Vec<ActiveItem>);
|
#[derive(Debug)]
|
||||||
|
pub struct FloorItem {
|
||||||
|
pub id: ActiveItemId,
|
||||||
|
pub item: FloorItemType,
|
||||||
|
pub map_area: MapArea,
|
||||||
|
pub x: f32,
|
||||||
|
pub y: f32,
|
||||||
|
pub z: f32,
|
||||||
|
}
|
||||||
|
|
||||||
impl ActiveInventory {
|
#[derive(Debug)]
|
||||||
pub fn as_client_inventory_items(&self) -> [InventoryItem; 30] {
|
pub enum InventoryError {
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CharacterInventory(Vec<InventoryItem>);
|
||||||
|
|
||||||
|
impl CharacterInventory {
|
||||||
|
pub fn as_client_inventory_items(&self) -> [character::InventoryItem; 30] {
|
||||||
self.0.iter()
|
self.0.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.fold([InventoryItem::default(); 30], |mut inventory, (index, item)| {
|
.fold([character::InventoryItem::default(); 30], |mut inventory, (slot, item)| {
|
||||||
let bytes = item.as_client_bytes();
|
let bytes = item.item.as_client_bytes();
|
||||||
inventory[index].data1.copy_from_slice(&bytes[0..12]);
|
inventory[slot].data1.copy_from_slice(&bytes[0..12]);
|
||||||
inventory[index].data2.copy_from_slice(&bytes[12..16]);
|
inventory[slot].data2.copy_from_slice(&bytes[12..16]);
|
||||||
inventory[index].item_id = item.id.0;
|
inventory[slot].item_id = item.id.0;
|
||||||
|
|
||||||
// does this do anything?
|
// does this do anything?
|
||||||
inventory[index].equipped = match item.item {
|
inventory[slot].equipped = if item.equipped { 1 } else { 0 };
|
||||||
ItemInstance::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 1,
|
|
||||||
_ => 0,
|
|
||||||
};
|
|
||||||
// because this actually equips the item
|
// because this actually equips the item
|
||||||
inventory[index].flags |= match item.item {
|
inventory[slot].flags |= if item.equipped { 8 } else { 0 };
|
||||||
ItemInstance::Individual(ItemEntity {location: ItemLocation::Inventory{ equipped: true, ..}, ..}) => 8,
|
|
||||||
_ => 0,
|
|
||||||
};
|
|
||||||
inventory
|
inventory
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_item(&mut self, item: InventoryItem) -> Result<usize, InventoryError> {
|
||||||
|
self.0.push(item);
|
||||||
|
Ok(self.count() - 1)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn count(&self) -> usize {
|
pub fn count(&self) -> usize {
|
||||||
self.0.len()
|
self.0.len()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inventory_item_index(item: &ItemInstance) -> usize {
|
|
||||||
match item {
|
#[derive(Debug)]
|
||||||
ItemInstance::Individual(i) => {
|
pub enum ItemManagerError {
|
||||||
match i.location {
|
EntityGatewayError,
|
||||||
ItemLocation::Inventory{index, ..} => index,
|
CouldNotAddToInventory(FloorItem),
|
||||||
_ => panic!()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ItemInstance::Stacked(i) => {
|
|
||||||
match i[0].location {
|
|
||||||
ItemLocation::Inventory{index, ..} => index,
|
|
||||||
_ => panic!()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => panic!(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ActiveItemOnFloor {
|
pub struct ItemManager {
|
||||||
pub map_area: MapArea,
|
id: usize,
|
||||||
pub x: f32,
|
active_to_entity: HashMap<ActiveItemId, ActiveItemEntityId>,
|
||||||
pub y: f32,
|
|
||||||
pub z: f32,
|
|
||||||
pub item: ActiveItem,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stack_items(items: Vec<ItemEntity>) -> Vec<ItemInstance> {
|
impl ItemManager {
|
||||||
let mut stacks = HashMap::new();
|
pub fn new() -> ItemManager {
|
||||||
|
ItemManager {
|
||||||
for item in items {
|
|
||||||
stacks.entry(item.item.item_type()).or_insert(Vec::new()).push(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ActiveBank([Option<ActiveItemId>; 200]);
|
|
||||||
|
|
||||||
pub struct ActiveItemDatabase {
|
|
||||||
id: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ActiveItemDatabase {
|
|
||||||
pub fn new() -> ActiveItemDatabase {
|
|
||||||
ActiveItemDatabase {
|
|
||||||
id: 0,
|
id: 0,
|
||||||
|
active_to_entity: HashMap::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn activate_item(&mut self, item: ItemInstance) -> ActiveItem {
|
fn next_id(&mut self) -> ActiveItemId {
|
||||||
self.id += 1;
|
self.id += 1;
|
||||||
ActiveItem {
|
ActiveItemId(self.id as u32)
|
||||||
id: ActiveItemId(self.id),
|
|
||||||
item: item,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: deactivate item
|
pub fn get_character_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> CharacterInventory {
|
||||||
|
|
||||||
pub fn get_character_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> ActiveInventory {
|
|
||||||
let items = entity_gateway.get_items_by_character(&character);
|
let items = entity_gateway.get_items_by_character(&character);
|
||||||
let inventory_items = items.into_iter()
|
let inventory_items = items.into_iter()
|
||||||
.filter(|item| {
|
.filter_map(|item| {
|
||||||
match item.location {
|
match item.location {
|
||||||
ItemLocation::Inventory{..} => true,
|
ItemLocation::Inventory{slot, equipped, ..} => Some((item.id, item.item, slot, equipped)),
|
||||||
_ => false,
|
_ => None,
|
||||||
}
|
}
|
||||||
}).collect();
|
})
|
||||||
let mut stacked = stack_items(inventory_items);
|
.fold(BTreeMap::new(), |mut acc, (id, item, slot, equipped)| {
|
||||||
stacked.sort_by(|a, b| {
|
if item.is_stackable() {
|
||||||
inventory_item_index(a).partial_cmp(&inventory_item_index(b)).unwrap()
|
if let ItemDetail::Tool(tool) = item {
|
||||||
});
|
let stacked = acc.entry(slot).or_insert((HeldItemType::Stacked(tool, 0), ActiveItemEntityId::Stacked(Vec::new()), false));
|
||||||
let activated = stacked.into_iter().map(|i| self.activate_item(i));
|
if let HeldItemType::Stacked(_, ref mut item_count) = stacked.0 {
|
||||||
ActiveInventory(activated.take(30).collect())
|
*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));
|
||||||
|
}
|
||||||
|
|
||||||
|
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 activate_item_drop<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, item_drop: ItemDrop) -> Result<ActiveItemOnFloor, ShipError> {
|
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_detail = match item_drop.item {
|
let item = match item_drop.item {
|
||||||
ItemDropType::Weapon(w) => Some(ItemDetail::Weapon(w)),
|
ItemDropType::Weapon(w) => FloorItemType::Individual(ItemDetail::Weapon(w)),
|
||||||
ItemDropType::Armor(w) => Some(ItemDetail::Armor(w)),
|
ItemDropType::Armor(w) => FloorItemType::Individual(ItemDetail::Armor(w)),
|
||||||
ItemDropType::Shield(w) => Some(ItemDetail::Shield(w)),
|
ItemDropType::Shield(w) => FloorItemType::Individual(ItemDetail::Shield(w)),
|
||||||
ItemDropType::Unit(w) => Some(ItemDetail::Unit(w)),
|
ItemDropType::Unit(w) => FloorItemType::Individual(ItemDetail::Unit(w)),
|
||||||
ItemDropType::Tool(w) => Some(ItemDetail::Tool(w)),
|
ItemDropType::TechniqueDisk(w) => FloorItemType::Individual(ItemDetail::TechniqueDisk(w)),
|
||||||
ItemDropType::TechniqueDisk(w) => Some(ItemDetail::TechniqueDisk(w)),
|
ItemDropType::Mag(w) => FloorItemType::Individual(ItemDetail::Mag(w)),
|
||||||
ItemDropType::Mag(w) => Some(ItemDetail::Mag(w)),
|
ItemDropType::Tool(w) => FloorItemType::Individual(ItemDetail::Tool(w)),
|
||||||
ItemDropType::Meseta(_) => None
|
//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 active_entity_ids = match &item {
|
||||||
let item_entity = entity_gateway.create_item(NewItemEntity {
|
FloorItemType::Individual(i) => {
|
||||||
item: item,
|
let entity = entity_gateway.create_item(NewItemEntity {
|
||||||
location: ItemLocation::Floor {
|
item: i.clone(),
|
||||||
|
location: ItemLocation::LocalFloor {
|
||||||
|
character_id: character.id,
|
||||||
map_area: item_drop.map_area,
|
map_area: item_drop.map_area,
|
||||||
x: item_drop.x,
|
x: item_drop.x,
|
||||||
y: item_drop.y,
|
y: item_drop.y,
|
||||||
z: item_drop.z,
|
z: item_drop.z,
|
||||||
}
|
}
|
||||||
}).unwrap();
|
}).ok_or(ItemManagerError::EntityGatewayError)?;
|
||||||
stack_items(vec![item_entity]).pop().ok_or(ShipError::ItemError)?
|
ActiveItemEntityId::Individual(entity.id)
|
||||||
},
|
},
|
||||||
None => {
|
FloorItemType::Stacked(tool, count) => {
|
||||||
let meseta = match item_drop.item {
|
let entities = (0..*count).map(|_| {
|
||||||
ItemDropType::Meseta(m) => m,
|
entity_gateway.create_item(NewItemEntity {
|
||||||
_ => panic!(),
|
item: ItemDetail::Tool(*tool),
|
||||||
};
|
location: ItemLocation::LocalFloor {
|
||||||
ItemInstance::Meseta(Meseta(meseta))
|
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,
|
map_area: item_drop.map_area,
|
||||||
x: item_drop.x,
|
x: item_drop.x,
|
||||||
y: item_drop.y,
|
y: item_drop.y,
|
||||||
z: item_drop.z,
|
z: item_drop.z,
|
||||||
item: active_item,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
pub fn move_item_from_floor_to_inventory<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, client: &mut ClientState, floor_item: FloorItem) -> Result<(), ItemManagerError> {
|
||||||
mod test {
|
match floor_item.item {
|
||||||
use super::*;
|
FloorItemType::Individual(item) => {
|
||||||
use crate::entity::character::CharacterEntityId;
|
let inventory_item = InventoryItem {
|
||||||
use crate::entity::item;
|
id: floor_item.id,
|
||||||
use crate::entity::item::{ItemEntity, ItemDetail, ItemEntityId, ItemLocation};
|
item: HeldItemType::Individual(item.clone()),
|
||||||
use crate::entity::item::tool::Tool;
|
equipped: false,
|
||||||
#[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);
|
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.len() == 5);
|
let item_entity_id = self.active_to_entity.get(&floor_item.id).unwrap(); // TODO: unwrap
|
||||||
assert!(stacked.iter().filter(|k| {
|
if let ActiveItemEntityId::Stacked(item_ids) = item_entity_id {
|
||||||
**k == ItemInstance::Individual(item6.clone())
|
let slot = client.inventory.add_item(inventory_item).unwrap(); // TODO: unwrap
|
||||||
}).count() == 1);
|
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| {
|
Ok(())
|
||||||
**k == ItemInstance::Individual(item3.clone())
|
|
||||||
}).count() == 1);
|
|
||||||
|
|
||||||
assert!(stacked.iter().filter(|k| {
|
|
||||||
**k == ItemInstance::Individual(item1.clone())
|
|
||||||
}).count() == 1);
|
|
||||||
|
|
||||||
assert!(stacked.iter().filter(|k| {
|
|
||||||
**k == ItemInstance::Stacked(vec![item2.clone(), item4.clone(), item5.clone()])
|
|
||||||
}).count() == 1);
|
|
||||||
|
|
||||||
assert!(stacked.iter().filter(|k| {
|
|
||||||
**k == ItemInstance::Stacked(vec![item7.clone(), item8.clone(), item9.clone()])
|
|
||||||
}).count() == 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
use libpso::packet::messages::*;
|
use libpso::packet::messages::*;
|
||||||
use crate::ship::ship::{ShipError};
|
use crate::ship::ship::{ShipError};
|
||||||
use crate::ship::items::{ActiveItemOnFloor};
|
use crate::ship::items::{FloorItem};
|
||||||
|
use crate::ship::location::AreaClient;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
|
||||||
pub fn item_drop(client: u8, target: u8, item_drop: &ActiveItemOnFloor) -> Result<ItemDrop, ShipError> {
|
pub fn item_drop(client: u8, target: u8, item_drop: &FloorItem) -> Result<ItemDrop, ShipError> {
|
||||||
let item_bytes = item_drop.item.as_client_bytes();
|
let item_bytes = item_drop.item.as_client_bytes();
|
||||||
Ok(ItemDrop {
|
Ok(ItemDrop {
|
||||||
client: client,
|
client: client,
|
||||||
@ -16,8 +17,32 @@ pub fn item_drop(client: u8, target: u8, item_drop: &ActiveItemOnFloor) -> Resul
|
|||||||
z: item_drop.z,
|
z: item_drop.z,
|
||||||
y: item_drop.y,
|
y: item_drop.y,
|
||||||
item_bytes: item_bytes[0..12].try_into()?,
|
item_bytes: item_bytes[0..12].try_into()?,
|
||||||
item_id: item_drop.item.id.0,
|
item_id: item_drop.id.0,
|
||||||
item_bytes2: item_bytes[12..16].try_into()?,
|
item_bytes2: item_bytes[12..16].try_into()?,
|
||||||
unknown2: 0,
|
unknown2: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_item(area_client: AreaClient, item: &FloorItem) -> Result<CreateItem, ShipError> {
|
||||||
|
let bytes = item.item.as_client_bytes();
|
||||||
|
Ok(CreateItem {
|
||||||
|
client: area_client.local_client.id(),
|
||||||
|
target: 0,
|
||||||
|
item_data: bytes[0..12].try_into()?,
|
||||||
|
item_id: item.id.0,
|
||||||
|
item_data2: bytes[12..16].try_into()?,
|
||||||
|
unknown: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_item_from_floor(area_client: AreaClient, item: &FloorItem) -> Result<RemoveItemFromFloor, ShipError> {
|
||||||
|
Ok(RemoveItemFromFloor {
|
||||||
|
client: area_client.local_client.id(),
|
||||||
|
target: 0,
|
||||||
|
client_id: area_client.local_client.id(),
|
||||||
|
unknown: 0,
|
||||||
|
area: item.map_area.area_value(),
|
||||||
|
unknown2: 0,
|
||||||
|
item_id: item.id.0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -4,13 +4,13 @@ use crate::common::serverstate::ClientId;
|
|||||||
use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients};
|
use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients};
|
||||||
use crate::login::login::get_login_status;
|
use crate::login::login::get_login_status;
|
||||||
use crate::entity::gateway::EntityGateway;
|
use crate::entity::gateway::EntityGateway;
|
||||||
use crate::ship::items::ActiveItemDatabase;
|
use crate::ship::items::ItemManager;
|
||||||
|
|
||||||
pub fn validate_login<EG: EntityGateway>(id: ClientId,
|
pub fn validate_login<EG: EntityGateway>(id: ClientId,
|
||||||
pkt: &Login,
|
pkt: &Login,
|
||||||
entity_gateway: &mut EG,
|
entity_gateway: &mut EG,
|
||||||
clients: &mut Clients,
|
clients: &mut Clients,
|
||||||
item_database: &mut ActiveItemDatabase,
|
item_manager: &mut ItemManager,
|
||||||
ship_name: &String)
|
ship_name: &String)
|
||||||
-> Result<Vec<SendShipPacket>, ShipError> {
|
-> Result<Vec<SendShipPacket>, ShipError> {
|
||||||
Ok(match get_login_status(entity_gateway, pkt) {
|
Ok(match get_login_status(entity_gateway, pkt) {
|
||||||
@ -26,7 +26,7 @@ pub fn validate_login<EG: EntityGateway>(id: ClientId,
|
|||||||
.clone();
|
.clone();
|
||||||
let settings = entity_gateway.get_user_settings_by_user(&user)
|
let settings = entity_gateway.get_user_settings_by_user(&user)
|
||||||
.ok_or(ShipError::ClientNotFound(id))?;
|
.ok_or(ShipError::ClientNotFound(id))?;
|
||||||
let inventory = item_database.get_character_inventory(entity_gateway, &character);
|
let inventory = item_manager.get_character_inventory(entity_gateway, &character);
|
||||||
|
|
||||||
clients.insert(id, ClientState::new(user, settings, character, inventory, pkt.session));
|
clients.insert(id, ClientState::new(user, settings, character, inventory, pkt.session));
|
||||||
vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&&ship_name, 3))]
|
vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&&ship_name, 3))]
|
||||||
|
@ -4,10 +4,10 @@ use libpso::packet::messages::*;
|
|||||||
use crate::common::serverstate::ClientId;
|
use crate::common::serverstate::ClientId;
|
||||||
use crate::ship::ship::{SendShipPacket, ShipError, Clients, Rooms};
|
use crate::ship::ship::{SendShipPacket, ShipError, Clients, Rooms};
|
||||||
use crate::ship::location::{ClientLocation, ClientLocationError};
|
use crate::ship::location::{ClientLocation, ClientLocationError};
|
||||||
use crate::ship::drops::{ItemDrop};
|
use crate::ship::drops::ItemDrop;
|
||||||
use crate::ship::items::ActiveItemDatabase;
|
use crate::ship::items::{ItemManager, ItemManagerError};
|
||||||
use crate::entity::gateway::EntityGateway;
|
use crate::entity::gateway::EntityGateway;
|
||||||
use libpso::{utf8_to_utf16_array};
|
use libpso::utf8_to_utf16_array;
|
||||||
use crate::ship::packet::builder;
|
use crate::ship::packet::builder;
|
||||||
|
|
||||||
fn send_to_client(id: ClientId, target: u8, msg: DirectMessage, client_location: &ClientLocation)
|
fn send_to_client(id: ClientId, target: u8, msg: DirectMessage, client_location: &ClientLocation)
|
||||||
@ -50,9 +50,11 @@ pub fn request_item<EG>(id: ClientId,
|
|||||||
client_location: &ClientLocation,
|
client_location: &ClientLocation,
|
||||||
clients: &mut Clients,
|
clients: &mut Clients,
|
||||||
rooms: &mut Rooms,
|
rooms: &mut Rooms,
|
||||||
active_items: &mut ActiveItemDatabase)
|
item_manager: &mut ItemManager)
|
||||||
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
||||||
where EG: EntityGateway {
|
where
|
||||||
|
EG: EntityGateway
|
||||||
|
{
|
||||||
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
||||||
let room = rooms.get_mut(room_id.0)
|
let room = rooms.get_mut(room_id.0)
|
||||||
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
|
||||||
@ -64,7 +66,6 @@ where EG: EntityGateway {
|
|||||||
return Err(ShipError::MonsterAlreadyDroppedItem(id, request_item.enemy_id))
|
return Err(ShipError::MonsterAlreadyDroppedItem(id, request_item.enemy_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
let _area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
||||||
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
||||||
|
|
||||||
let item_drop_packets = clients_in_area.into_iter()
|
let item_drop_packets = clients_in_area.into_iter()
|
||||||
@ -82,11 +83,12 @@ where EG: EntityGateway {
|
|||||||
z: request_item.z,
|
z: request_item.z,
|
||||||
item: item_drop_type,
|
item: item_drop_type,
|
||||||
};
|
};
|
||||||
|
//let floor_item_instance = FloorItemInstance::from_item_drop(entity_gateway, item_drop)?;
|
||||||
let activated_item = active_items.activate_item_drop(entity_gateway, item_drop)?;
|
//let activated_item = item_manager.activate_floor_item(floor_item_instance);
|
||||||
let client = clients.get_mut(&area_client.client).ok_or(ShipError::ClientNotFound(area_client.client))?;
|
let client = clients.get_mut(&area_client.client).ok_or(ShipError::ClientNotFound(area_client.client))?;
|
||||||
let item_drop_msg = builder::message::item_drop(request_item.client, request_item.target, &activated_item)?;
|
let floor_item = item_manager.drop_item_on_local_floor(entity_gateway, &client.character, item_drop).unwrap(); // TODO: unwrap
|
||||||
client.floor_items.push(activated_item);
|
let item_drop_msg = builder::message::item_drop(request_item.client, request_item.target, &floor_item)?;
|
||||||
|
client.floor_items.push(floor_item);
|
||||||
Ok((area_client.client, SendShipPacket::Message(Message::new(GameMessage::ItemDrop(item_drop_msg)))))
|
Ok((area_client.client, SendShipPacket::Message(Message::new(GameMessage::ItemDrop(item_drop_msg)))))
|
||||||
})
|
})
|
||||||
.filter_map(|item_drop_pkt| {
|
.filter_map(|item_drop_pkt| {
|
||||||
@ -97,3 +99,83 @@ where EG: EntityGateway {
|
|||||||
|
|
||||||
Ok(Box::new(item_drop_packets.into_iter()))
|
Ok(Box::new(item_drop_packets.into_iter()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pickup_item<EG>(id: ClientId,
|
||||||
|
pickup_item: &PickupItem,
|
||||||
|
entity_gateway: &mut EG,
|
||||||
|
client_location: &ClientLocation,
|
||||||
|
clients: &mut Clients,
|
||||||
|
rooms: &mut Rooms,
|
||||||
|
item_manager: &mut ItemManager)
|
||||||
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
||||||
|
where
|
||||||
|
EG: EntityGateway
|
||||||
|
{
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
enum ItemFloor {
|
||||||
|
Local,
|
||||||
|
Shared
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
||||||
|
let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
||||||
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
||||||
|
let room = rooms.get_mut(room_id.0)
|
||||||
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
|
||||||
|
.as_mut()
|
||||||
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;
|
||||||
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
||||||
|
|
||||||
|
let (item_index, (_, floor)) = client.floor_items.iter()
|
||||||
|
.zip(std::iter::repeat(ItemFloor::Local))
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, (item, _))| item.id.0 == pickup_item.item_id)
|
||||||
|
.next()
|
||||||
|
.or_else(|| {
|
||||||
|
room.floor_items.iter()
|
||||||
|
.zip(std::iter::repeat(ItemFloor::Shared))
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, (item, _))| item.id.0 == pickup_item.item_id)
|
||||||
|
.next()
|
||||||
|
})
|
||||||
|
.ok_or(ShipError::PickUpInvalidItemId(pickup_item.item_id))?;
|
||||||
|
|
||||||
|
let item = match floor {
|
||||||
|
ItemFloor::Local => client.floor_items.remove(item_index),
|
||||||
|
ItemFloor::Shared => room.floor_items.remove(item_index),
|
||||||
|
};
|
||||||
|
|
||||||
|
let remove_item = builder::message::remove_item_from_floor(area_client, &item)?;
|
||||||
|
let create_item = builder::message::create_item(area_client, &item)?;
|
||||||
|
//match client.inventory.add_item(item.item) {
|
||||||
|
match item_manager.move_item_from_floor_to_inventory(entity_gateway, &mut client, item) {
|
||||||
|
Ok(_) => {
|
||||||
|
Ok(Box::new(Vec::new().into_iter()
|
||||||
|
.chain(clients_in_area.clone().into_iter()
|
||||||
|
.filter(move |c| {
|
||||||
|
match floor {
|
||||||
|
ItemFloor::Local => c.client == id,
|
||||||
|
ItemFloor::Shared => true,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map(move |c| {
|
||||||
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::RemoveItemFromFloor(remove_item.clone()))))
|
||||||
|
}))
|
||||||
|
.chain(clients_in_area.into_iter().map(move |c| {
|
||||||
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::CreateItem(create_item.clone()))))
|
||||||
|
})))
|
||||||
|
)
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
// inventory full, probably
|
||||||
|
if let ItemManagerError::CouldNotAddToInventory(item) = err {
|
||||||
|
match floor {
|
||||||
|
ItemFloor::Local => client.floor_items.push(item),
|
||||||
|
ItemFloor::Shared => room.floor_items.push(item),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Box::new(None.into_iter()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ use rand::Rng;
|
|||||||
use crate::ship::map::Maps;
|
use crate::ship::map::Maps;
|
||||||
use crate::ship::drops::DropTable;
|
use crate::ship::drops::DropTable;
|
||||||
use crate::entity::character::SectionID;
|
use crate::entity::character::SectionID;
|
||||||
|
use crate::ship::items::FloorItem;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum RoomCreationError {
|
pub enum RoomCreationError {
|
||||||
@ -152,6 +153,7 @@ pub struct RoomState {
|
|||||||
pub section_id: SectionID,
|
pub section_id: SectionID,
|
||||||
pub random_seed: u32,
|
pub random_seed: u32,
|
||||||
pub bursting: bool,
|
pub bursting: bool,
|
||||||
|
pub floor_items: Vec<FloorItem>,
|
||||||
// items on ground
|
// items on ground
|
||||||
// enemy info
|
// enemy info
|
||||||
}
|
}
|
||||||
@ -231,6 +233,7 @@ impl RoomState {
|
|||||||
section_id: section_id,
|
section_id: section_id,
|
||||||
drop_table: Box::new(DropTable::new(room_mode.episode(), room_mode.difficulty(), section_id)),
|
drop_table: Box::new(DropTable::new(room_mode.episode(), room_mode.difficulty(), section_id)),
|
||||||
bursting: false,
|
bursting: false,
|
||||||
|
floor_items: Vec::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ pub enum ShipError {
|
|||||||
MonsterAlreadyDroppedItem(ClientId, u16),
|
MonsterAlreadyDroppedItem(ClientId, u16),
|
||||||
SliceError(#[from] std::array::TryFromSliceError),
|
SliceError(#[from] std::array::TryFromSliceError),
|
||||||
ItemError, // TODO: refine this
|
ItemError, // TODO: refine this
|
||||||
|
PickUpInvalidItemId(u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -157,14 +158,14 @@ pub struct ClientState {
|
|||||||
pub character: CharacterEntity,
|
pub character: CharacterEntity,
|
||||||
session: Session,
|
session: Session,
|
||||||
//guildcard: GuildCard,
|
//guildcard: GuildCard,
|
||||||
pub inventory: items::ActiveInventory,
|
pub inventory: items::CharacterInventory,
|
||||||
//bank: Bank,
|
//bank: Bank,
|
||||||
pub floor_items: Vec<items::ActiveItemOnFloor>,
|
pub floor_items: Vec<items::FloorItem>,
|
||||||
pub block: u32,
|
pub block: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientState {
|
impl ClientState {
|
||||||
pub fn new(user: UserAccountEntity, settings: UserSettingsEntity, character: CharacterEntity, inventory: items::ActiveInventory, /*bank: Bank,*/ session: Session) -> ClientState {
|
pub fn new(user: UserAccountEntity, settings: UserSettingsEntity, character: CharacterEntity, inventory: items::CharacterInventory, /*bank: Bank,*/ session: Session) -> ClientState {
|
||||||
ClientState {
|
ClientState {
|
||||||
user: user,
|
user: user,
|
||||||
settings: settings,
|
settings: settings,
|
||||||
@ -186,7 +187,7 @@ pub struct ShipServerState<EG: EntityGateway> {
|
|||||||
level_table: CharacterLevelTable,
|
level_table: CharacterLevelTable,
|
||||||
name: String,
|
name: String,
|
||||||
rooms: Rooms,
|
rooms: Rooms,
|
||||||
item_database: items::ActiveItemDatabase,
|
item_database: items::ItemManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<EG: EntityGateway> ShipServerState<EG> {
|
impl<EG: EntityGateway> ShipServerState<EG> {
|
||||||
@ -198,7 +199,7 @@ impl<EG: EntityGateway> ShipServerState<EG> {
|
|||||||
level_table: CharacterLevelTable::new(),
|
level_table: CharacterLevelTable::new(),
|
||||||
name: "Sona-Nyl".into(),
|
name: "Sona-Nyl".into(),
|
||||||
rooms: [None; MAX_ROOMS],
|
rooms: [None; MAX_ROOMS],
|
||||||
item_database: items::ActiveItemDatabase::new(),
|
item_database: items::ItemManager::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,6 +227,9 @@ impl<EG: EntityGateway> ShipServerState<EG> {
|
|||||||
GameMessage::RequestItem(request_item) => {
|
GameMessage::RequestItem(request_item) => {
|
||||||
handler::direct_message::request_item(id, request_item, &mut self.entity_gateway, &mut self.client_location, &mut self.clients, &mut self.rooms, &mut self.item_database).unwrap()
|
handler::direct_message::request_item(id, request_item, &mut self.entity_gateway, &mut self.client_location, &mut self.clients, &mut self.rooms, &mut self.item_database).unwrap()
|
||||||
},
|
},
|
||||||
|
GameMessage::PickupItem(pickup_item) => {
|
||||||
|
handler::direct_message::pickup_item(id, pickup_item, &mut self.entity_gateway, &mut self.client_location, &mut self.clients, &mut self.rooms, &mut self.item_database).unwrap()
|
||||||
|
},
|
||||||
_ => {
|
_ => {
|
||||||
let cmsg = msg.clone();
|
let cmsg = msg.clone();
|
||||||
Box::new(self.client_location.get_all_clients_by_client(id).unwrap().into_iter()
|
Box::new(self.client_location.get_all_clients_by_client(id).unwrap().into_iter()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user