Added notification lib & item drop (server side)

master
AlmTech Software 5 years ago
parent aff6224da9
commit 1678ac6597
  1. 12
      entities/entities/q_item/cl_init.lua
  2. 35
      entities/entities/q_item/init.lua
  3. 15
      entities/entities/q_item/shared.lua
  4. 57
      gamemode/engine/lib/server/sv_inventory.lua
  5. 35
      gamemode/engine/lib/server/sv_notify.lua
  6. 2
      gamemode/settings/sh_items.lua
  7. 4
      gamemode/settings/sv_settings.lua

@ -0,0 +1,12 @@
-- __ _ _______ _ __
-- / / /\ | | |__ __| | | \ \
-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \
-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > >
-- \ \ / ____ \| | | | | | | | __/ (__| | | | / /
-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/
include( "shared.lua" )
function ENT:Draw()
self:DrawModel()
end

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

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

@ -22,7 +22,10 @@ local function isStackable( item )
end end
function Quantum.Server.Inventory.SetSlotItem( char, pos, itemid, amount ) 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 ) local item = Quantum.Item.Get( itemid )
if( isEquippable( item ) || !isStackable( item ) ) then if( isEquippable( item ) || !isStackable( item ) ) then
amount = 1 amount = 1
@ -31,7 +34,6 @@ function Quantum.Server.Inventory.SetSlotItem( char, pos, itemid, amount )
amount = amount || 1 amount = amount || 1
char.inventory[pos] = { itemid, amount } char.inventory[pos] = { itemid, amount }
end end
Quantum.Debug( "Gave " .. char.name .. " " .. amount .. "x [" .. item.name .. "] at " .. tostring(pos) )
end end
function Quantum.Server.Inventory.GetSlotItem( char, pos ) return char.inventory[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 end
function Quantum.Server.Inventory.GiveItem( pl, itemid, amount ) -- Quantum.Server.Inventory.GiveItem( Entity(1), "test2", 21 ) 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 inv = Quantum.Server.Char.GetInventory( char )
local item = Quantum.Item.Get( itemid ) 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 if( #inv + 1 <= Quantum.Inventory.Width * Quantum.Inventory.Height || Quantum.Server.Inventory.FindStackable( char, item ) != nil ) then
sortItem( char, itemid, amount ) 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 else
Quantum.Debug( "Tried to give " .. tostring(pl) .. " a item but their inventory is full!" ) Quantum.Debug( "Tried to give " .. tostring(pl) .. " a item but their inventory is full!" )
end end
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

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

@ -20,7 +20,7 @@ Quantum.Item.Create( "test2", {
name = "Trash Item Test", name = "Trash Item Test",
desc = "This is literall trash\nLine breaker test :D\n\nTest :D", desc = "This is literall trash\nLine breaker test :D\n\nTest :D",
model = "models/props_phx/gears/bevel12.mdl", 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, soulbound = false,
equipable = false, equipable = false,
rarity = Quantum.Rarity.Trash, rarity = Quantum.Rarity.Trash,

@ -13,6 +13,10 @@ Quantum.Server.Settings.MaxHealth = 100
Quantum.Server.Settings.StarterMoney = 0 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 = { Quantum.Server.Settings.InitSpawnLocation = {
pos = Vector( 5054.682617, 3152.809326, 168.031250 ), pos = Vector( 5054.682617, 3152.809326, 168.031250 ),
ang = Angle( 3.009660, 89.639153, 0.000000 ) ang = Angle( 3.009660, 89.639153, 0.000000 )

Loading…
Cancel
Save