X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FTraffic%2FTrafficMgr.cxx;h=86be2196cb2e02e4da8b1a76152f65bda97089f3;hb=c1e29d09981dfcd47ff213069fbe8fd5c8ccaf0c;hp=dda17c06bdf8be601c2bb3ec25496a70840092da;hpb=d158c8168dffd71d385552b94ad9997d4de30bbd;p=flightgear.git diff --git a/src/Traffic/TrafficMgr.cxx b/src/Traffic/TrafficMgr.cxx index dda17c06b..86be2196c 100644 --- a/src/Traffic/TrafficMgr.cxx +++ b/src/Traffic/TrafficMgr.cxx @@ -14,7 +14,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * **************************************************************************/ @@ -33,6 +33,11 @@ * * *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include #include #include @@ -41,52 +46,177 @@ #include #include +#include #include +#include #include #include #include +#include #include #include #include #include +#include #include #include #include -#include
// That's pretty ugly, but I need fgFindAirportID +#include
#include "TrafficMgr.hxx" +SG_USING_STD(sort); /****************************************************************************** * TrafficManager *****************************************************************************/ FGTrafficManager::FGTrafficManager() { + score = 0; + runCount = 0; +} + +FGTrafficManager:: ~FGTrafficManager() +{ + for (ScheduleVectorIterator sched = scheduledAircraft.begin(); sched != scheduledAircraft.end(); sched++) + { + delete (*sched); + } + scheduledAircraft.clear(); + // for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++) +// { +// delete (*flt); +// } +// flights.clear(); } void FGTrafficManager::init() -{ +{ + //cerr << "Initializing Schedules" << endl; + //time_t now = time(NULL) + fgGetLong("/sim/time/warp"); + //currAircraft = scheduledAircraft.begin(); + //while (currAircraft != scheduledAircraft.end()) + // { + // if (!(currAircraft->init())) + // { + // currAircraft=scheduledAircraft.erase(currAircraft); + // //cerr << "Erasing " << currAircraft->getRegistration() << endl; + // } + // else + // { + // currAircraft++; + // } + // } + // Sort by points: Aircraft with frequent visits to the + // startup airport will be processed first + ulDir* d, *d2; + ulDirEnt* dent, *dent2; + SGPath aircraftDir = globals->get_fg_root(); + + /* keep the following three lines (which mimicks the old "fixed path" behavior) + * until we have some AI models with traffic in the base package + */ + SGPath path = aircraftDir; + path.append("Traffic/fgtraffic.xml"); + if (path.exists()) + readXML(path.str(),*this); + + aircraftDir.append("AI/Aircraft"); + if (aircraftDir.exists()) + { + if((d = ulOpenDir(aircraftDir.c_str())) == NULL) + return; + while((dent = ulReadDir(d)) != NULL) { + //cerr << "Scanning : " << dent->d_name << endl; + if (string(dent->d_name) != string(".") && + string(dent->d_name) != string("..") && + dent->d_isdir) + { + SGPath currACDir = aircraftDir; + currACDir.append(dent->d_name); + if ((d2 = ulOpenDir(currACDir.c_str())) == NULL) + return; + while ((dent2 = ulReadDir(d2)) != NULL) { + SGPath currFile = currACDir; + currFile.append(dent2->d_name); + if (currFile.extension() == string("xml")) + { + //cerr << "found " << dent2->d_name << " for parsing" << endl; + SGPath currFile = currACDir; + currFile.append(dent2->d_name); + SG_LOG(SG_GENERAL, SG_INFO, "Scanning " << currFile.str() << " for traffic"); + readXML(currFile.str(),*this); + } + } + ulCloseDir(d2); + } + } + ulCloseDir(d); + } + // Sort by points: Aircraft with frequent visits to the + // startup airport will be processed first + sort(scheduledAircraft.begin(), scheduledAircraft.end(), compareSchedules); currAircraft = scheduledAircraft.begin(); + currAircraftClosest = scheduledAircraft.begin(); + //cerr << "Done initializing schedules" << endl; } -void FGTrafficManager::update(double something) +void FGTrafficManager::update(double /*dt*/) { - - //static const SGPropertyNode *warp = globals->get_props()->getNode("/sim/time/warp"); - - //time_t now = time(NULL) + globals->get_warp(); + //SG_LOG( SG_GENERAL, SG_INFO, "Running TrafficManager::Update() "); + // Hack alert: Skip running for the first frames 1000 after + // initialization to allow proper initialization of wheather stuff + // and runway assignments + if (runCount < 1000) + { + runCount++; + return; + } + //runCount = 0; time_t now = time(NULL) + fgGetLong("/sim/time/warp"); - // cerr << "TrafficManager update" << globals->get_warp() << endl; - if(currAircraft == scheduledAircraft.end()) + if (scheduledAircraft.size() == 0) { + //SG_LOG( SG_GENERAL, SG_INFO, "Returned Running TrafficManager::Update() "); + return; + } + if(currAircraft == scheduledAircraft.end()) + { + //cerr << "resetting schedule " << endl; currAircraft = scheduledAircraft.begin(); - currAircraft->update(now); - currAircraft++; + } + if (!((*currAircraft)->update(now))) + { + // after proper initialization, we shouldnt get here. + // But let's make sure + SG_LOG( SG_GENERAL, SG_ALERT, "Failed to update aircraft schedule in traffic manager"); + } + currAircraft++; + //SG_LOG( SG_GENERAL, SG_INFO, "Done Running TrafficManager::Update() "); +} + +void FGTrafficManager::release(int id) +{ + releaseList.push_back(id); +} + +bool FGTrafficManager::isReleased(int id) +{ + IdListIterator i = releaseList.begin(); + while (i != releaseList.end()) + { + if ((*i) == id) + { + releaseList.erase(i); + return true; + } + i++; + } + return false; } void FGTrafficManager::startXML () { @@ -107,11 +237,11 @@ void FGTrafficManager::startElement (const char * name, const XMLAttributes &at if (attval != 0) { //cout << "including " << attval << endl; - string path = - globals->get_fg_root() + - string("/Traffic/") + - string(attval); - readXML(path, *this); + SGPath path = + globals->get_fg_root(); + path.append("/Traffic/"); + path.append(attval); + readXML(path.str(), *this); } // cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl; } @@ -125,6 +255,18 @@ void FGTrafficManager::endElement (const char * name) { livery = value; else if (element == string("registration")) registration = value; + else if (element == string("airline")) + airline = value; + else if (element == string("actype")) + acType = value; + else if (element == string("flighttype")) + flighttype = value; + else if (element == string("radius")) + radius = atoi(value.c_str()); + else if (element == string("offset")) + offset = atoi(value.c_str()); + else if (element == string("performance-class")) + m_class = value; else if (element == string("heavy")) { if(value == string("true")) @@ -161,7 +303,12 @@ void FGTrafficManager::endElement (const char * name) { //cerr << "Pusing back flight " << callsign << endl; //cerr << callsign << " " << fltrules << " "<< departurePort << " " << arrivalPort << " " // << cruiseAlt << " " << departureTime<< " "<< arrivalTime << " " << repeat << endl; - flights.push_back(FGScheduledFlight(callsign, + + //Prioritize aircraft + string apt = fgGetString("/sim/presets/airport-id"); + //cerr << "Airport information: " << apt << " " << departurePort << " " << arrivalPort << endl; + if (departurePort == apt) score++; + flights.push_back(new FGScheduledFlight(callsign, fltrules, departurePort, arrivalPort, @@ -173,13 +320,31 @@ void FGTrafficManager::endElement (const char * name) { else if (element == string("aircraft")) { //cerr << "Pushing back aircraft " << registration << endl; - scheduledAircraft.push_back(FGAISchedule(mdl, - livery, - registration, - heavy, - flights)); - while(flights.begin() != flights.end()) - flights.pop_back(); + scheduledAircraft.push_back(new FGAISchedule(mdl, + livery, + registration, + heavy, + acType, + airline, + m_class, + flighttype, + radius, + offset, + score, + flights)); + // while(flights.begin() != flights.end()) { +// flights.pop_back(); +// } + for (FGScheduledFlightVecIterator flt = flights.begin(); flt != flights.end(); flt++) + { + delete (*flt); + } + flights.clear(); + SG_LOG( SG_GENERAL, SG_BULK, "Reading aircraft : " + << registration + << " with prioritization score " + << score); + score = 0; } } @@ -197,11 +362,9 @@ void FGTrafficManager::pi (const char * target, const char * data) { } void FGTrafficManager::warning (const char * message, int line, int column) { - cout << "Warning: " << message << " (" << line << ',' << column << ')' - << endl; + SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')'); } void FGTrafficManager::error (const char * message, int line, int column) { - cout << "Error: " << message << " (" << line << ',' << column << ')' - << endl; + SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')'); }