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::release(void *id)
94 releaseList.push_back(id);
97 bool FGTrafficManager::isReleased(void *id)
99 IdListIterator i = releaseList.begin();
100 while (i != releaseList.end())
104 releaseList.erase(i);
112 void FGTrafficManager::startXML () {
113 //cout << "Start XML" << endl;
116 void FGTrafficManager::endXML () {
117 //cout << "End XML" << endl;
120 void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
122 //cout << "Start element " << name << endl;
123 //FGTrafficManager temp;
124 //for (int i = 0; i < atts.size(); i++)
125 // if (string(atts.getName(i)) == string("include"))
126 attval = atts.getValue("include");
129 //cout << "including " << attval << endl;
131 globals->get_fg_root() +
132 string("/Traffic/") +
134 readXML(path, *this);
136 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
139 void FGTrafficManager::endElement (const char * name) {
140 //cout << "End element " << name << endl;
141 string element(name);
142 if (element == string("model"))
144 else if (element == string("livery"))
146 else if (element == string("registration"))
147 registration = value;
148 else if (element == string("heavy"))
150 if(value == string("true"))
155 else if (element == string("callsign"))
157 else if (element == string("fltrules"))
159 else if (element == string("port"))
161 else if (element == string("time"))
163 else if (element == string("departure"))
165 departurePort = port;
166 departureTime = timeString;
168 else if (element == string("cruise-alt"))
169 cruiseAlt = atoi(value.c_str());
170 else if (element == string("arrival"))
173 arrivalTime = timeString;
175 else if (element == string("repeat"))
177 else if (element == string("flight"))
179 // We have loaded and parsed all the information belonging to this flight
180 // so we temporarily store it.
181 //cerr << "Pusing back flight " << callsign << endl;
182 //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
183 // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
184 flights.push_back(FGScheduledFlight(callsign,
193 else if (element == string("aircraft"))
195 //cerr << "Pushing back aircraft " << registration << endl;
196 scheduledAircraft.push_back(FGAISchedule(mdl,
201 while(flights.begin() != flights.end())
206 void FGTrafficManager::data (const char * s, int len) {
207 string token = string(s,len);
208 //cout << "Character data " << string(s,len) << endl;
209 if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
215 void FGTrafficManager::pi (const char * target, const char * data) {
216 //cout << "Processing instruction " << target << ' ' << data << endl;
219 void FGTrafficManager::warning (const char * message, int line, int column) {
220 cout << "Warning: " << message << " (" << line << ',' << column << ')'
224 void FGTrafficManager::error (const char * message, int line, int column) {
225 cout << "Error: " << message << " (" << line << ',' << column << ')'