Advertisement
Guest User

api_location

a guest
Mar 1st, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. x, y, z = 0, 0, 0
  2. direction = 0
  3.  
  4. function safeDig(side)
  5.   if not (peripheral.getType(side) == "turtle") then
  6.      if side == "front" then
  7.        return turtle.dig()
  8.      elseif side == "top" then
  9.        return turtle.digUp()
  10.      elseif side == "bottom" then
  11.        return turtle.digDown()
  12.      end
  13.   end
  14.  
  15.   return false
  16. end
  17.  
  18. function forward(steps)
  19.   steps = steps or 1
  20.  
  21.   for i = 1, steps do
  22.  
  23.     while not turtle.forward() do
  24.       safeDig("front")
  25.       sleep(0.6)
  26.     end
  27.    
  28.     if direction == 0 then
  29.       z = z - 1
  30.     elseif direction == 2 then
  31.       z = z + 1
  32.     elseif direction == 1 then
  33.       x = x + 1
  34.     elseif direction == 3 then
  35.       x = x - 1
  36.     end
  37.   end      
  38. end
  39.  
  40. function up()
  41.   while not turtle.up() do
  42.     safeDig("top")
  43.   end
  44.   y = y + 1
  45. end
  46.  
  47. function down()
  48.   while not turtle.down() do
  49.     safeDig("bottom")
  50.   end
  51.   y = y - 1
  52. end
  53.  
  54. function turnTo(newDirection)
  55.   local turns = newDirection - direction
  56.  
  57.   if turns <= -3 then
  58.     turns = turns + 4
  59.   elseif turns >= 3 then
  60.     turns = turns - 4
  61.   end
  62.  
  63.   while turns ~= 0 do
  64.     if turns < 0 then
  65.       turns = turns + 1
  66.       turtle.turnLeft()
  67.     else
  68.       turns = turns - 1
  69.       turtle.turnRight()
  70.     end
  71.   end
  72.  
  73.   direction = newDirection  
  74. end
  75.  
  76. function deploy(to)
  77.   forward()
  78.   if to < 0 then
  79.     turnTo(3)
  80.   elseif to > 0 then
  81.     turnTo(1)
  82.   end
  83.  
  84.   while x ~= to do
  85.     forward()
  86.   end
  87.  
  88.   turnTo(0)
  89.   forward()
  90. end
  91.  
  92. function dock()
  93.   turnTo(2)
  94.   while z < -1 do
  95.     forward()
  96.   end
  97.  
  98.   while y ~= -1 do
  99.     if y < -1 then
  100.       up()
  101.     else
  102.       down()
  103.     end
  104.   end
  105.  
  106.   if x < 0 then
  107.     while x < -1 do
  108.       turnTo(1)
  109.       forward()
  110.     end
  111.    
  112.     turnTo(2)
  113.     forward()
  114.     forward()
  115.     turnTo(1)
  116.     forward()
  117.   else
  118.     if x == 0 then
  119.       turnTo(1)
  120.       forward()
  121.     end
  122.      
  123.     while x > 1 do
  124.       turnTo(3)
  125.       forward()
  126.     end
  127.    
  128.     turnTo(2)
  129.     forward()
  130.     forward()
  131.     turnTo(3)
  132.     forward()
  133.   end
  134.  
  135.   up()
  136.   turnTo(0)
  137.   forward()
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement