From 1678ac659724a4112cdfb1084b51851c1fa3b672 Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Wed, 1 Jan 2020 03:06:13 +0100 Subject: [PATCH] Added notification lib & item drop (server side) --- entities/entities/q_item/cl_init.lua | 12 +++++ entities/entities/q_item/init.lua | 35 +++++++++++++ entities/entities/q_item/shared.lua | 15 ++++++ gamemode/engine/lib/server/sv_inventory.lua | 57 +++++++++++++++++++-- gamemode/engine/lib/server/sv_notify.lua | 35 +++++++++++++ gamemode/settings/sh_items.lua | 2 +- gamemode/settings/sv_settings.lua | 4 ++ 7 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 entities/entities/q_item/cl_init.lua create mode 100644 entities/entities/q_item/init.lua create mode 100644 entities/entities/q_item/shared.lua create mode 100644 gamemode/engine/lib/server/sv_notify.lua diff --git a/entities/entities/q_item/cl_init.lua b/entities/entities/q_item/cl_init.lua new file mode 100644 index 0000000..d615b1b --- /dev/null +++ b/entities/entities/q_item/cl_init.lua @@ -0,0 +1,12 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +include( "shared.lua" ) + +function ENT:Draw() + self:DrawModel() +end \ No newline at end of file diff --git a/entities/entities/q_item/init.lua b/entities/entities/q_item/init.lua new file mode 100644 index 0000000..add195e --- /dev/null +++ b/entities/entities/q_item/init.lua @@ -0,0 +1,35 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +AddCSLuaFile( "cl_init.lua" ) +AddCSLuaFile( "shared.lua" ) + +include( "shared.lua" ) + +function ENT:Initialize() + + self:SetModel( "models/props_phx/gears/bevel12.mdl" ) + self:PhysicsInit( SOLID_VPHYSICS ) + self:SetMoveType( MOVETYPE_VPHYSICS ) + self:SetSolid( SOLID_VPHYSICS ) + + local phys = self:GetPhysicsObject() + if( phys:IsValid() ) then phys:Wake() end + +end + +function ENT:Use( activator, caller ) + if( activator:IsPlayer() ) then + if( self.itemid != nil && self.amount != nil ) then + self:Remove() + -- add item to players inventory + Quantum.Server.Inventory.GiveItem( activator, self.itemid, self.amount ) + Quantum.Notify.ItemPickup( activator, Quantum.Item.Get( self.itemid ), self.amount ) -- notify the player + self:EmitSound( Quantum.Server.Settings.ItemPickupSound ) -- make a pickup sound + end + end +end diff --git a/entities/entities/q_item/shared.lua b/entities/entities/q_item/shared.lua new file mode 100644 index 0000000..0ed8dda --- /dev/null +++ b/entities/entities/q_item/shared.lua @@ -0,0 +1,15 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +ENT.Type = "anim" +ENT.Base = "base_entity" + +ENT.PrintName = "Quantum Item" +ENT.Author = "AlmTech" +ENT.Contact = "elias@almtech.se" +ENT.Spawnable = false +ENT.AdminSpawnable = false \ No newline at end of file diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index efa8d24..60a92e0 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -22,7 +22,10 @@ local function isStackable( item ) end function Quantum.Server.Inventory.SetSlotItem( char, pos, itemid, amount ) - if( amount < 1 ) then return end + if( amount < 1 ) then + char.inventory[pos] = nil -- remove the item + return + end local item = Quantum.Item.Get( itemid ) if( isEquippable( item ) || !isStackable( item ) ) then amount = 1 @@ -31,7 +34,6 @@ function Quantum.Server.Inventory.SetSlotItem( char, pos, itemid, amount ) amount = amount || 1 char.inventory[pos] = { itemid, amount } end - Quantum.Debug( "Gave " .. char.name .. " " .. amount .. "x [" .. item.name .. "] at " .. tostring(pos) ) end function Quantum.Server.Inventory.GetSlotItem( char, pos ) return char.inventory[pos] end @@ -112,7 +114,7 @@ local function sortItem( char, itemid, amount ) end function Quantum.Server.Inventory.GiveItem( pl, itemid, amount ) -- Quantum.Server.Inventory.GiveItem( Entity(1), "test2", 21 ) - local char = Quantum.Server.Char.GetCurrentCharacter( pl ) + local char = Quantum.Server.Char.GetCurrentCharacter( pl ) -- Quantum.Server.Inventory.GiveItem( Entity(1), "test", 1 ) local inv = Quantum.Server.Char.GetInventory( char ) local item = Quantum.Item.Get( itemid ) @@ -121,8 +123,55 @@ function Quantum.Server.Inventory.GiveItem( pl, itemid, amount ) -- Quantum.Serv if( #inv + 1 <= Quantum.Inventory.Width * Quantum.Inventory.Height || Quantum.Server.Inventory.FindStackable( char, item ) != nil ) then sortItem( char, itemid, amount ) - -- send net message to client about item update + 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 +end + +function Quantum.Server.Inventory.DropItem( pl, index, amount ) -- Quantum.Server.Inventory.DropItem( Entity(1), 1, 9 ) + local char = Quantum.Server.Char.GetCurrentCharacter( pl ) -- Quantum.Server.Inventory.DropItem( Entity(1), 4, 1 ) + local inv = Quantum.Server.Char.GetInventory( char ) + + if( inv[index] != nil ) then + local itemid = inv[index][1] + + local item = Quantum.Item.Get( itemid ) + + if( item.soulbound == true ) then + Quantum.Notify.Deny( pl, "You can not drop that item!" ) + return + end -- players cant drop soulbound items + + local am_diff = inv[index][2] - amount + + if( am_diff >= 0 ) then -- drop the item from the players inv + -- remove the items am_diff from its stack + Quantum.Server.Inventory.SetSlotItem( char, index, itemid, am_diff ) + + -- spawn the item infront of the player + local itemEnt = ents.Create( "q_item" ) + if( IsValid( itemEnt ) ) then + itemEnt:SetModel( item.model ) + itemEnt:SetPos( pl:GetPos() ) + itemEnt.amount = amount + itemEnt.itemid = itemid + itemEnt:Spawn() + + timer.Simple( math.Clamp( Quantum.Server.Settings.ItemDespawnTimer, 1, 600 ), function() + if( IsValid( itemEnt ) ) then + Quantum.Debug( "Despawned item " .. tostring(itemEnt) .. " [" .. itemEnt.itemid .. "]" ) + itemEnt:Remove() + end + end) + end + end + else + Quantum.Error( "Player " .. tostring( pl ) .. " tried to drop a something from index=" .. tostring(index) .. " where there exists no item." ) + end end \ No newline at end of file diff --git a/gamemode/engine/lib/server/sv_notify.lua b/gamemode/engine/lib/server/sv_notify.lua new file mode 100644 index 0000000..240c366 --- /dev/null +++ b/gamemode/engine/lib/server/sv_notify.lua @@ -0,0 +1,35 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +Quantum.Notify = {} + +local function makeColorAString( clr ) + return "Color(" .. tostring( clr.r ) .. "," .. tostring( clr.g ) .. "," .. tostring( clr.b ) .. ")" +end + +local baseClr = Color( 220, 220, 220 ) + +function Quantum.Notify.ItemPickup( pl, item, amount ) + local amtStr = "" + if( amount > 1 ) then amtStr = tostring(amount) .. "x " end + + local itemColor = item.rarity.color || baseClr + local itemName = item.name || "[ERROR name=nil]" + + local luaArgs = makeColorAString(baseClr) .. ",'You picked up '," .. makeColorAString(baseClr) .. ",'" .. tostring(amtStr) .. "'," .. makeColorAString(itemColor) .. "," .. "'" .. tostring(itemName) .. "'" + local luaFunc = "chat.AddText(" .. luaArgs .. ")" + + pl:SendLua( luaFunc ) +end + +function Quantum.Notify.Deny( pl, text ) + + local luaArgs = makeColorAString( Color( 245, 20, 20 ) ) .. ",'" .. tostring( text ) .. "'" + local luaFunc = "chat.AddText(" .. luaArgs .. ")" + + pl:SendLua( luaFunc ) +end \ No newline at end of file diff --git a/gamemode/settings/sh_items.lua b/gamemode/settings/sh_items.lua index f305e76..bb1850f 100644 --- a/gamemode/settings/sh_items.lua +++ b/gamemode/settings/sh_items.lua @@ -20,7 +20,7 @@ Quantum.Item.Create( "test2", { name = "Trash Item Test", desc = "This is literall trash\nLine breaker test :D\n\nTest :D", model = "models/props_phx/gears/bevel12.mdl", - stack = 25, --wrong change later to 10 or something + stack = 10, --wrong change later to 10 or something soulbound = false, equipable = false, rarity = Quantum.Rarity.Trash, diff --git a/gamemode/settings/sv_settings.lua b/gamemode/settings/sv_settings.lua index 650ec7c..1d56a82 100644 --- a/gamemode/settings/sv_settings.lua +++ b/gamemode/settings/sv_settings.lua @@ -13,6 +13,10 @@ Quantum.Server.Settings.MaxHealth = 100 Quantum.Server.Settings.StarterMoney = 0 +Quantum.Server.Settings.ItemDespawnTimer = 300 + +Quantum.Server.Settings.ItemPickupSound = "physics/cardboard/cardboard_box_impact_hard2.wav" + Quantum.Server.Settings.InitSpawnLocation = { pos = Vector( 5054.682617, 3152.809326, 168.031250 ), ang = Angle( 3.009660, 89.639153, 0.000000 )