Guest User

util_move

a guest
Jan 9th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. --[[ Code by mikebald
  2.      Modify / Take / Use how you like
  3. --]]
  4.  
  5. local position = {forward=0, right=0, down=0, face=1}
  6. local facing = { "forward", "right", "back", "left" }
  7.  
  8. function Debug()
  9.     print( "Forward: " .. position.forward )
  10.     print( "Right: " .. position.right )
  11.     print( "Down: " .. position.down )
  12.     print( "Face: " .. position.face )
  13.     print( "Facing: " .. facing[position.face] )
  14. end
  15.  
  16. function TurnTo( direction )
  17.     while facing[position.face] ~= direction do
  18.         turtle.turnRight()
  19.         position.face = position.face + 1
  20.         if position.face > 4 then position.face = 1 end
  21.     end
  22. end
  23.  
  24. function Forward( amount )
  25.     amount = amount or 1
  26.     for i=1,amount do
  27.         while not turtle.forward() do
  28.             if turtle.detect() then
  29.                 turtle.dig()
  30.             else
  31.                 turtle.attackUp()
  32.             end
  33.             sleep(0.5)
  34.         end
  35.         -- Update Position
  36.         if position.face == 1 then position.forward = position.forward + 1 end
  37.         if position.face == 3 then position.forward = position.forward - 1 end
  38.         if position.face == 2 then position.right = position.right + 1 end
  39.         if position.face == 4 then position.right = position.right - 1 end
  40.     end
  41. end
  42.  
  43. function MoveTo(forward, right, down)
  44.     local forwardMovement = forward - position.forward
  45.     local rightMovement = right - position.right
  46.     local downMovement = down - position.down
  47.    
  48.     if forwardMovement > 0 then
  49.         TurnTo("forward")
  50.         Forward(forwardMovement)
  51.     end
  52.     if forwardMovement < 0 then
  53.         TurnTo("back")
  54.         Forward(math.abs(forwardMovement))
  55.     end
  56.     if rightMovement > 0 then
  57.         TurnTo("right")
  58.         Forward(rightMovement)
  59.     end
  60.     if rightMovement < 0 then
  61.         TurnTo("left")
  62.         Forward(math.abs(rightMovement))
  63.     end
  64.     if downMovement > 0 then
  65.         Down(downMovement)
  66.     end
  67.     if downMovement < 0 then
  68.         Up(math.abs(downMovement))
  69.     end
  70.    
  71. end
  72.  
  73. function TurnAround()
  74.     TurnTo( facing[math.fmod(position.face + 2, 4)] )
  75. end
  76.  
  77. function Up( amount )
  78.     amount = amount or 1
  79.     for i=1,amount do
  80.         while not turtle.up() do
  81.             if turtle.detectUp() then
  82.                 turtle.digUp()
  83.             else
  84.                 turtle.attackUp()
  85.             end
  86.             sleep(0.5)
  87.         end
  88.         position.down = position.down - 1
  89.     end
  90. end
  91.  
  92. function Down( amount )
  93.     amount = amount or 1
  94.     for i=1,amount do
  95.         while not turtle.down() do
  96.             if turtle.detectDown() then
  97.                 turtle.digDown()
  98.             else
  99.                 turtle.attackDown()
  100.             end
  101.         end
  102.         position.down = position.down + 1
  103.     end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment