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