+ if(!initDone) {
+ init();
+ SG_LOG(SG_ATC, SG_WARN, "Warning - AIMgr::update(...) called before AIMgr::init()");
+ }
+
+ //cout << activated.size() << '\n';
+
+ Point3D userPos = Point3D(lon_node->getDoubleValue(), lat_node->getDoubleValue(), elev_node->getDoubleValue());
+
+ // TODO - make these class variables!!
+ static int i = 0;
+ static int j = 0;
+
+ // Don't update any planes for first 50 runs through - this avoids some possible initialisation anomalies
+ // Might not need it now we have fade-in though?
+ if(i < 50) {
+ ++i;
+ return;
+ }
+
+ if(j == 215) {
+ SearchByPos(25.0);
+ j = 0;
+ } else if(j == 200) {
+ // Go through the list of activated airports and remove those out of range
+ //cout << "The following airports have been activated by the AI system:\n";
+ ai_activated_map_iterator apt_itr = activated.begin();
+ while(apt_itr != activated.end()) {
+ //cout << "FIRST IS " << (*apt_itr).first << '\n';
+ if(dclGetHorizontalSeparation(userPos, dclGetAirportPos((*apt_itr).first)) > (35.0 * 1600.0)) {
+ // Then get rid of it and make sure the iterator is left pointing to the next one!
+ string s = (*apt_itr).first;
+ if(traffic.find(s) != traffic.end()) {
+ //cout << "s = " << s << ", traffic[s].size() = " << traffic[s].size() << '\n';
+ if(traffic[s].size()) {
+ apt_itr++;
+ } else {
+ //cout << "Erasing " << (*apt_itr).first << " and traffic" << '\n';
+ activated.erase(apt_itr++);
+ traffic.erase(s);
+ }
+ } else {
+ //cout << "Erasing " << (*apt_itr).first << ' ' << (*apt_itr).second << '\n';
+ activated.erase(apt_itr++);
+ }
+ } else {
+ apt_itr++;
+ }
+ }
+ } else if(j == 180) {
+ // Go through the list of activated airports and do the random airplane generation
+ ai_traffic_map_iterator it = traffic.begin();
+ while(it != traffic.end()) {
+ string s = (*it).first;
+ //cout << "s = " << s << " size = " << (*it).second.size() << '\n';
+ // Only generate extra traffic if within a certain distance of the user,
+ // TODO - maybe take users's tuned freq into account as well.
+ double d = dclGetHorizontalSeparation(userPos, dclGetAirportPos(s));
+ if(d < (15.0 * 1600.0)) {
+ double cd = 0.0;
+ bool gen = false;
+ //cout << "Size of list is " << (*it).second.size() << " at " << s << '\n';
+ if((*it).second.size()) {
+ FGAIEntity* e = *((*it).second.rbegin()); // Get the last airplane currently scheduled to arrive at this airport.
+ cd = dclGetHorizontalSeparation(e->GetPos(), dclGetAirportPos(s));
+ if(cd < (d < 5000 ? 10000 : d + 5000)) {
+ gen = true;
+ }
+ } else {
+ gen = true;
+ cd = 0.0;
+ }
+ if(gen) {
+ //cout << "Generating extra traffic at airport " << s << ", at least " << cd << " meters out\n";
+ //GenerateSimpleAirportTraffic(s, cd);
+ GenerateSimpleAirportTraffic(s, cd + 3000.0); // The random seems a bit wierd - traffic could get far too bunched without the +3000.
+ // TODO - make the anti-random constant variable depending on the ai-traffic level.
+ }
+ }
+ ++it;
+ }
+ }
+
+ ++j;
+
+ //cout << "Size of AI list is " << ai_list.size() << '\n';
+
+ // TODO - need to add a check of if any activated airports have gone out of range
+
+ string rs; // plane to be removed, if one.
+ if(removalList.size()) {
+ rs = *(removalList.begin());
+ removalList.pop_front();
+ } else {
+ rs = "";
+ }
+
+ // Traverse the list of active planes and run all their update methods
+ // TODO - spread the load - not all planes should need updating every frame.
+ // Note that this will require dt to be calculated for each plane though
+ // since they rely on it to calculate distance travelled.
+ ai_list_itr = ai_list.begin();
+ while(ai_list_itr != ai_list.end()) {
+ FGAIEntity *e = *ai_list_itr;
+ if(rs.size() && e->GetCallsign() == rs) {
+ //cout << "Removing " << rs << " from ai_list\n";
+ ai_list_itr = ai_list.erase(ai_list_itr);
+ delete e;
+ // This is a hack - we should deref this plane from the airport count!
+ } else {
+ e->Update(dt);
+ ++ai_list_itr;
+ }
+ }
+
+ //cout << "Size of AI list is " << ai_list.size() << '\n';
+}
+
+void FGAIMgr::ScheduleRemoval(string s) {
+ //cout << "Scheduling removal of plane " << s << " from AIMgr\n";
+ removalList.push_back(s);
+}
+
+// Activate AI traffic at an airport
+void FGAIMgr::ActivateAirport(string ident) {
+ ATC->AIRegisterAirport(ident);
+ // TODO - need to start the traffic more randomly
+ FGAILocalTraffic* local_traffic = new FGAILocalTraffic;
+ local_traffic->SetModel(_defaultModel); // currently hardwired to cessna.
+ //local_traffic->Init(ident, IN_PATTERN, TAKEOFF_ROLL);
+ local_traffic->Init(GenerateShortForm(GenerateUniqueCallsign()), ident);
+ local_traffic->FlyCircuits(1, true); // Fly 2 circuits with touch & go in between
+ ai_list.push_back(local_traffic);
+ traffic[ident].push_back(local_traffic);
+ //cout << "******** ACTIVATING AIRPORT, ident = " << ident << '\n';
+ activated[ident] = 1;
+}
+
+// Hack - Generate AI traffic at an airport with no facilities file
+void FGAIMgr::GenerateSimpleAirportTraffic(string ident, double min_dist) {
+ // Ugly hack - don't let VFR Cessnas operate at a hardwired list of major airports
+ // This will go eventually once airport .xml files specify the traffic profile
+ if(ident == "KSFO" || ident == "KDFW" || ident == "EGLL" || ident == "KORD" || ident == "KJFK"
+ || ident == "KMSP" || ident == "KLAX" || ident == "KBOS" || ident == "KEDW"
+ || ident == "KSEA" || ident == "EHAM") {
+ return;
+ }
+
+ /*
+ // TODO - check for military airports - this should be in the current data.
+ // UGGH - there's no point at the moment - everything is labelled civil in basic.dat!
+ FGAirport a;
+ if(dclFindAirportID(ident, &a)) {
+ cout << "CODE IS " << a.code << '\n';
+ } else {
+ // UG - can't find the airport!
+ return;
+ }
+ */
+
+ Point3D aptpos = dclGetAirportPos(ident); // TODO - check for elev of -9999
+ //cout << "ident = " << ident << ", elev = " << aptpos.elev() << '\n';
+
+ // Operate from airports at 3000ft and below only to avoid the default cloud layers and since we don't degrade AI performance with altitude.
+ if(aptpos.elev() > 3000) {
+ //cout << "High alt airports not yet supported - returning\n";
+ return;
+ }
+
+ // Rough hack for plane type - make 70% of the planes cessnas, the rest pipers.
+ bool cessna = true;
+
+ // Get the time and only operate VFR in the (approximate) daytime.
+ //SGTime *t = globals->get_time_params();
+ string time_str = fgGetString("sim/time/gmt-string");
+ int loc_time = atoi((time_str.substr(0,3)).c_str());
+ //cout << "gmt_time = " << loc_time << '\n';
+ loc_time += (int)((aptpos.lon() / 360.0) * 24.0);
+ while(loc_time < 0) loc_time += 24;
+ while(loc_time > 24) loc_time -= 24;
+ //cout << "loc_time = " << loc_time << '\n';
+ if(loc_time < 7 || loc_time > 19) return;
+
+ // Check that the visibility is OK for IFR operation.
+ double visibility;
+ FGEnvironment stationweather =
+ ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
+ ->getEnvironment(aptpos.lat(), aptpos.lon(), aptpos.elev()); // TODO - check whether this should take ft or m for elev.
+ visibility = stationweather.get_visibility_m();
+ // Technically we can do VFR down to 1 mile (1600m) but that's pretty murky!
+ //cout << "vis = " << visibility << '\n';
+ if(visibility < 3000) return;
+
+ ATC->AIRegisterAirport(ident);
+
+ // Next - get the distance from user to the airport.
+ Point3D userpos = Point3D(lon_node->getDoubleValue(), lat_node->getDoubleValue(), elev_node->getDoubleValue());
+ double d = dclGetHorizontalSeparation(userpos, aptpos); // in meters
+
+ int lev = fgGetInt("/sim/ai-traffic/level");
+ if(lev < 1 || lev > 3) lev = 2;
+ if(visibility < 6000) lev = 1;
+ //cout << "level = " << lev << '\n';
+
+ // Next - generate any local / circuit traffic
+
+ /*
+ // --------------------------- THIS BLOCK IS JUST FOR TESTING - COMMENT OUT BEFORE RELEASE ---------------
+ // Finally - generate VFR approaching traffic
+ //if(d > 2000) {
+ if(ident == "KPOC") {
+ double ad = 2000.0;
+ double avd = 3000.0; // average spacing of arriving traffic in meters - relate to airport business and AI density setting one day!
+ //while(ad < (d < 10000 ? 12000 : d + 2000)) {
+ for(int i=0; i<8; ++i) {
+ double dd = sg_random() * avd;
+ // put a minimum spacing in for now since I don't think tower will cope otherwise!
+ if(dd < 1500) dd = 1500;
+ //ad += dd;
+ ad += dd;
+ double dir = int(sg_random() * 36);
+ if(dir == 36) dir--;
+ dir *= 10;
+ //dir = 180;
+ if(sg_random() < 0.3) cessna = false;
+ else cessna = true;
+ string s = GenerateShortForm(GenerateUniqueCallsign(), (cessna ? "Cessna-" : "Piper-"));
+ FGAIGAVFRTraffic* t = new FGAIGAVFRTraffic();
+ t->SetModel(cessna ? _defaultModel : _piperModel);
+ //cout << "Generating VFR traffic " << s << " inbound to " << ident << " " << ad << " meters out from " << dir << " degrees\n";
+ Point3D tpos = dclUpdatePosition(aptpos, dir, 6.0, ad);
+ if(tpos.elev() > (aptpos.elev() + 3000.0)) tpos.setelev(aptpos.elev() + 3000.0);
+ t->Init(tpos, ident, s);
+ ai_list.push_back(t);
+ }
+ }
+ activated[ident] = 1;