2002-04-03 23:54:44 +00:00
|
|
|
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2002-12-19 14:03:36 +00:00
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2002-12-04 19:36:39 +00:00
|
|
|
#include <simgear/math/sg_random.h>
|
2002-12-19 14:03:36 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/misc/sgstream.hxx>
|
2002-12-04 19:36:39 +00:00
|
|
|
#include <simgear/constants.h>
|
2002-12-19 14:03:36 +00:00
|
|
|
#include <Main/globals.hxx>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include STL_FSTREAM
|
2002-12-04 19:36:39 +00:00
|
|
|
|
2002-04-03 23:54:44 +00:00
|
|
|
#include "ground.hxx"
|
2003-02-24 19:03:15 +00:00
|
|
|
#include "ATCutils.hxx"
|
2002-04-03 23:54:44 +00:00
|
|
|
|
2002-12-20 09:48:09 +00:00
|
|
|
SG_USING_STD(ifstream);
|
2003-01-03 16:30:44 +00:00
|
|
|
SG_USING_STD(cout);
|
2002-12-20 09:48:09 +00:00
|
|
|
|
2003-02-24 19:03:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-12-04 19:36:39 +00:00
|
|
|
FGGround::FGGround() {
|
|
|
|
display = false;
|
2002-12-19 14:03:36 +00:00
|
|
|
networkLoadOK = false;
|
2002-12-04 19:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FGGround::~FGGround() {
|
|
|
|
}
|
|
|
|
|
2002-12-19 14:03:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2002-12-04 19:36:39 +00:00
|
|
|
|
|
|
|
|
2002-12-19 14:03:36 +00:00
|
|
|
// 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.
|
2003-02-24 19:03:15 +00:00
|
|
|
// NOTE - currently it is assumed that all nodes are loaded before any arcs.
|
|
|
|
// It won't work ATM if this doesn't hold true.
|
2002-12-19 14:03:36 +00:00
|
|
|
bool FGGround::LoadNetwork() {
|
2002-12-04 19:36:39 +00:00
|
|
|
node* np;
|
|
|
|
arc* ap;
|
|
|
|
Gate* gp;
|
|
|
|
|
2003-02-24 19:03:15 +00:00
|
|
|
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.
|
|
|
|
|
2002-12-19 14:03:36 +00:00
|
|
|
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_GENERAL, 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_GENERAL, 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 '['
|
2003-01-03 16:30:44 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "Done reading " << path.c_str() << endl);
|
2002-12-19 14:03:36 +00:00
|
|
|
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 {
|
2003-03-05 21:31:54 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "**** ERROR ***** Unknown node type in taxi network...\n");
|
2002-12-19 14:03:36 +00:00
|
|
|
delete np;
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
fin >> buf; // rwy exit information - gets parsed later - FRAGILE - will break if buf is reused.
|
|
|
|
// Now the name
|
2003-03-05 21:31:54 +00:00
|
|
|
fin >> ch; // strip the leading " off
|
2002-12-19 14:03:36 +00:00
|
|
|
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 "
|
2003-03-05 21:31:54 +00:00
|
|
|
np->name += ch;
|
2002-12-19 14:03:36 +00:00
|
|
|
}
|
|
|
|
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
|
2003-03-05 21:31:54 +00:00
|
|
|
//cout << "strlen(buf) = " << strlen(buf) << endl;
|
2002-12-19 14:03:36 +00:00
|
|
|
if(strlen(buf) > 2) {
|
2003-03-05 21:31:54 +00:00
|
|
|
//cout << "Calling ParseRwyExits for " << buf << endl;
|
2002-12-19 14:03:36 +00:00
|
|
|
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 {
|
2003-03-05 21:31:54 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "**** ERROR ***** Unknown arc type in taxi network...\n");
|
2002-12-19 14:03:36 +00:00
|
|
|
delete ap;
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
// directed?
|
|
|
|
fin >> buf;
|
|
|
|
if(!strcmp(buf, "Y")) {
|
|
|
|
ap->directed = true;
|
|
|
|
} else if(!strcmp(buf, "N")) {
|
|
|
|
ap->directed = false;
|
|
|
|
} else {
|
2003-03-05 21:31:54 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "**** ERROR ***** Unknown arc directed value in taxi network - should be Y/N !!!\n");
|
2002-12-19 14:03:36 +00:00
|
|
|
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);
|
2003-02-24 19:03:15 +00:00
|
|
|
ap->distance = (int)dclGetHorizontalSeparation(network[ap->n1]->pos, network[ap->n2]->pos);
|
2003-02-25 09:32:49 +00:00
|
|
|
//cout << "Distance = " << ap->distance << '\n';
|
2002-12-19 14:03:36 +00:00
|
|
|
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);
|
2003-02-24 19:03:15 +00:00
|
|
|
gp->id = gateCount; // Warning - this will likely change in the future.
|
|
|
|
gp->used = false;
|
2002-12-19 14:03:36 +00:00
|
|
|
network.push_back(gp);
|
2003-02-24 19:03:15 +00:00
|
|
|
gates[gateCount] = gp;
|
|
|
|
gateCount++;
|
2002-12-19 14:03:36 +00:00
|
|
|
} else {
|
|
|
|
// Something has gone seriously pear-shaped
|
2003-03-05 21:31:54 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "********* ERROR - unknown ground network element type... aborting read of " << path.c_str() << '\n');
|
2002-12-19 14:03:36 +00:00
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fin >> skipeol;
|
|
|
|
}
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGGround::Init() {
|
|
|
|
display = false;
|
|
|
|
|
2002-12-04 19:36:39 +00:00
|
|
|
// For now we'll hardwire the threshold end
|
|
|
|
Point3D P010(-118.037483, 34.081358, 296 * SG_FEET_TO_METER);
|
|
|
|
double hdg = 25.32;
|
|
|
|
ortho.Init(P010, hdg);
|
2002-12-19 14:03:36 +00:00
|
|
|
|
|
|
|
networkLoadOK = LoadNetwork();
|
2002-04-03 23:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGGround::Update() {
|
2002-12-04 19:36:39 +00:00
|
|
|
// Each time step, what do we need to do?
|
|
|
|
// We need to go through the list of outstanding requests and acknowedgements
|
|
|
|
// and process at least one of them.
|
|
|
|
// We need to go through the list of planes under our control and check if
|
|
|
|
// any need to be addressed.
|
|
|
|
// We need to check for planes not under our control coming within our
|
|
|
|
// control area and address if necessary.
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
|
2003-02-24 19:03:15 +00:00
|
|
|
// 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.
|
2002-12-04 19:36:39 +00:00
|
|
|
int FGGround::GetRandomGateID() {
|
2003-02-24 19:03:15 +00:00
|
|
|
// Check that this airport actually has some gates!!
|
|
|
|
if(!gates.size()) {
|
|
|
|
return(-2);
|
|
|
|
}
|
2002-12-04 19:36:39 +00:00
|
|
|
|
|
|
|
gate_vec_type gateVec;
|
|
|
|
int num = 0;
|
|
|
|
int thenum;
|
|
|
|
int ID;
|
|
|
|
|
|
|
|
gatesItr = gates.begin();
|
|
|
|
while(gatesItr != gates.end()) {
|
2003-02-24 19:03:15 +00:00
|
|
|
if((gatesItr->second)->used == false) {
|
2002-12-04 19:36:39 +00:00
|
|
|
gateVec.push_back(gatesItr->second);
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
++gatesItr;
|
|
|
|
}
|
2003-02-24 19:03:15 +00:00
|
|
|
|
|
|
|
// Check that there are some unused gates!
|
|
|
|
if(!gateVec.size()) {
|
|
|
|
return(-1);
|
|
|
|
}
|
2002-12-04 19:36:39 +00:00
|
|
|
|
|
|
|
// Randomly select one from the list
|
2003-02-24 19:03:15 +00:00
|
|
|
sg_srandom_time();
|
2002-12-04 19:36:39 +00:00
|
|
|
thenum = (int)(sg_random() * gateVec.size());
|
2003-02-24 19:03:15 +00:00
|
|
|
ID = gateVec[thenum]->id;
|
|
|
|
|
2002-12-04 19:36:39 +00:00
|
|
|
return(ID);
|
|
|
|
}
|
|
|
|
|
2003-02-24 19:03:15 +00:00
|
|
|
// Return a pointer to an unused gate node
|
|
|
|
Gate* FGGround::GetGateNode() {
|
|
|
|
int id = GetRandomGateID();
|
|
|
|
if(id < 0) {
|
|
|
|
return(NULL);
|
|
|
|
} else {
|
|
|
|
return(gates[id]);
|
|
|
|
}
|
2002-04-03 23:54:44 +00:00
|
|
|
}
|
|
|
|
|
2003-03-05 21:31:54 +00:00
|
|
|
|
|
|
|
// WARNING - This is hardwired to my prototype logical network format
|
|
|
|
// and will almost certainly change when Bernie's stuff comes on-line.
|
|
|
|
node* FGGround::GetThresholdNode(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;
|
|
|
|
}
|
|
|
|
|
2002-12-04 19:36:39 +00:00
|
|
|
// Get a path from a point on a runway to a gate
|
2003-02-24 19:03:15 +00:00
|
|
|
// TODO !!
|
2002-12-04 19:36:39 +00:00
|
|
|
|
|
|
|
// Get a path from a node to another node
|
|
|
|
// Eventually we will need complex algorithms for this taking other traffic,
|
2003-02-24 19:03:15 +00:00
|
|
|
// 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));
|
|
|
|
};
|
2002-12-04 19:36:39 +00:00
|
|
|
|
2003-03-05 21:31:54 +00:00
|
|
|
// Get a path from a node to a runway threshold
|
|
|
|
ground_network_path_type FGGround::GetPath(node* A, string rwyID) {
|
|
|
|
node* b = GetThresholdNode(rwyID);
|
|
|
|
if(b == NULL) {
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - unable to find path to runway theshold in ground.cxx\n");
|
|
|
|
ground_network_path_type emptyPath;
|
|
|
|
emptyPath.erase(emptyPath.begin(), emptyPath.end());
|
|
|
|
return(emptyPath);
|
|
|
|
}
|
|
|
|
return GetShortestPath(A, b);
|
|
|
|
}
|
2002-12-04 19:36:39 +00:00
|
|
|
|
2003-02-24 19:03:15 +00:00
|
|
|
// 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';
|
2002-12-04 19:36:39 +00:00
|
|
|
}
|
2003-02-24 19:03:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-12-04 19:36:39 +00:00
|
|
|
}
|
2003-02-24 19:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2002-12-04 19:36:39 +00:00
|
|
|
}
|
2003-02-24 19:03:15 +00:00
|
|
|
++spItr;
|
|
|
|
}
|
2002-12-04 19:36:39 +00:00
|
|
|
|
2003-02-24 19:03:15 +00:00
|
|
|
//cout << "pathsCreated = " << pathsCreated << '\n';
|
|
|
|
if(pathsCreated > 0) {
|
|
|
|
SG_LOG(SG_GENERAL, 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.
|
|
|
|
}
|
|
|
|
|
2002-12-04 19:36:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2002-12-19 14:03:36 +00:00
|
|
|
// It is up to the calling function to check for non-zero size of returned array before use
|
2002-12-04 19:36:39 +00:00
|
|
|
node_array_type FGGround::GetExits(int rwyID) {
|
|
|
|
return(runways[rwyID].exits);
|
|
|
|
}
|
2002-12-19 14:03:36 +00:00
|
|
|
|
2002-12-04 19:36:39 +00:00
|
|
|
#if 0
|
2002-04-03 23:54:44 +00:00
|
|
|
void FGGround::NewArrival(plane_rec plane) {
|
2002-12-04 19:36:39 +00:00
|
|
|
// 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);
|
2002-04-03 23:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGGround::NewContact(plane_rec plane) {
|
2002-12-04 19:36:39 +00:00
|
|
|
// 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);
|
2002-04-03 23:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGGround::NextClearance(ground_rec &g) {
|
2002-12-04 19:36:39 +00:00
|
|
|
// 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.
|
2002-04-03 23:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGGround::AssignGate(ground_rec &g) {
|
2002-12-04 19:36:39 +00:00
|
|
|
// 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.
|
|
|
|
}
|
2002-12-20 09:48:09 +00:00
|
|
|
#endif //0
|
2003-02-24 19:03:15 +00:00
|
|
|
|