载入中...
搜索中...
未找到
InventorySystem.h
浏览该文件的文档.
1#pragma once
2
3// 库存操作静态入口 + 可插拔接纳 / 容量 / 堆叠 / 变更钩子。
4//
5// C++ 侧通过 register* 扩展;脚本侧通过 Bag 上的策略名字符串选用已注册规则。
6
8
9#include <functional>
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14namespace eve::inventory {
15
16class Bag;
17class EquipmentSet;
18
20public:
21 using AcceptFn =
22 std::function<bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)>;
23 using CapacityFn =
24 std::function<bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)>;
25 using StackFn =
26 std::function<bool(const ItemStack &a, const ItemStack &b, const ItemDefinition &def)>;
27 using ChangeHook = std::function<void(const InventoryChangeEvent &ev)>;
28
29 static void registerAcceptRule(const std::string &name, AcceptFn fn);
30 static void unregisterAcceptRule(const std::string &name);
31 static bool hasAcceptRule(const std::string &name);
32
33 static void registerCapacityPolicy(const std::string &name, CapacityFn fn);
34 static void unregisterCapacityPolicy(const std::string &name);
35 static bool hasCapacityPolicy(const std::string &name);
36
37 static void registerStackRule(const std::string &name, StackFn fn);
38 static void unregisterStackRule(const std::string &name);
39 static bool hasStackRule(const std::string &name);
40
41 static void registerChangeHook(const std::string &name, ChangeHook fn);
42 static void unregisterChangeHook(const std::string &name);
43 static bool hasChangeHook(const std::string &name);
44
46 static void ensureBuiltins();
47
48 static bool canAdd(Bag *bag, const std::string &itemId, int quantity, std::string *reason = nullptr);
50 static int addItem(Bag *bag, const std::string &itemId, int quantity);
51 static int removeItem(Bag *bag, const std::string &itemId, int quantity);
52 static int removeAt(Bag *bag, int slot, int quantity);
53 static bool swapSlots(Bag *bag, int slotA, int slotB);
54 static bool moveSlot(Bag *bag, int fromSlot, int toSlot);
55 static bool splitStack(Bag *bag, int slot, int quantity, int toSlot);
56 static int transfer(Bag *from, Bag *to, const std::string &itemId, int quantity);
57 static int transferSlot(Bag *from, int fromSlot, Bag *to, int quantity);
58
59 static int countItem(const Bag *bag, const std::string &itemId);
60 static int findItem(const Bag *bag, const std::string &itemId);
61 static int findItemByTag(const Bag *bag, const std::string &tag);
62 static float usedWeight(const Bag *bag);
63 static float usedVolume(const Bag *bag);
64 static int usedSlotCount(const Bag *bag);
65 static void clearBag(Bag *bag);
66
67 static bool equipFromBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag, int bagSlot);
68 static bool unequipToBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag);
69
70 static void pushEvent(InventoryChangeEvent ev);
71 static void pollEvents(std::vector<InventoryChangeEvent> &out);
72 static void clearEvents();
73 static const std::vector<InventoryChangeEvent> &events();
74
75 static int nextInstanceId();
76
77private:
78 static bool canStackTogether(const Bag &bag, const ItemStack &a, const ItemStack &b,
79 const ItemDefinition &def);
80 static bool checkAccept(const Bag &bag, const ItemDefinition &def, int quantity,
81 std::string *reason);
82 static bool checkCapacity(const Bag &bag, const ItemDefinition &def, int quantity,
83 std::string *reason);
84 static int freeSpaceInSlot(const Bag &bag, int slot, const ItemDefinition &def);
85 static void emit(InventoryChangeEvent ev);
86
87 static std::unordered_map<std::string, AcceptFn> &acceptRules();
88 static std::unordered_map<std::string, CapacityFn> &capacityPolicies();
89 static std::unordered_map<std::string, StackFn> &stackRules();
90 static std::unordered_map<std::string, ChangeHook> &changeHooks();
91 static std::vector<InventoryChangeEvent> &eventQueue();
92 static int &instanceCounter();
93 static bool &builtinsReady();
94};
95
96} // namespace eve::inventory
uint32_t a
uint32_t b
const char * name
Definition RockMesh.cpp:21
SettlementPipeline::Stage fn
格子型物品容器(脚本可直接操作)。
Definition Bag.h:17
static void unregisterStackRule(const std::string &name)
static int addItem(Bag *bag, const std::string &itemId, int quantity)
返回实际放入数量(可能部分成功)。
static void registerStackRule(const std::string &name, StackFn fn)
static bool equipFromBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag, int bagSlot)
static void ensureBuiltins()
确保内置规则已注册(模块首次使用时自动调用)。
static const std::vector< InventoryChangeEvent > & events()
static bool unequipToBag(EquipmentSet *eq, const std::string &equipSlot, Bag *bag)
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 bool hasChangeHook(const std::string &name)
std::function< void(const InventoryChangeEvent &ev)> ChangeHook
static void registerCapacityPolicy(const std::string &name, CapacityFn fn)
static float usedVolume(const Bag *bag)
std::function< bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)> CapacityFn
static void unregisterCapacityPolicy(const std::string &name)
static void pushEvent(InventoryChangeEvent ev)
static bool splitStack(Bag *bag, int slot, int quantity, int toSlot)
std::function< bool(const Bag &bag, const ItemDefinition &def, int quantity, std::string *reason)> AcceptFn
static void registerAcceptRule(const std::string &name, AcceptFn fn)
static int countItem(const Bag *bag, const std::string &itemId)
static void registerChangeHook(const std::string &name, ChangeHook fn)
static int usedSlotCount(const Bag *bag)
static bool hasStackRule(const std::string &name)
static int transfer(Bag *from, Bag *to, const std::string &itemId, int quantity)
static bool hasAcceptRule(const std::string &name)
static void unregisterAcceptRule(const std::string &name)
std::function< bool(const ItemStack &a, const ItemStack &b, const ItemDefinition &def)> StackFn
static void unregisterChangeHook(const std::string &name)
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 bool hasCapacityPolicy(const std::string &name)
static int transferSlot(Bag *from, int fromSlot, Bag *to, int quantity)
static int findItemByTag(const Bag *bag, const std::string &tag)
static void pollEvents(std::vector< InventoryChangeEvent > &out)
static int findItem(const Bag *bag, const std::string &itemId)
运行时容器:固定格数的背包 / 箱子 / 商店栏等。 行为由 InventorySystem 提供;本类暴露便于脚本绑定的薄封装方法。
Definition Bag.cpp:6
一次成功库存变更的事件(供脚本 poll / C++ hook)。
Definition ItemTypes.h:46
物品模板(进程级注册表中的定义,不含运行时数量)。
Definition ItemTypes.h:13
容器中的一格堆叠(空槽:itemId 为空或 quantity <= 0)。
Definition ItemTypes.h:30