]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/items/inventory.qh
Fix inventory system networking so that it stores the inventory state on the client...
[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 Inventory g_inventory;
41 NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
42 {
43     make_pure(this);
44     g_inventory = this;
45     const int majorBits = Readbits(Inventory_groups_major);
46     for (int i = 0; i < Inventory_groups_major; ++i) {
47         if (!(majorBits & BIT(i))) {
48             continue;
49         }
50         const int minorBits = Readbits(Inventory_groups_minor);
51         for (int j = 0; j < Inventory_groups_minor; ++j) {
52             if (!(minorBits & BIT(j))) {
53                 continue;
54             }
55             const GameItem it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
56             .int fld = inv_items[it.m_id];
57             int prev = this.(fld);
58             int next = this.(fld) = ReadByte();
59             LOG_DEBUGF("%s: %.0f -> %.0f", it.m_name, prev, next);
60         }
61     }
62     return true;
63 }
64 #endif
65
66 #ifdef SVQC
67 int minorBitsArr[Inventory_groups_major];
68 void Inventory_Write(Inventory data, Inventory store)
69 {
70     if (!data) {
71         WriteShort(MSG_ENTITY, 0);
72         return;
73     }
74     TC(Inventory, data);
75
76         for (int i = 0; i < Inventory_groups_major; ++i)
77                 minorBitsArr[i] = 0;
78
79     int majorBits = 0;
80     FOREACH(Items, true, {
81         .int fld = inv_items[it.m_id];
82         const bool changed = store.(fld) != data.(fld);
83         store.(fld) = data.(fld);
84         if (changed) {
85                         int maj = G_MAJOR(it.m_id);
86                         majorBits = BITSET(majorBits, BIT(maj), true);
87                         minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
88         }
89     });
90
91         Writebits(MSG_ENTITY, majorBits, Inventory_groups_major);
92         for (int i = 0; i < Inventory_groups_major; ++i)
93         {
94                 if (!(majorBits & BIT(i)))
95                         continue;
96
97                 const int minorBits = minorBitsArr[i];
98                 Writebits(MSG_ENTITY, minorBits, Inventory_groups_minor);
99                 for (int j = 0; j < Inventory_groups_minor; ++j)
100                 {
101                         if (!(minorBits & BIT(j)))
102                                 continue;
103
104                         const entity it = REGISTRY_GET(Items, Inventory_groups_minor * i + j);
105                         WriteByte(MSG_ENTITY, data.inv_items[it.m_id]);
106                 }
107         }
108 }
109 #endif
110
111 #undef G_MAJOR
112 #undef G_MINOR
113
114 #ifdef SVQC
115 bool Inventory_Send(Inventory this, Client to, int sf)
116 {
117     TC(Inventory, this);
118     WriteHeader(MSG_ENTITY, ENT_CLIENT_INVENTORY);
119     TC(PlayerState, this.owner);
120     Inventory_Write(this, to.inventory_store);
121     return true;
122 }
123
124 bool Inventory_customize(entity this, entity client)
125 {
126     // sends to spectators too!
127     return (PS(client) && PS(client).inventory == this);
128 }
129
130 void Inventory_new(PlayerState this)
131 {
132     Inventory inv = NEW(Inventory);
133     setcefc(inv, Inventory_customize);
134     Net_LinkEntity((inv.owner = this).inventory = inv, false, 0, Inventory_Send);
135 }
136 void Inventory_delete(entity e) { delete(e.inventory.inventory); delete(e.inventory); }
137 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
138
139 void InventoryStorage_attach(entity e) { e.inventory_store = NEW(Inventory); e.inventory_store.drawonlytoclient = e; }
140 void InventoryStorage_detach(entity e) { delete(e.inventory_store); }
141 #endif