X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Fcommon%2Fitems%2Finventory.qh;h=22a4d94c387335f833d0867f2acb1df01e776663;hb=acc08b9655874164549bc2600f78c6edbc3c084a;hp=017ada75c0288df84ce093b80dffefa8316a810c;hpb=629f6f3b55e891281bb265f8022d129d313c110f;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/common/items/inventory.qh b/qcsrc/common/items/inventory.qh index 017ada75c0..22a4d94c38 100644 --- a/qcsrc/common/items/inventory.qh +++ b/qcsrc/common/items/inventory.qh @@ -5,44 +5,65 @@ #ifdef GAMEQC CLASS(Inventory, Object) /** Stores counts of items, the id being the index */ - ATTRIBARRAY(Inventory, inv_items, int, Items_MAX); - /** Previous state */ - ATTRIB(Inventory, inventory, Inventory); + ATTRIBARRAY(Inventory, inv_items, int, REGISTRY_MAX(Items)); ENDCLASS(Inventory) /** Player inventory */ .Inventory inventory; +/** Player inventory storage (holds previous state) */ +.Inventory inventory_store; REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY) -const int Inventory_groups_minor = 8; // exactly 1 byte -const int Inventory_groups_major = 3; // ceil(Items_MAX / 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 #ifdef CSQC Inventory g_inventory; +void Inventory_remove(entity this) +{ + if(g_inventory == this) + g_inventory = NULL; +} + NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew) { make_pure(this); g_inventory = this; - const int majorBits = ReadByte(); + this.entremove = Inventory_remove; + 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; @@ -51,7 +72,7 @@ NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew) #ifdef SVQC int minorBitsArr[Inventory_groups_major]; -void Inventory_Write(Inventory data) +void Inventory_Write(Inventory data, Inventory store) { if (!data) { WriteShort(MSG_ENTITY, 0); @@ -65,28 +86,29 @@ void Inventory_Write(Inventory data) int majorBits = 0; FOREACH(Items, true, { .int fld = inv_items[it.m_id]; - const bool changed = data.inventory.(fld) != data.(fld); + const bool changed = store.(fld) != data.(fld); + store.(fld) = data.(fld); if (changed) { 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); } }); - 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))) continue; - const entity it = Items_from(Inventory_groups_minor * i + j); + const entity it = REGISTRY_GET(Items, Inventory_groups_minor * i + j); WriteByte(MSG_ENTITY, data.inv_items[it.m_id]); } } @@ -102,7 +124,7 @@ bool Inventory_Send(Inventory this, Client to, int sf) TC(Inventory, this); WriteHeader(MSG_ENTITY, ENT_CLIENT_INVENTORY); TC(PlayerState, this.owner); - Inventory_Write(this); + Inventory_Write(this, to.inventory_store); return true; } @@ -114,11 +136,25 @@ bool Inventory_customize(entity this, entity client) void Inventory_new(PlayerState this) { - Inventory inv = NEW(Inventory), bak = NEW(Inventory); - inv.inventory = bak; + Inventory inv = NEW(Inventory); setcefc(inv, Inventory_customize); - Net_LinkEntity((inv.owner = this).inventory = inv, false, 0, Inventory_Send); + this.inventory = inv; + inv.owner = this; + Net_LinkEntity(inv, false, 0, Inventory_Send); } -void Inventory_delete(entity e) { delete(e.inventory.inventory); delete(e.inventory); } +void Inventory_delete(entity e) { delete(e.inventory); } void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; } + +void Inventory_clear(entity store) +{ + // NOTE: you will need to perform Inventory_update after this to update the storage entity + // (unless store is the storage entity) + FOREACH(Items, true, { + .int fld = inv_items[it.m_id]; + store.(fld) = 0; + }); +} + +void InventoryStorage_attach(entity e) { e.inventory_store = NEW(Inventory); e.inventory_store.drawonlytoclient = e; } +void InventoryStorage_delete(entity e) { delete(e.inventory_store); } #endif