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 *****************************************************************************/
54 #include <simgear/compiler.h>
55 #include <simgear/math/polar3d.hxx>
56 #include <simgear/math/sg_geodesy.hxx>
57 #include <simgear/misc/sg_path.hxx>
58 #include <simgear/props/props.hxx>
59 #include <simgear/route/waypoint.hxx>
60 #include <simgear/structure/subsystem_mgr.hxx>
61 #include <simgear/xml/easyxml.hxx>
63 #include <AIModel/AIAircraft.hxx>
64 #include <AIModel/AIFlightPlan.hxx>
65 #include <AIModel/AIBase.hxx>
66 #include <Airports/simple.hxx>
67 #include <Main/fg_init.hxx>
71 #include "TrafficMgr.hxx"
75 /******************************************************************************
77 *****************************************************************************/
78 FGTrafficManager::FGTrafficManager()
84 FGTrafficManager:: ~FGTrafficManager()
86 for (ScheduleVectorIterator sched = scheduledAircraft.begin(); sched != scheduledAircraft.end(); sched++)
90 scheduledAircraft.clear();
91 // for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
99 void FGTrafficManager::init()
101 //cerr << "Initializing Schedules" << endl;
102 //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
103 //currAircraft = scheduledAircraft.begin();
104 //while (currAircraft != scheduledAircraft.end())
106 // if (!(currAircraft->init()))
108 // currAircraft=scheduledAircraft.erase(currAircraft);
109 // //cerr << "Erasing " << currAircraft->getRegistration() << endl;
116 // Sort by points: Aircraft with frequent visits to the
117 // startup airport will be processed first
119 ulDirEnt* dent, *dent2;
120 SGPath aircraftDir = globals->get_fg_root();
122 /* keep the following three lines (which mimicks the old "fixed path" behavior)
123 * until we have some AI models with traffic in the base package
125 SGPath path = aircraftDir;
126 path.append("Traffic/fgtraffic.xml");
127 readXML(path.str(),*this);
129 aircraftDir.append("AI/Aircraft");
130 if (aircraftDir.exists())
132 if((d = ulOpenDir(aircraftDir.c_str())) == NULL)
134 while((dent = ulReadDir(d)) != NULL) {
135 //cerr << "Scanning : " << dent->d_name << endl;
136 if (string(dent->d_name) != string(".") &&
137 string(dent->d_name) != string("..") &&
140 SGPath currACDir = aircraftDir;
141 currACDir.append(dent->d_name);
142 if ((d2 = ulOpenDir(currACDir.c_str())) == NULL)
144 while ((dent2 = ulReadDir(d2)) != NULL) {
145 SGPath currFile = currACDir;
146 currFile.append(dent2->d_name);
147 if (currFile.extension() == string("xml"))
149 //cerr << "found " << dent2->d_name << " for parsing" << endl;
150 SGPath currFile = currACDir;
151 currFile.append(dent2->d_name);
152 SG_LOG(SG_GENERAL, SG_INFO, "Scanning " << currFile.str() << " for traffic");
153 readXML(currFile.str(),*this);
161 // Sort by points: Aircraft with frequent visits to the
162 // startup airport will be processed first
163 sort(scheduledAircraft.begin(), scheduledAircraft.end(), compareSchedules);
164 currAircraft = scheduledAircraft.begin();
165 currAircraftClosest = scheduledAircraft.begin();
166 //cerr << "Done initializing schedules" << endl;
169 void FGTrafficManager::update(double /*dt*/)
171 //SG_LOG( SG_GENERAL, SG_INFO, "Running TrafficManager::Update() ");
178 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
179 if (scheduledAircraft.size() == 0) {
180 //SG_LOG( SG_GENERAL, SG_INFO, "Returned Running TrafficManager::Update() ");
183 if(currAircraft == scheduledAircraft.end())
185 //cerr << "resetting schedule " << endl;
186 currAircraft = scheduledAircraft.begin();
188 if (!((*currAircraft)->update(now)))
190 // after proper initialization, we shouldnt get here.
191 // But let's make sure
192 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to update aircraft schedule in traffic manager");
195 //SG_LOG( SG_GENERAL, SG_INFO, "Done Running TrafficManager::Update() ");
198 void FGTrafficManager::release(int id)
200 releaseList.push_back(id);
203 bool FGTrafficManager::isReleased(int id)
205 IdListIterator i = releaseList.begin();
206 while (i != releaseList.end())
210 releaseList.erase(i);
218 void FGTrafficManager::startXML () {
219 //cout << "Start XML" << endl;
222 void FGTrafficManager::endXML () {
223 //cout << "End XML" << endl;
226 void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
228 //cout << "Start element " << name << endl;
229 //FGTrafficManager temp;
230 //for (int i = 0; i < atts.size(); i++)
231 // if (string(atts.getName(i)) == string("include"))
232 attval = atts.getValue("include");
235 //cout << "including " << attval << endl;
237 globals->get_fg_root();
238 path.append("/Traffic/");
240 readXML(path.str(), *this);
242 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
245 void FGTrafficManager::endElement (const char * name) {
246 //cout << "End element " << name << endl;
247 string element(name);
248 if (element == string("model"))
250 else if (element == string("livery"))
252 else if (element == string("registration"))
253 registration = value;
254 else if (element == string("airline"))
256 else if (element == string("actype"))
258 else if (element == string("flighttype"))
260 else if (element == string("radius"))
261 radius = atoi(value.c_str());
262 else if (element == string("offset"))
263 offset = atoi(value.c_str());
264 else if (element == string("performance-class"))
266 else if (element == string("heavy"))
268 if(value == string("true"))
273 else if (element == string("callsign"))
275 else if (element == string("fltrules"))
277 else if (element == string("port"))
279 else if (element == string("time"))
281 else if (element == string("departure"))
283 departurePort = port;
284 departureTime = timeString;
286 else if (element == string("cruise-alt"))
287 cruiseAlt = atoi(value.c_str());
288 else if (element == string("arrival"))
291 arrivalTime = timeString;
293 else if (element == string("repeat"))
295 else if (element == string("flight"))
297 // We have loaded and parsed all the information belonging to this flight
298 // so we temporarily store it.
299 //cerr << "Pusing back flight " << callsign << endl;
300 //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
301 // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
303 //Prioritize aircraft
304 string apt = fgGetString("/sim/presets/airport-id");
305 //cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl;
306 if (departurePort == apt) score++;
307 flights.push_back(new FGScheduledFlight(callsign,
316 else if (element == string("aircraft"))
318 //cerr << "Pushing back aircraft " << registration << endl;
319 scheduledAircraft.push_back(new FGAISchedule(mdl,
331 // while(flights.begin() != flights.end()) {
332 // flights.pop_back();
334 for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
339 SG_LOG( SG_GENERAL, SG_BULK, "Reading aircraft : "
341 << " with prioritization score "
347 void FGTrafficManager::data (const char * s, int len) {
348 string token = string(s,len);
349 //cout << "Character data " << string(s,len) << endl;
350 if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
356 void FGTrafficManager::pi (const char * target, const char * data) {
357 //cout << "Processing instruction " << target << ' ' << data << endl;
360 void FGTrafficManager::warning (const char * message, int line, int column) {
361 SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
364 void FGTrafficManager::error (const char * message, int line, int column) {
365 SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');