Advertisement
montana_1

mainTunnel

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