]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/TrafficMgr.cxx
Maik JUSTUS: last changes for better consistency with The FlightGear Way.
[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>
67
68
69
70 #include "TrafficMgr.hxx"
71
72 SG_USING_STD(sort);
73  
74 /******************************************************************************
75  * TrafficManager
76  *****************************************************************************/
77 FGTrafficManager::FGTrafficManager()
78 {
79   score = 0;
80 }
81
82 FGTrafficManager:: ~FGTrafficManager()
83 {
84   for (ScheduleVectorIterator sched = scheduledAircraft.begin(); sched != scheduledAircraft.end(); sched++)
85     {
86       delete (*sched);
87     }
88   scheduledAircraft.clear();
89   // for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
90 //     {
91 //       delete (*flt);
92 //     }
93 //   flights.clear();
94 }
95
96
97 void FGTrafficManager::init()
98
99   //cerr << "Initializing Schedules" << endl;
100   //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
101   //currAircraft = scheduledAircraft.begin();
102   //while (currAircraft != scheduledAircraft.end())
103   //  {
104   //    if (!(currAircraft->init()))
105   //    {
106   //      currAircraft=scheduledAircraft.erase(currAircraft);
107   //      //cerr << "Erasing " << currAircraft->getRegistration() << endl;
108   //    }
109   //   else 
110   //    {
111   //      currAircraft++;
112   //    }
113   //   }
114   // Sort by points: Aircraft with frequent visits to the
115   // startup airport will be processed first
116   sort(scheduledAircraft.begin(), scheduledAircraft.end(), compareSchedules);
117   currAircraft = scheduledAircraft.begin();
118   currAircraftClosest = scheduledAircraft.begin();
119   //cerr << "Done initializing schedules" << endl;
120 }
121
122 void FGTrafficManager::update(double something)
123 {
124   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
125   if (scheduledAircraft.size() == 0)
126     return;
127   if(currAircraft == scheduledAircraft.end())
128     {
129       //cerr << "resetting schedule " << endl;
130       currAircraft = scheduledAircraft.begin();
131     }
132   if (!((*currAircraft)->update(now)))
133     {
134       // after proper initialization, we shouldnt get here.
135       // But let's make sure
136       cerr << "Failed to update aircraft schedule in traffic manager" << endl;
137     }
138   currAircraft++;
139 }
140
141 void FGTrafficManager::release(int id)
142 {
143   releaseList.push_back(id);
144 }
145
146 bool FGTrafficManager::isReleased(int id)
147 {
148   IdListIterator i = releaseList.begin();
149   while (i != releaseList.end())
150     {
151       if ((*i) == id)
152         {
153           releaseList.erase(i);
154           return true;
155         }
156       i++;
157     }
158   return false;
159 }
160
161 void  FGTrafficManager::startXML () {
162   //cout << "Start XML" << endl;
163 }
164
165 void  FGTrafficManager::endXML () {
166   //cout << "End XML" << endl;
167 }
168
169 void  FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
170   const char * attval;
171   //cout << "Start element " << name << endl;
172   //FGTrafficManager temp;
173   //for (int i = 0; i < atts.size(); i++)
174   //  if (string(atts.getName(i)) == string("include"))
175   attval = atts.getValue("include");
176   if (attval != 0)
177       {
178         //cout << "including " << attval << endl;
179         SGPath path = 
180           globals->get_fg_root();
181         path.append("/Traffic/");
182         path.append(attval);
183         readXML(path.str(), *this);
184       }
185   //  cout << "  " << atts.getName(i) << '=' << atts.getValue(i) << endl; 
186 }
187
188 void  FGTrafficManager::endElement (const char * name) {
189   //cout << "End element " << name << endl;
190   string element(name);
191   if (element == string("model"))
192     mdl = value;
193   else if (element == string("livery"))
194     livery = value;
195   else if (element == string("registration"))
196     registration = value;
197   else if (element == string("airline"))
198     airline = value;
199   else if (element == string("actype"))
200     acType = value;
201   else if (element == string("flighttype"))
202     flighttype = value;
203   else if (element == string("radius"))
204     radius = atoi(value.c_str());
205   else if (element == string("offset"))
206     offset = atoi(value.c_str());
207   else if (element == string("performance-class"))
208     m_class = value;
209   else if (element == string("heavy"))
210     {
211       if(value == string("true"))
212         heavy = true;
213       else
214         heavy = false;
215     }
216   else if (element == string("callsign"))
217     callsign = value;
218   else if (element == string("fltrules"))
219     fltrules = value;
220   else if (element == string("port"))
221     port = value;
222   else if (element == string("time"))
223     timeString = value;
224   else if (element == string("departure"))
225     {
226       departurePort = port;
227       departureTime = timeString;
228     }
229   else if (element == string("cruise-alt"))
230     cruiseAlt = atoi(value.c_str());
231   else if (element == string("arrival"))
232     {
233       arrivalPort = port;
234       arrivalTime = timeString;
235     }
236   else if (element == string("repeat"))
237     repeat = value;
238   else if (element == string("flight"))
239     {
240       // We have loaded and parsed all the information belonging to this flight
241       // so we temporarily store it. 
242       //cerr << "Pusing back flight " << callsign << endl;
243       //cerr << callsign  <<  " " << fltrules     << " "<< departurePort << " " <<  arrivalPort << " "
244       //   << cruiseAlt <<  " " << departureTime<< " "<< arrivalTime   << " " << repeat << endl;
245
246       //Prioritize aircraft 
247       string apt = fgGetString("/sim/presets/airport-id");
248       //cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl;
249       if (departurePort == apt) score++;
250       flights.push_back(new FGScheduledFlight(callsign,
251                                           fltrules,
252                                           departurePort,
253                                           arrivalPort,
254                                           cruiseAlt,
255                                           departureTime,
256                                           arrivalTime,
257                                           repeat));
258     }
259   else if (element == string("aircraft"))
260     {
261       //cerr << "Pushing back aircraft " << registration << endl;
262       scheduledAircraft.push_back(new FGAISchedule(mdl, 
263                                                livery, 
264                                                registration, 
265                                                heavy,
266                                                acType, 
267                                                airline, 
268                                                m_class, 
269                                                flighttype,
270                                                radius,
271                                                offset,
272                                                score,
273                                                flights));
274      //  while(flights.begin() != flights.end()) {
275 //      flights.pop_back();
276 //       }
277       for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
278     {
279       delete (*flt);
280     }
281   flights.clear();
282       SG_LOG( SG_GENERAL, SG_BULK, "Reading aircraft : " 
283               << registration 
284               << " with prioritization score " 
285               << score);
286       score = 0;
287     }
288 }
289
290 void  FGTrafficManager::data (const char * s, int len) {
291   string token = string(s,len);
292   //cout << "Character data " << string(s,len) << endl;
293   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
294       value += token;
295   else
296     value = string("");
297 }
298
299 void  FGTrafficManager::pi (const char * target, const char * data) {
300   //cout << "Processing instruction " << target << ' ' << data << endl;
301 }
302
303 void  FGTrafficManager::warning (const char * message, int line, int column) {
304   SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
305 }
306
307 void  FGTrafficManager::error (const char * message, int line, int column) {
308   SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');
309 }