载入中...
搜索中...
未找到
Equipment.cpp
浏览该文件的文档.
2#include "inventory/Bag.h"
4#include "inventory/Item.h"
5
6#include <algorithm>
7
8namespace eve::inventory {
9
10void EquipmentSet::destroy() { delete this; }
11
12void EquipmentSet::defineSlot(const std::string &slotName) {
13 if (slotName.empty()) return;
14 if (slots_.find(slotName) == slots_.end()) {
15 slots_.emplace(slotName, Slot{});
16 order_.push_back(slotName);
17 }
18}
19
20void EquipmentSet::clearSlotAllowedTags(const std::string &slotName) {
21 auto it = slots_.find(slotName);
22 if (it == slots_.end()) return;
23 it->second.allowedTags.clear();
24}
25
26void EquipmentSet::addSlotAllowedTag(const std::string &slotName, const std::string &tag) {
27 defineSlot(slotName);
28 if (tag.empty()) return;
29 auto &tags = slots_[slotName].allowedTags;
30 if (std::find(tags.begin(), tags.end(), tag) == tags.end()) tags.push_back(tag);
31}
32
33bool EquipmentSet::hasSlot(const std::string &slotName) const {
34 return slots_.find(slotName) != slots_.end();
35}
36
37int EquipmentSet::getSlotCount() const { return int(order_.size()); }
38
39std::string EquipmentSet::getSlotName(int index) const {
40 if (index < 0 || index >= int(order_.size())) return {};
41 return order_[size_t(index)];
42}
43
44bool EquipmentSet::isSlotEmpty(const std::string &slotName) const {
45 auto it = slots_.find(slotName);
46 if (it == slots_.end()) return true;
47 return it->second.stack.empty();
48}
49
50std::string EquipmentSet::getSlotItemId(const std::string &slotName) const {
51 auto it = slots_.find(slotName);
52 if (it == slots_.end()) return {};
53 return it->second.stack.itemId;
54}
55
56int EquipmentSet::getSlotQuantity(const std::string &slotName) const {
57 auto it = slots_.find(slotName);
58 if (it == slots_.end()) return 0;
59 return it->second.stack.quantity;
60}
61
62int EquipmentSet::getSlotInstanceId(const std::string &slotName) const {
63 auto it = slots_.find(slotName);
64 if (it == slots_.end()) return 0;
65 return it->second.stack.instanceId;
66}
67
68const ItemStack *EquipmentSet::stackAt(const std::string &slotName) const {
69 auto it = slots_.find(slotName);
70 return it == slots_.end() ? nullptr : &it->second.stack;
71}
72
73ItemStack *EquipmentSet::stackAt(const std::string &slotName) {
74 auto it = slots_.find(slotName);
75 return it == slots_.end() ? nullptr : &it->second.stack;
76}
77
78const std::vector<std::string> *EquipmentSet::allowedTags(const std::string &slotName) const {
79 auto it = slots_.find(slotName);
80 return it == slots_.end() ? nullptr : &it->second.allowedTags;
81}
82
83bool EquipmentSet::canEquipFromBag(Bag *bag, int bagSlot, const std::string &equipSlot,
84 std::string *reason) const {
85 if (!bag || equipSlot.empty()) {
86 if (reason) *reason = "invalid_args";
87 return false;
88 }
89 auto it = slots_.find(equipSlot);
90 if (it == slots_.end()) {
91 if (reason) *reason = "unknown_slot";
92 return false;
93 }
94 if (bagSlot < 0 || bagSlot >= bag->getSlotCount()) {
95 if (reason) *reason = "bad_bag_slot";
96 return false;
97 }
98 const auto &bs = bag->slots()[size_t(bagSlot)];
99 if (bs.empty()) {
100 if (reason) *reason = "empty_bag_slot";
101 return false;
102 }
103 const ItemDefinition *def = ItemRegistry::find(bs.itemId);
104 if (!def) {
105 if (reason) *reason = "unknown_item";
106 return false;
107 }
108 if (!def->equipSlot.empty() && def->equipSlot != equipSlot) {
109 if (reason) *reason = "equip_slot_mismatch";
110 return false;
111 }
112 const auto &allowed = it->second.allowedTags;
113 if (!allowed.empty()) {
114 bool ok = false;
115 for (const auto &t : allowed) {
116 if (def->hasTag(t) || bs.hasTag(t)) {
117 ok = true;
118 break;
119 }
120 }
121 if (!ok) {
122 if (reason) *reason = "tag_mismatch";
123 return false;
124 }
125 }
126 // If current slot occupied, bagSlot will receive the old item (swap); bagSlot becomes empty
127 // after take, so always OK capacity-wise for swap. If we'd need an extra empty slot we don't.
128 return true;
129}
130
131bool EquipmentSet::equipFromBag(const std::string &equipSlot, Bag *bag, int bagSlot) {
132 return InventorySystem::equipFromBag(this, equipSlot, bag, bagSlot);
133}
134
135bool EquipmentSet::unequipToBag(const std::string &equipSlot, Bag *bag) {
136 return InventorySystem::unequipToBag(this, equipSlot, bag);
137}
138
139bool EquipmentSet::clearSlot(const std::string &slotName) {
140 auto it = slots_.find(slotName);
141 if (it == slots_.end() || it->second.stack.empty()) return false;
142 std::string itemId = it->second.stack.itemId;
143 int qty = it->second.stack.quantity;
144 it->second.stack.clear();
146 ev.action = "unequip";
147 ev.itemId = itemId;
148 ev.quantity = qty;
149 ev.equipSlot = slotName;
150 InventorySystem::pushEvent(std::move(ev));
151 return true;
152}
153
154} // namespace eve::inventory
格子型物品容器(脚本可直接操作)。
Definition Bag.h:17
const std::vector< ItemStack > & slots() const
Definition Bag.h:101
int getSlotCount() const
格数。
Definition Bag.h:38
const std::vector< std::string > * allowedTags(const std::string &slotName) const
Definition Equipment.cpp:78
void clearSlotAllowedTags(const std::string &slotName)
Definition Equipment.cpp:20
bool hasSlot(const std::string &slotName) const
Definition Equipment.cpp:33
int getSlotInstanceId(const std::string &slotName) const
Definition Equipment.cpp:62
void addSlotAllowedTag(const std::string &slotName, const std::string &tag)
Definition Equipment.cpp:26
void defineSlot(const std::string &slotName)
声明一个装备槽;已存在则保留当前物品,仅更新允许标签。
Definition Equipment.cpp:12
std::string getSlotName(int index) const
Definition Equipment.cpp:39
bool clearSlot(const std::string &slotName)
直接清空槽位(物品丢弃,不回背包)。
bool equipFromBag(const std::string &equipSlot, Bag *bag, int bagSlot)
bool isSlotEmpty(const std::string &slotName) const
Definition Equipment.cpp:44
std::string getSlotItemId(const std::string &slotName) const
Definition Equipment.cpp:50
const ItemStack * stackAt(const std::string &slotName) const
Definition Equipment.cpp:68
int getSlotQuantity(const std::string &slotName) const
Definition Equipment.cpp:56
bool unequipToBag(const std::string &equipSlot, Bag *bag)
bool canEquipFromBag(Bag *bag, int bagSlot, const std::string &equipSlot, std::string *reason=nullptr) const
Definition Equipment.cpp:83
static bool equipFromBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag, int bagSlot)
static bool unequipToBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag)
static void pushEvent(InventoryChangeEvent ev)
static const ItemDefinition * find(const std::string &id)
Definition Item.cpp:53
运行时容器:固定格数的背包 / 箱子 / 商店栏等。 行为由 InventorySystem 提供;本类暴露便于脚本绑定的薄封装方法。
Definition Bag.cpp:6
一次成功库存变更的事件(供脚本 poll / C++ hook)。
Definition ItemTypes.h:46
std::string action
add/remove/move/swap/split/merge/transfer/equip/unequip
Definition ItemTypes.h:47
物品模板(进程级注册表中的定义,不含运行时数量)。
Definition ItemTypes.h:13
std::string equipSlot
非空表示建议装备槽名;是否可装备由 EquipmentSet 槽位配置最终决定。
Definition ItemTypes.h:22
bool hasTag(const std::string &tag) const
Definition Item.cpp:11
容器中的一格堆叠(空槽:itemId 为空或 quantity <= 0)。
Definition ItemTypes.h:30