Advertisement
theinsekt

position2

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