]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/tower.cxx
Roy Vegard Ovesen:
[flightgear.git] / src / ATC / tower.cxx
index cd388f4dc0791b397724ce098593292c680cedd4..10f35cb3aceeaf4e078c5e30633c33b3e422aaf6 100644 (file)
@@ -58,6 +58,7 @@ TowerPlaneRec::TowerPlaneRec() :
        finalAcknowledged(false),
        rwyVacatedReported(false),
        rwyVacatedAcknowledged(false),
+       goAroundReported(false),
        instructedToGoAround(false),
        onRwy(false),
        nextOnRwy(false),
@@ -84,6 +85,7 @@ TowerPlaneRec::TowerPlaneRec(PlaneRec p) :
        finalAcknowledged(false),
        rwyVacatedReported(false),
        rwyVacatedAcknowledged(false),
+       goAroundReported(false),
        instructedToGoAround(false),
        onRwy(false),
        nextOnRwy(false),
@@ -110,6 +112,7 @@ TowerPlaneRec::TowerPlaneRec(Point3D pt) :
        finalAcknowledged(false),
        rwyVacatedReported(false),
        rwyVacatedAcknowledged(false),
+       goAroundReported(false),
        instructedToGoAround(false),
        onRwy(false),
        nextOnRwy(false),
@@ -137,6 +140,7 @@ TowerPlaneRec::TowerPlaneRec(PlaneRec p, Point3D pt) :
        finalAcknowledged(false),
        rwyVacatedReported(false),
        rwyVacatedAcknowledged(false),
+       goAroundReported(false),
        instructedToGoAround(false),
        onRwy(false),
        nextOnRwy(false),
@@ -168,8 +172,6 @@ will break when planes start queueing. (CRITICAL)
 
 Report-Runway-Vacated is left as only user ATC option following a go-around. (MAJOR)
 
-Report Go-Around should be added to user options following reporting final or downwind. (MEDIUM).
-
 Report-Downwind is not added as ATC option when user takes off to fly a circuit. (MAJOR)
 
 eta of USER can be calculated very wrongly in circuit if flying straight out and turn4 etc are with +ve ortho y. 
@@ -183,6 +185,8 @@ GetPos() of the AI planes is called erratically - either too much or not enough.
 GO-AROUND is instructed very late at < 12s to landing - possibly make more dependent on chance of rwy clearing before landing (FEATURE)
 
 Need to make clear when TowerPlaneRecs do or don't get deleted in RemoveFromCircuitList etc. (MINOR until I misuse it - then CRITICAL!)
+
+FGTower::RemoveAllUserDialogOptions() really ought to be replaced by an ATCDialog::clear_entries() function. (MINOR - efficiency).
 *******************************************/
 
 FGTower::FGTower() {
@@ -227,7 +231,6 @@ FGTower::~FGTower() {
 
 void FGTower::Init() {
        //cout << "Initialising tower " << ident << '\n';
-    display = false;
        
        // Pointers to user's position
        user_lon_node = fgGetNode("/position/longitude-deg", true);
@@ -252,7 +255,7 @@ void FGTower::Init() {
                                ground = new FGGround(ident);
                                separateGround = false;
                                ground->Init();
-                               if(display) {
+                               if(_display) {
                                        ground->SetDisplay();
                                } else {
                                        ground->SetNoDisplay();
@@ -264,7 +267,7 @@ void FGTower::Init() {
                        ground = new FGGround(ident);
                        separateGround = false;
                        ground->Init();
-                       if(display) {
+                       if(_display) {
                                ground->SetDisplay();
                        } else {
                                ground->SetNoDisplay();
@@ -276,7 +279,7 @@ void FGTower::Init() {
                ground = new FGGround(ident);
                separateGround = false;
                ground->Init();
-               if(display) {
+               if(_display) {
                        ground->SetDisplay();
                } else {
                        ground->SetNoDisplay();
@@ -312,6 +315,7 @@ void FGTower::Init() {
                // For now assume that this means the user is not at the airport and is in the air.
                // TODO FIXME - this will break when user starts on apron, at hold short, etc.
                if(!OnAnyRunway(Point3D(user_lon_node->getDoubleValue(), user_lat_node->getDoubleValue(), 0.0))) {
+                       //cout << ident << "  ADD 0\n";
                        current_atcdialog->add_entry(ident, "@AP Tower @CS @MI miles @CD of the airport for full stop with the ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
                }
        }
@@ -402,7 +406,8 @@ void FGTower::Update(double dt) {
                // The display stuff might have to get more clever than this when not separate 
                // since the tower and ground might try communicating simultaneously even though
                // they're mean't to be the same contoller/frequency!!
-               if(display) {
+               // We could also get rid of this by overloading FGATC's Set(No)Display() functions.
+               if(_display) {
                        ground->SetDisplay();
                } else {
                        ground->SetNoDisplay();
@@ -419,6 +424,7 @@ void FGTower::Update(double dt) {
        // Call the base class update for the response time handling.
        FGATC::Update(dt);
 
+       /*
        if(ident == "KEMT") {   
                // For AI debugging convienience - may be removed
                Point3D user_pos;
@@ -430,6 +436,7 @@ void FGTower::Update(double dt) {
                fgSetDouble("/AI/user/ortho-y", user_ortho_pos.y());
                fgSetDouble("/AI/user/elev", user_elev_node->getDoubleValue());
        }
+       */
        
        //cout << "Done T" << endl;
 }
@@ -450,9 +457,13 @@ void FGTower::ReceiveUserCallback(int code) {
                ReportFinal("USER");
        } else if(code == (int)USER_REPORT_RWY_VACATED) {
                ReportRunwayVacated("USER");
+       } else if(code == (int)USER_REPORT_GOING_AROUND) {
+               ReportGoingAround("USER");
        }
 }
 
+// **************** RESPONSE FUNCTIONS ****************
+
 void FGTower::Respond() {
        //cout << "\nEntering Respond, responseID = " << responseID << endl;
        TowerPlaneRec* t = FindPlane(responseID);
@@ -494,7 +505,7 @@ void FGTower::Respond() {
                                }
                        }
                        trns += ConvertRwyNumToSpokenString(activeRwy);
-                       if(display) {
+                       if(_display) {
                                globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
                        } else {
                                //cout << "Not displaying, trns was " << trns << '\n';
@@ -502,29 +513,8 @@ void FGTower::Respond() {
                        t->vfrArrivalAcknowledged = true;
                } else if(t->downwindReported) {
                        //cout << "Tower " << ident << " is responding to downwind reported...\n";
+                       ProcessDownwindReport(t);
                        t->downwindReported = false;
-                       int i = 1;
-                       for(tower_plane_rec_list_iterator twrItr = circuitList.begin(); twrItr != circuitList.end(); twrItr++) {
-                               if((*twrItr)->plane.callsign == responseID) break;
-                               ++i;
-                       }
-                       string trns = t->plane.callsign;
-                       trns += " Number ";
-                       trns += ConvertNumToSpokenDigits(i);
-                       trns += " ";
-                       if((i == 1) && (!rwyList.size()) && (t->nextOnRwy)) {
-                               trns += "Cleared to land";      // TODO - clear for the option if appropriate
-                               t->clearedToLand = true;
-                               if(!t->isUser) t->planePtr->RegisterTransmission(7);
-                       }
-                       if(display) {
-                               globals->get_ATC_display()->RegisterSingleMessage(trns);
-                       }
-                       if(t->isUser) {
-                               if(t->opType == TTT_UNKNOWN) t->opType = CIRCUIT;
-                               current_atcdialog->add_entry(ident, "@CS Clear of the runway", "Report runway vacated", TOWER, USER_REPORT_RWY_VACATED);
-                               // TODO - add report going around as an option
-                       }
                } else if(t->holdShortReported) {
                        //cout << "Tower " << ident << " is reponding to holdShortReported...\n";
                        if(t->nextOnRwy) {
@@ -550,7 +540,7 @@ void FGTower::Respond() {
                                // Not currently sure under which circumstances we do or don't bother transmitting this.
                                string trns = t->plane.callsign;
                                trns += " hold position";
-                               if(display) {
+                               if(_display) {
                                        globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
                                }
                                // TODO - add some idea of what traffic is blocking him.
@@ -572,8 +562,11 @@ void FGTower::Respond() {
                                t->clearedToLand = true;
                                // Maybe remove report downwind from menu here as well incase user didn't bother to?
                                if(t->isUser) {
+                                       //cout << "ADD VACATED B\n";
+                                       // Put going around at the top (and hence default) since that'll be more desperate,
+                                       // or put rwy vacated at the top since that'll be more common?
+                                       current_atcdialog->add_entry(ident, "@CS Going Around", "Report going around", TOWER, USER_REPORT_GOING_AROUND);
                                        current_atcdialog->add_entry(ident, "@CS Clear of the runway", "Report runway vacated", TOWER, USER_REPORT_RWY_VACATED);
-                                       // TODO - add report going around as an option.
                                } else {
                                        t->planePtr->RegisterTransmission(7);
                                }
@@ -585,7 +578,7 @@ void FGTower::Respond() {
                                trns += " continue approach";
                                t->clearedToLand = false;
                        }
-                       if(display && disp) {
+                       if(_display && disp) {
                                globals->get_ATC_display()->RegisterSingleMessage(trns);
                        }
                        t->finalAcknowledged = true;
@@ -601,8 +594,81 @@ void FGTower::Respond() {
        //cout << "Done Respond\n" << endl;
 }
 
+void FGTower::ProcessDownwindReport(TowerPlaneRec* t) {
+       int i = 1;
+       int a = 0;      // Count of preceding planes on approach
+       bool cf = false;        // conflicting traffic on final
+       bool cc = false;        // preceding traffic in circuit
+       for(tower_plane_rec_list_iterator twrItr = circuitList.begin(); twrItr != circuitList.end(); twrItr++) {
+               if((*twrItr)->plane.callsign == responseID) break;
+               ++i;
+       }
+       if(i > 1) { cc = true; }
+       doThresholdETACalc();
+       TowerPlaneRec* tt;
+       for(tower_plane_rec_list_iterator twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
+               if((*twrItr)->eta < (t->eta + 45)) {
+                       a++;
+                       tt = *twrItr;
+                       cf = true;
+                       // This should set the flagged plane to be the last conflicting one, and hence the one to follow.
+                       // It ignores the fact that we might have problems slotting into the approach traffic behind it - 
+                       // eventually we'll need some fancy algorithms for that!
+               }
+       }
+       string trns = t->plane.callsign;
+       trns += " Number ";
+       trns += ConvertNumToSpokenDigits(i + a);
+       // This assumes that the number spoken is landing position, not circuit position, since some of the traffic might be on straight-in final.
+       trns += " ";
+       if((i == 1) && (!rwyList.size()) && (t->nextOnRwy) && (!cf)) {  // Unfortunately nextOnRwy currently doesn't handle circuit/straight-in ordering properly at present, hence the cf check below.
+               trns += "Cleared to land";      // TODO - clear for the option if appropriate
+               t->clearedToLand = true;
+               if(!t->isUser) t->planePtr->RegisterTransmission(7);
+       } else if((i+a) > 1) {
+               trns += "Follow the ";
+               string s = tt->plane.callsign;
+               int p = s.find('-');
+               s = s.substr(0,p);
+               trns += s;
+               if((tt->opType) == CIRCUIT) {
+                       if(tt->planePtr->GetLeg() == FINAL) {
+                               trns += " on final";
+                       } else if(tt->planePtr->GetLeg() == TURN4) {
+                               trns += " turning final";
+                       } else if(tt->planePtr->GetLeg() == BASE) {
+                               trns += " on base";
+                       } else if(tt->planePtr->GetLeg() == TURN3) {
+                               trns += " turning base";
+                       }
+               } else {
+                       double miles_out = CalcDistOutMiles(tt);
+                       if(miles_out < 2) {
+                               trns += " on short final";
+                       } else {
+                               trns += " on ";
+                               trns += ConvertNumToSpokenDigits((int)miles_out);
+                               trns += " mile final";
+                       }
+               }
+       }
+       if(_display) {
+               globals->get_ATC_display()->RegisterSingleMessage(trns);
+       }
+       if(t->isUser) {
+               if(t->opType == TTT_UNKNOWN) t->opType = CIRCUIT;
+               //cout << "ADD VACATED A\n";
+               // Put going around at the top (and hence default) since that'll be more desperate,
+               // or put rwy vacated at the top since that'll be more common?
+               //cout << "ident = " << ident << ", adding go-around option\n";
+               current_atcdialog->add_entry(ident, "@CS Going Around", "Report going around", TOWER, USER_REPORT_GOING_AROUND);
+               current_atcdialog->add_entry(ident, "@CS Clear of the runway", "Report runway vacated", TOWER, USER_REPORT_RWY_VACATED);
+       }
+}
+
 void FGTower::ProcessRunwayVacatedReport(TowerPlaneRec* t) {
        //cout << "Processing rwy vacated...\n";
+       current_atcdialog->remove_entry(ident, USER_REPORT_GOING_AROUND, TOWER);
        string trns = t->plane.callsign;
        if(separateGround) {
                trns += " Contact ground on ";
@@ -618,12 +684,14 @@ void FGTower::ProcessRunwayVacatedReport(TowerPlaneRec* t) {
                if(!t->isUser) t->planePtr->RegisterTransmission(6);    // TODO - this is a mega-hack!!
        }
        //cout << "trns = " << trns << '\n';
-       if(display) {
+       if(_display) {
                globals->get_ATC_display()->RegisterSingleMessage(trns);
        }
        // Maybe we should check that the plane really *has* vacated the runway!
 }
 
+// *********** END RESPONSE FUNCTIONS *****************
+
 // Currently this assumes we *are* next on the runway and doesn't check for planes about to land - 
 // this should be done prior to calling this function.
 void FGTower::ClearHoldingPlane(TowerPlaneRec* t) {
@@ -725,7 +793,7 @@ void FGTower::ClearHoldingPlane(TowerPlaneRec* t) {
                departed = false;
                timeSinceLastDeparture = 0.0;
        }
-       if(display) {
+       if(_display) {
                globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
        }
        //cout << "Done ClearHoldingPlane " << endl;
@@ -934,29 +1002,53 @@ void FGTower::CheckCircuitList(double dt) {
                }
                
                if(t->leg == FINAL && !(t->instructedToGoAround)) {
+                       doThresholdETACalc();
+                       doThresholdUseOrder();
+                       /*
+                       if(t->isUser) {
+                               cout << "Checking USER on final... ";
+                               cout << "eta " << t->eta;
+                               if(t->clearedToLand) cout << " cleared to land\n";
+                       }
+                       */
                        //cout << "YES FINAL, t->eta = " << t->eta << ", rwyList.size() = " << rwyList.size() << '\n';
                        if(t->landingType == FULL_STOP) {
                                t->opType = INBOUND;
                                //cout << "\n******** SWITCHING TO INBOUND AT POINT AAA *********\n\n";
-                               if(t->eta < 12 && rwyList.size()) {
-                                       // TODO - need to make this more sophisticated 
-                                       // eg. is the plane accelerating down the runway taking off [OK],
-                                       // or stationary near the start [V. BAD!!].
-                                       // For now this should stop the AI plane landing on top of the user.
-                                       string trns = t->plane.callsign;
-                                       trns += " GO AROUND TRAFFIC ON RUNWAY I REPEAT GO AROUND";
-                                       if(display) {
-                                               globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
-                                       }
-                                       t->instructedToGoAround = true;
-                                       t->clearedToLand = false;
-                                       // Assume it complies!!!
-                                       t->opType = CIRCUIT;
-                                       t->leg = CLIMBOUT;
-                                       if(t->planePtr) {
-                                               //cout << "Registering Go-around transmission with AI plane\n";
-                                               t->planePtr->RegisterTransmission(13);
+                       }
+                       if(t->eta < 12 && rwyList.size()) {
+                               // TODO - need to make this more sophisticated 
+                               // eg. is the plane accelerating down the runway taking off [OK],
+                               // or stationary near the start [V. BAD!!].
+                               // For now this should stop the AI plane landing on top of the user.
+                               string trns = t->plane.callsign;
+                               trns += " GO AROUND TRAFFIC ON RUNWAY I REPEAT GO AROUND";
+                               pending_transmission = trns;
+                               ImmediateTransmit();
+                               t->instructedToGoAround = true;
+                               t->clearedToLand = false;
+                               // Assume it complies!!!
+                               t->opType = CIRCUIT;
+                               t->leg = CLIMBOUT;
+                               if(t->planePtr) {
+                                       //cout << "Registering Go-around transmission with AI plane\n";
+                                       t->planePtr->RegisterTransmission(13);
+                               }
+                       } else if(!t->clearedToLand) {
+                               if(t->nextOnRwy) {
+                                       if(!rwyList.size()) {
+                                               string trns = t->plane.callsign;
+                                               trns += " Cleared to land";
+                                               pending_transmission = trns;
+                                               Transmit();
+                                               //if(t->isUser) cout << "Transmitting cleared to Land!!!\n";
+                                               t->clearedToLand = true;
+                                               if(!t->isUser) {
+                                                       t->planePtr->RegisterTransmission(7);
+                                               }
                                        }
+                               } else {
+                                       //if(t->isUser) cout << "Not next\n";
                                }
                        }
                } else if(t->leg == LANDING_ROLL) {
@@ -1066,9 +1158,8 @@ void FGTower::CheckApproachList(double dt) {
                        // For now this should stop the AI plane landing on top of the user.
                        string trns = t->plane.callsign;
                        trns += " GO AROUND TRAFFIC ON RUNWAY I REPEAT GO AROUND";
-                       if(display) {
-                               globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
-                       }
+                       pending_transmission = trns;
+                       ImmediateTransmit();
                        t->instructedToGoAround = true;
                        t->clearedToLand = false;
                        t->nextOnRwy = false;   // But note this is recalculated so don't rely on it
@@ -1143,12 +1234,12 @@ void FGTower::CheckDepartureList(double dt) {
                if(distout > 10000) {
                        string trns = t->plane.callsign;
                        trns += " You are now clear of my airspace, good day";
-                       if(display) {
-                               globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
-                       }
+                       pending_transmission = trns;
+                       Transmit();
                        if(t->isUser) {
                                // Change the communication options
                                RemoveAllUserDialogOptions();
+                               //cout << "ADD A\n";
                                current_atcdialog->add_entry(ident, "@AP Tower @CS @MI miles @CD of the airport for full stop with the ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
                        } else {
                                // Send a clear-of-airspace signal
@@ -1438,6 +1529,8 @@ bool FGTower::AddToTrafficList(TowerPlaneRec* t, bool holding) {
        if(!trafficList.size()) {
                t->nextOnRwy = true;
                // conflict and firstTime should be false and true respectively in this case anyway.
+       } else {
+               t->nextOnRwy = false;
        }
        trafficList.push_back(t);
        //cout << "\tE\t" << trafficList.size() << endl;
@@ -1673,15 +1766,17 @@ bool FGTower::doThresholdUseOrder() {
        
        tower_plane_rec_list_iterator twrItr;
        // Do the approach list first
-       //cout << "A" << flush;
+       //if(ident == "KRHV") cout << "A" << flush;
        for(twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
                TowerPlaneRec* tpr = *twrItr;
+               //if(ident == "KRHV") cout << tpr->plane.callsign << '\n';
                conflict = AddToTrafficList(tpr);
        }       
        // Then the circuit list
-       //cout << "C" << flush;
+       //if(ident == "KRHV") cout << "C" << flush;
        for(twrItr = circuitList.begin(); twrItr != circuitList.end(); twrItr++) {
                TowerPlaneRec* tpr = *twrItr;
+               //if(ident == "KRHV") cout << tpr->plane.callsign << '\n';
                conflict = AddToTrafficList(tpr);
        }
        // And finally the hold list
@@ -1691,8 +1786,10 @@ bool FGTower::doThresholdUseOrder() {
                AddToTrafficList(tpr, true);
        }
        
+       
        if(0) {
-       //if(ident == "KEMT") {
+       //if(ident == "KRHV") {
+               cout << "T\n";
                for(twrItr = trafficList.begin(); twrItr != trafficList.end(); twrItr++) {
                        TowerPlaneRec* tpr = *twrItr;
                        cout << tpr->plane.callsign << '\t' << tpr->eta << '\t';
@@ -1768,9 +1865,7 @@ void FGTower::RegisterAIPlane(PlaneRec plane, FGAIPlane* ai, tower_traffic_type
        CalcETA(t);
        
        if(op == CIRCUIT && lg != LEG_UNKNOWN) {
-               cout << "AAAAAAAAAAAAAAAaa" << endl;
                AddToCircuitList(t);
-               cout << "BBBBBBBBBBBBBBbbb" << endl;
        } else {
                // FLAG A WARNING
        }
@@ -1909,7 +2004,6 @@ void FGTower::ReportLongFinal(string ID) {
 //void FGTower::ReportOuterMarker(string ID);
 //void FGTower::ReportMiddleMarker(string ID);
 //void FGTower::ReportInnerMarker(string ID);
-//void FGTower::ReportGoingAround(string ID);
 
 void FGTower::ReportRunwayVacated(string ID) {
        //cout << "Report Runway Vacated Called at tower " << ident << " by plane " << ID << '\n';
@@ -2051,6 +2145,34 @@ void FGTower::ReportDownwind(string ID) {
        }
 }
 
+void FGTower::ReportGoingAround(string ID) {
+       if(ID == "USER") {
+               ID = fgGetString("/sim/user/callsign");
+               RemoveAllUserDialogOptions();   // TODO - it would be much more efficient if ATCDialog simply had a clear() function!!!
+               current_atcdialog->add_entry(ident, "@AP Tower @CS Downwind @RW", "Report Downwind", TOWER, (int)USER_REPORT_DOWNWIND);
+       }
+       TowerPlaneRec* t = FindPlane(ID);
+       if(t) {
+               //t->goAroundReported = true;  // No need to set this until we start responding to it.
+               responseReqd = false;   // might change in the future but for now we'll not distract them during the go-around.
+               // If the plane is in the app list, remove it and put it in the circuit list instead.
+               RemoveFromAppList(ID);
+               t->leg = CLIMBOUT;
+               if(t->isUser) {
+                       t->pos.setlon(user_lon_node->getDoubleValue());
+                       t->pos.setlat(user_lat_node->getDoubleValue());
+                       t->pos.setelev(user_elev_node->getDoubleValue());
+               } else {
+                       // ASSERT(t->planePtr != NULL);
+                       t->pos = t->planePtr->GetPos();
+               }
+               CalcETA(t);
+               AddToCircuitList(t);
+       } else {
+               SG_LOG(SG_ATC, SG_WARN, "WARNING: Unable to find plane " << ID << " in FGTower::ReportDownwind(...)");
+       }
+}
+
 string FGTower::GenText(const string& m, int c) {
        const int cmax = 300;
        //string message;