]> git.mxchange.org Git - flightgear.git/blob - src/Traffic/TrafficMgr.cxx
Fix a numeric_limits problem for older stdc++ libraries.
[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/props/props.hxx>
51 #include <simgear/route/waypoint.hxx>
52 #include <simgear/structure/subsystem_mgr.hxx>
53 #include <simgear/xml/easyxml.hxx>
54
55 #include <AIModel/AIFlightPlan.hxx>
56 #include <AIModel/AIManager.hxx>
57 #include <Airports/simple.hxx>
58 #include <Main/fg_init.hxx>   // That's pretty ugly, but I need fgFindAirportID
59
60
61
62 #include "TrafficMgr.hxx"
63
64  
65 /******************************************************************************
66  * TrafficManager
67  *****************************************************************************/
68 FGTrafficManager::FGTrafficManager()
69 {
70 }
71
72
73 void FGTrafficManager::init()
74 {
75   currAircraft = scheduledAircraft.begin();
76 }
77
78 void FGTrafficManager::update(double something)
79 {
80
81   //static const SGPropertyNode *warp = globals->get_props()->getNode("/sim/time/warp");
82   
83   //time_t now = time(NULL) + globals->get_warp();
84   time_t now = time(NULL) + fgGetLong("/sim/time/warp");
85   //  cerr << "TrafficManager update" << globals->get_warp() << endl;
86     if(currAircraft == scheduledAircraft.end())
87       currAircraft = scheduledAircraft.begin();
88     currAircraft->update(now);
89     currAircraft++;
90 }
91
92 void  FGTrafficManager::startXML () {
93   //cout << "Start XML" << endl;
94 }
95
96 void  FGTrafficManager::endXML () {
97   //cout << "End XML" << endl;
98 }
99
100 void  FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
101   const char * attval;
102   //cout << "Start element " << name << endl;
103   //FGTrafficManager temp;
104   //for (int i = 0; i < atts.size(); i++)
105   //  if (string(atts.getName(i)) == string("include"))
106   attval = atts.getValue("include");
107   if (attval != 0)
108       {
109         //cout << "including " << attval << endl;
110         string path = 
111           globals->get_fg_root()   + 
112           string("/Traffic/")      + 
113           string(attval);
114         readXML(path, *this);
115       }
116   //  cout << "  " << atts.getName(i) << '=' << atts.getValue(i) << endl; 
117 }
118
119 void  FGTrafficManager::endElement (const char * name) {
120   //cout << "End element " << name << endl;
121   string element(name);
122   if (element == string("model"))
123     mdl = value;
124   else if (element == string("livery"))
125     livery = value;
126   else if (element == string("registration"))
127     registration = value;
128   else if (element == string("heavy"))
129     {
130       if(value == string("true"))
131         heavy = true;
132       else
133         heavy = false;
134     }
135   else if (element == string("callsign"))
136     callsign = value;
137   else if (element == string("fltrules"))
138     fltrules = value;
139   else if (element == string("port"))
140     port = value;
141   else if (element == string("time"))
142     timeString = value;
143   else if (element == string("departure"))
144     {
145       departurePort = port;
146       departureTime = timeString;
147     }
148   else if (element == string("cruise-alt"))
149     cruiseAlt = atoi(value.c_str());
150   else if (element == string("arrival"))
151     {
152       arrivalPort = port;
153       arrivalTime = timeString;
154     }
155   else if (element == string("repeat"))
156     repeat = value;
157   else if (element == string("flight"))
158     {
159       // We have loaded and parsed all the information belonging to this flight
160       // so we temporarily store it. 
161       //cerr << "Pusing back flight " << callsign << endl;
162       //cerr << callsign  <<  " " << fltrules     << " "<< departurePort << " " <<  arrivalPort << " "
163       //   << cruiseAlt <<  " " << departureTime<< " "<< arrivalTime   << " " << repeat << endl;
164       flights.push_back(FGScheduledFlight(callsign,
165                                           fltrules,
166                                           departurePort,
167                                           arrivalPort,
168                                           cruiseAlt,
169                                           departureTime,
170                                           arrivalTime,
171                                           repeat));
172     }
173   else if (element == string("aircraft"))
174     {
175       //cerr << "Pushing back aircraft " << registration << endl;
176       scheduledAircraft.push_back(FGAISchedule(mdl, 
177                                                 livery, 
178                                                 registration, 
179                                                 heavy, 
180                                                 flights));
181       while(flights.begin() != flights.end())
182         flights.pop_back();
183     }
184 }
185
186 void  FGTrafficManager::data (const char * s, int len) {
187   string token = string(s,len);
188   //cout << "Character data " << string(s,len) << endl;
189   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
190       value += token;
191   else
192     value = string("");
193 }
194
195 void  FGTrafficManager::pi (const char * target, const char * data) {
196   //cout << "Processing instruction " << target << ' ' << data << endl;
197 }
198
199 void  FGTrafficManager::warning (const char * message, int line, int column) {
200   cout << "Warning: " << message << " (" << line << ',' << column << ')'   
201        << endl;
202 }
203
204 void  FGTrafficManager::error (const char * message, int line, int column) {
205   cout << "Error: " << message << " (" << line << ',' << column << ')'
206        << endl;
207 }