#include <vector>
#include <algorithm>
-#include <plib/ul.h>
-
#include <simgear/compiler.h>
#include <simgear/misc/sg_path.hxx>
+#include <simgear/misc/sg_dir.hxx>
#include <simgear/props/props.hxx>
#include <simgear/route/waypoint.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
/******************************************************************************
* TrafficManager
*****************************************************************************/
-FGTrafficManager::FGTrafficManager()
+FGTrafficManager::FGTrafficManager() :
+ inited(false),
+ enabled("/sim/traffic-manager/enabled"),
+ aiEnabled("/sim/ai/enabled"),
+ realWxEnabled("/environment/realwx/enabled"),
+ metarValid("/environment/metar/valid")
{
//score = 0;
//runCount = 0;
//cerr << "Saving AI traffic heuristics" << endl;
saveData = true;
cachefile.open(cacheData.str().c_str());
+ cachefile << "[TrafficManagerCachedata:ref:2011:09:04]" << endl;
}
}
for (ScheduleVectorIterator sched = scheduledAircraft.begin();
if (saveData) {
cachefile << (*sched)->getRegistration() << " "
<< (*sched)->getRunCount() << " "
- << (*sched)->getHits() << endl;
+ << (*sched)->getHits() << " "
+ << (*sched)->getLastUsed() << endl;
}
delete(*sched);
}
void FGTrafficManager::init()
{
- ulDir *d, *d2;
- ulDirEnt *dent, *dent2;
-
+ if (!enabled || !aiEnabled) {
+ return;
+ }
+
heuristicsVector heuristics;
HeuristicMap heurMap;
if (string(fgGetString("/sim/traffic-manager/datafile")) == string("")) {
- SGPath aircraftDir = globals->get_fg_root();
- SGPath path = aircraftDir;
-
- aircraftDir.append("AI/Traffic");
- if ((d = ulOpenDir(aircraftDir.c_str())) != NULL) {
- while ((dent = ulReadDir(d)) != NULL) {
- 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")) {
- SGPath currFile = currACDir;
- currFile.append(dent2->d_name);
- SG_LOG(SG_GENERAL, SG_DEBUG,
- "Scanning " << currFile.
- str() << " for traffic");
- readXML(currFile.str(), *this);
- }
- }
- ulCloseDir(d2);
- }
- }
- ulCloseDir(d);
+ simgear::Dir trafficDir(SGPath(globals->get_fg_root(), "AI/Traffic"));
+ simgear::PathList d = trafficDir.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT);
+
+ for (unsigned int i=0; i<d.size(); ++i) {
+ simgear::Dir d2(d[i]);
+ simgear::PathList trafficFiles = d2.children(simgear::Dir::TYPE_FILE, ".xml");
+ for (unsigned int j=0; j<trafficFiles.size(); ++j) {
+ SGPath curFile = trafficFiles[j];
+ SG_LOG(SG_GENERAL, SG_DEBUG,
+ "Scanning " << curFile.str() << " for traffic");
+ readXML(curFile.str(), *this);
+ }
}
} else {
fgSetBool("/sim/traffic-manager/heuristics", false);
airport[0], airport[1], airport[2]);
cacheData.append(buffer);
cacheData.append(airport + "-cache.txt");
+ string revisionStr;
if (cacheData.exists()) {
ifstream data(cacheData.c_str());
- while (1) {
- Heuristic h; // = new Heuristic;
- data >> h.registration >> h.runCount >> h.hits;
- if (data.eof())
- break;
- HeuristicMapIterator itr = heurMap.find(h.registration);
- if (itr != heurMap.end()) {
- SG_LOG(SG_GENERAL, SG_WARN,"Traffic Manager Warning: found duplicate tailnumber " <<
- h.registration << " for AI aircraft");
+ data >> revisionStr;
+ if (revisionStr != "[TrafficManagerCachedata:ref:2011:09:04]") {
+ SG_LOG(SG_GENERAL, SG_ALERT,"Traffic Manager Warning: discarding outdated cachefile " <<
+ cacheData.c_str() << " for Airport " << airport);
+ } else {
+ while (1) {
+ Heuristic h; // = new Heuristic;
+ data >> h.registration >> h.runCount >> h.hits >> h.lastRun;
+ if (data.eof())
+ break;
+ HeuristicMapIterator itr = heurMap.find(h.registration);
+ if (itr != heurMap.end()) {
+ SG_LOG(SG_GENERAL, SG_WARN,"Traffic Manager Warning: found duplicate tailnumber " <<
+ h.registration << " for AI aircraft");
+ } else {
+ heurMap[h.registration] = h;
+ heuristics.push_back(h);
+ }
}
- heurMap[h.registration] = h;
- heuristics.push_back(h);
}
}
- }
+ }
for (currAircraft = scheduledAircraft.begin();
currAircraft != scheduledAircraft.end(); currAircraft++) {
string registration = (*currAircraft)->getRegistration();
} else {
(*currAircraft)->setrunCount(itr->second.runCount);
(*currAircraft)->setHits(itr->second.hits);
- //cerr <<"Runcount " << itr->second->runCount << ".Hits " << itr->second->hits << endl;
+ (*currAircraft)->setLastUsed(itr->second.lastRun);
+ //cerr <<"Runcount " << itr->second.runCount << ". Hits " << itr->second.hits << ". Last run " << itr->second.lastRun<< endl;
}
}
//cerr << "Done" << endl;
//for (heuristicsVectorIterator hvi = heuristics.begin();
// hvi != heuristics.end(); hvi++) {
// delete(*hvi);
- //}
+ //}
}
// Do sorting and scoring separately, to take advantage of the "homeport| variable
for (currAircraft = scheduledAircraft.begin();
currAircraft != scheduledAircraft.end(); currAircraft++) {
(*currAircraft)->setScore();
}
+
sort(scheduledAircraft.begin(), scheduledAircraft.end(),
compareSchedules);
currAircraft = scheduledAircraft.begin();
currAircraftClosest = scheduledAircraft.begin();
+ inited = true;
}
void FGTrafficManager::update(double /*dt */ )
{
- if (fgGetBool("/environment/metar/valid") == false) {
+ if (!enabled || !aiEnabled || (realWxEnabled && !metarValid)) {
return;
}
+
+ if (!inited) {
+ // lazy-initialization, we've been enabled at run-time
+ SG_LOG(SG_GENERAL, SG_INFO, "doing lazy-init of TrafficManager");
+ init();
+ }
+
time_t now = time(NULL) + fgGetLong("/sim/time/warp");
if (scheduledAircraft.size() == 0) {
return;
}
//cerr << "Processing << " << (*currAircraft)->getRegistration() << " with score " << (*currAircraft)->getScore() << endl;
if (!((*currAircraft)->update(now, userCart))) {
- // NOTE: With traffic manager II, this statement below is no longer true
- // 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)->taint();
}
currAircraft++;
}