Advertisement
mrkite

turtle mining

Dec 4th, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.04 KB | None | 0 0
  1. function splash() -- splash screen
  2.   term.clear()
  3.   term.setCursorPos(1,1)
  4.   print("Deep Underground Mining Buddy")
  5.   print("  v1.0  by mrkite\n\n")
  6. end
  7. function gety() -- get the current y position
  8.   print("Stand next to me and tell me your feet position")
  9.   return tonumber(io.read())
  10. end
  11.  
  12. function getwidth() -- get the width of the quarry
  13.   print("How large an area should I dig? (example: 30)")
  14.   return tonumber(io.read())
  15. end
  16.  
  17. function emptyInventory() -- dump all but slot 1 (fuel)
  18.   for i=1,16 do
  19.     turtle.select(i)
  20.     turtle.drop()
  21.   end
  22.   turtle.select(1)
  23. end
  24.  
  25. function refuel() -- refuel all fuel we're carrying
  26.   for i=1,16 do
  27.     turtle.select(i)
  28.     turtle.refuel()
  29.   end
  30.   turtle.select(1)
  31. end
  32.  
  33. function flip() -- do a 180
  34.   left()
  35.   left()
  36. end
  37.  
  38. function up() -- move up and keep track of our position
  39.   local moved=false
  40.   while not moved do
  41.     if turtle.detectUp() then
  42.       turtle.digUp()
  43.     end
  44.     if turtle.up() then
  45.       pos.y=pos.y+1
  46.       fromHome=fromHome-1 -- up is always closer to home
  47.       moved=true
  48.     else
  49.       turtle.attackUp()
  50.     end
  51.   end
  52. end
  53.  
  54. function down() -- move down and keep track of our position
  55.   local moved=false
  56.   while not moved do
  57.     if turtle.detectDown() then
  58.       turtle.digDown()
  59.     end
  60.     if turtle.down() then
  61.       pos.y=pos.y-1
  62.       fromHome=fromHome+1 -- down is always away from home
  63.       moved=true
  64.     else
  65.       turtle.attackDown()
  66.     end
  67.   end
  68. end
  69.  
  70. function forward() -- more forward and keep track of our position
  71.   local moved=false
  72.   while not moved do
  73.     if turtle.detect() then
  74.       turtle.dig()
  75.     end
  76.     if turtle.forward() then
  77.       if facing==0 then  -- facing home
  78.         pos.x=pos.x-1
  79.         fromHome=fromHome-1
  80.       elseif facing==1 then -- facing sidebranch
  81.         pos.z=pos.z+1
  82.         fromHome=fromHome+1
  83.       elseif facing==2 then -- facing away from home
  84.         pos.x=pos.x+1
  85.         fromHome=fromHome+1
  86.       else                  -- facing main tunnel
  87.         pos.z=pos.z-1
  88.         fromHome=fromHome-1
  89.       end
  90.       moved=true
  91.     else
  92.       turtle.attack()  -- couldn't move, someone in the way?
  93.     end
  94.   end
  95. end
  96.  
  97. function left() -- turn left and keep track of direction
  98.   turtle.turnLeft()
  99.   if facing==0 then
  100.     facing=3
  101.   else
  102.     facing=facing-1
  103.   end
  104. end
  105. function right() -- turn right and keep track of direction
  106.   turtle.turnRight()
  107.   if facing==3 then
  108.     facing=0
  109.   else
  110.     facing=facing+1
  111.   end
  112. end
  113.  
  114. function faceMain()
  115.   while facing~=3 do
  116.     left()
  117.   end
  118. end
  119. function faceHome()
  120.   while facing~=0 do
  121.     left()
  122.   end
  123. end
  124.  
  125. function returnHome(temporary) -- return to home, housekeep, and possibly return
  126.   local oldX=pos.x
  127.   local oldY=pos.y
  128.   local oldZ=pos.z
  129.   local oldFace=facing
  130.  
  131.   print("returning home")
  132.  
  133.   faceMain()
  134.   while pos.z~=0 do -- head back to main tunnel if necessary
  135.     forward()
  136.   end
  137.   faceHome()
  138.   while pos.x~=0 do -- head back home if necessary
  139.     forward()
  140.   end
  141.   while pos.y<home do -- we're at home, but below it
  142.     up()
  143.   end
  144.   refuel()  -- lets refuel before we empty our inventory
  145.   emptyInventory()
  146.   if turtle.getFuelLevel()==0 then -- failed to refuel
  147.     print("Waiting for fuel")
  148.     while turtle.getFuelLevel()==0 do
  149.       sleep(1)
  150.       refuel()
  151.     end
  152.   end
  153.   if temporary then -- return to where we left off
  154.     print("returning to duty")
  155.     while pos.y>oldY do
  156.       checkAll()
  157.       down()
  158.     end
  159.     flip()
  160.     while pos.x<oldX do
  161.       checkAll()
  162.       forward()
  163.     end
  164.     left()
  165.     while pos.z<oldZ do
  166.       checkAll()
  167.       forward()
  168.     end
  169.     while facing~=oldFace do
  170.       left()
  171.     end
  172.   end
  173. end
  174.  
  175. function checkFuel()  -- checks if we only have enough fuel to get home
  176.   if turtle.getFuelLevel()==fromHome then
  177.     print("refueling")
  178.     refuel()
  179.     if turtle.getFuelLevel()==fromHome then -- did we fail?
  180.       returnHome(true)  -- return home to refuel, but come back
  181.     end
  182.   end
  183. end
  184.  
  185. function checkStorage() -- checks to see if we have any empty slots
  186.   for i=1,16 do
  187.     if turtle.getItemCount(i)==0 then -- we're okay as long as one is empty
  188.       return
  189.     end
  190.   end
  191.   returnHome(true) -- return home to unload, but come back
  192. end
  193.  
  194. function checkAll() -- checks all reasons for returning home
  195.   checkFuel()
  196.   checkStorage()
  197. end
  198.  
  199. function downToBottom() -- step 1.. tunnel down to 7
  200.   while pos.y>7 do
  201.     checkAll()
  202.     down()
  203.   end
  204. end
  205.  
  206. function mainTunnel(cnt) -- dig the main tunnel
  207.   while cnt>0 do
  208.     checkAll()
  209.     forward()
  210.     cnt=cnt-1
  211.   end
  212. end
  213.  
  214. function sideTunnel(cnt) -- dig a side tunnel
  215.   while cnt>0 do
  216.     checkAll()
  217.     forward()
  218.     turtle.digUp()
  219.     turtle.digDown()
  220.     turtle.turnLeft() -- no need to keep track of position here
  221.     turtle.dig()
  222.     turtle.turnLeft() -- since we spin completely around
  223.     turtle.turnLeft()
  224.     turtle.dig()
  225.     turtle.turnLeft() -- and end up facing the original direction
  226.     cnt=cnt-1
  227.   end
  228. end
  229.  
  230. function branch() -- do the branch mine from 7 to 15
  231.   for y=7,14 do
  232.     for m=0,mainLength do
  233.       --serpentine
  234.       if m%2==y%2 then
  235.         right()
  236.       else
  237.         left()
  238.       end
  239.       sideTunnel(branchWidth)
  240.       if m%2==y%2 then
  241.         left()
  242.       else
  243.         right()
  244.       end
  245.       mainTunnel(5)
  246.     end
  247.     up()
  248.     flip()
  249.     if y%2==1 then
  250.       mainTunnel(2)
  251.     else
  252.       mainTunnel(3)
  253.     end
  254.   end
  255. end
  256.  
  257. -- main routine
  258.  
  259. splash()
  260. pos={}
  261. pos.x=0  --home is always at 0,0
  262. pos.z=0
  263. pos.y=gety()
  264. if pos.y<16 then
  265.   print("Error - I should be started at 16 or above")
  266.   exit()
  267. end
  268. home=pos.y
  269. fromHome=0 -- we start 0 moves from home
  270. facing=2 -- we start facing 2
  271.  
  272. branchWidth=getwidth()
  273. mainLength=math.floor(branchWidth/5)
  274. if mainLength%2==0 then -- must be an odd number or things will get screwed
  275.   mainLength=mainLength+1
  276. end
  277.  
  278. print("Make sure there's a chest behind me, then press any key to begin")
  279. a,b=os.pullEvent()
  280. while a~="key" do a,b=os.pullEvent() end
  281.  
  282. flip() -- turn around to align with chest
  283.  
  284. checkAll()
  285. downToBottom()
  286. flip() -- face the mine
  287. branch()
  288. returnHome(false) -- we're done, go home and stay there
  289. flip() -- face away from chest
  290.  
  291. print("All done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement