Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local recipes = {}
- local lp = #args > 0 and peripheral.wrap(args[1]) or peripheral.wrap("right")
- if not lp.getTurtleConnect() then lp.setTurtleConnect(true) end
- local ae = peripheral.wrap("bottom")
- local pipe = peripheral.wrap("top")
- local furnace = peripheral.wrap("back")
- local crucible = peripheral.wrap("front")
- local signalSide = #args > 1 and args[2] or "left"
- local furnaceDir = #args > 2 and args[3] or "south"
- -- http://lua-users.org/wiki/SplitJoin
- local function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local beans = {}
- local function scanBeans()
- local allItems = lp.getAvailableItems()
- --print(#allItems, " items in lp system")
- local foundABean = false
- for i,item in pairs(allItems) do
- if lp.getItemID(item[1]) == 25366 then
- foundABean = true
- local nbt = lp.getNBTTagCompound(item[1])
- if nbt then
- local name = nbt.value.Aspects.value[1].value.key.value
- if not beans[name] then beans[name] = {lpID=item[1], qty=item[2]}
- else print("duplicate bean: ",name) end
- end
- end
- end
- if not foundABean then
- sleep(5)
- os.reboot()
- end
- end
- local function readRecipes()
- local recipeFile = fs.open("recipes","r")
- if not recipeFile then return 0 end
- local line = recipeFile.readLine()
- while line do
- local recipe = split(line,",")
- if #recipe % 2 ~= 0 then break end
- recipes[recipe[2]] = {}
- for i=3,#recipe,2 do
- table.insert(recipes[recipe[2]],{type=recipe[i],qty=recipe[i+1]})
- end
- line = recipeFile.readLine()
- end
- recipeFile.close()
- end
- scanBeans()
- readRecipes()
- local function clearInventory()
- for i=1,16 do
- local n = turtle.getItemCount(i)
- if ae.pullItem("up",i,n) - n ~= 0 then i = i - 1 end
- end
- end
- local function itemInSlot(slot)
- return turtle.getItemCount(slot)
- end
- local function requestBean(name, amount)
- if amount > 64 then return 0 end
- local slot = 1
- while slot < 17 and itemInSlot(slot) > 0 do slot = slot + 1 end
- if itemInSlot(slot) > 0 then print("turtle inventory full while trying to request ", name); return 0 end
- local result = lp.makeRequest(beans[name].lpID, amount)
- if result ~= "DONE" then
- print("Failed to get ", amount, "name: ", result)
- return 0
- else
- return slot
- end
- end
- local function loadFurnace(amount,slot)
- local movedAmount = 0
- local toMove = amount
- repeat
- movedAmount = movedAmount + furnace.pullItem(furnaceDir,slot,toMove)
- if movedAmount then toMove = amount - movedAmount end
- if not movedAmount or toMove > 0 then sleep(0.5) end
- until movedAmount and toMove <= 0
- return movedAmount
- end
- local function loadBean(name, amount)
- local beanInSlot = requestBean(name,amount)
- local movedBeans = 0
- if beanInSlot > 0 then movedBeans = loadFurnace(amount,beanInSlot) end
- --if itemInSlot(beanInSlot) == 0 then
- --wifi.transmit(channel,channel,textutils.serialize(movedBeans))
- --end
- return movedBeans
- end
- local signal = false
- clearInventory()
- sleep(2)
- while true do
- signal = rs.getInput(signalSide)
- if signal then
- os.pullEvent("redstone")
- signal = rs.getInput(signalSide)
- --print("new item in crucible")
- end
- while not signal do
- local item = crucible.getStackInSlot(1)
- if not item then item = {name="nil"} end
- if not recipes[item.name] then
- print("no suitable recipe found for item ",item.name)
- sleep(5)
- else
- --print(item.name," ",#recipes[item.name])
- local nBean = 0
- for i=1,#recipes[item.name] do
- local bean = recipes[item.name][i].type
- local qty = tonumber(recipes[item.name][i].qty)
- local aspects = crucible.getAspects()
- for j=1,#aspects do
- if bean == string.lower(aspects[j].name) then
- qty = qty - aspects[j].quantity
- break
- end
- end
- while qty > 0 and pipe.getSuctionType("in") ~= bean do
- sleep(0.2)
- end
- if qty > 0 and pipe.getSuctionType("in") == bean then
- --print("loading ",qty," ",bean)
- nBean = loadBean(bean,qty)
- if nBean ~= qty then print(nBean," of ",recipes[item.name][i].qty," moved to furnace") end
- if #recipes[item.name] == 1 then
- while #crucible.getAspects() == 0 do
- sleep(0.2)
- end
- while #crucible.getAspects() > 0 do
- sleep(0.2)
- end
- end
- while pipe.getSuctionType("in") == bean do
- if #crucible.getAspects() == 0 then break end
- sleep(0.2)
- end
- sleep(0.2)
- end
- end
- end
- while #crucible.getAspects() > 0 do
- sleep(0.5)
- end
- signal = rs.getInput(signalSide)
- end
- clearInventory()
- end
Advertisement
Add Comment
Please, Sign In to add comment