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