|
@ -71,8 +71,8 @@ pub struct ItemManager { |
|
|
room_item_id_counter: HashMap<RoomId, Box<dyn FnMut() -> ClientItemId + Send>>,
|
|
|
room_item_id_counter: HashMap<RoomId, Box<dyn FnMut() -> ClientItemId + Send>>,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl ItemManager {
|
|
|
|
|
|
pub fn new() -> ItemManager {
|
|
|
|
|
|
|
|
|
impl Default for ItemManager {
|
|
|
|
|
|
fn default() -> ItemManager {
|
|
|
ItemManager {
|
|
|
ItemManager {
|
|
|
id_counter: 0,
|
|
|
id_counter: 0,
|
|
|
character_inventory: HashMap::new(),
|
|
|
character_inventory: HashMap::new(),
|
|
@ -83,7 +83,9 @@ impl ItemManager { |
|
|
room_item_id_counter: HashMap::new(),
|
|
|
room_item_id_counter: HashMap::new(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ItemManager {
|
|
|
pub fn next_global_item_id(&mut self) -> ClientItemId {
|
|
|
pub fn next_global_item_id(&mut self) -> ClientItemId {
|
|
|
self.id_counter += 1;
|
|
|
self.id_counter += 1;
|
|
|
ClientItemId(self.id_counter)
|
|
|
ClientItemId(self.id_counter)
|
|
@ -110,11 +112,11 @@ impl ItemManager { |
|
|
entity_ids: items.iter().map(|i| i.id).collect(),
|
|
|
entity_ids: items.iter().map(|i| i.id).collect(),
|
|
|
item_id: self.next_global_item_id(),
|
|
|
item_id: self.next_global_item_id(),
|
|
|
tool: items.get(0)
|
|
|
tool: items.get(0)
|
|
|
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
|
|
|
|
|
|
|
|
|
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
|
|
|
.item
|
|
|
.item
|
|
|
.clone()
|
|
|
.clone()
|
|
|
.as_tool()
|
|
|
.as_tool()
|
|
|
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
|
|
|
|
|
|
|
|
|
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
|
|
|
})
|
|
|
})
|
|
|
},
|
|
|
},
|
|
|
})
|
|
|
})
|
|
@ -137,11 +139,11 @@ impl ItemManager { |
|
|
entity_ids: items.iter().map(|i| i.id).collect(),
|
|
|
entity_ids: items.iter().map(|i| i.id).collect(),
|
|
|
item_id: self.next_global_item_id(),
|
|
|
item_id: self.next_global_item_id(),
|
|
|
tool: items.get(0)
|
|
|
tool: items.get(0)
|
|
|
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
|
|
|
|
|
|
|
|
|
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
|
|
|
.item
|
|
|
.item
|
|
|
.clone()
|
|
|
.clone()
|
|
|
.as_tool()
|
|
|
.as_tool()
|
|
|
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
|
|
|
|
|
|
|
|
|
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
|
|
|
})
|
|
|
})
|
|
|
},
|
|
|
},
|
|
|
})
|
|
|
})
|
|
@ -160,18 +162,15 @@ impl ItemManager { |
|
|
inventory.initialize_item_ids(base_inventory_id);
|
|
|
inventory.initialize_item_ids(base_inventory_id);
|
|
|
let base_bank_id = ((area_client.local_client.id() as u32) << 21) | 0x20000;
|
|
|
let base_bank_id = ((area_client.local_client.id() as u32) << 21) | 0x20000;
|
|
|
let default_bank = self.character_bank.get_mut(&character.id);
|
|
|
let default_bank = self.character_bank.get_mut(&character.id);
|
|
|
match default_bank {
|
|
|
|
|
|
Some(default_bank) => {
|
|
|
|
|
|
|
|
|
if let Some(default_bank ) = default_bank {
|
|
|
default_bank.initialize_item_ids(base_bank_id);
|
|
|
default_bank.initialize_item_ids(base_bank_id);
|
|
|
},
|
|
|
|
|
|
None => {},
|
|
|
|
|
|
}
|
|
|
}
|
|
|
self.character_room.insert(character.id, room_id);
|
|
|
self.character_room.insert(character.id, room_id);
|
|
|
self.character_floor.insert(character.id, RoomFloorItems::new());
|
|
|
|
|
|
self.room_floor.entry(room_id).or_insert(RoomFloorItems::new());
|
|
|
|
|
|
|
|
|
self.character_floor.insert(character.id, RoomFloorItems::default());
|
|
|
|
|
|
self.room_floor.entry(room_id).or_insert_with(RoomFloorItems::default);
|
|
|
|
|
|
|
|
|
let mut inc = 0x00810000;
|
|
|
let mut inc = 0x00810000;
|
|
|
self.room_item_id_counter.entry(room_id).or_insert(Box::new(move || {
|
|
|
|
|
|
|
|
|
self.room_item_id_counter.entry(room_id).or_insert_with(|| Box::new(move || {
|
|
|
inc += 1;
|
|
|
inc += 1;
|
|
|
ClientItemId(inc)
|
|
|
ClientItemId(inc)
|
|
|
}));
|
|
|
}));
|
|
@ -202,13 +201,11 @@ impl ItemManager { |
|
|
pub fn remove_character_from_room(&mut self, character: &CharacterEntity) {
|
|
|
pub fn remove_character_from_room(&mut self, character: &CharacterEntity) {
|
|
|
self.character_inventory.remove(&character.id);
|
|
|
self.character_inventory.remove(&character.id);
|
|
|
self.character_floor.remove(&character.id);
|
|
|
self.character_floor.remove(&character.id);
|
|
|
self.character_room.remove(&character.id)
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.map(|room| {
|
|
|
|
|
|
if self.character_room.iter().find(|(_, r)| *r == room).is_none() {
|
|
|
|
|
|
|
|
|
if let Some(room) = self.character_room.remove(&character.id).as_ref() {
|
|
|
|
|
|
if self.character_room.iter().any(|(_, r)| r == room) {
|
|
|
self.room_floor.remove(room);
|
|
|
self.room_floor.remove(room);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn get_floor_item_by_id(&self, character: &CharacterEntity, item_id: ClientItemId) -> Result<(&FloorItem, FloorType), anyhow::Error> {
|
|
|
pub fn get_floor_item_by_id(&self, character: &CharacterEntity, item_id: ClientItemId) -> Result<(&FloorItem, FloorType), anyhow::Error> {
|
|
@ -220,7 +217,7 @@ impl ItemManager { |
|
|
.or_else(|| {
|
|
|
.or_else(|| {
|
|
|
shared_floor.get_item_by_id(item_id).map(|item| (item, FloorType::Shared))
|
|
|
shared_floor.get_item_by_id(item_id).map(|item| (item, FloorType::Shared))
|
|
|
})
|
|
|
})
|
|
|
.ok_or(ItemManagerError::NoSuchItemId(item_id).into())
|
|
|
|
|
|
|
|
|
.ok_or_else(|| ItemManagerError::NoSuchItemId(item_id).into())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub async fn character_picks_up_item<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, item_id: ClientItemId)
|
|
|
pub async fn character_picks_up_item<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, item_id: ClientItemId)
|
|
@ -228,7 +225,7 @@ impl ItemManager { |
|
|
let local_floor = self.character_floor.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let local_floor = self.character_floor.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
|
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
|
let floor_item = local_floor.get_item_handle_by_id(item_id)
|
|
|
let floor_item = local_floor.get_item_handle_by_id(item_id)
|
|
|
.or_else(|| {
|
|
|
.or_else(|| {
|
|
@ -238,7 +235,7 @@ impl ItemManager { |
|
|
|
|
|
|
|
|
let trigger_create_item = match floor_item.item() {
|
|
|
let trigger_create_item = match floor_item.item() {
|
|
|
Some(FloorItem::Individual(individual_floor_item)) => {
|
|
|
Some(FloorItem::Individual(individual_floor_item)) => {
|
|
|
let new_inventory_item = inventory.pick_up_individual_floor_item(&individual_floor_item);
|
|
|
|
|
|
|
|
|
let new_inventory_item = inventory.pick_up_individual_floor_item(individual_floor_item);
|
|
|
match new_inventory_item {
|
|
|
match new_inventory_item {
|
|
|
Some((new_inventory_item, slot)) => {
|
|
|
Some((new_inventory_item, slot)) => {
|
|
|
entity_gateway.change_item_location(
|
|
|
entity_gateway.change_item_location(
|
|
@ -247,7 +244,7 @@ impl ItemManager { |
|
|
character_id: character.id,
|
|
|
character_id: character.id,
|
|
|
}
|
|
|
}
|
|
|
).await?;
|
|
|
).await?;
|
|
|
if let Some(_) = new_inventory_item.mag() {
|
|
|
|
|
|
|
|
|
if new_inventory_item.mag().is_some() {
|
|
|
entity_gateway.change_mag_owner(&new_inventory_item.entity_id, character).await?;
|
|
|
entity_gateway.change_mag_owner(&new_inventory_item.entity_id, character).await?;
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
},
|
|
@ -258,13 +255,13 @@ impl ItemManager { |
|
|
TriggerCreateItem::Yes
|
|
|
TriggerCreateItem::Yes
|
|
|
},
|
|
|
},
|
|
|
Some(FloorItem::Stacked(stacked_floor_item)) => {
|
|
|
Some(FloorItem::Stacked(stacked_floor_item)) => {
|
|
|
let new_inventory_item = inventory.pick_up_stacked_floor_item(&stacked_floor_item);
|
|
|
|
|
|
|
|
|
let new_inventory_item = inventory.pick_up_stacked_floor_item(stacked_floor_item);
|
|
|
|
|
|
|
|
|
match new_inventory_item {
|
|
|
match new_inventory_item {
|
|
|
Some((new_inventory_item, slot)) => {
|
|
|
Some((new_inventory_item, slot)) => {
|
|
|
for entity_id in &new_inventory_item.entity_ids {
|
|
|
for entity_id in &new_inventory_item.entity_ids {
|
|
|
entity_gateway.change_item_location(
|
|
|
entity_gateway.change_item_location(
|
|
|
&entity_id,
|
|
|
|
|
|
|
|
|
entity_id,
|
|
|
ItemLocation::Inventory {
|
|
|
ItemLocation::Inventory {
|
|
|
character_id: character.id,
|
|
|
character_id: character.id,
|
|
|
}
|
|
|
}
|
|
@ -288,7 +285,7 @@ impl ItemManager { |
|
|
return Err(ItemManagerError::CouldNotAddToInventory(item_id).into());
|
|
|
return Err(ItemManagerError::CouldNotAddToInventory(item_id).into());
|
|
|
}
|
|
|
}
|
|
|
character.meseta = std::cmp::min(character.meseta + meseta_floor_item.meseta.0, 999999);
|
|
|
character.meseta = std::cmp::min(character.meseta + meseta_floor_item.meseta.0, 999999);
|
|
|
entity_gateway.save_character(&character).await?;
|
|
|
|
|
|
|
|
|
entity_gateway.save_character(character).await?;
|
|
|
TriggerCreateItem::No
|
|
|
TriggerCreateItem::No
|
|
|
},
|
|
|
},
|
|
|
None => {
|
|
|
None => {
|
|
@ -381,9 +378,9 @@ impl ItemManager { |
|
|
},
|
|
|
},
|
|
|
};
|
|
|
};
|
|
|
|
|
|
|
|
|
self.character_floor.entry(character.id).or_insert(RoomFloorItems::new()).add_item(floor_item);
|
|
|
|
|
|
|
|
|
self.character_floor.entry(character.id).or_insert_with(RoomFloorItems::default).add_item(floor_item);
|
|
|
// TODO: make these real errors
|
|
|
// TODO: make these real errors
|
|
|
self.character_floor.get(&character.id).ok_or(ItemManagerError::Idunnoman)?.get_item_by_id(item_id).ok_or(ItemManagerError::Idunnoman.into())
|
|
|
|
|
|
|
|
|
self.character_floor.get(&character.id).ok_or(ItemManagerError::Idunnoman)?.get_item_by_id(item_id).ok_or_else(|| ItemManagerError::Idunnoman.into())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub async fn player_drop_item_on_shared_floor<EG: EntityGateway>(&mut self,
|
|
|
pub async fn player_drop_item_on_shared_floor<EG: EntityGateway>(&mut self,
|
|
@ -395,7 +392,7 @@ impl ItemManager { |
|
|
-> Result<(), anyhow::Error> {
|
|
|
-> Result<(), anyhow::Error> {
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
|
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
|
let dropped_inventory_item = inventory.take_item_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
let dropped_inventory_item = inventory.take_item_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
|
|
|
|
|
@ -439,12 +436,12 @@ impl ItemManager { |
|
|
amount: u32)
|
|
|
amount: u32)
|
|
|
-> Result<FloorItem, anyhow::Error> {
|
|
|
-> Result<FloorItem, anyhow::Error> {
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
|
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
if character.meseta < amount {
|
|
|
if character.meseta < amount {
|
|
|
return Err(ItemManagerError::CouldNotDropMeseta.into())
|
|
|
return Err(ItemManagerError::CouldNotDropMeseta.into())
|
|
|
}
|
|
|
}
|
|
|
character.meseta -= amount;
|
|
|
character.meseta -= amount;
|
|
|
entity_gateway.save_character(&character).await?;
|
|
|
|
|
|
|
|
|
entity_gateway.save_character(character).await?;
|
|
|
|
|
|
|
|
|
let item_id = self.room_item_id_counter.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?();
|
|
|
let item_id = self.room_item_id_counter.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?();
|
|
|
let floor_item = FloorItem::Meseta(MesetaFloorItem {
|
|
|
let floor_item = FloorItem::Meseta(MesetaFloorItem {
|
|
@ -470,7 +467,7 @@ impl ItemManager { |
|
|
-> Result<&StackedFloorItem, anyhow::Error> {
|
|
|
-> Result<&StackedFloorItem, anyhow::Error> {
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
|
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
|
|
|
|
|
|
|
|
|
let item_to_split = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
let item_to_split = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
|
|
|
|
|
|
|
|
@ -594,7 +591,7 @@ impl ItemManager { |
|
|
|
|
|
|
|
|
entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
|
|
|
entity_gateway.set_character_bank(&character.id, &bank.as_bank_entity(&character.id, &BankName("".into())), BankName("".into())).await?;
|
|
|
entity_gateway.set_character_bank(&character.id, &bank.as_bank_entity(&character.id, &BankName("".into())), BankName("".into())).await?;
|
|
|
inventory.slot(inventory_item_slot).ok_or(ItemManagerError::Idunnoman.into())
|
|
|
|
|
|
|
|
|
inventory.slot(inventory_item_slot).ok_or_else(|| ItemManagerError::Idunnoman.into())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub async fn player_feeds_mag_item<EG: EntityGateway>(&mut self,
|
|
|
pub async fn player_feeds_mag_item<EG: EntityGateway>(&mut self,
|
|
@ -881,7 +878,7 @@ impl ItemManager { |
|
|
let sorted_inventory_items: Vec<InventoryItem> = item_ids.iter()
|
|
|
let sorted_inventory_items: Vec<InventoryItem> = item_ids.iter()
|
|
|
.filter(|&client_item_id| *client_item_id < 0xFFFFFFFF)
|
|
|
.filter(|&client_item_id| *client_item_id < 0xFFFFFFFF)
|
|
|
.map(|&client_item_id| inventory.get_item_by_id(ClientItemId(client_item_id)))
|
|
|
.map(|&client_item_id| inventory.get_item_by_id(ClientItemId(client_item_id)))
|
|
|
.filter(|&x| x.is_some() == true)
|
|
|
|
|
|
|
|
|
.filter(|&x| x.is_some())
|
|
|
.map(|x| x.cloned().unwrap())
|
|
|
.map(|x| x.cloned().unwrap())
|
|
|
.collect();
|
|
|
.collect();
|
|
|
|
|
|
|
|
|