Added item dropping & bug fixes

master
AlmTech Software 5 years ago
parent e119203ab9
commit 5e92215da7
  1. 1
      entities/entities/q_item/init.lua
  2. 8
      gamemode/engine/core/client/cl_hud.lua
  3. 24
      gamemode/engine/core/client/cl_inventory_net.lua
  4. 3
      gamemode/engine/core/sh_player_binds.lua
  5. 3
      gamemode/engine/derma/lib/cl_menu_iteminfo.lua
  6. 2
      gamemode/engine/derma/menus/menu_character.lua
  7. 2
      gamemode/engine/derma/menus/menu_charinfo.lua
  8. 3
      gamemode/engine/lib/server/sv_character.lua
  9. 23
      gamemode/engine/lib/server/sv_inventory.lua
  10. 37
      gamemode/engine/lib/server/sv_networking.lua
  11. 8
      gamemode/engine/sh_debug.lua
  12. 5
      gamemode/shared.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

@ -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

@ -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)
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

@ -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

@ -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

@ -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

@ -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 )

@ -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

@ -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 )
@ -137,6 +144,8 @@ local function sortItem( pl, char, itemid, amount )
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
break
@ -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()

@ -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
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)

@ -23,3 +23,11 @@ function Quantum.Error( txt )
return
end
end
function Quantum.Warn( txt )
if( txt ) then
MsgC( Color( 255, 10, 10 ), prefix .. "[WARNING] " .. txt .. "\n" )
else
return
end
end

@ -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
Loading…
Cancel
Save