--- ABRIS specifications

dofile (LockOn_Options.script_path.."ABRIS\\ABRIS_localizer.lua")
dofile (LockOn_Options.script_path.."ABRIS\\ABRIS_waypoint_type_name.lua")
dofile ("./Config/Map/MapCoords.lua")
dofile(LockOn_Options.common_script_path.."Transliterate.lua")

x_size         = 0.98
y_size         = 1.295

frame_start_y  = 23/1024
frame_end_y    = 754/1024
tex_scale      = (frame_end_y - frame_start_y)/(2*y_size)
render_start    = 55/1024
render_end      = 497/1024

y_render_size   = (render_end - render_start)*0.5/tex_scale

aspect_render_map = y_render_size/x_size


device_timer_dt = 0.1

signals_templates =
{
    LOCALIZE("ПРОЛЕТ ППМ").. " ", --
    LOCALIZE("ПОДХОД К ТОЧКЕ").." ",--  MSG_APPROACH_TO_STP,
    LOCALIZE("НАБОР").." ",--  MSG_APPROACH_TO_CLIMBPOINT,
    LOCALIZE("СНИЖЕНИЕ").." ",--    MSG_APPROACH_TO_DIVEPOINT,
    LOCALIZE("ПОВОРОТ").." ",--  MSG_APPROACH_TO_TURNPOINT,
    LOCALIZE("ЗОНА").." ",--    MSG_BORDER_AVOIDANCE_1,
    LOCALIZE("ЗОНА").." ",--    MSG_BORDER_AVOIDANCE_2,
    LOCALIZE("ЗОНА УВД").." ",-- MSG_BORDER_AIR_TRAFFIC_CONTROL_ZONE,
    LOCALIZE("ВЫХОД ПО ЛБУ").." ",--  MSG_LIMIT_LBU,
    LOCALIZE("ВЫХОД ПО ЗПУ").." ",--  MSG_LIMIT_COURSE,
    LOCALIZE("ПРЕПЯТСТВИЕ ПО КУРСУ").." ",--  MSG_COLLISION_DETECTION_HORIZON_LEVEL,
    LOCALIZE("ПРЕПЯТСТВИЕ ПО ВЫСОТЕ").." ",--    MSG_COLLISION_DETECTION_VERTICAL_LEVEL,
    "3D ",
    "2D ",
    LOCALIZE("НЕТ НАВ").." ",
    "PDOP ",
    LOCALIZE("ПОРОГ"),
}

package.path = package.path..";./LuaSerializer/?.lua"

local S        = require("Serializer")
local lfs      = require("lua-lfs")

save_on_exit         = false
use_saved_database   = true
save_system_points   = false
save_mission_objects = false


common_path          = LockOn_Options.script_path.."ABRIS/"

function   default_path() 
    return common_path
end

function db_default_path(loader)
    if not loader then     return common_path.."Database/"
    else                   return common_path.."Loader/"  end
end

function load_file(file)
    local v = do_mission_file("ABRIS/"..file)
    if    v then return end
    
    dbg_print(string.format("ABRIS load default %s\n",default_path()..file)) 
    local f = loadfile(default_path()..file)
    if f then f() 
    else 
    dbg_print(string.format("ABRIS load default failed %s\n",default_path()..file)) 
    end
end

function load_db_file(loader,file)  
    if not loader then  load_file("Database/"..file)
    else                load_file("Loader/"  ..file)
    end
end




function save_file(name,table_to_save,file_to_save)
    local str = S:serialize_to_string(name,table_to_save)
    dbg_print(str)
    save_to_mission(file_to_save,str)
end

function save_db_file(loader,name,table_to_save,file_to_save)
    local             path = file_to_save
    if    loader then path = "ABRIS/Loader/"  ..path
    else              path = "ABRIS/Database/"..path
    save_file(name,table_to_save,path)
    end
end

navigation = {}
routes     = {}
additional = {}

function LoadNavigation(loader)
    load_db_file(loader,"NAVIGATION.lua")   
	
	if  LockOn_Options.cockpit_language ~= "russian" and navigation.waypoints then
		for i,o in pairs(navigation.waypoints) do
			if o.callsign ~= nil then
			   o.callsign  = TransliterateToLatDefault(o.callsign)
			end
			if o.name ~= nil then
			   o.name  = TransliterateToLatDefault(o.name)
			end
		end
	end
end

function LoadRoutes(loader)        load_db_file(loader,"ROUTES.lua")                                end
function LoadAdditional(loader)    load_db_file(loader,"ADDITIONAL.lua")                            end

function SaveNavigation(loader)    save_db_file(loader,"navigation",navigation,"NAVIGATION.lua")    end
function SaveRoutes(loader)        save_db_file(loader,"routes"    ,routes    ,"ROUTES.lua")        end
function SaveAdditional(loader)    save_db_file(loader,"additional",additional,"ADDITIONAL.lua")    end


Options          = {}
System_Options   = {}
function LoadOptions()
    load_file("Options.lua")
    Options        = opt.Options
    System_Options = opt.System_Options
end

function SaveOptions()
      save_file("opt",
                {Options        = Options,
                 System_Options = System_Options},
                 "Options.lua")
end

LoadOptions()

function LoadAirdromeCallsigns()
    fileName = "./Scripts/World/Airdromes.lua"
    f = loadfile(fileName)
	if  f then  
		f()
		airdrome_callsign = {}
		for ia, a in pairs(Airdromes) do
			airdrome_callsign[ia] = LOCALIZE(a.code)
		end
	end
end


post_messages = 
{
    {message = "starting BIOS......................",next_time = 5},
    {message = "CPU   :486DX4.........OK",next_time = 0.5},
    {message = "memory:2097152KB......OK",next_time = 0.5},
    {message = "video :EDGA32768KB....OK",next_time = 0.5},
    {message = "mounted flash",next_time = 2},
    {message = "booting EDOS............",next_time = 5},
    {message = "ABRIS started",next_time = 5},
    {message = "ABRIS performing full test",next_time = 60},
    {message = "ABRIS ready , loading ui..",next_time = 2},
}
local last_post = 0;
local abris     = GetSelf()

function update_post(t,t_last)

    if t_last <  0 then
       last_post = 0 
    end
    last_post = last_post + 1
    if last_post > #post_messages then 
    return t
    end
    local msg = post_messages[last_post]
    abris:print_to_console(msg.message)
    return t + msg.next_time
end

COMPUTER_FAILURE = 0
SNS_ANTENNA_FAILURE = 1

Damage = {  {Failure = COMPUTER_FAILURE, Element = 3, Slope_param = 4.0}, 
            {Failure = COMPUTER_FAILURE, Element = 4, Slope_param = 1.0}, 
            {Failure = COMPUTER_FAILURE, Element = 5, Slope_param = 2.0}, 
            {Failure = SNS_ANTENNA_FAILURE, Element = 58}}

start_mode = {master = 0,level_2 = 0,level_3 = 0,level_4 = 0}-- menu

if LockOn_Options.flight.easy_radar then
start_mode = {master = 5,level_2 = 0,level_3 = 0,level_4 = 0}-- show map
end


dofile("./Scripts/units.lua")
function get_SAM_threat_params(wstype)
-- wstype - unsigned long representation of wsType structure ( one note ,by default wsType have big-endian  presentation , but mr . Dmut uses little endian for id 
-- that is the cause of  that wstype is LITTLE ENDIAN
    local  unit_specification = unit_by_ws[wstype]
    if     unit_specification == nil         then  error(string.format("bad unit descriptor %x",wstype))return 0,0,0 end      
    if     unit_specification.sensor  == nil then                               return 0,0,0 end      
    return unit_specification.sensor.min_alt_finding_target,
           unit_specification.sensor.max_alt_finding_target,
           unit_specification.sensor.max_range_finding_target
end

function Load_Beacons_Storage()
    local fileName = "Scripts/World/Radio/Beacons.lua"
    local f = loadfile(fileName)
    if    f     then
          f()
    end
end
