Get/Put item implementation + tests

This commit is contained in:
KimLS 2015-02-20 16:24:32 -08:00
parent 551c0ef368
commit 2d617f0ea7
8 changed files with 328 additions and 55 deletions

View File

@ -17,9 +17,52 @@
*/
#include "inventory.h"
#include "data_verification.h"
#include <map>
struct EQEmu::Inventory::impl
{
std::map<int, ItemContainer> containers_;
};
EQEmu::Inventory::Inventory() {
impl_ = new impl;
}
EQEmu::Inventory::~Inventory() {
delete impl_;
}
std::shared_ptr<EQEmu::ItemInstance> EQEmu::Inventory::Get(int container_id, int slot_id) {
auto iter = impl_->containers_.find(container_id);
if(iter != impl_->containers_.end()) {
return iter->second.Get(slot_id);
}
return std::shared_ptr<ItemInstance>(nullptr);
}
std::shared_ptr<EQEmu::ItemInstance> EQEmu::Inventory::Get(int container_id, int slot_id, int bag_idx) {
auto iter = impl_->containers_.find(container_id);
if(iter != impl_->containers_.end()) {
auto item = iter->second.Get(slot_id);
if(item) {
return item->GetItem(bag_idx);
}
}
return std::shared_ptr<ItemInstance>(nullptr);
}
bool EQEmu::Inventory::Put(int container_id, int slot_id, std::shared_ptr<ItemInstance> inst) {
if(impl_->containers_.count(container_id) == 0) {
auto &container = impl_->containers_[container_id];
return container.Put(slot_id, inst);
} else {
ItemContainer container;
bool v = container.Put(slot_id, inst);
impl_->containers_[container_id] = container;
return v;
}
}

View File

@ -21,8 +21,6 @@
#include "item_container.h"
#include <string>
namespace EQEmu
{
enum InventoryType : int
@ -44,8 +42,12 @@ namespace EQEmu
Inventory();
~Inventory();
std::shared_ptr<ItemInstance> Get(int container_id, int slot_id);
std::shared_ptr<ItemInstance> Get(int container_id, int slot_id, int bag_idx);
bool Put(int container_id, int slot_id, std::shared_ptr<ItemInstance> inst);
private:
std::map<int, ItemContainer> containers_;
struct impl;
impl *impl_;
};
} // EQEmu

View File

@ -33,7 +33,7 @@ bool EQEmu::ItemContainer::Put(int slot_id, std::shared_ptr<ItemInstance> inst)
auto iter = impl_->items.find(slot_id);
if(iter == impl_->items.end()) {
impl_->items[slot_id] = inst;
//trigger put in slot_id
//trigger insert in slot_id
return true;
}

View File

@ -18,53 +18,111 @@
#include "item_instance.h"
#include "data_verification.h"
#include "item_container.h"
struct EQEmu::ItemInstance::impl {
const ItemData *base_item_;
ItemData *modified_item_;
int16 charges_;
uint32 color_;
bool attuned_;
std::string custom_data_;
uint32 ornament_idfile_;
uint32 ornament_icon_;
uint32 ornament_hero_model_;
uint64 tracking_id_;
ItemContainer contents_;
};
EQEmu::ItemInstance::ItemInstance() {
base_item_ = nullptr;
modified_item_ = nullptr;
charges_ = -1;
color_ = 0;
attuned_ = false;
ornament_idfile_ = 0;
ornament_icon_ = 0;
ornament_hero_model_ = 0;
tracking_id_ = 0;
impl_ = new impl;
impl_->base_item_ = nullptr;
impl_->modified_item_ = nullptr;
impl_->charges_ = -1;
impl_->color_ = 0;
impl_->attuned_ = false;
impl_->ornament_idfile_ = 0;
impl_->ornament_icon_ = 0;
impl_->ornament_hero_model_ = 0;
impl_->tracking_id_ = 0;
}
EQEmu::ItemInstance::ItemInstance(const ItemData* idata) {
base_item_ = idata;
modified_item_ = nullptr;
charges_ = -1;
color_ = 0;
attuned_ = false;
ornament_idfile_ = 0;
ornament_icon_ = 0;
ornament_hero_model_ = 0;
tracking_id_ = 0;
impl_ = new impl;
impl_->base_item_ = idata;
impl_->modified_item_ = nullptr;
impl_->charges_ = -1;
impl_->color_ = 0;
impl_->attuned_ = false;
impl_->ornament_idfile_ = 0;
impl_->ornament_icon_ = 0;
impl_->ornament_hero_model_ = 0;
impl_->tracking_id_ = 0;
}
EQEmu::ItemInstance::ItemInstance(const ItemData* idata, int16 charges) {
base_item_ = idata;
modified_item_ = nullptr;
charges_ = charges;
color_ = 0;
attuned_ = false;
ornament_idfile_ = 0;
ornament_icon_ = 0;
ornament_hero_model_ = 0;
tracking_id_ = 0;
impl_ = new impl;
impl_->base_item_ = idata;
impl_->modified_item_ = nullptr;
impl_->charges_ = charges;
impl_->color_ = 0;
impl_->attuned_ = false;
impl_->ornament_idfile_ = 0;
impl_->ornament_icon_ = 0;
impl_->ornament_hero_model_ = 0;
impl_->tracking_id_ = 0;
}
EQEmu::ItemInstance::~ItemInstance() {
delete impl_;
}
const ItemData *EQEmu::ItemInstance::GetItem() {
return impl_->modified_item_ ? impl_->modified_item_ : impl_->base_item_;
}
std::shared_ptr<EQEmu::ItemInstance> EQEmu::ItemInstance::GetItem(int index) {
if(EQEmu::ValueWithin(index, 0, 200)) {
auto iter = contents_.find(index);
if(iter != contents_.end()) {
return iter->second;
}
if(EQEmu::ValueWithin(index, 0, 255)) {
return impl_->contents_.Get(index);
}
return std::shared_ptr<EQEmu::ItemInstance>(nullptr);
}
bool EQEmu::ItemInstance::PutItem(int index, std::shared_ptr<ItemInstance> inst) {
if(!inst || !inst->GetItem()) {
return false;
}
if(!impl_->base_item_) {
return false;
}
auto *item = impl_->base_item_;
if(item->ItemClass == ItemClassContainer) { // Bag
if(!EQEmu::ValueWithin(index, 0, (int)item->BagSlots)) {
return false;
}
return impl_->contents_.Put(index, inst);
}
else if(item->ItemClass == ItemClassCommon) { // Augment
if(!EQEmu::ValueWithin(index, 0, (int)EmuConstants::ITEM_COMMON_SIZE)) {
return false;
}
if(!item->AugSlotVisible[index]) {
return false;
}
auto *aug_item = inst->GetItem();
int aug_type = aug_item->AugType;
if(aug_type == -1 || (1 << (item->AugSlotType[index] - 1)) & aug_type) {
return impl_->contents_.Put(index, inst);
}
return false;
}
return false;
}

View File

@ -20,13 +20,10 @@
#define COMMON_ITEM_INSTANCE_H
#include "item_data.h"
#include <string>
#include <memory>
#include <map>
namespace EQEmu
{
class ItemContainer;
class ItemInstance
{
public:
@ -35,22 +32,12 @@ namespace EQEmu
ItemInstance(const ItemData* idata, int16 charges);
~ItemInstance();
const ItemData *GetItem();
std::shared_ptr<ItemInstance> GetItem(int index);
void SetContainer(ItemContainer *parent) { parent_ = parent; }
bool PutItem(int index, std::shared_ptr<ItemInstance> inst);
private:
const ItemData *base_item_;
ItemData *modified_item_;
int16 charges_;
uint32 color_;
bool attuned_;
std::string custom_data_;
uint32 ornament_idfile_;
uint32 ornament_icon_;
uint32 ornament_hero_model_;
uint64 tracking_id_;
std::map<int, std::shared_ptr<ItemInstance>> contents_;
ItemContainer *parent_;
struct impl;
impl *impl_;
};
} // EQEmu

View File

@ -12,6 +12,7 @@ SET(tests_headers
fixed_memory_test.h
fixed_memory_variable_test.h
hextoi_32_64_test.h
inventory_test.h
ipc_mutex_test.h
memory_mapped_file_test.h
string_util_test.h

180
tests/inventory_test.h Normal file
View File

@ -0,0 +1,180 @@
/* EQEMu: Everquest Server Emulator
Copyright (C) 2001-2015 EQEMu Development Team (http://eqemulator.net)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __EQEMU_TESTS_INVENTORY_H
#define __EQEMU_TESTS_INVENTORY_H
#include "cppunit/cpptest.h"
#include "../common/inventory.h"
#include <string.h>
class InventoryTest : public Test::Suite {
typedef void(InventoryTest::*TestFunction)(void);
public:
InventoryTest() {
InitContainer();
InitArmor();
InitAugment();
InitStackable();
InitInventory();
TEST_ADD(InventoryTest::InventoryVerifyInitialItemsTest);
}
~InventoryTest() {
}
void InitContainer() {
memset(&container, 0, sizeof(container));
strcpy(container.Name, "Backpack");
strcpy(container.IDFile, "IT64");
container.ID = 1000;
container.BagSize = 3;
container.BagSlots = 8;
container.BagType = 5;
container.BagWR = 50;
container.ItemClass = 1;
container.Classes = 65535U;
container.Focus.Effect = -1;
container.ItemType = 11;
container.NoDrop = 1;
container.NoRent = 1;
container.Races = 131071U;
container.Size = 3;
container.SkillModType = -1;
container.Click.Effect = -1;
container.Weight = 30;
container.StackSize = 1;
container.Proc.Effect = -1;
container.Worn.Effect = -1;
container.Scroll.Effect = -1;
}
void InitArmor() {
memset(&armor, 0, sizeof(armor));
strcpy(armor.Name, "Cloth Shirt");
strcpy(armor.IDFile, "IT64");
armor.ID = 1001;
armor.AC = 4;
armor.AugSlotType[0] = 7;
for(int i = 0; i < 6; ++i)
armor.AugSlotVisible[i] = 1;
armor.Size = 2;
armor.Slots = 131072;
armor.Classes = 65535U;
armor.Focus.Effect = -1;
armor.ItemType = 10;
armor.NoDrop = 1;
armor.NoRent = 1;
armor.Races = 131071U;
armor.SkillModType = -1;
armor.Click.Effect = -1;
armor.Weight = 8;
armor.StackSize = 1;
armor.Proc.Effect = -1;
armor.Worn.Effect = -1;
armor.Scroll.Effect = -1;
}
void InitAugment() {
memset(&augment, 0, sizeof(augment));
strcpy(augment.Name, "Cloth Augment");
strcpy(augment.IDFile, "IT64");
augment.ID = 1002;
augment.AWis = 10;
augment.AInt = 10;
augment.AugType = 64;
augment.Slots = 2072574;
augment.Classes = 65535U;
augment.Focus.Effect = -1;
augment.ItemType = 54;
augment.NoDrop = 1;
augment.NoRent = 1;
augment.Races = 131071U;
augment.SkillModType = -1;
augment.Click.Effect = -1;
augment.Weight = 5;
augment.StackSize = 1;
augment.Size = 1;
augment.Proc.Effect = -1;
augment.Worn.Effect = -1;
augment.Scroll.Effect = -1;
}
void InitStackable() {
memset(&stackable, 0, sizeof(stackable));
strcpy(stackable.Name, "Stackable Item");
strcpy(stackable.IDFile, "IT64");
stackable.ID = 1003;
stackable.Classes = 65535U;
stackable.Focus.Effect = -1;
stackable.ItemType = 54;
stackable.NoDrop = 1;
stackable.NoRent = 1;
stackable.Races = 131071U;
stackable.SkillModType = -1;
stackable.Click.Effect = -1;
stackable.Weight = 5;
stackable.StackSize = 100;
stackable.Stackable = 1;
stackable.Size = 1;
stackable.Proc.Effect = -1;
stackable.Worn.Effect = -1;
stackable.Scroll.Effect = -1;
}
void InitInventory()
{
std::shared_ptr<EQEmu::ItemInstance> bag(new EQEmu::ItemInstance(&container));
bag->PutItem(0, std::shared_ptr<EQEmu::ItemInstance>(new EQEmu::ItemInstance(&armor)));
bag->PutItem(1, std::shared_ptr<EQEmu::ItemInstance>(new EQEmu::ItemInstance(&augment)));
bag->PutItem(7, std::shared_ptr<EQEmu::ItemInstance>(new EQEmu::ItemInstance(&stackable, 45)));
inv.Put(0, 23, bag); //23 first inv slot
}
void InventoryVerifyInitialItemsTest()
{
auto m_bag = inv.Get(0, 23);
TEST_ASSERT(m_bag);
TEST_ASSERT(m_bag->GetItem());
TEST_ASSERT(m_bag->GetItem()->ID == 1000);
auto m_armor = m_bag->GetItem(0);
TEST_ASSERT(m_armor);
TEST_ASSERT(m_armor->GetItem());
TEST_ASSERT(m_armor->GetItem()->ID == 1001);
auto m_augment = m_bag->GetItem(1);
TEST_ASSERT(m_augment);
TEST_ASSERT(m_augment->GetItem());
TEST_ASSERT(m_augment->GetItem()->ID == 1002);
auto m_stackable = m_bag->GetItem(7);
TEST_ASSERT(m_stackable);
TEST_ASSERT(m_stackable->GetItem());
TEST_ASSERT(m_stackable->GetItem()->ID == 1003);
}
private:
EQEmu::Inventory inv;
ItemData container;
ItemData armor;
ItemData augment;
ItemData stackable;
};
#endif

View File

@ -29,6 +29,7 @@
#include "string_util_test.h"
#include "data_verification_test.h"
#include "skills_util_test.h"
#include "inventory_test.h"
int main() {
try {
@ -44,7 +45,8 @@ int main() {
tests.add(new StringUtilTest());
tests.add(new DataVerificationTest());
tests.add(new SkillsUtilsTest());
tests.run(*output, true);
tests.add(new InventoryTest());
tests.run(*output, false);
} catch(...) {
return -1;
}