-// // // FGAIAircraft - FGAIBase-derived class creates an AI airplane
+// FGAIAircraft - FGAIBase-derived class creates an AI airplane
//
// Written by David Culp, started October 2003.
//
#include <Scenery/scenery.hxx>
#include <Scenery/tilemgr.hxx>
#include <Airports/dynamics.hxx>
+#include <Airports/simple.hxx>
#include <string>
#include <math.h>
static string tempReg;
-class AI_OutOfSight{};
-class FP_Inactive{};
-
FGAIAircraft::FGAIAircraft(FGAISchedule *ref) : FGAIBase(otAircraft) {
trafficRef = ref;
if (trafficRef) {
roll = 0;
headingChangeRate = 0.0;
headingError = 0;
+ minBearing = 360;
+ speedFraction =1.0;
holdPos = false;
+ needsTaxiClearance = false;
_performance = 0; //TODO initialize to JET_TRANSPORT from PerformanceDB
dt = 0;
void FGAIAircraft::Run(double dt) {
FGAIAircraft::dt = dt;
-
- try {
- updatePrimaryTargetValues(); // target hdg, alt, speed
- }
- catch (AI_OutOfSight) {
- return;
+
+ bool outOfSight = false,
+ flightplanActive = true;
+ updatePrimaryTargetValues(flightplanActive, outOfSight); // target hdg, alt, speed
+ if (outOfSight) {
+ return;
}
- catch (FP_Inactive) {
- //return;
- groundTargetSpeed = 0;
+
+ if (!flightplanActive) {
+ groundTargetSpeed = 0;
}
handleATCRequests(); // ATC also has a word to say
return;
dt_count = 0;
+ double distanceToDescent;
+ if(reachedEndOfCruise(distanceToDescent)) {
+ if (!loadNextLeg(distanceToDescent)) {
+ setDie(true);
+ return;
+ }
+ prev = fp->getPreviousWaypoint();
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ }
if (! leadPointReached(curr)) {
controlHeading(curr);
controlSpeed(curr, next);
//TODO let the fp handle this (loading of next leg)
fp->IncrementWaypoint( trafficRef != 0 );
- if (!(fp->getNextWaypoint()) && trafficRef != 0)
+ if ( ((!(fp->getNextWaypoint()))) && (trafficRef != 0) )
if (!loadNextLeg()) {
setDie(true);
return;
}
-bool FGAIAircraft::loadNextLeg() {
+bool FGAIAircraft::loadNextLeg(double distance) {
int leg;
if ((leg = fp->getLeg()) == 10) {
trafficRef->getRadius(),
trafficRef->getFlightType(),
acType,
- company);
+ company,
+ distance);
//cerr << "created leg " << leg << " for " << trafficRef->getCallSign() << endl;
}
return true;
}
double range = 500.0;
- if (!globals->get_tile_mgr()->scenery_available(pos, range)) {
- // Try to shedule tiles for that position.
- globals->get_tile_mgr()->update( pos, range );
+ if (globals->get_tile_mgr()->schedule_scenery(pos, range, 5.0))
+ {
+ double alt;
+ if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0))
+ tgt_altitude_ft = alt * SG_METER_TO_FEET;
}
-
- double alt;
- if (getGroundElevationM(SGGeod::fromGeodM(pos, 20000), alt, 0))
- tgt_altitude_ft = alt * SG_METER_TO_FEET;
}
}
altitude_ft = (tgt_altitude_ft + groundOffset);
else
altitude_ft += 0.1 * ((tgt_altitude_ft+groundOffset) - altitude_ft);
+ tgt_vs = 0;
}
cerr << "Error: Could not find Dynamics at airport : " << trafficRef->getDepartureAirport()->getId() << endl;
}
break;
+ case 7:
+ if (trafficRef->getDepartureAirport()->getDynamics()) {
+ controller = trafficRef->getArrivalAirport()->getDynamics()->getApproachController();
+ }
+ break;
case 9: // Taxiing for parking
if (trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork()->exists())
controller = trafficRef->getArrivalAirport()->getDynamics()->getGroundNetwork();
// << dist_to_go << ": Lead distance "
// << lead_dist << " " << curr->name
// << " Ground target speed " << groundTargetSpeed << endl;
-
- return dist_to_go < lead_dist;
+ double bearing = 0;
+ if (speed > 50) { // don't do bearing calculations for ground traffic
+ bearing = getBearing(fp->getBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr));
+ if (bearing < minBearing) {
+ minBearing = bearing;
+ if (minBearing < 10) {
+ minBearing = 10;
+ }
+ if ((minBearing < 360.0) && (minBearing > 10.0)) {
+ speedFraction = cos(minBearing *SG_DEGREES_TO_RADIANS);
+ } else {
+ speedFraction = 1.0;
+ }
+ }
+ }
+ if (trafficRef) {
+ //cerr << "Tracking callsign : \"" << fgGetString("/ai/track-callsign") << "\"" << endl;
+/* if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) {
+ cerr << trafficRef->getCallSign() << " " << tgt_altitude_ft << " " << _getSpeed() << " "
+ << _getAltitude() << " "<< _getLatitude() << " " << _getLongitude() << " " << dist_to_go << " " << lead_dist << " " << curr->name << " " << vs << " " << tgt_vs << " " << bearing << " " << minBearing << " " << speedFraction << endl;
+ }*/
+ }
+ if ((dist_to_go < lead_dist) || (bearing > (minBearing * 1.1))) {
+ minBearing = 360;
+ return true;
+ } else {
+ return false;
+ }
}
//cerr << trafficRef->getCallSign() << " has passed waypoint " << prev->name << " at speed " << speed << endl;
if (prev->name == "PushBackPoint") {
dep->getDynamics()->releaseParking(fp->getGate());
- time_t holdUntil = now + 120;
- fp->setTime(holdUntil);
- //cerr << _getCallsign() << "Holding at pushback point" << endl;
+ AccelTo(0.0);
+ setTaxiClearanceRequest(true);
}
// This is the last taxi waypoint, and marks the the end of the flight plan
/**
* Update target values (heading, alt, speed) depending on flight plan or control properties
*/
-void FGAIAircraft::updatePrimaryTargetValues() {
+void FGAIAircraft::updatePrimaryTargetValues(bool& flightplanActive, bool& aiOutOfSight) {
if (fp) // AI object has a flightplan
{
//TODO make this a function of AIBase
}
if (trafficRef) {
//cerr << trafficRef->getRegistration() << " Setting altitude to " << altitude_ft;
- if (! aiTrafficVisible()) {
+ aiOutOfSight = !aiTrafficVisible();
+ if (aiOutOfSight) {
setDie(true);
//cerr << trafficRef->getRegistration() << " is set to die " << endl;
- throw AI_OutOfSight();
+ aiOutOfSight = true;
+ return;
}
}
timeElapsed = now - fp->getStartTime();
- if (! fp->isActive(now)) {
- throw FP_Inactive();
- }
+ flightplanActive = fp->isActive(now);
} else {
// no flight plan, update target heading, speed, and altitude
// from control properties. These default to the initial
if (onGround())
speed = _performance->actualSpeed(this, groundTargetSpeed, dt);
else
- speed = _performance->actualSpeed(this, tgt_speed, dt);
+ speed = _performance->actualSpeed(this, (tgt_speed *speedFraction), dt);
updateHeading();
roll = _performance->actualBankAngle(this, tgt_roll, dt);
//TODO calculate wind correction angle (tgt_yaw)
}
+
+
+bool FGAIAircraft::reachedEndOfCruise(double &distance) {
+ FGAIFlightPlan::waypoint* curr = fp->getCurrentWaypoint();
+ if (curr->name == "BOD") {
+ double dist = fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
+ double descentSpeed = (getPerformance()->vDescent() * SG_NM_TO_METER) / 3600.0; // convert from kts to meter/s
+ double descentRate = (getPerformance()->descentRate() * SG_FEET_TO_METER) / 60.0; // convert from feet/min to meter/s
+
+ double verticalDistance = ((altitude_ft - 2000.0) - trafficRef->getArrivalAirport()->getElevation()) *SG_FEET_TO_METER;
+ double descentTimeNeeded = verticalDistance / descentRate;
+ double distanceCovered = descentSpeed * descentTimeNeeded;
+
+ //cerr << "Tracking : " << fgGetString("/ai/track-callsign");
+ if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) {
+ cerr << "Checking for end of cruise stage for :" << trafficRef->getCallSign() << endl;
+ cerr << "Descent rate : " << descentRate << endl;
+ cerr << "Descent speed : " << descentSpeed << endl;
+ cerr << "VerticalDistance : " << verticalDistance << ". Altitude : " << altitude_ft << ". Elevation " << trafficRef->getArrivalAirport()->getElevation() << endl;
+ cerr << "DecentTimeNeeded : " << descentTimeNeeded << endl;
+ cerr << "DistanceCovered : " << distanceCovered << endl;
+ }
+ //cerr << "Distance = " << distance << endl;
+ distance = distanceCovered;
+ if (dist < distanceCovered) {
+ if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) {
+ //exit(1);
+ }
+ return true;
+ } else {
+ return false;
+ }
+ } else {
+ return false;
+ }
+}
+
+void FGAIAircraft::resetPositionFromFlightPlan()
+{
+ // the one behind you
+ FGAIFlightPlan::waypoint* prev = 0;
+ // the one ahead
+ FGAIFlightPlan::waypoint* curr = 0;
+ // the next plus 1
+ FGAIFlightPlan::waypoint* next = 0;
+
+ prev = fp->getPreviousWaypoint();
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+
+ setLatitude(prev->latitude);
+ setLongitude(prev->longitude);
+ double tgt_heading = fp->getBearing(curr, next);
+ setHeading(tgt_heading);
+ setAltitude(prev->altitude);
+ setSpeed(prev->speed);
+}
+
+double FGAIAircraft::getBearing(double crse)
+{
+ double hdgDiff = fabs(hdg-crse);
+ if (hdgDiff > 180)
+ hdgDiff = fabs(hdgDiff - 360);
+ return hdgDiff;
+}
+
+time_t FGAIAircraft::checkForArrivalTime(string wptName) {
+ FGAIFlightPlan::waypoint* curr = 0;
+ curr = fp->getCurrentWaypoint();
+
+ double tracklength = fp->checkTrackLength(wptName);
+ if (tracklength > 0.1) {
+ tracklength += fp->getDistanceToGo(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr);
+ } else {
+ return 0;
+ }
+ time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+ time_t arrivalTime = fp->getArrivalTime();
+
+ time_t ete = tracklength / ((speed * SG_NM_TO_METER) / 3600.0);
+ time_t secondsToGo = arrivalTime - now;
+ if (trafficRef->getCallSign() == fgGetString("/ai/track-callsign")) {
+ cerr << "Checking arrival time: ete " << ete << ". Time to go : " << secondsToGo << ". Track length = " << tracklength << endl;
+ }
+ return (ete - secondsToGo); // Positive when we're too slow...
+}
\ No newline at end of file