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() :
48 allowTransmission(true) {
51 void FGTrafficRecord::setPositionAndIntentions(int pos, FGAIFlightPlan *route)
55 if (intentions.size()) {
56 intVecIterator i = intentions.begin();
58 SG_LOG(SG_GENERAL, SG_ALERT, "Error in FGTrafficRecord::setPositionAndIntentions");
59 //cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
60 for (intVecIterator i = intentions.begin(); i != intentions.end() ; i++) {
61 //cerr << (*i) << " ";
68 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
69 int size = route->getNrOfWayPoints();
70 //cerr << "Setting pos" << pos << " ";
71 //cerr << "setting intentions ";
72 for (int i = 0; i < size; i++) {
73 int val = route->getRouteIndex(i);
75 if ((val) && (val != pos))
77 intentions.push_back(val);
82 //while (route->next(&legNr, &routeNr)) {
83 //intentions.push_back(routeNr);
85 //route->rewind(currentPos);
90 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord &other)
93 //cerr << "Start check 1" << endl;
94 if (currentPos == other.currentPos)
96 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
99 // else if (other.intentions.size())
101 // cerr << "Start check 2" << endl;
102 // intVecIterator i = other.intentions.begin();
103 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
105 // if (i != other.intentions.end()) {
106 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
109 else if (intentions.size()) {
110 //cerr << "Start check 3" << endl;
111 intVecIterator i = intentions.begin();
112 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
113 while (i != intentions.end()) {
114 if ((*i) == other.currentPos) {
119 if (i != intentions.end()) {
120 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
124 //cerr << "Done !!" << endl;
128 void FGTrafficRecord::setPositionAndHeading(double lat, double lon, double hdg,
129 double spd, double alt)
138 int FGTrafficRecord::crosses(FGGroundNetwork *net, FGTrafficRecord &other)
140 if (checkPositionAndIntentions(other) || (other.checkPositionAndIntentions(*this)))
143 int currentTargetNode = 0, otherTargetNode = 0;
145 currentTargetNode = net->findSegment(currentPos )->getEnd()->getIndex(); // OKAY,...
146 if (other.currentPos > 0)
147 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
148 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
149 return currentTargetNode;
150 if (intentions.size())
152 for (i = intentions.begin(); i != intentions.end(); i++)
155 if ((currentTargetNode == net->findSegment(*i)->getEnd()->getIndex()))
157 //cerr << "Current crosses at " << currentTargetNode <<endl;
158 return currentTargetNode;
163 if (other.intentions.size())
165 for (i = other.intentions.begin(); i != other.intentions.end(); i++)
168 if (otherTargetNode == net->findSegment(*i)->getEnd()->getIndex())
170 //cerr << "Other crosses at " << currentTargetNode <<endl;
171 return otherTargetNode;
176 if (intentions.size() && other.intentions.size())
178 for (i = intentions.begin(); i != intentions.end(); i++)
180 for (j = other.intentions.begin(); j != other.intentions.end(); j++)
182 //cerr << "finding segment " << *i << " and " << *j << endl;
183 if (((*i) > 0) && ((*j) > 0)) {
184 currentTargetNode = net->findSegment(*i)->getEnd()->getIndex();
185 otherTargetNode = net->findSegment(*j)->getEnd()->getIndex();
186 if (currentTargetNode == otherTargetNode)
188 //cerr << "Routes will cross at " << currentTargetNode << endl;
189 return currentTargetNode;
198 bool FGTrafficRecord::onRoute(FGGroundNetwork *net, FGTrafficRecord &other)
200 int node = -1, othernode = -1;
202 node = net->findSegment(currentPos)->getEnd()->getIndex();
203 if (other.currentPos > 0)
204 othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
205 if ((node == othernode) && (node != -1))
207 if (other.intentions.size())
209 for (intVecIterator i = other.intentions.begin(); i != other.intentions.end(); i++)
213 othernode = net->findSegment(*i)->getEnd()->getIndex();
214 if ((node == othernode) && (node > -1))
219 //if (other.currentPos > 0)
220 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
221 //if (intentions.size())
223 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
227 // node = net->findSegment(*i)->getEnd()->getIndex();
228 // if ((node == othernode) && (node > -1))
237 bool FGTrafficRecord::isOpposing (FGGroundNetwork *net, FGTrafficRecord &other, int node)
239 // Check if current segment is the reverse segment for the other aircraft
241 //cerr << "Current segment " << currentPos << endl;
242 if ((currentPos > 0) && (other.currentPos > 0))
244 opp = net->findSegment(currentPos)->opposite();
246 if (opp->getIndex() == other.currentPos)
250 for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
252 if ((opp = net->findSegment(other.currentPos)->opposite()))
255 if (opp->getIndex() == net->findSegment(*i)->getIndex())
257 if (net->findSegment(*i)->getStart()->getIndex() == node) {
259 //cerr << "Found the node " << node << endl;
265 if (other.intentions.size())
267 for (intVecIterator j = other.intentions.begin(); j != other.intentions.end(); j++)
269 // cerr << "Current segment 1 " << (*i) << endl;
271 if ((opp = net->findSegment(*i)->opposite()))
273 if (opp->getIndex() ==
274 net->findSegment(*j)->getIndex())
276 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
277 // << " and " << net->findSegment(*j)->getIndex()
278 // << " are opposites " << endl;
279 if (net->findSegment(*i)->getStart()->getIndex() == node) {
281 //cerr << "Found the node " << node << endl;
295 void FGTrafficRecord::setSpeedAdjustment(double spd)
297 instruction.setChangeSpeed(true);
298 instruction.setSpeed(spd);
301 void FGTrafficRecord::setHeadingAdjustment(double heading)
303 instruction.setChangeHeading(true);
304 instruction.setHeading(heading);
307 bool FGTrafficRecord::pushBackAllowed() {
308 double course, az2,dist;
309 SGGeod curr(SGGeod::fromDegM(getLongitude(),
313 double userLatitude = fgGetDouble("/position/latitude-deg");
314 double userLongitude = fgGetDouble("/position/longitude-deg");
315 SGGeod user(SGGeod::fromDeg(userLongitude,userLatitude));
316 SGGeodesy::inverse(curr, user, course, az2, dist);
317 //cerr << "Distance to user : " << dist << endl;
324 /***************************************************************************
327 **************************************************************************/
328 FGATCInstruction::FGATCInstruction()
331 holdPosition = false;
333 changeHeading = false;
334 changeAltitude = false;
335 resolveCircularWait = false;
342 bool FGATCInstruction::hasInstruction()
344 return (holdPattern || holdPosition || changeSpeed || changeHeading || changeAltitude || resolveCircularWait);
347 string FGATCController::getGateName(FGAIAircraft *ref)
349 return ref->atGate();
352 void FGATCController::transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir)
354 string sender, receiver;
357 string atisInformation;
360 case ATC_AIR_TO_GROUND:
361 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
362 switch (rec->getLeg()) {
366 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(rec->getLeg());
368 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(3);
369 receiver = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Ground";
370 atisInformation = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getAtisInformation();
373 receiver = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Tower";
377 case ATC_GROUND_TO_AIR:
378 receiver = rec->getAircraft()->getTrafficRef()->getCallSign();
379 switch (rec->getLeg()) {
383 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(rec->getLeg());
385 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(3);
386 sender = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Ground";
387 atisInformation = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getAtisInformation();
391 sender = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Tower";
403 string transponderCode;
407 case MSG_ANNOUNCE_ENGINE_START:
408 text = sender + ". Ready to Start up";
410 case MSG_REQUEST_ENGINE_START:
411 text = receiver + ", This is " + sender + ". Position " +getGateName(rec->getAircraft()) +
412 ". Information " + atisInformation + ". " +
413 rec->getAircraft()->getTrafficRef()->getFlightRules() + " to " +
414 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->getName() + ". Request start-up";
416 // Acknowledge engine startup permission
417 // Assign departure runway
418 // Assign SID, if necessery (TODO)
419 case MSG_PERMIT_ENGINE_START:
420 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
422 heading = rec->getAircraft()->getTrafficRef()->getCourse();
423 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
424 rwyClass= rec->getAircraft()->GetFlightPlan()->getRunwayClassFromTrafficType(fltType);
426 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
427 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
428 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getSID(activeRunway, heading);
429 rec->getAircraft()->GetFlightPlan()->setSID(fp);
431 SID = fp->getName() + " departure";
433 SID = "fly runway heading ";
435 //snprintf(buffer, 7, "%3.2f", heading);
436 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
437 transponderCode = genTransponderCode(fltRules);
438 rec->getAircraft()->SetTransponderCode(transponderCode);
439 text = receiver + ". Start-up approved. " + atisInformation + " correct, runway " + activeRunway
440 + ", " + SID + ", squawk " + transponderCode + ". " +
441 "For push-back and taxi clearance call " + taxiFreqStr + ". " + sender + " control.";
443 case MSG_DENY_ENGINE_START:
444 text = receiver + ". Standby";
446 case MSG_ACKNOWLEDGE_ENGINE_START:
447 fp = rec->getAircraft()->GetFlightPlan()->getSID();
449 SID = rec->getAircraft()->GetFlightPlan()->getSID()->getName() + " departure";
451 SID = "fly runway heading ";
453 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
454 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
455 transponderCode = rec->getAircraft()->GetTransponderCode();
456 text = receiver + ". Start-up approved. " + atisInformation + " correct, runway " +
457 activeRunway + ", " + SID + ", squawk " + transponderCode + ". " +
458 "For push-back and taxi clearance call " + taxiFreqStr + ". " + sender;
460 case MSG_REQUEST_PUSHBACK_CLEARANCE:
461 text = receiver + ". Request push-back. " + sender;
463 case MSG_PERMIT_PUSHBACK_CLEARANCE:
464 text = receiver + ". Push-back approved. " + sender;
466 case MSG_HOLD_PUSHBACK_CLEARANCE:
467 text = receiver + ". Standby. " + sender;
470 text = sender + ". Transmitting unknown Message";
473 double onBoardRadioFreq0 = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
474 double onBoardRadioFreq1 = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
475 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
476 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
477 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << endl;
479 // Display ATC message only when one of the radios is tuned
480 // the relevant frequency.
481 // Note that distance attenuation is currently not yet implemented
482 //cerr << "Transmitting " << text << " at " << stationFreq;
483 if ((onBoardRadioFreqI0 == stationFreq) || (onBoardRadioFreqI1 == stationFreq)) {
484 if (rec->allowTransmissions()) {
485 fgSetString("/sim/messages/atc", text.c_str());
490 string FGATCController::formatATCFrequency3_2(int freq) {
492 snprintf(buffer, 7, "%3.2f", ( (float) freq / 100.0) );
493 return string(buffer);
496 string FGATCController::genTransponderCode(string fltRules) {
497 if (fltRules == "VFR") {
498 return string("1200");
501 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8,rand() % 8, rand() % 8);
502 return string(buffer);
506 /***************************************************************************
507 * class FGTowerController
509 **************************************************************************/
510 FGTowerController::FGTowerController() :
516 void FGTowerController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
517 double lat, double lon, double heading,
518 double speed, double alt, double radius, int leg,
521 TrafficVectorIterator i = activeTraffic.begin();
522 // Search whether the current id alread has an entry
523 // This might be faster using a map instead of a vector, but let's start by taking a safe route
524 if (activeTraffic.size()) {
525 //while ((i->getId() != id) && i != activeTraffic.end()) {
526 while (i != activeTraffic.end()) {
527 if (i->getId() == id) {
534 // Add a new TrafficRecord if no one exsists for this aircraft.
535 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
539 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
540 rec.setRunway(intendedRoute->getRunway());
542 //rec.setCallSign(callsign);
543 rec.setAircraft(ref);
544 activeTraffic.push_back(rec);
546 i->setPositionAndHeading(lat, lon, heading, speed, alt);
550 void FGTowerController::update(int id, double lat, double lon, double heading, double speed, double alt,
553 TrafficVectorIterator i = activeTraffic.begin();
554 // Search search if the current id has an entry
555 // This might be faster using a map instead of a vector, but let's start by taking a safe route
556 TrafficVectorIterator current, closest;
557 if (activeTraffic.size()) {
558 //while ((i->getId() != id) && i != activeTraffic.end()) {
559 while (i != activeTraffic.end()) {
560 if (i->getId() == id) {
567 // // update position of the current aircraft
568 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
569 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
571 i->setPositionAndHeading(lat, lon, heading, speed, alt);
576 // // see if we already have a clearance record for the currently active runway
577 ActiveRunwayVecIterator rwy = activeRunways.begin();
578 // again, a map might be more efficient here
579 if (activeRunways.size()) {
580 //while ((rwy->getRunwayName() != current->getRunway()) && (rwy != activeRunways.end())) {
581 while (rwy != activeRunways.end()) {
582 if (rwy->getRunwayName() == current->getRunway()) {
588 if (rwy == activeRunways.end()) {
589 ActiveRunway aRwy(current->getRunway(), id);
590 activeRunways.push_back(aRwy); // Since there are no clearance records for this runway yet
591 current->setHoldPosition(false); // Clear the current aircraft to continue
594 // Okay, we have a clearance record for this runway, so check
595 // whether the clearence ID matches that of the current aircraft
596 if (id == rwy->getCleared()) {
597 current->setHoldPosition(false);
599 current->setHoldPosition(true);
605 void FGTowerController::signOff(int id)
607 TrafficVectorIterator i = activeTraffic.begin();
608 // Search search if the current id alread has an entry
609 // This might be faster using a map instead of a vector, but let's start by taking a safe route
610 if (activeTraffic.size()) {
611 //while ((i->getId() != id) && i != activeTraffic.end()) {
612 while (i != activeTraffic.end()) {
613 if (i->getId() == id) {
619 // If this aircraft has left the runway, we can clear the departure record for this runway
620 ActiveRunwayVecIterator rwy = activeRunways.begin();
621 if (activeRunways.size()) {
622 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
623 while (rwy != activeRunways.end()) {
624 if (rwy->getRunwayName() == i->getRunway()) {
629 if (rwy != activeRunways.end()) {
630 rwy = activeRunways.erase(rwy);
632 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff");
635 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
636 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
638 i = activeTraffic.erase(i);
643 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
644 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
645 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
646 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
647 // Note that this function is probably obsolete
648 bool FGTowerController::hasInstruction(int id)
650 TrafficVectorIterator i = activeTraffic.begin();
651 // Search search if the current id has an entry
652 // This might be faster using a map instead of a vector, but let's start by taking a safe route
653 if (activeTraffic.size())
655 //while ((i->getId() != id) && i != activeTraffic.end()) {
656 while (i != activeTraffic.end()) {
657 if (i->getId() == id) {
663 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
664 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
666 return i->hasInstruction();
672 FGATCInstruction FGTowerController::getInstruction(int id)
674 TrafficVectorIterator i = activeTraffic.begin();
675 // Search search if the current id has an entry
676 // This might be faster using a map instead of a vector, but let's start by taking a safe route
677 if (activeTraffic.size()) {
678 //while ((i->getId() != id) && i != activeTraffic.end()) {
679 while (i != activeTraffic.end()) {
680 if (i->getId() == id) {
686 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
687 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
689 return i->getInstruction();
691 return FGATCInstruction();
694 /***************************************************************************
695 * class FGStartupController
697 **************************************************************************/
698 FGStartupController::FGStartupController() :
702 lastTransmission = 0;
705 void FGStartupController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
706 double lat, double lon, double heading,
707 double speed, double alt, double radius, int leg,
710 TrafficVectorIterator i = activeTraffic.begin();
711 // Search whether the current id alread has an entry
712 // This might be faster using a map instead of a vector, but let's start by taking a safe route
713 if (activeTraffic.size()) {
714 //while ((i->getId() != id) && i != activeTraffic.end()) {
715 while (i != activeTraffic.end()) {
716 if (i->getId() == id) {
723 // Add a new TrafficRecord if no one exsists for this aircraft.
724 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
728 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
729 rec.setRunway(intendedRoute->getRunway());
731 //rec.setCallSign(callsign);
732 rec.setAircraft(ref);
733 rec.setHoldPosition(true);
734 activeTraffic.push_back(rec);
736 i->setPositionAndHeading(lat, lon, heading, speed, alt);
742 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
743 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
744 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
745 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
746 // Note that this function is probably obsolete
747 bool FGStartupController::hasInstruction(int id)
749 TrafficVectorIterator i = activeTraffic.begin();
750 // Search search if the current id has an entry
751 // This might be faster using a map instead of a vector, but let's start by taking a safe route
752 if (activeTraffic.size())
754 //while ((i->getId() != id) && i != activeTraffic.end()) {
755 while (i != activeTraffic.end()) {
756 if (i->getId() == id) {
762 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
763 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
765 return i->hasInstruction();
771 FGATCInstruction FGStartupController::getInstruction(int id)
773 TrafficVectorIterator i = activeTraffic.begin();
774 // Search search if the current id has an entry
775 // This might be faster using a map instead of a vector, but let's start by taking a safe route
776 if (activeTraffic.size()) {
777 //while ((i->getId() != id) && i != activeTraffic.end()) {
778 while (i != activeTraffic.end()) {
779 if (i->getId() == id) {
785 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
786 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
788 return i->getInstruction();
790 return FGATCInstruction();
793 void FGStartupController::signOff(int id)
795 TrafficVectorIterator i = activeTraffic.begin();
796 // Search search if the current id alread has an entry
797 // This might be faster using a map instead of a vector, but let's start by taking a safe route
798 if (activeTraffic.size()) {
799 //while ((i->getId() != id) && i != activeTraffic.end()) {
800 while (i != activeTraffic.end()) {
801 if (i->getId() == id) {
807 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
808 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
810 i = activeTraffic.erase(i);
814 void FGStartupController::update(int id, double lat, double lon, double heading, double speed, double alt,
817 TrafficVectorIterator i = activeTraffic.begin();
818 // Search search if the current id has an entry
819 // This might be faster using a map instead of a vector, but let's start by taking a safe route
820 TrafficVectorIterator current, closest;
821 if (activeTraffic.size()) {
822 //while ((i->getId() != id) && i != activeTraffic.end()) {
823 while (i != activeTraffic.end()) {
824 if (i->getId() == id) {
831 // // update position of the current aircraft
832 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
833 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
835 i->setPositionAndHeading(lat, lon, heading, speed, alt);
840 int state = i->getState();
841 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
842 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
843 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
844 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
845 // << " at parking " << getGateName(i->getAircraft()) << endl;
847 if ((now - lastTransmission) > 3 + (rand() % 15)) {
851 if ((state == 0) && available) {
852 if (now > startTime) {
853 //cerr << "Transmitting startup msg" << endl;
854 transmit(&(*i), MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
856 lastTransmission = now;
860 if ((state == 1) && available) {
861 if (now > startTime+60) {
862 transmit(&(*i), MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
864 lastTransmission = now;
868 if ((state == 2) && available) {
869 if (now > startTime+80) {
870 transmit(&(*i), MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
872 lastTransmission = now;
876 if ((state == 3) && available){
877 if (now > startTime+100) {
878 transmit(&(*i), MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
880 lastTransmission = now;
884 // TODO: Switch to APRON control and request pushback Clearance.
885 // Get Push back clearance
886 if ((state == 4) && available){
887 if (now > startTime+130) {
888 transmit(&(*i), MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
890 lastTransmission = now;
894 if ((state == 5) && available){
895 if (now > startTime+130) {
896 if (i->pushBackAllowed()) {
897 i->allowRepeatedTransmissions();
898 transmit(&(*i), MSG_PERMIT_PUSHBACK_CLEARANCE, ATC_GROUND_TO_AIR);
901 transmit(&(*i), MSG_HOLD_PUSHBACK_CLEARANCE, ATC_GROUND_TO_AIR);
902 i->suppressRepeatedTransmissions();
904 lastTransmission = now;
908 if ((state == 6) && available){
909 i->setHoldPosition(false);