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() :
43 allowTransmission(true),
53 void FGTrafficRecord::setPositionAndIntentions(int pos, FGAIFlightPlan *route)
57 if (intentions.size()) {
58 intVecIterator i = intentions.begin();
60 SG_LOG(SG_GENERAL, SG_ALERT, "Error in FGTrafficRecord::setPositionAndIntentions");
61 //cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
62 for (intVecIterator i = intentions.begin(); i != intentions.end() ; i++) {
63 //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;
344 bool FGATCInstruction::hasInstruction()
346 return (holdPattern || holdPosition || changeSpeed || changeHeading || changeAltitude || resolveCircularWait);
349 /***************************************************************************
352 **************************************************************************/
357 FGATCController::FGATCController()
361 lastTransmission = 0;
364 string FGATCController::getGateName(FGAIAircraft *ref)
366 return ref->atGate();
369 void FGATCController::transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir)
371 string sender, receiver;
375 string atisInformation;
383 string transponderCode;
388 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
389 //cerr << "transmitting for: " << sender << "Leg = " << rec->getLeg() << endl;
390 switch (rec->getLeg()) {
393 freqId = rec->getNextFrequency();
395 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(rec->getLeg()+freqId);
397 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getGroundFrequency(3);
398 receiver = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Ground";
399 atisInformation = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getAtisInformation();
402 receiver = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getName() + "-Tower";
405 // Swap sender and receiver value in case of a ground to air transmission
406 if (msgDir == ATC_GROUND_TO_AIR) {
412 case MSG_ANNOUNCE_ENGINE_START:
413 text = sender + ". Ready to Start up";
415 case MSG_REQUEST_ENGINE_START:
416 text = receiver + ", This is " + sender + ". Position " +getGateName(rec->getAircraft()) +
417 ". Information " + atisInformation + ". " +
418 rec->getAircraft()->getTrafficRef()->getFlightRules() + " to " +
419 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->getName() + ". Request start-up";
421 // Acknowledge engine startup permission
422 // Assign departure runway
423 // Assign SID, if necessery (TODO)
424 case MSG_PERMIT_ENGINE_START:
425 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
427 heading = rec->getAircraft()->getTrafficRef()->getCourse();
428 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
429 rwyClass= rec->getAircraft()->GetFlightPlan()->getRunwayClassFromTrafficType(fltType);
431 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getActiveRunway(rwyClass, 1, activeRunway, heading);
432 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
433 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->getDynamics()->getSID(activeRunway, heading);
434 rec->getAircraft()->GetFlightPlan()->setSID(fp);
436 SID = fp->getName() + " departure";
438 SID = "fly runway heading ";
440 //snprintf(buffer, 7, "%3.2f", heading);
441 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
442 transponderCode = genTransponderCode(fltRules);
443 rec->getAircraft()->SetTransponderCode(transponderCode);
444 text = receiver + ". Start-up approved. " + atisInformation + " correct, runway " + activeRunway
445 + ", " + SID + ", squawk " + transponderCode + ". " +
446 "For push-back and taxi clearance call " + taxiFreqStr + ". " + sender + " control.";
448 case MSG_DENY_ENGINE_START:
449 text = receiver + ". Standby";
451 case MSG_ACKNOWLEDGE_ENGINE_START:
452 fp = rec->getAircraft()->GetFlightPlan()->getSID();
454 SID = rec->getAircraft()->GetFlightPlan()->getSID()->getName() + " departure";
456 SID = "fly runway heading ";
458 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
459 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
460 transponderCode = rec->getAircraft()->GetTransponderCode();
461 text = receiver + ". Start-up approved. " + atisInformation + " correct, runway " +
462 activeRunway + ", " + SID + ", squawk " + transponderCode + ". " +
463 "For push-back and taxi clearance call " + taxiFreqStr + ". " + sender;
465 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
466 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
467 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
469 case MSG_INITIATE_CONTACT:
470 text = receiver + ". With you. " + sender;
472 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
473 text = receiver + ". Roger. " + sender;
475 case MSG_REQUEST_PUSHBACK_CLEARANCE:
476 text = receiver + ". Request push-back. " + sender;
478 case MSG_PERMIT_PUSHBACK_CLEARANCE:
479 text = receiver + ". Push-back approved. " + sender;
481 case MSG_HOLD_PUSHBACK_CLEARANCE:
482 text = receiver + ". Standby. " + sender;
484 case MSG_REQUEST_TAXI_CLEARANCE:
485 text = receiver + ". Ready to Taxi. " + sender;
487 case MSG_ISSUE_TAXI_CLEARANCE:
488 text = receiver + ". Cleared to taxi. " + sender;
490 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
491 text = receiver + ". Cleared to taxi. " + sender;
493 case MSG_HOLD_POSITION:
494 text = receiver + ". Hold Position. " + sender;
496 case MSG_ACKNOWLEDGE_HOLD_POSITION:
497 text = receiver + ". Holding Position. " + sender;
499 case MSG_RESUME_TAXI:
500 text = receiver + ". Resume Taxiing. " + sender;
502 case MSG_ACKNOWLEDGE_RESUME_TAXI:
503 text = receiver + ". Continuing Taxi. " + sender;
506 text = text + sender + ". Transmitting unknown Message";
509 double onBoardRadioFreq0 = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
510 double onBoardRadioFreq1 = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
511 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
512 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
513 cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << endl;
515 // Display ATC message only when one of the radios is tuned
516 // the relevant frequency.
517 // Note that distance attenuation is currently not yet implemented
518 if ((onBoardRadioFreqI0 == stationFreq) || (onBoardRadioFreqI1 == stationFreq)) {
519 if (rec->allowTransmissions()) {
520 fgSetString("/sim/messages/atc", text.c_str());
525 string FGATCController::formatATCFrequency3_2(int freq) {
527 snprintf(buffer, 7, "%3.2f", ( (float) freq / 100.0) );
528 return string(buffer);
531 // TODO: Set transponder codes according to real-world routes.
532 // The current version just returns a random string of four octal numbers.
533 string FGATCController::genTransponderCode(string fltRules) {
534 if (fltRules == "VFR") {
535 return string("1200");
538 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8,rand() % 8, rand() % 8);
539 return string(buffer);
543 /***************************************************************************
544 * class FGTowerController
546 **************************************************************************/
547 FGTowerController::FGTowerController() :
553 void FGTowerController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
554 double lat, double lon, double heading,
555 double speed, double alt, double radius, int leg,
558 TrafficVectorIterator i = activeTraffic.begin();
559 // Search whether the current id alread has an entry
560 // This might be faster using a map instead of a vector, but let's start by taking a safe route
561 if (activeTraffic.size()) {
562 //while ((i->getId() != id) && i != activeTraffic.end()) {
563 while (i != activeTraffic.end()) {
564 if (i->getId() == id) {
571 // Add a new TrafficRecord if no one exsists for this aircraft.
572 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
576 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
577 rec.setRunway(intendedRoute->getRunway());
579 //rec.setCallSign(callsign);
580 rec.setAircraft(ref);
581 activeTraffic.push_back(rec);
583 i->setPositionAndHeading(lat, lon, heading, speed, alt);
587 void FGTowerController::update(int id, double lat, double lon, double heading, double speed, double alt,
590 TrafficVectorIterator i = activeTraffic.begin();
591 // Search search if the current id has an entry
592 // This might be faster using a map instead of a vector, but let's start by taking a safe route
593 TrafficVectorIterator current, closest;
594 if (activeTraffic.size()) {
595 //while ((i->getId() != id) && i != activeTraffic.end()) {
596 while (i != activeTraffic.end()) {
597 if (i->getId() == id) {
604 // // update position of the current aircraft
605 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
606 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
608 i->setPositionAndHeading(lat, lon, heading, speed, alt);
613 // // see if we already have a clearance record for the currently active runway
614 ActiveRunwayVecIterator rwy = activeRunways.begin();
615 // again, a map might be more efficient here
616 if (activeRunways.size()) {
617 //while ((rwy->getRunwayName() != current->getRunway()) && (rwy != activeRunways.end())) {
618 while (rwy != activeRunways.end()) {
619 if (rwy->getRunwayName() == current->getRunway()) {
625 if (rwy == activeRunways.end()) {
626 ActiveRunway aRwy(current->getRunway(), id);
627 activeRunways.push_back(aRwy); // Since there are no clearance records for this runway yet
628 current->setHoldPosition(false); // Clear the current aircraft to continue
631 // Okay, we have a clearance record for this runway, so check
632 // whether the clearence ID matches that of the current aircraft
633 if (id == rwy->getCleared()) {
634 current->setHoldPosition(false);
636 current->setHoldPosition(true);
642 void FGTowerController::signOff(int id)
644 TrafficVectorIterator i = activeTraffic.begin();
645 // Search search if the current id alread has an entry
646 // This might be faster using a map instead of a vector, but let's start by taking a safe route
647 if (activeTraffic.size()) {
648 //while ((i->getId() != id) && i != activeTraffic.end()) {
649 while (i != activeTraffic.end()) {
650 if (i->getId() == id) {
656 // If this aircraft has left the runway, we can clear the departure record for this runway
657 ActiveRunwayVecIterator rwy = activeRunways.begin();
658 if (activeRunways.size()) {
659 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
660 while (rwy != activeRunways.end()) {
661 if (rwy->getRunwayName() == i->getRunway()) {
666 if (rwy != activeRunways.end()) {
667 rwy = activeRunways.erase(rwy);
669 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff");
672 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
673 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
675 i = activeTraffic.erase(i);
680 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
681 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
682 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
683 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
684 // Note that this function is probably obsolete
685 bool FGTowerController::hasInstruction(int id)
687 TrafficVectorIterator i = activeTraffic.begin();
688 // Search search if the current id has an entry
689 // This might be faster using a map instead of a vector, but let's start by taking a safe route
690 if (activeTraffic.size())
692 //while ((i->getId() != id) && i != activeTraffic.end()) {
693 while (i != activeTraffic.end()) {
694 if (i->getId() == id) {
700 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
701 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
703 return i->hasInstruction();
709 FGATCInstruction FGTowerController::getInstruction(int id)
711 TrafficVectorIterator i = activeTraffic.begin();
712 // Search search if the current id has an entry
713 // This might be faster using a map instead of a vector, but let's start by taking a safe route
714 if (activeTraffic.size()) {
715 //while ((i->getId() != id) && i != activeTraffic.end()) {
716 while (i != activeTraffic.end()) {
717 if (i->getId() == id) {
723 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
724 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
726 return i->getInstruction();
728 return FGATCInstruction();
731 /***************************************************************************
732 * class FGStartupController
734 **************************************************************************/
735 FGStartupController::FGStartupController() :
740 void FGStartupController::announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentPosition,
741 double lat, double lon, double heading,
742 double speed, double alt, double radius, int leg,
745 TrafficVectorIterator i = activeTraffic.begin();
746 // Search whether the current id alread has an entry
747 // This might be faster using a map instead of a vector, but let's start by taking a safe route
748 if (activeTraffic.size()) {
749 //while ((i->getId() != id) && i != activeTraffic.end()) {
750 while (i != activeTraffic.end()) {
751 if (i->getId() == id) {
758 // Add a new TrafficRecord if no one exsists for this aircraft.
759 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
763 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
764 rec.setRunway(intendedRoute->getRunway());
766 //rec.setCallSign(callsign);
767 rec.setAircraft(ref);
768 rec.setHoldPosition(true);
769 activeTraffic.push_back(rec);
771 i->setPositionAndHeading(lat, lon, heading, speed, alt);
777 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
778 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
779 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
780 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
781 // Note that this function is probably obsolete
782 bool FGStartupController::hasInstruction(int id)
784 TrafficVectorIterator i = activeTraffic.begin();
785 // Search search if the current id has an entry
786 // This might be faster using a map instead of a vector, but let's start by taking a safe route
787 if (activeTraffic.size())
789 //while ((i->getId() != id) && i != activeTraffic.end()) {
790 while (i != activeTraffic.end()) {
791 if (i->getId() == id) {
797 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
798 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: checking ATC instruction for aircraft without traffic record");
800 return i->hasInstruction();
806 FGATCInstruction FGStartupController::getInstruction(int id)
808 TrafficVectorIterator i = activeTraffic.begin();
809 // Search search if the current id has an entry
810 // This might be faster using a map instead of a vector, but let's start by taking a safe route
811 if (activeTraffic.size()) {
812 //while ((i->getId() != id) && i != activeTraffic.end()) {
813 while (i != activeTraffic.end()) {
814 if (i->getId() == id) {
820 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
821 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: requesting ATC instruction for aircraft without traffic record");
823 return i->getInstruction();
825 return FGATCInstruction();
828 void FGStartupController::signOff(int id)
830 TrafficVectorIterator i = activeTraffic.begin();
831 // Search search if the current id alread has an entry
832 // This might be faster using a map instead of a vector, but let's start by taking a safe route
833 if (activeTraffic.size()) {
834 //while ((i->getId() != id) && i != activeTraffic.end()) {
835 while (i != activeTraffic.end()) {
836 if (i->getId() == id) {
842 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
843 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: Aircraft without traffic record is signing off from tower");
845 i = activeTraffic.erase(i);
849 void FGStartupController::update(int id, double lat, double lon, double heading, double speed, double alt,
852 TrafficVectorIterator i = activeTraffic.begin();
853 // Search search if the current id has an entry
854 // This might be faster using a map instead of a vector, but let's start by taking a safe route
855 TrafficVectorIterator current, closest;
856 if (activeTraffic.size()) {
857 //while ((i->getId() != id) && i != activeTraffic.end()) {
858 while (i != activeTraffic.end()) {
859 if (i->getId() == id) {
866 // // update position of the current aircraft
867 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
868 SG_LOG(SG_GENERAL, SG_ALERT, "AI error: updating aircraft without traffic record");
870 i->setPositionAndHeading(lat, lon, heading, speed, alt);
875 int state = i->getState();
876 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
877 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
878 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
879 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
880 // << " at parking " << getGateName(i->getAircraft()) << endl;
882 if ((now - lastTransmission) > 3 + (rand() % 15)) {
886 if ((state == 0) && available) {
887 if (now > startTime) {
888 //cerr << "Transmitting startup msg" << endl;
889 transmit(&(*i), MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
891 lastTransmission = now;
895 if ((state == 1) && available) {
896 if (now > startTime+60) {
897 transmit(&(*i), MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
899 lastTransmission = now;
903 if ((state == 2) && available) {
904 if (now > startTime+80) {
905 transmit(&(*i), MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
907 lastTransmission = now;
911 if ((state == 3) && available){
912 if (now > startTime+100) {
913 transmit(&(*i), MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
915 lastTransmission = now;
919 // Note: The next four stages are only necessesary when Startup control is
920 // on a different frequency, compared to ground control
921 if ((state == 4) && available){
922 if (now > startTime+130) {
923 transmit(&(*i), MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND);
926 lastTransmission = now;
930 if ((state == 5) && available){
931 if (now > startTime+140) {
932 transmit(&(*i), MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
934 lastTransmission = now;
938 if ((state == 6) && available){
939 if (now > startTime+150) {
940 transmit(&(*i), MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR);
942 lastTransmission = now;
948 // TODO: Switch to APRON control and request pushback Clearance.
949 // Get Push back clearance
950 if ((state == 7) && available){
951 if (now > startTime+180) {
952 transmit(&(*i), MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
954 lastTransmission = now;
958 if ((state == 8) && available){
959 if (now > startTime+200) {
960 if (i->pushBackAllowed()) {
961 i->allowRepeatedTransmissions();
962 transmit(&(*i), MSG_PERMIT_PUSHBACK_CLEARANCE, ATC_GROUND_TO_AIR);
965 transmit(&(*i), MSG_HOLD_PUSHBACK_CLEARANCE, ATC_GROUND_TO_AIR);
966 i->suppressRepeatedTransmissions();
968 lastTransmission = now;
972 if ((state == 9) && available){
973 i->setHoldPosition(false);