]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/items/inventory.qh
Inventory system
[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 prev = data.inv_items[i];
20         int next = data.inv_items[i] = ReadByte();
21         dprintf("%s: %.0f -> %.0f\n", ITEMS[i].m_name, prev, next);
22     }));
23 }
24 #endif
25
26 #ifdef SVQC
27 void Inventory_Write(Inventory data)
28 {
29     int bits = 0;
30     ITEMS_FOREACH(true, LAMBDA({
31         .int idx = inv_items[i];
32         bits = BITSET(bits, BIT(i), data.inventory.(idx) != (data.inventory.(idx) = data.(idx)));
33     }));
34     WriteInt24_t(MSG_ENTITY, bits);
35     ITEMS_FOREACH(bits & BIT(i), LAMBDA({
36         WriteByte(MSG_ENTITY, data.inv_items[i]);
37     }));
38 }
39 #endif
40
41 #ifdef SVQC
42 bool Inventory_Send(entity to, int sf)
43 {
44     WriteByte(MSG_ENTITY, ENT_CLIENT_INVENTORY);
45     entity e = self.owner;
46     if (IS_SPEC(e)) e = e.enemy;
47     Inventory data = e.inventory;
48     Inventory_Write(data);
49     return true;
50 }
51
52 void Inventory_new(entity e)
53 {
54     Inventory inv = new(Inventory), bak = new(Inventory);
55     inv.classname = "inventory", bak.classname = "inventory";
56     inv.inventory = bak;
57     inv.drawonlytoclient = e;
58     Net_LinkEntity((inv.owner = e).inventory = inv, false, 0, Inventory_Send);
59 }
60 void Inventory_delete(entity e) { remove(e.inventory.inventory); remove(e.inventory); }
61 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
62 #endif
63
64 #endif