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