Merge remote-tracking branch 'remotes/origin/master' into TimePath/itemsys
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / items / inventory.qh
1 #ifndef INVENTORY_H
2 #define INVENTORY_H
3
4 #include "all.qh"
5 #include "item/pickup.qh"
6
7 entityclass(Inventory);
8 /** Stores counts of items, the id being the index */
9 class(Inventory) .int inv_items[MAX_ITEMS];
10
11 /** Player inventory; Inventories also have one inventory for storing the previous state */
12 .Inventory inventory;
13
14 #ifdef CSQC
15 void Inventory_Read(Inventory data)
16 {
17     const int bits = ReadInt24_t();
18     ITEMS_FOREACH(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);
23     }));
24 }
25 #endif
26
27 #ifdef SVQC
28 void Inventory_Write(Inventory data)
29 {
30     int bits = 0;
31     ITEMS_FOREACH(true, LAMBDA({
32         .int fld = inv_items[i];
33         bits = BITSET(bits, BIT(i), data.inventory.(fld) != (data.inventory.(fld) = data.(fld)));
34     }));
35     WriteInt24_t(MSG_ENTITY, bits);
36     ITEMS_FOREACH(bits & BIT(i), LAMBDA({
37         WriteByte(MSG_ENTITY, data.inv_items[i]);
38     }));
39 }
40 #endif
41
42 #ifdef SVQC
43 bool Inventory_Send(entity to, int sf)
44 {
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);
50     return true;
51 }
52
53 void Inventory_new(entity e)
54 {
55     Inventory inv = new(Inventory), bak = new(Inventory);
56     inv.classname = "inventory", bak.classname = "inventory";
57     inv.inventory = bak;
58     inv.drawonlytoclient = e;
59     Net_LinkEntity((inv.owner = e).inventory = inv, false, 0, Inventory_Send);
60 }
61 void Inventory_delete(entity e) { remove(e.inventory.inventory); remove(e.inventory); }
62 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
63 #endif
64
65 #endif