]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/SchedFlight.cxx
Make sure 'make dist' keeps working.
[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  * - Find an alternative for the depricated Point3D class
37  *
38  *****************************************************************************/
39
40 #ifdef HAVE_CONFIG_H
41 #  include "config.h"
42 #endif
43
44 #include <stdlib.h>
45 #include <time.h>
46 #include <iostream>
47 #include <fstream>
48
49
50 #include <string>
51 #include <vector>
52
53 #include <plib/sg.h>
54
55 #include <simgear/compiler.h>
56 #include <simgear/math/polar3d.hxx>
57 #include <simgear/math/sg_geodesy.hxx>
58 #include <simgear/props/props.hxx>
59 #include <simgear/route/waypoint.hxx>
60 #include <simgear/structure/subsystem_mgr.hxx>
61 #include <simgear/timing/sg_time.hxx>
62 #include <simgear/xml/easyxml.hxx>
63
64 #include <AIModel/AIFlightPlan.hxx>
65 #include <AIModel/AIManager.hxx>
66 #include <Airports/simple.hxx>
67 #include <Main/fg_init.hxx>   // That's pretty ugly, but I need fgFindAirportID
68
69
70
71 #include <Main/globals.hxx>
72
73 #include "SchedFlight.hxx"
74
75
76 /******************************************************************************
77  * FGScheduledFlight stuff
78  *****************************************************************************/
79
80 FGScheduledFlight::FGScheduledFlight()
81 {
82 }
83   
84 FGScheduledFlight::FGScheduledFlight(const FGScheduledFlight &other)
85 {
86   callsign        = other.callsign;
87   fltRules        = other.fltRules;
88   departurePort   = other.departurePort;
89   depId           = other.depId;
90   arrId           = other.arrId;
91   departureTime   = other.departureTime;
92   cruiseAltitude  = other.cruiseAltitude;
93   arrivalPort     = other.arrivalPort;
94   arrivalTime     = other.arrivalTime;
95   repeatPeriod    = other.repeatPeriod;
96   initialized     = other.initialized;
97 }
98
99 FGScheduledFlight::FGScheduledFlight(const string& cs,
100                    const string& fr,
101                    const string& depPrt,
102                    const string& arrPrt,
103                    int cruiseAlt,
104                    const string& deptime,
105                    const string& arrtime,
106                    const string& rep)
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
119   // Process the repeat period string
120   if (rep.find("WEEK",0) != string::npos)
121     {
122       repeatPeriod = 7*24*60*60; // in seconds
123     }
124   else if (rep.find("Hr", 0) != string::npos)
125     {
126       repeatPeriod = 60*60*atoi(rep.substr(0,2).c_str());
127     }
128   else
129     {
130       repeatPeriod = 365*24*60*60;
131       SG_LOG( SG_GENERAL, SG_ALERT, "Unknown repeat period in flight plan "
132                                     "of flight '" << cs << "': " << rep );
133     }
134
135   // What we still need to do is preprocess the departure and
136   // arrival times. 
137   departureTime = processTimeString(deptime);
138   arrivalTime   = processTimeString(arrtime);
139   if (departureTime > arrivalTime)
140     {
141       departureTime -= repeatPeriod;
142     }
143   initialized = false;
144 }
145
146
147 FGScheduledFlight:: ~FGScheduledFlight()
148 {
149 }
150
151 time_t FGScheduledFlight::processTimeString(const string& theTime)
152 {
153   int weekday;
154   int timeOffsetInDays;
155   int targetHour;
156   int targetMinute;
157   int targetSecond;
158
159   tm targetTimeDate;
160   SGTime* currTimeDate = globals->get_time_params();
161  
162   string timeCopy = theTime;
163
164
165   // okay first split theTime string into
166   // weekday, hour, minute, second;
167   // Check if a week day is specified 
168   if (timeCopy.find("/",0) != string::npos)
169     {
170       weekday          = atoi(timeCopy.substr(0,1).c_str());
171       timeOffsetInDays = weekday - currTimeDate->getGmt()->tm_wday;
172       timeCopy = timeCopy.substr(2,timeCopy.length());
173     }
174   else 
175     {
176       timeOffsetInDays = 0;
177     }
178   // TODO: verify status of each token.
179   targetHour   = atoi(timeCopy.substr(0,2).c_str());
180   targetMinute = atoi(timeCopy.substr(3,5).c_str());
181   targetSecond = atoi(timeCopy.substr(6,8).c_str());
182   targetTimeDate.tm_year  = currTimeDate->getGmt()->tm_year;
183   targetTimeDate.tm_mon   = currTimeDate->getGmt()->tm_mon;
184   targetTimeDate.tm_mday  = currTimeDate->getGmt()->tm_mday;
185   targetTimeDate.tm_hour  = targetHour;
186   targetTimeDate.tm_min   = targetMinute;
187   targetTimeDate.tm_sec   = targetSecond;
188
189   time_t processedTime = sgTimeGetGMT(&targetTimeDate);
190   processedTime += timeOffsetInDays*24*60*60;
191   if (processedTime < currTimeDate->get_cur_time())
192     {
193       processedTime += repeatPeriod;
194     }
195   //tm *temp = currTimeDate->getGmt();
196   //char buffer[512];
197   //sgTimeFormatTime(&targetTimeDate, buffer);
198   //cout << "Scheduled Time " << buffer << endl; 
199   //cout << "Time :" << time(NULL) << " SGTime : " << sgTimeGetGMT(temp) << endl;
200   return processedTime;
201 }
202
203 void FGScheduledFlight::update()
204 {
205   departureTime += repeatPeriod;
206   arrivalTime  += repeatPeriod;
207 }
208
209 void FGScheduledFlight::adjustTime(time_t now)
210 {
211   //cerr << "1: Adjusting schedule please wait: " << now 
212   //   << " " << arrivalTime << " " << arrivalTime+repeatPeriod << endl;
213   // Make sure that the arrival time is in between 
214   // the current time and the next repeat period.
215   while ((arrivalTime < now) || (arrivalTime > now+repeatPeriod))
216     {
217       if (arrivalTime < now)
218         {
219           departureTime += repeatPeriod;
220           arrivalTime   += repeatPeriod;
221         }
222       else if (arrivalTime > now+repeatPeriod)
223         {
224           departureTime -= repeatPeriod;
225           arrivalTime   -= repeatPeriod;
226         }
227       //      cerr << "2: Adjusting schedule please wait: " << now 
228       //   << " " << arrivalTime << " " << arrivalTime+repeatPeriod << endl;
229     }
230 }
231
232
233 FGAirport *FGScheduledFlight::getDepartureAirport()
234 {
235   if (!(initialized))
236     {
237       initializeAirports();
238     }
239   if (initialized)
240     return departurePort;
241   else
242     return 0;
243 }
244 FGAirport * FGScheduledFlight::getArrivalAirport  ()
245 {
246    if (!(initialized))
247     {
248       initializeAirports();
249     }
250    if (initialized)
251      return arrivalPort;
252    else
253      return 0;
254 }
255
256 // Upon the first time of requesting airport information
257 // for this scheduled flight, these data need to be
258 // looked up in the main FlightGear database. 
259 // Missing or bogus Airport codes are currently ignored,
260 // but we should improve that. The best idea is probably to cancel
261 // this flight entirely by removing it from the schedule, if one
262 // of the airports cannot be found. 
263 bool FGScheduledFlight::initializeAirports()
264 {
265   //cerr << "Initializing using : " << depId << " " << arrId << endl;
266   departurePort = globals->get_airports()->search(depId);
267   if(departurePort == NULL)
268     {
269       SG_LOG( SG_GENERAL, SG_WARN, "Traffic manager could not find departure airport : " << depId);
270       return false;
271     }
272   arrivalPort = globals->get_airports()->search(arrId);
273   if(arrivalPort == NULL)
274     {
275       SG_LOG( SG_GENERAL, SG_WARN, "Traffic manager could not find arrival airport   : " << arrId);
276       return false;
277     }
278
279   //cerr << "Found : " << departurePort->getId() << endl;
280   //cerr << "Found : " << arrivalPort->getId() << endl;
281   initialized = true;
282   return true;
283 }
284
285
286 bool compareScheduledFlights(FGScheduledFlight *a, FGScheduledFlight *b) 
287
288   return (*a) < (*b); 
289 };