--[[
Draken35 Library... V1.01
Contains reusable code from different authors... Credits in the functions (orginal code might have been modified to work in DCS or for my purpuses)


]]--
trigger.action.outText( 'd35lib - V1.01', 2 )
d35lib = {}
--############################################################################################################################################
function d35lib.ConvertNumToWords(n)
  
	-- Convert a number to words
	-- Author: Nick Gammon
	-- Date: 18th March 2010

	-- Example of use:
	-- words  = ConvertNumToWords('94921277802687490518')

	-- Return nil and an error message so you can check for failure,
	-- or assert, eg. words = assert(ConvertNumToWords('2687490518'))
	
	-- See: http://www.gammon.com.au/forum/?id=10155
	
	inverse_units = {
		"vigintillion ",     -- 10^63
		"novemdecillion ",   -- 10^60
		"octodecillion ",    -- 10^57
		"septendecillion ",  -- 10^54
		"sexdecillion ",     -- 10^51
		"quindecillion ",    -- 10^48
		"quattuordecillion ",-- 10^45
		"tredecillion ",     -- 10^42
		"duodecillion ",    -- 10^39
		"undecillion ",     -- 10^36
		"decillion ",       -- 10^33
		"nonillion ",       -- 10^30
		"octillion ",       -- 10^27
		"septillion ",      -- 10^24
		"sextillion ",      -- 10^21
		"quintillion ",     -- 10^18
		"quadrillion ",     -- 10^15
		"trillion ",        -- 10^12
		"billion ",         -- 10^9
		"million ",         -- 10^6
		"thousand ",        -- 10^3
	} -- inverse_units
  
	inverse_numbers = {
		"one ",
		"two ",
		"three ",
		"four ",
		"five ",
		"six ",
		"seven ",
		"eight ",
		"nine ",
		"ten ",
		"eleven ",
		"twelve ",
		"thirteen ",
		"fourteen ",
		"fifteen ",
		"sixteen ",
		"seventeen ",
		"eighteen ",
		"nineteen ",
		"twenty ",
		[30] = "thirty ",
		[40] = "forty ",
		[50] = "fifty ",
		[60] = "sixty ",
		[70] = "seventy ",
		[80] = "eighty ",
		[90] = "ninety ",
	}  -- inverse_numbers
 
	

	s = tostring(n)
  
	-- preliminary sanity checks
	c = string.match (s, "%D")
	if c then
		return nil, "Non-numeric digit '" .. c .. "' in number"
	end -- if

	if #s == 0 then
		return nil, "No number supplied"
	elseif #s > 66 then
		return nil, "Number too big to convert to words"
	end -- if
  
	-- make multiple of 3
	while #s % 3 > 0 do
		s = "0" .. s
	end -- while
      
	result = ""
	start = #inverse_units - (#s / 3) + 2
  
	for i = start, #inverse_units do
		group = tonumber(string.sub (s, 1, 3))
	if group > 0 then
		result = result .. d35lib.ConvertTo999 (group) .. inverse_units [i]
	end -- if not zero
		s = string.sub(s, 4)    
	end -- for
  
	result = result .. d35lib.ConvertTo999(tonumber (s)) 

	if result == "" then
		result = "zero"
	end -- if
  
	return (string.gsub(result, " +$", ""))  -- trim trailing spaces

end -- ConvertNumToWords
--############################################################################################################################################
function d35lib.ConvertTo999(n)
  
	-- Convert a number to words
	-- Author: Nick Gammon
	-- Date: 18th March 2010

	-- Example of use:
	-- words  = ConvertNumToWords('94921277802687490518')

	-- Return nil and an error message so you can check for failure,
	-- or assert, eg. words = assert(ConvertNumToWords('2687490518'))
	
	-- See: http://www.gammon.com.au/forum/?id=10155
	
	if n <= 0 then
		return ""
	end -- if zero
  
	hundreds = math.floor(n / 100)
	tens = math.floor(n % 100)
	result = ""

	-- if over 99 we need to say x hundred  
	if hundreds > 0 then
  
    result = inverse_numbers[hundreds] .. "hundred "
    if tens == 0 then
      return result
    end -- if only a digit in the hundreds column
  
	-- to have "and" between things like "hundred and ten"
	-- uncomment the next line
	--result = result .. "and "

	end -- if
  
	-- up to twenty it is then just five hundred (and) fifteen
	if tens <= 20 then
		return result .. inverse_numbers[tens] 
	end -- if

	-- otherwise we need: thirty (something)
	result = result .. inverse_numbers[math.floor(tens / 10) * 10] 
  
	-- get final digit (eg. thirty four)
	local digits = math.floor(n % 10)

	-- to put a hyphen between things like "forty-two" 
	-- uncomment the WITH HYPHEN line and 
	-- comment out the NO HYPHEN line

	if digits > 0 then
		 result = result ..  inverse_numbers [digits]  -- NO HYPHEN
		--result = string.sub(result, 1, -2) .. "-" ..  inverse_numbers[digits]  -- WITH HYPHEN
	end -- if 

	return result
  
end -- ConvertTo999
--############################################################################################################################################
function d35lib.getDistance(_point1, _point2)

    local xUnit = _point1.x
    local yUnit = _point1.z
    local xZone = _point2.x
    local yZone = _point2.z

    local xDiff = xUnit - xZone
    local yDiff = yUnit - yZone

    return math.sqrt(xDiff * xDiff + yDiff * yDiff)
end
--############################################################################################################################################
function d35lib.getRelativeDirection(_refHeading,_point1, _point2)
    local tgtBearing
    local xUnit = _point1.x
    local yUnit = _point1.z
    local xZone = _point2.x
    local yZone = _point2.z
    
    local xDiff = xUnit - xZone
    local yDiff = yUnit - yZone
    
    local tgtAngle = math.deg(math.atan(yDiff/xDiff))
    
    if xDiff > 0 then 
    tgtBearing = 180 + tgtAngle 
    end
    
    if xDiff < 0 and tgtAngle > 0 then
		tgtBearing = tgtAngle 
    end
    
    if xDiff < 0 and tgtAngle < 0 then
		tgtBearing = 360 + tgtAngle
    end   
	
	tgtBearing = tgtBearing - _refHeading
	if tgtBearing > 360 then
		tgtBearing = tgtBearing - 360
	end
	if tgtBearing < 0 then
		tgtBearing =  360 + tgtBearing 
	end
	
    return tgtBearing
end
--############################################################################################################################################
function d35lib.getClockDirection(_direction)
	-- by cfrag
	if not _direction then return 0 end
	while _direction < 0 do 
		_direction = _direction + 360
	end
		
	if _direction < 15 then -- special case 12 o'clock past 12 o'clock
		return 12
	end
	
	_direction = _direction + 15 -- add offset so we get all other times correct
	return math.floor(_direction/30)
	
end
--############################################################################################################################################
function d35lib.getDirection(_point1, _point2)
    local tgtBearing
    local xUnit = _point1.x
    local yUnit = _point1.z
    local xZone = _point2.x
    local yZone = _point2.z
    
    local xDiff = xUnit - xZone
    local yDiff = yUnit - yZone
    
    local tgtAngle = math.deg(math.atan(yDiff/xDiff))
    
    if xDiff > 0 then 
    tgtBearing = 180 + tgtAngle 
    end
    
    if xDiff < 0 and tgtAngle > 0 then
    tgtBearing = tgtAngle 
    end
    
    if xDiff < 0 and tgtAngle < 0 then
    tgtBearing = 360 + tgtAngle
    end
    
    
 env.info("xDiff "..xDiff.." yDiff "..yDiff)    
    return tgtBearing
end
--############################################################################################################################################
function d35lib.stringSplit(inputstr, sep)
	if sep == nil then
			sep = "%s"
	end
	local t={}
	for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
			table.insert(t, str)
	end
	return t
end
--############################################################################################################################################
function d35lib.getMagVar()
  local map = env.mission.theatre
  local MagVar=0
  
  if map=="Caucasus" then
    MagVar=6
  elseif map=="Nevada" then
    MagVar=12
  elseif map=="PersianGulf" then
    MagVar=2	
  elseif map=="Syria" then
    MagVar=5
  elseif map=="Normandy" then
    MagVar=-10
  elseif map=="TheChannel" then
    MagVar=-10
  elseif map=="MarianaIslands" then
    MagVar=2
  end

  return MagVar
end
--############################################################################################################################################