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.
30 #include <osg/Geometry>
31 #include <osg/MatrixTransform>
34 #include <simgear/scene/material/EffectGeode.hxx>
35 #include <simgear/scene/material/matlib.hxx>
36 #include <simgear/scene/material/mat.hxx>
37 #include <Scenery/scenery.hxx>
39 #include "trafficcontrol.hxx"
40 #include "atc_mgr.hxx"
41 #include <AIModel/AIAircraft.hxx>
42 #include <AIModel/AIFlightPlan.hxx>
43 #include <AIModel/performancedata.hxx>
44 #include <AIModel/performancedb.hxx>
45 #include <ATC/atc_mgr.hxx>
46 #include <Traffic/TrafficMgr.hxx>
47 #include <Airports/groundnetwork.hxx>
48 #include <Airports/dynamics.hxx>
49 #include <Airports/simple.hxx>
53 /***************************************************************************
55 **************************************************************************/
56 time_t ActiveRunway::requestTimeSlot(time_t eta)
59 time_t separation = 90;
61 if (estimatedArrivalTimes.size() == 0) {
62 estimatedArrivalTimes.push_back(eta);
65 TimeVectorIterator i = estimatedArrivalTimes.begin();
66 //cerr << "Checking eta slots " << eta << ": " << endl;
67 for (i = estimatedArrivalTimes.begin();
68 i != estimatedArrivalTimes.end(); i++) {
69 //cerr << "Stored time : " << (*i) << endl;
71 i = estimatedArrivalTimes.begin();
72 if ((eta + separation) < (*i)) {
75 //cerr << "Storing at beginning" << endl;
77 while ((i != estimatedArrivalTimes.end()) && (!found)) {
78 TimeVectorIterator j = i + 1;
79 if (j == estimatedArrivalTimes.end()) {
80 if (((*i) + separation) < eta) {
81 //cerr << "Storing at end" << endl;
84 newEta = (*i) + separation;
85 //cerr << "Storing at end + separation" << endl;
88 if ((((*j) - (*i)) > (separation * 2))) { // found a potential slot
89 // now check whether this slot is usable:
90 // 1) eta should fall between the two points
91 // i.e. eta > i AND eta < j
93 //cerr << "Found potential slot after " << (*i) << endl;
94 if (eta > (*i) && (eta < (*j))) {
96 if (eta < ((*i) + separation)) {
97 newEta = (*i) + separation;
98 //cerr << "Using original" << (*i) << " + separation " << endl;
101 //cerr << "Using original after " << (*i) << endl;
103 } else if (eta < (*i)) {
105 newEta = (*i) + separation;
106 //cerr << "Using delayed slot after " << (*i) << endl;
109 if (((*j) - separation) < eta) {
111 if (((*i) + separation) < eta) {
113 cerr << "Using original after " << (*i) << endl;
115 newEta = (*i) + separation;
116 cerr << "Using " << (*i) << " + separation " << endl;
124 //cerr << ". done. New ETA : " << newEta << endl;
126 estimatedArrivalTimes.push_back(newEta);
127 sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
128 // do some housekeeping : remove any timestamps that are past
129 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
130 TimeVectorIterator i = estimatedArrivalTimes.begin();
131 while (i != estimatedArrivalTimes.end()) {
133 //cerr << "Deleting timestamp " << (*i) << " (now = " << now << "). " << endl;
134 estimatedArrivalTimes.erase(i);
135 i = estimatedArrivalTimes.begin();
143 void ActiveRunway::printDepartureCue()
145 cout << "Departure cue for " << rwy << ": " << endl;
146 for (AircraftVecIterator atc = departureCue.begin(); atc != departureCue.end(); atc++) {
147 cout << " " << (*atc)->getCallSign() << " " << (*atc)->getTakeOffStatus();
148 cout << " " << (*atc)->_getLatitude() << " " << (*atc)->_getLongitude() << (*atc)-> getSpeed() << " " << (*atc)->getAltitude() << endl;
153 FGAIAircraft* ActiveRunway::getFirstOfStatus(int stat)
155 for (AircraftVecIterator atc =departureCue.begin(); atc != departureCue.end(); atc++) {
156 if ((*atc)->getTakeOffStatus() == stat)
164 /***************************************************************************
166 **************************************************************************/
167 FGTrafficRecord::FGTrafficRecord():
168 id(0), waitsForId(0),
173 allowTransmission(true),
176 latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0)
180 void FGTrafficRecord::setPositionAndIntentions(int pos,
181 FGAIFlightPlan * route)
185 if (intentions.size()) {
186 intVecIterator i = intentions.begin();
188 SG_LOG(SG_GENERAL, SG_ALERT,
189 "Error in FGTrafficRecord::setPositionAndIntentions at " << SG_ORIGIN);
190 cerr << "Pos : " << pos << " Curr " << *(intentions.begin()) << endl;
191 for (intVecIterator i = intentions.begin();
192 i != intentions.end(); i++) {
199 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
200 int size = route->getNrOfWayPoints();
201 //cerr << "Setting pos" << pos << " ";
202 //cerr << "setting intentions ";
203 for (int i = 0; i < size; i++) {
204 int val = route->getRouteIndex(i);
206 if ((val) && (val != pos)) {
207 intentions.push_back(val);
212 //while (route->next(&legNr, &routeNr)) {
213 //intentions.push_back(routeNr);
215 //route->rewind(currentPos);
220 * Check if another aircraft is ahead of the current one, and on the same
221 * return true / false is the is/isn't the case.
223 ****************************************************************************/
225 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
228 //cerr << "Start check 1" << endl;
229 if (currentPos == other.currentPos) {
230 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
233 // else if (other.intentions.size())
235 // cerr << "Start check 2" << endl;
236 // intVecIterator i = other.intentions.begin();
237 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
239 // if (i != other.intentions.end()) {
240 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
243 else if (intentions.size()) {
244 //cerr << "Start check 3" << endl;
245 intVecIterator i = intentions.begin();
246 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
247 while (i != intentions.end()) {
248 if ((*i) == other.currentPos) {
253 if (i != intentions.end()) {
254 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
258 //cerr << "Done !!" << endl;
262 void FGTrafficRecord::setPositionAndHeading(double lat, double lon,
263 double hdg, double spd,
273 int FGTrafficRecord::crosses(FGGroundNetwork * net,
274 FGTrafficRecord & other)
276 if (checkPositionAndIntentions(other)
277 || (other.checkPositionAndIntentions(*this)))
280 int currentTargetNode = 0, otherTargetNode = 0;
282 currentTargetNode = net->findSegment(currentPos)->getEnd()->getIndex(); // OKAY,...
283 if (other.currentPos > 0)
284 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
285 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
286 return currentTargetNode;
287 if (intentions.size()) {
288 for (i = intentions.begin(); i != intentions.end(); i++) {
290 if ((currentTargetNode ==
291 net->findSegment(*i)->getEnd()->getIndex())) {
292 //cerr << "Current crosses at " << currentTargetNode <<endl;
293 return currentTargetNode;
298 if (other.intentions.size()) {
299 for (i = other.intentions.begin(); i != other.intentions.end();
302 if (otherTargetNode ==
303 net->findSegment(*i)->getEnd()->getIndex()) {
304 //cerr << "Other crosses at " << currentTargetNode <<endl;
305 return otherTargetNode;
310 if (intentions.size() && other.intentions.size()) {
311 for (i = intentions.begin(); i != intentions.end(); i++) {
312 for (j = other.intentions.begin(); j != other.intentions.end();
314 //cerr << "finding segment " << *i << " and " << *j << endl;
315 if (((*i) > 0) && ((*j) > 0)) {
317 net->findSegment(*i)->getEnd()->getIndex();
319 net->findSegment(*j)->getEnd()->getIndex();
320 if (currentTargetNode == otherTargetNode) {
321 //cerr << "Routes will cross at " << currentTargetNode << endl;
322 return currentTargetNode;
331 bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
332 FGTrafficRecord & other)
334 int node = -1, othernode = -1;
336 node = net->findSegment(currentPos)->getEnd()->getIndex();
337 if (other.currentPos > 0)
339 net->findSegment(other.currentPos)->getEnd()->getIndex();
340 if ((node == othernode) && (node != -1))
342 if (other.intentions.size()) {
343 for (intVecIterator i = other.intentions.begin();
344 i != other.intentions.end(); i++) {
346 othernode = net->findSegment(*i)->getEnd()->getIndex();
347 if ((node == othernode) && (node > -1))
352 //if (other.currentPos > 0)
353 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
354 //if (intentions.size())
356 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
360 // node = net->findSegment(*i)->getEnd()->getIndex();
361 // if ((node == othernode) && (node > -1))
370 bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
371 FGTrafficRecord & other, int node)
373 // Check if current segment is the reverse segment for the other aircraft
375 //cerr << "Current segment " << currentPos << endl;
376 if ((currentPos > 0) && (other.currentPos > 0)) {
377 opp = net->findSegment(currentPos)->opposite();
379 if (opp->getIndex() == other.currentPos)
383 for (intVecIterator i = intentions.begin(); i != intentions.end();
385 if ((opp = net->findSegment(other.currentPos)->opposite())) {
387 if (opp->getIndex() ==
388 net->findSegment(*i)->getIndex()) {
389 if (net->findSegment(*i)->getStart()->getIndex() ==
392 //cerr << "Found the node " << node << endl;
398 if (other.intentions.size()) {
399 for (intVecIterator j = other.intentions.begin();
400 j != other.intentions.end(); j++) {
401 // cerr << "Current segment 1 " << (*i) << endl;
403 if ((opp = net->findSegment(*i)->opposite())) {
404 if (opp->getIndex() ==
405 net->findSegment(*j)->getIndex()) {
406 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
407 // << " and " << net->findSegment(*j)->getIndex()
408 // << " are opposites " << endl;
409 if (net->findSegment(*i)->getStart()->
410 getIndex() == node) {
412 //cerr << "Found the node " << node << endl;
426 bool FGTrafficRecord::isActive(int margin)
428 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
429 time_t deptime = aircraft->getTrafficRef()->getDepartureTime();
430 return ((now + margin) > deptime);
434 void FGTrafficRecord::setSpeedAdjustment(double spd)
436 instruction.setChangeSpeed(true);
437 instruction.setSpeed(spd);
440 void FGTrafficRecord::setHeadingAdjustment(double heading)
442 instruction.setChangeHeading(true);
443 instruction.setHeading(heading);
446 bool FGTrafficRecord::pushBackAllowed()
448 return allowPushback;
454 /***************************************************************************
457 **************************************************************************/
458 FGATCInstruction::FGATCInstruction()
461 holdPosition = false;
463 changeHeading = false;
464 changeAltitude = false;
465 resolveCircularWait = false;
473 bool FGATCInstruction::hasInstruction()
475 return (holdPattern || holdPosition || changeSpeed || changeHeading
476 || changeAltitude || resolveCircularWait);
479 /***************************************************************************
482 **************************************************************************/
487 FGATCController::FGATCController()
489 //cerr << "running FGATController constructor" << endl;
492 lastTransmission = 0;
496 FGATCController::~FGATCController()
498 //cerr << "running FGATController destructor" << endl;
501 string FGATCController::getGateName(FGAIAircraft * ref)
503 return ref->atGate();
506 bool FGATCController::isUserAircraft(FGAIAircraft* ac)
508 return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false;
511 void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
512 AtcMsgDir msgDir, bool audible)
514 string sender, receiver;
519 string atisInformation;
528 string transponderCode;
531 string instructionText;
534 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
535 if (rec->getAircraft()->getTaxiClearanceRequest()) {
536 instructionText = "push-back and taxi";
538 instructionText = "taxi";
540 //cerr << "transmitting for: " << sender << "Leg = " << rec->getLeg() << endl;
541 switch (rec->getLeg()) {
544 freqId = rec->getNextFrequency();
546 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
547 getDynamics()->getGroundFrequency(rec->getLeg() + freqId);
549 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
550 getDynamics()->getGroundFrequency(2);
552 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
553 getDynamics()->getTowerFrequency(2);
555 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
556 getName() + "-Ground";
558 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
559 getDynamics()->getAtisSequence();
563 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
564 getName() + "-Tower";
567 // Swap sender and receiver value in case of a ground to air transmission
568 if (msgDir == ATC_GROUND_TO_AIR) {
574 case MSG_ANNOUNCE_ENGINE_START:
575 text = sender + ". Ready to Start up";
577 case MSG_REQUEST_ENGINE_START:
579 receiver + ", This is " + sender + ". Position " +
580 getGateName(rec->getAircraft()) + ". Information " +
581 atisInformation + ". " +
582 rec->getAircraft()->getTrafficRef()->getFlightRules() +
584 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->
585 getName() + ". Request start-up";
587 // Acknowledge engine startup permission
588 // Assign departure runway
589 // Assign SID, if necessery (TODO)
590 case MSG_PERMIT_ENGINE_START:
591 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
593 heading = rec->getAircraft()->getTrafficRef()->getCourse();
594 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
596 rec->getAircraft()->GetFlightPlan()->
597 getRunwayClassFromTrafficType(fltType);
599 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
600 getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
602 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
603 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
604 getDynamics()->getSID(activeRunway, heading);
605 rec->getAircraft()->GetFlightPlan()->setSID(fp);
607 SID = fp->getName() + " departure";
609 SID = "fly runway heading ";
611 //snprintf(buffer, 7, "%3.2f", heading);
612 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
613 transponderCode = genTransponderCode(fltRules);
614 rec->getAircraft()->SetTransponderCode(transponderCode);
616 receiver + ". Start-up approved. " + atisInformation +
617 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
618 transponderCode + ". " +
619 "For "+ instructionText + " clearance call " + taxiFreqStr + ". " +
620 sender + " control.";
622 case MSG_DENY_ENGINE_START:
623 text = receiver + ". Standby";
625 case MSG_ACKNOWLEDGE_ENGINE_START:
626 fp = rec->getAircraft()->GetFlightPlan()->getSID();
629 rec->getAircraft()->GetFlightPlan()->getSID()->getName() +
632 SID = "fly runway heading ";
634 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
635 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
636 transponderCode = rec->getAircraft()->GetTransponderCode();
639 receiver + ". Start-up approved. " + atisInformation +
640 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
641 transponderCode + ". " +
642 "For " + instructionText + " clearance call " + taxiFreqStr + ". " +
645 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
646 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
647 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
649 case MSG_INITIATE_CONTACT:
650 text = receiver + ". With you. " + sender;
652 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
653 text = receiver + ". Roger. " + sender;
655 case MSG_REQUEST_PUSHBACK_CLEARANCE:
656 if (rec->getAircraft()->getTaxiClearanceRequest()) {
657 text = receiver + ". Request push-back. " + sender;
659 text = receiver + ". Request Taxi clearance. " + sender;
662 case MSG_PERMIT_PUSHBACK_CLEARANCE:
663 if (rec->getAircraft()->getTaxiClearanceRequest()) {
664 text = receiver + ". Push-back approved. " + sender;
666 text = receiver + ". Cleared to Taxi." + sender;
669 case MSG_HOLD_PUSHBACK_CLEARANCE:
670 text = receiver + ". Standby. " + sender;
672 case MSG_REQUEST_TAXI_CLEARANCE:
673 text = receiver + ". Ready to Taxi. " + sender;
675 case MSG_ISSUE_TAXI_CLEARANCE:
676 text = receiver + ". Cleared to taxi. " + sender;
678 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
679 text = receiver + ". Cleared to taxi. " + sender;
681 case MSG_HOLD_POSITION:
682 text = receiver + ". Hold Position. " + sender;
684 case MSG_ACKNOWLEDGE_HOLD_POSITION:
685 text = receiver + ". Holding Position. " + sender;
687 case MSG_RESUME_TAXI:
688 text = receiver + ". Resume Taxiing. " + sender;
690 case MSG_ACKNOWLEDGE_RESUME_TAXI:
691 text = receiver + ". Continuing Taxi. " + sender;
693 case MSG_REPORT_RUNWAY_HOLD_SHORT:
694 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
695 //activeRunway = "test";
696 text = receiver + ". Holding short runway "
700 //cerr << "1 Currently at leg " << rec->getLeg() << endl;
702 case MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT:
703 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
704 text = receiver + "Roger. Holding short runway "
708 //cerr << "2 Currently at leg " << rec->getLeg() << endl;
710 case MSG_SWITCH_TOWER_FREQUENCY:
711 towerFreqStr = formatATCFrequency3_2(towerFreq);
712 text = receiver + "Contact Tower at " + towerFreqStr + ". " + sender;
714 //cerr << "3 Currently at leg " << rec->getLeg() << endl;
716 case MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY:
717 towerFreqStr = formatATCFrequency3_2(towerFreq);
718 text = receiver + "Roger, switching to tower at " + towerFreqStr + ". " + sender;
720 //cerr << "4 Currently at leg " << rec->getLeg() << endl;
724 text = text + sender + ". Transmitting unknown Message";
728 double onBoardRadioFreq0 =
729 fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
730 double onBoardRadioFreq1 =
731 fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
732 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
733 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
734 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << endl;
736 // Display ATC message only when one of the radios is tuned
737 // the relevant frequency.
738 // Note that distance attenuation is currently not yet implemented
739 if ((onBoardRadioFreqI0 == stationFreq)
740 || (onBoardRadioFreqI1 == stationFreq)) {
741 if (rec->allowTransmissions()) {
742 fgSetString("/sim/messages/atc", text.c_str());
746 FGATCDialogNew::instance()->addEntry(1, text);
750 string FGATCController::formatATCFrequency3_2(int freq)
753 snprintf(buffer, 7, "%3.2f", ((float) freq / 100.0));
754 return string(buffer);
757 // TODO: Set transponder codes according to real-world routes.
758 // The current version just returns a random string of four octal numbers.
759 string FGATCController::genTransponderCode(string fltRules)
761 if (fltRules == "VFR") {
762 return string("1200");
765 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8,
767 return string(buffer);
771 void FGATCController::init()
774 FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
775 mgr->addController(this);
780 /***************************************************************************
781 * class FGTowerController
783 **************************************************************************/
784 FGTowerController::FGTowerController(FGAirportDynamics *par) :
791 void FGTowerController::announcePosition(int id,
792 FGAIFlightPlan * intendedRoute,
793 int currentPosition, double lat,
794 double lon, double heading,
795 double speed, double alt,
796 double radius, int leg,
800 TrafficVectorIterator i = activeTraffic.begin();
801 // Search whether the current id alread has an entry
802 // This might be faster using a map instead of a vector, but let's start by taking a safe route
803 if (activeTraffic.size()) {
804 //while ((i->getId() != id) && i != activeTraffic.end()) {
805 while (i != activeTraffic.end()) {
806 if (i->getId() == id) {
812 // Add a new TrafficRecord if no one exsists for this aircraft.
813 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
817 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
818 rec.setRunway(intendedRoute->getRunway());
820 //rec.setCallSign(callsign);
821 rec.setRadius(radius);
822 rec.setAircraft(ref);
823 activeTraffic.push_back(rec);
824 // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
825 ActiveRunwayVecIterator rwy = activeRunways.begin();
826 if (activeRunways.size()) {
827 while (rwy != activeRunways.end()) {
828 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
834 if (rwy == activeRunways.end()) {
835 ActiveRunway aRwy(intendedRoute->getRunway(), id);
836 aRwy.addToDepartureCue(ref);
837 activeRunways.push_back(aRwy);
838 rwy = (activeRunways.end()-1);
840 rwy->addToDepartureCue(ref);
843 //cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() << " for takeoff " << endl;
845 i->setPositionAndHeading(lat, lon, heading, speed, alt);
849 void FGTowerController::updateAircraftInformation(int id, double lat, double lon,
850 double heading, double speed, double alt,
853 TrafficVectorIterator i = activeTraffic.begin();
854 // Search whether the current id has an entry
855 // This might be faster using a map instead of a vector, but let's start by taking a safe route
856 TrafficVectorIterator current, closest;
857 if (activeTraffic.size()) {
858 //while ((i->getId() != id) && i != activeTraffic.end()) {
859 while (i != activeTraffic.end()) {
860 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,
869 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
871 i->setPositionAndHeading(lat, lon, heading, speed, alt);
876 // see if we already have a clearance record for the currently active runway
877 // NOTE: dd. 2011-08-07: Because the active runway has been constructed in the announcePosition function, we may safely assume that is
878 // already exists here. So, we can simplify the current code.
880 ActiveRunwayVecIterator rwy = activeRunways.begin();
881 //if (parent->getId() == fgGetString("/sim/presets/airport-id")) {
882 // for (rwy = activeRunways.begin(); rwy != activeRunways.end(); rwy++) {
883 // rwy->printDepartureCue();
887 rwy = activeRunways.begin();
888 while (rwy != activeRunways.end()) {
889 if (rwy->getRunwayName() == current->getRunway()) {
895 // only bother running the following code if the current aircraft is the
896 // first in line for depature
897 /* if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
898 if (rwy->getCleared()) {
899 if (id == rwy->getCleared()) {
900 current->setHoldPosition(false);
902 current->setHoldPosition(true);
905 // For now. At later stages, this will probably be the place to check for inbound traffc.
909 // only bother with aircraft that have a takeoff status of 2, since those are essentially under tower control
910 FGAIAircraft* ac= rwy->getFirstAircraftInDepartureCue();
911 if (ac->getTakeOffStatus() == 1) {
912 ac->setTakeOffStatus(2);
914 if (current->getAircraft()->getTakeOffStatus() == 2) {
915 current -> setHoldPosition(false);
917 current->setHoldPosition(true);
919 int clearanceId = rwy->getCleared();
921 if (id == clearanceId) {
922 current->setHoldPosition(false);
925 if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
927 FGAIAircraft *ac = rwy->getFirstOfStatus(1);
929 ac->setTakeOffStatus(2);
935 void FGTowerController::signOff(int id)
937 TrafficVectorIterator i = activeTraffic.begin();
938 // Search search if the current id alread has an entry
939 // This might be faster using a map instead of a vector, but let's start by taking a safe route
940 if (activeTraffic.size()) {
941 //while ((i->getId() != id) && i != activeTraffic.end()) {
942 while (i != activeTraffic.end()) {
943 if (i->getId() == id) {
949 // If this aircraft has left the runway, we can clear the departure record for this runway
950 ActiveRunwayVecIterator rwy = activeRunways.begin();
951 if (activeRunways.size()) {
952 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
953 while (rwy != activeRunways.end()) {
954 if (rwy->getRunwayName() == i->getRunway()) {
959 if (rwy != activeRunways.end()) {
961 rwy->updateDepartureCue();
963 SG_LOG(SG_GENERAL, SG_ALERT,
964 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff at " << SG_ORIGIN);
967 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
968 SG_LOG(SG_GENERAL, SG_ALERT,
969 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
971 i->getAircraft()->resetTakeOffStatus();
972 i = activeTraffic.erase(i);
973 //cerr << "Signing off from tower controller" << endl;
978 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
979 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
980 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
981 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
982 // Note that this function is probably obsolete
983 bool FGTowerController::hasInstruction(int id)
985 TrafficVectorIterator i = activeTraffic.begin();
986 // Search search if the current id has an entry
987 // This might be faster using a map instead of a vector, but let's start by taking a safe route
988 if (activeTraffic.size()) {
989 //while ((i->getId() != id) && i != activeTraffic.end()) {
990 while (i != activeTraffic.end()) {
991 if (i->getId() == id) {
997 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
998 SG_LOG(SG_GENERAL, SG_ALERT,
999 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1001 return i->hasInstruction();
1007 FGATCInstruction FGTowerController::getInstruction(int id)
1009 TrafficVectorIterator i = activeTraffic.begin();
1010 // Search search if the current id has an entry
1011 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1012 if (activeTraffic.size()) {
1013 //while ((i->getId() != id) && i != activeTraffic.end()) {
1014 while (i != activeTraffic.end()) {
1015 if (i->getId() == id) {
1021 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1022 SG_LOG(SG_GENERAL, SG_ALERT,
1023 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1025 return i->getInstruction();
1027 return FGATCInstruction();
1030 void FGTowerController::render(bool visible) {
1031 //cerr << "FGTowerController::render function not yet implemented" << endl;
1034 string FGTowerController::getName() {
1035 return string(parent->getId() + "-tower");
1038 void FGTowerController::update(double dt)
1045 /***************************************************************************
1046 * class FGStartupController
1048 **************************************************************************/
1049 FGStartupController::FGStartupController(FGAirportDynamics *par):
1055 void FGStartupController::announcePosition(int id,
1056 FGAIFlightPlan * intendedRoute,
1057 int currentPosition, double lat,
1058 double lon, double heading,
1059 double speed, double alt,
1060 double radius, int leg,
1064 TrafficVectorIterator i = activeTraffic.begin();
1065 // Search whether the current id alread has an entry
1066 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1067 if (activeTraffic.size()) {
1068 //while ((i->getId() != id) && i != activeTraffic.end()) {
1069 while (i != activeTraffic.end()) {
1070 if (i->getId() == id) {
1076 // Add a new TrafficRecord if no one exsists for this aircraft.
1077 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1078 FGTrafficRecord rec;
1081 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1082 rec.setRunway(intendedRoute->getRunway());
1084 rec.setPositionAndIntentions(currentPosition, intendedRoute);
1085 //rec.setCallSign(callsign);
1086 rec.setAircraft(ref);
1087 rec.setHoldPosition(true);
1088 activeTraffic.push_back(rec);
1090 i->setPositionAndIntentions(currentPosition, intendedRoute);
1091 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1097 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
1098 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1099 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1100 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1101 // Note that this function is probably obsolete
1102 bool FGStartupController::hasInstruction(int id)
1104 TrafficVectorIterator i = activeTraffic.begin();
1105 // Search search if the current id has an entry
1106 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1107 if (activeTraffic.size()) {
1108 //while ((i->getId() != id) && i != activeTraffic.end()) {
1109 while (i != activeTraffic.end()) {
1110 if (i->getId() == id) {
1116 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1117 SG_LOG(SG_GENERAL, SG_ALERT,
1118 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1120 return i->hasInstruction();
1126 FGATCInstruction FGStartupController::getInstruction(int id)
1128 TrafficVectorIterator i = activeTraffic.begin();
1129 // Search search if the current id has an entry
1130 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1131 if (activeTraffic.size()) {
1132 //while ((i->getId() != id) && i != activeTraffic.end()) {
1133 while (i != activeTraffic.end()) {
1134 if (i->getId() == id) {
1140 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1141 SG_LOG(SG_GENERAL, SG_ALERT,
1142 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1144 return i->getInstruction();
1146 return FGATCInstruction();
1149 void FGStartupController::signOff(int id)
1151 TrafficVectorIterator i = activeTraffic.begin();
1152 // Search search if the current id alread has an entry
1153 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1154 if (activeTraffic.size()) {
1155 //while ((i->getId() != id) && i != activeTraffic.end()) {
1156 while (i != activeTraffic.end()) {
1157 if (i->getId() == id) {
1163 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1164 SG_LOG(SG_GENERAL, SG_ALERT,
1165 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
1167 //cerr << i->getAircraft()->getCallSign() << " signing off from startupcontroller" << endl;
1168 i = activeTraffic.erase(i);
1172 bool FGStartupController::checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
1175 int state = i->getState();
1176 if ((state == st) && available) {
1177 if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
1179 //cerr << "Checking state " << st << " for " << i->getAircraft()->getCallSign() << endl;
1180 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
1181 int n = trans_num->getIntValue();
1183 trans_num->setIntValue(-1);
1184 // PopupCallback(n);
1185 //cerr << "Selected transmission message " << n << endl;
1186 FGATCDialogNew::instance()->removeEntry(1);
1188 //cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
1189 transmit(&(*i), msgId, msgDir, false);
1193 if (now > startTime) {
1194 //cerr << "Transmitting startup msg" << endl;
1195 transmit(&(*i), msgId, msgDir, true);
1197 lastTransmission = now;
1205 void FGStartupController::updateAircraftInformation(int id, double lat, double lon,
1206 double heading, double speed, double alt,
1209 TrafficVectorIterator i = activeTraffic.begin();
1210 // Search search if the current id has an entry
1211 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1212 TrafficVectorIterator current, closest;
1213 if (activeTraffic.size()) {
1214 //while ((i->getId() != id) && i != activeTraffic.end()) {
1215 while (i != activeTraffic.end()) {
1216 if (i->getId() == id) {
1222 // // update position of the current aircraft
1224 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1225 SG_LOG(SG_GENERAL, SG_ALERT,
1226 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1228 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1231 setDt(getDt() + dt);
1233 int state = i->getState();
1235 // The user controlled aircraft should have crased here, because it doesn't have a traffic reference.
1236 // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight.
1237 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
1238 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1239 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
1240 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
1241 // << " at parking " << getGateName(i->getAircraft()) << endl;
1243 if ((now - lastTransmission) > 3 + (rand() % 15)) {
1247 checkTransmissionState(0, now, (startTime + 0 ), i, MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
1248 checkTransmissionState(1, now, (startTime + 60 ), i, MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
1249 checkTransmissionState(2, now, (startTime + 80 ), i, MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
1250 checkTransmissionState(3, now, (startTime + 100), i, MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
1251 if (checkTransmissionState(4, now, (startTime + 130), i, MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND)) {
1254 checkTransmissionState(5, now, (startTime + 140), i, MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
1255 checkTransmissionState(6, now, (startTime + 150), i, MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR);
1256 checkTransmissionState(7, now, (startTime + 180), i, MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
1260 if ((state == 8) && available) {
1261 if (now > startTime + 200) {
1262 if (i->pushBackAllowed()) {
1263 i->allowRepeatedTransmissions();
1264 transmit(&(*i), MSG_PERMIT_PUSHBACK_CLEARANCE,
1265 ATC_GROUND_TO_AIR, true);
1268 transmit(&(*i), MSG_HOLD_PUSHBACK_CLEARANCE,
1269 ATC_GROUND_TO_AIR, true);
1270 i->suppressRepeatedTransmissions();
1272 lastTransmission = now;
1276 if ((state == 9) && available) {
1277 i->setHoldPosition(false);
1281 // Note that this function is copied from simgear. for maintanance purposes, it's probabtl better to make a general function out of that.
1282 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
1283 double lon, double elev, double hdg, double slope)
1285 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
1286 obj_pos = geod.makeZUpFrame();
1287 // hdg is not a compass heading, but a counter-clockwise rotation
1288 // around the Z axis
1289 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
1291 obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS,
1296 void FGStartupController::render(bool visible)
1299 SGMaterialLib *matlib = globals->get_matlib();
1302 globals->get_scenery()->get_scene_graph()->removeChild(group);
1303 //while (group->getNumChildren()) {
1304 // cerr << "Number of children: " << group->getNumChildren() << endl;
1305 //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
1306 //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0);
1307 //geode->releaseGLObjects();
1308 //group->removeChild(geode);
1313 group = new osg::Group;
1314 FGScenery * local_scenery = globals->get_scenery();
1315 double elevation_meters = 0.0;
1316 double elevation_feet = 0.0;
1319 //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
1321 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1322 for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) {
1323 if (i->isActive(300)) {
1324 // Handle start point
1325 int pos = i->getCurrentPosition();
1326 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "pos = " << pos << endl;
1328 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(pos);
1329 SGGeod start(SGGeod::fromDeg((i->getLongitude()), (i->getLatitude())));
1330 SGGeod end (SGGeod::fromDeg(segment->getEnd()->getLongitude(), segment->getEnd()->getLatitude()));
1332 double length = SGGeodesy::distanceM(start, end);
1333 //heading = SGGeodesy::headingDeg(start->getGeod(), end->getGeod());
1335 double az2, heading; //, distanceM;
1336 SGGeodesy::inverse(start, end, heading, az2, length);
1337 double coveredDistance = length * 0.5;
1339 SGGeodesy::direct(start, heading, coveredDistance, center, az2);
1340 //cerr << "Active Aircraft : Centerpoint = (" << center.getLatitudeDeg() << ", " << center.getLongitudeDeg() << "). Heading = " << heading << endl;
1341 ///////////////////////////////////////////////////////////////////////////////
1342 // Make a helper function out of this
1343 osg::Matrix obj_pos;
1344 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1345 obj_trans->setDataVariance(osg::Object::STATIC);
1346 // Experimental: Calculate slope here, based on length, and the individual elevations
1347 double elevationStart;
1348 if (isUserAircraft((i)->getAircraft())) {
1349 elevationStart = fgGetDouble("/position/ground-elev-m");
1351 elevationStart = ((i)->getAircraft()->_getAltitude() * SG_FEET_TO_METER);
1353 double elevationEnd = segment->getEnd()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1354 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1355 SGGeod center2 = end;
1356 center2.setElevationM(SG_MAX_ELEVATION_M);
1357 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1358 elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1359 //elevation_meters += 0.5;
1362 elevationEnd = parent->getElevation();
1364 segment->getEnd()->setElevation(elevationEnd);
1367 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1368 double elevDiff = elevationEnd - elevationStart;
1370 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1372 //cerr << "1. Using mean elevation : " << elevationMean << " and " << slope << endl;
1374 WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5 + dx, -(heading), slope );
1377 obj_trans->setMatrix( obj_pos );
1378 //osg::Vec3 center(0, 0, 0)
1380 float width = length /2.0;
1381 osg::Vec3 corner(-width, 0, 0.25f);
1382 osg::Vec3 widthVec(2*width + 1, 0, 0);
1383 osg::Vec3 heightVec(0, 1, 0);
1384 osg::Geometry* geometry;
1385 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1386 simgear::EffectGeode* geode = new simgear::EffectGeode;
1387 geode->setName("test");
1388 geode->addDrawable(geometry);
1389 //osg::Node *custom_obj;
1391 if (segment->hasBlock(now)) {
1392 mat = matlib->find("UnidirectionalTaperRed");
1394 mat = matlib->find("UnidirectionalTaperGreen");
1397 geode->setEffect(mat->get_effect());
1398 obj_trans->addChild(geode);
1399 // wire as much of the scene graph together as we can
1400 //->addChild( obj_trans );
1401 group->addChild( obj_trans );
1402 /////////////////////////////////////////////////////////////////////
1404 //cerr << "BIG FAT WARNING: current position is here : " << pos << endl;
1406 for (intVecIterator j = (i)->getIntentions().begin(); j != (i)->getIntentions().end(); j++) {
1407 osg::Matrix obj_pos;
1410 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "intention = " << k << endl;
1411 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1412 obj_trans->setDataVariance(osg::Object::STATIC);
1413 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(k);
1415 double elevationStart = segment->getStart()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1416 double elevationEnd = segment->getEnd ()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1417 if ((elevationStart == 0) || (elevationStart == parent->getElevation())) {
1418 SGGeod center2 = segment->getStart()->getGeod();
1419 center2.setElevationM(SG_MAX_ELEVATION_M);
1420 if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) {
1421 elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5;
1422 //elevation_meters += 0.5;
1425 elevationStart = parent->getElevation();
1427 segment->getStart()->setElevation(elevationStart);
1429 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1430 SGGeod center2 = segment->getEnd()->getGeod();
1431 center2.setElevationM(SG_MAX_ELEVATION_M);
1432 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1433 elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1434 //elevation_meters += 0.5;
1437 elevationEnd = parent->getElevation();
1439 segment->getEnd()->setElevation(elevationEnd);
1442 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1443 double elevDiff = elevationEnd - elevationStart;
1444 double length = segment->getLength();
1445 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1447 //cerr << "2. Using mean elevation : " << elevationMean << " and " << slope << endl;
1450 WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), elevationMean + 0.5 + dx, -(segment->getHeading()), slope );
1452 //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
1454 obj_trans->setMatrix( obj_pos );
1455 //osg::Vec3 center(0, 0, 0)
1457 float width = segment->getLength() /2.0;
1458 osg::Vec3 corner(-width, 0, 0.25f);
1459 osg::Vec3 widthVec(2*width + 1, 0, 0);
1460 osg::Vec3 heightVec(0, 1, 0);
1461 osg::Geometry* geometry;
1462 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1463 simgear::EffectGeode* geode = new simgear::EffectGeode;
1464 geode->setName("test");
1465 geode->addDrawable(geometry);
1466 //osg::Node *custom_obj;
1468 if (segment->hasBlock(now)) {
1469 mat = matlib->find("UnidirectionalTaperRed");
1471 mat = matlib->find("UnidirectionalTaperGreen");
1474 geode->setEffect(mat->get_effect());
1475 obj_trans->addChild(geode);
1476 // wire as much of the scene graph together as we can
1477 //->addChild( obj_trans );
1478 group->addChild( obj_trans );
1480 //cerr << "BIG FAT WARNING: k is here : " << pos << endl;
1486 globals->get_scenery()->get_scene_graph()->addChild(group);
1490 string FGStartupController::getName() {
1491 return string(parent->getId() + "-startup");
1494 void FGStartupController::update(double dt)
1501 /***************************************************************************
1502 * class FGApproachController
1504 **************************************************************************/
1505 FGApproachController::FGApproachController(FGAirportDynamics *par):
1512 void FGApproachController::announcePosition(int id,
1513 FGAIFlightPlan * intendedRoute,
1514 int currentPosition,
1515 double lat, double lon,
1516 double heading, double speed,
1517 double alt, double radius,
1518 int leg, FGAIAircraft * ref)
1521 TrafficVectorIterator i = activeTraffic.begin();
1522 // Search whether the current id alread has an entry
1523 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1524 if (activeTraffic.size()) {
1525 //while ((i->getId() != id) && i != activeTraffic.end()) {
1526 while (i != activeTraffic.end()) {
1527 if (i->getId() == id) {
1533 // Add a new TrafficRecord if no one exsists for this aircraft.
1534 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1535 FGTrafficRecord rec;
1538 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1539 rec.setRunway(intendedRoute->getRunway());
1541 //rec.setCallSign(callsign);
1542 rec.setAircraft(ref);
1543 activeTraffic.push_back(rec);
1545 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1549 void FGApproachController::updateAircraftInformation(int id, double lat, double lon,
1550 double heading, double speed, double alt,
1553 TrafficVectorIterator i = activeTraffic.begin();
1554 // Search search if the current id has an entry
1555 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1556 TrafficVectorIterator current, closest;
1557 if (activeTraffic.size()) {
1558 //while ((i->getId() != id) && i != activeTraffic.end()) {
1559 while (i != activeTraffic.end()) {
1560 if (i->getId() == id) {
1566 // // update position of the current aircraft
1567 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1568 SG_LOG(SG_GENERAL, SG_ALERT,
1569 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1571 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1573 //cerr << "ApproachController: checking for speed" << endl;
1575 current->getAircraft()->
1576 checkForArrivalTime(string("final001"));
1577 if (time_diff > 15) {
1578 current->setSpeedAdjustment(current->getAircraft()->
1579 getPerformance()->vDescent() *
1581 } else if (time_diff > 5) {
1582 current->setSpeedAdjustment(current->getAircraft()->
1583 getPerformance()->vDescent() *
1585 } else if (time_diff < -15) {
1586 current->setSpeedAdjustment(current->getAircraft()->
1587 getPerformance()->vDescent() *
1589 } else if (time_diff < -5) {
1590 current->setSpeedAdjustment(current->getAircraft()->
1591 getPerformance()->vDescent() *
1594 current->clearSpeedAdjustment();
1596 //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff);
1598 setDt(getDt() + dt);
1601 void FGApproachController::signOff(int id)
1603 TrafficVectorIterator i = activeTraffic.begin();
1604 // Search search if the current id alread has an entry
1605 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1606 if (activeTraffic.size()) {
1607 //while ((i->getId() != id) && i != activeTraffic.end()) {
1608 while (i != activeTraffic.end()) {
1609 if (i->getId() == id) {
1615 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1616 SG_LOG(SG_GENERAL, SG_ALERT,
1617 "AI error: Aircraft without traffic record is signing off from approach at " << SG_ORIGIN);
1619 i = activeTraffic.erase(i);
1623 void FGApproachController::update(double dt)
1630 bool FGApproachController::hasInstruction(int id)
1632 TrafficVectorIterator i = activeTraffic.begin();
1633 // Search search if the current id has an entry
1634 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1635 if (activeTraffic.size()) {
1636 //while ((i->getId() != id) && i != activeTraffic.end()) {
1637 while (i != activeTraffic.end()) {
1638 if (i->getId() == id) {
1644 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1645 SG_LOG(SG_GENERAL, SG_ALERT,
1646 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1648 return i->hasInstruction();
1654 FGATCInstruction FGApproachController::getInstruction(int id)
1656 TrafficVectorIterator i = activeTraffic.begin();
1657 // Search search if the current id has an entry
1658 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1659 if (activeTraffic.size()) {
1660 //while ((i->getId() != id) && i != activeTraffic.end()) {
1661 while (i != activeTraffic.end()) {
1662 if (i->getId() == id) {
1668 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1669 SG_LOG(SG_GENERAL, SG_ALERT,
1670 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1672 return i->getInstruction();
1674 return FGATCInstruction();
1678 ActiveRunway *FGApproachController::getRunway(string name)
1680 ActiveRunwayVecIterator rwy = activeRunways.begin();
1681 if (activeRunways.size()) {
1682 while (rwy != activeRunways.end()) {
1683 if (rwy->getRunwayName() == name) {
1689 if (rwy == activeRunways.end()) {
1690 ActiveRunway aRwy(name, 0);
1691 activeRunways.push_back(aRwy);
1692 rwy = activeRunways.end() - 1;
1697 void FGApproachController::render(bool visible) {
1698 //cerr << "FGApproachController::render function not yet implemented" << endl;
1703 string FGApproachController::getName() {
1704 return string(parent->getId() + "-approach");