Advertisement
montana_1

CMining rev 2

Oct 21st, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 place torch --
  181. function placeTorch(direction) -- Optionally add functionality to place torch when no torch can be placed
  182.     local torch = false
  183.     for i = 1, 16 do -- loop through the slots
  184.         turtle.select(i) -- change to the slot
  185.         local x = turtle.getItemDetail(i)
  186.         if(x ~= nil) then
  187.             local istr = string.sub(x.name,string.find(x.name,":",0)+1)
  188.             if(istr == "torch") then
  189.                 if(direction == "up") then
  190.                     if(turtle.placeUp()) then
  191.                         torch = true
  192.                         turtle.select(1)
  193.                     end
  194.                 elseif(direction == "down")then
  195.                     if(turtle.placeDown()) then
  196.                         torch = true
  197.                         turtle.select(1)
  198.                     end
  199.                 else
  200.                     if(turtle.place()) then
  201.                         torch = true
  202.                         turtle.select(1)
  203.                     end
  204.                 end
  205.             end
  206.         end
  207.     end
  208.     turtle.select(1)
  209.     return torch
  210. end
  211.  
  212. -- Function to get turtle heading --
  213. function getHeading()
  214.     if(turtle.getFuelLevel() < 2) then
  215.         if(not consumeFuel(400)) then
  216.             error("Insufficient fuel")
  217.         end
  218.     end
  219.     local start = vector.new(gps.locate())
  220.     while(turtle.detect()) do
  221.         turtle.dig()
  222.     end
  223.     turtle.forward()
  224.     local current = vector.new(gps.locate())
  225.     turtle.back()
  226.     if(start.z - current.z > 0) then
  227.         heading = 'N'
  228.     elseif (start.z - current.z < 0) then
  229.         heading = 'S'
  230.     end
  231.     if(start.x - current.x > 0) then
  232.         heading = 'W'
  233.     elseif (start.x - current.x < 0) then
  234.         heading = 'E'
  235.     end
  236.         return heading
  237. end
  238.  
  239. -- Function to set turtle heading --
  240. function setHeading(heading,newHeading)
  241.      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
  242.         if(turtle.getFuelLevel() < 2) then
  243.             error("Insufficient fuel")
  244.         end
  245.                 local start = vector.new(gps.locate())
  246.                 while(turtle.detect()) do
  247.                         turtle.dig()
  248.                 end
  249.                 turtle.forward()
  250.                 local current = vector.new(gps.locate())
  251.                 turtle.back()
  252.                 if(start.z - current.z > 0) then
  253.                         heading = 'N'
  254.                 elseif (start.z - current.z < 0) then
  255.                         heading = 'S'
  256.                 end
  257.                 if(start.x - current.x > 0) then
  258.                         heading = 'W'
  259.                 elseif (start.x - current.x < 0) then
  260.                         heading = 'E'
  261.                 end
  262.         end
  263.  
  264.     if(heading ~= newHeading) then
  265.         if(newHeading == 'N' or newHeading == 'n') then
  266.             if(heading == 'S' or heading == 's') then
  267.                 turtle.turnLeft()
  268.                 turtle.turnLeft()
  269.             end
  270.             if(heading == 'E' or heading == 'e') then
  271.                 turtle.turnLeft()
  272.             end
  273.             if(heading == 'W' or heading == 'w') then
  274.                 turtle.turnRight()
  275.             end
  276.         end
  277.         if(newHeading == 'E' or newHeading == 'e') then
  278.             if(heading == 'S' or heading == 's') then
  279.                 turtle.turnLeft()
  280.             end
  281.             if(heading == 'N' or heading == 'n') then
  282.                 turtle.turnRight()
  283.             end
  284.             if(heading == 'W' or heading == 'w') then
  285.                 turtle.turnRight()
  286.                 turtle.turnRight()
  287.             end
  288.         end
  289.         if(newHeading == 'S' or newHeading == 's') then
  290.             if(heading == 'N' or heading == 'n') then
  291.                 turtle.turnLeft()
  292.                 turtle.turnLeft()
  293.             end
  294.             if(heading == 'E' or heading == 'e') then
  295.                 turtle.turnRight()
  296.             end
  297.             if(heading == 'W' or heading == 'w') then
  298.                 turtle.turnLeft()
  299.             end
  300.         end
  301.         if(newHeading == 'W' or newHeading == 'w') then
  302.             if(heading == 'S' or heading == 's') then
  303.                 turtle.turnRight()
  304.             end
  305.             if(heading == 'E' or heading == 'e') then
  306.                 turtle.turnLeft()
  307.                 turtle.turnLeft()
  308.             end
  309.             if(heading == 'N' or heading == 'n') then
  310.                 turtle.turnLeft()
  311.             end
  312.         end
  313.     end
  314. end
  315.  
  316. -- Function to go to a specific location --
  317. function goToLocation(location,heading)
  318.     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
  319.         heading = getHeading()
  320.     end
  321.    
  322.     local current = vector.new(gps.locate())
  323.     if(location.x ~= current.x or location.y ~= current.y or location.z ~= current.z) then
  324.         if(turtle.getFuelLevel() < 2) then
  325.             error("Insufficient fuel")
  326.         end
  327.     end
  328.     while(location.y ~= current.y) do
  329.         if(location.y > current.y) then
  330.             while(turtle.detectUp()) do
  331.                 turtle.digUp()
  332.             end
  333.             turtle.up()
  334.         else
  335.             while(turtle.detectDown()) do
  336.                 turtle.digDown()
  337.             end
  338.             turtle.down()
  339.         end
  340.         current = vector.new(gps.locate())
  341.     end
  342.    
  343.     while(location.z ~= current.z) do
  344.         if(location.z > current.z) then
  345.             setHeading(heading,'S')
  346.             heading = 'S'
  347.         elseif(location.z < current.z) then
  348.             setHeading(heading,'N')
  349.             heading = 'N'
  350.         end
  351.         while(turtle.detect()) do
  352.             turtle.dig()
  353.         end
  354.         turtle.forward()
  355.         current = vector.new(gps.locate())
  356.     end
  357.  
  358.     while(location.x ~= current.x) do
  359.         if(location.x > current.x) then
  360.             setHeading(heading,'E')
  361.             heading = 'E'
  362.         elseif(location.x < current.x) then
  363.             setHeading(heading,'W')
  364.             heading = 'W'
  365.         end
  366.         while(turtle.detect()) do
  367.             turtle.dig()
  368.         end
  369.         turtle.forward()
  370.         current = vector.new(gps.locate())
  371.     end
  372.     return heading
  373. end
  374.  
  375. -- Function to decide whether to mine whole ore vein based on priorities list --
  376. function shouldMineWhole(istr,xlist) -- must add functionality for flagging ores
  377.     for key,value in pairs(xlist) do
  378.         if(istr == value[1] and value[5] == "1") then
  379.             return true
  380.         end
  381.     end
  382.     return false
  383. end
  384.  
  385. -- Function to mine entire ore pocket --
  386. function mineVein(start,moves,back,xtable) -- This function needs work on flagging ore, also could be significantly more efficient
  387.     --Establish current GPS location--
  388.     local current = vector.new(gps.locate())
  389.     --Check for sufficient fuel, if not, try to refuel, if refuel fails, function return false and
  390.     --recursion tree will collapse with turtle returning to where it started--
  391.     if(not sufficientFuel(start,current,turtle.getFuelLevel(),5 + moves)) then
  392.         if(not consumeFuel(400)) then
  393.             return -2
  394.         end
  395.     end
  396.     --Check for inventory space, if no inventory space, try to create some. if no space can be created,
  397.     --function return false and recursion tree will collapse, with turtle returning to where it started.
  398.     if(not invSpace()) then
  399.         sortInv()
  400.         clrInv(xtable)
  401.         if(not invSpace()) then
  402.             return -1
  403.         end
  404.     end
  405.    
  406.     --Check above turtle for ores--
  407.     local success,data = turtle.inspect()
  408.     if(success) then
  409.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  410.         --print(istr)
  411.         if(shouldMineWhole(istr,xtable)) then
  412.             turtle.dig()
  413.             turtle.forward()
  414.             mineVein(start,moves+1,false,xtable)
  415.             turtle.back()
  416.         end
  417.     end
  418.     if(moves == 0) then
  419.         if(current.y == start.y + 1) then
  420.             local success,data = turtle.inspectUp()
  421.             if(success) then
  422.                 local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  423.                 --print(istr)
  424.                 if(shouldMineWhole(istr,xtable)) then
  425.                     turtle.digUp()
  426.                     turtle.up()
  427.                     mineVein(start,moves+1,true,xtable)
  428.                     turtle.down()
  429.                 end
  430.             end
  431.         end
  432.         if(current.y == start.y) then
  433.             local success,data = turtle.inspectDown()
  434.             if(success) then
  435.                 local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  436.                 --print(istr)
  437.                 if(shouldMineWhole(istr,xtable)) then
  438.                     turtle.digDown()
  439.                     turtle.down()
  440.                     mineVein(start,moves+1,true,xtable)
  441.                     turtle.up()
  442.                 end
  443.             end
  444.         end
  445.     end
  446.    
  447.     --will ensure turtle does not check sides on start.
  448.     if(moves < 1) then
  449.         return 1
  450.     end
  451.     turtle.turnLeft()
  452.     local success,data = turtle.inspect()
  453.     if(success) then
  454.         local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  455.         --print(istr)
  456.         if(shouldMineWhole(istr,xtable)) then
  457.             turtle.dig()
  458.             turtle.forward()
  459.             mineVein(start,moves+1,false,xtable)
  460.             turtle.back()
  461.         end
  462.     end
  463.     if(back) then
  464.         turtle.turnLeft()
  465.         local success,data = turtle.inspect()
  466.         if(success) then
  467.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  468.             --print(istr)
  469.             if(shouldMineWhole(istr,xtable)) then
  470.                 turtle.dig()
  471.                 turtle.forward()
  472.                 mineVein(start,moves+1,false,xtable)
  473.                 turtle.back()
  474.             end
  475.         end
  476.         turtle.turnLeft()
  477.         local success,data = turtle.inspect()
  478.         if(success) then
  479.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  480.             --print(istr)
  481.             if(shouldMineWhole(istr,xtable)) then
  482.                 turtle.dig()
  483.                 turtle.forward()
  484.                 mineVein(start,moves+1,false,xtable)
  485.                 turtle.back()
  486.             end
  487.         end
  488.         turtle.turnLeft()
  489.         local success,data = turtle.inspectUp()
  490.         if(success) then
  491.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  492.             --print(istr)
  493.             if(shouldMineWhole(istr,xtable)) then
  494.                 turtle.digUp()
  495.                 turtle.up()
  496.                 mineVein(start,moves+1,true,xtable)
  497.                 turtle.down()
  498.             end
  499.         end
  500.         local success,data = turtle.inspectDown()
  501.         if(success) then
  502.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  503.             --print(istr)
  504.             if(shouldMineWhole(istr,xtable)) then
  505.                 turtle.digDown()
  506.                 turtle.down()
  507.                 mineVein(start,moves+1,true,xtable)
  508.                 turtle.up()
  509.             end
  510.         end
  511.     else
  512.         turtle.turnRight()
  513.         turtle.turnRight()
  514.         local success,data = turtle.inspect()
  515.         if(success) then
  516.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  517.             --print(istr)
  518.             if(shouldMineWhole(istr,xtable)) then
  519.                 turtle.dig()
  520.                 turtle.forward()
  521.                 mineVein(start,moves+1,false,xtable)
  522.                 turtle.back()
  523.             end
  524.         end
  525.         turtle.turnLeft()
  526.         local success,data = turtle.inspectUp()
  527.         if(success) then
  528.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  529.             --print(istr)
  530.             if(shouldMineWhole(istr,xtable)) then
  531.                 turtle.digUp()
  532.                 turtle.up()
  533.                 mineVein(start,moves+1,true,xtable)
  534.                 turtle.down()
  535.             end
  536.         end
  537.         local success,data = turtle.inspectDown()
  538.         if(success) then
  539.             local istr = string.sub(data.name,string.find(data.name,":",0)+1)
  540.             --print(istr)
  541.             if(shouldMineWhole(istr,xtable)) then
  542.                 turtle.digDown()
  543.                 turtle.down()
  544.                 mineVein(start,moves+1,true,xtable)
  545.                 turtle.up()
  546.             end
  547.         end
  548.     end
  549.     return 1
  550. end
  551.  
  552. -- Function to mine branch --
  553. function mineBranch(branchStart,branchHeading,currentHeading,branchLimit,fuelRemainder,plist,torchLength) -- Still needs optimization and functions to replace repeated blocks
  554.     -- Search for GPS signal --
  555.     local current = vector.new(gps.locate())
  556.     if(current.x == nil or current.y == nil or current.z == nil) then
  557.         -- GPS signal could not be established --
  558.         error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  559.     end
  560.     -- GPS signal established --
  561.     if(not sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
  562.         if(not consumeFuel(4000)) then
  563.             error("Insufficient fuel!")
  564.         end
  565.     end
  566.     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
  567.         currentHeading = getHeading()
  568.     end
  569.    
  570.     --print("Heading: ", currentHeading)
  571.    
  572.     if(branchStart.y ~= current.y) then
  573.         -- If y level is not the same --
  574.         currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z), currentHeading)
  575.         current = vector.new(gps.locate())
  576.     end
  577.     -- Ensure z level is the same as starting y level --
  578.     if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's') then
  579.         if(branchStart.z ~= current.z) then
  580.             -- If z level is not the same --
  581.             currentHeading = goToLocation(vector.new(branchStart.x,current.y,current.z), currentHeading)
  582.             current = vector.new(gps.locate())
  583.         end
  584.     end
  585.     -- Ensure x level is the same as starting y level --
  586.     if(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then
  587.         if(branchStart.x ~= current.x) then
  588.             -- If x level is not the same --
  589.             currentHeading = goToLocation(vector.new(current.x,current.y,branchStart.z), currentHeading)
  590.             current = vector.new(gps.locate())
  591.         end
  592.     end
  593.     setHeading(currentHeading,branchHeading)
  594.     currentHeading = branchHeading
  595.     --[[
  596.         Done
  597.         0   : Not done, still mining
  598.         1   : Is done, mining was successful
  599.         -1  : Inventory full
  600.         -2  : Insufficient fuel
  601.     ]]--
  602.     done = 0
  603.     while(done == 0) do
  604.         current = vector.new(gps.locate())
  605.         if(current.x == nil or current.y == nil or current.z == nil) then
  606.             -- GPS signal could not be established --
  607.             error("Could not establish GPS signal, please ensure GPS servers are running and try again")
  608.         else
  609.             -- Ensure distance from branchStart is less than branchLimit --
  610.             -- Along the North/South line --
  611.             if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's')then
  612.                 -- Check if distance from branchStart is within branchLimit --
  613.                 if(math.abs(branchStart.z - current.z)>=branchLimit) then
  614.                     -- Distance from branchStart is not within branchLimit --
  615.                     currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
  616.                     currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  617.                     done = 1
  618.                 else
  619.                     -- Distance from branchStart is within branchLimit --
  620.                     -- Check for sufficient fuel --
  621.                     if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
  622.                         if(invSpace()) then
  623.                             if(current.y == branchStart.y) then
  624.                                 turtle.turnLeft()
  625.                                 if(done == 1) then
  626.                                     done = mineVein(branchStart,0,false,plist)
  627.                                 end
  628.                                 turtle.turnRight()
  629.                                 turtle.turnRight()
  630.                                 if(done == 1) then
  631.                                     done = mineVein(branchStart,0,false,plist)
  632.                                 end
  633.                                 turtle.turnLeft()
  634.                                 while(turtle.detect()) do
  635.                                     turtle.dig()
  636.                                 end
  637.                                 turtle.forward()
  638.                                 turtle.turnLeft()
  639.                                 if(done == 1) then
  640.                                     done = mineVein(branchStart,0,false,plist)
  641.                                 end
  642.                                 turtle.turnRight()
  643.                                 turtle.turnRight()
  644.                                 if(done == 1) then
  645.                                     done = mineVein(branchStart,0,false,plist)
  646.                                 end
  647.                                 turtle.turnLeft()
  648.                                 while(turtle.detectUp()) do
  649.                                     turtle.digUp()
  650.                                 end
  651.                                 turtle.up()
  652.                             elseif(current.y - 1 == branchStart.y) then
  653.                                 turtle.turnLeft()
  654.                                 if(done == 1) then
  655.                                     done = mineVein(branchStart,0,false,plist)
  656.                                 end
  657.                                 turtle.turnRight()
  658.                                 turtle.turnRight()
  659.                                 if(done == 1) then
  660.                                     done = mineVein(branchStart,0,false,plist)
  661.                                 end
  662.                                 turtle.turnLeft()
  663.                                 while(turtle.detect()) do
  664.                                     turtle.dig()
  665.                                 end
  666.                                 turtle.forward()
  667.                                 turtle.turnLeft()
  668.                                 if(done == 1) then
  669.                                     done = mineVein(branchStart,0,false,plist)
  670.                                 end
  671.                                 turtle.turnRight()
  672.                                 turtle.turnRight()
  673.                                 if(done == 1) then
  674.                                     done = mineVein(branchStart,0,false,plist)
  675.                                 end
  676.                                 turtle.turnLeft()
  677.                                 while(turtle.detectDown()) do
  678.                                     turtle.digDown()
  679.                                 end
  680.                                 turtle.down()
  681.                             else
  682.                                 currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading)
  683.                             end
  684.                         else
  685.                             sortInv()
  686.                             clrInv(plist)
  687.                             if(not invSpace()) then
  688.                                 currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
  689.                                 currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  690.                                 done = -1
  691.                             end
  692.                         end
  693.                     else
  694.                         if(not consumeFuel(4000)) then
  695.                             currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading)
  696.                             currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  697.                             done = -2
  698.                         end
  699.                     end
  700.                 end
  701.             -- Along the East/West line --
  702.             elseif(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then
  703.                 -- Check if distance from branchStart is within branchLimit --
  704.                 if(math.abs(branchStart.x - current.x)>=branchLimit) then
  705.                     -- Distance from branchStart is not within branchLimit --
  706.                     currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
  707.                     currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  708.                     done = 1
  709.                 else
  710.                     -- Distance from branchStart is within branchLimit --
  711.                     -- Check for sufficient fuel --
  712.                     if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then
  713.                         if(invSpace()) then
  714.                             if(current.y == branchStart.y) then
  715.                                 turtle.turnLeft()
  716.                                 if(done == 1) then
  717.                                     done = mineVein(branchStart,0,false,plist)
  718.                                 end
  719.                                 turtle.turnRight()
  720.                                 turtle.turnRight()
  721.                                 if(done == 1) then
  722.                                     done = mineVein(branchStart,0,false,plist)
  723.                                 end
  724.                                 turtle.turnLeft()
  725.                                 while(turtle.detect()) do
  726.                                     turtle.dig()
  727.                                 end
  728.                                 turtle.forward()
  729.                                 turtle.turnLeft()
  730.                                 if(done == 1) then
  731.                                     done = mineVein(branchStart,0,false,plist)
  732.                                 end
  733.                                 turtle.turnRight()
  734.                                 turtle.turnRight()
  735.                                 if(done == 1) then
  736.                                     done = mineVein(branchStart,0,false,plist)
  737.                                 end
  738.                                 turtle.turnLeft()
  739.                                 while(turtle.detectUp()) do
  740.                                     turtle.digUp()
  741.                                 end
  742.                                 turtle.up()
  743.                             elseif(current.y - 1 == branchStart.y) then
  744.                                 turtle.turnLeft()
  745.                                 if(done == 1) then
  746.                                     done = mineVein(branchStart,0,false,plist)
  747.                                 end
  748.                                 turtle.turnRight()
  749.                                 turtle.turnRight()
  750.                                 if(done == 1) then
  751.                                     done = mineVein(branchStart,0,false,plist)
  752.                                 end
  753.                                 turtle.turnLeft()
  754.                                 while(turtle.detect()) do
  755.                                     turtle.dig()
  756.                                 end
  757.                                 turtle.forward()
  758.                                 turtle.turnLeft()
  759.                                 if(done == 1) then
  760.                                     done = mineVein(branchStart,0,false,plist)
  761.                                 end
  762.                                 turtle.turnRight()
  763.                                 turtle.turnRight()
  764.                                 if(done == 1) then
  765.                                     done = mineVein(branchStart,0,false,plist)
  766.                                 end
  767.                                 turtle.turnLeft()
  768.                                 while(turtle.detectDown()) do
  769.                                     turtle.digDown()
  770.                                 end
  771.                                 turtle.down()
  772.                             else
  773.                                 currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading)
  774.                             end
  775.                         else
  776.                             sortInv()
  777.                             clrInv(plist)
  778.                             if(not invSpace()) then
  779.                                 currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
  780.                                 currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  781.                                 done = -1
  782.                             end
  783.                         end
  784.                     else
  785.                         if(not consumeFuel(4000)) then
  786.                             currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading)
  787.                             currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading)
  788.                             done = -2
  789.                         end
  790.                     end
  791.                 end
  792.             end
  793.         end
  794.     end
  795.     return done
  796. end
  797. x = lstToTable()
  798. print(mineBranch(vector.new(gps.locate()),getHeading(),"",5,10,x,0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement