Advertisement
theinsekt

turtle3

Apr 11th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. -- API for getting and using the
  2. -- turtle functions in a given directions
  3.  
  4. -- advantages:
  5. -- Can use direction as an input argument to your
  6. -- function instead of having to write one
  7. -- for each direction,
  8. -- also can get the reverse direction
  9.  
  10. -- tip:
  11. -- if called function nil, then the error  probably was that
  12. -- it was an invalid direction.
  13. -- the get functions returns nil if it's an invalid
  14. -- direction.
  15. -- you can see in the table in the function which
  16. -- directions are supported.
  17.  
  18. -- directions
  19. f="f" -- forward
  20. b="b" -- back
  21. u="u" -- up
  22. d="d" -- down
  23. tr="r" -- turn right
  24. tl="l" -- turn left
  25.  
  26. -- lookup table for: function inv(dir)
  27. invT={
  28. f=b,
  29. b=f,
  30. u=d,
  31. d=u,
  32. tr=tl,
  33. tl=tr,
  34. }
  35. -- returns the reverse/inverted direction
  36. function inv(dir)
  37.  return invT[dir]
  38. end --function
  39.  
  40. -- returns true if the direction, is r or l
  41. -- else returns false
  42. function isTurn(dir)
  43.   return dir=="r" or dir=="l"
  44. end
  45.  
  46. function isUorD(dir)
  47.   return dir=="u" or dir=="d"
  48. end
  49.  
  50. function isForB(dir)
  51.   return dir=="f" or dir=="b"
  52. end
  53.  
  54. -- lookup tables to quickly get the
  55. -- turtle function for a given direction
  56. detectT={
  57. f=turtle.detect,
  58. u=turtle.detectUp,
  59. d=turtle.detectDown,
  60. }
  61. compareT={
  62. f=turtle.compare,
  63. u=turtle.compareUp,
  64. d=turtle.compareDown,
  65. }
  66. digT={
  67. f=turtle.dig,
  68. u=turtle.digUp,
  69. d=turtle.digDown,
  70. }
  71. goT={
  72. f=turtle.forward,
  73. b=turtle.back,
  74. u=turtle.up,
  75. d=turtle.down,
  76. tr=turtle.turnRight,
  77. tl=turtle.turnLeft,
  78. }
  79.  
  80.  
  81.  
  82.  
  83. -- functions for calling the a turtle function in the
  84. -- given direction
  85.  
  86. function detect(dir)
  87.   local f=getDetect(dir)
  88.   if f==nil then
  89.     error("Error: Invalid direction")
  90.   end
  91.   return f()
  92. end --function
  93.  
  94. function compare(dir)
  95.   local f=getCompareT(dir)
  96.   if f==nil then
  97.     error("Error: Invalid direction")
  98.   end
  99.   return f()
  100. end --function
  101.  
  102. function dig(dir)
  103.   local f=getDig(dir)
  104.   if f==nil then
  105.     error("Error: Invalid direction")
  106.   end
  107.   return f()
  108. end --function
  109.  
  110. function go(dir)
  111.   local f=getGo(dir)
  112.   if f==nil then
  113.     error("Error: Invalid direction")
  114.   end
  115.   return f()
  116. end --function
  117.  
  118. -- get the turtle function in the given direction
  119. -- useful if another function takes, a function
  120. -- as an input argument
  121.  
  122. function getDetect(dir)
  123.   return detectT[dir]
  124. end --function
  125.  
  126. function getCompare(dir)
  127.   return compareT[dir]
  128. end --function
  129.  
  130. function getDig(dir)
  131.   return digT[dir]
  132. end --function
  133.  
  134. function getGo(dir)
  135.   return goT[dir]
  136. end --function
  137.  
  138.  
  139. --experimental stuff
  140. placeT={
  141. f=turtle.place,
  142. u=turtle.placeUp,
  143. d=turtle.placeDown,
  144. }
  145. function getPlace(dir)
  146.   return placeT[dir]
  147. end
  148.  
  149. --message only works for forward and it's a sign
  150. function place(dir,message)
  151.   local f=getPlace(dir)
  152.   if f==nil then
  153.     error("Error: Invalid direction")
  154.   end
  155.   return f(message)
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement