Advertisement
Wyvern67

Turtle Track API - Real Coordinates

Jan 9th, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. local old={}
  2. turtle.x=0
  3. turtle.y=0
  4. turtle.z=0
  5. turtle.facing="north"
  6.  
  7. if turtle.onEachMove==nil then
  8.     local function onEachMove()
  9.         return true
  10.     end
  11.     turtle.onEachMove=onEachMove
  12. end
  13.  
  14. local function directionToNumber(direction)
  15.     if direction == "north" then
  16.         return 0
  17.     elseif direction == "east" then
  18.         return 1
  19.     elseif direction == "south" then
  20.         return 2
  21.     elseif direction == "west" then
  22.         return 3
  23.     end
  24. end
  25.  
  26. local function turnTo(direction)
  27.     if ( directionToNumber(turtle.facing ) - 1) % 4 == directionToNumber(direction) then
  28.         while turtle.facing ~= direction do
  29.             turtle.turnLeft()
  30.         end
  31.     else
  32.         while turtle.facing ~= direction do
  33.             turtle.turnRight()
  34.         end
  35.     end
  36.     return true
  37. end
  38. turtle.turnTo = turnTo
  39.  
  40. local function goto(x,y,z)
  41.     local success = true
  42.     local oldDirection = turtle.facing
  43.  
  44.     while turtle.y < y do --go y+
  45.         success = success and turtle.up()
  46.         if not success then return false end
  47.     end
  48.     while turtle.y > y do --go y-
  49.         success = success and turtle.down()
  50.         if not success then return false end
  51.     end
  52.  
  53.     if turtle.x < x then --go x+
  54.         turtle.turnTo("east")
  55.         while turtle.x < x do
  56.             success = success and turtle.forward()
  57.             if not success then return false end
  58.         end
  59.     end
  60.     if turtle.x > x then --go x-
  61.         turtle.turnTo("east")
  62.         while turtle.x > x do
  63.             success = success and turtle.back()
  64.             if not success then return false end
  65.         end
  66.     end
  67.  
  68.     if turtle.z < z then --go z+
  69.         turtle.turnTo("south") --that's z+, i don't know either. fuck that
  70.         while turtle.z < z do
  71.             success = success and turtle.forward()
  72.             if not success then return false end
  73.         end
  74.     end
  75.     if turtle.z > z then --go z-
  76.         turtle.turnTo("south")
  77.         while turtle.z > z do
  78.             success = success and turtle.back()
  79.             if not success then return false end
  80.         end
  81.     end
  82.  
  83.     turtle.turnTo(oldDirection)
  84.     return success
  85. end
  86. turtle.goto = goto
  87.  
  88. old.turtleUp=turtle.up
  89. function up()
  90.     if old.turtleUp() then
  91.         turtle.y = turtle.y+1
  92.         turtle.onEachMove()
  93.         return true
  94.     else
  95.         return false
  96.     end
  97. end
  98. turtle.up=up
  99.  
  100. old.turtleDown=turtle.down
  101. function down()
  102.     if old.turtleDown() then
  103.         turtle.y=turtle.y-1
  104.         turtle.onEachMove()
  105.         return true
  106.     else
  107.         return false
  108.     end
  109. end
  110. turtle.down=down
  111.  
  112. old.turtleForward=turtle.forward
  113. function forward()
  114.     if old.turtleForward() then
  115.         if turtle.facing == "north" then
  116.             turtle.z=turtle.z-1
  117.         elseif turtle.facing == "south" then
  118.             turtle.z=turtle.z+1
  119.         elseif turtle.facing == "east" then
  120.             turtle.x=turtle.x+1
  121.         elseif turtle.facing == "west" then
  122.             turtle.x=turtle.x-1
  123.         end
  124.         turtle.onEachMove()
  125.         return true
  126.     else
  127.         return false
  128.     end
  129. end
  130. turtle.forward = forward
  131.  
  132. old.turtleBack=turtle.back
  133. function back()
  134.     if old.turtleBack() then
  135.         if turtle.facing == "north" then
  136.             turtle.z=turtle.z+1
  137.         elseif turtle.facing == "south" then
  138.             turtle.z=turtle.z-1
  139.         elseif turtle.facing == "east" then
  140.             turtle.x=turtle.x-1
  141.         elseif turtle.facing == "west" then
  142.             turtle.x=turtle.x+1
  143.         end
  144.         turtle.onEachMove()
  145.         return true
  146.     else
  147.         return false
  148.     end
  149. end
  150. turtle.back = back
  151.  
  152. old.turtleTurnLeft=turtle.turnLeft
  153. function turnLeft()
  154.     if old.turtleTurnLeft() then
  155.         if turtle.facing == "north" then
  156.             turtle.facing = "west"
  157.         elseif turtle.facing == "west" then
  158.             turtle.facing = "south"
  159.         elseif turtle.facing == "south" then
  160.             turtle.facing = "east"
  161.         elseif turtle.facing == "east" then
  162.             turtle.facing = "north"
  163.         end
  164.         turtle.onEachMove()
  165.         return true
  166.     else
  167.         return false
  168.     end
  169. end
  170. turtle.turnLeft = turnLeft
  171.  
  172. old.turtleTurnRight=turtle.turnRight
  173. function turnRight()
  174.     if old.turtleTurnRight() then
  175.         if turtle.facing == "north" then
  176.             turtle.facing = "east"
  177.         elseif turtle.facing == "east" then
  178.             turtle.facing = "south"
  179.         elseif turtle.facing == "south" then
  180.             turtle.facing = "west"
  181.         elseif turtle.facing == "west" then
  182.             turtle.facing = "north"
  183.         end
  184.         turtle.onEachMove()
  185.         return true
  186.     else
  187.         return false
  188.     end
  189. end
  190. turtle.turnRight = turnRight
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement