]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/AILocalTraffic.cxx
Moved random ground cover object management code (userdata.[ch]xx) over
[flightgear.git] / src / ATC / AILocalTraffic.cxx
index 1285aee5fe77cfef3a67700e921d8cf2076bfcee..4a7b0da31f059221118f0b7c2dd976cede87fcbc 100644 (file)
 #  include <config.h>
 #endif
 
+#include <simgear/scene/model/location.hxx>
+
+#include <Airports/runways.hxx>
 #include <Main/globals.hxx>
-#include <Main/location.hxx>
 #include <Scenery/scenery.hxx>
+#include <Scenery/tilemgr.hxx>
 #include <simgear/math/point3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/misc/sg_path.hxx>
@@ -39,6 +42,16 @@ SG_USING_STD(string);
 #include "ATCutils.hxx"
 
 FGAILocalTraffic::FGAILocalTraffic() {
+       ATC = globals->get_ATC_mgr();
+       
+       // TODO - unhardwire this - possibly let the AI manager set the callsign
+       plane.callsign = "Trainer-two-five-charlie";
+       plane.type = GA_SINGLE;
+       
+       roll = 0.0;
+       pitch = 0.0;
+       hdg = 270.0;
+       
        //Hardwire initialisation for now - a lot of this should be read in from config eventually
        Vr = 70.0;
        best_rate_of_climb_speed = 70.0;
@@ -54,35 +67,124 @@ FGAILocalTraffic::FGAILocalTraffic() {
        //stall_speed_landing_config;
        nominalTaxiSpeed = 8.0;
        taxiTurnRadius = 8.0;
+       wheelOffset = 1.45;     // Warning - hardwired to the C172 - we need to read this in from file.
+       elevInitGood = false;
        // Init the property nodes
        wind_from_hdg = fgGetNode("/environment/wind-from-heading-deg", true);
        wind_speed_knots = fgGetNode("/environment/wind-speed-kts", true);
        circuitsToFly = 0;
+       liningUp = false;
+       taxiRequestPending = false;
+       taxiRequestCleared = false;
+       holdingShort = false;
+       clearedToLineUp = false;
+       clearedToTakeOff = false;
+       reportReadyForDeparture = false;
+       contactTower = false;
+       contactGround = false;
 }
 
 FGAILocalTraffic::~FGAILocalTraffic() {
 }
 
-void FGAILocalTraffic::Init() {
+
+// Get details of the active runway
+// It is assumed that by the time this is called the tower control and airport code will have been set up.
+void FGAILocalTraffic::GetRwyDetails() {
+       //cout << "GetRwyDetails called" << endl;
+       
+       rwy.rwyID = tower->GetActiveRunway();
+       
+       // Now we need to get the threshold position and rwy heading
+       
+       SGPath path( globals->get_fg_root() );
+       path.append( "Airports" );
+       path.append( "runways.mk4" );
+       FGRunways runways( path.c_str() );
+
+       FGRunway runway;
+       bool rwyGood = runways.search(airportID, rwy.rwyID, &runway);
+       if(rwyGood) {
+               // Get the threshold position
+       hdg = runway.heading;   // TODO - check - is this our heading we are setting here, and if so should we be?
+               //cout << "hdg reset to " << hdg << '\n';
+               double other_way = hdg - 180.0;
+               while(other_way <= 0.0) {
+                       other_way += 360.0;
+               }
+
+       // move to the +l end/center of the runway
+               //cout << "Runway center is at " << runway.lon << ", " << runway.lat << '\n';
+       Point3D origin = Point3D(runway.lon, runway.lat, aptElev);
+               Point3D ref = origin;
+       double tshlon, tshlat, tshr;
+               double tolon, tolat, tor;
+               rwy.length = runway.length * SG_FEET_TO_METER;
+       geo_direct_wgs_84 ( aptElev, ref.lat(), ref.lon(), other_way, 
+                               rwy.length / 2.0 - 25.0, &tshlat, &tshlon, &tshr );
+       geo_direct_wgs_84 ( aptElev, ref.lat(), ref.lon(), hdg, 
+                               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';
+               rwy.hdg = hdg;
+               // Set the projection for the local area
+               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);
+       } else {
+               SG_LOG(SG_ATC, SG_ALERT, "Help  - can't get good runway in FGAILocalTraffic!!\n");
+       }
+}
+
+/* 
+There are two possible scenarios during initialisation:
+The first is that the user is flying towards the airport, and hence the traffic
+could be initialised anywhere, as long as the AI planes are consistent with
+each other.
+The second is that the user has started the sim at or close to the airport, and
+hence the traffic must be initialised with respect to the user as well as each other.
+To a certain extent it's FGAIMgr that has to worry about this, but we need to provide
+sufficient initialisation functionality within the plane classes to allow the manager
+to initialy position them where and how required.
+*/
+bool FGAILocalTraffic::Init(string ICAO, OperatingState initialState, PatternLeg initialLeg) {
+       //cout << "FGAILocalTraffic.Init(...) called" << endl;
        // Hack alert - Hardwired path!!
        string planepath = "Aircraft/c172/Models/c172-dpm.ac";
        SGPath path = globals->get_fg_root();
        path.append(planepath);
-       aip.init(planepath.c_str());
-       aip.setVisible(true);
+        ssgBranch *model = sgLoad3DModel( path.str(),
+                                          planepath.c_str(),
+                                          globals->get_props(),
+                                          globals->get_sim_time_sec() );
+       aip.init( model );
+       aip.setVisible(false);          // This will be set to true once a valid ground elevation has been determined
        globals->get_scenery()->get_scene_graph()->addKid(aip.getSceneGraph());
-       // is it OK to leave it like this until the first time transform is called?
-       // Really ought to be started in a parking space unless otherwise specified?
        
        // Find the tower frequency - this is dependent on the ATC system being initialised before the AI system
-       // FIXME - ATM this is hardwired.
-       airportID = "KEMT";
+       airportID = ICAO;
        AirportATC a;
-       if(globals->get_ATC_mgr()->GetAirportATCDetails((string)airportID, &a)) {
+       if(ATC->GetAirportATCDetails(airportID, &a)) {
                if(a.tower_freq) {      // Has a tower
-                       tower = (FGTower*)globals->get_ATC_mgr()->GetATCPointer((string)airportID, TOWER);      // Maybe need some error checking here
+                       tower = (FGTower*)ATC->GetATCPointer((string)airportID, TOWER); // Maybe need some error checking here
+                       if(tower == NULL) {
+                               // Something has gone wrong - abort or carry on with un-towered operation?
+                               return(false);
+                       }
                        freq = (double)tower->get_freq() / 100.0;
-                       //cout << "***********************************AILocalTraffic freq = " << freq << '\n';
+                       ground = tower->GetGroundPtr();
+                       if(ground == NULL) {
+                               // Something has gone wrong :-(
+                               SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't get a ground pointer from tower control in FGAILocalTraffic::Init() :-(");
+                               return(false);
+                       } else if((initialState == PARKED) || (initialState == TAXIING)) {
+                               freq = (double)ground->get_freq() / 100.0;
+                       }
+                       //cout << "AILocalTraffic freq is " << freq << '\n';
                } else {
                        // Check CTAF, unicom etc
                }
@@ -90,105 +192,384 @@ void FGAILocalTraffic::Init() {
                //cout << "Unable to find airport details in FGAILocalTraffic::Init()\n";
        }
 
-       // Initiallise the FGAirportData structure
-       // This needs a complete overhaul soon - what happens if we have 2 AI planes at same airport - they don't both need a structure
-       // This needs to be handled by the ATC manager or similar so only one set of physical data per airport is instantiated
-       // ie. TODO TODO FIXME FIXME
-       airport.Init();
+       // Get the airport elevation
+       aptElev = dclGetAirportElev(airportID.c_str()) * SG_FEET_TO_METER;
+       //cout << "Airport elev in AILocalTraffic = " << aptElev << '\n';
+       // WARNING - we use this elev for the whole airport - some assumptions in the code 
+       // might fall down with very slopey airports.
+
+       //cout << "In Init(), initialState = " << initialState << endl;
+       operatingState = initialState;
+       switch(operatingState) {
+       case PARKED:
+               ourGate = ground->GetGateNode();
+               if(ourGate == NULL) {
+                       // Implies no available gates - what shall we do?
+                       // For now just vanish the plane - possibly we can make this more elegant in the future
+                       SG_LOG(SG_ATC, SG_ALERT, "No gate found by FGAILocalTraffic whilst attempting Init at " << airportID << '\n');
+                       return(false);
+               }
+               pitch = 0.0;
+               roll = 0.0;
+               vel = 0.0;
+               slope = 0.0;
+               pos = ourGate->pos;
+               pos.setelev(aptElev);
+               hdg = ourGate->heading;
+               
+               // Now we've set the position we can do the ground elev
+               elevInitGood = false;
+               inAir = false;
+               DoGroundElev();
+               
+               Transform();
+               break;
+       case TAXIING:
+               // FIXME - implement this case properly
+               return(false);  // remove this line when fixed!
+               break;
+       case IN_PATTERN:
+               // For now we'll always start the in_pattern case on the threshold ready to take-off
+               // since we've got the implementation for this case already.
+               // TODO - implement proper generic in_pattern startup.
+               
+               // Get the active runway details (and copy them into rwy)
+               GetRwyDetails();
+
+               // Initial position on threshold for now
+               pos.setlat(rwy.threshold_pos.lat());
+               pos.setlon(rwy.threshold_pos.lon());
+               pos.setelev(rwy.threshold_pos.elev());
+               hdg = rwy.hdg;
+               
+               // Now we've set the position we can do the ground elev
+               // This might not always be necessary if we implement in-air start
+               elevInitGood = false;
+               inAir = false;
+               DoGroundElev();
+               
+               pitch = 0.0;
+               roll = 0.0;
+               leg = TAKEOFF_ROLL;
+               vel = 0.0;
+               slope = 0.0;
+               
+               circuitsToFly = 0;              // ie just fly this circuit and then stop
+               touchAndGo = false;
+               // FIXME TODO - pattern direction is still hardwired
+               patternDirection = -1;          // Left
+               // At the bare minimum we ought to make sure it goes the right way at dual parallel rwy airports!
+               if(rwy.rwyID.size() == 3) {
+                       patternDirection = (rwy.rwyID.substr(2,1) == "R" ? 1 : -1);
+               }
+               
+               operatingState = IN_PATTERN;
+               
+               Transform();
+               break;
+       default:
+               SG_LOG(SG_ATC, SG_ALERT, "Attempt to set unknown operating state in FGAILocalTraffic.Init(...)\n");
+               return(false);
+       }
+       
        
+       return(true);
 }
 
 // Commands to do something from higher level logic
 void FGAILocalTraffic::FlyCircuits(int numCircuits, bool tag) {
-       circuitsToFly += numCircuits - 1;       // Hack (-1) because we only test and decrement circuitsToFly after landing
-                                                                               // thus flying one to many circuits.  TODO - Need to sort this out better!
-       touchAndGo = tag;
+       //cout << "FlyCircuits called" << endl;
        
-       //At the moment we'll assume that we are always finished previous circuits when called,
-       //And just teleport to the threshold to start.
-       //This is a hack though, we need to check where we are and taxi out if appropriate.
-       operatingState = IN_PATTERN;
-
-       // Hardwire to KEMT for now
-       // Hardwired points at each end of KEMT runway
-       Point3D P010(-118.037483, 34.081358, 296 * SG_FEET_TO_METER);
-       Point3D P190(-118.032308, 34.090456, 299.395263 * SG_FEET_TO_METER);
-       Point3D takeoff_end;
-       bool d010 = true;       // use this to change the hardwired runway direction
-       if(d010) {
-               rwy.threshold_pos = P010;
-               takeoff_end = P190;
-               rwy.hdg = 25.32;        //from default.apt
-               rwy.ID = 1;
-               patternDirection = -1;  // Left
-               pos.setelev(rwy.threshold_pos.elev() + (-8.5 * SG_FEET_TO_METER));  // This is a complete hack - the rendered runway takes the underlying scenery elev rather than the published runway elev so I should use height above terrain or something.
-       } else {
-               rwy.threshold_pos = P190;
-               takeoff_end = P010;
-               rwy.hdg = 205.32;
-               rwy.ID = 19;
-               patternDirection = 1;   // Right
-               pos.setelev(rwy.threshold_pos.elev() + (-0.0 * SG_FEET_TO_METER));  // This is a complete hack - the rendered runway takes the underlying scenery elev rather than the published runway elev so I should use height above terrain or something.
+       switch(operatingState) {
+       case IN_PATTERN:
+               circuitsToFly += numCircuits;
+               return;
+               break;
+       case TAXIING:
+               // For now we'll punt this and do nothing
+               break;
+       case PARKED:
+               circuitsToFly = numCircuits;    // Note that one too many circuits gets flown because we only test and decrement circuitsToFly after landing
+                                                                               // thus flying one too many circuits.  TODO - Need to sort this out better!
+               touchAndGo = tag;
+#if 0  
+               // Get the active runway details (and copy them into rwy)
+               GetRwyDetails();
+               
+               // Get the takeoff node for the active runway, get a path to it and start taxiing
+               path = ground->GetPath(ourGate, rwy.rwyID);
+               if(path.size() < 2) {
+                       // something has gone wrong
+                       SG_LOG(SG_ATC, SG_ALERT, "Invalid path from gate to theshold in FGAILocalTraffic::FlyCircuits\n");
+                       return;
+               }
+               /*
+               cout << "path returned was:" << endl;
+               for(unsigned int i=0; i<path.size(); ++i) {
+                       switch(path[i]->struct_type) {
+                       case NODE:
+                               cout << "NODE " << ((node*)(path[i]))->nodeID << endl;
+                               break;
+                       case ARC:
+                               cout << "ARC\n";
+                               break;
+                       }
+               }
+               */
+               // pop the gate - we're here already!
+               path.erase(path.begin());
+               //path.erase(path.begin());
+               /*
+               cout << "path after popping front is:" << endl;
+               for(unsigned int i=0; i<path.size(); ++i) {
+                       switch(path[i]->struct_type) {
+                       case NODE:
+                               cout << "NODE " << ((node*)(path[i]))->nodeID << endl;
+                               break;
+                       case ARC:
+                               cout << "ARC\n";
+                               break;
+                       }
+               }
+               */
+               
+               taxiState = TD_OUTBOUND;
+               StartTaxi();
+               
+               // Maybe the below should be set when we get to the threshold and prepare for TO?
+               // FIXME TODO - pattern direction is still hardwired
+               patternDirection = -1;          // Left
+               // At the bare minimum we ought to make sure it goes the right way at dual parallel rwy airports!
+               if(rwy.rwyID.size() == 3) {
+                       patternDirection = (rwy.rwyID.substr(2,1) == "R" ? 1 : -1);
+               }
+
+               Transform();
+#endif
+               break;
        }
-       
-       //rwy.threshold_pos.setlat(34.081358);
-       //rwy.threshold_pos.setlon(-118.037483);
-       //rwy.mag_hdg = 12.0;
-       //rwy.mag_var = 14.0;
-       //rwy.hdg = rwy.mag_hdg + rwy.mag_var;
-       //rwy.threshold_pos.setelev(296 * SG_FEET_TO_METER);
-       
-       // Initial position on threshold for now
-       // TODO - check wind / default runway
-       pos.setlat(rwy.threshold_pos.lat());
-       pos.setlon(rwy.threshold_pos.lon());
-       hdg = rwy.hdg;
-       
-       pitch = 0.0;
-       roll = 0.0;
-       leg = TAKEOFF_ROLL;
-       vel = 0.0;
-       slope = 0.0;
-       
-       // Now set the position of the plane and then re-get the elevation!! (Didn't work - elev always returned as zero) :-(
-       //aip.setPosition(pos.lon(), pos.lat(), pos.elev() * SG_METER_TO_FEET);
-       //cout << "*********************** elev in FGAILocalTraffic = " << aip.getFGLocation()->get_cur_elev_m() << '\n';
-       
-       // Set the projection for the local area
-       ortho.Init(rwy.threshold_pos, rwy.hdg); 
-       rwy.end1ortho = ortho.ConvertToLocal(rwy.threshold_pos);        // should come out as zero
-       // Hardwire to KEMT for now
-       rwy.end2ortho = ortho.ConvertToLocal(takeoff_end);
-       //cout << "*********************************************************************************\n";
-       //cout << "*********************************************************************************\n";
-       //cout << "*********************************************************************************\n";
-       //cout << "end1ortho = " << rwy.end1ortho << '\n';
-       //cout << "end2ortho = " << rwy.end2ortho << '\n';      // end2ortho.x() should be zero or thereabouts
-       
-       Transform();
 }   
 
 // Run the internal calculations
 void FGAILocalTraffic::Update(double dt) {
-       //std::cout << "In FGAILocalTraffic::Update\n";
-       // Hardwire flying traffic pattern for now - eventually also needs to be able to taxi to and from runway and GA parking area.
+       //cout << "A" << flush;
+       double responseTime = 10.0;             // seconds - this should get more sophisticated at some point
+       responseCounter += dt;
+       if((contactTower) && (responseCounter >= 8.0)) {
+               // Acknowledge request before changing frequency so it gets rendered if the user is on the same freq
+               string trns = "Tower ";
+               double f = globals->get_ATC_mgr()->GetFrequency(airportID, TOWER) / 100.0;      
+               char buf[10];
+               sprintf(buf, "%f", f);
+               trns += buf;
+               trns += " ";
+               trns += plane.callsign;
+               Transmit(trns);
+               responseCounter = 0.0;
+               contactTower = false;
+               changeFreq = true;
+               changeFreqType = TOWER;
+       }
+       
+       if((changeFreq) && (responseCounter > 8.0)) {
+               switch(changeFreqType) {
+               case TOWER:
+                       freq = (double)tower->get_freq() / 100.0;
+                       //Transmit("DING!");
+                       // Contact the tower, even if only virtually
+                       changeFreq = false;
+                       tower->ContactAtHoldShort(plane, this, CIRCUIT);
+                       break;
+               case GROUND:
+                       freq = (double)ground->get_freq() / 100.0;
+                       break;
+               // And to avoid compiler warnings...
+               case APPROACH:
+                       break;
+               case ATIS:
+                       break;
+               case ENROUTE:
+                       break;
+               case DEPARTURE:
+                       break;
+               case INVALID:
+                       break;
+               }
+       }
+       
+       //cout << "." << flush;
+               
        switch(operatingState) {
        case IN_PATTERN:
+               //cout << "In IN_PATTERN\n";
+               if(!inAir) DoGroundElev();
+               if(!elevInitGood) {
+                       if(aip.getSGLocation()->get_cur_elev_m() > -9990.0) {
+                               pos.setelev(aip.getSGLocation()->get_cur_elev_m() + wheelOffset);
+                               //cout << "TAKEOFF_ROLL, POS = " << pos.lon() << ", " << pos.lat() << ", " << pos.elev() << '\n';
+                               //Transform();
+                               aip.setVisible(true);
+                               //cout << "Making plane visible!\n";
+                               elevInitGood = true;
+                       }
+               }
                FlyTrafficPattern(dt);
                Transform();
                break;
        case TAXIING:
-               Taxi(dt);
+               //cout << "In TAXIING\n";
+               //cout << "*" << flush;
+               if(!elevInitGood) {
+                       //DoGroundElev();
+                       if(aip.getSGLocation()->get_cur_elev_m() > -9990.0) {
+                               pos.setelev(aip.getSGLocation()->get_cur_elev_m() + wheelOffset);
+                               //Transform();
+                               aip.setVisible(true);
+                               //Transform();
+                               //cout << "Making plane visible!\n";
+                               elevInitGood = true;
+                       }
+               }
+               DoGroundElev();
+               //cout << "," << flush;
+               if(!((holdingShort) && (!clearedToLineUp))) {
+                       //cout << "|" << flush;
+                       Taxi(dt);
+               }
+               //cout << ";" << flush;
+               if((clearedToTakeOff) && (responseCounter >= 8.0)) {
+                       // possible assumption that we're at the hold short here - may not always hold
+                       // TODO - sort out the case where we're cleared to line-up first and then cleared to take-off on the rwy.
+                       taxiState = TD_LINING_UP;
+                       path = ground->GetPath(holdShortNode, rwy.rwyID);
+                       /*
+                       cout << "path returned was:" << endl;
+                       for(unsigned int i=0; i<path.size(); ++i) {
+                               switch(path[i]->struct_type) {
+                                       case NODE:
+                                       cout << "NODE " << ((node*)(path[i]))->nodeID << endl;
+                                       break;
+                                       case ARC:
+                                       cout << "ARC\n";
+                                       break;
+                               }
+                       }
+                       */
+                       clearedToTakeOff = false;       // We *are* still cleared - this simply stops the response recurring!!
+                       holdingShort = false;
+                       string trns = "Cleared for take-off ";
+                       trns += plane.callsign;
+                       Transmit(trns);
+                       StartTaxi();
+               }
+               //cout << "^" << flush;
                Transform();
                break;
-       case PARKED:
+               case PARKED:
+               //cout << "In PARKED\n";
+               if(!elevInitGood) {
+                       DoGroundElev();
+                       if(aip.getSGLocation()->get_cur_elev_m() > -9990.0) {
+                               pos.setelev(aip.getSGLocation()->get_cur_elev_m() + wheelOffset);
+                               //Transform();
+                               aip.setVisible(true);
+                               //Transform();
+                               //cout << "Making plane visible!\n";
+                               elevInitGood = true;
+                       }
+               }
+               
+               if(circuitsToFly) {
+                       if((taxiRequestPending) && (taxiRequestCleared)) {
+                               //cout << "&" << flush;
+                               // Get the active runway details (and copy them into rwy)
+                               GetRwyDetails();
+                               
+                               // Get the takeoff node for the active runway, get a path to it and start taxiing
+                               path = ground->GetPathToHoldShort(ourGate, rwy.rwyID);
+                               if(path.size() < 2) {
+                                       // something has gone wrong
+                                       SG_LOG(SG_ATC, SG_ALERT, "Invalid path from gate to theshold in FGAILocalTraffic::FlyCircuits\n");
+                                       return;
+                               }
+                               /*
+                               cout << "path returned was:\n";
+                               for(unsigned int i=0; i<path.size(); ++i) {
+                                       switch(path[i]->struct_type) {
+                                               case NODE:
+                                               cout << "NODE " << ((node*)(path[i]))->nodeID << endl;
+                                               break;
+                                               case ARC:
+                                               cout << "ARC\n";
+                                               break;
+                                       }
+                               }
+                               */
+                               path.erase(path.begin());       // pop the gate - we're here already!
+                               taxiState = TD_OUTBOUND;
+                               taxiRequestPending = false;
+                               holdShortNode = (node*)(*(path.begin() + path.size()));
+                               StartTaxi();
+                       } else if(!taxiRequestPending) {
+                               //cout << "(" << flush;
+                               ground->RequestDeparture(plane, this);
+                               // Do some communication
+                               // airport name + tower + airplane callsign + location + request taxi for + operation type + ?
+                               string trns = "";
+                               trns += tower->get_name();
+                               trns += " tower ";
+                               trns += plane.callsign;
+                               trns += " on apron parking request taxi for traffic pattern";
+                               //cout << "trns = " << trns << endl;
+                               Transmit(trns);
+                               taxiRequestCleared = false;
+                               taxiRequestPending = true;
+                       }
+               }
+               
+               //cout << "!" << flush;
+                               
+               // Maybe the below should be set when we get to the threshold and prepare for TO?
+               // FIXME TODO - pattern direction is still hardwired
+               patternDirection = -1;          // Left
+               // At the bare minimum we ought to make sure it goes the right way at dual parallel rwy airports!
+               if(rwy.rwyID.size() == 3) {
+                       patternDirection = (rwy.rwyID.substr(2,1) == "R" ? 1 : -1);
+               }               
                // Do nothing
+               Transform();
+               //cout << ")" << flush;
+               break;
+       default:
+               break;
+       }
+       //cout << "I " << flush;
+}
+
+void FGAILocalTraffic::RegisterTransmission(int code) {
+       switch(code) {
+       case 1: // taxi request cleared
+               taxiRequestCleared = true;
+               SG_LOG(SG_ATC, SG_INFO, "AI local traffic " << plane.callsign << " cleared to taxi...");
+               break;
+       case 2: // contact tower
+               responseCounter = 0;
+               contactTower = true;
+               SG_LOG(SG_ATC, SG_INFO, "AI local traffic " << plane.callsign << " told to contact tower...");
+               break;
+       case 3: // Cleared to line up
+               responseCounter = 0;
+               clearedToLineUp = true;
+               SG_LOG(SG_ATC, SG_INFO, "AI local traffic " << plane.callsign << " cleared to line-up...");
+               break;
+       case 4: // cleared to take-off
+               responseCounter = 0;
+               clearedToTakeOff = true;
+               SG_LOG(SG_ATC, SG_INFO, "AI local traffic " << plane.callsign << " cleared to take-off...");
                break;
        default:
                break;
        }
-       //cout << "elev in FGAILocalTraffic = " << aip.getFGLocation()->get_cur_elev_m() << '\n';
-       // This should become if(the plane has moved) then Transform()
 }
 
 // Fly a traffic pattern
@@ -197,7 +578,6 @@ void FGAILocalTraffic::Update(double dt) {
 void FGAILocalTraffic::FlyTrafficPattern(double dt) {
        // Need to differentiate between in-air (IAS governed) and on-ground (vel governed)
        // Take-off is an interesting case - we are on the ground but takeoff speed is IAS governed.
-       bool inAir = true;      // FIXME - possibly make into a class variable
        
        static bool transmitted = false;        // FIXME - this is a hack
 
@@ -227,23 +607,28 @@ void FGAILocalTraffic::FlyTrafficPattern(double dt) {
 
        switch(leg) {
        case TAKEOFF_ROLL:
-               inAir = false;
+               //inAir = false;
                track = rwy.hdg;
                if(vel < 80.0) {
                        double dveldt = 5.0;
                        vel += dveldt * dt;
                }
+               if(aip.getSGLocation()->get_cur_elev_m() > -9990.0) {
+                       pos.setelev(aip.getSGLocation()->get_cur_elev_m() + wheelOffset);
+               }
                IAS = vel + (cos((hdg - wind_from) * DCL_DEGREES_TO_RADIANS) * wind_speed);
                if(IAS >= 70) {
                        leg = CLIMBOUT;
                        pitch = 10.0;
                        IAS = best_rate_of_climb_speed;
                        slope = 7.0;
+                       inAir = true;
                }
                break;
        case CLIMBOUT:
                track = rwy.hdg;
                if((pos.elev() - rwy.threshold_pos.elev()) * SG_METER_TO_FEET > 600) {
+                       cout << "Turning to crosswind, distance from threshold = " << orthopos.y() << '\n'; 
                        leg = TURN1;
                }
                break;
@@ -353,15 +738,26 @@ void FGAILocalTraffic::FlyTrafficPattern(double dt) {
                // Try and track the extended centreline
                track = rwy.hdg - (0.2 * orthopos.x());
                //cout << "orthopos.x() = " << orthopos.x() << " hdg = " << hdg << '\n';
-               if(pos.elev() <= rwy.threshold_pos.elev()) {
-                       pos.setelev(rwy.threshold_pos.elev());// + (-8.5 * SG_FEET_TO_METER));  // This is a complete hack - the rendered runway takes the underlying scenery elev rather than the published runway elev so I should use height above terrain or something.
-                       slope = 0.0;
-                       pitch = 0.0;
-                       leg = LANDING_ROLL;
+               if(pos.elev() < (rwy.threshold_pos.elev()+20.0+wheelOffset)) {
+                       DoGroundElev(); // Need to call it here expicitly on final since it's only called
+                                       // for us in update(...) when the inAir flag is false.
+               }
+               if(pos.elev() < (rwy.threshold_pos.elev()+10.0+wheelOffset)) {
+                       if(aip.getSGLocation()->get_cur_elev_m() > -9990.0) {
+                               if((aip.getSGLocation()->get_cur_elev_m() + wheelOffset) > pos.elev()) {
+                                       slope = 0.0;
+                                       pitch = 0.0;
+                                       leg = LANDING_ROLL;
+                                       inAir = false;
+                               }
+                       }       // else need a fallback position based on arpt elev in case ground elev determination fails?
                }
                break;
        case LANDING_ROLL:
-               inAir = false;
+               //inAir = false;
+               if(aip.getSGLocation()->get_cur_elev_m() > -9990.0) {
+                       pos.setelev(aip.getSGLocation()->get_cur_elev_m() + wheelOffset);
+               }
                track = rwy.hdg;
                double dveldt = -5.0;
                vel += dveldt * dt;
@@ -434,12 +830,11 @@ void FGAILocalTraffic::TransmitPatternPositionReport(void) {
        
        trns += tower->get_name();
        trns += " Traffic ";
-       // FIXME - add the callsign to the class variables
-       trns += "Trainer-two-five-charlie ";
+       trns += plane.callsign;
        if(patternDirection == 1) {
-               trns += "right ";
+               trns += " right ";
        } else {
-               trns += "left ";
+               trns += " left ";
        }
        
        // We could probably get rid of this whole switch statement and just pass a string containing the leg from the FlyPattern function.
@@ -480,7 +875,7 @@ void FGAILocalTraffic::TransmitPatternPositionReport(void) {
 void FGAILocalTraffic::ExitRunway(Point3D orthopos) {
        //cout << "In ExitRunway" << endl;
        //cout << "Runway ID is " << rwy.ID << endl;
-       node_array_type exitNodes = airport.GetExits(rwy.ID);   //I suppose we ought to have some fallback for rwy with no defined exits?
+       node_array_type exitNodes = ground->GetExits(rwy.rwyID);        //I suppose we ought to have some fallback for rwy with no defined exits?
        /*
        cout << "Node ID's of exits are ";
        for(unsigned int i=0; i<exitNodes.size(); ++i) {
@@ -511,12 +906,18 @@ void FGAILocalTraffic::ExitRunway(Point3D orthopos) {
                        }
                        ++nItr;
                }
-               in_dest = airport.GetGateNode();
-               // TODO - add a NULL pointer (no available gates) check for in_dest and have a fallback position
-               // (possibly taxi off runway and disappear?)
-               path = airport.GetPath(rwyExit, in_dest);
-               //cout << "path returned was:" << endl;
+               ourGate = ground->GetGateNode();
+               if(ourGate == NULL) {
+                       // Implies no available gates - what shall we do?
+                       // For now just vanish the plane - possibly we can make this more elegant in the future
+                       SG_LOG(SG_ATC, SG_ALERT, "No gate found by FGAILocalTraffic whilst landing at " << airportID << '\n');
+                       aip.setVisible(false);
+                       operatingState = PARKED;
+                       return;
+               }
+               path = ground->GetPath(rwyExit, ourGate);
                /*
+               cout << "path returned was:" << endl;
                for(unsigned int i=0; i<path.size(); ++i) {
                        switch(path[i]->struct_type) {
                        case NODE:
@@ -532,7 +933,7 @@ void FGAILocalTraffic::ExitRunway(Point3D orthopos) {
                StartTaxi();
        } else {
                // Something must have gone wrong with the ground network file - or there is only a rwy here and no exits defined
-               SG_LOG(SG_GENERAL, SG_ALERT, "No exits found by FGAILocalTraffic from runway " << rwy.ID << " at " << airportID << '\n');
+               SG_LOG(SG_ATC, SG_ALERT, "No exits found by FGAILocalTraffic from runway " << rwy.rwyID << " at " << airportID << '\n');
                // What shall we do - just remove the plane from sight?
                aip.setVisible(false);
                operatingState = PARKED;
@@ -547,7 +948,7 @@ void FGAILocalTraffic::GetNextTaxiNode() {
        //cout << "taxiPathPos = " << taxiPathPos << endl;
        ground_network_path_iterator pathItr = path.begin() + taxiPathPos;
        if(pathItr == path.end()) {
-               SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN AILocalTraffic::GetNextTaxiNode - no more nodes in path\n");
+               SG_LOG(SG_ATC, SG_ALERT, "ERROR IN AILocalTraffic::GetNextTaxiNode - no more nodes in path\n");
        } else {
                if((*pathItr)->struct_type == NODE) {
                        //cout << "ITS A NODE" << endl;
@@ -562,13 +963,13 @@ void FGAILocalTraffic::GetNextTaxiNode() {
                        pathItr++;
                        taxiPathPos++;
                        if(pathItr == path.end()) {
-                               SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN AILocalTraffic::GetNextTaxiNode - path ended with an arc\n");
+                               SG_LOG(SG_ATC, SG_ALERT, "ERROR IN AILocalTraffic::GetNextTaxiNode - path ended with an arc\n");
                        } else if((*pathItr)->struct_type == NODE) {
                                nextTaxiNode = (node*)*pathItr;
                                ++taxiPathPos;
                        } else {
                                //OOPS - two non-nodes in a row - that shouldn't happen ATM
-                               SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN AILocalTraffic::GetNextTaxiNode - two non-nodes in sequence\n");
+                               SG_LOG(SG_ATC, SG_ALERT, "ERROR IN AILocalTraffic::GetNextTaxiNode - two non-nodes in sequence\n");
                        }
                }
        }
@@ -589,14 +990,53 @@ void FGAILocalTraffic::StartTaxi() {
        //cout << "First taxi heading is " << desiredTaxiHeading << endl;
 }
 
+// speed in knots, headings in degrees, radius in meters.
+static double TaxiTurnTowardsHeading(double current_hdg, double desired_hdg, double speed, double radius, double dt) {
+       // wrap heading - this prevents a logic bug where the plane would just go round in circles!!
+       while(current_hdg < 0.0) {
+               current_hdg += 360.0;
+       }
+       while(current_hdg > 360.0) {
+               current_hdg -= 360.0;
+       }
+       if(fabs(current_hdg - desired_hdg) > 0.1) {
+               // Which is the quickest direction to turn onto heading?
+               if(desired_hdg > current_hdg) {
+                       if((desired_hdg - current_hdg) <= 180) {
+                               // turn right
+                               current_hdg += ((speed * 0.514444 * dt) / (radius * DCL_PI)) * 180.0;
+                               // TODO - check that increments are less than the delta that we check for the right direction
+                               // Probably need to reduce convergence speed as convergence is reached
+                       } else {
+                               current_hdg -= ((speed * 0.514444 * dt) / (radius * DCL_PI)) * 180.0;   
+                       }
+               } else {
+                       if((current_hdg - desired_hdg) <= 180) {
+                               // turn left
+                               current_hdg -= ((speed * 0.514444 * dt) / (radius * DCL_PI)) * 180.0;
+                               // TODO - check that increments are less than the delta that we check for the right direction
+                               // Probably need to reduce convergence speed as convergence is reached
+                       } else {
+                               current_hdg += ((speed * 0.514444 * dt) / (radius * DCL_PI)) * 180.0;   
+                       }
+               }               
+       }
+       return(current_hdg);
+}
+
 void FGAILocalTraffic::Taxi(double dt) {
        //cout << "Taxi called" << endl;
        // Logic - if we are further away from next point than turn radius then head for it
        // If we have reached turning point then get next point and turn onto that heading
        // Look out for the finish!!
 
-       Point3D orthopos = ortho.ConvertToLocal(pos);   // ortho position of the plane
+       //Point3D orthopos = ortho.ConvertToLocal(pos); // ortho position of the plane
        desiredTaxiHeading = GetHeadingFromTo(pos, nextTaxiNode->pos);
+       
+       bool lastNode = (taxiPathPos == path.size() ? true : false);
+       if(lastNode) {
+               //cout << "LAST NODE\n";
+       }
 
        // HACK ALERT! - for now we will taxi at constant speed for straights and turns
        
@@ -604,37 +1044,16 @@ void FGAILocalTraffic::Taxi(double dt) {
        double dist_to_go = dclGetHorizontalSeparation(pos, nextTaxiNode->pos); // we may be able to do this more cheaply using orthopos
        //cout << "dist_to_go = " << dist_to_go << endl;
        if((nextTaxiNode->type == GATE) && (dist_to_go <= 0.1)) {
+               // This might be more robust to outward paths starting with a gate if we check for either
+               // last node or TD_INBOUND ?
                // park up
-               //taxiing = false;
-               //parked = true;
                operatingState = PARKED;
-       } else if((dist_to_go > taxiTurnRadius) || (nextTaxiNode->type == GATE)) {
+       } else if(((dist_to_go > taxiTurnRadius) || (nextTaxiNode->type == GATE)) && (!liningUp)){
                // if the turn radius is r, and speed is s, then in a time dt we turn through
                // ((s.dt)/(PI.r)) x 180 degrees
                // or alternatively (s.dt)/r radians
                //cout << "hdg = " << hdg << " desired taxi heading = " << desiredTaxiHeading << '\n';
-               if(fabs(hdg - desiredTaxiHeading) > 0.1) {
-                       // Which is the quickest direction to turn onto heading?
-                       if(desiredTaxiHeading > hdg) {
-                               if((desiredTaxiHeading - hdg) <= 180) {
-                                       // turn right
-                                       hdg += ((nominalTaxiSpeed * 0.514444 * dt) / (taxiTurnRadius * DCL_PI)) * 180.0;
-                                       // TODO - check that increments are less than the delta that we check for the right direction
-                                       // Probably need to reduce convergence speed as convergence is reached
-                               } else {
-                                       hdg -= ((nominalTaxiSpeed * 0.514444 * dt) / (taxiTurnRadius * DCL_PI)) * 180.0;        
-                               }
-                       } else {
-                               if((hdg - desiredTaxiHeading) <= 180) {
-                                       // turn left
-                                       hdg -= ((nominalTaxiSpeed * 0.514444 * dt) / (taxiTurnRadius * DCL_PI)) * 180.0;
-                                       // TODO - check that increments are less than the delta that we check for the right direction
-                                       // Probably need to reduce convergence speed as convergence is reached
-                               } else {
-                                       hdg += ((nominalTaxiSpeed * 0.514444 * dt) / (taxiTurnRadius * DCL_PI)) * 180.0;        
-                               }
-                       }               
-               }
+               hdg = TaxiTurnTowardsHeading(hdg, desiredTaxiHeading, nominalTaxiSpeed, taxiTurnRadius, dt);
                double vel = nominalTaxiSpeed;
                //cout << "vel = " << vel << endl;
                double dist = vel * 0.514444 * dt;
@@ -644,9 +1063,40 @@ void FGAILocalTraffic::Taxi(double dt) {
                double slope = 0.0;
                pos = dclUpdatePosition(pos, track, slope, dist);
                //cout << "Updated position...\n";
-               // FIXME - HACK in absense of proper ground elevation determination
-               // Linearly interpolate altitude when taxiing between N and S extremes of orthopos
-               pos.setelev((287.5 + ((299.3 - 287.5) * fabs(orthopos.y() / 1000.0))) * SG_FEET_TO_METER);
+               if(aip.getSGLocation()->get_cur_elev_m() > -9990) {
+                       pos.setelev(aip.getSGLocation()->get_cur_elev_m() + wheelOffset);
+               } // else don't change the elev until we get a valid ground elev again!
+       } else if(lastNode) {
+               if(taxiState == TD_LINING_UP) {
+                       if((!liningUp) && (dist_to_go <= taxiTurnRadius)) {
+                               liningUp = true;
+                       }
+                       if(liningUp) {
+                               hdg = TaxiTurnTowardsHeading(hdg, rwy.hdg, nominalTaxiSpeed, taxiTurnRadius, dt);
+                               double vel = nominalTaxiSpeed;
+                               //cout << "vel = " << vel << endl;
+                               double dist = vel * 0.514444 * dt;
+                               //cout << "dist = " << dist << endl;
+                               double track = hdg;
+                               //cout << "track = " << track << endl;
+                               double slope = 0.0;
+                               pos = dclUpdatePosition(pos, track, slope, dist);
+                               //cout << "Updated position...\n";
+                               if(aip.getSGLocation()->get_cur_elev_m() > -9990) {
+                                       pos.setelev(aip.getSGLocation()->get_cur_elev_m() + wheelOffset);
+                               } // else don't change the elev until we get a valid ground elev again!
+                               if(fabs(hdg - rwy.hdg) <= 1.0) {
+                                       operatingState = IN_PATTERN;
+                                       leg = TAKEOFF_ROLL;
+                                       inAir = false;
+                                       liningUp = false;
+                               }
+                       }
+               } else if(taxiState == TD_OUTBOUND) {
+                       // Pause awaiting further instructions
+                       // and for now assume we've reached the hold-short node
+                       holdingShort = true;
+               } // else at the moment assume TD_INBOUND always ends in a gate in which case we can ignore it
        } else {
                // Time to turn (we've already checked it's not the end we're heading for).
                // set the target node to be the next node which will prompt automatically turning onto
@@ -657,3 +1107,35 @@ void FGAILocalTraffic::Taxi(double dt) {
        }
 }
 
+
+// Warning - ground elev determination is CPU intensive
+// Either this function or the logic of how often it is called
+// will almost certainly change.
+void FGAILocalTraffic::DoGroundElev() {
+
+       // It would be nice if we could set the correct tile center here in order to get a correct
+       // answer with one call to the function, but what I tried in the two commented-out lines
+       // below only intermittently worked, and I haven't quite groked why yet.
+       //SGBucket buck(pos.lon(), pos.lat());
+       //aip.getSGLocation()->set_tile_center(Point3D(buck.get_center_lon(), buck.get_center_lat(), 0.0));
+       
+       double visibility_meters = fgGetDouble("/environment/visibility-m");
+       //globals->get_tile_mgr()->prep_ssg_nodes( acmodel_location,
+       globals->get_tile_mgr()->prep_ssg_nodes( aip.getSGLocation(),   visibility_meters );
+        Point3D scenery_center = globals->get_scenery()->get_center();
+       globals->get_tile_mgr()->update( aip.getSGLocation(), visibility_meters, (aip.getSGLocation())->get_absolute_view_pos( scenery_center ) );
+       // save results of update in SGLocation for fdm...
+       
+       //if ( globals->get_scenery()->get_cur_elev() > -9990 ) {
+       //      acmodel_location->
+       //      set_cur_elev_m( globals->get_scenery()->get_cur_elev() );
+       //}
+       
+       // The need for this here means that at least 2 consecutive passes are needed :-(
+       aip.getSGLocation()->set_tile_center( globals->get_scenery()->get_next_center() );
+       
+       //cout << "Transform Elev is " << globals->get_scenery()->get_cur_elev() << '\n';
+       aip.getSGLocation()->set_cur_elev_m(globals->get_scenery()->get_cur_elev());
+       //return(globals->get_scenery()->get_cur_elev());
+}
+