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 void FGTrafficRecord::setPositionAndIntentions(int pos, FGAIFlightPlan *route)
38 if (intentions.size()) {
39 intVecIterator i = intentions.begin();
41 SG_LOG(SG_GENERAL, SG_ALERT, "Error in FGTrafficRecord::setPositionAndIntentions");
42 //cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
43 for (intVecIterator i = intentions.begin(); i != intentions.end() ; i++) {
44 //cerr << (*i) << " ";
51 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
52 int size = route->getNrOfWayPoints();
53 //cerr << "Setting pos" << pos << " ";
54 //cerr << "setting intentions ";
55 for (int i = 0; i < size; i++) {
56 int val = route->getRouteIndex(i);
58 if ((val) && (val != pos))
60 intentions.push_back(val);
65 //while (route->next(&legNr, &routeNr)) {
66 //intentions.push_back(routeNr);
68 //route->rewind(currentPos);
73 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord &other)
76 //cerr << "Start check 1" << endl;
77 if (currentPos == other.currentPos)
79 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
82 // else if (other.intentions.size())
84 // cerr << "Start check 2" << endl;
85 // intVecIterator i = other.intentions.begin();
86 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
88 // if (i != other.intentions.end()) {
89 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
92 else if (intentions.size()) {
93 //cerr << "Start check 3" << endl;
94 intVecIterator i = intentions.begin();
95 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
96 while (i != intentions.end()) {
97 if ((*i) == other.currentPos) {
102 if (i != intentions.end()) {
103 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
107 //cerr << "Done !!" << endl;
111 void FGTrafficRecord::setPositionAndHeading(double lat, double lon, double hdg,
112 double spd, double alt)
121 int FGTrafficRecord::crosses(FGGroundNetwork *net, FGTrafficRecord &other)
123 if (checkPositionAndIntentions(other) || (other.checkPositionAndIntentions(*this)))
126 int currentTargetNode = 0, otherTargetNode = 0;
128 currentTargetNode = net->findSegment(currentPos )->getEnd()->getIndex(); // OKAY,...
129 if (other.currentPos > 0)
130 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
131 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
132 return currentTargetNode;
133 if (intentions.size())
135 for (i = intentions.begin(); i != intentions.end(); i++)
138 if ((currentTargetNode == net->findSegment(*i)->getEnd()->getIndex()))
140 //cerr << "Current crosses at " << currentTargetNode <<endl;
141 return currentTargetNode;
146 if (other.intentions.size())
148 for (i = other.intentions.begin(); i != other.intentions.end(); i++)
151 if (otherTargetNode == net->findSegment(*i)->getEnd()->getIndex())
153 //cerr << "Other crosses at " << currentTargetNode <<endl;
154 return otherTargetNode;
159 if (intentions.size() && other.intentions.size())
161 for (i = intentions.begin(); i != intentions.end(); i++)
163 for (j = other.intentions.begin(); j != other.intentions.end(); j++)
165 //cerr << "finding segment " << *i << " and " << *j << endl;
166 if (((*i) > 0) && ((*j) > 0)) {
167 currentTargetNode = net->findSegment(*i)->getEnd()->getIndex();
168 otherTargetNode = net->findSegment(*j)->getEnd()->getIndex();
169 if (currentTargetNode == otherTargetNode)
171 //cerr << "Routes will cross at " << currentTargetNode << endl;
172 return currentTargetNode;
181 bool FGTrafficRecord::onRoute(FGGroundNetwork *net, FGTrafficRecord &other)
183 int node = -1, othernode = -1;
185 node = net->findSegment(currentPos)->getEnd()->getIndex();
186 if (other.currentPos > 0)
187 othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
188 if ((node == othernode) && (node != -1))
190 if (other.intentions.size())
192 for (intVecIterator i = other.intentions.begin(); i != other.intentions.end(); i++)
196 othernode = net->findSegment(*i)->getEnd()->getIndex();
197 if ((node == othernode) && (node > -1))
202 //if (other.currentPos > 0)
203 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
204 //if (intentions.size())
206 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
210 // node = net->findSegment(*i)->getEnd()->getIndex();
211 // if ((node == othernode) && (node > -1))
220 bool FGTrafficRecord::isOpposing (FGGroundNetwork *net, FGTrafficRecord &other, int node)
222 // Check if current segment is the reverse segment for the other aircraft
224 //cerr << "Current segment " << currentPos << endl;
225 if ((currentPos > 0) && (other.currentPos > 0))
227 opp = net->findSegment(currentPos)->opposite();
229 if (opp->getIndex() == other.currentPos)
233 for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
235 if (opp = net->findSegment(other.currentPos)->opposite())
238 if (opp->getIndex() == net->findSegment(*i)->getIndex())
240 if (net->findSegment(*i)->getStart()->getIndex() == node) {
242 //cerr << "Found the node " << node << endl;
248 if (other.intentions.size())
250 for (intVecIterator j = other.intentions.begin(); j != other.intentions.end(); j++)
252 // cerr << "Current segment 1 " << (*i) << endl;
254 if (opp = net->findSegment(*i)->opposite())
256 if (opp->getIndex() ==
257 net->findSegment(*j)->getIndex())
259 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
260 // << " and " << net->findSegment(*j)->getIndex()
261 // << " are opposites " << endl;
262 if (net->findSegment(*i)->getStart()->getIndex() == node) {
264 //cerr << "Found the node " << node << endl;
278 void FGTrafficRecord::setSpeedAdjustment(double spd)
280 instruction.setChangeSpeed(true);
281 instruction.setSpeed(spd);
284 void FGTrafficRecord::setHeadingAdjustment(double heading)
286 instruction.setChangeHeading(true);
287 instruction.setHeading(heading);
292 /***************************************************************************
295 **************************************************************************/
296 FGATCInstruction::FGATCInstruction()
299 holdPosition = false;
301 changeHeading = false;
302 changeAltitude = false;
309 bool FGATCInstruction::hasInstruction()
311 return (holdPattern || holdPosition || changeSpeed || changeHeading || changeAltitude);
316 /***************************************************************************
317 * class FGTowerController
319 **************************************************************************/
320 FGTowerController::FGTowerController() :
326 void FGTowerController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
327 double lat, double lon, double heading,
328 double speed, double alt, double radius, int leg,
331 TrafficVectorIterator i = activeTraffic.begin();
332 // Search whether the current id alread has an entry
333 // This might be faster using a map instead of a vector, but let's start by taking a safe route
334 if (activeTraffic.size()) {
335 //while ((i->getId() != id) && i != activeTraffic.end()) {
336 while (i != activeTraffic.end()) {
337 if (i->getId() == id) {
344 // Add a new TrafficRecord if no one exsists for this aircraft.
345 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
348 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
349 rec.setRunway(intendedRoute->getRunway());
351 rec.setCallSign(callsign);
352 activeTraffic.push_back(rec);
354 i->setPositionAndHeading(lat, lon, heading, speed, alt);
358 void FGTowerController::update(int id, double lat, double lon, double heading, double speed, double alt,
361 TrafficVectorIterator i = activeTraffic.begin();
362 // Search search if the current id has an entry
363 // This might be faster using a map instead of a vector, but let's start by taking a safe route
364 TrafficVectorIterator current, closest;
365 if (activeTraffic.size()) {
366 //while ((i->getId() != id) && i != activeTraffic.end()) {
367 while (i != activeTraffic.end()) {
368 if (i->getId() == id) {
375 // // update position of the current aircraft
376 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
377 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
379 i->setPositionAndHeading(lat, lon, heading, speed, alt);
384 // // see if we already have a clearance record for the currently active runway
385 ActiveRunwayVecIterator rwy = activeRunways.begin();
386 // again, a map might be more efficient here
387 if (activeRunways.size()) {
388 //while ((rwy->getRunwayName() != current->getRunway()) && (rwy != activeRunways.end())) {
389 while (rwy != activeRunways.end()) {
390 if (rwy->getRunwayName() == current->getRunway()) {
396 if (rwy == activeRunways.end()) {
397 ActiveRunway aRwy(current->getRunway(), id);
398 activeRunways.push_back(aRwy); // Since there are no clearance records for this runway yet
399 current->setHoldPosition(false); // Clear the current aircraft to continue
402 // Okay, we have a clearance record for this runway, so check
403 // whether the clearence ID matches that of the current aircraft
404 if (id == rwy->getCleared()) {
405 current->setHoldPosition(false);
407 current->setHoldPosition(true);
413 void FGTowerController::signOff(int id)
415 TrafficVectorIterator i = activeTraffic.begin();
416 // Search search if the current id alread has an entry
417 // This might be faster using a map instead of a vector, but let's start by taking a safe route
418 if (activeTraffic.size()) {
419 //while ((i->getId() != id) && i != activeTraffic.end()) {
420 while (i != activeTraffic.end()) {
421 if (i->getId() == id) {
427 // If this aircraft has left the runway, we can clear the departure record for this runway
428 ActiveRunwayVecIterator rwy = activeRunways.begin();
429 if (activeRunways.size()) {
430 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
431 while (rwy != activeRunways.end()) {
432 if (rwy->getRunwayName() == i->getRunway()) {
437 if (rwy != activeRunways.end()) {
438 rwy = activeRunways.erase(rwy);
440 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff");
443 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
444 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
446 i = activeTraffic.erase(i);
451 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
452 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
453 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
454 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
455 // Note that this function is probably obsolete
456 bool FGTowerController::hasInstruction(int id)
458 TrafficVectorIterator i = activeTraffic.begin();
459 // Search search if the current id has an entry
460 // This might be faster using a map instead of a vector, but let's start by taking a safe route
461 if (activeTraffic.size())
463 //while ((i->getId() != id) && i != activeTraffic.end()) {
464 while (i != activeTraffic.end()) {
465 if (i->getId() == id) {
471 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
472 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
474 return i->hasInstruction();
480 FGATCInstruction FGTowerController::getInstruction(int id)
482 TrafficVectorIterator i = activeTraffic.begin();
483 // Search search if the current id has an entry
484 // This might be faster using a map instead of a vector, but let's start by taking a safe route
485 if (activeTraffic.size()) {
486 //while ((i->getId() != id) && i != activeTraffic.end()) {
487 while (i != activeTraffic.end()) {
488 if (i->getId() == id) {
494 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
495 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
497 return i->getInstruction();
499 return FGATCInstruction();