Advertisement
DustinLuhmann

Untitled

Mar 3rd, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.17 KB | None | 0 0
  1. --stripMine
  2. --Dustin Luhmann
  3. --Version 1.3.1
  4. --3/3/2015
  5. --http://pastebin.com/pLCS9ENG
  6.  
  7. --[[TO RUN: pastebin get pLCS9ENG <filename>; <filename>]]
  8. --Example > pastebin get pLCS9ENG test
  9. --        > test
  10. STRIP_DISTANCE = 50 --Blocks
  11. STRIP_GAP_DISTANCE = 2 --Blocks
  12. NUMBER_OF_RUNS = 16
  13.  
  14. TORCH_IDX = 2
  15. TORCH_SPACING = 10 -- 10 --Blocks
  16. TORCH_HEIGHT = 0 --Blocks
  17.  
  18. DIRECTION_OF_CHEST_FROM_HOME = "west"
  19.  
  20. INVENTORY_SPACE = 16
  21.  
  22. HOME_POSITION = {0,0,0} --{X,Y,Z}
  23. HEADINGS = {"north","east","south","west"}
  24. X_IDX = 1
  25. Y_IDX = 2
  26. Z_IDX = 3
  27.  
  28. currentPosition = {0,0,0}
  29. heading = 1
  30. isFull = false
  31.  
  32. function mine()
  33.     turtle.dig()
  34. end
  35.  
  36. function mineUp()
  37.     turtle.digUp()
  38. end
  39.  
  40. function mineDown()
  41.     turtle.digDown()
  42. end
  43.  
  44. function forward()
  45.     if HEADINGS[heading] == "north" then
  46.         currentPosition[Y_IDX] = currentPosition[Y_IDX] + 1
  47.     elseif HEADINGS[heading] == "south" then
  48.         currentPosition[Y_IDX] = currentPosition[Y_IDX] - 1
  49.     elseif HEADINGS[heading] == "east" then
  50.         currentPosition[X_IDX] = currentPosition[X_IDX] + 1
  51.     elseif HEADINGS[heading] == "west" then
  52.         currentPosition[X_IDX] = currentPosition[X_IDX] - 1
  53.     end
  54.     turtle.forward()
  55. end
  56.  
  57. function turnLeft()
  58.     if HEADINGS[heading] == "north" then
  59.         heading = 4
  60.     else
  61.         heading = heading - 1
  62.     end
  63.     turtle.turnLeft()
  64. end
  65.  
  66. function turnRight()
  67.     if HEADINGS[heading] == "west" then
  68.         heading = 1
  69.     else
  70.         heading = heading + 1
  71.     end
  72.     turtle.turnRight()
  73. end
  74.  
  75. function up()
  76.     currentPosition[Z_IDX] = currentPosition[Z_IDX] + 1
  77.     turtle.digUp()
  78.     turtle.up()
  79. end
  80.  
  81. function down()
  82.     currentPosition[Z_IDX] = currentPosition[Z_IDX] - 1
  83.     turtle.digDown()
  84.     turtle.down()
  85. end
  86.  
  87. function checkSpace()
  88.     for i = 1, INVENTORY_SPACE do
  89.         if turtle.getItemCount(i) == 0 then
  90.             return true
  91.         end
  92.     end
  93.     isFull = true
  94.     return false
  95. end
  96.  
  97. function mineThreeHigh()
  98.     up()
  99.     up()
  100.     mine()
  101.     down()
  102.     mine()
  103.     down()
  104.     mine()
  105.     wait()
  106.     while turtle.detect() do
  107.         mine()
  108.         wait()
  109.     end
  110.     forward()
  111. end
  112.  
  113. function mineAStrip()
  114.     local distanceTraveled = 0
  115.     while distanceTraveled < STRIP_DISTANCE and turtle.detect() and checkSpace() do
  116.         mineThreeHigh()
  117.         distanceTraveled = distanceTraveled + 1
  118.     end
  119.     turnLeft()
  120.     turnLeft()
  121.     distanceBack = distanceTraveled
  122.     for i = 1, distanceTraveled do
  123.         if (distanceBack % TORCH_SPACING == 0) then
  124.             placeTorch()
  125.         end
  126.         distanceBack = distanceBack - 1
  127.         while turtle.detect() do
  128.             mine()
  129.             wait()
  130.         end
  131.         forward()
  132.     end
  133.     turnRight()
  134.     turnRight()
  135. end
  136.  
  137. function moveToStripInDirectionOf(direction)
  138.     if direction == "right" then
  139.         turnRight()
  140.     elseif direction == "left" then
  141.         turnLeft()
  142.     else
  143.         print("ERROR: moveToStripInDirectionOf was not given left or right")
  144.         return
  145.     end
  146.     for i = 0, STRIP_GAP_DISTANCE do
  147.         mineThreeHigh()
  148.     end
  149.     if direction == "right" then
  150.         turnLeft()
  151.     else
  152.         turnRight()
  153.     end
  154. end
  155.  
  156. function wait()
  157.    turnLeft()
  158.    turnRight()
  159. end
  160.  
  161. function returnHome()
  162.     if currentPosition[Z_IDX] > 0 then
  163.         while turtle.detectDown() do
  164.             mineDown()
  165.             wait()
  166.         end
  167.         down()
  168.     elseif currentPosition[Z_IDX] < 0 then
  169.         while turtle.detectUp() do
  170.             mineUp()
  171.             wait()
  172.         end
  173.         up()
  174.     end
  175.     if currentPosition[Y_IDX] > 0 then
  176.         while currentPosition[Y_IDX] > 0 do
  177.             while HEADINGS[heading] ~= "south" do
  178.                 turnLeft()
  179.             end
  180.             while turtle.detect() do
  181.                 mine()
  182.                 wait()
  183.             end
  184.             forward()
  185.         end
  186.     elseif currentPosition[Y_IDX] < 0 then
  187.         while currentPosition[Y_IDX] < 0 do
  188.             while HEADINGS[heading] ~= "north" do
  189.                 turnLeft()
  190.             end
  191.             while turtle.detect() do
  192.                 mine()
  193.                 wait()
  194.             end
  195.             forward()
  196.         end
  197.     end
  198.     if currentPosition[X_IDX] > 0 then
  199.         while currentPosition[X_IDX] > 0 do
  200.             while HEADINGS[heading] ~= "west" do
  201.                 turnLeft()
  202.             end
  203.             while turtle.detect() do
  204.                 mine()
  205.                 wait()
  206.             end
  207.             forward()
  208.         end
  209.     elseif currentPosition[X_IDX] < 0 then
  210.         while currentPosition[X_IDX] < 0 do
  211.             while HEADINGS[heading] ~= "east" do
  212.                 turnLeft()
  213.             end
  214.             while turtle.detect() do
  215.                 mine()
  216.                 wait()
  217.             end
  218.             forward()
  219.         end
  220.     end
  221.     while HEADINGS[heading] ~= "north" do
  222.         turnLeft()
  223.     end
  224. end
  225.  
  226. function go(x,y,x)
  227.     if x >= 0 then
  228.         while HEADINGS[heading] ~= "east" do
  229.             turnLeft()
  230.         end
  231.         for i = 0, (x - 1) do
  232.             forward()
  233.         end
  234.     elseif x < 0 then
  235.         while HEADINGS[heading] ~= "west" do
  236.             turnLeft()
  237.         end
  238.         for i = 0, (x - 1) * -1 do
  239.             forward()
  240.         end
  241.     end
  242.     if y >= 0 then
  243.         while HEADINGS[heading] ~= "north" do
  244.             turnLeft()
  245.         end
  246.         for i = 0, (y - 1) do
  247.             forward()
  248.         end
  249.     elseif y < 0 then
  250.         while HEADINGS[heading] ~= "south" do
  251.             turnLeft()
  252.         end
  253.         for i = 0, (y - 1) * -1 do
  254.             forward()
  255.         end
  256.     if z > 0 then
  257.         for i = 0, (z - 1) do
  258.             up()
  259.         end
  260.     end
  261.     elseif z < 0 then
  262.         for i = 0, (z - 1) * -1 do
  263.             down()
  264.         end
  265.     end
  266. end
  267.  
  268. function placeTorch()
  269.     oldHeading = heading
  270.     oldHeight = currentPosition[Z_IDX]
  271.     while HEADINGS[heading] ~= "west" do
  272.         turnLeft()
  273.     end
  274.     if currentPosition[Z_IDX] > TORCH_HEIGHT then
  275.         while currentPosition[Z_IDX] > TORCH_HEIGHT do
  276.             down()
  277.         end
  278.     elseif currentPosition[Z_IDX] < TORCH_HEIGHT then
  279.         while currentPosition[Z_IDX] < TORCH_HEIGHT do
  280.             up()
  281.         end
  282.     end
  283.     -- mine()
  284.     turtle.select(TORCH_IDX)
  285.     turtle.placeUp()
  286.     while heading ~= oldHeading do
  287.         turnRight()
  288.     end
  289.     if oldHeight < currentPosition[Z_IDX] then
  290.         while currentPosition[Z_IDX] > oldHeight do
  291.             down()
  292.         end
  293.     elseif oldHeight > currentPosition[Z_IDX] then
  294.         while currentPosition[Z_IDX] < oldHeight do
  295.             up()
  296.         end
  297.     end
  298. end
  299.  
  300. function emptyInventory()
  301.     oldHeading = heading
  302.     while HEADINGS[heading] ~= DIRECTION_OF_CHEST_FROM_HOME do
  303.         turnLeft()
  304.     end
  305.     for i = 1, INVENTORY_SPACE do
  306.         if i ~= TORCH_IDX then
  307.             turtle.select(i)
  308.             turtle.drop()
  309.         end
  310.     end
  311.     while heading ~= oldHeading do
  312.         turnRight()
  313.     end
  314.     isFull = false
  315.     turtle.select(1)
  316. end
  317.  
  318. function run()
  319.     runs = 0
  320.     go(0,0,0)
  321.     while checkSpace() and runs ~= NUMBER_OF_RUNS do
  322.         runs = 0
  323.         while runs < NUMBER_OF_RUNS  and not isFull do
  324.             mineAStrip()
  325.             if runs + 1 ~= NUMBER_OF_RUNS and not isFull then
  326.                 moveToStripInDirectionOf("right")
  327.             end
  328.             runs = runs + 1
  329.         end
  330.         returnHome()
  331.         emptyInventory()
  332.     end
  333.     print("Program Over")
  334. end
  335.  
  336. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement