载入中...
搜索中...
未找到
Bag.cpp
浏览该文件的文档.
1#include "inventory/Bag.h"
3
4#include <algorithm>
5
6namespace eve::inventory {
7
8Bag::Bag(int slotCount) {
9 if (slotCount < 0) slotCount = 0;
10 slots_.assign(size_t(slotCount), ItemStack{});
11}
12
13void Bag::destroy() { delete this; }
14
15void Bag::setSlotCount(int slotCount) {
16 if (slotCount < 0) slotCount = 0;
17 slots_.resize(size_t(slotCount));
18}
19
20void Bag::clearAcceptTags() { acceptTags_.clear(); }
21
22void Bag::addAcceptTag(const std::string &tag) {
23 if (tag.empty()) return;
24 if (std::find(acceptTags_.begin(), acceptTags_.end(), tag) == acceptTags_.end())
25 acceptTags_.push_back(tag);
26}
27
28int Bag::getAcceptTagCount() const { return int(acceptTags_.size()); }
29
30std::string Bag::getAcceptTag(int index) const {
31 if (index < 0 || index >= int(acceptTags_.size())) return {};
32 return acceptTags_[size_t(index)];
33}
34
35void Bag::clearRejectTags() { rejectTags_.clear(); }
36
37void Bag::addRejectTag(const std::string &tag) {
38 if (tag.empty()) return;
39 if (std::find(rejectTags_.begin(), rejectTags_.end(), tag) == rejectTags_.end())
40 rejectTags_.push_back(tag);
41}
42
43int Bag::getRejectTagCount() const { return int(rejectTags_.size()); }
44
45std::string Bag::getRejectTag(int index) const {
46 if (index < 0 || index >= int(rejectTags_.size())) return {};
47 return rejectTags_[size_t(index)];
48}
49
50void Bag::setExtra(const std::string &key, const std::string &value) { extra_[key] = value; }
51
52std::string Bag::getExtra(const std::string &key, const std::string &fallback) const {
53 auto it = extra_.find(key);
54 return it == extra_.end() ? fallback : it->second;
55}
56
57bool Bag::canAddItem(const std::string &itemId, int quantity) {
58 return InventorySystem::canAdd(this, itemId, quantity, nullptr);
59}
60
61std::string Bag::canAddItemReason(const std::string &itemId, int quantity) {
62 std::string reason;
63 InventorySystem::canAdd(this, itemId, quantity, &reason);
64 return reason;
65}
66
67int Bag::addItem(const std::string &itemId, int quantity) {
68 return InventorySystem::addItem(this, itemId, quantity);
69}
70
71int Bag::removeItem(const std::string &itemId, int quantity) {
72 return InventorySystem::removeItem(this, itemId, quantity);
73}
74
75int Bag::removeAt(int slot, int quantity) { return InventorySystem::removeAt(this, slot, quantity); }
76
77bool Bag::swapSlots(int slotA, int slotB) { return InventorySystem::swapSlots(this, slotA, slotB); }
78
79bool Bag::moveSlot(int fromSlot, int toSlot) {
80 return InventorySystem::moveSlot(this, fromSlot, toSlot);
81}
82
83bool Bag::splitStack(int slot, int quantity, int toSlot) {
84 return InventorySystem::splitStack(this, slot, quantity, toSlot);
85}
86
87int Bag::countItem(const std::string &itemId) const {
88 return InventorySystem::countItem(this, itemId);
89}
90
91int Bag::findItem(const std::string &itemId) const {
92 return InventorySystem::findItem(this, itemId);
93}
94
95int Bag::findItemByTag(const std::string &tag) const {
96 return InventorySystem::findItemByTag(this, tag);
97}
98
99float Bag::getUsedWeight() const { return InventorySystem::usedWeight(this); }
100
101float Bag::getUsedVolume() const { return InventorySystem::usedVolume(this); }
102
104
106
107bool Bag::isSlotEmpty(int slot) const {
108 if (slot < 0 || slot >= getSlotCount()) return true;
109 return slots_[size_t(slot)].empty();
110}
111
112std::string Bag::getSlotItemId(int slot) const {
113 if (slot < 0 || slot >= getSlotCount()) return {};
114 return slots_[size_t(slot)].itemId;
115}
116
117int Bag::getSlotQuantity(int slot) const {
118 if (slot < 0 || slot >= getSlotCount()) return 0;
119 return slots_[size_t(slot)].quantity;
120}
121
122int Bag::getSlotInstanceId(int slot) const {
123 if (slot < 0 || slot >= getSlotCount()) return 0;
124 return slots_[size_t(slot)].instanceId;
125}
126
127float Bag::getSlotDurability(int slot) const {
128 if (slot < 0 || slot >= getSlotCount()) return -1.f;
129 return slots_[size_t(slot)].durability;
130}
131
132void Bag::setSlotDurability(int slot, float durability) {
133 if (slot < 0 || slot >= getSlotCount()) return;
134 if (!slots_[size_t(slot)].empty()) slots_[size_t(slot)].durability = durability;
135}
136
137std::string Bag::getSlotProp(int slot, const std::string &key, const std::string &fallback) const {
138 if (slot < 0 || slot >= getSlotCount()) return fallback;
139 return slots_[size_t(slot)].getProp(key, fallback);
140}
141
142void Bag::setSlotProp(int slot, const std::string &key, const std::string &value) {
143 if (slot < 0 || slot >= getSlotCount()) return;
144 if (!slots_[size_t(slot)].empty()) slots_[size_t(slot)].setProp(key, value);
145}
146
147bool Bag::slotHasTag(int slot, const std::string &tag) const {
148 if (slot < 0 || slot >= getSlotCount()) return false;
149 return slots_[size_t(slot)].hasTag(tag);
150}
151
152void Bag::addSlotTag(int slot, const std::string &tag) {
153 if (slot < 0 || slot >= getSlotCount() || tag.empty()) return;
154 auto &s = slots_[size_t(slot)];
155 if (s.empty()) return;
156 if (!s.hasTag(tag)) s.tags.push_back(tag);
157}
158
159} // namespace eve::inventory
std::string value
uint32_t s
Definition Weather.cpp:28
int getSlotInstanceId(int slot) const
Definition Bag.cpp:122
float getUsedWeight() const
Definition Bag.cpp:99
void setSlotCount(int slotCount)
调整格数:扩容追加空槽;缩容会丢弃被裁掉的槽(不自动转移)。
Definition Bag.cpp:15
std::string canAddItemReason(const std::string &itemId, int quantity)
Definition Bag.cpp:61
std::string getAcceptTag(int index) const
Definition Bag.cpp:30
bool canAddItem(const std::string &itemId, int quantity)
便捷操作(转发 InventorySystem):增删 / 移动 / 查询。
Definition Bag.cpp:57
void setSlotDurability(int slot, float durability)
Definition Bag.cpp:132
std::string getSlotProp(int slot, const std::string &key, const std::string &fallback={}) const
Definition Bag.cpp:137
int addItem(const std::string &itemId, int quantity)
Definition Bag.cpp:67
int getRejectTagCount() const
Definition Bag.cpp:43
void addSlotTag(int slot, const std::string &tag)
Definition Bag.cpp:152
bool slotHasTag(int slot, const std::string &tag) const
Definition Bag.cpp:147
bool splitStack(int slot, int quantity, int toSlot)
Definition Bag.cpp:83
int removeItem(const std::string &itemId, int quantity)
Definition Bag.cpp:71
void clearRejectTags()
Definition Bag.cpp:35
int findItem(const std::string &itemId) const
Definition Bag.cpp:91
void addRejectTag(const std::string &tag)
Definition Bag.cpp:37
std::string getSlotItemId(int slot) const
Definition Bag.cpp:112
bool moveSlot(int fromSlot, int toSlot)
Definition Bag.cpp:79
int getSlotCount() const
格数。
Definition Bag.h:38
int countItem(const std::string &itemId) const
Definition Bag.cpp:87
float getUsedVolume() const
Definition Bag.cpp:101
int getAcceptTagCount() const
Definition Bag.cpp:28
bool swapSlots(int slotA, int slotB)
Definition Bag.cpp:77
std::string getExtra(const std::string &key, const std::string &fallback={}) const
Definition Bag.cpp:52
int getSlotQuantity(int slot) const
Definition Bag.cpp:117
void setExtra(const std::string &key, const std::string &value)
容器级额外属性(键值)。
Definition Bag.cpp:50
int removeAt(int slot, int quantity)
Definition Bag.cpp:75
bool isSlotEmpty(int slot) const
Definition Bag.cpp:107
float getSlotDurability(int slot) const
Definition Bag.cpp:127
void setSlotProp(int slot, const std::string &key, const std::string &value)
Definition Bag.cpp:142
void clearAcceptTags()
可接受 / 拒绝的标签过滤。
Definition Bag.cpp:20
int findItemByTag(const std::string &tag) const
Definition Bag.cpp:95
void addAcceptTag(const std::string &tag)
Definition Bag.cpp:22
Bag(int slotCount)
创建指定格数的空容器。
Definition Bag.cpp:8
int getUsedSlotCount() const
Definition Bag.cpp:103
void destroy()
释放资源并使其失效。
Definition Bag.cpp:13
std::string getRejectTag(int index) const
Definition Bag.cpp:45
static int addItem(Bag *bag, const std::string &itemId, int quantity)
返回实际放入数量(可能部分成功)。
static bool canAdd(Bag *bag, const std::string &itemId, int quantity, std::string *reason=nullptr)
static bool swapSlots(Bag *bag, int slotA, int slotB)
static float usedWeight(const Bag *bag)
static float usedVolume(const Bag *bag)
static bool splitStack(Bag *bag, int slot, int quantity, int toSlot)
static int countItem(const Bag *bag, const std::string &itemId)
static int usedSlotCount(const Bag *bag)
static int removeAt(Bag *bag, int slot, int quantity)
static int removeItem(Bag *bag, const std::string &itemId, int quantity)
static bool moveSlot(Bag *bag, int fromSlot, int toSlot)
static int findItemByTag(const Bag *bag, const std::string &tag)
static int findItem(const Bag *bag, const std::string &itemId)
运行时容器:固定格数的背包 / 箱子 / 商店栏等。 行为由 InventorySystem 提供;本类暴露便于脚本绑定的薄封装方法。
Definition Bag.cpp:6
容器中的一格堆叠(空槽:itemId 为空或 quantity <= 0)。
Definition ItemTypes.h:30