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