]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/items/inventory.qh
Cleanup
[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[Items_MAX];
10
11 /** Player inventory; Inventories also have one inventory for storing the previous state */
12 .Inventory inventory;
13
14 #ifdef CSQC
15 NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
16 {
17     make_pure(this);
18     const int bits = ReadInt24_t();
19     FOREACH(Items, bits & BIT(it.m_id), LAMBDA(
20         .int fld = inv_items[it.m_id];
21         int prev = this.(fld);
22         int next = this.(fld) = ReadByte();
23         LOG_TRACEF("%s: %.0f -> %.0f\n", it.m_name, prev, next);
24     ));
25     return true;
26 }
27 #endif
28
29 #ifdef SVQC
30 void Inventory_Write(Inventory data)
31 {
32     int bits = 0;
33     FOREACH(Items, true, LAMBDA(
34         .int fld = inv_items[it.m_id];
35         bits = BITSET(bits, BIT(it.m_id), data.inventory.(fld) != (data.inventory.(fld) = data.(fld)));
36     ));
37     WriteInt24_t(MSG_ENTITY, bits);
38     FOREACH(Items, bits & BIT(it.m_id), LAMBDA(
39         WriteByte(MSG_ENTITY, data.inv_items[it.m_id]);
40     ));
41 }
42 #endif
43
44 #ifdef SVQC
45 bool Inventory_Send(entity this, entity to, int sf)
46 {
47     WriteHeader(MSG_ENTITY, ENT_CLIENT_INVENTORY);
48     entity e = self.owner;
49     if (/*IS_SPEC(e)*/ (e.classname == "spectator")) e = e.enemy;
50     Inventory data = e.inventory;
51     Inventory_Write(data);
52     return true;
53 }
54
55 void Inventory_new(entity e)
56 {
57     Inventory inv = new(Inventory), bak = new(Inventory);
58     make_pure(inv); make_pure(bak);
59     inv.inventory = bak;
60     inv.drawonlytoclient = e;
61     Net_LinkEntity((inv.owner = e).inventory = inv, false, 0, Inventory_Send);
62 }
63 void Inventory_delete(entity e) { remove(e.inventory.inventory); remove(e.inventory); }
64 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
65 #endif
66
67 #endif