summaryrefslogtreecommitdiff
path: root/gamemode/structuresystem/common.lua
blob: 49962230ecb7c53a73606b46fec38da3d1b4d2dc (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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.info:Dock(BOTTOM)
  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)
  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
  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.Image = v.Image
        thisrecipe.haslevelsfor = v.CanCraft(ply)
        thisrecipe.hasmatsfor = true
        PrintTable(thisrecipe.Req)
        for l,m in pairs(thisrecipe.Req) do
          if(Resources[l] < m) then
            thisrecipe.hasmatsfor = false
            break
          end
        end
        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("DImageButton")
      testbut:SetText(v.Name)
      testbut:SetImage(v.Image)
      testbut:SetSize(buts,buts)
      testbut.Paint = function(self,w,h)
        draw.RoundedBox( 8, 5, 5, w-10, h-10, Color( 0, 0, 0 ) )
        local fillcolor = Color(0,0,200)
        if v.haslevelsfor then
          if v.hasmatsfor then
            fillcolor = Color(200,200,200)
          else
            fillcolor = Color(200,0,0)
          end
        else
          fillcolor = Color(200,200,0)
        end
        draw.RoundedBox(8,10,10,w-20,h-20,fillcolor)
      end
      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)
        DermaPanel.makebutton:SetEnabled(v.haslevelsfor and v.hasmatsfor)
      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

  --Check that we're allowed to craft this
  if not recipe.CanCraft(ply) then
    ply:SendMessage("You can't craft that!", 3, Color(255, 255, 255, 255))
    return
  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