robocyclone

Untitled

Feb 18th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. --Declare nescessary variables
  2.  
  3. local x, y = 0,0
  4. local direction = "north"
  5. local homeCoordX, homeCoordY = 0, 0
  6.  
  7. local digSize = ...
  8.  
  9. local success, idtable = turtle.inspectDown()
  10.  
  11. if success and idtable.name == "minecraft:chest" then --Make sure there is a chest at Home
  12.     print("Success")
  13. else
  14.     error("Need chest below turtle to start digging!")
  15. end
  16.  
  17. function testForAvailableFuel()
  18.     for i = 1, 16 do
  19.         turtle.select(i)
  20.         if turtle.refuel(0) then --If the item in slot i is fuel, then return true but don't use it.
  21.             return true
  22.         end
  23.     end
  24.     return false
  25. end
  26.  
  27. function testForInvSpace()
  28.     local invTake = 0
  29.     for i = 1, 16 do
  30.         if turtle.getItemDetail(i) then
  31.             invTake = invTake + 1
  32.         end
  33.     end
  34.     if invTake <= 8 then
  35.         return false
  36.     else
  37.         return true
  38.     end
  39. end
  40.  
  41. function goHomeAndBack()
  42.     local xDig = x
  43.     local yDig = y
  44.     fuel()
  45.     if xDig > 0 then moveForward("west", (0 - xDig)) elseif xDig < 0 then moveForward("east", math.abs(xDig)) end
  46.     if yDig > 0 then moveForward("south", (0 - yDig)) elseif yDig < 0 then moveForward("north", math.abs(yDig)) end
  47.     local success2, idtable2 = turtle.inspectDown()
  48.     if success2 and idtable2.name == "minecraft:chest" then
  49.         for i = 1, 16 do
  50.             turtle.select(i)
  51.             turtle.dropDown()
  52.         end
  53.     end
  54.     if xDig > 0 then moveForward("east", xDig) elseif xDig < 0 then moveForward("west", (0 - xDig)) end
  55.     if yDig > 0 then moveForward("north", yDig) elseif yDig < 0 then moveForward("south", (0 - yDig)) end
  56. end
  57.  
  58. function fuel()
  59.     local testSuccess = testForAvailableFuel()
  60.     if turtle.getFuelLevel() <= math.floor(turtle.getFuelLimit()/2) and testSuccess == true then --If the turtle's fuel is less than half the limit AND there is fuel available
  61.         for i = 1, 16 do
  62.             turtle.select(i)
  63.             if turtle.refuel(0) then
  64.                 turtle.refuel(math.floor(turtle.getItemCount(i)/2))
  65.             end
  66.         end
  67.     else return "No available fuel or too much fuel"
  68.     end
  69. end
  70.  
  71. function basicDig()
  72.     if turtle.detectUp() then turtle.digUp() end
  73.     if turtle.detectDown() then turtle.digDown() end
  74.     if turtle.detect() then turtle.dig() end
  75. end
  76.  
  77. function moveForward(dirToGo, amount)
  78.     if dirToGo ~= "north" or dirToGo ~= "south" or dirToGo ~= "east" or dirToGo ~= "west" or amount == 0 then return end
  79.     for i = 1, amount do
  80.         if dirToGo == "north" then --If needs to go north..
  81.             y = y + 1
  82.             if direction == "north" then --And is facing north..
  83.                 turtle.forward()
  84.             elseif direction == "south" then --Facing south..
  85.                 turtle.turnLeft()
  86.                 turtle.turnLeft()
  87.                 turtle.forward()
  88.             elseif direction == "west" then
  89.                 turtle.turnRight()
  90.                 turtle.forward()
  91.             elseif direction == "east" then
  92.                 turtle.turnLeft()
  93.                 turtle.forward()
  94.             end
  95.             direction = "north"
  96.         elseif dirToGo == "east" then --If needs to go east..
  97.             x = x + 1
  98.             if direction == "north" then
  99.                 turtle.turnRight()
  100.                 turtle.forward()
  101.             elseif direction == "west" then
  102.                 turtle.turnLeft()
  103.                 turtle.turnLeft()
  104.                 turtle.forward()
  105.             elseif direction == "east" then
  106.                 turtle.forward()
  107.             elseif direction == "south" then
  108.                 turtle.turnLeft()
  109.                 turtle.forward()
  110.             end
  111.             direction = "east"
  112.         elseif dirToGo == "south" then --If needs to go south..
  113.             y = y - 1
  114.             if direction == "south" then
  115.                 turtle.forward()
  116.             elseif direction == "north" then
  117.                 turtle.turnLeft()
  118.                 turtle.turnLeft()
  119.                 turtle.forward()
  120.             elseif direction == "east" then
  121.                 turtle.turnRight()
  122.                 turtle.forward()
  123.             elseif direction == "west" then
  124.                 turtle.turnLeft()
  125.                 turtle.forward()
  126.             end
  127.             direction = "south"
  128.         elseif dirToGo == "west" then --If needs to go west..
  129.             x = x - 1
  130.             if direction == "west" then
  131.                 turtle.forward()
  132.             elseif direction == "east" then
  133.                 turtle.turnLeft()
  134.                 turtle.turnLeft()
  135.                 turtle.forward()
  136.             elseif direction == "south" then
  137.                 turtle.turnRight()
  138.                 turtle.forward()
  139.             elseif direction == "north" then
  140.                 turtle.turnLeft()
  141.                 turtle.forward()
  142.             end
  143.             direction = "west"
  144.         end
  145.         fuel()
  146.     end
  147. end
  148.    
  149. local compassRotation = {"north", "east", "south", "west"}
  150.  
  151. function turtleTurn(degAmt, startFace) --Use only in emergencies. Not integrated into coordinate plane
  152.     local sNum = startFace
  153.     if degAmt == 90 then
  154.         turtle.turnRight()
  155.         direction = compassRotation[startFace + 1]
  156.     elseif degAmt == 180 then
  157.         turtle.turnRight()
  158.         turtle.turnRight()
  159.         direction = compassRotation[startFace + 2]
  160.     elseif degAmt == 270 then
  161.         turtle.turnLeft()
  162.     end
  163. end
  164.  
  165. function switch(num)
  166.     if num ~= 4 then return num + 1 end
  167.     if num == 4 then return 1 end
  168. end
  169.  
  170. function dig(amt) --Dig function; parameter "amt", same as digSize specified in beginning
  171.  
  172. end
  173.  
  174. dig(digSize)
Advertisement
Add Comment
Please, Sign In to add comment