Advertisement
theinsekt

position

Feb 22nd, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. --os.loadAPI("theinsekt/turtle2")
  2.  
  3. -- TODO: Change so that multiple positions can be used.
  4. -- this can implemented by not using these local variables and
  5. -- instead working on a position table.
  6. -- Then a new table can be created and changed independently
  7. -- of the old one.
  8. x=0 -- forward, back
  9. y=0 -- up, down
  10. z=0 -- right, left (not turn)
  11. rot=0 -- direction/rotation the turtle is facing
  12.  
  13. -- this can be used to make sure
  14. -- the turtle isn't outside a
  15. -- cube of a certain size
  16. function getLargest()
  17.   -- make local copies of the coordinates
  18.   -- but use the absolute value
  19.   local x=math.abs(x)
  20.   local y=math.abs(y)
  21.   local z=math.abs(z)
  22.   if x>y then
  23.     if x>z then
  24.       return x
  25.     else
  26.       return z
  27.     end
  28.   else
  29.     if y>z then
  30.       return y
  31.     else
  32.       return z
  33.     end
  34.   end
  35. end
  36.  
  37. function printit()
  38.   print("before: "..x.." "..y.." "..z.." "..rot)
  39. end
  40.  
  41. function reset()
  42.   x=0
  43.   y=0
  44.   z=0
  45.   rot=0
  46. end
  47.  
  48. -- put function in the table
  49. horzT={
  50.   [0]=function(length)
  51.     x=x+length
  52.   end,
  53.   [1]=function(length)
  54.     z=z+length
  55.   end,
  56.   [2]=function(length)
  57.     x=x-length
  58.   end,
  59.   [3]=function(length)
  60.     z=z-length
  61.   end,
  62. }
  63.  
  64. -- table that can update the coordinates
  65. -- if you go a given length in the
  66. -- in a direction
  67. updateT={
  68.   f=function(length)
  69.     horzT[rot](length)
  70.   end,
  71.   b=function(length)
  72.     horzT[rot](length)
  73.   end,
  74.   u=function(length)
  75.     y=y+length
  76.   end,
  77.   d=function(length)
  78.     y=y-length
  79.   end,
  80.   r=function(length)
  81.    rot=(rot+length)%4
  82.   end,
  83.   l=function(length)
  84.     rot=(rot-length)%4
  85.   end,
  86. }
  87.  
  88. -- updates the position values
  89. -- given the input values
  90. -- returns nil
  91. function update(dir, length)
  92.   --print("before: "..x.." "..y.." "..z.." "..rot)
  93.   --print(dir.." "..length)
  94.   updateT[dir](length)
  95.   --print("after: "..x.." "..y.." "..z.." "..rot)
  96. end
  97.  
  98. function whatIf(dir, length)
  99.   update(dir,length)
  100.   local a, b, c, d=get()
  101.   update(dir,-length)
  102.   return a,b,c,d
  103. end
  104.  
  105. function whatIfLargest(dir, length)
  106.   update(dir,length)
  107.   local res=getLargest()
  108.   --print(x.." "..y.." "..z" "..rot)
  109.   update(dir,-length)
  110.   return res
  111. end
  112.  
  113. -- returns the position values
  114. -- x,y,z,rot
  115. function get()
  116.   return x, y, z, rot
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement