From 9ea65b0c6a2b53766e5aa66cb6d86644a70da21f Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Fri, 27 May 2016 18:30:57 -0400 Subject: More work on structure system --- entities/entities/gms_base_entity.lua | 4 +- gamemode/structuresystem/common_smelt.lua | 145 +++++++++++++++++---- .../structuresystem/structures/stonefurnace.lua | 27 +++- 3 files changed, 140 insertions(+), 36 deletions(-) diff --git a/entities/entities/gms_base_entity.lua b/entities/entities/gms_base_entity.lua index eff03cb..09ad3d7 100644 --- a/entities/entities/gms_base_entity.lua +++ b/entities/entities/gms_base_entity.lua @@ -18,7 +18,9 @@ function ENT:Initialize() self:SetSolid( SOLID_VPHYSICS ) end - self:onInitialize() + if self.onInitialize then + self:onInitialize() + end end function ENT:Draw() diff --git a/gamemode/structuresystem/common_smelt.lua b/gamemode/structuresystem/common_smelt.lua index 43637e1..68a50b1 100644 --- a/gamemode/structuresystem/common_smelt.lua +++ b/gamemode/structuresystem/common_smelt.lua @@ -5,80 +5,169 @@ end function genericMakeFurnace(tbl) local oldusefunc = tbl.onUse local overrideuse = function(self, ply) + oldusefunc(self,ply) if SERVER or ply != LocalPlayer() then return end + + PrintTable(tbl.genericRecipes) + + tbl.Recipes = {} + for k,v in pairs(tbl.genericRecipes) do + local ratio = v.Ratio + local mults = {1,5,10,20,50} + for i,j in pairs(mults) do + local thisrecipe = {} + thisrecipe.Req = {} + thisrecipe.Results = {} + for l,m in pairs(v.Requirements) do + thisrecipe.Req[l] = ratio[1] * j * m + end + for l,m in pairs(v.Results) do + thisrecipe.Results[l] = ratio[2] * j * m + end + print("This recipe is :") + PrintTable(thisrecipe) + thisrecipe.Name = v.Name .. " x" .. j + thisrecipe.Description = v.Description + thisrecipe.genericNum = k + thisrecipe.mult = j + table.insert(tbl.Recipes,0,thisrecipe) + end + end + + print("Recipes are:") + PrintTable(tbl.Recipes) + local DermaPanel = vgui.Create( "DFrame" ) DermaPanel:SetPos( 100, 100 ) + local buts = 128 DermaPanel:SetSize( ScrW() / 1.3, ScrH() / 1.4 ) DermaPanel:SetTitle( tbl.Name ) DermaPanel:SetDraggable( true ) DermaPanel:Center() local Grid = vgui.Create("DGrid",DermaPanel) Grid:SetPos(10,30) - Grid:SetColWide(128) - Grid:SetRowHeight(128) + Grid:SetColWide(buts) + Grid:SetRowHeight(buts) + DermaPanel.info = vgui.Create("DPanel",DermaPanel) + DermaPanel.info:SetPos(10,(buts * 2) + 30) + DermaPanel.info:SetSize(ScrW() / 1.3 - 20, buts * 0.75) + DermaPanel.infotext = vgui.Create("DLabel",DermaPanel.info) + DermaPanel.infotext:SetText("") + DermaPanel.infotext:SetPos(10,10) + DermaPanel.infotext:SetDark(true) + DermaPanel.infotext:SetFont( "ScoreboardSub" ) + DermaPanel.infotext:Dock(TOP) + DermaPanel.reqtext = vgui.Create("DLabel",DermaPanel.info) + DermaPanel.reqtext:SetPos(10,30) + DermaPanel.reqtext:SetDark(true) + DermaPanel.reqtext:Dock(FILL) for k,v in pairs(tbl.Recipes) do local testbut = vgui.Create("DButton") testbut:SetText(v.Name) - testbut:SetSize(128,128) + testbut:SetSize(buts,buts) testbut.DoClick = function(button) - net.Start("makerecipe") - net.WriteString(tbl.Name) - if tbl.uniquedata then net.WriteUInt(self:EntIndex(), GMS.NETINT_BITCOUNT) end - net.WriteUInt(k, GMS.NETINT_BITCOUNT) - net.SendToServer() + print("Recipe is:") + PrintTable(v) + DermaPanel.structname = tbl.Name + DermaPanel.recipeNum = v.genericNum + DermaPanel.recipeMult = v.mult + local text = v.Description .. "\nRequires:" + for i,j in pairs(v.Req) do + text = text .. "\n" .. i .. "x" .. j + end + DermaPanel.infotext:SetText(v.Name) + DermaPanel.reqtext:SetText(text) + local cancraft = true + for i,j in pairs(v.Req) do + if Resources[i] < j then + print("Can't craft becaues " .. i .. "(" .. Resources[i] .. ") is less than " .. j) + cancraft = false + end + end + DermaPanel.makebutton:SetEnabled(cancraft) end Grid:AddItem(testbut) DermaPanel:MakePopup() end - oldusefunc(self,ply) + DermaPanel.makebutton = vgui.Create("DButton",DermaPanel) + DermaPanel.makebutton:SetPos(10,(buts * 2) + (buts * 0.75) + 30) + DermaPanel.makebutton:SetSize(ScrW() / 1.3 - 20, (buts / 4)) + DermaPanel.makebutton:SetText("Craft") + DermaPanel.makebutton.DoClick = function() + net.Start("makerecipe") + net.WriteString(DermaPanel.structname) + if tbl.uniquedata then net.WriteUInt(self:EntIndex(), GMS.NETINT_BITCOUNT) end + net.WriteUInt(DermaPanel.recipeNum, GMS.NETINT_BITCOUNT) + net.WriteUInt(DermaPanel.recipeMult, GMS.NETINT_BITCOUNT) + net.SendToServer() + end + end tbl.onUse = overrideuse end net.Receive( "makerecipe", function(ln,ply) local tblname = net.ReadString() local tbl = GMS.Structures[tblname] - assert(tbl != nil,"Maybe someone's trying to hack lol") + assert(tbl != nil,"Tried to make a recipe in a structure that dosen't exist!") if tbl.uniquedata then local entnum = net.ReadUInt(GMS.NETINT_BITCOUNT) tbl = GMS.UniqueStructures[entnum] end local recipenum = net.ReadUInt(GMS.NETINT_BITCOUNT) - assert(tbl.Recipes != nil and tbl.Recipes[recipenum] != nil, "Invalid recpie!") - local recipe = tbl.Recipes[recipenum] - local numrequired = 1 - for k, v in pairs( recipe.Req ) do - numrequired = numrequired + v - end - local time = math.pow(numrequired,tbl.timemult) - ((numrequired * tbl.timemult) * math.pow(ply:GetSkill("Smelting"),tbl.skillease)) - time = time * numrequired / 5 + local mult = net.ReadUInt(GMS.NETINT_BITCOUNT) + print("Attempting to craft:") + print("Table name:" .. tblname) + print("Recipenum:" .. recipenum) + print("Multiplier:" .. mult) + print("This recipe is:") + print("Table is:") + PrintTable(tbl) + local recipe = tbl.genericRecipes[recipenum] + print("Recipe:") + PrintTable(recipe) - for k,v in pairs(recipe.Req) do - if ply:GetResource(k) < v then + --Check that we have enough ingredients + for k,v in pairs(recipe.Requirements) do + if ply:GetResource(k * mult) < v then ply:SendMessage("You don't have enough!", 3, Color(255, 255, 255, 255)) return end end + --We have enough resources! make it! - startProcessGeneric(ply,"Crafting " .. recipe.Name, time, function(player) + startProcessGeneric(ply,"Crafting " .. recipe.Name, recipe.Time(ply,mult), function(player) for k,v in pairs(recipe.Results) do - player:IncResource(k,v) + player:IncResource(k * mult,v) end - for k,v in pairs(recipe.Req) do - ply:DecResource(k,v) + for k,v in pairs(recipe.Requirements) do + ply:DecResource(k * mult,v) end end) end) +function genericGiveRecipie(tbl, recipe) + tbl.genericRecipes = tbl.genericRecipes or {} + table.insert(tbl.genericRecipes, recipe) +end + function recipieForSmelt(tbl, name, description, required, result, ratio, mults) + local recipie = {} + recipie.Req = {} + recipie.Req[required] = ratio[1] + recipie.Results = {} + recipie.Results[result] = ratio[2] + local num = table.insert(tbl.GenericRecipe,0,recipe) for k,v in pairs(mults) do + if v == 0 then continue end local thisrecipie = {} - thisrecipie.Name = name .. " x" .. v + thisrecipie.Name = name .. " x" .. math.floor(v) thisrecipie.Description = description thisrecipie.Req = {} - thisrecipie.Req[required] = ratio[1] * v + thisrecipie.Req[required] = math.floor(ratio[1] * v) thisrecipie.Results = {} - thisrecipie.Results[result] = ratio[2] * v - table.insert(tbl,0,thisrecipie) + thisrecipie.Results[result] = math.floor(ratio[2] * v) + thisrecipie.GenericRecipe = num + table.insert(tbl.Recipes,0,thisrecipie) end end diff --git a/gamemode/structuresystem/structures/stonefurnace.lua b/gamemode/structuresystem/structures/stonefurnace.lua index 84c0311..5914ed2 100644 --- a/gamemode/structuresystem/structures/stonefurnace.lua +++ b/gamemode/structuresystem/structures/stonefurnace.lua @@ -14,18 +14,31 @@ end STRUCT.uniquedata = false STRUCT.onUse = function(self, ply) - print("I am the old use function!") + if CLIENT and ply != LocalPlayer() then return end + print("onUse called!") end -STRUCT.Recipes = {} - STRUCT.timemult = 0.5 STRUCT.skillease = 0.25 -genericMakeFurnace(STRUCT) -recipieForSmelt(STRUCT.Recipes,"Copper", "Smelt copper ore into copper", "Copper Ore", "Copper", {1,1}, {1,5,10}) +local timefunc = function(ply, num) + print("Inputs: num=" .. num) + local time = math.pow(num,STRUCT.timemult) - ((num * STRUCT.timemult) * math.pow(ply:GetSkill("Smelting"),STRUCT.skillease )) + time = time * num / 5 + print("Time was: " .. time) + return time +end -print("stone furnace's recipies:") -PrintTable(STRUCT.Recipes) +local genericRecipe = { + ["Name"] = "Copper", + ["Description"] = "Smelt some copper or into copper!", + ["Requirements"] = {["Copper Ore"] = 1}, + ["Results"] = {["Copper"] = 1}, + ["Ratio"] = {1,1}, + ["Time"] = timefunc +} + +genericGiveRecipie(STRUCT,genericRecipe) +genericMakeFurnace(STRUCT) registerStructure(STRUCT) -- cgit v1.2.3-70-g09d2