2002-04-03 23:54:44 +00:00
// FGTower - a class to provide tower control at towered airports.
//
// Written by David Luff, started March 2002.
//
// Copyright (C) 2002 David C. Luff - david.luff@nottingham.ac.uk
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// 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.
2003-11-14 09:41:31 +00:00
# ifdef HAVE_CONFIG_H
# include <config.h>
# endif
# ifdef HAVE_STRINGS_H
# include <strings.h> // bcopy()
# else
# include <string.h> // MSVC doesn't have strings.h
# endif
2003-11-12 10:06:18 +00:00
2002-04-03 23:54:44 +00:00
# include <Main/globals.hxx>
2003-03-27 15:41:09 +00:00
# include <Airports/runways.hxx>
# include <simgear/math/sg_geodesy.hxx>
# include <simgear/debug/logstream.hxx>
2002-04-03 23:54:44 +00:00
# include "tower.hxx"
# include "ATCdisplay.hxx"
2002-12-04 20:06:20 +00:00
# include "ATCmgr.hxx"
2003-03-27 15:41:09 +00:00
# include "ATCutils.hxx"
2003-11-05 10:06:57 +00:00
# include "ATCDialog.hxx"
2003-03-27 15:41:09 +00:00
# include "commlist.hxx"
2003-04-15 22:55:22 +00:00
# include "AILocalTraffic.hxx"
2002-12-04 20:06:20 +00:00
2003-03-11 17:15:23 +00:00
SG_USING_STD ( cout ) ;
2002-12-04 20:06:20 +00:00
// TowerPlaneRec
TowerPlaneRec : : TowerPlaneRec ( ) :
2004-01-23 17:18:24 +00:00
planePtr ( NULL ) ,
2003-11-05 10:06:57 +00:00
clearedToLand ( false ) ,
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
downwindReported ( false ) ,
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
finalAcknowledged ( false ) ,
2003-11-05 17:24:58 +00:00
rwyVacatedReported ( false ) ,
rwyVacatedAcknowledged ( false ) ,
2004-03-09 16:20:55 +00:00
goAroundReported ( false ) ,
2003-11-05 10:06:57 +00:00
instructedToGoAround ( false ) ,
onRwy ( false ) ,
nextOnRwy ( false ) ,
vfrArrivalReported ( false ) ,
vfrArrivalAcknowledged ( false ) ,
opType ( TTT_UNKNOWN ) ,
leg ( LEG_UNKNOWN ) ,
landingType ( AIP_LT_UNKNOWN ) ,
2004-01-23 17:18:24 +00:00
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane . callsign = " UNKNOWN " ;
2002-12-04 20:06:20 +00:00
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec : : TowerPlaneRec ( const PlaneRec & p ) :
2004-01-23 17:18:24 +00:00
planePtr ( NULL ) ,
2003-11-05 10:06:57 +00:00
clearedToLand ( false ) ,
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
downwindReported ( false ) ,
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
finalAcknowledged ( false ) ,
2003-11-05 17:24:58 +00:00
rwyVacatedReported ( false ) ,
rwyVacatedAcknowledged ( false ) ,
2004-03-09 16:20:55 +00:00
goAroundReported ( false ) ,
2003-11-05 10:06:57 +00:00
instructedToGoAround ( false ) ,
onRwy ( false ) ,
nextOnRwy ( false ) ,
vfrArrivalReported ( false ) ,
vfrArrivalAcknowledged ( false ) ,
opType ( TTT_UNKNOWN ) ,
leg ( LEG_UNKNOWN ) ,
landingType ( AIP_LT_UNKNOWN ) ,
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane = p ;
2002-12-04 20:06:20 +00:00
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec : : TowerPlaneRec ( const Point3D & pt ) :
2004-01-23 17:18:24 +00:00
planePtr ( NULL ) ,
2003-11-05 10:06:57 +00:00
clearedToLand ( false ) ,
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
downwindReported ( false ) ,
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
finalAcknowledged ( false ) ,
2003-11-05 17:24:58 +00:00
rwyVacatedReported ( false ) ,
rwyVacatedAcknowledged ( false ) ,
2004-03-09 16:20:55 +00:00
goAroundReported ( false ) ,
2003-11-05 10:06:57 +00:00
instructedToGoAround ( false ) ,
onRwy ( false ) ,
nextOnRwy ( false ) ,
vfrArrivalReported ( false ) ,
vfrArrivalAcknowledged ( false ) ,
opType ( TTT_UNKNOWN ) ,
leg ( LEG_UNKNOWN ) ,
landingType ( AIP_LT_UNKNOWN ) ,
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane . callsign = " UNKNOWN " ;
2002-12-04 20:06:20 +00:00
pos = pt ;
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec : : TowerPlaneRec ( const PlaneRec & p , const Point3D & pt ) :
2004-01-23 17:18:24 +00:00
planePtr ( NULL ) ,
2003-11-05 10:06:57 +00:00
clearedToLand ( false ) ,
clearedToLineUp ( false ) ,
clearedToTakeOff ( false ) ,
holdShortReported ( false ) ,
downwindReported ( false ) ,
longFinalReported ( false ) ,
longFinalAcknowledged ( false ) ,
finalReported ( false ) ,
finalAcknowledged ( false ) ,
2003-11-05 17:24:58 +00:00
rwyVacatedReported ( false ) ,
rwyVacatedAcknowledged ( false ) ,
2004-03-09 16:20:55 +00:00
goAroundReported ( false ) ,
2003-11-05 10:06:57 +00:00
instructedToGoAround ( false ) ,
onRwy ( false ) ,
nextOnRwy ( false ) ,
vfrArrivalReported ( false ) ,
vfrArrivalAcknowledged ( false ) ,
opType ( TTT_UNKNOWN ) ,
leg ( LEG_UNKNOWN ) ,
landingType ( AIP_LT_UNKNOWN ) ,
isUser ( false )
2002-12-04 20:06:20 +00:00
{
2003-03-27 15:41:09 +00:00
plane = p ;
2002-12-04 20:06:20 +00:00
pos = pt ;
}
// FGTower
2002-04-03 23:54:44 +00:00
2003-10-16 11:28:44 +00:00
/*******************************************
TODO List
2004-01-23 17:18:24 +00:00
Currently user is assumed to have taken off again when leaving the runway - check speed / elev for taxiing - in . ( MAJOR )
2003-10-16 11:28:44 +00:00
2004-01-23 17:18:24 +00:00
Use track instead of heading to determine what leg of the circuit the user is flying . ( MINOR )
2003-10-16 11:28:44 +00:00
2004-01-23 17:18:24 +00:00
Use altitude as well as position to try to determine if the user has left the circuit . ( MEDIUM - other issues as well ) .
2003-10-16 11:28:44 +00:00
Currently HoldShortReported code assumes there will be only one plane holding for the runway at once and
2004-01-23 17:18:24 +00:00
will break when planes start queueing . ( CRITICAL )
Report - Runway - Vacated is left as only user ATC option following a go - around . ( MAJOR )
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 .
This can then screw up circuit ordering for other planes ( MEDIUM )
USER leaving circuit needs to be more robustly considered when intentions unknown
Currently only considered during climbout and breaks when user turns ( MEDIUM ) .
2003-10-16 11:28:44 +00:00
2004-01-23 17:18:24 +00:00
GetPos ( ) of the AI planes is called erratically - either too much or not enough . ( MINOR )
GO - AROUND is instructed very late at < 12 s 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 ! )
2004-03-09 16:20:55 +00:00
FGTower : : RemoveAllUserDialogOptions ( ) really ought to be replaced by an ATCDialog : : clear_entries ( ) function . ( MINOR - efficiency ) .
2004-03-20 04:12:05 +00:00
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 ) .
2003-10-16 11:28:44 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-04-03 23:54:44 +00:00
FGTower : : FGTower ( ) {
2003-03-10 13:41:37 +00:00
ATCmgr = globals - > get_ATC_mgr ( ) ;
2003-03-27 15:41:09 +00:00
2004-01-23 17:18:24 +00:00
_type = TOWER ;
2003-03-27 15:41:09 +00:00
// Init the property nodes - TODO - need to make sure we're getting surface winds.
wind_from_hdg = fgGetNode ( " /environment/wind-from-heading-deg " , true ) ;
2003-07-31 01:41:46 +00:00
wind_speed_knots = fgGetNode ( " /environment/wind-speed-kt " , true ) ;
2003-03-27 15:41:09 +00:00
2003-09-22 23:57:57 +00:00
update_count = 0 ;
update_count_max = 15 ;
2003-03-27 15:41:09 +00:00
holdListItr = holdList . begin ( ) ;
2004-01-23 17:18:24 +00:00
appList . clear ( ) ;
2003-03-27 15:41:09 +00:00
appListItr = appList . begin ( ) ;
depListItr = depList . begin ( ) ;
rwyListItr = rwyList . begin ( ) ;
circuitListItr = circuitList . begin ( ) ;
trafficListItr = trafficList . begin ( ) ;
2004-01-23 17:18:24 +00:00
vacatedList . clear ( ) ;
vacatedListItr = vacatedList . begin ( ) ;
2003-09-19 16:51:27 +00:00
freqClear = true ;
2003-09-22 23:57:57 +00:00
timeSinceLastDeparture = 9999 ;
departed = false ;
2003-11-04 12:00:14 +00:00
nominal_downwind_leg_pos = 1000.0 ;
nominal_base_leg_pos = - 1000.0 ;
// TODO - set nominal crosswind leg pos based on minimum distance from takeoff end of rwy.
2004-01-23 17:18:24 +00:00
_departureControlled = false ;
2002-04-03 23:54:44 +00:00
}
FGTower : : ~ FGTower ( ) {
2003-03-10 13:41:37 +00:00
if ( ! separateGround ) {
delete ground ;
}
2002-04-03 23:54:44 +00:00
}
void FGTower : : Init ( ) {
2004-01-23 17:18:24 +00:00
//cout << "Initialising tower " << ident << '\n';
2003-03-10 13:41:37 +00:00
2003-03-27 15:41:09 +00:00
// Pointers to user's position
user_lon_node = fgGetNode ( " /position/longitude-deg " , true ) ;
user_lat_node = fgGetNode ( " /position/latitude-deg " , true ) ;
user_elev_node = fgGetNode ( " /position/altitude-ft " , true ) ;
2003-04-15 22:55:22 +00:00
user_hdg_node = fgGetNode ( " /orientation/heading-deg " , true ) ;
2003-03-27 15:41:09 +00:00
2003-03-10 13:41:37 +00:00
// Need some way to initialise rwyOccupied flag correctly if the user is on the runway and to know its the user.
// I'll punt the startup issue for now though!!!
rwyOccupied = false ;
// Setup the ground control at this airport
AirportATC a ;
2003-03-27 15:41:09 +00:00
//cout << "Tower ident = " << ident << '\n';
2003-03-10 13:41:37 +00:00
if ( ATCmgr - > GetAirportATCDetails ( ident , & a ) ) {
if ( a . ground_freq ) { // Ground control
ground = ( FGGround * ) ATCmgr - > GetATCPointer ( ident , GROUND ) ;
separateGround = true ;
if ( ground = = NULL ) {
// Something has gone wrong :-(
2003-03-27 15:41:09 +00:00
SG_LOG ( SG_ATC , SG_WARN , " ERROR - ground has frequency but can't get ground pointer :-( " ) ;
2003-03-10 13:41:37 +00:00
ground = new FGGround ( ident ) ;
separateGround = false ;
ground - > Init ( ) ;
2004-03-10 16:01:17 +00:00
if ( _display ) {
2003-03-10 13:41:37 +00:00
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
}
} else {
// Initialise ground anyway to do the shortest path stuff!
// Note that we're now responsible for updating and deleting this - NOT the ATCMgr.
ground = new FGGround ( ident ) ;
separateGround = false ;
ground - > Init ( ) ;
2004-03-10 16:01:17 +00:00
if ( _display ) {
2003-03-10 13:41:37 +00:00
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
}
} else {
2003-03-27 15:41:09 +00:00
SG_LOG ( SG_ATC , SG_ALERT , " Unable to find airport details for " < < ident < < " in FGTower::Init() " ) ;
2003-03-11 17:15:23 +00:00
// Initialise ground anyway to avoid segfault later
ground = new FGGround ( ident ) ;
separateGround = false ;
ground - > Init ( ) ;
2004-03-10 16:01:17 +00:00
if ( _display ) {
2003-03-11 17:15:23 +00:00
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
2004-01-23 17:18:24 +00:00
// TODO - attempt to get a departure control pointer to see if we need to hand off departing traffic to departure.
2003-03-27 15:41:09 +00:00
// Get the airport elevation
2004-01-23 17:18:24 +00:00
aptElev = dclGetAirportElev ( ident . c_str ( ) ) ;
2003-03-27 15:41:09 +00:00
2004-01-23 17:18:24 +00:00
// TODO - this function only assumes one active rwy.
2003-03-27 15:41:09 +00:00
DoRwyDetails ( ) ;
2004-01-23 17:18:24 +00:00
// TODO - this currently assumes only one active runway.
rwyOccupied = OnActiveRunway ( Point3D ( user_lon_node - > getDoubleValue ( ) , user_lat_node - > getDoubleValue ( ) , 0.0 ) ) ;
2003-03-27 15:41:09 +00:00
if ( rwyOccupied ) {
2004-01-23 17:18:24 +00:00
//cout << "User found on active runway\n";
2003-03-27 15:41:09 +00:00
// Assume the user is started at the threshold ready to take-off
TowerPlaneRec * t = new TowerPlaneRec ;
2003-11-05 10:06:57 +00:00
t - > plane . callsign = fgGetString ( " /sim/user/callsign " ) ;
t - > plane . type = GA_SINGLE ; // FIXME - hardwired!!
2003-04-15 22:55:22 +00:00
t - > opType = TTT_UNKNOWN ; // We don't know if the user wants to do circuits or a departure...
2003-09-22 23:57:57 +00:00
t - > landingType = AIP_LT_UNKNOWN ;
2003-04-15 22:55:22 +00:00
t - > leg = TAKEOFF_ROLL ;
2003-03-27 15:41:09 +00:00
t - > isUser = true ;
t - > planePtr = NULL ;
t - > clearedToTakeOff = true ;
rwyList . push_back ( t ) ;
2003-09-22 23:57:57 +00:00
departed = false ;
2003-11-05 10:06:57 +00:00
} else {
2004-01-23 17:18:24 +00:00
//cout << "User not on active runway\n";
2003-11-05 10:06:57 +00:00
// 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.
2004-01-23 17:18:24 +00:00
if ( ! OnAnyRunway ( Point3D ( user_lon_node - > getDoubleValue ( ) , user_lat_node - > getDoubleValue ( ) , 0.0 ) ) ) {
2004-03-09 16:20:55 +00:00
//cout << ident << " ADD 0\n";
2004-01-23 17:18:24 +00:00
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 ) ;
}
2003-03-27 15:41:09 +00:00
}
2002-04-03 23:54:44 +00:00
}
2003-03-27 15:41:09 +00:00
void FGTower : : Update ( double dt ) {
2003-10-15 21:50:05 +00:00
//cout << "T" << endl;
2003-04-15 22:55:22 +00:00
// Each time step, what do we need to do?
// We need to go through the list of outstanding requests and acknowedgements
// and process at least one of them.
// We need to go through the list of planes under our control and check if
// any need to be addressed.
// We need to check for planes not under our control coming within our
// control area and address if necessary.
2003-03-10 13:41:37 +00:00
// TODO - a lot of the below probably doesn't need to be called every frame and should be staggered.
// Sort the arriving planes
2003-04-15 22:55:22 +00:00
2003-09-22 23:57:57 +00:00
/*
if ( ident = = " KEMT " ) {
cout < < update_count < < " \t tL: " < < trafficList . size ( ) < < " cL: " < < circuitList . size ( ) < < " hL: " < < holdList . size ( ) < < " aL: " < < appList . size ( ) < < ' \n ' ;
}
*/
2003-11-05 10:06:57 +00:00
//if(ident == "EGNX") cout << display << '\n';
2003-09-22 23:57:57 +00:00
if ( departed ! = false ) {
timeSinceLastDeparture + = dt ;
//if(ident == "KEMT")
// cout << " dt = " << dt << " timeSinceLastDeparture = " << timeSinceLastDeparture << '\n';
}
2003-10-19 20:38:08 +00:00
//cout << ident << " respond = " << respond << " responseReqd = " << responseReqd << '\n';
2003-10-15 14:10:30 +00:00
if ( respond ) {
if ( ! responseReqd ) SG_LOG ( SG_ATC , SG_ALERT , " ERROR - respond is true and responseReqd is false in FGTower::Update(...) " ) ;
Respond ( ) ;
respond = false ;
responseReqd = false ;
}
2003-03-10 13:41:37 +00:00
// Calculate the eta of each plane to the threshold.
// For ground traffic this is the fastest they can get there.
// For air traffic this is the middle approximation.
2003-09-22 23:57:57 +00:00
if ( update_count = = 1 ) {
2003-04-15 22:55:22 +00:00
doThresholdETACalc ( ) ;
}
2003-03-10 13:41:37 +00:00
// Order the list of traffic as per expected threshold use and flag any conflicts
2003-09-22 23:57:57 +00:00
if ( update_count = = 2 ) {
//bool conflicts = doThresholdUseOrder();
doThresholdUseOrder ( ) ;
2003-04-15 22:55:22 +00:00
}
2003-03-10 13:41:37 +00:00
// sortConficts() !!!
2003-09-22 23:57:57 +00:00
if ( update_count = = 4 ) {
2003-10-14 11:10:17 +00:00
CheckHoldList ( dt ) ;
2003-04-15 22:55:22 +00:00
}
2003-09-22 23:57:57 +00:00
// Uggh - HACK - why have we got rwyOccupied - wouldn't simply testing rwyList.size() do?
if ( rwyList . size ( ) ) {
rwyOccupied = true ;
} else {
rwyOccupied = false ;
}
2003-10-14 11:10:17 +00:00
if ( update_count = = 5 & & rwyOccupied ) {
CheckRunwayList ( dt ) ;
2003-03-27 15:41:09 +00:00
}
2003-09-22 23:57:57 +00:00
2003-10-14 11:10:17 +00:00
if ( update_count = = 6 ) {
CheckCircuitList ( dt ) ;
2003-03-27 15:41:09 +00:00
}
2003-09-22 23:57:57 +00:00
if ( update_count = = 7 ) {
2003-10-14 11:10:17 +00:00
CheckApproachList ( dt ) ;
2003-09-19 16:51:27 +00:00
}
2004-01-23 17:18:24 +00:00
if ( update_count = = 8 ) {
CheckDepartureList ( dt ) ;
}
2003-09-22 23:57:57 +00:00
// TODO - do one plane from the departure list and set departed = false when out of consideration
//doCommunication();
2003-03-10 13:41:37 +00:00
if ( ! separateGround ) {
// 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!!
2004-03-10 16:01:17 +00:00
// We could also get rid of this by overloading FGATC's Set(No)Display() functions.
if ( _display ) {
2003-03-10 13:41:37 +00:00
ground - > SetDisplay ( ) ;
} else {
ground - > SetNoDisplay ( ) ;
}
2003-03-27 15:41:09 +00:00
ground - > Update ( dt ) ;
}
2003-04-15 22:55:22 +00:00
2003-09-22 23:57:57 +00:00
+ + update_count ;
2003-04-15 22:55:22 +00:00
// How big should ii get - ie how long should the update cycle interval stretch?
2003-09-22 23:57:57 +00:00
if ( update_count > = update_count_max ) {
update_count = 0 ;
2003-04-15 22:55:22 +00:00
}
2003-10-06 22:40:37 +00:00
// Call the base class update for the response time handling.
FGATC : : Update ( dt ) ;
2003-09-05 10:22:18 +00:00
2004-03-09 16:20:55 +00:00
/*
2003-09-05 10:22:18 +00:00
if ( ident = = " KEMT " ) {
// For AI debugging convienience - may be removed
Point3D user_pos ;
user_pos . setlon ( user_lon_node - > getDoubleValue ( ) ) ;
user_pos . setlat ( user_lat_node - > getDoubleValue ( ) ) ;
user_pos . setelev ( user_elev_node - > getDoubleValue ( ) ) ;
Point3D user_ortho_pos = ortho . ConvertToLocal ( user_pos ) ;
fgSetDouble ( " /AI/user/ortho-x " , user_ortho_pos . x ( ) ) ;
fgSetDouble ( " /AI/user/ortho-y " , user_ortho_pos . y ( ) ) ;
fgSetDouble ( " /AI/user/elev " , user_elev_node - > getDoubleValue ( ) ) ;
}
2004-03-09 16:20:55 +00:00
*/
2003-10-15 21:50:05 +00:00
//cout << "Done T" << endl;
2003-03-27 15:41:09 +00:00
}
2003-11-04 12:00:14 +00:00
void FGTower : : ReceiveUserCallback ( int code ) {
2003-11-05 10:06:57 +00:00
if ( code = = ( int ) USER_REQUEST_VFR_DEPARTURE ) {
2004-01-23 17:18:24 +00:00
//cout << "User requested departure\n";
2003-11-05 10:06:57 +00:00
} else if ( code = = ( int ) USER_REQUEST_VFR_ARRIVAL ) {
VFRArrivalContact ( " USER " ) ;
} else if ( code = = ( int ) USER_REQUEST_VFR_ARRIVAL_FULL_STOP ) {
VFRArrivalContact ( " USER " , FULL_STOP ) ;
} else if ( code = = ( int ) USER_REQUEST_VFR_ARRIVAL_TOUCH_AND_GO ) {
VFRArrivalContact ( " USER " , TOUCH_AND_GO ) ;
2003-11-05 17:24:58 +00:00
} else if ( code = = ( int ) USER_REPORT_DOWNWIND ) {
ReportDownwind ( " USER " ) ;
} else if ( code = = ( int ) USER_REPORT_3_MILE_FINAL ) {
// For now we'll just call report final instead of report long final to avoid having to alter the response code
ReportFinal ( " USER " ) ;
} else if ( code = = ( int ) USER_REPORT_RWY_VACATED ) {
ReportRunwayVacated ( " USER " ) ;
2004-03-09 16:20:55 +00:00
} else if ( code = = ( int ) USER_REPORT_GOING_AROUND ) {
ReportGoingAround ( " USER " ) ;
2003-11-04 12:00:14 +00:00
}
}
2004-03-12 15:59:14 +00:00
// **************** RESPONSE FUNCTIONS ****************
2003-10-15 14:10:30 +00:00
void FGTower : : Respond ( ) {
2004-01-23 17:18:24 +00:00
//cout << "\nEntering Respond, responseID = " << responseID << endl;
2003-10-15 14:10:30 +00:00
TowerPlaneRec * t = FindPlane ( responseID ) ;
if ( t ) {
// This will grow!!!
2003-11-05 10:06:57 +00:00
if ( t - > vfrArrivalReported & & ! t - > vfrArrivalAcknowledged ) {
2004-01-23 17:18:24 +00:00
//cout << "Tower " << ident << " is responding to VFR arrival reported...\n";
2003-11-05 10:06:57 +00:00
// Testing - hardwire straight in for now
string trns = t - > plane . callsign ;
trns + = " " ;
trns + = name ;
2003-11-05 17:24:58 +00:00
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.
Point3D op = ortho . ConvertToLocal ( t - > pos ) ;
if ( op . y ( ) < - 1000 ) {
trns + = " Report three mile straight-in runway " ;
2004-01-23 17:18:24 +00:00
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 ) ;
} else {
t - > planePtr - > RegisterTransmission ( 14 ) ;
}
2003-11-05 17:24:58 +00:00
} else {
// For now we'll just request reporting downwind.
// TODO - In real life something like 'report 2 miles southwest right downwind rwy 19R' might be used
// but I'm not sure how to handle all permutations of which direction to tell to report from yet.
trns + = " Report " ;
2004-01-23 17:18:24 +00:00
//cout << "Responding, rwy.patterDirection is " << rwy.patternDirection << '\n';
trns + = ( ( rwy . patternDirection = = 1 ) ? " right " : " left " ) ;
2003-11-05 17:24:58 +00:00
trns + = " downwind runway " ;
2004-01-23 17:18:24 +00:00
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 ) ;
} else {
t - > planePtr - > RegisterTransmission ( 15 ) ;
}
2003-11-05 17:24:58 +00:00
}
2003-11-05 10:06:57 +00:00
trns + = ConvertRwyNumToSpokenString ( activeRwy ) ;
2004-03-10 16:01:17 +00:00
if ( _display ) {
2004-09-30 15:43:32 +00:00
//globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
pending_transmission = trns ;
Transmit ( ) ;
2003-11-05 10:06:57 +00:00
} else {
2004-01-23 17:18:24 +00:00
//cout << "Not displaying, trns was " << trns << '\n';
2003-11-05 10:06:57 +00:00
}
t - > vfrArrivalAcknowledged = true ;
} else if ( t - > downwindReported ) {
2004-01-23 17:18:24 +00:00
//cout << "Tower " << ident << " is responding to downwind reported...\n";
2004-03-12 15:59:14 +00:00
ProcessDownwindReport ( t ) ;
2003-10-15 14:10:30 +00:00
t - > downwindReported = false ;
2003-10-15 21:50:05 +00:00
} else if ( t - > holdShortReported ) {
2004-01-23 17:18:24 +00:00
//cout << "Tower " << ident << " is reponding to holdShortReported...\n";
2003-10-15 21:50:05 +00:00
if ( t - > nextOnRwy ) {
if ( rwyOccupied ) { // TODO - ought to add a sanity check that it isn't this plane only on the runway (even though it shouldn't be!!)
// Do nothing for now - consider acknowloging hold short eventually
} else {
ClearHoldingPlane ( t ) ;
t - > leg = TAKEOFF_ROLL ;
rwyList . push_back ( t ) ;
rwyOccupied = true ;
// WARNING - WE ARE ASSUMING ONLY ONE PLANE REPORTING HOLD AT A TIME BELOW
// FIXME TODO - FIX THIS!!!
if ( holdList . size ( ) ) {
if ( holdListItr = = holdList . end ( ) ) {
holdListItr = holdList . begin ( ) ;
}
holdList . erase ( holdListItr ) ;
holdListItr = holdList . begin ( ) ;
}
}
} else {
// Tell him to hold and what position he is.
// Not currently sure under which circumstances we do or don't bother transmitting this.
string trns = t - > plane . callsign ;
trns + = " hold position " ;
2004-03-10 16:01:17 +00:00
if ( _display ) {
2004-09-30 15:43:32 +00:00
//globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
pending_transmission = trns ;
Transmit ( ) ;
2003-10-15 21:50:05 +00:00
}
// TODO - add some idea of what traffic is blocking him.
}
t - > holdShortReported = false ;
2003-10-16 16:06:22 +00:00
} else if ( t - > finalReported & & ! ( t - > finalAcknowledged ) ) {
2004-01-23 17:18:24 +00:00
//cout << "Tower " << ident << " is responding to finalReported...\n";
2003-10-19 20:38:08 +00:00
bool disp = true ;
2003-10-16 16:06:22 +00:00
string trns = t - > plane . callsign ;
2004-01-23 17:18:24 +00:00
//cout << (t->nextOnRwy ? "Next on rwy " : "Not next!! ");
//cout << (rwyOccupied ? "RWY OCCUPIED!!\n" : "Rwy not occupied\n");
if ( t - > nextOnRwy & & ! rwyOccupied & & ! ( t - > instructedToGoAround ) ) {
2003-10-16 16:06:22 +00:00
if ( t - > landingType = = FULL_STOP ) {
trns + = " cleared to land " ;
} else {
trns + = " cleared for the option " ;
}
// TODO - add winds
t - > clearedToLand = true ;
2004-01-23 17:18:24 +00:00
// Maybe remove report downwind from menu here as well incase user didn't bother to?
if ( t - > isUser ) {
2004-03-09 16:20:55 +00:00
//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 ) ;
2004-01-23 17:18:24 +00:00
current_atcdialog - > add_entry ( ident , " @CS Clear of the runway " , " Report runway vacated " , TOWER , USER_REPORT_RWY_VACATED ) ;
} else {
t - > planePtr - > RegisterTransmission ( 7 ) ;
}
2003-10-19 20:38:08 +00:00
} else if ( t - > eta < 20 ) {
// Do nothing - we'll be telling it to go around in less than 10 seconds if the
// runway doesn't clear so no point in calling "continue approach".
disp = false ;
2003-10-16 16:06:22 +00:00
} else {
trns + = " continue approach " ;
t - > clearedToLand = false ;
}
2004-03-10 16:01:17 +00:00
if ( _display & & disp ) {
2004-09-30 15:43:32 +00:00
//globals->get_ATC_display()->RegisterSingleMessage(trns);
pending_transmission = trns ;
Transmit ( ) ;
2003-10-16 16:06:22 +00:00
}
t - > finalAcknowledged = true ;
2003-11-05 17:24:58 +00:00
} else if ( t - > rwyVacatedReported & & ! ( t - > rwyVacatedAcknowledged ) ) {
2004-01-23 17:18:24 +00:00
ProcessRunwayVacatedReport ( t ) ;
2003-11-05 17:24:58 +00:00
t - > rwyVacatedAcknowledged = true ;
2003-10-15 21:50:05 +00:00
}
}
2004-01-23 17:18:24 +00:00
//freqClear = true; // FIXME - set this to come true after enough time to render the message
_releaseCounter = 0.0 ;
_releaseTime = 5.5 ;
_runReleaseCounter = true ;
//cout << "Done Respond\n" << endl;
}
2004-03-12 15:59:14 +00:00
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
2004-03-22 20:57:27 +00:00
TowerPlaneRec * tc = NULL ;
2004-03-12 15:59:14 +00:00
for ( tower_plane_rec_list_iterator twrItr = circuitList . begin ( ) ; twrItr ! = circuitList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = responseID ) break ;
2004-03-16 10:35:29 +00:00
tc = * twrItr ;
2004-03-12 15:59:14 +00:00
+ + i ;
}
if ( i > 1 ) { cc = true ; }
doThresholdETACalc ( ) ;
2004-03-22 20:57:27 +00:00
TowerPlaneRec * tf = NULL ;
2004-03-12 15:59:14 +00:00
for ( tower_plane_rec_list_iterator twrItr = appList . begin ( ) ; twrItr ! = appList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > eta < ( t - > eta + 45 ) ) {
a + + ;
2004-03-16 10:35:29 +00:00
tf = * twrItr ;
2004-03-12 15:59:14 +00:00
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 + = " " ;
2004-03-22 20:57:27 +00:00
TowerPlaneRec * tt = NULL ;
2004-03-12 15:59:14 +00:00
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 ) {
2004-03-16 10:35:29 +00:00
//First set tt to point to the correct preceding plane - final or circuit
2004-03-22 20:57:27 +00:00
if ( tc & & tf ) {
2004-03-16 10:35:29 +00:00
tt = ( tf - > eta < tc - > eta ? tf : tc ) ;
2004-03-22 20:57:27 +00:00
} else if ( tc ) {
2004-03-16 10:35:29 +00:00
tt = tc ;
2004-03-22 20:57:27 +00:00
} else if ( tf ) {
2004-03-16 10:35:29 +00:00
tt = tf ;
} else {
// We should never get here!
SG_LOG ( SG_ATC , SG_ALERT , " ALERT - Logic error in FGTower::ProcessDownwindReport " ) ;
2004-03-22 20:57:27 +00:00
return ;
2004-03-16 10:35:29 +00:00
}
2004-03-12 15:59:14 +00:00
trns + = " Follow the " ;
string s = tt - > plane . callsign ;
int p = s . find ( ' - ' ) ;
s = s . substr ( 0 , p ) ;
trns + = s ;
if ( ( tt - > opType ) = = CIRCUIT ) {
2004-03-22 22:09:47 +00:00
PatternLeg leg ;
if ( t - > isUser ) {
leg = tt - > leg ;
} else {
leg = tt - > planePtr - > GetLeg ( ) ;
}
if ( leg = = FINAL ) {
2004-03-12 15:59:14 +00:00
trns + = " on final " ;
2004-03-22 22:09:47 +00:00
} else if ( leg = = TURN4 ) {
2004-03-12 15:59:14 +00:00
trns + = " turning final " ;
2004-03-22 22:09:47 +00:00
} else if ( leg = = BASE ) {
2004-03-12 15:59:14 +00:00
trns + = " on base " ;
2004-03-22 22:09:47 +00:00
} else if ( leg = = TURN3 ) {
2004-03-12 15:59:14 +00:00
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 ) {
2004-09-30 15:43:32 +00:00
//globals->get_ATC_display()->RegisterSingleMessage(trns);
pending_transmission = trns ;
Transmit ( ) ;
2004-03-12 15:59:14 +00:00
}
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 ) ;
}
}
2004-01-23 17:18:24 +00:00
void FGTower : : ProcessRunwayVacatedReport ( TowerPlaneRec * t ) {
//cout << "Processing rwy vacated...\n";
2004-03-20 02:55:25 +00:00
if ( t - > isUser ) current_atcdialog - > remove_entry ( ident , USER_REPORT_GOING_AROUND , TOWER ) ;
2004-01-23 17:18:24 +00:00
string trns = t - > plane . callsign ;
if ( separateGround ) {
trns + = " Contact ground on " ;
double f = globals - > get_ATC_mgr ( ) - > GetFrequency ( ident , GROUND ) / 100.0 ;
char buf [ 10 ] ;
sprintf ( buf , " %.2f " , f ) ;
trns + = buf ;
trns + = " Good Day " ;
if ( ! t - > isUser ) t - > planePtr - > RegisterTransmission ( 5 ) ;
} else {
// Cop-out!!
trns + = " cleared for taxi to the GA parking " ;
if ( ! t - > isUser ) t - > planePtr - > RegisterTransmission ( 6 ) ; // TODO - this is a mega-hack!!
}
//cout << "trns = " << trns << '\n';
2004-03-10 16:01:17 +00:00
if ( _display ) {
2004-09-30 15:43:32 +00:00
//globals->get_ATC_display()->RegisterSingleMessage(trns);
pending_transmission = trns ;
Transmit ( ) ;
2004-01-23 17:18:24 +00:00
}
2004-03-20 02:55:25 +00:00
RemoveFromRwyList ( t - > plane . callsign ) ;
AddToVacatedList ( t ) ;
2004-01-23 17:18:24 +00:00
// Maybe we should check that the plane really *has* vacated the runway!
2003-10-15 21:50:05 +00:00
}
2004-03-12 15:59:14 +00:00
// *********** END RESPONSE FUNCTIONS *****************
2003-10-15 21:50:05 +00:00
// 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 ) {
//cout << "Entering ClearHoldingPlane..." << endl;
// Lets Roll !!!!
string trns = t - > plane . callsign ;
//if(departed plane < some threshold in time away) {
if ( 0 ) { // FIXME
//if(timeSinceLastDeparture <= 60.0 && departed == true) {
trns + = " line up " ;
t - > clearedToLineUp = true ;
t - > planePtr - > RegisterTransmission ( 3 ) ; // cleared to line-up
//} else if(arriving plane < some threshold away) {
} else if ( GetTrafficETA ( 2 ) < 150.0 & & ( timeSinceLastDeparture > 60.0 | | departed = = false ) ) { // Hack - hardwired time
trns + = " cleared immediate take-off " ;
if ( trafficList . size ( ) ) {
tower_plane_rec_list_iterator trfcItr = trafficList . begin ( ) ;
trfcItr + + ; // At the moment the holding plane should be first in trafficList.
// Note though that this will break if holding planes aren't put in trafficList in the future.
TowerPlaneRec * trfc = * trfcItr ;
trns + = " ... traffic is " ;
switch ( trfc - > plane . type ) {
case UNKNOWN :
break ;
case GA_SINGLE :
trns + = " a Cessna " ; // TODO - add ability to specify actual plane type somewhere
break ;
case GA_HP_SINGLE :
trns + = " a Piper " ;
break ;
case GA_TWIN :
trns + = " a King-air " ;
break ;
case GA_JET :
trns + = " a Learjet " ;
break ;
case MEDIUM :
trns + = " a Regional " ;
break ;
case HEAVY :
trns + = " a Heavy " ;
break ;
case MIL_JET :
trns + = " Military " ;
break ;
}
//if(trfc->opType == STRAIGHT_IN || trfc->opType == TTT_UNKNOWN) {
if ( trfc - > opType = = STRAIGHT_IN ) {
double miles_out = CalcDistOutMiles ( trfc ) ;
if ( miles_out < 2 ) {
trns + = " on final " ;
} else {
trns + = " on " ;
trns + = ConvertNumToSpokenDigits ( ( int ) miles_out ) ;
trns + = " mile final " ;
}
} else if ( trfc - > opType = = CIRCUIT ) {
//cout << "Getting leg of " << trfc->plane.callsign << '\n';
switch ( trfc - > leg ) {
case FINAL :
trns + = " on final " ;
break ;
case TURN4 :
trns + = " turning final " ;
break ;
case BASE :
trns + = " on base " ;
break ;
case TURN3 :
trns + = " turning base " ;
break ;
case DOWNWIND :
trns + = " in circuit " ; // At the moment the user plane is generally flagged as unknown opType when downwind incase its a downwind departure which means we won't get here.
break ;
// And to eliminate compiler warnings...
case TAKEOFF_ROLL : break ;
case CLIMBOUT : break ;
case TURN1 : break ;
case CROSSWIND : break ;
case TURN2 : break ;
case LANDING_ROLL : break ;
case LEG_UNKNOWN : break ;
}
}
} else {
// By definition there should be some arriving traffic if we're cleared for immediate takeoff
SG_LOG ( SG_ATC , SG_WARN , " Warning: Departing traffic cleared for *immediate* take-off despite no arriving traffic in FGTower " ) ;
2003-10-15 14:10:30 +00:00
}
2003-10-15 21:50:05 +00:00
t - > clearedToTakeOff = true ;
t - > planePtr - > RegisterTransmission ( 4 ) ; // cleared to take-off - TODO differentiate between immediate and normal take-off
departed = false ;
timeSinceLastDeparture = 0.0 ;
} else {
//} else if(timeSinceLastDeparture > 60.0 || departed == false) { // Hack - test for timeSinceLastDeparture should be in lineup block eventually
trns + = " cleared for take-off " ;
// TODO - add traffic is... ?
t - > clearedToTakeOff = true ;
t - > planePtr - > RegisterTransmission ( 4 ) ; // cleared to take-off
departed = false ;
timeSinceLastDeparture = 0.0 ;
}
2004-03-10 16:01:17 +00:00
if ( _display ) {
2004-09-30 15:43:32 +00:00
//globals->get_ATC_display()->RegisterSingleMessage(trns, 0);
pending_transmission = trns ;
Transmit ( ) ;
2003-10-15 14:10:30 +00:00
}
2003-10-15 21:50:05 +00:00
//cout << "Done ClearHoldingPlane " << endl;
2003-10-15 14:10:30 +00:00
}
2004-01-23 17:18:24 +00:00
// ***************************************************************************************
// ********** Functions to periodically check what the various traffic is doing **********
2003-10-14 11:10:17 +00:00
// Do one plane from the hold list
void FGTower : : CheckHoldList ( double dt ) {
2003-10-15 21:50:05 +00:00
//cout << "Entering CheckHoldList..." << endl;
2003-10-14 11:10:17 +00:00
if ( holdList . size ( ) ) {
//cout << "*holdListItr = " << *holdListItr << endl;
if ( holdListItr = = holdList . end ( ) ) {
holdListItr = holdList . begin ( ) ;
}
//cout << "*holdListItr = " << *holdListItr << endl;
//Process(*holdListItr);
TowerPlaneRec * t = * holdListItr ;
//cout << "t = " << t << endl;
if ( t - > holdShortReported ) {
2003-10-15 21:50:05 +00:00
// NO-OP - leave it to the response handler.
2003-10-14 11:10:17 +00:00
} else { // not responding to report, but still need to clear if clear
if ( t - > nextOnRwy ) {
//cout << "departed = " << departed << '\n';
//cout << "timeSinceLastDeparture = " << timeSinceLastDeparture << '\n';
if ( rwyOccupied ) {
2003-10-15 21:50:05 +00:00
// Do nothing
2003-10-14 11:10:17 +00:00
} else if ( timeSinceLastDeparture < = 60.0 & & departed = = true ) {
// Do nothing - this is a bit of a hack - should maybe do line up be ready here
} else {
2003-10-15 21:50:05 +00:00
ClearHoldingPlane ( t ) ;
t - > leg = TAKEOFF_ROLL ;
rwyList . push_back ( t ) ;
rwyOccupied = true ;
holdList . erase ( holdListItr ) ;
holdListItr = holdList . begin ( ) ;
2003-10-14 11:10:17 +00:00
}
}
// TODO - rationalise the considerable code duplication above!
}
+ + holdListItr ;
}
2003-10-15 21:50:05 +00:00
//cout << "Done CheckHoldList" << endl;
2003-10-14 11:10:17 +00:00
}
// do the ciruit list
void FGTower : : CheckCircuitList ( double dt ) {
2003-10-15 21:50:05 +00:00
//cout << "Entering CheckCircuitList..." << endl;
2003-10-14 11:10:17 +00:00
// Clear the constraints - we recalculate here.
base_leg_pos = 0.0 ;
downwind_leg_pos = 0.0 ;
crosswind_leg_pos = 0.0 ;
if ( circuitList . size ( ) ) { // Do one plane from the circuit
if ( circuitListItr = = circuitList . end ( ) ) {
circuitListItr = circuitList . begin ( ) ;
}
TowerPlaneRec * t = * circuitListItr ;
2004-01-23 17:18:24 +00:00
//cout << ident << ' ' << circuitList.size() << ' ' << t->plane.callsign << " " << t->leg << " eta " << t->eta << '\n';
2003-10-14 11:10:17 +00:00
if ( t - > isUser ) {
t - > pos . setlon ( user_lon_node - > getDoubleValue ( ) ) ;
t - > pos . setlat ( user_lat_node - > getDoubleValue ( ) ) ;
t - > pos . setelev ( user_elev_node - > getDoubleValue ( ) ) ;
2004-01-23 17:18:24 +00:00
//cout << ident << ' ' << circuitList.size() << ' ' << t->plane.callsign << " " << t->leg << " eta " << t->eta << '\n';
2003-10-14 11:10:17 +00:00
} else {
t - > pos = t - > planePtr - > GetPos ( ) ; // We should probably only set the pos's on one walk through the traffic list in the update function, to save a few CPU should we end up duplicating this.
t - > landingType = t - > planePtr - > GetLandingOption ( ) ;
//cout << "AI plane landing option is " << t->landingType << '\n';
}
Point3D tortho = ortho . ConvertToLocal ( t - > pos ) ;
if ( t - > isUser ) {
// Need to figure out which leg he's on
//cout << "rwy.hdg = " << rwy.hdg << " user hdg = " << user_hdg_node->getDoubleValue();
double ho = GetAngleDiff_deg ( user_hdg_node - > getDoubleValue ( ) , rwy . hdg ) ;
2003-10-15 14:10:30 +00:00
//cout << " ho = " << ho << " abs(ho = " << abs(ho) << '\n';
2003-10-14 11:10:17 +00:00
// 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!
2004-03-29 10:25:21 +00:00
if ( fabs ( ho ) < 30 ) {
2003-10-14 11:10:17 +00:00
// 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 - > leg = FINAL ;
//cout << "Final\n";
} else {
t - > leg = CLIMBOUT ; // TODO - check elev wrt. apt elev to differentiate takeoff roll and climbout
//cout << "Climbout\n";
// If it's the user we may be unsure of his/her intentions.
// (Hopefully the AI planes won't try confusing the sim!!!)
2004-01-23 17:18:24 +00:00
//cout << "tortho.y = " << tortho.y() << '\n';
2003-10-14 11:10:17 +00:00
if ( t - > opType = = TTT_UNKNOWN ) {
if ( tortho . y ( ) > 5000 ) {
// 5 km out from threshold - assume it's a departure
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.
2004-01-23 17:18:24 +00:00
//cout << ident << " Removing user from circuitList (TTT_UNKNOWN)\n";
2003-10-14 11:10:17 +00:00
circuitList . erase ( circuitListItr ) ;
RemoveFromTrafficList ( t - > plane . callsign ) ;
circuitListItr = circuitList . begin ( ) ;
}
} 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 ) ;
2004-01-23 17:18:24 +00:00
//cout << ident << " removing user from circuitList (CIRCUIT)\n";
2003-10-14 11:10:17 +00:00
circuitList . erase ( circuitListItr ) ;
circuitListItr = circuitList . begin ( ) ;
}
}
}
2004-03-29 10:25:21 +00:00
} else if ( fabs ( ho ) < 60 ) {
2003-10-14 11:10:17 +00:00
// 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 = TURN1 ;
//cout << "Turn1\n";
} else {
t - > leg = TURN4 ;
//cout << "Turn4\n";
}
2004-03-29 10:25:21 +00:00
} else if ( fabs ( ho ) < 120 ) {
2003-10-14 11:10:17 +00:00
// 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 = CROSSWIND ;
//cout << "Crosswind\n";
} else {
t - > leg = BASE ;
//cout << "Base\n";
}
2004-03-29 10:25:21 +00:00
} else if ( fabs ( ho ) < 150 ) {
2003-10-14 11:10:17 +00:00
// 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 ) ) {
t - > leg = TURN2 ;
//cout << "Turn2\n";
} else {
t - > leg = TURN3 ;
// Probably safe now to assume the user is flying a circuit
t - > opType = CIRCUIT ;
//cout << "Turn3\n";
}
} else {
// downwind
t - > leg = DOWNWIND ;
//cout << "Downwind\n";
}
if ( t - > leg = = FINAL ) {
if ( OnActiveRunway ( t - > pos ) ) {
t - > leg = LANDING_ROLL ;
}
}
} else {
t - > leg = t - > planePtr - > GetLeg ( ) ;
}
// Set the constraints IF this is the first plane in the circuit
// TODO - at the moment we're constraining plane 2 based on plane 1 - this won't (or might not) work for 3 planes in the circuit!!
if ( circuitListItr = = circuitList . begin ( ) ) {
switch ( t - > leg ) {
2004-01-23 17:18:24 +00:00
case FINAL :
2003-10-14 11:10:17 +00:00
// Base leg must be at least as far out as the plane is - actually possibly not necessary for separation, but we'll use that for now.
base_leg_pos = tortho . y ( ) ;
//cout << "base_leg_pos = " << base_leg_pos << '\n';
break ;
2004-01-23 17:18:24 +00:00
case TURN4 :
2003-10-14 11:10:17 +00:00
// Fall through to base
2004-01-23 17:18:24 +00:00
case BASE :
2003-10-14 11:10:17 +00:00
base_leg_pos = tortho . y ( ) ;
//cout << "base_leg_pos = " << base_leg_pos << '\n';
break ;
2004-01-23 17:18:24 +00:00
case TURN3 :
2003-10-14 11:10:17 +00:00
// Fall through to downwind
2004-01-23 17:18:24 +00:00
case DOWNWIND :
2003-10-14 11:10:17 +00:00
// Only have the downwind leg pos as turn-to-base constraint if more negative than we already have.
base_leg_pos = ( tortho . y ( ) < base_leg_pos ? tortho . y ( ) : base_leg_pos ) ;
//cout << "base_leg_pos = " << base_leg_pos;
downwind_leg_pos = tortho . x ( ) ; // Assume that a following plane can simply be constrained by the immediately in front downwind plane
//cout << " downwind_leg_pos = " << downwind_leg_pos << '\n';
break ;
2004-01-23 17:18:24 +00:00
case TURN2 :
2003-10-14 11:10:17 +00:00
// Fall through to crosswind
2004-01-23 17:18:24 +00:00
case CROSSWIND :
2003-10-14 11:10:17 +00:00
crosswind_leg_pos = tortho . y ( ) ;
//cout << "crosswind_leg_pos = " << crosswind_leg_pos << '\n';
2003-10-19 20:38:08 +00:00
t - > instructedToGoAround = false ;
2003-10-14 11:10:17 +00:00
break ;
2004-01-23 17:18:24 +00:00
case TURN1 :
2003-10-14 11:10:17 +00:00
// Fall through to climbout
2004-01-23 17:18:24 +00:00
case CLIMBOUT :
2003-10-14 11:10:17 +00:00
// Only use current by constraint as largest
crosswind_leg_pos = ( tortho . y ( ) > crosswind_leg_pos ? tortho . y ( ) : crosswind_leg_pos ) ;
//cout << "crosswind_leg_pos = " << crosswind_leg_pos << '\n';
break ;
2004-01-23 17:18:24 +00:00
case TAKEOFF_ROLL :
2003-10-14 11:10:17 +00:00
break ;
2004-01-23 17:18:24 +00:00
case LEG_UNKNOWN :
2003-10-14 11:10:17 +00:00
break ;
2004-01-23 17:18:24 +00:00
case LANDING_ROLL :
2003-10-14 11:10:17 +00:00
break ;
2004-01-23 17:18:24 +00:00
default :
2003-10-14 11:10:17 +00:00
break ;
}
}
2004-01-23 17:18:24 +00:00
if ( t - > leg = = FINAL & & ! ( t - > instructedToGoAround ) ) {
2004-03-12 15:59:14 +00:00
doThresholdETACalc ( ) ;
doThresholdUseOrder ( ) ;
/*
if ( t - > isUser ) {
cout < < " Checking USER on final... " ;
cout < < " eta " < < t - > eta ;
if ( t - > clearedToLand ) cout < < " cleared to land \n " ;
}
*/
2004-01-23 17:18:24 +00:00
//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";
2004-03-12 15:59:14 +00:00
}
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 ) {
2004-03-20 04:12:05 +00:00
// 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 ) {
2004-03-12 15:59:14 +00:00
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 ) ;
}
2004-01-23 17:18:24 +00:00
}
2004-03-12 15:59:14 +00:00
} else {
//if(t->isUser) cout << "Not next\n";
2003-10-19 20:38:08 +00:00
}
}
2003-10-14 11:10:17 +00:00
} else if ( t - > leg = = LANDING_ROLL ) {
2004-01-23 17:18:24 +00:00
//cout << t->plane.callsign << " has landed - adding to rwyList\n";
2003-10-14 11:10:17 +00:00
rwyList . push_front ( t ) ;
2003-10-16 16:06:22 +00:00
// TODO - if(!clearedToLand) shout something!!
t - > clearedToLand = false ;
2003-10-14 11:10:17 +00:00
RemoveFromTrafficList ( t - > plane . callsign ) ;
if ( t - > isUser ) {
t - > opType = TTT_UNKNOWN ;
} // TODO - allow the user to specify opType via ATC menu
2004-01-23 17:18:24 +00:00
//cout << ident << " Removing " << t->plane.callsign << " from circuitList..." << endl;
2003-10-14 11:10:17 +00:00
circuitListItr = circuitList . erase ( circuitListItr ) ;
if ( circuitListItr = = circuitList . end ( ) ) {
circuitListItr = circuitList . begin ( ) ;
2005-12-19 13:03:19 +00:00
// avoid increment of circuitListItr (would increment to second element, or crash if no element left)
return ;
2003-10-14 11:10:17 +00:00
}
}
+ + circuitListItr ;
}
2003-10-15 21:50:05 +00:00
//cout << "Done CheckCircuitList" << endl;
2003-10-14 11:10:17 +00:00
}
// Do the runway list - we'll do the whole runway list since it's important and there'll never be many planes on the rwy at once!!
// FIXME - at the moment it looks like we're only doing the first plane from the rwy list.
// (However, at the moment there should only be one airplane on the rwy at once, until we
// start allowing planes to line up whilst previous arrival clears the rwy.)
void FGTower : : CheckRunwayList ( double dt ) {
2003-10-15 21:50:05 +00:00
//cout << "Entering CheckRunwayList..." << endl;
2003-10-14 11:10:17 +00:00
if ( rwyOccupied ) {
if ( ! rwyList . size ( ) ) {
rwyOccupied = false ;
} else {
rwyListItr = rwyList . begin ( ) ;
TowerPlaneRec * t = * rwyListItr ;
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 {
t - > pos = t - > planePtr - > GetPos ( ) ; // We should probably only set the pos's on one walk through the traffic list in the update function, to save a few CPU should we end up duplicating this.
}
bool on_rwy = OnActiveRunway ( t - > pos ) ;
if ( ! on_rwy ) {
2004-01-23 17:18:24 +00:00
// TODO - for all of these we need to check what the user is *actually* doing!
2003-10-14 11:10:17 +00:00
if ( ( t - > opType = = INBOUND ) | | ( t - > opType = = STRAIGHT_IN ) ) {
2004-01-23 17:18:24 +00:00
//cout << "Tower " << ident << " is removing plane " << t->plane.callsign << " from rwy list (vacated)\n";
//cout << "Size of rwylist was " << rwyList.size() << '\n';
//cout << "Size of vacatedList was " << vacatedList.size() << '\n';
RemoveFromRwyList ( t - > plane . callsign ) ;
2004-03-20 02:55:25 +00:00
AddToVacatedList ( t ) ;
2004-01-23 17:18:24 +00:00
//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.
2003-10-14 11:10:17 +00:00
} else if ( t - > opType = = OUTBOUND ) {
depList . push_back ( t ) ;
rwyList . pop_front ( ) ;
departed = true ;
timeSinceLastDeparture = 0.0 ;
} else if ( t - > opType = = CIRCUIT ) {
2004-01-23 17:18:24 +00:00
//cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
2003-10-14 11:10:17 +00:00
circuitList . push_back ( t ) ;
AddToTrafficList ( t ) ;
rwyList . pop_front ( ) ;
departed = true ;
timeSinceLastDeparture = 0.0 ;
} else if ( t - > opType = = TTT_UNKNOWN ) {
depList . push_back ( t ) ;
2004-01-23 17:18:24 +00:00
//cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
2003-10-14 11:10:17 +00:00
circuitList . push_back ( t ) ;
AddToTrafficList ( t ) ;
rwyList . pop_front ( ) ;
departed = true ;
timeSinceLastDeparture = 0.0 ; // TODO - we need to take into account that the user might taxi-in when flagged opType UNKNOWN - check speed/altitude etc to make decision as to what user is up to.
} else {
// HELP - we shouldn't ever get here!!!
}
}
}
}
2003-10-15 21:50:05 +00:00
//cout << "Done CheckRunwayList" << endl;
2003-10-14 11:10:17 +00:00
}
// Do one plane from the approach list
void FGTower : : CheckApproachList ( double dt ) {
2004-01-23 17:18:24 +00:00
//cout << "CheckApproachList called for " << ident << endl;
//cout << "AppList.size is " << appList.size() << endl;
2003-10-14 11:10:17 +00:00
if ( appList . size ( ) ) {
if ( appListItr = = appList . end ( ) ) {
appListItr = appList . begin ( ) ;
}
TowerPlaneRec * t = * appListItr ;
//cout << "t = " << t << endl;
2004-01-23 17:18:24 +00:00
//cout << "Checking " << t->plane.callsign << endl;
2003-10-14 11:10:17 +00:00
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 {
// TODO - set/update the position if it's an AI plane
2004-01-23 17:18:24 +00:00
}
2004-03-20 04:12:05 +00:00
doThresholdETACalc ( ) ; // We need this here because planes in the lists are not guaranteed to *always* have the correct ETA
2004-01-23 17:18:24 +00:00
//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
// 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 " ;
2004-03-10 16:01:17 +00:00
pending_transmission = trns ;
ImmediateTransmit ( ) ;
2004-01-23 17:18:24 +00:00
t - > instructedToGoAround = true ;
t - > clearedToLand = false ;
t - > nextOnRwy = false ; // But note this is recalculated so don't rely on it
// Assume it complies!!!
t - > opType = CIRCUIT ;
t - > leg = CLIMBOUT ;
if ( ! t - > isUser ) {
if ( t - > planePtr ) {
//cout << "Registering Go-around transmission with AI plane\n";
t - > planePtr - > RegisterTransmission ( 13 ) ;
}
} else {
// TODO - add Go-around ack to comm options,
// remove report rwy vacated. (possibly).
}
2004-03-20 04:12:05 +00:00
} 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";
}
2004-01-23 17:18:24 +00:00
}
// Check for landing...
bool landed = false ;
if ( ! t - > isUser ) {
if ( t - > planePtr ) {
if ( t - > planePtr - > GetLeg ( ) = = LANDING_ROLL ) {
landed = true ;
}
} else {
SG_LOG ( SG_ATC , SG_ALERT , " WARNING - not user and null planePtr in CheckApproachList! " ) ;
}
} else { // user
if ( OnActiveRunway ( t - > pos ) ) {
landed = true ;
}
}
if ( landed ) {
// Duplicated in CheckCircuitList - must be able to rationalise this somehow!
//cout << "A " << t->plane.callsign << " has landed, adding to rwyList...\n";
rwyList . push_front ( t ) ;
// TODO - if(!clearedToLand) shout something!!
t - > clearedToLand = false ;
RemoveFromTrafficList ( t - > plane . callsign ) ;
//if(t->isUser) {
// t->opType = TTT_UNKNOWN;
//} // TODO - allow the user to specify opType via ATC menu
appListItr = appList . erase ( appListItr ) ;
if ( appListItr = = appList . end ( ) ) {
appListItr = appList . begin ( ) ;
}
}
2003-10-14 11:10:17 +00:00
+ + appListItr ;
}
2004-01-23 17:18:24 +00:00
//cout << "Done" << endl;
}
// Do one plane from the departure list
void FGTower : : CheckDepartureList ( double dt ) {
if ( depList . size ( ) ) {
if ( depListItr = = depList . end ( ) ) {
depListItr = depList . begin ( ) ;
}
TowerPlaneRec * t = * depListItr ;
//cout << "Dep list, checking " << t->plane.callsign;
double distout ; // meters
if ( t - > isUser ) distout = dclGetHorizontalSeparation ( Point3D ( lon , lat , elev ) , Point3D ( user_lon_node - > getDoubleValue ( ) , user_lat_node - > getDoubleValue ( ) , user_elev_node - > getDoubleValue ( ) ) ) ;
else distout = dclGetHorizontalSeparation ( Point3D ( lon , lat , elev ) , t - > planePtr - > GetPos ( ) ) ;
//cout << " distout = " << distout << '\n';
if ( distout > 10000 ) {
string trns = t - > plane . callsign ;
trns + = " You are now clear of my airspace, good day " ;
2004-03-10 16:01:17 +00:00
pending_transmission = trns ;
Transmit ( ) ;
2004-01-23 17:18:24 +00:00
if ( t - > isUser ) {
// Change the communication options
RemoveAllUserDialogOptions ( ) ;
2004-03-09 16:20:55 +00:00
//cout << "ADD A\n";
2004-01-23 17:18:24 +00:00
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
// TODO - implement this once we actually have departing AI traffic (currently all circuits or arrivals).
}
RemovePlane ( t - > plane . callsign ) ;
} else {
+ + depListItr ;
}
}
}
// ********** End periodic check functions ***********************************************
// ***************************************************************************************
// Remove all dialog options for this tower.
void FGTower : : RemoveAllUserDialogOptions ( ) {
current_atcdialog - > remove_entry ( ident , USER_REQUEST_VFR_DEPARTURE , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REQUEST_VFR_ARRIVAL , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REQUEST_VFR_ARRIVAL_FULL_STOP , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REQUEST_VFR_ARRIVAL_TOUCH_AND_GO , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REPORT_3_MILE_FINAL , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REPORT_DOWNWIND , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REPORT_RWY_VACATED , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REPORT_GOING_AROUND , TOWER ) ;
2003-10-14 11:10:17 +00:00
}
2003-03-27 15:41:09 +00:00
2003-06-11 21:49:46 +00:00
// Returns true if positions of crosswind/downwind/base leg turns should be constrained by previous traffic
// plus the constraint position as a rwy orientated orthopos (meters)
bool FGTower : : GetCrosswindConstraint ( double & cpos ) {
if ( crosswind_leg_pos ! = 0.0 ) {
cpos = crosswind_leg_pos ;
return ( true ) ;
} else {
cpos = 0.0 ;
return ( false ) ;
}
}
bool FGTower : : GetDownwindConstraint ( double & dpos ) {
2003-11-04 12:00:14 +00:00
if ( fabs ( downwind_leg_pos ) > nominal_downwind_leg_pos ) {
2003-06-11 21:49:46 +00:00
dpos = downwind_leg_pos ;
return ( true ) ;
} else {
dpos = 0.0 ;
return ( false ) ;
}
}
bool FGTower : : GetBaseConstraint ( double & bpos ) {
2003-11-04 12:00:14 +00:00
if ( base_leg_pos < nominal_base_leg_pos ) {
2003-06-11 21:49:46 +00:00
bpos = base_leg_pos ;
return ( true ) ;
} else {
2003-11-04 12:00:14 +00:00
bpos = nominal_base_leg_pos ;
2003-06-11 21:49:46 +00:00
return ( false ) ;
}
}
2003-03-27 15:41:09 +00:00
// Figure out which runways are active.
// For now we'll just be simple and do one active runway - eventually this will get much more complex
// This is a private function - public interface to the results of this is through GetActiveRunway
void FGTower : : DoRwyDetails ( ) {
//cout << "GetRwyDetails called" << endl;
// Based on the airport-id and wind get the active runway
//wind
double hdg = wind_from_hdg - > getDoubleValue ( ) ;
double speed = wind_speed_knots - > getDoubleValue ( ) ;
hdg = ( speed = = 0.0 ? 270.0 : hdg ) ;
//cout << "Heading = " << hdg << '\n';
FGRunway runway ;
2003-08-29 04:11:23 +00:00
bool rwyGood = globals - > get_runways ( ) - > search ( ident , int ( hdg ) , & runway ) ;
2003-03-27 15:41:09 +00:00
if ( rwyGood ) {
2004-01-23 17:18:24 +00:00
//cout << "RUNWAY GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOD\n";
2004-12-22 23:57:07 +00:00
activeRwy = runway . _rwy_no ;
rwy . rwyID = runway . _rwy_no ;
2003-03-27 15:41:09 +00:00
SG_LOG ( SG_ATC , SG_INFO , " Active runway for airport " < < ident < < " is " < < activeRwy ) ;
// Get the threshold position
2004-12-22 23:57:07 +00:00
double other_way = runway . _heading - 180.0 ;
2003-03-27 15:41:09 +00:00
while ( other_way < = 0.0 ) {
other_way + = 360.0 ;
}
// move to the +l end/center of the runway
2004-12-22 23:57:07 +00:00
//cout << "Runway center is at " << runway._lon << ", " << runway._lat << '\n';
Point3D origin = Point3D ( runway . _lon , runway . _lat , aptElev ) ;
2003-03-27 15:41:09 +00:00
Point3D ref = origin ;
double tshlon , tshlat , tshr ;
double tolon , tolat , tor ;
2004-12-22 23:57:07 +00:00
rwy . length = runway . _length * SG_FEET_TO_METER ;
rwy . width = runway . _width * SG_FEET_TO_METER ;
2003-03-27 15:41:09 +00:00
geo_direct_wgs_84 ( aptElev , ref . lat ( ) , ref . lon ( ) , other_way ,
rwy . length / 2.0 - 25.0 , & tshlat , & tshlon , & tshr ) ;
2004-12-22 23:57:07 +00:00
geo_direct_wgs_84 ( aptElev , ref . lat ( ) , ref . lon ( ) , runway . _heading ,
2003-03-27 15:41:09 +00:00
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
rwy . threshold_pos = Point3D ( tshlon , tshlat , aptElev ) ;
Point3D takeoff_end = Point3D ( tolon , tolat , aptElev ) ;
//cout << "Threshold position = " << tshlon << ", " << tshlat << ", " << aptElev << '\n';
//cout << "Takeoff position = " << tolon << ", " << tolat << ", " << aptElev << '\n';
2004-12-22 23:57:07 +00:00
rwy . hdg = runway . _heading ;
2003-03-27 15:41:09 +00:00
// 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
rwy . end2ortho = ortho . ConvertToLocal ( takeoff_end ) ;
2003-11-05 17:24:58 +00:00
// Set the pattern direction
// TODO - we'll check for a facilities file with this in eventually - for now assume left traffic except
// for certain circumstances (RH parallel rwy).
rwy . patternDirection = - 1 ; // Left
if ( rwy . rwyID . size ( ) = = 3 ) {
rwy . patternDirection = ( rwy . rwyID . substr ( 2 , 1 ) = = " R " ? 1 : - 1 ) ;
}
2004-01-23 17:18:24 +00:00
//cout << "Doing details, rwy.patterDirection is " << rwy.patternDirection << '\n';
2003-03-27 15:41:09 +00:00
} else {
SG_LOG ( SG_ATC , SG_ALERT , " Help - can't get good runway in FGTower!! " ) ;
activeRwy = " NN " ;
}
}
// Figure out if a given position lies on the active runway
// Might have to change when we consider more than one active rwy.
2005-10-25 13:49:55 +00:00
bool FGTower : : OnActiveRunway ( const Point3D & pt ) {
2003-03-31 16:24:41 +00:00
// 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 ) ;
2003-03-27 15:41:09 +00:00
2003-03-31 16:24:41 +00:00
//cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
//cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
double rlen = rwy . length / 2.0 + 5.0 ;
double rwidth = rwy . width / 2.0 ;
double ldiff = fabs ( xyp . y ( ) - xyc . y ( ) ) ;
double wdiff = fabs ( xyp . x ( ) - xyc . x ( ) ) ;
return ( ( ldiff < rlen ) & & ( wdiff < rwidth ) ) ;
2003-03-27 15:41:09 +00:00
}
// Figure out if a given position lies on any runway or not
2003-03-31 16:24:41 +00:00
// Only call this at startup - reading the runways database is expensive and needs to be fixed!
2005-10-25 13:49:55 +00:00
bool FGTower : : OnAnyRunway ( const Point3D & pt ) {
2003-03-27 15:41:09 +00:00
ATCData ad ;
double dist = current_commlist - > FindClosest ( lon , lat , elev , ad , TOWER , 10.0 ) ;
if ( dist < 0.0 ) {
return ( false ) ;
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
// Based on the airport-id, go through all the runways and check for a point in them
// TODO - do we actually need to search for the airport - surely we already know our ident and
// can just search runways of our airport???
//cout << "Airport ident is " << ad.ident << '\n';
FGRunway runway ;
2003-08-29 04:11:23 +00:00
bool rwyGood = globals - > get_runways ( ) - > search ( ad . ident , & runway ) ;
2003-03-27 15:41:09 +00:00
if ( ! rwyGood ) {
SG_LOG ( SG_ATC , SG_WARN , " Unable to find any runways for airport ID " < < ad . ident < < " in FGTower " ) ;
}
bool on = false ;
2004-12-22 23:57:07 +00:00
while ( runway . _id = = ad . ident ) {
2003-03-31 16:24:41 +00:00
on = OnRunway ( pt , runway ) ;
2004-12-22 23:57:07 +00:00
//cout << "Runway " << runway._rwy_no << ": On = " << (on ? "true\n" : "false\n");
2003-03-27 15:41:09 +00:00
if ( on ) return ( true ) ;
2003-08-29 04:11:23 +00:00
globals - > get_runways ( ) - > next ( & runway ) ;
2003-03-27 15:41:09 +00:00
}
return ( on ) ;
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
2003-09-19 16:51:27 +00:00
// Returns true if successful
2005-10-25 13:49:55 +00:00
bool FGTower : : RemoveFromTrafficList ( const string & id ) {
2003-09-19 16:51:27 +00:00
tower_plane_rec_list_iterator twrItr ;
for ( twrItr = trafficList . begin ( ) ; twrItr ! = trafficList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
if ( tpr - > plane . callsign = = id ) {
trafficList . erase ( twrItr ) ;
2004-01-23 17:18:24 +00:00
trafficListItr = trafficList . begin ( ) ;
2003-09-19 16:51:27 +00:00
return ( true ) ;
}
}
SG_LOG ( SG_ATC , SG_WARN , " Warning - unable to remove aircraft " < < id < < " from trafficList in FGTower " ) ;
return ( false ) ;
}
2004-01-23 17:18:24 +00:00
// Returns true if successful
2005-10-25 13:49:55 +00:00
bool FGTower : : RemoveFromAppList ( const string & id ) {
2004-01-23 17:18:24 +00:00
tower_plane_rec_list_iterator twrItr ;
for ( twrItr = appList . begin ( ) ; twrItr ! = appList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
if ( tpr - > plane . callsign = = id ) {
appList . erase ( twrItr ) ;
appListItr = appList . begin ( ) ;
return ( true ) ;
}
}
//SG_LOG(SG_ATC, SG_WARN, "Warning - unable to remove aircraft " << id << " from appList in FGTower");
return ( false ) ;
}
// Returns true if successful
2005-10-25 13:49:55 +00:00
bool FGTower : : RemoveFromRwyList ( const string & id ) {
2004-01-23 17:18:24 +00:00
tower_plane_rec_list_iterator twrItr ;
for ( twrItr = rwyList . begin ( ) ; twrItr ! = rwyList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
if ( tpr - > plane . callsign = = id ) {
rwyList . erase ( twrItr ) ;
rwyListItr = rwyList . begin ( ) ;
return ( true ) ;
}
}
//SG_LOG(SG_ATC, SG_WARN, "Warning - unable to remove aircraft " << id << " from rwyList in FGTower");
return ( false ) ;
}
2003-09-19 16:51:27 +00:00
// Add a tower plane rec with ETA to the traffic list in the correct position ETA-wise
// and set nextOnRwy if so.
// Returns true if this could cause a threshold ETA conflict with other traffic, false otherwise.
// For planes holding they are put in the first position with time to go, and the return value is
// true if in the first position (nextOnRwy) and false otherwise.
2003-09-22 23:57:57 +00:00
// See the comments in FGTower::doThresholdUseOrder for notes on the ordering
2003-09-19 16:51:27 +00:00
bool FGTower : : AddToTrafficList ( TowerPlaneRec * t , bool holding ) {
2003-09-22 23:57:57 +00:00
//cout << "ADD: " << trafficList.size();
2003-10-15 21:50:05 +00:00
//cout << "AddToTrafficList called, currently size = " << trafficList.size() << ", holding = " << holding << endl;
2003-09-19 16:51:27 +00:00
double separation_time = 90.0 ; // seconds - this is currently a guess for light plane separation, and includes a few seconds for a holding plane to taxi onto the rwy.
2003-09-22 23:57:57 +00:00
double departure_sep_time = 60.0 ; // Separation time behind departing airplanes. Comments above also apply.
2003-09-19 16:51:27 +00:00
bool conflict = false ;
double lastETA = 0.0 ;
bool firstTime = true ;
// FIXME - make this more robust for different plane types eg. light following heavy.
tower_plane_rec_list_iterator twrItr ;
2003-09-22 23:57:57 +00:00
//twrItr = trafficList.begin();
//while(1) {
2003-09-19 16:51:27 +00:00
for ( twrItr = trafficList . begin ( ) ; twrItr ! = trafficList . end ( ) ; twrItr + + ) {
2003-09-22 23:57:57 +00:00
//if(twrItr == trafficList.end()) {
// cout << " END ";
// trafficList.push_back(t);
// return(holding ? firstTime : conflict);
//} else {
TowerPlaneRec * tpr = * twrItr ;
if ( holding ) {
//cout << (tpr->isUser ? "USER!\n" : "NOT user\n");
//cout << "tpr->eta - lastETA = " << tpr->eta - lastETA << '\n';
double dep_allowance = ( timeSinceLastDeparture < departure_sep_time ? departure_sep_time - timeSinceLastDeparture : 0.0 ) ;
double slot_time = ( firstTime ? separation_time + dep_allowance : separation_time + departure_sep_time ) ;
// separation_time + departure_sep_time in the above accounts for the fact that the arrival could be touch and go,
// and if not needs time to clear the rwy anyway.
if ( tpr - > eta - lastETA > slot_time ) {
t - > nextOnRwy = firstTime ;
2003-09-19 16:51:27 +00:00
trafficList . insert ( twrItr , t ) ;
2003-09-22 23:57:57 +00:00
//cout << "\tH\t" << trafficList.size() << '\n';
return ( firstTime ) ;
}
firstTime = false ;
} else {
if ( t - > eta < tpr - > eta ) {
// Ugg - this one's tricky.
// 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
if ( tpr - > nextOnRwy ) {
tpr - > nextOnRwy = false ;
t - > nextOnRwy = true ;
}
trafficList . insert ( twrItr , t ) ;
} else { // Ooops - this ones tricky - we have a potential conflict!
conflict = true ;
// HACK - just add anyway for now and flag conflict - TODO - FIX THIS using CIRCUIT/STRAIGHT_IN and VFR/IFR precedence rules.
if ( tpr - > nextOnRwy ) {
tpr - > nextOnRwy = false ;
t - > nextOnRwy = true ;
}
trafficList . insert ( twrItr , t ) ;
}
//cout << "\tC\t" << trafficList.size() << '\n';
return ( conflict ) ;
2003-09-19 16:51:27 +00:00
}
}
2003-09-22 23:57:57 +00:00
//}
//++twrItr;
2003-09-19 16:51:27 +00:00
}
// If we get here we must be at the end of the list, or maybe the list is empty.
if ( ! trafficList . size ( ) ) {
t - > nextOnRwy = true ;
// conflict and firstTime should be false and true respectively in this case anyway.
2004-03-12 15:59:14 +00:00
} else {
t - > nextOnRwy = false ;
2003-09-19 16:51:27 +00:00
}
trafficList . push_back ( t ) ;
2003-10-15 21:50:05 +00:00
//cout << "\tE\t" << trafficList.size() << endl;
2003-09-19 16:51:27 +00:00
return ( holding ? firstTime : conflict ) ;
}
2003-10-19 20:38:08 +00:00
// Add a tower plane rec with ETA to the circuit list in the correct position ETA-wise
// Returns true if this might cause a separation conflict (based on ETA) with other traffic, false otherwise.
2004-01-23 17:18:24 +00:00
// Safe to add a plane that is already in - planes with the same callsign are not added.
2003-10-19 20:38:08 +00:00
bool FGTower : : AddToCircuitList ( TowerPlaneRec * t ) {
2004-01-23 17:18:24 +00:00
if ( ! t ) {
//cout << "**********************************************\n";
//cout << "AddToCircuitList called with NULL pointer!!!!!\n";
//cout << "**********************************************\n";
return false ;
}
2003-10-19 20:38:08 +00:00
//cout << "ADD: " << circuitList.size();
2004-01-23 17:18:24 +00:00
//cout << ident << " AddToCircuitList called for " << t->plane.callsign << ", currently size = " << circuitList.size() << endl;
2003-10-19 20:38:08 +00:00
double separation_time = 60.0 ; // seconds - this is currently a guess for light plane separation, and includes a few seconds for a holding plane to taxi onto the rwy.
bool conflict = false ;
tower_plane_rec_list_iterator twrItr ;
2004-01-23 17:18:24 +00:00
// First check if the plane is already in the list
//cout << "A" << endl;
//cout << "Checking whether " << t->plane.callsign << " is already in circuit list..." << endl;
//cout << "B" << endl;
2003-10-19 20:38:08 +00:00
for ( twrItr = circuitList . begin ( ) ; twrItr ! = circuitList . end ( ) ; twrItr + + ) {
2004-01-23 17:18:24 +00:00
if ( ( * twrItr ) - > plane . callsign = = t - > plane . callsign ) {
//cout << "In list - returning...\n";
return false ;
}
}
//cout << "Not in list - adding..." << endl;
for ( twrItr = circuitList . begin ( ) ; twrItr ! = circuitList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
//cout << tpr->plane.callsign << " eta is " << tpr->eta << '\n';
//cout << "New eta is " << t->eta << '\n';
if ( t - > eta < tpr - > eta ) {
// Ugg - this one's tricky.
// 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 ) ;
} 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 ) ;
}
//cout << "\tC\t" << circuitList.size() << '\n';
return ( conflict ) ;
}
2003-10-19 20:38:08 +00:00
}
// If we get here we must be at the end of the list, or maybe the list is empty.
2004-01-23 17:18:24 +00:00
//cout << ident << " adding " << t->plane.callsign << " to circuitList" << endl;
2003-10-19 20:38:08 +00:00
circuitList . push_back ( t ) ; // TODO - check the separation with the preceding plane for the conflict flag.
//cout << "\tE\t" << circuitList.size() << endl;
return ( conflict ) ;
}
2004-03-20 02:55:25 +00:00
// 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 ) ;
}
2003-09-19 16:51:27 +00:00
// Calculate the eta of a plane to the threshold.
2003-03-10 13:41:37 +00:00
// For ground traffic this is the fastest they can get there.
// For air traffic this is the middle approximation.
2003-09-22 23:57:57 +00:00
void FGTower : : CalcETA ( TowerPlaneRec * tpr , bool printout ) {
2003-03-10 13:41:37 +00:00
// For now we'll be very crude and hardwire expected speeds to C172-like values
2003-03-27 15:41:09 +00:00
// The speeds below are specified in knots IAS and then converted to m/s
double app_ias = 100.0 * 0.514444 ; // Speed during straight-in approach
double circuit_ias = 80.0 * 0.514444 ; // Speed around circuit
double final_ias = 70.0 * 0.514444 ; // Speed during final approach
2003-09-19 16:51:27 +00:00
2003-09-22 23:57:57 +00:00
//if(printout) {
2003-10-15 21:50:05 +00:00
//cout << "In CalcETA, airplane ident = " << tpr->plane.callsign << '\n';
//cout << (tpr->isUser ? "USER\n" : "AI\n");
//cout << flush;
2003-09-22 23:57:57 +00:00
//}
2003-03-27 15:41:09 +00:00
// Sign convention - dist_out is -ve for approaching planes and +ve for departing planes
// dist_across is +ve in the pattern direction - ie a plane correctly on downwind will have a +ve dist_across
2003-09-19 16:51:27 +00:00
Point3D op = ortho . ConvertToLocal ( tpr - > pos ) ;
2003-09-22 23:57:57 +00:00
//if(printout) {
2003-11-04 12:00:14 +00:00
//if(!tpr->isUser) cout << "Orthopos is " << op.x() << ", " << op.y() << ' ';
2004-01-23 17:18:24 +00:00
//cout << "opType is " << tpr->opType << '\n';
2003-09-22 23:57:57 +00:00
//}
2003-09-19 16:51:27 +00:00
double dist_out_m = op . y ( ) ;
2004-01-23 17:18:24 +00:00
double dist_across_m = fabs ( op . x ( ) ) ; // The fabs is a hack to cope with the fact that we don't know the circuit direction yet
2003-09-19 16:51:27 +00:00
//cout << "Doing ETA calc for " << tpr->plane.callsign << '\n';
2004-01-23 17:18:24 +00:00
if ( tpr - > opType = = STRAIGHT_IN | | tpr - > opType = = INBOUND ) {
//cout << "CASE 1\n";
2003-09-19 16:51:27 +00:00
double dist_to_go_m = sqrt ( ( dist_out_m * dist_out_m ) + ( dist_across_m * dist_across_m ) ) ;
if ( dist_to_go_m < 1000 ) {
tpr - > eta = dist_to_go_m / final_ias ;
} else {
tpr - > eta = ( 1000.0 / final_ias ) + ( ( dist_to_go_m - 1000.0 ) / app_ias ) ;
}
} else if ( tpr - > opType = = CIRCUIT | | tpr - > opType = = TTT_UNKNOWN ) { // Hack alert - UNKNOWN has sort of been added here as a temporary hack.
2004-01-23 17:18:24 +00:00
//cout << "CASE 2\n";
2003-09-19 16:51:27 +00:00
// It's complicated - depends on if base leg is delayed or not
2003-09-22 23:57:57 +00:00
//if(printout) {
2004-01-23 17:18:24 +00:00
//cout << "Leg = " << tpr->leg << '\n';
2003-09-22 23:57:57 +00:00
//}
2003-09-19 16:51:27 +00:00
if ( tpr - > leg = = LANDING_ROLL ) {
tpr - > eta = 0 ;
} else if ( ( tpr - > leg = = FINAL ) | | ( tpr - > leg = = TURN4 ) ) {
2004-01-23 17:18:24 +00:00
//cout << "dist_out_m = " << dist_out_m << '\n';
2003-09-19 16:51:27 +00:00
tpr - > eta = fabs ( dist_out_m ) / final_ias ;
} else if ( ( tpr - > leg = = BASE ) | | ( tpr - > leg = = TURN3 ) ) {
tpr - > eta = ( fabs ( dist_out_m ) / final_ias ) + ( dist_across_m / circuit_ias ) ;
} else {
// Need to calculate where base leg is likely to be
// FIXME - for now I'll hardwire it to 1000m which is what AILocalTraffic uses!!!
// TODO - as a matter of design - AILocalTraffic should get the nominal no-traffic base turn distance from Tower, since in real life the published pattern might differ from airport to airport
double nominal_base_dist_out_m = - 1000 ;
double current_base_dist_out_m ;
if ( ! GetBaseConstraint ( current_base_dist_out_m ) ) {
current_base_dist_out_m = nominal_base_dist_out_m ;
}
2003-11-04 12:00:14 +00:00
//cout << "current_base_dist_out_m = " << current_base_dist_out_m << '\n';
2003-09-19 16:51:27 +00:00
double nominal_dist_across_m = 1000 ; // Hardwired value from AILocalTraffic
double current_dist_across_m ;
if ( ! GetDownwindConstraint ( current_dist_across_m ) ) {
current_dist_across_m = nominal_dist_across_m ;
}
2004-01-23 17:18:24 +00:00
double nominal_cross_dist_out_m = 2000 ; // Bit of a guess - AI plane turns to crosswind at 700ft agl.
2003-09-19 16:51:27 +00:00
tpr - > eta = fabs ( current_base_dist_out_m ) / final_ias ; // final
2003-11-04 12:00:14 +00:00
//cout << "a = " << tpr->eta << '\n';
2003-09-19 16:51:27 +00:00
if ( ( tpr - > leg = = DOWNWIND ) | | ( tpr - > leg = = TURN2 ) ) {
tpr - > eta + = dist_across_m / circuit_ias ;
2003-11-04 12:00:14 +00:00
//cout << "b = " << tpr->eta << '\n';
2003-09-19 16:51:27 +00:00
tpr - > eta + = fabs ( current_base_dist_out_m - dist_out_m ) / circuit_ias ;
2003-11-04 12:00:14 +00:00
//cout << "c = " << tpr->eta << '\n';
2003-09-19 16:51:27 +00:00
} else if ( ( tpr - > leg = = CROSSWIND ) | | ( tpr - > leg = = TURN1 ) ) {
2004-01-23 17:18:24 +00:00
//cout << "CROSSWIND calc: ";
//cout << tpr->eta << ' ';
2003-09-22 23:57:57 +00:00
if ( dist_across_m > nominal_dist_across_m ) {
tpr - > eta + = dist_across_m / circuit_ias ;
2004-01-23 17:18:24 +00:00
//cout << "a ";
2003-09-22 23:57:57 +00:00
} else {
tpr - > eta + = nominal_dist_across_m / circuit_ias ;
2004-01-23 17:18:24 +00:00
//cout << "b ";
2003-09-22 23:57:57 +00:00
}
2004-01-23 17:18:24 +00:00
//cout << tpr->eta << ' ';
2003-09-22 23:57:57 +00:00
// should we use the dist across of the previous plane if there is previous still on downwind?
//if(printout) cout << "bb = " << tpr->eta << '\n';
if ( dist_out_m > nominal_cross_dist_out_m ) {
tpr - > eta + = fabs ( current_base_dist_out_m - dist_out_m ) / circuit_ias ;
2004-01-23 17:18:24 +00:00
//cout << "c ";
2003-09-22 23:57:57 +00:00
} else {
tpr - > eta + = fabs ( current_base_dist_out_m - nominal_cross_dist_out_m ) / circuit_ias ;
2004-01-23 17:18:24 +00:00
//cout << "d ";
2003-09-22 23:57:57 +00:00
}
2004-01-23 17:18:24 +00:00
//cout << tpr->eta << ' ';
2003-09-22 23:57:57 +00:00
//if(printout) cout << "cc = " << tpr->eta << '\n';
if ( nominal_dist_across_m > dist_across_m ) {
tpr - > eta + = ( nominal_dist_across_m - dist_across_m ) / circuit_ias ;
2004-01-23 17:18:24 +00:00
//cout << "e ";
2003-09-22 23:57:57 +00:00
} else {
// Nothing to add
2004-01-23 17:18:24 +00:00
//cout << "f ";
2003-09-22 23:57:57 +00:00
}
2004-01-23 17:18:24 +00:00
//cout << tpr->eta << '\n';
2003-09-22 23:57:57 +00:00
//if(printout) cout << "dd = " << tpr->eta << '\n';
2003-03-27 15:41:09 +00:00
} else {
2003-09-19 16:51:27 +00:00
// We've only just started - why not use a generic estimate?
2003-09-22 23:57:57 +00:00
tpr - > eta = 240.0 ;
2003-03-27 15:41:09 +00:00
}
}
2003-09-22 23:57:57 +00:00
//if(printout) {
// cout << "ETA = " << tpr->eta << '\n';
//}
2003-11-04 12:00:14 +00:00
//if(!tpr->isUser) cout << tpr->plane.callsign << '\t' << tpr->eta << '\n';
2003-09-19 16:51:27 +00:00
} else {
tpr - > eta = 99999 ;
}
}
// Calculate the distance of a plane to the threshold in meters
// TODO - Modify to calculate flying distance of a plane in the circuit
double FGTower : : CalcDistOutM ( TowerPlaneRec * tpr ) {
return ( dclGetHorizontalSeparation ( rwy . threshold_pos , tpr - > pos ) ) ;
}
// Calculate the distance of a plane to the threshold in miles
// TODO - Modify to calculate flying distance of a plane in the circuit
double FGTower : : CalcDistOutMiles ( TowerPlaneRec * tpr ) {
return ( CalcDistOutM ( tpr ) / 1600.0 ) ; // FIXME - use a proper constant if possible.
}
2004-01-23 17:18:24 +00:00
// Iterate through all the lists, update the position of, and call CalcETA for all the planes.
2003-09-19 16:51:27 +00:00
void FGTower : : doThresholdETACalc ( ) {
2003-10-15 21:50:05 +00:00
//cout << "Entering doThresholdETACalc..." << endl;
2003-09-19 16:51:27 +00:00
tower_plane_rec_list_iterator twrItr ;
// Do the approach list first
for ( twrItr = appList . begin ( ) ; twrItr ! = appList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
2004-01-23 17:18:24 +00:00
if ( ! ( tpr - > isUser ) ) tpr - > pos = tpr - > planePtr - > GetPos ( ) ;
//cout << "APP: ";
2003-09-19 16:51:27 +00:00
CalcETA ( tpr ) ;
}
// Then the circuit list
2004-01-23 17:18:24 +00:00
//cout << "Circuit list size is " << circuitList.size() << '\n';
2003-09-19 16:51:27 +00:00
for ( twrItr = circuitList . begin ( ) ; twrItr ! = circuitList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
2004-01-23 17:18:24 +00:00
if ( ! ( tpr - > isUser ) ) tpr - > pos = tpr - > planePtr - > GetPos ( ) ;
//cout << "CIRC: ";
2003-09-19 16:51:27 +00:00
CalcETA ( tpr ) ;
2003-03-10 13:41:37 +00:00
}
2003-10-15 21:50:05 +00:00
//cout << "Done doThresholdETCCalc" << endl;
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
2003-03-10 13:41:37 +00:00
2003-09-19 16:51:27 +00:00
// Check that the planes in traffic list are correctly ordered,
// that the nearest (timewise) is flagged next on rwy, and return
// true if any threshold use conflicts are detected, false otherwise.
2003-03-10 13:41:37 +00:00
bool FGTower : : doThresholdUseOrder ( ) {
2003-10-15 21:50:05 +00:00
//cout << "Entering doThresholdUseOrder..." << endl;
2003-09-19 16:51:27 +00:00
bool conflict = false ;
2003-09-22 23:57:57 +00:00
// Wipe out traffic list, go through circuit, app and hold list, and reorder them in traffic list.
// Here's the rather simplistic assumptions we're using:
// Currently all planes are assumed to be GA light singles with corresponding speeds and separation times.
// In order of priority for runway use:
// STRAIGHT_IN > CIRCUIT > HOLDING_FOR_DEPARTURE
// No modification of planes speeds occurs - conflicts are resolved by delaying turn for base,
// and holding planes until a space.
// When calculating if a holding plane can use the runway, time clearance from last departure
// as well as time clearance to next arrival must be considered.
trafficList . clear ( ) ;
tower_plane_rec_list_iterator twrItr ;
// Do the approach list first
2004-03-12 15:59:14 +00:00
//if(ident == "KRHV") cout << "A" << flush;
2003-09-22 23:57:57 +00:00
for ( twrItr = appList . begin ( ) ; twrItr ! = appList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
2004-03-12 15:59:14 +00:00
//if(ident == "KRHV") cout << tpr->plane.callsign << '\n';
2003-09-22 23:57:57 +00:00
conflict = AddToTrafficList ( tpr ) ;
}
// Then the circuit list
2004-03-12 15:59:14 +00:00
//if(ident == "KRHV") cout << "C" << flush;
2003-09-22 23:57:57 +00:00
for ( twrItr = circuitList . begin ( ) ; twrItr ! = circuitList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
2004-03-12 15:59:14 +00:00
//if(ident == "KRHV") cout << tpr->plane.callsign << '\n';
2003-09-22 23:57:57 +00:00
conflict = AddToTrafficList ( tpr ) ;
}
// And finally the hold list
2003-10-15 21:50:05 +00:00
//cout << "H" << endl;
2003-09-22 23:57:57 +00:00
for ( twrItr = holdList . begin ( ) ; twrItr ! = holdList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
AddToTrafficList ( tpr , true ) ;
}
2004-03-12 15:59:14 +00:00
2003-09-22 23:57:57 +00:00
if ( 0 ) {
2004-03-12 15:59:14 +00:00
//if(ident == "KRHV") {
cout < < " T \n " ;
2003-09-22 23:57:57 +00:00
for ( twrItr = trafficList . begin ( ) ; twrItr ! = trafficList . end ( ) ; twrItr + + ) {
TowerPlaneRec * tpr = * twrItr ;
cout < < tpr - > plane . callsign < < ' \t ' < < tpr - > eta < < ' \t ' ;
}
2003-10-15 21:50:05 +00:00
cout < < endl ;
2003-09-22 23:57:57 +00:00
}
2003-09-19 16:51:27 +00:00
2003-10-15 21:50:05 +00:00
//cout << "Done doThresholdUseOrder" << endl;
2003-09-19 16:51:27 +00:00
return ( conflict ) ;
2003-03-10 13:41:37 +00:00
}
2003-03-27 15:41:09 +00:00
2003-09-19 16:51:27 +00:00
// Return the ETA of plane no. list_pos (1-based) in the traffic list.
// i.e. list_pos = 1 implies next to use runway.
2003-09-22 23:57:57 +00:00
double FGTower : : GetTrafficETA ( unsigned int list_pos , bool printout ) {
2003-09-19 16:51:27 +00:00
if ( trafficList . size ( ) < list_pos ) {
return ( 99999 ) ;
}
tower_plane_rec_list_iterator twrItr ;
twrItr = trafficList . begin ( ) ;
for ( unsigned int i = 1 ; i < list_pos ; i + + , twrItr + + ) ;
2003-09-22 23:57:57 +00:00
TowerPlaneRec * tpr = * twrItr ;
CalcETA ( tpr , printout ) ;
//cout << "ETA returned = " << tpr->eta << '\n';
return ( tpr - > eta ) ;
2003-09-19 16:51:27 +00:00
}
2005-10-25 13:49:55 +00:00
void FGTower : : ContactAtHoldShort ( const PlaneRec & plane , FGAIPlane * requestee , tower_traffic_type operation ) {
2003-03-27 15:41:09 +00:00
// HACK - assume that anything contacting at hold short is new for now - FIXME LATER
TowerPlaneRec * t = new TowerPlaneRec ;
t - > plane = plane ;
t - > planePtr = requestee ;
t - > holdShortReported = true ;
t - > clearedToLineUp = false ;
t - > clearedToTakeOff = false ;
t - > opType = operation ;
2003-10-19 20:38:08 +00:00
t - > pos = requestee - > GetPos ( ) ;
2003-03-27 15:41:09 +00:00
2003-09-19 16:51:27 +00:00
//cout << "Hold Short reported by " << plane.callsign << '\n';
2003-10-19 20:38:08 +00:00
SG_LOG ( SG_ATC , SG_BULK , " Hold Short reported by " < < plane . callsign ) ;
2003-10-15 21:50:05 +00:00
/*
2003-09-19 16:51:27 +00:00
bool next = AddToTrafficList ( t , true ) ;
if ( next ) {
double teta = GetTrafficETA ( 2 ) ;
if ( teta < 150.0 ) {
t - > clearanceCounter = 7.0 ; // This reduces the delay before response to 3 secs if an immediate takeoff is reqd
//cout << "Reducing response time to request due imminent traffic\n";
}
} else {
}
2003-10-15 21:50:05 +00:00
*/
// TODO - possibly add the reduced interval to clearance when immediate back in under the new scheme
2003-03-27 15:41:09 +00:00
holdList . push_back ( t ) ;
2003-10-15 21:50:05 +00:00
responseReqd = true ;
2003-03-27 15:41:09 +00:00
}
2002-12-17 11:36:14 +00:00
2003-10-19 20:38:08 +00:00
// 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.
2005-10-25 13:49:55 +00:00
void FGTower : : RegisterAIPlane ( const PlaneRec & plane , FGAIPlane * ai , const tower_traffic_type & op , const PatternLeg & lg ) {
2003-10-19 20:38:08 +00:00
// At the moment this is only going to be tested with inserting an AI plane on downwind
TowerPlaneRec * t = new TowerPlaneRec ;
t - > plane = plane ;
t - > planePtr = ai ;
t - > opType = op ;
t - > leg = lg ;
t - > pos = ai - > GetPos ( ) ;
CalcETA ( t ) ;
if ( op = = CIRCUIT & & lg ! = LEG_UNKNOWN ) {
AddToCircuitList ( t ) ;
} else {
// FLAG A WARNING
}
doThresholdUseOrder ( ) ;
}
2005-10-25 13:49:55 +00:00
void FGTower : : DeregisterAIPlane ( const string & id ) {
2004-01-23 17:18:24 +00:00
RemovePlane ( id ) ;
}
2003-11-05 10:06:57 +00:00
// Contact tower for VFR approach
// 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
2005-10-25 13:49:55 +00:00
void FGTower : : VFRArrivalContact ( const string & ID , const LandingType & opt ) {
2004-01-23 17:18:24 +00:00
//cout << "USER Request Landing Clearance called for ID " << ID << '\n';
2003-09-19 16:51:27 +00:00
2003-11-05 10:06:57 +00:00
// For now we'll assume that the user is a light plane and can get him/her to join the circuit if necessary.
TowerPlaneRec * t ;
string usercall = fgGetString ( " /sim/user/callsign " ) ;
if ( ID = = " USER " | | ID = = usercall ) {
t = FindPlane ( usercall ) ;
if ( ! t ) {
2003-11-05 17:24:58 +00:00
//cout << "NOT t\n";
2003-11-05 10:06:57 +00:00
t = new TowerPlaneRec ;
t - > isUser = true ;
t - > pos . setlon ( user_lon_node - > getDoubleValue ( ) ) ;
t - > pos . setlat ( user_lat_node - > getDoubleValue ( ) ) ;
t - > pos . setelev ( user_elev_node - > getDoubleValue ( ) ) ;
} else {
2003-11-05 17:24:58 +00:00
//cout << "IS t\n";
2003-11-05 10:06:57 +00:00
// Oops - the plane is already registered with this tower - maybe we took off and flew a giant circuit without
// quite getting out of tower airspace - just ignore for now and treat as new arrival.
// TODO - Maybe should remove from departure and circuit list if in there though!!
}
} else {
// Oops - something has gone wrong - put out a warning
cout < < " WARNING - FGTower::VFRContact(string ID, LandingType lt) called with ID " < < ID < < " which does not appear to be the user. \n " ;
return ;
}
2003-09-19 16:51:27 +00:00
// TODO
2003-11-05 10:06:57 +00:00
// Calculate where the plane is in relation to the active runway and it's circuit
2003-09-19 16:51:27 +00:00
// and set the op-type as appropriate.
// HACK - to get up and running I'm going to assume that the user contacts tower on a staight-in final for now.
t - > opType = STRAIGHT_IN ;
t - > plane . type = GA_SINGLE ; // FIXME - Another assumption!
2003-11-05 10:06:57 +00:00
t - > plane . callsign = usercall ;
t - > vfrArrivalReported = true ;
responseReqd = true ;
2003-09-19 16:51:27 +00:00
appList . push_back ( t ) ; // Not necessarily permanent
AddToTrafficList ( t ) ;
2003-11-05 17:24:58 +00:00
current_atcdialog - > remove_entry ( ident , USER_REQUEST_VFR_ARRIVAL , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REQUEST_VFR_ARRIVAL_FULL_STOP , TOWER ) ;
current_atcdialog - > remove_entry ( ident , USER_REQUEST_VFR_ARRIVAL_TOUCH_AND_GO , TOWER ) ;
2002-12-17 11:36:14 +00:00
}
2003-09-19 16:51:27 +00:00
2004-01-23 17:18:24 +00:00
// landingType defaults to AIP_LT_UNKNOWN
2005-10-25 13:49:55 +00:00
void FGTower : : VFRArrivalContact ( const PlaneRec & plane , FGAIPlane * requestee , const LandingType & lt ) {
2004-01-23 17:18:24 +00:00
//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 ;
t - > plane = plane ;
t - > planePtr = requestee ;
t - > landingType = lt ;
t - > pos = requestee - > GetPos ( ) ;
//cout << "Hold Short reported by " << plane.callsign << '\n';
SG_LOG ( SG_ATC , SG_BULK , " VFR arrival contact made by " < < plane . callsign ) ;
//cout << "VFR arrival contact made by " << plane.callsign << '\n';
// HACK - to get up and running I'm going to assume a staight-in final for now.
t - > opType = STRAIGHT_IN ;
t - > vfrArrivalReported = true ;
responseReqd = true ;
//cout << "Before adding, appList.size = " << appList.size() << " at " << ident << '\n';
appList . push_back ( t ) ; // Not necessarily permanent
//cout << "After adding, appList.size = " << appList.size() << " at " << ident << '\n';
AddToTrafficList ( t ) ;
}
2005-10-25 13:49:55 +00:00
void FGTower : : RequestDepartureClearance ( const string & ID ) {
2003-03-27 15:41:09 +00:00
//cout << "Request Departure Clearance called...\n";
2003-10-16 16:06:22 +00:00
}
2005-10-25 13:49:55 +00:00
void FGTower : : ReportFinal ( const string & ID ) {
2004-01-23 17:18:24 +00:00
//cout << "Report Final Called at tower " << ident << " by plane " << ID << '\n';
2005-10-25 13:49:55 +00:00
string uid = ID ;
2003-11-05 17:24:58 +00:00
if ( ID = = " USER " ) {
2005-10-25 13:49:55 +00:00
uid = fgGetString ( " /sim/user/callsign " ) ;
2003-11-05 17:24:58 +00:00
current_atcdialog - > remove_entry ( ident , USER_REPORT_3_MILE_FINAL , TOWER ) ;
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec * t = FindPlane ( uid ) ;
2003-10-16 16:06:22 +00:00
if ( t ) {
t - > finalReported = true ;
t - > finalAcknowledged = false ;
if ( ! ( t - > clearedToLand ) ) {
responseReqd = true ;
2004-01-23 17:18:24 +00:00
} else {
// possibly respond with wind even if already cleared to land?
t - > finalReported = false ;
t - > finalAcknowledged = true ;
// HACK!! - prevents next reporting being misinterpreted as this one.
}
2003-10-16 16:06:22 +00:00
} else {
SG_LOG ( SG_ATC , SG_WARN , " WARNING: Unable to find plane " < < ID < < " in FGTower::ReportFinal(...) " ) ;
}
}
2005-10-25 13:49:55 +00:00
void FGTower : : ReportLongFinal ( const string & ID ) {
string uid = ID ;
2003-11-05 17:24:58 +00:00
if ( ID = = " USER " ) {
2005-10-25 13:49:55 +00:00
uid = fgGetString ( " /sim/user/callsign " ) ;
2003-11-05 17:24:58 +00:00
current_atcdialog - > remove_entry ( ident , USER_REPORT_3_MILE_FINAL , TOWER ) ;
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec * t = FindPlane ( uid ) ;
2003-11-05 17:24:58 +00:00
if ( t ) {
t - > longFinalReported = true ;
t - > longFinalAcknowledged = false ;
if ( ! ( t - > clearedToLand ) ) {
responseReqd = true ;
} // possibly respond with wind even if already cleared to land?
} else {
SG_LOG ( SG_ATC , SG_WARN , " WARNING: Unable to find plane " < < ID < < " in FGTower::ReportLongFinal(...) " ) ;
}
}
2002-12-17 11:36:14 +00:00
//void FGTower::ReportOuterMarker(string ID);
//void FGTower::ReportMiddleMarker(string ID);
//void FGTower::ReportInnerMarker(string ID);
2003-10-16 16:06:22 +00:00
2005-10-25 13:49:55 +00:00
void FGTower : : ReportRunwayVacated ( const string & ID ) {
2004-01-23 17:18:24 +00:00
//cout << "Report Runway Vacated Called at tower " << ident << " by plane " << ID << '\n';
2005-10-25 13:49:55 +00:00
string uid = ID ;
2003-11-05 17:24:58 +00:00
if ( ID = = " USER " ) {
2005-10-25 13:49:55 +00:00
uid = fgGetString ( " /sim/user/callsign " ) ;
2003-11-05 17:24:58 +00:00
current_atcdialog - > remove_entry ( ident , USER_REPORT_RWY_VACATED , TOWER ) ;
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec * t = FindPlane ( uid ) ;
2003-11-05 17:24:58 +00:00
if ( t ) {
2004-01-23 17:18:24 +00:00
//cout << "Found it...\n";
2003-11-05 17:24:58 +00:00
t - > rwyVacatedReported = true ;
responseReqd = true ;
} else {
SG_LOG ( SG_ATC , SG_WARN , " WARNING: Unable to find plane " < < ID < < " in FGTower::ReportRunwayVacated(...) " ) ;
2004-01-23 17:18:24 +00:00
SG_LOG ( SG_ATC , SG_ALERT , " A WARNING: Unable to find plane " < < ID < < " in FGTower::ReportRunwayVacated(...) " ) ;
//cout << "WARNING: Unable to find plane " << ID << " in FGTower::ReportRunwayVacated(...)\n";
2003-11-05 17:24:58 +00:00
}
2002-12-17 11:36:14 +00:00
}
2003-09-22 23:57:57 +00:00
2005-10-25 13:49:55 +00:00
TowerPlaneRec * FGTower : : FindPlane ( const string & ID ) {
2004-01-23 17:18:24 +00:00
//cout << "FindPlane called for " << ID << "...\n";
2003-10-15 14:10:30 +00:00
tower_plane_rec_list_iterator twrItr ;
// Do the approach list first
for ( twrItr = appList . begin ( ) ; twrItr ! = appList . end ( ) ; twrItr + + ) {
2004-01-23 17:18:24 +00:00
//cout << "appList callsign is " << (*twrItr)->plane.callsign << '\n';
2003-10-15 14:10:30 +00:00
if ( ( * twrItr ) - > plane . callsign = = ID ) return ( * twrItr ) ;
}
// Then the circuit list
for ( twrItr = circuitList . begin ( ) ; twrItr ! = circuitList . end ( ) ; twrItr + + ) {
2004-01-23 17:18:24 +00:00
//cout << "circuitList callsign is " << (*twrItr)->plane.callsign << '\n';
2003-10-15 14:10:30 +00:00
if ( ( * twrItr ) - > plane . callsign = = ID ) return ( * twrItr ) ;
}
2004-01-23 17:18:24 +00:00
// Then the runway list
//cout << "rwyList.size() is " << rwyList.size() << '\n';
for ( twrItr = rwyList . begin ( ) ; twrItr ! = rwyList . end ( ) ; twrItr + + ) {
//cout << "rwyList callsign is " << (*twrItr)->plane.callsign << '\n';
if ( ( * twrItr ) - > plane . callsign = = ID ) return ( * twrItr ) ;
}
// The hold list
2003-10-15 14:10:30 +00:00
for ( twrItr = holdList . begin ( ) ; twrItr ! = holdList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) return ( * twrItr ) ;
}
2004-01-23 17:18:24 +00:00
// And finally the vacated list
for ( twrItr = vacatedList . begin ( ) ; twrItr ! = vacatedList . end ( ) ; twrItr + + ) {
//cout << "vacatedList callsign is " << (*twrItr)->plane.callsign << '\n';
if ( ( * twrItr ) - > plane . callsign = = ID ) return ( * twrItr ) ;
}
2003-10-15 14:10:30 +00:00
SG_LOG ( SG_ATC , SG_WARN , " Unable to find " < < ID < < " in FGTower::FindPlane(...) " ) ;
2004-01-23 17:18:24 +00:00
//exit(-1);
2003-10-15 14:10:30 +00:00
return ( NULL ) ;
}
2005-10-25 13:49:55 +00:00
void FGTower : : RemovePlane ( const string & ID ) {
2004-01-23 17:18:24 +00:00
//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 + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) {
t = * twrItr ;
twrItr = appList . erase ( twrItr ) ;
appListItr = appList . begin ( ) ;
2005-12-11 13:17:20 +00:00
break ;
2004-01-23 17:18:24 +00:00
}
}
for ( twrItr = depList . begin ( ) ; twrItr ! = depList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) {
t = * twrItr ;
twrItr = depList . erase ( twrItr ) ;
depListItr = depList . begin ( ) ;
2005-12-11 13:17:20 +00:00
break ;
2004-01-23 17:18:24 +00:00
}
}
for ( twrItr = circuitList . begin ( ) ; twrItr ! = circuitList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) {
t = * twrItr ;
twrItr = circuitList . erase ( twrItr ) ;
circuitListItr = circuitList . begin ( ) ;
2005-12-11 13:17:20 +00:00
break ;
2004-01-23 17:18:24 +00:00
}
}
for ( twrItr = holdList . begin ( ) ; twrItr ! = holdList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) {
t = * twrItr ;
twrItr = holdList . erase ( twrItr ) ;
holdListItr = holdList . begin ( ) ;
2005-12-11 13:17:20 +00:00
break ;
2004-01-23 17:18:24 +00:00
}
}
for ( twrItr = rwyList . begin ( ) ; twrItr ! = rwyList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) {
t = * twrItr ;
twrItr = rwyList . erase ( twrItr ) ;
rwyListItr = rwyList . begin ( ) ;
2005-12-11 13:17:20 +00:00
break ;
2004-01-23 17:18:24 +00:00
}
}
for ( twrItr = vacatedList . begin ( ) ; twrItr ! = vacatedList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) {
t = * twrItr ;
twrItr = vacatedList . erase ( twrItr ) ;
vacatedListItr = vacatedList . begin ( ) ;
2005-12-11 13:17:20 +00:00
break ;
2004-01-23 17:18:24 +00:00
}
}
for ( twrItr = trafficList . begin ( ) ; twrItr ! = trafficList . end ( ) ; twrItr + + ) {
if ( ( * twrItr ) - > plane . callsign = = ID ) {
t = * twrItr ;
twrItr = trafficList . erase ( twrItr ) ;
trafficListItr = trafficList . begin ( ) ;
2005-12-11 13:17:20 +00:00
break ;
2004-01-23 17:18:24 +00:00
}
}
2005-10-26 18:20:27 +00:00
// And finally, delete the record.
delete t ;
2004-01-23 17:18:24 +00:00
}
2005-10-25 13:49:55 +00:00
void FGTower : : ReportDownwind ( const string & ID ) {
2003-10-15 14:10:30 +00:00
//cout << "ReportDownwind(...) called\n";
2005-10-25 13:49:55 +00:00
string uid = ID ;
2003-11-05 17:24:58 +00:00
if ( ID = = " USER " ) {
2005-10-25 13:49:55 +00:00
uid = fgGetString ( " /sim/user/callsign " ) ;
2003-11-05 17:24:58 +00:00
current_atcdialog - > remove_entry ( ident , USER_REPORT_DOWNWIND , TOWER ) ;
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec * t = FindPlane ( uid ) ;
2003-10-15 14:10:30 +00:00
if ( t ) {
t - > downwindReported = true ;
responseReqd = true ;
2004-01-23 17:18:24 +00:00
// If the plane is in the app list, remove it and put it in the circuit list instead.
// Ideally we might want to do this at the 2 mile report prior to 45 deg entry, but at
// the moment that would b&gg?r up the constraint position calculations.
RemoveFromAppList ( ID ) ;
t - > leg = DOWNWIND ;
2004-03-05 12:34:08 +00:00
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 ( ) ;
}
2004-01-23 17:18:24 +00:00
CalcETA ( t ) ;
2004-03-09 16:20:55 +00:00
AddToCircuitList ( t ) ;
} else {
SG_LOG ( SG_ATC , SG_WARN , " WARNING: Unable to find plane " < < ID < < " in FGTower::ReportDownwind(...) " ) ;
}
}
2005-10-25 13:49:55 +00:00
void FGTower : : ReportGoingAround ( const string & ID ) {
string uid = ID ;
2004-03-09 16:20:55 +00:00
if ( ID = = " USER " ) {
2005-10-25 13:49:55 +00:00
uid = fgGetString ( " /sim/user/callsign " ) ;
2004-03-09 16:20:55 +00:00
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 ) ;
}
2005-10-25 13:49:55 +00:00
TowerPlaneRec * t = FindPlane ( uid ) ;
2004-03-09 16:20:55 +00:00
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 ) ;
2004-01-23 17:18:24 +00:00
AddToCircuitList ( t ) ;
2003-10-15 14:10:30 +00:00
} else {
SG_LOG ( SG_ATC , SG_WARN , " WARNING: Unable to find plane " < < ID < < " in FGTower::ReportDownwind(...) " ) ;
}
2003-10-06 15:20:47 +00:00
}
2003-11-05 10:06:57 +00:00
string FGTower : : GenText ( const string & m , int c ) {
const int cmax = 300 ;
//string message;
char tag [ 4 ] ;
char crej = ' @ ' ;
char mes [ cmax ] ;
char dum [ cmax ] ;
//char buf[10];
char * pos ;
int len ;
//FGTransmission t;
string usercall = fgGetString ( " /sim/user/callsign " ) ;
//transmission_list_type tmissions = transmissionlist_station[station];
//transmission_list_iterator current = tmissions.begin();
//transmission_list_iterator last = tmissions.end();
//for ( ; current != last ; ++current ) {
// if ( current->get_code().c1 == code.c1 &&
// current->get_code().c2 == code.c2 &&
// current->get_code().c3 == code.c3 ) {
//if ( ttext ) message = current->get_transtext();
//else message = current->get_menutext();
strcpy ( & mes [ 0 ] , m . c_str ( ) ) ;
// Replace all the '@' parameters with the actual text.
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 ) ;
2004-04-30 00:52:11 +00:00
memmove ( & tag [ 0 ] , pos , 3 ) ;
2003-11-05 10:06:57 +00:00
tag [ 3 ] = ' \0 ' ;
int i ;
len = 0 ;
for ( i = 0 ; i < cmax ; i + + ) {
if ( mes [ i ] = = crej ) {
len = i ;
break ;
}
}
strncpy ( & dum [ 0 ] , & mes [ 0 ] , len ) ;
dum [ len ] = ' \0 ' ;
if ( strcmp ( tag , " @ST " ) = = 0 )
//strcat( &dum[0], tpars.station.c_str() );
strcat ( & dum [ 0 ] , ident . c_str ( ) ) ;
else if ( strcmp ( tag , " @AP " ) = = 0 )
//strcat( &dum[0], tpars.airport.c_str() );
strcat ( & dum [ 0 ] , name . c_str ( ) ) ;
else if ( strcmp ( tag , " @CS " ) = = 0 )
//strcat( &dum[0], tpars.callsign.c_str() );
strcat ( & dum [ 0 ] , usercall . c_str ( ) ) ;
else if ( strcmp ( tag , " @TD " ) = = 0 ) {
/*
if ( tpars . tdir = = 1 ) {
char buf [ ] = " left " ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
}
else {
char buf [ ] = " right " ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
}
*/
}
else if ( strcmp ( tag , " @HE " ) = = 0 ) {
/*
char buf [ 10 ] ;
sprintf ( buf , " %i " , ( int ) ( tpars . heading ) ) ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
*/
}
else if ( strcmp ( tag , " @VD " ) = = 0 ) {
/*
if ( tpars . VDir = = 1 ) {
char buf [ ] = " Descend and maintain " ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
}
else if ( tpars . VDir = = 2 ) {
char buf [ ] = " Maintain " ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
}
else if ( tpars . VDir = = 3 ) {
char buf [ ] = " Climb and maintain " ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
}
*/
}
else if ( strcmp ( tag , " @AL " ) = = 0 ) {
/*
char buf [ 10 ] ;
sprintf ( buf , " %i " , ( int ) ( tpars . alt ) ) ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
*/
}
else if ( strcmp ( tag , " @MI " ) = = 0 ) {
char buf [ 10 ] ;
//sprintf( buf, "%3.1f", tpars.miles );
2004-01-23 17:18:24 +00:00
int dist_miles = ( int ) dclGetHorizontalSeparation ( Point3D ( lon , lat , elev ) , Point3D ( user_lon_node - > getDoubleValue ( ) , user_lat_node - > getDoubleValue ( ) , user_elev_node - > getDoubleValue ( ) ) ) / 1600 ;
2003-11-05 10:06:57 +00:00
sprintf ( buf , " %i " , dist_miles ) ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
}
else if ( strcmp ( tag , " @FR " ) = = 0 ) {
/*
char buf [ 10 ] ;
sprintf ( buf , " %6.2f " , tpars . freq ) ;
strcat ( & dum [ 0 ] , & buf [ 0 ] ) ;
*/
}
else if ( strcmp ( tag , " @RW " ) = = 0 ) {
strcat ( & dum [ 0 ] , ConvertRwyNumToSpokenString ( activeRwy ) . c_str ( ) ) ;
} else if ( strcmp ( tag , " @CD " ) = = 0 ) { // @CD = compass direction
double h = GetHeadingFromTo ( Point3D ( lon , lat , elev ) , Point3D ( user_lon_node - > getDoubleValue ( ) , user_lat_node - > getDoubleValue ( ) , user_elev_node - > getDoubleValue ( ) ) ) ;
while ( h < 0.0 ) h + = 360.0 ;
while ( h > 360.0 ) h - = 360.0 ;
if ( h < 22.5 | | h > 337.5 ) {
strcat ( & dum [ 0 ] , " North " ) ;
} else if ( h < 67.5 ) {
strcat ( & dum [ 0 ] , " North-East " ) ;
} else if ( h < 112.5 ) {
strcat ( & dum [ 0 ] , " East " ) ;
} else if ( h < 157.5 ) {
strcat ( & dum [ 0 ] , " South-East " ) ;
} else if ( h < 202.5 ) {
strcat ( & dum [ 0 ] , " South " ) ;
} else if ( h < 247.5 ) {
strcat ( & dum [ 0 ] , " South-West " ) ;
} else if ( h < 292.5 ) {
strcat ( & dum [ 0 ] , " West " ) ;
} else {
strcat ( & dum [ 0 ] , " North-West " ) ;
}
} else {
cout < < " Tag " < < tag < < " not found " < < endl ;
break ;
}
strcat ( & dum [ 0 ] , & mes [ len + 3 ] ) ;
strcpy ( & mes [ 0 ] , & dum [ 0 ] ) ;
+ + check ;
if ( check > 10 ) {
SG_LOG ( SG_ATC , SG_WARN , " WARNING: Possibly endless loop terminated in FGTransmissionlist::gen_text(...) " ) ;
break ;
}
}
//cout << mes << endl;
//break;
//}
//}
if ( mes ! = " " ) return mes ;
else return " No transmission found " ;
}
2003-09-22 23:57:57 +00:00
ostream & operator < < ( ostream & os , tower_traffic_type ttt ) {
switch ( ttt ) {
case ( CIRCUIT ) : return ( os < < " CIRCUIT " ) ;
case ( INBOUND ) : return ( os < < " INBOUND " ) ;
case ( OUTBOUND ) : return ( os < < " OUTBOUND " ) ;
case ( TTT_UNKNOWN ) : return ( os < < " UNKNOWN " ) ;
case ( STRAIGHT_IN ) : return ( os < < " STRAIGHT_IN " ) ;
}
return ( os < < " ERROR - Unknown switch in tower_traffic_type operator << " ) ;
}