5 #include "item/pickup.qh"
7 entityclass(Inventory);
8 /** Stores counts of items, the id being the index */
9 class(Inventory) .int inv_items[MAX_ITEMS];
11 /** Player inventory; Inventories also have one inventory for storing the previous state */
15 void Inventory_Read(Inventory data)
17 const int bits = ReadInt24_t();
18 FOREACH(ITEMS, 0, ITEM_COUNT, bits & BIT(i), LAMBDA({
19 .int fld = inv_items[i];
20 int prev = data.(fld);
21 int next = data.(fld) = ReadByte();
22 dprintf("%s: %.0f -> %.0f\n", ITEMS[i].m_name, prev, next);
28 void Inventory_Write(Inventory data)
31 FOREACH(ITEMS, 0, ITEM_COUNT, true, LAMBDA({
32 .int fld = inv_items[i];
33 bits = BITSET(bits, BIT(i), data.inventory.(fld) != (data.inventory.(fld) = data.(fld)));
35 WriteInt24_t(MSG_ENTITY, bits);
36 FOREACH(ITEMS, 0, ITEM_COUNT, bits & BIT(i), LAMBDA({
37 WriteByte(MSG_ENTITY, data.inv_items[i]);
43 bool Inventory_Send(entity to, int sf)
45 WriteByte(MSG_ENTITY, ENT_CLIENT_INVENTORY);
46 entity e = self.owner;
47 if (IS_SPEC(e)) e = e.enemy;
48 Inventory data = e.inventory;
49 Inventory_Write(data);
53 void Inventory_new(entity e)
55 Inventory inv = new(Inventory), bak = new(Inventory);
56 inv.classname = "inventory", bak.classname = "inventory";
58 inv.drawonlytoclient = e;
59 Net_LinkEntity((inv.owner = e).inventory = inv, false, 0, Inventory_Send);
61 void Inventory_delete(entity e) { remove(e.inventory.inventory); remove(e.inventory); }
62 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }