Skip to content

Commit

Permalink
ui: human: move location formatting to ui directory
Browse files Browse the repository at this point in the history
Simply move some functions from libs/utils.lua to ui/human.lua.
  • Loading branch information
jtoppins committed Aug 27, 2023
1 parent f3d3bdc commit 7afd756
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 161 deletions.
135 changes: 0 additions & 135 deletions src/dct/libs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,141 +232,6 @@ function utils.centroid2D(point, pcentroid, n)
return vector.Vector2D(c), n1
end

utils.posfmt = {
["DD"] = 1,
["DDM"] = 2,
["DMS"] = 3,
["MGRS"] = 4,
}

-- reduce the accuracy of the position to the precision specified
function utils.degradeLL(lat, long, precision)
local multiplier = math.pow(10, precision)
lat = math.modf(lat * multiplier) / multiplier
long = math.modf(long * multiplier) / multiplier
return lat, long
end

-- set up formatting args for the LL string
local function getLLformatstr(precision, fmt)
local decimals = precision
if fmt == utils.posfmt.DDM then
if precision > 1 then
decimals = precision - 1
else
decimals = 0
end
elseif fmt == utils.posfmt.DMS then
if precision > 4 then
decimals = precision - 2
elseif precision > 2 then
decimals = precision - 3
else
decimals = 0
end
end
if decimals == 0 then
return "%02.0f"
else
return "%0"..(decimals+3).."."..decimals.."f"
end
end

function utils.LLtostring(lat, long, precision, fmt)
local northing = "N"
local easting = "E"
local degsym = '°'

if lat < 0 then
northing = "S"
end

if long < 0 then
easting = "W"
end

lat, long = utils.degradeLL(lat, long, precision)
lat = math.abs(lat)
long = math.abs(long)

local fmtstr = getLLformatstr(precision, fmt)

if fmt == utils.posfmt.DD then
return string.format(fmtstr..degsym, lat)..northing..
" "..
string.format(fmtstr..degsym, long)..easting
end

-- we give the minutes and seconds a little push in case the division
-- from the truncation with this multiplication gives us a value ending
-- in .99999...
local tolerance = 1e-8

local latdeg = math.floor(lat)
local latmind = (lat - latdeg)*60 + tolerance
local longdeg = math.floor(long)
local longmind = (long - longdeg)*60 + tolerance

if fmt == utils.posfmt.DDM then
return string.format("%02d"..degsym..fmtstr.."'", latdeg, latmind)..
northing..
" "..
string.format("%03d"..degsym..fmtstr.."'", longdeg, longmind)..
easting
end

local latmin = math.floor(latmind)
local latsecd = (latmind - latmin)*60 + tolerance
local longmin = math.floor(longmind)
local longsecd = (longmind - longmin)*60 + tolerance

return string.format("%02d"..degsym.."%02d'"..fmtstr.."\"",
latdeg, latmin, latsecd)..
northing..
" "..
string.format("%03d"..degsym.."%02d'"..fmtstr.."\"",
longdeg, longmin, longsecd)..
easting
end

function utils.MGRStostring(mgrs, precision)
local str = mgrs.UTMZone .. " " .. mgrs.MGRSDigraph

if precision == 0 then
return str
end

local divisor = 10^(5-precision)
local fmtstr = "%0"..precision.."d"

if precision == 0 then
return str
end

return str.." "..string.format(fmtstr, (mgrs.Easting/divisor))..
" "..string.format(fmtstr, (mgrs.Northing/divisor))
end

function utils.degrade_position(position, precision)
local lat, long = coord.LOtoLL(position)
lat, long = utils.degradeLL(lat, long, precision)
return coord.LLtoLO(lat, long, 0)
end

function utils.fmtposition(position, precision, fmt)
precision = math.floor(precision)
assert(precision >= 0 and precision <= 5,
"value error: precision range [0,5]")
local lat, long = coord.LOtoLL(position)

if fmt == utils.posfmt.MGRS then
return utils.MGRStostring(coord.LLtoMGRS(lat, long),
precision)
end

return utils.LLtostring(lat, long, precision, fmt)
end

function utils.trimTypeName(typename)
if typename ~= nil then
return string.match(typename, "[^.]-$")
Expand Down
27 changes: 14 additions & 13 deletions src/dct/settings/theater.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
local utils = require("libs.utils")
local enum = require("dct.enum")
local dctutils = require("dct.libs.utils")
local uihuman = require("dct.ui.human")

local function validate_weapon_restrictions(cfgdata, tbl)
local path = cfgdata.file
Expand Down Expand Up @@ -86,7 +87,7 @@ local function gridfmt_transform(tbl)
if type(v) == "number" then
ntbl[k] = v
else
ntbl[k] = dctutils.posfmt[string.upper(v)]
ntbl[k] = uihuman.posfmt[string.upper(v)]
assert(ntbl[k] ~= nil, "invalid grid format for "..k)
end
end
Expand Down Expand Up @@ -190,18 +191,18 @@ local function theatercfgs(config)
["default"] = {
["gridfmt"] = {
-- default is DMS, no need to list
["Ka-50"] = dctutils.posfmt.DDM,
["Mi-8MT"] = dctutils.posfmt.DDM,
["SA342M"] = dctutils.posfmt.DDM,
["SA342L"] = dctutils.posfmt.DDM,
["UH-1H"] = dctutils.posfmt.DDM,
["A-10A"] = dctutils.posfmt.MGRS,
["A-10C"] = dctutils.posfmt.MGRS,
["A-10C_2"] = dctutils.posfmt.MGRS,
["F-5E-3"] = dctutils.posfmt.DDM,
["F-16C_50"] = dctutils.posfmt.DDM,
["FA-18C_hornet"] = dctutils.posfmt.DDM,
["M-2000C"] = dctutils.posfmt.DDM,
["Ka-50"] = uihuman.posfmt.DDM,
["Mi-8MT"] = uihuman.posfmt.DDM,
["SA342M"] = uihuman.posfmt.DDM,
["SA342L"] = uihuman.posfmt.DDM,
["UH-1H"] = uihuman.posfmt.DDM,
["A-10A"] = uihuman.posfmt.MGRS,
["A-10C"] = uihuman.posfmt.MGRS,
["A-10C_2"] = uihuman.posfmt.MGRS,
["F-5E-3"] = uihuman.posfmt.DDM,
["F-16C_50"] = uihuman.posfmt.DDM,
["FA-18C_hornet"] = uihuman.posfmt.DDM,
["M-2000C"] = uihuman.posfmt.DDM,
},
["ato"] = {},
},
Expand Down
Loading

0 comments on commit 7afd756

Please sign in to comment.