@ -14,12 +14,12 @@ const INVENTORY_CAPACITY: usize = 30;
#[ derive(Debug, Clone) ]
#[ derive(Debug, Clone) ]
pub struct InventorySlot ( pub usize ) ;
pub struct InventorySlot ( pub usize ) ;
#[ derive(Debug, Clone) ]
#[ derive(Debug, Clone) ]
pub struct IndividualInventoryItem {
pub struct IndividualInventoryItem {
pub entity_id : ItemEntityId ,
pub entity_id : ItemEntityId ,
pub item_id : ClientItemId ,
pub item_id : ClientItemId ,
pub item : ItemDetail ,
pub item : ItemDetail ,
pub equipped : bool ,
}
}
impl IndividualInventoryItem {
impl IndividualInventoryItem {
@ -154,17 +154,6 @@ impl InventoryItem {
}
}
}
}
pub fn equipped ( & self ) -> bool {
match self {
InventoryItem ::Individual ( individual_inventory_item ) = > {
individual_inventory_item . equipped
} ,
InventoryItem ::Stacked ( _ ) = > {
false
}
}
}
pub fn as_client_bytes ( & self ) -> [ u8 ; 16 ] {
pub fn as_client_bytes ( & self ) -> [ u8 ; 16 ] {
match self {
match self {
InventoryItem ::Individual ( item ) = > {
InventoryItem ::Individual ( item ) = > {
@ -353,13 +342,15 @@ impl<'a> InventoryItemHandle<'a> {
pub struct CharacterInventory {
pub struct CharacterInventory {
item_id_counter : u32 ,
item_id_counter : u32 ,
items : Vec < InventoryItem > ,
items : Vec < InventoryItem > ,
equipped : EquippedEntity ,
}
}
impl CharacterInventory {
impl CharacterInventory {
pub fn new ( items : Vec < InventoryItem > ) -> CharacterInventory {
pub fn new ( items : Vec < InventoryItem > , equipped : & EquippedEntity ) -> CharacterInventory {
CharacterInventory {
CharacterInventory {
item_id_counter : 0 ,
item_id_counter : 0 ,
items : items ,
items : items ,
equipped : equipped . clone ( ) ,
}
}
}
}
@ -378,10 +369,22 @@ impl CharacterInventory {
inventory [ slot ] . data1 . copy_from_slice ( & bytes [ 0 . . 12 ] ) ;
inventory [ slot ] . data1 . copy_from_slice ( & bytes [ 0 . . 12 ] ) ;
inventory [ slot ] . data2 . copy_from_slice ( & bytes [ 12 . . 16 ] ) ;
inventory [ slot ] . data2 . copy_from_slice ( & bytes [ 12 . . 16 ] ) ;
inventory [ slot ] . item_id = item . item_id ( ) . 0 ;
inventory [ slot ] . item_id = item . 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 [ slot ] . equipped = 0 ;
inventory [ slot ] . flags = 0 ;
if let InventoryItem ::Individual ( individual_item ) = item {
if self . equipped . is_equipped ( & individual_item . entity_id ) {
if let ItemDetail ::Unit ( _ ) = individual_item . item {
inventory [ slot ] . data1 [ 4 ] = self . equipped . unit . iter ( )
. enumerate ( )
. find ( | ( _ , u_id ) | * * u_id = = Some ( individual_item . entity_id ) )
. map ( | ( a , b ) | a )
. unwrap_or ( 0 ) as u8
}
inventory [ slot ] . equipped = 1 ;
inventory [ slot ] . flags | = 8 ;
}
}
inventory
inventory
} )
} )
}
}
@ -413,7 +416,7 @@ impl CharacterInventory {
. filter ( | ( _ , item ) | {
. filter ( | ( _ , item ) | {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let ItemDetail ::Mag ( _ ) = & individual_inventory_item . item {
if let ItemDetail ::Mag ( _ ) = & individual_inventory_item . item {
return individual_inventory_item . equipped
return self . equipped . is_equipped ( & individual_inventory_item . entity_id )
}
}
}
}
false
false
@ -431,7 +434,7 @@ impl CharacterInventory {
. filter ( | ( _ , item ) | {
. filter ( | ( _ , item ) | {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let ItemDetail ::Armor ( _ ) = & individual_inventory_item . item {
if let ItemDetail ::Armor ( _ ) = & individual_inventory_item . item {
return individual_inventory_item . equipped
return self . equipped . is_equipped ( & individual_inventory_item . entity_id )
}
}
}
}
false
false
@ -449,7 +452,7 @@ impl CharacterInventory {
. filter ( | ( _ , item ) | {
. filter ( | ( _ , item ) | {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let ItemDetail ::Shield ( _ ) = & individual_inventory_item . item {
if let ItemDetail ::Shield ( _ ) = & individual_inventory_item . item {
return individual_inventory_item . equipped
return self . equipped . is_equipped ( & individual_inventory_item . entity_id )
}
}
}
}
false
false
@ -467,7 +470,7 @@ impl CharacterInventory {
. filter ( | ( _ , item ) | {
. filter ( | ( _ , item ) | {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let InventoryItem ::Individual ( individual_inventory_item ) = item {
if let ItemDetail ::Weapon ( _ ) = & individual_inventory_item . item {
if let ItemDetail ::Weapon ( _ ) = & individual_inventory_item . item {
return individual_inventory_item . equipped
return self . equipped . is_equipped ( & individual_inventory_item . entity_id )
}
}
}
}
false
false
@ -509,7 +512,6 @@ impl CharacterInventory {
entity_id : floor_item . entity_id ,
entity_id : floor_item . entity_id ,
item_id : floor_item . item_id ,
item_id : floor_item . item_id ,
item : floor_item . item . clone ( ) ,
item : floor_item . item . clone ( ) ,
equipped : false ,
} ) ) ;
} ) ) ;
if let Some ( InventoryItem ::Individual ( new_item ) ) = self . items . last ( ) {
if let Some ( InventoryItem ::Individual ( new_item ) ) = self . items . last ( ) {
@ -573,7 +575,6 @@ impl CharacterInventory {
entity_id : individual_bank_item . entity_id ,
entity_id : individual_bank_item . entity_id ,
item_id : individual_bank_item . item_id ,
item_id : individual_bank_item . item_id ,
item : individual_bank_item . item . clone ( ) ,
item : individual_bank_item . item . clone ( ) ,
equipped : false ,
} ) ) ;
} ) ) ;
( true , self . count ( ) - 1 )
( true , self . count ( ) - 1 )
} ,
} ,
@ -582,7 +583,7 @@ impl CharacterInventory {
. enumerate ( )
. enumerate ( )
. find_map ( | ( index , item ) | {
. find_map ( | ( index , item ) | {
if let InventoryItem ::Stacked ( stacked_inventory_item ) = item {
if let InventoryItem ::Stacked ( stacked_inventory_item ) = item {
if stacked_inventory _item . tool = = stacked_inventory_item . tool {
if stacked_bank _item . tool = = stacked_inventory_item . tool {
return Some ( ( index , stacked_inventory_item ) )
return Some ( ( index , stacked_inventory_item ) )
}
}
}
}
@ -639,6 +640,53 @@ impl CharacterInventory {
self . items = sorted_items ;
self . items = sorted_items ;
}
}
pub fn equip ( & mut self , id : & ClientItemId , equip_slot : u8 ) {
for item in & self . items {
if let InventoryItem ::Individual ( inventory_item ) = item {
if inventory_item . item_id = = * id {
match inventory_item . item {
ItemDetail ::Weapon ( _ ) = > self . equipped . weapon = Some ( inventory_item . entity_id ) ,
ItemDetail ::Armor ( _ ) = > self . equipped . armor = Some ( inventory_item . entity_id ) ,
ItemDetail ::Shield ( _ ) = > self . equipped . shield = Some ( inventory_item . entity_id ) ,
ItemDetail ::Unit ( _ ) = > {
if let Some ( unit ) = self . equipped . unit . get_mut ( equip_slot as usize ) {
* unit = Some ( inventory_item . entity_id )
}
}
ItemDetail ::Mag ( _ ) = > self . equipped . mag = Some ( inventory_item . entity_id ) ,
_ = > { }
}
}
}
}
}
pub fn unequip ( & mut self , id : & ClientItemId ) {
for item in & self . items {
if let InventoryItem ::Individual ( inventory_item ) = item {
if inventory_item . item_id = = * id {
match inventory_item . item {
ItemDetail ::Weapon ( _ ) = > self . equipped . weapon = None ,
ItemDetail ::Armor ( _ ) = > {
self . equipped . armor = None ;
self . equipped . unit = [ None ; 4 ] ;
}
ItemDetail ::Shield ( _ ) = > self . equipped . shield = None ,
ItemDetail ::Unit ( _ ) = > {
for unit in self . equipped . unit . iter_mut ( ) {
if * unit = = Some ( inventory_item . entity_id ) {
* unit = None
}
}
}
ItemDetail ::Mag ( _ ) = > self . equipped . mag = Some ( inventory_item . entity_id ) ,
_ = > { }
}
}
}
}
}
pub fn as_inventory_entity ( & self , character_id : & CharacterEntityId ) -> InventoryEntity {
pub fn as_inventory_entity ( & self , character_id : & CharacterEntityId ) -> InventoryEntity {
InventoryEntity {
InventoryEntity {
items : self . items . iter ( )
items : self . items . iter ( )
@ -673,25 +721,7 @@ impl CharacterInventory {
}
}
pub fn as_equipped_entity ( & self ) -> EquippedEntity {
pub fn as_equipped_entity ( & self ) -> EquippedEntity {
self . items . iter ( )
. fold ( ( EquippedEntity ::default ( ) , 0 ) , | ( mut equipped , mut unit_slot ) , item | {
if let InventoryItem ::Individual ( individual_item ) = item {
if individual_item . equipped {
match individual_item . item {
ItemDetail ::Weapon ( _ ) = > equipped . weapon = Some ( individual_item . entity_id ) ,
ItemDetail ::Armor ( _ ) = > equipped . armor = Some ( individual_item . entity_id ) ,
ItemDetail ::Shield ( _ ) = > equipped . shield = Some ( individual_item . entity_id ) ,
ItemDetail ::Unit ( _ ) = > {
equipped . unit [ unit_slot ] = Some ( individual_item . entity_id ) ;
unit_slot + = 1 ;
}
ItemDetail ::Mag ( _ ) = > equipped . mag = Some ( individual_item . entity_id ) ,
_ = > { } ,
}
}
}
( equipped , unit_slot )
} ) . 0
self . equipped . clone ( )
}
}
}
}