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/AIAircraft.hxx>
29 #include <AIModel/AIFlightPlan.hxx>
30 #include <Traffic/TrafficMgr.hxx>
31 #include <Airports/groundnetwork.hxx>
32 #include <Airports/dynamics.hxx>
34 /***************************************************************************
36 **************************************************************************/
37 FGTrafficRecord::FGTrafficRecord() :
49 allowTransmission(true) {
52 void FGTrafficRecord::setPositionAndIntentions(int pos, FGAIFlightPlan *route)
56 if (intentions.size()) {
57 intVecIterator i = intentions.begin();
59 SG_LOG(SG_GENERAL, SG_ALERT, "Error in FGTrafficRecord::setPositionAndIntentions");
60 //cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
61 for (intVecIterator i = intentions.begin(); i != intentions.end() ; i++) {
62 //cerr << (*i) << " ";
69 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
70 int size = route->getNrOfWayPoints();
71 //cerr << "Setting pos" << pos << " ";
72 //cerr << "setting intentions ";
73 for (int i = 0; i < size; i++) {
74 int val = route->getRouteIndex(i);
76 if ((val) && (val != pos))
78 intentions.push_back(val);
83 //while (route->next(&legNr, &routeNr)) {
84 //intentions.push_back(routeNr);
86 //route->rewind(currentPos);
91 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord &other)
94 //cerr << "Start check 1" << endl;
95 if (currentPos == other.currentPos)
97 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
100 // else if (other.intentions.size())
102 // cerr << "Start check 2" << endl;
103 // intVecIterator i = other.intentions.begin();
104 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
106 // if (i != other.intentions.end()) {
107 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
110 else if (intentions.size()) {
111 //cerr << "Start check 3" << endl;
112 intVecIterator i = intentions.begin();
113 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
114 while (i != intentions.end()) {
115 if ((*i) == other.currentPos) {
120 if (i != intentions.end()) {
121 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
125 //cerr << "Done !!" << endl;
129 void FGTrafficRecord::setPositionAndHeading(double lat, double lon, double hdg,
130 double spd, double alt)
139 int FGTrafficRecord::crosses(FGGroundNetwork *net, FGTrafficRecord &other)
141 if (checkPositionAndIntentions(other) || (other.checkPositionAndIntentions(*this)))
144 int currentTargetNode = 0, otherTargetNode = 0;
146 currentTargetNode = net->findSegment(currentPos )->getEnd()->getIndex(); // OKAY,...
147 if (other.currentPos > 0)
148 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
149 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
150 return currentTargetNode;
151 if (intentions.size())
153 for (i = intentions.begin(); i != intentions.end(); i++)
156 if ((currentTargetNode == net->findSegment(*i)->getEnd()->getIndex()))
158 //cerr << "Current crosses at " << currentTargetNode <<endl;
159 return currentTargetNode;
164 if (other.intentions.size())
166 for (i = other.intentions.begin(); i != other.intentions.end(); i++)
169 if (otherTargetNode == net->findSegment(*i)->getEnd()->getIndex())
171 //cerr << "Other crosses at " << currentTargetNode <<endl;
172 return otherTargetNode;
177 if (intentions.size() && other.intentions.size())
179 for (i = intentions.begin(); i != intentions.end(); i++)
181 for (j = other.intentions.begin(); j != other.intentions.end(); j++)
183 //cerr << "finding segment " << *i << " and " << *j << endl;
184 if (((*i) > 0) && ((*j) > 0)) {
185 currentTargetNode = net->findSegment(*i)->getEnd()->getIndex();
186 otherTargetNode = net->findSegment(*j)->getEnd()->getIndex();
187 if (currentTargetNode == otherTargetNode)
189 //cerr << "Routes will cross at " << currentTargetNode << endl;
190 return currentTargetNode;
199 bool FGTrafficRecord::onRoute(FGGroundNetwork *net, FGTrafficRecord &other)
201 int node = -1, othernode = -1;
203 node = net->findSegment(currentPos)->getEnd()->getIndex();
204 if (other.currentPos > 0)
205 othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
206 if ((node == othernode) && (node != -1))
208 if (other.intentions.size())
210 for (intVecIterator i = other.intentions.begin(); i != other.intentions.end(); i++)
214 othernode = net->findSegment(*i)->getEnd()->getIndex();
215 if ((node == othernode) && (node > -1))
220 //if (other.currentPos > 0)
221 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
222 //if (intentions.size())
224 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
228 // node = net->findSegment(*i)->getEnd()->getIndex();
229 // if ((node == othernode) && (node > -1))
238 bool FGTrafficRecord::isOpposing (FGGroundNetwork *net, FGTrafficRecord &other, int node)
240 // Check if current segment is the reverse segment for the other aircraft
242 //cerr << "Current segment " << currentPos << endl;
243 if ((currentPos > 0) && (other.currentPos > 0))
245 opp = net->findSegment(currentPos)->opposite();
247 if (opp->getIndex() == other.currentPos)
251 for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
253 if ((opp = net->findSegment(other.currentPos)->opposite()))
256 if (opp->getIndex() == net->findSegment(*i)->getIndex())
258 if (net->findSegment(*i)->getStart()->getIndex() == node) {
260 //cerr << "Found the node " << node << endl;
266 if (other.intentions.size())
268 for (intVecIterator j = other.intentions.begin(); j != other.intentions.end(); j++)
270 // cerr << "Current segment 1 " << (*i) << endl;
272 if ((opp = net->findSegment(*i)->opposite()))
274 if (opp->getIndex() ==
275 net->findSegment(*j)->getIndex())
277 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
278 // << " and " << net->findSegment(*j)->getIndex()
279 // << " are opposites " << endl;
280 if (net->findSegment(*i)->getStart()->getIndex() == node) {
282 //cerr << "Found the node " << node << endl;
296 void FGTrafficRecord::setSpeedAdjustment(double spd)
298 instruction.setChangeSpeed(true);
299 instruction.setSpeed(spd);
302 void FGTrafficRecord::setHeadingAdjustment(double heading)
304 instruction.setChangeHeading(true);
305 instruction.setHeading(heading);
308 bool FGTrafficRecord::pushBackAllowed() {
309 double course, az2,dist;
310 SGGeod curr(SGGeod::fromDegM(getLongitude(),
314 double userLatitude = fgGetDouble("/position/latitude-deg");
315 double userLongitude = fgGetDouble("/position/longitude-deg");
316 SGGeod user(SGGeod::fromDeg(userLongitude,userLatitude));
317 SGGeodesy::inverse(curr, user, course, az2, dist);
318 //cerr << "Distance to user : " << dist << endl;
325 /***************************************************************************
328 **************************************************************************/
329 FGATCInstruction::FGATCInstruction()
332 holdPosition = false;
334 changeHeading = false;
335 changeAltitude = false;
336 resolveCircularWait = false;
343 bool FGATCInstruction::hasInstruction()
345 return (holdPattern || holdPosition || changeSpeed || changeHeading || changeAltitude || resolveCircularWait);
348 string FGATCController::getGateName(FGAIAircraft *ref)
350 return ref->atGate();
353 void FGATCController::transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir)
355 string sender, receiver;
359 string atisInformation;
367 string transponderCode;
372 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
373 switch (rec->getLeg()) {
376 freqId = rec->getNextFrequency();
378 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(rec->getLeg()+freqId);
380 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(3);
381 receiver = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Ground";
382 atisInformation = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getAtisInformation();
385 receiver = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Tower";
388 // Swap sender and receiver value in case of a ground to air transmission
389 if (msgDir == ATC_GROUND_TO_AIR) {
395 case MSG_ANNOUNCE_ENGINE_START:
396 text = sender + ". Ready to Start up";
398 case MSG_REQUEST_ENGINE_START:
399 text = receiver + ", This is " + sender + ". Position " +getGateName(rec->getAircraft()) +
400 ". Information " + atisInformation + ". " +
401 rec->getAircraft()->getTrafficRef()->getFlightRules() + " to " +
402 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->getName() + ". Request start-up";
404 // Acknowledge engine startup permission
405 // Assign departure runway
406 // Assign SID, if necessery (TODO)
407 case MSG_PERMIT_ENGINE_START:
408 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
410 heading = rec->getAircraft()->getTrafficRef()->getCourse();
411 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
412 rwyClass= rec->getAircraft()->GetFlightPlan()->getRunwayClassFromTrafficType(fltType);
414 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
415 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
416 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getSID(activeRunway, heading);
417 rec->getAircraft()->GetFlightPlan()->setSID(fp);
419 SID = fp->getName() + " departure";
421 SID = "fly runway heading ";
423 //snprintf(buffer, 7, "%3.2f", heading);
424 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
425 transponderCode = genTransponderCode(fltRules);
426 rec->getAircraft()->SetTransponderCode(transponderCode);
427 text = receiver + ". Start-up approved. " + atisInformation + " correct, runway " + activeRunway
428 + ", " + SID + ", squawk " + transponderCode + ". " +
429 "For push-back and taxi clearance call " + taxiFreqStr + ". " + sender + " control.";
431 case MSG_DENY_ENGINE_START:
432 text = receiver + ". Standby";
434 case MSG_ACKNOWLEDGE_ENGINE_START:
435 fp = rec->getAircraft()->GetFlightPlan()->getSID();
437 SID = rec->getAircraft()->GetFlightPlan()->getSID()->getName() + " departure";
439 SID = "fly runway heading ";
441 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
442 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
443 transponderCode = rec->getAircraft()->GetTransponderCode();
444 text = receiver + ". Start-up approved. " + atisInformation + " correct, runway " +
445 activeRunway + ", " + SID + ", squawk " + transponderCode + ". " +
446 "For push-back and taxi clearance call " + taxiFreqStr + ". " + sender;
448 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
449 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
450 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
452 case MSG_REQUEST_PUSHBACK_CLEARANCE:
453 text = receiver + ". Request push-back. " + sender;
455 case MSG_PERMIT_PUSHBACK_CLEARANCE:
456 text = receiver + ". Push-back approved. " + sender;
458 case MSG_HOLD_PUSHBACK_CLEARANCE:
459 text = receiver + ". Standby. " + sender;
462 text = sender + ". Transmitting unknown Message";
465 double onBoardRadioFreq0 = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
466 double onBoardRadioFreq1 = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
467 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
468 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
469 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << endl;
471 // Display ATC message only when one of the radios is tuned
472 // the relevant frequency.
473 // Note that distance attenuation is currently not yet implemented
474 if ((onBoardRadioFreqI0 == stationFreq) || (onBoardRadioFreqI1 == stationFreq)) {
475 if (rec->allowTransmissions()) {
476 fgSetString("/sim/messages/atc", text.c_str());
481 string FGATCController::formatATCFrequency3_2(int freq) {
483 snprintf(buffer, 7, "%3.2f", ( (float) freq / 100.0) );
484 return string(buffer);
487 // TODO: Set transponder codes according to real-world routes.
488 // The current version just returns a random string of four octal numbers.
489 string FGATCController::genTransponderCode(string fltRules) {
490 if (fltRules == "VFR") {
491 return string("1200");
494 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8,rand() % 8, rand() % 8);
495 return string(buffer);
499 /***************************************************************************
500 * class FGTowerController
502 **************************************************************************/
503 FGTowerController::FGTowerController() :
509 void FGTowerController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
510 double lat, double lon, double heading,
511 double speed, double alt, double radius, int leg,
514 TrafficVectorIterator i = activeTraffic.begin();
515 // Search whether the current id alread has an entry
516 // This might be faster using a map instead of a vector, but let's start by taking a safe route
517 if (activeTraffic.size()) {
518 //while ((i->getId() != id) && i != activeTraffic.end()) {
519 while (i != activeTraffic.end()) {
520 if (i->getId() == id) {
527 // Add a new TrafficRecord if no one exsists for this aircraft.
528 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
532 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
533 rec.setRunway(intendedRoute->getRunway());
535 //rec.setCallSign(callsign);
536 rec.setAircraft(ref);
537 activeTraffic.push_back(rec);
539 i->setPositionAndHeading(lat, lon, heading, speed, alt);
543 void FGTowerController::update(int id, double lat, double lon, double heading, double speed, double alt,
546 TrafficVectorIterator i = activeTraffic.begin();
547 // Search search if the current id has an entry
548 // This might be faster using a map instead of a vector, but let's start by taking a safe route
549 TrafficVectorIterator current, closest;
550 if (activeTraffic.size()) {
551 //while ((i->getId() != id) && i != activeTraffic.end()) {
552 while (i != activeTraffic.end()) {
553 if (i->getId() == id) {
560 // // update position of the current aircraft
561 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
562 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
564 i->setPositionAndHeading(lat, lon, heading, speed, alt);
569 // // see if we already have a clearance record for the currently active runway
570 ActiveRunwayVecIterator rwy = activeRunways.begin();
571 // again, a map might be more efficient here
572 if (activeRunways.size()) {
573 //while ((rwy->getRunwayName() != current->getRunway()) && (rwy != activeRunways.end())) {
574 while (rwy != activeRunways.end()) {
575 if (rwy->getRunwayName() == current->getRunway()) {
581 if (rwy == activeRunways.end()) {
582 ActiveRunway aRwy(current->getRunway(), id);
583 activeRunways.push_back(aRwy); // Since there are no clearance records for this runway yet
584 current->setHoldPosition(false); // Clear the current aircraft to continue
587 // Okay, we have a clearance record for this runway, so check
588 // whether the clearence ID matches that of the current aircraft
589 if (id == rwy->getCleared()) {
590 current->setHoldPosition(false);
592 current->setHoldPosition(true);
598 void FGTowerController::signOff(int id)
600 TrafficVectorIterator i = activeTraffic.begin();
601 // Search search if the current id alread has an entry
602 // This might be faster using a map instead of a vector, but let's start by taking a safe route
603 if (activeTraffic.size()) {
604 //while ((i->getId() != id) && i != activeTraffic.end()) {
605 while (i != activeTraffic.end()) {
606 if (i->getId() == id) {
612 // If this aircraft has left the runway, we can clear the departure record for this runway
613 ActiveRunwayVecIterator rwy = activeRunways.begin();
614 if (activeRunways.size()) {
615 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
616 while (rwy != activeRunways.end()) {
617 if (rwy->getRunwayName() == i->getRunway()) {
622 if (rwy != activeRunways.end()) {
623 rwy = activeRunways.erase(rwy);
625 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff");
628 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
629 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
631 i = activeTraffic.erase(i);
636 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
637 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
638 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
639 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
640 // Note that this function is probably obsolete
641 bool FGTowerController::hasInstruction(int id)
643 TrafficVectorIterator i = activeTraffic.begin();
644 // Search search if the current id has an entry
645 // This might be faster using a map instead of a vector, but let's start by taking a safe route
646 if (activeTraffic.size())
648 //while ((i->getId() != id) && i != activeTraffic.end()) {
649 while (i != activeTraffic.end()) {
650 if (i->getId() == id) {
656 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
657 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
659 return i->hasInstruction();
665 FGATCInstruction FGTowerController::getInstruction(int id)
667 TrafficVectorIterator i = activeTraffic.begin();
668 // Search search if the current id has an entry
669 // This might be faster using a map instead of a vector, but let's start by taking a safe route
670 if (activeTraffic.size()) {
671 //while ((i->getId() != id) && i != activeTraffic.end()) {
672 while (i != activeTraffic.end()) {
673 if (i->getId() == id) {
679 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
680 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
682 return i->getInstruction();
684 return FGATCInstruction();
687 /***************************************************************************
688 * class FGStartupController
690 **************************************************************************/
691 FGStartupController::FGStartupController() :
695 lastTransmission = 0;
698 void FGStartupController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
699 double lat, double lon, double heading,
700 double speed, double alt, double radius, int leg,
703 TrafficVectorIterator i = activeTraffic.begin();
704 // Search whether the current id alread has an entry
705 // This might be faster using a map instead of a vector, but let's start by taking a safe route
706 if (activeTraffic.size()) {
707 //while ((i->getId() != id) && i != activeTraffic.end()) {
708 while (i != activeTraffic.end()) {
709 if (i->getId() == id) {
716 // Add a new TrafficRecord if no one exsists for this aircraft.
717 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
721 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
722 rec.setRunway(intendedRoute->getRunway());
724 //rec.setCallSign(callsign);
725 rec.setAircraft(ref);
726 rec.setHoldPosition(true);
727 activeTraffic.push_back(rec);
729 i->setPositionAndHeading(lat, lon, heading, speed, alt);
735 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
736 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
737 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
738 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
739 // Note that this function is probably obsolete
740 bool FGStartupController::hasInstruction(int id)
742 TrafficVectorIterator i = activeTraffic.begin();
743 // Search search if the current id has an entry
744 // This might be faster using a map instead of a vector, but let's start by taking a safe route
745 if (activeTraffic.size())
747 //while ((i->getId() != id) && i != activeTraffic.end()) {
748 while (i != activeTraffic.end()) {
749 if (i->getId() == id) {
755 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
756 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
758 return i->hasInstruction();
764 FGATCInstruction FGStartupController::getInstruction(int id)
766 TrafficVectorIterator i = activeTraffic.begin();
767 // Search search if the current id has an entry
768 // This might be faster using a map instead of a vector, but let's start by taking a safe route
769 if (activeTraffic.size()) {
770 //while ((i->getId() != id) && i != activeTraffic.end()) {
771 while (i != activeTraffic.end()) {
772 if (i->getId() == id) {
778 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
779 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
781 return i->getInstruction();
783 return FGATCInstruction();
786 void FGStartupController::signOff(int id)
788 TrafficVectorIterator i = activeTraffic.begin();
789 // Search search if the current id alread has an entry
790 // This might be faster using a map instead of a vector, but let's start by taking a safe route
791 if (activeTraffic.size()) {
792 //while ((i->getId() != id) && i != activeTraffic.end()) {
793 while (i != activeTraffic.end()) {
794 if (i->getId() == id) {
800 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
801 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
803 i = activeTraffic.erase(i);
807 void FGStartupController::update(int id, double lat, double lon, double heading, double speed, double alt,
810 TrafficVectorIterator i = activeTraffic.begin();
811 // Search search if the current id has an entry
812 // This might be faster using a map instead of a vector, but let's start by taking a safe route
813 TrafficVectorIterator current, closest;
814 if (activeTraffic.size()) {
815 //while ((i->getId() != id) && i != activeTraffic.end()) {
816 while (i != activeTraffic.end()) {
817 if (i->getId() == id) {
824 // // update position of the current aircraft
825 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
826 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
828 i->setPositionAndHeading(lat, lon, heading, speed, alt);
833 int state = i->getState();
834 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
835 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
836 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
837 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
838 // << " at parking " << getGateName(i->getAircraft()) << endl;
840 if ((now - lastTransmission) > 3 + (rand() % 15)) {
844 if ((state == 0) && available) {
845 if (now > startTime) {
846 //cerr << "Transmitting startup msg" << endl;
847 transmit(&(*i), MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
849 lastTransmission = now;
853 if ((state == 1) && available) {
854 if (now > startTime+60) {
855 transmit(&(*i), MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
857 lastTransmission = now;
861 if ((state == 2) && available) {
862 if (now > startTime+80) {
863 transmit(&(*i), MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
865 lastTransmission = now;
869 if ((state == 3) && available){
870 if (now > startTime+100) {
871 transmit(&(*i), MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
873 lastTransmission = now;
877 // Note: The next two stages are only necessesary when Startup control is
878 // on a different frequency, compared to ground control
879 if ((state == 4) && available){
880 if (now > startTime+130) {
881 transmit(&(*i), MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND);
884 lastTransmission = now;
890 // TODO: Switch to APRON control and request pushback Clearance.
891 // Get Push back clearance
892 if ((state == 5) && available){
893 if (now > startTime+160) {
894 transmit(&(*i), MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
896 lastTransmission = now;
900 if ((state == 6) && available){
901 if (now > startTime+180) {
902 if (i->pushBackAllowed()) {
903 i->allowRepeatedTransmissions();
904 transmit(&(*i), MSG_PERMIT_PUSHBACK_CLEARANCE, ATC_GROUND_TO_AIR);
907 transmit(&(*i), MSG_HOLD_PUSHBACK_CLEARANCE, ATC_GROUND_TO_AIR);
908 i->suppressRepeatedTransmissions();
910 lastTransmission = now;
914 if ((state == 6) && available){
915 i->setHoldPosition(false);