From 78e6a209f169d6150360060c9a629aae277e941c Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 2 Dec 2022 12:19:28 +0000 Subject: [PATCH] Add some missing cases to Nasal FlightPlan binding Expose 'between' restrictions as a 2-tuple. --- src/Scripting/NasalFlightPlan.cxx | 29 ++++++++++++++++++- .../unit_tests/Navaids/test_fpNasal.cxx | 13 +++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Scripting/NasalFlightPlan.cxx b/src/Scripting/NasalFlightPlan.cxx index 349882bb5..9ed4131a0 100644 --- a/src/Scripting/NasalFlightPlan.cxx +++ b/src/Scripting/NasalFlightPlan.cxx @@ -2054,6 +2054,30 @@ static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args) if (argc > 2) { units = routeUnitsFromArg(args[2]); } + } else if (naIsVector(args[0])) { + const auto altTuple = args[0]; + // we need a second restriction type arg, and the tuple should be of length 2 + if ((argc < 2) || (naVec_size(altTuple) != 2)) { + naRuntimeError(c, "missing/bad arguments to leg.setAltitude"); + } + + rr = routeRestrictionFromArg(args[1]); + if (rr != RESTRICT_BETWEEN) { + naRuntimeError(c, "leg.setAltitude: passed a 2-tuple, but restriction type is not 'between'"); + } + + double constraintAltitude; + const auto ok = convertToNum(naVec_get(altTuple, 0), constraintAltitude) + && convertToNum(naVec_get(altTuple, 1), altitude); + if (!ok) { + naRuntimeError(c, "leg.setAltitude: tuple members not convertible to numeric altitudes"); + } + + if (argc > 2) { + units = routeUnitsFromArg(args[2]); + } + + // TODO: store constraint altitude } leg->setAltitude(rr, altitude, units); @@ -2077,7 +2101,10 @@ static naRef f_leg_altitude(naContext c, naRef me, int argc, naRef* args) } if (leg->altitudeRestriction() == RESTRICT_BETWEEN) { - // make a 2-tuple for the between case + naRef result = naNewVector(c); + naVec_append(result, naNum(leg->waypoint()->constraintAltitude(units))); + naVec_append(result, naNum(leg->altitude(units))); + return result; } return naNum(leg->altitude(units)); diff --git a/test_suite/unit_tests/Navaids/test_fpNasal.cxx b/test_suite/unit_tests/Navaids/test_fpNasal.cxx index b52c437fa..38aff579d 100644 --- a/test_suite/unit_tests/Navaids/test_fpNasal.cxx +++ b/test_suite/unit_tests/Navaids/test_fpNasal.cxx @@ -197,6 +197,19 @@ void FPNasalTests::testRestrictions() CPPUNIT_ASSERT(ok); CPPUNIT_ASSERT_EQUAL(RESTRICT_DELETE, fp1->legAtIndex(3)->speedRestriction()); + + ok = FGTestApi::executeNasal(R"( + var fp = flightplan(); # retrieve the global flightplan + var leg = fp.getWP(3); + leg.setSpeed(0.8, 'at', "Mach"); + leg.setAltitude(300, 'above', 'FL'); + )"); + CPPUNIT_ASSERT(ok); + + CPPUNIT_ASSERT_EQUAL(RESTRICT_AT, fp1->legAtIndex(3)->speedRestriction()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.8, fp1->legAtIndex(3)->speedMach(), 0.01); + CPPUNIT_ASSERT_EQUAL(RESTRICT_ABOVE, fp1->legAtIndex(3)->altitudeRestriction()); + CPPUNIT_ASSERT_DOUBLES_EQUAL(30000, fp1->legAtIndex(3)->altitudeFt(), 1.0); } void FPNasalTests::testSegfaultWaypointGhost()