akihex

autoCrucibleMk2

Aug 7th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local args = {...}
  2. local recipes = {}
  3.  
  4. local lp = #args > 0 and peripheral.wrap(args[1]) or peripheral.wrap("right")
  5. if not lp.getTurtleConnect() then lp.setTurtleConnect(true) end
  6.  
  7. local ae = peripheral.wrap("bottom")
  8. local pipe = peripheral.wrap("top")
  9. local furnace = peripheral.wrap("back")
  10. local crucible = peripheral.wrap("front")
  11. local signalSide = #args > 1 and args[2] or "left"
  12. local furnaceDir = #args > 2 and args[3] or "south"
  13.  
  14.  
  15. -- http://lua-users.org/wiki/SplitJoin
  16. local function split(str, pat)
  17.    local t = {}
  18.    local fpat = "(.-)" .. pat
  19.    local last_end = 1
  20.    local s, e, cap = str:find(fpat, 1)
  21.    while s do
  22.       if s ~= 1 or cap ~= "" then
  23.      table.insert(t,cap)
  24.       end
  25.       last_end = e+1
  26.       s, e, cap = str:find(fpat, last_end)
  27.    end
  28.    if last_end <= #str then
  29.       cap = str:sub(last_end)
  30.       table.insert(t, cap)
  31.    end
  32.    return t
  33. end
  34.  
  35. local beans = {}
  36.  
  37. local function scanBeans()
  38.   local allItems = lp.getAvailableItems()
  39.   --print(#allItems, " items in lp system")
  40.   local foundABean = false
  41.   for i,item in pairs(allItems) do
  42.     if lp.getItemID(item[1]) == 25366 then
  43.       foundABean = true
  44.       local nbt = lp.getNBTTagCompound(item[1])
  45.       if nbt then
  46.         local name = nbt.value.Aspects.value[1].value.key.value
  47.         if not beans[name] then beans[name] = {lpID=item[1], qty=item[2]}
  48.         else print("duplicate bean: ",name) end
  49.       end
  50.     end
  51.   end
  52.   if not foundABean then
  53.     sleep(5)
  54.     os.reboot()
  55.   end
  56. end
  57.  
  58. local function readRecipes()
  59.   local recipeFile = fs.open("recipes","r")
  60.   if not recipeFile then return 0 end
  61.   local line = recipeFile.readLine()
  62.   while line do
  63.     local recipe = split(line,",")
  64.     if #recipe % 2 ~= 0 then break end
  65.     recipes[recipe[2]] = {}
  66.     for i=3,#recipe,2 do
  67.       table.insert(recipes[recipe[2]],{type=recipe[i],qty=recipe[i+1]})
  68.     end
  69.     line = recipeFile.readLine()
  70.   end
  71.   recipeFile.close()
  72. end
  73.  
  74. scanBeans()
  75. readRecipes()
  76.  
  77. local function clearInventory()
  78.   for i=1,16 do
  79.     local n = turtle.getItemCount(i)
  80.     if ae.pullItem("up",i,n) - n ~= 0 then i = i - 1 end
  81.   end
  82. end
  83.  
  84. local function itemInSlot(slot)
  85.   return turtle.getItemCount(slot)
  86. end
  87.  
  88. local function requestBean(name, amount)
  89.   if amount > 64 then return 0 end
  90.   local slot = 1
  91.   while slot < 17 and itemInSlot(slot) > 0 do slot = slot + 1 end
  92.   if itemInSlot(slot) > 0 then print("turtle inventory full while trying to request ", name); return 0 end
  93.   local result = lp.makeRequest(beans[name].lpID, amount)
  94.   if result ~= "DONE" then
  95.     print("Failed to get ", amount, "name: ", result)
  96.     return 0
  97.   else
  98.     return slot
  99.   end
  100. end
  101.  
  102. local function loadFurnace(amount,slot)
  103.   local movedAmount = 0
  104.   local toMove = amount
  105.   repeat
  106.     movedAmount = movedAmount + furnace.pullItem(furnaceDir,slot,toMove)
  107.     if movedAmount then toMove = amount - movedAmount end
  108.     if not movedAmount or toMove > 0 then sleep(0.5) end
  109.   until movedAmount and toMove <= 0
  110.   return movedAmount
  111. end
  112.  
  113. local function loadBean(name, amount)
  114.   local beanInSlot = requestBean(name,amount)
  115.   local movedBeans = 0
  116.   if beanInSlot > 0 then movedBeans = loadFurnace(amount,beanInSlot) end
  117.   --if itemInSlot(beanInSlot) == 0 then
  118.     --wifi.transmit(channel,channel,textutils.serialize(movedBeans))
  119.   --end
  120.   return movedBeans
  121. end
  122.  
  123.  
  124. local signal = false
  125. clearInventory()
  126.  
  127. sleep(2)
  128.  
  129. while true do
  130.   signal = rs.getInput(signalSide)
  131.   if signal then
  132.     os.pullEvent("redstone")
  133.     signal = rs.getInput(signalSide)
  134.     --print("new item in crucible")
  135.   end
  136.   while not signal do
  137.     local item = crucible.getStackInSlot(1)
  138.     if not item then item = {name="nil"} end
  139.     if not recipes[item.name] then
  140.       print("no suitable recipe found for item ",item.name)
  141.       sleep(5)
  142.     else
  143.       --print(item.name," ",#recipes[item.name])
  144.       local nBean = 0
  145.       for i=1,#recipes[item.name] do
  146.         local bean = recipes[item.name][i].type
  147.         local qty = tonumber(recipes[item.name][i].qty)
  148.         local aspects = crucible.getAspects()
  149.         for j=1,#aspects do
  150.           if bean == string.lower(aspects[j].name) then
  151.             qty = qty - aspects[j].quantity
  152.             break
  153.           end
  154.         end
  155.         while qty > 0 and pipe.getSuctionType("in") ~= bean do
  156.           sleep(0.2)
  157.         end
  158.         if qty > 0 and pipe.getSuctionType("in") == bean then
  159.           --print("loading ",qty," ",bean)
  160.           nBean = loadBean(bean,qty)
  161.           if nBean ~= qty then print(nBean," of ",recipes[item.name][i].qty," moved to furnace") end
  162.           if #recipes[item.name] == 1 then
  163.             while #crucible.getAspects() == 0 do
  164.               sleep(0.2)
  165.             end
  166.             while #crucible.getAspects() > 0 do
  167.               sleep(0.2)
  168.             end
  169.           end
  170.           while pipe.getSuctionType("in") == bean do
  171.             if #crucible.getAspects() == 0 then break end
  172.             sleep(0.2)
  173.           end
  174.           sleep(0.2)
  175.         end
  176.       end
  177.     end
  178.     while #crucible.getAspects() > 0 do
  179.       sleep(0.5)
  180.     end
  181.     signal = rs.getInput(signalSide)
  182.   end
  183.   clearInventory()
  184. end
Advertisement
Add Comment
Please, Sign In to add comment