X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Fcommon%2Fitems%2Finventory.qh;h=ca53f718d1ae2462f780dfd262f1d26b347e2989;hb=0a80807da0987ca2d3922a0534c5673bf3a7c928;hp=2ec0cdb7b1cb367ede1ea9903235df2461580bd7;hpb=8639f042c5ef61287b2f7e0dbf17f8c08a2c0ebf;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/common/items/inventory.qh b/qcsrc/common/items/inventory.qh index 2ec0cdb7b..ca53f718d 100644 --- a/qcsrc/common/items/inventory.qh +++ b/qcsrc/common/items/inventory.qh @@ -2,9 +2,10 @@ #include "all.qh" +#ifdef GAMEQC CLASS(Inventory, Object) /** Stores counts of items, the id being the index */ - ATTRIBARRAY(Inventory, inv_items, int, Items_MAX); + ATTRIBARRAY(Inventory, inv_items, int, REGISTRY_MAX(Items)); /** Previous state */ ATTRIB(Inventory, inventory, Inventory); ENDCLASS(Inventory) @@ -14,11 +15,26 @@ ENDCLASS(Inventory) REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY) -const int Inventory_groups_major = 16; -const int Inventory_groups_minor = 8; // ceil(Items_MAX / Inventory_groups_major) +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 #ifdef CSQC Inventory g_inventory; @@ -26,21 +42,21 @@ NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew) { make_pure(this); g_inventory = this; - const int majorBits = ReadShort(); + 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; } - const GameItem it = Items_from(Inventory_groups_minor * i + j); + const GameItem it = REGISTRY_GET(Items, Inventory_groups_minor * i + j); .int fld = inv_items[it.m_id]; int prev = this.(fld); int next = this.(fld) = ReadByte(); - LOG_TRACEF("%s: %.0f -> %.0f", it.m_name, prev, next); + LOG_DEBUGF("%s: %.0f -> %.0f", it.m_name, prev, next); } } return true; @@ -48,6 +64,7 @@ NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew) #endif #ifdef SVQC +int minorBitsArr[Inventory_groups_major]; void Inventory_Write(Inventory data) { if (!data) { @@ -56,45 +73,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 + 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]; + Writebits(MSG_ENTITY, minorBits, Inventory_groups_minor); + for (int j = 0; j < Inventory_groups_minor; ++j) + { + if (!(minorBits & BIT(j))) + continue; + + const entity it = REGISTRY_GET(Items, Inventory_groups_minor * i + j); + WriteByte(MSG_ENTITY, data.inv_items[it.m_id]); + } + } } #endif