ZombieZach

ZombieZach's Turtle api

Dec 6th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. -- Turtle API: built by ZomibeZach
  2. -- F 0= north 1= east 2= south 3= west
  3. -- south z+ east x+
  4. x = 0
  5. y = 0
  6. z = 0
  7. facing = 0
  8.  
  9. function tFuel()
  10.     if turtle.getFuelLevel() <= 10 then
  11.         turtle.select(16)
  12.         turtle.refuel(1)
  13.         turtle.select(1)
  14.         rednet.open("right")
  15.         rednet.send(10, "Refueled to level: "..turtle.getFuelLevel())
  16.         rednet.close("right")
  17.     end
  18. end
  19.  
  20. function startLoc()
  21.     rednet.open("right")
  22.     x, y, z = gps.locate(3)
  23.     facing = 0
  24.     rednet.close("right")
  25.     return facing
  26. end
  27.  
  28. function getLoc()
  29.     return x, y, z, facing
  30. end
  31.  
  32. function tl(ct)
  33.     ct = ct or 1
  34.     for i = 1, ct do
  35.         turtle.turnLeft()
  36.         facing = (facing + 3)%4
  37.     end
  38.     return i
  39. end
  40.  
  41. function tr(ct)
  42.     ct = ct or 1
  43.     for i = 1, ct do
  44.         turtle.turnRight()
  45.         facing = (facing + 1)%4
  46.     end
  47. end
  48.  
  49. function up()
  50.     tFuel()
  51.     if turtle.detectUp() == false then
  52.         turtle.up()
  53.         y = y + 1
  54.     end
  55. end
  56.  
  57. function dn()
  58.     tFuel()
  59.     if turtle.detectDown() == false then
  60.         turtle.down()
  61.         y = y - 1
  62.     end
  63. end
  64.  
  65. function fd()
  66.     tFuel()
  67.     if turtle.detect() == false then
  68.         turtle.forward()
  69.         if facing == 0 then
  70.             z = z + 1
  71.         elseif facing == 2 then
  72.             z = z - 1
  73.         elseif facing == 1 then
  74.             x = x + 1
  75.         elseif facing == 3 then
  76.             x = x - 1
  77.         end
  78.     end
  79. end
  80.  
  81. function bk()
  82.     rt(2)
  83.     if turtle.detect() == false then
  84.         fd()
  85.         if facing == 0 then
  86.             z = z + 1
  87.         elseif facing == 2 then
  88.             z = z - 1
  89.         elseif facing == 1 then
  90.             x = x + 1
  91.         elseif facing == 3 then
  92.             x = x - 1
  93.         end
  94.        
  95.     end
  96. end
  97.  
  98. function gotoX(toX)
  99.     if toX > x then
  100.         face(3)
  101.     elseif toX< x then
  102.         face(1)
  103.     end
  104.    
  105.     while x ~= toX do
  106.         fd()
  107.         if turtle.detect() == true then
  108.             tl()
  109.             if turtle.detect() == false then
  110.                 fd()
  111.                 tr()
  112.             elseif turtle.detect() == true then
  113.                 tl()
  114.                 if turtle.detect() == false then
  115.                     fd()
  116.                     tr(2)
  117.                    
  118.                 end
  119.             else
  120.                 tl(2)
  121.                 if turtle.detect() == false then
  122.                     fd()
  123.                     tr(3)
  124.                 end
  125.             end
  126.         end
  127.     end
  128. end
  129.  
  130.  
  131. function gotoZ(toZ)
  132.  
  133.     if toZ > z then
  134.         face(2)
  135.        
  136.     elseif toZ < z then
  137.         face(0)
  138.     end
  139.    
  140.     while z ~= toZ do
  141.     fd()
  142.         if turtle.detect() == true then
  143.             tl()
  144.             if turtle.detect() == false then
  145.                 fd()
  146.                 tr()
  147.             elseif turtle.detect() == true then
  148.                 tl()
  149.                 if turtle.detect() == false then
  150.                     fd()
  151.                     tr(2)
  152.                 end
  153.             else
  154.                 tl(2)
  155.                 if turtle.detect() == false then
  156.                     fd()
  157.                     tr(3)
  158.                 end
  159.             end
  160.         end
  161.     end
  162. end
  163.  
  164. function gotoY(toY)
  165.     if toY > y then
  166.         while toY ~= y do
  167.             dn()
  168.         end
  169.     elseif toY < y then
  170.         while toY ~= y do
  171.             up()
  172.         end
  173.     end
  174. end
  175.  
  176. function gotoA(toX, toY, toZ)
  177.     gotoX(toX)
  178.     gotoY(toY)
  179.     gotoZ(toZ)
  180. end
  181.  
  182. function face(dir)
  183.     if dir == 0 and facing == 3 then
  184.         tr()
  185.     elseif dir == 3 and facing == 0 then
  186.         tl()
  187.     elseif dir > facing then
  188.         tr(dir - facing)
  189.     elseif dir < facing then
  190.         tl(facing - dir)
  191.     end
  192. end
  193.  
  194. function getX()
  195.     return x
  196. end
  197.  
  198. function getZ()
  199.     return z
  200. end
  201.  
  202. function getY()
  203.     return y
  204. end
  205.    
  206.    
  207. print("Turtles Made Simple: T-APIS")
Advertisement
Add Comment
Please, Sign In to add comment