X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Fcommon%2Fitems%2Finventory.qh;h=9075c0912ed2a5044ba8e7b80b299a05723f31ba;hb=bc3f297ed082b23fb33dd0d8f5dcd33bb0198507;hp=c7f602e653835d6b519843bea740acc2ca0caf10;hpb=51c4ab5336d3887b618c13e5f566cf19636af951;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/common/items/inventory.qh b/qcsrc/common/items/inventory.qh index c7f602e65..9075c0912 100644 --- a/qcsrc/common/items/inventory.qh +++ b/qcsrc/common/items/inventory.qh @@ -1,14 +1,12 @@ -#ifndef INVENTORY_H -#define INVENTORY_H +#pragma once #include "all.qh" -#include "item/pickup.qh" CLASS(Inventory, Object) /** Stores counts of items, the id being the index */ - ATTRIBARRAY(Inventory, inv_items, int, Items_MAX) + ATTRIBARRAY(Inventory, inv_items, int, Items_MAX); /** Previous state */ - ATTRIB(Inventory, inventory, Inventory, NULL) + ATTRIB(Inventory, inventory, Inventory); ENDCLASS(Inventory) /** Player inventory */ @@ -16,17 +14,35 @@ 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) + +#define G_MAJOR(id) (floor((id) / Inventory_groups_minor)) +#define G_MINOR(id) ((id) % Inventory_groups_minor) + #ifdef CSQC +Inventory g_inventory; NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew) { make_pure(this); - const int bits = ReadInt24_t(); - FOREACH(Items, bits & BIT(it.m_id), { - .int fld = inv_items[it.m_id]; - int prev = this.(fld); - int next = this.(fld) = ReadByte(); - LOG_TRACEF("%s: %.0f -> %.0f\n", it.m_name, prev, next); - }); + g_inventory = this; + const int majorBits = ReadShort(); + for (int i = 0; i < Inventory_groups_major; ++i) { + if (!(majorBits & BIT(i))) { + continue; + } + const int minorBits = ReadByte(); + for (int j = 0; j < Inventory_groups_minor; ++j) { + if (!(minorBits & BIT(j))) { + continue; + } + const GameItem it = Items_from(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); + } + } return true; } #endif @@ -35,22 +51,56 @@ NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew) void Inventory_Write(Inventory data) { if (!data) { - WriteInt24_t(MSG_ENTITY, 0); + WriteShort(MSG_ENTITY, 0); return; } TC(Inventory, data); - int bits = 0; + + int majorBits = 0; FOREACH(Items, true, { .int fld = inv_items[it.m_id]; - bits = BITSET(bits, BIT(it.m_id), data.inventory.(fld) != (data.inventory.(fld) = data.(fld))); + const bool changed = data.inventory.(fld) != data.(fld); + if (changed) { + majorBits = BITSET(majorBits, BIT(G_MAJOR(it.m_id)), true); + } }); - WriteInt24_t(MSG_ENTITY, bits); - FOREACH(Items, bits & BIT(it.m_id), { - WriteByte(MSG_ENTITY, data.inv_items[it.m_id]); + 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 } #endif +#undef G_MAJOR +#undef G_MINOR + #ifdef SVQC bool Inventory_Send(Inventory this, Client to, int sf) { @@ -74,5 +124,3 @@ void Inventory_new(entity e) void Inventory_delete(entity e) { delete(e.inventory.inventory); delete(e.inventory); } void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; } #endif - -#endif