Advertisement
Descaii

RunBot

Jan 26th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.17 KB | None | 0 0
  1. if turtle.getFuelLevel()==0 then
  2.     turtle.refuel(20)
  3. end
  4. MoveCount = 0
  5. Kill = false
  6. PosOffset = {x=0,y=0,z=0}
  7. Forward = {x=1,y=0,z=0}
  8. function shift(direction)
  9.     if direction=="right" then
  10.         print("shifting direction right")
  11.         if Forward.x==1 then
  12.             Forward.x=0
  13.             Forward.z=1
  14.         elseif Forward.z==1 then
  15.             Forward.x=-1
  16.             Forward.z=0
  17.         elseif Forward.x==-1 then
  18.             Forward.x=0
  19.             Forward.z=-1
  20.         elseif Forward.z==-1 then
  21.             Forward.z=0
  22.             Forward.x=1
  23.         end
  24.     end
  25.     if direction=="left" then
  26.         print("shifting direction left")
  27.         if Forward.x==1 then
  28.             Forward.x=0
  29.             Forward.z=-1
  30.         elseif Forward.z==-1 then
  31.             Forward.x=-1
  32.             Forward.z=0
  33.         elseif Forward.x==-1 then
  34.             Forward.x=0
  35.             Forward.z=1
  36.         elseif Forward.z==1 then
  37.             Forward.z=0
  38.             Forward.x=1
  39.         end
  40.     end
  41. end
  42. function GetDirectionFromForward()
  43.     if Forward.x==1 then
  44.         return "forward"
  45.     elseif Forward.x==-1 then
  46.         return "backwards"
  47.     elseif Forward.z==1 then
  48.         return "right"
  49.     elseif Forward.z==-1 then
  50.         return "left"
  51.     end
  52. end
  53. function base(num)
  54.     if num>0 then
  55.         return 1
  56.     elseif num<0 then
  57.         return -1
  58.     else
  59.         return 0
  60.     end
  61. end
  62. function SetDirection(direction)
  63.     if GetDirectionFromForward()~=direction:lower() then
  64.         repeat
  65.             turtle.turnRight()
  66.             shift("right")
  67.         until
  68.             GetDirectionFromForward()==direction:lower()
  69.     end
  70. end
  71. function ShiftOffsetForward()
  72.     PosOffset.x=PosOffset.x+Forward.x
  73.     PosOffset.y=PosOffset.y+Forward.y
  74.     PosOffset.z=PosOffset.z+Forward.z
  75. end
  76. function isore(...)
  77.     local tb = {...}
  78.     if tb and #tb>0 then
  79.         if pcall(function() return tb[2] end) and tb[2].name then
  80.             return tb[2].name:find("ore")~=nil
  81.         end
  82.     end
  83. end
  84. function isoff()
  85.     if PosOffset.x~=0 or PosOffset.y~=0 or PosOffset.z~=0 then
  86.         return true
  87.     end
  88. end
  89. function returnTunnel()
  90.     print("returning to tunnel")
  91.     if PosOffset.x~=0 then
  92.         print('adjusting x')
  93.         repeat
  94.             print("adjusting x to "..(-base(PosOffset.x))..", current = "..Forward.x..","..Forward.z)
  95.             shift("right")
  96.             turtle.turnRight()
  97.         until Forward.x==-base(PosOffset.x)
  98.         repeat
  99.             if turtle.detect() then turtle.dig() end
  100.             turtle.forward()
  101.             local offori = math.abs(PosOffset.x)
  102.             ShiftOffsetForward()
  103.             if math.abs(PosOffset.x) > offori then
  104.                 turtle.turnRight()
  105.                 turtle.turnRight()
  106.                 shift("right")
  107.                 shift("right")
  108.             end
  109.         until PosOffset.x==0
  110.     end
  111.     if PosOffset.z~=0 then
  112.         print('adjusting z : '..Forward.z)
  113.         repeat
  114.             print("adjusting z to "..(-base(PosOffset.z))..", current = "..Forward.x..","..Forward.z)
  115.             shift("right")
  116.             turtle.turnRight()
  117.         until Forward.z==-base(PosOffset.z)
  118.         repeat
  119.             if turtle.detect() then turtle.dig() end
  120.             turtle.forward()
  121.             local offori = math.abs(PosOffset.z)
  122.             ShiftOffsetForward()
  123.             if math.abs(PosOffset.z) > offori then
  124.                 turtle.turnRight()
  125.                 turtle.turnRight()
  126.                 shift("right")
  127.                 shift("right")
  128.             end
  129.         until PosOffset.z==0
  130.     end
  131.     if PosOffset.y~=0 then
  132.         print('adjusting y')
  133.         if PosOffset.y>0 then
  134.             repeat
  135.                 if turtle.detectDown() then turtle.digDown() end
  136.                 turtle.down()
  137.                 PosOffset.y=PosOffset.y-1
  138.             until PosOffset.y==0
  139.         end
  140.         if PosOffset.y<0 then
  141.             repeat
  142.                 if turtle.detectUp() then turtle.digUp() end
  143.                 turtle.up()
  144.                 PosOffset.y=PosOffset.y+1
  145.             until PosOffset.y==0
  146.         end
  147.     end
  148.     SetDirection("Forward")
  149.     check(true)
  150. end
  151. function check(turn)
  152.     print("checking for ore")
  153.     local cont = true
  154.     local incheck = function()
  155.         if cont and turtle.detectUp() then
  156.             if isore(turtle.inspectUp()) then
  157.                 print("FoundUp")
  158.                 turtle.digUp()
  159.                 turtle.up()
  160.                 PosOffset.y = PosOffset.y+1
  161.                 check(true)
  162.             end
  163.         end
  164.         if cont and turtle.detectDown() then
  165.             if isore(turtle.inspectDown()) then
  166.                 print("FoundDown")
  167.                 turtle.digDown()
  168.                 turtle.down()
  169.                 PosOffset.y = PosOffset.y-1
  170.                 check(true)
  171.             end
  172.         end
  173.     end
  174.     incheck()
  175.     if cont and turn then
  176.         print("Turning")
  177.         turtle.turnRight()
  178.         shift("right")
  179.         if cont and turtle.detect() then
  180.             if isore(turtle.inspect()) then
  181.                 turtle.dig()
  182.                 turtle.forward()
  183.                 ShiftOffsetForward()
  184.                 cont = false
  185.                 check(true)
  186.             end
  187.         end
  188.         if cont then
  189.             turtle.turnLeft();turtle.turnLeft()
  190.             shift("left");shift("left")
  191.             if isore(turtle.inspect()) then
  192.                 turtle.dig()
  193.                 turtle.forward()
  194.                 ShiftOffsetForward()
  195.                 check(true)
  196.             end
  197.         end
  198.     end
  199.     if cont and PosOffset.x~=0 or PosOffset.y ~= 0 or PosOffset.z~=0 then
  200.         returnTunnel()
  201.     end
  202.     SetDirection("forward")
  203. end
  204. function mine()
  205.     table.foreach(Forward,print)
  206.     if turtle.getFuelLevel()>0 then
  207.         print("Continuing Tunnel")
  208.         check()
  209.         if turtle.detect() then
  210.             turtle.dig()
  211.             turtle.forward()
  212.             MoveCount = MoveCount+1
  213.         else
  214.             repeat
  215.                 turtle.forward()
  216.                 MoveCount = MoveCount+1
  217.             until
  218.                 (turtle.detect() or turtle.getFuelLevel()==0)
  219.  
  220.             mine()
  221.         end
  222.     else
  223.         print("Out of fuel")
  224.     end
  225.     print(MoveCount)
  226. end
  227. for i = 1,100 do
  228.     mine()
  229. end
  230. SetDirection("backwards")
  231. for i = 1,MoveCount do
  232.     repeat if turtle.detect() then turtle.dig() end until not turtle.detect()
  233.     turtle.forward()
  234. end
  235. print("Offset")
  236. table.foreach(PosOffset,print)
  237. print("Forward")
  238. table.foreach(Forward,print)
  239. print("Movements:"..MoveCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement