diff --git a/addon-main.nas b/addon-main.nas index 7cae3da..d0f1c85 100644 --- a/addon-main.nas +++ b/addon-main.nas @@ -55,6 +55,7 @@ var loadExtraNasalFiles = func (addon) { "nasal/dialogs/thermal", "nasal/flight-plan", "nasal/scenario", + "nasal/io/flight-plan-writer", "aerotow", ]; diff --git a/nasal/flight-plan.nas b/nasal/flight-plan.nas index 79ab508..15f5117 100644 --- a/nasal/flight-plan.nas +++ b/nasal/flight-plan.nas @@ -28,20 +28,18 @@ var FlightPlan = { new: func (addon, message, routeDialog) { var obj = { parents: [FlightPlan] }; - obj.addon = addon; - obj.message = message; - obj.routeDialog = routeDialog; + obj.addon = addon; + obj.message = message; + obj.routeDialog = routeDialog; + obj.flightPlanWriter = FlightPlanWriter.new(addon); obj.addonNodePath = addon.node.getPath(); obj.wptCount = 0; - obj.fpFileHandler = nil; # Handler for wrire flight plan to file obj.coord = nil; # Coordinates for flight plan obj.heading = nil; # AI plane heading obj.altitude = nil; # AI plane altitude - obj.flightPlanPath = addon.storagePath ~ "/AI/FlightPlans/" ~ FlightPlan.FILENAME_FLIGHTPLAN; - return obj; }, @@ -169,14 +167,7 @@ var FlightPlan = { return 0; } - me.fpFileHandler = io.open(me.flightPlanPath, "w"); - io.write( - me.fpFileHandler, - "\n\n" ~ - "\n\n" ~ - "\n" ~ - " \n" - ); + me.flightPlanWriter.open(); var aircraft = Aircraft.getSelected(me.addon); @@ -234,12 +225,7 @@ var FlightPlan = { me.addWptEnd(); - io.write( - me.fpFileHandler, - " \n" ~ - "\n\n" - ); - io.close(me.fpFileHandler); + me.flightPlanWriter.close(); return 1; }, @@ -373,51 +359,8 @@ var FlightPlan = { var ktas = contains(performance, "ktas") ? performance.ktas : nil; name = name == nil ? me.wptCount : name; - var data = me.getWptString(name, coord, alt, ktas, groundAir, sec); - - io.write(me.fpFileHandler, data); + me.flightPlanWriter.write(name, coord, alt, ktas, groundAir, sec); me.wptCount = me.wptCount + 1; }, - - # - # Get single waypoint data as a string. - # - # name - Name of waypoint. Special names are: "WAIT", "END". - # coord - The Coord object - # alt - Altitude AMSL of AI plane - # ktas - True air speed of AI plane - # groundAir - Allowe value: "ground or "air". The "ground" means that AI plane is on the ground, "air" - in air - # sec - Number of seconds for "WAIT" waypoint - # - getWptString: func (name, coord = nil, alt = nil, ktas = nil, groundAir = nil, sec = nil) { - var str = " \n" - ~ " " ~ name ~ "\n"; - - if (coord != nil) { - str = str ~ " " ~ coord.lat() ~ "\n"; - str = str ~ " " ~ coord.lon() ~ "\n"; - str = str ~ " \n"; - } - - if (alt != nil) { - # str = str ~ " " ~ alt ~ "\n"; - str = str ~ " " ~ alt ~ "\n"; - } - - if (ktas != nil) { - str = str ~ " " ~ ktas ~ "\n"; - } - - if (groundAir != nil) { - var onGround = groundAir == "ground" ? "true" : "false"; - str = str ~ " " ~ onGround ~ "\n"; - } - - if (sec != nil) { - str = str ~ " " ~ sec ~ "\n"; - } - - return str ~ " \n"; - }, }; diff --git a/nasal/io/flight-plan-writer.nas b/nasal/io/flight-plan-writer.nas new file mode 100644 index 0000000..9a6b5b2 --- /dev/null +++ b/nasal/io/flight-plan-writer.nas @@ -0,0 +1,111 @@ +# +# Aerotow Everywhere - Add-on for FlightGear +# +# Written and developer by Roman Ludwicki (PlayeRom, SP-ROM) +# +# Copyright (C) 2022 Roman Ludwicki +# +# Aerotow Everywhere is an Open Source project and it is licensed +# under the GNU Public License v3 (GPLv3) +# + +# +# Object for write flight plan to the XML file +# +var FlightPlanWriter = { + # + # Constructor + # + # addon - Addon object + # + new: func (addon) { + var obj = { parents: [FlightPlanWriter] }; + + obj.fpFileHandler = nil; # Handler for wrire flight plan to file + obj.flightPlanPath = addon.storagePath ~ "/AI/FlightPlans/" ~ FlightPlan.FILENAME_FLIGHTPLAN; + + return obj; + }, + + # + # Open XML file to wrire flight plan + # + open: func () { + me.fpFileHandler = io.open(me.flightPlanPath, "w"); + + if (me.fpFileHandler) { + io.write( + me.fpFileHandler, + "\n\n" ~ + "\n\n" ~ + "\n" ~ + " \n" + ); + } + }, + + # + # Write single waypoint to XML file with flight plan + # + # name - Name of waypoint. Special names are: "WAIT", "END". + # coord - The Coord object + # alt - Altitude AMSL of AI plane + # ktas - True air speed of AI plane + # groundAir - Allowe value: "ground or "air". The "ground" means that AI plane is on the ground, "air" - in air + # sec - Number of seconds for "WAIT" waypoint + # + write: func (name, coord = nil, alt = nil, ktas = nil, groundAir = nil, sec = nil) { + if (!me.fpFileHandler) { + return; + } + + var str = " \n" + ~ " " ~ name ~ "\n"; + + if (coord != nil) { + str = str ~ " " ~ coord.lat() ~ "\n"; + str = str ~ " " ~ coord.lon() ~ "\n"; + str = str ~ " \n"; + } + + if (alt != nil) { + # str = str ~ " " ~ alt ~ "\n"; + str = str ~ " " ~ alt ~ "\n"; + } + + if (ktas != nil) { + str = str ~ " " ~ ktas ~ "\n"; + } + + if (groundAir != nil) { + var onGround = groundAir == "ground" ? "true" : "false"; + str = str ~ " " ~ onGround ~ "\n"; + } + + if (sec != nil) { + str = str ~ " " ~ sec ~ "\n"; + } + + str = str ~ " \n"; + + io.write(me.fpFileHandler, str); + }, + + # + # Close XML file with flight plan + # + close: func () { + if (me.fpFileHandler) { + io.write( + me.fpFileHandler, + " \n" ~ + "\n\n" + ); + + io.close(me.fpFileHandler); + me.fpFileHandler = nil; + } + }, +};