Remove some unnecessary item bits, increase maximum items to 32 now that the item...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / items / inventory.qh
1 #pragma once
2
3 #include "all.qh"
4
5 #ifdef GAMEQC
6 CLASS(Inventory, Object)
7     /** Stores counts of items, the id being the index */
8     ATTRIBARRAY(Inventory, inv_items, int, REGISTRY_MAX(Items));
9     /** Previous state */
10     ATTRIB(Inventory, inventory, Inventory);
11 ENDCLASS(Inventory)
12
13 /** Player inventory */
14 .Inventory inventory;
15
16 REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY)
17
18 const int Inventory_groups_minor = 8; // exactly 1 byte
19 const int Inventory_groups_major = 4; // ceil(REGISTRY_MAX(Items) / Inventory_groups_minor)
20
21 #define G_MAJOR(id) (floor((id) / Inventory_groups_minor))
22 #define G_MINOR(id) ((id) % Inventory_groups_minor)
23 #endif
24
25 #ifdef CSQC
26 Inventory g_inventory;
27 NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
28 {
29     make_pure(this);
30     g_inventory = this;
31     const int majorBits = ReadByte();
32     for (int i = 0; i < Inventory_groups_major; ++i) {
33         if (!(majorBits & BIT(i))) {
34             continue;
35         }
36         const int minorBits = ReadByte();
37         for (int j = 0; j < Inventory_groups_minor; ++j) {
38             if (!(minorBits & BIT(j))) {
39                 continue;
40             }
41             const GameItem it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
42             .int fld = inv_items[it.m_id];
43             int prev = this.(fld);
44             int next = this.(fld) = ReadByte();
45             LOG_DEBUGF("%s: %.0f -> %.0f", it.m_name, prev, next);
46         }
47     }
48     return true;
49 }
50 #endif
51
52 #ifdef SVQC
53 int minorBitsArr[Inventory_groups_major];
54 void Inventory_Write(Inventory data)
55 {
56     if (!data) {
57         WriteShort(MSG_ENTITY, 0);
58         return;
59     }
60     TC(Inventory, data);
61
62         for (int i = 0; i < Inventory_groups_major; ++i)
63                 minorBitsArr[i] = 0;
64
65     int majorBits = 0;
66     FOREACH(Items, true, {
67         .int fld = inv_items[it.m_id];
68         const bool changed = data.inventory.(fld) != data.(fld);
69         if (changed) {
70                         int maj = G_MAJOR(it.m_id);
71                         majorBits = BITSET(majorBits, BIT(maj), true);
72                         minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
73         }
74     });
75     WriteByte(MSG_ENTITY, majorBits);
76
77         for (int i = 0; i < Inventory_groups_major; ++i)
78         {
79                 if (!(majorBits & BIT(i)))
80                         continue;
81
82                 const int minorBits = minorBitsArr[i];
83                 WriteByte(MSG_ENTITY, minorBits);
84                 for (int j = 0; j < Inventory_groups_minor; ++j)
85                 {
86                         if (!(minorBits & BIT(j)))
87                                 continue;
88
89                         const entity it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
90                         WriteByte(MSG_ENTITY, data.inv_items[it.m_id]);
91                 }
92         }
93 }
94 #endif
95
96 #undef G_MAJOR
97 #undef G_MINOR
98
99 #ifdef SVQC
100 bool Inventory_Send(Inventory this, Client to, int sf)
101 {
102     TC(Inventory, this);
103     WriteHeader(MSG_ENTITY, ENT_CLIENT_INVENTORY);
104     TC(PlayerState, this.owner);
105     Inventory_Write(this);
106     return true;
107 }
108
109 bool Inventory_customize(entity this, entity client)
110 {
111     // sends to spectators too!
112     return (PS(client) && PS(client).inventory == this);
113 }
114
115 void Inventory_new(PlayerState this)
116 {
117     Inventory inv = NEW(Inventory), bak = NEW(Inventory);
118     inv.inventory = bak;
119     setcefc(inv, Inventory_customize);
120     Net_LinkEntity((inv.owner = this).inventory = inv, false, 0, Inventory_Send);
121 }
122 void Inventory_delete(entity e) { delete(e.inventory.inventory); delete(e.inventory); }
123 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
124 #endif