robocyclone

Turtle Dig Progress

Dec 30th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 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() --Unsure how to go about function
  28.     for i = 1, 16 do
  29.         turtle.select(i)
  30.         if turtle.
  31. end]]
  32.  
  33. function fuel()
  34.     local testSuccess = testForAvailableFuel()
  35.     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
  36.         for i = 1, 16 do
  37.             turtle.select(i)
  38.             if turtle.refuel(0) then
  39.                 turtle.refuel(math.floor(turtle.getItemCount(i)/2))
  40.             end
  41.         end
  42.     end
  43. end
  44.  
  45. function digForward(dirToGo, amount)
  46.     for i = 1, amount do
  47.         if dirToGo == "north" then --If needs to go north..
  48.             y = y + 1
  49.             if direction == "north" then --And is facing north..
  50.                 turtle.forward()
  51.             elseif direction == "south" then --Facing south..
  52.                 turtle.turnLeft()
  53.                 turtle.turnLeft()
  54.                 turtle.forward()
  55.             elseif direction == "west" then
  56.                 turtle.turnRight()
  57.                 turtle.forward()
  58.             elseif direction == "east" then
  59.                 turtle.turnLeft()
  60.                 turtle.forward()
  61.             end
  62.             direction = "north"
  63.         elseif dirToGo == "east" then --If needs to go east..
  64.             x = x + 1
  65.             if direction == "north" then
  66.                 turtle.turnRight()
  67.                 turtle.forward()
  68.             elseif direction == "west" then
  69.                 turtle.turnLeft()
  70.                 turtle.turnLeft()
  71.                 turtle.forward()
  72.             elseif direction == "east" then
  73.                 turtle.forward()
  74.             elseif direction == "south" then
  75.                 turtle.turnLeft()
  76.                 turtle.forward()
  77.             end
  78.             direction = "east"
  79.         elseif dirToGo == "south" then --If needs to go south..
  80.             y = y - 1
  81.             if direction == "south" then
  82.                 turtle.forward()
  83.             elseif direction == "north" then
  84.                 turtle.turnLeft()
  85.                 turtle.turnLeft()
  86.                 turtle.forward()
  87.             elseif direction == "east" then
  88.                 turtle.turnRight()
  89.                 turtle.forward()
  90.             elseif direction == "west" then
  91.                 turtle.turnLeft()
  92.                 turtle.forward()
  93.             end
  94.             direction = "south"
  95.         elseif dirToGo == "west" then --If needs to go west..
  96.             x = x - 1
  97.             if direction == "west" then
  98.                 turtle.forward()
  99.             elseif direction == "east" then
  100.                 turtle.turnLeft()
  101.                 turtle.turnLeft()
  102.                 turtle.forward()
  103.             elseif direction == "south" then
  104.                 turtle.turnRight()
  105.                 turtle.forward()
  106.             elseif direction == "north" then
  107.                 turtle.turnLeft()
  108.                 turtle.forward()
  109.             end
  110.             direction = "west"
  111.         end
  112.         fuel()
  113.     end
  114. end
  115.  
  116.  --[[Turtle needs to dig in a perfect square, digSize x digSize in size.
  117.     To do this, go north digSize, east digSize - 1, south digSize - 1, etc.]]
  118.    
  119. function basicDig()
  120.     if turtle.detectUp() then turtle.digUp() end
  121.     if turtle.detectDown() then turtle.digDown() end
  122.     if turtle.detect() then turtle.dig() end
  123. end
  124.    
  125. local compassRotation = {"north", "east", "south", "west"}
  126.  
  127. function dig(amt) --Dig function; parameter "amt", same as digSize specified in beginning
  128.     local digLeft = amt
  129.     for i = 1, digLeft do
  130.         basicDig()
  131.        
  132. end
  133.  
  134. dig(digSize)
Advertisement
Add Comment
Please, Sign In to add comment