- Add more functions for 'Utils.lua'.
- Update logic for BAirboss.
This commit is contained in:
@@ -1,6 +1,46 @@
|
||||
Utils = {}
|
||||
|
||||
do
|
||||
--- Creates a deep copy of a object.
|
||||
-- Usually this object is a table.
|
||||
-- See also: from http://lua-users.org/wiki/CopyTable
|
||||
-- @param object object to copy
|
||||
-- @return copy of object
|
||||
function Utils.deepCopy(object)
|
||||
local lookup_table = {}
|
||||
local function _copy(object)
|
||||
if type(object) ~= "table" then
|
||||
return object
|
||||
elseif lookup_table[object] then
|
||||
return lookup_table[object]
|
||||
end
|
||||
local new_table = {}
|
||||
lookup_table[object] = new_table
|
||||
for index, value in pairs(object) do
|
||||
new_table[_copy(index)] = _copy(value)
|
||||
end
|
||||
return setmetatable(new_table, getmetatable(object))
|
||||
end
|
||||
return _copy(object)
|
||||
end
|
||||
|
||||
|
||||
--- Vector addition.
|
||||
-- @tparam Vec3 vec1 first vector
|
||||
-- @tparam Vec3 vec2 second vector
|
||||
-- @treturn Vec3 new vector, sum of vec1 and vec2.
|
||||
function Utils.vecAdd(vec1, vec2)
|
||||
return {x = vec1.x + vec2.x, y = vec1.y + vec2.y, z = vec1.z + vec2.z}
|
||||
end
|
||||
|
||||
--- Vector substraction.
|
||||
-- @tparam Vec3 vec1 first vector
|
||||
-- @tparam Vec3 vec2 second vector
|
||||
-- @treturn Vec3 new vector, vec2 substracted from vec1.
|
||||
function Utils.vecSub(vec1, vec2)
|
||||
return {x = vec1.x - vec2.x, y = vec1.y - vec2.y, z = vec1.z - vec2.z}
|
||||
end
|
||||
|
||||
function Utils.vecTranslate(vec3,degree,distance)
|
||||
local point = {x = vec3.x,y = vec3.z or vec3.y,}
|
||||
|
||||
@@ -13,8 +53,76 @@ do
|
||||
return point
|
||||
end
|
||||
|
||||
function Utils.makeVec3(vec,AGL)
|
||||
local result = Utils.deepCopy(vec)
|
||||
if not vec.z then
|
||||
result.z = vec.y
|
||||
result.y = 0
|
||||
if AGL then
|
||||
result.y = land.getHeight(vec)
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
--- Vector magnitude
|
||||
-- @tparam Vec3 vec vector
|
||||
-- @treturn number magnitude of vector vec
|
||||
function Utils.mag(vec)
|
||||
return (vec.x^2 + vec.y^2 + vec.z^2)^0.5
|
||||
end
|
||||
|
||||
--- Returns distance in meters between two points.
|
||||
-- @tparam Vec2|Vec3 point1 first point
|
||||
-- @tparam Vec2|Vec3 point2 second point
|
||||
-- @treturn number distance between given points.
|
||||
function Utils.get2DDist(point1, point2)
|
||||
if not point1 then
|
||||
log:warn("Utils.get2DDist 1st input value is nil")
|
||||
end
|
||||
if not point2 then
|
||||
log:warn("Utils.get2DDist 2nd input value is nil")
|
||||
end
|
||||
point1 = Utils.makeVec3(point1)
|
||||
point2 = Utils.makeVec3(point2)
|
||||
return Utils.mag({x = point1.x - point2.x, y = 0, z = point1.z - point2.z})
|
||||
end
|
||||
|
||||
function Utils.getNorthCorrection(gPoint) --gets the correction needed for true north
|
||||
local point = Utils.deepCopy(gPoint)
|
||||
if not point.z then --Vec2; convert to Vec3
|
||||
point.z = point.y
|
||||
point.y = 0
|
||||
end
|
||||
local lat, lon = coord.LOtoLL(point)
|
||||
local north_posit = coord.LLtoLO(lat + 1, lon)
|
||||
return math.atan2(north_posit.z - point.z, north_posit.x - point.x)
|
||||
end
|
||||
|
||||
--- Returns heading-error corrected direction.
|
||||
-- True-north corrected direction from point along vector vec.
|
||||
-- @tparam Vec3 vec
|
||||
-- @tparam Vec2 point
|
||||
-- @return heading-error corrected direction from point.
|
||||
function Utils.getDir(vec, point)
|
||||
local dir = math.atan2(vec.z, vec.x)
|
||||
if point then
|
||||
dir = dir + mist.getNorthCorrection(point)
|
||||
end
|
||||
if dir < 0 then
|
||||
dir = dir + 2 * math.pi -- put dir in range of 0 to 2*pi
|
||||
end
|
||||
return dir
|
||||
end
|
||||
|
||||
function Utils.getDirection(vec3_1,vec3_2)
|
||||
local p1 = Utils.makeVec3(vec3_1,true)
|
||||
local p2 = Utils.makeVec3(vec3_2,true)
|
||||
local dir = Utils.getDir(Utils.vecSub(p1, p2))
|
||||
|
||||
return dir
|
||||
end
|
||||
|
||||
--- Converts a TACAN Channel/Mode couple into a frequency in Hz.
|
||||
-- @param #number TACANChannel The TACAN channel, i.e. the 10 in "10X".
|
||||
|
||||
Reference in New Issue
Block a user