#include <simgear/compiler.h>
-#include <plib/sg.h>
#include <plib/ul.h>
#include <Environment/environment_mgr.hxx>
#include "dynamics.hxx"
FGAirportDynamics::FGAirportDynamics(FGAirport* ap) :
- _ap(ap), rwyPrefs(ap) {
+ _ap(ap), rwyPrefs(ap), SIDs(ap) {
lastUpdate = 0;
// For testing only. This needs to be refined when we move ATIS functionality over.
// Note that the ground network should also be copied
FGAirportDynamics::FGAirportDynamics(const FGAirportDynamics& other) :
- rwyPrefs(other.rwyPrefs)
+ rwyPrefs(other.rwyPrefs),
+ SIDs(other.SIDs)
{
for (FGParkingVecConstIterator ip= other.parkings.begin(); ip != other.parkings.end(); ip++)
parkings.push_back(*(ip));
//exit(1);
}
-bool FGAirportDynamics::innerGetActiveRunway(const string &trafficType, int action, string &runway)
+bool FGAirportDynamics::innerGetActiveRunway(const string &trafficType, int action, string &runway, double heading)
{
double windSpeed;
double windHeading;
// Note that the randomization below, is just a placeholder to choose between
// multiple active runways for this action. This should be
// under ATC control.
- runway = takeoff[(rand() % nr)];
+ runway = chooseRwyByHeading (takeoff, heading);
}
else
{ // Fallback
int nr = landing.size();
if (nr)
{
- runway = landing[(rand() % nr)];
+ runway = chooseRwyByHeading (landing, heading);
}
else
{ //fallback
return true;
}
-void FGAirportDynamics::getActiveRunway(const string &trafficType, int action, string &runway)
+string FGAirportDynamics::chooseRwyByHeading(stringVec rwys, double heading) {
+ double bestError = 360.0;
+ double rwyHeading, headingError;
+ string runway;
+ for (stringVecIterator i = rwys.begin(); i != rwys.end(); i++) {
+ FGRunway *rwy = _ap->getRunwayByIdent((*i));
+ rwyHeading = rwy->headingDeg();
+ headingError = fabs(heading - rwyHeading);
+ if (headingError > 180)
+ headingError = fabs(headingError - 360);
+ if (headingError < bestError) {
+ runway = (*i);
+ bestError = headingError;
+ }
+ }
+ //cerr << "Using active runway " << runway << " for heading " << heading << endl;
+ return runway;
+}
+
+void FGAirportDynamics::getActiveRunway(const string &trafficType, int action, string &runway, double heading)
{
- bool ok = innerGetActiveRunway(trafficType, action, runway);
+ bool ok = innerGetActiveRunway(trafficType, action, runway, heading);
if (!ok) {
runway = chooseRunwayFallback();
}
// so that at least I can start working on assigning different frequencies to different
// operations.
-int FGAirportDynamics::getGroundFrequency(int leg) {
+int FGAirportDynamics::getGroundFrequency(unsigned leg) {
//return freqGround.size() ? freqGround[0] : 0; };
- int groundFreq;
+ int groundFreq = 0;
if (leg < 2) {
SG_LOG(SG_ATC, SG_ALERT, "Leg value is smaller than two at " << SG_ORIGIN);
}
if (freqGround.size() == 0) {
return 0;
}
- if ((freqGround.size() >= leg-1) && (leg > 1)) {
+ if ((freqGround.size() > leg-1) && (leg > 1)) {
groundFreq = freqGround[leg-1];
}
if ((freqGround.size() < leg-1) && (leg > 1)) {
- groundFreq = (freqGround.size() < (leg-2)) ? freqGround[freqGround.size()-1] : freqGround[leg-2];
+ groundFreq = (freqGround.size() < (leg-1)) ? freqGround[freqGround.size()-1] : freqGround[leg-2];
}
if ((freqGround.size() >= leg-1) && (leg > 1)) {
groundFreq = freqGround[leg-2];
}
return groundFreq;
-}
\ No newline at end of file
+}
+
+FGAIFlightPlan *FGAirportDynamics::getSID(string activeRunway, double heading)
+{
+ return SIDs.getBest(activeRunway, heading);
+}