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 *****************************************************************************/
48 #include <simgear/compiler.h>
49 #include <simgear/math/polar3d.hxx>
50 #include <simgear/math/sg_geodesy.hxx>
51 #include <simgear/misc/sg_path.hxx>
52 #include <simgear/props/props.hxx>
53 #include <simgear/route/waypoint.hxx>
54 #include <simgear/structure/subsystem_mgr.hxx>
55 #include <simgear/xml/easyxml.hxx>
57 #include <AIModel/AIAircraft.hxx>
58 #include <AIModel/AIFlightPlan.hxx>
59 #include <AIModel/AIBase.hxx>
60 #include <Airports/simple.hxx>
61 #include <Main/fg_init.hxx> // That's pretty ugly, but I need fgFindAirportID
65 #include "TrafficMgr.hxx"
69 /******************************************************************************
71 *****************************************************************************/
72 FGTrafficManager::FGTrafficManager()
77 void FGTrafficManager::init()
79 //cerr << "Initializing Schedules" << endl;
80 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
81 currAircraft = scheduledAircraft.begin();
82 while (currAircraft != scheduledAircraft.end())
84 if (!(currAircraft->init()))
86 currAircraft=scheduledAircraft.erase(currAircraft);
87 //cerr << "Erasing " << currAircraft->getRegistration() << endl;
95 //cerr << "Sorting by distance " << endl;
96 sort(scheduledAircraft.begin(), scheduledAircraft.end());
97 currAircraft = scheduledAircraft.begin();
98 currAircraftClosest = scheduledAircraft.begin();
99 //cerr << "Done initializing schedules" << endl;
102 void FGTrafficManager::update(double something)
104 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
105 if(currAircraft == scheduledAircraft.end())
107 //cerr << "resetting schedule " << endl;
108 currAircraft = scheduledAircraft.begin();
110 if (!(currAircraft->update(now)))
112 // after proper initialization, we shouldnt get here.
113 // But let's make sure
114 cerr << "Failed to update aircraft schedule in traffic manager" << endl;
119 void FGTrafficManager::release(int id)
121 releaseList.push_back(id);
124 bool FGTrafficManager::isReleased(int id)
126 IdListIterator i = releaseList.begin();
127 while (i != releaseList.end())
131 releaseList.erase(i);
139 void FGTrafficManager::startXML () {
140 //cout << "Start XML" << endl;
143 void FGTrafficManager::endXML () {
144 //cout << "End XML" << endl;
147 void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
149 //cout << "Start element " << name << endl;
150 //FGTrafficManager temp;
151 //for (int i = 0; i < atts.size(); i++)
152 // if (string(atts.getName(i)) == string("include"))
153 attval = atts.getValue("include");
156 //cout << "including " << attval << endl;
158 globals->get_fg_root();
159 path.append("/Traffic/");
161 readXML(path.str(), *this);
163 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
166 void FGTrafficManager::endElement (const char * name) {
167 //cout << "End element " << name << endl;
168 string element(name);
169 if (element == string("model"))
171 else if (element == string("livery"))
173 else if (element == string("registration"))
174 registration = value;
175 else if (element == string("airline"))
177 else if (element == string("actype"))
179 else if (element == string("flighttype"))
181 else if (element == string("radius"))
182 radius = atoi(value.c_str());
183 else if (element == string("offset"))
184 offset = atoi(value.c_str());
185 else if (element == string("performance-class"))
187 else if (element == string("heavy"))
189 if(value == string("true"))
194 else if (element == string("callsign"))
196 else if (element == string("fltrules"))
198 else if (element == string("port"))
200 else if (element == string("time"))
202 else if (element == string("departure"))
204 departurePort = port;
205 departureTime = timeString;
207 else if (element == string("cruise-alt"))
208 cruiseAlt = atoi(value.c_str());
209 else if (element == string("arrival"))
212 arrivalTime = timeString;
214 else if (element == string("repeat"))
216 else if (element == string("flight"))
218 // We have loaded and parsed all the information belonging to this flight
219 // so we temporarily store it.
220 //cerr << "Pusing back flight " << callsign << endl;
221 //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
222 // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
223 flights.push_back(FGScheduledFlight(callsign,
232 else if (element == string("aircraft"))
234 //cerr << "Pushing back aircraft " << registration << endl;
235 scheduledAircraft.push_back(FGAISchedule(mdl,
246 while(flights.begin() != flights.end())
251 void FGTrafficManager::data (const char * s, int len) {
252 string token = string(s,len);
253 //cout << "Character data " << string(s,len) << endl;
254 if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
260 void FGTrafficManager::pi (const char * target, const char * data) {
261 //cout << "Processing instruction " << target << ' ' << data << endl;
264 void FGTrafficManager::warning (const char * message, int line, int column) {
265 cout << "Warning: " << message << " (" << line << ',' << column << ')'
269 void FGTrafficManager::error (const char * message, int line, int column) {
270 cout << "Error: " << message << " (" << line << ',' << column << ')'