2019-12-29 21:37:52 +00:00
# A3XX FMGC Flightplan Driver
2020-04-23 17:21:59 +00:00
# Copyright (c) 2020 Josh Davidson (Octal450) and Jonathan Redpath (legoboyvdlp)
2019-12-29 21:37:52 +00:00
var wpDep = nil;
var wpArr = nil;
var pos = nil;
var geoPosPrev = geo.Coord.new();
var currentLegCourseDist = nil;
var courseDistanceFrom = nil;
var courseDistanceFromPrev = nil;
var sizeWP = nil;
var magTrueError = 0;
2020-04-19 09:24:06 +00:00
var DEBUG_DISCONT = 1;
2020-01-05 20:21:28 +00:00
# Props.getNode
2019-12-29 21:37:52 +00:00
var magHDG = props.globals.getNode("/orientation/heading-magnetic-deg", 1);
var trueHDG = props.globals.getNode("/orientation/heading-deg", 1);
var FMGCdep = props.globals.getNode("/FMGC/internal/dep-arpt", 1);
var FMGCarr = props.globals.getNode("/FMGC/internal/arr-arpt", 1);
2020-01-05 20:21:28 +00:00
var toFromSet = props.globals.getNode("/FMGC/internal/tofrom-set", 1);
2019-12-29 21:37:52 +00:00
2020-01-05 20:21:28 +00:00
# Props.initNode
2019-12-29 21:37:52 +00:00
var wpID = [[props.globals.initNode("/FMGC/flightplan[0]/wp[0]/id", "", "STRING")], [props.globals.initNode("/FMGC/flightplan[1]/wp[0]/id", "", "STRING")], [props.globals.initNode("/FMGC/flightplan[2]/wp[0]/id", "", "STRING")]];
var wpLat = [[props.globals.initNode("/FMGC/flightplan[0]/wp[0]/lat", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[1]/wp[0]/lat", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[2]/wp[0]/lat", 0, "DOUBLE")]];
var wpLon = [[props.globals.initNode("/FMGC/flightplan[0]/wp[0]/lon", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[1]/wp[0]/lon", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[2]/wp[0]/lon", 0, "DOUBLE")]];
var wpCourse = [[props.globals.initNode("/FMGC/flightplan[0]/wp[0]/course", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[1]/wp[0]/course", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[2]/wp[0]/course", 0, "DOUBLE")]];
var wpDistance = [[props.globals.initNode("/FMGC/flightplan[0]/wp[0]/distance", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[1]/wp[0]/distance", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[2]/wp[0]/distance", 0, "DOUBLE")]];
var wpCoursePrev = [[props.globals.initNode("/FMGC/flightplan[0]/wp[0]/course-from-prev", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[1]/wp[0]/course-from-prev", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[2]/wp[0]/course-from-prev", 0, "DOUBLE")]];
var wpDistancePrev = [[props.globals.initNode("/FMGC/flightplan[0]/wp[0]/distance-from-prev", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[1]/wp[0]/distance-from-prev", 0, "DOUBLE")], [props.globals.initNode("/FMGC/flightplan[2]/wp[0]/distance-from-prev", 0, "DOUBLE")]];
2020-01-05 20:21:28 +00:00
var flightPlanController = {
2020-04-28 23:41:08 +00:00
flightplans: [createFlightplan(), createFlightplan(), createFlightplan(), nil],
2020-01-05 20:21:28 +00:00
temporaryFlag: [0, 0],
# These flags are only for the main flgiht-plan
active: props.globals.initNode("/FMGC/flightplan[2]/active", 0, "BOOL"),
currentToWpt: nil, # container for the current TO waypoint ghost
currentToWptIndex: props.globals.initNode("/FMGC/flightplan[2]/current-wp", 0, "INT"),
2020-04-23 17:21:59 +00:00
currentToWptIndexTemp: 0,
2020-02-02 20:34:27 +00:00
currentToWptID: props.globals.initNode("/FMGC/flightplan[2]/current-leg", "", "STRING"),
2020-01-23 14:14:48 +00:00
courseToWpt: props.globals.initNode("/FMGC/flightplan[2]/current-leg-course", 0, "DOUBLE"),
courseMagToWpt: props.globals.initNode("/FMGC/flightplan[2]/current-leg-course-mag", 0, "DOUBLE"),
2020-04-23 17:21:59 +00:00
distToWpt: props.globals.initNode("/FMGC/flightplan[2]/current-leg-dist", 0, "DOUBLE"),
2020-04-27 13:58:38 +00:00
wptType: nil,
2020-04-23 17:21:59 +00:00
wptTypeNoAdvanceDelete: 0,
2020-01-05 20:21:28 +00:00
distanceToDest: [0, 0, 0],
2020-02-02 20:34:27 +00:00
num: [props.globals.initNode("/FMGC/flightplan[0]/num", 0, "INT"), props.globals.initNode("/FMGC/flightplan[1]/num", 0, "INT"), props.globals.initNode("/FMGC/flightplan[2]/num", 0, "INT")],
2020-01-05 20:21:28 +00:00
arrivalIndex: [0, 0, 0],
arrivalDist: 0,
_arrivalDist: 0,
2020-03-24 16:46:27 +00:00
fromWptTime: nil,
fromWptAlt: nil,
_timeTemp: nil,
_altTemp: nil,
2020-01-05 20:21:28 +00:00
2019-12-29 21:37:52 +00:00
reset: func() {
2020-01-05 20:21:28 +00:00
me.temporaryFlag[0] = 0;
me.temporaryFlag[1] = 0;
me.resetFlightplan(0);
me.resetFlightplan(1);
me.resetFlightplan(2);
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
resetFlightplan: func(n) {
me.flightplans[n].cleanPlan();
me.flightplans[n].departure = nil;
me.flightplans[n].destination = nil;
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
createTemporaryFlightPlan: func(n) {
2020-01-07 20:48:27 +00:00
me.resetFlightplan(n);
2020-01-05 20:21:28 +00:00
me.flightplans[n] = me.flightplans[2].clone();
me.temporaryFlag[n] = 1;
2020-03-28 15:53:50 +00:00
if (canvas_mcdu.myDirTo[n] != nil) {
canvas_mcdu.myDirTo[n].updateTmpy();
}
2020-05-04 10:48:22 +00:00
if (canvas_mcdu.myHold[n] != nil) {
canvas_mcdu.myHold[n].updateTmpy();
}
if (canvas_mcdu.myAirways[n] != nil) {
canvas_mcdu.myAirways[n].updateTmpy();
}
2020-01-05 20:21:28 +00:00
me.flightPlanChanged(n);
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
2020-04-28 23:41:08 +00:00
loadFlightPlan: func(path) {
call(func {me.flightplans[3] = createFlightplan(path);}, nil, var err = []);
if (size(err) or me.flightplans[3] == nil) {
print(err[0]);
print("Load failed.");
}
2020-05-06 23:06:24 +00:00
# try to fix fgfp
2020-04-28 23:41:08 +00:00
me.flightplans[3].destination = airportinfo(getprop("FMGC/internal/arr-arpt"));
me.destroyTemporaryFlightPlan(3, 1);
},
2020-01-05 20:21:28 +00:00
destroyTemporaryFlightPlan: func(n, a) { # a = 1 activate, a = 0 erase
if (a == 1) {
2020-01-07 20:48:27 +00:00
flightPlanTimer.stop();
me.resetFlightplan(2);
2020-01-05 20:21:28 +00:00
me.flightplans[2] = me.flightplans[n].clone();
me.flightPlanChanged(2);
2020-01-07 20:48:27 +00:00
flightPlanTimer.start();
2019-12-29 21:37:52 +00:00
}
2020-04-28 23:41:08 +00:00
if (n == 3) { return; }
2020-01-05 20:21:28 +00:00
me.resetFlightplan(n);
me.temporaryFlag[n] = 0;
2020-03-28 15:53:50 +00:00
if (canvas_mcdu.myDirTo[n] != nil) {
canvas_mcdu.myDirTo[n].updateTmpy();
}
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
updateAirports: func(dep, arr, plan) {
me.resetFlightplan(plan);
me.flightplans[plan].departure = airportinfo(dep);
me.flightplans[plan].destination = airportinfo(arr);
if (plan == 2) {
2020-04-28 14:16:08 +00:00
me.destroyTemporaryFlightPlan(0, 0);
me.destroyTemporaryFlightPlan(1, 0);
2020-01-05 20:21:28 +00:00
me.currentToWptIndex.setValue(0);
2019-12-29 21:37:52 +00:00
}
2020-01-05 20:21:28 +00:00
2020-04-19 09:29:34 +00:00
me.addDiscontinuity(1, plan);
2020-04-28 11:35:23 +00:00
# reset mcdu if it exists
2020-05-14 19:02:18 +00:00
if (canvas_mcdu.myFpln[0] != nil) { canvas_mcdu.myFpln[0].scroll = 0; }
if (canvas_mcdu.myFpln[1] != nil) { canvas_mcdu.myFpln[1].scroll = 0; }
2020-04-28 11:35:23 +00:00
if (canvas_mcdu.myArrival[0] != nil) { canvas_mcdu.myArrival[0].reset(); }
if (canvas_mcdu.myArrival[1] != nil) { canvas_mcdu.myArrival[1].reset(); }
if (canvas_mcdu.myDeparture[0] != nil) { canvas_mcdu.myDeparture[0].reset(); }
if (canvas_mcdu.myDeparture[1] != nil) { canvas_mcdu.myDeparture[1].reset(); }
2020-01-16 17:08:14 +00:00
#todo if plan = 2, kill any tmpy flightplan
2020-01-05 20:21:28 +00:00
me.flightPlanChanged(plan);
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
autoSequencing: func() {
2020-03-24 16:46:27 +00:00
me._timeTemp = math.round(getprop("/sim/time/utc/minute") + (getprop("/sim/time/utc/second") / 60));
if (me._timeTemp < 10) {
me._timeTemp = "0" ~ me._timeTemp;
}
me.fromWptTime = getprop("/sim/time/utc/hour") ~ me._timeTemp;
me._altTemp = getprop("/systems/navigation/adr/output/baro-alt-corrected-1-capt");
if (me._altTemp > getprop("FMGC/internal/trans-alt")) {
me.fromWptAlt = "FL" ~ math.round(me._altTemp / 100);
} else {
if (me._altTemp > 0) {
me.fromWptAlt = math.round(me._altTemp);
} else {
me.fromWptAlt = "M" ~ math.round(me._altTemp);
}
}
# todo setlistener on sim/time/warp to recompute predictions
2020-04-23 17:21:59 +00:00
# Advancing logic
me.currentToWptIndexTemp = me.currentToWptIndex.getValue();
if (me.currentToWptIndexTemp < 1) {
me.currentToWptIndex.setValue(1);
} else if (me.num[2].getValue() > 2) {
if (me.currentToWptIndexTemp == 2) { # Clean up after a no-sequence waypoint
me.currentToWptIndex.setValue(1); # MUST be set first
# TODO: Add support for deleting multiple waypoints at once, this will do for now
if (me.temporaryFlag[0] == 1 and wpID[0][0] == wpID[2][0]) {
me.deleteWP(0, 0);
}
if (me.temporaryFlag[1] == 1 and wpID[1][0] == wpID[2][0]) {
me.deleteWP(0, 1);
}
me.deleteWP(0, 2, 0, 1);
if (me.temporaryFlag[0] == 1 and wpID[0][0] == wpID[2][0]) {
me.deleteWP(0, 0);
}
if (me.temporaryFlag[1] == 1 and wpID[1][0] == wpID[2][0]) {
me.deleteWP(0, 1);
}
me.deleteWP(0, 2, 0, 1);
} else {
2020-04-27 13:58:38 +00:00
me.wptType = me.flightplans[2].getWP(me.currentToWptIndexTemp).wp_type;
me.wptTypeNoAdvanceDelete = me.wptType == "radialIntercept" or me.wptType == "vectors" or me.wptType == "hdgToAlt";
2020-04-23 17:21:59 +00:00
if (me.wptTypeNoAdvanceDelete) {
me.currentToWptIndex.setValue(2);
} else {
if (me.temporaryFlag[0] == 1 and wpID[0][0] == wpID[2][0]) {
me.deleteWP(0, 0);
}
if (me.temporaryFlag[1] == 1 and wpID[1][0] == wpID[2][0]) {
me.deleteWP(0, 1);
}
me.deleteWP(0, 2, 0, 1);
}
2019-12-29 21:37:52 +00:00
}
}
},
2020-01-05 20:21:28 +00:00
2020-04-26 13:52:07 +00:00
# for these two remember to call flightPlanChanged. We are assuming this is called from a function which will all flightPlanChanged itself.
2020-01-05 20:21:28 +00:00
addDiscontinuity: func(index, plan) {
2020-04-19 09:24:06 +00:00
if (DEBUG_DISCONT) { return; }
2020-03-29 18:54:26 +00:00
if (index > 0) {
if (me.flightplans[plan].getWP(index).wp_name != "DISCONTINUITY" and me.flightplans[plan].getWP(index - 1).wp_name != "DISCONTINUITY") {
me.flightplans[plan].insertWP(createDiscontinuity(), index);
}
} else {
if (me.flightplans[plan].getWP(index).wp_name != "DISCONTINUITY") {
me.flightplans[plan].insertWP(createDiscontinuity(), index);
}
}
2020-01-05 20:21:28 +00:00
},
2020-04-26 13:52:07 +00:00
# insertTP - insert PPOS waypoint denoted "T-P" at specified index
# args: n, index
# n: flightplan to which the PPOS waypoint will be inserted
# index: optional argument, defaults to 1, index which the waypoint will be at.
# Default to one, as direct to will insert TP, then create leg to DIRTO waypoint, then delete waypoint[0]
insertTP: func(n, index = 1) {
me.flightplans[n].insertWP(createWP(geo.aircraft_position(), "T-P"), index);
2020-03-31 15:33:28 +00:00
},
2020-04-26 13:52:07 +00:00
# childWPBearingDistance - return waypoint at bearing and distance from specified waypoint ghost
# args: wpt, bearing, dist, name, typeStr
# wpt: waypoint ghost
# bearing: bearing of waypoint to be created from specified waypoint
# distance: distance of waypoint to be created from specified waypoint, nautical miles
# name: name of waypoint to be created
# typeStr: optional argument to be passed to createWP, must be one of "sid", "star" "approach" "missed" or "pseudo"
2020-04-24 09:34:28 +00:00
childWPBearingDistance: func(wpt, bearing, dist, name, typeStr = "") {
2020-05-14 19:02:18 +00:00
var coordinates = greatCircleMove(wpt.lat, wpt.lon, num(bearing), num(dist));
2020-04-24 09:34:28 +00:00
if (typeStr != "") {
return createWP(coordinates, name, typeStr);
2020-05-14 19:02:18 +00:00
} else {
return createWP(coordinates, name);
2020-04-24 09:34:28 +00:00
}
},
2020-04-26 13:52:07 +00:00
# insertNOSID - create default SID and add to flightplan
# args: n: plan on which the SID will be created
# The default SID is a leg from departure runway to a point 2.5 miles on the runway extended centreline
# if NO SID has already been inserted, we will not insert another one.
2020-04-24 11:23:33 +00:00
insertNOSID: func(n) {
var wptStore = me.flightplans[n].getWP(0);
if (wptStore.wp_type == "runway") {
2020-04-24 11:44:39 +00:00
if (me.flightplans[n].getWP(1).id == "1500") { # check if we have NO SID already loaded
2020-04-24 11:23:33 +00:00
me.deleteWP(1, n, 1);
}
2020-04-26 13:52:07 +00:00
2020-04-24 11:23:33 +00:00
# fudge the altitude since we cannot create a hdgtoAlt from nasal. Assume 600 feet per mile - 2.5 miles
2020-04-24 11:44:39 +00:00
me.flightplans[n].insertWP(me.childWPBearingDistance(wptStore, me.flightplans[n].departure_runway.heading, 2.5, "1500", "sid"), 1);
2020-04-24 11:23:33 +00:00
}
me.flightPlanChanged(n);
},
2020-04-26 13:52:07 +00:00
# insertNOSTAR - create default STAR and add to flightplan
# args: n: plan on which the STAR will be created
# The default STAR is a leg from departure runway to a point 5 miles on the runway extended centreline
# if NO STAR has already been inserted, we will not insert another one.
2020-04-24 09:34:28 +00:00
insertNOSTAR: func(n) {
var wptStore = me.flightplans[n].getWP(me.arrivalIndex[n]);
if (wptStore.wp_type == "runway") {
if (me.flightplans[n].getWP(me.arrivalIndex[n] - 1).id == "CF") { # check if we have NO STAR already loaded
me.deleteWP(me.arrivalIndex[n] - 1, n, 1);
}
var hdg = me.flightplans[n].destination_runway.heading + 180;
if (hdg > 360) {
hdg = hdg - 360;
}
me.flightplans[n].insertWP(me.childWPBearingDistance(wptStore, hdg, 5, "CF", "star"), me.arrivalIndex[n]);
}
me.flightPlanChanged(n);
},
2020-04-26 13:52:07 +00:00
# directTo - create leg direct from present position to a specified waypoint
# args: waypointGhost, plan
# waypointGost: waypoint ghost of the waypoint
# plan: plan on which the direct to leg will be created
# We first insert a PPOS waypoint at index 1
# We check if the flightplan already contains the waypoint passed to the function
# If it exists, we delete intermediate waypoints
# If it does not, we insert the waypoint at index 2 and add a discontinuity at index 3
# In either case, we delete the current FROM waypoint, index 0, and call flightPlanChanged to recalculate
# We attempt to get the distance from the aircraft current position to the chosen waypoint and update mcdu with it
2020-03-31 15:33:28 +00:00
directTo: func(waypointGhost, plan) {
if (me.flightplans[plan].indexOfWP(waypointGhost) == -1) {
2020-04-26 13:52:07 +00:00
me.insertTP(plan, 1);
2020-03-31 15:33:28 +00:00
me.flightplans[plan].insertWP(createWPFrom(waypointGhost), 2);
me.addDiscontinuity(3, plan);
} else {
2020-04-26 13:52:07 +00:00
# we want to delete the intermediate waypoints up to but not including the waypoint. Leave index 0, we delete it later.
# example - waypoint dirto is index 5, we want to delete indexes 1 -> 4. 5 - 1 = 4.
# so four individual deletions. Delete index 1 four times.
# Add one extra for the TP, so while > 2
var timesToDelete = me.flightplans[plan].indexOfWP(waypointGhost);
2020-04-07 15:59:43 +00:00
while (timesToDelete > 1) {
me.deleteWP(1, plan, 1);
timesToDelete -= 1;
}
2020-04-26 13:52:07 +00:00
# Add TP afterwards, this is essential
me.insertTP(plan, 1);
2020-03-31 15:33:28 +00:00
}
2020-04-26 13:52:07 +00:00
me.deleteWP(0, plan);
2020-03-31 15:33:28 +00:00
me.flightPlanChanged(plan);
2020-03-31 15:48:36 +00:00
var curAircraftPosDirTo = geo.aircraft_position();
canvas_mcdu.myDirTo[plan].updateDist(me.flightplans[plan].getWP(1).courseAndDistanceFrom(curAircraftPosDirTo)[1]);
2020-03-29 18:54:26 +00:00
},
2020-04-11 17:12:18 +00:00
deleteWP: func(index, n, a = 0, s = 0) { # a = 1, means adding a waypoint via deleting intermediate. s = 1, means autosequencing
2020-01-05 20:21:28 +00:00
var wp = wpID[n][index].getValue();
2020-04-11 17:12:18 +00:00
if (((s == 0 and left(wp, 4) != FMGCdep.getValue() and left(wp, 4) != FMGCarr.getValue()) or (s == 1)) and me.flightplans[n].getPlanSize() > 2) {
2020-01-11 13:30:08 +00:00
if (me.flightplans[n].getWP(index).id != "DISCONTINUITY" and a == 0) { # if it is a discont, don't make a new one
2020-01-07 20:48:27 +00:00
me.flightplans[n].deleteWP(index);
2020-02-03 19:16:05 +00:00
if (me.flightplans[n].getWP(index) != nil and s == 0) {
2020-01-23 14:14:48 +00:00
if (me.flightplans[n].getWP(index).id != "DISCONTINUITY") { # else, if the next one isn't a discont, add one
me.addDiscontinuity(index, n);
}
2020-01-05 20:21:28 +00:00
}
2019-12-29 21:37:52 +00:00
} else {
2020-01-07 20:48:27 +00:00
me.flightplans[n].deleteWP(index);
2019-12-29 21:37:52 +00:00
}
2020-01-16 21:02:35 +00:00
me.flightPlanChanged(n);
2020-01-11 13:30:08 +00:00
canvas_nd.A3XXRouteDriver.triggerSignal("fp-removed");
2020-01-07 20:48:27 +00:00
return 2;
} else {
return 1;
2020-01-05 20:21:28 +00:00
}
},
2020-05-14 19:02:18 +00:00
# createDuplicateNames - helper to spawn DUPLICATENAMES page
# args: ghostContainer, index, flag, plan
# ghostContainer: vector of fgPositioned ghosts
# index: index
# flag: is it a navaids DUPLICATENAMES page or not?
# plan: plan
# flagPBD: do we return back to PBD handler or to default waypoint handler?
createDuplicateNames: func(ghostContainer, index, flag, plan, flagPBD = 0, bearing = -999, distance = -99) {
if (canvas_mcdu.myDuplicate[plan] != nil) {
canvas_mcdu.myDuplicate[plan].del();
}
canvas_mcdu.myDuplicate[plan] = nil;
canvas_mcdu.myDuplicate[plan] = mcdu.duplicateNamesPage.new(ghostContainer, index, flag, plan, flagPBD, bearing, distance);
setprop("MCDU[" ~ plan ~ "]/page", "DUPLICATENAMES");
},
2020-01-05 20:21:28 +00:00
insertAirport: func(text, index, plan, override = 0, overrideIndex = -1) {
if (index == 0) {
return 1;
}
var airport = findAirportsByICAO(text);
if (size(airport) == 0) {
return 0;
}
if (size(airport) == 1 or override) {
if (!override) {
if (me.flightplans[plan].indexOfWP(airport[0]) == -1) {
me.flightplans[plan].insertWP(createWPFrom(airport[0]), index);
2020-03-29 18:54:26 +00:00
me.addDiscontinuity(index + 1, plan);
2020-01-05 20:21:28 +00:00
me.flightPlanChanged(plan);
2020-01-07 20:48:27 +00:00
return 2;
2020-01-05 20:21:28 +00:00
} else {
var numToDel = me.flightplans[plan].indexOfWP(airport[0]) - index;
2020-01-07 20:48:27 +00:00
while (numToDel > 0) {
2020-03-29 18:54:26 +00:00
me.deleteWP(index, plan, 1);
2020-01-07 20:48:27 +00:00
numToDel -= 1;
}
return 2;
2020-01-05 20:21:28 +00:00
}
2019-12-29 21:37:52 +00:00
} else {
2020-01-05 20:21:28 +00:00
if (me.flightplans[plan].indexOfWP(airport[overrideIndex]) == -1) {
me.flightplans[plan].insertWP(createWPFrom(airport[overrideIndex]), index);
2020-03-29 18:54:26 +00:00
me.addDiscontinuity(index + 1, plan);
2020-01-05 20:21:28 +00:00
me.flightPlanChanged(plan);
2020-01-07 20:48:27 +00:00
return 2;
2020-01-05 20:21:28 +00:00
} else {
var numToDel = me.flightplans[plan].indexOfWP(airport[overrideIndex]) - index;
2020-01-07 20:48:27 +00:00
while (numToDel > 0) {
2020-03-29 18:54:26 +00:00
me.deleteWP(index, plan, 1);
2020-01-07 20:48:27 +00:00
numToDel -= 1;
}
return 2;
2020-01-05 20:21:28 +00:00
}
2019-12-29 21:37:52 +00:00
}
2020-01-05 20:21:28 +00:00
} elsif (size(airport) >= 1) {
2020-05-14 19:02:18 +00:00
me.createDuplicateNames(airport, index, 0, plan);
2020-01-11 13:30:08 +00:00
return 2;
2019-12-29 21:37:52 +00:00
}
},
2020-01-05 20:21:28 +00:00
2020-01-11 13:30:08 +00:00
insertFix: func(text, index, plan, override = 0, overrideIndex = -1) { # override - means always choose [0]
2020-01-05 20:21:28 +00:00
if (index == 0) {
2019-12-29 21:37:52 +00:00
return 1;
}
2020-01-05 20:21:28 +00:00
var fix = findFixesByID(text);
if (size(fix) == 0) {
2019-12-29 21:37:52 +00:00
return 0;
2020-01-05 20:21:28 +00:00
}
if (size(fix) == 1 or override) {
2020-01-07 20:48:27 +00:00
if (!override) {
if (me.flightplans[plan].indexOfWP(fix[0]) == -1) {
me.flightplans[plan].insertWP(createWPFrom(fix[0]), index);
2020-03-29 18:54:26 +00:00
me.addDiscontinuity(index + 1, plan);
2020-01-07 20:48:27 +00:00
me.flightPlanChanged(plan);
return 2;
} else {
var numToDel = me.flightplans[plan].indexOfWP(fix[0]) - index;
while (numToDel > 0) {
2020-03-29 18:54:26 +00:00
me.deleteWP(index, plan, 1);
2020-01-07 20:48:27 +00:00
numToDel -= 1;
}
return 2;
}
2020-01-05 20:21:28 +00:00
} else {
2020-01-07 20:48:27 +00:00
if (me.flightplans[plan].indexOfWP(fix[overrideIndex]) == -1) {
me.flightplans[plan].insertWP(createWPFrom(fix[overrideIndex]), index);
2020-03-29 18:54:26 +00:00
me.addDiscontinuity(index + 1, plan);
2020-01-07 20:48:27 +00:00
me.flightPlanChanged(plan);
return 2;
} else {
var numToDel = me.flightplans[plan].indexOfWP(fix[overrideIndex]) - index;
while (numToDel > 0) {
2020-03-29 18:54:26 +00:00
me.deleteWP(index, plan, 1);
2020-01-07 20:48:27 +00:00
numToDel -= 1;
}
return 2;
}
2020-01-05 20:21:28 +00:00
}
2020-01-07 20:48:27 +00:00
} elsif (size(fix) >= 1) {
2020-05-14 19:02:18 +00:00
me.createDuplicateNames(fix, index, 0, plan);
2020-01-11 13:30:08 +00:00
return 2;
2019-12-29 21:37:52 +00:00
}
},
2020-01-05 20:21:28 +00:00
2020-01-11 13:30:08 +00:00
insertLatLonFix: func(text, index, plan) {
if (index == 0) {
return 1;
}
var lat = split("/", text)[0];
var lon = split("/", text)[1];
var latDecimal = mcdu.stringToDegrees(lat, "lat");
var lonDecimal = mcdu.stringToDegrees(lon, "lon");
if (latDecimal > 90 or latDecimal < -90 or lonDecimal > 180 or lonDecimal < -180) {
return 1;
}
var myWpLatLon = createWP(latDecimal, lonDecimal, "LL" ~ index);
if (me.flightplans[plan].indexOfWP(myWpLatLon) == -1) {
me.flightplans[plan].insertWP(myWpLatLon, index);
2020-03-29 18:54:26 +00:00
me.addDiscontinuity(index + 1, plan);
2020-01-11 13:30:08 +00:00
me.flightPlanChanged(plan);
return 2;
} else {
var numToDel = me.flightplans[plan].indexOfWP(myWpLatLon) - index;
while (numToDel > 0) {
2020-03-29 18:54:26 +00:00
me.deleteWP(index, plan, 1);
2020-01-11 13:30:08 +00:00
numToDel -= 1;
}
return 2;
}
},
insertNavaid: func(text, index, plan, override = 0, overrideIndex = -1) {
2020-01-05 20:21:28 +00:00
if (index == 0) {
2019-12-29 21:37:52 +00:00
return 1;
}
2020-01-05 20:21:28 +00:00
var navaid = findNavaidsByID(text);
if (size(navaid) == 0) {
2019-12-29 21:37:52 +00:00
return 0;
2020-01-05 20:21:28 +00:00
}
if (size(navaid) == 1 or override) {
2020-01-07 20:48:27 +00:00
if (!override) {
if (me.flightplans[plan].indexOfWP(navaid[0]) == -1) {
me.flightplans[plan].insertWP(createWPFrom(navaid[0]), index);
2020-03-29 18:54:26 +00:00
me.addDiscontinuity(index + 1, plan);
2020-01-07 20:48:27 +00:00
me.flightPlanChanged(plan);
return 2;
} else {
var numToDel = me.flightplans[plan].indexOfWP(navaid[0]) - index;
while (numToDel > 0) {
2020-03-29 18:54:26 +00:00
me.deleteWP(index, plan, 1);
2020-01-07 20:48:27 +00:00
numToDel -= 1;
}
return 2;
}
2019-12-29 21:37:52 +00:00
} else {
2020-01-07 20:48:27 +00:00
if (me.flightplans[plan].indexOfWP(navaid[overrideIndex]) == -1) {
me.flightplans[plan].insertWP(createWPFrom(navaid[overrideIndex]), index);
2020-03-29 18:54:26 +00:00
me.addDiscontinuity(index + 1, plan);
2020-01-07 20:48:27 +00:00
me.flightPlanChanged(plan);
return 2;
} else {
var numToDel = me.flightplans[plan].indexOfWP(navaid[overrideIndex]) - index;
while (numToDel > 0) {
2020-03-29 18:54:26 +00:00
me.deleteWP(index, plan, 1);
2020-01-07 20:48:27 +00:00
numToDel -= 1;
}
return 2;
}
2019-12-29 21:37:52 +00:00
}
2020-01-07 20:48:27 +00:00
} elsif (size(navaid) >= 1) {
2020-05-14 19:02:18 +00:00
me.createDuplicateNames(navaid, index, 1, plan);
return 2;
}
},
# getWPforPBD - parse scratchpad text to find waypoint ghost for PBD
# args: text, index, plan
# text: scratchpad text
# index: index at which waypoint will be inserted
# plan: plan to which waypoint will be inserted
# return:
# 0: not in database
# 1: notAllowed
# 2: o.k.
getWPforPBD: func(text, index, plan, override = 0, overrideIndex = -1) {
if (index == 0) {
return 1;
}
var textSplit = split("/", text);
if (size(split(".", textSplit[2])) != 1 or size(textSplit[1]) < 2 or size(textSplit[1]) > 3) {
return 1;
}
var wpGhost = nil;
var wpGhostContainer = nil;
var type = nil;
if (size(textSplit[0]) == 5) {
wpGhostContainer = findFixesByID(textSplit[0]);
if (size(wpGhostContainer) == 0) {
return 0;
}
type = "fix";
} elsif (size(textSplit[0]) == 4) {
wpGhostContainer = findAirportsByICAO(textSplit[0]);
if (size(wpGhostContainer) == 0) {
return 0;
}
type = "airport";
} elsif (size(textSplit[0]) == 3 or size(textSplit[0]) == 2) {
wpGhostContainer = findNavaidsByID(textSplit[0]);
if (size(wpGhostContainer) == 0) {
return 0;
}
type = "navaid";
} else {
return 1;
}
if (size(wpGhostContainer) == 0 or override) {
if (!override) {
wpGhost = wpGhostContainer[0];
} else {
wpGhost = wpGhostContainer[overrideIndex];
}
} else {
if (type == "navaid") {
me.createDuplicateNames(wpGhostContainer, index, 1, plan, 1, num(textSplit[1]), num(textSplit[2]));
} else {
me.createDuplicateNames(wpGhostContainer, index, 0, plan, 1, num(textSplit[1]), num(textSplit[2]));
2020-01-11 13:30:08 +00:00
}
return 2;
2019-12-29 21:37:52 +00:00
}
2020-05-14 19:02:18 +00:00
var localMagvar = magvar(wpGhost.lat, wpGhost.lon);
me.insertPlaceBearingDistance(wpGhost, textSplit[1] + localMagvar, textSplit[2], index, plan);
return 2;
},
# insertPlaceBearingDistance - insert PBD waypoint at specified index,
# at some specified bearing, distance from a specified location
# args: wp, index, plan
# wpt: waypoint ghost
# index: index to insert at in plan
# plan: plan to insert to
insertPlaceBearingDistance: func(wp, bearing, distance, index, plan) {
me.flightplans[plan].insertWP(me.childWPBearingDistance(wp, bearing, distance, "PBD" ~ index), index);
me.addDiscontinuity(index + 1, plan);
me.flightPlanChanged(plan);
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
2020-03-29 15:55:30 +00:00
scratchpad: func(text, index, plan) { # return 0 not in database, 1 not allowed, 2 success, 3 = not allowed due to dir to
2020-03-28 15:53:50 +00:00
if (mcdu.dirToFlag) {
2020-03-29 15:55:30 +00:00
return 3;
2020-03-28 15:53:50 +00:00
}
2020-01-07 20:48:27 +00:00
if (!fmgc.flightPlanController.temporaryFlag[plan]) {
if (text == "CLR" and me.flightplans[2].getWP(index).wp_name == "DISCONTINUITY") {
var thePlan = 2;
} else {
fmgc.flightPlanController.createTemporaryFlightPlan(plan);
var thePlan = plan;
}
} else {
var thePlan = plan;
}
2020-05-14 19:02:18 +00:00
if (size(split("/", text)) == 3) {
return me.getWPforPBD(text, index, thePlan);
} elsif (text == "CLR") {
2020-01-11 13:30:08 +00:00
return me.deleteWP(index, thePlan, 0);
2020-04-30 18:35:46 +00:00
} elsif (size(text) > 12) {
2020-01-11 13:30:08 +00:00
return me.insertLatLonFix(text, index, thePlan);
2020-01-05 20:21:28 +00:00
} elsif (size(text) == 5) {
2020-01-07 20:48:27 +00:00
return me.insertFix(text, index, thePlan);
2020-01-05 20:21:28 +00:00
} elsif (size(text) == 4) {
2020-01-07 20:48:27 +00:00
return me.insertAirport(text, index, thePlan);
2020-01-05 20:21:28 +00:00
} elsif (size(text) == 3 or size(text) == 2) {
2020-01-07 20:48:27 +00:00
return me.insertNavaid(text, index, thePlan);
2020-01-05 20:21:28 +00:00
} else {
return 1;
}
},
flightPlanChanged: func(n) {
2019-12-29 21:37:52 +00:00
sizeWP = size(wpID[n]);
2020-01-05 20:21:28 +00:00
for (var counter = sizeWP; counter < me.flightplans[n].getPlanSize(); counter += 1) { # create new properties if they are required
append(wpID[n], props.globals.initNode("/FMGC/flightplan[" ~ n ~ "]/wp[" ~ counter ~ "]/text", "", "STRING"));
2019-12-29 21:37:52 +00:00
append(wpLat[n], props.globals.initNode("/FMGC/flightplan[" ~ n ~ "]/wp[" ~ counter ~ "]/lat", 0, "DOUBLE"));
append(wpLon[n], props.globals.initNode("/FMGC/flightplan[" ~ n ~ "]/wp[" ~ counter ~ "]/lon", 0, "DOUBLE"));
append(wpCourse[n], props.globals.initNode("/FMGC/flightplan[" ~ n ~ "]/wp[" ~ counter ~ "]/course", 0, "DOUBLE"));
append(wpDistance[n], props.globals.initNode("/FMGC/flightplan[" ~ n ~ "]/wp[" ~ counter ~ "]/distance", 0, "DOUBLE"));
append(wpCoursePrev[n], props.globals.initNode("/FMGC/flightplan[" ~ n ~ "]/wp[" ~ counter ~ "]/course-from-prev", 0, "DOUBLE"));
append(wpDistancePrev[n], props.globals.initNode("/FMGC/flightplan[" ~ n ~ "]/wp[" ~ counter ~ "]/distance-from-prev", 0, "DOUBLE"));
}
2020-01-05 20:21:28 +00:00
me.updatePlans();
2020-01-11 13:30:08 +00:00
canvas_nd.A3XXRouteDriver.triggerSignal("fp-added");
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
updatePlans: func() {
me.updateCurrentWaypoint();
me._arrivalDist = 0;
for (var n = 0; n <= 2; n += 1) {
for (var wpt = 0; wpt < me.flightplans[n].getPlanSize(); wpt += 1) { # Iterate through the waypoints and update their data
var curAircraftPos = geo.aircraft_position(); # don't want to get this corrupted so make sure it is a local variable
var waypointHashStore = me.flightplans[n].getWP(wpt);
courseDistanceFrom = waypointHashStore.courseAndDistanceFrom(curAircraftPos);
wpID[n][wpt].setValue(waypointHashStore.wp_name);
wpLat[n][wpt].setValue(waypointHashStore.wp_lat);
wpLon[n][wpt].setValue(waypointHashStore.wp_lon);
wpCourse[n][wpt].setValue(waypointHashStore.courseAndDistanceFrom(curAircraftPos)[0]);
wpDistance[n][wpt].setValue(waypointHashStore.courseAndDistanceFrom(curAircraftPos)[1]);
2020-03-25 20:46:04 +00:00
if (wpt == 1) {
2020-03-28 17:06:42 +00:00
if (me.flightplans[n].getWP(wpt).wp_name != "DISCONTINUITY" and me.flightplans[n].getWP(wpt).wp_type != "vectors" and me.flightplans[n].getWP(wpt).wp_type != "hdgToAlt" and wpt <= me.arrivalIndex[n]) {
2020-04-24 15:47:13 +00:00
# print("Adding " ~ courseDistanceFrom[1] ~ " miles for waypoint " ~ me.flightplans[n].getWP(wpt).wp_name);
2020-03-28 15:53:50 +00:00
me._arrivalDist += courseDistanceFrom[1]; # distance to next waypoint, therafter to end of flightplan
}
2020-03-25 20:46:04 +00:00
}
2020-03-07 17:38:57 +00:00
if (left(wpID[n][wpt].getValue(), 4) == FMGCarr.getValue() and wpt != 0) {
if (me.arrivalIndex[n] != wpt) {
me.arrivalIndex[n] = wpt;
}
}
2020-01-05 20:21:28 +00:00
if (wpt > 0) {
2020-03-28 15:53:50 +00:00
geoPosPrev.set_latlon(me.flightplans[n].getWP(wpt - 1).lat, me.flightplans[n].getWP(wpt - 1).lon);
2020-01-05 20:21:28 +00:00
courseDistanceFromPrev = waypointHashStore.courseAndDistanceFrom(geoPosPrev);
wpCoursePrev[n][wpt].setValue(courseDistanceFromPrev[0]);
wpDistancePrev[n][wpt].setValue(courseDistanceFromPrev[1]);
2020-03-25 20:46:04 +00:00
if (wpt > 1) {
2020-03-28 15:53:50 +00:00
if (me.flightplans[n].getWP(wpt - 1).wp_name != "DISCONTINUITY" and me.flightplans[n].getWP(wpt).wp_name != "DISCONTINUITY" and me.flightplans[n].getWP(wpt - 1).wp_type != "vectors" and me.flightplans[n].getWP(wpt - 1).wp_type != "hdgToAlt" and me.flightplans[n].getWP(wpt).wp_type != "vectors" and me.flightplans[n].getWP(wpt).wp_type != "hdgToAlt" and wpt <= me.arrivalIndex[n]) {
2020-04-24 15:47:13 +00:00
# print("Adding " ~ courseDistanceFromPrev[1] ~ " miles for waypoint " ~ me.flightplans[n].getWP(wpt).wp_name);
me._arrivalDist += courseDistanceFromPrev[1];
2020-03-25 20:46:04 +00:00
}
2020-03-07 15:20:20 +00:00
}
2020-01-05 20:21:28 +00:00
} else {
# use PPOS for the first waypoint
wpCoursePrev[n][wpt].setValue(courseDistanceFrom[0]);
wpDistancePrev[n][wpt].setValue(courseDistanceFrom[1]);
}
2020-01-23 17:02:46 +00:00
if (left(wpID[n][wpt].getValue(), 4) == FMGCarr.getValue() and wpt != 0) {
2020-01-16 21:02:35 +00:00
if (me.arrivalIndex[n] != wpt) { # don't merge line 397 and 398 if statements
if (canvas_mcdu.myFpln[0] != nil) {
canvas_mcdu.myFpln[0].destInfo();
}
if (canvas_mcdu.myFpln[1] != nil) {
canvas_mcdu.myFpln[1].destInfo();
}
2020-01-07 20:48:27 +00:00
}
2020-01-05 20:21:28 +00:00
}
2019-12-29 21:37:52 +00:00
}
}
2020-04-24 15:47:13 +00:00
# print("Total: " ~ me._arrivalDist);
2020-01-05 20:21:28 +00:00
me.arrivalDist = me._arrivalDist;
me.updateMCDUDriver(n);
2019-12-29 21:37:52 +00:00
},
2020-01-05 20:21:28 +00:00
updateCurrentWaypoint: func() {
2020-02-03 17:45:00 +00:00
for (var india = 0; india <= 2; india += 1) {
if (toFromSet.getBoolValue() and me.flightplans[india].departure != nil and me.flightplans[india].destination != nil) { # check if flightplan exists
2020-01-05 20:21:28 +00:00
var curAircraftPos = geo.aircraft_position(); # don't want to get this corrupted so make sure it is a local variable
2020-02-03 17:45:00 +00:00
if (india == 2) { # main plan
2020-01-05 20:21:28 +00:00
if (!me.active.getBoolValue()) {
me.active.setValue(1);
2019-12-29 21:37:52 +00:00
}
2020-02-03 17:45:00 +00:00
if (me.currentToWptIndex.getValue() > me.flightplans[india].getPlanSize()) {
me.currentToWptIndex.setValue(me.flightplans[india].getPlanSize());
2019-12-29 21:37:52 +00:00
}
2020-02-03 17:45:00 +00:00
me.currentToWpt = me.flightplans[india].getWP(me.currentToWptIndex.getValue());
2019-12-29 21:37:52 +00:00
2020-02-02 20:34:27 +00:00
if (me.currentToWptID.getValue() != me.currentToWpt.wp_name) {
me.currentToWptID.setValue(me.currentToWpt.wp_name);
}
2020-01-23 14:14:48 +00:00
me.courseToWpt.setValue(me.currentToWpt.courseAndDistanceFrom(curAircraftPos)[0]);
me.distToWpt.setValue(me.currentToWpt.courseAndDistanceFrom(curAircraftPos)[1]);
2019-12-29 21:37:52 +00:00
magTrueError = magHDG.getValue() - trueHDG.getValue();
2020-01-23 14:14:48 +00:00
me.courseMagToWpt.setValue(me.courseToWpt.getValue() + magTrueError);
2019-12-29 21:37:52 +00:00
}
2020-02-03 17:45:00 +00:00
if (me.num[india].getValue() != me.flightplans[india].getPlanSize()) {
me.num[india].setValue(me.flightplans[india].getPlanSize());
2020-02-02 20:34:27 +00:00
}
2019-12-29 21:37:52 +00:00
} else {
2020-02-03 17:45:00 +00:00
if (india == 2) {
2020-01-05 20:21:28 +00:00
if (me.active.getBoolValue()) {
me.active.setValue(0);
2019-12-29 21:37:52 +00:00
}
2020-02-02 20:34:27 +00:00
if (me.currentToWptID.getValue() != "") {
me.currentToWptID.setValue("");
}
}
2020-02-03 17:45:00 +00:00
if (me.num[india].getValue() != 0) {
me.num[india].setValue(0);
2019-12-29 21:37:52 +00:00
}
}
2020-01-05 20:21:28 +00:00
}
},
2020-01-07 20:48:27 +00:00
updateMCDUDriver: func() {
2020-01-05 20:21:28 +00:00
for (var i = 0; i <= 1; i += 1) {
2020-01-07 20:48:27 +00:00
if (canvas_mcdu.myFpln[i] != nil) {
canvas_mcdu.myFpln[i].updatePlan();
2019-12-29 21:37:52 +00:00
}
2020-03-28 15:53:50 +00:00
if (canvas_mcdu.myDirTo[i] != nil) {
canvas_mcdu.myDirTo[i].updateFromFpln();
}
2019-12-29 21:37:52 +00:00
}
},
};
2020-01-05 20:21:28 +00:00
var flightPlanTimer = maketimer(0.1, flightPlanController, flightPlanController.updatePlans);