Advertisement
BigSHinyToys

turtle extention

May 8th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. --[[
  2.         Extended turtle functions
  3.         by BigSHinyToys
  4. ]]--
  5.  
  6. if not turtle then
  7.     error("Not turtle")
  8.     return
  9. end
  10.  
  11. if turtle.restore then
  12.     error("API currently active already")
  13.     return
  14. end
  15.  
  16. local oldUp = turtle.up
  17. local oldDown = turtle.down
  18. local oldBack = turtle.back
  19. local oldForward = turtle.forward
  20.  
  21. local oldTurnLeft = turtle.turnLeft
  22. local oldTurnRight = turtle.turnRight
  23.  
  24. local pos = {x = 0,y = 0,z = 0,f = 1}
  25. local tFace= {"north","east","south","west"}
  26. local tInc = {{"y",1},{"x",1},{"y",-1},{"x",-1}}
  27.  
  28. function turtle.restore()
  29.     turtle.up = oldUp
  30.     turtle.down = oldDown
  31.     turtle.back = oldBack
  32.     turtle.forward = oldForward
  33.  
  34.     turtle.turnLeft = oldTurnLeft
  35.     turtle.turnRight = oldTurnRight
  36.    
  37.     turtle.restore = nil
  38.     turtle.getInternalGPS = nil
  39.     turtle.setInternalGPS = nil
  40.     turtle.face = nil
  41.     turtle.getFace = nil
  42.     turtle.goto = nil
  43. end
  44.  
  45. function turtle.getInternalGPS()
  46.     return pos.x , pos.y , pos.z , pos.f
  47. end
  48.  
  49. function turtle.setInternalGPS(...)
  50.     local tIn = {...}
  51.     for i = 1,4 do
  52.         if tIn[i] and type(tIn[i]) == "number" then
  53.            
  54.         else
  55.             return false
  56.         end
  57.     end
  58.     if tIn[4] >= 1 and tIn[4] <= 4 then
  59.         pos.x = tIn[1]
  60.         pos.y = tIn[2]
  61.         pos.z = tIn[3]
  62.         pos.f = tIn[4]
  63.         return true
  64.     else
  65.         return false
  66.     end
  67. end
  68.  
  69. function turtle.up()
  70.     if oldUp() then
  71.         pos.z = pos.z + 1
  72.         return true
  73.     else
  74.         return false
  75.     end
  76. end
  77.  
  78. function turtle.down()
  79.     if oldDown() then
  80.         pos.z = pos.z - 1
  81.         return true
  82.     else
  83.         return false
  84.     end
  85. end
  86.  
  87. function turtle.turnLeft()
  88.     pos.f = pos.f - 1
  89.     if pos.f < 1 then
  90.         pos.f = 4
  91.     end
  92.     return oldTurnLeft()
  93. end
  94.  
  95. function turtle.turnRight()
  96.     pos.f = pos.f + 1
  97.     if pos.f > 4 then
  98.         pos.f = 1
  99.     end
  100.     return oldTurnRight()
  101. end
  102.  
  103. function turtle.back()
  104.     if oldBack() then
  105.         local face = tInc[pos.f][1]
  106.         pos[face] = pos[face] - tInc[pos.f][2]
  107.         return true
  108.     else
  109.         return false
  110.     end
  111. end
  112.  
  113. function turtle.forward()
  114.     if oldForward() then
  115.         local face = tInc[pos.f][1]
  116.         pos[face] = pos[face] + tInc[pos.f][2]
  117.         return true
  118.     else
  119.         return false
  120.     end
  121. end
  122.  
  123. function turtle.face(direction)
  124.     local function test(nFace)
  125.         if nFace < 1 then
  126.             nFace = 4
  127.         end
  128.         if nFace > 4 then
  129.             nFace = 1
  130.         end
  131.         return nFace
  132.     end
  133.    
  134.     local face = direction
  135.     if direction < 1 or direction > 4 then
  136.         return false
  137.     end
  138.     if face == pos.f then
  139.         return true
  140.     elseif face == test(pos.f+1) then
  141.         return turtle.turnRight()
  142.     elseif face == test(pos.f-1) then
  143.         return turtle.turnLeft()
  144.     else
  145.         turtle.turnRight()
  146.         turtle.turnRight()
  147.         return true
  148.     end
  149. end
  150.  
  151. function turtle.getFace()
  152.     return pos.f
  153. end
  154.  
  155. function turtle.goto(x,y,z,f)
  156.     if x and x ~= pos.x then
  157.         if pos.x < x then
  158.             turtle.face(2)
  159.         else
  160.             turtle.face(4)
  161.         end
  162.         for i = 1,math.abs(pos.x - x) do
  163.             if not turtle.forward() then
  164.                 return false
  165.             end
  166.         end
  167.     end
  168.     if y and y ~= pos.y then
  169.         if pos.y < y then
  170.             turtle.face(1)
  171.         else
  172.             turtle.face(3)
  173.         end
  174.         for i = 1,math.abs(pos.y - y) do
  175.             if not turtle.forward() then
  176.                 return false
  177.             end
  178.         end
  179.     end
  180.     if z and z ~= pos.z then
  181.         if pos.z < z then
  182.             for i = 1,math.abs(pos.z - z) do
  183.                 turtle.up()
  184.             end
  185.         else
  186.             for i = 1,math.abs(pos.z - z) do
  187.                 turtle.down()
  188.             end
  189.         end
  190.     end
  191.     if f and f >= 1 and f <= 4 then
  192.         turtle.face(f)
  193.     end
  194. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement