From b90a255d03f20f0ce431b13e330d7fa5dcf3c570 Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Wed, 8 Jan 2020 21:16:18 +0100 Subject: [PATCH] Began work on crafting system --- gamemode/engine/core/client/cl_holster.lua | 12 +-- gamemode/engine/lib/server/sv_inventory.lua | 14 ++++ gamemode/engine/lib/sh_recipe.lua | 82 +++++++++++++++++++++ gamemode/settings/sh_settings.lua | 2 +- 4 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 gamemode/engine/lib/sh_recipe.lua diff --git a/gamemode/engine/core/client/cl_holster.lua b/gamemode/engine/core/client/cl_holster.lua index e9496ba..90ade34 100644 --- a/gamemode/engine/core/client/cl_holster.lua +++ b/gamemode/engine/core/client/cl_holster.lua @@ -32,9 +32,11 @@ function Quantum.Client.Holster.SwitchHolster() local curWep= getCurEquippedWeapon() if( curWep != nil && curWep != "quantum_hands" ) then local wepEnt = client:GetWeapon( curWep ) - client:SelectWeapon( client:GetWeapon( curWep ) ) + print(wepEnt) + input.SelectWeapon( wepEnt ) else - client:SelectWeapon( client:GetWeapon( "quantum_hands" ) ) + local wepEnt = client:GetWeapon( "quantum_hands" ) + input.SelectWeapon( wepEnt ) end end end @@ -57,7 +59,7 @@ function Quantum.Client.Holster.CheckBind() end hook.Add( "Think", "Quantum_Client_Holster_Hook", function() - -- if( !Quantum.Client.IsInMenu ) then - -- Quantum.Client.Holster.CheckBind() - -- end + if( !Quantum.Client.IsInMenu ) then + Quantum.Client.Holster.CheckBind() + end end) \ 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 fdb3582..dc7cb32 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -188,6 +188,20 @@ function Quantum.Server.Inventory.FindStackable( char, item ) end end +function Quantum.Server.Inventory.GetItemAmount( char, itemid, inv ) + inv = inv || Quantum.Server.Char.GetInventory( char ) + local amount = 0 + + for pos, item in pairs( inv ) do + if( item[1] == itemid && item[2] != nil ) then + if( item[2] > 0 ) then + amount = amount + item[2] + end + end + end + return amount +end + function Quantum.Server.Inventory.FindItemSpot( char ) local inv = Quantum.Server.Char.GetInventory( char ) local pos = 0 diff --git a/gamemode/engine/lib/sh_recipe.lua b/gamemode/engine/lib/sh_recipe.lua new file mode 100644 index 0000000..f42a5b4 --- /dev/null +++ b/gamemode/engine/lib/sh_recipe.lua @@ -0,0 +1,82 @@ +-- __ _ _______ _ __ +-- / / /\ | | |__ __| | | \ \ +-- / / / \ | |_ __ ___ | | ___ ___| |__ \ \ +-- < < / /\ \ | | '_ ` _ \| |/ _ \/ __| '_ \ > > +-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / +-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ + +Quantum.Recipe = {} + +Quantum.Recipes = {} + +function Quantum.Recipe.Add( itemid, station, tbl ) + if( Quantum.Item.Get( itemid ) == nil ) then return end + + local returnTbl = { + name = tbl.name || "Secret Recipe" -- name of the recipe + station = station, + creates = itemid -- what the recipe creates + amount = tbl.amount -- how much you get from 1 craft + recipe = tbl.recipe || {} + } + + Quantum.Recipes[ itemid ] = returnTbl + + return returnTbl +end + +function Quantum.Recipe.Get( itemid ) + return Quantum.Recipes[itemid] +end + +function Quantum.Recipe.GetAvailableAmountForReq( recipe, pos, inv ) + if( inv != nil ) then + return Quantum.Server.Inventory.GetItemAmount( nil, recipe[pos].item, inv ) + end +end + +function Quantum.Recipe.GetNeededAmountForReq( recipe, pos, inv ) + if( inv != nil ) then + return Quantum.Recipe.GetAvailableAmountForReq( recipe, pos, inv ) - recipe[pos].amount + end +end + +local function canMakeReq( diff ) return diff >= 0 end + +function Quantum.Recipe.CanMake( inv, itemid ) + if( inv != nil && itemid != nil ) then + + local recipeTbl = Quantum.Recipe.Get( itemid ) + local rTbl = recipeTbl.recipe + + local canMake = {} + local failedReq = {} + + for i, req in pairs( rTbl ) do + canMake[i] = canMakeReq( Quantum.Recipe.GetNeededAmountForReq( rTbl, i, inv ) ) + if( !canMake[i] ) then + failedReq[i] = req.item + end + end + + return #failedReq <= 0, failedReq + + end +end + +function Quantum.Recipe.MakeItem( pl, itemid ) + local recipe = Quantum.Recipe.Get( itemid ) + local char = Quantum.Server.Char.GetCurrentCharacter( pl ) + local inv = Quantum.Server.Char.GetInventory( char ) + + if( recipe != nil ) then + local canMake, failedReq = Quantum.Recipe.CanMake( inv, itemid ) + + if( canMake ) then + -- create item + else + -- Dont make the item + return + end + end +end \ No newline at end of file diff --git a/gamemode/settings/sh_settings.lua b/gamemode/settings/sh_settings.lua index 6947ea3..07224a1 100644 --- a/gamemode/settings/sh_settings.lua +++ b/gamemode/settings/sh_settings.lua @@ -30,7 +30,7 @@ Quantum.Money = { Quantum.Bind = { OpenInventory = KEY_TAB, - HolsterMain = KEY_F + HolsterMain = KEY_Q } Quantum.Models = {