1 // trafficrecord.cxx - Implementation of AIModels ATC code.
3 // Written by Durk Talsma, started September 2006.
5 // Copyright (C) 2006 Durk Talsma.
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "trafficcontrol.hxx"
28 #include <AIModel/AIFlightPlan.hxx>
31 /***************************************************************************
33 **************************************************************************/
34 FGTrafficRecord::FGTrafficRecord() :
46 void FGTrafficRecord::setPositionAndIntentions(int pos, FGAIFlightPlan *route)
50 if (intentions.size()) {
51 intVecIterator i = intentions.begin();
53 SG_LOG(SG_GENERAL, SG_ALERT, "Error in FGTrafficRecord::setPositionAndIntentions");
54 //cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
55 for (intVecIterator i = intentions.begin(); i != intentions.end() ; i++) {
56 //cerr << (*i) << " ";
63 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
64 int size = route->getNrOfWayPoints();
65 //cerr << "Setting pos" << pos << " ";
66 //cerr << "setting intentions ";
67 for (int i = 0; i < size; i++) {
68 int val = route->getRouteIndex(i);
70 if ((val) && (val != pos))
72 intentions.push_back(val);
77 //while (route->next(&legNr, &routeNr)) {
78 //intentions.push_back(routeNr);
80 //route->rewind(currentPos);
85 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord &other)
88 //cerr << "Start check 1" << endl;
89 if (currentPos == other.currentPos)
91 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
94 // else if (other.intentions.size())
96 // cerr << "Start check 2" << endl;
97 // intVecIterator i = other.intentions.begin();
98 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
100 // if (i != other.intentions.end()) {
101 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
104 else if (intentions.size()) {
105 //cerr << "Start check 3" << endl;
106 intVecIterator i = intentions.begin();
107 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
108 while (i != intentions.end()) {
109 if ((*i) == other.currentPos) {
114 if (i != intentions.end()) {
115 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
119 //cerr << "Done !!" << endl;
123 void FGTrafficRecord::setPositionAndHeading(double lat, double lon, double hdg,
124 double spd, double alt)
133 int FGTrafficRecord::crosses(FGGroundNetwork *net, FGTrafficRecord &other)
135 if (checkPositionAndIntentions(other) || (other.checkPositionAndIntentions(*this)))
138 int currentTargetNode = 0, otherTargetNode = 0;
140 currentTargetNode = net->findSegment(currentPos )->getEnd()->getIndex(); // OKAY,...
141 if (other.currentPos > 0)
142 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
143 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
144 return currentTargetNode;
145 if (intentions.size())
147 for (i = intentions.begin(); i != intentions.end(); i++)
150 if ((currentTargetNode == net->findSegment(*i)->getEnd()->getIndex()))
152 //cerr << "Current crosses at " << currentTargetNode <<endl;
153 return currentTargetNode;
158 if (other.intentions.size())
160 for (i = other.intentions.begin(); i != other.intentions.end(); i++)
163 if (otherTargetNode == net->findSegment(*i)->getEnd()->getIndex())
165 //cerr << "Other crosses at " << currentTargetNode <<endl;
166 return otherTargetNode;
171 if (intentions.size() && other.intentions.size())
173 for (i = intentions.begin(); i != intentions.end(); i++)
175 for (j = other.intentions.begin(); j != other.intentions.end(); j++)
177 //cerr << "finding segment " << *i << " and " << *j << endl;
178 if (((*i) > 0) && ((*j) > 0)) {
179 currentTargetNode = net->findSegment(*i)->getEnd()->getIndex();
180 otherTargetNode = net->findSegment(*j)->getEnd()->getIndex();
181 if (currentTargetNode == otherTargetNode)
183 //cerr << "Routes will cross at " << currentTargetNode << endl;
184 return currentTargetNode;
193 bool FGTrafficRecord::onRoute(FGGroundNetwork *net, FGTrafficRecord &other)
195 int node = -1, othernode = -1;
197 node = net->findSegment(currentPos)->getEnd()->getIndex();
198 if (other.currentPos > 0)
199 othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
200 if ((node == othernode) && (node != -1))
202 if (other.intentions.size())
204 for (intVecIterator i = other.intentions.begin(); i != other.intentions.end(); i++)
208 othernode = net->findSegment(*i)->getEnd()->getIndex();
209 if ((node == othernode) && (node > -1))
214 //if (other.currentPos > 0)
215 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
216 //if (intentions.size())
218 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
222 // node = net->findSegment(*i)->getEnd()->getIndex();
223 // if ((node == othernode) && (node > -1))
232 bool FGTrafficRecord::isOpposing (FGGroundNetwork *net, FGTrafficRecord &other, int node)
234 // Check if current segment is the reverse segment for the other aircraft
236 //cerr << "Current segment " << currentPos << endl;
237 if ((currentPos > 0) && (other.currentPos > 0))
239 opp = net->findSegment(currentPos)->opposite();
241 if (opp->getIndex() == other.currentPos)
245 for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
247 if (opp = net->findSegment(other.currentPos)->opposite())
250 if (opp->getIndex() == net->findSegment(*i)->getIndex())
252 if (net->findSegment(*i)->getStart()->getIndex() == node) {
254 //cerr << "Found the node " << node << endl;
260 if (other.intentions.size())
262 for (intVecIterator j = other.intentions.begin(); j != other.intentions.end(); j++)
264 // cerr << "Current segment 1 " << (*i) << endl;
266 if (opp = net->findSegment(*i)->opposite())
268 if (opp->getIndex() ==
269 net->findSegment(*j)->getIndex())
271 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
272 // << " and " << net->findSegment(*j)->getIndex()
273 // << " are opposites " << endl;
274 if (net->findSegment(*i)->getStart()->getIndex() == node) {
276 //cerr << "Found the node " << node << endl;
290 void FGTrafficRecord::setSpeedAdjustment(double spd)
292 instruction.setChangeSpeed(true);
293 instruction.setSpeed(spd);
296 void FGTrafficRecord::setHeadingAdjustment(double heading)
298 instruction.setChangeHeading(true);
299 instruction.setHeading(heading);
304 /***************************************************************************
307 **************************************************************************/
308 FGATCInstruction::FGATCInstruction()
311 holdPosition = false;
313 changeHeading = false;
314 changeAltitude = false;
315 resolveCircularWait = false;
322 bool FGATCInstruction::hasInstruction()
324 return (holdPattern || holdPosition || changeSpeed || changeHeading || changeAltitude || resolveCircularWait);
329 /***************************************************************************
330 * class FGTowerController
332 **************************************************************************/
333 FGTowerController::FGTowerController() :
339 void FGTowerController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
340 double lat, double lon, double heading,
341 double speed, double alt, double radius, int leg,
344 TrafficVectorIterator i = activeTraffic.begin();
345 // Search whether the current id alread has an entry
346 // This might be faster using a map instead of a vector, but let's start by taking a safe route
347 if (activeTraffic.size()) {
348 //while ((i->getId() != id) && i != activeTraffic.end()) {
349 while (i != activeTraffic.end()) {
350 if (i->getId() == id) {
357 // Add a new TrafficRecord if no one exsists for this aircraft.
358 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
362 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
363 rec.setRunway(intendedRoute->getRunway());
365 rec.setCallSign(callsign);
366 activeTraffic.push_back(rec);
368 i->setPositionAndHeading(lat, lon, heading, speed, alt);
372 void FGTowerController::update(int id, double lat, double lon, double heading, double speed, double alt,
375 TrafficVectorIterator i = activeTraffic.begin();
376 // Search search if the current id has an entry
377 // This might be faster using a map instead of a vector, but let's start by taking a safe route
378 TrafficVectorIterator current, closest;
379 if (activeTraffic.size()) {
380 //while ((i->getId() != id) && i != activeTraffic.end()) {
381 while (i != activeTraffic.end()) {
382 if (i->getId() == id) {
389 // // update position of the current aircraft
390 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
391 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
393 i->setPositionAndHeading(lat, lon, heading, speed, alt);
398 // // see if we already have a clearance record for the currently active runway
399 ActiveRunwayVecIterator rwy = activeRunways.begin();
400 // again, a map might be more efficient here
401 if (activeRunways.size()) {
402 //while ((rwy->getRunwayName() != current->getRunway()) && (rwy != activeRunways.end())) {
403 while (rwy != activeRunways.end()) {
404 if (rwy->getRunwayName() == current->getRunway()) {
410 if (rwy == activeRunways.end()) {
411 ActiveRunway aRwy(current->getRunway(), id);
412 activeRunways.push_back(aRwy); // Since there are no clearance records for this runway yet
413 current->setHoldPosition(false); // Clear the current aircraft to continue
416 // Okay, we have a clearance record for this runway, so check
417 // whether the clearence ID matches that of the current aircraft
418 if (id == rwy->getCleared()) {
419 current->setHoldPosition(false);
421 current->setHoldPosition(true);
427 void FGTowerController::signOff(int id)
429 TrafficVectorIterator i = activeTraffic.begin();
430 // Search search if the current id alread has an entry
431 // This might be faster using a map instead of a vector, but let's start by taking a safe route
432 if (activeTraffic.size()) {
433 //while ((i->getId() != id) && i != activeTraffic.end()) {
434 while (i != activeTraffic.end()) {
435 if (i->getId() == id) {
441 // If this aircraft has left the runway, we can clear the departure record for this runway
442 ActiveRunwayVecIterator rwy = activeRunways.begin();
443 if (activeRunways.size()) {
444 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
445 while (rwy != activeRunways.end()) {
446 if (rwy->getRunwayName() == i->getRunway()) {
451 if (rwy != activeRunways.end()) {
452 rwy = activeRunways.erase(rwy);
454 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff");
457 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
458 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
460 i = activeTraffic.erase(i);
465 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
466 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
467 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
468 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
469 // Note that this function is probably obsolete
470 bool FGTowerController::hasInstruction(int id)
472 TrafficVectorIterator i = activeTraffic.begin();
473 // Search search if the current id has an entry
474 // This might be faster using a map instead of a vector, but let's start by taking a safe route
475 if (activeTraffic.size())
477 //while ((i->getId() != id) && i != activeTraffic.end()) {
478 while (i != activeTraffic.end()) {
479 if (i->getId() == id) {
485 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
486 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
488 return i->hasInstruction();
494 FGATCInstruction FGTowerController::getInstruction(int id)
496 TrafficVectorIterator i = activeTraffic.begin();
497 // Search search if the current id has an entry
498 // This might be faster using a map instead of a vector, but let's start by taking a safe route
499 if (activeTraffic.size()) {
500 //while ((i->getId() != id) && i != activeTraffic.end()) {
501 while (i != activeTraffic.end()) {
502 if (i->getId() == id) {
508 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
509 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
511 return i->getInstruction();
513 return FGATCInstruction();