+void FGAIAircraft::loadNextLeg()
+{
+ //delete fp;
+ //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
+
+
+ //FGAIModelEntity entity;
+ //entity.m_class = "jet_transport";
+ //entity.path = modelPath.c_str();
+ //entity.flightplan = "none";
+ //entity.latitude = _getLatitude();
+ //entity.longitude = _getLongitude();
+ //entity.altitude = trafficRef->getCruiseAlt() * 100; // convert from FL to feet
+ //entity.speed = 450; // HACK ALERT
+ //entity.fp = new FGAIFlightPlan(&entity, courseToDest, i->getDepartureTime(), dep, arr);
+ int leg;
+ if ((leg = fp->getLeg()) == 10)
+ {
+ trafficRef->next();
+ leg = 1;
+ fp->setLeg(leg);
+
+ //cerr << "Resetting leg : " << leg << endl;
+ }
+ //{
+ //leg++;
+ //fp->setLeg(leg);
+ //cerr << "Creating leg number : " << leg << endl;
+ FGAirport *dep = trafficRef->getDepartureAirport();
+ FGAirport *arr = trafficRef->getArrivalAirport();
+ if (!(dep && arr))
+ {
+ setDie(true);
+ //cerr << "Failed to get airport in AIAircraft::ProcessFlightplan()" << endl;
+ //if (dep)
+ // cerr << "Departure " << dep->getId() << endl;
+ //if (arr)
+ // cerr << "Arrival " << arr->getId() << endl;
+ }
+ else
+ {
+ double cruiseAlt = trafficRef->getCruiseAlt() * 100;
+ //cerr << "Creating new leg using " << cruiseAlt << " as cruise altitude."<< endl;
+
+ fp->create (dep,
+ arr,
+ leg,
+ cruiseAlt, //(trafficRef->getCruiseAlt() * 100), // convert from FL to feet
+ trafficRef->getSpeed(),
+ _getLatitude(),
+ _getLongitude(),
+ false,
+ trafficRef->getRadius(),
+ trafficRef->getFlightType(),
+ acType,
+ company);
+ //prev = fp->getPreviousWaypoint();
+ //curr = fp->getCurrentWaypoint();
+ //next = fp->getNextWaypoint();
+ //cerr << "25" << endl;
+ //if (next)
+ // {
+ // //cerr << "Next waypoint" << next->name << endl;
+ // fp->setLeadDistance(speed, tgt_heading, curr, next);
+ // }
+ //cerr << "25.1" << endl;
+ //if (curr->crossat > -1000.0) {
+ // //cerr << "25.1a" << endl;
+ // use_perf_vs = false;
+ //
+ // tgt_vs = (curr->crossat - altitude)/
+ // (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
+ // //cerr << "25.1b" << endl;
+ // tgt_altitude = curr->crossat;
+ //} else {
+ // //cerr << "25.1c" << endl;
+ // use_perf_vs = true;
+ // //cerr << "25.1d" << endl;
+ // tgt_altitude = prev->altitude;
+ // //cerr << "Setting target altitude : " <<tgt_altitude << endl;
+ // }
+ //cerr << "26" << endl;
+ //tgt_speed = prev->speed;
+ //hdg_lock = alt_lock = true;
+ //no_roll = prev->on_ground;
+
+ }
+ //}
+ //else
+ //{
+ //delete entity.fp;
+ //entity.fp = new FGAIFlightPlan(&entity,
+ // 999, // A hack
+ // trafficRef->getDepartureTime(),
+ // trafficRef->getDepartureAirport(),
+ // trafficRef->getArrivalAirport(),
+ // false,
+ // acType,
+ // company);
+ //SetFlightPlan(entity.fp);
+}
+
+
+
+// Note: This code is copied from David Luff's AILocalTraffic
+// Warning - ground elev determination is CPU intensive
+// Either this function or the logic of how often it is called
+// will almost certainly change.
+
+void FGAIAircraft::getGroundElev(double dt) {
+ dt_elev_count += dt;
+ //return;
+ if (dt_elev_count < (3.0) + (rand() % 10)) //Update minimally every three secs, but add some randomness to prevent all IA objects doing this in synchrony
+ {
+ return;
+ }
+ else
+ {
+ dt_elev_count = 0;
+ }
+ // It would be nice if we could set the correct tile center here in order to get a correct
+ // answer with one call to the function, but what I tried in the two commented-out lines
+ // below only intermittently worked, and I haven't quite groked why yet.
+ //SGBucket buck(pos.lon(), pos.lat());
+ //aip.getSGLocation()->set_tile_center(Point3D(buck.get_center_lon(), buck.get_center_lat(), 0.0));
+
+ // Only do the proper hitlist stuff if we are within visible range of the viewer.
+ double visibility_meters = fgGetDouble("/environment/visibility-m");
+
+
+ FGViewer* vw = globals->get_current_view();
+ double
+ course,
+ distance;
+
+ //Point3D currView(vw->getLongitude_deg(),
+ // vw->getLatitude_deg(), 0.0);
+ SGWayPoint current (pos.lon(),
+ pos.lat(),
+ 0);
+ SGWayPoint view ( vw->getLongitude_deg(),
+ vw->getLatitude_deg(),
+ 0);
+ view.CourseAndDistance(current, &course, &distance);
+ if(distance > visibility_meters) {
+ //aip.getSGLocation()->set_cur_elev_m(aptElev);
+ return;
+ }
+
+ // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
+ double range = 500.0;
+ if (!globals->get_tile_mgr()->scenery_available(pos.lat(), pos.lon(), range))
+ {
+ // Try to shedule tiles for that position.
+ globals->get_tile_mgr()->update( aip.getSGLocation(), range );
+ }
+
+ // FIXME: make sure the pos.lat/pos.lon values are in degrees ...
+ double alt;
+ if (globals->get_scenery()->get_elevation_m(pos.lat(), pos.lon(),
+ 20000.0, alt))
+ tgt_altitude = alt * SG_METER_TO_FEET;
+ //cerr << "Target altitude : " << tgt_altitude << endl;
+ // if (globals->get_scenery()->get_elevation_m(pos.lat(), pos.lon(),
+ // 20000.0, alt))
+ // tgt_altitude = alt * SG_METER_TO_FEET;
+ //cerr << "Target altitude : " << tgt_altitude << endl;
+}
+
+void FGAIAircraft::doGroundAltitude()
+{
+ if (fabs(altitude - (tgt_altitude+groundOffset)) > 1000.0)
+ altitude = (tgt_altitude + groundOffset);
+ else
+ altitude += 0.1 * ((tgt_altitude+groundOffset) - altitude);
+}