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