Advertisement
DustinLuhmann

Untitled

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