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 FGTrafficManager:: ~FGTrafficManager()
85 for (ScheduleVectorIterator sched = scheduledAircraft.begin(); sched != scheduledAircraft.end(); sched++)
89 scheduledAircraft.clear();
90 // for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
98 void FGTrafficManager::init()
100 //cerr << "Initializing Schedules" << endl;
101 //time_t now = time(NULL) + fgGetLong("/sim/time/warp");
102 //currAircraft = scheduledAircraft.begin();
103 //while (currAircraft != scheduledAircraft.end())
105 // if (!(currAircraft->init()))
107 // currAircraft=scheduledAircraft.erase(currAircraft);
108 // //cerr << "Erasing " << currAircraft->getRegistration() << endl;
115 // Sort by points: Aircraft with frequent visits to the
116 // startup airport will be processed first
117 sort(scheduledAircraft.begin(), scheduledAircraft.end(), compareSchedules);
118 currAircraft = scheduledAircraft.begin();
119 currAircraftClosest = scheduledAircraft.begin();
120 //cerr << "Done initializing schedules" << endl;
123 void FGTrafficManager::update(double something)
130 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
131 if (scheduledAircraft.size() == 0)
133 if(currAircraft == scheduledAircraft.end())
135 //cerr << "resetting schedule " << endl;
136 currAircraft = scheduledAircraft.begin();
138 if (!((*currAircraft)->update(now)))
140 // after proper initialization, we shouldnt get here.
141 // But let's make sure
142 cerr << "Failed to update aircraft schedule in traffic manager" << endl;
147 void FGTrafficManager::release(int id)
149 releaseList.push_back(id);
152 bool FGTrafficManager::isReleased(int id)
154 IdListIterator i = releaseList.begin();
155 while (i != releaseList.end())
159 releaseList.erase(i);
167 void FGTrafficManager::startXML () {
168 //cout << "Start XML" << endl;
171 void FGTrafficManager::endXML () {
172 //cout << "End XML" << endl;
175 void FGTrafficManager::startElement (const char * name, const XMLAttributes &atts) {
177 //cout << "Start element " << name << endl;
178 //FGTrafficManager temp;
179 //for (int i = 0; i < atts.size(); i++)
180 // if (string(atts.getName(i)) == string("include"))
181 attval = atts.getValue("include");
184 //cout << "including " << attval << endl;
186 globals->get_fg_root();
187 path.append("/Traffic/");
189 readXML(path.str(), *this);
191 // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl;
194 void FGTrafficManager::endElement (const char * name) {
195 //cout << "End element " << name << endl;
196 string element(name);
197 if (element == string("model"))
199 else if (element == string("livery"))
201 else if (element == string("registration"))
202 registration = value;
203 else if (element == string("airline"))
205 else if (element == string("actype"))
207 else if (element == string("flighttype"))
209 else if (element == string("radius"))
210 radius = atoi(value.c_str());
211 else if (element == string("offset"))
212 offset = atoi(value.c_str());
213 else if (element == string("performance-class"))
215 else if (element == string("heavy"))
217 if(value == string("true"))
222 else if (element == string("callsign"))
224 else if (element == string("fltrules"))
226 else if (element == string("port"))
228 else if (element == string("time"))
230 else if (element == string("departure"))
232 departurePort = port;
233 departureTime = timeString;
235 else if (element == string("cruise-alt"))
236 cruiseAlt = atoi(value.c_str());
237 else if (element == string("arrival"))
240 arrivalTime = timeString;
242 else if (element == string("repeat"))
244 else if (element == string("flight"))
246 // We have loaded and parsed all the information belonging to this flight
247 // so we temporarily store it.
248 //cerr << "Pusing back flight " << callsign << endl;
249 //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " "
250 // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl;
252 //Prioritize aircraft
253 string apt = fgGetString("/sim/presets/airport-id");
254 //cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl;
255 if (departurePort == apt) score++;
256 flights.push_back(new FGScheduledFlight(callsign,
265 else if (element == string("aircraft"))
267 //cerr << "Pushing back aircraft " << registration << endl;
268 scheduledAircraft.push_back(new FGAISchedule(mdl,
280 // while(flights.begin() != flights.end()) {
281 // flights.pop_back();
283 for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++)
288 SG_LOG( SG_GENERAL, SG_BULK, "Reading aircraft : "
290 << " with prioritization score "
296 void FGTrafficManager::data (const char * s, int len) {
297 string token = string(s,len);
298 //cout << "Character data " << string(s,len) << endl;
299 if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
305 void FGTrafficManager::pi (const char * target, const char * data) {
306 //cout << "Processing instruction " << target << ' ' << data << endl;
309 void FGTrafficManager::warning (const char * message, int line, int column) {
310 SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
313 void FGTrafficManager::error (const char * message, int line, int column) {
314 SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');