From 7870bf103afb33e262161094f55c8253c7ec179c Mon Sep 17 00:00:00 2001 From: KimLS Date: Fri, 27 Feb 2015 02:40:44 -0800 Subject: [PATCH] Working on can equip, putting it in the general inventory class. --- common/inventory.cpp | 44 +++++++++++++++++++++++++++++++++++++++- common/inventory.h | 3 ++- tests/CMakeLists.txt | 2 +- tests/inventory_test.h | 46 ++++++++++++++++++++++++++++++++++-------- tests/main.cpp | 3 +++ zone/client.h | 1 - zone/inventory.cpp | 40 ++---------------------------------- zone/mob.cpp | 3 ++- 8 files changed, 91 insertions(+), 51 deletions(-) diff --git a/common/inventory.cpp b/common/inventory.cpp index ecdf35934..878e9f450 100644 --- a/common/inventory.cpp +++ b/common/inventory.cpp @@ -98,10 +98,14 @@ const std::string EQEmu::InventorySlot::ToString() const { struct EQEmu::Inventory::impl { std::map containers_; + int race_; + int class_; }; -EQEmu::Inventory::Inventory() { +EQEmu::Inventory::Inventory(int race, int class_) { impl_ = new impl; + impl_->race_ = race; + impl_->class_ = class_; } EQEmu::Inventory::~Inventory() { @@ -231,6 +235,44 @@ EQEmu::InventorySlot EQEmu::Inventory::CalcSlotFromMaterial(int material) { } } +bool EQEmu::Inventory::CanEquip(std::shared_ptr inst, const EQEmu::InventorySlot &slot) { + if(!inst) { + return false; + } + + if(slot.Type() != 0) { + return false; + } + + if(!EQEmu::ValueWithin(slot.Slot(), EQEmu::PersonalSlotCharm, EQEmu::PersonalSlotAmmo)) { + return false; + } + + auto item = inst->GetItem(); + //check slot + + int use_slot = -1; + if(slot.Slot() == EQEmu::PersonalSlotPowerSource) { + use_slot = EQEmu::PersonalSlotAmmo; + } + else if(slot.Slot() == EQEmu::PersonalSlotAmmo) { + use_slot = EQEmu::PersonalSlotPowerSource; + } + else { + use_slot = slot.Slot(); + } + + if(!(item->Slots & (1 << use_slot))) { + return false; + } + + if(!item->IsEquipable(impl_->race_, impl_->class_)) { + return false; + } + + return true; +} + bool EQEmu::Inventory::Serialize(MemoryBuffer &buf) { buf.SetWritePosition(0); buf.SetReadPosition(0); diff --git a/common/inventory.h b/common/inventory.h index b50bc7b0a..c3606949d 100644 --- a/common/inventory.h +++ b/common/inventory.h @@ -120,7 +120,7 @@ namespace EQEmu class Inventory { public: - Inventory(); + Inventory(int race, int class_); ~Inventory(); std::shared_ptr Get(const InventorySlot &slot); @@ -130,6 +130,7 @@ namespace EQEmu //utility static int CalcMaterialFromSlot(const InventorySlot &slot); static InventorySlot CalcSlotFromMaterial(int material); + bool CanEquip(std::shared_ptr inst, const EQEmu::InventorySlot &slot); bool Serialize(MemoryBuffer &buf); private: struct impl; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5e10cddba..3738a5719 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,7 +22,7 @@ SET(tests_headers ADD_EXECUTABLE(tests ${tests_sources} ${tests_headers}) -TARGET_LINK_LIBRARIES(tests common cppunit) +TARGET_LINK_LIBRARIES(tests common cppunit debug ${MySQL_LIBRARY_DEBUG} optimized ${MySQL_LIBRARY_RELEASE}) INSTALL(TARGETS tests RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}) diff --git a/tests/inventory_test.h b/tests/inventory_test.h index 55ff1f655..6cb34c6b3 100644 --- a/tests/inventory_test.h +++ b/tests/inventory_test.h @@ -26,13 +26,14 @@ class InventoryTest : public Test::Suite { typedef void(InventoryTest::*TestFunction)(void); public: - InventoryTest() { + InventoryTest() : inv(1, 1) { InitContainer(); InitArmor(); InitAugment(); InitStackable(); InitInventory(); TEST_ADD(InventoryTest::InventoryVerifyInitialItemsTest); + TEST_ADD(InventoryTest::InventoryCanEquipTest); } ~InventoryTest() { @@ -144,15 +145,15 @@ private: std::shared_ptr m_armor(new EQEmu::ItemInstance(&armor)); std::shared_ptr m_augment(new EQEmu::ItemInstance(&augment)); std::shared_ptr m_stackable(new EQEmu::ItemInstance(&stackable, 45)); - inv.Put(EQEmu::InventorySlot(0, 23), m_bag); - inv.Put(EQEmu::InventorySlot(0, 23, 0), m_armor); - inv.Put(EQEmu::InventorySlot(0, 23, 1), m_augment); - inv.Put(EQEmu::InventorySlot(0, 23, 7), m_stackable); + inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1), m_bag); + inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1, 0), m_armor); + inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1, 1), m_augment); + inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1, 7), m_stackable); } void InventoryVerifyInitialItemsTest() { - auto m_bag = inv.Get(EQEmu::InventorySlot(0, 23)); + auto m_bag = inv.Get(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1)); TEST_ASSERT(m_bag); TEST_ASSERT(m_bag->GetItem()); TEST_ASSERT(m_bag->GetItem()->ID == 1000); @@ -173,12 +174,41 @@ private: TEST_ASSERT(m_stackable->GetItem()->ID == 1003); } + void InventoryCanEquipTest() { + auto m_bag = inv.Get(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1)); + TEST_ASSERT(m_bag); + TEST_ASSERT(m_bag->GetItem()); + TEST_ASSERT(m_bag->GetItem()->ID == 1000); + + auto m_armor = m_bag->Get(0); + TEST_ASSERT(m_armor); + TEST_ASSERT(m_armor->GetItem()); + TEST_ASSERT(m_armor->GetItem()->ID == 1001); + + auto can_equip = inv.CanEquip(m_armor, EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotChest)); + TEST_ASSERT(can_equip); + + can_equip = inv.CanEquip(m_armor, EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotWaist)); + TEST_ASSERT(!can_equip); + + armor.Classes -= 1; + can_equip = inv.CanEquip(m_armor, EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotChest)); + TEST_ASSERT(!can_equip); + armor.Classes += 1; + + armor.Races -= 1; + can_equip = inv.CanEquip(m_armor, EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotChest)); + TEST_ASSERT(!can_equip); + armor.Races += 1; + } + void InventorySwapItemsTest() { - auto swap_result = inv.Swap(EQEmu::InventorySlot(0, 23), EQEmu::InventorySlot(0, 24), 0); + auto swap_result = inv.Swap(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1), + EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral2), 0); TEST_ASSERT(swap_result == true); - auto m_bag = inv.Get(EQEmu::InventorySlot(0, 24)); + auto m_bag = inv.Get(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral2)); TEST_ASSERT(m_bag); TEST_ASSERT(m_bag->GetItem()); TEST_ASSERT(m_bag->GetItem()->ID == 1000); diff --git a/tests/main.cpp b/tests/main.cpp index 420280f30..d9dad9da6 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "../common/eqemu_logsys.h" #include "memory_mapped_file_test.h" #include "ipc_mutex_test.h" #include "fixed_memory_test.h" @@ -32,6 +33,8 @@ #include "inventory_test.h" #include "memory_buffer_test.h" +EQEmuLogSys Log; + int main() { try { std::ofstream outfile("test_output.txt"); diff --git a/zone/client.h b/zone/client.h index f4aa36412..2ceadae2a 100644 --- a/zone/client.h +++ b/zone/client.h @@ -825,7 +825,6 @@ public: //New Inventory bool SwapItem(const EQEmu::InventorySlot &src, const EQEmu::InventorySlot &dest, int number_in_stack); - bool CanEquipItem(std::shared_ptr inst, const EQEmu::InventorySlot &slot); // // class Client::TextLink diff --git a/zone/inventory.cpp b/zone/inventory.cpp index 207124e9e..55aa08620 100644 --- a/zone/inventory.cpp +++ b/zone/inventory.cpp @@ -3116,50 +3116,14 @@ bool Client::SwapItem(const EQEmu::InventorySlot &src, const EQEmu::InventorySlo auto i_src = m_inventory.Get(src); auto i_dest = m_inventory.Get(dest); - if(dest.IsEquipment() && !CanEquipItem(i_dest, dest)) { + if(dest.IsEquipment() && !m_inventory.CanEquip(i_dest, dest)) { return false; } printf("Equip check passes %s -> %s\n", src.ToString().c_str(), dest.ToString().c_str()); - + bool res = m_inventory.Swap(src, dest, number_in_stack); return true; } -bool Client::CanEquipItem(std::shared_ptr inst, const EQEmu::InventorySlot &slot) { - if(!inst) { - return false; - } - - if(slot.Type() != 0) { - return false; - } - - if(!EQEmu::ValueWithin(slot.Slot(), EQEmu::PersonalSlotCharm, EQEmu::PersonalSlotAmmo)) { - return false; - } - - auto item = inst->GetItem(); - //check slot - - int use_slot = -1; - if(slot.Slot() == EQEmu::PersonalSlotPowerSource) { - use_slot = EQEmu::PersonalSlotAmmo; - } - else if(slot.Slot() == EQEmu::PersonalSlotAmmo) { - use_slot = EQEmu::PersonalSlotPowerSource; - } else { - use_slot = slot.Slot(); - } - - if(!(item->Slots & (1 << use_slot))) { - return false; - } - - if(!item->IsEquipable(GetBaseRace(), GetBaseClass())) { - return false; - } - - return true; -} diff --git a/zone/mob.cpp b/zone/mob.cpp index 909db26c8..d7a63f065 100644 --- a/zone/mob.cpp +++ b/zone/mob.cpp @@ -102,7 +102,8 @@ Mob::Mob(const char* in_name, m_TargetLocation(glm::vec3()), m_TargetV(glm::vec3()), flee_timer(FLEE_CHECK_TIMER), - m_Position(position) + m_Position(position), + m_inventory(in_race, in_class) { targeted = 0; tar_ndx=0;