add ItemStack::fields

This commit is contained in:
MihailRis 2025-02-15 20:30:22 +03:00
parent fd8c26d585
commit d86c5508d2
5 changed files with 85 additions and 29 deletions

View File

@ -186,17 +186,26 @@ void SlotView::draw(const DrawContext& pctx, const Assets& assets) {
} }
} }
if (stack.getCount() > 1) { if (stack.getCount() > 1 || stack.getFields() != nullptr) {
auto font = assets.get<Font>("normal"); auto font = assets.get<Font>("normal");
std::wstring text = std::to_wstring(stack.getCount());
if (stack.getCount() > 1) {
std::wstring text = std::to_wstring(stack.getCount());
int x = pos.x+slotSize-text.length()*8; int x = pos.x+slotSize-text.length()*8;
int y = pos.y+slotSize-16; int y = pos.y+slotSize-16;
batch->setColor({0, 0, 0, 1.0f}); batch->setColor({0, 0, 0, 1.0f});
font->draw(*batch, text, x+1, y+1, nullptr, 0); font->draw(*batch, text, x+1, y+1, nullptr, 0);
batch->setColor(glm::vec4(1.0f)); batch->setColor(glm::vec4(1.0f));
font->draw(*batch, text, x, y, nullptr, 0); font->draw(*batch, text, x, y, nullptr, 0);
}
if (stack.getFields() != nullptr) {
batch->setColor({0, 0, 0, 1.0f});
font->draw(*batch, L"#", pos.x+1, pos.y+1, nullptr, 0);
batch->setColor(glm::vec4(1.0f));
font->draw(*batch, L"#", pos.x, pos.y, nullptr, 0);
}
} }
} }

View File

@ -72,8 +72,12 @@ void Inventory::deserialize(const dv::value& src) {
if (item.has("count")){ if (item.has("count")){
count = item["count"].asInteger(); count = item["count"].asInteger();
} }
dv::value fields = nullptr;
if (item.has("fields")) {
fields = item["fields"];
}
auto& slot = slots[i]; auto& slot = slots[i];
slot.set(ItemStack(id, count)); slot.set(ItemStack(id, count, fields));
} }
} }
@ -92,6 +96,10 @@ dv::value Inventory::serialize() const {
if (count) { if (count) {
slotmap["count"] = count; slotmap["count"] = count;
} }
const auto& fields = item.getFields();
if (fields != nullptr) {
slotmap["fields"] = fields;
}
} }
return map; return map;
} }
@ -110,5 +118,3 @@ void Inventory::convert(dv::value& data, const ContentReport* report) {
inventory.convert(report); inventory.convert(report);
data = inventory.serialize(); data = inventory.serialize();
} }
const size_t Inventory::npos = -1;

View File

@ -26,10 +26,6 @@ public:
itemid_t id, size_t begin = 0, size_t end = -1, size_t minCount = 1 itemid_t id, size_t begin = 0, size_t end = -1, size_t minCount = 1
); );
inline size_t size() const {
return slots.size();
}
void move( void move(
ItemStack& item, ItemStack& item,
const ContentIndices& indices, const ContentIndices& indices,
@ -46,17 +42,21 @@ public:
void convert(const ContentReport* report); void convert(const ContentReport* report);
static void convert(dv::value& data, const ContentReport* report); static void convert(dv::value& data, const ContentReport* report);
inline void setId(int64_t id) { size_t size() const {
return slots.size();
}
void setId(int64_t id) {
this->id = id; this->id = id;
} }
inline int64_t getId() const { int64_t getId() const {
return id; return id;
} }
inline bool isVirtual() const { bool isVirtual() const {
return id < 0; return id < 0;
} }
static const size_t npos; static constexpr size_t npos = -1;
}; };

View File

@ -3,13 +3,18 @@
#include "content/Content.hpp" #include "content/Content.hpp"
#include "ItemDef.hpp" #include "ItemDef.hpp"
ItemStack::ItemStack(itemid_t item, itemcount_t count) ItemStack::ItemStack(itemid_t item, itemcount_t count, dv::value data)
: item(item), count(count) { : item(item), count(count), fields(std::move(data)) {
} }
void ItemStack::set(const ItemStack& item) { void ItemStack::set(const ItemStack& item) {
set(ItemStack(item));
}
void ItemStack::set(ItemStack&& item) {
this->item = item.item; this->item = item.item;
this->count = item.count; this->count = item.count;
this->fields = std::move(item.fields);
if (count == 0) { if (count == 0) {
this->item = 0; this->item = 0;
} }
@ -22,14 +27,14 @@ bool ItemStack::accepts(const ItemStack& other) const {
if (isEmpty()) { if (isEmpty()) {
return true; return true;
} }
return item == other.getItemId(); return item == other.getItemId() && other.fields == nullptr;
} }
void ItemStack::move(ItemStack& item, const ContentIndices& indices) { void ItemStack::move(ItemStack& item, const ContentIndices& indices) {
auto& def = indices.items.require(item.getItemId()); auto& def = indices.items.require(item.getItemId());
itemcount_t count = std::min(item.count, def.stackSize - this->count); itemcount_t count = std::min(item.count, def.stackSize - this->count);
if (isEmpty()) { if (isEmpty()) {
set(ItemStack(item.getItemId(), count)); set(ItemStack(item.getItemId(), count, std::move(item.fields)));
} else { } else {
setCount(this->count + count); setCount(this->count + count);
} }
@ -39,6 +44,27 @@ void ItemStack::move(ItemStack& item, const ContentIndices& indices) {
void ItemStack::setCount(itemcount_t count) { void ItemStack::setCount(itemcount_t count) {
this->count = count; this->count = count;
if (count == 0) { if (count == 0) {
item = 0; clear();
} }
} }
void ItemStack::setField(std::string_view name, dv::value value) {
if (fields == nullptr) {
if (value == nullptr) {
return;
}
fields = dv::object();
}
if (value == nullptr) {
fields.erase(std::string(name));
return;
}
fields[std::string(name)] = std::move(value);
}
dv::value* ItemStack::getField(const std::string& name) const {
if (fields == nullptr) {
return nullptr;
}
return fields.at(name).ptr;
}

View File

@ -2,20 +2,31 @@
#include "constants.hpp" #include "constants.hpp"
#include "typedefs.hpp" #include "typedefs.hpp"
#include "data/dv.hpp"
class ContentIndices; class ContentIndices;
class ItemStack { class ItemStack {
itemid_t item = ITEM_EMPTY; itemid_t item = ITEM_EMPTY;
itemcount_t count = 0; itemcount_t count = 0;
dv::value fields = nullptr;
public: public:
ItemStack() = default; ItemStack() = default;
ItemStack(itemid_t item, itemcount_t count); ItemStack(itemid_t item, itemcount_t count, dv::value data=nullptr);
void set(const ItemStack& item); void set(const ItemStack& item);
void set(ItemStack&& item);
void setCount(itemcount_t count); void setCount(itemcount_t count);
/// @brief Set a field in the item stack data.
void setField(std::string_view name, dv::value value);
/// @brief Get a field from the item stack data.
/// @param name field name
/// @return value pointer or nullptr if the field does not exist.
dv::value* getField(const std::string& name) const;
bool accepts(const ItemStack& item) const; bool accepts(const ItemStack& item) const;
/// @brief Move items from one stack to another. /// @brief Move items from one stack to another.
@ -24,19 +35,23 @@ public:
/// @param indices content indices /// @param indices content indices
void move(ItemStack& item, const ContentIndices& indices); void move(ItemStack& item, const ContentIndices& indices);
inline void clear() { void clear() {
set(ItemStack(0, 0)); set(ItemStack(0, 0));
} }
inline bool isEmpty() const { bool isEmpty() const {
return item == ITEM_EMPTY; return item == ITEM_EMPTY;
} }
inline itemid_t getItemId() const { itemid_t getItemId() const {
return item; return item;
} }
inline itemcount_t getCount() const { itemcount_t getCount() const {
return count; return count;
} }
const dv::value& getFields() const {
return fields;
}
}; };