summaryrefslogtreecommitdiff
path: root/gamemode/structuresystem/common.lua
blob: 56cb355482385f764864822bb4a607b7ca398080 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
local buts = 90
local recipeWidth = buts*4
local recipeHeight = buts*1.5

local function makeCraftingWindow(name)
  local pw = 735
  local ph = 500
  local DermaPanel = vgui.Create( "DFrame" )
  DermaPanel:SetPos( 100, 100 )
  DermaPanel:SetSize( pw, ph )
  DermaPanel:SetTitle( name )
  DermaPanel:SetDraggable( true )
  DermaPanel:Center()
  DermaPanel.Paint = function(self,w,h) 
	
	draw.RoundedBox( 0, 0, 0, w, h, Color(86,86,86,200) )
  
  end
  DermaPanel.scroll = vgui.Create( "DScrollPanel", DermaPanel )
  DermaPanel.scroll:Dock(FILL)
  DermaPanel.scroll:SetPos( 0, 25 )
  DermaPanel.Grid = vgui.Create("DGrid",DermaPanel.scroll)
  DermaPanel.Grid:SetPos(0,0)
  DermaPanel.Grid:SetColWide(recipeWidth+5)
  DermaPanel.Grid:SetCols(pw / recipeWidth)
  DermaPanel.Grid:SetRowHeight(recipeHeight+5)
  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 mults = v.Mults
	  if v.SmeltAll == true and !table.HasValue(mults,-1) then table.insert(mults,-1) end
      for i,j in pairs(mults) do
        local thisrecipe = {}
        thisrecipe.Req = {}
        thisrecipe.Results = {}
		thisrecipe.Ratio = v.Ratio
        for l,m in pairs(v.Requirements) do
		  if v.SmeltAll and j == -1 then
			thisrecipe.Req[l] = thisrecipe.Ratio[1] * m
			break
		  end
          thisrecipe.Req[l] = thisrecipe.Ratio[1] * j * m
        end
        for l,m in pairs(v.Results) do
          thisrecipe.Results[l] = thisrecipe.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
        for l,m in pairs(thisrecipe.Req) do
          if(Resources[l] == nil or Resources[l] < m) then
            thisrecipe.hasmatsfor = false
            break
          end
        end
        thisrecipe.mult = j
		
		if j == -1 then
			thisrecipe.Name = "All "..v.Name
			thisrecipe.hasmatsfor = true
			thisrecipe.SmeltAll = true
			thisrecipe.MaxAmount = v.MaxAmount
			if(Resources[v.Name] == nil or Resources[v.Name] < 1) then
				thisrecipe.hasmatsfor = false
			else
			thisrecipe.mult = Resources[v.Name]
			end
		end
		
        table.insert(tbl.Recipes,0,thisrecipe)
      end
    end

    local DermaPanel = makeCraftingWindow(tbl.Name)
	local infoPosX = 130
	local infoPosY = 0
    for k,v in pairs(tbl.Recipes) do
      local recipeButton = vgui.Create("DButton")
	  local text = v.Name .. "\n" .. v.Description .. "\nRequires:"
        for i,j in pairs(v.Req) do
          text = text .. "\n" .. i .. " x" .. j
		  if v.SmeltAll then text = text .. "\nMax Amount - " ..v.MaxAmount end 
        end
	  recipeButton.text = text
      recipeButton:SetText("")
      recipeButton:SetSize(recipeWidth,recipeHeight)
	  
	  DermaPanel.infotext = vgui.Create("DLabel",recipeButton)
	  DermaPanel.infotext:SetText(text)
	  DermaPanel.infotext:SetPos(infoPosX,infoPosY)
	  DermaPanel.infotext:SetSize(recipeWidth-infoPosX,recipeHeight)
	  DermaPanel.infotext:SetFont( "TargetID" )
	  DermaPanel.infotext:SetWrap(true)
	  
      recipeButton.Paint = function()
        //draw.RoundedBox( 8, 5, 5, w-10, h-10, Color( 0, 0, 0 ) )
		
        local fillcolor = Color(255,255,255)
        if v.haslevelsfor then
          if v.hasmatsfor then
            fillcolor = Color(23,180,23)
          else
            fillcolor = Color(200,0,0)
          end
        else
          fillcolor = Color(200,200,0)
        end
        draw.RoundedBox(0,0,0,recipeWidth,recipeHeight,Color(48,48,48,255))
		draw.RoundedBox(0,15,15,110,recipeHeight-30,fillcolor)
		draw.RoundedBox(8,20,20,100,recipeHeight-40,Color(98,98,98))
		
		for t,y in pairs(v.Req) do
			if v.SmeltAll and Resources[t] != nil and Resources[t] >= 1 then
				v.hasmatsfor = true
				break
			end			
			if Resources[t] != nil and Resources[t] < y then
				v.hasmatsfor = false
				break
			end
		end
		
      end
      recipeButton.DoClick = function(button)
		DermaPanel.structname = tbl.Name
        DermaPanel.recipeNum = v.genericNum
        DermaPanel.recipeMult = v.mult
        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.WriteTable(v.Ratio)
		net.WriteBool(v.SmeltAll)
		if v.SmeltAll then net.WriteUInt(v.MaxAmount, GMS.NETINT_BITCOUNT) end
		net.SendToServer()
		
      end
	  
	  local img = vgui.Create("DImage", recipeButton)
	  img:SetPos(24, 20)
	  img:SetSize(buts, buts)
	  img:SetImage(v.Image)
	  
      DermaPanel.Grid:AddItem(recipeButton)
      DermaPanel:MakePopup()
    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]
  local ratio = net.ReadTable()
  local SmeltAll = net.ReadBool()
  local maxAmount
  if SmeltAll then maxAmount = net.ReadUInt(GMS.NETINT_BITCOUNT) end
  
  --Check that we have enough ingredients
  for k,v in pairs(recipe.Requirements) do
  
    if SmeltAll and ply:GetResource(k) > 1 then
		mult = ply:GetResource(k)
		if ply:GetResource(k) > maxAmount then mult = maxAmount end
	else
  
    if ply:GetResource(k) < ratio[1] * v * mult then
	  ply:SendMessage("You don't have enough!", 3, Color(255, 255, 255, 255))
	  return
	end
	
	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!
  local prsName = "Crafting"
  if recipe.Skill != nil then prsName = recipe.Skill end
  local prcName = prsName
  if prcName == "Weapon_Crafting" then prcName = "Crafting" end
  startProcessGeneric(ply,prcName.." " .. recipe.Name, recipe.Time(ply,mult), function(player)
	for k,v in pairs(recipe.Requirements) do
      player:DecResource(k,ratio[1]*v*mult)
    end
    for k,v in pairs(recipe.Results) do
      player:IncResource(k,ratio[2]*v*mult)
    end
    
	
	if recipe.Skill != nil then
	if recipe.Skill == "Smelting" then player:IncXP( prsName, math.Clamp( math.Round( ( mult * 10 ) / player:GetSkill( prsName ) ), 1, 1000 ) )
	elseif recipe.Skill == "Weapon_Crafting" then player:IncXP( prsName, math.Clamp( math.Round( ( mult * 15 ) / player:GetSkill( prsName ) ), 1, 1000 ) )
	--elseif recipe.Skill == "Swimming" then player:IncXP( recipe.Skill, math.Clamp( math.Round( 100 / player:GetSkill( prsName ) ), 1, 1000 ) )
	else player:IncXP( prsName, math.Clamp( math.Round( 150 / player:GetSkill( prsName ) ), 1, 1000 ) ) end
	end
  end)
  
end)

function genericGiveRecipie(tbl, recipe)
  tbl.genericRecipes = tbl.genericRecipes or {}
  table.insert(tbl.genericRecipes, recipe)
end