From dc15ceffa2a1569d7d938759f18c1a6988e159b3 Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Mon, 6 Jan 2020 22:01:30 +0100 Subject: [PATCH] Optimized equip item networking --- .../engine/core/client/cl_character_net.lua | 25 ++++++++++++++----- gamemode/engine/derma/menus/menu_charinfo.lua | 19 +++++++++----- gamemode/engine/lib/server/sv_inventory.lua | 7 +++--- gamemode/engine/lib/server/sv_networking.lua | 19 +++++++++++--- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/gamemode/engine/core/client/cl_character_net.lua b/gamemode/engine/core/client/cl_character_net.lua index be91ff6..06f4157 100644 --- a/gamemode/engine/core/client/cl_character_net.lua +++ b/gamemode/engine/core/client/cl_character_net.lua @@ -10,6 +10,7 @@ Quantum.Client.InventoryNet = {} Quantum.Inventory.Size = Quantum.Inventory.Width * Quantum.Inventory.Height function Quantum.Client.InventoryNet.SetItem( index, itemid, amount ) + Quantum.Debug( "Running SetItem" ) if( Quantum.Client.Inventory == nil ) then Quantum.Client.Inventory = {} end if( amount >= 1 ) then @@ -19,19 +20,32 @@ function Quantum.Client.InventoryNet.SetItem( index, itemid, amount ) end end +function Quantum.Client.InventoryNet.SetEquipItem( itemindex, itemid, equipslot ) + Quantum.Debug( "Running SetEquipItem" ) + print( "Slot: ".. equipslot, "Itemindex: " .. itemindex ) + if( Quantum.Client.Equipped == nil ) then Quantum.Client.Equipped = {} end + + if( itemindex > 0 ) then + Quantum.Client.Equipped[equipslot] = itemindex + else + Quantum.Client.Equipped[equipslot] = nil -- remove it if unequipp + end +end + local intcodeFunctions = { - [Quantum.IntCode.SET_ITEM] = Quantum.Client.InventoryNet.SetItem + [Quantum.IntCode.SET_ITEM] = Quantum.Client.InventoryNet.SetItem, + [Quantum.IntCode.EQUIP_ITEM] = Quantum.Client.InventoryNet.SetEquipItem } net.Receive( "quantum_item_action", function( len, pl ) -- used for updating the players inventory on the client local intcode = net.ReadInt( Quantum.IntCode.BIT_SIZE ) -- Parameters - local index = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) - local itemid = net.ReadString() - local amount = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) + local par1 = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) + local par2 = net.ReadString() + local par3 = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) - intcodeFunctions[intcode]( index, itemid, amount ) + intcodeFunctions[intcode]( par1, par2, par3 ) end) net.Receive( "quantum_char_update", function( len, pl ) @@ -92,7 +106,6 @@ function Quantum.Client.InventoryNet.EquipItem( index ) if( itemTbl.equipslot != nil ) then net.Start( "quantum_item_action" ) - print("####", Quantum.IntCode.EQUIP_ITEM) Quantum.WriteIntcode( Quantum.IntCode.EQUIP_ITEM ) net.WriteInt( index, Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) net.SendToServer() diff --git a/gamemode/engine/derma/menus/menu_charinfo.lua b/gamemode/engine/derma/menus/menu_charinfo.lua index 8d1f5cb..d47e7a9 100644 --- a/gamemode/engine/derma/menus/menu_charinfo.lua +++ b/gamemode/engine/derma/menus/menu_charinfo.lua @@ -244,30 +244,37 @@ function menu.open( dt ) local equipSlotSpacing = padding*10 local equipSlot_PanelWidth, equipSlot_PanelHeight = itemWidth * slotScale, itemHeight * slotScale + print("ITEMS IN SLOT#########################") -- HEAD f.equippanels[Quantum.EquipSlots.Head] = createEquipSlotPanel( Quantum.EquipSlots.Head, slotXpos, equipSlotYpos, slotScale, f ) -- create the panel + print( "Helmet:", getItemInSlot(equipped[ Quantum.EquipSlots.Head ]) ) f.equippanels[Quantum.EquipSlots.Head].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Head ]) ) -- give its current item equipSlotYpos = equipSlotYpos + equipSlot_PanelHeight + equipSlotSpacing -- CHEST - f.equippanels[Quantum.EquipSlots.Chest] = createEquipSlotPanel( Quantum.EquipSlots.Chest, slotXpos, equipSlotYpos, slotScale, f ) -- create the panel - f.equippanels[Quantum.EquipSlots.Chest].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Chest ]) ) -- give its current item + f.equippanels[Quantum.EquipSlots.Chest] = createEquipSlotPanel( Quantum.EquipSlots.Chest, slotXpos, equipSlotYpos, slotScale, f ) + print( "Chest:", getItemInSlot(equipped[ Quantum.EquipSlots.Chest ]) ) + f.equippanels[Quantum.EquipSlots.Chest].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Chest ]) ) equipSlotYpos = equipSlotYpos + equipSlot_PanelHeight + equipSlotSpacing -- LEGS - f.equippanels[Quantum.EquipSlots.Legs] = createEquipSlotPanel( Quantum.EquipSlots.Legs, slotXpos, equipSlotYpos, slotScale, f ) -- create the panel - f.equippanels[Quantum.EquipSlots.Legs].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Legs ]) ) -- give its current item + f.equippanels[Quantum.EquipSlots.Legs] = createEquipSlotPanel( Quantum.EquipSlots.Legs, slotXpos, equipSlotYpos, slotScale, f ) + print( "Legs:", getItemInSlot(equipped[ Quantum.EquipSlots.Legs ]) ) + f.equippanels[Quantum.EquipSlots.Legs].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Legs ]) ) equipSlotYpos = equipSlotYpos + equipSlot_PanelHeight + equipSlotSpacing -- BOOTS - f.equippanels[Quantum.EquipSlots.Boots] = createEquipSlotPanel( Quantum.EquipSlots.Boots, slotXpos, equipSlotYpos, slotScale, f ) -- create the panel - f.equippanels[Quantum.EquipSlots.Boots].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Boots ]) ) -- give its current item + f.equippanels[Quantum.EquipSlots.Boots] = createEquipSlotPanel( Quantum.EquipSlots.Boots, slotXpos, equipSlotYpos, slotScale, f ) + print( "Boots:", getItemInSlot(equipped[ Quantum.EquipSlots.Boots ]) ) + f.equippanels[Quantum.EquipSlots.Boots].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Boots ]) ) equipSlotYpos = equipSlotYpos + equipSlot_PanelHeight + equipSlotSpacing -- WEAPON f.equippanels[Quantum.EquipSlots.Weapon] = createEquipSlotPanel( Quantum.EquipSlots.Weapon, slotXpos + equipSlot_PanelWidth + equipSlotSpacing/2, (equipSlotYpos + equipSlot_PanelHeight) / 2, slotScale, f ) -- create the panel + print( "Weapon:", getItemInSlot(equipped[ Quantum.EquipSlots.Weapon ]) ) f.equippanels[Quantum.EquipSlots.Weapon].SetItem( getItemInSlot(equipped[ Quantum.EquipSlots.Weapon ]) ) -- give its current item + print("END ################333") ---- Inventory panel ---- local inv = vgui.Create( "DPanel", f ) -- section for all of the item panels diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index b93d3d8..dfd3892 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -37,7 +37,8 @@ function Quantum.Server.Inventory.EquipItem( pl, itemindex ) Quantum.Error( tostring(pl) .. " tried to equip an non-equippable item: (" .. tostring(itemTbl[1]) .. ")" ) return else - if( table.KeyFromValue( Quantum.EquipSlots, equipslot ) != nil ) then + local equipslotKey = table.KeyFromValue( Quantum.EquipSlots, equipslot ) + if( equipslotKey != nil ) then char.equipped[equipslot] = itemindex -- set it in the table if( itemTbl.equipeffect != nil ) then @@ -45,7 +46,7 @@ function Quantum.Server.Inventory.EquipItem( pl, itemindex ) end Quantum.Debug( tostring(pl) .. " equipped item (" .. tostring(slotitem[1]) .. ") - (" .. tostring(itemindex) .. ")" ) -- NETWORKING -- - Quantum.Net.Inventory.Update( pl ) -- update the client + Quantum.Net.Inventory.SetEquipItem( pl, itemindex, equipslot ) else Quantum.Error( tostring(pl) .. " tried to equip an item in a non-existent equip slot: (" .. tostring(equipslot) .. ")" ) @@ -69,7 +70,7 @@ function Quantum.Server.Inventory.UnEquipItem( pl, equipslot, char ) Quantum.Debug( tostring(pl) .. " unequipped item (" .. tostring( itemTbl.id ) .. ") - (" .. tostring( char.equipped[equipslot] ) .. ")" ) char.equipped[equipslot] = nil - Quantum.Net.Inventory.Update( pl ) -- update the client + Quantum.Net.Inventory.SetEquipItem( pl, -1, equipslot ) end end diff --git a/gamemode/engine/lib/server/sv_networking.lua b/gamemode/engine/lib/server/sv_networking.lua index 9afb663..40f5937 100644 --- a/gamemode/engine/lib/server/sv_networking.lua +++ b/gamemode/engine/lib/server/sv_networking.lua @@ -131,6 +131,17 @@ function Quantum.Net.Inventory.SetItem( pl, index, itemid, amount ) -- sends a i net.Send( pl ) end +function Quantum.Net.Inventory.SetEquipItem( pl, index, equipslot ) + net.Start( "quantum_item_action" ) + + Quantum.WriteIntcode( Quantum.IntCode.EQUIP_ITEM ) -- write the opcode first + net.WriteInt( index, Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) + net.WriteString( "" ) + net.WriteInt( equipslot, Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) + + net.Send( pl ) +end + function Quantum.Net.Inventory.Update( pl ) Quantum.Debug( "Updating " .. tostring(pl) .. " character." ) @@ -165,11 +176,11 @@ local intcodeFunctions = { net.Receive( "quantum_item_action", function( len, pl ) local intcode = net.ReadInt( Quantum.IntCode.BIT_SIZE ) - local index = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) - local itemid = net.ReadString() - local amount = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) + local par1 = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) + local par2 = net.ReadString() + local par3 = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) - intcodeFunctions[intcode]( pl, index, itemid, amount ) + intcodeFunctions[intcode]( pl, par1, par2, par3 ) end)