Advertisement
oli414

[CC] new

Jun 24th, 2015
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. if not os.loadAPI("path") then
  2.     print("Please make sure to install the pathfinding API")
  3. end
  4.  
  5. position = vector.new(0, 0, 0)
  6. positionModifier = vector.new(0, 0, 0)
  7. direction = {x = 1, y = 0}
  8.  
  9. function update()
  10.     if turtle.detect() then
  11.         path.setMap(position:add(vector.new(direction.x, direction.y, 0)), -1)
  12.     else
  13.         path.setMap(position:add(vector.new(direction.x, direction.y, 0)), 1)
  14.     end
  15.     if turtle.detectUp() then
  16.         path.setMap(position:add(vector.new(0, 0, 1)), -1)
  17.     else
  18.         path.setMap(position:add(vector.new(0, 0, 1)), 1)
  19.     end
  20.     if turtle.detectDown() then
  21.         path.setMap(position:add(vector.new(0, 0, -1)), -1)
  22.     else
  23.         path.setMap(position:add(vector.new(0, 0, -1)), 1)
  24.     end
  25. end
  26.  
  27. function moveForward()
  28.     update()
  29.     if (not turtle.detect()) and turtle.forward() then
  30.         position = position:add(vector.new(direction.x, direction.y, 0))
  31.         return true
  32.     end
  33.     return false
  34. end
  35.  
  36. function moveBack()
  37.     update()
  38.     if turtle.back() then
  39.         position = position:sub(vector.new(direction.x, direction.y, 0))
  40.         return true
  41.     end
  42.     return false
  43. end
  44.  
  45. function moveUp()
  46.     update()
  47.     if (not turtle.detectUp()) and turtle.up() then
  48.         position = position:add(vector.new(0, 0, 1))
  49.         return true
  50.     end
  51.     return false
  52. end
  53.  
  54. function moveDown()
  55.     update()
  56.     if (not turtle.detectDown()) and turtle.down() then
  57.         position = position:add(vector.new(0, 0, -1))
  58.         return true
  59.     end
  60.     return false
  61. end
  62.  
  63. function turnLeft()
  64.     update()
  65.     turtle.turnLeft()
  66.     local temp = direction.y
  67.     direction.y = direction.x * -1
  68.     direction.x = temp
  69. end
  70.  
  71. function turnRight()
  72.     update()
  73.     turtle.turnRight()
  74.     local temp = direction.x
  75.     direction.x = direction.y * -1
  76.     direction.y = temp
  77. end
  78.  
  79. function rotateInDirection(dir)
  80.     if direction.x ~= dir.x or direction.y ~= dir.y then
  81.         if direction.x == dir.x or direction.y == dir.y then
  82.             turnLeft()
  83.             turnLeft()
  84.         elseif direction.x == dir.y and direction.y == dir.x * -1 then
  85.             turnRight()
  86.         else
  87.             turnLeft()
  88.         end
  89.     end
  90. end
  91.  
  92. function goTo(point)
  93.     path.generatePath(position, point)
  94.     local nextPosition = path.getPath()
  95.     while nextPosition ~= nil do
  96.         if nextPosition:sub(position):tostring() == vector.new(1, 0, 0):tostring() then
  97.             rotateInDirection({x = 1, y = 0})
  98.             if not moveForward() then
  99.                 path.generatePath(position, point)
  100.             end
  101.         elseif nextPosition:sub(position):tostring() == vector.new(-1, 0, 0):tostring() then
  102.             rotateInDirection({x = -1, y = 0})
  103.             if not moveForward() then
  104.                 path.generatePath(position, point)
  105.             end
  106.         elseif nextPosition:sub(position):tostring() == vector.new(0, 1, 0):tostring() then
  107.             rotateInDirection({x = 0, y = 1})
  108.             if not moveForward() then
  109.                 path.generatePath(position, point)
  110.             end
  111.         elseif nextPosition:sub(position):tostring() == vector.new(0, -1, 0):tostring() then
  112.             rotateInDirection({x = 0, y = -1})
  113.             if not moveForward() then
  114.                 path.generatePath(position, point)
  115.             end
  116.         elseif nextPosition:sub(position):tostring() == vector.new(0, 0, 1):tostring() then
  117.             if not moveUp() then
  118.                 path.generatePath(position, point)
  119.             end
  120.         elseif nextPosition:sub(position):tostring() == vector.new(0, 0, -1):tostring() then
  121.             if not moveDown() then
  122.                 path.generatePath(position, point)
  123.             end
  124.         end
  125.         nextPosition = path.getPath()
  126.     end
  127. end
  128.  
  129. while true do
  130.     p = vector.new(0,0,0)
  131.     p.x = tonumber(read())
  132.     p.y = tonumber(read())
  133.     p.z = tonumber(read())
  134.     goTo(p)
  135.     print("Done")
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement