Advertisement
Guest User

Diamond Finder for ComputerCraft Turtle

a guest
Jan 28th, 2013
2,543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.76 KB | None | 0 0
  1. --DiamondFinder V0.0
  2. --By Schilcote
  3.  
  4. --Digs about 10 blocks from the bedrock level and searches for diamonds. Returns to the surface when it's out of fuel.
  5. --You must harvest a block of diamond ore with Silk Touch and place it in slot 1 for this to work, of course.
  6. --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?)
  7.  
  8. --Use the DiamondFinder Bloodhound program on another turtle to locate this one.
  9.  
  10. mode="init"
  11. dumptimer=0
  12. signaltimer=0
  13. readjusttimer=0
  14. doublebacktimer=0
  15.  
  16. DOUBLE_BACK=1024 --Double back after 1024 block-breakings.
  17.  
  18. while true do
  19.  
  20.     --Basic life functions: must run in all modes, keeps turtle running
  21.  
  22.     --print(mode)
  23.    
  24.     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)
  25.         if signaltimer >= 3 then --We want to broadcast our signal every 3 cycles
  26.             rednet.broadcast("DiamondFinder Location Signal") --Broadcast a rednet message so that we can be found.
  27.             signaltimer=0 --Reset cycle counter
  28.         else --If the timer isn't up yet...
  29.             signaltimer=signaltimer+1 --Increment it
  30.         end
  31.     end
  32.  
  33.     if mode~="init" then --If we're actually operating (thus not in init mode) then...
  34.         if turtle.getFuelLevel() < 512 then --If our fuel level is low...
  35.             shell.run("refuel","all")  --Use any burnables in our inventory to refuel.
  36.             turtle.select(1)
  37.            
  38.             if turtle.getFuelLevel() < 512 then --If that didn't fix it...
  39.                 mode="fuelpanic" --Panic!
  40.                 for t=1,50 do --Unconditionally head up 50 blocks, so the fuelpanic code doesn't get stuck in a cave
  41.                     turtle.digUp()
  42.                     turtle.up()
  43.                 end
  44.             end
  45.         end
  46.     end
  47.    
  48.     if dumptimer >= 512 then --If we've run 512 cycles without dumping our excess...
  49.         for i=3,16 do --For every slot besides slot 1 and 2...
  50.             turtle.select(i) --Select the slot
  51.             turtle.refuel(64) --try to eat what's in it
  52.             turtle.drop(64) --and if we can't, throw it away.
  53.         end
  54.         dumptimer=0 --Reset the timer
  55.         turtle.select(1) --And re-select slot 1
  56.     else
  57.         dumptimer=dumptimer+1 --Otherwise, increment dumptimer.
  58.     end
  59.    
  60.     if turtle.getItemCount(2) == 64 then --If we've filled up our diamond slot (holy crap!) then...
  61.         mode="full" --Enter full mode.
  62.     end
  63.    
  64.     --Mode code
  65.    
  66.     if mode=="init" then --Init mode: set ourselves up to go mining
  67.        
  68.         turtle.select(1) --Program always assumes that the diamond ore slot is selected, so select that slot
  69.        
  70.         if turtle.getItemCount(1)==0 then --If there's nothing in slot 1, we can't work
  71.             print("Please place diamond ore in slot 1.")
  72.             break
  73.         end
  74.        
  75.         if turtle.getItemCount(2)==0 then --If there's nothing in slot 2, we can't work either
  76.             print("Please place non-ore diamond in slot 2.")
  77.             break
  78.         end
  79.        
  80.         if turtle.getFuelLevel() < 512 then --If we don't have much fuel...
  81.             shell.run("refuel","all")
  82.             turtle.select(1)
  83.             if turtle.getFuelLevel() < 512 then
  84.                 print("Please add fuel.")
  85.                 break
  86.             end
  87.         end
  88.        
  89.         rednet.open("right")
  90.        
  91.         if not rednet.isOpen("right")  then
  92.             print("No modem detected. Please install modem.")
  93.             break
  94.         end
  95.        
  96.         --if not fs.exists("startup") then --If there isn't already a startup program...
  97.             print("Creating persistance program.")
  98.             file=fs.open("startup","w") --Make our own.
  99.             file.writeLine("print(\"Restart detected. Resuming operation.\")")
  100.             file.writeLine("shell.run(\"diamondfinder\")") --This assumes the main program is named "diamondfinder".
  101.         --end
  102.        
  103.         print("Beginning operation.")
  104.         mode="startmine"
  105.     end
  106.    
  107.     if mode=="startmine" then  --Pre-mining mode: go to Z=10 (roughly) and then switch to mining mode.
  108.         --print("In startmine mode")
  109.    
  110.         if not turtle.down() then --Dig until we can't for some reason...
  111.             --print("Couldn't go down.")
  112.             if not turtle.digDown() then --And try to dig. If we can't (implying we've hit bedrock)...
  113.             --print("Couldn't dig down, must've hit bedrock")
  114.                 for i=1,12 do --Do the following 12 times (The most likely place to find diamond is 12 blocks above bedrock)...
  115.                     if not turtle.up() then --Try to go up, and if we cant...
  116.                         turtle.digUp() --Break the block above us and try again.
  117.                         turtle.up()
  118.                     end
  119.                 end
  120.                 mode="mine" --Switch over to mining mode.
  121.             end
  122.         end
  123.     end
  124.    
  125.     if mode=="mine" then --Mining mode: Get working. Find the ore in slot 1 and bring it back home.
  126.         --print("In mine mode.")
  127.        
  128.         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.
  129.             mode="startmine" --Go back into startmine mode to do the calibration.
  130.             readjusttimer=0 --Reset the timer
  131.         else
  132.             readjusttimer=readjusttimer+1 --Increment the timer every cycle in mine mode.
  133.         end
  134.            
  135.            
  136.         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.
  137.             turtle.turnLeft()
  138.             turtle.dig()
  139.             turtle.forward()
  140.             turtle.turnLeft()
  141.             doublebacktimer=0
  142.         else
  143.             doublebacktimer=doublebacktimer+1 --Increment the timer every cycle in mine mode.
  144.         end
  145.        
  146.         if turtle.compare() then --Check if the block in front of us is diamond.
  147.             --print("Saw diamond in front of me")
  148.             turtle.dig()
  149.             mode="vein"
  150.         end
  151.        
  152.         if turtle.compareUp() then  --Do similar things if there's diamond above us...
  153.             --print("Saw diamond above me")
  154.             turtle.digUp()
  155.             turtle.up()
  156.             mode="vein"
  157.         end
  158.        
  159.         if turtle.compareDown() then --Or below us
  160.             --print("Saw diamond below me")
  161.             turtle.digDown()
  162.             turtle.down()
  163.             mode="vein"
  164.         end
  165.        
  166.         if mode~="vein" then    --Don't want to do the searching code if we've already found a vein
  167.             --print("Not in vein mode")
  168.                
  169.             if not turtle.forward() then
  170.                 --print("turtle.forward returned false")
  171.                 if not turtle.dig() then --Dig and run the following code if it doesn't work.
  172.                     --print("turtle.dig returned false")
  173.                     turtle.digUp()
  174.                     turtle.up() --We might have encountered some bedrock or something. Go up.
  175.                 end
  176.             end
  177.            
  178.             if math.random(4)==0 then --1 in four chance that we...
  179.                 turtle.turnLeft() --Turn left to cover more area.
  180.             end
  181.            
  182.             if math.random(8)==0 then --1 in eight chance that we...
  183.                 if math.random()==0 then --randomly...
  184.                     turtle.digUp()
  185.                     turtle.up() --Go up...
  186.                 else
  187.                     turtle.digDown()
  188.                     turtle.down() --Or down.
  189.                 end
  190.             end
  191.         end
  192.     end
  193.    
  194.     if mode=="vein" then --Vein mining mode: we've found a diamond, now look for the rest in this cluster.
  195.         cycles=1
  196.         while cycles <= 6 do --Run this code 6 times
  197.             for i=1,4 do --Do this for each direction we can face.
  198.                 if turtle.compare() then --If there's more diamond in front of us...
  199.                     turtle.dig() --Dig it out.
  200.                     turtle.forward() --And then head in that direction.
  201.                 end
  202.                 turtle.turnLeft() --Spin around so that we can check every facing.
  203.             end
  204.            
  205.             if turtle.compareUp() then  --If there's diamond above us...
  206.                 turtle.digUp()  --Dig it out
  207.                 turtle.up()
  208.             end
  209.            
  210.             if turtle.compareDown() then --Or below us
  211.                 turtle.digDown()
  212.                 turtle.down()
  213.             end
  214.            
  215.             cycles=cycles+1
  216.         end
  217.         mode="mine" --Go back to search mode after we're done.
  218.     end
  219.    
  220.     if mode=="fuelpanic" then --Fuel panic mode: We're about to run out of fuel, so head to the surface and start yelling.
  221.        
  222.         while turtle.detectUp() do --Then continue moving up 'till there's nothing above us, so we can be more easily found
  223.             turtle.digUp()
  224.             turtle.up()
  225.         end
  226.        
  227.         rednet.broadcast("DiamondFinder Panic Signal") --Broadcast a panic signal over rednet so someone can find us (probably using a turtle)
  228.        
  229.         for i=3,16 do --For each slot...
  230.             turtle.select(i) --Select that slot...
  231.             turtle.refuel(64) --And try to refuel from it.
  232.         end
  233.         turtle.select(1) --Re-select the diamond ore, 'cos the rest of the program assumes it's selected
  234.        
  235.         if turtle.getFuelLevel() <= 2048 then --If we've refuelled (either from inserted burnables or being a solar turtle)
  236.             mode="startmine" --Enter startmine mode so we can get back to work!
  237.         end
  238.        
  239.         sleep(1) --Wait a second.
  240.        
  241.     end
  242.    
  243.     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.
  244.         while turtle.digUp() do --While we can dig up (implying there's a block above us)
  245.             turtle.up() --Go up, thus getting up to the surface and stopping there.
  246.         end
  247.        
  248.         rednet.broadcast("DiamondFinder Full Signal")
  249.        
  250.         if turtle.getItemCount(2) < 64 then --If we find we have less than 64 diamonds...
  251.             mode="init" --Switch over to init mode, so the player can set us back up and we can go back to work.
  252.         end
  253.        
  254.         sleep(1) --Wait a second.
  255.        
  256.     end
  257.    
  258. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement