Advertisement
MigukNamja

Platform

Oct 13th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.00 KB | None | 0 0
  1. ----------------------------------
  2. -- Warning ! WORK IN PROGRESS ! --
  3. -- Has not been fully tested !  --
  4. ----------------------------------
  5. --
  6. -- Copyright 2013 MigukNamja
  7. --
  8. -- MigukNamja's Platform Builder
  9. --
  10. --
  11.  
  12. -- Globals
  13. gForward = 0 -- Set by program arg
  14. gRight = 0   -- Set by program arg
  15.  
  16. --
  17. -- Place a platform block
  18. --
  19. gSlot = 1
  20. gTotalPlaced = 0
  21. function place()
  22.   while turtle.getItemCount(gSlot) == 0 do
  23.     gSlot = gSlot + 1
  24.   end
  25.  
  26.   turtle.select(gSlot)
  27.  
  28.   if not turtle.compareDown() then -- don't bother digging and placing if platform already there
  29.     turtle.digDown()
  30.     turtle.placeDown()
  31.     gTotalPlaced = gTotalPlaced + 1
  32.   end
  33. end
  34.  
  35. --
  36. -- Return to the starting position and orientation
  37. --
  38. function returnToStart()  
  39.   -- odd number of columns will have turtle at the far end (top) of the platform
  40.   -- so, turtle should return to bottom row
  41.   if gRight % 2 == 1 then
  42.     local y = gForward - 1
  43.     turtle.turnLeft()
  44.     turtle.turnLeft()
  45.     assert( moveForward( y ) == y )
  46.   end
  47.  
  48.   local x = gRight - 1
  49.   turtle.turnRight()
  50.   assert( moveForward( x ) == x )
  51.   turtle.turnRight()
  52. end
  53.  
  54. -- Move forward 'blocks' number of blocks, digging when necessary
  55. -- Hitting bedrock or something undiggable like an entity (creature) will
  56. --   prevent forward movement
  57. --
  58. -- Return actual blocks moved forward
  59. function moveForward(blocks)
  60.   if blocks < 0 then return moveBack(0-blocks) end
  61.   if blocks == 0 then return 0 end
  62.  
  63.   local moved = 0
  64.  
  65.   while moved < blocks do
  66.     if turtle.forward() then -- air, nothing to dig, but go down
  67.       moved = moved + 1
  68.     elseif turtle.dig() then -- had to dig to go forwards
  69.       if not turtle.forward() then
  70.         local attempts = 0
  71.         while turtle.dig() and attempts < 20 do
  72.           attempts = attempts + 1 -- take care water won't cause infinite digging
  73.           sleep(0.25) -- might have to chew through a stack of sand or gravel
  74.         end
  75.        
  76.         if turtle.forward() then
  77.           moved = moved + 1
  78.         end
  79.       else
  80.         moved = moved + 1
  81.       end
  82.     else -- undiggable, give up (for now)
  83.       return moved
  84.     end
  85.    
  86.     sleep(0.25) -- sleep to avoid yielding error
  87.   end
  88.  
  89.   return moved
  90. end
  91.  
  92. -- Move backwards, digging when necessary
  93. function moveBack(blocks)
  94.   if blocks < 0 then return moveForward(0-blocks) end
  95.   if blocks == 0 then return 0 end
  96.  
  97.   local moved = 0
  98.  
  99.   turtle.turnRight()
  100.   turtle.turnRight()
  101.   moved = moveForward(blocks)
  102.   turtle.turnRight()
  103.   turtle.turnRight()
  104.  
  105.   return moved
  106. end
  107.  
  108. function usage()
  109.   print( "Usage: platform <blocks_forward> <blocks_right>" )
  110.   print( " Both values must be a least 1" )
  111. end
  112.  
  113. function parseArgs(args)
  114.   local validSyntax = false
  115.  
  116.   if #args ~= 2 then
  117.     return false
  118.   elseif args[1] == "help" then
  119.     return false
  120.   elseif args[1] == nil or args[2] == nil then
  121.     return false
  122.   else
  123.     gForward = tonumber( args[1] )
  124.     gRight   = tonumber( args[2] )
  125.        
  126.     if gForward <= 0 or gRight <= 0 then
  127.       validSyntax = false
  128.     else
  129.       validSyntax = true
  130.     end
  131.   end
  132.  
  133.   return validSyntax
  134. end
  135.  
  136. local tArgs = { ... }
  137. validSyntax = parseArgs(tArgs)
  138. if not validSyntax then
  139.   usage()
  140.   return
  141. end
  142.  
  143. print( "Making platform ", gForward, " blocks forward and ", gRight, " blocks rightwards." )
  144.  
  145. local x = 1
  146. local y = 1
  147. turtle.select(1)
  148. for x=1,gRight do
  149.   for y=1,gForward do
  150.     place()
  151.     if y < gForward then
  152.       assert( moveForward(1) == 1 )
  153.     end
  154.   end
  155.  
  156.   if x < gRight then
  157.     if (x % 2) == 1 then -- odd numbered row, turn rightwards and get lined up for next row
  158.       turtle.turnRight()
  159.       assert( moveForward(1) == 1 )
  160.       turtle.turnRight()
  161.     else -- even numbered row, turn leftwards and get lined up for next row
  162.       turtle.turnLeft()
  163.       assert( moveForward(1) == 1 )
  164.       turtle.turnLeft()
  165.     end
  166.   end
  167. end
  168.  
  169. --returnToStart()
  170.  
  171. print( "I have finished the assigned task, my master !" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement