Viproz

Beta ASTU Mining

Nov 27th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.08 KB | None | 0 0
  1. function mySplit(inputstr, sep) -- this function is not mine, found on google, thanks to user973713 : http://stackoverflow.com/questions/1426954/split-string-in-lua
  2.         if sep == nil then
  3.                 sep = "%s"
  4.         end
  5.         t={} ; i=1
  6.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  7.                 t[i] = str
  8.                 i = i + 1
  9.         end
  10.         return t
  11. end
  12.  
  13. -- If you find any errors on the code (even english mistake) please report it on my CC post : http://www.computercraft.info/forums2/index.php?/topic/6454-
  14.  
  15. -- If you want to help me for coding you can add me on skype (pseudo : Viproz)
  16.  
  17. -- Si vous etes franΓ§ais et bien sachez que vous n'etes pas le seul sur ce forum !
  18.  
  19. securityMode = true -- true done 1*2 tunnels and false 1*1, usefull to have a alot less of cobble.
  20.  
  21. -- Give here the position of the start
  22. x = -200
  23. y = 90
  24. z = 50
  25.  
  26. world = "mine" -- Only used for http turtles.
  27.  
  28. -- not implemented yet, I'm working on the php code
  29. urlManager = false -- Use for the http gestion of the turtle, tu it to false to desactivate it. Put your php page to enable it (ex : http://google.com/CC.php
  30.  
  31. -- To enable a ore detection you need to put ["block id"] = true.
  32. -- To desactivate a ore detection just replace true by false.
  33.  
  34. ores = {["14"] = true, -- Gold
  35.         ["15"] = true, -- Iron
  36.         ["16"] = true, -- Coal
  37.         ["21"] = true, -- Lapis Lazuli
  38.         ["49"] = false, -- Obsidian
  39.         ["56"] = true, -- Dimond
  40.         ["73"] = true, -- Redstone
  41.         ["129"] = true, -- Emeralt
  42.         ["244"] = true, -- Mod Ores
  43.         ["245"] = true,
  44.         ["248"] = true,
  45.         ["249"] = true,
  46.         ["688"] = true,
  47.         ["703"] = true,
  48.         ["2001"] = true}
  49.  
  50. -- Get the IDetector Addon
  51. local detectID = peripheral.wrap("right")
  52.  
  53.  
  54. print("Starting minning !")
  55.  
  56. -- cannot use '-' with the split function so change the zero to 10'000
  57. zeroX = x + 10000
  58. zeroY = y + 10000
  59. zeroZ = z + 10000
  60.  
  61. -- reset the orient var
  62. local orient = 0
  63.  
  64. if fs.exists("MiningPos") then
  65.     orientationFile = fs.open("MiningOrtientation", "r")
  66.     ori = tonumber(orientationFile.readLine())
  67.     while ori > 0 do
  68.         turtle.turnLeft()
  69.         ori = ori - 1
  70.     end
  71.     orientationFile.close()
  72.    
  73.     rFile = fs.open("MiningPos", "r")
  74.     crashPosLine = rFile.readLine()
  75.     rFile.close()
  76.  
  77.     crashPos = mySplit(crashPosLine, ",")
  78.     myX = tonumber(crashPos[1])
  79.     myY = tonumber(crashPos[2])
  80.     myZ = tonumber(crashPos[3])
  81.    
  82.     backOnRoad()
  83. else
  84.     myX = zeroX
  85.     myY = zeroY
  86.     myZ = zeroZ
  87. end
  88.  
  89.  
  90. local function tryRefuel() -- The refuel function when fuel is low
  91.     for n=1,16 do
  92.         if turtle.getItemCount(n) > 0 then
  93.             turtle.select(n)
  94.             if turtle.refuel() then
  95.                 turtle.select(1)
  96.                 return true
  97.             end
  98.         end
  99.     end
  100.     turtle.select(1)
  101.     return false
  102. end
  103.  
  104.  
  105.  
  106. local orientFile = fs.open("MiningOrtientation", "w") -- the file will always be open, i don't know if it's very bad
  107.  
  108. function turnRight()
  109.     if turtle.turnRight() then
  110.         orient = (orient + 1) % 4
  111.         orientFile.seek(0)
  112.         orientFile.write(orient)
  113.         return true
  114.     end
  115. end
  116. turnRight()
  117.  
  118. local function isFull() -- check if the turtle is full
  119.     for n=16,1,-1 do
  120.         if turtle.getItemCount(n) < 1 then
  121.             turtle.select(1)
  122.             return false -- if a free slot is found
  123.         end
  124.     end
  125.     return true -- if not
  126. end
  127.  
  128. local function OreDown() -- detect if an ore is down
  129.     if not turtle.detectDown() then
  130.         return false -- if no block, no ore
  131.     end
  132.     id = detectID.IDetectDown() -- use the IDetecting module
  133.     if ores[id..""] == true then
  134.         return true
  135.     else
  136.         return false
  137.     end
  138. end
  139.  
  140. local function OreUp()
  141.     if not turtle.detectUp() then
  142.         return false
  143.     end
  144.     id = detectID.IDetectUp()
  145.     if ores[id..""] == true then
  146.         return true
  147.     else
  148.         return false
  149.     end
  150. end
  151.  
  152. local function OreForward()
  153.     if not turtle.detect() then
  154.         return false
  155.     end
  156.     id = detectID.IDetect()
  157.     if ores[id..""] == true then
  158.         return true
  159.     else
  160.         return false
  161.     end
  162. end
  163.  
  164. local function OreRight() -- Cannot use the function alone, won't return to the right facing
  165.     turtle.turnRight()
  166.    
  167.     orientationRightFile = fs.open("MiningOrtientation", "w")
  168.     orientationRightFile.write("1")
  169.     orientationRightFile.close()
  170.    
  171.     if not turtle.detect() then
  172.         return false
  173.     end
  174.     id = detectID.IDetect()
  175.     if ores[id..""] == true then
  176.         return true
  177.     else
  178.         return false
  179.     end
  180. end
  181.  
  182. local function OreBehind() -- Cannot use the function alone, won't return to the right facing
  183.     turtle.turnRight()
  184.    
  185.     orientationRightFile = fs.open("MiningOrtientation", "w")
  186.     orientationRightFile.write("2")
  187.     orientationRightFile.close()
  188.    
  189.     if not turtle.detect() then
  190.         return false
  191.     end
  192.     id = detectID.IDetect()
  193.     if ores[id..""] == true then
  194.         return true
  195.     else
  196.         return false
  197.     end
  198. end
  199.  
  200. local function OreLeft() -- Cannot use the function alone, won't return to the right facing
  201.     turtle.turnRight()
  202.    
  203.     orientationRightFile = fs.open("MiningOrtientation", "w")
  204.     orientationRightFile.write("3")
  205.     orientationRightFile.close()
  206.    
  207.     if not turtle.detect() then
  208.         return false
  209.     end
  210.     id = detectID.IDetect()
  211.     if ores[id..""] == 1 then
  212.         return true
  213.     else
  214.         return false
  215.     end
  216. end
  217.  
  218. local function scanOre()
  219.  
  220.     if OreDown() then -- if ore is found, save it into our string
  221.         posOfOres = posOfOres..myZ..","..(myY - 1)..","..myX..";"
  222.     end
  223.     if OreUp() then -- if ore is found, save it into our string
  224.         posOfOres = posOfOres..myZ..","..(myY + 1)..","..myX..";"
  225.     end
  226.     if OreForward() then -- if ore is found, save it into our string
  227.         posOfOres = posOfOres..(myZ + 1)..","..myY..","..myX..";"
  228.     end
  229.     if OreRight() then -- if ore is found, save it into our string
  230.         posOfOres = posOfOres..myZ..","..myY..","..(myX + 1)..";"
  231.     end
  232.     if OreBehind() then -- if ore is found, save it into our string
  233.         posOfOres = posOfOres..(myZ - 1)..","..myY..","..myX..";"
  234.     end
  235.     if OreLeft() then -- if ore is found, save it into our string
  236.         posOfOres = posOfOres..myZ..","..myY..","..(myX - 1)..";"
  237.     end
  238.     turtle.turnRight()
  239.    
  240.     orientationRightFile = fs.open("MiningOrtientation", "w")
  241.     orientationRightFile.write("0")
  242.     orientationRightFile.close()
  243. end
  244.  
  245. local function backOnRoad() -- use to return on the main road, don't need to be on the same z coord.
  246.     while myY < zeroY do -- if that, go up
  247.         if turtle.detectUp() then
  248.             turtle.digUp()
  249.             if turtle.up() then
  250.                 myY = myY + 1
  251.             end
  252.         else
  253.             if turtle.up() then
  254.                 myY = myY + 1
  255.             end
  256.         end
  257.     end
  258.    
  259.     while myY > zeroY do -- if that go down
  260.         if turtle.detectDown() then
  261.             turtle.digDown()
  262.             if turtle.down() then
  263.                 myY = myY - 1
  264.             end
  265.         else
  266.             if turtle.down() then
  267.                 myY = myY - 1
  268.             end
  269.         end
  270.     end
  271.    
  272.     if (myX < zeroX) then
  273.         turtle.turnRight()
  274.         orientationRightFile = fs.open("MiningOrtientation", "w")
  275.         orientationRightFile.write("1")
  276.         orientationRightFile.close()
  277.        
  278.         while myX < zeroX do -- if that go right
  279.             if turtle.detect() then
  280.                 turtle.dig()
  281.                 if turtle.forward() then
  282.                     myX = myX + 1
  283.                 end
  284.             else
  285.                 if turtle.forward() then
  286.                     myX = myX + 1
  287.                 end
  288.             end
  289.         end
  290.         turtle.turnLeft()
  291.         orientationRightFile = fs.open("MiningOrtientation", "w")
  292.         orientationRightFile.write("0")
  293.         orientationRightFile.close()
  294.     end
  295.    
  296.     if (myX > zeroX) then
  297.         turtle.turnLeft()
  298.         orientationRightFile = fs.open("MiningOrtientation", "w")
  299.         orientationRightFile.write("3")
  300.         orientationRightFile.close()
  301.    
  302.         while myX > zeroX do -- if that go left
  303.             if turtle.detect() then
  304.                 turtle.dig()
  305.                 if turtle.forward() then
  306.                     myX = myX - 1
  307.                 end
  308.             else
  309.                 if turtle.forward() then
  310.                     myX = myX - 1
  311.                 end
  312.             end
  313.        
  314.         end
  315.         turtle.turnRight()
  316.         orientationRightFile = fs.open("MiningOrtientation", "w")
  317.         orientationRightFile.write("0")
  318.         orientationRightFile.close()
  319.     end
  320. end
  321.  
  322. local function mineBlock(indexBest) -- function used to mine an ore, take indexBest who is the case of the table who is the nearest
  323.     blockAMiner = mySplit(oreData[indexBest], ",") -- separate coords into a table
  324.    
  325.     if myY < tonumber(blockAMiner[2]) then -- to number function is need to compare a string contains an int with an int
  326.         if turtle.detectUp() then
  327.             turtle.digUp()
  328.             if turtle.up() then
  329.                 myY = myY + 1
  330.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "") -- remove the actual pos of the turtle is very important
  331.                 return true
  332.             end
  333.         else
  334.             if turtle.up() then
  335.                 myY = myY + 1
  336.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  337.                 return true
  338.             end
  339.         end
  340.     end
  341.     if myY > tonumber(blockAMiner[2]) then
  342.         if turtle.detectDown() then
  343.             turtle.digDown()
  344.             if turtle.down() then
  345.                 myY = myY - 1
  346.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  347.                 return true
  348.             end
  349.         else
  350.             if turtle.down() then
  351.                 myY = myY - 1
  352.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  353.                 return true
  354.             end
  355.         end
  356.     end
  357.     if myX < tonumber(blockAMiner[3]) then
  358.         turtle.turnRight()
  359.         orientationRightFile = fs.open("MiningOrtientation", "w")
  360.         orientationRightFile.write("1")
  361.         orientationRightFile.close()
  362.        
  363.         if turtle.detect() then
  364.             turtle.dig()
  365.             if turtle.forward() then
  366.                 myX = myX + 1
  367.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  368.                 turtle.turnLeft()
  369.                 orientationRightFile = fs.open("MiningOrtientation", "w")
  370.                 orientationRightFile.write("0")
  371.                 orientationRightFile.close()
  372.        
  373.                 return true
  374.             end
  375.         else
  376.             if turtle.forward() then
  377.                 myX = myX + 1
  378.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  379.                 turtle.turnLeft()
  380.                 orientationRightFile = fs.open("MiningOrtientation", "w")
  381.                 orientationRightFile.write("0")
  382.                 orientationRightFile.close()
  383.        
  384.                 return true
  385.             end
  386.         end
  387.     end
  388.     if myX > tonumber(blockAMiner[3]) then
  389.         turtle.turnLeft()
  390.         orientationRightFile = fs.open("MiningOrtientation", "w")
  391.         orientationRightFile.write("3")
  392.         orientationRightFile.close()
  393.        
  394.         if turtle.detect() then
  395.             turtle.dig()
  396.             if turtle.forward() then
  397.                 myX = myX - 1
  398.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  399.                 turtle.turnRight()
  400.                 orientationRightFile = fs.open("MiningOrtientation", "w")
  401.                 orientationRightFile.write("0")
  402.                 orientationRightFile.close()
  403.        
  404.                 return true
  405.             end
  406.         else
  407.             if turtle.forward() then
  408.                 myX = myX - 1
  409.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  410.                 turtle.turnRight()
  411.                 orientationRightFile = fs.open("MiningOrtientation", "w")
  412.                 orientationRightFile.write("0")
  413.                 orientationRightFile.close()
  414.        
  415.                 return true
  416.             end
  417.         end
  418.     end
  419.     if myZ < tonumber(blockAMiner[1]) then
  420.         if turtle.detect() then
  421.             turtle.dig()
  422.             if turtle.forward() then
  423.                 myZ = myZ + 1
  424.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  425.                 return true
  426.             end
  427.         else
  428.             if turtle.forward() then
  429.                 myZ = myZ + 1
  430.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  431.                 return true
  432.             end
  433.         end
  434.     end
  435.     if myZ > tonumber(blockAMiner[1]) then
  436.         turtle.turnRight()
  437.         turtle.turnRight()
  438.         if turtle.detect() then
  439.             turtle.dig()
  440.             if turtle.forward() then
  441.                 myZ = myZ - 1
  442.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  443.                 turtle.turnLeft()
  444.                 turtle.turnLeft()
  445.                 return true
  446.             end
  447.         else
  448.             if turtle.forward() then
  449.                 myZ = myZ - 1
  450.                 posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
  451.                 turtle.turnLeft()
  452.                 turtle.turnLeft()
  453.                 return true
  454.             end
  455.         end
  456.     end
  457. end
  458.  
  459.  
  460. local function miner() -- the big function with a french word 'miner' who say mine ^^
  461.     posOfOres = string.char() -- clear the posOfOres var
  462.    
  463.    
  464.     while 1 do -- infinite loop but stop if there is no ores left, see the next comment
  465.        
  466.         scanOre() -- use the scan ore func to scan the environement
  467.        
  468.         if posOfOres == "" then -- if there is no ore left, stop the loop
  469.             break
  470.         end
  471.        
  472.         print(posOfOres) -- a small debug think
  473.        
  474.         oreData = mySplit(posOfOres, ";") -- plit all the diffrents pos of ores, the ; is used as separtor
  475.        
  476.         oreClosest = {10000000, 0} -- initialing the table with a hight value, so it's verrrrrryyy far
  477.         for i=1, #oreData do -- here we are going to do the for loop for each ore in the string posOfOres
  478.           tablePosMine = mySplit(oreData[i], ",") -- plit to have the x,y,z
  479.          
  480.           -- i'm actually very proud of that, that's caculate the nearest ore perfectly !
  481.           score = math.abs(tablePosMine[1] - myZ) + math.abs(tablePosMine[2] - myY) + math.abs(tablePosMine[3] - myX)
  482.          
  483.           if score < oreClosest[1] then -- if the result is less than the old result it's closer so save that into the table
  484.             oreClosest = {score, i}
  485.           end
  486.         end
  487.        
  488.         print("Now pos : "..myZ..","..myY..","..myX..";") -- some debug stuff again
  489.         print("Go to : "..oreData[oreClosest[2]])
  490.        
  491.         mineBlock(oreClosest[2]) -- once we go out of the for loop, we found the closest ore so just mine it !
  492.        
  493.     end
  494.    
  495.     backOnRoad() -- once we go out of the loop, if we move we need to return in the good way
  496.    
  497.     if securityMode == true then -- secutity mode ! Just for 1*2 tunnels ^^
  498.         turtle.digUp()
  499.     end
  500.    
  501.     -- writing comments is good, just realise that function was not on the best place ;)
  502.     if turtle.detect() then -- check if there is a block in front
  503.         turtle.dig() -- if true, dig
  504.     end
  505.    
  506.     if turtle.forward() then -- once all the check stuff it's done, just continue !
  507.         myZ = myZ + 1 -- increment the var same as usual, realy need myZ++ func in lua...
  508.     end
  509. end
  510.  
  511. local function back() -- we tutch to the end here, go back to home whan have trouble or whan it's full
  512.     turtle.turnRight()
  513.     turtle.turnRight()
  514.    
  515.     while myZ > zeroZ do
  516.         if turtle.detect() then
  517.             turtle.dig()
  518.             if turtle.forward() then
  519.                 myZ = myZ - 1
  520.             end
  521.         else
  522.             if turtle.forward() then
  523.                 myZ = myZ - 1
  524.             end
  525.         end
  526.     end
  527.    
  528.     turtle.turnRight()
  529.     turtle.turnRight()
  530. end
  531.  
  532. local function putInChest() -- very bad func actually, if there is no chest it will put the stone on floor...
  533.     if not turtle.back() then -- the chest is one block back
  534.         return false -- allways check if the movement is corectly executed
  535.     end
  536.    
  537.     if not turtle.detectDown() then
  538.         return false
  539.     end
  540.    
  541.     for i = 1, 16 do
  542.         turtle.select(i)
  543.         if not turtle.dropDown() then
  544.             print("Chest full !")
  545.             turtle.forward()
  546.             return false
  547.         end
  548.     end
  549.     turtle.forward()
  550.     return true
  551. end
  552.  
  553. while 1 do -- we are now on the main loop !
  554.     neededFuel = myZ + 200 -- , the +200 is because i don't check the fuel level on the mineral loop, you can easly go to the nether and get fuel with lava buckets (with label, turtles don't loose the fuel level when breack !)
  555.    
  556.     if turtle.getFuelLevel() > neededFuel and not isFull() then -- check if we have enough fuel to return
  557.         miner() -- if all is good, execute my frenglish function !
  558.     elseif isFull() and myZ > zeroZ then -- check if it's full and not already tried to put items in the chest
  559.         print("Inventory full, return to base.")
  560.         back() -- go back to home !
  561.     elseif isFull() then -- if the turtle is already on the zero pos execute
  562.         if not putInChest() then
  563.             print("Error with the chest")
  564.             return -- return if i have an error
  565.         end
  566.     elseif turtle.getFuelLevel() <= neededFuel then
  567.         if not tryRefuel() then -- if hasn't enouth fuel to continue, go back and stop...
  568.             back()
  569.             return
  570.         end
  571.     end
  572. end
Advertisement
Add Comment
Please, Sign In to add comment