Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
inventory.qh
Go to the documentation of this file.
1#pragma once
2
3#include "all.qh"
4
5#ifdef GAMEQC
10
15
16REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY)
17
18const int Inventory_groups_minor = 8; // must be a multiple of 8 (one byte) to optimize bandwidth usage
19const 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
25{
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
41
43
45{
46 if(g_inventory == this)
48}
49
50NET_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
77NET_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
88{
89 if (!data) {
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
134bool 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
144{
145 // sends to spectators too!
146 return (PS(client) && PS(client).inventory == this);
147}
148
150{
151 Inventory inv = NEW(Inventory);
153 this.inventory = inv;
154 inv.owner = this;
155 Net_LinkEntity(inv, false, 0, Inventory_Send);
156}
157
158void Inventory_delete(entity e) { delete(e.inventory); }
159void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
160
162{
163 //LOG_DEBUGF("entity %i picked up %s", player, this.m_name);
164 entity store = IS_PLAYER(player) ? PS(player) : player;
165 ++store.inventory.inv_items[this.m_id];
166 Inventory_update(store);
167}
168
170{
171 // NOTE: you will need to perform Inventory_update after this to update the storage entity
172 // (unless store is the storage entity)
173 FOREACH(Items, true, {
174 .int fld = inv_items[it.m_id];
175 store.(fld) = 0;
176 });
177}
178
179void InventoryStorage_attach(entity e) { e.inventory_store = NEW(Inventory); e.inventory_store.drawonlytoclient = e; }
180void InventoryStorage_delete(entity e) { delete(e.inventory_store); }
181#endif
#define BIT(n)
Only ever assign into the first 24 bits in QC (so max is BIT(23)).
Definition bits.qh:8
#define BITSET(var, mask, flag)
Definition bits.qh:11
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
int m_id
Definition item.qh:127
int inv_items[REGISTRY_MAX(Items)]
Stores counts of items, the id being the index.
Definition inventory.qh:8
Purpose: common player state, usable on client and server Client: singleton representing the viewed p...
Definition state.qh:8
fields which are explicitly/manually set are marked with "M", fields set automatically are marked wit...
Definition weapon.qh:44
void Pickup_Update(entity item, int count)
Definition pickup.qc:15
entity owner
Definition main.qh:87
#define IS_PLAYER(s)
Definition player.qh:243
int m_id
Definition effect.qh:19
Weapons
Definition guide.qh:113
prev
Definition all.qh:71
next
Definition all.qh:93
void Inventory_update(entity e)
Definition inventory.qh:159
int minorBitsArr[Inventory_groups_major]
Definition inventory.qh:86
bool Inventory_Send(Inventory this, Client to, int sf)
Definition inventory.qh:134
Inventory inventory_store
Player inventory storage (holds previous state)
Definition inventory.qh:14
#define G_MAJOR(id)
Definition inventory.qh:35
void InventoryStorage_attach(entity e)
Definition inventory.qh:179
void Inventory_remove(entity this)
Definition inventory.qh:44
const int Inventory_groups_minor
Definition inventory.qh:18
const int Inventory_groups_major
Definition inventory.qh:19
void Inventory_clear(entity store)
Definition inventory.qh:169
void Inventory_pickupitem(Pickup this, entity player)
Definition inventory.qh:161
void Inventory_new(PlayerState this)
Definition inventory.qh:149
Inventory inventory
Player inventory.
Definition inventory.qh:12
bool Inventory_customize(entity this, entity client)
Definition inventory.qh:143
Inventory g_inventory
Definition inventory.qh:42
#define G_MINOR(id)
Definition inventory.qh:36
void Inventory_delete(entity e)
Definition inventory.qh:158
void Inventory_Write(Inventory data, Inventory store)
Definition inventory.qh:87
void InventoryStorage_delete(entity e)
Definition inventory.qh:180
#define FOREACH(list, cond, body)
Definition iter.qh:19
#define TC(T, sym)
Definition _all.inc:82
#define NET_HANDLE(id, param)
Definition net.qh:15
const int MSG_ENTITY
Definition net.qh:115
#define WriteHeader(to, id)
Definition net.qh:221
#define REGISTER_NET_LINKED(id)
Definition net.qh:55
void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
Definition net.qh:123
int ReadByte()
int Readbits(int num)
Definition net.qh:372
#define LOG_DEBUGF(...)
Definition log.qh:81
float ceil(float f)
void WriteShort(float data, float dest, float desto)
void WriteByte(float data, float dest, float desto)
float floor(float f)
#define NEW(cname,...)
Definition oo.qh:117
#define ATTRIBARRAY(cname, name, type, cnt)
Definition oo.qh:266
#define make_pure(e)
direct use is
Definition oo.qh:13
#define CLASS(...)
Definition oo.qh:145
#define ENDCLASS(cname)
Definition oo.qh:281
#define NULL
Definition post.qh:14
#define error
Definition pre.qh:6
#define REGISTRY_COUNT(id)
Definition registry.qh:18
#define REGISTRY_GET(id, i)
Definition registry.qh:43
#define REGISTRY_MAX(id)
Definition registry.qh:17
#define setcefc(e, f)
#define PS(this)
Definition state.qh:18
#define STATIC_INIT(func)
during worldspawn
Definition static.qh:32