//
// 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.
#ifdef HAVE_CONFIG_H
# include <config.h>
#include <simgear/debug/logstream.hxx>
#include "tower.hxx"
-#include "ATCdisplay.hxx"
#include "ATCmgr.hxx"
#include "ATCutils.hxx"
#include "ATCDialog.hxx"
finalAcknowledged(false),
rwyVacatedReported(false),
rwyVacatedAcknowledged(false),
+ goAroundReported(false),
instructedToGoAround(false),
onRwy(false),
nextOnRwy(false),
plane.callsign = "UNKNOWN";
}
-TowerPlaneRec::TowerPlaneRec(PlaneRec p) :
+TowerPlaneRec::TowerPlaneRec(const PlaneRec& p) :
planePtr(NULL),
clearedToLand(false),
clearedToLineUp(false),
finalAcknowledged(false),
rwyVacatedReported(false),
rwyVacatedAcknowledged(false),
+ goAroundReported(false),
instructedToGoAround(false),
onRwy(false),
nextOnRwy(false),
plane = p;
}
-TowerPlaneRec::TowerPlaneRec(Point3D pt) :
+TowerPlaneRec::TowerPlaneRec(const Point3D& pt) :
planePtr(NULL),
clearedToLand(false),
clearedToLineUp(false),
finalAcknowledged(false),
rwyVacatedReported(false),
rwyVacatedAcknowledged(false),
+ goAroundReported(false),
instructedToGoAround(false),
onRwy(false),
nextOnRwy(false),
pos = pt;
}
-TowerPlaneRec::TowerPlaneRec(PlaneRec p, Point3D pt) :
+TowerPlaneRec::TowerPlaneRec(const PlaneRec& p, const Point3D& pt) :
planePtr(NULL),
clearedToLand(false),
clearedToLineUp(false),
finalAcknowledged(false),
rwyVacatedReported(false),
rwyVacatedAcknowledged(false),
+ goAroundReported(false),
instructedToGoAround(false),
onRwy(false),
nextOnRwy(false),
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.
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).
+
+At the moment planes in the lists are not guaranteed to always have a sensible ETA - it should be set as part of AddList functions, and lists should only be accessed this way. (FAIRLY MAJOR).
*******************************************/
-FGTower::FGTower() {
+FGTower::FGTower() :
+ separateGround(true),
+ ground(0)
+{
ATCmgr = globals->get_ATC_mgr();
_type = TOWER;
update_count_max = 15;
holdListItr = holdList.begin();
- appList.clear();
appListItr = appList.begin();
depListItr = depList.begin();
rwyListItr = rwyList.begin();
circuitListItr = circuitList.begin();
trafficListItr = trafficList.begin();
- vacatedList.clear();
vacatedListItr = vacatedList.begin();
freqClear = true;
void FGTower::Init() {
//cout << "Initialising tower " << ident << '\n';
- display = false;
// Pointers to user's position
user_lon_node = fgGetNode("/position/longitude-deg", true);
ground = new FGGround(ident);
separateGround = false;
ground->Init();
- if(display) {
+ if(_display) {
ground->SetDisplay();
} else {
ground->SetNoDisplay();
ground = new FGGround(ident);
separateGround = false;
ground->Init();
- if(display) {
+ if(_display) {
ground->SetDisplay();
} else {
ground->SetNoDisplay();
ground = new FGGround(ident);
separateGround = false;
ground->Init();
- if(display) {
+ if(_display) {
ground->SetDisplay();
} else {
ground->SetNoDisplay();
// TODO - attempt to get a departure control pointer to see if we need to hand off departing traffic to departure.
// Get the airport elevation
- aptElev = dclGetAirportElev(ident.c_str());
+ aptElev = fgGetAirportElev(ident.c_str());
// TODO - this function only assumes one active rwy.
DoRwyDetails();
t->planePtr = NULL;
t->clearedToTakeOff = true;
rwyList.push_back(t);
+ rwyListItr = rwyList.begin();
departed = false;
} else {
//cout << "User not on active runway\n";
// 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))) {
- 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);
+ //cout << ident << " ADD 0\n";
+ current_atcdialog->add_entry(ident, "@AP Tower, @CS @MI miles @CD of the airport for full stop with ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
}
}
}
// 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();
// 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;
fgSetDouble("/AI/user/ortho-y", user_ortho_pos.y());
fgSetDouble("/AI/user/elev", user_elev_node->getDoubleValue());
}
+ */
//cout << "Done T" << endl;
}
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);
string trns = t->plane.callsign;
trns += " ";
trns += name;
- trns += " Tower";
+ trns += " Tower,";
// Should we clear staight in or for downwind entry?
// For now we'll clear straight in if greater than 1km from a line drawn through the threshold perpendicular to the rwy.
// Later on we might check the actual heading and direct some of those to enter on downwind or base.
trns += " Report three mile straight-in runway ";
t->opType = STRAIGHT_IN;
if(t->isUser) {
- current_atcdialog->add_entry(ident, "@AP Tower @CS @MI mile final Runway @RW", "Report Final", TOWER, (int)USER_REPORT_3_MILE_FINAL);
+ current_atcdialog->add_entry(ident, "@AP Tower, @CS @MI mile final Runway @RW", "Report Final", TOWER, (int)USER_REPORT_3_MILE_FINAL);
} else {
t->planePtr->RegisterTransmission(14);
}
t->opType = CIRCUIT;
// leave it in the app list until it gets into pattern though.
if(t->isUser) {
- current_atcdialog->add_entry(ident, "@AP Tower @CS Downwind @RW", "Report Downwind", TOWER, (int)USER_REPORT_DOWNWIND);
+ current_atcdialog->add_entry(ident, "@AP Tower, @CS Downwind @RW", "Report Downwind", TOWER, (int)USER_REPORT_DOWNWIND);
} else {
t->planePtr->RegisterTransmission(15);
}
}
trns += ConvertRwyNumToSpokenString(activeRwy);
- if(display) {
- globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
+ if(_display) {
+ pending_transmission = trns;
+ Transmit();
} else {
//cout << "Not displaying, trns was " << trns << '\n';
}
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) {
ClearHoldingPlane(t);
t->leg = TAKEOFF_ROLL;
rwyList.push_back(t);
+ rwyListItr = rwyList.begin();
rwyOccupied = true;
// WARNING - WE ARE ASSUMING ONLY ONE PLANE REPORTING HOLD AT A TIME BELOW
// FIXME TODO - FIX THIS!!!
- if(holdList.size()) {
+ if(!holdList.empty()) {
if(holdListItr == holdList.end()) {
holdListItr = holdList.begin();
}
// Not currently sure under which circumstances we do or don't bother transmitting this.
string trns = t->plane.callsign;
trns += " hold position";
- if(display) {
- globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
+ if(_display) {
+ pending_transmission = trns;
+ Transmit();
}
// TODO - add some idea of what traffic is blocking him.
}
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);
}
trns += " continue approach";
t->clearedToLand = false;
}
- if(display && disp) {
- globals->get_ATC_display()->RegisterSingleMessage(trns);
+ if(_display && disp) {
+ pending_transmission = trns;
+ Transmit();
}
t->finalAcknowledged = true;
} else if(t->rwyVacatedReported && !(t->rwyVacatedAcknowledged)) {
//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
+ TowerPlaneRec* tc = NULL;
+ for(tower_plane_rec_list_iterator twrItr = circuitList.begin(); twrItr != circuitList.end(); twrItr++) {
+ if((*twrItr)->plane.callsign == responseID) break;
+ tc = *twrItr;
+ ++i;
+ }
+ if(i > 1) { cc = true; }
+ doThresholdETACalc();
+ TowerPlaneRec* tf = NULL;
+ for(tower_plane_rec_list_iterator twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
+ if((*twrItr)->eta < (t->eta + 45)) {
+ a++;
+ tf = *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 += " ";
+ TowerPlaneRec* tt = NULL;
+ if((i == 1) && rwyList.empty() && (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) {
+ //First set tt to point to the correct preceding plane - final or circuit
+ if(tc && tf) {
+ tt = (tf->eta < tc->eta ? tf : tc);
+ } else if(tc) {
+ tt = tc;
+ } else if(tf) {
+ tt = tf;
+ } else {
+ // We should never get here!
+ SG_LOG(SG_ATC, SG_ALERT, "ALERT - Logic error in FGTower::ProcessDownwindReport");
+ return;
+ }
+ trns += "Follow the ";
+ string s = tt->plane.callsign;
+ int p = s.find('-');
+ s = s.substr(0,p);
+ trns += s;
+ if((tt->opType) == CIRCUIT) {
+ PatternLeg leg;
+ if(tt->isUser) {
+ leg = tt->leg;
+ } else {
+ leg = tt->planePtr->GetLeg();
+ }
+ if(leg == FINAL) {
+ trns += " on final";
+ } else if(leg == TURN4) {
+ trns += " turning final";
+ } else if(leg == BASE) {
+ trns += " on base";
+ } else if(leg == 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) {
+ pending_transmission = trns;
+ Transmit();
+ }
+ 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";
+ if(t->isUser) current_atcdialog->remove_entry(ident, USER_REPORT_GOING_AROUND, TOWER);
string trns = t->plane.callsign;
if(separateGround) {
trns += " Contact ground on ";
if(!t->isUser) t->planePtr->RegisterTransmission(5);
} else {
// Cop-out!!
- trns += " cleared for taxi to the GA parking";
+ trns += " cleared for taxi to general aviation parking";
if(!t->isUser) t->planePtr->RegisterTransmission(6); // TODO - this is a mega-hack!!
}
//cout << "trns = " << trns << '\n';
- if(display) {
- globals->get_ATC_display()->RegisterSingleMessage(trns);
+ if(_display) {
+ pending_transmission = trns;
+ Transmit();
}
+ RemoveFromRwyList(t->plane.callsign);
+ AddToVacatedList(t);
// 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) {
departed = false;
timeSinceLastDeparture = 0.0;
}
- if(display) {
- globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
+ if(_display) {
+ pending_transmission = trns;
+ Transmit();
}
//cout << "Done ClearHoldingPlane " << endl;
}
// Do one plane from the hold list
void FGTower::CheckHoldList(double dt) {
//cout << "Entering CheckHoldList..." << endl;
- if(holdList.size()) {
+ if(!holdList.empty()) {
//cout << "*holdListItr = " << *holdListItr << endl;
if(holdListItr == holdList.end()) {
holdListItr = holdList.begin();
ClearHoldingPlane(t);
t->leg = TAKEOFF_ROLL;
rwyList.push_back(t);
+ rwyListItr = rwyList.begin();
rwyOccupied = true;
holdList.erase(holdListItr);
holdListItr = holdList.begin();
+ if (holdList.empty())
+ return;
}
}
// TODO - rationalise the considerable code duplication above!
downwind_leg_pos = 0.0;
crosswind_leg_pos = 0.0;
- if(circuitList.size()) { // Do one plane from the circuit
+ if(!circuitList.empty()) { // Do one plane from the circuit
if(circuitListItr == circuitList.end()) {
circuitListItr = circuitList.begin();
}
//cout << " ho = " << ho << " abs(ho = " << abs(ho) << '\n';
// TODO FIXME - get the wind and convert this to track, or otherwise use track somehow!!!
// If it's gusty might need to filter the value, although we are leaving 30 degrees each way leeway!
- if(abs(ho) < 30) {
+ if(fabs(ho) < 30) {
// could be either takeoff, climbout or landing - check orthopos.y
//cout << "tortho.y = " << tortho.y() << '\n';
if((tortho.y() < 0) || (t->leg == TURN4) || (t->leg == FINAL)) {
t->opType = OUTBOUND; // TODO - could check if the user has climbed significantly above circuit altitude as well.
// Since we are unknown operation we should be in depList already.
//cout << ident << " Removing user from circuitList (TTT_UNKNOWN)\n";
- circuitList.erase(circuitListItr);
+ circuitListItr = circuitList.erase(circuitListItr);
RemoveFromTrafficList(t->plane.callsign);
- circuitListItr = circuitList.begin();
+ if (circuitList.empty())
+ return;
}
} else if(t->opType == CIRCUIT) {
if(tortho.y() > 10000) {
// 10 km out - assume the user has abandoned the circuit!!
t->opType = OUTBOUND;
depList.push_back(t);
+ depListItr = depList.begin();
//cout << ident << " removing user from circuitList (CIRCUIT)\n";
- circuitList.erase(circuitListItr);
- circuitListItr = circuitList.begin();
+ circuitListItr = circuitList.erase(circuitListItr);
+ if (circuitList.empty())
+ return;
}
}
}
- } else if(abs(ho) < 60) {
+ } else if(fabs(ho) < 60) {
// turn1 or turn 4
// TODO - either fix or doublecheck this hack by looking at heading and pattern direction
if((t->leg == CLIMBOUT) || (t->leg == TURN1)) {
t->leg = TURN4;
//cout << "Turn4\n";
}
- } else if(abs(ho) < 120) {
+ } else if(fabs(ho) < 120) {
// crosswind or base
// TODO - either fix or doublecheck this hack by looking at heading and pattern direction
if((t->leg == TURN1) || (t->leg == CROSSWIND)) {
t->leg = BASE;
//cout << "Base\n";
}
- } else if(abs(ho) < 150) {
+ } else if(fabs(ho) < 150) {
// turn2 or turn 3
// TODO - either fix or doublecheck this hack by looking at heading and pattern direction
if((t->leg == CROSSWIND) || (t->leg == TURN2)) {
}
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);
+ }
+ 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) {
+ // The whip through the appList is a hack since currently t->nextOnRwy doesn't always work
+ // TODO - fix this!
+ bool cf = false;
+ for(tower_plane_rec_list_iterator twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
+ if((*twrItr)->eta < t->eta) {
+ cf = true;
}
- 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->nextOnRwy && !cf) {
+ 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) {
circuitListItr = circuitList.erase(circuitListItr);
if(circuitListItr == circuitList.end() ) {
circuitListItr = circuitList.begin();
+ // avoid increment of circuitListItr (would increment to second element, or crash if no element left)
+ return;
}
}
++circuitListItr;
//cout << "Size of rwylist was " << rwyList.size() << '\n';
//cout << "Size of vacatedList was " << vacatedList.size() << '\n';
RemoveFromRwyList(t->plane.callsign);
- vacatedList.push_back(t);
+ AddToVacatedList(t);
//cout << "Size of rwylist is " << rwyList.size() << '\n';
//cout << "Size of vacatedList is " << vacatedList.size() << '\n';
// At the moment we wait until Runway Vacated is reported by the plane before telling to contact ground etc.
// It's possible we could be a bit more proactive about this.
} else if(t->opType == OUTBOUND) {
depList.push_back(t);
+ depListItr = depList.begin();
rwyList.pop_front();
departed = true;
timeSinceLastDeparture = 0.0;
} else if(t->opType == CIRCUIT) {
//cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
circuitList.push_back(t);
+ circuitListItr = circuitList.begin();
AddToTrafficList(t);
rwyList.pop_front();
departed = true;
timeSinceLastDeparture = 0.0;
} else if(t->opType == TTT_UNKNOWN) {
depList.push_back(t);
+ depListItr = depList.begin();
//cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
circuitList.push_back(t);
+ circuitListItr = circuitList.begin();
AddToTrafficList(t);
rwyList.pop_front();
departed = true;
void FGTower::CheckApproachList(double dt) {
//cout << "CheckApproachList called for " << ident << endl;
//cout << "AppList.size is " << appList.size() << endl;
- if(appList.size()) {
+ if(!appList.empty()) {
if(appListItr == appList.end()) {
appListItr = appList.begin();
}
} else {
// TODO - set/update the position if it's an AI plane
}
+ doThresholdETACalc(); // We need this here because planes in the lists are not guaranteed to *always* have the correct ETA
//cout << "eta is " << t->eta << ", rwy is " << (rwyList.size() ? "occupied " : "clear ") << '\n';
if(t->eta < 12 && rwyList.size() && !(t->instructedToGoAround)) {
// TODO - need to make this more sophisticated
// 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
// TODO - add Go-around ack to comm options,
// remove report rwy vacated. (possibly).
}
- }
- if(t->nextOnRwy && !(t->clearedToLand) && !(t->instructedToGoAround)) {
- // check distance away and whether runway occupied
- // and schedule transmission if necessary
+ } else if(t->eta < 90 && !t->clearedToLand) {
+ //doThresholdETACalc();
+ doThresholdUseOrder();
+ // The whip through the appList is a hack since currently t->nextOnRwy doesn't always work
+ // TODO - fix this!
+ bool cf = false;
+ for(tower_plane_rec_list_iterator twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
+ if((*twrItr)->eta < t->eta) {
+ cf = true;
+ }
+ }
+ if(t->nextOnRwy && !cf) {
+ 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";
+ }
}
// Check for landing...
if(appListItr == appList.end() ) {
appListItr = appList.begin();
}
+ if (appList.empty())
+ return;
+
}
++appListItr;
// Do one plane from the departure list
void FGTower::CheckDepartureList(double dt) {
- if(depList.size()) {
+ if(!depList.empty()) {
if(depListItr == depList.end()) {
depListItr = depList.begin();
}
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();
- 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);
+ //cout << "ADD A\n";
+ current_atcdialog->add_entry(ident, "@AP Tower, @CS @MI miles @CD of the airport for full stop with ATIS", "Contact tower for VFR arrival (full stop)", TOWER, (int)USER_REQUEST_VFR_ARRIVAL_FULL_STOP);
} else {
// Send a clear-of-airspace signal
// TODO - implement this once we actually have departing AI traffic (currently all circuits or arrivals).
bool rwyGood = globals->get_runways()->search(ident, int(hdg), &runway);
if(rwyGood) {
//cout << "RUNWAY GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOD\n";
- activeRwy = runway.rwy_no;
- rwy.rwyID = runway.rwy_no;
+ activeRwy = runway._rwy_no;
+ rwy.rwyID = runway._rwy_no;
SG_LOG(SG_ATC, SG_INFO, "Active runway for airport " << ident << " is " << activeRwy);
// Get the threshold position
- double other_way = runway.heading - 180.0;
+ double other_way = runway._heading - 180.0;
while(other_way <= 0.0) {
other_way += 360.0;
}
// move to the +l end/center of the runway
- //cout << "Runway center is at " << runway.lon << ", " << runway.lat << '\n';
- Point3D origin = Point3D(runway.lon, runway.lat, aptElev);
+ //cout << "Runway center is at " << runway._lon << ", " << runway._lat << '\n';
+ Point3D origin = Point3D(runway._lon, runway._lat, aptElev);
Point3D ref = origin;
double tshlon, tshlat, tshr;
double tolon, tolat, tor;
- rwy.length = runway.length * SG_FEET_TO_METER;
- rwy.width = runway.width * SG_FEET_TO_METER;
+ rwy.length = runway._length * SG_FEET_TO_METER;
+ rwy.width = runway._width * SG_FEET_TO_METER;
geo_direct_wgs_84 ( aptElev, ref.lat(), ref.lon(), other_way,
rwy.length / 2.0 - 25.0, &tshlat, &tshlon, &tshr );
- geo_direct_wgs_84 ( aptElev, ref.lat(), ref.lon(), runway.heading,
+ geo_direct_wgs_84 ( aptElev, ref.lat(), ref.lon(), runway._heading,
rwy.length / 2.0 - 25.0, &tolat, &tolon, &tor );
// Note - 25 meters in from the runway end is a bit of a hack to put the plane ahead of the user.
// now copy what we need out of runway into rwy
Point3D takeoff_end = Point3D(tolon, tolat, aptElev);
//cout << "Threshold position = " << tshlon << ", " << tshlat << ", " << aptElev << '\n';
//cout << "Takeoff position = " << tolon << ", " << tolat << ", " << aptElev << '\n';
- rwy.hdg = runway.heading;
+ rwy.hdg = runway._heading;
// Set the projection for the local area based on this active runway
ortho.Init(rwy.threshold_pos, rwy.hdg);
rwy.end1ortho = ortho.ConvertToLocal(rwy.threshold_pos); // should come out as zero
// Figure out if a given position lies on the active runway
// Might have to change when we consider more than one active rwy.
-bool FGTower::OnActiveRunway(Point3D pt) {
+bool FGTower::OnActiveRunway(const Point3D& pt) {
// TODO - check that the centre calculation below isn't confused by displaced thesholds etc.
Point3D xyc((rwy.end1ortho.x() + rwy.end2ortho.x())/2.0, (rwy.end1ortho.y() + rwy.end2ortho.y())/2.0, 0.0);
Point3D xyp = ortho.ConvertToLocal(pt);
// Figure out if a given position lies on any runway or not
// Only call this at startup - reading the runways database is expensive and needs to be fixed!
-bool FGTower::OnAnyRunway(Point3D pt) {
+bool FGTower::OnAnyRunway(const Point3D& pt) {
ATCData ad;
double dist = current_commlist->FindClosest(lon, lat, elev, ad, TOWER, 10.0);
if(dist < 0.0) {
SG_LOG(SG_ATC, SG_WARN, "Unable to find any runways for airport ID " << ad.ident << " in FGTower");
}
bool on = false;
- while(runway.id == ad.ident) {
+ while(runway._id == ad.ident) {
on = OnRunway(pt, runway);
- //cout << "Runway " << runway.rwy_no << ": On = " << (on ? "true\n" : "false\n");
+ //cout << "Runway " << runway._rwy_no << ": On = " << (on ? "true\n" : "false\n");
if(on) return(true);
globals->get_runways()->next(&runway);
}
// Returns true if successful
-bool FGTower::RemoveFromTrafficList(string id) {
+bool FGTower::RemoveFromTrafficList(const string& id) {
tower_plane_rec_list_iterator twrItr;
for(twrItr = trafficList.begin(); twrItr != trafficList.end(); twrItr++) {
TowerPlaneRec* tpr = *twrItr;
// Returns true if successful
-bool FGTower::RemoveFromAppList(string id) {
+bool FGTower::RemoveFromAppList(const string& id) {
tower_plane_rec_list_iterator twrItr;
for(twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
TowerPlaneRec* tpr = *twrItr;
}
// Returns true if successful
-bool FGTower::RemoveFromRwyList(string id) {
+bool FGTower::RemoveFromRwyList(const string& id) {
tower_plane_rec_list_iterator twrItr;
for(twrItr = rwyList.begin(); twrItr != rwyList.end(); twrItr++) {
TowerPlaneRec* tpr = *twrItr;
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;
// It depends on what the two planes are doing and whether there's a conflict what we do.
if(tpr->eta - t->eta > separation_time) { // No probs, plane 2 can squeeze in before plane 1 with no apparent conflict
circuitList.insert(twrItr, t);
+ circuitListItr = circuitList.begin();
} else { // Ooops - this ones tricky - we have a potential conflict!
conflict = true;
// HACK - just add anyway for now and flag conflict.
circuitList.insert(twrItr, t);
+ circuitListItr = circuitList.begin();
}
//cout << "\tC\t" << circuitList.size() << '\n';
return(conflict);
// If we get here we must be at the end of the list, or maybe the list is empty.
//cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
circuitList.push_back(t); // TODO - check the separation with the preceding plane for the conflict flag.
+ circuitListItr = circuitList.begin();
//cout << "\tE\t" << circuitList.size() << endl;
return(conflict);
}
+// Add to vacated list only if not already present
+void FGTower::AddToVacatedList(TowerPlaneRec* t) {
+ tower_plane_rec_list_iterator twrItr;
+ bool found = false;
+ for(twrItr = vacatedList.begin(); twrItr != vacatedList.end(); twrItr++) {
+ if((*twrItr)->plane.callsign == t->plane.callsign) {
+ found = true;
+ }
+ }
+ if(found) return;
+ vacatedList.push_back(t);
+}
+
// Calculate the eta of a plane to the threshold.
// For ground traffic this is the fastest they can get there.
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
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';
}
-void FGTower::ContactAtHoldShort(PlaneRec plane, FGAIPlane* requestee, tower_traffic_type operation) {
+void FGTower::ContactAtHoldShort(const PlaneRec& plane, FGAIPlane* requestee, tower_traffic_type operation) {
// HACK - assume that anything contacting at hold short is new for now - FIXME LATER
TowerPlaneRec* t = new TowerPlaneRec;
t->plane = plane;
// Register the presence of an AI plane at a point where contact would already have been made in real life
// CAUTION - currently it is assumed that this plane's callsign is unique - it is up to AIMgr to generate unique callsigns.
-void FGTower::RegisterAIPlane(PlaneRec plane, FGAIPlane* ai, tower_traffic_type op, PatternLeg lg) {
+void FGTower::RegisterAIPlane(const PlaneRec& plane, FGAIPlane* ai, const tower_traffic_type& op, const PatternLeg& lg) {
// At the moment this is only going to be tested with inserting an AI plane on downwind
TowerPlaneRec* t = new TowerPlaneRec;
t->plane = plane;
CalcETA(t);
if(op == CIRCUIT && lg != LEG_UNKNOWN) {
- cout << "AAAAAAAAAAAAAAAaa" << endl;
AddToCircuitList(t);
- cout << "BBBBBBBBBBBBBBbbb" << endl;
} else {
// FLAG A WARNING
}
doThresholdUseOrder();
}
-void FGTower::DeregisterAIPlane(string id) {
+void FGTower::DeregisterAIPlane(const string& id) {
RemovePlane(id);
}
// eg "Cessna Charlie Foxtrot Golf Foxtrot Sierra eight miles South of the airport for full stop with Bravo"
// This function probably only called via user interaction - AI planes will have an overloaded function taking a planerec.
// opt defaults to AIP_LT_UNKNOWN
-void FGTower::VFRArrivalContact(string ID, LandingType opt) {
+void FGTower::VFRArrivalContact(const string& ID, const LandingType& opt) {
//cout << "USER Request Landing Clearance called for ID " << ID << '\n';
// For now we'll assume that the user is a light plane and can get him/her to join the circuit if necessary.
responseReqd = true;
appList.push_back(t); // Not necessarily permanent
+ appListItr = appList.begin();
AddToTrafficList(t);
current_atcdialog->remove_entry(ident, USER_REQUEST_VFR_ARRIVAL, TOWER);
}
// landingType defaults to AIP_LT_UNKNOWN
-void FGTower::VFRArrivalContact(PlaneRec plane, FGAIPlane* requestee, LandingType lt) {
+void FGTower::VFRArrivalContact(const PlaneRec& plane, FGAIPlane* requestee, const LandingType& lt) {
//cout << "VFRArrivalContact called for plane " << plane.callsign << " at " << ident << '\n';
// Possible hack - assume this plane is new for now - TODO - should check really
TowerPlaneRec* t = new TowerPlaneRec;
//cout << "Before adding, appList.size = " << appList.size() << " at " << ident << '\n';
appList.push_back(t); // Not necessarily permanent
+ appListItr = appList.begin();
//cout << "After adding, appList.size = " << appList.size() << " at " << ident << '\n';
AddToTrafficList(t);
}
-void FGTower::RequestDepartureClearance(string ID) {
+void FGTower::RequestDepartureClearance(const string& ID) {
//cout << "Request Departure Clearance called...\n";
}
-void FGTower::ReportFinal(string ID) {
+void FGTower::ReportFinal(const string& ID) {
//cout << "Report Final Called at tower " << ident << " by plane " << ID << '\n';
+ string uid=ID;
if(ID == "USER") {
- ID = fgGetString("/sim/user/callsign");
+ uid = fgGetString("/sim/user/callsign");
current_atcdialog->remove_entry(ident, USER_REPORT_3_MILE_FINAL, TOWER);
}
- TowerPlaneRec* t = FindPlane(ID);
+ TowerPlaneRec* t = FindPlane(uid);
if(t) {
t->finalReported = true;
t->finalAcknowledged = false;
}
}
-void FGTower::ReportLongFinal(string ID) {
+void FGTower::ReportLongFinal(const string& ID) {
+ string uid=ID;
if(ID == "USER") {
- ID = fgGetString("/sim/user/callsign");
+ uid = fgGetString("/sim/user/callsign");
current_atcdialog->remove_entry(ident, USER_REPORT_3_MILE_FINAL, TOWER);
}
- TowerPlaneRec* t = FindPlane(ID);
+ TowerPlaneRec* t = FindPlane(uid);
if(t) {
t->longFinalReported = true;
t->longFinalAcknowledged = false;
//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) {
+void FGTower::ReportRunwayVacated(const string& ID) {
//cout << "Report Runway Vacated Called at tower " << ident << " by plane " << ID << '\n';
+ string uid=ID;
if(ID == "USER") {
- ID = fgGetString("/sim/user/callsign");
+ uid = fgGetString("/sim/user/callsign");
current_atcdialog->remove_entry(ident, USER_REPORT_RWY_VACATED, TOWER);
}
- TowerPlaneRec* t = FindPlane(ID);
+ TowerPlaneRec* t = FindPlane(uid);
if(t) {
//cout << "Found it...\n";
t->rwyVacatedReported = true;
}
}
-TowerPlaneRec* FGTower::FindPlane(string ID) {
+TowerPlaneRec* FGTower::FindPlane(const string& ID) {
//cout << "FindPlane called for " << ID << "...\n";
tower_plane_rec_list_iterator twrItr;
// Do the approach list first
return(NULL);
}
-void FGTower::RemovePlane(string ID) {
+void FGTower::RemovePlane(const string& ID) {
//cout << ident << " RemovePlane called for " << ID << '\n';
// We have to be careful here - we want to erase the plane from all lists it is in,
// but we can only delete it once, AT THE END.
TowerPlaneRec* t = NULL;
tower_plane_rec_list_iterator twrItr;
- for(twrItr = appList.begin(); twrItr != appList.end(); twrItr++) {
+ for(twrItr = appList.begin(); twrItr != appList.end();) {
if((*twrItr)->plane.callsign == ID) {
t = *twrItr;
twrItr = appList.erase(twrItr);
appListItr = appList.begin();
- }
+ // HACK: aircraft are sometimes more than once in a list, so we need to
+ // remove them all before we can delete the TowerPlaneRec class
+ //break;
+ } else
+ ++twrItr;
}
- for(twrItr = depList.begin(); twrItr != depList.end(); twrItr++) {
+ for(twrItr = depList.begin(); twrItr != depList.end();) {
if((*twrItr)->plane.callsign == ID) {
t = *twrItr;
twrItr = depList.erase(twrItr);
depListItr = depList.begin();
- }
+ } else
+ ++twrItr;
}
- for(twrItr = circuitList.begin(); twrItr != circuitList.end(); twrItr++) {
+ for(twrItr = circuitList.begin(); twrItr != circuitList.end();) {
if((*twrItr)->plane.callsign == ID) {
t = *twrItr;
twrItr = circuitList.erase(twrItr);
circuitListItr = circuitList.begin();
- }
+ } else
+ ++twrItr;
}
- for(twrItr = holdList.begin(); twrItr != holdList.end(); twrItr++) {
+ for(twrItr = holdList.begin(); twrItr != holdList.end();) {
if((*twrItr)->plane.callsign == ID) {
t = *twrItr;
twrItr = holdList.erase(twrItr);
holdListItr = holdList.begin();
- }
+ } else
+ ++twrItr;
}
- for(twrItr = rwyList.begin(); twrItr != rwyList.end(); twrItr++) {
+ for(twrItr = rwyList.begin(); twrItr != rwyList.end();) {
if((*twrItr)->plane.callsign == ID) {
t = *twrItr;
twrItr = rwyList.erase(twrItr);
rwyListItr = rwyList.begin();
- }
+ } else
+ ++twrItr;
}
- for(twrItr = vacatedList.begin(); twrItr != vacatedList.end(); twrItr++) {
+ for(twrItr = vacatedList.begin(); twrItr != vacatedList.end();) {
if((*twrItr)->plane.callsign == ID) {
t = *twrItr;
twrItr = vacatedList.erase(twrItr);
vacatedListItr = vacatedList.begin();
- }
+ } else
+ ++twrItr;
}
- for(twrItr = trafficList.begin(); twrItr != trafficList.end(); twrItr++) {
+ for(twrItr = trafficList.begin(); twrItr != trafficList.end();) {
if((*twrItr)->plane.callsign == ID) {
t = *twrItr;
twrItr = trafficList.erase(twrItr);
trafficListItr = trafficList.begin();
- }
+ } else
+ ++twrItr;
}
- // And finally, delete the record if we found it.
- if(t) delete t;
+ // And finally, delete the record.
+ delete t;
}
-void FGTower::ReportDownwind(string ID) {
+void FGTower::ReportDownwind(const string& ID) {
//cout << "ReportDownwind(...) called\n";
+ string uid=ID;
if(ID == "USER") {
- ID = fgGetString("/sim/user/callsign");
+ uid = fgGetString("/sim/user/callsign");
current_atcdialog->remove_entry(ident, USER_REPORT_DOWNWIND, TOWER);
}
- TowerPlaneRec* t = FindPlane(ID);
+ TowerPlaneRec* t = FindPlane(uid);
if(t) {
t->downwindReported = true;
responseReqd = true;
// the moment that would b&gg?r up the constraint position calculations.
RemoveFromAppList(ID);
t->leg = DOWNWIND;
- t->pos = t->planePtr->GetPos();
+ 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(...)");
+ }
+}
+
+void FGTower::ReportGoingAround(const string& ID) {
+ string uid=ID;
+ if(ID == "USER") {
+ uid = 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(uid);
+ 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);
- //cout << "DDDDDDDDDDDDDDDDdddddddd" << endl;
AddToCircuitList(t);
- //cout << "EEEEEEEEEEEEEEEEEEEEeeeeee" << endl;
} else {
SG_LOG(SG_ATC, SG_WARN, "WARNING: Unable to find plane " << ID << " in FGTower::ReportDownwind(...)");
}
int check = 0; // If mes gets overflowed the while loop can go infinite
while ( strchr(&mes[0], crej) != NULL ) { // ie. loop until no more occurances of crej ('@') found
pos = strchr( &mes[0], crej );
- bcopy(pos, &tag[0], 3);
+ memmove(&tag[0], pos, 3);
tag[3] = '\0';
int i;
len = 0;
//break;
//}
//}
- if ( mes != "" ) return mes;
+ if ( mes[0] ) return mes;
else return "No transmission found";
}