1 /******************************************************************************
3 * Written by Durk Talsma, started May 5, 2004.
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.
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.
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.
20 **************************************************************************/
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.
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
31 * 4) Each aircraft leaves at exactly the departure time.
32 * 5) Each aircraft arrives at exactly the specified arrival time.
35 *****************************************************************************/
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>
55 #include <AIModel/AIFlightPlan.hxx>
56 #include <AIModel/AIBase.hxx>
57 #include <Airports/simple.hxx>
58 #include <Main/fg_init.hxx> // That's pretty ugly, but I need fgFindAirportID
62 #include "TrafficMgr.hxx"
65 /******************************************************************************
67 *****************************************************************************/
68 FGTrafficManager::FGTrafficManager()
73 void FGTrafficManager::init()
75 currAircraft = scheduledAircraft.begin();
78 void FGTrafficManager::update(double something)
81 //static const SGPropertyNode *warp = globals->get_props()->getNode("/sim/time/warp");
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);
92 void FGTrafficManager::startXML () {
93 //cout << "Start XML" << endl;
96 void FGTrafficManager::endXML () {
97 //cout << "End XML" << endl;
100 void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
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");
109 //cout << "including " << attval << endl;
111 globals->get_fg_root() +
112 string("/Traffic/") +
114 readXML(path, *this);
116 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
119 void FGTrafficManager::endElement (const char * name) {
120 //cout << "End element " << name << endl;
121 string element(name);
122 if (element == string("model"))
124 else if (element == string("livery"))
126 else if (element == string("registration"))
127 registration = value;
128 else if (element == string("heavy"))
130 if(value == string("true"))
135 else if (element == string("callsign"))
137 else if (element == string("fltrules"))
139 else if (element == string("port"))
141 else if (element == string("time"))
143 else if (element == string("departure"))
145 departurePort = port;
146 departureTime = timeString;
148 else if (element == string("cruise-alt"))
149 cruiseAlt = atoi(value.c_str());
150 else if (element == string("arrival"))
153 arrivalTime = timeString;
155 else if (element == string("repeat"))
157 else if (element == string("flight"))
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,
173 else if (element == string("aircraft"))
175 //cerr << "Pushing back aircraft " << registration << endl;
176 scheduledAircraft.push_back(FGAISchedule(mdl,
181 while(flights.begin() != flights.end())
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))
195 void FGTrafficManager::pi (const char * target, const char * data) {
196 //cout << "Processing instruction " << target << ' ' << data << endl;
199 void FGTrafficManager::warning (const char * message, int line, int column) {
200 cout << "Warning: " << message << " (" << line << ',' << column << ')'
204 void FGTrafficManager::error (const char * message, int line, int column) {
205 cout << "Error: " << message << " (" << line << ',' << column << ')'