View difference between Paste ID: wei73hxp and LHmkgw25
SHOW: | | - or go back to the newest paste.
1
local tArgs = {...}
2
local p = peripheral.wrap("bottom")
3
local c = peripheral.wrap("top")
4-
local product = p.getStackInSlot(1)
4+
5-
local recipe = {product = product.display_name, schematic = {}, ingredient = {}}
5+
local function loadRecipes() -- return contents of recipes as table
6-
for i=2, 82 do
6+
	local data = nil
7-
  local slot = p.getStackInSlot(i)
7+
	if fs.exists("recipes") then
8-
  local name = slot.display_name
8+
		file = fs.open("recipes", "r")
9-
  if slot then
9+
		data = textutils.unserialize(file.readAll())
10-
    recipe.schematic[i] = name
10+
		file.close()
11-
    if recipe.ingredient[name] then
11+
	end
12-
      recipe.ingredient[name] = recipe.ingredient[name] + slot.qty
12+
	return data
13-
    else
13+
14-
      recipe.ingredient[name] = slot.qty
14+
15-
    end
15+
local function learnRecipe() --returns disp_name (string), recipeLearned (bool), recipeExists (bool)
16-
  end
16+
 	local product = p.getStackInSlot(1)
17
	local recipeExists, recipeLearned = false
18
	if product then
19
		local recipe = {product = product.display_name, schematic = {}, ingredient = {}}
20-
local chestSize = c.getInventorySize()
20+
		for i=2, 82 do
21-
local chest = {quantity={}}
21+
			local slot = p.getStackInSlot(i)
22-
for i=1, chestSize do
22+
			if slot then
23-
  local slot = c.getStackInSlot(i)
23+
				local name = slot.display_name
24-
  if slot then
24+
				recipe.schematic[i] = name
25-
    local name = slot.display_name
25+
				if recipe.ingredient[name] then
26-
    chest[i] = name
26+
					recipe.ingredient[name] = recipe.ingredient[name] + slot.qty
27-
    if chest.quantity[name] then
27+
				else
28-
      chest.quantity[name] = chest.quantity[name] + slot.qty
28+
				  recipe.ingredient[name] = slot.qty
29-
    else
29+
				end
30-
      chest.quantity[name] = slot.qty
30+
			end
31-
    end  
31+
		end --ends for schematic record loop
32-
  end
32+
		local savedRecipes = {}
33
		local writeRecipe = false
34
		if fs.exists("recipes") then	
35-
for k,v in pairs(recipe.ingredient) do
35+
			savedRecipes = loadRecipes()
36-
  local name = k 
36+
			if savedRecipes[recipe.product] then
37-
  local required = v
37+
				--print("Recipe for "..recipe.product.. " already exists.")
38-
  local available = chest.quantity[k]
38+
				recipeExists = true
39-
  if available >= required then
39+
			else writeRecipe = true
40-
    term.setTextColor(colors.green)
40+
			end
41-
    print(k.." | "..available.."/"..v)
41+
		else writeRecipe = true
42-
  else
42+
		end
43-
    term.setTextColor(colors.red)
43+
		if writeRecipe then
44-
    print(k.." : "..available.."/"..v)
44+
			savedRecipes[product.display_name] = recipe	
45-
  end
45+
			local file = fs.open("recipes", "w")
46
			file.write(textutils.serialize(savedRecipes))
47
			file.close()
48
			recipeLearned = true
49
		end
50
	end
51
	return product.displayname, recipeLearned, recipeExists
52
end
53
54
local function chestQuery() -- returns table | table.slotNum == display_name, table.qty.display_name == total qty
55
	local chestSize = c.getInventorySize()
56
	local chest = {slotNum = {}, qty = {}}
57
	for i=1, chestSize do
58
	  local slot = c.getStackInSlot(i)
59
	  if slot then
60
		local name = slot.display_name
61
		chest.slotNum[i] = name
62
		if chest.qty[name] then
63
		  chest.qty[name] = chest.qty[name] + slot.qty
64
		else
65
		  chest.qty[name] = slot.qty
66
		end  
67
	  end
68
	end
69
	return(chest)
70
end
71
72
local function checkStock(item) -- returns boolean 'craftable', table missingIngredients
73
	local recipes = loadRecipes()
74
	local stock = chestQuery() 
75
	local craftable = false
76
	
77
	for k,v in pairs(recipes) do
78
		if k == item then
79
			craftable = true
80
		end
81
	end
82
83
	local missingIngredients = recipes[item].ingredient
84
	local missingSum = 0
85
	if craftable then
86
		for k,v in pairs(stock.qty) do
87
			missingIngredients[k] = missingIngredients[k] - v
88
			missingSum = missingSum + missingIngredients[k]
89
		end
90
	end
91
	
92
	if missingSum <= 0 then
93
		craftable = true
94
	else
95
		craftable = false
96
	end
97
	
98
	return craftable, missingIngredients
99
end
100
101
local function craft(item)
102
	if checkStock(item) then
103
		local recipes = loadRecipes()
104
		local schematic = recipes[item].schematic
105
		local chest = chestQuery()
106
		
107
		for k,v in pairs(schematic) do
108
			for k1,v1 in pairs(chest.slotNum) do
109
				if k==k1 then
110
					c.pushItem("down",1,1)
111
					p.pullItemIntoSlot("up", k)
112
				end
113
			end
114
		end
115
	end
116
end
117
118
if tArgs[1] == "learn" then	
119
	local x,y,z = learnRecipe()
120
	if y then
121
		print("Sucessfully learnt.")
122
	elseif z then
123
		print("Recipe already known.")
124
	else
125
		print("Recipe cannot be learnt. (Invalid product?)")
126
	end
127
elseif tArgs[1] == "recipes" then
128
	if fs.exists("recipes") then
129
		local file = fs.open("recipes","r")
130
		local recipes = textutils.unserialize(file.readAll())
131
		file.close()
132
		for k,v in pairs(recipes) do
133
			print(k)
134
		end
135
	else 
136
		print("No recipes exist.")
137
	end
138
elseif tArgs[1] == "craft" then
139
	if #tArgs >= 2 then
140
		local itemName = tArgs[2]
141
		if #tArgs >= 3 then
142
			for i=3,#tArgs do
143
				itemName = itemName.. " ".. tArgs[i]
144
			end
145
		end
146
		craft(itemName)
147
	end
148
elseif tArgs[1] == "chestQuery" then
149
	print(textutils.serialize(chestQuery()))
150
elseif tArgs[1] == "checkStock" then
151
	if #tArgs >= 2 then
152
	local itemName = tArgs[2]
153
	if #tArgs >= 3 then
154
		for i=3,#tArgs do
155
			itemName = itemName.. " ".. tArgs[i]
156
		end
157
	end
158
	local x, y = checkStock(itemName)
159
	print(x)
160
	print(textutils.serialize(y))
161
	end
162
end