From 503cb4a9a95bb04218afb6bea287008ab4ccd278 Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Mon, 6 Jan 2020 18:32:00 +0100 Subject: [PATCH] Converted item functions to effects & bug fixes --- gamemode/cl_init.lua | 7 +- .../engine/core/client/cl_character_net.lua | 4 +- .../engine/derma/lib/cl_menu_iteminfo.lua | 70 +++++++++++++++---- gamemode/engine/lib/server/sv_inventory.lua | 8 +-- gamemode/engine/lib/sh_effects.lua | 19 +++-- gamemode/engine/lib/sh_items.lua | 4 +- gamemode/init.lua | 6 +- gamemode/settings/sh_effects.lua | 29 ++++++++ gamemode/settings/sh_items.lua | 13 +--- 9 files changed, 117 insertions(+), 43 deletions(-) create mode 100644 gamemode/settings/sh_effects.lua diff --git a/gamemode/cl_init.lua b/gamemode/cl_init.lua index d0d2aff..6625b96 100644 --- a/gamemode/cl_init.lua +++ b/gamemode/cl_init.lua @@ -46,8 +46,9 @@ if CLIENT then include( fol .. "cl_menu.lua" ) end - local function loadAllItems() + local function loadAllItemsAndEffects() include( "settings/sh_items.lua" ) + include( "settings/sh_effects.lua" ) end function Quantum.Client.Load() @@ -60,8 +61,8 @@ if CLIENT then Quantum.Debug( "Loaded all files." ) -- add all of the items - loadAllItems() - Quantum.Debug( "Loaded all items." ) + loadAllItemsAndEffects() + Quantum.Debug( "Loaded all items & effects." ) end Quantum.Client.Load() diff --git a/gamemode/engine/core/client/cl_character_net.lua b/gamemode/engine/core/client/cl_character_net.lua index c4f263d..be91ff6 100644 --- a/gamemode/engine/core/client/cl_character_net.lua +++ b/gamemode/engine/core/client/cl_character_net.lua @@ -55,7 +55,7 @@ function Quantum.Client.InventoryNet.UseItem( index ) local item = Quantum.Client.Inventory[index] local itemTbl = Quantum.Item.Get( item[1] ) if( itemTbl != nil && item[2] > 0 ) then - if( itemTbl.usefunction != nil ) then + if( itemTbl.useeffect != nil ) then Quantum.Client.InventoryNet.SetItem( index, item[1], item[2] - 1 ) -- remove one from the inventory on the client @@ -72,7 +72,7 @@ function Quantum.Client.InventoryNet.EatItem( index ) local item = Quantum.Client.Inventory[index] local itemTbl = Quantum.Item.Get( item[1] ) if( itemTbl != nil && item[2] > 0 ) then - if( itemTbl.consumefunction != nil ) then + if( itemTbl.consumeeffect != nil ) then Quantum.Client.InventoryNet.SetItem( index, item[1], item[2] - 1 ) -- remove one from the inventory on the client diff --git a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua index a503e5a..80f0671 100644 --- a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua +++ b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua @@ -188,7 +188,7 @@ function iteminfo.giveoptions( p, page ) end - if( item.usefunction != nil ) then -- USE + if( item.useeffect != nil ) then -- USE op.use = vgui.Create( "DButton", options ) op.use:SetText( "Use Item" ) @@ -213,7 +213,7 @@ function iteminfo.giveoptions( p, page ) ypos = ypos + op.use.h + yspacing end - if( item.consumefunction ) then -- EAT + if( item.consumeeffect != nil ) then -- EAT op.eat = vgui.Create( "DButton", options ) op.eat:SetText( "Consume" ) @@ -335,14 +335,17 @@ function iteminfo.givetooltip( p, page ) rare:SetPos( title.x, title.y + title.h + padding_s ) rare.x, rare.y = rare:GetPos() - local equip = vgui.Create( "DLabel", self ) - equip:SetText( Quantum.EquipSlotsNames[self.item.equipslot] || "ERROR EQUIPTYPE" ) - equip:SetFont( "q_tooltip_equiptype" ) - equip:SetTextColor( Color( 255, 255, 255, 255 ) ) - equip:SizeToContents() - equip.w, equip.h = equip:GetSize() - equip:SetPos( title.x, rare.y + rare.h + padding_s ) - equip.x, equip.y = equip:GetPos() + local equip + if( Quantum.EquipSlotsNames[self.item.equipslot] != nil ) then + equip = vgui.Create( "DLabel", self ) + equip:SetText( Quantum.EquipSlotsNames[self.item.equipslot] || "ERROR EQUIPTYPE" ) + equip:SetFont( "q_tooltip_equiptype" ) + equip:SetTextColor( Color( 255, 255, 255, 255 ) ) + equip:SizeToContents() + equip.w, equip.h = equip:GetSize() + equip:SetPos( title.x, rare.y + rare.h + padding_s ) + equip.x, equip.y = equip:GetPos() + end local desc = vgui.Create( "DLabel", self ) desc:SetText( self.item.desc || "ERROR DESC" ) @@ -350,9 +353,52 @@ function iteminfo.givetooltip( p, page ) desc:SetTextColor( Color( 205, 205, 205, 255 ) ) desc:SizeToContents() desc.w, desc.h = desc:GetSize() - desc:SetPos( title.x, equip.y + equip.h + padding_s ) + + if( IsValid(equip) ) then + desc:SetPos( title.x, equip.y + equip.h + padding_s ) + else + desc:SetPos( title.x, rare.y + rare.h + padding_s ) + end + desc.x, desc.y = desc:GetPos() + local yposBase = desc.y + desc.h + padding_s + + if( self.item.useeffect != nil ) then + local effectTbl = Quantum.Effect.Get( self.item.useeffect ) + + if( effectTbl.desc != nil ) then + local use = vgui.Create( "DLabel", self ) + use:SetText( "Use: " .. effectTbl.desc || "ERROR DESC USE" ) + use:SetFont( "q_tooltip_desc" ) + use:SetTextColor( Color( 18, 224, 66, 255 ) ) + use:SizeToContents() + use.w, use.h = use:GetSize() + use:SetPos( title.x, yposBase ) + use.x, use.y = use:GetPos() + + yposBase = yposBase + use.y + padding_s + end + end + + if( self.item.consumeeffect != nil ) then + + local effectTbl = Quantum.Effect.Get( self.item.consumeeffect ) + + if( effectTbl.desc != nil ) then + local eat = vgui.Create( "DLabel", self ) + eat:SetText( "Consume: " .. effectTbl.desc || "ERROR DESC EAT" ) + eat:SetFont( "q_tooltip_desc" ) + eat:SetTextColor( Color( 18, 224, 66, 255 ) ) + eat:SizeToContents() + eat.w, eat.h = eat:GetSize() + eat:SetPos( title.x, yposBase ) + eat.x, eat.y = eat:GetPos() + + yposBase = yposBase + eat.y + padding_s + end + end + if( self.item.soulbound == true ) then local sb = vgui.Create( "DLabel", self ) sb:SetText( "Soulbound" ) @@ -360,7 +406,7 @@ function iteminfo.givetooltip( p, page ) sb:SetTextColor( Color( 235, 64, 52, 255 ) ) sb:SizeToContents() sb.w, sb.h = sb:GetSize() - sb:SetPos( title.x, desc.y + desc.h + padding_s ) + sb:SetPos( title.x, yposBase ) end -- Correct the tooltips size so its content fits inside of it diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index ec5af6b..02c8fbb 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -283,9 +283,9 @@ function Quantum.Server.Inventory.UseItem( pl, index ) if( item != nil || #item > 0 ) then local itemTbl = Quantum.Item.Get( item[1] ) - if( itemTbl.usefunction != nil ) then + if( itemTbl.useeffect != nil ) then Quantum.Server.Inventory.SetSlotItem( pl, char, index, item[1], item[2] - 1 ) - itemTbl.usefunction(pl) -- call the function + Quantum.Effect.Give( pl, itemTbl.useeffect ) -- call the function end end end @@ -298,9 +298,9 @@ function Quantum.Server.Inventory.EatItem( pl, index ) if( item != nil || #item > 0 ) then local itemTbl = Quantum.Item.Get( item[1] ) - if( itemTbl.consumefunction != nil ) then + if( itemTbl.consumeeffect != nil ) then Quantum.Server.Inventory.SetSlotItem( pl, char, index, item[1], item[2] - 1 ) - itemTbl.consumefunction( pl ) + Quantum.Effect.Give( pl, itemTbl.consumeeffect ) end end end \ No newline at end of file diff --git a/gamemode/engine/lib/sh_effects.lua b/gamemode/engine/lib/sh_effects.lua index d523d14..3844364 100644 --- a/gamemode/engine/lib/sh_effects.lua +++ b/gamemode/engine/lib/sh_effects.lua @@ -12,9 +12,10 @@ Quantum.Effects = {} -- container for all of the effects function Quantum.Effect.Create( effectid, tbl ) local effect = { id = effectid, - icon = tbl.icon, + title = tbl.title || "Unknown Effect", + desc = tbl.desc || "An effect that does stuff.", rarity = tbl.rarity || Quantum.Rarity.Common, - duration = tbl.duration || -1, + duration = tbl.duration, func = { start = tbl.startfunc || Quantum.EmptyFunction, runtime = tbl.runtimefunc || Quantum.EmptyFunction, @@ -35,7 +36,7 @@ if SERVER then -- server only functions if( effectTbl != nil ) then - local hookID = "Quantum_Effects_RunTime_" .. tostring(pl) .. "_" .. tostring(effectid) + local hookID = "Quantum_Effects_RunTime_" .. tostring(pl:SteamID64()) .. "_" .. tostring(effectid) pl.effecthooks[ #pl.effecthooks + 1 ] = hookID Quantum.Debug( "Adding runtime effect hook: " .. hookID ) @@ -48,8 +49,8 @@ if SERVER then -- server only functions end - function Quantum.Effect.RemoveRuntimeFunction( pl, effectid ) - local hookID = "Quantum_Effects_RunTime_" .. tostring(pl) .. "_" .. tostring(effectid) + function Quantum.Effect.RemoveRuntimeFunction( pl, effectid, hookID ) + hookID = hookID || "Quantum_Effects_RunTime_" .. tostring(pl:SteamID64()) .. "_" .. tostring(effectid) Quantum.Debug( "Removing runtime effect hook: " .. hookID ) hook.Remove( "Think", hookID ) end @@ -58,7 +59,7 @@ if SERVER then -- server only functions if( pl.effecthooks != nil ) then Quantum.Debug( "Removing all runtime hooks for " .. tostring(pl) .. "." ) for n, hookid in pairs( pl.effecthooks ) do - Quantum.Effect.RemoveRuntimeFunction( pl, hookid ) + Quantum.Effect.RemoveRuntimeFunction( pl, nil, hookid ) end end end @@ -86,6 +87,7 @@ if SERVER then -- server only functions for i, effect in pairs( char.effects ) do Quantum.Effect.Remove( pl, effect, char ) -- remove the effect end + Quantum.Effect.RemoveAllRuntimeFunctions( pl ) end end @@ -94,6 +96,9 @@ if SERVER then -- server only functions local char = Quantum.Server.Char.GetCurrentCharacter( pl ) if( effectTbl != nil ) then + + Quantum.Debug( "Giving " .. tostring(pl) .. " the '" .. tostring(effectid) .. "' effect." ) + if( effectTbl.func.start != nil && effectTbl.func.start != Quantum.EmptyFunction ) then effectTbl.func.start( pl ) end @@ -102,7 +107,7 @@ if SERVER then -- server only functions Quantum.Effect.AddRuntimeFunction( pl, effectid ) end - if( effectTbl.duration > 0 ) then + if( effectTbl.duration != nil ) then timer.Simple( effectTbl.duration, function() Quantum.Effect.Remove( pl, effectid ) -- remove the effect from the player after its duration is over end) diff --git a/gamemode/engine/lib/sh_items.lua b/gamemode/engine/lib/sh_items.lua index a9e466a..c40cf6b 100644 --- a/gamemode/engine/lib/sh_items.lua +++ b/gamemode/engine/lib/sh_items.lua @@ -19,8 +19,8 @@ function Quantum.Item.Create( itemid, args ) equipslot = args.equipslot, -- slot for the equipable equipeffect = args.equipeffect, -- equip buff like in other MMO RPG games rarity = args.rarity || Quantum.Rarity.Trash, -- rarity of the item - usefunction = args.usefunction, -- use function - consumefunction = args.consumefunction --consume function + useeffect = args.useeffect, -- use effect + consumeeffect = args.consumeeffect --consume effect } item.stack = math.Clamp( item.stack, 1, Quantum.Inventory.MaxStackSize ) -- clamp it so it does not go over the max size if( item.equipslot != nil ) then item.stack = 1 end -- make the stack size to 1 if equipable diff --git a/gamemode/init.lua b/gamemode/init.lua index ad05dcb..9897f19 100644 --- a/gamemode/init.lua +++ b/gamemode/init.lua @@ -13,6 +13,7 @@ if SERVER then AddCSLuaFile( "settings/sh_settings.lua" ) AddCSLuaFile( "settings/sh_items.lua" ) + AddCSLuaFile( "settings/sh_effects.lua" ) AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) @@ -110,8 +111,9 @@ if SERVER then end - local function loadAllItems() + local function loadAllItemsAndEffects() include( "settings/sh_items.lua" ) + include( "settings/sh_effects.lua" ) end function Quantum.Server.Load() @@ -121,7 +123,7 @@ if SERVER then addAllDermaMenus() -- Creation of stuff - loadAllItems() -- load the items + loadAllItemsAndEffects() end Quantum.Server.Load() diff --git a/gamemode/settings/sh_effects.lua b/gamemode/settings/sh_effects.lua new file mode 100644 index 0000000..ec33031 --- /dev/null +++ b/gamemode/settings/sh_effects.lua @@ -0,0 +1,29 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + + +Quantum.Effect.Create( "eat_potatoe", { + title = "Potatoe Powers", + desc = "Gives you 1000 health for 10 seconds.", + rarity = Quantum.Rarity.Legendary, + duration = 10, + startfunc = function( pl ) + pl:SetHealth( 1000 ) + end, + stopfunc = function( pl ) + pl:SetHealth( pl:GetMaxHealth() ) + end +} ) + +Quantum.Effect.Create( "eat_trash", { + title = "Bad Health", + desc = "You die.", + rarity = Quantum.Rarity.Trash, + startfunc = function( pl ) + pl:Kill() + end +} ) \ No newline at end of file diff --git a/gamemode/settings/sh_items.lua b/gamemode/settings/sh_items.lua index 37e01a1..4f4e07f 100644 --- a/gamemode/settings/sh_items.lua +++ b/gamemode/settings/sh_items.lua @@ -11,8 +11,6 @@ Quantum.Item.Create( "test", { model = "models/props_phx/gears/bevel12.mdl", soulbound = true, rarity = Quantum.Rarity.Rare, - usefunction = function() print( "Test!" ) end, - consumefunction = function() print( "Test 2!" ) end } ) Quantum.Item.Create( "test2", { @@ -22,10 +20,7 @@ Quantum.Item.Create( "test2", { stack = 10, soulbound = false, rarity = Quantum.Rarity.Trash, - consumefunction = function( user ) - Quantum.Notify.Info( user, "You consumed trash and therefore died!" ) - user:Kill() - end + consumeeffect = "eat_trash" } ) Quantum.Item.Create( "bomb", { @@ -45,9 +40,5 @@ Quantum.Item.Create( "potatoe", { soulbound = false, rarity = Quantum.Rarity.Legendary, equipslot = Quantum.EquipSlots.Head, - consumefunction = function( user ) - Quantum.Notify.Info( user, "You consumed a legendary potatoe! You now have 1000 health for 10 seconds!" ) - user:SetHealth( 1000 ) - timer.Simple( 10, function() user:SetHealth( user:GetMaxHealth() ) end) - end + consumeeffect = "eat_potatoe" } ) \ No newline at end of file