Advertisement
Guest User

rotutil.lua

a guest
Jul 9th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. names={"South","West","North","East","Up","Down"}
  2. local fM={nil,3,1,2,4}
  3. offsets={{0,0,1},{-1,0,0},{0,0,-1},{1,0,0},{0,1,0},{0,-1,0}}
  4. opposite={3,4,1,2,6,5}
  5. function addDir(dir,x,y,z,t)
  6.   t=t or 1
  7.   if not offsets[dir] then error("invalid dir",2) end
  8.   if not (x and y and z) then error("must provide x,y,z",2) end
  9.   local px,py,pz=unpack(offsets[dir])
  10.   --print("x=",x," px=",px," y=",y," py=",py," z=",z," pz=",pz," t=",t)
  11.   return x+px*t,y+py*t,z+pz*t
  12. end
  13. function fromMeta(meta)
  14.   return fM[meta]
  15. end
  16. function rotL(dir)
  17.   if dir<1 or dir>6 then error("invalid dir",2) end
  18.   if dir>=5 then
  19.     return dir
  20.   end
  21.   if dir == 1 then return 4 end
  22.   return dir-1
  23. end
  24. function rotR(dir)
  25.   if dir<1 or dir>6 then error("invalid dir",2) end
  26.   if dir>=5 then
  27.     return dir
  28.   end
  29.   if dir ==4 then return 1 end
  30.   return dir+1
  31. end
  32. function bound(dir,x,y,z,width,height,depth)
  33.  if not names[dir] then error("invalid dir",2) end
  34.  if not (x and y and z and width and height and depth) then error("must provide 7 args to bound",2) end
  35.  if depth>0 then depth=depth-1 else depth=depth+1 end
  36.  if height>0 then height=height-1 else height=height+1 end
  37.  if width>0 then width=width-1 else width=width+1 end
  38.  local x2,_,z2=addDir(dir,x,y,z,depth)
  39.  local x3,_,z3=addDir(rotR(dir),x,y,z,width)
  40.  if x2==x then
  41.    x=x3
  42.    z=z2
  43.  else
  44.    x=x2
  45.    z=z3
  46.  end
  47.  return x,y+height,z
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement