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