]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/SchedFlight.cxx
Autopilot: use SimGear function
[flightgear.git] / src / Traffic / SchedFlight.cxx
1 /******************************************************************************
2  * SchedFlight.cxx
3  * Written by Durk Talsma, started May 5, 2004.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  *
20  **************************************************************************/
21  
22 /* This a prototype version of a top-level flight plan manager for Flightgear.
23  * It parses the fgtraffic.txt file and determine for a specific time/date, 
24  * where each aircraft listed in this file is at the current time. 
25  * 
26  * I'm currently assuming the following simplifications:
27  * 1) The earth is a perfect sphere
28  * 2) Each aircraft flies a perfect great circle route.
29  * 3) Each aircraft flies at a constant speed (with infinite accelerations and
30  *    decelerations) 
31  * 4) Each aircraft leaves at exactly the departure time. 
32  * 5) Each aircraft arrives at exactly the specified arrival time. 
33  *
34  * TODO:
35  * - Check the code for known portability issues
36  *
37  *****************************************************************************/
38
39 #ifdef HAVE_CONFIG_H
40 #  include "config.h"
41 #endif
42
43 #include <stdlib.h>
44 #include <time.h>
45 #include <iostream>
46 #include <fstream>
47
48
49 #include <string>
50 #include <vector>
51
52 #include <simgear/compiler.h>
53 #include <simgear/props/props.hxx>
54 #include <simgear/route/waypoint.hxx>
55 #include <simgear/structure/subsystem_mgr.hxx>
56 #include <simgear/timing/sg_time.hxx>
57 #include <simgear/xml/easyxml.hxx>
58
59 #include <AIModel/AIFlightPlan.hxx>
60 #include <AIModel/AIManager.hxx>
61 #include <Airports/simple.hxx>
62 #include <Main/fg_init.hxx>   // That's pretty ugly, but I need fgFindAirportID
63
64
65
66 #include <Main/globals.hxx>
67
68 #include "SchedFlight.hxx"
69
70
71 /******************************************************************************
72  * FGScheduledFlight stuff
73  *****************************************************************************/
74
75 FGScheduledFlight::FGScheduledFlight()
76 {
77    initialized = false;
78    available = true;
79 }
80   
81 FGScheduledFlight::FGScheduledFlight(const FGScheduledFlight &other)
82 {
83   callsign          = other.callsign;
84   fltRules          = other.fltRules;
85   departurePort     = other.departurePort;
86   depId             = other.depId;
87   arrId             = other.arrId;
88   departureTime     = other.departureTime;
89   cruiseAltitude    = other.cruiseAltitude;
90   arrivalPort       = other.arrivalPort;
91   arrivalTime       = other.arrivalTime;
92   repeatPeriod      = other.repeatPeriod;
93   initialized       = other.initialized;
94   requiredAircraft  = other.requiredAircraft;
95   available         = other.available;
96 }
97
98 FGScheduledFlight::FGScheduledFlight(const string& cs,
99                    const string& fr,
100                    const string& depPrt,
101                    const string& arrPrt,
102                    int cruiseAlt,
103                    const string& deptime,
104                    const string& arrtime,
105                    const string& rep,
106                    const string& reqAC)
107 {
108   callsign          = cs;
109   fltRules          = fr;
110   //departurePort.setId(depPrt);
111   //arrivalPort.setId(arrPrt);
112   depId = depPrt;
113   arrId = arrPrt;
114   //cerr << "Constructor: departure " << depId << ". arrival " << arrId << endl;
115   //departureTime     = processTimeString(deptime);
116   //arrivalTime       = processTimeString(arrtime);
117   cruiseAltitude    = cruiseAlt;
118   requiredAircraft  = reqAC;
119
120   // Process the repeat period string
121   if (rep.find("WEEK",0) != string::npos)
122     {
123       repeatPeriod = 7*24*60*60; // in seconds
124     }
125   else if (rep.find("Hr", 0) != string::npos)
126     {
127       repeatPeriod = 60*60*atoi(rep.substr(0,2).c_str());
128     }
129   else
130     {
131       repeatPeriod = 365*24*60*60;
132       SG_LOG( SG_GENERAL, SG_ALERT, "Unknown repeat period in flight plan "
133                                     "of flight '" << cs << "': " << rep );
134     }
135
136   // What we still need to do is preprocess the departure and
137   // arrival times. 
138   departureTime = processTimeString(deptime);
139   arrivalTime   = processTimeString(arrtime);
140   //departureTime += rand() % 300; // Make sure departure times are not limited to 5 minute increments.
141   if (departureTime > arrivalTime)
142     {
143       departureTime -= repeatPeriod;
144     }
145   initialized = false;
146   available   = true;
147 }
148
149
150 FGScheduledFlight:: ~FGScheduledFlight()
151 {
152 }
153
154 time_t FGScheduledFlight::processTimeString(const string& theTime)
155 {
156   int weekday;
157   int timeOffsetInDays;
158   int targetHour;
159   int targetMinute;
160   int targetSecond;
161
162   tm targetTimeDate;
163   SGTime* currTimeDate = globals->get_time_params();
164  
165   string timeCopy = theTime;
166
167
168   // okay first split theTime string into
169   // weekday, hour, minute, second;
170   // Check if a week day is specified 
171   if (timeCopy.find("/",0) != string::npos)
172     {
173       weekday          = atoi(timeCopy.substr(0,1).c_str());
174       timeOffsetInDays = weekday - currTimeDate->getGmt()->tm_wday;
175       timeCopy = timeCopy.substr(2,timeCopy.length());
176     }
177   else 
178     {
179       timeOffsetInDays = 0;
180     }
181   // TODO: verify status of each token.
182   targetHour   = atoi(timeCopy.substr(0,2).c_str());
183   targetMinute = atoi(timeCopy.substr(3,5).c_str());
184   targetSecond = atoi(timeCopy.substr(6,8).c_str());
185   targetTimeDate.tm_year  = currTimeDate->getGmt()->tm_year;
186   targetTimeDate.tm_mon   = currTimeDate->getGmt()->tm_mon;
187   targetTimeDate.tm_mday  = currTimeDate->getGmt()->tm_mday;
188   targetTimeDate.tm_hour  = targetHour;
189   targetTimeDate.tm_min   = targetMinute;
190   targetTimeDate.tm_sec   = targetSecond;
191
192   time_t processedTime = sgTimeGetGMT(&targetTimeDate);
193   processedTime += timeOffsetInDays*24*60*60;
194   if (processedTime < currTimeDate->get_cur_time())
195     {
196       processedTime += repeatPeriod;
197     }
198   //tm *temp = currTimeDate->getGmt();
199   //char buffer[512];
200   //sgTimeFormatTime(&targetTimeDate, buffer);
201   //cout << "Scheduled Time " << buffer << endl; 
202   //cout << "Time :" << time(NULL) << " SGTime : " << sgTimeGetGMT(temp) << endl;
203   return processedTime;
204 }
205
206 void FGScheduledFlight::update()
207 {
208   departureTime += repeatPeriod;
209   arrivalTime  += repeatPeriod;
210 }
211
212 void FGScheduledFlight::adjustTime(time_t now)
213 {
214   //cerr << "1: Adjusting schedule please wait: " << now 
215   //   << " " << arrivalTime << " " << arrivalTime+repeatPeriod << endl;
216   // Make sure that the arrival time is in between 
217   // the current time and the next repeat period.
218   while ((arrivalTime < now) || (arrivalTime > now+repeatPeriod))
219     {
220       if (arrivalTime < now)
221         {
222           departureTime += repeatPeriod;
223           arrivalTime   += repeatPeriod;
224         }
225       else if (arrivalTime > now+repeatPeriod)
226         {
227           departureTime -= repeatPeriod;
228           arrivalTime   -= repeatPeriod;
229         }
230       //      cerr << "2: Adjusting schedule please wait: " << now 
231       //   << " " << arrivalTime << " " << arrivalTime+repeatPeriod << endl;
232     }
233 }
234
235
236 FGAirport *FGScheduledFlight::getDepartureAirport()
237 {
238   if (!(initialized))
239     {
240       initializeAirports();
241     }
242   if (initialized)
243     return departurePort;
244   else
245     return 0;
246 }
247 FGAirport * FGScheduledFlight::getArrivalAirport  ()
248 {
249    if (!(initialized))
250     {
251       initializeAirports();
252     }
253    if (initialized)
254      return arrivalPort;
255    else
256      return 0;
257 }
258
259 // Upon the first time of requesting airport information
260 // for this scheduled flight, these data need to be
261 // looked up in the main FlightGear database. 
262 // Missing or bogus Airport codes are currently ignored,
263 // but we should improve that. The best idea is probably to cancel
264 // this flight entirely by removing it from the schedule, if one
265 // of the airports cannot be found. 
266 bool FGScheduledFlight::initializeAirports()
267 {
268   //cerr << "Initializing using : " << depId << " " << arrId << endl;
269   departurePort = FGAirport::findByIdent(depId);
270   if(departurePort == NULL)
271     {
272       SG_LOG( SG_GENERAL, SG_WARN, "Traffic manager could not find departure airport : " << depId);
273       return false;
274     }
275   arrivalPort = FGAirport::findByIdent(arrId);
276   if(arrivalPort == NULL)
277     {
278       SG_LOG( SG_GENERAL, SG_WARN, "Traffic manager could not find arrival airport   : " << arrId);
279       return false;
280     }
281
282   //cerr << "Found : " << departurePort->getId() << endl;
283   //cerr << "Found : " << arrivalPort->getId() << endl;
284   initialized = true;
285   return true;
286 }
287
288
289 bool compareScheduledFlights(FGScheduledFlight *a, FGScheduledFlight *b) 
290
291   return (*a) < (*b); 
292 };