aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/player_recipes.lua
blob: 0c326f0b7da9740bbe061411ea392948b2d7734f (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
local meta = FindMetaTable("Player")

if (SERVER) then
	util.AddNetworkString("CreateRecipe")
	util.AddNetworkString("ResetRecipes")

	function meta:CreateRecipe(item)
		if (!self:CanCreateItem(item)) then return end

		local Rec,Item = GetRecipeForItem(item)

		if (!Rec) then return false end

		for a,b in pairs(Rec.Resources) do
			self:RemoveItem(a,b)
		end

		if(Item.OnCraft) then
			Item:OnCraft(self)
		end

		self:AddItem(item,1)
	end

	function meta:ResetKnownRecipes()
		net.Start("ResetRecipes") net.Send(self)
	end

	net.Receive("CreateRecipe",function(siz,pl) pl:CreateRecipe(net.ReadString()) end)
else
	function RequestCreateRecipe(item)
		net.Start("CreateRecipe")
			net.WriteString(item)
		net.SendToServer()
	end

	function DiscoverItems(Combinations)
		local Dat = {}

		for k,v in pairs(GAMEMODE.Recipes) do
			if not v.Recipe then continue end
			local Ab 	= v.Recipe.Resources
			local Tools = v.Recipe.Tools
			local PA	= table.Count(Ab)

			if (PA == table.Count(Combinations)) then
				local Check = 0

				for e,c in pairs(Combinations) do
					for a,b in pairs(Ab) do
						if (c.Name == a and c.Quantity == b) then Check = Check+1 break end
					end
				end

				if (Check == PA) then table.insert(Dat,v) end
			end
		end

		if (table.Count(Dat) > 0) then
			for e,c in pairs(GAMEMODE.KnownRecipes) do
				for k,v in pairs(Dat) do
					if (v.Name == c.Name) then table.remove(Dat,k) break end
				end
			end

			if (table.Count(Dat) > 0) then
				for k,v in pairs(Dat) do
					LocalPlayer():AddNote("You discovered the recipe for "..v.Name)
				end

				table.Add(GAMEMODE.KnownRecipes,Dat)

				ReloadRecipes()
			end
		end
	end

	net.Receive("ResetRecipes",function()
		GAMEMODE.KnownRecipes = {
			GetItemByName("Axe"),
			GetItemByName("Campfire"),
			GetItemByName("Pickaxe"),
			GetItemByName("Research Table")
		}

		ReloadRecipes()
	end)
end

function meta:CanCreateItem(name)
	local Rec,Item = GetRecipeForItem(name)

	if (!Rec) then return false end

	for k,v in pairs(Rec) do
		for a,b in pairs(v) do
			if (!self:HasItem(a,b)) then return false end
		end
	end

	return true
end