Blackhome

Coord Vector Functions

Feb 1st, 2026 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.76 KB | Gaming | 0 0
  1.     -- pastebin get PE0k7NQY CVF
  2.  
  3.     CVF = {}
  4.  
  5.     function CVF.add(pos, delta)
  6.         local dy = delta.y
  7.         if not delta.y then dy = 0 end
  8.         if pos.y then dy = dy + pos.y end
  9.         return {x = pos.x + delta.x, y = dy, z = pos.z + delta.z}
  10.     end
  11.  
  12.     function CVF.sub(pos, delta)
  13.         local dy = 0
  14.         if delta.y then dy = -delta.y end
  15.         if pos.y then dy = pos.y + dy end
  16.         return {x = pos.x - delta.x, y = dy, z = pos.z - delta.z}
  17.     end
  18.  
  19.     function CVF.mult(s, vec)
  20.         local newVec = {}
  21.         for k, v in pairs(vec) do
  22.             newVec[k] = s * v
  23.         end
  24.         return newVec
  25.     end
  26.  
  27.     function CVF.equalCoords(coord1, coord2)
  28.         return coord1.x == coord2.x and coord1.y == coord2.y and coord1.z == coord2.z
  29.     end
  30.  
  31.     function CVF.getRightDir(orientation)
  32.         return {x = -orientation.z, z = orientation.x}
  33.     end
  34.  
  35.     function CVF.getLeftDir(orientation)
  36.         return {x = orientation.z, z = -orientation.x}
  37.     end
  38.  
  39.     function CVF.isXDir(dir)
  40.         if dir.x ~= 0 then return true end
  41.         return false
  42.     end
  43.  
  44.     function CVF.getTurnDirection(from, to)
  45.         local function vectorsEqual(a, b)
  46.             return a.x == b.x and a.z == b.z
  47.         end
  48.  
  49.         if vectorsEqual(from, to) then return nil end
  50.         local left  = {x = from.z, z = -from.x}
  51.         local right = {x = -from.z, z = from.x}
  52.         local back  = {x = -from.x, z = -from.z}
  53.         if vectorsEqual(to, left) then
  54.             return "left"
  55.         elseif vectorsEqual(to, right) then
  56.             return "right"
  57.         elseif vectorsEqual(to, back) then
  58.             return "around"
  59.         else
  60.             error("Invalid target direction")
  61.         end
  62.     end
  63.  
  64.     return CVF
Advertisement
Add Comment
Please, Sign In to add comment