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 #include <client/hud/panel/pickup.qh>
42 Inventory g_inventory;
44 void Inventory_remove(entity this)
46 if(g_inventory == this)
50 NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
54 this.entremove = Inventory_remove;
55 const int majorBits = Readbits(Inventory_groups_major);
56 for (int i = 0; i < Inventory_groups_major; ++i) {
57 if (!(majorBits & BIT(i))) {
60 const int minorBits = Readbits(Inventory_groups_minor);
61 for (int j = 0; j < Inventory_groups_minor; ++j) {
62 if (!(minorBits & BIT(j))) {
65 const GameItem it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
66 .int fld = inv_items[it.m_id];
67 int prev = this.(fld);
68 int next = this.(fld) = ReadByte();
70 Pickup_Update(it, next - prev);
71 LOG_DEBUGF("%s: %.0f -> %.0f", it.m_name, prev, next);
77 NET_HANDLE(TE_CSQC_WEAPONPICKUP, bool isnew)
79 const Weapon it = REGISTRY_GET(Weapons, ReadByte());
86 int minorBitsArr[Inventory_groups_major];
87 void Inventory_Write(Inventory data, Inventory store)
90 WriteShort(MSG_ENTITY, 0);
95 for (int i = 0; i < Inventory_groups_major; ++i)
99 FOREACH(Items, true, {
100 .int fld = inv_items[it.m_id];
101 const bool changed = store.(fld) != data.(fld);
102 store.(fld) = data.(fld);
104 int maj = G_MAJOR(it.m_id);
105 majorBits = BITSET(majorBits, BIT(maj), true);
106 minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
110 Writebits(MSG_ENTITY, majorBits, Inventory_groups_major);
111 for (int i = 0; i < Inventory_groups_major; ++i)
113 if (!(majorBits & BIT(i)))
116 const int minorBits = minorBitsArr[i];
117 Writebits(MSG_ENTITY, minorBits, Inventory_groups_minor);
118 for (int j = 0; j < Inventory_groups_minor; ++j)
120 if (!(minorBits & BIT(j)))
123 const entity it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
124 WriteByte(MSG_ENTITY, data.inv_items[it.m_id]);
134 bool Inventory_Send(Inventory this, Client to, int sf)
137 WriteHeader(MSG_ENTITY, ENT_CLIENT_INVENTORY);
138 TC(PlayerState, this.owner);
139 Inventory_Write(this, to.inventory_store);
143 bool Inventory_customize(entity this, entity client)
145 // sends to spectators too!
146 return (PS(client) && PS(client).inventory == this);
149 void Inventory_new(PlayerState this)
151 Inventory inv = NEW(Inventory);
152 setcefc(inv, Inventory_customize);
153 this.inventory = inv;
155 Net_LinkEntity(inv, false, 0, Inventory_Send);
158 void Inventory_delete(entity e) { delete(e.inventory); }
159 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
161 void Inventory_pickupitem(Pickup this, entity player)
163 //LOG_DEBUGF("entity %i picked up %s", player, this.m_name);
164 entity store = IS_PLAYER(player) ? PS(player) : player;
165 ++store.inventory.inv_items[this.m_id];
166 Inventory_update(store);
169 void Inventory_clear(entity store)
171 // NOTE: you will need to perform Inventory_update after this to update the storage entity
172 // (unless store is the storage entity)
173 FOREACH(Items, true, {
174 .int fld = inv_items[it.m_id];
179 void InventoryStorage_attach(entity e) { e.inventory_store = NEW(Inventory); e.inventory_store.drawonlytoclient = e; }
180 void InventoryStorage_delete(entity e) { delete(e.inventory_store); }