]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/SchedFlight.cxx
Merge branch 'maint2' into next
[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    initialized = false;
83    available = true;
84 }
85   
86 FGScheduledFlight::FGScheduledFlight(const FGScheduledFlight &other)
87 {
88   callsign          = other.callsign;
89   fltRules          = other.fltRules;
90   departurePort     = other.departurePort;
91   depId             = other.depId;
92   arrId             = other.arrId;
93   departureTime     = other.departureTime;
94   cruiseAltitude    = other.cruiseAltitude;
95   arrivalPort       = other.arrivalPort;
96   arrivalTime       = other.arrivalTime;
97   repeatPeriod      = other.repeatPeriod;
98   initialized       = other.initialized;
99   requiredAircraft  = other.requiredAircraft;
100   available         = other.available;
101 }
102
103 FGScheduledFlight::FGScheduledFlight(const string& cs,
104                    const string& fr,
105                    const string& depPrt,
106                    const string& arrPrt,
107                    int cruiseAlt,
108                    const string& deptime,
109                    const string& arrtime,
110                    const string& rep,
111                    const string& reqAC)
112 {
113   callsign          = cs;
114   fltRules          = fr;
115   //departurePort.setId(depPrt);
116   //arrivalPort.setId(arrPrt);
117   depId = depPrt;
118   arrId = arrPrt;
119   //cerr << "Constructor: departure " << depId << ". arrival " << arrId << endl;
120   //departureTime     = processTimeString(deptime);
121   //arrivalTime       = processTimeString(arrtime);
122   cruiseAltitude    = cruiseAlt;
123   requiredAircraft  = reqAC;
124
125   // Process the repeat period string
126   if (rep.find("WEEK",0) != string::npos)
127     {
128       repeatPeriod = 7*24*60*60; // in seconds
129     }
130   else if (rep.find("Hr", 0) != string::npos)
131     {
132       repeatPeriod = 60*60*atoi(rep.substr(0,2).c_str());
133     }
134   else
135     {
136       repeatPeriod = 365*24*60*60;
137       SG_LOG( SG_GENERAL, SG_ALERT, "Unknown repeat period in flight plan "
138                                     "of flight '" << cs << "': " << rep );
139     }
140
141   // What we still need to do is preprocess the departure and
142   // arrival times. 
143   departureTime = processTimeString(deptime);
144   arrivalTime   = processTimeString(arrtime);
145   //departureTime += rand() % 300; // Make sure departure times are not limited to 5 minute increments.
146   if (departureTime > arrivalTime)
147     {
148       departureTime -= repeatPeriod;
149     }
150   initialized = false;
151   available   = true;
152 }
153
154
155 FGScheduledFlight:: ~FGScheduledFlight()
156 {
157 }
158
159 time_t FGScheduledFlight::processTimeString(const string& theTime)
160 {
161   int weekday;
162   int timeOffsetInDays;
163   int targetHour;
164   int targetMinute;
165   int targetSecond;
166
167   tm targetTimeDate;
168   SGTime* currTimeDate = globals->get_time_params();
169  
170   string timeCopy = theTime;
171
172
173   // okay first split theTime string into
174   // weekday, hour, minute, second;
175   // Check if a week day is specified 
176   if (timeCopy.find("/",0) != string::npos)
177     {
178       weekday          = atoi(timeCopy.substr(0,1).c_str());
179       timeOffsetInDays = weekday - currTimeDate->getGmt()->tm_wday;
180       timeCopy = timeCopy.substr(2,timeCopy.length());
181     }
182   else 
183     {
184       timeOffsetInDays = 0;
185     }
186   // TODO: verify status of each token.
187   targetHour   = atoi(timeCopy.substr(0,2).c_str());
188   targetMinute = atoi(timeCopy.substr(3,5).c_str());
189   targetSecond = atoi(timeCopy.substr(6,8).c_str());
190   targetTimeDate.tm_year  = currTimeDate->getGmt()->tm_year;
191   targetTimeDate.tm_mon   = currTimeDate->getGmt()->tm_mon;
192   targetTimeDate.tm_mday  = currTimeDate->getGmt()->tm_mday;
193   targetTimeDate.tm_hour  = targetHour;
194   targetTimeDate.tm_min   = targetMinute;
195   targetTimeDate.tm_sec   = targetSecond;
196
197   time_t processedTime = sgTimeGetGMT(&targetTimeDate);
198   processedTime += timeOffsetInDays*24*60*60;
199   if (processedTime < currTimeDate->get_cur_time())
200     {
201       processedTime += repeatPeriod;
202     }
203   //tm *temp = currTimeDate->getGmt();
204   //char buffer[512];
205   //sgTimeFormatTime(&targetTimeDate, buffer);
206   //cout << "Scheduled Time " << buffer << endl; 
207   //cout << "Time :" << time(NULL) << " SGTime : " << sgTimeGetGMT(temp) << endl;
208   return processedTime;
209 }
210
211 void FGScheduledFlight::update()
212 {
213   departureTime += repeatPeriod;
214   arrivalTime  += repeatPeriod;
215 }
216
217 void FGScheduledFlight::adjustTime(time_t now)
218 {
219   //cerr << "1: Adjusting schedule please wait: " << now 
220   //   << " " << arrivalTime << " " << arrivalTime+repeatPeriod << endl;
221   // Make sure that the arrival time is in between 
222   // the current time and the next repeat period.
223   while ((arrivalTime < now) || (arrivalTime > now+repeatPeriod))
224     {
225       if (arrivalTime < now)
226         {
227           departureTime += repeatPeriod;
228           arrivalTime   += repeatPeriod;
229         }
230       else if (arrivalTime > now+repeatPeriod)
231         {
232           departureTime -= repeatPeriod;
233           arrivalTime   -= repeatPeriod;
234         }
235       //      cerr << "2: Adjusting schedule please wait: " << now 
236       //   << " " << arrivalTime << " " << arrivalTime+repeatPeriod << endl;
237     }
238 }
239
240
241 FGAirport *FGScheduledFlight::getDepartureAirport()
242 {
243   if (!(initialized))
244     {
245       initializeAirports();
246     }
247   if (initialized)
248     return departurePort;
249   else
250     return 0;
251 }
252 FGAirport * FGScheduledFlight::getArrivalAirport  ()
253 {
254    if (!(initialized))
255     {
256       initializeAirports();
257     }
258    if (initialized)
259      return arrivalPort;
260    else
261      return 0;
262 }
263
264 // Upon the first time of requesting airport information
265 // for this scheduled flight, these data need to be
266 // looked up in the main FlightGear database. 
267 // Missing or bogus Airport codes are currently ignored,
268 // but we should improve that. The best idea is probably to cancel
269 // this flight entirely by removing it from the schedule, if one
270 // of the airports cannot be found. 
271 bool FGScheduledFlight::initializeAirports()
272 {
273   //cerr << "Initializing using : " << depId << " " << arrId << endl;
274   departurePort = FGAirport::findByIdent(depId);
275   if(departurePort == NULL)
276     {
277       SG_LOG( SG_GENERAL, SG_WARN, "Traffic manager could not find departure airport : " << depId);
278       return false;
279     }
280   arrivalPort = FGAirport::findByIdent(arrId);
281   if(arrivalPort == NULL)
282     {
283       SG_LOG( SG_GENERAL, SG_WARN, "Traffic manager could not find arrival airport   : " << arrId);
284       return false;
285     }
286
287   //cerr << "Found : " << departurePort->getId() << endl;
288   //cerr << "Found : " << arrivalPort->getId() << endl;
289   initialized = true;
290   return true;
291 }
292
293
294 bool compareScheduledFlights(FGScheduledFlight *a, FGScheduledFlight *b) 
295
296   return (*a) < (*b); 
297 };