Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - function mySplit(inputstr, sep) -- this function is not mine, found on google, thanks to user973713 : http://stackoverflow.com/questions/1426954/split-string-in-lua
 - if sep == nil then
 - sep = "%s"
 - end
 - t={} ; i=1
 - for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
 - t[i] = str
 - i = i + 1
 - end
 - return t
 - end
 - -- 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-
 - -- If you want to help me for coding you can add me on skype (pseudo : Viproz)
 - -- Si vous etes franΓ§ais et bien sachez que vous n'etes pas le seul sur ce forum !
 - securityMode = true -- true done 1*2 tunnels and false 1*1, usefull to have a alot less of cobble.
 - -- Give here the position of the start
 - x = -200
 - y = 90
 - z = 50
 - world = "mine" -- Only used for http turtles.
 - -- not implemented yet, I'm working on the php code
 - 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
 - -- To enable a ore detection you need to put ["block id"] = true.
 - -- To desactivate a ore detection just replace true by false.
 - ores = {["14"] = true, -- Gold
 - ["15"] = true, -- Iron
 - ["16"] = true, -- Coal
 - ["21"] = true, -- Lapis Lazuli
 - ["49"] = false, -- Obsidian
 - ["56"] = true, -- Dimond
 - ["73"] = true, -- Redstone
 - ["129"] = true, -- Emeralt
 - ["244"] = true, -- Mod Ores
 - ["245"] = true,
 - ["248"] = true,
 - ["249"] = true,
 - ["688"] = true,
 - ["703"] = true,
 - ["2001"] = true}
 - -- Get the IDetector Addon
 - local detectID = peripheral.wrap("right")
 - print("Starting minning !")
 - -- cannot use '-' with the split function so change the zero to 10'000
 - zeroX = x + 10000
 - zeroY = y + 10000
 - zeroZ = z + 10000
 - -- reset the orient var
 - local orient = 0
 - if fs.exists("MiningPos") then
 - orientationFile = fs.open("MiningOrtientation", "r")
 - ori = tonumber(orientationFile.readLine())
 - while ori > 0 do
 - turtle.turnLeft()
 - ori = ori - 1
 - end
 - orientationFile.close()
 - rFile = fs.open("MiningPos", "r")
 - crashPosLine = rFile.readLine()
 - rFile.close()
 - crashPos = mySplit(crashPosLine, ",")
 - myX = tonumber(crashPos[1])
 - myY = tonumber(crashPos[2])
 - myZ = tonumber(crashPos[3])
 - backOnRoad()
 - else
 - myX = zeroX
 - myY = zeroY
 - myZ = zeroZ
 - end
 - local function tryRefuel() -- The refuel function when fuel is low
 - for n=1,16 do
 - if turtle.getItemCount(n) > 0 then
 - turtle.select(n)
 - if turtle.refuel() then
 - turtle.select(1)
 - return true
 - end
 - end
 - end
 - turtle.select(1)
 - return false
 - end
 - local orientFile = fs.open("MiningOrtientation", "w") -- the file will always be open, i don't know if it's very bad
 - function turnRight()
 - if turtle.turnRight() then
 - orient = (orient + 1) % 4
 - orientFile.seek(0)
 - orientFile.write(orient)
 - return true
 - end
 - end
 - turnRight()
 - local function isFull() -- check if the turtle is full
 - for n=16,1,-1 do
 - if turtle.getItemCount(n) < 1 then
 - turtle.select(1)
 - return false -- if a free slot is found
 - end
 - end
 - return true -- if not
 - end
 - local function OreDown() -- detect if an ore is down
 - if not turtle.detectDown() then
 - return false -- if no block, no ore
 - end
 - id = detectID.IDetectDown() -- use the IDetecting module
 - if ores[id..""] == true then
 - return true
 - else
 - return false
 - end
 - end
 - local function OreUp()
 - if not turtle.detectUp() then
 - return false
 - end
 - id = detectID.IDetectUp()
 - if ores[id..""] == true then
 - return true
 - else
 - return false
 - end
 - end
 - local function OreForward()
 - if not turtle.detect() then
 - return false
 - end
 - id = detectID.IDetect()
 - if ores[id..""] == true then
 - return true
 - else
 - return false
 - end
 - end
 - local function OreRight() -- Cannot use the function alone, won't return to the right facing
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("1")
 - orientationRightFile.close()
 - if not turtle.detect() then
 - return false
 - end
 - id = detectID.IDetect()
 - if ores[id..""] == true then
 - return true
 - else
 - return false
 - end
 - end
 - local function OreBehind() -- Cannot use the function alone, won't return to the right facing
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("2")
 - orientationRightFile.close()
 - if not turtle.detect() then
 - return false
 - end
 - id = detectID.IDetect()
 - if ores[id..""] == true then
 - return true
 - else
 - return false
 - end
 - end
 - local function OreLeft() -- Cannot use the function alone, won't return to the right facing
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("3")
 - orientationRightFile.close()
 - if not turtle.detect() then
 - return false
 - end
 - id = detectID.IDetect()
 - if ores[id..""] == 1 then
 - return true
 - else
 - return false
 - end
 - end
 - local function scanOre()
 - if OreDown() then -- if ore is found, save it into our string
 - posOfOres = posOfOres..myZ..","..(myY - 1)..","..myX..";"
 - end
 - if OreUp() then -- if ore is found, save it into our string
 - posOfOres = posOfOres..myZ..","..(myY + 1)..","..myX..";"
 - end
 - if OreForward() then -- if ore is found, save it into our string
 - posOfOres = posOfOres..(myZ + 1)..","..myY..","..myX..";"
 - end
 - if OreRight() then -- if ore is found, save it into our string
 - posOfOres = posOfOres..myZ..","..myY..","..(myX + 1)..";"
 - end
 - if OreBehind() then -- if ore is found, save it into our string
 - posOfOres = posOfOres..(myZ - 1)..","..myY..","..myX..";"
 - end
 - if OreLeft() then -- if ore is found, save it into our string
 - posOfOres = posOfOres..myZ..","..myY..","..(myX - 1)..";"
 - end
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("0")
 - orientationRightFile.close()
 - end
 - local function backOnRoad() -- use to return on the main road, don't need to be on the same z coord.
 - while myY < zeroY do -- if that, go up
 - if turtle.detectUp() then
 - turtle.digUp()
 - if turtle.up() then
 - myY = myY + 1
 - end
 - else
 - if turtle.up() then
 - myY = myY + 1
 - end
 - end
 - end
 - while myY > zeroY do -- if that go down
 - if turtle.detectDown() then
 - turtle.digDown()
 - if turtle.down() then
 - myY = myY - 1
 - end
 - else
 - if turtle.down() then
 - myY = myY - 1
 - end
 - end
 - end
 - if (myX < zeroX) then
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("1")
 - orientationRightFile.close()
 - while myX < zeroX do -- if that go right
 - if turtle.detect() then
 - turtle.dig()
 - if turtle.forward() then
 - myX = myX + 1
 - end
 - else
 - if turtle.forward() then
 - myX = myX + 1
 - end
 - end
 - end
 - turtle.turnLeft()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("0")
 - orientationRightFile.close()
 - end
 - if (myX > zeroX) then
 - turtle.turnLeft()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("3")
 - orientationRightFile.close()
 - while myX > zeroX do -- if that go left
 - if turtle.detect() then
 - turtle.dig()
 - if turtle.forward() then
 - myX = myX - 1
 - end
 - else
 - if turtle.forward() then
 - myX = myX - 1
 - end
 - end
 - end
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("0")
 - orientationRightFile.close()
 - end
 - end
 - local function mineBlock(indexBest) -- function used to mine an ore, take indexBest who is the case of the table who is the nearest
 - blockAMiner = mySplit(oreData[indexBest], ",") -- separate coords into a table
 - if myY < tonumber(blockAMiner[2]) then -- to number function is need to compare a string contains an int with an int
 - if turtle.detectUp() then
 - turtle.digUp()
 - if turtle.up() then
 - myY = myY + 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "") -- remove the actual pos of the turtle is very important
 - return true
 - end
 - else
 - if turtle.up() then
 - myY = myY + 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - return true
 - end
 - end
 - end
 - if myY > tonumber(blockAMiner[2]) then
 - if turtle.detectDown() then
 - turtle.digDown()
 - if turtle.down() then
 - myY = myY - 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - return true
 - end
 - else
 - if turtle.down() then
 - myY = myY - 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - return true
 - end
 - end
 - end
 - if myX < tonumber(blockAMiner[3]) then
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("1")
 - orientationRightFile.close()
 - if turtle.detect() then
 - turtle.dig()
 - if turtle.forward() then
 - myX = myX + 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - turtle.turnLeft()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("0")
 - orientationRightFile.close()
 - return true
 - end
 - else
 - if turtle.forward() then
 - myX = myX + 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - turtle.turnLeft()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("0")
 - orientationRightFile.close()
 - return true
 - end
 - end
 - end
 - if myX > tonumber(blockAMiner[3]) then
 - turtle.turnLeft()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("3")
 - orientationRightFile.close()
 - if turtle.detect() then
 - turtle.dig()
 - if turtle.forward() then
 - myX = myX - 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("0")
 - orientationRightFile.close()
 - return true
 - end
 - else
 - if turtle.forward() then
 - myX = myX - 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - turtle.turnRight()
 - orientationRightFile = fs.open("MiningOrtientation", "w")
 - orientationRightFile.write("0")
 - orientationRightFile.close()
 - return true
 - end
 - end
 - end
 - if myZ < tonumber(blockAMiner[1]) then
 - if turtle.detect() then
 - turtle.dig()
 - if turtle.forward() then
 - myZ = myZ + 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - return true
 - end
 - else
 - if turtle.forward() then
 - myZ = myZ + 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - return true
 - end
 - end
 - end
 - if myZ > tonumber(blockAMiner[1]) then
 - turtle.turnRight()
 - turtle.turnRight()
 - if turtle.detect() then
 - turtle.dig()
 - if turtle.forward() then
 - myZ = myZ - 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - turtle.turnLeft()
 - turtle.turnLeft()
 - return true
 - end
 - else
 - if turtle.forward() then
 - myZ = myZ - 1
 - posOfOres = posOfOres:gsub(myZ..","..myY..","..myX..";", "")
 - turtle.turnLeft()
 - turtle.turnLeft()
 - return true
 - end
 - end
 - end
 - end
 - local function miner() -- the big function with a french word 'miner' who say mine ^^
 - posOfOres = string.char() -- clear the posOfOres var
 - while 1 do -- infinite loop but stop if there is no ores left, see the next comment
 - scanOre() -- use the scan ore func to scan the environement
 - if posOfOres == "" then -- if there is no ore left, stop the loop
 - break
 - end
 - print(posOfOres) -- a small debug think
 - oreData = mySplit(posOfOres, ";") -- plit all the diffrents pos of ores, the ; is used as separtor
 - oreClosest = {10000000, 0} -- initialing the table with a hight value, so it's verrrrrryyy far
 - for i=1, #oreData do -- here we are going to do the for loop for each ore in the string posOfOres
 - tablePosMine = mySplit(oreData[i], ",") -- plit to have the x,y,z
 - -- i'm actually very proud of that, that's caculate the nearest ore perfectly !
 - score = math.abs(tablePosMine[1] - myZ) + math.abs(tablePosMine[2] - myY) + math.abs(tablePosMine[3] - myX)
 - if score < oreClosest[1] then -- if the result is less than the old result it's closer so save that into the table
 - oreClosest = {score, i}
 - end
 - end
 - print("Now pos : "..myZ..","..myY..","..myX..";") -- some debug stuff again
 - print("Go to : "..oreData[oreClosest[2]])
 - mineBlock(oreClosest[2]) -- once we go out of the for loop, we found the closest ore so just mine it !
 - end
 - backOnRoad() -- once we go out of the loop, if we move we need to return in the good way
 - if securityMode == true then -- secutity mode ! Just for 1*2 tunnels ^^
 - turtle.digUp()
 - end
 - -- writing comments is good, just realise that function was not on the best place ;)
 - if turtle.detect() then -- check if there is a block in front
 - turtle.dig() -- if true, dig
 - end
 - if turtle.forward() then -- once all the check stuff it's done, just continue !
 - myZ = myZ + 1 -- increment the var same as usual, realy need myZ++ func in lua...
 - end
 - end
 - local function back() -- we tutch to the end here, go back to home whan have trouble or whan it's full
 - turtle.turnRight()
 - turtle.turnRight()
 - while myZ > zeroZ do
 - if turtle.detect() then
 - turtle.dig()
 - if turtle.forward() then
 - myZ = myZ - 1
 - end
 - else
 - if turtle.forward() then
 - myZ = myZ - 1
 - end
 - end
 - end
 - turtle.turnRight()
 - turtle.turnRight()
 - end
 - local function putInChest() -- very bad func actually, if there is no chest it will put the stone on floor...
 - if not turtle.back() then -- the chest is one block back
 - return false -- allways check if the movement is corectly executed
 - end
 - if not turtle.detectDown() then
 - return false
 - end
 - for i = 1, 16 do
 - turtle.select(i)
 - if not turtle.dropDown() then
 - print("Chest full !")
 - turtle.forward()
 - return false
 - end
 - end
 - turtle.forward()
 - return true
 - end
 - while 1 do -- we are now on the main loop !
 - 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 !)
 - if turtle.getFuelLevel() > neededFuel and not isFull() then -- check if we have enough fuel to return
 - miner() -- if all is good, execute my frenglish function !
 - elseif isFull() and myZ > zeroZ then -- check if it's full and not already tried to put items in the chest
 - print("Inventory full, return to base.")
 - back() -- go back to home !
 - elseif isFull() then -- if the turtle is already on the zero pos execute
 - if not putInChest() then
 - print("Error with the chest")
 - return -- return if i have an error
 - end
 - elseif turtle.getFuelLevel() <= neededFuel then
 - if not tryRefuel() then -- if hasn't enouth fuel to continue, go back and stop...
 - back()
 - return
 - end
 - end
 - end
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment