Advertisement
montana_1

Continue Mining

Oct 15th, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.27 KB | None | 0 0
  1. -- Function to set the heading of the turtle --
  2.  
  3. function setHeading(heading,newHeading)
  4.      if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
  5.         if(turtle.getFuelLevel() < 2) then
  6.             error("Insufficient fuel")
  7.         end
  8.                 local start = vector.new(gps.locate())
  9.         local heading = ""
  10.                 if(turtle.detect()) then
  11.                         turtle.dig()
  12.                 end
  13.                 turtle.forward()
  14.                 local current = vector.new(gps.locate())
  15.                 turtle.back()
  16.                 if(start.z - current.z > 0) then
  17.                         heading = 'N'
  18.                 elseif (start.z - current.z < 0) then
  19.                         heading = 'S'
  20.                 end
  21.                 if(start.x - current.x > 0) then
  22.                         heading = 'W'
  23.                 elseif (start.x - current.x < 0) then
  24.                         heading = 'E'
  25.                 end
  26.         end
  27.  
  28.     if(heading ~= newHeading) then
  29.         if(newHeading == 'N' or newHeading == 'n') then
  30.             if(heading == 'S' or heading == 's') then
  31.                 turtle.turnLeft()
  32.                 turtle.turnLeft()
  33.             end
  34.             if(heading == 'E' or heading == 'e') then
  35.                 turtle.turnLeft()
  36.             end
  37.             if(heading == 'W' or heading == 'w') then
  38.                 turtle.turnRight()
  39.             end
  40.         end
  41.         if(newHeading == 'E' or newHeading == 'e') then
  42.             if(heading == 'S' or heading == 's') then
  43.                 turtle.turnLeft()
  44.             end
  45.             if(heading == 'N' or heading == 'n') then
  46.                 turtle.turnRight()
  47.             end
  48.             if(heading == 'W' or heading == 'w') then
  49.                 turtle.turnRight()
  50.                 turtle.turnRight()
  51.             end
  52.         end
  53.         if(newHeading == 'S' or newHeading == 's') then
  54.             if(heading == 'N' or heading == 'n') then
  55.                 turtle.turnLeft()
  56.                 turtle.turnLeft()
  57.             end
  58.             if(heading == 'E' or heading == 'e') then
  59.                 turtle.turnRight()
  60.             end
  61.             if(heading == 'W' or heading == 'w') then
  62.                 turtle.turnLeft()
  63.             end
  64.         end
  65.         if(newHeading == 'W' or newHeading == 'w') then
  66.             if(heading == 'S' or heading == 's') then
  67.                 turtle.turnRight()
  68.             end
  69.             if(heading == 'E' or heading == 'e') then
  70.                 turtle.turnLeft()
  71.                 turtle.turnLeft()
  72.             end
  73.             if(heading == 'N' or heading == 'n') then
  74.                 turtle.turnLeft()
  75.             end
  76.         end
  77.     end
  78. end
  79.  
  80.  
  81. -- Function to get the heading of the turtle --
  82.  
  83. function getHeading()
  84.     if(turtle.getFuelLevel() < 2) then
  85.         error("Insufficient fuel")
  86.     end
  87.     local start = vector.new(gps.locate())
  88.     local heading = ""
  89.     if(turtle.detect()) then
  90.         turtle.dig()
  91.     end
  92.     turtle.forward()
  93.     local current = vector.new(gps.locate())
  94.     turtle.back()
  95.     if(start.z - current.z > 0) then
  96.         heading = 'N'
  97.     elseif (start.z - current.z < 0) then
  98.         heading = 'S'
  99.     end
  100.     if(start.x - current.x > 0) then
  101.         heading = 'W'
  102.     elseif (start.x - current.x < 0) then
  103.         heading = 'E'
  104.     end
  105.         return heading
  106. end
  107.  
  108.  
  109. -- Function to go to a specific location --
  110.  
  111. function goToLocation(location,heading)
  112.     if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then
  113.         local heading = getHeading()
  114.     end
  115.    
  116.     local current = vector.new(gps.locate())
  117.     if(location.x ~= current.x or location.y ~= current.y or location.z ~= current.z) then
  118.         if(turtle.getFuelLevel() < 2) then
  119.             error("Insufficient fuel")
  120.         end
  121.     end
  122.     while(location.y ~= current.y) do
  123.         if(location.y > current.y) then
  124.             if(turtle.detectUp()) then
  125.                 turtle.digUp()
  126.             end
  127.             turtle.up()
  128.         else
  129.             if(turtle.detectDown()) then
  130.                 turtle.digDown()
  131.             end
  132.             turtle.down()
  133.         end
  134.         current = vector.new(gps.locate())
  135.     end
  136.    
  137.     while(location.z ~= current.z) do
  138.         if(location.z > current.z) then
  139.             setHeading(heading,'S')
  140.             heading = 'S'
  141.         elseif(location.z < current.z) then
  142.             setHeading(heading,'N')
  143.             heading = 'N'
  144.         end
  145.         if(turtle.detect()) then
  146.             turtle.dig()
  147.         end
  148.         turtle.forward()
  149.         current = vector.new(gps.locate())
  150.     end
  151.  
  152.     while(location.x ~= current.x) do
  153.         if(location.x > current.x) then
  154.             setHeading(heading,'E')
  155.             heading = 'E'
  156.         elseif(location.x < current.x) then
  157.             setHeading(heading,'W')
  158.             heading = 'W'
  159.         end
  160.         if(turtle.detect()) then
  161.             turtle.dig()
  162.         end
  163.         turtle.forward()
  164.         current = vector.new(gps.locate())
  165.     end
  166.     return heading
  167. end
  168.  
  169.  
  170. -- Function to check if turtle has sufficient fuel --
  171.  
  172. function sufficientFuel(vector1,vector2,fuel,remainder)
  173.     local distance = math.abs(vector1.x - vector2.x) + math.abs(vector1.y - vector2.y) + math.abs(vector1.z - vector2.z)
  174.     if(fuel <= distance + remainder) then
  175.         return false
  176.     else
  177.         return true
  178.     end
  179. end
  180.  
  181.  
  182. -- Function to refuel turtle appropriately --
  183.  
  184. function consumeFuel(maxFuel)
  185.     local refuel = false
  186.     for i = 1, 16 do
  187.         turtle.select(i)
  188.         local x = turtle.getItemDetail(i)
  189.         if(x ~= nil) then
  190.             local istr = string.sub(x.name,string.find(x.name,":",0)+1)
  191.             print(istr,"    t")
  192.             if(istr == "planks" or istr == "stick" or istr == "log") then
  193.                 turtle.refuel()
  194.                 refuel = true
  195.             end
  196.             if(turtle.getFuelLevel() < maxFuel and istr == "coal") then
  197.                 turtle.refuel(1)
  198.                 refuel = true
  199.             end
  200.         end
  201.     end
  202.     turtle.select(1)
  203.     print(turtle.getFuelLevel())
  204.     return refuel
  205. end
  206.  
  207.  
  208. -- Function to check for inv space --
  209.  
  210. function invSpace()
  211.     for i = 1, 16 do
  212.         turtle.select(i)
  213.         local x = turtle.getItemDetail(i)
  214.         if(x == nil or turtle.compare()) then
  215.             turtle.select(1)
  216.             return true
  217.         end
  218.     end
  219.     turtle.select(1)
  220.     return false
  221. end
  222.  
  223.  
  224. -- Function to sort inventory --
  225.  
  226. function sortInv()
  227.     for i = 1, 16 do -- loop through the slots
  228.         turtle.select(i)
  229.         if(turtle.getItemDetail(i) ~= nil) then
  230.             for c = i, 16 do
  231.                 if(turtle.getItemDetail(c) ~= nil) then
  232.                     if(turtle.compareTo(c)) then
  233.                         turtle.select(c)
  234.                         turtle.transferTo(i)
  235.                         turtle.select(i)
  236.                     end
  237.                 end
  238.             end
  239.         end
  240.     end
  241.     turtle.select(1)
  242. end
  243.  
  244.  
  245. -- Function to mine entire ore veins --
  246.  
  247. function mineVein(start,moves,back)
  248.     local current = vector.new(gps.locate())
  249.     if(not sufficientFuel(start,current,turtle.getFuelLevel(),5 + moves)) then
  250.         if(not consumeFuel(400)) then
  251.             return false
  252.         end
  253.     end
  254.     if(not invSpace()) then
  255.         sortInv()
  256.         if(not invSpace()) then
  257.             return false
  258.         end
  259.     end
  260.    
  261.    
  262.     local success,data = turtle.inspect()
  263.     if(success) then
  264.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  265.         print(istr)
  266.         if(istr == "iron_ore") then
  267.             turtle.dig()
  268.             turtle.forward()
  269.             mineVein(start,moves+1,false)
  270.             turtle.back()
  271.         end
  272.     end
  273.     if(moves < 1) then
  274.         return true
  275.     end
  276.     turtle.turnLeft()
  277.     local success,data = turtle.inspect()
  278.     if(success) then
  279.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  280.         print(istr)
  281.         if(istr == "iron_ore") then
  282.             turtle.dig()
  283.             turtle.forward()
  284.             mineVein(start,moves+1,false)
  285.             turtle.back()
  286.         end
  287.     end
  288.     if(back) then
  289.         turtle.turnLeft()
  290.         local success,data = turtle.inspect()
  291.         if(success) then
  292.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  293.             print(istr)
  294.             if(istr == "iron_ore") then
  295.                 turtle.dig()
  296.                 turtle.forward()
  297.                 mineVein(start,moves+1,false)
  298.                 turtle.back()
  299.             end
  300.         end
  301.         turtle.turnLeft()
  302.         local success,data = turtle.inspect()
  303.         if(success) then
  304.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  305.             print(istr)
  306.             if(istr == "iron_ore") then
  307.                 turtle.dig()
  308.                 turtle.forward()
  309.                 mineVein(start,moves+1,false)
  310.                 turtle.back()
  311.             end
  312.         end
  313.         turtle.turnLeft()
  314.         local success,data = turtle.inspectUp()
  315.         if(success) then
  316.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  317.             print(istr)
  318.             if(istr == "iron_ore") then
  319.                 turtle.digUp()
  320.                 turtle.up()
  321.                 mineVein(start,moves+1,true)
  322.                 turtle.down()
  323.             end
  324.         end
  325.         --
  326.         local success,data = turtle.inspectDown()
  327.         if(success) then
  328.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  329.             print(istr)
  330.             if(istr == "iron_ore") then
  331.                 turtle.digDown()
  332.                 turtle.down()
  333.                 mineVein(start,moves+1,true)
  334.                 turtle.up()
  335.             end
  336.         end
  337.         --
  338.     else
  339.         turtle.turnRight()
  340.         turtle.turnRight()
  341.         local success,data = turtle.inspect()
  342.         if(success) then
  343.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  344.             print(istr)
  345.             if(istr == "iron_ore") then
  346.                 turtle.dig()
  347.                 turtle.forward()
  348.                 mineVein(start,moves+1,false)
  349.                 turtle.back()
  350.             end
  351.         end
  352.         turtle.turnLeft()
  353.         local success,data = turtle.inspectUp()
  354.         if(success) then
  355.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  356.             print(istr)
  357.             if(istr == "iron_ore") then
  358.                 turtle.digUp()
  359.                 turtle.up()
  360.                 mineVein(start,moves+1,true)
  361.                 turtle.down()
  362.             end
  363.         end
  364.         --
  365.         local success,data = turtle.inspectDown()
  366.         if(success) then
  367.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  368.             print(istr)
  369.             if(istr == "iron_ore") then
  370.                 turtle.digDown()
  371.                 turtle.down()
  372.                 mineVein(start,moves+1,true)
  373.                 turtle.up()
  374.             end
  375.         end
  376.         --
  377.     end
  378.     return true
  379. end
  380.  
  381.  
  382. -- Starting variables --
  383. -- (These are only temporary) --
  384.  
  385. level = 12
  386.  
  387. limit = 16
  388.  
  389.  
  390. --|-----------------------|--
  391. --| **  Main function  ** |--
  392. --V-----------------------V--
  393.  
  394. --      Check if GPS_DATA file exists   --
  395. if(fs.exists("GPS_DATA")) then
  396.     --      GPS_DATA exists                 --
  397.         --      Check validity of GPS_DATA      --
  398.     gpsData = fs.open("GPS_DATA","r")
  399.     local start = vector.new(tonumber(gpsData.readLine()),tonumber(gpsData.readLine()),tonumber(gpsData.readLine()))
  400.     sHeading = gpsData.readLine()
  401.     if(start.x == nil or start.y == nil or start.z == nil) then
  402.         --      GPS_DATA Invalid                --
  403.                 term.clear()
  404.                 term.setCursorPos(1,1)
  405.                 error("Invalid GPS information")
  406.     else
  407.         --      GPS_DATA Valid          --
  408.                 term.clear()
  409.                 term.setCursorPos(1,1)
  410.                 print("GPS_DATA exists, start: (",start.x,",",start.y,",",start.z,")")
  411.                 print("Heading: ", sHeading)
  412.         --      Search for GPS signal   --
  413.                 local current = vector.new(gps.locate())
  414.                 if(current.x == nil or current.y == nil or current.z == nil) then
  415.                         --      GPS signal could not be established     --
  416.                         error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  417.                 else
  418.             --      GPS signal established          --
  419.                         print("GPS locate, current: (",current.x,",",current.y,",",current.z,")")
  420.             --      Ensure y level is the same as starting y level  --
  421.             if(not sufficientFuel(current,start,turtle.getFuelLevel(),5)) then
  422.                 if(not consumeFuel(400)) then
  423.                     error("Insufficient fuel!")
  424.                 end
  425.             end
  426.                         local cHeading = getHeading()
  427.             print("Heading: ", cHeading)
  428.                         if(start.y ~= current.y) then
  429.                                 --      if y level is not the same      --
  430.                                 cHeading = goToLocation(vector.new(current.x,start.y,current.z), cHeading)
  431.                 current = vector.new(gps.locate())
  432.  
  433.                         end
  434.                         --      Ensure z level is the same as starting y level  --
  435.                         if(sHeading == 'N' or sHeading == 'n' or sHeading == 'S' or sHeading == 's') then
  436.                                 if(start.z ~= current.z) then
  437.                                         --      if z level is not the same      --
  438.                                         cHeading = goToLocation(vector.new(start.x,current.y,current.z), cHeading)
  439.                     current = vector.new(gps.locate())
  440.                                 end
  441.                         end
  442.                         --      Ensure x level is the same as starting y level  --
  443.                         if(sHeading == 'E' or sHeading == 'e' or sHeading == 'W' or sHeading == 'w') then
  444.                                 if(start.x ~= current.x) then
  445.                                         --      if x level is not the same      --
  446.                                         cHeading = goToLocation(vector.new(current.x,current.y,start.z), cHeading)
  447.                     current = vector.new(gps.locate())
  448.                                 end
  449.                         end
  450.                         setHeading(cHeading,sHeading)
  451.             cHeading = sHeading
  452.             done = false
  453.             while(done == false) do
  454.                 current = vector.new(gps.locate())
  455.                 if(current.x == nil or current.y == nil or current.z == nil) then
  456.                                 --      GPS signal could not be established     --
  457.                                 print("Could not establish GPS signal, please ensure GPS servers are running and try again")
  458.                                 error()
  459.                         else
  460.                     --      Ensure distance from start is less than limit   --
  461.                                 --      Along the North/South line      --
  462.                                 if(sHeading == 'N' or sHeading == 'n' or sHeading == 'S' or sHeading == 's')then
  463.                                         --      Check if distance from start is within limit    --
  464.                                         if(math.abs(start.z - current.z)>limit) then
  465.                             --      Distance from start is not within limit         --
  466.                             cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading)
  467.                             cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
  468.                             done = true
  469.                                         else
  470.                             --      Distance from start is within limit     --
  471.                             --  Check for sufficient fuel   --
  472.                             if(sufficientFuel(current,start,turtle.getFuelLevel(),5)) then
  473.                                 if(invSpace()) then
  474.                                     if(current.y == start.y) then
  475.                                         --checkSides()
  476.                                         if(turtle.detect()) then
  477.                                             turtle.dig()
  478.                                         end
  479.                                         turtle.forward()
  480.                                         --checkSides()
  481.                                         if(turtle.detectUp()) then
  482.                                             turtle.digUp()
  483.                                         end
  484.                                         turtle.up()
  485.                                     elseif(current.y - 1 == start.y) then
  486.                                         --checkSides()
  487.                                         if(turtle.detect()) then
  488.                                             turtle.dig()
  489.                                         end
  490.                                         turtle.forward()
  491.                                         --checkSides()
  492.                                         if(turtle.detectDown()) then
  493.                                             turtle.digDown()
  494.                                         end
  495.                                         turtle.down()
  496.                                     else
  497.                                         cHeading = goToLocation(vector.new(current.x,start.y,current.z),cHeading)
  498.                                     end
  499.                                 else
  500.                                     sortInv()
  501.                                     if(not invSpace()) then
  502.                                         cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading)
  503.                                         cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
  504.                                         done = true
  505.                                     end
  506.                                 end
  507.                             else
  508.                                 if(not consumeFuel(400)) then
  509.                                     cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading)
  510.                                     cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
  511.                                     done = true
  512.                                 end
  513.                             end
  514.                                         end
  515.                                 --      Along the East/West line                --
  516.                                 elseif(sHeading == 'E' or sHeading == 'e' or sHeading == 'W' or sHeading == 'w') then
  517.                                         --      Check if distance from start is within limit    --
  518.                                         if(math.abs(start.x - current.x)>limit) then
  519.                             --      Distance from start is not within limit         --
  520.                             cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading)
  521.                             cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
  522.                             done = true
  523.                                         else
  524.                             --      Distance from start is within limit     --
  525.                             --  Check for sufficient fuel   --
  526.                             if(sufficientFuel(current,start,turtle.getFuelLevel(),5)) then
  527.                                 if(invSpace()) then
  528.                                     if(current.y == start.y) then
  529.                                         --checkSides()
  530.                                         if(turtle.detect()) then
  531.                                             turtle.dig()
  532.                                         end
  533.                                         turtle.forward()
  534.                                         --checkSides()
  535.                                         if(turtle.detectUp()) then
  536.                                             turtle.digUp()
  537.                                         end
  538.                                         turtle.up()
  539.                                     elseif(current.y - 1 == start.y) then
  540.                                         --checkSides()
  541.                                         if(turtle.detect()) then
  542.                                             turtle.dig()
  543.                                         end
  544.                                         turtle.forward()
  545.                                         --checkSides()
  546.                                         if(turtle.detectDown()) then
  547.                                             turtle.digDown()
  548.                                         end
  549.                                         turtle.down()
  550.                                     else
  551.                                         cHeading = goToLocation(vector.new(current.x,start.y,current.z),cHeading)
  552.                                     end
  553.                                 else
  554.                                     sortInv()
  555.                                     if(not invSpace()) then
  556.                                         cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading)
  557.                                         cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
  558.                                         done = true
  559.                                     end
  560.                                 end
  561.                             else
  562.                                 if(not consumeFuel(400)) then
  563.                                     cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading)
  564.                                     cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading)
  565.                                     done = true
  566.                                 end
  567.                             end
  568.                                         end
  569.                                 end
  570.                 end
  571.             end
  572.             print("Mining complete")
  573.         end
  574.     end
  575. else
  576.     --      GPS_DATA not found      --
  577.         term.clear()
  578.         term.setCursorPos(1,1)
  579.         print("File 'GPS_DATA' does not exist, please run program to initiate mining.")
  580.         error()
  581. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement