bcash8

Crafting Turtle 1.0

Mar 16th, 2021 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.91 KB | None | 0 0
  1. local recipes = {}
  2. local rSlots = {1,2,3,5,6,7,9,10,11}
  3. local hSlots = {4,8,12,13,14,15}
  4. local chest = {input = "up", trash = "front", final = "down"}
  5. local pause = false
  6.  
  7. function init()
  8.      if fs.exists("recipes") then
  9.           f = fs.open("recipes","r")
  10.           recipies = textutils.unserialize(f.readAll())
  11.           f.close()
  12.      else
  13.           f = fs.open("recipes","w")
  14.           f.write(textutils.serialize(recipes))
  15.           f.close()
  16.      end
  17. end
  18.  
  19. function wl(str,posW,posH)
  20.      local l = math.floor(string.len(str)/2)
  21.      term.setCursorPos(posW-l,posH)
  22.      term.write(str)
  23. end
  24.  
  25. function save(path,data)
  26.      f = fs.open(path,"w")
  27.      f.write(textutils.serialize(data))
  28.      f.close()
  29. end
  30.  
  31. function dropItems(place,c)
  32.      if not c then c = 64 end
  33.      if place == "up" then
  34.           turtle.dropUp(c)
  35.      elseif place == "down" then
  36.           turtle.dropDown(c)
  37.      elseif place == "front" then
  38.           turtle.drop(c)
  39.      end
  40. end
  41.  
  42. function suckItems(place,c)
  43.      if not c then c = 64 end
  44.      if place == "up" then
  45.           turtle.suckUp(c)
  46.      elseif place == "down" then
  47.           turtle.suckDown(c)
  48.      elseif place == "front" then
  49.           turtle.suck(c)
  50.      end
  51. end
  52.  
  53. function distribute()
  54.      local slot = turtle.getSelectedSlot()
  55.      local data = turtle.getItemDetail(slot)
  56.      if data then
  57.           local required = {}
  58.           local c = data.count
  59.           for k,v in pairs(recipes[1].grid) do
  60.                if v == data.name then
  61.                     table.insert(required, {k,turtle.getItemCount(k)})
  62.                end
  63.           end
  64.  
  65.           table.sort(required, function(a, b) return a[2] < b[2] end)
  66.          
  67.           for k,v in pairs(required) do
  68.                required[k][2] = required[#required][2] - required[k][2]
  69.                c = c - required[k][2]
  70.           end
  71.          
  72.           if c > 0 then
  73.                if c % #required ~= 0 then
  74.                     dropItems(chest.input,c % #required)
  75.                end
  76.  
  77.                 for k,v in pairs(required) do
  78.                      required[k][2] = math.floor(required[k][2] + (c/#required))
  79.                 end
  80.           end
  81.          
  82.           for k,v in pairs(required) do
  83.                turtle.select(slot)
  84.                turtle.transferTo(required[k][1],required[k][2])
  85.           end
  86.      end
  87.      if turtle.getItemCount(slot) > 0 then
  88.           turtle.select(slot)
  89.           dropItems(chest.trash)
  90.      end
  91. end
  92.  
  93. function checkForItems()
  94.      turtle.select(16)
  95.      suckItems(chest.input)
  96.      if turtle.getItemCount(16) > 0 then
  97.           distribute()
  98.           return true
  99.      else
  100.           sleep(delay)
  101.           return false
  102.      end
  103. end
  104.  
  105. function checkRecipe()
  106.      local good = true
  107.      for k,v in pairs(recipes[1]) do
  108.           local data = turtle.getItemDetail(k)
  109.           if data then
  110.                if data.name ~= v and data.count > 0 then
  111.                     turtle.select(k)
  112.                     dropItems(chest.input)
  113.                     good = false
  114.                end
  115.           else
  116.                good = false
  117.           end
  118.      end
  119.      return good
  120. end
  121.  
  122. function attemptCraft()
  123.      for k,v in pairs(hSlots) do
  124.           if turtle.getItemCount(v) > 0 then
  125.                turtle.select(v)
  126.                dropItems(chest.input)
  127.           end
  128.      end
  129.  
  130.      turtle.select(16)
  131.      if turtle.craft() then
  132.           dropItems(chest.final)
  133.           if checkRecipe() then
  134.                attemptCraft()
  135.           end
  136.           return true
  137.      end
  138. end
  139.  
  140. function newRecipe()
  141. while true do
  142.      term.clear()
  143.      term.setCursorPos(10,14)
  144.      term.write("Press [TAB] To exit")
  145.      term.setCursorPos(1,1)
  146.      term.write("Press [SPACE] when ready")
  147.      e,key = os.pullEvent("key")
  148.      if key == keys.space then
  149.           local tmpRecipe = {}
  150.           tmpRecipe.grid = {}
  151.           for k,v in pairs(rSlots) do
  152.                local data = turtle.getItemDetail(v)
  153.                if data then
  154.                     tmpRecipe.grid[v] = data.name
  155.                end
  156.           end
  157.  
  158.           turtle.select(16)
  159.           if turtle.craft() then
  160.                local data = turtle.getItemDetail(16)
  161.                tmpRecipe.name = data.name
  162.                print("SUCCESS")
  163.                for k,v in pairs(tmpRecipe.grid) do
  164.                     print("[" .. k .. "] =" .. v)
  165.                end
  166.                print(tmpRecipe.name)
  167.                print("Press [ENTER] to save")
  168.                print("or press any other key to not save")
  169.                e,key = os.pullEvent("key")
  170.                if key == keys.enter then
  171.                     table.insert(recipes,1,tmpRecipe)
  172.                     save("recipes", recipes)
  173.                     print("Saved")
  174.                     sleep(3)
  175.                     return
  176.                else
  177.                     print("Not Saved")
  178.                     sleep(3)
  179.                end
  180.           else
  181.                print("INVALID RECIPE")
  182.                sleep(3)
  183.           end
  184.      elseif key == keys.tab then
  185.           return
  186.      end    
  187. end
  188. end
  189.  
  190. function main()
  191.      while true do
  192.           term.clear()
  193.           wl("---CRAFT BOT BOI---", 20,1)
  194.           wl("Press [ENTER] to add new recipe",20,7)
  195.           if recipes[1] then
  196.                wl(recipes[1].name,20,3)
  197.                if checkRecipe() then
  198.                     attemptCraft()
  199.                else
  200.                     if not pause then
  201.                          checkForItems()
  202.                     else
  203.                          newRecipe()
  204.                          pause = false
  205.                     end
  206.                end
  207.           else
  208.                newRecipe()
  209.           end
  210.      end
  211. end
  212.  
  213. function waitForKey()
  214.      while true do
  215.           e,key = os.pullEvent("key")
  216.           if key == keys.enter then
  217.                pause = true
  218.           end
  219.      end
  220. end
  221.  
  222. init()
  223. parallel.waitForAll(main,waitForKey)
Add Comment
Please, Sign In to add comment