Advertisement
theinsekt

poz

Nov 7th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. --this is an experiment
  2. --will hold movement help functions
  3. --and change in position caused by movement
  4.  
  5.  
  6. -- directions
  7. f="f" -- forward
  8. b="b" -- back
  9. u="u" -- up
  10. d="d" -- down
  11. tr="r" -- turn right
  12. tl="l" -- turn left
  13. r="r" -- right, not used yet
  14. l="l" -- left, not used yet
  15. n="n" -- north (the direction that has been set to north, not the in game north)
  16. s="s" -- south
  17. w="w" -- west
  18. e="e" -- east
  19.  
  20. sr="sr" -- strafe right, not yet implemented
  21. sl="sl" -- strafe left, not yet implemented
  22.  
  23.  
  24. -- position data names
  25. x="x" -- forward, back
  26. y="y" -- up, down
  27. z="z" -- right, left (not turn)
  28. dir="dir" -- direction the turtle is facing, n, e, s, w
  29.  
  30. -- lookup table for: function rev(dir)
  31. revT={
  32. f=b,
  33. b=f,
  34. u=d,
  35. d=u,
  36. r=l,
  37. l=r,
  38. n=s,
  39. s=n,
  40. w=e,
  41. e=w,
  42. }
  43. -- returns the reverse/inverted direction
  44. function rev(dir)
  45.  return revT[dir]
  46. end --function
  47.  
  48.  
  49. -- functions to update pos in horizontal movement
  50. horzT={
  51.   n=function(posT,length)
  52.     posT[x]=posT[x]+length
  53.   end,
  54.   e=function(posT,length)
  55.     posT[z]=posT[z]+length
  56.   end,
  57.   s=function(posT,length)
  58.     posT[x]=posT[x]-length
  59.   end,
  60.   w=function(posT,length)
  61.     posT[z]=posT[z]-length
  62.   end,
  63. }
  64.  
  65. getUpdateFuncT={
  66. f=function(dir)
  67.     return horzT[dir]
  68. end,
  69. b=function(dir)
  70.     return horzT[rev(dir)]
  71. end,
  72. tr=function(dir)
  73.  
  74. end
  75. }
  76.  
  77. -- lookup tables to quickly get the
  78. -- turtle function for a given direction
  79. detectT={
  80. f=turtle.detect,
  81. u=turtle.detectUp,
  82. d=turtle.detectDown,
  83. }
  84. compareT={
  85. f=turtle.compare,
  86. u=turtle.compareUp,
  87. d=turtle.compareDown,
  88. }
  89. digT={
  90. f=turtle.dig,
  91. u=turtle.digUp,
  92. d=turtle.digDown,
  93. }
  94. goT={
  95. f=turtle.forward,
  96. b=turtle.back,
  97. u=turtle.up,
  98. d=turtle.down,
  99. tr=turtle.turnRight,
  100. tl=turtle.turnLeft,
  101. }
  102.  
  103. turnT={
  104. tr=turtle.turnRight,
  105. tl=turtle.turnLeft,
  106. }
  107.  
  108.  
  109.  
  110. go2T={
  111. f=function(pos,length)
  112.     -- go forward the given length
  113.     local dist=0
  114.     while dist < length and turtle.forward() do
  115.         dist=dist+1
  116.     end
  117.     -- update the position
  118.     horzT[pos["dir"]](pos,dist)
  119.     -- return the completed length
  120.     return dist
  121. end
  122.  
  123.  
  124.  
  125. b=function(length,pos)
  126.     -- go back the given length
  127.     local dist=0
  128.     while dist < length and turtle.back() do
  129.         dist=dist+1
  130.     end
  131.     -- update the position
  132.     horzT[pos[dr]](pos,-dist)
  133.     -- return the completed length
  134.     return dist
  135. end
  136.  
  137.  
  138. }
  139.  
  140.  
  141. function testGoHelp()
  142.    
  143.     go=turtle.forward
  144.     update=getUpdateFuncT[f](pos[dr])
  145. end
  146.  
  147.    
  148.  
  149. function helpGo(go, update, length,pos)
  150.     local dist=0
  151.     while dist < length and go() do
  152.         dist = dist + 1
  153.     end
  154.     update(pos,dist)
  155.     return dist
  156. end
  157.  
  158. function getNewPoz(x,y,z,dir)
  159. if x==nil then x=0 end
  160. if y==nil then y=0 end
  161. if z==nil then z=0 end
  162. if rot==nil then dir="n" end
  163. local pos={
  164. ["x"]=x,
  165. ["y"]=y,
  166. ["z"]=z,
  167. [dr]=dir,
  168. }
  169. end
  170.  
  171. -- may change the direction the turtle is facing
  172. go(dir,length,pos)
  173.     if length < 0 then
  174.         return go(rev(dir),-length,pos)
  175.     end
  176.     --length must be positive here
  177.     gof=goT[dir]
  178.     if gof == nil then
  179.         --turn to right direction
  180.        
  181.        
  182.         dir=f
  183.         gof=goT[dir]
  184.     end
  185.     local dist=0
  186.     while dist<length and gof() do
  187.        
  188.     end
  189. end
  190.  
  191. -- lookup table for knowing the tuen value
  192. -- when facing direction and want to face other direction
  193. faceT={
  194. n={n=0,e=1,s=2,w=-1},
  195. e={n=-1,e=0,s=1,w=2},
  196. w={n=1,e=2,s=-1,w=0},
  197. s={n=2,e=-1,s=0,w=1},
  198. }
  199.  
  200. --warning: does not update pos
  201. rotate(length)
  202.     if length==0 then
  203.         return true
  204.     end
  205.     local turning
  206.     if length>0 then
  207.         turning=turtle.turnRight
  208.     else
  209.         turning=turle.turnLeft
  210.     end
  211.     local repeats=0
  212.     while repeats<length do
  213.         turning()
  214.         repeats=repeats+1
  215.     end
  216.     return true
  217. end
  218.  
  219.  
  220. -- turns the turtle so it's facing in the given newDir
  221. face(newDir, pos)
  222.     local oldDir=pos["dir"]
  223.     rotate(faceT[oldDir][newDir])
  224.     pos["dir"]=newDir
  225.     return true
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement