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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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>
70 #include "TrafficMgr.hxx"
74 /******************************************************************************
76 *****************************************************************************/
77 FGTrafficManager::FGTrafficManager()
83 void FGTrafficManager::init()
85 //cerr << "Initializing Schedules" << endl;
86 //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
87 //currAircraft = scheduledAircraft.begin();
88 //while (currAircraft != scheduledAircraft.end())
90 // if (!(currAircraft->init()))
92 // currAircraft=scheduledAircraft.erase(currAircraft);
93 // //cerr << "Erasing " << currAircraft->getRegistration() << endl;
100 //cerr << "Sorting by distance " << endl;
101 sort(scheduledAircraft.begin(), scheduledAircraft.end());
102 currAircraft = scheduledAircraft.begin();
103 currAircraftClosest = scheduledAircraft.begin();
104 //cerr << "Done initializing schedules" << endl;
107 void FGTrafficManager::update(double something)
109 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
110 if (scheduledAircraft.size() == 0)
112 if(currAircraft == scheduledAircraft.end())
114 //cerr << "resetting schedule " << endl;
115 currAircraft = scheduledAircraft.begin();
117 if (!(currAircraft->update(now)))
119 // after proper initialization, we shouldnt get here.
120 // But let's make sure
121 cerr << "Failed to update aircraft schedule in traffic manager" << endl;
126 void FGTrafficManager::release(int id)
128 releaseList.push_back(id);
131 bool FGTrafficManager::isReleased(int id)
133 IdListIterator i = releaseList.begin();
134 while (i != releaseList.end())
138 releaseList.erase(i);
146 void FGTrafficManager::startXML () {
147 //cout << "Start XML" << endl;
150 void FGTrafficManager::endXML () {
151 //cout << "End XML" << endl;
154 void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
156 //cout << "Start element " << name << endl;
157 //FGTrafficManager temp;
158 //for (int i = 0; i < atts.size(); i++)
159 // if (string(atts.getName(i)) == string("include"))
160 attval = atts.getValue("include");
163 //cout << "including " << attval << endl;
165 globals->get_fg_root();
166 path.append("/Traffic/");
168 readXML(path.str(), *this);
170 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
173 void FGTrafficManager::endElement (const char * name) {
174 //cout << "End element " << name << endl;
175 string element(name);
176 if (element == string("model"))
178 else if (element == string("livery"))
180 else if (element == string("registration"))
181 registration = value;
182 else if (element == string("airline"))
184 else if (element == string("actype"))
186 else if (element == string("flighttype"))
188 else if (element == string("radius"))
189 radius = atoi(value.c_str());
190 else if (element == string("offset"))
191 offset = atoi(value.c_str());
192 else if (element == string("performance-class"))
194 else if (element == string("heavy"))
196 if(value == string("true"))
201 else if (element == string("callsign"))
203 else if (element == string("fltrules"))
205 else if (element == string("port"))
207 else if (element == string("time"))
209 else if (element == string("departure"))
211 departurePort = port;
212 departureTime = timeString;
214 else if (element == string("cruise-alt"))
215 cruiseAlt = atoi(value.c_str());
216 else if (element == string("arrival"))
219 arrivalTime = timeString;
221 else if (element == string("repeat"))
223 else if (element == string("flight"))
225 // We have loaded and parsed all the information belonging to this flight
226 // so we temporarily store it.
227 //cerr << "Pusing back flight " << callsign << endl;
228 //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
229 // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
231 //Prioritize aircraft
232 string apt = fgGetString("/sim/presets/airport-id");
233 //cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl;
234 if (departurePort == apt) score++;
235 flights.push_back(FGScheduledFlight(callsign,
244 else if (element == string("aircraft"))
246 //cerr << "Pushing back aircraft " << registration << endl;
247 scheduledAircraft.push_back(FGAISchedule(mdl,
259 while(flights.begin() != flights.end())
261 SG_LOG( SG_GENERAL, SG_BULK, "Reading aircraft : "
263 << " with prioritization score "
269 void FGTrafficManager::data (const char * s, int len) {
270 string token = string(s,len);
271 //cout << "Character data " << string(s,len) << endl;
272 if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
278 void FGTrafficManager::pi (const char * target, const char * data) {
279 //cout << "Processing instruction " << target << ' ' << data << endl;
282 void FGTrafficManager::warning (const char * message, int line, int column) {
283 SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
286 void FGTrafficManager::error (const char * message, int line, int column) {
287 SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');