Advertisement
MigukNamja

Minecraft Turtle Line Dig

Aug 18th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. ----------------------------------
  2. -- Warning ! WORK IN PROGRESS ! --
  3. -- Has not been fully tested !  --
  4. ----------------------------------
  5. --
  6. -- MigukNamja's 1st Line Dig Program
  7. -- Copyright 2013 MigukNamja
  8. --
  9.  
  10. -- Move forward 'blocks' number of blocks, digging when necessary
  11. -- Hitting bedrock or something undiggable like an entity (creature) will
  12. --   prevent forward movement
  13. --
  14. -- Return actual blocks moved forward
  15. function moveForward(blocks)
  16.   if blocks < 0 then return moveBack(0-blocks) end
  17.   if blocks == 0 then return 0 end
  18.  
  19.   local moved = 0
  20.  
  21.   while moved < blocks do
  22.     if turtle.forward() then -- air, nothing to dig, but go down
  23.       moved = moved + 1
  24.     elseif turtle.dig() then -- had to dig to go forwards
  25.       if not turtle.forward() then
  26.         local attempts = 0
  27.         while turtle.dig() and attempts < 20 do
  28.           attempts = attempts + 1 -- take care water won't cause infinite digging
  29.           sleep(0.25) -- might have to chew through a stack of sand or gravel
  30.         end
  31.        
  32.         if turtle.forward() then
  33.           moved = moved + 1
  34.         end
  35.       else
  36.         moved = moved + 1
  37.       end
  38.     else -- undiggable, give up (for now)
  39.       return moved
  40.     end
  41.    
  42.     sleep(0.25) -- sleep to avoid yielding error
  43.   end
  44.  
  45.   return moved
  46. end
  47.  
  48. -- Move backwards, digging when necessary
  49. function moveBack(blocks)
  50.   if blocks < 0 then return moveForward(0-blocks) end
  51.   if blocks == 0 then return 0 end
  52.  
  53.   local moved = 0
  54.  
  55.   turtle.turnRight()
  56.   turtle.turnRight()
  57.   moved = moveForward(blocks)
  58.   turtle.turnRight()
  59.   turtle.turnRight()
  60.  
  61.   return moved
  62. end
  63.  
  64. function usage()
  65.   print( "Usage: lineDig <blocks_forward>" )
  66. end
  67.  
  68. function parseArgs(args)
  69.   local validSyntax = false
  70.   local bForward = 0
  71.  
  72.   if #args ~= 1 then
  73.     return false
  74.   elseif args[1] == "help" then
  75.     return false
  76.   elseif args[1] == nil then
  77.     return false
  78.   else
  79.     bForward = tonumber( args[1] )
  80.    
  81.     if bForward < 0 then
  82.       validSyntax = false
  83.     else
  84.       validSyntax = true
  85.     end
  86.   end
  87.  
  88.   return validSyntax, bForward
  89. end
  90.  
  91. local tArgs = { ... }
  92. validSyntax, bForward = parseArgs(tArgs)
  93. if not validSyntax then
  94.   usage()
  95.   return
  96. end
  97.  
  98. print( "Digging ", bForward, " blocks forward." )
  99.  
  100. -- Dig forward
  101. assert( moveForward( bForward ) == bForward )
  102.  
  103. -- Traverse in the reverse direction from the dig coordinates
  104. assert( moveBack( bForward ) == bForward )
  105.  
  106. print( "I have finished the assigned task, my master !" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement