1
0
Fork 0

Merging David Luff's AI/ATC code with AIModels. Part 1:

- Move Dave's original code to a new directory (ATCDCL) so we can recycle
    the original ATC directory for generic ATC functions.
This commit is contained in:
durk 2008-05-12 10:38:04 +00:00
parent aacebaf4b8
commit 4e24fba0d5
46 changed files with 12 additions and 13308 deletions

View file

@ -649,7 +649,7 @@ AC_CONFIG_FILES([ \
src/Include/config.h-msvc8 \
src/Aircraft/Makefile \
src/Airports/Makefile \
src/ATC/Makefile \
src/ATCDCL/Makefile \
src/Autopilot/Makefile \
src/Cockpit/Makefile \
src/Cockpit/built_in/Makefile \

View file

@ -1,79 +0,0 @@
// FGAIEntity - abstract base class an artificial intelligence entity
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/*****************************************************************
*
* WARNING - Curt has some ideas about AI traffic so anything in here
* may get rewritten or scrapped. Contact Curt http://www.flightgear.org/~curt
* before spending any time or effort on this code!!!
*
******************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <Main/globals.hxx>
#include <Scenery/scenery.hxx>
#include <simgear/constants.h>
#include <simgear/math/point3d.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/misc/sg_path.hxx>
#include <string>
#include "AIEntity.hxx"
FGAIEntity::FGAIEntity() {
}
FGAIEntity::~FGAIEntity() {
//cout << "FGAIEntity dtor called..." << endl;
//cout << "Removing model from scene graph..." << endl;
globals->get_scenery()->get_scene_graph()->removeChild(_aip.getSceneGraph());
//cout << "Done!" << endl;
}
void FGAIEntity::SetModel(osg::Node* model) {
_model = model;
_aip.init(_model.get());
_aip.setVisible(false);
globals->get_scenery()->get_scene_graph()->addChild(_aip.getSceneGraph());
}
void FGAIEntity::Update(double dt) {
}
const string &FGAIEntity::GetCallsign() {
static string s = "";
return(s);
}
void FGAIEntity::RegisterTransmission(int code) {
}
// Run the internal calculations
//void FGAIEntity::Update() {
void FGAIEntity::Transform() {
_aip.setPosition(_pos.lon(), _pos.lat(), _pos.elev() * SG_METER_TO_FEET);
_aip.setOrientation(_roll, _pitch, _hdg);
_aip.update();
}

View file

@ -1,72 +0,0 @@
// FGAIEntity - abstract base class an artificial intelligence entity
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_AIEntity_HXX
#define _FG_AIEntity_HXX
#include <simgear/math/point3d.hxx>
#include <simgear/scene/model/placement.hxx>
/*****************************************************************
*
* FGAIEntity - this class implements the minimum requirement
* for any AI entity - a position, an orientation, an associated
* 3D model, and the ability to be moved. It does nothing useful
* and all AI entities are expected to be derived from it.
*
******************************************************************/
class FGAIEntity {
public:
FGAIEntity();
virtual ~FGAIEntity();
// Set the 3D model to use (Must be called)
void SetModel(osg::Node* model);
// Run the internal calculations
virtual void Update(double dt)=0;
// Send a transmission *TO* the AIEntity.
// FIXME int code is a hack - eventually this will receive Alexander's coded messages.
virtual void RegisterTransmission(int code)=0;
inline const Point3D& GetPos() const { return(_pos); }
virtual const string& GetCallsign()=0;
protected:
Point3D _pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
double _hdg; //True heading in degrees
double _roll; //degrees
double _pitch; //degrees
char* _model_path; //Path to the 3D model
osg::ref_ptr<osg::Node> _model; // Pointer to the model
SGModelPlacement _aip;
void Transform();
};
#endif // _FG_AIEntity_HXX

View file

@ -1,463 +0,0 @@
// FGAILocalTraffic - AIEntity derived class with enough logic to
// fly and interact with the traffic pattern.
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <Airports/runways.hxx>
#include <Main/globals.hxx>
#include <simgear/math/point3d.hxx>
#include <string>
#include <math.h>
SG_USING_STD(string);
#include "ATC.hxx"
#include "ATCmgr.hxx"
#include "AILocalTraffic.hxx"
#include "AIGAVFRTraffic.hxx"
#include "ATCutils.hxx"
#include "tower.hxx"
FGAIGAVFRTraffic::FGAIGAVFRTraffic() {
ATC = globals->get_ATC_mgr();
_towerContactedIncoming = false;
_clearedStraightIn = false;
_clearedDownwindEntry = false;
_incoming = false;
_straightIn = false;
_downwindEntry = false;
_climbout = false;
_local = false;
_established = false;
_e45 = false;
_entering = false;
_turning = false;
_cruise_climb_ias = 90.0;
_cruise_ias = 110.0;
patternDirection = -1.0;
// TESTING - REMOVE OR COMMENT OUT BEFORE COMMIT!!!
//_towerContactPrinted = false;
}
FGAIGAVFRTraffic::~FGAIGAVFRTraffic() {
}
// We should never need to Init FGAIGAVFRTraffic in the pattern since that implies arrivel
// and we can just use an FGAILocalTraffic instance for that instead.
// Init en-route to destID at point pt.
// TODO - no idea what to do if pt is above planes ceiling due mountains!!
bool FGAIGAVFRTraffic::Init(const Point3D& pt, const string& destID, const string& callsign) {
FGAILocalTraffic::Init(callsign, destID, EN_ROUTE);
// TODO FIXME - to get up and running we're going to ignore elev and get FGAIMgr to
// pass in known good values for the test location. Need to fix this!!! (or at least canonically decide who has responsibility for setting elev).
_enroute = true;
_destID = destID;
_pos = pt;
_destPos = fgGetAirportPos(destID); // TODO - check if we are within the tower catchment area already.
_cruise_alt = (_destPos.elev() + 2500.0) * SG_FEET_TO_METER; // TODO look at terrain elevation as well
_pos.setelev(_cruise_alt);
// initially set waypoint as airport location
_wp = _destPos;
// Set the initial track
track = GetHeadingFromTo(_pos, _wp);
// And set the plane to keep following it.
SetTrack(GetHeadingFromTo(_pos, _wp));
_roll = 0.0;
_pitch = 0.0;
slope = 0.0;
// TODO - set climbout if altitude is below normal cruising altitude?
//Transform();
// Assume it's OK to set the plane visible
_aip.setVisible(true);
//cout << "Setting visible true\n";
Transform();
return(true);
}
// Init at srcID to fly to destID
bool FGAIGAVFRTraffic::Init(const string& srcID, const string& destID, const string& callsign, OperatingState state) {
_enroute = false;
FGAILocalTraffic::Init(callsign, srcID, PARKED);
return(true);
}
void FGAIGAVFRTraffic::Update(double dt) {
if(_enroute) {
//cout << "_enroute\n";
//cout << "e" << flush;
FlyPlane(dt);
//cout << "f" << flush;
Transform();
//cout << "g" << flush;
FGAIPlane::Update(dt);
//cout << "h" << flush;
responseCounter += dt;
// we shouldn't really need this since there's a LOD of 10K on the whole plane anyway I think.
// There are two _aip.setVisible statements set when _local = true that can be removed if the below is removed.
if(dclGetHorizontalSeparation(_pos, Point3D(fgGetDouble("/position/longitude-deg"), fgGetDouble("/position/latitude-deg"), 0.0)) > 8000) _aip.setVisible(false);
else _aip.setVisible(true);
} else if(_local) {
//cout << "L";
//cout << "_local\n";
FGAILocalTraffic::Update(dt);
}
}
void FGAIGAVFRTraffic::FlyPlane(double dt) {
if(_climbout) {
// Check whether to level off
if(_pos.elev() >= _cruise_alt) {
slope = 0.0;
_pitch = 0.0;
IAS = _cruise_ias; // FIXME - use smooth transistion to new speed and attitude.
_climbout = false;
} else {
slope = 4.0;
_pitch = 5.0;
IAS = _cruise_climb_ias;
}
} else {
// TESTING
/*
if(dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 < 8.1) {
if(!_towerContactPrinted) {
if(airportID == "KSQL") {
cout << "****************************************************************\n";
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
cout << "****************************************************************\n";
}
_towerContactPrinted = true;
}
}
*/
// if distance to destination is less than 6 - 9 miles contact tower
// and prepare to become _incoming after response.
// Possibly check whether to start descent before this?
//cout << "." << flush;
//cout << "sep = " << dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 << '\n';
if(dclGetHorizontalSeparation(_destPos, _pos) / 1600.0 < 8.0) {
//cout << "-" << flush;
if(!_towerContactedIncoming) {
//cout << "_" << flush;
GetAirportDetails(airportID);
//cout << "L" << flush;
if(_controlled) {
freq = (double)tower->get_freq() / 100.0;
tuned_station = tower;
} else {
freq = 122.8; // TODO - need to get the correct CTAF/Unicom frequency if no tower
tuned_station = NULL;
}
//cout << "freq = " << freq << endl;
GetRwyDetails(airportID);
//"@AP Tower @CS @MI miles @CD of the airport for full stop with ATIS"
// 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);
}
if(_controlled) {
pending_transmission = tower->get_name();
pending_transmission += " Tower ";
} else {
pending_transmission = "Traffic ";
// TODO - find some way of getting uncontrolled airport name
}
pending_transmission += plane.callsign;
//char buf[10];
int dist_miles = (int)dclGetHorizontalSeparation(_pos, _destPos) / 1600;
//sprintf(buf, " %i ", dist_miles);
pending_transmission += " ";
pending_transmission += ConvertNumToSpokenDigits(dist_miles);
if(dist_miles > 1) pending_transmission += " miles ";
else pending_transmission += " mile ";
pending_transmission += GetCompassDirection(GetHeadingFromTo(_destPos, _pos));
pending_transmission += " of the airport for full stop with ATIS";
//cout << pending_transmission << endl;
Transmit(14); // 14 is the callback code, NOT the timeout!
responseCounter = 0;
_towerContactedIncoming = true;
} else {
//cout << "?" << flush;
if(_clearedStraightIn && responseCounter > 5.5) {
//cout << "5 " << flush;
_clearedStraightIn = false;
_straightIn = true;
_incoming = true;
_wp = GetPatternApproachPos();
//_hdg = GetHeadingFromTo(_pos, _wp); // TODO - turn properly!
SetTrack(GetHeadingFromTo(_pos, _wp));
slope = atan((_wp.elev() - _pos.elev()) / dclGetHorizontalSeparation(_wp, _pos)) * DCL_RADIANS_TO_DEGREES;
double thesh_offset = 0.0;
Point3D opos = ortho.ConvertToLocal(_pos);
double angToApt = atan((_pos.elev() - fgGetAirportElev(airportID)) / (opos.y() - thesh_offset)) * DCL_RADIANS_TO_DEGREES;
//cout << "angToApt = " << angToApt << ' ';
slope = (angToApt > -5.0 ? 0.0 : angToApt);
//cout << "slope = " << slope << '\n';
pending_transmission = "Straight-in ";
pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
pending_transmission += " ";
pending_transmission += plane.callsign;
//cout << pending_transmission << '\n';
ConditionalTransmit(4);
} else if(_clearedDownwindEntry && responseCounter > 5.5) {
//cout << "6" << flush;
_clearedDownwindEntry = false;
_downwindEntry = true;
_incoming = true;
_wp = GetPatternApproachPos();
SetTrack(GetHeadingFromTo(_pos, _wp));
slope = atan((_wp.elev() - _pos.elev()) / dclGetHorizontalSeparation(_wp, _pos)) * DCL_RADIANS_TO_DEGREES;
//cout << "slope = " << slope << '\n';
pending_transmission = "Report ";
pending_transmission += (patternDirection == 1 ? "right downwind " : "left downwind ");
pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
pending_transmission += " ";
pending_transmission += plane.callsign;
//cout << pending_transmission << '\n';
ConditionalTransmit(4);
}
}
if(_pos.elev() < (fgGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;
}
}
if(_incoming) {
//cout << "i" << '\n';
Point3D orthopos = ortho.ConvertToLocal(_pos);
// TODO - Check whether to start descent
// become _local after the 3 mile report.
if(_pos.elev() < (fgGetAirportElev(airportID) + (1000.0 * SG_FEET_TO_METER))) slope = 0.0;
// TODO - work out why I needed to add the above line to stop the plane going underground!!!
// (Although it's worth leaving it in as a robustness check anyway).
if(_straightIn) {
//cout << "A " << flush;
if(fabs(orthopos.x()) < 10.0 && !_established) {
SetTrack(rwy.hdg);
_established = true;
//cout << "Established at " << orthopos << '\n';
}
double thesh_offset = 30.0;
//cout << "orthopos.y = " << orthopos.y() << " alt = " << _pos.elev() - fgGetAirportElev(airportID) << '\n';
if(_established && (orthopos.y() > -5400.0)) {
slope = atan((_pos.elev() - fgGetAirportElev(airportID)) / (orthopos.y() - thesh_offset)) * DCL_RADIANS_TO_DEGREES;
//cout << "slope0 = " << slope << '\n';
}
//cout << "slope1 = " << slope << '\n';
if(slope > -5.5) slope = 0.0; // ie we're too low.
//cout << "slope2 = " << slope << '\n';
slope += 0.001; // To avoid yo-yoing with the above.
//if(_established && (orthopos.y() > -5400.0)) slope = -5.5;
if(_established && (orthopos.y() > -4800.0)) {
pending_transmission = "3 mile final Runway ";
pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
pending_transmission += " ";
pending_transmission += plane.callsign;
//cout << pending_transmission << '\n';
ConditionalTransmit(35);
_local = true;
_aip.setVisible(true); // HACK
_enroute = false;
StraightInEntry(true);
}
} else if(_downwindEntry) {
//cout << "B" << flush;
if(_entering) {
//cout << "C" << flush;
if(_turning) {
if(fabs(_hdg - (rwy.hdg + 180)) < 2.0) { // TODO - use track instead of _hdg?
//cout << "Going Local...\n";
leg = DOWNWIND;
_local = true;
_aip.setVisible(true); // HACK
_enroute = false;
_entering = false;
_turning = false;
DownwindEntry();
}
}
if(fabs(orthopos.x() - (patternDirection == 1 ? 1000 : -1000)) < (_e45 ? 175 : 550)) { // Caution - hardwired turn clearances.
//cout << "_turning...\n";
_turning = true;
SetTrack(rwy.hdg + 180.0);
} // TODO - need to check for other traffic in the pattern and enter much more integilently than that!!!
} else {
//cout << "D" << flush;
//cout << '\n' << dclGetHorizontalSeparation(_wp, _pos) << '\n';
//cout << ortho.ConvertToLocal(_pos);
//cout << ortho.ConvertToLocal(_wp);
if(dclGetHorizontalSeparation(_wp, _pos) < 100.0) {
pending_transmission = "2 miles out for ";
pending_transmission += (patternDirection == 1 ? "right " : "left ");
pending_transmission += "downwind Runway ";
pending_transmission += ConvertRwyNumToSpokenString(rwy.rwyID);
pending_transmission += " ";
pending_transmission += plane.callsign;
//cout << pending_transmission << '\n';
// TODO - are we at pattern altitude??
slope = 0.0;
ConditionalTransmit(30);
if(_e45) {
SetTrack(patternDirection == 1 ? rwy.hdg - 135.0 : rwy.hdg + 135.0);
} else {
SetTrack(patternDirection == 1 ? rwy.hdg + 90.0 : rwy.hdg - 90.0);
}
//if(_hdg < 0.0) _hdg += 360.0;
_entering = true;
} else {
SetTrack(GetHeadingFromTo(_pos, _wp));
}
}
}
} else {
// !_incoming
slope = 0.0;
}
// FIXME - lots of hackery in the next six lines!!!!
//double track = _hdg;
double crab = 0.0; // This is a placeholder for when we take wind into account.
_hdg = track + crab;
double vel = _cruise_ias;
double dist = vel * 0.514444 * dt;
_pos = dclUpdatePosition(_pos, track, slope, dist);
}
void FGAIGAVFRTraffic::RegisterTransmission(int code) {
switch(code) {
case 1: // taxi request cleared
FGAILocalTraffic::RegisterTransmission(code);
break;
case 2: // contact tower
FGAILocalTraffic::RegisterTransmission(code);
break;
case 3: // Cleared to line up
FGAILocalTraffic::RegisterTransmission(code);
break;
case 4: // cleared to take-off
FGAILocalTraffic::RegisterTransmission(code);
break;
case 5: // contact ground
FGAILocalTraffic::RegisterTransmission(code);
break;
case 6: // taxi to the GA parking
FGAILocalTraffic::RegisterTransmission(code);
break;
case 7: // Cleared to land
FGAILocalTraffic::RegisterTransmission(code);
break;
case 13: // Go around!
FGAILocalTraffic::RegisterTransmission(code);
break;
case 14: // VFR approach for straight-in
responseCounter = 0;
_clearedStraightIn = true;
break;
case 15: // VFR approach for downwind entry
responseCounter = 0;
_clearedDownwindEntry = true;
break;
default:
SG_LOG(SG_ATC, SG_WARN, "FGAIGAVFRTraffic::RegisterTransmission(...) called with unknown code " << code);
FGAILocalTraffic::RegisterTransmission(code);
break;
}
}
// Callback handler
// TODO - Really should enumerate these coded values.
void FGAIGAVFRTraffic::ProcessCallback(int code) {
// 1 - Request Departure from ground
// 2 - Report at hold short
// 10 - report crosswind
// 11 - report downwind
// 12 - report base
// 13 - report final
// 14 - Contact Tower for VFR arrival
// 99 - Remove self
if(code < 14) {
FGAILocalTraffic::ProcessCallback(code);
} else if(code == 14) {
if(_controlled) {
tower->VFRArrivalContact(plane, this, FULL_STOP);
}
// TODO else possibly announce arrival intentions at uncontrolled airport?
} else if(code == 99) {
// Might handle this different in future - hence separated from the other codes to pass to AILocalTraffic.
FGAILocalTraffic::ProcessCallback(code);
}
}
// Return an appropriate altitude to fly at based on the desired altitude and direction
// whilst respecting the quadrangle rule.
int FGAIGAVFRTraffic::GetQuadrangleAltitude(int dir, int des_alt) {
return(8888);
// TODO - implement me!
}
// Calculates the position needed to set up for either pattern entry or straight in approach.
// Currently returns one of three positions dependent on initial position wrt threshold of active rwy.
// 1/ A few miles out on extended centreline for straight-in.
// 2/ At an appropriate point on circuit side of rwy for a 45deg entry to downwind.
// 3/ At and appropriate point on non-circuit side of rwy at take-off end for perpendicular entry to circuit overflying end-of-rwy.
Point3D FGAIGAVFRTraffic::GetPatternApproachPos() {
//cout << "\n\n";
//cout << "Calculating pattern approach pos for " << plane.callsign << '\n';
Point3D orthopos = ortho.ConvertToLocal(_pos);
Point3D tmp;
//cout << "patternDirection = " << patternDirection << '\n';
if(orthopos.y() >= -1000.0) { // Note that this has to be set the same as the calculation in tower.cxx - at the moment approach type is not transmitted properly between the two.
//cout << "orthopos.x = " << orthopos.x() << '\n';
if((orthopos.x() * patternDirection) > 0.0) { // 45 deg entry
tmp.setx(2000 * patternDirection);
tmp.sety((rwy.end2ortho.y() / 2.0) + 2000);
tmp.setelev(fgGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
_e45 = true;
//cout << "45 deg entry... ";
} else {
tmp.setx(1000 * patternDirection * -1);
tmp.sety(rwy.end2ortho.y());
tmp.setelev(fgGetAirportElev(airportID) + (1000 * SG_FEET_TO_METER));
_e45 = false;
//cout << "90 deg entry... ";
}
} else {
tmp.setx(0);
tmp.sety(-5400);
tmp.setelev((5400.0 / 6.0) + fgGetAirportElev(airportID) + 10.0);
//cout << "Straight in... ";
}
//cout << "Waypoint is " << tmp << '\n';
//cout << ortho.ConvertFromLocal(tmp) << '\n';
//cout << '\n';
//exit(-1);
return ortho.ConvertFromLocal(tmp);
}
//FGAIGAVFRTraffic::
//FGAIGAVFRTraffic::
//FGAIGAVFRTraffic::

View file

@ -1,126 +0,0 @@
// FGAILocalTraffic - AIEntity derived class with enough logic to
// fly and interact with the traffic pattern.
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_AIGAVFRTraffic_HXX
#define _FG_AIGAVFRTraffic_HXX
#include <simgear/math/point3d.hxx>
#include <Main/fg_props.hxx>
#include "AILocalTraffic.hxx"
#include <string>
SG_USING_STD(string);
class FGAIGAVFRTraffic : public FGAILocalTraffic {
public:
FGAIGAVFRTraffic();
~FGAIGAVFRTraffic();
// Init en-route to destID at point pt. (lat, lon, elev) (elev in meters, lat and lon in degrees).
bool Init(const Point3D& pt, const string& destID, const string& callsign);
// Init at srcID to fly to destID
bool Init(const string& srcID, const string& destID, const string& callsign, OperatingState state = PARKED);
// Run the internal calculations
void Update(double dt);
// Return what type of landing we're doing on this circuit
//LandingType GetLandingOption();
void RegisterTransmission(int code);
// Process callbacks sent by base class
// (These codes are not related to the codes above)
void ProcessCallback(int code);
protected:
// Do what is necessary to land and parkup at home airport
void ReturnToBase(double dt);
//void GetRwyDetails(string id);
private:
FGATCMgr* ATC;
// This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
// High-level stuff
OperatingState operatingState;
bool touchAndGo; //True if circuits should be flown touch and go, false for full stop
// Performance characteristics of the plane in knots and ft/min - some of this might get moved out into FGAIPlane
double best_rate_of_climb_speed;
double best_rate_of_climb;
double nominal_climb_speed;
double nominal_climb_rate;
double nominal_cruise_speed;
double nominal_circuit_speed;
double nominal_descent_rate;
double nominal_approach_speed;
double nominal_final_speed;
double stall_speed_landing_config;
// environment - some of this might get moved into FGAIPlane
SGPropertyNode_ptr wind_from_hdg; //degrees
SGPropertyNode_ptr wind_speed_knots; //knots
atc_type changeFreqType; // the service we need to change to
void CalculateSoD(double base_leg_pos, double downwind_leg_pos, bool pattern_direction);
// GA VFR specific
bool _towerContactedIncoming;
bool _straightIn;
bool _clearedStraightIn;
bool _downwindEntry;
bool _clearedDownwindEntry;
Point3D _wp; // Next waypoint (ie. the one we're currently heading for)
bool _enroute;
string _destID;
bool _climbout;
double _cruise_alt;
double _cruise_ias;
double _cruise_climb_ias;
Point3D _destPos;
bool _local;
bool _incoming;
bool _established;
bool _e45;
bool _entering;
bool _turning;
//ssgBranch* _model;
int GetQuadrangleAltitude(int dir, int des_alt);
Point3D GetPatternApproachPos();
void FlyPlane(double dt);
// HACK for testing - remove or comment out before CVS commit!!!
//bool _towerContactPrinted;
};
#endif // _FG_AILocalTraffic_HXX

File diff suppressed because it is too large Load diff

View file

@ -1,233 +0,0 @@
// FGAILocalTraffic - AIEntity derived class with enough logic to
// fly and interact with the traffic pattern.
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_AILocalTraffic_HXX
#define _FG_AILocalTraffic_HXX
#include <simgear/math/point3d.hxx>
#include <Main/fg_props.hxx>
#include "AIPlane.hxx"
#include "ATCProjection.hxx"
#include "ground.hxx"
class FGGround;
class FGTower;
struct Gate;
#include <string>
SG_USING_STD(string);
enum TaxiState {
TD_INBOUND,
TD_OUTBOUND,
TD_NONE,
TD_LINING_UP
};
enum OperatingState {
IN_PATTERN,
TAXIING,
PARKED,
EN_ROUTE
};
struct StartOfDescent {
PatternLeg leg;
double x; // Runway aligned orthopos
double y; // ditto
};
class FGAILocalTraffic : public FGAIPlane {
public:
// At the moment we expect the expanded short form callsign - eventually we will just want the reg + type.
FGAILocalTraffic();
~FGAILocalTraffic();
// Initialise
bool Init(const string& callsign, const string& ICAO, OperatingState initialState = PARKED, PatternLeg initialLeg = DOWNWIND);
// Run the internal calculations
void Update(double dt);
// Go out and practice circuits
void FlyCircuits(int numCircuits, bool tag);
// Return what type of landing we're doing on this circuit
LandingType GetLandingOption();
// TODO - this will get more complex and moved into the main class
// body eventually since the position approved to taxi to will have
// to be passed.
inline void ApproveTaxiRequest() {taxiRequestCleared = true;}
inline void DenyTaxiRequest() {taxiRequestCleared = false;}
void RegisterTransmission(int code);
// Process callbacks sent by base class
// (These codes are not related to the codes above)
void ProcessCallback(int code);
// This is a hack and will probably go eventually
inline bool AtHoldShort() {return holdingShort;}
protected:
// Attempt to enter the traffic pattern in a reasonably intelligent manner
void EnterTrafficPattern(double dt);
// Set up the internal state to be consistent for a downwind entry.
void DownwindEntry();
// Ditto for straight-in
void StraightInEntry(bool des = false);
// Do what is necessary to land and parkup at home airport
void ReturnToBase(double dt);
// Airport/runway/pattern details
string airportID; // The ICAO code of the airport that we're operating around
double aptElev; // Airport elevation
FGGround* ground; // A pointer to the ground control.
FGTower* tower; // A pointer to the tower control.
bool _controlled; // Set true if we find tower control working for the airport, false otherwise.
RunwayDetails rwy;
double patternDirection; // 1 for right, -1 for left (This is double because we multiply/divide turn rates
// with it to get RH/LH turns - DON'T convert it to int under ANY circumstances!!
double glideAngle; // Assumed to be visual glidepath angle for FGAILocalTraffic - can be found at www.airnav.com
// Its conceivable that patternDirection and glidePath could be moved into the RunwayDetails structure.
// Its possible that this might be moved out to the ground/airport class at some point.
FGATCAlignedProjection ortho; // Orthogonal mapping of the local area with the threshold at the origin
// and the runway aligned with the y axis.
void GetAirportDetails(const string& id);
void GetRwyDetails(const string& id);
double responseCounter; // timer in seconds to allow response to requests to be a little while after them
// Will almost certainly get moved to FGAIPlane.
private:
FGATCMgr* ATC;
// This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
// High-level stuff
OperatingState operatingState;
int circuitsToFly; //Number of circuits still to do in this session NOT INCLUDING THE CURRENT ONE
bool touchAndGo; //True if circuits should be flown touch and go, false for full stop
bool transmitted; // Set true when a position report for the current leg has been transmitted.
// Performance characteristics of the plane in knots and ft/min - some of this might get moved out into FGAIPlane
double Vr;
double best_rate_of_climb_speed;
double best_rate_of_climb;
double nominal_climb_speed;
double nominal_climb_rate;
double nominal_circuit_speed;
double min_circuit_speed;
double max_circuit_speed;
double nominal_descent_rate;
double nominal_approach_speed;
double nominal_final_speed;
double stall_speed_landing_config;
double nominal_taxi_speed;
// Physical/rendering stuff
double wheelOffset; // Height above ground at which we need to render the plane whilst taxiing
bool elevInitGood; // We have had at least one good elev reading
bool inAir; // True when off the ground
// environment - some of this might get moved into FGAIPlane
SGPropertyNode_ptr wind_from_hdg; //degrees
SGPropertyNode_ptr wind_speed_knots; //knots
// Pattern details that (may) change
int numInPattern; // Number of planes in the pattern (this might get more complicated if high performance GA aircraft fly a higher pattern eventually)
int numAhead; // More importantly - how many of them are ahead of us?
double distToNext; // And even more importantly, how near are we getting to the one immediately ahead?
//PatternLeg leg; // Our current position in the pattern - now moved to FGAIPlane
StartOfDescent SoD; // Start of descent calculated wrt wind, pattern size & altitude, glideslope etc
bool descending; // We're in the coming down phase of the pattern
double targetDescentRate; // m/s
// Taxiing details
// At the moment this assumes that all taxiing in is to gates (a loose term that includes
// any permitted parking spot) and that all taxiing out is to runways.
bool parked;
bool taxiing;
bool taxiRequestPending;
bool taxiRequestCleared;
TaxiState taxiState;
double desiredTaxiHeading;
double taxiTurnRadius;
double nominalTaxiSpeed;
Gate* ourGate;
ground_network_path_type path; // a path through the ground network for the plane to taxi
unsigned int taxiPathPos; // position of iterator in taxi path when applicable
node* nextTaxiNode; // next node in taxi path
node* holdShortNode;
//Runway out_dest; //FIXME - implement this
bool holdingShort;
bool reportReadyForDeparture; // set true when ATC has requested that the plane report when ready for departure
bool clearedToLineUp;
bool clearedToTakeOff;
bool _clearedToLand; // also implies cleared for the option.
bool liningUp; // Set true when the turn onto the runway heading is commenced when taxiing out
bool goAround; // Set true if need to go-around
bool goAroundCalled; // Set true during go-around only after we have called our go-around on the radio
bool contactTower; // we have been told to contact tower
bool contactGround; // we have been told to contact ground
bool changeFreq; // true when we need to change frequency
bool _taxiToGA; // Temporary mega-hack indicating we are to taxi to the GA parking and disconnect from tower control.
bool _removeSelf; // Indicates that we wish to remove this instance. The use of a variable is a hack to allow time for messages to purge before removal, due to the fagility of the current dialog system.
atc_type changeFreqType; // the service we need to change to
bool freeTaxi; // False if the airport has a facilities file with a logical taxi network defined, true if we need to calculate our own taxiing points.
// Hack for getting close to the runway when atan can go pear-shaped
double _savedSlope;
void FlyTrafficPattern(double dt);
// TODO - need to add something to define what option we are flying - Touch and go / Stop and go / Landing properly / others?
void TransmitPatternPositionReport();
void CalculateSoD(double base_leg_pos, double downwind_leg_pos, bool pattern_direction);
void ExitRunway(const Point3D& orthopos);
void StartTaxi();
void Taxi(double dt);
void GetNextTaxiNode();
void DoGroundElev();
// Set when the plane should be invisible *regardless of distance from user*.
bool _invisible;
};
#endif // _FG_AILocalTraffic_HXX

View file

@ -1,624 +0,0 @@
// AIMgr.cxx - implementation of FGAIMgr
// - a global management class for FlightGear generated AI traffic
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <Main/fg_props.hxx>
#include <Main/globals.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/math/sg_random.h>
#include <simgear/scene/model/modellib.hxx>
#include <list>
#ifdef _MSC_VER
# include <io.h>
#else
# include <sys/types.h> // for directory reading
# include <dirent.h> // for directory reading
#endif
#include <Environment/environment_mgr.hxx>
#include <Environment/environment.hxx>
#include "AIMgr.hxx"
#include "AILocalTraffic.hxx"
#include "AIGAVFRTraffic.hxx"
#include "ATCutils.hxx"
#include "commlist.hxx"
SG_USING_STD(list);
SG_USING_STD(cout);
using namespace simgear;
FGAIMgr::FGAIMgr() {
ATC = globals->get_ATC_mgr();
initDone = false;
ai_callsigns_used["GFS"] = 1; // so we don't inadvertently use this
// TODO - use the proper user callsign when it becomes user settable.
removalList.clear();
activated.clear();
_havePiperModel = true;
}
FGAIMgr::~FGAIMgr() {
for (ai_list_itr = ai_list.begin(); ai_list_itr != ai_list.end(); ai_list_itr++) {
delete (*ai_list_itr);
}
}
void FGAIMgr::init() {
//cout << "AIMgr::init called..." << endl;
// Pointers to user's position
lon_node = fgGetNode("/position/longitude-deg", true);
lat_node = fgGetNode("/position/latitude-deg", true);
elev_node = fgGetNode("/position/altitude-ft", true);
lon = lon_node->getDoubleValue();
lat = lat_node->getDoubleValue();
elev = elev_node->getDoubleValue();
// Load up models at the start to avoid pausing later
// Hack alert - Hardwired paths!!
string planepath = "Aircraft/c172p/Models/c172p.xml";
bool _loadedDefaultOK = true;
try {
_defaultModel = SGModelLib::loadPagedModel(planepath.c_str(), globals->get_props());
} catch(sg_exception&) {
_loadedDefaultOK = false;
}
if(!_loadedDefaultOK ) {
// Just load the same 3D model as the default user plane - that's *bound* to exist!
// TODO - implement robust determination of availability of GA AI aircraft models
planepath = "Aircraft/c172p/Models/c172p.ac";
_defaultModel = SGModelLib::loadPagedModel(planepath.c_str(), globals->get_props());
}
planepath = "Aircraft/pa28-161/Models/pa28-161.ac";
try {
_piperModel = SGModelLib::loadPagedModel(planepath.c_str(), globals->get_props());
} catch(sg_exception&) {
_havePiperModel = false;
}
// go through the $FG_ROOT/ATC directory and find all *.taxi files
SGPath path(globals->get_fg_root());
path.append("ATC/");
string dir = path.dir();
string ext;
string file, f_ident;
int pos;
ulDir *d;
struct ulDirEnt *de;
if ( (d = ulOpenDir( dir.c_str() )) == NULL ) {
SG_LOG(SG_ATC, SG_WARN, "cannot open directory " << dir);
} else {
// load all .taxi files
while ( (de = ulReadDir(d)) != NULL ) {
file = de->d_name;
pos = file.find(".");
ext = file.substr(pos + 1);
if(ext == "taxi") {
f_ident = file.substr(0, pos);
const FGAirport *a = fgFindAirportID( f_ident);
if(a){
SGBucket sgb(a->getLongitude(), a->getLatitude());
int idx = sgb.gen_index();
if(facilities.find(idx) != facilities.end()) {
facilities[idx]->push_back(f_ident);
} else {
ID_list_type* apts = new ID_list_type;
apts->push_back(f_ident);
facilities[idx] = apts;
}
SG_LOG(SG_ATC, SG_BULK, "Mapping " << f_ident << " to bucket " << idx);
}
}
}
ulCloseDir(d);
}
// See if are in range at startup and activate if necessary
SearchByPos(15.0);
initDone = true;
//cout << "AIMgr::init done..." << endl;
/*
// TESTING
FGATCAlignedProjection ortho;
ortho.Init(fgGetAirportPos("KEMT"), 205.0); // Guess of rwy19 heading
//Point3D ip = ortho.ConvertFromLocal(Point3D(6000, 1000, 1000)); // 90 deg entry
//Point3D ip = ortho.ConvertFromLocal(Point3D(-7000, 3000, 1000)); // 45 deg entry
Point3D ip = ortho.ConvertFromLocal(Point3D(1000, -7000, 1000)); // straight-in
ATC->AIRegisterAirport("KEMT");
FGAIGAVFRTraffic* p = new FGAIGAVFRTraffic();
p->SetModel(_defaultModel);
p->Init(ip, "KEMT", GenerateShortForm(GenerateUniqueCallsign()));
ai_list.push_back(p);
traffic[ident].push_back(p);
activated["KEMT"] = 1;
*/
}
void FGAIMgr::bind() {
}
void FGAIMgr::unbind() {
}
void FGAIMgr::update(double dt) {
if(!initDone) {
init();
SG_LOG(SG_ATC, SG_WARN, "Warning - AIMgr::update(...) called before AIMgr::init()");
}
//cout << activated.size() << '\n';
Point3D userPos = Point3D(lon_node->getDoubleValue(), lat_node->getDoubleValue(), elev_node->getDoubleValue());
// TODO - make these class variables!!
static int i = 0;
static int j = 0;
// Don't update any planes for first 50 runs through - this avoids some possible initialisation anomalies
// Might not need it now we have fade-in though?
if(i < 50) {
++i;
return;
}
if(j == 215) {
SearchByPos(25.0);
j = 0;
} else if(j == 200) {
// Go through the list of activated airports and remove those out of range
//cout << "The following airports have been activated by the AI system:\n";
ai_activated_map_iterator apt_itr = activated.begin();
while(apt_itr != activated.end()) {
//cout << "FIRST IS " << (*apt_itr).first << '\n';
if(dclGetHorizontalSeparation(userPos, fgGetAirportPos((*apt_itr).first)) > (35.0 * 1600.0)) {
// Then get rid of it and make sure the iterator is left pointing to the next one!
string s = (*apt_itr).first;
if(traffic.find(s) != traffic.end()) {
//cout << "s = " << s << ", traffic[s].size() = " << traffic[s].size() << '\n';
if(!traffic[s].empty()) {
apt_itr++;
} else {
//cout << "Erasing " << (*apt_itr).first << " and traffic" << '\n';
activated.erase(apt_itr);
apt_itr = activated.upper_bound(s);
traffic.erase(s);
}
} else {
//cout << "Erasing " << (*apt_itr).first << ' ' << (*apt_itr).second << '\n';
activated.erase(apt_itr);
apt_itr = activated.upper_bound(s);
}
} else {
apt_itr++;
}
}
} else if(j == 180) {
// Go through the list of activated airports and do the random airplane generation
ai_traffic_map_iterator it = traffic.begin();
while(it != traffic.end()) {
string s = (*it).first;
//cout << "s = " << s << " size = " << (*it).second.size() << '\n';
// Only generate extra traffic if within a certain distance of the user,
// TODO - maybe take users's tuned freq into account as well.
double d = dclGetHorizontalSeparation(userPos, fgGetAirportPos(s));
if(d < (15.0 * 1600.0)) {
double cd = 0.0;
bool gen = false;
//cout << "Size of list is " << (*it).second.size() << " at " << s << '\n';
if((*it).second.size()) {
FGAIEntity* e = *((*it).second.rbegin()); // Get the last airplane currently scheduled to arrive at this airport.
cd = dclGetHorizontalSeparation(e->GetPos(), fgGetAirportPos(s));
if(cd < (d < 5000 ? 10000 : d + 5000)) {
gen = true;
}
} else {
gen = true;
cd = 0.0;
}
if(gen) {
//cout << "Generating extra traffic at airport " << s << ", at least " << cd << " meters out\n";
//GenerateSimpleAirportTraffic(s, cd);
GenerateSimpleAirportTraffic(s, cd + 3000.0); // The random seems a bit wierd - traffic could get far too bunched without the +3000.
// TODO - make the anti-random constant variable depending on the ai-traffic level.
}
}
++it;
}
}
++j;
//cout << "Size of AI list is " << ai_list.size() << '\n';
// TODO - need to add a check of if any activated airports have gone out of range
string rs; // plane to be removed, if one.
if(removalList.size()) {
rs = *(removalList.begin());
removalList.pop_front();
} else {
rs = "";
}
// Traverse the list of active planes and run all their update methods
// TODO - spread the load - not all planes should need updating every frame.
// Note that this will require dt to be calculated for each plane though
// since they rely on it to calculate distance travelled.
ai_list_itr = ai_list.begin();
while(ai_list_itr != ai_list.end()) {
FGAIEntity *e = *ai_list_itr;
if(rs.size() && e->GetCallsign() == rs) {
//cout << "Removing " << rs << " from ai_list\n";
ai_list_itr = ai_list.erase(ai_list_itr);
delete e;
// This is a hack - we should deref this plane from the airport count!
} else {
e->Update(dt);
++ai_list_itr;
}
}
//cout << "Size of AI list is " << ai_list.size() << '\n';
}
void FGAIMgr::ScheduleRemoval(const string& s) {
//cout << "Scheduling removal of plane " << s << " from AIMgr\n";
removalList.push_back(s);
}
// Activate AI traffic at an airport
void FGAIMgr::ActivateAirport(const string& ident) {
ATC->AIRegisterAirport(ident);
// TODO - need to start the traffic more randomly
FGAILocalTraffic* local_traffic = new FGAILocalTraffic;
local_traffic->SetModel(_defaultModel.get()); // currently hardwired to cessna.
//local_traffic->Init(ident, IN_PATTERN, TAKEOFF_ROLL);
local_traffic->Init(GenerateShortForm(GenerateUniqueCallsign()), ident);
local_traffic->FlyCircuits(1, true); // Fly 2 circuits with touch & go in between
ai_list.push_back(local_traffic);
traffic[ident].push_back(local_traffic);
//cout << "******** ACTIVATING AIRPORT, ident = " << ident << '\n';
activated[ident] = 1;
}
// Hack - Generate AI traffic at an airport with no facilities file
void FGAIMgr::GenerateSimpleAirportTraffic(const string& ident, double min_dist) {
// Ugly hack - don't let VFR Cessnas operate at a hardwired list of major airports
// This will go eventually once airport .xml files specify the traffic profile
if(ident == "KSFO" || ident == "KDFW" || ident == "EGLL" || ident == "KORD" || ident == "KJFK"
|| ident == "KMSP" || ident == "KLAX" || ident == "KBOS" || ident == "KEDW"
|| ident == "KSEA" || ident == "EHAM") {
return;
}
/*
// TODO - check for military airports - this should be in the current data.
// UGGH - there's no point at the moment - everything is labelled civil in basic.dat!
FGAirport a = fgFindAirportID(ident, &a);
if(a) {
cout << "CODE IS " << a.code << '\n';
} else {
// UG - can't find the airport!
return;
}
*/
Point3D aptpos = fgGetAirportPos(ident); // TODO - check for elev of -9999
//cout << "ident = " << ident << ", elev = " << aptpos.elev() << '\n';
// Operate from airports at 3000ft and below only to avoid the default cloud layers and since we don't degrade AI performance with altitude.
if(aptpos.elev() > 3000) {
//cout << "High alt airports not yet supported - returning\n";
return;
}
// Rough hack for plane type - make 70% of the planes cessnas, the rest pipers.
bool cessna = true;
// Get the time and only operate VFR in the (approximate) daytime.
struct tm *t = globals->get_time_params()->getGmt();
int loc_time = t->tm_hour + int(aptpos.lon() / (360.0 / 24.0));
while (loc_time < 0)
loc_time += 24;
while (loc_time >= 24)
loc_time -= 24;
//cout << "loc_time = " << loc_time << '\n';
if(loc_time < 7 || loc_time > 19) return;
// Check that the visibility is OK for IFR operation.
double visibility;
FGEnvironment stationweather =
((FGEnvironmentMgr *)globals->get_subsystem("environment"))
->getEnvironment(aptpos.lat(), aptpos.lon(), aptpos.elev()); // TODO - check whether this should take ft or m for elev.
visibility = stationweather.get_visibility_m();
// Technically we can do VFR down to 1 mile (1600m) but that's pretty murky!
//cout << "vis = " << visibility << '\n';
if(visibility < 3000) return;
ATC->AIRegisterAirport(ident);
// Next - get the distance from user to the airport.
Point3D userpos = Point3D(lon_node->getDoubleValue(), lat_node->getDoubleValue(), elev_node->getDoubleValue());
double d = dclGetHorizontalSeparation(userpos, aptpos); // in meters
int lev = fgGetInt("/sim/ai-traffic/level");
if(lev < 1)
return;
if (lev > 3)
lev = 3;
if(visibility < 6000) lev = 1;
//cout << "level = " << lev << '\n';
// Next - generate any local / circuit traffic
/*
// --------------------------- THIS BLOCK IS JUST FOR TESTING - COMMENT OUT BEFORE RELEASE ---------------
// Finally - generate VFR approaching traffic
//if(d > 2000) {
if(ident == "KPOC") {
double ad = 2000.0;
double avd = 3000.0; // average spacing of arriving traffic in meters - relate to airport business and AI density setting one day!
//while(ad < (d < 10000 ? 12000 : d + 2000)) {
for(int i=0; i<8; ++i) {
double dd = sg_random() * avd;
// put a minimum spacing in for now since I don't think tower will cope otherwise!
if(dd < 1500) dd = 1500;
//ad += dd;
ad += dd;
double dir = int(sg_random() * 36);
if(dir == 36) dir--;
dir *= 10;
//dir = 180;
if(sg_random() < 0.3) cessna = false;
else cessna = true;
string s = GenerateShortForm(GenerateUniqueCallsign(), (cessna ? "Cessna-" : "Piper-"));
FGAIGAVFRTraffic* t = new FGAIGAVFRTraffic();
t->SetModel(cessna ? _defaultModel : _piperModel);
//cout << "Generating VFR traffic " << s << " inbound to " << ident << " " << ad << " meters out from " << dir << " degrees\n";
Point3D tpos = dclUpdatePosition(aptpos, dir, 6.0, ad);
if(tpos.elev() > (aptpos.elev() + 3000.0)) tpos.setelev(aptpos.elev() + 3000.0);
t->Init(tpos, ident, s);
ai_list.push_back(t);
}
}
activated[ident] = 1;
return;
//---------------------------------------------------------------------------------------------------
*/
double ad; // Minimum distance out of first arriving plane in meters.
double mind; // Minimum spacing of traffic in meters
double avd; // average spacing of arriving traffic in meters - relate to airport business and AI density setting one day!
// Finally - generate VFR approaching traffic
//if(d > 2000) {
if(1) {
if(lev == 3) {
ad = 5000.0;
mind = 2000.0;
avd = 6000.0;
} else if(lev == 2) {
ad = 8000.0;
mind = 4000.0;
avd = 10000.0;
} else {
ad = 9000.0; // Start the first aircraft at least 9K out for now.
mind = 6000.0;
avd = 15000.0;
}
/*
// Check if there is already arriving traffic at this airport
cout << "BING A " << ident << '\n';
if(traffic.find(ident) != traffic.end()) {
cout << "BING B " << ident << '\n';
ai_list_type lst = traffic[ident];
cout << "BING C " << ident << '\n';
if(lst.size()) {
cout << "BING D " << ident << '\n';
double cd = dclGetHorizontalSeparation(aptpos, (*lst.rbegin())->GetPos());
cout << "ident = " << ident << ", cd = " << cd << '\n';
if(cd > ad) ad = cd;
}
}
*/
if(min_dist != 0) ad = min_dist;
//cout << "ident = " << ident << ", ad = " << ad << '\n';
while(ad < (d < 5000 ? 15000 : d + 10000)) {
double dd = mind + (sg_random() * (avd - mind));
ad += dd;
double dir = int(sg_random() * 36);
if(dir == 36) dir--;
dir *= 10;
if(sg_random() < 0.3) cessna = false;
else cessna = true;
string s = GenerateShortForm(GenerateUniqueCallsign(), (cessna ? "Cessna-" : "Piper-"));
FGAIGAVFRTraffic* t = new FGAIGAVFRTraffic();
t->SetModel(cessna ? _defaultModel.get() : (_havePiperModel ? _piperModel.get() : _defaultModel.get()));
//cout << "Generating VFR traffic " << s << " inbound to " << ident << " " << ad << " meters out from " << dir << " degrees\n";
Point3D tpos = dclUpdatePosition(aptpos, dir, 6.0, ad);
if(tpos.elev() > (aptpos.elev() + 3000.0)) tpos.setelev(aptpos.elev() + 3000.0); // FEET yuk :-(
t->Init(tpos, ident, s);
ai_list.push_back(t);
traffic[ident].push_back(t);
}
}
}
/*
// Generate a VFR arrival at airport apt, at least distance d (meters) out.
void FGAIMgr::GenerateVFRArrival(const string& apt, double d) {
}
*/
// Search for valid airports in the vicinity of the user and activate them if necessary
void FGAIMgr::SearchByPos(double range) {
//cout << "In SearchByPos(...)" << endl;
// get bucket number for plane position
lon = lon_node->getDoubleValue();
lat = lat_node->getDoubleValue();
elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
SGBucket buck(lon, lat);
// get neigboring buckets
int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
//cout << "bx = " << bx << endl;
int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
//cout << "by = " << by << endl;
// Search for airports with facitities files --------------------------
// loop over bucket range
for ( int i=-bx; i<=bx; i++) {
//cout << "i loop\n";
for ( int j=-by; j<=by; j++) {
//cout << "j loop\n";
buck = sgBucketOffset(lon, lat, i, j);
long int bucket = buck.gen_index();
//cout << "bucket is " << bucket << endl;
if(facilities.find(bucket) != facilities.end()) {
ID_list_type* apts = facilities[bucket];
ID_list_iterator current = apts->begin();
ID_list_iterator last = apts->end();
//cout << "Size of apts is " << apts->size() << endl;
//double rlon = lon * SGD_DEGREES_TO_RADIANS;
//double rlat = lat * SGD_DEGREES_TO_RADIANS;
//Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
//Point3D airport;
for(; current != last; ++current) {
//cout << "Found " << *current << endl;;
if(activated.find(*current) == activated.end()) {
//cout << "Activating " << *current << endl;
//FGAirport a;
//if(dclFindAirportID(*current, &a)) {
// // We can do something here based on distance from the user if we wish.
//}
//string s = *current;
//cout << "s = " << s << '\n';
ActivateAirport(*current);
//ActivateSimpleAirport(*current); // TODO - put this back to ActivateAirport when that code is done.
//cout << "Activation done" << endl;
} else {
//cout << *current << " already activated" << endl;
}
}
}
}
}
//-------------------------------------------------------------
// Search for any towered airports in the vicinity ------------
comm_list_type towered;
comm_list_iterator twd_itr;
int num_twd = current_commlist->FindByPos(lon, lat, elev, range, &towered, TOWER);
if (num_twd != 0) {
double closest = 1000000;
string s = "";
for(twd_itr = towered.begin(); twd_itr != towered.end(); twd_itr++) {
// Only activate the closest airport not already activated each time.
if(activated.find(twd_itr->ident) == activated.end()) {
double sep = dclGetHorizontalSeparation(Point3D(lon, lat, elev), fgGetAirportPos(twd_itr->ident));
if(sep < closest) {
closest = sep;
s = twd_itr->ident;
}
}
}
if(s.size()) {
// TODO - find out why empty strings come through here when all in-range airports done.
GenerateSimpleAirportTraffic(s);
//cout << "**************ACTIVATING SIMPLE AIRPORT, ident = " << s << '\n';
activated[s] = 1;
}
}
}
string FGAIMgr::GenerateCallsign() {
// For now we'll just generate US callsigns until we can regionally identify airports.
string s = "N";
// Add 3 to 5 numbers and make up to 5 with letters.
//sg_srandom_time();
double d = sg_random();
int n = int(d * 3);
if(n == 3) --n;
//cout << "First n, n = " << n << '\n';
int j = 3 + n;
//cout << "j = " << j << '\n';
for(int i=0; i<j; ++i) {
int n = int(sg_random() * 10);
if(n == 10) --n;
s += (char)('0' + n);
}
for(int i=j; i<5; ++i) {
int n = int(sg_random() * 26);
if(n == 26) --n;
//cout << "Alpha, n = " << n << '\n';
s += (char)('A' + n);
}
//cout << "s = " << s << '\n';
return(s);
}
string FGAIMgr::GenerateUniqueCallsign() {
while(1) {
string s = GenerateCallsign();
if(!ai_callsigns_used[s]) {
ai_callsigns_used[s] = 1;
return(s);
}
}
}
// This will be moved somewhere else eventually!!!!
string FGAIMgr::GenerateShortForm(const string& callsign, const string& plane_str, bool local) {
//cout << callsign << '\n';
string s;
if(local) s = "Trainer-";
else s = plane_str;
for(int i=3; i>0; --i) {
char c = callsign[callsign.size() - i];
//cout << c << '\n';
string tmp = "";
tmp += c;
if(isalpha(c)) s += GetPhoneticIdent(c);
else s += ConvertNumToSpokenDigits(tmp);
if(i > 1) s += '-';
}
return(s);
}

View file

@ -1,139 +0,0 @@
// AIMgr.hxx - definition of FGAIMgr
// - a global management class for FlightGear generated AI traffic
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_AIMGR_HXX
#define _FG_AIMGR_HXX
#include <simgear/structure/subsystem_mgr.hxx>
#include <Main/fg_props.hxx>
#include <list>
#include "ATCmgr.hxx"
#include "AIEntity.hxx"
SG_USING_STD(list);
class FGAIMgr : public SGSubsystem
{
private:
FGATCMgr* ATC;
// This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
// A list of pointers to all currently active AI stuff
typedef list <FGAIEntity*> ai_list_type;
typedef ai_list_type::iterator ai_list_iterator;
typedef ai_list_type::const_iterator ai_list_const_iterator;
// Everything put in this list should be created dynamically
// on the heap and ***DELETED WHEN REMOVED!!!!!***
ai_list_type ai_list;
ai_list_iterator ai_list_itr;
// Any member function of FGATCMgr is permitted to leave this iterator pointing
// at any point in or at the end of the list.
// Hence any new access must explicitly first check for atc_list.end() before dereferencing.
// A list of airport or airplane ID's
typedef list < string > ID_list_type;
typedef ID_list_type::iterator ID_list_iterator;
// Temporary storage of ID of planes scheduled for removeal
ID_list_type removalList;
// A map of airport-IDs that have taxiway network files against bucket number
typedef map < int, ID_list_type* > ai_apt_map_type;
typedef ai_apt_map_type::iterator ai_apt_map_iterator;
ai_apt_map_type facilities;
// A map of airport ID's that we've activated AI traffic at
typedef map < string, int > ai_activated_map_type;
typedef ai_activated_map_type::iterator ai_activated_map_iterator;
ai_activated_map_type activated;
// AI traffic lists mapped by airport
typedef map < string, ai_list_type > ai_traffic_map_type;
typedef ai_traffic_map_type::iterator ai_traffic_map_iterator;
ai_traffic_map_type traffic;
// A map of callsigns that we have used (eg CFGFS or N0546D - the code will generate Cessna-four-six-delta from this later)
typedef map < string, int > ai_callsigns_map_type;
typedef ai_callsigns_map_type::iterator ai_callsigns_map_iterator;
ai_callsigns_map_type ai_callsigns_used;
// Position of the Users Aircraft
double lon;
double lat;
double elev;
// Pointers to current users position
SGPropertyNode_ptr lon_node;
SGPropertyNode_ptr lat_node;
SGPropertyNode_ptr elev_node;
public:
FGAIMgr();
~FGAIMgr();
void init();
void bind();
void unbind();
void update(double dt);
// Signal that it is OK to remove a plane of callsign s
// (To be called by the plane itself).
void ScheduleRemoval(const string& s);
private:
osg::ref_ptr<osg::Node> _defaultModel; // Cessna 172!
osg::ref_ptr<osg::Node> _piperModel; // pa28-161
bool initDone; // Hack - guard against update getting called before init
// Remove a class from the ai_list and delete it from memory
//void RemoveFromList(const char* id, atc_type tp);
// Activate AI traffic at an airport
void ActivateAirport(const string& ident);
// Hack - Generate AI traffic at an airport with no facilities file, with the first plane being at least min_dist out.
void GenerateSimpleAirportTraffic(const string& ident, double min_dist = 0.0);
// Search for valid airports in the vicinity of the user and activate them if necessary
void SearchByPos(double range);
string GenerateCallsign();
string GenerateUniqueCallsign();
string GenerateShortForm(const string& callsign, const string& plane_str = "Cessna-", bool local = false);
// TODO - implement a proper robust system for registering and loading AI GA aircraft models
bool _havePiperModel;
};
#endif // _FG_AIMGR_HXX

View file

@ -1,272 +0,0 @@
// FGAIPlane - abstract base class for an AI plane
//
// Written by David Luff, started 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <Main/globals.hxx>
#include <Main/fg_props.hxx>
#include <simgear/math/point3d.hxx>
#include <simgear/debug/logstream.hxx>
#include <simgear/sound/soundmgr_openal.hxx>
#include <math.h>
#include <string>
SG_USING_STD(string);
#include "AIPlane.hxx"
FGAIPlane::FGAIPlane() {
leg = LEG_UNKNOWN;
tuned_station = NULL;
pending_transmission = "";
_timeout = 0;
_pending = false;
_callback_code = 0;
_transmit = false;
_transmitting = false;
voice = false;
playing = false;
voiceOK = false;
vPtr = NULL;
track = 0.0;
_tgtTrack = 0.0;
_trackSet = false;
_tgtRoll = 0.0;
_rollSuspended = false;
}
FGAIPlane::~FGAIPlane() {
}
void FGAIPlane::Update(double dt) {
if(_pending) {
if(tuned_station) {
if(tuned_station->GetFreqClear()) {
//cout << "TUNED STATION FREQ CLEAR\n";
tuned_station->SetFreqInUse();
_pending = false;
_transmit = true;
_transmitting = false;
} else {
if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
_timeout -= dt;
if(_timeout <= 0.0) {
_timeout = 0.0;
_pending = false;
// timed out - don't render.
if(_callback_code == 99) {
// MEGA-HACK - 99 is the remove self callback - currently this *does* need to be run even if the transmission isn't made.
ProcessCallback(_callback_code);
}
}
}
}
} else {
// Not tuned to ATC - Just go ahead and transmit
//cout << "NOT TUNED TO ATC\n";
_pending = false;
_transmit = true;
_transmitting = false;
}
}
// This turns on rendering if on the same freq as the user
// TODO - turn it off if user switches to another freq - keep track of where in message we are etc.
if(_transmit) {
//cout << "transmit\n";
double user_freq0 = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
double user_freq1 = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
_counter = 0.0;
_max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
//cout << "Transmission = " << pending_transmission << '\n';
// The radios dialog seems to set slightly imprecise freqs, eg 118.099998
// The eplison stuff below is a work-around
double eps0 = fabs(freq - user_freq0);
double eps1 = fabs(freq - user_freq1);
if(eps0 < 0.002 || eps1 < 0.002) {
//cout << "Transmitting..." << endl;
// we are on the same frequency, so check distance to the user plane
if(1) {
// For now assume in range !!!
// TODO - implement range checking
Render(plane.callsign, false);
}
}
// Run the callback regardless of whether on same freq as user or not.
if(_callback_code) {
ProcessCallback(_callback_code);
}
_transmit = false;
_transmitting = true;
} else if(_transmitting) {
if(_counter >= _max_count) {
NoRender(plane.callsign);
_transmitting = false;
// For now we'll let ATC decide whether to respond
//if(tuned_station) tuned_station->SetResponseReqd(plane.callsign);
//if(tuned_station->get_ident() == "KRHV") cout << "Notifying transmission finished" << endl;
if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
}
_counter += dt;
}
// Fly the plane if necessary
if(_trackSet) {
while((_tgtTrack - track) > 180.0) track += 360.0;
while((track - _tgtTrack) > 180.0) track -= 360.0;
double turn_time = 60.0;
track += (360.0 / turn_time) * dt * (_tgtTrack > track ? 1.0 : -1.0);
// TODO - bank a bit less for small turns.
Bank(25.0 * (_tgtTrack > track ? 1.0 : -1.0));
if(fabs(track - _tgtTrack) < 2.0) { // TODO - might need to optimise the delta there - it's on the large (safe) side atm.
track = _tgtTrack;
LevelWings();
}
}
if(!_rollSuspended) {
if(fabs(_roll - _tgtRoll) > 0.6) {
// This *should* bank us smoothly to any angle
_roll -= ((_roll - _tgtRoll)/fabs(_roll - _tgtRoll));
} else {
_roll = _tgtRoll;
}
}
}
void FGAIPlane::Transmit(int callback_code) {
SG_LOG(SG_ATC, SG_INFO, "Transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
_pending = true;
_callback_code = callback_code;
_timeout = 0.0;
}
void FGAIPlane::ConditionalTransmit(double timeout, int callback_code) {
SG_LOG(SG_ATC, SG_INFO, "Timed transmit called for plane " << plane.callsign << ", msg = " << pending_transmission);
_pending = true;
_callback_code = callback_code;
_timeout = timeout;
}
void FGAIPlane::ImmediateTransmit(int callback_code) {
Render(plane.callsign, false);
if(callback_code) {
ProcessCallback(callback_code);
}
}
// Derived classes should override this.
void FGAIPlane::ProcessCallback(int code) {
}
// Render a transmission
// Outputs the transmission either on screen or as audio depending on user preference
// The refname is a string to identify this sample to the sound manager
// The repeating flag indicates whether the message should be repeated continuously or played once.
void FGAIPlane::Render(const string& refname, bool repeating) {
fgSetString("/sim/messages/ai-plane", pending_transmission.c_str());
#ifdef ENABLE_AUDIO_SUPPORT
voice = (voiceOK && fgGetBool("/sim/sound/voice"));
if(voice) {
int len;
unsigned char* buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), len, voice);
if(voice) {
SGSoundSample* simple = new SGSoundSample(buf, len, 8000);
// TODO - at the moment the volume is always set off comm1
// and can't be changed after the transmission has started.
simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume"));
globals->get_soundmgr()->add(simple, refname);
if(repeating) {
globals->get_soundmgr()->play_looped(refname);
} else {
globals->get_soundmgr()->play_once(refname);
}
}
delete[] buf;
}
#endif // ENABLE_AUDIO_SUPPORT
if(!voice) {
// first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
for(unsigned int i = 0; i < pending_transmission.length(); ++i) {
if((pending_transmission.substr(i,1) == "_") || (pending_transmission.substr(i,1) == "/")) {
pending_transmission[i] = ' ';
}
}
}
playing = true;
}
// Cease rendering a transmission.
void FGAIPlane::NoRender(const string& refname) {
if(playing) {
if(voice) {
#ifdef ENABLE_AUDIO_SUPPORT
globals->get_soundmgr()->stop(refname);
globals->get_soundmgr()->remove(refname);
#endif
}
playing = false;
}
}
/*
*/
void FGAIPlane::RegisterTransmission(int code) {
}
// Return what type of landing we're doing on this circuit
LandingType FGAIPlane::GetLandingOption() {
return(FULL_STOP);
}
ostream& operator << (ostream& os, PatternLeg pl) {
switch(pl) {
case(TAKEOFF_ROLL): return(os << "TAKEOFF ROLL");
case(CLIMBOUT): return(os << "CLIMBOUT");
case(TURN1): return(os << "TURN1");
case(CROSSWIND): return(os << "CROSSWIND");
case(TURN2): return(os << "TURN2");
case(DOWNWIND): return(os << "DOWNWIND");
case(TURN3): return(os << "TURN3");
case(BASE): return(os << "BASE");
case(TURN4): return(os << "TURN4");
case(FINAL): return(os << "FINAL");
case(LANDING_ROLL): return(os << "LANDING ROLL");
case(LEG_UNKNOWN): return(os << "UNKNOWN");
}
return(os << "ERROR - Unknown switch in PatternLeg operator << ");
}
ostream& operator << (ostream& os, LandingType lt) {
switch(lt) {
case(FULL_STOP): return(os << "FULL STOP");
case(STOP_AND_GO): return(os << "STOP AND GO");
case(TOUCH_AND_GO): return(os << "TOUCH AND GO");
case(AIP_LT_UNKNOWN): return(os << "UNKNOWN");
}
return(os << "ERROR - Unknown switch in LandingType operator << ");
}

View file

@ -1,165 +0,0 @@
// FGAIPlane - abstract base class for an AI plane
//
// Written by David Luff, started 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_AI_PLANE_HXX
#define _FG_AI_PLANE_HXX
#include <simgear/math/point3d.hxx>
#include "AIEntity.hxx"
#include "ATC.hxx"
enum PatternLeg {
TAKEOFF_ROLL,
CLIMBOUT,
TURN1,
CROSSWIND,
TURN2,
DOWNWIND,
TURN3,
BASE,
TURN4,
FINAL,
LANDING_ROLL,
LEG_UNKNOWN
};
ostream& operator << (ostream& os, PatternLeg pl);
enum LandingType {
FULL_STOP,
STOP_AND_GO,
TOUCH_AND_GO,
AIP_LT_UNKNOWN
};
ostream& operator << (ostream& os, LandingType lt);
/*****************************************************************
*
* FGAIPlane - this class is derived from FGAIEntity and adds the
* practical requirement for an AI plane - the ability to send radio
* communication, and simple performance details for the actual AI
* implementation to use. The AI implementation is expected to be
* in derived classes - this class does nothing useful on its own.
*
******************************************************************/
class FGAIPlane : public FGAIEntity {
public:
FGAIPlane();
virtual ~FGAIPlane();
// Run the internal calculations
void Update(double dt);
// Send a transmission *TO* the AIPlane.
// FIXME int code is a hack - eventually this will receive Alexander's coded messages.
virtual void RegisterTransmission(int code);
// Return the current pattern leg the plane is flying.
inline PatternLeg GetLeg() {return leg;}
// Return what type of landing we're doing on this circuit
virtual LandingType GetLandingOption();
// Return the callsign
inline const string& GetCallsign() {return plane.callsign;}
protected:
PlaneRec plane;
double mag_hdg; // degrees - the heading that the physical aircraft is *pointing*
double track; // track that the physical aircraft is *following* - degrees relative to *true* north
double crab; // Difference between heading and track due to wind.
double mag_var; // degrees
double IAS; // Indicated airspeed in knots
double vel; // velocity along track in knots
double vel_si; // velocity along track in m/s
double slope; // Actual slope that the plane is flying (degrees) - +ve is uphill
double AoA; // degrees - difference between slope and pitch
// We'll assume that the plane doesn't yaw or slip - the track/heading difference is to allow for wind
double freq; // The comm frequency that we're operating on
// We need some way for this class to display its radio transmissions if on the
// same frequency and in the vicinity of the user's aircraft
// This may need to be done independently of ATC eg CTAF
// Make radio transmission - this simply sends the transmission for physical rendering if the users
// aircraft is on the same frequency and in range. It is up to the derived classes to let ATC know
// what is going on.
string pending_transmission; // derived classes set this string before calling Transmit(...)
FGATC* tuned_station; // and this if they are tuned to ATC
// Transmit a message when channel becomes free of other dialog
void Transmit(int callback_code = 0);
// Transmit a message if channel becomes free within timeout (seconds). timeout of zero implies no limit
void ConditionalTransmit(double timeout, int callback_code = 0);
// Transmit regardless of other dialog on the channel eg emergency
void ImmediateTransmit(int callback_code = 0);
inline void SetTrack(double t) { _tgtTrack = t; _trackSet = true; }
inline void ClearTrack() { _trackSet = false; }
inline void Bank(double r) { _tgtRoll = r; }
inline void LevelWings(void) { _tgtRoll = 0.0; }
virtual void ProcessCallback(int code);
PatternLeg leg;
private:
bool _pending;
double _timeout;
int _callback_code; // A callback code to be notified and processed by the derived classes
// A value of zero indicates no callback required
bool _transmit; // we are to transmit
bool _transmitting; // we are transmitting
double _counter;
double _max_count;
// Render a transmission (in string pending_transmission)
// Outputs the transmission either on screen or as audio depending on user preference
// The refname is a string to identify this sample to the sound manager
// The repeating flag indicates whether the message should be repeated continuously or played once.
void Render(const string& refname, bool repeating);
// Cease rendering a transmission.
// Requires the sound manager refname if audio, else "".
void NoRender(const string& refname);
// Rendering related stuff
bool voice; // Flag - true if we are using voice
bool playing; // Indicates a message in progress
bool voiceOK; // Flag - true if at least one voice has loaded OK
FGATCVoice* vPtr;
// Navigation
double _tgtTrack; // Track to be following if _trackSet is true
bool _trackSet; // Set true if tgtTrack is to be followed
double _tgtRoll;
bool _rollSuspended; // Set true when a derived class has suspended AIPlane's roll control
};
#endif // _FG_AI_PLANE_HXX

View file

@ -1,291 +0,0 @@
// Implementation of FGATC - ATC subsystem base class.
//
// Written by David Luff, started February 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/sound/soundmgr_openal.hxx>
#include <simgear/structure/exception.hxx>
#include <Main/globals.hxx>
#include <Main/fg_props.hxx>
#include "ATC.hxx"
FGATC::FGATC() {
freqClear = true;
receiving = false;
respond = false;
runResponseCounter = false;
_runReleaseCounter = false;
responseID = "";
responseReqd = false;
_type = INVALID;
_display = false;
_displaying = false;
// Transmission timing stuff
pending_transmission = "";
_timeout = 0;
_pending = false;
_callback_code = 0;
_transmit = false;
_transmitting = false;
_counter = 0.0;
_max_count = 5.0;
_voiceOK = false;
}
FGATC::~FGATC() {
}
// Derived classes wishing to use the response counter should call this from their own Update(...).
void FGATC::Update(double dt) {
if(runResponseCounter) {
//cout << responseCounter << '\t' << responseTime << '\n';
if(responseCounter >= responseTime) {
runResponseCounter = false;
respond = true;
//cout << "RESPOND\n";
} else {
responseCounter += dt;
}
}
if(_runReleaseCounter) {
if(_releaseCounter >= _releaseTime) {
freqClear = true;
_runReleaseCounter = false;
} else {
_releaseCounter += dt;
}
}
// Transmission stuff cribbed from AIPlane.cxx
if(_pending) {
if(GetFreqClear()) {
//cout << "TUNED STATION FREQ CLEAR\n";
SetFreqInUse();
_pending = false;
_transmit = true;
_transmitting = false;
} else {
if(_timeout > 0.0) { // allows count down to be avoided by initially setting it to zero
_timeout -= dt;
if(_timeout <= 0.0) {
_timeout = 0.0;
_pending = false;
// timed out - don't render.
}
}
}
}
if(_transmit) {
_counter = 0.0;
_max_count = 5.0; // FIXME - hardwired length of message - need to calculate it!
//cout << "Transmission = " << pending_transmission << '\n';
if(_display) {
//Render(pending_transmission, ident, false);
Render(pending_transmission);
}
// Run the callback regardless of whether on same freq as user or not.
//cout << "_callback_code = " << _callback_code << '\n';
if(_callback_code) {
ProcessCallback(_callback_code);
}
_transmit = false;
_transmitting = true;
} else if(_transmitting) {
if(_counter >= _max_count) {
//NoRender(plane.callsign); commented out since at the moment NoRender is designed just to stop repeating messages,
// and this will be primarily used on single messages.
_transmitting = false;
//if(tuned_station) tuned_station->NotifyTransmissionFinished(plane.callsign);
// TODO - need to let the plane the transmission is aimed at that it's finished.
// However, for now we'll just release the frequency since if we don't it all goes pear-shaped
_releaseCounter = 0.0;
_releaseTime = 0.9;
_runReleaseCounter = true;
}
_counter += dt;
}
}
void FGATC::ReceiveUserCallback(int code) {
SG_LOG(SG_ATC, SG_WARN, "WARNING - whichever ATC class was intended to receive callback code " << code << " didn't get it!!!");
}
void FGATC::SetResponseReqd(const string& rid) {
receiving = false;
responseReqd = true;
respond = false; // TODO - this ignores the fact that more than one plane could call this before response
// Shouldn't happen with AI only, but user could confuse things??
responseID = rid;
runResponseCounter = true;
responseCounter = 0.0;
responseTime = 1.8; // TODO - randomize this slightly.
}
void FGATC::NotifyTransmissionFinished(const string& rid) {
//cout << "Transmission finished, callsign = " << rid << '\n';
receiving = false;
responseID = rid;
if(responseReqd) {
runResponseCounter = true;
responseCounter = 0.0;
responseTime = 1.2; // TODO - randomize this slightly, and allow it to be dependent on the transmission and how busy the ATC is.
respond = false; // TODO - this ignores the fact that more than one plane could call this before response
// Shouldn't happen with AI only, but user could confuse things??
} else {
freqClear = true;
}
}
void FGATC::Transmit(int callback_code) {
SG_LOG(SG_ATC, SG_INFO, "Transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
_pending = true;
_callback_code = callback_code;
_timeout = 0.0;
}
void FGATC::ConditionalTransmit(double timeout, int callback_code) {
SG_LOG(SG_ATC, SG_INFO, "Timed transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
_pending = true;
_callback_code = callback_code;
_timeout = timeout;
}
void FGATC::ImmediateTransmit(int callback_code) {
SG_LOG(SG_ATC, SG_INFO, "Immediate transmit called by " << ident << " " << _type << ", msg = " << pending_transmission);
if(_display) {
//Render(pending_transmission, ident, false);
Render(pending_transmission);
// At the moment Render doesn't work except for ATIS
}
if(callback_code) {
ProcessCallback(callback_code);
}
}
// Derived classes should override this.
void FGATC::ProcessCallback(int code) {
}
void FGATC::AddPlane(const string& pid) {
}
int FGATC::RemovePlane() {
return 0;
}
void FGATC::SetData(ATCData* d) {
lon = d->lon;
lat = d->lat;
elev = d->elev;
x = d->x;
y = d->y;
z = d->z;
range = d->range;
ident = d->ident;
name = d->name;
freq = d->freq;
}
// Render a transmission
// Outputs the transmission either on screen or as audio depending on user preference
// The refname is a string to identify this sample to the sound manager
// The repeating flag indicates whether the message should be repeated continuously or played once.
void FGATC::Render(string& msg, const string& refname, bool repeating) {
if (repeating)
fgSetString("/sim/messages/atis", msg.c_str());
else
fgSetString("/sim/messages/atc", msg.c_str());
#ifdef ENABLE_AUDIO_SUPPORT
_voice = (_voiceOK && fgGetBool("/sim/sound/voice"));
if(_voice) {
int len;
unsigned char* buf = _vPtr->WriteMessage((char*)msg.c_str(), len, _voice);
if(_voice) {
try {
SGSoundSample *simple
= new SGSoundSample(buf, len, 8000);
// TODO - at the moment the volume is always set off comm1
// and can't be changed after the transmission has started.
simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume"));
globals->get_soundmgr()->add(simple, refname);
if(repeating) {
globals->get_soundmgr()->play_looped(refname);
} else {
globals->get_soundmgr()->play_once(refname);
}
} catch ( sg_io_exception &e ) {
SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage());
}
}
delete[] buf;
}
#endif // ENABLE_AUDIO_SUPPORT
if(!_voice) {
// first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
for(unsigned int i = 0; i < msg.length(); ++i) {
if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
msg[i] = ' ';
}
}
}
_playing = true;
}
// Cease rendering a transmission.
void FGATC::NoRender(const string& refname) {
if(_playing) {
if(_voice) {
#ifdef ENABLE_AUDIO_SUPPORT
globals->get_soundmgr()->stop(refname);
globals->get_soundmgr()->remove(refname);
#endif
}
_playing = false;
}
}
// Generate the text of a message from its parameters and the current context.
string FGATC::GenText(const string& m, int c) {
return("");
}
ostream& operator << (ostream& os, atc_type atc) {
switch(atc) {
case(INVALID): return(os << "INVALID");
case(ATIS): return(os << "ATIS");
case(GROUND): return(os << "GROUND");
case(TOWER): return(os << "TOWER");
case(APPROACH): return(os << "APPROACH");
case(DEPARTURE): return(os << "DEPARTURE");
case(ENROUTE): return(os << "ENROUTE");
}
return(os << "ERROR - Unknown switch in atc_type operator << ");
}

View file

@ -1,308 +0,0 @@
// FGATC - abstract base class for the various actual atc classes
// such as FGATIS, FGTower etc.
//
// Written by David Luff, started Feburary 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_ATC_HXX
#define _FG_ATC_HXX
#include <simgear/constants.h>
#include <simgear/compiler.h>
#include <simgear/misc/sgstream.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/debug/logstream.hxx>
#include STL_IOSTREAM
#include STL_STRING
#include "ATCVoice.hxx"
SG_USING_STD(ostream);
SG_USING_STD(string);
SG_USING_STD(ios);
enum plane_type {
UNKNOWN,
GA_SINGLE,
GA_HP_SINGLE,
GA_TWIN,
GA_JET,
MEDIUM,
HEAVY,
MIL_JET
};
// PlaneRec - a structure holding ATC-centric details of planes under control
// This might move or change eventually
struct PlaneRec {
plane_type type;
string callsign;
int squawkcode;
};
// Possible types of ATC type that the radios may be tuned to.
// INVALID implies not tuned in to anything.
enum atc_type {
INVALID,
ATIS,
GROUND,
TOWER,
APPROACH,
DEPARTURE,
ENROUTE
};
const int ATC_NUM_TYPES = 7;
// DCL - new experimental ATC data store
struct ATCData {
atc_type type;
// I've deliberately used float instead of double here to keep the size down - we'll be storing thousands of these in memory.
// In fact, we could probably ditch x, y and z and generate on the fly as needed.
// On the other hand, we'll probably end up reading this data directly from the DAFIF eventually anyway!!
float lon, lat, elev;
float x, y, z;
//int freq;
unsigned short int freq;
//int range;
unsigned short int range;
string ident;
string name;
};
// perhaps we could use an FGRunway instead of this.
// That wouldn't cache the orthopos though.
struct RunwayDetails {
Point3D threshold_pos;
Point3D end1ortho; // ortho projection end1 (the threshold ATM)
Point3D end2ortho; // ortho projection end2 (the take off end in the current hardwired scheme)
double hdg; // true runway heading
double length; // In *METERS*
double width; // ditto
string rwyID;
int patternDirection; // -1 for left, 1 for right
};
ostream& operator << (ostream& os, atc_type atc);
class FGATC {
public:
FGATC();
virtual ~FGATC();
// Run the internal calculations
// Derived classes should call this method from their own Update methods if they
// wish to use the response timer functionality.
virtual void Update(double dt);
// Recieve a coded callback from the ATC menu system based on the user's selection
virtual void ReceiveUserCallback(int code);
// Add plane to a stack
virtual void AddPlane(const string& pid);
// Remove plane from stack
virtual int RemovePlane();
// Indicate that this instance should output to the display if appropriate
inline void SetDisplay() { _display = true; }
// Indicate that this instance should not output to the display
inline void SetNoDisplay() { _display = false; }
// Generate the text of a message from its parameters and the current context.
virtual string GenText(const string& m, int c);
// Returns true if OK to transmit on this frequency
inline bool GetFreqClear() { return freqClear; }
// Indicate that the frequency is in use
inline void SetFreqInUse() { freqClear = false; receiving = true; }
// Transmission to the ATC is finished and a response is required
void SetResponseReqd(const string& rid);
// Transmission finished - let ATC decide if a response is reqd and clear freq if necessary
void NotifyTransmissionFinished(const string& rid);
// Transmission finished and no response required
inline void ReleaseFreq() { freqClear = true; receiving = false; } // TODO - check that the plane releasing the freq is the right one etc.
// The above 3 funcs under development!!
// The idea is that AI traffic or the user ATC dialog box calls FreqInUse() when they begin transmitting,
// and that the tower control sets freqClear back to true following a reply.
// AI traffic should check FreqClear() is true prior to transmitting.
// The user will just have to wait for a gap in dialog as in real life.
// Return the type of ATC station that the class represents
inline atc_type GetType() { return _type; }
// Set the core ATC data
void SetData(ATCData* d);
inline double get_lon() const { return lon; }
inline void set_lon(const double ln) {lon = ln;}
inline double get_lat() const { return lat; }
inline void set_lat(const double lt) {lat = lt;}
inline double get_elev() const { return elev; }
inline void set_elev(const double ev) {elev = ev;}
inline double get_x() const { return x; }
inline void set_x(const double ecs) {x = ecs;}
inline double get_y() const { return y; }
inline void set_y(const double why) {y = why;}
inline double get_z() const { return z; }
inline void set_z(const double zed) {z = zed;}
inline int get_freq() const { return freq; }
inline void set_freq(const int fq) {freq = fq;}
inline int get_range() const { return range; }
inline void set_range(const int rg) {range = rg;}
inline const string& get_ident() { return ident; }
inline void set_ident(const string& id) { ident = id; }
inline const string& get_name() { return name; }
inline void set_name(const string& nm) { name = nm; }
protected:
// Render a transmission
// Outputs the transmission either on screen or as audio depending on user preference
// The refname is a string to identify this sample to the sound manager
// The repeating flag indicates whether the message should be repeated continuously or played once.
void Render(string& msg, const string& refname = "", bool repeating = false);
// Cease rendering all transmission from this station.
// Requires the sound manager refname if audio, else "".
void NoRender(const string& refname);
// Transmit a message when channel becomes free of other dialog
void Transmit(int callback_code = 0);
// Transmit a message if channel becomes free within timeout (seconds). timeout of zero implies no limit
void ConditionalTransmit(double timeout, int callback_code = 0);
// Transmit regardless of other dialog on the channel eg emergency
void ImmediateTransmit(int callback_code = 0);
virtual void ProcessCallback(int code);
double lon, lat, elev;
double x, y, z;
int freq;
int range;
string ident; // Code of the airport its at.
string name; // Name transmitted in the broadcast.
atc_type _type;
// Rendering related stuff
bool _voice; // Flag - true if we are using voice
bool _playing; // Indicates a message in progress
bool _voiceOK; // Flag - true if at least one voice has loaded OK
FGATCVoice* _vPtr;
string pending_transmission; // derived classes set this string before calling Transmit(...)
bool freqClear; // Flag to indicate if the frequency is clear of ongoing dialog
bool receiving; // Flag to indicate we are receiving a transmission
bool responseReqd; // Flag to indicate we should be responding to a request/report
bool runResponseCounter; // Flag to indicate the response counter should be run
double responseTime; // Time to take from end of request transmission to beginning of response
// The idea is that this will be slightly random.
double responseCounter; // counter to implement the above
string responseID; // ID of the plane to respond to
bool respond; // Flag to indicate now is the time to respond - ie set following the count down of the response timer.
// Derived classes only need monitor this flag, and use the response ID, as long as they call FGATC::Update(...)
bool _runReleaseCounter; // A timer for releasing the frequency after giving the message enough time to display
double _releaseTime;
double _releaseCounter;
bool _display; // Flag to indicate whether we should be outputting to the ATC display.
bool _displaying; // Flag to indicate whether we are outputting to the ATC display.
private:
// Transmission timing stuff.
bool _pending;
double _timeout;
int _callback_code; // A callback code to be notified and processed by the derived classes
// A value of zero indicates no callback required
bool _transmit; // we are to transmit
bool _transmitting; // we are transmitting
double _counter;
double _max_count;
};
inline istream&
operator >> ( istream& fin, ATCData& a )
{
double f;
char ch;
char tp;
fin >> tp;
switch(tp) {
case 'I':
a.type = ATIS;
break;
case 'T':
a.type = TOWER;
break;
case 'G':
a.type = GROUND;
break;
case 'A':
a.type = APPROACH;
break;
case '[':
a.type = INVALID;
return fin >> skipeol;
default:
SG_LOG(SG_GENERAL, SG_ALERT, "Warning - unknown type \'" << tp << "\' found whilst reading ATC frequency data!\n");
a.type = INVALID;
return fin >> skipeol;
}
fin >> a.lat >> a.lon >> a.elev >> f >> a.range
>> a.ident;
a.name = "";
fin >> ch;
if(ch != '"') a.name += ch;
while(1) {
//in >> noskipws
fin.unsetf(ios::skipws);
fin >> ch;
if((ch == '"') || (ch == 0x0A)) {
break;
} // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
a.name += ch;
}
fin.setf(ios::skipws);
//cout << "Comm name = " << a.name << '\n';
a.freq = (int)(f*100.0 + 0.5);
// cout << a.ident << endl;
// generate cartesian coordinates
Point3D geod( a.lon * SGD_DEGREES_TO_RADIANS, a.lat * SGD_DEGREES_TO_RADIANS, a.elev );
Point3D cart = sgGeodToCart( geod );
a.x = cart.x();
a.y = cart.y();
a.z = cart.z();
return fin >> skipeol;
}
#endif // _FG_ATC_HXX

View file

@ -1,424 +0,0 @@
// ATCDialog.cxx - Functions and classes to handle the pop-up ATC dialog
//
// Written by Alexander Kappes and David Luff, started February 2003.
//
// Copyright (C) 2003 Alexander Kappes and David Luff
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <simgear/compiler.h>
#include <simgear/structure/commands.hxx>
#include <Main/globals.hxx>
#include <GUI/gui.h> // mkDialog
#include <GUI/new_gui.hxx>
#include "ATCDialog.hxx"
#include "ATC.hxx"
#include "ATCmgr.hxx"
#include "commlist.hxx"
#include "ATCutils.hxx"
#include <Airports/simple.hxx>
#include <sstream>
SG_USING_STD(ostringstream);
FGATCDialog *current_atcdialog;
// For the command manager - maybe eventually this should go in the built in command list
static bool do_ATC_dialog(const SGPropertyNode* arg) {
current_atcdialog->PopupDialog();
return(true);
}
static bool do_ATC_freq_search(const SGPropertyNode* arg) {
current_atcdialog->FreqDialog();
return(true);
}
ATCMenuEntry::ATCMenuEntry() {
stationid = "";
//stationfr = 0;
transmission = "";
menuentry = "";
callback_code = 0;
}
ATCMenuEntry::~ATCMenuEntry() {
}
static void atcUppercase(string &s) {
for(unsigned int i=0; i<s.size(); ++i) {
s[i] = toupper(s[i]);
}
}
// find child whose <name>...</name> entry matches 'name'
static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
SGPropertyNode* p;
for (int i = 0; i < prop->nChildren(); i++)
if ((p = getNamedNode(prop->getChild(i), name)))
return p;
if (!strcmp(prop->getStringValue("name"), name))
return prop;
return 0;
}
FGATCDialog::FGATCDialog() {
_callbackPending = false;
_callbackTimer = 0.0;
_callbackWait = 0.0;
_callbackPtr = NULL;
_callbackCode = 0;
_gui = (NewGUI *)globals->get_subsystem("gui");
}
FGATCDialog::~FGATCDialog() {
}
void FGATCDialog::Init() {
// Add ATC-dialog to the command list
globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
// Add ATC-freq-search to the command list
globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
// initialize properties polled in Update()
globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
}
void FGATCDialog::Update(double dt) {
static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
string s = airport->getStringValue();
if (!s.empty()) {
airport->setStringValue("");
FreqDisplay(s);
}
static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
int n = trans_num->getIntValue();
if (n >= 0) {
trans_num->setIntValue(-1);
PopupCallback(n);
}
if(_callbackPending) {
if(_callbackTimer > _callbackWait) {
_callbackPtr->ReceiveUserCallback(_callbackCode);
_callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
_callbackPending = false;
} else {
_callbackTimer += dt;
}
}
}
// Add an entry
void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
ATCMenuEntry a;
a.stationid = station;
a.transmission = transmission;
a.menuentry = menutext;
a.callback_code = code;
(available_dialog[type])[station.c_str()].push_back(a);
}
void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
atcmentry_vec_type* p = &((available_dialog[type])[station]);
atcmentry_vec_iterator current = p->begin();
while(current != p->end()) {
if(current->transmission == trans) current = p->erase(current);
else ++current;
}
}
void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
atcmentry_vec_type* p = &((available_dialog[type])[station]);
atcmentry_vec_iterator current = p->begin();
while(current != p->end()) {
if(current->callback_code == code) current = p->erase(current);
else ++current;
}
}
// query the database whether the transmission is already registered;
bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
atcmentry_vec_type* p = &((available_dialog[type])[station]);
atcmentry_vec_iterator current = p->begin();
for ( ; current != p->end() ; ++current ) {
if ( current->transmission == trans ) return true;
}
return false;
}
// query the database whether the transmission is already registered;
bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
atcmentry_vec_type* p = &((available_dialog[type])[station]);
atcmentry_vec_iterator current = p->begin();
for ( ; current != p->end() ; ++current ) {
if ( current->callback_code == code ) return true;
}
return false;
}
// Display the ATC popup dialog box with options relevant to the users current situation.
void FGATCDialog::PopupDialog() {
const char *dialog_name = "atc-dialog";
SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
if (!dlg)
return;
_gui->closeDialog(dialog_name);
SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
// remove all transmission buttons
button_group->removeChildren("button", false);
string label;
FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
if (!atcptr) {
label = "Not currently tuned to any ATC service";
mkDialog(label.c_str());
return;
}
if(atcptr->GetType() == ATIS) {
label = "Tuned to ATIS - no communication possible";
mkDialog(label.c_str());
return;
}
atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
atcmentry_vec_iterator current = atcmlist.begin();
atcmentry_vec_iterator last = atcmlist.end();
if(!atcmlist.size()) {
label = "No transmission available";
mkDialog(label.c_str());
return;
}
const int bufsize = 32;
char buf[bufsize];
// loop over all entries in atcmentrylist
for (int n = 0; n < 10; ++n) {
snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
fgSetBool(buf, false);
if (current == last)
continue;
// add transmission button (modified copy of <button-template>)
SGPropertyNode *entry = button_group->getNode("button", n, true);
copyProperties(button_group->getNode("button-template", true), entry);
entry->removeChildren("hide", false);
entry->setStringValue("property", buf);
entry->setIntValue("keynum", '1' + n);
if (n == 0)
entry->setBoolValue("default", true);
snprintf(buf, bufsize, "%d", n + 1);
string legend = string(buf) + ". " + current->menuentry;
entry->setStringValue("legend", legend.c_str());
entry->setIntValue("binding/value", n);
current++;
}
_gui->showDialog(dialog_name);
return;
}
void FGATCDialog::PopupCallback(int num) {
FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
if (!atcptr)
return;
if (atcptr->GetType() == TOWER) {
//cout << "TOWER " << endl;
//cout << "ident is " << atcptr->get_ident() << endl;
atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
unsigned int size = atcmlist.size();
if(size && num < size) {
//cout << "Doing callback...\n";
ATCMenuEntry a = atcmlist[num];
atcptr->SetFreqInUse();
string pilot = atcptr->GenText(a.transmission, a.callback_code);
fgSetString("/sim/messages/pilot", pilot.c_str());
// This is the user's speech getting displayed.
_callbackPending = true;
_callbackTimer = 0.0;
_callbackWait = 5.0;
_callbackPtr = atcptr;
_callbackCode = a.callback_code;
} else {
//cout << "No options available...\n";
}
//cout << "Donded" << endl;
}
}
// map() key data type (removes duplicates and sorts by distance)
struct atcdata {
atcdata() {}
atcdata(const string i, const string n, const double d) {
id = i, name = n, distance = d;
}
bool operator<(const atcdata& a) const {
return id != a.id && distance < a.distance;
}
bool operator==(const atcdata& a) const {
return id == a.id && distance == a.distance;
}
string id;
string name;
double distance;
};
void FGATCDialog::FreqDialog() {
const char *dialog_name = "atc-freq-search";
SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
if (!dlg)
return;
_gui->closeDialog(dialog_name);
SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
// remove all dynamic airport/ATC buttons
button_group->removeChildren("button", false);
// Find the ATC stations within a reasonable range
comm_list_type atc_stations;
comm_list_iterator atc_stat_itr;
double lon = fgGetDouble("/position/longitude-deg");
double lat = fgGetDouble("/position/latitude-deg");
double elev = fgGetDouble("/position/altitude-ft");
Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
lat * SGD_DEGREES_TO_RADIANS, elev));
// search stations in range
int num_stat = current_commlist->FindByPos(lon, lat, elev, 50.0, &atc_stations);
if (num_stat != 0) {
map<atcdata, bool> uniq;
// fill map (sorts by distance and removes duplicates)
comm_list_iterator itr = atc_stations.begin();
for (; itr != atc_stations.end(); ++itr) {
Point3D station = Point3D(itr->x, itr->y, itr->z);
double distance = aircraft.distance3Dsquared(station);
uniq[atcdata(itr->ident, itr->name, distance)] = true;
}
// create button per map entry (modified copy of <button-template>)
map<atcdata, bool>::iterator uit = uniq.begin();
for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
SGPropertyNode *entry = button_group->getNode("button", n, true);
copyProperties(button_group->getNode("button-template", true), entry);
entry->removeChildren("hide", false);
entry->setStringValue("legend", uit->first.id.c_str());
entry->setStringValue("binding[0]/value", uit->first.id.c_str());
}
}
// (un)hide message saying no things in range
SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
range_error->setBoolValue("hide", num_stat);
_gui->showDialog(dialog_name);
}
void FGATCDialog::FreqDisplay(string& ident) {
const char *dialog_name = "atc-freq-display";
SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
if (!dlg)
return;
_gui->closeDialog(dialog_name);
SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
// remove all frequency entries
freq_group->removeChildren("group", false);
atcUppercase(ident);
string label;
const FGAirport *a = fgFindAirportID(ident);
if (!a) {
label = "Airport " + ident + " not found in database.";
mkDialog(label.c_str());
return;
}
// set title
label = ident + " Frequencies";
dlg->setStringValue("text/label", label.c_str());
int n = 0; // Number of ATC frequencies at this airport
comm_list_type stations;
int found = current_commlist->FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 20.0, &stations);
if(found) {
ostringstream ostr;
comm_list_iterator itr = stations.begin();
for (n = 0; itr != stations.end(); ++itr) {
if(itr->ident != ident)
continue;
if(itr->type == INVALID)
continue;
// add frequency line (modified copy of <group-template>)
SGPropertyNode *entry = freq_group->getNode("group", n, true);
copyProperties(freq_group->getNode("group-template", true), entry);
entry->removeChildren("hide", false);
ostr << itr->type;
entry->setStringValue("text[0]/label", ostr.str().c_str());
char buf[8];
snprintf(buf, 8, "%.2f", (itr->freq / 100.0)); // Convert from KHz to MHz
if(buf[5] == '3') buf[5] = '2';
if(buf[5] == '8') buf[5] = '7';
buf[7] = '\0';
entry->setStringValue("text[1]/label", buf);
ostr.seekp(0);
n++;
}
}
if(n == 0) {
label = "No frequencies found for airport " + ident;
mkDialog(label.c_str());
return;
}
_gui->showDialog(dialog_name);
}

View file

@ -1,115 +0,0 @@
// ATCDialog.hxx - Functions and classes to handle the pop-up ATC dialog
//
// Written by Alexander Kappes and David Luff, started February 2003.
//
// Copyright (C) 2003 Alexander Kappes and David Luff
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef ATC_DIALOG_HXX
#define ATC_DIALOG_HXX
#include <simgear/compiler.h>
#include <vector>
#include <map>
#include "ATC.hxx"
SG_USING_STD(vector);
SG_USING_STD(map);
class NewGUI;
// ATCMenuEntry - an encapsulation of an entry in the ATC dialog
struct ATCMenuEntry {
string stationid; // ID of transmitting station
//int stationfr; // ?
string transmission; // Actual speech of transmission
string menuentry; // Shortened version for display in the dialog
int callback_code; // This code is supplied by the registering station, and then
// returned to the registering station if that option is chosen.
// The actual value is only understood by the registering station -
// FGATCDialog only stores it and returns it if appropriate.
ATCMenuEntry();
~ATCMenuEntry();
};
typedef vector < ATCMenuEntry > atcmentry_vec_type;
typedef atcmentry_vec_type::iterator atcmentry_vec_iterator;
typedef map < string, atcmentry_vec_type > atcmentry_map_type;
typedef atcmentry_map_type::iterator atcmentry_map_iterator;
//void ATCDialogInit();
//void ATCDoDialog(atc_type type);
class FGATCDialog {
public:
FGATCDialog();
~FGATCDialog();
void Init();
void Update(double dt);
void PopupDialog();
void PopupCallback(int);
void add_entry( const string& station, const string& transmission, const string& menutext, atc_type type, int code);
void remove_entry( const string &station, const string &trans, atc_type type );
void remove_entry( const string &station, int code, atc_type type );
// query the database whether the transmission is already registered;
bool trans_reg( const string &station, const string &trans, atc_type type );
// query the database whether the transmission is already registered;
bool trans_reg( const string &station, int code, atc_type type );
// Display a frequency search dialog for nearby stations
void FreqDialog();
// Display the comm ATC frequencies for airport ident
// where ident is a valid ICAO code.
void FreqDisplay(string& ident);
private:
atcmentry_map_type available_dialog[ATC_NUM_TYPES];
int freq;
bool reset;
bool _callbackPending;
double _callbackTimer;
double _callbackWait;
FGATC* _callbackPtr;
int _callbackCode;
NewGUI *_gui;
};
extern FGATCDialog *current_atcdialog;
#endif // ATC_DIALOG_HXX

View file

@ -1,118 +0,0 @@
// ATCProjection.cxx - A convienience projection class for the ATC/AI system.
//
// Written by David Luff, started 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "ATCProjection.hxx"
#include <math.h>
#include <simgear/constants.h>
FGATCProjection::FGATCProjection() {
_origin.setlat(0.0);
_origin.setlon(0.0);
_origin.setelev(0.0);
_correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
}
FGATCProjection::FGATCProjection(const Point3D& centre) {
_origin = centre;
_correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
}
FGATCProjection::~FGATCProjection() {
}
void FGATCProjection::Init(const Point3D& centre) {
_origin = centre;
_correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
}
Point3D FGATCProjection::ConvertToLocal(const Point3D& pt) {
double delta_lat = pt.lat() - _origin.lat();
double delta_lon = pt.lon() - _origin.lon();
double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
return(Point3D(x,y,0.0));
}
Point3D FGATCProjection::ConvertFromLocal(const Point3D& pt) {
double delta_lat = asin(pt.y() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
double delta_lon = (asin(pt.x() / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, 0.0));
}
/**********************************************************************************/
FGATCAlignedProjection::FGATCAlignedProjection() {
_origin.setlat(0.0);
_origin.setlon(0.0);
_origin.setelev(0.0);
_correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
}
FGATCAlignedProjection::FGATCAlignedProjection(const Point3D& centre, double heading) {
_origin = centre;
_theta = heading * SG_DEGREES_TO_RADIANS;
_correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
}
FGATCAlignedProjection::~FGATCAlignedProjection() {
}
void FGATCAlignedProjection::Init(const Point3D& centre, double heading) {
_origin = centre;
_theta = heading * SG_DEGREES_TO_RADIANS;
_correction_factor = cos(_origin.lat() * SG_DEGREES_TO_RADIANS);
}
Point3D FGATCAlignedProjection::ConvertToLocal(const Point3D& pt) {
// convert from lat/lon to orthogonal
double delta_lat = pt.lat() - _origin.lat();
double delta_lon = pt.lon() - _origin.lon();
double y = sin(delta_lat * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M;
double x = sin(delta_lon * SG_DEGREES_TO_RADIANS) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
// Align
if(_theta != 0.0) {
double xbar = x;
x = x*cos(_theta) - y*sin(_theta);
y = (xbar*sin(_theta)) + (y*cos(_theta));
}
return(Point3D(x,y,pt.elev()));
}
Point3D FGATCAlignedProjection::ConvertFromLocal(const Point3D& pt) {
// de-align
double thi = _theta * -1.0;
double x = pt.x()*cos(thi) - pt.y()*sin(thi);
double y = (pt.x()*sin(thi)) + (pt.y()*cos(thi));
// convert from orthogonal to lat/lon
double delta_lat = asin(y / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES;
double delta_lon = (asin(x / SG_EQUATORIAL_RADIUS_M) * SG_RADIANS_TO_DEGREES) / _correction_factor;
return(Point3D(_origin.lon()+delta_lon, _origin.lat()+delta_lat, pt.elev()));
}

View file

@ -1,73 +0,0 @@
// ATCProjection.hxx - A convienience projection class for the ATC/AI system.
//
// Written by David Luff, started 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_ATC_PROJECTION_HXX
#define _FG_ATC_PROJECTION_HXX
#include <simgear/math/point3d.hxx>
// FGATCProjection - a class to project an area local to an airport onto an orthogonal co-ordinate system
class FGATCProjection {
public:
FGATCProjection();
FGATCProjection(const Point3D& centre);
~FGATCProjection();
void Init(const Point3D& centre);
// Convert a lat/lon co-ordinate (degrees) to the local projection (meters)
Point3D ConvertToLocal(const Point3D& pt);
// Convert a local projection co-ordinate (meters) to lat/lon (degrees)
Point3D ConvertFromLocal(const Point3D& pt);
private:
Point3D _origin; // lat/lon of local area origin
double _correction_factor; // Reduction in surface distance per degree of longitude due to latitude. Saves having to do a cos() every call.
};
// FGATCAlignedProjection - a class to project an area local to a runway onto an orthogonal co-ordinate system
// with the origin at the threshold and the runway aligned with the y axis.
class FGATCAlignedProjection {
public:
FGATCAlignedProjection();
FGATCAlignedProjection(const Point3D& centre, double heading);
~FGATCAlignedProjection();
void Init(const Point3D& centre, double heading);
// Convert a lat/lon co-ordinate (degrees) to the local projection (meters)
Point3D ConvertToLocal(const Point3D& pt);
// Convert a local projection co-ordinate (meters) to lat/lon (degrees)
Point3D ConvertFromLocal(const Point3D& pt);
private:
Point3D _origin; // lat/lon of local area origin (the threshold)
double _theta; // the rotation angle for alignment in radians
double _correction_factor; // Reduction in surface distance per degree of longitude due to latitude. Saves having to do a cos() every call.
};
#endif // _FG_ATC_PROJECTION_HXX

View file

@ -1,191 +0,0 @@
// FGATCVoice.cxx - a class to encapsulate an ATC voice
//
// Written by David Luff, started November 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/misc/sg_path.hxx>
#include <simgear/debug/logstream.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/math/sg_random.h>
#include <Main/globals.hxx>
#include "ATCVoice.hxx"
#include <stdlib.h>
FGATCVoice::FGATCVoice() {
SoundData = 0;
rawSoundData = 0;
}
FGATCVoice::~FGATCVoice() {
if (rawSoundData)
free( rawSoundData );
delete SoundData;
}
// Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
// Return true if successful.
bool FGATCVoice::LoadVoice(const string& voice) {
// FIXME CLO: disabled to try to see if this is causign problemcs
// return false;
ifstream fin;
SGPath path = globals->get_fg_root();
path.append( "ATC" );
string file = voice + ".wav";
SoundData = new SGSoundSample();
rawSoundData = (char *)SoundData->load_file(path.c_str(), file.c_str());
rawDataSize = SoundData->get_size();
path = globals->get_fg_root();
string wordPath = "ATC/" + voice + ".vce";
path.append(wordPath);
// Now load the word data
fin.open(path.c_str(), ios::in);
if(!fin) {
SG_LOG(SG_ATC, SG_ALERT, "Unable to open input file " << path.c_str());
return(false);
}
SG_LOG(SG_ATC, SG_INFO, "Opened word data file " << wordPath << " OK...");
char numwds[10];
char wrd[100];
string wrdstr;
char wrdOffsetStr[20];
char wrdLengthStr[20];
unsigned int wrdOffset; // Offset into the raw sound data that the word sample begins
unsigned int wrdLength; // Length of the word sample in bytes
WordData wd;
fin >> numwds;
unsigned int numwords = atoi(numwds);
//cout << numwords << '\n';
for(unsigned int i=0; i < numwords; ++i) {
fin >> wrd;
wrdstr = wrd;
fin >> wrdOffsetStr;
fin >> wrdLengthStr;
wrdOffset = atoi(wrdOffsetStr);
wrdLength = atoi(wrdLengthStr);
wd.offset = wrdOffset;
wd.length = wrdLength;
wordMap[wrdstr] = wd;
//cout << wrd << "\t\t" << wrdOffset << "\t\t" << wrdLength << '\n';
//cout << i << '\n';
}
fin.close();
return(true);
}
typedef list < string > tokenList_type;
typedef tokenList_type::iterator tokenList_iterator;
// Given a desired message, return a pointer to the data buffer and write the buffer length into len.
unsigned char* FGATCVoice::WriteMessage(char* message, int& len, bool& dataOK) {
// What should we do here?
// First - parse the message into a list of tokens.
// Sort the tokens into those we understand and those we don't.
// Add all the raw lengths of the token sound data, allocate enough space, and fill it with the rqd data.
tokenList_type tokenList;
tokenList_iterator tokenListItr;
// TODO - at the moment we're effectively taking 3 passes through the data.
// There is no need for this - 2 should be sufficient - we can probably ditch the tokenList.
char* token;
char mes[1000];
int numWords = 0;
strcpy(mes, message);
const char delimiters[] = " \t.,;:\"";
token = strtok(mes, delimiters);
while(token != NULL) {
tokenList.push_back(token);
++numWords;
//cout << "token = " << token << '\n';
token = strtok(NULL, delimiters);
}
WordData* wdptr = new WordData[numWords];
int word = 0;
unsigned int cumLength = 0;
tokenListItr = tokenList.begin();
while(tokenListItr != tokenList.end()) {
if(wordMap.find(*tokenListItr) == wordMap.end()) {
// Oh dear - the token isn't in the sound file
//cout << "word " << *tokenListItr << " not found :-(\n";
} else {
wdptr[word] = wordMap[*tokenListItr];
cumLength += wdptr[word].length;
//cout << *tokenListItr << " found at offset " << wdptr[word].offset << " with length " << wdptr[word].length << endl;
word++;
}
++tokenListItr;
}
// Check for no tokens found else slScheduler can be crashed
if(!word) {
dataOK = false;
return(NULL);
}
unsigned char* tmpbuf = new unsigned char[cumLength];
unsigned char* outbuf = new unsigned char[cumLength];
len = cumLength;
unsigned int bufpos = 0;
for(int i=0; i<word; ++i) {
/*
* Sanity check for corrupt/mismatched sound data input - avoids a seg fault
* (As long as the calling function checks the return value!!)
* This check should be left in even when the default Flightgear files are known
* to be OK since it checks for mis-indexing of voice files by 3rd party developers.
*/
if((wdptr[i].offset + wdptr[i].length) > rawDataSize) {
SG_LOG(SG_ATC, SG_ALERT, "ERROR - mismatch between ATC .wav and .vce file in ATCVoice.cxx\n");
SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length
<< " exceeds rawdata size: " << rawDataSize << endl);
delete[] wdptr;
dataOK = false;
return(NULL);
}
memcpy(tmpbuf + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length);
bufpos += wdptr[i].length;
}
// tmpbuf now contains the message starting at the beginning - but we want it to start at a random position.
unsigned int offsetIn = (int)(cumLength * sg_random());
if(offsetIn > cumLength) offsetIn = cumLength;
memcpy(outbuf, tmpbuf + offsetIn, (cumLength - offsetIn));
memcpy(outbuf + (cumLength - offsetIn), tmpbuf, offsetIn);
delete[] tmpbuf;
delete[] wdptr;
dataOK = true;
return(outbuf);
}

View file

@ -1,87 +0,0 @@
// FGATCVoice.hxx - a class to encapsulate an ATC voice
//
// Written by David Luff, started November 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_ATC_VOICE
#define _FG_ATC_VOICE
#include <simgear/compiler.h>
#if defined( SG_HAVE_STD_INCLUDES ) || defined( __BORLANDC__ ) || (__APPLE__)
# include <fstream>
# include <iostream>
#else
# include <fstream.h>
# include <iostream.h>
#endif
#include <map>
#include <list>
#include <string>
#include <simgear/sound/sample_openal.hxx>
SG_USING_STD(map);
SG_USING_STD(list);
SG_USING_STD(string);
SG_USING_STD(cout);
SG_USING_STD(ios);
SG_USING_STD(ofstream);
SG_USING_STD(ifstream);
struct WordData {
unsigned int offset; // Offset of beginning of word sample into raw sound sample
unsigned int length; // Byte length of word sample
};
typedef map < string, WordData > atc_word_map_type;
typedef atc_word_map_type::iterator atc_word_map_iterator;
typedef atc_word_map_type::const_iterator atc_word_map_const_iterator;
class FGATCVoice {
public:
FGATCVoice();
~FGATCVoice();
// Load the two voice files - one containing the raw sound data (.wav) and one containing the word positions (.vce).
// Return true if successful.
bool LoadVoice(const string& voice);
// Given a desired message, return a pointer to the data buffer and write the buffer length into len.
// Sets dataOK = true if the returned buffer is valid.
unsigned char* WriteMessage(char* message, int& len, bool& dataOK);
private:
// the sound and word position data
char* rawSoundData;
unsigned int rawDataSize;
SGSoundSample *SoundData;
// A map of words vs. byte position and length in rawSoundData
atc_word_map_type wordMap;
};
#endif // _FG_ATC_VOICE

View file

@ -1,723 +0,0 @@
// ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
//
// Written by David Luff, started February 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/misc/sg_path.hxx>
#include <simgear/debug/logstream.hxx>
#include <Airports/simple.hxx>
#include "ATCmgr.hxx"
#include "commlist.hxx"
#include "ATCDialog.hxx"
#include "ATCutils.hxx"
#include "transmissionlist.hxx"
#include "ground.hxx"
/*
// periodic radio station search wrapper
static void fgATCSearch( void ) {
globals->get_ATC_mgr()->Search();
}
*/ //This wouldn't compile - including Time/event.hxx breaks it :-(
// Is this still true?? -EMH-
AirportATC::AirportATC() :
lon(0.0),
lat(0.0),
elev(0.0),
atis_freq(0.0),
atis_active(false),
tower_freq(0.0),
tower_active(false),
ground_freq(0.0),
ground_active(false),
set_by_AI(false),
numAI(0)
//airport_atc_map.clear();
{
for(int i=0; i<ATC_NUM_TYPES; ++i) {
set_by_comm[0][i] = false;
set_by_comm[1][i] = false;
}
}
FGATCMgr::FGATCMgr() {
comm_ident[0] = "";
comm_ident[1] = "";
//last_comm_ident[0] = "";
//last_comm_ident[1] = "";
//approach_ident = "";
last_in_range = false;
comm_type[0] = INVALID;
comm_type[1] = INVALID;
comm_atc_ptr[0] = NULL;
comm_atc_ptr[1] = NULL;
comm_valid[0] = false;
comm_valid[1] = false;
initDone = false;
}
FGATCMgr::~FGATCMgr() {
delete v1;
}
void FGATCMgr::bind() {
}
void FGATCMgr::unbind() {
}
void FGATCMgr::init() {
//cout << "ATCMgr::init called..." << endl;
comm_node[0] = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
comm_node[1] = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
lon_node = fgGetNode("/position/longitude-deg", true);
lat_node = fgGetNode("/position/latitude-deg", true);
elev_node = fgGetNode("/position/altitude-ft", true);
atc_list_itr = atc_list.begin();
// Search for connected ATC stations once per 0.8 seconds or so
// globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
// FGEvent::FG_EVENT_READY, 800);
//
// For some reason the above doesn't compile - including Time/event.hxx stops compilation.
// Is this still true after the reorganization of the event managar??
// -EMH-
// Initialise the frequency search map
current_commlist = new FGCommList;
SGPath p_comm( globals->get_fg_root() );
current_commlist->init( p_comm );
#ifdef ENABLE_AUDIO_SUPPORT
// Load all available voices.
// For now we'll do one hardwired one
v1 = new FGATCVoice;
try {
voiceOK = v1->LoadVoice("default");
voice = true;
} catch ( sg_io_exception & ) {
voiceOK = false;
voice = false;
delete v1;
v1 = 0;
}
/* I've loaded the voice even if /sim/sound/pause is true
* since I know no way of forcing load of the voice if the user
* subsequently switches /sim/sound/audible to true.
* (which is the right thing to do -- CLO) :-) */
#else
voice = false;
#endif
// Initialise the ATC Dialogs
//cout << "Initing Transmissions..." << endl;
SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
current_transmissionlist = new FGTransmissionList;
SGPath p_transmission( globals->get_fg_root() );
p_transmission.append( "ATC/default.transmissions" );
current_transmissionlist->init( p_transmission );
//cout << "Done Transmissions" << endl;
SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
current_atcdialog = new FGATCDialog;
current_atcdialog->Init();
initDone = true;
//cout << "ATCmgr::init done!" << endl;
}
void FGATCMgr::update(double dt) {
if(!initDone) {
init();
SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
}
current_atcdialog->Update(dt);
//cout << "Entering update..." << endl;
//Traverse the list of active stations.
//Only update one class per update step to avoid the whole ATC system having to calculate between frames.
//Eventually we should only update every so many steps.
//cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << endl;
if(atc_list.size()) {
if(atc_list_itr == atc_list.end()) {
atc_list_itr = atc_list.begin();
}
//cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
//cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
(*atc_list_itr)->Update(dt * atc_list.size());
//cout << "Done ATC update..." << endl;
++atc_list_itr;
}
/*
cout << "ATC_LIST: " << atc_list.size() << ' ';
for(atc_list_iterator it = atc_list.begin(); it != atc_list.end(); it++) {
cout << (*it)->get_ident() << ' ';
}
cout << '\n';
*/
// Search the tuned frequencies every now and then - this should be done with the event scheduler
static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
/*
if(i == 7) {
//cout << "About to AreaSearch()" << endl;
AreaSearch();
}
*/
if(i == 15) {
//cout << "About to search(1)" << endl;
FreqSearch(1);
}
if(i == 30) {
//cout << "About to search(2)" << endl;
FreqSearch(2);
i = 0;
}
++i;
//cout << "comm1 type = " << comm_type[0] << '\n';
//cout << "Leaving update..." << endl;
}
// Returns frequency in KHz - should I alter this to return in MHz?
unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
ATCData test;
bool ok = current_commlist->FindByCode(ident, test, tp);
return(ok ? test.freq : 0);
}
// Register the fact that the AI system wants to activate an airport
// Might need more sophistication in this in the future - eg registration by aircraft call-sign.
bool FGATCMgr::AIRegisterAirport(const string& ident) {
SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
//cout << "AI registered airport " << ident << " with the ATC system" << '\n';
if(airport_atc_map.find(ident) != airport_atc_map.end()) {
airport_atc_map[ident]->set_by_AI = true;
airport_atc_map[ident]->numAI++;
return(true);
} else {
const FGAirport *ap = fgFindAirportID(ident);
if (ap) {
//cout << "ident = " << ident << '\n';
AirportATC *a = new AirportATC;
// I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
a->lon = ap->getLongitude();
a->lat = ap->getLatitude();
a->elev = ap->getElevation();
a->atis_freq = GetFrequency(ident, ATIS);
//cout << "ATIS freq = " << a->atis_freq << '\n';
a->atis_active = false;
a->tower_freq = GetFrequency(ident, TOWER);
//cout << "Tower freq = " << a->tower_freq << '\n';
a->tower_active = false;
a->ground_freq = GetFrequency(ident, GROUND);
//cout << "Ground freq = " << a->ground_freq << '\n';
a->ground_active = false;
// TODO - some airports will have a tower/ground frequency but be inactive overnight.
a->set_by_AI = true;
a->numAI = 1;
airport_atc_map[ident] = a;
return(true);
} else {
SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't find airport " << ident << " in AIRegisterAirport(...)");
}
}
return(false);
}
// Register the fact that the comm radio is tuned to an airport
// Channel is zero based
bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
//cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
if(airport_atc_map.find(ident) != airport_atc_map.end()) {
//cout << "IN MAP - flagging set by comm..." << endl;
airport_atc_map[ident]->set_by_comm[chan][tp] = true;
if(tp == ATIS) {
airport_atc_map[ident]->atis_active = true;
} else if(tp == TOWER) {
airport_atc_map[ident]->tower_active = true;
} else if(tp == GROUND) {
airport_atc_map[ident]->ground_active = true;
} else if(tp == APPROACH) {
//a->approach_active = true;
} // TODO - there *must* be a better way to do this!!!
return(true);
} else {
//cout << "NOT IN MAP - creating new..." << endl;
const FGAirport *ap = fgFindAirportID(ident);
if (ap) {
AirportATC *a = new AirportATC;
// I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
a->lon = ap->getLongitude();
a->lat = ap->getLatitude();
a->elev = ap->getElevation();
a->atis_freq = GetFrequency(ident, ATIS);
a->atis_active = false;
a->tower_freq = GetFrequency(ident, TOWER);
a->tower_active = false;
a->ground_freq = GetFrequency(ident, GROUND);
a->ground_active = false;
if(tp == ATIS) {
a->atis_active = true;
} else if(tp == TOWER) {
a->tower_active = true;
} else if(tp == GROUND) {
a->ground_active = true;
} else if(tp == APPROACH) {
//a->approach_active = true;
} // TODO - there *must* be a better way to do this!!!
// TODO - some airports will have a tower/ground frequency but be inactive overnight.
a->set_by_AI = false;
a->numAI = 0;
a->set_by_comm[chan][tp] = true;
airport_atc_map[ident] = a;
return(true);
}
}
return(false);
}
// Remove from list only if not needed by the AI system or the other comm channel
// Note that chan is zero based.
void FGATCMgr::CommRemoveFromList(const string& id, const atc_type& tp, int chan) {
SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
//cout << "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan << '\n';
if(airport_atc_map.find(id) != airport_atc_map.end()) {
AirportATC* a = airport_atc_map[id];
//cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
if(a->set_by_AI && tp != ATIS) {
// Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
FGATC* aptr = GetATCPointer(id, tp);
switch(chan) {
case 0:
//cout << "chan 1\n";
a->set_by_comm[0][tp] = false;
if(!a->set_by_comm[1][tp]) {
//cout << "not set by comm2\n";
if(aptr != NULL) {
//cout << "Got pointer\n";
aptr->SetNoDisplay();
//cout << "Setting no display...\n";
} else {
//cout << "Not got pointer\n";
}
}
break;
case 1:
a->set_by_comm[1][tp] = false;
if(!a->set_by_comm[0][tp]) {
if(aptr != NULL) {
aptr->SetNoDisplay();
//cout << "Setting no display...\n";
}
}
break;
}
//airport_atc_map[id] = a;
return;
} else {
switch(chan) {
case 0:
a->set_by_comm[0][tp] = false;
// Remove only if not also set by the other comm channel
if(!a->set_by_comm[1][tp]) {
a->tower_active = false;
a->ground_active = false;
RemoveFromList(id, tp);
}
break;
case 1:
a->set_by_comm[1][tp] = false;
if(!a->set_by_comm[0][tp]) {
a->tower_active = false;
a->ground_active = false;
RemoveFromList(id, tp);
}
break;
}
}
}
}
// Remove from list - should only be called from above or similar
// This function *will* remove it from the list regardless of who else might want it.
void FGATCMgr::RemoveFromList(const string& id, const atc_type& tp) {
//cout << "FGATCMgr::RemoveFromList called..." << endl;
//cout << "Requested type = " << tp << endl;
//cout << "id = " << id << endl;
atc_list_iterator it = atc_list.begin();
while(it != atc_list.end()) {
//cout << "type = " << (*it)->GetType() << '\n';
//cout << "Ident = " << (*it)->get_ident() << '\n';
if( ((*it)->get_ident() == id)
&& ((*it)->GetType() == tp) ) {
//Before removing it stop it transmitting!!
//cout << "OBLITERATING FROM LIST!!!\n";
(*it)->SetNoDisplay();
(*it)->Update(0.00833);
delete (*it);
atc_list.erase(it);
atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
break;
}
++it;
}
}
// Find in list - return a currently active ATC pointer given ICAO code and type
// Return NULL if the given service is not in the list
// - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
//cout << "Entering FindInList for " << id << ' ' << tp << endl;
atc_list_iterator it = atc_list.begin();
while(it != atc_list.end()) {
if( ((*it)->get_ident() == id)
&& ((*it)->GetType() == tp) ) {
return(*it);
}
++it;
}
// If we get here it's not in the list
//cout << "Couldn't find it in the list though :-(" << endl;
return(NULL);
}
// Returns true if the airport is found in the map
bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
if(airport_atc_map.find(icao) != airport_atc_map.end()) {
*a = *airport_atc_map[icao];
return(true);
} else {
return(false);
}
}
// Return a pointer to a given sort of ATC at a given airport and activate if necessary
// Returns NULL if service doesn't exist - calling function should check for this.
// We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
// - at the moment all these GetATC... functions exposed are just too complicated.
FGATC* FGATCMgr::GetATCPointer(const string& icao, const atc_type& type) {
if(airport_atc_map.find(icao) == airport_atc_map.end()) {
//cout << "Unable to find " << icao << ' ' << type << " in the airport_atc_map" << endl;
return NULL;
}
//cout << "In GetATCPointer, found " << icao << ' ' << type << endl;
AirportATC *a = airport_atc_map[icao];
//cout << "a->lon = " << a->lon << '\n';
//cout << "a->elev = " << a->elev << '\n';
//cout << "a->tower_freq = " << a->tower_freq << '\n';
switch(type) {
case TOWER:
if(a->tower_active) {
// Get the pointer from the list
return(FindInList(icao, type));
} else {
ATCData data;
if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
FGTower* t = new FGTower;
t->SetData(&data);
atc_list.push_back(t);
a->tower_active = true;
airport_atc_map[icao] = a;
//cout << "Initing tower " << icao << " in GetATCPointer()\n";
t->Init();
return(t);
} else {
SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
}
}
break;
case APPROACH:
break;
case ATIS:
SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
break;
case GROUND:
//cout << "IN CASE GROUND" << endl;
if(a->ground_active) {
// Get the pointer from the list
return(FindInList(icao, type));
} else {
ATCData data;
if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
FGGround* g = new FGGround;
g->SetData(&data);
atc_list.push_back(g);
a->ground_active = true;
airport_atc_map[icao] = a;
g->Init();
return(g);
} else {
SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
}
}
break;
case INVALID:
break;
case ENROUTE:
break;
case DEPARTURE:
break;
}
SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
//cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
return(NULL);
}
// Return a pointer to an appropriate voice for a given type of ATC
// creating the voice if necessary - ie. make sure exactly one copy
// of every voice in use exists in memory.
//
// TODO - in the future this will get more complex and dole out country/airport
// specific voices, and possible make sure that the same voice doesn't get used
// at different airports in quick succession if a large enough selection are available.
FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
// TODO - implement me better - maintain a list of loaded voices and other voices!!
if(voice) {
switch(type) {
case ATIS:
if(voiceOK) {
return(v1);
}
case TOWER:
return(NULL);
case APPROACH:
return(NULL);
case GROUND:
return(NULL);
default:
return(NULL);
}
return(NULL);
} else {
return(NULL);
}
}
// Search for ATC stations by frequency
void FGATCMgr::FreqSearch(int channel) {
int chan = channel - 1; // Convert to zero-based for the arrays
ATCData data;
double freq = comm_node[chan]->getDoubleValue();
lon = lon_node->getDoubleValue();
lat = lat_node->getDoubleValue();
elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
// Query the data store and get the closest match if any
if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
// We have a match
// What's the logic?
// If this channel not previously valid then easy - add ATC to list
// If this channel was valid then - Have we tuned to a different service?
// If so - de-register one and add the other
if(comm_valid[chan]) {
if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
// Then we're still tuned into the same service so do nought and return
return;
} else {
// Something's changed - either the location or the service type
// We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
}
}
// At this point we can assume that we need to add the service.
comm_ident[chan] = data.ident;
comm_type[chan] = data.type;
comm_x[chan] = (double)data.x;
comm_y[chan] = (double)data.y;
comm_z[chan] = (double)data.z;
comm_lon[chan] = (double)data.lon;
comm_lat[chan] = (double)data.lat;
comm_elev[chan] = (double)data.elev;
comm_valid[chan] = true;
// This was a switch-case statement but the compiler didn't like the new variable creation with it.
if(comm_type[chan] == ATIS) {
CommRegisterAirport(comm_ident[chan], chan, ATIS);
FGATC* app = FindInList(comm_ident[chan], ATIS);
if(app != NULL) {
// The station is already in the ATC list
//cout << "In list - flagging SetDisplay..." << endl;
comm_atc_ptr[chan] = app;
app->SetDisplay();
} else {
// Generate the station and put in the ATC list
//cout << "Not in list - generating..." << endl;
FGATIS* a = new FGATIS;
a->SetData(&data);
comm_atc_ptr[chan] = a;
a->SetDisplay();
//a->Init();
atc_list.push_back(a);
}
} else if (comm_type[chan] == TOWER) {
//cout << "TOWER TOWER TOWER\n";
CommRegisterAirport(comm_ident[chan], chan, TOWER);
//cout << "Done (TOWER)" << endl;
FGATC* app = FindInList(comm_ident[chan], TOWER);
if(app != NULL) {
// The station is already in the ATC list
SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is in list - flagging SetDisplay...");
//cout << comm_ident[chan] << " is in list - flagging SetDisplay...\n";
comm_atc_ptr[chan] = app;
app->SetDisplay();
} else {
// Generate the station and put in the ATC list
SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is not in list - generating...");
//cout << comm_ident[chan] << " is not in list - generating...\n";
FGTower* t = new FGTower;
t->SetData(&data);
comm_atc_ptr[chan] = t;
//cout << "Initing tower in FreqSearch()\n";
t->Init();
t->SetDisplay();
atc_list.push_back(t);
}
} else if (comm_type[chan] == GROUND) {
CommRegisterAirport(comm_ident[chan], chan, GROUND);
//cout << "Done (GROUND)" << endl;
FGATC* app = FindInList(comm_ident[chan], GROUND);
if(app != NULL) {
// The station is already in the ATC list
comm_atc_ptr[chan] = app;
app->SetDisplay();
} else {
// Generate the station and put in the ATC list
FGGround* g = new FGGround;
g->SetData(&data);
comm_atc_ptr[chan] = g;
g->Init();
g->SetDisplay();
atc_list.push_back(g);
}
} else if (comm_type[chan] == APPROACH) {
// We have to be a bit more carefull here since approaches are also searched by area
CommRegisterAirport(comm_ident[chan], chan, APPROACH);
//cout << "Done (APPROACH)" << endl;
FGATC* app = FindInList(comm_ident[chan], APPROACH);
if(app != NULL) {
// The station is already in the ATC list
app->AddPlane("Player");
app->SetDisplay();
comm_atc_ptr[chan] = app;
} else {
// Generate the station and put in the ATC list
FGApproach* a = new FGApproach;
a->SetData(&data);
comm_atc_ptr[chan] = a;
a->Init();
a->SetDisplay();
a->AddPlane("Player");
atc_list.push_back(a);
}
}
} else {
if(comm_valid[chan]) {
if(comm_type[chan] != APPROACH) {
// Currently approaches are removed by Alexander's out-of-range mechanism
CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
}
// Note that we *don't* call SetNoDisplay() here because the other comm channel
// might be tuned into the same station - this is handled by CommRemoveFromList(...)
comm_type[chan] = INVALID;
comm_atc_ptr[chan] = NULL;
comm_valid[chan] = false;
}
}
}
// Search ATC stations by area in order that we appear 'on the radar'
void FGATCMgr::AreaSearch() {
// Search for Approach stations
comm_list_type approaches;
comm_list_iterator app_itr;
lon = lon_node->getDoubleValue();
lat = lat_node->getDoubleValue();
elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
// search stations in range
int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
if (num_app != 0) {
//cout << num_app << " approaches found in radiostack search !!!!" << endl;
for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
FGATC* app = FindInList(app_itr->ident, app_itr->type);
if(app != NULL) {
// The station is already in the ATC list
//cout << "In list adding player\n";
app->AddPlane("Player");
//app->Update();
} else {
// Generate the station and put in the ATC list
FGApproach* a = new FGApproach;
a->SetData(&(*app_itr));
//cout << "Adding player\n";
a->AddPlane("Player");
//a->Update();
atc_list.push_back(a);
}
}
}
// remove planes which are out of range
// TODO - I'm not entirely sure that this belongs here.
atc_list_iterator it = atc_list.begin();
while(it != atc_list.end()) {
if((*it)->GetType() == APPROACH ) {
int np = (*it)->RemovePlane();
// if approach has no planes left remove it from ATC list
if ( np == 0) {
//cout << "REMOVING AN APPROACH STATION WITH NO PLANES..." << endl;
(*it)->SetNoDisplay();
(*it)->Update(0.00833);
delete (*it);
atc_list.erase(it);
atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
break; // the other stations will be checked next time
}
}
++it;
}
}

View file

@ -1,212 +0,0 @@
// ATCMgr.hxx - definition of FGATCMgr
// - a global management class for FlightGear generated ATC
//
// Written by David Luff, started February 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_ATCMGR_HXX
#define _FG_ATCMGR_HXX
#include <simgear/structure/subsystem_mgr.hxx>
#include <Main/fg_props.hxx>
#include <GUI/gui.h>
#include <string>
#include <list>
#include <map>
#include "tower.hxx"
#include "approach.hxx"
SG_USING_STD(string);
SG_USING_STD(list);
SG_USING_STD(map);
// Structure for holding details of the ATC frequencies at a given airport, and whether they are in the active list or not.
// These can then be cross referenced with the commlists which are stored by frequency or bucket.
// Non-available services are denoted by a frequency of zero.
// These structures are only intended to be created for in-use airports, and removed when no longer needed.
struct AirportATC {
AirportATC();
float lon;
float lat;
float elev;
float atis_freq;
bool atis_active;
float tower_freq;
bool tower_active;
float ground_freq;
bool ground_active;
//float approach_freq;
//bool approach_active;
//float departure_freq;
//bool departure_active;
// NOTE - the *_active flags determine whether the service is active in atc_list,
// *NOT* whether the tower etc is closed or not!!!!
// Flags to ensure the stations don't get wrongly deactivated
bool set_by_AI; // true when the AI manager has activated this station
unsigned int numAI; // Ref count of the number of AI planes registered
bool set_by_comm[2][ATC_NUM_TYPES]; // true when the relevant comm_freq has activated this station and type
};
class FGATCMgr : public SGSubsystem
{
private:
bool initDone; // Hack - guard against update getting called before init
// A map of airport ID vs frequencies and ATC provision
typedef map < string, AirportATC* > airport_atc_map_type;
typedef airport_atc_map_type::iterator airport_atc_map_iterator;
typedef airport_atc_map_type::const_iterator airport_atc_map_const_iterator;
airport_atc_map_type airport_atc_map;
airport_atc_map_iterator airport_atc_map_itr;
// A list of pointers to all currently active ATC classes
typedef list <FGATC*> atc_list_type;
typedef atc_list_type::iterator atc_list_iterator;
typedef atc_list_type::const_iterator atc_list_const_iterator;
// Everything put in this list should be created dynamically
// on the heap and ***DELETED WHEN REMOVED!!!!!***
atc_list_type atc_list;
atc_list_iterator atc_list_itr;
// Any member function of FGATCMgr is permitted to leave this iterator pointing
// at any point in or at the end of the list.
// Hence any new access must explicitly first check for atc_list.end() before dereferencing.
// Position of the Users Aircraft
double lon;
double lat;
double elev;
// Type of ATC control that the user's radios are tuned to.
atc_type comm_type[2];
// Pointer to the ATC station that the user is currently tuned into.
FGATC* comm_atc_ptr[2];
double comm_freq[2];
// Pointers to users current communication frequencies.
SGPropertyNode_ptr comm_node[2];
// Pointers to current users position
SGPropertyNode_ptr lon_node;
SGPropertyNode_ptr lat_node;
SGPropertyNode_ptr elev_node;
// Position of the ATC that the comm radios are tuned to in order to decide
// whether transmission will be received.
double comm_x[2], comm_y[2], comm_z[2], comm_lon[2], comm_lat[2], comm_elev[2];
double comm_range[2], comm_effective_range[2];
bool comm_valid[2];
string comm_ident[2];
//string last_comm_ident[2];
//string approach_ident;
bool last_in_range;
//FGATIS atis;
//FGGround ground;
FGTower tower;
FGApproach approach;
//FGDeparture departure;
// Voice related stuff
bool voice; // Flag - true if we are using voice
#ifdef ENABLE_AUDIO_SUPPORT
bool voiceOK; // Flag - true if at least one voice has loaded OK
FGATCVoice* v1;
#endif
public:
FGATCMgr();
~FGATCMgr();
void init();
void bind();
void unbind();
void update(double dt);
// Returns true if the airport is found in the map
bool GetAirportATCDetails(const string& icao, AirportATC* a);
// Return a pointer to a given sort of ATC at a given airport and activate if necessary
// Returns NULL if service doesn't exist - calling function should check for this.
FGATC* GetATCPointer(const string& icao, const atc_type& type);
// Return a pointer to an appropriate voice for a given type of ATC
// creating the voice if necessary - ie. make sure exactly one copy
// of every voice in use exists in memory.
//
// TODO - in the future this will get more complex and dole out country/airport
// specific voices, and possible make sure that the same voice doesn't get used
// at different airports in quick succession if a large enough selection are available.
FGATCVoice* GetVoicePointer(const atc_type& type);
atc_type GetComm1ATCType() { return(comm_type[0]); }
FGATC* GetComm1ATCPointer() { return(comm_atc_ptr[0]); }
atc_type GetComm2ATCType() { return(comm_type[1]); }
FGATC* GetComm2ATCPointer() { return(comm_atc_ptr[1]); }
// Get the frequency of a given service at a given airport
// Returns zero if not found
unsigned short int GetFrequency(const string& ident, const atc_type& tp);
// Register the fact that the AI system wants to activate an airport
bool AIRegisterAirport(const string& ident);
// Register the fact that the comm radio is tuned to an airport
bool CommRegisterAirport(const string& ident, int chan, const atc_type& tp);
private:
// Remove a class from the atc_list and delete it from memory
// *if* no other comm channel or AI plane is using it.
void CommRemoveFromList(const string& id, const atc_type& tp, int chan);
// Remove a class from the atc_list and delete it from memory
// Should be called from the above - not directly!!
void RemoveFromList(const string& id, const atc_type& tp);
// Return a pointer to a class in the list given ICAO code and type
// (external interface to this is through GetATCPointer)
// Return NULL if the given service is not in the list
// - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
FGATC* FindInList(const string& id, const atc_type& tp);
// Search the specified channel for stations on the same frequency and in range.
void FreqSearch(int channel);
// Search ATC stations by area in order that we appear 'on the radar'
void AreaSearch();
};
#endif // _FG_ATCMGR_HXX

View file

@ -1,314 +0,0 @@
// ATCutils.cxx - Utility functions for the ATC / AI system
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sstream>
#include <math.h>
#include <simgear/math/point3d.hxx>
#include <simgear/constants.h>
#include <simgear/misc/sg_path.hxx>
#include <simgear/debug/logstream.hxx>
#include <plib/sg.h>
//#include <iomanip.h>
#include <Airports/runways.hxx>
#include <Main/globals.hxx>
#include "ATCutils.hxx"
#include "ATCProjection.hxx"
static const string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "niner"};
// Convert any number to spoken digits
string ConvertNumToSpokenDigits(const string &n) {
//cout << "n = " << n << endl;
static const string pt = "decimal";
string str = "";
for(unsigned int i=0; i<n.length(); ++i) {
//cout << "n.substr(" << i << ",1 = " << n.substr(i,1) << endl;
if(n.substr(i,1) == " ") {
// do nothing
} else if(n.substr(i,1) == ".") {
str += pt;
} else {
str += nums[atoi((n.substr(i,1)).c_str())];
}
if(i != (n.length()-1)) { // ie. don't add a space at the end.
str += " ";
}
}
return(str);
}
// Convert an integer to spoken digits
string ConvertNumToSpokenDigits(int n) {
std::ostringstream buf;
buf << n;
return(ConvertNumToSpokenDigits(buf.str()));
}
// Convert a 2 digit rwy number to a spoken-style string
string ConvertRwyNumToSpokenString(int n) {
// Basic error/sanity checking
while(n < 0) {
n += 36;
}
while(n > 36) {
n -= 36;
}
if(n == 0) {
n = 36; // Is this right?
}
string str = "";
int index = n/10;
str += nums[index];
n -= (index * 10);
//str += "-";
str += " "; //Changed this for the benefit of the voice token parser - prefer the "-" in the visual output though.
str += nums[n];
return(str);
}
// Assumes we get a two-digit string optionally appended with L, R or C
// eg 01 07L 29R 36
// Anything else is not guaranteed to be handled correctly!
string ConvertRwyNumToSpokenString(const string &s) {
if(s.size() < 3) {
return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
} else {
string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
if(s.substr(2,1) == "L") {
r += " left";
} else if(s.substr(2,1) == "R") {
r += " right";
} else if(s.substr(2,1) == "C") {
r += " center";
} else {
SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
}
return(r);
}
}
// Return the phonetic letter of a letter represented as an integer 1->26
string GetPhoneticIdent(int i) {
// TODO - Check i is between 1 and 26 and wrap if necessary
return(GetPhoneticIdent(char('a' + (i-1))));
}
// Return the phonetic letter of a character in the range a-z or A-Z.
// Currently always returns prefixed by lowercase.
string GetPhoneticIdent(char c) {
c = tolower(c);
// TODO - Check c is between a and z and wrap if necessary
switch(c) {
case 'a' : return("alpha");
case 'b' : return("bravo");
case 'c' : return("charlie");
case 'd' : return("delta");
case 'e' : return("echo");
case 'f' : return("foxtrot");
case 'g' : return("golf");
case 'h' : return("hotel");
case 'i' : return("india");
case 'j' : return("juliet");
case 'k' : return("kilo");
case 'l' : return("lima");
case 'm' : return("mike");
case 'n' : return("november");
case 'o' : return("oscar");
case 'p' : return("papa");
case 'q' : return("quebec");
case 'r' : return("romeo");
case 's' : return("sierra");
case 't' : return("tango");
case 'u' : return("uniform");
case 'v' : return("victor");
case 'w' : return("whiskey");
case 'x' : return("x-ray");
case 'y' : return("yankee");
case 'z' : return("zulu");
}
// We shouldn't get here
return("Error");
}
// Get the compass direction associated with a heading in degrees
// Currently returns 8 direction resolution (N, NE, E etc...)
// Might be modified in future to return 4, 8 or 16 resolution but defaulting to 8.
string GetCompassDirection(double h) {
while(h < 0.0) h += 360.0;
while(h > 360.0) h -= 360.0;
if(h < 22.5 || h > 337.5) {
return("North");
} else if(h < 67.5) {
return("North-East");
} else if(h < 112.5) {
return("East");
} else if(h < 157.5) {
return("South-East");
} else if(h < 202.5) {
return("South");
} else if(h < 247.5) {
return("South-West");
} else if(h < 292.5) {
return("West");
} else {
return("North-West");
}
}
//================================================================================================================
// Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
double dclGetHorizontalSeparation(const Point3D& pos1, const Point3D& pos2) {
double x; //East-West separation
double y; //North-South separation
double z; //Horizontal separation - z = sqrt(x^2 + y^2)
double lat1 = pos1.lat() * SG_DEGREES_TO_RADIANS;
double lon1 = pos1.lon() * SG_DEGREES_TO_RADIANS;
double lat2 = pos2.lat() * SG_DEGREES_TO_RADIANS;
double lon2 = pos2.lon() * SG_DEGREES_TO_RADIANS;
y = sin(fabs(lat1 - lat2)) * SG_EQUATORIAL_RADIUS_M;
x = sin(fabs(lon1 - lon2)) * SG_EQUATORIAL_RADIUS_M * (cos((lat1 + lat2) / 2.0));
z = sqrt(x*x + y*y);
return(z);
}
// Given a point and a line, get the HORIZONTAL shortest distance from the point to a point on the line.
// Expects to be fed orthogonal co-ordinates, NOT lat & lon !
// The units of the separation will be those of the input.
double dclGetLinePointSeparation(double px, double py, double x1, double y1, double x2, double y2) {
double vecx = x2-x1;
double vecy = y2-y1;
double magline = sqrt(vecx*vecx + vecy*vecy);
double u = ((px-x1)*(x2-x1) + (py-y1)*(y2-y1)) / (magline * magline);
double x0 = x1 + u*(x2-x1);
double y0 = y1 + u*(y2-y1);
vecx = px - x0;
vecy = py - y0;
double d = sqrt(vecx*vecx + vecy*vecy);
if(d < 0) {
d *= -1;
}
return(d);
}
// Given a position (lat/lon/elev), heading and vertical angle (degrees), and distance (meters), calculate the new position.
// This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
// Assumes that the ground is not hit!!! Expects heading and angle in degrees, distance in meters.
Point3D dclUpdatePosition(const Point3D& pos, double heading, double angle, double distance) {
//cout << setprecision(10) << pos.lon() << ' ' << pos.lat() << '\n';
heading *= DCL_DEGREES_TO_RADIANS;
angle *= DCL_DEGREES_TO_RADIANS;
double lat = pos.lat() * DCL_DEGREES_TO_RADIANS;
double lon = pos.lon() * DCL_DEGREES_TO_RADIANS;
double elev = pos.elev();
//cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
double horiz_dist = distance * cos(angle);
double vert_dist = distance * sin(angle);
double north_dist = horiz_dist * cos(heading);
double east_dist = horiz_dist * sin(heading);
//cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
double delta_lat = asin(north_dist / (double)SG_EQUATORIAL_RADIUS_M);
double delta_lon = asin(east_dist / (double)SG_EQUATORIAL_RADIUS_M) * (1.0 / cos(lat)); // I suppose really we should use the average of the original and new lat but we'll assume that this will be good enough.
//cout << delta_lon*DCL_RADIANS_TO_DEGREES << ' ' << delta_lat*DCL_RADIANS_TO_DEGREES << '\n';
lat += delta_lat;
lon += delta_lon;
elev += vert_dist;
//cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
//cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
return(Point3D(lon*DCL_RADIANS_TO_DEGREES, lat*DCL_RADIANS_TO_DEGREES, elev));
}
// Get a heading in degrees from one lat/lon to another.
// This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
// Warning - at the moment we are not checking for identical points - currently it returns 0 in this instance.
double GetHeadingFromTo(const Point3D& A, const Point3D& B) {
double latA = A.lat() * DCL_DEGREES_TO_RADIANS;
double lonA = A.lon() * DCL_DEGREES_TO_RADIANS;
double latB = B.lat() * DCL_DEGREES_TO_RADIANS;
double lonB = B.lon() * DCL_DEGREES_TO_RADIANS;
double xdist = sin(lonB - lonA) * (double)SG_EQUATORIAL_RADIUS_M * cos((latA+latB)/2.0);
double ydist = sin(latB - latA) * (double)SG_EQUATORIAL_RADIUS_M;
double heading = atan2(xdist, ydist) * DCL_RADIANS_TO_DEGREES;
return heading < 0.0 ? heading + 360 : heading;
}
// Given a heading (in degrees), bound it from 0 -> 360
void dclBoundHeading(double &hdg) {
while(hdg < 0.0) {
hdg += 360.0;
}
while(hdg > 360.0) {
hdg -= 360.0;
}
}
// smallest difference between two angles in degrees
// difference is negative if a1 > a2 and positive if a2 > a1
double GetAngleDiff_deg( const double &a1, const double &a2) {
double a3 = a2 - a1;
while (a3 < 180.0) a3 += 360.0;
while (a3 > 180.0) a3 -= 360.0;
return a3;
}
// Runway stuff
// Given a Point3D (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
bool OnRunway(const Point3D& pt, const FGRunway& rwy) {
FGATCAlignedProjection ortho;
Point3D centre(rwy._lon, rwy._lat, 0.0); // We don't need the elev
ortho.Init(centre, rwy._heading);
Point3D xyc = ortho.ConvertToLocal(centre);
Point3D xyp = ortho.ConvertToLocal(pt);
//cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
//cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
if((fabs(xyp.y() - xyc.y()) < ((rwy._length/2.0) + 5.0))
&& (fabs(xyp.x() - xyc.x()) < (rwy._width/2.0))) {
return(true);
}
return(false);
}

View file

@ -1,103 +0,0 @@
// ATCutils.hxx - Utility functions for the ATC / AI subsytem
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <Airports/simple.hxx>
#include <Airports/runways.hxx>
#include <math.h>
#include <simgear/math/point3d.hxx>
#include <string>
SG_USING_STD(string);
// These are defined here because I had a problem with SG_DEGREES_TO_RADIANS
#define DCL_PI 3.1415926535f
#define DCL_DEGREES_TO_RADIANS (DCL_PI/180.0)
#define DCL_RADIANS_TO_DEGREES (180.0/DCL_PI)
/*******************************
*
* Communication functions
*
********************************/
// Convert any number to spoken digits
string ConvertNumToSpokenDigits(const string &n);
// Convert an integer to spoken digits
string ConvertNumToSpokenDigits(int n);
// Convert a 2 digit rwy number to a spoken-style string
string ConvertRwyNumToSpokenString(int n);
// Convert rwy number string to a spoken-style string
// eg "05L" to "zero five left"
// Assumes we get a two-digit string optionally appended with R, L, or C
// eg 01 07L 29R 36
// Anything else is not guaranteed to be handled correctly!
string ConvertRwyNumToSpokenString(const string &s);
// Return the phonetic letter of a letter represented as an integer 1->26
string GetPhoneticIdent(int i);
// Return the phonetic letter of a character in the range a-z or A-Z.
// Currently always returns prefixed by lowercase.
string GetPhoneticIdent(char c);
// Get the compass direction associated with a heading in degrees
// Currently returns 8 direction resolution (N, NE, E etc...)
// Might be modified in future to return 4, 8 or 16 resolution but defaulting to 8.
string GetCompassDirection(double h);
/*******************************
*
* Positional functions
*
********************************/
// Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
double dclGetHorizontalSeparation(const Point3D& pos1, const Point3D& pos2);
// Given a point and a line, get the HORIZONTAL shortest distance from the point to a point on the line.
// Expects to be fed orthogonal co-ordinates, NOT lat & lon !
double dclGetLinePointSeparation(double px, double py, double x1, double y1, double x2, double y2);
// Given a position (lat/lon/elev), heading, vertical angle, and distance, calculate the new position.
// Assumes that the ground is not hit!!! Expects heading and angle in degrees, distance in meters.
Point3D dclUpdatePosition(const Point3D& pos, double heading, double angle, double distance);
// Get a heading from one lat/lon to another (in degrees)
double GetHeadingFromTo(const Point3D& A, const Point3D& B);
// Given a heading (in degrees), bound it from 0 -> 360
void dclBoundHeading(double &hdg);
// smallest difference between two angles in degrees
// difference is negative if a1 > a2 and positive if a2 > a1
double GetAngleDiff_deg( const double &a1, const double &a2);
/****************
*
* Runways
*
****************/
// Given a Point3D (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
bool OnRunway(const Point3D& pt, const FGRunway& rwy);

View file

@ -1,23 +0,0 @@
noinst_LIBRARIES = libATC.a
libATC_a_SOURCES = \
ATC.hxx ATC.cxx \
atis.hxx atis.cxx \
tower.hxx tower.cxx \
approach.hxx approach.cxx \
ground.hxx ground.cxx \
commlist.hxx commlist.cxx \
ATCDialog.hxx ATCDialog.cxx \
ATCVoice.hxx ATCVoice.cxx \
ATCmgr.hxx ATCmgr.cxx \
ATCutils.hxx ATCutils.cxx \
ATCProjection.hxx ATCProjection.cxx \
AIMgr.hxx AIMgr.cxx \
AIEntity.hxx AIEntity.cxx \
AIPlane.hxx AIPlane.cxx \
AILocalTraffic.hxx AILocalTraffic.cxx \
AIGAVFRTraffic.hxx AIGAVFRTraffic.cxx \
transmission.hxx transmission.cxx \
transmissionlist.hxx transmissionlist.cxx
INCLUDES = -I$(top_srcdir) -I$(top_srcdir)/src

View file

@ -1,759 +0,0 @@
// FGApproach - a class to provide approach control at larger airports.
//
// Written by Alexander Kappes, started March 2002.
//
// Copyright (C) 2002 Alexander Kappes
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "approach.hxx"
#include "transmission.hxx"
#include "transmissionlist.hxx"
#include "ATCDialog.hxx"
#include <Airports/runways.hxx>
#include <simgear/constants.h>
#include <simgear/math/polar3d.hxx>
#include <simgear/misc/sg_path.hxx>
#include <Environment/environment_mgr.hxx>
#include <Environment/environment.hxx>
#include <GUI/gui.h>
//Constructor
FGApproach::FGApproach(){
comm1_node = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
comm2_node = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
_type = APPROACH;
num_planes = 0;
lon_node = fgGetNode("/position/longitude-deg", true);
lat_node = fgGetNode("/position/latitude-deg", true);
elev_node = fgGetNode("/position/altitude-ft", true);
hdg_node = fgGetNode("/orientation/heading-deg", true);
speed_node = fgGetNode("/velocities/airspeed-kt", true);
etime_node = fgGetNode("/sim/time/elapsed-sec", true);
first = true;
active_runway = "";
int i;
for ( i=0; i<max_planes; i++) {
planes[i].contact = 0;
planes[i].wpn = 0;
planes[i].dnwp = -999.;
planes[i].on_crs = true;
planes[i].turn_rate = 10.0;
planes[i].desc_rate = 1000.0;
planes[i].clmb_rate = 500.0;
planes[i].tlm = 0.0;
planes[i].lmc.c1 = 0;
planes[i].lmc.c2 = 0;
planes[i].lmc.c3 = -1;
planes[i].wp_change = false;
}
}
//Destructor
FGApproach::~FGApproach(){
}
void FGApproach::Init() {
}
// ============================================================================
// the main update function
// ============================================================================
void FGApproach::Update(double dt) {
const int max_trans = 20;
FGTransmission tmissions[max_trans];
int wpn;
atc_type station = APPROACH;
TransCode code;
TransPar TPar;
int i,j;
//double course, d,
double adif, datp;
//char buf[10];
string message;
//static string atcmsg1[10];
//static string atcmsg2[10];
string mentry;
string transm;
TransPar tpars;
//static bool TransDisplayed = false;
update_plane_dat();
if ( active_runway == "" ) get_active_runway();
double comm1_freq = comm1_node->getDoubleValue();
//bool DisplayTransmissions = true;
for (i=0; i<num_planes; i++) {
if ( planes[i].ident == "Player") {
station = APPROACH;
tpars.station = name;
tpars.callsign = "Player";
tpars.airport = ident;
//cout << "ident = " << ident << " name = " << name << '\n';
int num_trans = 0;
// is the frequency of the station tuned in?
if ( freq == (int)(comm1_freq*100.0 + 0.5) ) {
current_transmissionlist->query_station( station, tmissions, max_trans, num_trans );
// loop over all transmissions for station
for ( j=0; j<=num_trans-1; j++ ) {
code = tmissions[j].get_code();
//cout << "code is " << code.c1 << " " << code.c2 << " " << code.c3 << '\n';
// select proper transmissions
if(code.c3 != 2) { // DCL - hack to prevent request crossing airspace being displayed since this isn't implemented yet.
if ( ( code.c2 == -1 && planes[i].lmc.c3 == 0 ) ||
( code.c1 == 0 && code.c2 == planes[i].lmc.c2 ) ) {
mentry = current_transmissionlist->gen_text(station, code, tpars, false);
transm = current_transmissionlist->gen_text(station, code, tpars, true);
// is the transmission already registered?
if (!current_atcdialog->trans_reg( ident, transm, APPROACH )) {
current_atcdialog->add_entry( ident, transm, mentry, APPROACH, 0 );
}
}
}
}
}
}
}
for ( i=0; i<num_planes; i++ ) {
//cout << "TPar.airport = " << TPar.airport << " TPar.station = " << TPar.station << " TPar.callsign = " << TPar.callsign << '\n';
//if ( planes[i].ident == TPar.callsign && name == TPar.airport && TPar.station == "approach" ) {
//if ( TPar.request && TPar.intention == "landing" && ident == TPar.intid) {
if(planes[i].ident == "Player" && fgGetBool("/sim/atc/opt0")) {
//cout << "Landing requested\n";
fgSetBool("/sim/atc/opt0", false);
planes[i].wpn = 0;
// ===========================
// === calculate waypoints ===
// ===========================
calc_wp( i );
update_param( i );
wpn = planes[i].wpn-1;
planes[i].aalt = planes[i].wpts[wpn-1][2];
planes[i].ahdg = planes[i].wpts[wpn][4];
// generate the message
code.c1 = 1;
code.c2 = 1;
code.c3 = 0;
adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
tpars.station = name;
tpars.callsign = "Player";
if ( adif < 0 ) tpars.tdir = 1;
else tpars.tdir = 2;
tpars.heading = planes[i].ahdg;
if (planes[i].alt-planes[i].aalt > 100.0) tpars.VDir = 1;
else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
else tpars.VDir = 2;
tpars.alt = planes[i].aalt;
message = current_transmissionlist->gen_text(station, code, tpars, true );
//cout << message << '\n';
set_message(message);
planes[i].lmc = code;
planes[i].tlm = etime_node->getDoubleValue();
planes[i].on_crs = true;
planes[i].contact = 1;
}
//}
//if(1) {
if ( planes[i].contact == 1 ) {
// =========================
// === update parameters ===
// =========================
update_param( i );
//cout << planes[i].brg << " " << planes[i].dist << " " << planes[i].wpts[wpn+1][0]
//<< " " << planes[i].wpts[wpn+1][1] << " " << planes[i].wpts[wpn+1][4]
//cout << wpn << " distance to current course = " << planes[i].dcc << endl;
//cout << etime_node->getDoubleValue() << endl;
// =========================
// === reached waypoint? ===
// =========================
wpn = planes[i].wpn-2;
adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] )
* SGD_DEGREES_TO_RADIANS;
datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
planes[i].spd/3600. * planes[i].turn_rate +
planes[i].spd/3600. * 3.0;
//cout << adif/SGD_DEGREES_TO_RADIANS << " "
// << datp << " " << planes[i].dnc << " " << planes[i].dcc <<endl;
if ( fabs(planes[i].dnc) < datp ) {
//if ( fabs(planes[i].dnc) < 0.3 && planes[i].dnwp < 1.0 ) {
//cout << "Reached next waypoint!\n";
planes[i].wpn -= 1;
wpn = planes[i].wpn-1;
planes[i].ahdg = planes[i].wpts[wpn][4];
planes[i].aalt = planes[i].wpts[wpn-1][2];
planes[i].wp_change = true;
// generate the message
adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
tpars.station = name;
tpars.callsign = "Player";
if ( adif < 0 ) tpars.tdir = 1;
else tpars.tdir = 2;
tpars.heading = planes[i].ahdg;
if ( wpn-1 != 0) {
code.c1 = 1;
code.c2 = 1;
code.c3 = 0;
if (planes[i].alt-planes[i].aalt > 100.0) tpars.VDir = 1;
else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
else tpars.VDir = 2;
tpars.alt = planes[i].aalt;
message = current_transmissionlist->gen_text(station, code, tpars, true );
//cout << "Approach transmitting...\n";
//cout << message << endl;
set_message(message);
}
else {
code.c1 = 1;
code.c2 = 3;
code.c3 = 0;
tpars.runway = active_runway;
message = current_transmissionlist->gen_text(station, code, tpars, true);
//cout << "Approach transmitting 2 ...\n";
//cout << message << endl;
set_message(message);
}
planes[i].lmc = code;
planes[i].tlm = etime_node->getDoubleValue();
planes[i].on_crs = true;
update_param( i );
}
// =========================
// === come off course ? ===
// =========================
if ( fabs(planes[i].dcc) > 1.0 &&
( !planes[i].wp_change || etime_node->getDoubleValue() - planes[i].tlm > tbm ) ) {
//cout << "Off course!\n";
if ( planes[i].on_crs ) {
if ( planes[i].dcc < 0) {
planes[i].ahdg += 30.0;
}
else {
planes[i].ahdg -= 30.0;
}
if (planes[i].ahdg > 360.0) planes[i].ahdg -= 360.0;
else if (planes[i].ahdg < 0.0) planes[i].ahdg += 360.0;
}
//cout << planes[i].on_crs << " "
// << angle_diff_deg( planes[i].hdg, planes[i].ahdg) << " "
// << etime_node->getDoubleValue() << " "
// << planes[i].tlm << endl;
// generate the message
if ( planes[i].on_crs ||
( fabs(angle_diff_deg( planes[i].hdg, planes[i].ahdg )) > 30.0 &&
etime_node->getDoubleValue() - planes[i].tlm > tbm) ) {
// generate the message
code.c1 = 1;
code.c2 = 4;
code.c3 = 0;
adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
tpars.station = name;
tpars.callsign = "Player";
tpars.miles = fabs(planes[i].dcc);
if ( adif < 0 ) tpars.tdir = 1;
else tpars.tdir = 2;
tpars.heading = planes[i].ahdg;
message = current_transmissionlist->gen_text(station, code, tpars, true);
//cout << "Approach transmitting 3 ...\n";
//cout << message << '\n';
set_message(message);
planes[i].lmc = code;
planes[i].tlm = etime_node->getDoubleValue();
}
planes[i].on_crs = false;
}
else if ( !planes[i].on_crs ) {
//cout << "Off course 2!\n";
wpn = planes[i].wpn-1;
adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] )
* SGD_DEGREES_TO_RADIANS;
datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
planes[i].spd/3600. * planes[i].turn_rate +
planes[i].spd/3600. * 3.0;
if ( fabs(planes[i].dcc) < datp ) {
planes[i].ahdg = fabs(planes[i].wpts[wpn][4]);
// generate the message
code.c1 = 1;
code.c2 = 2;
code.c3 = 0;
tpars.station = name;
tpars.callsign = "Player";
if ( adif < 0 ) tpars.tdir = 1;
else tpars.tdir = 2;
tpars.heading = planes[i].ahdg;
message = current_transmissionlist->gen_text(station, code, tpars, true);
//cout << "Approach transmitting 4 ...\n";
//cout << message << '\n';
set_message(message);
planes[i].lmc = code;
planes[i].tlm = etime_node->getDoubleValue();
planes[i].on_crs = true;
}
}
else if ( planes[i].wp_change ) {
planes[i].wp_change = false;
}
// ===================================================================
// === Less than two minutes away from touchdown? -> Contact Tower ===
// ===================================================================
if ( planes[i].wpn == 2 && planes[i].dnwp < planes[i].spd/60.*2.0 ) {
double freq = 121.95; // Hardwired - FIXME
// generate message
code.c1 = 1;
code.c2 = 5;
code.c3 = 0;
tpars.station = name;
tpars.callsign = "Player";
tpars.freq = freq;
message = current_transmissionlist->gen_text(station, code, tpars, true);
//cout << "Approach transmitting 5 ...\n";
//cout << message << '\n';
set_message(message);
planes[i].lmc = code;
planes[i].tlm = etime_node->getDoubleValue();
planes[i].contact = 2;
}
}
}
}
// ============================================================================
// update course parameters
// ============================================================================
void FGApproach::update_param( const int &i ) {
double course, d;
int wpn = planes[i].wpn-1; // this is the current waypoint
planes[i].dcc = calc_psl_dist(planes[i].brg, planes[i].dist,
planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
planes[i].wpts[wpn][4]);
planes[i].dnc = calc_psl_dist(planes[i].brg, planes[i].dist,
planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
planes[i].wpts[wpn-1][4]);
calc_hd_course_dist(planes[i].brg, planes[i].dist,
planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
&course, &d);
planes[i].dnwp = d;
}
// ============================================================================
// smallest difference between two angles in degree
// difference is negative if a1 > a2 and positive if a2 > a1
// ===========================================================================
double FGApproach::angle_diff_deg( const double &a1, const double &a2) {
double a3 = a2 - a1;
if (a3 < 180.0) a3 += 360.0;
if (a3 > 180.0) a3 -= 360.0;
return a3;
}
// ============================================================================
// calculate waypoints
// ============================================================================
void FGApproach::calc_wp( const int &i ) {
int j;
double course, d, cd, a1;
int wpn = planes[i].wpn;
// waypoint 0: Threshold of active runway
calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
Point3D(active_rw_lon*SGD_DEGREES_TO_RADIANS,active_rw_lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
&course, &d);
double d1 = active_rw_hdg+180.0;
if ( d1 > 360.0 ) d1 -=360.0;
calc_cd_head_dist(360.0-course*SGD_RADIANS_TO_DEGREES, d/SG_NM_TO_METER,
d1, active_rw_len/SG_NM_TO_METER/2.0,
&planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
planes[i].wpts[wpn][2] = elev;
planes[i].wpts[wpn][4] = 0.0;
planes[i].wpts[wpn][5] = 0.0;
wpn += 1;
// ======================
// horizontal navigation
// ======================
// waypoint 1: point for turning onto final
calc_cd_head_dist(planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1], d1, lfl,
&planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
&course, &d);
planes[i].wpts[wpn][4] = course;
planes[i].wpts[wpn][5] = d;
wpn += 1;
// calculate course and distance from plane position to waypoint 1
calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[1][0],
planes[i].wpts[1][1], &course, &d);
// check if airport is not between plane and waypoint 1 and
// DCA to airport on course to waypoint 1 is larger than 10 miles
double zero = 0.0;
if ( fabs(angle_diff_deg( planes[i].wpts[1][0], planes[i].brg )) < 90.0 ||
calc_psl_dist( zero, zero, planes[i].brg, planes[i].dist, course ) > 10.0 ) {
// check if turning angle at waypoint 1 would be > max_ta
if ( fabs(angle_diff_deg( planes[i].wpts[1][4], course )) > max_ta ) {
cd = calc_psl_dist(planes[i].brg, planes[i].dist,
planes[i].wpts[1][0], planes[i].wpts[1][1],
planes[i].wpts[1][4]);
a1 = atan2(cd,planes[i].wpts[1][1]);
planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
if ( planes[i].wpts[wpn][0] < 0.0) planes[i].wpts[wpn][0] += 360.0;
if ( planes[i].wpts[wpn][0] > 360.0) planes[i].wpts[wpn][0] -= 360.0;
planes[i].wpts[wpn][1] = fabs(cd) / sin(fabs(a1));
calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
&course, &d);
planes[i].wpts[wpn][4] = course;
planes[i].wpts[wpn][5] = d;
wpn += 1;
calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[wpn-1][0],
planes[i].wpts[wpn-1][1], &course, &d);
}
} else {
double leg = 10.0;
a1 = atan2(planes[i].wpts[1][1], leg );
if ( angle_diff_deg(planes[i].brg,planes[i].wpts[1][0]) < 0 )
planes[i].wpts[wpn][0] = planes[i].wpts[1][0] + a1/SGD_DEGREES_TO_RADIANS;
else planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
planes[i].wpts[wpn][1] = sqrt( planes[i].wpts[1][1]*planes[i].wpts[1][1] + leg*leg );
calc_hd_course_dist(planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
&course, &d);
planes[i].wpts[wpn][4] = course;
planes[i].wpts[wpn][5] = d;
wpn += 1;
calc_hd_course_dist(planes[i].brg, planes[i].dist,
planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
&course, &d);
}
planes[i].wpts[wpn][0] = planes[i].brg;
planes[i].wpts[wpn][1] = planes[i].dist;
planes[i].wpts[wpn][2] = planes[i].alt;
planes[i].wpts[wpn][4] = course;
planes[i].wpts[wpn][5] = d;
wpn += 1;
planes[i].wpn = wpn;
// Now check if legs are too short or if legs can be shortend
// legs must be at least 2 flight minutes long
double mdist = planes[i].spd / 60.0 * 2.0;
for ( j=2; j<wpn-1; ++j ) {
if ( planes[i].wpts[j][1] < mdist) {
}
}
// ====================
// vertical navigation
// ====================
double alt = elev+3000.0;
planes[i].wpts[1][2] = round_alt( true, alt );
for ( j=2; j<wpn-1; ++j ) {
double dalt = planes[i].alt - planes[i].wpts[j-1][2];
if ( dalt > 0 ) {
alt = planes[i].wpts[j-1][2] +
(planes[i].wpts[j][5] / planes[i].spd) * 60.0 * planes[i].desc_rate;
planes[i].wpts[j][2] = round_alt( false, alt );
if ( planes[i].wpts[j][2] > planes[i].alt )
planes[i].wpts[j][2] = round_alt( false, planes[i].alt );
}
else {
planes[i].wpts[j][2] = planes[i].wpts[1][2];
}
}
cout << "Plane position: " << planes[i].brg << " " << planes[i].dist << endl;
for ( j=0; j<wpn; ++j ) {
cout << "Waypoint " << j << endl;
cout << "------------------" << endl;
cout << planes[i].wpts[j][0] << " " << planes[i].wpts[j][1]
<< " " << planes[i].wpts[j][2] << " " << planes[i].wpts[j][5];
cout << endl << endl;
}
}
// ============================================================================
// round altitude value to next highest/lowest 500 feet
// ============================================================================
double FGApproach::round_alt( const bool hl, double alt ) {
alt = alt/1000.0;
if ( hl ) {
if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+1)*1000.0;
else alt = ((int)(alt)+0.5)*1000.0;
}
else {
if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+0.5)*1000.0;
else alt = ((int)(alt))*1000.0;
}
return alt;
}
// ============================================================================
// get active runway
// ============================================================================
void FGApproach::get_active_runway() {
//cout << "Entering FGApproach::get_active_runway()\n";
FGEnvironment stationweather =
((FGEnvironmentMgr *)globals->get_subsystem("environment"))
->getEnvironment(lat, lon, elev);
double hdg = stationweather.get_wind_from_heading_deg();
FGRunway runway;
if ( globals->get_runways()->search( ident, int(hdg), &runway) ) {
active_runway = runway._rwy_no;
active_rw_hdg = runway._heading;
active_rw_lon = runway._lon;
active_rw_lat = runway._lat;
active_rw_len = runway._length;
//cout << "Active runway is: " << active_runway << " heading = "
// << active_rw_hdg
// << " lon = " << active_rw_lon
// << " lat = " << active_rw_lat <<endl;
}
else cout << "FGRunways search failed\n";
}
// ========================================================================
// update infos about plane
// ========================================================================
void FGApproach::update_plane_dat() {
//cout << "Update Approach " << ident << " " << num_planes << " registered" << endl;
// update plane positions
int i;
for (i=0; i<num_planes; i++) {
planes[i].lon = lon_node->getDoubleValue();
planes[i].lat = lat_node->getDoubleValue();
planes[i].alt = elev_node->getDoubleValue();
planes[i].hdg = hdg_node->getDoubleValue();
planes[i].spd = speed_node->getDoubleValue();
/*Point3D aircraft = sgGeodToCart( Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,
planes[i].lat*SGD_DEGREES_TO_RADIANS,
planes[i].alt*SG_FEET_TO_METER) );*/
double course, distance;
calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,planes[i].lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
&course, &distance);
planes[i].dist = distance/SG_NM_TO_METER;
planes[i].brg = 360.0-course*SGD_RADIANS_TO_DEGREES;
//cout << "Plane Id: " << planes[i].ident << " Distance to " << ident
// << " is " << planes[i].dist << " miles " << "Bearing " << planes[i].brg << endl;
}
}
// =======================================================================
// Add plane to Approach list
// =======================================================================
void FGApproach::AddPlane(const string& pid) {
int i;
for ( i=0; i<num_planes; i++) {
if ( planes[i].ident == pid) {
//cout << "Plane already registered: " << planes[i].ident << ' ' << ident << ' ' << num_planes << endl;
return;
}
}
planes[num_planes].ident = pid;
++num_planes;
//cout << "Plane added to list: " << ident << " " << num_planes << endl;
return;
}
// ================================================================================
// closest distance between a point (h1,d1) and a straigt line (h2,d2,h3) in 2 dim.
// ================================================================================
double FGApproach::calc_psl_dist(const double &h1, const double &d1,
const double &h2, const double &d2,
const double &h3)
{
double a1 = h1 * SGD_DEGREES_TO_RADIANS;
double a2 = h2 * SGD_DEGREES_TO_RADIANS;
double a3 = h3 * SGD_DEGREES_TO_RADIANS;
double x1 = cos(a1) * d1;
double y1 = sin(a1) * d1;
double x2 = cos(a2) * d2;
double y2 = sin(a2) * d2;
double x3 = cos(a3);
double y3 = sin(a3);
// formula: dis = sqrt( (v1-v2)**2 - ((v1-v2)*v3)**2 ); vi = (xi,yi)
double val1 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
double val2 = ((x1-x2)*x3 + (y1-y2)*y3) * ((x1-x2)*x3 + (y1-y2)*y3);
double dis = val1 - val2;
// now get sign for offset
//cout << x1 << " " << x2 << " " << y1 << " " << y2 << " "
// << x3 << " " << y3 << " "
// << val1 << " " << val2 << " " << dis << endl;
x3 *= sqrt(val2);
y3 *= sqrt(val2);
double da = fabs(atan2(y3,x3) - atan2(y1-y2,x1-x2));
if ( da > SGD_PI ) da -= SGD_2PI;
if ( fabs(da) > SGD_PI_2) {
//if ( x3*(x1-x2) < 0.0 && y3*(y1-y2) < 0.0) {
x3 *= -1.0;
y3 *= -1.0;
}
//cout << x3 << " " << y3 << endl;
double dis1 = x1-x2-x3;
double dis2 = y1-y2-y3;
dis = sqrt(dis);
da = atan2(dis2,dis1);
if ( da < 0.0 ) da += SGD_2PI;
if ( da < a3 ) dis *= -1.0;
//cout << dis1 << " " << dis2 << " " << da*SGD_RADIANS_TO_DEGREES << " " << h3
// << " " << sqrt(dis1*dis1 + dis2*dis2) << " " << dis << endl;
//cout << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << dis << endl;
return dis;
}
// ========================================================================
// Calculate new bear/dist given starting bear/dis, and offset radial,
// and distance.
// ========================================================================
void FGApproach::calc_cd_head_dist(const double &h1, const double &d1,
const double &course, const double &dist,
double *h2, double *d2)
{
double a1 = h1 * SGD_DEGREES_TO_RADIANS;
double a2 = course * SGD_DEGREES_TO_RADIANS;
double x1 = cos(a1) * d1;
double y1 = sin(a1) * d1;
double x2 = cos(a2) * dist;
double y2 = sin(a2) * dist;
*d2 = sqrt((x1+x2)*(x1+x2) + (y1+y2)*(y1+y2));
*h2 = atan2( (y1+y2), (x1+x2) ) * SGD_RADIANS_TO_DEGREES;
if ( *h2 < 0 ) *h2 = *h2+360;
}
// ========================================================================
// get heading and distance between two points; point1 ---> point2
// ========================================================================
void FGApproach::calc_hd_course_dist(const double &h1, const double &d1,
const double &h2, const double &d2,
double *course, double *dist)
{
double a1 = h1 * SGD_DEGREES_TO_RADIANS;
double a2 = h2 * SGD_DEGREES_TO_RADIANS;
double x1 = cos(a1) * d1;
double y1 = sin(a1) * d1;
double x2 = cos(a2) * d2;
double y2 = sin(a2) * d2;
*dist = sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
*course = atan2( (y2-y1), (x2-x1) ) * SGD_RADIANS_TO_DEGREES;
if ( *course < 0 ) *course = *course+360;
//cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << *dist << " " << *course << endl;
}
int FGApproach::RemovePlane() {
// first check if anything has to be done
bool rmplane = false;
int i;
for (i=0; i<num_planes; i++) {
if (planes[i].dist > range*SG_NM_TO_METER) {
rmplane = true;
break;
}
}
if (!rmplane) return num_planes;
// now make a copy of the plane list
PlaneApp tmp[max_planes];
for (i=0; i<num_planes; i++) {
tmp[i] = planes[i];
}
int np = 0;
// now check which planes are still in range
for (i=0; i<num_planes; i++) {
if (tmp[i].dist <= range*SG_NM_TO_METER) {
planes[np] = tmp[i];
np += 1;
}
}
num_planes = np;
return num_planes;
}
void FGApproach::set_message(const string &msg)
{
fgSetString("/sim/messages/approach", msg.c_str());
}

View file

@ -1,231 +0,0 @@
// approach.hxx -- Approach class
//
// Written by Alexander Kappes, started March 2002.
//
// Copyright (C) 2002 Alexander Kappes
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_APPROACH_HXX
#define _FG_APPROACH_HXX
#include <stdio.h>
#include <simgear/compiler.h>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/magvar/magvar.hxx>
#include <simgear/timing/sg_time.hxx>
#include <simgear/bucket/newbucket.hxx>
#include <Main/fg_props.hxx>
#ifdef SG_HAVE_STD_INCLUDES
# include <istream>
#include <iomanip>
#elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
# include <iostream.h>
#elif defined( __BORLANDC__ )
# include <iostream>
#else
# include <istream.h>
#include <iomanip.h>
#endif
#if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
SG_USING_STD(istream);
#endif
SG_USING_STD(string);
#include "ATC.hxx"
#include "transmission.hxx"
//DCL - a complete guess for now.
#define FG_APPROACH_DEFAULT_RANGE 100
// Contains all the information about a plane that the approach control needs
const int max_planes = 20; // max number of planes on the stack
const int max_wp = 10; // max number of waypoints for approach phase
const double max_ta = 130; // max turning angle for plane during approach
const double tbm = 2.0; // min time (in sec) between two messages
const double lfl = 10.0; // length of final leg
struct PlaneApp {
// variables for plane if it's on the radar
string ident; // indentification of plane
double lon; // longitude in degrees
double lat; // latitude in degrees
double alt; // Altitute above sea level in feet
double hdg; // heading of plane in degrees
double dist; // distance to airport in miles
double brg; // bearing relative to airport in degrees
double spd; // speed above ground
int contact; // contact with approach established?
// 0 = no contact yet
// 1 = in contact
// 2 = handed off to tower
double turn_rate; // standard turning rate of the plane in seconds per degree
double desc_rate; // standard descent rate of the plane in feets per minute
double clmb_rate; // standard climb rate of the plane in feets per minute
// additional variables if contact has been established
int wpn; // number of waypoints
double wpts[max_wp][6]; // assigned waypoints for approach phase
// first wp in list is airport
// last waypoint point at which contact was established
// second index: 0 = bearing to airport
// second index: 1 = distance to airport
// second index: 2 = alt
// second index: 3 = ETA
// second index: 4 = heading to next waypoint
// second index: 5 = distance to next waypoint
double dnwp; // distance to next waypoint
double dcc; // closest distance to current assigned course
double dnc; // closest distance to course from next to next to next wp
double aalt; // assigned altitude
double ahdg; // assigned heading
bool on_crs; // is the plane on course?
bool wp_change; // way point has changed
double tlm; // time when last message was sent
TransCode lmc; // code of last message
};
class FGApproach : public FGATC {
int bucket;
string active_runway;
double active_rw_hdg;
double active_rw_lon;
double active_rw_lat;
double active_rw_len;
int num_planes; // number of planes on the stack
PlaneApp planes[max_planes]; // Array of planes
string transmission;
bool first;
SGPropertyNode_ptr comm1_node;
SGPropertyNode_ptr comm2_node;
SGPropertyNode_ptr atcmenu_node;
SGPropertyNode_ptr atcopt0_node;
SGPropertyNode_ptr atcopt1_node;
SGPropertyNode_ptr atcopt2_node;
SGPropertyNode_ptr atcopt3_node;
SGPropertyNode_ptr atcopt4_node;
SGPropertyNode_ptr atcopt5_node;
SGPropertyNode_ptr atcopt6_node;
SGPropertyNode_ptr atcopt7_node;
SGPropertyNode_ptr atcopt8_node;
SGPropertyNode_ptr atcopt9_node;
// for failure modeling
string trans_ident; // transmitted ident
bool approach_failed; // approach failed?
public:
FGApproach(void);
~FGApproach(void);
void Init();
void Update(double dt);
// Add new plane to stack if not already registered
// Input: pid - id of plane (name)
// Output: "true" if added; "false" if already existend
void AddPlane(const string& pid);
// Remove plane from stack if out of range
int RemovePlane();
inline double get_bucket() const { return bucket; }
inline int get_pnum() const { return num_planes; }
inline const string& get_trans_ident() { return trans_ident; }
private:
void calc_wp( const int &i);
void update_plane_dat();
void get_active_runway();
void update_param(const int &i);
double round_alt( bool hl, double alt );
double angle_diff_deg( const double &a1, const double &a2);
void set_message(const string &s);
// ========================================================================
// get point2 given starting point1 and course and distance
// input: point1 = heading in degrees, distance
// input: course in degrees, distance
// output: point2 = heading in degrees, distance
// ========================================================================
void calc_cd_head_dist(const double &h1, const double &d1,
const double &course, const double &dist,
double *h2, double *d2);
// ========================================================================
// get heading and distance between two points; point2 ---> point1
// input: point1 = heading in degrees, distance
// input: point2 = heading in degrees, distance
// output: course in degrees, distance
// ========================================================================
void calc_hd_course_dist(const double &h1, const double &d1,
const double &h2, const double &d2,
double *course, double *dist);
// ========================================================================
// closest distance between a point and a straigt line in 2 dim.
// the input variables are given in (heading, distance)
// relative to a common point
// input: point = heading in degrees, distance
// input: straigt line = anker vector (heading in degrees, distance),
// heading of direction vector
// output: distance
// ========================================================================
double calc_psl_dist(const double &h1, const double &d1,
const double &h2, const double &d2,
const double &h3);
// Pointers to current users position
SGPropertyNode_ptr lon_node;
SGPropertyNode_ptr lat_node;
SGPropertyNode_ptr elev_node;
SGPropertyNode_ptr hdg_node;
SGPropertyNode_ptr speed_node;
SGPropertyNode_ptr etime_node;
//Update the transmission string
void UpdateTransmission(void);
friend istream& operator>> ( istream&, FGApproach& );
};
#endif // _FG_APPROACH_HXX

View file

@ -1,225 +0,0 @@
// atis.cxx - routines to generate the ATIS info string
// This is the implementation of the FGATIS class
//
// Written by David Luff, started October 2001.
//
// Copyright (C) 2001 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/compiler.h>
#include <stdlib.h> // atoi()
#include <stdio.h> // sprintf
#include <string>
SG_USING_STD(string);
#include STL_IOSTREAM
SG_USING_STD(cout);
#include <simgear/misc/sg_path.hxx>
#include <Environment/environment_mgr.hxx>
#include <Environment/environment.hxx>
#include <Main/fg_props.hxx>
#include <Main/globals.hxx>
#include <Airports/runways.hxx>
#include "atis.hxx"
#include "commlist.hxx"
#include "ATCutils.hxx"
#include "ATCmgr.hxx"
FGATIS::FGATIS() :
transmission(""),
trans_ident(""),
atis_failed(false),
refname("atis")
//type(ATIS)
{
_vPtr = globals->get_ATC_mgr()->GetVoicePointer(ATIS);
_voiceOK = (_vPtr == NULL ? false : true);
_type = ATIS;
}
FGATIS::~FGATIS() {
}
// Main update function - checks whether we are displaying or not the correct message.
void FGATIS::Update(double dt) {
if(_display) {
if(_displaying) {
// Check if we need to update the message
// - basically every hour and if the weather changes significantly at the station
} else {
// We need to get and display the message
UpdateTransmission();
//cout << "ATIS.CXX - calling ATCMgr to render transmission..." << endl;
Render(transmission, refname, true);
_displaying = true;
}
} else {
// We shouldn't be displaying
if(_displaying) {
//cout << "ATIS.CXX - calling NoRender()..." << endl;
NoRender(refname);
_displaying = false;
}
}
}
// Sets the actual broadcast ATIS transmission.
void FGATIS::UpdateTransmission() {
double visibility;
char buf[10];
int phonetic_id;
string phonetic_id_string;
string time_str = fgGetString("sim/time/gmt-string");
int hours;
// int minutes;
FGEnvironment stationweather =
((FGEnvironmentMgr *)globals->get_subsystem("environment"))
->getEnvironment(lat, lon, 0.0);
transmission = "";
// UK CAA radiotelephony manual indicated ATIS transmissions start with "This is"
// Not sure about rest of the world though.
transmission += "This_is ";
// transmitted station name.
transmission += name;
// Add "Information"
transmission += " information";
//cout << "In atis.cxx, time_str = " << time_str << '\n';
// Add the recording identifier
// For now we will assume we only transmit every hour
hours = atoi((time_str.substr(1,2)).c_str()); //Warning - this is fragile if the
//time string format changes
//cout << "In atis.cxx, hours = " << hours << endl;
phonetic_id = current_commlist->GetCallSign(ident, hours, 0);
phonetic_id_string = GetPhoneticIdent(phonetic_id);
transmission += " ";
transmission += phonetic_id_string;
// Output the recording time. - we'll just output the last whole hour for now.
// FIXME - this only gets GMT time but that appears to be all the clock outputs for now
//cout << "in atis.cxx, time = " << time_str << endl;
transmission = transmission + " / Weather " + ConvertNumToSpokenDigits((time_str.substr(0,3) + "00")) + " hours zulu";
// Get the temperature
int temp;
temp = (int)stationweather.get_temperature_degc();
// HACK ALERT - at the moment the new environment subsystem returns bogus temperatures
// FIXME - take out this hack when this gets fixed upstream
if((temp < -50) || (temp > 60)) {
temp = 15;
}
sprintf(buf, "%i", abs(temp));
transmission += " / Temperature ";
if(temp < 0) {
transmission += "minus ";
}
string tempstr1 = buf;
string tempstr2;
transmission += ConvertNumToSpokenDigits(tempstr1);
transmission += " degrees_Celsius";
// Get the visibility
visibility = stationweather.get_visibility_m();
sprintf(buf, "%i", int(visibility/1600));
transmission += " / Visibility ";
tempstr1 = buf;
transmission += ConvertNumToSpokenDigits(tempstr1);
transmission += " miles";
// Get the cloudbase
// FIXME: kludge for now
if (strcmp(fgGetString("/environment/clouds/layer[0]/type"), "clear")) {
double cloudbase =
fgGetDouble("/environment/clouds/layer[0]/elevation-ft");
// For some reason the altitude returned doesn't seem to correspond to the actual cloud altitude.
char buf3[10];
char buf4[10];
// cout << "cloudbase = " << cloudbase << endl;
sprintf(buf3, "%i", int(cloudbase)/1000);
sprintf(buf4, "%i", ((int)cloudbase % 1000)/100);
transmission += " / Cloudbase";
if(int(cloudbase)/1000) {
tempstr1 = buf3;
transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " thousand";
}
if(((int)cloudbase % 1000)/100) {
tempstr1 = buf4;
transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " hundred";
}
transmission += " feet";
}
// Get the pressure / altimeter
double P = fgGetDouble("/environment/pressure-sea-level-inhg");
if(ident.substr(0,2) == "EG" && fgGetBool("/sim/atc/use-millibars") == true) {
// Convert to millibars for the UK!
P *= 33.864;
sprintf(buf, "%.0f", P);
} else {
sprintf(buf, "%.2f", P);
}
transmission += " / Altimeter ";
tempstr1 = buf;
transmission += ConvertNumToSpokenDigits(tempstr1);
// Based on the airport-id and wind get the active runway
//FGRunway *r;
double speed = stationweather.get_wind_speed_kt();
double hdg = stationweather.get_wind_from_heading_deg();
if (speed == 0) {
hdg = 270; // This forces West-facing rwys to be used in no-wind situations
// which is consistent with Flightgear's initial setup.
transmission += " / Winds_light_and_variable";
} else {
// FIXME: get gust factor in somehow
char buf5[10];
char buf6[10];
sprintf(buf5, "%i", int(speed));
sprintf(buf6, "%i", int(hdg));
tempstr1 = buf5;
tempstr2 = buf6;
transmission = transmission + " / Winds " + ConvertNumToSpokenDigits(tempstr1) + " knots from "
+ ConvertNumToSpokenDigits(tempstr2) + " degrees";
}
string rwy_no = globals->get_runways()->search(ident, int(hdg));
if(rwy_no != "NN") {
transmission += " / Landing_and_departing_runway ";
transmission += ConvertRwyNumToSpokenString(atoi(rwy_no.c_str()));
//cout << "in atis.cxx, r.rwy_no = " << rwy_no << " r.id = " << r->id << " r.heading = " << r->heading << endl;
}
// Anything else?
transmission += " / Advise_controller_on_initial_contact_you_have ";
transmission += phonetic_id_string;
transmission += " /// ";
}

View file

@ -1,95 +0,0 @@
// atis.hxx -- ATIS class
//
// Written by David Luff, started October 2001.
// Based on nav.hxx by Curtis Olson, started April 2000.
//
// Copyright (C) 2001 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_ATIS_HXX
#define _FG_ATIS_HXX
#include <stdio.h>
#include <string>
#include <simgear/compiler.h>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/magvar/magvar.hxx>
#include <simgear/timing/sg_time.hxx>
#ifdef SG_HAVE_STD_INCLUDES
# include <istream>
# include <iomanip>
#elif defined( __BORLANDC__ ) || (__APPLE__)
# include <iostream>
#else
# include <istream.h>
# include <iomanip.h>
#endif
SG_USING_STD(istream);
SG_USING_STD(string);
#include "ATC.hxx"
//DCL - a complete guess for now.
#define FG_ATIS_DEFAULT_RANGE 30
class FGATIS : public FGATC {
//atc_type type;
string transmission; // The actual ATIS transmission
// This is not stored in default.atis but is generated
// from the prevailing conditions when required.
// for failure modeling
string trans_ident; // transmitted ident
bool atis_failed; // atis failed?
// Aircraft position
// ATIS is actually a special case in that unlike other ATC eg.tower it doesn't actually know about
// or the whereabouts of the aircraft it is transmitting to. However, to ensure consistancy of
// operation with the other ATC classes the ATIS class must calculate range to the aircraft in order
// to decide whether to render the transmission - hence the users plane details must be stored.
//SGPropertyNode_ptr airplane_lon_node;
//SGPropertyNode_ptr airplane_lat_node;
//SGPropertyNode_ptr airplane_elev_node;
public:
FGATIS(void);
~FGATIS(void);
//run the ATIS instance
void Update(double dt);
//inline void set_type(const atc_type tp) {type = tp;}
inline const string& get_trans_ident() { return trans_ident; }
inline void set_refname(const string& r) { refname = r; }
private:
string refname; // Holds the refname of a transmission in progress
//Update the transmission string
void UpdateTransmission(void);
friend istream& operator>> ( istream&, FGATIS& );
};
#endif // _FG_ATIS_HXX

View file

@ -1,346 +0,0 @@
// commlist.cxx -- comm frequency lookup class
//
// Written by David Luff and Alexander Kappes, started Jan 2003.
// Based on navlist.cxx by Curtis Olson, started April 2000.
//
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/debug/logstream.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/math/sg_random.h>
#include <simgear/bucket/newbucket.hxx>
#include <Airports/simple.hxx>
#include "commlist.hxx"
//#include "atislist.hxx"
#include "ATCutils.hxx"
FGCommList *current_commlist;
// Constructor
FGCommList::FGCommList( void ) {
}
// Destructor
FGCommList::~FGCommList( void ) {
}
// load the navaids and build the map
bool FGCommList::init( const SGPath& path ) {
SGPath temp = path;
commlist_freq.erase(commlist_freq.begin(), commlist_freq.end());
commlist_bck.erase(commlist_bck.begin(), commlist_bck.end());
temp.append( "ATC/default.atis" );
LoadComms(temp);
temp = path;
temp.append( "ATC/default.tower" );
LoadComms(temp);
temp = path;
temp.append( "ATC/default.ground" );
LoadComms(temp);
temp = path;
temp.append( "ATC/default.approach" );
LoadComms(temp);
return true;
}
bool FGCommList::LoadComms(const SGPath& path) {
sg_gzifstream fin( path.str() );
if ( !fin.is_open() ) {
SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
exit(-1);
}
// read in each line of the file
fin >> skipcomment;
#ifdef __MWERKS__
char c = 0;
while ( fin.get(c) && c != '\0' ) {
fin.putback(c);
#else
while ( !fin.eof() ) {
#endif
ATCData a;
fin >> a;
if(a.type == INVALID) {
SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
} else {
// Push all stations onto frequency map
commlist_freq[a.freq].push_back(a);
// Push non-atis stations onto bucket map as well
// In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
//if(a.type != ATIS) {
// get bucket number
SGBucket bucket(a.lon, a.lat);
int bucknum = bucket.gen_index();
commlist_bck[bucknum].push_back(a);
//}
}
fin >> skipcomment;
}
fin.close();
return true;
}
// query the database for the specified frequency, lon and lat are in
// degrees, elev is in meters
// If no atc_type is specified, it returns true if any non-invalid type is found
// If atc_type is specifed, returns true only if the specified type is found
bool FGCommList::FindByFreq( double lon, double lat, double elev, double freq,
ATCData* ad, atc_type tp )
{
lon *= SGD_DEGREES_TO_RADIANS;
lat *= SGD_DEGREES_TO_RADIANS;
// HACK - if freq > 1000 assume it's in KHz, otherwise assume MHz.
// A bit ugly but it works for now!!!!
comm_list_type stations;
if(freq > 1000.0) {
stations = commlist_freq[(int)freq];
} else {
stations = commlist_freq[(int)(freq*100.0 + 0.5)];
}
comm_list_iterator current = stations.begin();
comm_list_iterator last = stations.end();
// double az1, az2, s;
Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
Point3D station;
const double orig_max_d = 1e100;
double max_d = orig_max_d;
double d;
// TODO - at the moment this loop returns the first match found in range
// We want to return the closest match in the event of a frequency conflict
for ( ; current != last ; ++current ) {
//cout << "testing " << current->get_ident() << endl;
station = Point3D(current->x, current->y, current->z);
//cout << "aircraft = " << aircraft << endl;
//cout << "station = " << station << endl;
d = aircraft.distance3Dsquared( station );
//cout << " dist = " << sqrt(d)
// << " range = " << current->range * SG_NM_TO_METER << endl;
// TODO - match up to twice the published range so we can model
// reduced signal strength
// NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
if ( d < (current->range * SG_NM_TO_METER
* current->range * SG_NM_TO_METER ) ) {
//cout << "matched = " << current->ident << endl;
if((tp == INVALID) || (tp == (*current).type)) {
if(d < max_d) {
max_d = d;
*ad = *current;
}
}
}
}
if(max_d < orig_max_d) {
return true;
} else {
return false;
}
}
int FGCommList::FindByPos(double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp)
{
// number of relevant stations found within range
int found = 0;
stations->erase(stations->begin(), stations->end());
// get bucket number for plane position
SGBucket buck(lon, lat);
// get neigboring buckets
int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2);
int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 );
// loop over bucket range
for ( int i=-bx; i<=bx; i++) {
for ( int j=-by; j<=by; j++) {
buck = sgBucketOffset(lon, lat, i, j);
long int bucket = buck.gen_index();
comm_list_type Fstations = commlist_bck[bucket];
comm_list_iterator current = Fstations.begin();
comm_list_iterator last = Fstations.end();
double rlon = lon * SGD_DEGREES_TO_RADIANS;
double rlat = lat * SGD_DEGREES_TO_RADIANS;
// double az1, az2, s;
Point3D aircraft = sgGeodToCart( Point3D(rlon, rlat, elev) );
Point3D station;
double d;
for(; current != last; ++current) {
if((current->type == tp) || (tp == INVALID)) {
station = Point3D(current->x, current->y, current->z);
d = aircraft.distance3Dsquared( station );
// NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
if ( d < (current->range * SG_NM_TO_METER
* current->range * SG_NM_TO_METER ) ) {
stations->push_back(*current);
++found;
}
}
}
}
}
return found;
}
// Returns the distance in meters to the closest station of a given type,
// with the details written into ATCData& ad. If no type is specifed simply
// returns the distance to the closest station of any type.
// Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
// Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
// say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result
// and giving up after 1000.
double FGCommList::FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp, double max_range) {
int num_stations = 0;
int range = 10;
comm_list_type stations;
comm_list_iterator itr;
double distance = -9999.0;
while(num_stations == 0) {
num_stations = FindByPos(lon, lat, elev, range, &stations, tp);
if(num_stations) {
double closest = max_range * SG_NM_TO_METER;
double tmp;
for(itr = stations.begin(); itr != stations.end(); ++itr) {
ATCData ad2 = *itr;
//Point3D p1(*itr.lon, *itr.lat, *itr.elev);
Point3D p1(ad2.lon, ad2.lat, ad2.elev);
const FGAirport *a = fgFindAirportID(ad2.ident);
if (a) {
Point3D p2(lon, lat, elev);
tmp = dclGetHorizontalSeparation(p1, p2);
if(tmp <= closest) {
closest = tmp;
distance = tmp;
ad = *itr;
}
}
}
//cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
return(distance);
}
if(range > max_range) {
break;
}
range *= 10;
}
return(-9999.0);
}
// Find by Airport code.
// This is basically a wrapper for a call to the airport database to get the airport
// position followed by a call to FindByPos(...)
bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
const FGAirport *a = fgFindAirportID( ICAO);
if ( a) {
comm_list_type stations;
int found = FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 10.0, &stations, tp);
if(found) {
comm_list_iterator itr = stations.begin();
while(itr != stations.end()) {
if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
ad = *itr;
return true;
}
++itr;
}
}
} else {
return false;
}
return false;
}
// TODO - this function should move somewhere else eventually!
// Return an appropriate call-sign for an ATIS transmission.
int FGCommList::GetCallSign( const string& apt_id, int hours, int mins )
{
atis_transmission_type tran;
if(atislog.find(apt_id) == atislog.end()) {
// This station has not transmitted yet - return a random identifier
// and add the transmission to the log
tran.hours = hours;
tran.mins = mins;
sg_srandom_time();
tran.callsign = int(sg_random() * 25) + 1; // This *should* give a random int between 1 and 26
//atislog[apt_id].push_back(tran);
atislog[apt_id] = tran;
} else {
// This station has transmitted - calculate the appropriate identifier
// and add the transmission to the log if it has changed
tran = atislog[apt_id];
// This next bit assumes that no-one comes back to the same ATIS station
// after running FlightGear for more than 24 hours !!
if((tran.hours == hours) && (tran.mins == mins)) {
return(tran.callsign);
} else {
if(tran.hours == hours) {
// The minutes must have changed
tran.mins = mins;
tran.callsign++;
} else {
if(hours < tran.hours) {
hours += 24;
}
tran.callsign += (hours - tran.hours);
if(mins != 0) {
// Assume transmissions were made on every hour
tran.callsign++;
}
tran.hours = hours;
tran.mins = mins;
}
// Wrap if we've exceeded Zulu
if(tran.callsign > 26) {
tran.callsign -= 26;
}
// And write the new transmission to the log
atislog[apt_id] = tran;
}
}
return(tran.callsign);
}

View file

@ -1,140 +0,0 @@
// commlist.hxx -- comm frequency lookup class
//
// Written by David Luff and Alexander Kappes, started Jan 2003.
// Based on navlist.hxx by Curtis Olson, started April 2000.
//
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
/*****************************************************************
*
* FGCommList is used to store communication frequency information
* for the ATC and AI subsystems. Two maps are maintained - one
* searchable by location and one searchable by frequency. The
* data structure returned from the search is the ATCData struct
* defined in ATC.hxx, containing location, frequency, name, range
* and type of the returned station.
*
******************************************************************/
#ifndef _FG_COMMLIST_HXX
#define _FG_COMMLIST_HXX
#include <simgear/compiler.h>
#include <simgear/misc/sg_path.hxx>
#include <map>
#include <list>
#include <string>
#include "ATC.hxx"
#include "atis.hxx"
SG_USING_STD(list);
SG_USING_STD(map);
SG_USING_STD(vector);
SG_USING_STD(string);
// A list of ATC stations
typedef list < ATCData > comm_list_type;
typedef comm_list_type::iterator comm_list_iterator;
typedef comm_list_type::const_iterator comm_list_const_iterator;
// A map of ATC station lists
typedef map < int, comm_list_type > comm_map_type;
typedef comm_map_type::iterator comm_map_iterator;
typedef comm_map_type::const_iterator comm_map_const_iterator;
class FGCommList {
public:
FGCommList();
~FGCommList();
// load all comm frequencies and build the map
bool init( const SGPath& path );
// query the database for the specified frequency, lon and lat are
// in degrees, elev is in meters.
// If no atc_type is specified, it returns true if any non-invalid type is found.
// If atc_type is specifed, returns true only if the specified type is found.
// Returns the station closest to the supplied position.
// The data found is written into the passed-in ATCData structure.
bool FindByFreq( double lon, double lat, double elev, double freq, ATCData* ad, atc_type tp = INVALID );
// query the database by location, lon and lat are in degrees, elev is in meters, range is in nautical miles.
// Returns the number of stations of the specified atc_type tp that are in range of the position defined by
// lon, lat and elev, and pushes them into stations.
// If no atc_type is specifed, returns the number of all stations in range, and pushes them into stations
// ** stations is erased before use **
int FindByPos( double lon, double lat, double elev, double range, comm_list_type* stations, atc_type tp = INVALID );
// Returns the distance in meters to the closest station of a given type,
// with the details written into ATCData& ad. If no type is specifed simply
// returns the distance to the closest station of any type.
// Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
// Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
// say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result
// and giving up after 1000.
// !!!Be warned that searching anything over 100 miles will pause the sim unacceptably!!!
// (The ability to search longer ranges should be used during init only).
double FindClosest( double lon, double lat, double elev, ATCData& ad, atc_type tp = INVALID, double max_range = 100.0 );
// Find by Airport code.
bool FindByCode( const string& ICAO, ATCData& ad, atc_type tp = INVALID );
// Return the callsign for an ATIS transmission given transmission time and airport id
// This maybe should get moved somewhere else!!
int GetCallSign( const string& apt_id, int hours, int mins );
private:
// Comm stations mapped by frequency
comm_map_type commlist_freq;
// Comm stations mapped by bucket
comm_map_type commlist_bck;
// Load comms from a specified path (which must include the filename)
bool LoadComms(const SGPath& path);
//----------- This stuff is left over from atislist.[ch]xx and maybe should move somewhere else
// Add structure and map for storing a log of atis transmissions
// made in this session of FlightGear. This allows the callsign
// to be allocated correctly wrt time.
typedef struct {
int hours;
int mins;
int callsign;
} atis_transmission_type;
typedef map < string, atis_transmission_type > atis_log_type;
typedef atis_log_type::iterator atis_log_iterator;
typedef atis_log_type::const_iterator atis_log_const_iterator;
atis_log_type atislog;
//-----------------------------------------------------------------------------------------------
};
extern FGCommList *current_commlist;
#endif // _FG_COMMLIST_HXX

View file

@ -1,732 +0,0 @@
// FGGround - a class to provide ground control at larger 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/misc/sg_path.hxx>
#include <simgear/math/sg_random.h>
#include <simgear/debug/logstream.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/constants.h>
#include <Main/globals.hxx>
#include <stdlib.h>
#include STL_FSTREAM
#include "ground.hxx"
#include "ATCutils.hxx"
#include "AILocalTraffic.hxx"
#include "ATCmgr.hxx"
SG_USING_STD(ifstream);
SG_USING_STD(cout);
node::node() {
}
node::~node() {
for(unsigned int i=0; i < arcs.size(); ++i) {
delete arcs[i];
}
}
// Make sure that a_path.cost += distance is safe from the moment it's created.
a_path::a_path() {
cost = 0;
}
FGGround::FGGround() {
ATCmgr = globals->get_ATC_mgr();
_type = GROUND;
networkLoadOK = false;
ground_traffic.erase(ground_traffic.begin(), ground_traffic.end());
ground_traffic_itr = ground_traffic.begin();
// Init the property nodes - TODO - need to make sure we're getting surface winds.
wind_from_hdg = fgGetNode("/environment/wind-from-heading-deg", true);
wind_speed_knots = fgGetNode("/environment/wind-speed-kt", true);
// TODO - get the actual airport elevation
aptElev = 0.0;
}
FGGround::FGGround(const string& id) {
ATCmgr = globals->get_ATC_mgr();
networkLoadOK = false;
ground_traffic.erase(ground_traffic.begin(), ground_traffic.end());
ground_traffic_itr = ground_traffic.begin();
ident = id;
// Init the property nodes - TODO - need to make sure we're getting surface winds.
wind_from_hdg = fgGetNode("/environment/wind-from-heading-deg", true);
wind_speed_knots = fgGetNode("/environment/wind-speed-kt", true);
// TODO - get the actual airport elevation
aptElev = 0.0;
}
FGGround::~FGGround() {
}
void FGGround::ParseRwyExits(node* np, char* es) {
char* token;
char estr[20];
strcpy(estr, es);
const char delimiters[] = "-";
token = strtok(estr, delimiters);
while(token != NULL) {
int i = atoi(token);
//cout << "token = " << token << endl;
//cout << "rwy number = " << i << endl;
//runways[(atoi(token))].exits.push_back(np);
runways[i].exits.push_back(np);
//cout << "token = " << token << '\n';
token = strtok(NULL, delimiters);
}
}
// Load the ground logical network of the current instances airport
// Return true if successfull.
// TODO - currently the file is assumed to reside in the base/ATC directory.
// This might change to something more thought out in the future.
// NOTE - currently it is assumed that all nodes are loaded before any arcs.
// It won't work ATM if this doesn't hold true.
bool FGGround::LoadNetwork() {
node* np;
arc* ap;
Gate* gp;
int gateCount = 0; // This is used to allocate gateID's from zero upwards
// This may well change in the future - probably to reading in the real-world
// gate numbers from file.
ifstream fin;
SGPath path = globals->get_fg_root();
//string taxiPath = "ATC/" + ident + ".taxi";
string taxiPath = "ATC/KEMT.taxi"; // FIXME - HARDWIRED FOR TESTING
path.append(taxiPath);
SG_LOG(SG_ATC, SG_INFO, "Trying to read taxiway data for " << ident << "...");
//cout << "Trying to read taxiway data for " << ident << "..." << endl;
fin.open(path.c_str(), ios::in);
if(!fin) {
SG_LOG(SG_ATC, SG_ALERT, "Unable to open taxiway data input file " << path.c_str());
//cout << "Unable to open taxiway data input file " << path.c_str() << endl;
return(false);
}
char ch;
char buf[30];
while(!fin.eof()) {
fin >> buf;
// Node, arc, or [End]?
//cout << "Read in ground network element type = " << buf << endl;
if(!strcmp(buf, "[End]")) { // TODO - maybe make this more robust to spelling errors by just looking for '['
SG_LOG(SG_ATC, SG_INFO, "Done reading " << path.c_str() << endl);
break;
} else if(!strcmp(buf, "N")) {
// Node
np = new node;
np->struct_type = NODE;
fin >> buf;
np->nodeID = atoi(buf);
fin >> buf;
np->pos.setlon(atof(buf));
fin >> buf;
np->pos.setlat(atof(buf));
fin >> buf;
np->pos.setelev(atof(buf));
fin >> buf; // node type
if(!strcmp(buf, "J")) {
np->type = JUNCTION;
} else if(!strcmp(buf, "T")) {
np->type = TJUNCTION;
} else if(!strcmp(buf, "H")) {
np->type = HOLD;
} else {
SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown node type in taxi network...\n");
delete np;
return(false);
}
fin >> buf; // rwy exit information - gets parsed later - FRAGILE - will break if buf is reused.
// Now the name
fin >> ch; // strip the leading " off
np->name = "";
while(1) {
fin.unsetf(ios::skipws);
fin >> ch;
if((ch == '"') || (ch == 0x0A)) {
break;
} // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
np->name += ch;
}
fin.setf(ios::skipws);
network.push_back(np);
// FIXME - fragile - replies on buf not getting modified from exits read to here
// see if we also need to push it onto the runway exit list
//cout << "strlen(buf) = " << strlen(buf) << endl;
if(strlen(buf) > 2) {
//cout << "Calling ParseRwyExits for " << buf << endl;
ParseRwyExits(np, buf);
}
} else if(!strcmp(buf, "A")) {
ap = new arc;
ap->struct_type = ARC;
fin >> buf;
ap->n1 = atoi(buf);
fin >> buf;
ap->n2 = atoi(buf);
fin >> buf;
if(!strcmp(buf, "R")) {
ap->type = RUNWAY;
} else if(!strcmp(buf, "T")) {
ap->type = TAXIWAY;
} else {
SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown arc type in taxi network...\n");
delete ap;
return(false);
}
// directed?
fin >> buf;
if(!strcmp(buf, "Y")) {
ap->directed = true;
} else if(!strcmp(buf, "N")) {
ap->directed = false;
} else {
SG_LOG(SG_ATC, SG_ALERT, "**** ERROR ***** Unknown arc directed value in taxi network - should be Y/N !!!\n");
delete ap;
return(false);
}
// Now the name
ap->name = "";
while(1) {
fin.unsetf(ios::skipws);
fin >> ch;
ap->name += ch;
if((ch == '"') || (ch == 0x0A)) {
break;
} // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
}
fin.setf(ios::skipws);
ap->distance = (int)dclGetHorizontalSeparation(network[ap->n1]->pos, network[ap->n2]->pos);
//cout << "Distance = " << ap->distance << '\n';
network[ap->n1]->arcs.push_back(ap);
network[ap->n2]->arcs.push_back(ap);
} else if(!strcmp(buf, "G")) {
gp = new Gate;
gp->struct_type = NODE;
gp->type = GATE;
fin >> buf;
gp->nodeID = atoi(buf);
fin >> buf;
gp->pos.setlon(atof(buf));
fin >> buf;
gp->pos.setlat(atof(buf));
fin >> buf;
gp->pos.setelev(atof(buf));
fin >> buf; // gate type - ignore this for now
fin >> buf; // gate heading
gp->heading = atoi(buf);
// Now the name
gp->name = "";
while(1) {
fin.unsetf(ios::skipws);
fin >> ch;
gp->name += ch;
if((ch == '"') || (ch == 0x0A)) {
break;
} // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
}
fin.setf(ios::skipws);
gp->id = gateCount; // Warning - this will likely change in the future.
gp->used = false;
network.push_back(gp);
gates[gateCount] = gp;
gateCount++;
} else {
// Something has gone seriously pear-shaped
SG_LOG(SG_ATC, SG_ALERT, "********* ERROR - unknown ground network element type... aborting read of " << path.c_str() << '\n');
return(false);
}
fin >> skipeol;
}
return(true);
}
void FGGround::Init() {
untowered = false;
// Figure out which is the active runway - TODO - it would be better to have ground call tower
// for runway operation details, but at the moment we can't guarantee that tower control at a given airport
// will be initialised before ground so we can't do that.
DoRwyDetails();
//cout << "In FGGround::Init, active rwy is " << activeRwy << '\n';
ortho.Init(rwy.threshold_pos, rwy.hdg);
networkLoadOK = LoadNetwork();
}
void FGGround::Update(double dt) {
// 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.
// Lets take the example of a plane which has just contacted ground
// following landing - presumably requesting where to go?
// First we need to establish the position of the plane within the logical network.
// Next we need to decide where its going.
if(ground_traffic.size()) {
if(ground_traffic_itr == ground_traffic.end()) {
ground_traffic_itr = ground_traffic.begin();
}
//Process(*ground_traffic_itr);
GroundRec* g = *ground_traffic_itr;
if(g->taxiRequestOutstanding) {
double responseTime = 10.0; // seconds - this should get more sophisticated at some point
if(g->clearanceCounter > responseTime) {
// DO CLEARANCE
// TODO - move the mechanics of making up the transmission out of the main Update(...) routine.
string trns = "";
trns += g->plane.callsign;
trns += " taxi holding point runway "; // TODO - add the holding point name
// eg " taxi holding point G2 runway "
trns += ConvertRwyNumToSpokenString(activeRwy);
if(_display) {
fgSetString("/sim/messages/ground", trns.c_str());
}
g->planePtr->RegisterTransmission(1); // cleared to taxi
g->clearanceCounter = 0.0;
g->taxiRequestOutstanding = false;
} else {
g->clearanceCounter += (dt * ground_traffic.size());
}
} else if(((FGAILocalTraffic*)(g->planePtr))->AtHoldShort()) { // That's a hack - eventually we should monitor actual position
// HACK ALERT - the automatic cast to AILocalTraffic has to go once we have other sorts working!!!!! FIXME TODO
// NOTE - we don't need to do the contact tower bit unless we have separate tower and ground
string trns = g->plane.callsign;
trns += " contact Tower ";
double f = globals->get_ATC_mgr()->GetFrequency(ident, TOWER) / 100.0;
char buf[10];
sprintf(buf, "%.2f", f);
trns += buf;
if(_display) {
fgSetString("/sim/messages/ground", trns.c_str());
}
g->planePtr->RegisterTransmission(2); // contact tower
delete *ground_traffic_itr;
ground_traffic.erase(ground_traffic_itr);
ground_traffic_itr = ground_traffic.begin();
}
++ground_traffic_itr;
}
// Call the base class update for the response time handling.
FGATC::Update(dt);
}
// 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
// Copied from FGTower - TODO - it would be better to implement this just once, and have ground call tower
// for runway operation details, but at the moment we can't guarantee that tower control at a given airport
// will be initialised before ground so we can't do that.
void FGGround::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;
bool rwyGood = globals->get_runways()->search(ident, int(hdg), &runway);
if(rwyGood) {
activeRwy = runway._rwy_no;
rwy.rwyID = runway._rwy_no;
SG_LOG(SG_ATC, SG_INFO, "In FGGround, active runway for airport " << ident << " is " << activeRwy);
// Get the threshold position
double other_way = runway._heading - 180.0;
while(other_way <= 0.0) {
other_way += 360.0;
}
// move to the +l end/center of the runway
//cout << "Runway center is at " << runway._lon << ", " << runway._lat << '\n';
Point3D origin = Point3D(runway._lon, runway._lat, aptElev);
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(), runway._heading,
rwy.length / 2.0 - 25.0, &tolat, &tolon, &tor );
// Note - 25 meters in from the runway end is a bit of a hack to put the plane ahead of the user.
// now copy what we need out of runway into rwy
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 = runway._heading;
// Set the projection for the local area based on this active runway
ortho.Init(rwy.threshold_pos, rwy.hdg);
rwy.end1ortho = ortho.ConvertToLocal(rwy.threshold_pos); // should come out as zero
rwy.end2ortho = ortho.ConvertToLocal(takeoff_end);
} else {
SG_LOG(SG_ATC, SG_ALERT, "Help - can't get good runway in FGTower!!");
activeRwy = "NN";
}
}
// Return a random gate ID of an unused gate.
// Two error values may be returned and must be checked for by the calling function:
// -2 signifies that no gates exist at this airport.
// -1 signifies that all gates are currently full.
int FGGround::GetRandomGateID() {
// Check that this airport actually has some gates!!
if(!gates.size()) {
return(-2);
}
gate_vec_type gateVec;
int num = 0;
int thenum;
int ID;
gatesItr = gates.begin();
while(gatesItr != gates.end()) {
if((gatesItr->second)->used == false) {
gateVec.push_back(gatesItr->second);
num++;
}
++gatesItr;
}
// Check that there are some unused gates!
if(!gateVec.size()) {
return(-1);
}
// Randomly select one from the list
sg_srandom_time();
thenum = (int)(sg_random() * gateVec.size());
ID = gateVec[thenum]->id;
return(ID);
}
// Return a pointer to an unused gate node
Gate* FGGround::GetGateNode() {
int id = GetRandomGateID();
if(id < 0) {
return(NULL);
} else {
return(gates[id]);
}
}
node* FGGround::GetHoldShortNode(const string& rwyID) {
return(NULL); // TODO - either implement me or remove me!!!
}
// WARNING - This is hardwired to my prototype logical network format
// and will almost certainly change when Bernie's stuff comes on-line.
// Returns NULL if it can't find a valid node.
node* FGGround::GetThresholdNode(const string& rwyID) {
// For now go through all the nodes and parse their names
// Maybe in the future we'll map threshold nodes by ID
//cout << "Size of network is " << network.size() << '\n';
for(unsigned int i=0; i<network.size(); ++i) {
//cout << "Name = " << network[i]->name << '\n';
if(network[i]->name.size()) {
string s = network[i]->name;
// Warning - the next bit is fragile and dependent on my current naming scheme
//cout << "substr = " << s.substr(0,3) << '\n';
//cout << "size of s = " << s.size() << '\n';
if(s.substr(0,3) == "rwy") {
//cout << "subsubstr = " << s.substr(4, s.size() - 4) << '\n';
if(s.substr(4, s.size() - 4) == rwyID) {
return network[i];
}
}
}
}
return NULL;
}
// Get a path from a point on a runway to a gate
// TODO !!
// Get a path from a node to another node
// Eventually we will need complex algorithms for this taking other traffic,
// shortest path and suitable paths into accout.
// For now we'll just call the shortest path algorithm.
ground_network_path_type FGGround::GetPath(node* A, node* B) {
return(GetShortestPath(A, B));
};
// Get a path from a node to a runway threshold
ground_network_path_type FGGround::GetPath(node* A, const string& rwyID) {
node* b = GetThresholdNode(rwyID);
if(b == NULL) {
SG_LOG(SG_ATC, SG_ALERT, "ERROR - unable to find path to runway theshold in ground.cxx for airport " << ident << '\n');
ground_network_path_type emptyPath;
emptyPath.erase(emptyPath.begin(), emptyPath.end());
return(emptyPath);
}
return GetShortestPath(A, b);
}
// Get a path from a node to a runway hold short point
// Bit of a hack this at the moment!
ground_network_path_type FGGround::GetPathToHoldShort(node* A, const string& rwyID) {
ground_network_path_type path = GetPath(A, rwyID);
path.pop_back(); // That should be the threshold stripped of
path.pop_back(); // and that should be the arc from hold short to threshold
// This isn't robust though - TODO - implement properly!
return(path);
}
// A shortest path algorithm from memory (ie. I can't find the bl&*dy book again!)
// I'm sure there must be enchancements that we can make to this, such as biasing the
// order in which the nodes are searched out from in favour of those geographically
// closer to the destination.
// Note that we are working with the master set of nodes and arcs so we mustn't change
// or delete them - we only delete the paths that we create during the algorithm.
ground_network_path_type FGGround::GetShortestPath(node* A, node* B) {
a_path* pathPtr;
shortest_path_map_type pathMap;
node_array_type nodesLeft;
// Debugging check
int pathsCreated = 0;
// Initialise the algorithm
nodesLeft.push_back(A);
pathPtr = new a_path;
pathsCreated++;
pathPtr->path.push_back(A);
pathPtr->cost = 0;
pathMap[A->nodeID] = pathPtr;
bool solution_found = false; // Flag to indicate that at least one candidate path has been found
int solution_cost = -1; // Cost of current best cost solution. -1 indicates no solution found yet.
a_path solution_path;
node* nPtr; // nPtr is used to point to the node we are currently working with
while(nodesLeft.size()) {
//cout << "\n*****nodesLeft*****\n";
//for(unsigned int i=0; i<nodesLeft.size(); ++i) {
//cout << nodesLeft[i]->nodeID << '\n';
//}
//cout << "*******************\n\n";
nPtr = *nodesLeft.begin(); // Thought - definate optimization possibilities here in the choice of which nodes we process first.
nodesLeft.erase(nodesLeft.begin());
//cout << "Walking out from node " << nPtr->nodeID << '\n';
for(unsigned int i=0; i<nPtr->arcs.size(); ++i) {
//cout << "ARC TO " << ((nPtr->arcs[i]->n1 == nPtr->nodeID) ? nPtr->arcs[i]->n2 : nPtr->arcs[i]->n1) << '\n';
}
if((solution_found) && (solution_cost <= pathMap[nPtr->nodeID]->cost)) {
// Do nothing - we've already found a solution and this partial path is already more expensive
} else {
// This path could still be better than the current solution - check it out
for(unsigned int i=0; i<(nPtr->arcs.size()); i++) {
// Map the new path against the end node, ie. *not* the one we just started with.
unsigned int end_nodeID = ((nPtr->arcs[i]->n1 == nPtr->nodeID) ? nPtr->arcs[i]->n2 : nPtr->arcs[i]->n1);
//cout << "end_nodeID = " << end_nodeID << '\n';
//cout << "pathMap size is " << pathMap.size() << '\n';
if(end_nodeID == nPtr->nodeID) {
//cout << "Circular arc!\n";
// Then its a circular arc - don't bother!!
//nPtr->arcs.erase(nPtr->arcs.begin() + i);
} else {
// see if the end node is already in the map or not
if(pathMap.find(end_nodeID) == pathMap.end()) {
//cout << "Not in the map" << endl;;
// Not in the map - easy!
pathPtr = new a_path;
pathsCreated++;
*pathPtr = *pathMap[nPtr->nodeID]; // *copy* the path
pathPtr->path.push_back(nPtr->arcs[i]);
pathPtr->path.push_back(network[end_nodeID]);
pathPtr->cost += nPtr->arcs[i]->distance;
pathMap[end_nodeID] = pathPtr;
nodesLeft.push_back(network[end_nodeID]); // By definition this can't be in the list already, or
// it would also have been in the map and hence OR'd with this one.
if(end_nodeID == B->nodeID) {
//cout << "Solution found!!!" << endl;
// Since this node wasn't in the map this is by definition the first solution
solution_cost = pathPtr->cost;
solution_path = *pathPtr;
solution_found = true;
}
} else {
//cout << "Already in the map" << endl;
// In the map - not so easy - need to get rid of an arc from the higher cost one.
//cout << "Current cost of node " << end_nodeID << " is " << pathMap[end_nodeID]->cost << endl;
int newCost = pathMap[nPtr->nodeID]->cost + nPtr->arcs[i]->distance;
//cout << "New cost is of node " << nPtr->nodeID << " is " << newCost << endl;
if(newCost >= pathMap[end_nodeID]->cost) {
// No need to do anything.
//cout << "Not doing anything!" << endl;
} else {
delete pathMap[end_nodeID];
pathsCreated--;
pathPtr = new a_path;
pathsCreated++;
*pathPtr = *pathMap[nPtr->nodeID]; // *copy* the path
pathPtr->path.push_back(nPtr->arcs[i]);
pathPtr->path.push_back(network[end_nodeID]);
pathPtr->cost += nPtr->arcs[i]->distance;
pathMap[end_nodeID] = pathPtr;
// We need to add this node to the list-to-do again to force a recalculation
// onwards from this node with the new lower cost to node cost.
nodesLeft.push_back(network[end_nodeID]);
if(end_nodeID == B->nodeID) {
//cout << "Solution found!!!" << endl;
// Need to check if there is a previous better solution
if((solution_cost < 0) || (pathPtr->cost < solution_cost)) {
solution_cost = pathPtr->cost;
solution_path = *pathPtr;
solution_found = true;
}
}
}
}
}
}
}
}
// delete all the paths before returning
shortest_path_map_iterator spItr = pathMap.begin();
while(spItr != pathMap.end()) {
if(spItr->second != NULL) {
delete spItr->second;
--pathsCreated;
}
++spItr;
}
//cout << "pathsCreated = " << pathsCreated << '\n';
if(pathsCreated > 0) {
SG_LOG(SG_ATC, SG_ALERT, "WARNING - Possible memory leak in FGGround::GetShortestPath\n\
Please report to flightgear-devel@flightgear.org\n");
}
//cout << (solution_found ? "Result: solution found\n" : "Result: no solution found\n");
return(solution_path.path); // TODO - we really ought to have a fallback position incase a solution isn't found.
}
// Randomly or otherwise populate some of the gates with parked planes
// (might eventually be done by the AIMgr if and when lots of AI traffic is generated)
// Return a list of exits from a given runway
// It is up to the calling function to check for non-zero size of returned array before use
node_array_type FGGround::GetExits(const string& rwyID) {
// FIXME - get a 07L or similar in here and we're stuffed!!!
return(runways[atoi(rwyID.c_str())].exits);
}
void FGGround::RequestDeparture(const PlaneRec& plane, FGAIEntity* requestee) {
// For now we'll just automatically clear all planes to the runway hold.
// This communication needs to be delayed 20 sec or so from receiving the request.
// Even if display=false we still need to start the timer in case display=true when communication starts.
// We also need to bear in mind we also might have other outstanding communications, although for now we'll punt that issue!
// FIXME - sort the above!
// HACK - assume that anything requesting departure is new for now - FIXME LATER
GroundRec* g = new GroundRec;
g->plane = plane;
g->planePtr = requestee;
g->taxiRequestOutstanding = true;
g->clearanceCounter = 0;
g->cleared = false;
g->incoming = false;
// TODO - need to handle the next 3 as well
//Point3D current_pos;
//node* destination;
//node* last_clearance;
ground_traffic.push_back(g);
}
#if 0
void FGGround::NewArrival(plane_rec plane) {
// What are we going to do here?
// We need to start a new ground_rec and add the plane_rec to it
// We need to decide what gate we are going to clear it to.
// Then we need to add clearing it to that gate to the pending transmissions queue? - or simply transmit?
// Probably simply transmit for now and think about a transmission queue later if we need one.
// We might need one though in order to add a little delay for response time.
ground_rec* g = new ground_rec;
g->plane_rec = plane;
g->current_pos = ConvertWGS84ToXY(plane.pos);
g->node = GetNode(g->current_pos); // TODO - might need to sort out node/arc here
AssignGate(g);
g->cleared = false;
ground_traffic.push_back(g);
NextClearance(g);
}
void FGGround::NewContact(plane_rec plane) {
// This is a bit of a convienience function at the moment and is likely to change.
if(at a gate or apron)
NewDeparture(plane);
else
NewArrival(plane);
}
void FGGround::NextClearance(ground_rec &g) {
// Need to work out where we can clear g to.
// Assume the pilot doesn't need progressive instructions
// We *should* already have a gate or holding point assigned by the time we get here
// but it wouldn't do any harm to check.
// For now though we will hardwire it to clear to the final destination.
}
void FGGround::AssignGate(ground_rec &g) {
// We'll cheat for now - since we only have the user's aircraft and a couple of airports implemented
// we'll hardwire the gate!
// In the long run the logic of which gate or area to send the plane to could be somewhat non-trivial.
}
#endif //0

View file

@ -1,367 +0,0 @@
// FGGround - a class to provide ground control at larger 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_GROUND_HXX
#define _FG_GROUND_HXX
#include <map>
#include <vector>
#include <list>
#include <simgear/math/point3d.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/props/props.hxx>
#include "ATC.hxx"
#include "ATCProjection.hxx"
#include STL_IOSTREAM
#include STL_STRING
SG_USING_STD(string);
SG_USING_STD(ios);
SG_USING_STD(map);
SG_USING_STD(vector);
SG_USING_STD(list);
class FGAIEntity;
class FGATCMgr;
//////////////////////////////////////////////////////
// Types for the logical network data structure
enum arc_type {
RUNWAY,
TAXIWAY
};
enum node_type {
GATE,
APRON,
HOLD,
JUNCTION,
TJUNCTION
};
enum GateType {
TRANSPORT_PASSENGER,
TRANSPORT_PASSENGER_NARROWBODY,
TRANSPORT_PASSENGER_WIDEBODY,
TRANSPORT_CARGO,
GA_LOCAL,
GA_LOCAL_SINGLE,
GA_LOCAL_TWIN,
GA_TRANSIENT,
GA_TRANSIENT_SINGLE,
GA_TRANSIENT_TWIN,
OTHER // ie. anything goes!!
};
enum network_element_type {
NODE,
ARC
};
struct ground_network_element {
network_element_type struct_type;
};
struct arc : public ground_network_element {
int distance;
string name;
arc_type type;
bool directed; //false if 2-way, true if 1-way.
//This is a can of worms since arcs might be one way in different directions under different circumstances
unsigned int n1; // The nodeID of the first node
unsigned int n2; // The nodeID of the second node
// If the arc is directed then flow is normally from n1 to n2. See the above can of worms comment though.
};
typedef vector <arc*> arc_array_type; // This was and may become again a list instead of vector
typedef arc_array_type::iterator arc_array_iterator;
typedef arc_array_type::const_iterator arc_array_const_iterator;
struct node : public ground_network_element {
node();
~node();
unsigned int nodeID; //each node in an airport needs a unique ID number - this is ZERO-BASED to match array position
Point3D pos;
Point3D orthoPos;
string name;
node_type type;
arc_array_type arcs;
double max_turn_radius;
};
typedef vector <node*> node_array_type;
typedef node_array_type::iterator node_array_iterator;
typedef node_array_type::const_iterator node_array_const_iterator;
struct Gate : public node {
GateType gateType;
int max_weight; //units??
//airline_code airline; //For the future - we don't have any airline codes ATM
int id; // The gate number in the logical scheme of things
string name; // The real-world gate letter/number
//node* pNode;
bool used;
double heading; // The direction the parked-up plane should point in degrees
};
typedef vector < Gate* > gate_vec_type;
typedef gate_vec_type::iterator gate_vec_iterator;
typedef gate_vec_type::const_iterator gate_vec_const_iterator;
// A map of gate vs. the logical (internal FGFS) gate ID
typedef map < int, Gate* > gate_map_type;
typedef gate_map_type::iterator gate_map_iterator;
typedef gate_map_type::const_iterator gate_map_const_iterator;
// Runways - all the runway stuff is likely to change in the future
struct Rwy {
int id; //note this is a very simplified scheme for now - R & L are not differentiated
//It should work for simple one rwy airports
node_array_type exits; //Array of available exits from runway
// should probably add an FGRunway structure here as well eventually
// Eventually we will also want some encoding of real-life preferred runways
// This will get us up and running for single runway airports though.
};
typedef vector < Rwy > runway_array_type;
typedef runway_array_type::iterator runway_array_iterator;
typedef runway_array_type::const_iterator runway_array_const_iterator;
// end logical network types
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// Structures to use the network
// A path through the network
typedef vector < ground_network_element* > ground_network_path_type;
typedef ground_network_path_type::iterator ground_network_path_iterator;
typedef ground_network_path_type::const_iterator ground_network_path_const_iterator;
//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////
//
// Stuff for the shortest-path algorithms
struct a_path {
a_path();
ground_network_path_type path;
int cost;
};
// Paths mapped by nodeID reached so-far
typedef map < unsigned int, a_path* > shortest_path_map_type;
typedef shortest_path_map_type::iterator shortest_path_map_iterator;
// Nodes mapped by their ID
//typedef map < unsigned int, node* > node_map_type;
//typedef node_map_type::iterator node_map_iterator;
////////////////////////////////////////////////
// Planes active within the ground network.
// A more specialist plane rec to include ground information
struct GroundRec {
FGAIEntity* planePtr; // This might move to the planeRec eventually
PlaneRec plane;
Point3D current_pos;
node* destination;
node* last_clearance;
bool taxiRequestOutstanding; // Plane has requested taxi and we haven't responded yet
double clearanceCounter; // Hack for communication timing - counter since clearance requested in seconds
bool cleared; // set true when the plane has been cleared to somewhere
bool incoming; //true for arrivals, false for departures
// status?
// Almost certainly need to add more here
};
typedef list < GroundRec* > ground_rec_list;
typedef ground_rec_list::iterator ground_rec_list_itr;
typedef ground_rec_list::const_iterator ground_rec_list_const_itr;
//////////////////////////////////////////////////////////////////////////////////////////
// Hack
// perhaps we could use an FGRunway instead of this
struct GRunwayDetails {
Point3D threshold_pos;
Point3D end1ortho; // ortho projection end1 (the threshold ATM)
Point3D end2ortho; // ortho projection end2 (the take off end in the current hardwired scheme)
double hdg; // true runway heading
double length; // In *METERS*
string rwyID;
};
///////////////////////////////////////////////////////////////////////////////
//
// FGGround
//
///////////////////////////////////////////////////////////////////////////////
class FGGround : public FGATC {
public:
FGGround();
FGGround(const string& id);
~FGGround();
void Init();
void Update(double dt);
inline const string& get_trans_ident() { return trans_ident; }
// Contact ground control on arrival, assumed to request any gate
//void NewArrival(plane_rec plane);
// Contact ground control on departure, assumed to request currently active runway.
void RequestDeparture(const PlaneRec& plane, FGAIEntity* requestee);
// Contact ground control when the calling routine doesn't know if arrival
// or departure is appropriate.
//void NewContact(plane_rec plane);
// Make a request of ground control.
//void Request(ground_request request);
// Randomly fill some of the available gates and GA parking spots with planes
void PopulateGates();
// Return a suitable gate (maybe this should be a list of suitable gates so the plane or controller can choose the closest one)
void ReturnGate(Gate &gate, GateType type);
// Return a pointer to an unused gate
Gate* GetGateNode();
// Return a pointer to a hold short node
node* GetHoldShortNode(const string& rwyID);
// Runway stuff - this might change in the future.
// Get a list of exits from a given runway
// It is up to the calling function to check for non-zero size of returned array before use
node_array_type GetExits(const string& rwyID);
// Get a path from one node to another
ground_network_path_type GetPath(node* A, node* B);
// Get a path from a node to a runway threshold
ground_network_path_type GetPath(node* A, const string& rwyID);
// Get a path from a node to a runway hold short point
// Bit of a hack this at the moment!
ground_network_path_type GetPathToHoldShort(node* A, const string& rwyID);
private:
FGATCMgr* ATCmgr;
// This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
// Need a data structure to hold details of the various active planes
// Need a data structure to hold details of the logical network
// including which gates are filled - or possibly another data structure
// with the positions of the inactive planes.
// Need a data structure to hold outstanding communications from aircraft.
// Possibly need a data structure to hold outstanding communications to aircraft.
// The logical network
// NODES WILL BE STORED IN THE NETWORK IN ORDER OF nodeID NUMBER
// ie. NODE 5 WILL BE AT network[5]
node_array_type network;
// A map of all the gates indexed against internal (FGFS) ID
gate_map_type gates;
gate_map_iterator gatesItr;
FGATCAlignedProjection ortho;
// Planes currently active
//ground_rec_list ground_traffic;
// Find the shortest route through the logical network between two points.
//FindShortestRoute(point a, point b);
// Assign a gate or parking location to a new arrival
//AssignGate(ground_rec &g);
// Generate the next clearance for an airplane
//NextClearance(ground_rec &g);
// environment - need to make sure we're getting the surface winds and not winds aloft.
SGPropertyNode_ptr wind_from_hdg; //degrees
SGPropertyNode_ptr wind_speed_knots; //knots
// for failure modeling
string trans_ident; // transmitted ident
bool ground_failed; // ground failed?
bool networkLoadOK; // Indicates whether LoadNetwork returned true or false at last attempt
// Tower control
bool untowered; // True if this is an untowered airport (we still need the ground class for shortest path implementation etc
//FGATC* tower; // Pointer to the tower control
// Logical runway details - this might change in the future.
//runway_array_type runways; // STL way
Rwy runways[37]; // quick hack!
// Physical runway details
double aptElev; // Airport elevation
string activeRwy; // Active runway number - For now we'll disregard multiple / alternate runway operation.
RunwayDetails rwy; // Assumed to be the active one for now.// 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
// Copied from FGTower - TODO - it would be better to implement this just once, and have ground call tower
// for runway operation details, but at the moment we can't guarantee that tower control at a given airport
// will be initialised before ground so we can't do that.
void DoRwyDetails();
// Load the logical ground network for this airport from file.
// Return true if successfull.
bool LoadNetwork();
// Parse a runway exit string and push the supplied node pointer onto the runway exit list
void ParseRwyExits(node* np, char* es);
// Return a random gate ID of an unused gate.
// Two error values may be returned and must be checked for by the calling function:
// -2 signifies that no gates exist at this airport.
// -1 signifies that all gates are currently full.
// TODO - modify to return a suitable gate based on aircraft size/weight.
int GetRandomGateID();
// Return a pointer to the node at a runway threshold
// Returns NULL if unsuccessful.
node* GetThresholdNode(const string& rwyID);
// A shortest path algorithm from memory (I can't find the bl&*dy book again!)
ground_network_path_type GetShortestPath(node* A, node* B);
// Planes
ground_rec_list ground_traffic;
ground_rec_list_itr ground_traffic_itr;
};
#endif // _FG_GROUND_HXX

File diff suppressed because it is too large Load diff

View file

@ -1,365 +0,0 @@
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_TOWER_HXX
#define _FG_TOWER_HXX
#include <simgear/compiler.h>
#include <simgear/math/point3d.hxx>
#include <simgear/misc/sgstream.hxx>
#include <plib/sg.h>
#include STL_IOSTREAM
#include STL_STRING
SG_USING_STD(string);
SG_USING_STD(ios);
#include "ATC.hxx"
#include "ATCProjection.hxx"
#include "AIPlane.hxx"
class FGATCMgr;
class FGGround;
//DCL - a complete guess for now.
#define FG_TOWER_DEFAULT_RANGE 30
enum tower_traffic_type {
CIRCUIT,
INBOUND, // CIRCUIT traffic gets changed to INBOUND when on final of the full-stop circuit.
OUTBOUND,
TTT_UNKNOWN, // departure, but we don't know if for circuits or leaving properly
STRAIGHT_IN
};
ostream& operator << (ostream& os, tower_traffic_type ttt);
enum tower_callback_type {
USER_REQUEST_VFR_DEPARTURE = 1,
USER_REQUEST_VFR_ARRIVAL = 2,
USER_REQUEST_VFR_ARRIVAL_FULL_STOP = 3,
USER_REQUEST_VFR_ARRIVAL_TOUCH_AND_GO = 4,
USER_REPORT_3_MILE_FINAL = 5,
USER_REPORT_DOWNWIND = 6,
USER_REPORT_RWY_VACATED = 7,
USER_REPORT_GOING_AROUND = 8,
USER_REQUEST_TAKE_OFF = 9
};
// TODO - need some differentiation of IFR and VFR traffic in order to give the former priority.
// Structure for holding details of a plane under tower control.
// Not fixed yet - may include more stuff later.
class TowerPlaneRec {
public:
TowerPlaneRec();
TowerPlaneRec(const PlaneRec& p);
TowerPlaneRec(const Point3D& pt);
TowerPlaneRec(const PlaneRec& p, const Point3D& pt);
FGAIPlane* planePtr; // This might move to the planeRec eventually
PlaneRec plane;
Point3D pos;
double eta; // seconds
double dist_out; // meters from theshold
bool clearedToLand;
bool clearedToLineUp;
bool clearedToTakeOff;
// ought to add time cleared to depart so we can nag if necessary
bool holdShortReported;
bool lineUpReported;
bool downwindReported;
bool longFinalReported;
bool longFinalAcknowledged;
bool finalReported;
bool finalAcknowledged;
bool rwyVacatedReported;
bool rwyVacatedAcknowledged;
bool goAroundReported; // set true if plane informs tower that it's going around.
bool instructedToGoAround; // set true if plane told by tower to go around.
bool onRwy; // is physically on the runway
bool nextOnRwy; // currently projected by tower to be the next on the runway
bool gearWasUp; // Tell to ATC about gear
bool gearUpReported; // Tell to pilot about landing gear
bool vfrArrivalReported;
bool vfrArrivalAcknowledged;
// Type of operation the plane is doing
tower_traffic_type opType;
// Whereabouts in circuit if doing circuits
PatternLeg leg;
LandingType landingType;
bool isUser; // true if this plane is the user
};
typedef list < TowerPlaneRec* > tower_plane_rec_list_type;
typedef tower_plane_rec_list_type::iterator tower_plane_rec_list_iterator;
typedef tower_plane_rec_list_type::const_iterator tower_plane_rec_list_const_iterator;
class FGTower : public FGATC {
public:
FGTower();
~FGTower();
void Init();
void Update(double dt);
void ReceiveUserCallback(int code);
// 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.
void VFRArrivalContact(const string& ID, const LandingType& opt = AIP_LT_UNKNOWN);
// For the AI planes...
void VFRArrivalContact(const PlaneRec& plane, FGAIPlane* requestee, const LandingType& lt = AIP_LT_UNKNOWN);
void RequestDepartureClearance(const string& ID);
void RequestTakeOffClearance(const string& ID);
void ReportFinal(const string& ID);
void ReportLongFinal(const string& ID);
void ReportOuterMarker(const string& ID);
void ReportMiddleMarker(const string& ID);
void ReportInnerMarker(const string& ID);
void ReportRunwayVacated(const string& ID);
void ReportReadyForDeparture(const string& ID);
void ReportDownwind(const string& ID);
void ReportGoingAround(const string& ID);
// Contact tower when at a hold short for departure - for now we'll assume plane - maybe vehicles might want to cross runway eventually?
void ContactAtHoldShort(const PlaneRec& plane, FGAIPlane* requestee, tower_traffic_type operation);
// Register the presence of an AI plane at a point where contact would already have been made in real life
// CAUTION - currently it is assumed that this plane's callsign is unique - it is up to AIMgr to generate unique callsigns.
void RegisterAIPlane(const PlaneRec& plane, FGAIPlane* ai, const tower_traffic_type& op, const PatternLeg& lg = LEG_UNKNOWN);
// Deregister and remove an AI plane.
void DeregisterAIPlane(const string& id);
// Public interface to the active runway - this will get more complex
// in the future and consider multi-runway use, airplane weight etc.
inline const string& GetActiveRunway() const { return activeRwy; }
inline const RunwayDetails& GetActiveRunwayDetails() const { return rwy; }
// Get the pattern direction of the active rwy.
inline int GetPatternDirection() const { return rwy.patternDirection; }
inline const string& get_trans_ident() const { return trans_ident; }
inline FGGround* const GetGroundPtr() const { return ground; }
// 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 GetCrosswindConstraint(double& cpos);
bool GetDownwindConstraint(double& dpos);
bool GetBaseConstraint(double& bpos);
string GenText(const string& m, int c);
string GetWeather();
string GetATISID();
private:
FGATCMgr* ATCmgr;
// This is purely for synactic convienience to avoid writing globals->get_ATC_mgr()-> all through the code!
// Respond to a transmission
void Respond();
void ProcessRunwayVacatedReport(TowerPlaneRec* t);
void ProcessDownwindReport(TowerPlaneRec* t);
// Remove all options from the user dialog choice
void RemoveAllUserDialogOptions();
// Periodic checks on the various traffic.
void CheckHoldList(double dt);
void CheckCircuitList(double dt);
void CheckRunwayList(double dt);
void CheckApproachList(double dt);
void CheckDepartureList(double dt);
// 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 ClearHoldingPlane(TowerPlaneRec* t);
// Find a pointer to plane of callsign ID within the internal data structures
TowerPlaneRec* FindPlane(const string& ID);
// Remove and delete all instances of a plane with a given ID
void RemovePlane(const string& ID);
// Figure out if a given position lies on the active runway
// Might have to change when we consider more than one active rwy.
bool OnActiveRunway(const Point3D& pt);
// Figure out if a given position lies on a runway or not
bool OnAnyRunway(const Point3D& pt, bool onGround);
// Calculate the eta of a plane to the threshold.
// For ground traffic this is the fastest they can get there.
// For air traffic this is the middle approximation.
void CalcETA(TowerPlaneRec* tpr, bool printout = false);
// Iterate through all the lists and call CalcETA for all the planes.
void doThresholdETACalc();
// Order the list of traffic as per expected threshold use and flag any conflicts
bool doThresholdUseOrder();
// Calculate the crow-flys distance of a plane to the threshold in meters
double CalcDistOutM(TowerPlaneRec* tpr);
// Calculate the crow-flys distance of a plane to the threshold in miles
double CalcDistOutMiles(TowerPlaneRec* tpr);
/*
void doCommunication();
*/
void IssueLandingClearance(TowerPlaneRec* tpr);
void IssueGoAround(TowerPlaneRec* tpr);
void IssueDepartureClearance(TowerPlaneRec* tpr);
unsigned int update_count; // Convienince counter for speading computational load over several updates
unsigned int update_count_max; // ditto.
double timeSinceLastDeparture; // Time in seconds since last departure from active rwy.
bool departed; // set true when the above needs incrementing with time, false when it doesn't.
// environment - need to make sure we're getting the surface winds and not winds aloft.
SGPropertyNode_ptr wind_from_hdg; //degrees
SGPropertyNode_ptr wind_speed_knots; //knots
double aptElev; // Airport elevation
string activeRwy; // Active runway number - For now we'll disregard multiple / alternate runway operation.
RunwayDetails rwy; // Assumed to be the active one for now.
bool rwyOccupied; // Active runway occupied flag. For now we'll disregard land-and-hold-short operations.
FGATCAlignedProjection ortho; // Orthogonal mapping of the local area with the active runway threshold at the origin
FGATCAlignedProjection ortho_temp; // Ortho for any runway (needed to get plane position in airport)
// 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 DoRwyDetails();
// Need a data structure to hold details of the various active planes
// or possibly another data structure with the positions of the inactive planes.
// Need a data structure to hold outstanding communications from aircraft.
// Linked-list of planes on approach to active rwy ordered with nearest first (timewise).
// Includes planes that have landed but not yet vacated runway.
// Somewhat analagous to the paper strips used (used to be used?) in real life.
// Doesn't include planes in circuit until they turn onto base/final?
// TODO - may need to alter this for operation to more than one active rwy.
tower_plane_rec_list_type appList;
tower_plane_rec_list_iterator appListItr;
// What should we do with planes approaching the airport to join the circuit somewhere
// but not on straight-in though? - put them in here for now.
tower_plane_rec_list_type circuitAppList;
tower_plane_rec_list_iterator circuitAppListItr;
// List of departed planes (planes doing circuits go into circuitList not depList after departure)
tower_plane_rec_list_type depList;
tower_plane_rec_list_iterator depListItr;
// List of planes in the circuit (ordered by nearest to landing first)
tower_plane_rec_list_type circuitList;
tower_plane_rec_list_iterator circuitListItr;
// List of planes waiting to depart
tower_plane_rec_list_type holdList;
tower_plane_rec_list_iterator holdListItr;
// List of planes on rwy
tower_plane_rec_list_type rwyList;
tower_plane_rec_list_iterator rwyListItr;
// List of all planes due to use a given rwy arranged in projected order of rwy use
tower_plane_rec_list_type trafficList; // TODO - needs to be expandable to more than one rwy
tower_plane_rec_list_iterator trafficListItr;
// List of planes that have vacated the runway inbound but not yet handed off to ground
tower_plane_rec_list_type vacatedList;
tower_plane_rec_list_iterator vacatedListItr;
// Returns true if successful
bool RemoveFromTrafficList(const string& id);
bool RemoveFromAppList(const string& id);
bool RemoveFromRwyList(const string& id);
// Return the ETA of plane no. list_pos (1-based) in the traffic list.
// i.e. list_pos = 1 implies next to use runway.
double GetTrafficETA(unsigned int list_pos, bool printout = false);
// Add a tower plane rec with ETA to the traffic list in the correct position ETA-wise.
// Returns true if this could cause a threshold ETA conflict with other traffic, false otherwise.
bool AddToTrafficList(TowerPlaneRec* t, bool holding = false);
bool AddToCircuitList(TowerPlaneRec* t);
// Add to vacated list only if not already present
void AddToVacatedList(TowerPlaneRec* t);
void AddToHoldingList(TowerPlaneRec* t);
// Ground can be separate or handled by tower in real life.
// In the program we will always use a separate FGGround class, but we need to know
// whether it is supposed to be separate or not to give the correct instructions.
bool separateGround; // true if ground control is separate
FGGround* ground; // The ground control associated with this airport.
bool _departureControlled; // true if we need to hand off departing traffic to departure control
//FGDeparture* _departure; // The relevant departure control (once we've actually written it!)
// for failure modeling
string trans_ident; // transmitted ident
bool tower_failed; // tower failed?
// Pointers to current users position and orientation
SGPropertyNode_ptr user_lon_node;
SGPropertyNode_ptr user_lat_node;
SGPropertyNode_ptr user_elev_node;
SGPropertyNode_ptr user_hdg_node;
// Details of the general traffic flow etc in the circuit
double crosswind_leg_pos; // Distance from threshold crosswind leg is being turned to in meters (actual operation - *not* ideal circuit)
double downwind_leg_pos; // Actual offset distance in meters from the runway that planes are flying the downwind leg
// Currently not sure whether the above should be always +ve or just take the natural orthopos sign (+ve for RH circuit, -ve for LH).
double base_leg_pos; // Actual offset distance from the threshold (-ve) that planes are turning to base leg.
double nominal_crosswind_leg_pos;
double nominal_downwind_leg_pos;
double nominal_base_leg_pos;
friend istream& operator>> ( istream&, FGTower& );
};
#endif //_FG_TOWER_HXX

View file

@ -1,100 +0,0 @@
// FGTransmission - a class to provide transmission control at larger airports.
//
// Written by Alexander Kappes, started March 2002.
// Based on ground.cxx 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "transmission.hxx"
#include <simgear/misc/sg_path.hxx>
//Constructor
FGTransmission::FGTransmission(){
}
//Destructor
FGTransmission::~FGTransmission(){
}
void FGTransmission::Init() {
}
// ============================================================================
// extract parameters from transmission
// ============================================================================
TransPar FGTransmission::Parse() {
TransPar tpar;
string tokens[20];
int msglen,toklen;
//char dum;
int i,j,k;
const char *capl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
msglen = strlen( TransText.c_str() );
int tkn = 0;
for ( i=0; i < msglen; ++i ) {
if ( TransText.c_str()[i] != ' ' ) {
if ( TransText.c_str()[i] != ',' ) tokens[tkn] += TransText.c_str()[i];
} else if ( !tokens[tkn].empty() ) {
if ( tkn <= 20 ) {
tkn += 1;
} else {
cout << "Too many tokens" << endl;
}
}
}
for ( i=0; i<20; ++i) {
if ( tokens[i] == "request" ) {
tpar.request = true;
} else if ( tokens[i] == "approach" ) {
tpar.station = "approach";
tpar.airport = tokens[i-1];
} else if ( tokens[i] == "landing" ) {
tpar.intention = "landing";
for ( j=i+1; j<=i+2; ++j ) {
if ( !tokens[j].empty() ) {
toklen = strlen( tokens[j].c_str() );
bool aid = true;
for ( k=0; k<toklen; ++k )
if ( ! strpbrk( &tokens[j].c_str()[k], capl )) {
aid = false;
break;
}
if ( aid ) tpar.intid = tokens[j];
}
}
} else if ( tokens[i] == "Player" ) {
tpar.callsign = tokens[i];
}
}
//cout << tpar.airport << endl;
//cout << tpar.request << endl;
//cout << tpar.intention << endl;
//cout << tpar.intid << endl;
return tpar;
}

View file

@ -1,169 +0,0 @@
// transmission.hxx -- Transmission class
//
// Written by Alexander Kappes, started March 2002.
// Based on nav.hxx by Curtis Olson, started April 2000.
//
// Copyright (C) 2001 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef _FG_TRANSMISSION_HXX
#define _FG_TRANSMISSION_HXX
#include <stdio.h>
#include <simgear/compiler.h>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/magvar/magvar.hxx>
#include <simgear/timing/sg_time.hxx>
#include <simgear/bucket/newbucket.hxx>
#include <Main/fg_props.hxx>
#ifdef SG_HAVE_STD_INCLUDES
# include <istream>
#include <iomanip>
#elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
# include <iostream.h>
#elif defined( __BORLANDC__ )
# include <iostream>
#else
# include <istream.h>
#include <iomanip.h>
#endif
#include "ATC.hxx"
#if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
SG_USING_STD(istream);
#endif
SG_USING_STD(string);
struct TransCode {
int c1;
int c2;
int c3;
};
// TransPar - a representation of the logic of a parsed speech transmission
struct TransPar {
string station;
string callsign;
string airport;
string intention; // landing, crossing
string intid; // (airport) ID for intention
bool request; // is the transmission a request or an answer?
int tdir; // turning direction: 1=left, 2=right
double heading;
int VDir; // vertical direction: 1=descent, 2=maintain, 3=climb
double alt;
double miles;
string runway;
double freq;
double time;
};
// FGTransmission - a class to encapsulate a speech transmission
class FGTransmission {
//int StationType; // Type of ATC station: 1 Approach
atc_type StationType;
TransCode Code; // DCL - no idea what this is.
string TransText; // The text of the spoken transmission
string MenuText; // An abbreviated version of the text for the menu entry
public:
FGTransmission(void);
~FGTransmission(void);
void Init();
inline atc_type get_station() const { return StationType; }
inline const TransCode& get_code() { return Code; }
inline const string& get_transtext() { return TransText; }
inline const string& get_menutext() { return MenuText; }
// Return the parsed logic of the transmission
TransPar Parse();
private:
friend istream& operator>> ( istream&, FGTransmission& );
};
inline istream&
operator >> ( istream& in, FGTransmission& a ) {
char ch;
int tmp;
static bool first_time = true;
static double julian_date = 0;
static const double MJD0 = 2415020.0;
if ( first_time ) {
julian_date = sgTimeCurrentMJD(0, 0) + MJD0;
first_time = false;
}
// Ugly hack alert - eventually we'll use xml format for the transmissions file
in >> tmp;
if(tmp == 1) {
a.StationType = APPROACH;
} else {
a.StationType = INVALID;
}
in >> a.Code.c1;
in >> a.Code.c2;
in >> a.Code.c3;
a.TransText = "";
in >> ch;
if ( ch != '"' ) a.TransText += ch;
while(1) {
//in >> noskipws
in.unsetf(ios::skipws);
in >> ch;
if ( ch != '"' ) a.TransText += ch;
if((ch == '"') || (ch == 0x0A)) {
break;
} // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
}
in.setf(ios::skipws);
a.MenuText = "";
in >> ch;
if ( ch != '"' ) a.MenuText += ch;
while(1) {
//in >> noskipws
in.unsetf(ios::skipws);
in >> ch;
if ( ch != '"' ) a.MenuText += ch;
if((ch == '"') || (ch == 0x0A)) {
break;
} // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
}
in.setf(ios::skipws);
//cout << "Code = " << a.Code << " Transmission text = " << a.TransText
// << " Menu text = " << a.MenuText << endl;
return in >> skipeol;
}
#endif // _FG_TRANSMISSION_HXX

View file

@ -1,263 +0,0 @@
// transmissionlist.cxx -- transmission management class
//
// Written by Alexander Kappes, started March 2002.
// Based on navlist.cxx by Curtis Olson, started April 2000.
//
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
#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
#include <simgear/debug/logstream.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include "transmissionlist.hxx"
#include <GUI/gui.h>
FGTransmissionList *current_transmissionlist;
FGTransmissionList::FGTransmissionList( void ) {
}
FGTransmissionList::~FGTransmissionList( void ) {
}
// load default.transmissions
bool FGTransmissionList::init( const SGPath& path ) {
FGTransmission a;
transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );
sg_gzifstream in( path.str() );
if ( !in.is_open() ) {
SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
exit(-1);
}
// read in each line of the file
// in >> skipeol;
// in >> skipcomment;
#ifdef __MWERKS__
char c = 0;
while ( in.get(c) && c != '\0' ) {
in.putback(c);
in >> a;
if ( a.get_type() != '[' ) {
transmissionlist_code[a.get_station()].push_back(a);
}
in >> skipcomment;
}
#else
double min = 100000;
double max = 0;
while ( ! in.eof() ) {
in >> a;
transmissionlist_station[a.get_station()].push_back(a);
in >> skipcomment;
if ( a.get_station() < min ) {
min = a.get_station();
}
if ( a.get_station() > max ) {
max = a.get_station();
}
/*
cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
<< a.get_code().c3 << " " << a.get_transtext()
<< " " << a.get_menutext() << endl;
*/
}
#endif
// init ATC menu
fgSetBool("/sim/atc/menu",false);
return true;
}
// query the database for the specified station type;
// for station see FlightGear/ATC/default.transmissions
bool FGTransmissionList::query_station( const atc_type &station, FGTransmission *t,
int max_trans, int &num_trans )
{
transmission_list_type tmissions = transmissionlist_station[station];
transmission_list_iterator current = tmissions.begin();
transmission_list_iterator last = tmissions.end();
for ( ; current != last ; ++current ) {
if (num_trans < max_trans) {
t[num_trans] = *current;
num_trans += 1;
}
else {
cout << "Transmissionlist error: Too many transmissions" << endl;
}
}
if ( num_trans != 0 ) return true;
else {
cout << "No transmission with station " << station << "found." << endl;
string empty;
return false;
}
}
string FGTransmissionList::gen_text(const atc_type &station, const TransCode code,
const TransPar &tpars, const bool ttext )
{
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;
// if (current_transmissionlist->query_station( station, &t ) ) {
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], message.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 );
memmove(&tag[0], pos, 3);
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() );
else if ( strcmp ( tag, "@AP" ) == 0 )
strcat( &dum[0], tpars.airport.c_str() );
else if ( strcmp ( tag, "@CS" ) == 0 )
strcat( &dum[0], tpars.callsign.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 );
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], tpars.runway.c_str() );
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;
}
}
return mes[0] ? mes : "No transmission found";
}

View file

@ -1,82 +0,0 @@
// transmissionlist.hxx -- transmission management class
//
// Written by Alexander Kappes, started March 2002.
// Based on navlist.hxx by Curtis Olson, started April 2000.
//
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#ifndef _FG_TRANSMISSIONLIST_HXX
#define _FG_TRANSMISSIONLIST_HXX
#include <simgear/compiler.h>
#include <simgear/misc/sg_path.hxx>
#include <map>
#include <vector>
#include "ATC.hxx"
#include "transmission.hxx"
SG_USING_STD(map);
SG_USING_STD(vector);
class FGTransmissionList {
// convenience types
typedef vector < FGTransmission > transmission_list_type;
typedef transmission_list_type::iterator transmission_list_iterator;
typedef transmission_list_type::const_iterator transmission_list_const_iterator;
// Map of transmission lists by station type
// typedef map < int, transmission_list_type, less<int> > transmission_map_type;
typedef map < atc_type, transmission_list_type > transmission_map_type;
typedef transmission_map_type::iterator transmission_map_iterator;
typedef transmission_map_type::const_iterator transmission_map_const_iterator;
transmission_map_type transmissionlist_station;
public:
FGTransmissionList();
~FGTransmissionList();
// load the transmission data and build the map
bool init( const SGPath& path );
// query the database for the specified code,
bool query_station( const atc_type &station, FGTransmission *a, int max_trans, int &num_trans );
// generate the transmission text given the code of the message
// and the parameters
// Set ttext = true to generate the spoken transmission text,
// or false to generate the abridged menu entry text.
string gen_text(const atc_type &station, const TransCode code,
const TransPar &tpars, const bool ttext);
};
void mkATCMenuInit (void);
void mkATCMenu (void);
extern FGTransmissionList *current_transmissionlist;
#endif // _FG_TRANSMISSIONLIST_HXX

View file

@ -33,7 +33,7 @@
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/math/polar3d.hxx>
#include SG_GLU_H
#include <ATC/ATCutils.hxx>
#include <ATCDCL/ATCutils.hxx>
// int x, int y, int width, int height, float scale_data, bool working)

View file

@ -35,7 +35,7 @@
#include <Environment/environment_mgr.hxx>
#include <Main/viewer.hxx>
#include <Main/viewmgr.hxx>
#include <ATC/ATCutils.hxx>
#include <ATCDCL/ATCutils.hxx>
#include "HUD.hxx"

View file

@ -43,7 +43,7 @@
#include "kln89_symbols.hxx"
#include <iostream>
#include <ATC/ATCProjection.hxx>
#include <ATCDCL/ATCProjection.hxx>
#include <Main/fg_props.hxx>
#include <simgear/math/point3d.hxx>

View file

@ -26,7 +26,7 @@
#endif
#include "kln89_page_apt.hxx"
#include <ATC/commlist.hxx>
#include <ATCDCL/commlist.hxx>
#include <Main/globals.hxx>
// This function is copied from Airports/runways.cxx

View file

@ -72,8 +72,8 @@
#include <Airports/runways.hxx>
#include <Airports/simple.hxx>
#include <AIModel/AIManager.hxx>
#include <ATC/ATCmgr.hxx>
#include <ATC/AIMgr.hxx>
#include <ATCDCL/ATCmgr.hxx>
#include <ATCDCL/AIMgr.hxx>
#include <Autopilot/route_mgr.hxx>
#include <Autopilot/xmlauto.hxx>
#include <Cockpit/cockpit.hxx>

View file

@ -34,8 +34,8 @@
#include <Aircraft/controls.hxx>
#include <Airports/runways.hxx>
#include <ATC/AIMgr.hxx>
#include <ATC/ATCmgr.hxx>
#include <ATCDCL/AIMgr.hxx>
#include <ATCDCL/ATCmgr.hxx>
#include <Autopilot/route_mgr.hxx>
#include <Cockpit/panel.hxx>
#include <GUI/new_gui.hxx>

View file

@ -68,8 +68,8 @@
#include <Sound/beacon.hxx>
#include <Sound/morse.hxx>
#include <FDM/flight.hxx>
#include <ATC/ATCmgr.hxx>
#include <ATC/AIMgr.hxx>
#include <ATCDCL/ATCmgr.hxx>
#include <ATCDCL/AIMgr.hxx>
#include <Time/tmp.hxx>
#include <Time/fg_timer.hxx>
#include <Environment/environment_mgr.hxx>

View file

@ -2,7 +2,7 @@ SUBDIRS = \
Include \
Aircraft \
Airports \
ATC \
ATCDCL \
Autopilot \
Cockpit \
Environment \