Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --DiamondFinder V0.0
- --By Schilcote
- --Digs about 10 blocks from the bedrock level and searches for diamonds. Returns to the surface when it's out of fuel.
- --You must harvest a block of diamond ore with Silk Touch and place it in slot 1 for this to work, of course.
- --You must also put a diamond in slot 2, so that the turtle knows not to throw them out (that would sure suck, wouldn't it?)
- --Use the DiamondFinder Bloodhound program on another turtle to locate this one.
- mode="init"
- dumptimer=0
- signaltimer=0
- readjusttimer=0
- doublebacktimer=0
- DOUBLE_BACK=1024 --Double back after 1024 block-breakings.
- while true do
- --Basic life functions: must run in all modes, keeps turtle running
- --print(mode)
- if not (mode=="fuelpanic" or mode=="full") then --As long as we're not in panic or full mode (and therefore already broadcasting a special signal)
- if signaltimer >= 3 then --We want to broadcast our signal every 3 cycles
- rednet.broadcast("DiamondFinder Location Signal") --Broadcast a rednet message so that we can be found.
- signaltimer=0 --Reset cycle counter
- else --If the timer isn't up yet...
- signaltimer=signaltimer+1 --Increment it
- end
- end
- if mode~="init" then --If we're actually operating (thus not in init mode) then...
- if turtle.getFuelLevel() < 512 then --If our fuel level is low...
- shell.run("refuel","all") --Use any burnables in our inventory to refuel.
- turtle.select(1)
- if turtle.getFuelLevel() < 512 then --If that didn't fix it...
- mode="fuelpanic" --Panic!
- for t=1,50 do --Unconditionally head up 50 blocks, so the fuelpanic code doesn't get stuck in a cave
- turtle.digUp()
- turtle.up()
- end
- end
- end
- end
- if dumptimer >= 512 then --If we've run 512 cycles without dumping our excess...
- for i=3,16 do --For every slot besides slot 1 and 2...
- turtle.select(i) --Select the slot
- turtle.refuel(64) --try to eat what's in it
- turtle.drop(64) --and if we can't, throw it away.
- end
- dumptimer=0 --Reset the timer
- turtle.select(1) --And re-select slot 1
- else
- dumptimer=dumptimer+1 --Otherwise, increment dumptimer.
- end
- if turtle.getItemCount(2) == 64 then --If we've filled up our diamond slot (holy crap!) then...
- mode="full" --Enter full mode.
- end
- --Mode code
- if mode=="init" then --Init mode: set ourselves up to go mining
- turtle.select(1) --Program always assumes that the diamond ore slot is selected, so select that slot
- if turtle.getItemCount(1)==0 then --If there's nothing in slot 1, we can't work
- print("Please place diamond ore in slot 1.")
- break
- end
- if turtle.getItemCount(2)==0 then --If there's nothing in slot 2, we can't work either
- print("Please place non-ore diamond in slot 2.")
- break
- end
- if turtle.getFuelLevel() < 512 then --If we don't have much fuel...
- shell.run("refuel","all")
- turtle.select(1)
- if turtle.getFuelLevel() < 512 then
- print("Please add fuel.")
- break
- end
- end
- rednet.open("right")
- if not rednet.isOpen("right") then
- print("No modem detected. Please install modem.")
- break
- end
- --if not fs.exists("startup") then --If there isn't already a startup program...
- print("Creating persistance program.")
- file=fs.open("startup","w") --Make our own.
- file.writeLine("print(\"Restart detected. Resuming operation.\")")
- file.writeLine("shell.run(\"diamondfinder\")") --This assumes the main program is named "diamondfinder".
- --end
- print("Beginning operation.")
- mode="startmine"
- end
- if mode=="startmine" then --Pre-mining mode: go to Z=10 (roughly) and then switch to mining mode.
- --print("In startmine mode")
- if not turtle.down() then --Dig until we can't for some reason...
- --print("Couldn't go down.")
- if not turtle.digDown() then --And try to dig. If we can't (implying we've hit bedrock)...
- --print("Couldn't dig down, must've hit bedrock")
- for i=1,12 do --Do the following 12 times (The most likely place to find diamond is 12 blocks above bedrock)...
- if not turtle.up() then --Try to go up, and if we cant...
- turtle.digUp() --Break the block above us and try again.
- turtle.up()
- end
- end
- mode="mine" --Switch over to mining mode.
- end
- end
- end
- if mode=="mine" then --Mining mode: Get working. Find the ore in slot 1 and bring it back home.
- --print("In mine mode.")
- if readjusttimer >= 64 then --Every 64 blocks we want to re-calibrate our height so we know we're at the best zlevel to get diamonds.
- mode="startmine" --Go back into startmine mode to do the calibration.
- readjusttimer=0 --Reset the timer
- else
- readjusttimer=readjusttimer+1 --Increment the timer every cycle in mine mode.
- end
- if doublebacktimer >= DOUBLE_BACK then --Every so often we want to turn around and head back the way we came one block over, so we don't miss anything or head off into the wild blue yonder never to be seen again.
- turtle.turnLeft()
- turtle.dig()
- turtle.forward()
- turtle.turnLeft()
- doublebacktimer=0
- else
- doublebacktimer=doublebacktimer+1 --Increment the timer every cycle in mine mode.
- end
- if turtle.compare() then --Check if the block in front of us is diamond.
- --print("Saw diamond in front of me")
- turtle.dig()
- mode="vein"
- end
- if turtle.compareUp() then --Do similar things if there's diamond above us...
- --print("Saw diamond above me")
- turtle.digUp()
- turtle.up()
- mode="vein"
- end
- if turtle.compareDown() then --Or below us
- --print("Saw diamond below me")
- turtle.digDown()
- turtle.down()
- mode="vein"
- end
- if mode~="vein" then --Don't want to do the searching code if we've already found a vein
- --print("Not in vein mode")
- if not turtle.forward() then
- --print("turtle.forward returned false")
- if not turtle.dig() then --Dig and run the following code if it doesn't work.
- --print("turtle.dig returned false")
- turtle.digUp()
- turtle.up() --We might have encountered some bedrock or something. Go up.
- end
- end
- if math.random(4)==0 then --1 in four chance that we...
- turtle.turnLeft() --Turn left to cover more area.
- end
- if math.random(8)==0 then --1 in eight chance that we...
- if math.random()==0 then --randomly...
- turtle.digUp()
- turtle.up() --Go up...
- else
- turtle.digDown()
- turtle.down() --Or down.
- end
- end
- end
- end
- if mode=="vein" then --Vein mining mode: we've found a diamond, now look for the rest in this cluster.
- cycles=1
- while cycles <= 6 do --Run this code 6 times
- for i=1,4 do --Do this for each direction we can face.
- if turtle.compare() then --If there's more diamond in front of us...
- turtle.dig() --Dig it out.
- turtle.forward() --And then head in that direction.
- end
- turtle.turnLeft() --Spin around so that we can check every facing.
- end
- if turtle.compareUp() then --If there's diamond above us...
- turtle.digUp() --Dig it out
- turtle.up()
- end
- if turtle.compareDown() then --Or below us
- turtle.digDown()
- turtle.down()
- end
- cycles=cycles+1
- end
- mode="mine" --Go back to search mode after we're done.
- end
- if mode=="fuelpanic" then --Fuel panic mode: We're about to run out of fuel, so head to the surface and start yelling.
- while turtle.detectUp() do --Then continue moving up 'till there's nothing above us, so we can be more easily found
- turtle.digUp()
- turtle.up()
- end
- rednet.broadcast("DiamondFinder Panic Signal") --Broadcast a panic signal over rednet so someone can find us (probably using a turtle)
- for i=3,16 do --For each slot...
- turtle.select(i) --Select that slot...
- turtle.refuel(64) --And try to refuel from it.
- end
- turtle.select(1) --Re-select the diamond ore, 'cos the rest of the program assumes it's selected
- if turtle.getFuelLevel() <= 2048 then --If we've refuelled (either from inserted burnables or being a solar turtle)
- mode="startmine" --Enter startmine mode so we can get back to work!
- end
- sleep(1) --Wait a second.
- end
- if mode=="full" then --Full mode: we can't hold any more diamonds! Return to the surface and signal for someone to pick us up.
- while turtle.digUp() do --While we can dig up (implying there's a block above us)
- turtle.up() --Go up, thus getting up to the surface and stopping there.
- end
- rednet.broadcast("DiamondFinder Full Signal")
- if turtle.getItemCount(2) < 64 then --If we find we have less than 64 diamonds...
- mode="init" --Switch over to init mode, so the player can set us back up and we can go back to work.
- end
- sleep(1) --Wait a second.
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement