1
0
Fork 0

Add some missing cases to Nasal FlightPlan binding

Expose 'between' restrictions as a 2-tuple.
This commit is contained in:
James Turner 2022-12-02 12:19:28 +00:00
parent 56996cc01a
commit 78e6a209f1
2 changed files with 41 additions and 1 deletions

View file

@ -2054,6 +2054,30 @@ static naRef f_leg_setAltitude(naContext c, naRef me, int argc, naRef* args)
if (argc > 2) { if (argc > 2) {
units = routeUnitsFromArg(args[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); 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) { 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)); return naNum(leg->altitude(units));

View file

@ -197,6 +197,19 @@ void FPNasalTests::testRestrictions()
CPPUNIT_ASSERT(ok); CPPUNIT_ASSERT(ok);
CPPUNIT_ASSERT_EQUAL(RESTRICT_DELETE, fp1->legAtIndex(3)->speedRestriction()); 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() void FPNasalTests::testSegfaultWaypointGhost()