Advertisement
Guest User

Move

a guest
May 27th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. function DemandLocation()
  2.  local pos = nil
  3.  while (pos == nil) do
  4.   pos = vector.new(gps.locate())
  5.  end
  6.  return pos
  7. end
  8.  
  9. function DirString()
  10.  if (Move.Dir == Move.DIR_NORTH) then
  11.   return "north"
  12.  elseif (Move.Dir == Move.DIR_EAST) then
  13.   return "east"
  14.  elseif (Move.Dir == Move.DIR_SOUTH) then
  15.   return "south"
  16.  elseif (Move.Dir == Move.DIR_WEST) then
  17.   return "west"
  18.  else
  19.   return nil
  20.  end
  21. end
  22.  
  23. function Face(direction)
  24.  while (Move.Dir ~= direction) do
  25.   TurnRight()
  26.  end
  27. end
  28.  
  29. function ForceDown()
  30.  while (TryDown() ~= true) do
  31.   turtle.digDown()
  32.  end
  33. end
  34.  
  35. function ForceForward()
  36.  while (TryForward() ~= true) do
  37.   turtle.dig()
  38.  end
  39. end
  40.  
  41. function ForceUp()
  42.  while (TryUp() ~= true) do
  43.   turtle.digUp()
  44.  end
  45. end
  46.  
  47. function Initialize()
  48.  Move.DIR_NORTH = 0
  49.  Move.DIR_EAST = 1
  50.  Move.DIR_SOUTH = 2
  51.  Move.DIR_WEST = 3
  52.  
  53.  local pos1 = DemandLocation()
  54.  ForceForward()
  55.  local pos2 = DemandLocation()
  56.  local delta = pos2 - pos1
  57.  
  58.  if (delta.z == -1) then
  59.   Move.Dir = Move.DIR_NORTH
  60.  elseif (delta.x == 1) then
  61.   Move.Dir = Move.DIR_EAST
  62.  elseif (delta.z == 1) then
  63.   Move.Dir = Move.DIR_SOUTH
  64.  elseif (delta.x == -1) then
  65.   Move.Dir = Move.DIR_WEST
  66.  end
  67.  
  68.  MoveTo(pos1)
  69. end
  70.  
  71. function MoveTo(newPos)
  72.  local currPos = DemandLocation()
  73.  local delta = newPos - currPos
  74.  
  75.  alignEastWest(delta)
  76.  alignNorthSouth(delta)
  77.  alignUpDown(delta)
  78. end
  79.  
  80. function TryDown()
  81.  return turtle.down()
  82. end
  83.  
  84. function TryForward()
  85.  return turtle.forward()
  86. end
  87.  
  88. function TryUp()
  89.  return turtle.up()
  90. end
  91.  
  92. function TurnLeft()
  93.  turtle.turnLeft()
  94.  Move.Dir = (Move.Dir + 3) % 4
  95. end
  96.  
  97. function TurnRight()
  98.  turtle.turnRight()
  99.  Move.Dir = (Move.Dir + 1) % 4
  100. end
  101.  
  102. function alignEastWest(delta)
  103.  if (delta.x < 0) then
  104.   Face(Move.DIR_WEST)
  105.   doMultiple(ForceForward, 0 - delta.x)
  106.  elseif (delta.x > 0) then
  107.   Face(Move.DIR_EAST)
  108.   doMultiple(ForceForward, delta.x)
  109.  end
  110. end
  111.  
  112. function alignNorthSouth(delta)
  113.  if (delta.z < 0) then
  114.   Face(Move.DIR_NORTH)
  115.   doMultiple(ForceForward, 0 - delta.z)
  116.  elseif (delta.z > 0) then
  117.   Face(Move.DIR_SOUTH)
  118.   doMultiple(ForceForward, delta.z)
  119.  end
  120. end
  121.  
  122. function alignUpDown(delta)
  123.  if (delta.y < 0) then
  124.   doMultiple(ForceDown, 0 - delta.y)
  125.  elseif (delta.y > 0) then
  126.   doMultiple(ForceUp, delta.y)
  127.  end
  128. end
  129.  
  130. function doMultiple(func, count)
  131.  for i = 1, count do
  132.   func()
  133.  end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement