]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Fix inventory system networking so that it stores the inventory state on the client...
authorMario <mario.mario@y7mail.com>
Sun, 18 Oct 2020 23:46:39 +0000 (09:46 +1000)
committerMario <mario.mario@y7mail.com>
Sun, 18 Oct 2020 23:46:39 +0000 (09:46 +1000)
qcsrc/common/items/inventory.qh
qcsrc/common/state.qc

index ca53f718d1ae2462f780dfd262f1d26b347e2989..c47be669978bb90b00afa5724297fc7d21c48544 100644 (file)
@@ -6,12 +6,12 @@
 CLASS(Inventory, Object)
     /** Stores counts of items, the id being the index */
     ATTRIBARRAY(Inventory, inv_items, int, REGISTRY_MAX(Items));
-    /** Previous state */
-    ATTRIB(Inventory, inventory, Inventory);
 ENDCLASS(Inventory)
 
 /** Player inventory */
 .Inventory inventory;
+/** Player inventory storage (holds previous state) */
+.Inventory inventory_store;
 
 REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY)
 
@@ -65,7 +65,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);
@@ -79,7 +79,8 @@ 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);
@@ -116,7 +117,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;
 }
 
@@ -128,11 +129,13 @@ 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);
 }
 void Inventory_delete(entity e) { delete(e.inventory.inventory); delete(e.inventory); }
 void Inventory_update(entity e) { e.inventory.SendFlags = 0xFFFFFF; }
+
+void InventoryStorage_attach(entity e) { e.inventory_store = NEW(Inventory); e.inventory_store.drawonlytoclient = e; }
+void InventoryStorage_detach(entity e) { delete(e.inventory_store); }
 #endif
index f117340ed948971560eaaffc383c2e7b27d57a92..7a5979e2bc2c0ba261c11281798bf1d2355fdc1a 100644 (file)
@@ -4,6 +4,8 @@
 
 void Inventory_new(PlayerState this);
 void Inventory_delete(entity this);
+void InventoryStorage_attach(PlayerState this);
+void InventoryStorage_detach(PlayerState this);
 
 void PlayerState_attach(entity this)
 {
@@ -53,6 +55,7 @@ void ClientState_attach(entity this)
        entcs_attach(this);
        anticheat_init(this);
        W_HitPlotOpen(this);
+       InventoryStorage_attach(this);
 }
 
 void bot_clientdisconnect(entity this);
@@ -71,6 +74,7 @@ void ClientState_detach(entity this)
     W_HitPlotClose(this);
     ClientData_Detach(this);
     entcs_detach(this);
+    InventoryStorage_detach(this);
        delete(CS(this));
        this._cs = NULL;