]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/TrafficMgr.cxx
new FSF address
[flightgear.git] / src / Traffic / TrafficMgr.cxx
1 /******************************************************************************
2  * TrafficMGr.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  *
35  *****************************************************************************/
36
37 #ifdef HAVE_CONFIG_H
38 #  include "config.h"
39 #endif
40
41 #include <stdlib.h>
42 #include <time.h>
43 #include <iostream>
44 #include <fstream>
45
46
47 #include <string>
48 #include <vector>
49 #include <algorithm>
50
51 #include <plib/sg.h>
52
53 #include <simgear/compiler.h>
54 #include <simgear/math/polar3d.hxx>
55 #include <simgear/math/sg_geodesy.hxx>
56 #include <simgear/misc/sg_path.hxx>
57 #include <simgear/props/props.hxx>
58 #include <simgear/route/waypoint.hxx>
59 #include <simgear/structure/subsystem_mgr.hxx>
60 #include <simgear/xml/easyxml.hxx>
61
62 #include <AIModel/AIAircraft.hxx>
63 #include <AIModel/AIFlightPlan.hxx>
64 #include <AIModel/AIBase.hxx>
65 #include <Airports/simple.hxx>
66 #include <Main/fg_init.hxx>   // That's pretty ugly, but I need fgFindAirportID
67
68
69
70 #include "TrafficMgr.hxx"
71
72 SG_USING_STD(sort);
73  
74 /******************************************************************************
75  * TrafficManager
76  *****************************************************************************/
77 FGTrafficManager::FGTrafficManager()
78 {
79 }
80
81
82 void FGTrafficManager::init()
83
84   //cerr << "Initializing Schedules" << endl;
85   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
86   currAircraft = scheduledAircraft.begin();
87   while (currAircraft != scheduledAircraft.end())
88     {
89       if (!(currAircraft->init()))
90         {
91           currAircraft=scheduledAircraft.erase(currAircraft);
92           //cerr << "Erasing " << currAircraft->getRegistration() << endl;
93         }
94       else 
95         {
96           currAircraft++;
97         }
98     }
99   //cerr << "Sorting by distance " << endl;
100   sort(scheduledAircraft.begin(), scheduledAircraft.end());
101   currAircraft = scheduledAircraft.begin();
102   currAircraftClosest = scheduledAircraft.begin();
103   //cerr << "Done initializing schedules" << endl;
104 }
105
106 void FGTrafficManager::update(double something)
107 {
108   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
109   if (scheduledAircraft.size() == 0)
110           return;
111   if(currAircraft == scheduledAircraft.end())
112     {
113       //cerr << "resetting schedule " << endl;
114       currAircraft = scheduledAircraft.begin();
115     }
116   if (!(currAircraft->update(now)))
117     {
118       // after proper initialization, we shouldnt get here.
119       // But let's make sure
120       cerr << "Failed to update aircraft schedule in traffic manager" << endl;
121     }
122   currAircraft++;
123 }
124
125 void FGTrafficManager::release(int id)
126 {
127   releaseList.push_back(id);
128 }
129
130 bool FGTrafficManager::isReleased(int id)
131 {
132   IdListIterator i = releaseList.begin();
133   while (i != releaseList.end())
134     {
135       if ((*i) == id)
136         {
137           releaseList.erase(i);
138           return true;
139         }
140       i++;
141     }
142   return false;
143 }
144
145 void  FGTrafficManager::startXML () {
146   //cout << "Start XML" << endl;
147 }
148
149 void  FGTrafficManager::endXML () {
150   //cout << "End XML" << endl;
151 }
152
153 void  FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
154   const char * attval;
155   //cout << "Start element " << name << endl;
156   //FGTrafficManager temp;
157   //for (int i = 0; i < atts.size(); i++)
158   //  if (string(atts.getName(i)) == string("include"))
159   attval = atts.getValue("include");
160   if (attval != 0)
161       {
162         //cout << "including " << attval << endl;
163         SGPath path = 
164           globals->get_fg_root();
165         path.append("/Traffic/");
166         path.append(attval);
167         readXML(path.str(), *this);
168       }
169   //  cout << "  " << atts.getName(i) << '=' << atts.getValue(i) << endl; 
170 }
171
172 void  FGTrafficManager::endElement (const char * name) {
173   //cout << "End element " << name << endl;
174   string element(name);
175   if (element == string("model"))
176     mdl = value;
177   else if (element == string("livery"))
178     livery = value;
179   else if (element == string("registration"))
180     registration = value;
181   else if (element == string("airline"))
182     airline = value;
183   else if (element == string("actype"))
184     acType = value;
185   else if (element == string("flighttype"))
186     flighttype = value;
187   else if (element == string("radius"))
188     radius = atoi(value.c_str());
189   else if (element == string("offset"))
190     offset = atoi(value.c_str());
191   else if (element == string("performance-class"))
192     m_class = value;
193   else if (element == string("heavy"))
194     {
195       if(value == string("true"))
196         heavy = true;
197       else
198         heavy = false;
199     }
200   else if (element == string("callsign"))
201     callsign = value;
202   else if (element == string("fltrules"))
203     fltrules = value;
204   else if (element == string("port"))
205     port = value;
206   else if (element == string("time"))
207     timeString = value;
208   else if (element == string("departure"))
209     {
210       departurePort = port;
211       departureTime = timeString;
212     }
213   else if (element == string("cruise-alt"))
214     cruiseAlt = atoi(value.c_str());
215   else if (element == string("arrival"))
216     {
217       arrivalPort = port;
218       arrivalTime = timeString;
219     }
220   else if (element == string("repeat"))
221     repeat = value;
222   else if (element == string("flight"))
223     {
224       // We have loaded and parsed all the information belonging to this flight
225       // so we temporarily store it. 
226       //cerr << "Pusing back flight " << callsign << endl;
227       //cerr << callsign  <<  " " << fltrules     << " "<< departurePort << " " <<  arrivalPort << " "
228       //   << cruiseAlt <<  " " << departureTime<< " "<< arrivalTime   << " " << repeat << endl;
229       flights.push_back(FGScheduledFlight(callsign,
230                                           fltrules,
231                                           departurePort,
232                                           arrivalPort,
233                                           cruiseAlt,
234                                           departureTime,
235                                           arrivalTime,
236                                           repeat));
237     }
238   else if (element == string("aircraft"))
239     {
240       //cerr << "Pushing back aircraft " << registration << endl;
241       scheduledAircraft.push_back(FGAISchedule(mdl, 
242                                                livery, 
243                                                registration, 
244                                                heavy,
245                                                acType, 
246                                                airline, 
247                                                m_class, 
248                                                flighttype,
249                                                radius,
250                                                offset,
251                                                flights));
252       while(flights.begin() != flights.end())
253         flights.pop_back();
254     }
255 }
256
257 void  FGTrafficManager::data (const char * s, int len) {
258   string token = string(s,len);
259   //cout << "Character data " << string(s,len) << endl;
260   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
261       value += token;
262   else
263     value = string("");
264 }
265
266 void  FGTrafficManager::pi (const char * target, const char * data) {
267   //cout << "Processing instruction " << target << ' ' << data << endl;
268 }
269
270 void  FGTrafficManager::warning (const char * message, int line, int column) {
271   cout << "Warning: " << message << " (" << line << ',' << column << ')'   
272        << endl;
273 }
274
275 void  FGTrafficManager::error (const char * message, int line, int column) {
276   cout << "Error: " << message << " (" << line << ',' << column << ')'
277        << endl;
278 }