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