+ // 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->name == "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->name == "WAIT") {
+ //cout << _name << ": begin WAIT: " << prev->name << " ";
+ //cout << curr->name << " " << next->name << endl;
+
+ elapsed_sec += next->time_sec;
+
+ if ( elapsed_sec >= day_sec)
+ continue;
+
+ fp->IncrementWaypoint(false);
+ next = fp->getNextWaypoint();
+
+ if (next->name != "WAITUNTIL" && next->name != "WAIT"
+ && next->name != "END") {
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+ }
+
+ } else if (next->name == "WAITUNTIL") {
+ double until_sec = processTimeString(next->time);
+
+ 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->name != "WAITUNTIL" && next->name != "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->latitude, prev->longitude, curr->latitude, curr->longitude);
+ elapsed_sec += distance_nm * 60 * 60 / prev->speed;
+
+ 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->name == "WAIT" ){
+ setSpeed(0);
+ lat = curr->latitude;
+ lon = curr->longitude;
+ _wait_count= time_diff;
+ _waiting = true;
+ } else if (next->name == "WAITUNTIL") {
+ setSpeed(0);
+ lat = curr->latitude;
+ lon = curr->longitude;
+ _waiting = true;
+ } else {
+ setSpeed(prev->speed);
+ distance_nm = speed * time_diff / (60 * 60);
+ double brg = getCourse(curr->latitude, curr->longitude, prev->latitude, prev->longitude);
+
+ //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->latitude, curr->longitude, brg,
+ distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
+ lon = geo_direct_wgs_84 (curr->latitude, curr->longitude, brg,
+ distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
+ recip = geo_direct_wgs_84 (curr->latitude, curr->longitude, brg,
+ distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
+ }
+
+ setLatitude(lat);
+ setLongitude(lon);
+
+ return true;
+}
+
+void FGAIShip::setWPPos() {
+
+ if (curr->name == "END" || curr->name == "WAIT" || curr->name == "WAITUNTIL"){
+ cout<< curr->name << endl;
+ return;
+ }
+
+ double elevation_m = 0;
+ wppos.setLatitudeDeg(curr->latitude);
+ wppos.setLongitudeDeg(curr->longitude);
+ wppos.setElevationFt(0);
+
+ if (curr->on_ground){
+
+ if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(wppos, 10000),
+ elevation_m, &_material)){
+ wppos.setElevationM(elevation_m);
+ }
+
+ } else {
+ wppos.setElevationFt(curr->altitude);
+ }
+
+}
+
+void FGAIShip::setXTrackError() {
+
+ double course = getCourse(prev->latitude, prev->longitude,
+ curr->latitude, curr->longitude);
+ double brg = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
+ curr->latitude, curr->longitude);
+ double xtrack_error_nm = sin((course - brg)* SG_DEGREES_TO_RADIANS) * _wp_range;
+
+ //if (_wp_range > _sp_turn_radius_ft / (2 * 6076.1155)){
+ 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;
+ _xtrack_error = xtrack_error_nm * 6076.1155;