2020-05-17 00:37:42 +00:00
|
|
|
# A3XX FMGC Waypoint database
|
2022-01-11 02:17:41 +00:00
|
|
|
# Copyright (c) 2022 Josh Davidson (Octal450) and Jonathan Redpath (legoboyvdlp)
|
2020-05-17 00:37:42 +00:00
|
|
|
|
2020-05-17 16:53:43 +00:00
|
|
|
var nilTree = {
|
|
|
|
"latitude": 0,
|
|
|
|
"longitude": 0,
|
|
|
|
"ident": "",
|
|
|
|
};
|
|
|
|
|
2020-05-17 00:37:42 +00:00
|
|
|
var WaypointDatabase = {
|
|
|
|
waypointsVec: [],
|
2020-05-21 18:02:59 +00:00
|
|
|
confirm: [0, 0],
|
2020-05-17 00:37:42 +00:00
|
|
|
# addWP - adds pilot waypoint to waypoints vector
|
|
|
|
# arg: wpObj - passed pilot waypoint object
|
|
|
|
# return:
|
|
|
|
# 0 - not allowed
|
|
|
|
# 2 - accepted
|
|
|
|
# 4 - database full
|
|
|
|
addWP: func(wpObj) {
|
|
|
|
# validate ghost
|
|
|
|
if (wpObj.wpGhost == nil) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# check size of database
|
|
|
|
if (me.getCount() >= 20) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wpObj.index >= me.getSize()) {
|
|
|
|
# add to end, since index doesn't exist
|
2020-05-17 13:27:28 +00:00
|
|
|
append(me.waypointsVec, wpObj);
|
2020-05-19 12:04:33 +00:00
|
|
|
me.write();
|
2020-05-17 00:37:42 +00:00
|
|
|
return 2;
|
|
|
|
} elsif (me.waypointsVec[wpObj.index] == nil) {
|
|
|
|
# add at passed index
|
2020-05-17 13:27:28 +00:00
|
|
|
me.waypointsVec[wpObj.index] = wpObj;
|
2020-05-19 12:04:33 +00:00
|
|
|
me.write();
|
2020-05-17 20:36:42 +00:00
|
|
|
return 2;
|
2020-05-17 00:37:42 +00:00
|
|
|
} else {
|
|
|
|
# fall back to end
|
|
|
|
logprint(4, "pilotWaypoint constructor claims index " ~ wpObj.index ~ " is nil, but it isn't!");
|
2020-05-17 13:27:28 +00:00
|
|
|
append(me.waypointsVec, wpObj);
|
2020-05-19 12:04:33 +00:00
|
|
|
me.write();
|
2020-05-17 00:37:42 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2020-05-18 23:09:09 +00:00
|
|
|
|
2020-05-17 00:37:42 +00:00
|
|
|
},
|
|
|
|
# delete - empties waypoints vector
|
2020-05-21 18:02:59 +00:00
|
|
|
# callerIdx is the calling mcdu
|
|
|
|
delete: func(callerIdx) {
|
2020-05-17 16:53:43 +00:00
|
|
|
var noDel = 0;
|
|
|
|
for (var i = 0; i < me.getSize(); i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] != nil) {
|
|
|
|
if (fmgc.flightPlanController.flightplans[2].indexOfWP(me.waypointsVec[i].wpGhost) == -1) { # docs says only checks active and secondary
|
|
|
|
me.waypointsVec[i] = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 12:04:33 +00:00
|
|
|
me.write();
|
2020-05-21 18:02:59 +00:00
|
|
|
if (me.getCount() != 0) {
|
2020-05-23 11:16:20 +00:00
|
|
|
mcdu.mcdu_message(callerIdx, "PILOT ELEMENT RETAINED");
|
2020-05-21 18:02:59 +00:00
|
|
|
}
|
2020-05-17 00:37:42 +00:00
|
|
|
},
|
|
|
|
# deleteAtIndex - delete at specific index. Set to nil, so it still exists in vector
|
|
|
|
deleteAtIndex: func(index) {
|
|
|
|
if (index < 0 or index >= me.getSize() or index >= 20) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
me.waypointsVec[index] = nil;
|
2020-05-19 12:04:33 +00:00
|
|
|
me.write();
|
2020-05-17 00:37:42 +00:00
|
|
|
},
|
|
|
|
# getNilIndex - find the first nil
|
|
|
|
# post 2020.1 use dedicated function vecindex()
|
|
|
|
getNilIndex: func() {
|
|
|
|
for (var i = 0; i < me.getSize(); i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] == nil) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
},
|
2020-05-20 17:50:09 +00:00
|
|
|
# getNonNilIndex - find the first non-nil
|
|
|
|
# post 2020.1 use dedicated function vecindex()
|
|
|
|
getNonNilIndex: func() {
|
|
|
|
for (var i = 0; i < me.getSize(); i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] != nil) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
},
|
2020-05-21 18:02:59 +00:00
|
|
|
# getNextFromIndex - find the next non-nil after a passed index
|
|
|
|
getNextFromIndex: func(index) {
|
|
|
|
for (var i = (index + 1); i < me.getSize(); i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] != nil) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i <= index; i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] != nil) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
# getPreviousFromIndex - find the next non-nil before a passed index
|
|
|
|
getPreviousFromIndex: func(index) {
|
|
|
|
for (var i = (index - 1); i >= 0; i = i - 1) {
|
|
|
|
if (me.waypointsVec[i] != nil) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = (me.getSize() - 1); i >= index; i = i - 1) {
|
|
|
|
if (me.waypointsVec[i] != nil) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
# getNoOfIndex - return what number passed item is in list, neglecting "nil"
|
|
|
|
getNoOfIndex: func(index) {
|
|
|
|
var count = 0;
|
|
|
|
for (var i = 0; i <= index; i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] == nil) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
},
|
2020-05-17 00:37:42 +00:00
|
|
|
# getCount - return size, neglecting "nil"
|
|
|
|
getCount: func() {
|
|
|
|
var count = 0;
|
|
|
|
for (var i = 0; i < me.getSize(); i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] == nil) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
},
|
|
|
|
# getSize - return maximum size of vector
|
|
|
|
getSize: func() {
|
|
|
|
return size(me.waypointsVec);
|
|
|
|
},
|
2020-05-17 13:27:28 +00:00
|
|
|
# getWP - try to find waypoint whose name matches passed argument
|
2020-05-17 00:37:42 +00:00
|
|
|
getWP: func(text) {
|
|
|
|
for (var i = 0; i < me.getSize(); i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] == nil) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (text == me.waypointsVec[i].wpGhost.wp_name) {
|
|
|
|
return me.waypointsVec[i].wpGhost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
},
|
2020-05-18 23:09:09 +00:00
|
|
|
# write - write to file, as a hash structure
|
2020-05-17 13:27:28 +00:00
|
|
|
write: func() {
|
2021-06-09 16:25:17 +00:00
|
|
|
var path = pts.Sim.fgHome.getValue() ~ "/Export/A320SavedWaypoints.xml";
|
2020-05-17 13:27:28 +00:00
|
|
|
var tree = {
|
2020-05-17 23:11:24 +00:00
|
|
|
waypoints: {
|
2020-05-17 13:27:28 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = 0; i < me.getSize(); i = i + 1) {
|
|
|
|
if (me.waypointsVec[i] != nil) {
|
2020-05-17 23:11:24 +00:00
|
|
|
tree.waypoints["waypoint" ~ i] = me.waypointsVec[i].tree;
|
2020-05-17 13:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:11:24 +00:00
|
|
|
io.writexml(path, props.Node.new(tree)); # write the data
|
2020-05-17 13:27:28 +00:00
|
|
|
},
|
2020-05-18 23:09:09 +00:00
|
|
|
# read - read from a file, extract using props interface
|
|
|
|
read: func() {
|
2021-06-09 16:25:17 +00:00
|
|
|
var path = pts.Sim.fgHome.getValue() ~ "/Export/A320SavedWaypoints.xml";
|
2020-05-19 12:04:33 +00:00
|
|
|
# create file if it doesn't exist
|
|
|
|
if (io.stat(path) == nil) {
|
|
|
|
me.write();
|
|
|
|
return;
|
|
|
|
}
|
2020-05-18 23:09:09 +00:00
|
|
|
var data = io.readxml(path).getChild("waypoints");
|
|
|
|
var pilotWP = nil;
|
|
|
|
for (var i = 0; i < 20; i = i + 1) {
|
|
|
|
pilotWP = nil;
|
|
|
|
var childNode = data.getChild("waypoint" ~ i);
|
|
|
|
if (childNode == nil) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var wpt = createWP({lat: num(childNode.getChild("latitude").getValue()), lon: num(childNode.getChild("longitude").getValue())},childNode.getChild("ident").getValue());
|
|
|
|
|
|
|
|
if (left(childNode.getChild("ident").getValue(), 3) == "PBD") {
|
|
|
|
pilotWP = pilotWaypoint.newAtPosition(wpt, "PBD", right(childNode.getChild("ident").getValue(), 1));
|
|
|
|
} else {
|
|
|
|
pilotWP = pilotWaypoint.newAtPosition(wpt, "LL", right(childNode.getChild("ident").getValue(), 1));
|
|
|
|
}
|
|
|
|
me.addWPToPos(pilotWP, right(childNode.getChild("ident").getValue(), 1));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# addWPToPos - helper for reading - inserts at specific index
|
|
|
|
# will create nil for intermediates
|
|
|
|
addWPToPos: func(wpObj, position) {
|
|
|
|
if (me.getSize() >= position) {
|
|
|
|
me.waypointsVec[position - 1] = wpObj;
|
|
|
|
} else {
|
|
|
|
var numToIns = position - me.getSize();
|
|
|
|
while (numToIns >= 1) {
|
|
|
|
append(me.waypointsVec, nil);
|
|
|
|
numToIns -= 1;
|
|
|
|
}
|
|
|
|
me.waypointsVec[position - 1] = wpObj;
|
|
|
|
}
|
|
|
|
},
|
2020-05-17 00:37:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var pilotWaypoint = {
|
2020-05-17 13:27:28 +00:00
|
|
|
new: func(positioned, typeStr) {
|
2020-05-17 00:37:42 +00:00
|
|
|
var pilotWp = { parents:[pilotWaypoint] };
|
|
|
|
|
|
|
|
# Figure out what the first index is we can use
|
|
|
|
var nilIndex = WaypointDatabase.getNilIndex();
|
|
|
|
var position = nil;
|
|
|
|
|
|
|
|
if (nilIndex == -1) {
|
|
|
|
position = WaypointDatabase.getSize() + 1;
|
|
|
|
} else {
|
|
|
|
position = nilIndex + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pilotWp.setId(typeStr ~ sprintf("%s", position));
|
|
|
|
pilotWp.index = position - 1;
|
|
|
|
|
|
|
|
# set ghost to created waypoint
|
2020-05-17 13:27:28 +00:00
|
|
|
pilotWp.wpGhost = createWP(positioned, pilotWp.id);
|
|
|
|
|
|
|
|
pilotWp.tree = {
|
2020-05-17 23:11:24 +00:00
|
|
|
"latitude": pilotWp.wpGhost.wp_lat,
|
|
|
|
"longitude": pilotWp.wpGhost.wp_lon,
|
|
|
|
"ident": pilotWp.id,
|
2020-05-17 13:27:28 +00:00
|
|
|
};
|
2020-05-17 00:37:42 +00:00
|
|
|
|
|
|
|
return pilotWp;
|
|
|
|
},
|
2020-05-18 23:09:09 +00:00
|
|
|
newAtPosition: func(positioned, typeStr, position) {
|
|
|
|
var pilotWp = { parents:[pilotWaypoint] };
|
|
|
|
|
|
|
|
pilotWp.setId(typeStr ~ sprintf("%s", position));
|
|
|
|
pilotWp.index = position - 1;
|
|
|
|
|
|
|
|
# set ghost to created waypoint
|
|
|
|
pilotWp.wpGhost = positioned;
|
|
|
|
|
|
|
|
pilotWp.tree = {
|
|
|
|
"latitude": pilotWp.wpGhost.wp_lat,
|
|
|
|
"longitude": pilotWp.wpGhost.wp_lon,
|
|
|
|
"ident": pilotWp.id,
|
|
|
|
};
|
|
|
|
|
|
|
|
return pilotWp;
|
|
|
|
},
|
2020-05-17 00:37:42 +00:00
|
|
|
setId: func(id) {
|
|
|
|
if (typeof(id) == "scalar") { me.id = id; }
|
|
|
|
},
|
|
|
|
getId: func() {
|
|
|
|
if (me.id != nil) { return id; }
|
|
|
|
},
|
2020-05-21 18:02:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
setlistener("/MCDU[0]/page", func() {
|
|
|
|
if (getprop("/MCDU[0]/page") != "PILOTWP" and getprop("/MCDU[0]/page") != "STATUS") {
|
|
|
|
WaypointDatabase.confirm[0] = 0;
|
|
|
|
}
|
|
|
|
}, 0, 0);
|
|
|
|
|
|
|
|
setlistener("/MCDU[1]/page", func() {
|
|
|
|
if (getprop("/MCDU[1]/page") != "PILOTWP" and getprop("/MCDU[1]/page") != "STATUS") {
|
|
|
|
WaypointDatabase.confirm[1] = 0;
|
|
|
|
}
|
|
|
|
}, 0, 0);
|