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 *****************************************************************************/
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>
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> // That's pretty ugly, but I need fgFindAirportID
70 #include "TrafficMgr.hxx"
74 /******************************************************************************
76 *****************************************************************************/
77 FGTrafficManager::FGTrafficManager()
82 void FGTrafficManager::init()
84 //cerr << "Initializing Schedules" << endl;
85 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
86 currAircraft = scheduledAircraft.begin();
87 while (currAircraft != scheduledAircraft.end())
89 if (!(currAircraft->init()))
91 currAircraft=scheduledAircraft.erase(currAircraft);
92 //cerr << "Erasing " << currAircraft->getRegistration() << endl;
99 //cerr << "Sorting by distance " << endl;
100 sort(scheduledAircraft.begin(), scheduledAircraft.end());
101 currAircraft = scheduledAircraft.begin();
102 currAircraftClosest = scheduledAircraft.begin();
103 //cerr << "Done initializing schedules" << endl;
106 void FGTrafficManager::update(double something)
108 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
109 if (scheduledAircraft.size() == 0)
111 if(currAircraft == scheduledAircraft.end())
113 //cerr << "resetting schedule " << endl;
114 currAircraft = scheduledAircraft.begin();
116 if (!(currAircraft->update(now)))
118 // after proper initialization, we shouldnt get here.
119 // But let's make sure
120 cerr << "Failed to update aircraft schedule in traffic manager" << endl;
125 void FGTrafficManager::release(int id)
127 releaseList.push_back(id);
130 bool FGTrafficManager::isReleased(int id)
132 IdListIterator i = releaseList.begin();
133 while (i != releaseList.end())
137 releaseList.erase(i);
145 void FGTrafficManager::startXML () {
146 //cout << "Start XML" << endl;
149 void FGTrafficManager::endXML () {
150 //cout << "End XML" << endl;
153 void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
155 //cout << "Start element " << name << endl;
156 //FGTrafficManager temp;
157 //for (int i = 0; i < atts.size(); i++)
158 // if (string(atts.getName(i)) == string("include"))
159 attval = atts.getValue("include");
162 //cout << "including " << attval << endl;
164 globals->get_fg_root();
165 path.append("/Traffic/");
167 readXML(path.str(), *this);
169 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
172 void FGTrafficManager::endElement (const char * name) {
173 //cout << "End element " << name << endl;
174 string element(name);
175 if (element == string("model"))
177 else if (element == string("livery"))
179 else if (element == string("registration"))
180 registration = value;
181 else if (element == string("airline"))
183 else if (element == string("actype"))
185 else if (element == string("flighttype"))
187 else if (element == string("radius"))
188 radius = atoi(value.c_str());
189 else if (element == string("offset"))
190 offset = atoi(value.c_str());
191 else if (element == string("performance-class"))
193 else if (element == string("heavy"))
195 if(value == string("true"))
200 else if (element == string("callsign"))
202 else if (element == string("fltrules"))
204 else if (element == string("port"))
206 else if (element == string("time"))
208 else if (element == string("departure"))
210 departurePort = port;
211 departureTime = timeString;
213 else if (element == string("cruise-alt"))
214 cruiseAlt = atoi(value.c_str());
215 else if (element == string("arrival"))
218 arrivalTime = timeString;
220 else if (element == string("repeat"))
222 else if (element == string("flight"))
224 // We have loaded and parsed all the information belonging to this flight
225 // so we temporarily store it.
226 //cerr << "Pusing back flight " << callsign << endl;
227 //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
228 // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
229 flights.push_back(FGScheduledFlight(callsign,
238 else if (element == string("aircraft"))
240 //cerr << "Pushing back aircraft " << registration << endl;
241 scheduledAircraft.push_back(FGAISchedule(mdl,
252 while(flights.begin() != flights.end())
257 void FGTrafficManager::data (const char * s, int len) {
258 string token = string(s,len);
259 //cout << "Character data " << string(s,len) << endl;
260 if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
266 void FGTrafficManager::pi (const char * target, const char * data) {
267 //cout << "Processing instruction " << target << ' ' << data << endl;
270 void FGTrafficManager::warning (const char * message, int line, int column) {
271 cout << "Warning: " << message << " (" << line << ',' << column << ')'
275 void FGTrafficManager::error (const char * message, int line, int column) {
276 cout << "Error: " << message << " (" << line << ',' << column << ')'