Skip to content

Commit

Permalink
Improve item handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed May 9, 2024
1 parent 3088197 commit 278492b
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 117 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 39 additions & 36 deletions korangar/src/interface/elements/containers/equipment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ pub struct EquipmentContainer {
impl EquipmentContainer {
pub fn new(items: PlainRemote<Vec<Item>>) -> Self {
const SLOT_POSITIONS: [EquipPosition; 9] = [
EquipPosition::HeadTop,
EquipPosition::HeadMiddle,
EquipPosition::HeadLower,
EquipPosition::Armor,
EquipPosition::Garment,
EquipPosition::Shoes,
EquipPosition::LeftHand,
EquipPosition::RightHand,
EquipPosition::Ammo,
EquipPosition::HEAD_TOP,
EquipPosition::HEAD_MIDDLE,
EquipPosition::HEAD_LOWER,
EquipPosition::ARMOR,
EquipPosition::GARMENT,
EquipPosition::SHOES,
EquipPosition::LEFT_HAND,
EquipPosition::RIGHT_HAND,
EquipPosition::AMMO,
];

let elements = {
Expand All @@ -42,31 +42,31 @@ impl EquipmentContainer {
.map(|index| {
let slot = SLOT_POSITIONS[index];
let display_name = match slot {
EquipPosition::None => panic!(),
EquipPosition::HeadLower => "Head lower",
EquipPosition::HeadMiddle => "Head middle",
EquipPosition::HeadTop => "Head top",
EquipPosition::RightHand => "Right hand",
EquipPosition::LeftHand => "Left hand",
EquipPosition::Armor => "Armor",
EquipPosition::Shoes => "Shoes",
EquipPosition::Garment => "Garment",
EquipPosition::LeftAccessory => "Left accessory",
EquipPosition::RigthAccessory => "Right accessory",
EquipPosition::CostumeHeadTop => "Costume head top",
EquipPosition::CostumeHeadMiddle => "Costume head middle",
EquipPosition::CostumeHeadLower => "Costume head lower",
EquipPosition::CostumeGarment => "Costume garment",
EquipPosition::Ammo => "Ammo",
EquipPosition::ShadowArmor => "Shadow ammo",
EquipPosition::ShadowWeapon => "Shadow weapon",
EquipPosition::ShadowShield => "Shadow shield",
EquipPosition::ShadowShoes => "Shadow shoes",
EquipPosition::ShadowRightAccessory => "Shadow right accessory",
EquipPosition::ShadowLeftAccessory => "Shadow left accessory",
EquipPosition::LeftRightAccessory => "Accessory",
EquipPosition::LeftRightHand => "Two hand weapon",
EquipPosition::ShadowLeftRightAccessory => "Shadow accessory",
_ if slot.contains(EquipPosition::HEAD_LOWER) => "Head lower",
_ if slot.contains(EquipPosition::HEAD_MIDDLE) => "Head middle",
_ if slot.contains(EquipPosition::HEAD_TOP) => "Head top",
_ if slot.contains(EquipPosition::RIGHT_HAND) => "Right hand",
_ if slot.contains(EquipPosition::LEFT_HAND) => "Left hand",
_ if slot.contains(EquipPosition::ARMOR) => "Armor",
_ if slot.contains(EquipPosition::SHOES) => "Shoes",
_ if slot.contains(EquipPosition::GARMENT) => "Garment",
_ if slot.contains(EquipPosition::LEFT_ACCESSORY) => "Left accessory",
_ if slot.contains(EquipPosition::RIGTH_ACCESSORY) => "Right accessory",
_ if slot.contains(EquipPosition::COSTUME_HEAD_TOP) => "Costume head top",
_ if slot.contains(EquipPosition::COSTUME_HEAD_MIDDLE) => "Costume head middle",
_ if slot.contains(EquipPosition::COSTUME_HEAD_LOWER) => "Costume head lower",
_ if slot.contains(EquipPosition::COSTUME_GARMENT) => "Costume garment",
_ if slot.contains(EquipPosition::AMMO) => "Ammo",
_ if slot.contains(EquipPosition::SHADOW_ARMOR) => "Shadow ammo",
_ if slot.contains(EquipPosition::SHADOW_WEAPON) => "Shadow weapon",
_ if slot.contains(EquipPosition::SHADOW_SHIELD) => "Shadow shield",
_ if slot.contains(EquipPosition::SHADOW_SHOES) => "Shadow shoes",
_ if slot.contains(EquipPosition::SHADOW_RIGHT_ACCESSORY) => "Shadow right accessory",
_ if slot.contains(EquipPosition::SHADOW_LEFT_ACCESSORY) => "Shadow left accessory",
_ if slot.contains(EquipPosition::LEFT_RIGHT_ACCESSORY) => "Accessory",
_ if slot.contains(EquipPosition::LEFT_RIGHT_HAND) => "Two hand weapon",
_ if slot.contains(EquipPosition::SHADOW_LEFT_RIGHT_ACCESSORY) => "Shadow accessory",
_ => panic!("no display name for equip position"),
};

let text = Text::default()
Expand All @@ -75,12 +75,15 @@ impl EquipmentContainer {
.with_width(dimension_bound!(!))
.wrap();

let item = items.iter().find(|item| item.equipped_position == slot).cloned();
let item = items.iter().find(|item| item.equipped_position.contains(slot)).cloned();

let item_box = ItemBox::new(
item,
ItemSource::Equipment { position: slot },
Box::new(move |mouse_mode| matches!(mouse_mode, MouseInputMode::MoveItem(_, item) if item.equip_position == slot)),
// ItemSource::Equipment { position: EquipPosition::HEAD_TOP | EquipPosition::HEAD_MIDDLE },
Box::new(
move |mouse_mode| matches!(mouse_mode, MouseInputMode::MoveItem(_, item) if item.equip_position.contains(slot)),
),
);

Container::new(vec![item_box.wrap(), text]).wrap()
Expand Down
7 changes: 7 additions & 0 deletions korangar/src/interface/elements/containers/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ impl PacketHistoryCallback {
version: 0,
}
}

pub fn clear_all(&self) {
let mut lock = self.buffer_pointer.lock().unwrap();

lock.0.clear();
lock.1 += 1;
}
}

impl PacketCallback for PacketHistoryCallback {
Expand Down
14 changes: 13 additions & 1 deletion korangar/src/inventory/hotbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ impl Hotbar {
}

/// Clear the slot without notifying the map server.
pub fn clear_slot(&mut self, slot: HotbarSlot) {
pub fn unset_slot(&mut self, slot: HotbarSlot) {
self.skills.mutate(|skills| {
skills[slot.0 as usize] = None;
});
}

/// Clear the slot and notify the map server.
pub fn clear_slot<Callback>(&mut self, networking_system: &mut NetworkingSystem<Callback>, slot: HotbarSlot)
where
Callback: PacketCallback,
{
let _ = networking_system.set_hotkey_data(HotbarTab(0), slot, HotkeyData::UNBOUND);

self.skills.mutate(|skills| {
skills[slot.0 as usize] = None;
});
Expand Down
9 changes: 5 additions & 4 deletions korangar/src/inventory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::loaders::{GameFileLoader, ScriptLoader, TextureLoader};
/*enum ItemDetails {
Regular {
amount: u16,
fags: u8, // bit 1 - is_identified; bit 2 - place_in_etc_tab;
flags: u8, // bit 1 - is_identified; bit 2 - place_in_etc_tab;
},
Equippable {
Expand All @@ -26,7 +26,7 @@ use crate::loaders::{GameFileLoader, ScriptLoader, TextureLoader};
option_data: [ItemOptions; 5], // fix count
refinement_level: u8,
enchantment_level: u8,
fags: u8, // bit 1 - is_identified; bit 2 - is_damaged; bit 3 - place_in_etc_tab
flags: u8, // bit 1 - is_identified; bit 2 - is_damaged; bit 3 - place_in_etc_tab
},
}*/

Expand Down Expand Up @@ -59,7 +59,7 @@ impl Inventory {
let items = item_data
.into_iter()
.map(|item_data| {
let resource_name = script_loader.get_item_resource_from_id(item_data.id);
let resource_name = script_loader.get_item_resource_from_id(item_data.id, item_data.is_identified);
let full_path = format!("À¯ÀúÀÎÅÍÆäÀ̽º\\item\\{resource_name}.bmp");
let texture = texture_loader.get(&full_path, game_file_loader).unwrap();

Expand All @@ -83,6 +83,7 @@ impl Inventory {
script_loader: &ScriptLoader,
item_index: ItemIndex,
item_id: ItemId,
is_identified: bool,
equip_position: EquipPosition,
equipped_position: EquipPosition,
) {
Expand All @@ -92,7 +93,7 @@ impl Inventory {
return ValueState::Mutated(());
}

let resource_name = script_loader.get_item_resource_from_id(item_id);
let resource_name = script_loader.get_item_resource_from_id(item_id, is_identified);
let full_path = format!("À¯ÀúÀÎÅÍÆäÀ̽º\\item\\{resource_name}.bmp");
let texture = texture_loader.get(&full_path, game_file_loader).unwrap();
let item = Item {
Expand Down
15 changes: 6 additions & 9 deletions korangar/src/loaders/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,20 @@ end
}*/

// TODO: move this to a different class that utilizes the script loader
pub fn get_item_resource_from_id(&self, item_id: ItemId) -> String {
pub fn get_item_resource_from_id(&self, item_id: ItemId, is_identified: bool) -> String {
use mlua::prelude::*;

let globals = self.state.globals();
let lookup_name = match is_identified {
true => "identifiedResourceName",
false => "undidentifiedResourceName",
};

globals
.get::<_, LuaTable>("tbl")
.unwrap()
.get::<_, LuaTable>(item_id.0)
.map(|table| {
table
.get::<_, LuaString>("unidentifiedResourceName")
.unwrap()
.to_str()
.unwrap()
.to_owned()
})
.map(|table| table.get::<_, LuaString>(lookup_name).unwrap().to_str().unwrap().to_owned())
.unwrap_or_else(|_| "»ç°ú".to_owned())
}
}
22 changes: 17 additions & 5 deletions korangar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,13 +843,20 @@ fn main() {
NetworkEvent::SetInventory { items } => {
player_inventory.fill(&mut game_file_loader, &mut texture_loader, &script_loader, items);
}
NetworkEvent::AddIventoryItem(item_index, item_data, equip_position, equipped_position) => {
NetworkEvent::AddIventoryItem {
item_index,
item_id,
is_identified,
equip_position,
equipped_position,
}=> {
player_inventory.add_item(
&mut game_file_loader,
&mut texture_loader,
&script_loader,
item_index,
item_data,
item_id,
is_identified,
equip_position,
equipped_position,
);
Expand All @@ -863,6 +870,10 @@ fn main() {
NetworkEvent::ChangeJob(account_id, job_id) => {
let entity = entities.iter_mut().find(|entity| entity.get_entity_id().0 == account_id.0).unwrap();

// FIX: A job change does not automatically send packets for the
// inventory and for unequipping items. We should probably manually
// request a full list of items and the hotbar.

entity.set_job(job_id as usize);
entity.reload_sprite(&mut game_file_loader, &mut sprite_loader, &mut action_loader, &script_loader);
}
Expand Down Expand Up @@ -959,13 +970,14 @@ fn main() {
match hotkey {
HotkeyState::Bound(hotkey) => {
let Some(mut skill) = player_skill_tree.find_skill(SkillId(hotkey.skill_id as u16)) else {
panic!("Skill in hotbar is not learned by the player");
hotbar.clear_slot(&mut networking_system, HotbarSlot(index as u16));
continue;
};

skill.skill_level = hotkey.quantity_or_skill_level;
hotbar.set_slot(HotbarSlot(index as u16), skill);
},
HotkeyState::Unbound => hotbar.clear_slot(HotbarSlot(index as u16)),
HotkeyState::Unbound => hotbar.unset_slot(HotbarSlot(index as u16)),
}
}
}
Expand Down Expand Up @@ -1247,7 +1259,7 @@ fn main() {
interface.open_window(&application, &mut focus_state, &PacketWindow::new(packet_callback.remote(), PlainTrackedState::default()))
}
#[cfg(feature = "debug")]
UserEvent::ClearPacketHistory => /*networking_system.clear_packet_history()*/ {},
UserEvent::ClearPacketHistory => packet_callback.clear_all(),
#[cfg(feature = "debug")]
UserEvent::CameraLookAround(offset) => debug_camera.look_around(offset),
#[cfg(feature = "debug")]
Expand Down
8 changes: 7 additions & 1 deletion korangar_networking/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ pub enum NetworkEvent {
SetInventory {
items: Vec<InventoryItem>,
},
AddIventoryItem(ItemIndex, ItemId, EquipPosition, EquipPosition),
AddIventoryItem {
item_index: ItemIndex,
item_id: ItemId,
is_identified: bool,
equip_position: EquipPosition,
equipped_position: EquipPosition,
},
SkillTree(Vec<SkillInformation>),
UpdateEquippedPosition {
index: ItemIndex,
Expand Down
1 change: 1 addition & 0 deletions korangar_networking/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ragnarok_packets::{EquipPosition, ItemId, ItemIndex};
pub struct InventoryItem {
pub index: ItemIndex,
pub id: ItemId,
pub is_identified: bool,
pub equip_position: EquipPosition,
pub equipped_position: EquipPosition,
}
16 changes: 11 additions & 5 deletions korangar_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,9 @@ where
packet.item_information.into_iter().map(|item| InventoryItem {
index: item.index,
id: item.item_id,
equip_position: EquipPosition::None,
equipped_position: EquipPosition::None,
is_identified: item.flags.contains(RegularItemFlags::IDENTIFIED),
equip_position: EquipPosition::NONE,
equipped_position: EquipPosition::NONE,
}),
);
NoNetworkEvents
Expand All @@ -703,6 +704,7 @@ where
packet.item_information.into_iter().map(|item| InventoryItem {
index: item.index,
id: item.item_id,
is_identified: item.flags.contains(EquippableItemFlags::IDENTIFIED),
equip_position: item.equip_position,
equipped_position: item.equipped_position,
}),
Expand Down Expand Up @@ -787,8 +789,12 @@ where
QuestEffect::None => NetworkEvent::RemoveQuestEffect(packet.entity_id),
_ => NetworkEvent::AddQuestEffect(packet),
})?;
packet_handler.register(|packet: ItemPickupPacket| {
NetworkEvent::AddIventoryItem(packet.index, packet.item_id, packet.equip_position, EquipPosition::None)
packet_handler.register(|packet: ItemPickupPacket| NetworkEvent::AddIventoryItem {
item_index: packet.index,
item_id: packet.item_id,
is_identified: packet.is_identified != 0,
equip_position: packet.equip_position,
equipped_position: EquipPosition::NONE,
})?;
packet_handler.register_noop::<RemoveItemFromInventoryPacket>()?;
packet_handler.register(|packet: ServerTickPacket| NetworkEvent::UpdateClientTick(packet.client_tick))?;
Expand Down Expand Up @@ -818,7 +824,7 @@ where
packet_handler.register(|packet: RequestUnequipItemStatusPacket| match packet.result {
RequestUnequipItemStatus::Success => Some(NetworkEvent::UpdateEquippedPosition {
index: packet.inventory_index,
equipped_position: EquipPosition::None,
equipped_position: EquipPosition::NONE,
}),
_ => None,
})?;
Expand Down
1 change: 1 addition & 0 deletions ragnarok_packets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bitflags = { workspace = true }
derive-new = { workspace = true }
korangar_interface = { workspace = true, optional = true }
ragnarok_bytes = { workspace = true, features = ["derive"] }
Expand Down
Loading

0 comments on commit 278492b

Please sign in to comment.