Fix item stats networking by refactoring algorithm that sends item stats
authorterencehill <piuntn@gmail.com>
Sun, 1 Sep 2019 08:41:06 +0000 (10:41 +0200)
committerterencehill <piuntn@gmail.com>
Sun, 1 Sep 2019 08:41:06 +0000 (10:41 +0200)
qcsrc/common/items/inventory.qh

index 75abe46..ed6c735 100644 (file)
@@ -50,6 +50,7 @@ NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
 #endif
 
 #ifdef SVQC
+int minorBitsArr[Inventory_groups_major];
 void Inventory_Write(Inventory data)
 {
     if (!data) {
@@ -58,45 +59,37 @@ void Inventory_Write(Inventory data)
     }
     TC(Inventory, data);
 
+       for (int i = 0; i < Inventory_groups_major; ++i)
+               minorBitsArr[i] = 0;
+
     int majorBits = 0;
     FOREACH(Items, true, {
         .int fld = inv_items[it.m_id];
         const bool changed = data.inventory.(fld) != data.(fld);
         if (changed) {
-            majorBits = BITSET(majorBits, BIT(G_MAJOR(it.m_id)), true);
+                       int maj = G_MAJOR(it.m_id);
+                       majorBits = BITSET(majorBits, BIT(maj), true);
+                       minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
         }
     });
     WriteShort(MSG_ENTITY, majorBits);
 
-    int minorBits = 0;
-    int lastMaj = 0;
-    int maj = 0;
-    FOREACH(Items, majorBits & BIT(maj = G_MAJOR(it.m_id)), {
-        .int fld = inv_items[it.m_id];
-        const bool changed = data.inventory.(fld) != (data.inventory.(fld) = data.(fld));
-        if (changed) {
-            if (maj != lastMaj) {
-                lastMaj = maj;
-#define X() MACRO_BEGIN \
-    if (minorBits) { \
-        WriteByte(MSG_ENTITY, minorBits); \
-        for (int j = 0; j < Inventory_groups_minor; ++j) { \
-            if (!(minorBits & BIT(j))) { \
-                continue; \
-            } \
-            const entity it = Items_from(Inventory_groups_minor * maj + j); \
-            WriteByte(MSG_ENTITY, data.inv_items[it.m_id]); \
-        } \
-    } \
-MACRO_END
-                X();
-                minorBits = 0;
-            }
-            minorBits = BITSET(minorBits, BIT(G_MINOR(it.m_id)), true);
-        }
-    });
-    X();
-#undef X
+       for (int i = 0; i < Inventory_groups_major; ++i)
+       {
+               if (!(majorBits & BIT(i)))
+                       continue;
+
+               const int minorBits = minorBitsArr[i];
+               WriteByte(MSG_ENTITY, minorBits);
+               for (int j = 0; j < Inventory_groups_minor; ++j)
+               {
+                       if (!(minorBits & BIT(j)))
+                               continue;
+
+                       const entity it = Items_from(Inventory_groups_minor * i + j);
+                       WriteByte(MSG_ENTITY, data.inv_items[it.m_id]);
+               }
+       }
 }
 #endif