diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2016-05-27 20:24:40 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2016-05-27 20:24:40 -0400 |
| commit | 427b41c5683d9c913bd7b66d540d8a882a33ebf6 (patch) | |
| tree | 8c91e778b2ad6385964573964674fb3aa97a9594 /gamemode/structuresystem/common.lua | |
| parent | 9ea65b0c6a2b53766e5aa66cb6d86644a70da21f (diff) | |
| download | gmstranded-427b41c5683d9c913bd7b66d540d8a882a33ebf6.tar.gz gmstranded-427b41c5683d9c913bd7b66d540d8a882a33ebf6.tar.bz2 gmstranded-427b41c5683d9c913bd7b66d540d8a882a33ebf6.zip | |
Refactored where the structure code was
Diffstat (limited to 'gamemode/structuresystem/common.lua')
| -rw-r--r-- | gamemode/structuresystem/common.lua | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/gamemode/structuresystem/common.lua b/gamemode/structuresystem/common.lua new file mode 100644 index 0000000..d7da6d6 --- /dev/null +++ b/gamemode/structuresystem/common.lua @@ -0,0 +1,145 @@ + + +local buts = 92 + +local function makeCraftingWindow(name) + local pw = ScrW() / 1.3 + local ph = ScrH() / 1.4 + local DermaPanel = vgui.Create( "DFrame" ) + DermaPanel:SetPos( 100, 100 ) + DermaPanel:SetSize( pw, ph ) + DermaPanel:SetTitle( name ) + DermaPanel:SetDraggable( true ) + DermaPanel:Center() + DermaPanel.Grid = vgui.Create("DGrid",DermaPanel) + DermaPanel.Grid:SetPos(10,30) + DermaPanel.Grid:SetColWide(buts) + DermaPanel.Grid:SetCols(pw / buts) + DermaPanel.Grid:SetRowHeight(buts) + DermaPanel.info = vgui.Create("DPanel",DermaPanel) + DermaPanel.info:SetPos(10,(buts * 2) + 30) + DermaPanel.info:SetSize(pw - 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) + + return DermaPanel +end + +if SERVER then + util.AddNetworkString( "makerecipe" ) +end + +function genericMakeCrafting(tbl) + local oldusefunc = tbl.onUse + local overrideuse = function(self, ply) + oldusefunc(self,ply) + if SERVER or ply != LocalPlayer() then return end + + tbl.Recipes = {} + for k,v in pairs(tbl.genericRecipes) do + local ratio = v.Ratio + local mults = v.Mults + 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 + thisrecipe.Name = v.Name .. " x" .. j + thisrecipe.Description = v.Description + thisrecipe.genericNum = k + thisrecipe.mult = j + table.insert(tbl.Recipes,0,thisrecipe) + end + end + + local DermaPanel = makeCraftingWindow(tbl.Name) + for k,v in pairs(tbl.Recipes) do + local testbut = vgui.Create("DButton") + testbut:SetText(v.Name) + testbut:SetSize(buts,buts) + testbut.DoClick = function(button) + 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 + cancraft = false + end + end + DermaPanel.makebutton:SetEnabled(cancraft) + end + DermaPanel.Grid:AddItem(testbut) + DermaPanel:MakePopup() + end + 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,"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) + local mult = net.ReadUInt(GMS.NETINT_BITCOUNT) + local recipe = tbl.genericRecipes[recipenum] + + --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, recipe.Time(ply,mult), function(player) + for k,v in pairs(recipe.Results) do + player:IncResource(k * mult,v) + end + 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 |
