REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY)
-const int Inventory_groups_minor = 8; // exactly 1 byte
-const int Inventory_groups_major = 4; // ceil(REGISTRY_MAX(Items) / Inventory_groups_minor)
+const int Inventory_groups_minor = 8; // must be a multiple of 8 (one byte) to optimize bandwidth usage
+const int Inventory_groups_major = 4; // must be >= ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor)
+#endif
+// no need to perform these checks on both server and client
+#ifdef CSQC
+STATIC_INIT(Inventory)
+{
+ if (Inventory_groups_minor / 8 != floor(Inventory_groups_minor / 8))
+ error("Inventory_groups_minor is not a multiple of 8.");
+ int min_major_value = ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor);
+ if (Inventory_groups_major < min_major_value)
+ error(sprintf("Inventory_groups_major can not be < %d.", min_major_value));
+}
+#endif
+
+#ifdef SVQC
#define G_MAJOR(id) (floor((id) / Inventory_groups_minor))
#define G_MINOR(id) ((id) % Inventory_groups_minor)
#endif
{
make_pure(this);
g_inventory = this;
- const int majorBits = ReadByte();
+ const int majorBits = Readbits(Inventory_groups_major);
for (int i = 0; i < Inventory_groups_major; ++i) {
if (!(majorBits & BIT(i))) {
continue;
}
- const int minorBits = ReadByte();
+ const int minorBits = Readbits(Inventory_groups_minor);
for (int j = 0; j < Inventory_groups_minor; ++j) {
if (!(minorBits & BIT(j))) {
continue;
minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
}
});
- WriteByte(MSG_ENTITY, majorBits);
+ Writebits(MSG_ENTITY, majorBits, Inventory_groups_major);
for (int i = 0; i < Inventory_groups_major; ++i)
{
if (!(majorBits & BIT(i)))
continue;
const int minorBits = minorBitsArr[i];
- WriteByte(MSG_ENTITY, minorBits);
+ Writebits(MSG_ENTITY, minorBits, Inventory_groups_minor);
for (int j = 0; j < Inventory_groups_minor; ++j)
{
if (!(minorBits & BIT(j)))
#define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
#define ReadVector2D() vec2(ReadFloat(), ReadFloat())
+ int Readbits(int num)
+ {
+ if (num > 16) return ReadInt24_t();
+ if (num > 8) return ReadShort();
+ return ReadByte();
+ }
+
float ReadApproxPastTime()
{
float dt = ReadByte();
#define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
#define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
+ void Writebits(float dst, float val, int num)
+ {
+ if (num > 16) { WriteInt24_t(dst, val); return; }
+ if (num > 8) { WriteShort(dst, val); return; }
+ WriteByte(dst, val);
+ }
+
// this will use the value:
// 128
// accuracy near zero is APPROXPASTTIME_MAX/(256*255)