summaryrefslogtreecommitdiff
path: root/gamemode/structuresystem/common.lua
blob: d7da6d677270a4a77e8d03c4535d5d6249efce4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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