From 5e92215da738493bf14bdc2aaaa510e95fcd4317 Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Thu, 2 Jan 2020 21:29:38 +0100 Subject: [PATCH] Added item dropping & bug fixes --- entities/entities/q_item/init.lua | 1 + gamemode/engine/core/client/cl_hud.lua | 8 +++- .../engine/core/client/cl_inventory_net.lua | 26 ++++++++++--- gamemode/engine/core/sh_player_binds.lua | 3 +- .../engine/derma/lib/cl_menu_iteminfo.lua | 3 +- .../engine/derma/menus/menu_character.lua | 2 +- gamemode/engine/derma/menus/menu_charinfo.lua | 2 + gamemode/engine/lib/server/sv_character.lua | 3 +- gamemode/engine/lib/server/sv_inventory.lua | 23 +++++++---- gamemode/engine/lib/server/sv_networking.lua | 39 +++++++++++++++---- gamemode/engine/sh_debug.lua | 8 ++++ gamemode/shared.lua | 5 +++ 12 files changed, 94 insertions(+), 29 deletions(-) diff --git a/entities/entities/q_item/init.lua b/entities/entities/q_item/init.lua index add195e..5e6298d 100644 --- a/entities/entities/q_item/init.lua +++ b/entities/entities/q_item/init.lua @@ -16,6 +16,7 @@ function ENT:Initialize() self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) + self:SetCollisionGroup( COLLISION_GROUP_WEAPON ) local phys = self:GetPhysicsObject() if( phys:IsValid() ) then phys:Wake() end diff --git a/gamemode/engine/core/client/cl_hud.lua b/gamemode/engine/core/client/cl_hud.lua index e47e9e9..306d4b0 100644 --- a/gamemode/engine/core/client/cl_hud.lua +++ b/gamemode/engine/core/client/cl_hud.lua @@ -59,8 +59,12 @@ end function GM:Think() if( Quantum.Client.IsInMenu ) then - if( gui.IsGameUIVisible() ) then -- hides the main menu for the player - gui.HideGameUI() + if( gui.IsGameUIVisible() ) then gui.HideGameUI() end -- hides the main menu for the player + + if( !LocalPlayer():Alive() ) then + if( IsValid( Quantum.Client.CurMenu ) ) then + Quantum.Client.CurMenu:Close() -- closes the current open menu on death + end end end end diff --git a/gamemode/engine/core/client/cl_inventory_net.lua b/gamemode/engine/core/client/cl_inventory_net.lua index ce3dc14..6f97ec2 100644 --- a/gamemode/engine/core/client/cl_inventory_net.lua +++ b/gamemode/engine/core/client/cl_inventory_net.lua @@ -7,8 +7,6 @@ Quantum.Client.InventoryNet = {} -local function calculateNeededBits( n ) return math.ceil( math.log( n, 2 ) ) end - Quantum.Inventory.Size = Quantum.Inventory.Width * Quantum.Inventory.Height function Quantum.Client.InventoryNet.SetItem( index, itemid, amount ) @@ -25,13 +23,29 @@ local intcodeFunctions = { [Quantum.IntCode.SET_ITEM] = Quantum.Client.InventoryNet.SetItem } -net.Receive( "quantum_item_action", function( len, pl ) +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( calculateNeededBits( Quantum.Inventory.Size ) ) + local index = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) local itemid = net.ReadString() - local amount = net.ReadInt( calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) + local amount = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) intcodeFunctions[intcode]( index, itemid, amount ) -end) \ No newline at end of file +end) + +net.Receive( "quantum_item_update", function( len, pl ) + local dtInv = net.ReadTable() + Quantum.Client.Inventory = dtInv || {} + Quantum.Debug( "Updated inventory." ) +end) + +function Quantum.Client.InventoryNet.DropItem( itemid, index, amount ) + if( !index ) then Quantum.Error( "Error: index=nil" ) return end + net.Start( "quantum_item_action" ) + Quantum.WriteIntcode( Quantum.IntCode.DROP_ITEM ) + net.WriteInt( index, Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) + net.WriteString( itemid ) + net.WriteInt( amount, Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) + net.SendToServer() +end diff --git a/gamemode/engine/core/sh_player_binds.lua b/gamemode/engine/core/sh_player_binds.lua index e168ca4..eb430db 100644 --- a/gamemode/engine/core/sh_player_binds.lua +++ b/gamemode/engine/core/sh_player_binds.lua @@ -33,7 +33,6 @@ if SERVER then if( keyfuncs[key] ) then keyfuncs[key]( ply ) end end function GM:ShowHelp( ply ) keyfuncs["mainMenu"]( ply ) end - function GM:ShowSpare1( ply ) keyfuncs["charinfo_DYNAMIC"]( ply ) end - function GM:ShowSpare2( ply ) keyfuncs["charinfo"]( ply ) end + function GM:ShowTeam( ply ) keyfuncs["charinfo_DYNAMIC"]( ply ) end end \ No newline at end of file diff --git a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua index 32c20b9..cbaa4f3 100644 --- a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua +++ b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua @@ -82,7 +82,7 @@ function iteminfo.dropamount( p, page, itemPanel ) itemPanel.SetItemAmount( amountInSlot - dropAmount ) end - ---- DROP NET HERE ---- + Quantum.Client.InventoryNet.DropItem( item.id, index, dropAmount ) end @@ -226,6 +226,7 @@ function iteminfo.giveoptions( p, page ) else p:GetParent().RemoveItem() ---- DROP NET HERE ---- + Quantum.Client.InventoryNet.DropItem( item.id, index, amount ) end end diff --git a/gamemode/engine/derma/menus/menu_character.lua b/gamemode/engine/derma/menus/menu_character.lua index 05bf92c..1d15d50 100644 --- a/gamemode/engine/derma/menus/menu_character.lua +++ b/gamemode/engine/derma/menus/menu_character.lua @@ -344,7 +344,7 @@ local pages = { snm.RunNetworkedFunc( "createChar", inputs ) ---- save it on the client - inputs.model = Quantum.Classes[inputs.class].Models[inputs.gender][inputs.modelIndex] + inputs.model = Quantum.Classes[inputs.class].Models[inputs.gender][inputs.modelIndex] || Quantum.Classes[inputs.class].Models[inputs.gender][1] Quantum.Client.Chars[#Quantum.Client.Chars + 1] = inputs diff --git a/gamemode/engine/derma/menus/menu_charinfo.lua b/gamemode/engine/derma/menus/menu_charinfo.lua index 2635aa7..91845fc 100644 --- a/gamemode/engine/derma/menus/menu_charinfo.lua +++ b/gamemode/engine/derma/menus/menu_charinfo.lua @@ -59,6 +59,8 @@ function menu.open( dt ) Quantum.Client.Cam.Stop() end + Quantum.Client.CurMenu = f + -- Default is the inventory page -- local bar = vgui.Create( "DPanel", f ) diff --git a/gamemode/engine/lib/server/sv_character.lua b/gamemode/engine/lib/server/sv_character.lua index c6dbfee..9b0b8fd 100644 --- a/gamemode/engine/lib/server/sv_character.lua +++ b/gamemode/engine/lib/server/sv_character.lua @@ -14,7 +14,7 @@ local function CreateCharTable( args ) class = Quantum.Classes[args.class] || Quantum.Classes[1], maxhealth = Quantum.Server.Settings.MaxHealth, health = args.health || Quantum.Server.Settings.MaxHealth, - model = Quantum.Classes[args.class].Models[args.gender][args.modelIndex] || "models/player.mdl", + model = Quantum.Classes[args.class].Models[args.gender][args.modelIndex] || Quantum.Classes[args.class].Models[args.gender][1] || "models/player.mdl", money = args.money || Quantum.Server.Settings.StarterMoney, inventory = args.inventory || {}, -- create new inventory later jobs = args.jobs || { @@ -72,6 +72,7 @@ function Quantum.Server.Char.SetCurrentCharacter( pl, index ) if( Quantum.Server.Char.Players[ id ] ) then pl.character = Quantum.Server.Char.Players[ id ] pl.charindex = index + Quantum.Net.Inventory.Update( pl ) -- update the players inventory on char change setupCharacter( pl, pl.character ) return pl.character else diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index 25a4c1f..e6b1638 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -79,6 +79,8 @@ end local function sortItem( pl, char, itemid, amount ) + Quantum.Debug( "--Stacking Debug--" ) + local item = Quantum.Item.Get( itemid ) local slotitem = Quantum.Server.Inventory.GetSlotItem( char, index ) local inv = Quantum.Server.Char.GetInventory( char ) @@ -106,12 +108,16 @@ local function sortItem( pl, char, itemid, amount ) local setAmt = math.Clamp( add, 1, stacksize ) Quantum.Server.Inventory.SetSlotItem( pl, char, index, itemid, setAmt ) + print( "1", itemid, setAmt, rest, index ) + end else local setAmt = math.Clamp( amount, 1, stacksize ) local pos = Quantum.Server.Inventory.FindItemSpot( char ) rest = rest - setAmt Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, setAmt ) + + print( "2", itemid, setAmt, rest, pos ) end while( rest >= stacksize ) do @@ -128,6 +134,7 @@ local function sortItem( pl, char, itemid, amount ) local pos = Quantum.Server.Inventory.FindItemSpot( char ) Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, setAmt ) + print( "3", itemid, setAmt, rest, pos ) else index = index + 1 itemInSlot = Quantum.Server.Inventory.GetSlotItem( char, index ) @@ -136,6 +143,8 @@ local function sortItem( pl, char, itemid, amount ) if( itemInSlot[1] == itemid && itemInSlot[2] < stacksize ) then rest = rest - ( stacksize - itemInSlot[2] ) Quantum.Server.Inventory.SetSlotItem( pl, char, index, itemid, stacksize ) + + print( "4", itemid, stacksize, rest, index ) if( rest <= 0 ) then rest = 0 @@ -145,15 +154,18 @@ local function sortItem( pl, char, itemid, amount ) else rest = rest - stacksize Quantum.Server.Inventory.SetSlotItem( pl, char, index, itemid, stacksize ) + print( "5", itemid, stacksize, rest, index ) end end end local stackIndex = Quantum.Server.Inventory.FindStackable( char, item ) + print( "stackIndex=", stackIndex ) local pos if( stackIndex == nil ) then pos = Quantum.Server.Inventory.FindItemSpot( char ) Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, rest ) + print( "6", itemid, rest, rest, pos ) else if( rest > 0 ) then pos = stackIndex @@ -165,9 +177,11 @@ local function sortItem( pl, char, itemid, amount ) if( rest <= 0 ) then Quantum.Server.Inventory.SetSlotItem( pl, char, pos, itemid, setAmt ) + print( "7", itemid, setAmt, rest, pos ) end end end + Quantum.Debug( "--End of Stacking Debug--" ) end function Quantum.Server.Inventory.GiveItem( pl, itemid, amount ) -- Quantum.Server.Inventory.GiveItem( Entity(1), "test2", 21 ) @@ -178,14 +192,7 @@ function Quantum.Server.Inventory.GiveItem( pl, itemid, amount ) -- Quantum.Serv if( item == nil ) then Quantum.Error( "Tried to give " .. tostring(pl) .. " a non-existent item! Item '" .. tostring(itemid) .. "' does not exist." ) return end if( #inv + 1 <= Quantum.Inventory.Size || Quantum.Server.Inventory.FindStackable( char, item ) != nil ) then - sortItem( pl, char, itemid, amount ) - -- Quantum.Debug( "Gave " .. char.name .. " " .. amount .. "x [" .. item.name .. "]" ) - -- Send net message to client about item update - -- ############################################ - -- ############################################ - -- ############################################ - -- ############################################ else Quantum.Debug( "Tried to give " .. tostring(pl) .. " a item but their inventory is full!" ) end @@ -215,7 +222,7 @@ function Quantum.Server.Inventory.DropItem( pl, index, amount ) -- Quantum.Serve local itemEnt = ents.Create( "q_item" ) if( IsValid( itemEnt ) ) then itemEnt:SetModel( item.model ) - itemEnt:SetPos( pl:GetPos() ) + itemEnt:SetPos( pl:GetPos() + pl:GetForward() ) itemEnt.amount = amount itemEnt.itemid = itemid itemEnt:Spawn() diff --git a/gamemode/engine/lib/server/sv_networking.lua b/gamemode/engine/lib/server/sv_networking.lua index b3d9146..ae49662 100644 --- a/gamemode/engine/lib/server/sv_networking.lua +++ b/gamemode/engine/lib/server/sv_networking.lua @@ -9,6 +9,7 @@ Quantum.Net = {} util.AddNetworkString( "quantum_menu_net" ) util.AddNetworkString( "quantum_menu_button_net" ) util.AddNetworkString( "quantum_item_action" ) +util.AddNetworkString( "quantum_item_update" ) local function checkCacheTable( ply, cache_id, dt ) Quantum.Debug( "Checking cache tables (" .. tostring(ply) .. " | " .. tostring(cache_id) .. " | " .. tostring(dt) .. ")" ) @@ -119,17 +120,39 @@ end) Quantum.Net.Inventory = {} -local function calculateNeededBits( n ) return math.ceil( math.log( n, 2 ) ) end - -local function WriteIntcode( intcode ) net.WriteInt( intcode, Quantum.IntCode.BIT_SIZE ) end - function Quantum.Net.Inventory.SetItem( pl, index, itemid, amount ) -- sends a item to the client with amount of it, this is a DYNAMIC networking solution net.Start( "quantum_item_action" ) - WriteIntcode( Quantum.IntCode.SET_ITEM ) -- write the opcode first - net.WriteInt( index, calculateNeededBits( Quantum.Inventory.Size ) ) + Quantum.WriteIntcode( Quantum.IntCode.SET_ITEM ) -- write the opcode first + net.WriteInt( index, Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) net.WriteString( itemid ) - net.WriteInt( amount, calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) + net.WriteInt( amount, Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) + + net.Send( pl ) +end +function Quantum.Net.Inventory.Update( pl ) + Quantum.Debug( "Updating " .. tostring(pl) .. " inventory." ) + net.Start( "quantum_item_update" ) + net.WriteTable( Quantum.Server.Char.GetInventory( Quantum.Server.Char.GetCurrentCharacter( pl ) ) ) net.Send( pl ) -end \ No newline at end of file +end + +local intcodeFunctions = { + [Quantum.IntCode.SET_ITEM] = function( pl, index, itemid, amount ) -- if the client is trying to set an item then kick the player + Quantum.Warn( "Player [" .. pl:Nick() .. " | " .. pl:SteamID() .. "] tried to use a blacklisted Intcode function called from the client!" ) + pl:Kick( "[Quantum Security] Tried to use a invalid Intcode function. Nice try." ) + end, + [Quantum.IntCode.DROP_ITEM] = function( pl, index, itemid, amount ) + Quantum.Server.Inventory.DropItem( pl, index, amount ) + end +} + +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 ) ) + + intcodeFunctions[intcode]( pl, index, itemid, amount ) +end) \ No newline at end of file diff --git a/gamemode/engine/sh_debug.lua b/gamemode/engine/sh_debug.lua index b9bf0d8..cb5d0e3 100644 --- a/gamemode/engine/sh_debug.lua +++ b/gamemode/engine/sh_debug.lua @@ -22,4 +22,12 @@ function Quantum.Error( txt ) else return end +end + +function Quantum.Warn( txt ) + if( txt ) then + MsgC( Color( 255, 10, 10 ), prefix .. "[WARNING] " .. txt .. "\n" ) + else + return + end end \ No newline at end of file diff --git a/gamemode/shared.lua b/gamemode/shared.lua index 8da42ad..ad33950 100644 --- a/gamemode/shared.lua +++ b/gamemode/shared.lua @@ -27,5 +27,10 @@ Quantum.IntCode = { EAT_ITEM = 3, EQUIP_ITEM = 4, DESTROY_ITEM = 5, + UPDATE = 6, BIT_SIZE = 3 } + +function Quantum.calculateNeededBits( n ) return math.ceil( math.log( n, 2 ) ) end + +function Quantum.WriteIntcode( intcode ) net.WriteInt( intcode, Quantum.IntCode.BIT_SIZE ) end \ No newline at end of file