]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/items/inventory.qh
Item Pickup panel
[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 ENDCLASS(Inventory)
10
11 /** Player inventory */
12 .Inventory inventory;
13 /** Player inventory storage (holds previous state) */
14 .Inventory inventory_store;
15
16 REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY)
17
18 const int Inventory_groups_minor = 8; // must be a multiple of 8 (one byte) to optimize bandwidth usage
19 const int Inventory_groups_major = 4; // must be >= ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor)
20 #endif
21
22 // no need to perform these checks on both server and client
23 #ifdef CSQC
24 STATIC_INIT(Inventory)
25 {
26         if (Inventory_groups_minor / 8 != floor(Inventory_groups_minor / 8))
27                 error("Inventory_groups_minor is not a multiple of 8.");
28         int min_major_value = ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor);
29         if (Inventory_groups_major < min_major_value)
30                 error(sprintf("Inventory_groups_major can not be < %d.", min_major_value));
31 }
32 #endif
33
34 #ifdef SVQC
35 #define G_MAJOR(id) (floor((id) / Inventory_groups_minor))
36 #define G_MINOR(id) ((id) % Inventory_groups_minor)
37 #endif
38
39 #ifdef CSQC
40 #include <client/hud/panel/pickup.qh>
41
42 Inventory g_inventory;
43
44 void Inventory_remove(entity this)
45 {
46     if(g_inventory == this)
47         g_inventory = NULL;
48 }
49
50 NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
51 {
52     make_pure(this);
53     g_inventory = this;
54     this.entremove = Inventory_remove;
55     const int majorBits = Readbits(Inventory_groups_major);
56     for (int i = 0; i < Inventory_groups_major; ++i) {
57         if (!(majorBits & BIT(i))) {
58             continue;
59         }
60         const int minorBits = Readbits(Inventory_groups_minor);
61         for (int j = 0; j < Inventory_groups_minor; ++j) {
62             if (!(minorBits & BIT(j))) {
63                 continue;
64             }
65             const GameItem it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
66             .int fld = inv_items[it.m_id];
67             int prev = this.(fld);
68             int next = this.(fld) = ReadByte();
69
70             Pickup_Update(it, next - prev);
71             LOG_DEBUGF("%s: %.0f -> %.0f", it.m_name, prev, next);
72         }
73     }
74     return true;
75 }
76
77 NET_HANDLE(TE_CSQC_WEAPONPICKUP, bool isnew)
78 {
79         const Weapon it = REGISTRY_GET(Weapons, ReadByte());
80         Pickup_Update(it, 1);
81         return true;
82 }
83 #endif
84
85 #ifdef SVQC
86 int minorBitsArr[Inventory_groups_major];
87 void Inventory_Write(Inventory data, Inventory store)
88 {
89     if (!data) {
90         WriteShort(MSG_ENTITY, 0);
91         return;
92     }
93     TC(Inventory, data);
94
95         for (int i = 0; i < Inventory_groups_major; ++i)
96                 minorBitsArr[i] = 0;
97
98     int majorBits = 0;
99     FOREACH(Items, true, {
100         .int fld = inv_items[it.m_id];
101         const bool changed = store.(fld) != data.(fld);
102         store.(fld) = data.(fld);
103         if (changed) {
104                         int maj = G_MAJOR(it.m_id);
105                         majorBits = BITSET(majorBits, BIT(maj), true);
106                         minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
107         }
108     });
109
110         Writebits(MSG_ENTITY, majorBits, Inventory_groups_major);
111         for (int i = 0; i < Inventory_groups_major; ++i)
112         {
113                 if (!(majorBits & BIT(i)))
114                         continue;
115
116                 const int minorBits = minorBitsArr[i];
117                 Writebits(MSG_ENTITY, minorBits, Inventory_groups_minor);
118                 for (int j = 0; j < Inventory_groups_minor; ++j)
119                 {
120                         if (!(minorBits & BIT(j)))
121                                 continue;
122
123                         const entity it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
124                         WriteByte(MSG_ENTITY, data.inv_items[it.m_id]);
125                 }
126         }
127 }
128 #endif
129
130 #undef G_MAJOR
131 #undef G_MINOR
132
133 #ifdef SVQC
134 bool Inventory_Send(Inventory this, Client to, int sf)
135 {
136     TC(Inventory, this);
137     WriteHeader(MSG_ENTITY, ENT_CLIENT_INVENTORY);
138     TC(PlayerState, this.owner);
139     Inventory_Write(this, to.inventory_store);
140     return true;
141 }
142
143 bool Inventory_customize(entity this, entity client)
144 {
145     // sends to spectators too!
146     return (PS(client) && PS(client).inventory == this);
147 }
148
149 void Inventory_new(PlayerState this)
150 {
151     Inventory inv = NEW(Inventory);
152     setcefc(inv, Inventory_customize);
153         this.inventory = inv;
154         inv.owner = this;
155         Net_LinkEntity(inv, false, 0, Inventory_Send);
156 }
157 void Inventory_delete(entity e) { delete(e.inventory); }
158 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
159
160 void Inventory_clear(entity store)
161 {
162     // NOTE: you will need to perform Inventory_update after this to update the storage entity
163     // (unless store is the storage entity)
164     FOREACH(Items, true, {
165         .int fld = inv_items[it.m_id];
166         store.(fld) = 0;
167     });
168 }
169
170 void InventoryStorage_attach(entity e) { e.inventory_store = NEW(Inventory); e.inventory_store.drawonlytoclient = e; }
171 void InventoryStorage_delete(entity e) { delete(e.inventory_store); }
172 #endif