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