+ roll = rl;
+}
+
+void FGAIShip::setLeadAngleGain(double g) {
+ _lead_angle_gain = g;
+}
+
+void FGAIShip::setLeadAngleLimit(double l) {
+ _lead_angle_limit = l;
+}
+
+void FGAIShip::setLeadAngleProp(double p) {
+ _proportion = p;
+}
+
+void FGAIShip::setRudderConstant(double rc) {
+ _rudder_constant = rc;
+}
+
+void FGAIShip::setSpeedConstant(double sc) {
+ _speed_constant = sc;
+}
+
+void FGAIShip::setFixedTurnRadius(double ftr) {
+ _fixed_turn_radius = ftr;
+}
+
+void FGAIShip::setRollFactor(double rf) {
+ _roll_factor = rf * -0.0083335;
+}
+
+void FGAIShip::setInitialTunnel(bool t) {
+ _initial_tunnel = t;
+ setTunnel(_initial_tunnel);
+}
+
+void FGAIShip::setTunnel(bool t) {
+ _tunnel = t;
+}
+
+void FGAIShip::setWPNames() {
+
+ if (prev != 0)
+ setPrevName(prev->getName());
+ else
+ setPrevName("");
+
+ if (curr != 0)
+ setCurrName(curr->getName());
+ else{
+ setCurrName("");
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: current wp name error" );
+ }
+
+ if (next != 0)
+ setNextName(next->getName());
+ else
+ setNextName("");
+
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: prev wp name " << prev->getName());
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: current wp name " << curr->getName());
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: next wp name " << next->getName());
+
+}
+
+double FGAIShip::getRange(double lat, double lon, double lat2, double lon2) const {
+
+ double course, distance, az2;
+
+ //calculate the bearing and range of the second pos from the first
+ geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
+ distance *= SG_METER_TO_NM;
+ return distance;
+}
+
+double FGAIShip::getCourse(double lat, double lon, double lat2, double lon2) const {
+
+ double course, distance, recip;
+
+ //calculate the bearing and range of the second pos from the first
+ geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &recip, &distance);
+ if (tgt_speed >= 0) {
+ return course;
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: course " << course);
+ } else {
+ return recip;
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: recip " << recip);
+ }
+}
+
+void FGAIShip::ProcessFlightPlan(double dt) {
+
+ if ( dt < 0.00001 ) {
+ return;
+ }
+
+ double time_sec = getDaySeconds();
+
+ _dt_count += dt;
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Check Execution time (currently once every 1 sec)
+ // Add a bit of randomization to prevent the execution of all flight plans
+ // in synchrony, which can add significant periodic framerate flutter.
+ ///////////////////////////////////////////////////////////////////////////
+
+ //cout << "_start_sec " << _start_sec << " time_sec " << time_sec << endl;
+ if (_dt_count < _next_run && _start_sec < time_sec)
+ return;
+
+ _next_run = 0.05 + (0.025 * sg_random());
+
+ double until_time_sec = 0;
+ _missed = false;
+
+ // check to see if we've reached the point for our next turn
+ // if the range to the waypoint is less than the calculated turn
+ // radius we can start the turn to the next leg
+ _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
+ _range_rate = (_wp_range - _old_range) / _dt_count;
+ double sp_turn_radius_nm = _sp_turn_radius_ft / 6076.1155;
+ // we need to try to identify a _missed waypoint
+
+ // calculate the time needed to turn through an arc of 90 degrees,
+ // and allow a time error
+ if (speed != 0)
+ _missed_time_sec = 10 + ((SGD_PI * sp_turn_radius_nm * 60 * 60) / (2 * fabs(speed)));
+ else
+ _missed_time_sec = 10;
+
+ _missed_range = 4 * sp_turn_radius_nm;
+
+ //cout << _name << " range_rate " << _range_rate << " " << _new_waypoint<< endl ;
+ //if ((_range_rate > 0) && !_new_waypoint){
+ if (_range_rate > 0 && _wp_range < _missed_range && !_new_waypoint){
+ _missed_count += _dt_count;
+ }
+
+ if (_missed_count >= 120)
+ setMissed(true);
+ else if (_missed_count >= _missed_time_sec)
+ setMissed(true);
+ else
+ setMissed(false);
+
+ _old_range = _wp_range;
+ setWPNames();
+
+ if ((_wp_range < (sp_turn_radius_nm * 1.25)) || _missed || (_waiting && !_new_waypoint)) {
+
+ if (_next_name == "TUNNEL"){
+ _tunnel = !_tunnel;
+
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " " << sp_turn_radius_nm );
+
+ fp->IncrementWaypoint(false);
+ next = fp->getNextWaypoint();
+
+ if (next->getName() == "WAITUNTIL" || next->getName() == "WAIT"
+ || next->getName() == "END" || next->getName() == "TUNNEL")
+ return;
+
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+
+ }else if(_next_name == "END" || fp->getNextWaypoint() == 0) {
+
+ if (_repeat) {
+ SG_LOG(SG_AI, SG_INFO, "AIShip: "<< _name << " Flightplan repeating ");
+ fp->restart();
+ prev = curr;
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ setWPNames();
+ _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
+ _old_range = _wp_range;
+ _range_rate = 0;
+ _new_waypoint = true;
+ _missed_count = 0;
+ _lead_angle = 0;
+ AccelTo(prev->getSpeed());
+ } else if (_restart){
+ SG_LOG(SG_AI, SG_INFO, "AIShip: " << _name << " Flightplan restarting ");
+ _missed_count = 0;
+ initFlightPlan();
+ } else {
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: " << _name << " Flightplan dying ");
+ setDie(true);
+ _dt_count = 0;
+ return;
+ }
+
+ } else if (_next_name == "WAIT") {
+
+ if (_wait_count < next->getTime_sec()) {
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " waiting ");
+ setSpeed(0);
+ _waiting = true;
+ _wait_count += _dt_count;
+ _dt_count = 0;
+ _lead_angle = 0;
+ return;
+ } else {
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name
+ << " wait done: getting new waypoints ");
+ _waiting = false;
+ _wait_count = 0;
+ fp->IncrementWaypoint(false);
+ next = fp->getNextWaypoint();
+
+ if (next->getName() == "WAITUNTIL" || next->getName() == "WAIT"
+ || next->getName() == "END" || next->getName() == "TUNNEL")
+ return;
+
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ }
+
+ } else if (_next_name == "WAITUNTIL") {
+ time_sec = getDaySeconds();
+ until_time_sec = processTimeString(next->getTime());
+ _until_time = next->getTime();
+ setUntilTime(next->getTime());
+ if (until_time_sec > time_sec) {
+ SG_LOG(SG_AI, SG_INFO, "AIShip: " << _name << " "
+ << curr->getName() << " waiting until: "
+ << _until_time << " " << until_time_sec << " now " << time_sec );
+ setSpeed(0);
+ _lead_angle = 0;
+ _waiting = true;
+ return;
+ } else {
+ SG_LOG(SG_AI, SG_INFO, "AIShip: "
+ << _name << " wait until done: getting new waypoints ");
+ setUntilTime("");
+ fp->IncrementWaypoint(false);
+
+ while (next->getName() == "WAITUNTIL") {
+ fp->IncrementWaypoint(false);
+ next = fp->getNextWaypoint();
+ }
+
+ if (next->getName() == "WAIT")
+ return;
+
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ _waiting = false;
+ }
+
+ } else {
+ //now reorganise the waypoints, so that next becomes current and so on
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " getting new waypoints ");
+ fp->IncrementWaypoint(false);
+ prev = fp->getPreviousWaypoint(); //first waypoint
+ curr = fp->getCurrentWaypoint(); //second waypoint
+ next = fp->getNextWaypoint(); //third waypoint (might not exist!)
+ }
+
+ setWPNames();
+ _new_waypoint = true;
+ _missed_count = 0;
+ _range_rate = 0;
+ _lead_angle = 0;
+ _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
+ _old_range = _wp_range;
+ setWPPos();
+ object_type type = getType();
+
+ if (type != 10)
+ AccelTo(prev->getSpeed());
+
+ _curr_alt = curr->getAltitude();
+ _prev_alt = prev->getAltitude();
+
+ } else {
+ _new_waypoint = false;
+ }
+
+ // now revise the required course for the next way point
+ _course = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
+
+ if (finite(_course))
+ TurnTo(_course);
+ else
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: Bearing or Range is not a finite number");
+
+ _dt_count = 0;
+} // end Processing FlightPlan
+
+bool FGAIShip::initFlightPlan() {
+
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: " << _name << " initializing waypoints ");
+
+ bool init = false;
+ _start_sec = 0;
+ _tunnel = _initial_tunnel;
+
+ fp->restart();
+ fp->IncrementWaypoint(false);
+
+ prev = fp->getPreviousWaypoint(); //first waypoint
+ curr = fp->getCurrentWaypoint(); //second waypoint
+ next = fp->getNextWaypoint(); //third waypoint (might not exist!)
+
+ while (curr->getName() == "WAIT" || curr->getName() == "WAITUNTIL") { // don't wait when initialising
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " re-initializing waypoints ");
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ } // end while loop
+
+ if (!_start_time.empty()){
+ _start_sec = processTimeString(_start_time);
+ double day_sec = getDaySeconds();
+
+ if (_start_sec < day_sec){
+ //cout << "flight plan has already started " << _start_time << endl;
+ init = advanceFlightPlan(_start_sec, day_sec);
+
+ } else if (_start_sec > day_sec && _repeat) {
+ //cout << "flight plan has not started, " << _start_time;
+ //cout << "offsetting start time by -24 hrs" << endl;
+ _start_sec -= _day;
+ init = advanceFlightPlan(_start_sec, day_sec);
+ }
+
+ if (init)
+ _start_sec = 0; // set to zero for an immediate start of the Flight Plan
+ else {
+ fp->restart();
+ fp->IncrementWaypoint(false);
+ prev = fp->getPreviousWaypoint();
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ return false;
+ }
+
+ } else {
+ setLatitude(prev->getLatitude());
+ setLongitude(prev->getLongitude());
+ setSpeed(prev->getSpeed());
+ }
+
+ setWPNames();
+ setHeading(getCourse(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude()));
+ _wp_range = getRange(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude());
+ _old_range = _wp_range;
+ _range_rate = 0;
+ _hdg_lock = true;
+ _missed = false;
+ _missed_count = 0;
+ _new_waypoint = true;
+
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: " << _name << " done initialising waypoints " << _tunnel);
+ if (prev)
+ init = true;
+
+ if (init)
+ return true;
+ else
+ return false;
+
+} // end of initialization
+
+
+double FGAIShip::processTimeString(const string& theTime) {
+
+ int Hour;
+ int Minute;
+ int Second;
+
+ // first split theTime string into
+ // hour, minute, second and convert to int;
+ Hour = atoi(theTime.substr(0,2).c_str());
+ Minute = atoi(theTime.substr(3,5).c_str());
+ Second = atoi(theTime.substr(6,8).c_str());
+
+ // offset by a day-sec to allow for starting a day earlier
+ double time_seconds = Hour * 3600
+ + Minute * 60
+ + Second;
+
+ return time_seconds;
+}
+
+double FGAIShip::getDaySeconds () {
+ // Date and time
+ struct tm *t = globals->get_time_params()->getGmt();
+
+ double day_seconds = t->tm_hour * 3600
+ + t->tm_min * 60
+ + t->tm_sec;
+
+ return day_seconds;
+}
+
+bool FGAIShip::advanceFlightPlan (double start_sec, double day_sec) {
+
+ double elapsed_sec = start_sec;
+ double distance_nm = 0;
+
+ //cout << "advancing flight plan start_sec: " << start_sec << " " << day_sec << endl;
+
+ while ( elapsed_sec < day_sec ) {
+
+ if (next->getName() == "END" || fp->getNextWaypoint() == 0) {
+
+ if (_repeat ) {
+ //cout << _name << ": " << "restarting flightplan" << endl;
+ fp->restart();
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ } else {
+ //cout << _name << ": " << "ending flightplan" << endl;
+ setDie(true);
+ return false;
+ }
+
+ } else if (next->getName() == "WAIT") {
+ //cout << _name << ": begin WAIT: " << prev->name << " ";
+ //cout << curr->name << " " << next->name << endl;
+
+ elapsed_sec += next->getTime_sec();
+
+ if ( elapsed_sec >= day_sec)
+ continue;
+
+ fp->IncrementWaypoint(false);
+ next = fp->getNextWaypoint();
+
+ if (next->getName() != "WAITUNTIL" && next->getName() != "WAIT"
+ && next->getName() != "END") {
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ }
+
+ } else if (next->getName() == "WAITUNTIL") {
+ double until_sec = processTimeString(next->getTime());
+
+ if (until_sec > _start_sec && start_sec < 0)
+ until_sec -= _day;
+
+ if (elapsed_sec < until_sec)
+ elapsed_sec = until_sec;
+
+ if (elapsed_sec >= day_sec )
+ break;
+
+ fp->IncrementWaypoint(false);
+ next = fp->getNextWaypoint();
+
+ if (next->getName() != "WAITUNTIL" && next->getName() != "WAIT") {
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ }
+
+ //cout << _name << ": end WAITUNTIL: ";
+ //cout << prev->name << " " << curr->name << " " << next->name << endl;
+
+ } else {
+ distance_nm = getRange(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude());
+ elapsed_sec += distance_nm * 60 * 60 / prev->getSpeed();
+
+ if (elapsed_sec >= day_sec)
+ continue;
+
+ fp->IncrementWaypoint(false);
+ prev = fp->getPreviousWaypoint();
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ }
+
+ } // end while
+
+ // the required position lies between the previous and current waypoints
+ // so we will calculate the distance back up the track from the current waypoint
+ // then calculate the lat and lon.
+
+ //cout << "advancing flight plan done elapsed_sec: " << elapsed_sec
+ // << " " << day_sec << endl;
+
+ double time_diff = elapsed_sec - day_sec;
+ double lat, lon, recip;
+
+ //cout << " time diff " << time_diff << endl;
+
+ if (next->getName() == "WAIT" ){
+ setSpeed(0);
+ lat = curr->getLatitude();
+ lon = curr->getLongitude();
+ _wait_count= time_diff;
+ _waiting = true;
+ } else if (next->getName() == "WAITUNTIL") {
+ setSpeed(0);
+ lat = curr->getLatitude();
+ lon = curr->getLongitude();
+ _waiting = true;
+ } else {
+ setSpeed(prev->getSpeed());
+ distance_nm = speed * time_diff / (60 * 60);
+ double brg = getCourse(curr->getLatitude(), curr->getLongitude(), prev->getLatitude(), prev->getLongitude());
+
+ //cout << " brg " << brg << " from " << curr->name << " to " << prev->name << " "
+ // << " lat " << curr->latitude << " lon " << curr->longitude
+ // << " distance m " << distance_nm * SG_NM_TO_METER << endl;
+
+ lat = geo_direct_wgs_84 (curr->getLatitude(), curr->getLongitude(), brg,
+ distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
+ lon = geo_direct_wgs_84 (curr->getLatitude(), curr->getLongitude(), brg,
+ distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
+ recip = geo_direct_wgs_84 (curr->getLatitude(), curr->getLongitude(), brg,
+ distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
+ }
+
+ setLatitude(lat);
+ setLongitude(lon);
+
+ return true;
+}
+
+void FGAIShip::setWPPos() {
+
+ if (curr->getName() == "END" || curr->getName() == "WAIT"
+ || curr->getName() == "WAITUNTIL" || curr->getName() == "TUNNEL"){
+ //cout << curr->name << " returning" << endl;
+ return;
+ }
+
+ double elevation_m = 0;
+ wppos.setLatitudeDeg(curr->getLatitude());
+ wppos.setLongitudeDeg(curr->getLongitude());
+ wppos.setElevationM(0);
+
+ if (curr->getOn_ground()){
+
+ if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(wppos, 3000),
+ elevation_m, NULL, 0)){
+ wppos.setElevationM(elevation_m);
+ }
+
+ //cout << curr->name << " setting measured elev " << elevation_m << endl;
+
+ } else {
+ wppos.setElevationM(curr->getAltitude());
+ //cout << curr->name << " setting FP elev " << elevation_m << endl;
+ }
+
+ curr->setAltitude(wppos.getElevationM());
+
+}
+
+void FGAIShip::setXTrackError() {
+
+ double course = getCourse(prev->getLatitude(), prev->getLongitude(),
+ curr->getLatitude(), curr->getLongitude());
+ double brg = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
+ curr->getLatitude(), curr->getLongitude());
+ double xtrack_error_nm = sin((course - brg)* SG_DEGREES_TO_RADIANS) * _wp_range;
+ double factor = -0.0045 * speed + 1;
+ double limit = _lead_angle_limit * factor;
+
+ if (_wp_range > 0){
+ _lead_angle = atan2(xtrack_error_nm,(_wp_range * _proportion)) * SG_RADIANS_TO_DEGREES;
+ } else
+ _lead_angle = 0;
+
+ _lead_angle *= _lead_angle_gain * factor;
+ _xtrack_error = xtrack_error_nm * 6076.1155;
+
+ SG_CLAMP_RANGE(_lead_angle, -limit, limit);
+