Working on can equip, putting it in the general inventory class.

This commit is contained in:
KimLS 2015-02-27 02:40:44 -08:00
parent 18b4d068ea
commit 7870bf103a
8 changed files with 91 additions and 51 deletions

View File

@ -98,10 +98,14 @@ const std::string EQEmu::InventorySlot::ToString() const {
struct EQEmu::Inventory::impl struct EQEmu::Inventory::impl
{ {
std::map<int, ItemContainer> containers_; std::map<int, ItemContainer> containers_;
int race_;
int class_;
}; };
EQEmu::Inventory::Inventory() { EQEmu::Inventory::Inventory(int race, int class_) {
impl_ = new impl; impl_ = new impl;
impl_->race_ = race;
impl_->class_ = class_;
} }
EQEmu::Inventory::~Inventory() { EQEmu::Inventory::~Inventory() {
@ -231,6 +235,44 @@ EQEmu::InventorySlot EQEmu::Inventory::CalcSlotFromMaterial(int material) {
} }
} }
bool EQEmu::Inventory::CanEquip(std::shared_ptr<EQEmu::ItemInstance> 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) { bool EQEmu::Inventory::Serialize(MemoryBuffer &buf) {
buf.SetWritePosition(0); buf.SetWritePosition(0);
buf.SetReadPosition(0); buf.SetReadPosition(0);

View File

@ -120,7 +120,7 @@ namespace EQEmu
class Inventory class Inventory
{ {
public: public:
Inventory(); Inventory(int race, int class_);
~Inventory(); ~Inventory();
std::shared_ptr<ItemInstance> Get(const InventorySlot &slot); std::shared_ptr<ItemInstance> Get(const InventorySlot &slot);
@ -130,6 +130,7 @@ namespace EQEmu
//utility //utility
static int CalcMaterialFromSlot(const InventorySlot &slot); static int CalcMaterialFromSlot(const InventorySlot &slot);
static InventorySlot CalcSlotFromMaterial(int material); static InventorySlot CalcSlotFromMaterial(int material);
bool CanEquip(std::shared_ptr<EQEmu::ItemInstance> inst, const EQEmu::InventorySlot &slot);
bool Serialize(MemoryBuffer &buf); bool Serialize(MemoryBuffer &buf);
private: private:
struct impl; struct impl;

View File

@ -22,7 +22,7 @@ SET(tests_headers
ADD_EXECUTABLE(tests ${tests_sources} ${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}) INSTALL(TARGETS tests RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})

View File

@ -26,13 +26,14 @@
class InventoryTest : public Test::Suite { class InventoryTest : public Test::Suite {
typedef void(InventoryTest::*TestFunction)(void); typedef void(InventoryTest::*TestFunction)(void);
public: public:
InventoryTest() { InventoryTest() : inv(1, 1) {
InitContainer(); InitContainer();
InitArmor(); InitArmor();
InitAugment(); InitAugment();
InitStackable(); InitStackable();
InitInventory(); InitInventory();
TEST_ADD(InventoryTest::InventoryVerifyInitialItemsTest); TEST_ADD(InventoryTest::InventoryVerifyInitialItemsTest);
TEST_ADD(InventoryTest::InventoryCanEquipTest);
} }
~InventoryTest() { ~InventoryTest() {
@ -144,15 +145,15 @@ private:
std::shared_ptr<EQEmu::ItemInstance> m_armor(new EQEmu::ItemInstance(&armor)); std::shared_ptr<EQEmu::ItemInstance> m_armor(new EQEmu::ItemInstance(&armor));
std::shared_ptr<EQEmu::ItemInstance> m_augment(new EQEmu::ItemInstance(&augment)); std::shared_ptr<EQEmu::ItemInstance> m_augment(new EQEmu::ItemInstance(&augment));
std::shared_ptr<EQEmu::ItemInstance> m_stackable(new EQEmu::ItemInstance(&stackable, 45)); std::shared_ptr<EQEmu::ItemInstance> m_stackable(new EQEmu::ItemInstance(&stackable, 45));
inv.Put(EQEmu::InventorySlot(0, 23), m_bag); inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1), m_bag);
inv.Put(EQEmu::InventorySlot(0, 23, 0), m_armor); inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1, 0), m_armor);
inv.Put(EQEmu::InventorySlot(0, 23, 1), m_augment); inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1, 1), m_augment);
inv.Put(EQEmu::InventorySlot(0, 23, 7), m_stackable); inv.Put(EQEmu::InventorySlot(EQEmu::InvTypePersonal, EQEmu::PersonalSlotGeneral1, 7), m_stackable);
} }
void InventoryVerifyInitialItemsTest() 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);
TEST_ASSERT(m_bag->GetItem()); TEST_ASSERT(m_bag->GetItem());
TEST_ASSERT(m_bag->GetItem()->ID == 1000); TEST_ASSERT(m_bag->GetItem()->ID == 1000);
@ -173,12 +174,41 @@ private:
TEST_ASSERT(m_stackable->GetItem()->ID == 1003); 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() 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); 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);
TEST_ASSERT(m_bag->GetItem()); TEST_ASSERT(m_bag->GetItem());
TEST_ASSERT(m_bag->GetItem()->ID == 1000); TEST_ASSERT(m_bag->GetItem()->ID == 1000);

View File

@ -20,6 +20,7 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <memory> #include <memory>
#include "../common/eqemu_logsys.h"
#include "memory_mapped_file_test.h" #include "memory_mapped_file_test.h"
#include "ipc_mutex_test.h" #include "ipc_mutex_test.h"
#include "fixed_memory_test.h" #include "fixed_memory_test.h"
@ -32,6 +33,8 @@
#include "inventory_test.h" #include "inventory_test.h"
#include "memory_buffer_test.h" #include "memory_buffer_test.h"
EQEmuLogSys Log;
int main() { int main() {
try { try {
std::ofstream outfile("test_output.txt"); std::ofstream outfile("test_output.txt");

View File

@ -825,7 +825,6 @@ public:
//New Inventory //New Inventory
bool SwapItem(const EQEmu::InventorySlot &src, const EQEmu::InventorySlot &dest, int number_in_stack); bool SwapItem(const EQEmu::InventorySlot &src, const EQEmu::InventorySlot &dest, int number_in_stack);
bool CanEquipItem(std::shared_ptr<EQEmu::ItemInstance> inst, const EQEmu::InventorySlot &slot);
// //
// class Client::TextLink // class Client::TextLink

View File

@ -3116,7 +3116,7 @@ bool Client::SwapItem(const EQEmu::InventorySlot &src, const EQEmu::InventorySlo
auto i_src = m_inventory.Get(src); auto i_src = m_inventory.Get(src);
auto i_dest = m_inventory.Get(dest); 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; return false;
} }
@ -3127,39 +3127,3 @@ bool Client::SwapItem(const EQEmu::InventorySlot &src, const EQEmu::InventorySlo
return true; return true;
} }
bool Client::CanEquipItem(std::shared_ptr<EQEmu::ItemInstance> 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;
}

View File

@ -102,7 +102,8 @@ Mob::Mob(const char* in_name,
m_TargetLocation(glm::vec3()), m_TargetLocation(glm::vec3()),
m_TargetV(glm::vec3()), m_TargetV(glm::vec3()),
flee_timer(FLEE_CHECK_TIMER), flee_timer(FLEE_CHECK_TIMER),
m_Position(position) m_Position(position),
m_inventory(in_race, in_class)
{ {
targeted = 0; targeted = 0;
tar_ndx=0; tar_ndx=0;