]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/TrafficMgr.cxx
Remove references to SceneView and CameraView
[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 #include <plib/ul.h>
53
54 #include <simgear/compiler.h>
55 #include <simgear/math/polar3d.hxx>
56 #include <simgear/math/sg_geodesy.hxx>
57 #include <simgear/misc/sg_path.hxx>
58 #include <simgear/props/props.hxx>
59 #include <simgear/route/waypoint.hxx>
60 #include <simgear/structure/subsystem_mgr.hxx>
61 #include <simgear/xml/easyxml.hxx>
62
63 #include <AIModel/AIAircraft.hxx>
64 #include <AIModel/AIFlightPlan.hxx>
65 #include <AIModel/AIBase.hxx>
66 #include <Airports/simple.hxx>
67 #include <Main/fg_init.hxx>
68
69
70
71 #include "TrafficMgr.hxx"
72
73 SG_USING_STD(sort);
74  
75 /******************************************************************************
76  * TrafficManager
77  *****************************************************************************/
78 FGTrafficManager::FGTrafficManager()
79 {
80   score = 0;
81   runCount = 0;
82 }
83
84 FGTrafficManager:: ~FGTrafficManager()
85 {
86   for (ScheduleVectorIterator sched = scheduledAircraft.begin(); sched != scheduledAircraft.end(); sched++)
87     {
88       delete (*sched);
89     }
90   scheduledAircraft.clear();
91   // for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
92 //     {
93 //       delete (*flt);
94 //     }
95 //   flights.clear();
96 }
97
98
99 void FGTrafficManager::init()
100
101   //cerr << "Initializing Schedules" << endl;
102   //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
103   //currAircraft = scheduledAircraft.begin();
104   //while (currAircraft != scheduledAircraft.end())
105   //  {
106   //    if (!(currAircraft->init()))
107   //    {
108   //      currAircraft=scheduledAircraft.erase(currAircraft);
109   //      //cerr << "Erasing " << currAircraft->getRegistration() << endl;
110   //    }
111   //   else 
112   //    {
113   //      currAircraft++;
114   //    }
115   //   }
116   // Sort by points: Aircraft with frequent visits to the
117   // startup airport will be processed first
118   ulDir* d, *d2;
119   ulDirEnt* dent, *dent2;
120   SGPath aircraftDir = globals->get_fg_root();
121
122   /* keep the following three lines (which mimicks the old "fixed path" behavior)
123    * until we have some AI models with traffic in the base package
124    */ 
125   SGPath path = aircraftDir;
126   path.append("Traffic/fgtraffic.xml");
127   if (path.exists())
128     readXML(path.str(),*this);
129
130   aircraftDir.append("AI/Aircraft");
131   if ((d = ulOpenDir(aircraftDir.c_str())) != NULL)
132     {
133       while((dent = ulReadDir(d)) != NULL) {
134         //cerr << "Scanning : " << dent->d_name << endl;
135         if (string(dent->d_name) != string(".")  && 
136             string(dent->d_name) != string("..") &&
137             dent->d_isdir)
138           {
139             SGPath currACDir = aircraftDir;
140             currACDir.append(dent->d_name);
141             if ((d2 = ulOpenDir(currACDir.c_str())) == NULL)
142               return;
143             while ((dent2 = ulReadDir(d2)) != NULL) {
144               SGPath currFile = currACDir;
145               currFile.append(dent2->d_name);
146               if (currFile.extension() == string("xml"))
147                 {
148                   //cerr << "found " << dent2->d_name << " for parsing" << endl;
149                   SGPath currFile = currACDir;
150                   currFile.append(dent2->d_name);
151                   SG_LOG(SG_GENERAL, SG_INFO, "Scanning " << currFile.str() << " for traffic");
152                   readXML(currFile.str(),*this);
153                 }
154             }
155             ulCloseDir(d2);
156           }
157       }
158       ulCloseDir(d);
159     }
160   // Sort by points: Aircraft with frequent visits to the
161   // startup airport will be processed first
162   sort(scheduledAircraft.begin(), scheduledAircraft.end(), compareSchedules);
163   currAircraft = scheduledAircraft.begin();
164   currAircraftClosest = scheduledAircraft.begin();
165   //cerr << "Done initializing schedules" << endl;
166 }
167
168 void FGTrafficManager::update(double /*dt*/)
169 {
170   //SG_LOG( SG_GENERAL, SG_INFO, "Running TrafficManager::Update() ");
171   // Hack alert: Skip running for the first frames 1000 after 
172   // initialization to allow proper initialization of wheather stuff 
173   // and runway assignments
174   if (runCount < 1000)
175     {
176       runCount++;
177       return;
178     }
179   //runCount = 0;
180   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
181   if (scheduledAircraft.size() == 0) {
182     //SG_LOG( SG_GENERAL, SG_INFO, "Returned Running TrafficManager::Update() ");
183     return;
184   }
185   if(currAircraft == scheduledAircraft.end())
186     {
187       //cerr << "resetting schedule " << endl;
188       currAircraft = scheduledAircraft.begin();
189     }
190   if (!((*currAircraft)->update(now)))
191     {
192       // after proper initialization, we shouldnt get here.
193       // But let's make sure
194       SG_LOG( SG_GENERAL, SG_ALERT, "Failed to update aircraft schedule in traffic manager");
195     }
196   currAircraft++;
197   //SG_LOG( SG_GENERAL, SG_INFO, "Done Running TrafficManager::Update() ");
198 }
199
200 void FGTrafficManager::release(int id)
201 {
202   releaseList.push_back(id);
203 }
204
205 bool FGTrafficManager::isReleased(int id)
206 {
207   IdListIterator i = releaseList.begin();
208   while (i != releaseList.end())
209     {
210       if ((*i) == id)
211         {
212           releaseList.erase(i);
213           return true;
214         }
215       i++;
216     }
217   return false;
218 }
219
220 void  FGTrafficManager::startXML () {
221   //cout << "Start XML" << endl;
222 }
223
224 void  FGTrafficManager::endXML () {
225   //cout << "End XML" << endl;
226 }
227
228 void  FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
229   const char * attval;
230   //cout << "Start element " << name << endl;
231   //FGTrafficManager temp;
232   //for (int i = 0; i < atts.size(); i++)
233   //  if (string(atts.getName(i)) == string("include"))
234   attval = atts.getValue("include");
235   if (attval != 0)
236       {
237         //cout << "including " << attval << endl;
238         SGPath path = 
239           globals->get_fg_root();
240         path.append("/Traffic/");
241         path.append(attval);
242         readXML(path.str(), *this);
243       }
244   //  cout << "  " << atts.getName(i) << '=' << atts.getValue(i) << endl; 
245 }
246
247 void  FGTrafficManager::endElement (const char * name) {
248   //cout << "End element " << name << endl;
249   string element(name);
250   if (element == string("model"))
251     mdl = value;
252   else if (element == string("livery"))
253     livery = value;
254   else if (element == string("registration"))
255     registration = value;
256   else if (element == string("airline"))
257     airline = value;
258   else if (element == string("actype"))
259     acType = value;
260   else if (element == string("flighttype"))
261     flighttype = value;
262   else if (element == string("radius"))
263     radius = atoi(value.c_str());
264   else if (element == string("offset"))
265     offset = atoi(value.c_str());
266   else if (element == string("performance-class"))
267     m_class = value;
268   else if (element == string("heavy"))
269     {
270       if(value == string("true"))
271         heavy = true;
272       else
273         heavy = false;
274     }
275   else if (element == string("callsign"))
276     callsign = value;
277   else if (element == string("fltrules"))
278     fltrules = value;
279   else if (element == string("port"))
280     port = value;
281   else if (element == string("time"))
282     timeString = value;
283   else if (element == string("departure"))
284     {
285       departurePort = port;
286       departureTime = timeString;
287     }
288   else if (element == string("cruise-alt"))
289     cruiseAlt = atoi(value.c_str());
290   else if (element == string("arrival"))
291     {
292       arrivalPort = port;
293       arrivalTime = timeString;
294     }
295   else if (element == string("repeat"))
296     repeat = value;
297   else if (element == string("flight"))
298     {
299       // We have loaded and parsed all the information belonging to this flight
300       // so we temporarily store it. 
301       //cerr << "Pusing back flight " << callsign << endl;
302       //cerr << callsign  <<  " " << fltrules     << " "<< departurePort << " " <<  arrivalPort << " "
303       //   << cruiseAlt <<  " " << departureTime<< " "<< arrivalTime   << " " << repeat << endl;
304
305       //Prioritize aircraft 
306       string apt = fgGetString("/sim/presets/airport-id");
307       //cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl;
308       if (departurePort == apt) score++;
309       flights.push_back(new FGScheduledFlight(callsign,
310                                           fltrules,
311                                           departurePort,
312                                           arrivalPort,
313                                           cruiseAlt,
314                                           departureTime,
315                                           arrivalTime,
316                                           repeat));
317     }
318   else if (element == string("aircraft"))
319     {
320       //cerr << "Pushing back aircraft " << registration << endl;
321       scheduledAircraft.push_back(new FGAISchedule(mdl, 
322                                                livery, 
323                                                registration, 
324                                                heavy,
325                                                acType, 
326                                                airline, 
327                                                m_class, 
328                                                flighttype,
329                                                radius,
330                                                offset,
331                                                score,
332                                                flights));
333      //  while(flights.begin() != flights.end()) {
334 //      flights.pop_back();
335 //       }
336       for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
337     {
338       delete (*flt);
339     }
340   flights.clear();
341       SG_LOG( SG_GENERAL, SG_BULK, "Reading aircraft : " 
342               << registration 
343               << " with prioritization score " 
344               << score);
345       score = 0;
346     }
347 }
348
349 void  FGTrafficManager::data (const char * s, int len) {
350   string token = string(s,len);
351   //cout << "Character data " << string(s,len) << endl;
352   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
353       value += token;
354   else
355     value = string("");
356 }
357
358 void  FGTrafficManager::pi (const char * target, const char * data) {
359   //cout << "Processing instruction " << target << ' ' << data << endl;
360 }
361
362 void  FGTrafficManager::warning (const char * message, int line, int column) {
363   SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
364 }
365
366 void  FGTrafficManager::error (const char * message, int line, int column) {
367   SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');
368 }