Advertisement
goldmech

Untitled

Jan 22nd, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1. -- pastebin run yBYLz3q4 2 1 2
  2. -- pastebin run yBYLz3q4 3 3 3
  3.  
  4.  
  5.  
  6. -- 1: width
  7. -- 2: height
  8. -- 3: depth
  9. args = {...}
  10.  
  11.  
  12.  
  13. --[[
  14.  
  15.  
  16.     MOVEMENT
  17.  
  18.  
  19. ]]
  20.  
  21. -- 1: forward
  22. -- 2: right
  23. -- 3: backwards
  24. -- 4: left
  25. pos = { x = 0, y = 0, z = 0, direction = 1 }
  26.  
  27. function move(amount)
  28.     if(pos.direction == 1) then
  29.         pos.z = pos.z + amount
  30.     elseif(pos.direction == 2) then
  31.         pos.x = pos.x + amount
  32.     elseif(pos.direction == 3) then
  33.         pos.z = pos.z + (amount * -1)
  34.     elseif(pos.direction == 4) then
  35.         pos.x = pos.x + (amount * -1)
  36.     end
  37. end
  38.  
  39. function refuel()
  40.     if(turtle.getFuelLevel() < 2) then
  41.         while not(turtle.refuel(1)) do
  42.             print("Out of fuel! Waiting for resupply...")
  43.         end
  44.     end
  45. end
  46.  
  47. function forward()
  48.     refuel()
  49.     if(turtle.forward()) then
  50.         move(1)
  51.     end
  52. end
  53.  
  54. function back()
  55.     refuel()
  56.     if(turtle.back()) then
  57.         move(-1)
  58.     end
  59. end
  60.  
  61. function right()
  62.     refuel()
  63.     if(turtle.turnRight()) then
  64.         pos.direction = pos.direction + 1
  65.         if(pos.direction > 4) then
  66.             pos.direction = 1
  67.         end
  68.     end
  69. end
  70.  
  71. function left()
  72.     refuel()
  73.     if(turtle.turnLeft()) then
  74.         pos.direction = pos.direction - 1
  75.         if(pos.direction < 1) then
  76.             pos.direction = 4
  77.         end
  78.     end
  79. end
  80.  
  81.  
  82. function down()
  83.     refuel()
  84.     if(turtle.down()) then
  85.         pos.y = pos.y - 1
  86.     end
  87. end
  88.  
  89. function up()
  90.     refuel()
  91.     if(turtle.up()) then
  92.         pos.y = pos.y + 1
  93.     end
  94. end
  95.  
  96. function orient(direction)
  97.     while not(pos.direction == direction) do
  98.         --print("ORIENT: ", pos.direction, " : ", direction, " : ", pos.direction == direction)
  99.         right()
  100.         --[[if(pos.direction > direction and pos.direction > 2) then
  101.             left()
  102.         elseif(pos.direction > direction and pos.direction < 2) then
  103.             right()
  104.         elseif(pos.direction < direction and pos.direction < 2) then
  105.             left()
  106.         else
  107.             right()
  108.         end]]
  109.     end
  110. end
  111.  
  112.  
  113. function goto(x,y,z)
  114.     while not(pos.x == x and pos.z == z and pos.y == y) do
  115.         if( pos.x > x and not(turtle.detect()) ) then
  116.             orient(4)
  117.             forward()
  118.         elseif( pos.x < x and not(turtle.detect()) ) then
  119.             orient(2)
  120.             forward()
  121.         elseif( pos.z < z and not(turtle.detect()) )  then
  122.             orient(1)
  123.             forward()
  124.         elseif( pos.z > z and not(turtle.detect()) ) then
  125.             orient(3)
  126.             forward()
  127.         elseif( pos.y > y ) then
  128.             down()
  129.         elseif( pos.y < y ) then
  130.             up()
  131.         else
  132.             return
  133.         end
  134.     end
  135. end
  136.  
  137.  
  138.  
  139.  
  140. --[[
  141.  
  142.  
  143.     INVENTORY
  144.    
  145.  
  146. ]]
  147. function getItems()
  148.     stacks = 0
  149.     list = {}
  150.     for i = 1, 16 do
  151.         list[table.getn(list)] = turtle.getItemDetail(i)
  152.  
  153.         if(not(turtle.getItemDetail(i) == nil)) then
  154.             stacks = stacks + 1
  155.         end
  156.     end
  157.  
  158.     output = {}
  159.     output["list"] = list
  160.     output["stacks"] = stacks
  161.  
  162.     return output
  163. end
  164.  
  165.  
  166.  
  167.  
  168.  
  169. -- Program Logic
  170.  
  171. function goRefuel(startX, startZ, startDir)
  172.     goto(1,pos.y,0)
  173. end
  174.  
  175.  
  176. -- Actual running logic
  177. function queryTurn(direction, x, w)
  178.     if(direction) then
  179.         right()
  180.         if(x < w -1) then
  181.             turtle.dig()
  182.             forward()
  183.         end
  184.         right()
  185.     else
  186.         left()
  187.         if(x < w -1) then
  188.             turtle.dig()
  189.             forward()
  190.         end
  191.         left()
  192.     end
  193. end
  194.  
  195. function query(w,d,h)
  196.  
  197.     miningForward = false
  198.  
  199.     items = getItems()
  200.  
  201.     for y=0, d - 1 do
  202.         for x=0, w - 1 do
  203.             for z=0, h - 1 do
  204.                 -- print("X: ", x, " Y: ", y, " Z: ", z, " W: ", w -1, " H: ", h - 1, " Z: ", z - 1)
  205.  
  206.                 items = getItems()
  207.                 while items.stacks == 16 do
  208.                     items = getItems()
  209.                     print("FULL, returning to drop off")
  210.                     goto(0, -1, -1)
  211.                 end
  212.                
  213.                 if(z < h - 1) then
  214.                     turtle.dig()
  215.                     forward()
  216.                 end
  217.  
  218.             end
  219.             if(x < w - 1) then
  220.                queryTurn(miningForward, x, w)
  221.             end
  222.             miningForward = not(miningForward)
  223.         end
  224.         --miningForward = not(miningForward)
  225.         -- print("MINDIR: ", miningForward)
  226.  
  227.         if( w % 2 > 0 ) then
  228.             left()
  229.             left()
  230.             miningForward = not(miningForward)
  231.         else
  232.             right()
  233.             right()
  234.             right()
  235.         end
  236.  
  237.         if( y < d - 1) then
  238.             turtle.digDown()
  239.             down()
  240.         end
  241.     end
  242.  
  243.    
  244.  
  245. end
  246.  
  247. -- Setup
  248. math.randomseed(os.time())
  249. refuel()
  250. turtle.up()
  251. turtle.dig()
  252. turtle.forward()
  253.  
  254. print("X: ", args[1], " Y: ", args[2], " Z: ", args[3])
  255.  
  256. query(args[1],args[2],args[3])
  257.  
  258. -- goto(pos.x,5,pos.z)
  259. goto(0,-1,-1)
  260. orient(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement