Advertisement
gravitowl

turtle superiority

Dec 23rd, 2022 (edited)
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.05 KB | None | 0 0
  1. local recipes = {
  2.   ["computercraft:mining_crafty_turtle"] = {
  3.     ["type"] = "crafting",
  4.     ["recipe"] = {
  5.       "minecraft:diamond_pickaxe", "computercraft:turtle_normal", "minecraft:crafting_table",
  6.       nil, nil, nil,
  7.       nil, nil, nil
  8.     }
  9.   },
  10.   ["computercraft:turtle_normal"] = {
  11.     ["type"] = "crafting",
  12.     ["recipe"] = {
  13.       "minecraft:iron_ingot", "minecraft:iron_ingot", "minecraft:iron_ingot",
  14.       "minecraft:iron_ingot", "computercraft:computer_normal", "minecraft:iron_ingot",
  15.       "minecraft:iron_ingot", "minecraft:chest", "minecraft:iron_ingot"
  16.     }
  17.   },
  18.   ["computercraft:computer_normal"] = {
  19.     ["type"] = "crafting",
  20.     ["recipe"] = {
  21.       "#c:stones", "#c:stones", "#c:stones",
  22.       "#c:stones", "minecraft:redstone", "#c:stones",
  23.       "#c:stones", "minecraft:glass_pane", "#c:stones"
  24.     }
  25.   },
  26.   ["computercraft:disk_drive"] = {
  27.     ["type"] = "crafting",
  28.     ["recipe"] = {
  29.       "#c:stones", "#c:stones", "#c:stones",
  30.       "#c:stones", "minecraft:redstone", "#c:stones",
  31.       "#c:stones", "minecraft:redstone", "#c:stones"
  32.     }
  33.   },
  34.   ["minecraft:glass_pane"] = {
  35.     ["type"] = "crafting",
  36.     ["recipe"] = {
  37.       "minecraft:glass","minecraft:glass", "minecraft:glass",
  38.       "minecraft:glass","minecraft:glass", "minecraft:glass",
  39.       nil, nil, nil
  40.     }
  41.   },
  42.   ["minecraft:chest"] = {
  43.     ["type"] = "crafting",
  44.     ["recipe"] = {
  45.       "#minecraft:planks", "#minecraft:planks", "#minecraft:planks",
  46.       "#minecraft:planks", nil, "#minecraft:planks",
  47.       "#minecraft:planks", "#minecraft:planks", "#minecraft:planks"
  48.     }
  49.   },
  50.   ["#minecraft:planks "] = {
  51.     ["type"] = "crafting",
  52.     ["recipe"] = {
  53.       "#minecraft:logs", nil, nil,
  54.       nil, nil, nil,
  55.       nil, nil, nil
  56.     }
  57.   },
  58.   ["minecraft:diamond_pickaxe"] = {
  59.     ["type"] = "crafting",
  60.     ["recipe"] = {
  61.       "minecraft:diamond", "minecraft:diamond", "minecraft:diamond",
  62.       nil, "minecraft:stick", nil,
  63.       nil, "minecraft:stick", nil
  64.     }
  65.   },
  66.   ["minecraft:stick"] = {
  67.     ["type"] = "crafting",
  68.     ["recipe"] = {
  69.       "#minecraft:planks", nil, nil,
  70.       "#minecraft:planks", nil, nil,
  71.       nil, nil, nil
  72.     }
  73.   },
  74.   ["minecraft:iron_ingot"] = {
  75.     ["type"] = "smelting",
  76.     ["recipe"] = {
  77.       "#minecraft:iron_ores"
  78.     }
  79.   },
  80.   ["minecraft:glass"] = {
  81.     ["type"] = "smelting",
  82.     ["recipe"] = {
  83.       "minecraft:sand"
  84.     }
  85.   },
  86.   ["minecraft:stone"] = {
  87.     ["type"] = "smelting",
  88.     ["recipe"] = {
  89.       "minecraft:cobblestone"
  90.     }
  91.   },
  92.   ["minecraft:smooth_stone"] = {
  93.     ["type"] = "smelting",
  94.     ["recipe"] = {
  95.       "minecraft:stone"
  96.     }
  97.   }
  98. }
  99.  
  100. local INVENTORY_SPACES = 16
  101.  
  102. local CRAFTING_SLOTS = {
  103.   1, 2, 3,
  104.   5, 6, 7,
  105.   9, 10, 11
  106. }
  107.  
  108. local function hasValue( tbl, val )
  109.   for index, value in ipairs(tbl) do
  110.       if value == val then
  111.         return true
  112.       end
  113.   end
  114.  
  115.   return false
  116. end
  117.  
  118. function string.starts(String,Start)
  119.   return string.sub(String,1,string.len(Start))==Start
  120. end
  121.  
  122. local function printTable(tbl)
  123.   for key, value in pairs(tbl) do
  124.     print(key.. ": " ..value)
  125.   end
  126. end
  127.  
  128. local function findItem(item, amount)
  129.   if(item == nil) then return false end
  130.   local name = item
  131.   local tag = false
  132.   if(string.starts(item, "#")) then
  133.     name = string.sub(name, 2)
  134.     tag = true
  135.   end
  136.  
  137.   for i = 1, 16, 1 do
  138.     local curItem = turtle.getItemDetail(i, true)
  139.  
  140.     if curItem ~= nil then
  141.       if (tag and curItem.tags[name] and (amount ~= nil and curItem.count ~= nil and curItem.count >= amount )) then
  142.         return i
  143.       elseif (curItem.name == name and (amount ~= nil and curItem.count ~= nil and curItem.count >= amount )) then
  144.         return i
  145.       end
  146.     end
  147.   end
  148.  
  149.   return false
  150. end
  151.  
  152. local function cleanInventory()
  153.   for i = 1, INVENTORY_SPACES, 1 do
  154.     local item = turtle.getItemDetail(i)
  155.     if item == nil then
  156.       for j = i+1, INVENTORY_SPACES, 1 do
  157.         local curItem = turtle.getItemDetail(j)
  158.         if curItem ~= nil then
  159.           turtle.select(j)
  160.           turtle.transferTo(i)
  161.           break
  162.         end
  163.       end
  164.     end
  165.  
  166.     item = turtle.getItemDetail(i)
  167.     if item == nil then
  168.       break
  169.     end
  170.     for j = i+1, INVENTORY_SPACES, 1 do
  171.       local curItem = turtle.getItemDetail(j)
  172.       if curItem ~= nil and curItem.name == item.name then
  173.         turtle.select(j)
  174.         turtle.transferTo(i)
  175.       end
  176.     end
  177.   end
  178. end
  179.  
  180. local function placeAndDestroy(dir)
  181.   if(dir == "front") then
  182.     while not turtle.place() do
  183.       turtle.dig()
  184.     end
  185.   elseif (dir == "up") then
  186.     while not turtle.placeUp() do
  187.       turtle.digUp()
  188.     end
  189.   elseif (dir == "down") then
  190.     while not turtle.placeDown() do
  191.       turtle.digDown()
  192.     end
  193.   end
  194. end
  195.  
  196. local function moveAndDestroy(dir)
  197.  
  198.   if(turtle.getFuelLevel() < 1) then return false end
  199.   if(dir == "front") then
  200.     while not turtle.forward() do
  201.       turtle.dig()
  202.     end
  203.   elseif (dir == "up") then
  204.     while not turtle.up() do
  205.       turtle.digUp()
  206.     end
  207.   elseif (dir == "down") then
  208.     while not turtle.down() do
  209.       turtle.digDown()
  210.     end
  211.   end
  212.   return true
  213. end
  214.  
  215. local function smelting(item, amount, makeItem)
  216.   if(turtle.getFuelLevel() < 4) then return false end
  217.   local itemIndex = findItem(recipes[item].recipe[1], amount)
  218.   if(itemIndex == false) then
  219.     if(recipes[recipes[item].recipe[1]] ~= nil) then
  220.       local itemName = recipes[item].recipe[1]
  221.       local succes = makeItem(itemName, amount)
  222.       if(not succes) then return false end
  223.  
  224.       itemIndex = findItem(recipes[item].recipe[1], amount)
  225.     else
  226.       return false
  227.     end
  228.   end
  229.  
  230.   local coalIndex = findItem("minecraft:coal", math.ceil(amount/8))
  231.   if(coalIndex == false) then return false end
  232.  
  233.   local block, data = turtle.inspect()
  234.   if (not (block and data.name == "minecraft:furnace")) then
  235.     local furnaceIndex = findItem("minecraft:furnace", 1)
  236.     if(furnaceIndex == false) then return false end
  237.  
  238.     turtle.select(furnaceIndex)
  239.    
  240.     placeAndDestroy("front")
  241.   end
  242.  
  243.   turtle.select(coalIndex)
  244.   turtle.drop(math.ceil(amount/8))
  245.  
  246.   moveAndDestroy("up")
  247.   moveAndDestroy("front")
  248.  
  249.   turtle.select(itemIndex)
  250.   turtle.dropDown(amount)
  251.  
  252.   sleep(amount*10)
  253.  
  254.   turtle.turnLeft()
  255.   turtle.turnLeft()
  256.   moveAndDestroy("front")
  257.   moveAndDestroy("down")
  258.   turtle.turnRight()
  259.   turtle.turnRight()
  260.   turtle.dig()
  261.  
  262.   return true
  263. end
  264.  
  265. local function crafting(item, amount, makeItem)
  266.   cleanInventory()
  267.   local chestIndex = findItem("minecraft:chest", 1)
  268.  
  269.   if(chestIndex == false) then return false end
  270.   turtle.select(chestIndex)
  271.   placeAndDestroy("front")
  272.  
  273.   local requiredItems = {}
  274.   for i = 1, 9, 1 do
  275.     if recipes[item].recipe[i] ~=nil then
  276.       if requiredItems[recipes[item].recipe[i]] == nil then
  277.         requiredItems[recipes[item].recipe[i]] = amount
  278.       else
  279.         requiredItems[recipes[item].recipe[i]] = requiredItems[recipes[item].recipe[i]] + amount
  280.       end
  281.     end
  282.   end
  283.  
  284.   for key, value in pairs(requiredItems) do
  285.     local itemSlot = findItem(key, value)
  286.     if itemSlot == false then
  287.       if recipes[key] ~= nil then
  288.         local succes = makeItem(key, amount)
  289.         if not succes then return false end
  290.       else
  291.         return false
  292.       end
  293.     end
  294.  
  295.     turtle.select(itemSlot)
  296.     turtle.drop(value)
  297.   end
  298.  
  299.   for i = 1, INVENTORY_SPACES, 1 do
  300.     turtle.select(i)
  301.     turtle.drop()
  302.   end
  303.  
  304.   for key, value in pairs(requiredItems) do
  305.     turtle.suck(value)
  306.   end
  307.  
  308.   -- turtle.dig()
  309.  
  310.   return true
  311. end
  312.  
  313. local function makeItem(item, amount)
  314.   if(recipes[item].type == "crafting") then
  315.     local succes = crafting(item, amount, makeItem)
  316.     if(not succes) then
  317.       print("Crafting of " ..item.. " failed.")
  318.       return false
  319.     end
  320.   elseif (recipes[item].type == "smelting") then
  321.     local succes = smelting(item, amount, makeItem)
  322.     if(not succes) then
  323.       print("Crafting of " ..item.. " failed.")
  324.       return false
  325.     end
  326.   end
  327. --  turtle.dig()
  328.   return true
  329. end
  330.  
  331. makeItem("minecraft:diamond_pickaxe", 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement