Guest User

gpi

a guest
Oct 9th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. _G["gpi"] = {}
  2. North,South,West,East,Invalid = 0,1,2,3,-1
  3. gpi.direction = -1
  4. gpi.location = {0,0,0}
  5. function gpi.locate(x,y,z)
  6.   if x == nil then
  7.     x2, y2, z2 = gps.locate(5)
  8.     if x2 then
  9.       oldTForward()
  10.       local x3, y3, z3 = gps.locate(5)
  11.       if x3 then
  12.         if x3 ~= x2 then
  13.           if x3 > x2 then
  14.             gpi.direction = 3
  15.           else
  16.             gpi.direction = 2
  17.           end
  18.         else
  19.           if z3 ~= z2 then
  20.             if z3 > z2 then
  21.               gpi.direction = 1
  22.             else
  23.               gpi.direction = -1
  24.             end
  25.           else
  26.             gpi.direction = -1
  27.           end
  28.         end
  29.       end
  30.     end
  31.   elseif y == nil then
  32.     error("Error: 3 or 0 Coordinates required.")
  33.   end
  34.   turtle.back()
  35.   gpi.location[1] = x2
  36.   gpi.location[2] = y2
  37.   gpi.location[3] = z2
  38.   return x2, y2, z2, gpi.direction
  39. end
  40. oldTForward = turtle.forward
  41. gpi.locate()
  42. function gpi.forward()
  43.   if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel <= 1 then
  44.   if gpi.direction > -1 and gpi.direction < 4 then
  45.   if oldTForward() then
  46.     if gpi.direction == 0 then
  47.       gpi.location[3] = gpi.location[3] - 1
  48.       oldTForward()
  49.     elseif gpi.direction == 1 then
  50.       gpi.location[3] = gpi.location[3] + 1
  51.       oldTForward()
  52.     elseif gpi.direction == 2 then
  53.       gpi.location[1] = gpi.location[1] - 1
  54.       oldTForward()
  55.     elseif gpi.direction == 3 then
  56.       gpi.location[1] = gpi.location[1] + 1
  57.       oldTForward()
  58.     end
  59.     return gpi.location[1], gpi.location[2], gpi.location[3]
  60.   else
  61.     return false
  62.   end
  63.   else
  64.     return false
  65.   end
  66.   else
  67.     return false
  68.   end
  69. end
  70. turtle.forward = gpi.forward
  71.  
  72. function gpi.restore()
  73.   turtle.forward = oldTForward
  74. end
  75.  
  76. oldTLeft = turtle.turnLeft
  77. function gpi.turnLeft()
  78.   if gpi.direction > -1 and gpi.direction < 4 then
  79.     if gpi.direction == 0 then
  80.       gpi.direction = 2
  81.       oldTLeft()
  82.     elseif gpi.direction == 1 then
  83.       gpi.direction = 3
  84.       oldTLeft()
  85.     elseif gpi.direction == 2 then
  86.       gpi.direction = 1
  87.       oldTLeft()
  88.     elseif gpi.direction == 3 then
  89.       gpi.direction = 0
  90.       oldTLeft()
  91.     end
  92.     return gpi.direction
  93.   else
  94.     return false
  95.   end
  96. end
  97. turtle.turnLeft = gpi.turnLeft
  98. oldTRight = turtle.turnRight()
  99. function gpi.turnRight()
  100.   if gpi.direction > -1 and gpi.direction < 4 then
  101.     if gpi.direction == 0 then
  102.       gpi.direction = 3
  103.       oldTRight()
  104.     elseif gpi.direction == 1 then
  105.       gpi.direction = 2
  106.       oldTRight()
  107.     elseif gpi.direction == 2 then
  108.       gpi.direction = 0
  109.       oldTRight()
  110.     elseif gpi.direciton == 3 then
  111.       gpi.direction = 1
  112.       oldTRight()
  113.     end
  114.     return gpi.direction
  115.   else
  116.     return false
  117.   end
  118. end
  119. turtle.turnRight = gpi.turnRight
  120. oldTBack = turtle.back
  121. function gpi.back()
  122.   if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel() >= 1 then
  123.   if gpi.direction > -1 and gpi.direction < 4 then
  124.   if oldTBack() then
  125.     oldTBack()
  126.     if gpi.direction == 0 then
  127.       gpi.location[3] = gpi.location[3] + 1
  128.     elseif gpi.direction == 1 then
  129.       gpi.location[3] = gpi.location[3] - 1
  130.     elseif gpi.direction == 2 then
  131.       gpi.location[1] = gpi.location[1] + 1
  132.     elseif gpi.direction == 3 then
  133.       gpi.location[1] = gpi.location[1] - 1
  134.     end
  135.     return gpi.location[1], gpi.location[2], gpi.location[3]
  136.   else
  137.     return false
  138.   end
  139.   else
  140.     return false
  141.   end
  142.   else
  143.     return false
  144.   end
  145. end
  146. turtle.back = gpi.back
  147. oldTUp = turtle.up
  148. function gpi.up()
  149.   if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel >= 1 then
  150.   if gpi.direction > -1 and gpi.direction < 4 then
  151.   if oldTUp() then
  152.     oldTUp()
  153.     gpi.location[2] = gpi.location[2] + 1
  154.     return gpi.location[1], gpi.location[2], gpi.location[3]
  155.   else
  156.     return false
  157.   end
  158.   else
  159.     return false
  160.   end
  161.   else
  162.     return false
  163.   end
  164. end
  165. turtle.up = gpi.up
  166. oldTDown = turtle.down
  167. function gpi.down()
  168.   if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel >= 1 then
  169.   if gpi.direction > -1 and gpi.direction < 4 then
  170.   if oldTDown() then
  171.     oldTDown()
  172.     gpi.location[2] = gpi.location[2] - 1
  173.     return gpi.location[1], gpi.location[2], gpi.location[3]
  174.   else
  175.     return false
  176.   end
  177.   else
  178.     return false
  179.   end
  180.   else
  181.     return false
  182.   end
  183. end
  184. turtle.down = gpi.down
  185. function gpi.getDirection()
  186.   return gpi.direction
  187. end
  188. function gpi.setDirection(dire)
  189.   if not tonumber(dire) then
  190.     error("Error: Got "..type(dire)..". Number Or Compass Expected.")
  191.   end
  192.   if dire > -1 and dire < 4 then
  193.   if gpi.direction > -1 and gpi.direction < 4 then
  194.   while gpi.direction ~= dire do
  195.     gpi.turnLeft()
  196.   end
  197.   return direction
  198.   else
  199.     return false
  200.   end
  201.   else
  202.     return false
  203.   end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment