载入中...
搜索中...
未找到
InventorySystem.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5// 库存操作静态入口 + 可插拔接纳 / 容量 / 堆叠 / 变更钩子。
6//
7// C++ 侧通过 register* 扩展;脚本侧通过 Bag 上的策略名字符串选用已注册规则。
8
10#include "common/Result.h"
11
12#include <functional>
13#include <memory>
14#include <string>
15#include <unordered_map>
16#include <vector>
17
18namespace eve::inventory {
19
20class Bag;
21class EquipmentSet;
22class InventoryResourceAccount;
23class InventorySaveSession;
24
27 std::string itemId;
28 int quantity = 0;
29};
30
54
78
81public:
82 using AcceptFn =
84 std::function<bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)>;
85 using CapacityFn =
87 std::function<bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)>;
88 using StackFn =
90 std::function<bool(const ItemStack &a, const ItemStack &b, const ItemDefinition &def)>;
91 using ChangeHook = std::function<void(const InventoryChangeEvent &ev)>;
92
94 static void registerAcceptRule(const std::string &name, AcceptFn fn);
96 static void unregisterAcceptRule(const std::string &name);
98 static bool hasAcceptRule(const std::string &name);
99
101 static void registerCapacityPolicy(const std::string &name, CapacityFn fn);
103 static void unregisterCapacityPolicy(const std::string &name);
105 static bool hasCapacityPolicy(const std::string &name);
106
108 static void registerStackRule(const std::string &name, StackFn fn);
110 static void unregisterStackRule(const std::string &name);
112 static bool hasStackRule(const std::string &name);
113
115 static void registerChangeHook(const std::string &name, ChangeHook fn);
117 static void unregisterChangeHook(const std::string &name);
119 static bool hasChangeHook(const std::string &name);
120
122 static void ensureBuiltins();
123
125 static bool canAdd(Bag *bag, const std::string &itemId, int quantity, std::string *reason = nullptr);
127 static int addItem(Bag *bag, const std::string &itemId, int quantity);
136 [[nodiscard]] static eve::Result<PreparedInventoryAdd>
137 prepareAddBatch(Bag *bag, const std::vector<InventoryItemGrant> &grants);
146 [[nodiscard]] static eve::Result<int> commitAddBatch(PreparedInventoryAdd prepared);
152 [[nodiscard]] static eve::Result<PreparedInventoryRemove>
153 prepareRemove(Bag *bag, const std::string &itemId, int quantity);
159 [[nodiscard]] static eve::Result<int> commitRemove(PreparedInventoryRemove prepared);
160 static int removeItem(Bag *bag, const std::string &itemId, int quantity);
162 static int removeAt(Bag *bag, int slot, int quantity);
164 static bool swapSlots(Bag *bag, int slotA, int slotB);
166 static bool moveSlot(Bag *bag, int fromSlot, int toSlot);
168 static bool splitStack(Bag *bag, int slot, int quantity, int toSlot);
170 static int transfer(Bag *from, Bag *to, const std::string &itemId, int quantity);
172 static int transferSlot(Bag *from, int fromSlot, Bag *to, int quantity);
173
175 static int countItem(const Bag *bag, const std::string &itemId);
177 static int findItem(const Bag *bag, const std::string &itemId);
179 static int findItemByTag(const Bag *bag, const std::string &tag);
181 static float usedWeight(const Bag *bag);
183 static float usedVolume(const Bag *bag);
185 static int usedSlotCount(const Bag *bag);
187 static void clearBag(Bag *bag);
188
190 static bool equipFromBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag, int bagSlot);
192 static bool unequipToBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag);
193
195 static void pushEvent(InventoryChangeEvent ev);
197 static void pollEvents(std::vector<InventoryChangeEvent> &out);
199 static void clearEvents();
201 static const std::vector<InventoryChangeEvent> &events();
202
204 static int nextInstanceId();
205
206private:
209
210 static void ensureNextInstanceIdAbove(int usedInstanceId) noexcept;
211
212 static bool canStackTogether(const Bag &bag, const ItemStack &a, const ItemStack &b,
213 const ItemDefinition &def);
214 static bool checkAccept(const Bag &bag, const ItemDefinition &def, int quantity,
215 std::string *reason);
216 static bool checkCapacity(const Bag &bag, const ItemDefinition &def, int quantity,
217 std::string *reason);
218 static int freeSpaceInSlot(const Bag &bag, int slot, const ItemDefinition &def);
219 static void emit(InventoryChangeEvent ev);
220 static int addItemImpl(Bag *bag, const std::string &itemId, int quantity, bool publish);
221
222 static std::unordered_map<std::string, AcceptFn> &acceptRules();
223 static std::unordered_map<std::string, CapacityFn> &capacityPolicies();
224 static std::unordered_map<std::string, StackFn> &stackRules();
225 static std::unordered_map<std::string, ChangeHook> &changeHooks();
226 static std::vector<InventoryChangeEvent> &eventQueue();
227 static int &instanceCounter();
228 static bool &builtinsReady();
229 static bool &changeHooksSuppressed();
230 static std::unique_ptr<Bag> cloneBag(const Bag &source);
231 static bool bagsEqual(const Bag &target, const Bag &baseline);
232};
233
234} // namespace eve::inventory
LogicalId target
LogicalId itemId
std::string from
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
HexCoordinates to
Cell the unit walks towards on this segment.
Definition HexUnits.cpp:64
std::string name
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
const std::string * tag
Move-only, checked operation results for the common layer.
Battle::Events events
const UnitySourceAsset & source
Move-only operation result carrying either a value or Status.
Definition Result.h:155
格子型物品容器(脚本可直接操作)。
Definition Bag.h:21
EVENGINE_API_FOUNDATION public API.
Definition Equipment.h:19
Adapts one Bag's item quantities to the common resource protocol.
Coordinates persistent inventory owners without duplicating their live state.
EVENGINE_API_FOUNDATION public API.
std::function< void(const InventoryChangeEvent &ev)> ChangeHook
std::function< bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)> CapacityFn
std::function< bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)> AcceptFn
std::function< bool(const ItemStack &a, const ItemStack &b, const ItemDefinition &def)> StackFn
Move-only, validated inventory state prepared for one Bag.
PreparedInventoryAdd()
Prepared inventory add.
~PreparedInventoryAdd()
Prepared inventory add.
PreparedInventoryAdd(PreparedInventoryAdd &&) noexcept
Prepared inventory add.
Move-only, validated inventory removal prepared for one Bag.
~PreparedInventoryRemove()
Prepared inventory remove.
PreparedInventoryRemove(PreparedInventoryRemove &&) noexcept
Prepared inventory remove.
PreparedInventoryRemove()
Prepared inventory remove.
运行时容器:固定格数的背包 / 箱子 / 商店栏等。 行为由 InventorySystem 提供;本类暴露便于脚本绑定的薄封装方法。
SettlementPipeline::Stage fn
一次成功库存变更的事件(供脚本 poll / C++ hook)。
Definition ItemTypes.h:55
One exact item quantity requested by an atomic inventory batch.
物品模板(进程级注册表中的定义,不含运行时数量)。
Definition ItemTypes.h:15
容器中的一格堆叠(空槽:itemId 为空或 quantity <= 0)。
Definition ItemTypes.h:34