Advertisement
LK005

BetterExcavate

Feb 11th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.41 KB | None | 0 0
  1. local tArgs = { ... }
  2. local input
  3. local target
  4.  
  5. write( "Length: " )
  6. input = read()
  7. local length = tonumber(input)
  8. if (type(length) ~= "number") or (length <= 1) then
  9.     print( "Error 400: length must be a number above 1" )
  10.     return
  11. end
  12.  
  13. write( "Width: " )
  14. input = read()
  15. local width = tonumber(input)
  16. if (type(width) ~= "number") or (width <= 1) then
  17.     print( "Error 400: width must be a number above 1" )
  18.     return
  19. end
  20.  
  21. write( "Depth/Hight: " )
  22. input = read()
  23. local depth = tonumber(input)
  24. if (type(depth) ~= "number") or (depth < 0) then
  25.     print( "Error 400: depth must be a number above 0" )
  26.     return
  27. end
  28.  
  29. write( "Direction: " )
  30. local nextTurn = read()
  31. if type(nextTurn) ~= "string" then
  32.     print( "Error 400: direction must be a string" )
  33.     return
  34. end
  35. if (nextTurn == "r") or (nextTurn == "R") or (nextTurn == "right") or (nextTurn == "Right") then
  36.     nextTurn = "r"
  37. elseif (nextTurn == "l") or (nextTurn == "L") or (nextTurn == "left") or (nextTurn == "Left") then
  38.     nextTurn = "l"
  39. else
  40.     print( "Error 400: direction must be derived from left or right" )
  41.     return
  42. end
  43.  
  44. write( "Should the turtle mine up or down?: " )
  45. local vDirection = read()
  46. if (vDirection == "u") or (vDirection == "up") or (vDirection == "Up") then
  47.     vDirection = "u"
  48.     depth = depth-(depth*2)
  49.     target = depth+1
  50.     print( "target= "..target )
  51. elseif (vDirection == "d") or (vDirection == "down") or (vDirection == "Down") then
  52.     vDirection = "d"
  53.     target = depth-1
  54.     print( "target= "..target )
  55. else
  56.     print( "Error 400: vertical direction must be derived from up or down" )
  57.     return
  58. end
  59.  
  60. print( "Make sure slot 1 contains a container with fuel" )
  61. print( "Make sute slot 2 contains an enderchest to receive items" )
  62. print( "_______________________________________" )
  63. write( "Press Enter to continue" )
  64. read( " " )
  65.  
  66. local unloaded = 0
  67. local collected = 0
  68.  
  69. local xPos,zPos,yPos = 0,0,0
  70. local facing = 1 -- This is relative direction from the start of the programm 1: north, 2: east, 3: south, 4: west
  71. local xDir,zDir = 0,1
  72.  
  73. local endOfRun = false
  74. local last = false
  75.  
  76. function dumpItems()
  77.     print( "Sending items home..." )
  78.  
  79.     turtle.select(2)
  80.     if vDirection == "d" then
  81.         if not turtle.placeUp() then
  82.             turtle.digUp()
  83.             turtle.placeUp()
  84.         end
  85.     elseif vDirection == "u" then
  86.         if not turtle.placeDown() then
  87.             turtle.digDown()
  88.             turtle.placeDown()
  89.         end
  90.     end
  91.  
  92.     for n=3,16 do
  93.         local itemCount = turtle.getItemCount(n)
  94.         if itemCount > 0 then
  95.             turtle.select(n)
  96.             if vDirection == "d" then
  97.                 turtle.dropUp()
  98.             elseif vDirection == "u" then
  99.                 turtle.dropDown()
  100.             end
  101.             unloaded = unloaded + itemCount
  102.         end
  103.     end
  104.  
  105.     turtle.select(2)
  106.     if vDirection == "d" then
  107.         turtle.digUp()
  108.     elseif vDirection == "u" then
  109.         turtle.digDown()
  110.     end
  111. end
  112.  
  113. function refuel()
  114.     print( "Refueling..." )
  115.  
  116.     turtle.select(1)
  117.     if vDirection == "d" then
  118.         if not turtle.placeUp() then
  119.             turtle.digUp()
  120.             turtle.placeUp()
  121.         end
  122.         turtle.suckUp(64)
  123.         turtle.refuel(64)
  124.         turtle.digUp()
  125.     elseif vDirection == "u" then
  126.         if not turtle.placeDown() then
  127.             turtle.digDown()
  128.             turtle.placeDown()
  129.         end
  130.         turtle.suckDown(64)
  131.         turtle.refuel(64)
  132.         turtle.digDown()
  133.     end
  134. end
  135.  
  136. function checkStats()
  137.     if turtle.getItemCount(15) > 0 then
  138.         dumpItems()
  139.     end
  140.  
  141.     if turtle.getFuelLevel() < 1000 then
  142.         refuel()
  143.     end
  144. end
  145.  
  146. function down()
  147.     if not turtle.down() then
  148.         turtle.digDown()
  149.         if not turtle.down() then
  150.             last = true
  151.             return
  152.         end
  153.     end
  154.  
  155.     yPos = yPos + 1
  156.  
  157.     return true
  158. end
  159.  
  160. function forward()
  161.     while turtle.detect() do
  162.         turtle.dig()
  163.     end
  164.     turtle.forward()
  165.  
  166.     checkStats()
  167.  
  168.     xPos = xPos + xDir
  169.     zPos = zPos + zDir
  170.  
  171.     return true
  172. end
  173.  
  174. function up()
  175.     while turtle.detectUp() do
  176.         turtle.digUp()
  177.     end
  178.  
  179.     turtle.up()
  180.  
  181.     yPos = yPos - 1
  182.  
  183.     return true
  184. end
  185.  
  186. function turn( direction )
  187.     if direction == "r" then
  188.         turtle.turnRight()
  189.         if facing == 4 then
  190.             facing = 1
  191.         else
  192.             facing = facing + 1
  193.         end
  194.         setDir()
  195.  
  196.         return true
  197.     elseif direction == "l" then
  198.         turtle.turnLeft()
  199.         if facing == 1 then
  200.             facing = 4
  201.         else
  202.             facing = facing - 1
  203.         end
  204.         setDir()
  205.  
  206.         return true
  207.     else
  208.         return false
  209.     end
  210. end
  211.  
  212. function setFacing( looking )
  213.     while facing ~= looking do
  214.         turn( "r" )
  215.     end
  216. end
  217.  
  218. function setDir()
  219.     if facing == 1 then
  220.         xDir = 0
  221.         zDir = 1
  222.     elseif facing == 2 then
  223.         xDir = 1
  224.         zDir = 0
  225.     elseif facing == 3 then
  226.         xDir = 0
  227.         zDir = -1
  228.     elseif facing == 4 then
  229.         xDir = -1
  230.         zDir = 0
  231.     end
  232. end
  233.  
  234. function translateFacing()
  235.     if facing == 1 then
  236.         return "North"
  237.     elseif facing == 2 then
  238.         return "East"
  239.     elseif facing == 3 then
  240.         return "South"
  241.     elseif facing == 4 then
  242.         return "West"
  243.     end
  244. end
  245.  
  246. function reverse()
  247.     turn( "r" )
  248.     turn( "r" )
  249. end
  250.  
  251. function digUpDown()
  252.     if turtle.detectUp() then
  253.         turtle.digUp()
  254.     end
  255.  
  256.     if turtle.detectDown() then
  257.         turtle.digDown()
  258.     end
  259. end
  260.  
  261. function zigzag()
  262.     for n=1,width do
  263.         for m=1,length-1 do
  264.             forward()
  265.             digUpDown()
  266.         end
  267.  
  268.         if n < width then
  269.             turn(nextTurn)
  270.             forward()
  271.             digUpDown()
  272.             turn(nextTurn)
  273.  
  274.             if nextTurn == "r" then
  275.                 nextTurn = "l"
  276.             elseif nextTurn == "l" then
  277.                 nextTurn = "r"
  278.             end
  279.         end
  280.     end
  281. end
  282.  
  283. function goTo( x,y,z )
  284.     print( "Moving to x: "..x..", y: "..y..", z: "..z.."..." )
  285.     while yPos > y do
  286.         up()
  287.     end
  288.  
  289.     while yPos < y do
  290.         down()
  291.     end
  292.  
  293.     if xPos > x then
  294.         setFacing(4)
  295.         while xPos > x do
  296.             forward()
  297.         end
  298.     elseif xPos < x then
  299.         setFacing(2)
  300.         while xPos < x do
  301.             forward()
  302.         end
  303.     end
  304.  
  305.     if zPos > z then
  306.         setFacing(3)
  307.         while zPos > z do
  308.             forward()
  309.         end
  310.     elseif zPos < z then
  311.         setFacing(1)
  312.         while zPos < z do
  313.             forward()
  314.         end
  315.     end
  316. end
  317.  
  318. function home() -- returns the turtle to 0,0,0
  319.     print( "Returning home..." )
  320.     print( "Current Coordinates: x="..xPos..", y="..yPos..", z="..zPos )
  321.     print( "Turtle is facing "..translateFacing() )
  322.  
  323.     goTo( 0,0,-1 )
  324.     setFacing(1)
  325. end
  326.  
  327. checkStats()
  328. digUpDown()
  329.  
  330. while not endOfRun do
  331.     zigzag()
  332.  
  333.     if last == false then
  334.         reverse()
  335.         print( "Moving to the next layer..." )
  336.         for i=1,3 do
  337.             if (yPos == target) and (depth ~= 0) then
  338.                 print( "I hit my target!" )
  339.                 last = true
  340.                 break
  341.             end
  342.             if vDirection == "d" then
  343.                 down()
  344.             elseif vDirection == "u" then
  345.                 up()
  346.             end
  347.         end
  348.         digUpDown()
  349.     else
  350.         print( "This is my last run..." )
  351.         endOfRun = true
  352.     end
  353. end
  354.  
  355. home()
  356.  
  357. dumpItems()
  358.  
  359. print( "In total "..unloaded.." items were mined..." )
  360.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement