Inventory (WIP part I)
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
#include "Inventory.h"
|
||||
|
||||
Inventory::Inventory(size_t size) : slots(size) {
|
||||
}
|
||||
|
||||
ItemStack& Inventory::getSlot(size_t index) {
|
||||
return slots[index];
|
||||
}
|
||||
|
||||
size_t Inventory::findEmptySlot(size_t begin, size_t end) const {
|
||||
end = std::min(slots.size(), end);
|
||||
for (size_t i = begin; i < end; i++) {
|
||||
if (slots[i].isEmpty()) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return npos;
|
||||
}
|
||||
|
||||
size_t Inventory::findSlotByItem(itemid_t id, size_t begin, size_t end) {
|
||||
end = std::min(slots.size(), end);
|
||||
for (size_t i = begin; i < end; i++) {
|
||||
if (slots[i].getItemId() == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return npos;
|
||||
}
|
||||
|
||||
void Inventory::move(
|
||||
ItemStack& item,
|
||||
const ContentIndices* indices,
|
||||
size_t begin,
|
||||
size_t end)
|
||||
{
|
||||
end = std::min(slots.size(), end);
|
||||
for (size_t i = begin; i < end && !item.isEmpty(); i++) {
|
||||
ItemStack& slot = slots[i];
|
||||
if (slot.accepts(item)) {
|
||||
slot.move(item, indices);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Inventory::read(const dynamic::Map* src) {
|
||||
auto slotsarr = src->list("slots");
|
||||
size_t slotscount = std::min(slotsarr->size(), slots.size());
|
||||
for (size_t i = 0; i < slotscount; i++) {
|
||||
auto item = slotsarr->map(i);
|
||||
itemid_t id = item->getInt("id", ITEM_EMPTY);
|
||||
itemcount_t count = item->getInt("count", 0);
|
||||
auto& slot = slots[i];
|
||||
slot.set(ItemStack(id, count));
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> Inventory::write() const {
|
||||
auto map = std::make_unique<dynamic::Map>();
|
||||
auto& slotsarr = map->putList("slots");
|
||||
for (size_t i = 0; i < slots.size(); i++) {
|
||||
auto& item = slots[i];
|
||||
itemid_t id = item.getItemId();
|
||||
itemcount_t count = item.getCount();
|
||||
|
||||
auto& slotmap = slotsarr.putMap();
|
||||
slotmap.put("id", id);
|
||||
if (count) {
|
||||
slotmap.put("count", count);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
const size_t Inventory::npos = -1;
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef ITEMS_INVENTORY_H_
|
||||
#define ITEMS_INVENTORY_H_
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "ItemStack.h"
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
class ContentIndices;
|
||||
|
||||
class Inventory {
|
||||
std::vector<ItemStack> slots;
|
||||
public:
|
||||
Inventory(size_t size);
|
||||
|
||||
ItemStack& getSlot(size_t index);
|
||||
size_t findEmptySlot(size_t begin=0, size_t end=-1) const;
|
||||
size_t findSlotByItem(itemid_t id, size_t begin=0, size_t end=-1);
|
||||
|
||||
inline size_t size() const {
|
||||
return slots.size();
|
||||
}
|
||||
|
||||
void move(
|
||||
ItemStack& item,
|
||||
const ContentIndices* indices,
|
||||
size_t begin=0,
|
||||
size_t end=-1);
|
||||
|
||||
/* deserializing inventory */
|
||||
void read(const dynamic::Map* src);
|
||||
/* serializing inventory */
|
||||
std::unique_ptr<dynamic::Map> write() const;
|
||||
|
||||
static const size_t npos;
|
||||
};
|
||||
|
||||
#endif // ITEMS_INVENTORY_H_
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "ItemStack.h"
|
||||
|
||||
#include "ItemDef.h"
|
||||
#include "../content/Content.h"
|
||||
|
||||
ItemStack::ItemStack() : item(ITEM_EMPTY), count(0) {
|
||||
}
|
||||
|
||||
ItemStack::ItemStack(itemid_t item, itemcount_t count) : item(item), count(count) {
|
||||
}
|
||||
|
||||
void ItemStack::set(const ItemStack& item) {
|
||||
this->item = item.item;
|
||||
this->count = item.count;
|
||||
}
|
||||
|
||||
bool ItemStack::accepts(const ItemStack& other) const {
|
||||
if (isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return item == other.getItemId();
|
||||
}
|
||||
|
||||
void ItemStack::move(ItemStack& item, const ContentIndices* indices) {
|
||||
auto def = indices->getItemDef(item.getItemId());
|
||||
int count = std::min(item.count, def->stackSize-this->count);
|
||||
if (isEmpty()) {
|
||||
set(ItemStack(item.getItemId(), count));
|
||||
} else {
|
||||
setCount(this->count + count);
|
||||
}
|
||||
item.setCount(item.count-count);
|
||||
}
|
||||
|
||||
void ItemStack::setCount(itemcount_t count) {
|
||||
this->count = count;
|
||||
if (count == 0) {
|
||||
item = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef ITEMS_ITEM_STACK_H_
|
||||
#define ITEMS_ITEM_STACK_H_
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../constants.h"
|
||||
|
||||
class ContentIndices;
|
||||
|
||||
class ItemStack {
|
||||
itemid_t item;
|
||||
itemcount_t count;
|
||||
public:
|
||||
ItemStack();
|
||||
|
||||
ItemStack(itemid_t item, itemcount_t count);
|
||||
|
||||
void set(const ItemStack& item);
|
||||
void setCount(itemcount_t count);
|
||||
|
||||
bool accepts(const ItemStack& item) const;
|
||||
void move(ItemStack& item, const ContentIndices* indices);
|
||||
|
||||
inline void clear() {
|
||||
set(ItemStack(0, 0));
|
||||
}
|
||||
|
||||
inline bool isEmpty() const {
|
||||
return item == ITEM_EMPTY;
|
||||
}
|
||||
|
||||
inline itemid_t getItemId() const {
|
||||
return item;
|
||||
}
|
||||
|
||||
inline itemcount_t getCount() const {
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ITEMS_ITEM_STACK_H_
|
||||
Reference in New Issue
Block a user