Lion4ever

Block Following Turtle

Jul 22nd, 2015
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.84 KB | None | 0 0
  1. local function step(n)
  2.   for i=1,n or 1 do
  3.     while not turtle.forward() do
  4.       if turtle.getFuelLevel() == 0 then
  5.         turtle.refuel()
  6.       elseif turtle.detect() then
  7.         turtle.dig()
  8.       else
  9.         turtle.attack()
  10.       end
  11.     end
  12.   end
  13.   return true
  14. end
  15.  
  16. local function stepUp(n)
  17.   for i=1,n or 1 do
  18.     while not turtle.up() do
  19.       if turtle.getFuelLevel() == 0 then
  20.         turtle.refuel()
  21.       elseif turtle.detectUp() then
  22.         turtle.digUp()
  23.       else
  24.         turtle.attackUp()
  25.       end
  26.     end
  27.   end
  28.   return true
  29. end
  30.  
  31. local function stepDown(n)
  32.   for i=1,n or 1 do
  33.     while not turtle.down() do
  34.       if turtle.getFuelLevel() == 0 then
  35.         turtle.refuel()
  36.       elseif turtle.detectDown() then
  37.         turtle.digDown()
  38.       else
  39.         turtle.attackDown()
  40.       end
  41.     end
  42.   end
  43.   return true
  44. end
  45.  
  46. local containers = {["minecraft:chest"]=true,
  47.                     ["minecraft:dispenser"]=true,
  48.                     ["minecraft:hopper"]=true,
  49.                     ["minecraft:dropper"]=true,
  50.                     ["minecraft:furnace"]=true,
  51.                     ["minecraft:lit_furnace"]=true,
  52.                     ["minecraft:ender_chest"]=true,
  53.                     ["EnderStorage:enderChest"]=true}
  54.  
  55. local inspections = {turtle.inspectUp,turtle.inspect,turtle.inspectDown}
  56. local sideNames = {"top","front","bottom"}
  57.  
  58. local function dropC(n)
  59.   local d,b = turtle.inspectUp()
  60.   if d and containers[b.name] then
  61.     turtle.dropUp(n or 64)
  62.   end
  63.   d,b = turtle.inspectUp()
  64.   if d and containers[b.name] then
  65.     turtle.drop(n or 64)
  66.   end
  67.   d,b = turtle.inspectDown()
  68.   if d and containers[b.name] then
  69.     turtle.dropDown(n or 64)
  70.   end
  71. end
  72.  
  73. local function suckC(n)
  74.   local d,b = turtle.inspectUp()
  75.   if d and containers[b.name] then
  76.     turtle.suckUp(n or 64)
  77.   end
  78.   d,b = turtle.inspect()
  79.   if d and containers[b.name] then
  80.     turtle.suck(n or 64)
  81.   end
  82.   d,b = turtle.inspectDown()
  83.   if d and containers[b.name] then
  84.     turtle.suckDown(n or 64)
  85.   end
  86. end
  87.  
  88. local movingDir = step
  89. local running = true
  90. local repeatSpot = false
  91.  
  92.  
  93. local function fuelFurnace(meta,side)
  94.   if side == "front" then
  95.     turtle.drop(8)
  96.     turtle.turnRight()
  97.     step()
  98.     turtle.turnLeft()
  99.     repeatSpot = true
  100.   end
  101. end
  102.  
  103.  
  104. local actions = {
  105.     ["minecraft:cobblestone"]=turtle.turnLeft,
  106.     ["minecraft:planks"]=turtle.turnRight,
  107.     ["minecraft:wool"]=function(color)
  108.       turtle.select(color+1)
  109.     end,
  110.     ["minecraft:log"] = function()
  111.       local h=0
  112.       while turtle.detect() do
  113.         turtle.dig()
  114.         turtle.digUp()
  115.         stepUp()
  116.         h = h + 1
  117.       end
  118.       stepDown(h)
  119.       step(2)
  120.       local s = turtle.getSelectedSlot()
  121.       for i=1,16 do
  122.         if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name =="minecraft:sapling" then
  123.           turtle.turnLeft()
  124.           turtle.turnLeft()
  125.           turtle.select(i)
  126.           turtle.place()
  127.           turtle.turnLeft()
  128.           turtle.turnLeft()
  129.           break
  130.         end
  131.       end
  132.       repeatSpot = true
  133.       turtle.select(s)
  134.     end,
  135.     ["minecraft:wooden_button"]=function()
  136.       local s = turtle.getSelectedSlot()
  137.         for i=1,16 do
  138.           if turtle.getItemCount(i) > 0 then
  139.             turtle.select(i)
  140.             dropC()
  141.           end
  142.         end
  143.       turtle.select(s)
  144.     end,   
  145.     ["minecraft:sandstone"]=function(meta)
  146.       if meta == 0 then
  147.         local s = turtle.getSelectedSlot()
  148.         for i=1,16 do
  149.           if turtle.getItemCount(i) == 0 then
  150.             turtle.select(i)
  151.             break
  152.           end
  153.         end
  154.         suckC(1)
  155.         local tn = turtle.getItemDetail() and turtle.getItemDetail().name
  156.         dropC(1)
  157.         for i=1,16 do
  158.           if turtle.getItemCount(i) > 0 and turtle.getItemDetail(i).name == tn then
  159.             turtle.select(i)
  160.             dropC()
  161.           end
  162.         end
  163.         turtle.select(s)
  164.       elseif meta == 1 then
  165.         dropC(1)
  166.       else
  167.         dropC()
  168.       end
  169.     end,   
  170.     ["minecraft:stonebrick"]=function(meta)
  171.       if meta == 0 then
  172.         local s = turtle.getSelectedSlot()
  173.         for i=1,16 do
  174.           if turtle.getItemCount(i) > 0 then
  175.             turtle.select(i)
  176.             suckC()
  177.           end
  178.         end
  179.         turtle.select(s)
  180.       elseif meta == 3 then
  181.         suckC(1)
  182.       else
  183.         suckC()
  184.       end
  185.     end,
  186.     ["minecraft:crafting_table"]= function() turtle.craft() end,
  187.     ["minecraft:wooden_slab"]=function() sleep(10) end,
  188.     ["minecraft:double_wooden_slab"]=function() sleep(300) end,
  189.     ["minecraft:glass"]=function()
  190.       if movingDir == step then
  191.         if not turtle.detectDown() then
  192.           movingDir = stepDown
  193.         elseif not turtle.detectUp() then
  194.           movingDir = stepUp
  195.         end
  196.       else
  197.         movingDir = step
  198.       end
  199.     end,
  200.     ["minecraft:fence"]=function()
  201.       if turtle.detect() then
  202.         turtle.dig()
  203.       end
  204.       turtle.place()
  205.     end,
  206.     ["minecraft:cobblestone_wall"]=function()
  207.       if turtle.detectDown() then
  208.         turtle.digDown()
  209.       end
  210.       turtle.placeDown()
  211.     end,
  212.     ["minecraft:stone_slab"]=function(meta)
  213.       if meta >=8 then
  214.         turtle.select((turtle.getSelectedSlot()+14) % 16 + 1)
  215.       else
  216.         turtle.select(turtle.getSelectedSlot() % 16 + 1)
  217.       end
  218.     end,
  219.     ["minecraft:redstone_block"]=function()
  220.       if turtle.getItemCount() == 0 then
  221.         movingDir()
  222.       end
  223.     end,
  224.     ["minecraft:noteblock"]=function(n,side)
  225.         rs.setOutput(side,true)
  226.         sleep(0.2)
  227.         rs.setOutput(side,false)
  228.         sleep(0.2)
  229.     end,
  230.     ["minecraft:stone_stairs"]=function()
  231.       if turtle.getItemSpace(16) > 0 then
  232.         turtle.dig()
  233.         repeatSpot = true
  234.       end
  235.     end,
  236.     ["minecraft:coal_block"]=function()
  237.       turtle.refuel()
  238.     end,
  239.     ["minecraft:stained_glass"]=function(meta)
  240.       turtle.transferTo(meta+1,1)
  241.     end,
  242.     ["minecraft:stained_glass_pane"]=function(meta)
  243.       turtle.transferTo(meta+1)
  244.     end,
  245.     ["minecraft:furnace"]=fuelFurnace,
  246.     ["minecraft:lit_furnace"]=fuelFurnace,
  247. }
  248.  
  249. while running do
  250.   for side,inspection in ipairs(inspections) do
  251.     local detect,blockTable = inspection()
  252.     if detect and actions[blockTable.name] then
  253.       actions[blockTable.name](blockTable.metadata,sideNames[side])
  254.       if repeatSpot then
  255.         break
  256.       end
  257.     end
  258.   end
  259.   if not repeatSpot then
  260.     movingDir()
  261.   end
  262.   repeatSpot = false
  263. end
Advertisement
Add Comment
Please, Sign In to add comment