6 CLASS(Inventory, Object)
7 /** Stores counts of items, the id being the index */
8 ATTRIBARRAY(Inventory, inv_items, int, REGISTRY_MAX(Items));
11 /** Player inventory */
13 /** Player inventory storage (holds previous state) */
14 .Inventory inventory_store;
16 REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY)
18 const int Inventory_groups_minor = 8; // must be a multiple of 8 (one byte) to optimize bandwidth usage
19 const int Inventory_groups_major = 4; // must be >= ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor)
22 // no need to perform these checks on both server and client
24 STATIC_INIT(Inventory)
26 if (Inventory_groups_minor / 8 != floor(Inventory_groups_minor / 8))
27 error("Inventory_groups_minor is not a multiple of 8.");
28 int min_major_value = ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor);
29 if (Inventory_groups_major < min_major_value)
30 error(sprintf("Inventory_groups_major can not be < %d.", min_major_value));
35 #define G_MAJOR(id) (floor((id) / Inventory_groups_minor))
36 #define G_MINOR(id) ((id) % Inventory_groups_minor)
40 Inventory g_inventory;
41 void Inventory_remove(entity this)
43 if(g_inventory == this)
47 NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
51 this.entremove = Inventory_remove;
52 const int majorBits = Readbits(Inventory_groups_major);
53 for (int i = 0; i < Inventory_groups_major; ++i) {
54 if (!(majorBits & BIT(i))) {
57 const int minorBits = Readbits(Inventory_groups_minor);
58 for (int j = 0; j < Inventory_groups_minor; ++j) {
59 if (!(minorBits & BIT(j))) {
62 const GameItem it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
63 .int fld = inv_items[it.m_id];
64 int prev = this.(fld);
65 int next = this.(fld) = ReadByte();
66 LOG_DEBUGF("%s: %.0f -> %.0f", it.m_name, prev, next);
74 int minorBitsArr[Inventory_groups_major];
75 void Inventory_Write(Inventory data, Inventory store)
78 WriteShort(MSG_ENTITY, 0);
83 for (int i = 0; i < Inventory_groups_major; ++i)
87 FOREACH(Items, true, {
88 .int fld = inv_items[it.m_id];
89 const bool changed = store.(fld) != data.(fld);
90 store.(fld) = data.(fld);
92 int maj = G_MAJOR(it.m_id);
93 majorBits = BITSET(majorBits, BIT(maj), true);
94 minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
98 Writebits(MSG_ENTITY, majorBits, Inventory_groups_major);
99 for (int i = 0; i < Inventory_groups_major; ++i)
101 if (!(majorBits & BIT(i)))
104 const int minorBits = minorBitsArr[i];
105 Writebits(MSG_ENTITY, minorBits, Inventory_groups_minor);
106 for (int j = 0; j < Inventory_groups_minor; ++j)
108 if (!(minorBits & BIT(j)))
111 const entity it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
112 WriteByte(MSG_ENTITY, data.inv_items[it.m_id]);
122 bool Inventory_Send(Inventory this, Client to, int sf)
125 WriteHeader(MSG_ENTITY, ENT_CLIENT_INVENTORY);
126 TC(PlayerState, this.owner);
127 Inventory_Write(this, to.inventory_store);
131 bool Inventory_customize(entity this, entity client)
133 // sends to spectators too!
134 return (PS(client) && PS(client).inventory == this);
137 void Inventory_new(PlayerState this)
139 Inventory inv = NEW(Inventory);
140 setcefc(inv, Inventory_customize);
141 Net_LinkEntity((inv.owner = this).inventory = inv, false, 0, Inventory_Send);
143 void Inventory_delete(entity e) { delete(e.inventory); }
144 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
146 void InventoryStorage_attach(entity e) { e.inventory_store = NEW(Inventory); e.inventory_store.drawonlytoclient = e; }
147 void InventoryStorage_delete(entity e) { delete(e.inventory_store); }
148 void InventoryStorage_clear(entity e)
150 // we don't need to network the changes, that is done when the inventory is detached
151 FOREACH(Items, true, {
152 .int fld = inv_items[it.m_id];
153 e.inventory_store.(fld) = 0;