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.
31 #include <osg/Geometry>
32 #include <osg/MatrixTransform>
35 #include <simgear/scene/material/EffectGeode.hxx>
36 #include <simgear/scene/material/matlib.hxx>
37 #include <simgear/scene/material/mat.hxx>
38 #include <simgear/scene/util/OsgMath.hxx>
39 #include <Scenery/scenery.hxx>
41 #include "trafficcontrol.hxx"
42 #include "atc_mgr.hxx"
43 #include <AIModel/AIAircraft.hxx>
44 #include <AIModel/AIFlightPlan.hxx>
45 #include <AIModel/performancedata.hxx>
46 #include <AIModel/performancedb.hxx>
47 #include <ATC/atc_mgr.hxx>
48 #include <Traffic/TrafficMgr.hxx>
49 #include <Airports/groundnetwork.hxx>
50 #include <Airports/dynamics.hxx>
51 #include <Airports/airport.hxx>
52 #include <Radio/radio.hxx>
60 /***************************************************************************
62 **************************************************************************/
63 time_t ActiveRunway::requestTimeSlot(time_t eta)
66 time_t separation = 90;
68 if (estimatedArrivalTimes.size() == 0) {
69 estimatedArrivalTimes.push_back(eta);
72 TimeVectorIterator i = estimatedArrivalTimes.begin();
73 //cerr << "Checking eta slots " << eta << ": " << endl;
74 for (i = estimatedArrivalTimes.begin();
75 i != estimatedArrivalTimes.end(); i++) {
76 //cerr << "Stored time : " << (*i) << endl;
78 i = estimatedArrivalTimes.begin();
79 if ((eta + separation) < (*i)) {
82 //cerr << "Storing at beginning" << endl;
84 while ((i != estimatedArrivalTimes.end()) && (!found)) {
85 TimeVectorIterator j = i + 1;
86 if (j == estimatedArrivalTimes.end()) {
87 if (((*i) + separation) < eta) {
88 //cerr << "Storing at end" << endl;
91 newEta = (*i) + separation;
92 //cerr << "Storing at end + separation" << endl;
95 if ((((*j) - (*i)) > (separation * 2))) { // found a potential slot
96 // now check whether this slot is usable:
97 // 1) eta should fall between the two points
98 // i.e. eta > i AND eta < j
100 //cerr << "Found potential slot after " << (*i) << endl;
101 if (eta > (*i) && (eta < (*j))) {
103 if (eta < ((*i) + separation)) {
104 newEta = (*i) + separation;
105 //cerr << "Using original" << (*i) << " + separation " << endl;
108 //cerr << "Using original after " << (*i) << endl;
110 } else if (eta < (*i)) {
112 newEta = (*i) + separation;
113 //cerr << "Using delayed slot after " << (*i) << endl;
116 if (((*j) - separation) < eta) {
118 if (((*i) + separation) < eta) {
120 cerr << "Using original after " << (*i) << endl;
122 newEta = (*i) + separation;
123 cerr << "Using " << (*i) << " + separation " << endl;
131 //cerr << ". done. New ETA : " << newEta << endl;
133 estimatedArrivalTimes.push_back(newEta);
134 sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
135 // do some housekeeping : remove any timestamps that are past
136 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
137 TimeVectorIterator i = estimatedArrivalTimes.begin();
138 while (i != estimatedArrivalTimes.end()) {
140 //cerr << "Deleting timestamp " << (*i) << " (now = " << now << "). " << endl;
141 estimatedArrivalTimes.erase(i);
142 i = estimatedArrivalTimes.begin();
150 void ActiveRunway::printDepartureCue()
152 cout << "Departure cue for " << rwy << ": " << endl;
153 for (AircraftVecIterator atc = departureCue.begin(); atc != departureCue.end(); atc++) {
154 cout << " " << (*atc)->getCallSign() << " " << (*atc)->getTakeOffStatus();
155 cout << " " << (*atc)->_getLatitude() << " " << (*atc)->_getLongitude() << (*atc)-> getSpeed() << " " << (*atc)->getAltitude() << endl;
160 FGAIAircraft* ActiveRunway::getFirstOfStatus(int stat)
162 for (AircraftVecIterator atc =departureCue.begin(); atc != departureCue.end(); atc++) {
163 if ((*atc)->getTakeOffStatus() == stat)
171 /***************************************************************************
173 **************************************************************************/
174 FGTrafficRecord::FGTrafficRecord():
175 id(0), waitsForId(0),
180 allowTransmission(true),
184 latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0),
189 void FGTrafficRecord::setPositionAndIntentions(int pos,
190 FGAIFlightPlan * route)
194 if (intentions.size()) {
195 intVecIterator i = intentions.begin();
197 SG_LOG(SG_ATC, SG_ALERT,
198 "Error in FGTrafficRecord::setPositionAndIntentions at " << SG_ORIGIN);
202 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
203 int size = route->getNrOfWayPoints();
204 //cerr << "Setting pos" << pos << " ";
205 //cerr << "setting intentions ";
206 for (int i = 2; i < size; i++) {
207 int val = route->getRouteIndex(i);
208 intentions.push_back(val);
213 * Check if another aircraft is ahead of the current one, and on the same
214 * return true / false is the is/isn't the case.
216 ****************************************************************************/
218 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
221 //cerr << "Start check 1" << endl;
222 if (currentPos == other.currentPos) {
223 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
226 // else if (other.intentions.size())
228 // cerr << "Start check 2" << endl;
229 // intVecIterator i = other.intentions.begin();
230 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
232 // if (i != other.intentions.end()) {
233 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
236 else if (intentions.size()) {
237 //cerr << "Start check 3" << endl;
238 intVecIterator i = intentions.begin();
239 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
240 while (i != intentions.end()) {
241 if ((*i) == other.currentPos) {
246 if (i != intentions.end()) {
247 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
251 //cerr << "Done !!" << endl;
255 void FGTrafficRecord::setPositionAndHeading(double lat, double lon,
256 double hdg, double spd,
266 int FGTrafficRecord::crosses(FGGroundNetwork * net,
267 FGTrafficRecord & other)
269 if (checkPositionAndIntentions(other)
270 || (other.checkPositionAndIntentions(*this)))
273 int currentTargetNode = 0, otherTargetNode = 0;
275 currentTargetNode = net->findSegment(currentPos)->getEnd()->getIndex(); // OKAY,...
276 if (other.currentPos > 0)
277 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
278 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
279 return currentTargetNode;
280 if (intentions.size()) {
281 for (i = intentions.begin(); i != intentions.end(); i++) {
283 if (currentTargetNode ==
284 net->findSegment(*i)->getEnd()->getIndex()) {
285 //cerr << "Current crosses at " << currentTargetNode <<endl;
286 return currentTargetNode;
291 if (other.intentions.size()) {
292 for (i = other.intentions.begin(); i != other.intentions.end();
295 if (otherTargetNode ==
296 net->findSegment(*i)->getEnd()->getIndex()) {
297 //cerr << "Other crosses at " << currentTargetNode <<endl;
298 return otherTargetNode;
303 if (intentions.size() && other.intentions.size()) {
304 for (i = intentions.begin(); i != intentions.end(); i++) {
305 for (j = other.intentions.begin(); j != other.intentions.end();
307 //cerr << "finding segment " << *i << " and " << *j << endl;
308 if (((*i) > 0) && ((*j) > 0)) {
310 net->findSegment(*i)->getEnd()->getIndex();
312 net->findSegment(*j)->getEnd()->getIndex();
313 if (currentTargetNode == otherTargetNode) {
314 //cerr << "Routes will cross at " << currentTargetNode << endl;
315 return currentTargetNode;
324 bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
325 FGTrafficRecord & other)
327 int node = -1, othernode = -1;
329 node = net->findSegment(currentPos)->getEnd()->getIndex();
330 if (other.currentPos > 0)
332 net->findSegment(other.currentPos)->getEnd()->getIndex();
333 if ((node == othernode) && (node != -1))
335 if (other.intentions.size()) {
336 for (intVecIterator i = other.intentions.begin();
337 i != other.intentions.end(); i++) {
339 othernode = net->findSegment(*i)->getEnd()->getIndex();
340 if ((node == othernode) && (node > -1))
345 //if (other.currentPos > 0)
346 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
347 //if (intentions.size())
349 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
353 // node = net->findSegment(*i)->getEnd()->getIndex();
354 // if ((node == othernode) && (node > -1))
363 bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
364 FGTrafficRecord & other, int node)
366 // Check if current segment is the reverse segment for the other aircraft
368 //cerr << "Current segment " << currentPos << endl;
369 if ((currentPos > 0) && (other.currentPos > 0)) {
370 opp = net->findSegment(currentPos)->opposite();
372 if (opp->getIndex() == other.currentPos)
376 for (intVecIterator i = intentions.begin(); i != intentions.end();
378 if ((opp = net->findSegment(other.currentPos)->opposite())) {
380 if (opp->getIndex() ==
381 net->findSegment(*i)->getIndex()) {
382 if (net->findSegment(*i)->getStart()->getIndex() ==
385 //cerr << "Found the node " << node << endl;
391 if (other.intentions.size()) {
392 for (intVecIterator j = other.intentions.begin();
393 j != other.intentions.end(); j++) {
394 // cerr << "Current segment 1 " << (*i) << endl;
396 if ((opp = net->findSegment(*i)->opposite())) {
397 if (opp->getIndex() ==
398 net->findSegment(*j)->getIndex()) {
399 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
400 // << " and " << net->findSegment(*j)->getIndex()
401 // << " are opposites " << endl;
402 if (net->findSegment(*i)->getStart()->
403 getIndex() == node) {
405 //cerr << "Found the node " << node << endl;
419 bool FGTrafficRecord::isActive(int margin) const
421 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
422 time_t deptime = aircraft->getTrafficRef()->getDepartureTime();
423 return ((now + margin) > deptime);
427 void FGTrafficRecord::setSpeedAdjustment(double spd)
429 instruction.setChangeSpeed(true);
430 instruction.setSpeed(spd);
433 void FGTrafficRecord::setHeadingAdjustment(double heading)
435 instruction.setChangeHeading(true);
436 instruction.setHeading(heading);
439 bool FGTrafficRecord::pushBackAllowed() const
441 return allowPushback;
447 /***************************************************************************
450 **************************************************************************/
451 FGATCInstruction::FGATCInstruction()
454 holdPosition = false;
456 changeHeading = false;
457 changeAltitude = false;
458 resolveCircularWait = false;
466 bool FGATCInstruction::hasInstruction() const
468 return (holdPattern || holdPosition || changeSpeed || changeHeading
469 || changeAltitude || resolveCircularWait);
472 /***************************************************************************
475 **************************************************************************/
480 FGATCController::FGATCController()
482 //cerr << "running FGATController constructor" << endl;
485 lastTransmission = 0;
487 lastTransmissionDirection = ATC_AIR_TO_GROUND;
491 FGATCController::~FGATCController()
493 //cerr << "running FGATController destructor" << endl;
496 string FGATCController::getGateName(FGAIAircraft * ref)
498 return ref->atGate();
501 bool FGATCController::isUserAircraft(FGAIAircraft* ac)
503 return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false;
506 void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent, AtcMsgId msgId,
507 AtcMsgDir msgDir, bool audible)
509 string sender, receiver;
514 string atisInformation;
523 string transponderCode;
526 string instructionText;
530 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
531 if (rec->getAircraft()->getTaxiClearanceRequest()) {
532 instructionText = "push-back and taxi";
534 instructionText = "taxi";
536 //cerr << "transmitting for: " << sender << "Leg = " << rec->getLeg() << endl;
537 switch (rec->getLeg()) {
540 freqId = rec->getNextFrequency();
542 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
543 getDynamics()->getGroundFrequency(rec->getLeg() + freqId);
545 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
546 getDynamics()->getGroundFrequency(2);
548 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
549 getDynamics()->getTowerFrequency(2);
551 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
552 getName() + "-Ground";
554 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
555 getDynamics()->getAtisSequence();
559 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
560 getName() + "-Tower";
563 // Swap sender and receiver value in case of a ground to air transmission
564 if (msgDir == ATC_GROUND_TO_AIR) {
571 case MSG_ANNOUNCE_ENGINE_START:
572 text = sender + ". Ready to Start up";
574 case MSG_REQUEST_ENGINE_START:
576 receiver + ", This is " + sender + ". Position " +
577 getGateName(rec->getAircraft()) + ". Information " +
578 atisInformation + ". " +
579 rec->getAircraft()->getTrafficRef()->getFlightRules() +
581 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->
582 getName() + ". Request start-up";
584 // Acknowledge engine startup permission
585 // Assign departure runway
586 // Assign SID, if necessery (TODO)
587 case MSG_PERMIT_ENGINE_START:
588 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
590 heading = rec->getAircraft()->getTrafficRef()->getCourse();
591 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
593 rec->getAircraft()->GetFlightPlan()->
594 getRunwayClassFromTrafficType(fltType);
596 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
597 getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
599 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
601 rec->getAircraft()->GetFlightPlan()->setSID(fp);
603 SID = fp->getName() + " departure";
605 SID = "fly runway heading ";
607 //snprintf(buffer, 7, "%3.2f", heading);
608 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
609 transponderCode = genTransponderCode(fltRules);
610 rec->getAircraft()->SetTransponderCode(transponderCode);
612 receiver + ". Start-up approved. " + atisInformation +
613 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
614 transponderCode + ". " +
615 "For "+ instructionText + " clearance call " + taxiFreqStr + ". " +
616 sender + " control.";
618 case MSG_DENY_ENGINE_START:
619 text = receiver + ". Standby";
621 case MSG_ACKNOWLEDGE_ENGINE_START:
622 fp = rec->getAircraft()->GetFlightPlan()->getSID();
625 rec->getAircraft()->GetFlightPlan()->getSID()->getName() +
628 SID = "fly runway heading ";
630 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
631 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
632 transponderCode = rec->getAircraft()->GetTransponderCode();
635 receiver + ". Start-up approved. " + atisInformation +
636 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
637 transponderCode + ". " +
638 "For " + instructionText + " clearance call " + taxiFreqStr + ". " +
641 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
642 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
643 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
645 case MSG_INITIATE_CONTACT:
646 text = receiver + ". With you. " + sender;
648 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
649 text = receiver + ". Roger. " + sender;
651 case MSG_REQUEST_PUSHBACK_CLEARANCE:
652 if (rec->getAircraft()->getTaxiClearanceRequest()) {
653 text = receiver + ". Request push-back. " + sender;
655 text = receiver + ". Request Taxi clearance. " + sender;
658 case MSG_PERMIT_PUSHBACK_CLEARANCE:
659 if (rec->getAircraft()->getTaxiClearanceRequest()) {
660 text = receiver + ". Push-back approved. " + sender;
662 text = receiver + ". Cleared to Taxi." + sender;
665 case MSG_HOLD_PUSHBACK_CLEARANCE:
666 text = receiver + ". Standby. " + sender;
668 case MSG_REQUEST_TAXI_CLEARANCE:
669 text = receiver + ". Ready to Taxi. " + sender;
671 case MSG_ISSUE_TAXI_CLEARANCE:
672 text = receiver + ". Cleared to taxi. " + sender;
674 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
675 text = receiver + ". Cleared to taxi. " + sender;
677 case MSG_HOLD_POSITION:
678 text = receiver + ". Hold Position. " + sender;
680 case MSG_ACKNOWLEDGE_HOLD_POSITION:
681 text = receiver + ". Holding Position. " + sender;
683 case MSG_RESUME_TAXI:
684 text = receiver + ". Resume Taxiing. " + sender;
686 case MSG_ACKNOWLEDGE_RESUME_TAXI:
687 text = receiver + ". Continuing Taxi. " + sender;
689 case MSG_REPORT_RUNWAY_HOLD_SHORT:
690 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
691 //activeRunway = "test";
692 text = receiver + ". Holding short runway "
696 //cerr << "1 Currently at leg " << rec->getLeg() << endl;
698 case MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT:
699 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
700 text = receiver + "Roger. Holding short runway "
704 //cerr << "2 Currently at leg " << rec->getLeg() << endl;
706 case MSG_SWITCH_TOWER_FREQUENCY:
707 towerFreqStr = formatATCFrequency3_2(towerFreq);
708 text = receiver + "Contact Tower at " + towerFreqStr + ". " + sender;
710 //cerr << "3 Currently at leg " << rec->getLeg() << endl;
712 case MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY:
713 towerFreqStr = formatATCFrequency3_2(towerFreq);
714 text = receiver + "Roger, switching to tower at " + towerFreqStr + ". " + sender;
716 //cerr << "4 Currently at leg " << rec->getLeg() << endl;
720 text = text + sender + ". Transmitting unknown Message";
724 double onBoardRadioFreq0 =
725 fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
726 double onBoardRadioFreq1 =
727 fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
728 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
729 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
730 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << endl;
732 // Display ATC message only when one of the radios is tuned
733 // the relevant frequency.
734 // Note that distance attenuation is currently not yet implemented
736 if ((stationFreq > 0)&&
737 ((onBoardRadioFreqI0 == stationFreq)||
738 (onBoardRadioFreqI1 == stationFreq))) {
739 if (rec->allowTransmissions()) {
741 if( fgGetBool( "/sim/radio/use-itm-attenuation", false ) ) {
742 //cerr << "Using ITM radio propagation" << endl;
743 FGRadioTransmission* radio = new FGRadioTransmission();
745 double sender_alt_ft, sender_alt;
747 sender_pos = parent->parent()->geod();
750 sender_alt_ft = rec->getAltitude();
751 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
752 sender_pos= SGGeod::fromDegM( rec->getLongitude(),
753 rec->getLatitude(), sender_alt );
755 double frequency = ((double)stationFreq) / 100;
756 radio->receiveATC(sender_pos, frequency, text, ground_to_air);
760 fgSetString("/sim/messages/atc", text.c_str());
765 FGATCDialogNew::instance()->addEntry(1, text);
770 string FGATCController::formatATCFrequency3_2(int freq)
773 snprintf(buffer, 7, "%3.2f", ((float) freq / 100.0));
774 return string(buffer);
777 // TODO: Set transponder codes according to real-world routes.
778 // The current version just returns a random string of four octal numbers.
779 string FGATCController::genTransponderCode(const string& fltRules)
781 if (fltRules == "VFR") {
782 return string("1200");
785 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8,
787 return string(buffer);
791 void FGATCController::init()
794 FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
795 mgr->addController(this);
800 /***************************************************************************
801 * class FGTowerController
803 **************************************************************************/
804 FGTowerController::FGTowerController(FGAirportDynamics *par) :
811 void FGTowerController::announcePosition(int id,
812 FGAIFlightPlan * intendedRoute,
813 int currentPosition, double lat,
814 double lon, double heading,
815 double speed, double alt,
816 double radius, int leg,
820 TrafficVectorIterator i = activeTraffic.begin();
821 // Search whether the current id alread has an entry
822 // This might be faster using a map instead of a vector, but let's start by taking a safe route
823 if (activeTraffic.size()) {
824 //while ((i->getId() != id) && i != activeTraffic.end()) {
825 while (i != activeTraffic.end()) {
826 if (i->getId() == id) {
832 // Add a new TrafficRecord if no one exsists for this aircraft.
833 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
837 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
838 rec.setRunway(intendedRoute->getRunway());
840 //rec.setCallSign(callsign);
841 rec.setRadius(radius);
842 rec.setAircraft(ref);
843 activeTraffic.push_back(rec);
844 // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
845 ActiveRunwayVecIterator rwy = activeRunways.begin();
846 if (activeRunways.size()) {
847 while (rwy != activeRunways.end()) {
848 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
854 if (rwy == activeRunways.end()) {
855 ActiveRunway aRwy(intendedRoute->getRunway(), id);
856 aRwy.addToDepartureCue(ref);
857 activeRunways.push_back(aRwy);
858 rwy = (activeRunways.end()-1);
860 rwy->addToDepartureCue(ref);
863 //cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() << " for takeoff " << endl;
865 i->setPositionAndHeading(lat, lon, heading, speed, alt);
869 void FGTowerController::updateAircraftInformation(int id, double lat, double lon,
870 double heading, double speed, double alt,
873 TrafficVectorIterator i = activeTraffic.begin();
874 // Search whether the current id has an entry
875 // This might be faster using a map instead of a vector, but let's start by taking a safe route
876 TrafficVectorIterator current, closest;
877 if (activeTraffic.size()) {
878 //while ((i->getId() != id) && i != activeTraffic.end()) {
879 while (i != activeTraffic.end()) {
880 if (i->getId() == id) {
886 // // update position of the current aircraft
887 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
888 SG_LOG(SG_ATC, SG_ALERT,
889 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
891 i->setPositionAndHeading(lat, lon, heading, speed, alt);
896 // see if we already have a clearance record for the currently active runway
897 // NOTE: dd. 2011-08-07: Because the active runway has been constructed in the announcePosition function, we may safely assume that is
898 // already exists here. So, we can simplify the current code.
900 ActiveRunwayVecIterator rwy = activeRunways.begin();
901 //if (parent->getId() == fgGetString("/sim/presets/airport-id")) {
902 // for (rwy = activeRunways.begin(); rwy != activeRunways.end(); rwy++) {
903 // rwy->printDepartureCue();
907 rwy = activeRunways.begin();
908 while (rwy != activeRunways.end()) {
909 if (rwy->getRunwayName() == current->getRunway()) {
915 // only bother running the following code if the current aircraft is the
916 // first in line for depature
917 /* if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
918 if (rwy->getCleared()) {
919 if (id == rwy->getCleared()) {
920 current->setHoldPosition(false);
922 current->setHoldPosition(true);
925 // For now. At later stages, this will probably be the place to check for inbound traffc.
929 // only bother with aircraft that have a takeoff status of 2, since those are essentially under tower control
930 FGAIAircraft* ac= rwy->getFirstAircraftInDepartureCue();
931 if (ac->getTakeOffStatus() == 1) {
932 ac->setTakeOffStatus(2);
934 if (current->getAircraft()->getTakeOffStatus() == 2) {
935 current -> setHoldPosition(false);
937 current->setHoldPosition(true);
939 int clearanceId = rwy->getCleared();
941 if (id == clearanceId) {
942 current->setHoldPosition(false);
945 if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
947 FGAIAircraft *ac = rwy->getFirstOfStatus(1);
949 ac->setTakeOffStatus(2);
955 void FGTowerController::signOff(int id)
957 TrafficVectorIterator i = activeTraffic.begin();
958 // Search search if the current id alread has an entry
959 // This might be faster using a map instead of a vector, but let's start by taking a safe route
960 if (activeTraffic.size()) {
961 //while ((i->getId() != id) && i != activeTraffic.end()) {
962 while (i != activeTraffic.end()) {
963 if (i->getId() == id) {
969 // If this aircraft has left the runway, we can clear the departure record for this runway
970 ActiveRunwayVecIterator rwy = activeRunways.begin();
971 if (activeRunways.size()) {
972 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
973 while (rwy != activeRunways.end()) {
974 if (rwy->getRunwayName() == i->getRunway()) {
979 if (rwy != activeRunways.end()) {
981 rwy->updateDepartureCue();
983 SG_LOG(SG_ATC, SG_ALERT,
984 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff at " << SG_ORIGIN);
987 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
988 SG_LOG(SG_ATC, SG_ALERT,
989 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
991 i->getAircraft()->resetTakeOffStatus();
992 i = activeTraffic.erase(i);
993 //cerr << "Signing off from tower controller" << endl;
998 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
999 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1000 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1001 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1002 // Note that this function is probably obsolete
1003 bool FGTowerController::hasInstruction(int id)
1005 TrafficVectorIterator i = activeTraffic.begin();
1006 // Search search if the current id has an entry
1007 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1008 if (activeTraffic.size()) {
1009 //while ((i->getId() != id) && i != activeTraffic.end()) {
1010 while (i != activeTraffic.end()) {
1011 if (i->getId() == id) {
1017 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1018 SG_LOG(SG_ATC, SG_ALERT,
1019 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1021 return i->hasInstruction();
1027 FGATCInstruction FGTowerController::getInstruction(int id)
1029 TrafficVectorIterator i = activeTraffic.begin();
1030 // Search search if the current id has an entry
1031 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1032 if (activeTraffic.size()) {
1033 //while ((i->getId() != id) && i != activeTraffic.end()) {
1034 while (i != activeTraffic.end()) {
1035 if (i->getId() == id) {
1041 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1042 SG_LOG(SG_ATC, SG_ALERT,
1043 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1045 return i->getInstruction();
1047 return FGATCInstruction();
1050 void FGTowerController::render(bool visible) {
1051 //cerr << "FGTowerController::render function not yet implemented" << endl;
1054 string FGTowerController::getName() {
1055 return string(parent->getId() + "-tower");
1058 void FGTowerController::update(double dt)
1065 /***************************************************************************
1066 * class FGStartupController
1068 **************************************************************************/
1069 FGStartupController::FGStartupController(FGAirportDynamics *par):
1075 void FGStartupController::announcePosition(int id,
1076 FGAIFlightPlan * intendedRoute,
1077 int currentPosition, double lat,
1078 double lon, double heading,
1079 double speed, double alt,
1080 double radius, int leg,
1084 TrafficVectorIterator i = activeTraffic.begin();
1085 // Search whether the current id alread has an entry
1086 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1087 if (activeTraffic.size()) {
1088 //while ((i->getId() != id) && i != activeTraffic.end()) {
1089 while (i != activeTraffic.end()) {
1090 if (i->getId() == id) {
1096 // Add a new TrafficRecord if no one exsists for this aircraft.
1097 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1098 FGTrafficRecord rec;
1101 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1102 rec.setRunway(intendedRoute->getRunway());
1104 rec.setPositionAndIntentions(currentPosition, intendedRoute);
1105 //rec.setCallSign(callsign);
1106 rec.setAircraft(ref);
1107 rec.setHoldPosition(true);
1108 activeTraffic.push_back(rec);
1110 i->setPositionAndIntentions(currentPosition, intendedRoute);
1111 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1117 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
1118 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1119 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1120 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1121 // Note that this function is probably obsolete
1122 bool FGStartupController::hasInstruction(int id)
1124 TrafficVectorIterator i = activeTraffic.begin();
1125 // Search search if the current id has an entry
1126 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1127 if (activeTraffic.size()) {
1128 //while ((i->getId() != id) && i != activeTraffic.end()) {
1129 while (i != activeTraffic.end()) {
1130 if (i->getId() == id) {
1136 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1137 SG_LOG(SG_ATC, SG_ALERT,
1138 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1140 return i->hasInstruction();
1146 FGATCInstruction FGStartupController::getInstruction(int id)
1148 TrafficVectorIterator i = activeTraffic.begin();
1149 // Search search if the current id has an entry
1150 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1151 if (activeTraffic.size()) {
1152 //while ((i->getId() != id) && i != activeTraffic.end()) {
1153 while (i != activeTraffic.end()) {
1154 if (i->getId() == id) {
1160 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1161 SG_LOG(SG_ATC, SG_ALERT,
1162 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1164 return i->getInstruction();
1166 return FGATCInstruction();
1169 void FGStartupController::signOff(int id)
1171 TrafficVectorIterator i = activeTraffic.begin();
1172 // Search search if the current id alread has an entry
1173 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1174 if (activeTraffic.size()) {
1175 //while ((i->getId() != id) && i != activeTraffic.end()) {
1176 while (i != activeTraffic.end()) {
1177 if (i->getId() == id) {
1183 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1184 SG_LOG(SG_ATC, SG_ALERT,
1185 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
1187 //cerr << i->getAircraft()->getCallSign() << " signing off from startupcontroller" << endl;
1188 i = activeTraffic.erase(i);
1192 bool FGStartupController::checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
1195 int state = i->getState();
1196 if ((state == st) && available) {
1197 if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
1199 //cerr << "Checking state " << st << " for " << i->getAircraft()->getCallSign() << endl;
1200 SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
1201 int n = trans_num->getIntValue();
1203 trans_num->setIntValue(-1);
1204 // PopupCallback(n);
1205 //cerr << "Selected transmission message " << n << endl;
1206 FGATCDialogNew::instance()->removeEntry(1);
1208 //cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
1209 transmit(&(*i), &(*parent), msgId, msgDir, false);
1213 if (now > startTime) {
1214 //cerr << "Transmitting startup msg" << endl;
1215 transmit(&(*i), &(*parent), msgId, msgDir, true);
1217 lastTransmission = now;
1225 void FGStartupController::updateAircraftInformation(int id, double lat, double lon,
1226 double heading, double speed, double alt,
1229 TrafficVectorIterator i = activeTraffic.begin();
1230 // Search search if the current id has an entry
1231 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1232 TrafficVectorIterator current, closest;
1233 if (activeTraffic.size()) {
1234 //while ((i->getId() != id) && i != activeTraffic.end()) {
1235 while (i != activeTraffic.end()) {
1236 if (i->getId() == id) {
1242 // // update position of the current aircraft
1244 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1245 SG_LOG(SG_ATC, SG_ALERT,
1246 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1248 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1251 setDt(getDt() + dt);
1253 int state = i->getState();
1255 // The user controlled aircraft should have crased here, because it doesn't have a traffic reference.
1256 // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight.
1257 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
1258 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1259 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
1260 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
1261 // << " at parking " << getGateName(i->getAircraft()) << endl;
1263 if ((now - lastTransmission) > 3 + (rand() % 15)) {
1267 checkTransmissionState(0, now, (startTime + 0 ), i, MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
1268 checkTransmissionState(1, now, (startTime + 60 ), i, MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
1269 checkTransmissionState(2, now, (startTime + 80 ), i, MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
1270 checkTransmissionState(3, now, (startTime + 100), i, MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
1271 if (checkTransmissionState(4, now, (startTime + 130), i, MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND)) {
1274 checkTransmissionState(5, now, (startTime + 140), i, MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
1275 checkTransmissionState(6, now, (startTime + 150), i, MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR);
1276 checkTransmissionState(7, now, (startTime + 180), i, MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
1280 if ((state == 8) && available) {
1281 if (now > startTime + 200) {
1282 if (i->pushBackAllowed()) {
1283 i->allowRepeatedTransmissions();
1284 transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE,
1285 ATC_GROUND_TO_AIR, true);
1288 transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE,
1289 ATC_GROUND_TO_AIR, true);
1290 i->suppressRepeatedTransmissions();
1292 lastTransmission = now;
1296 if ((state == 9) && available) {
1297 i->setHoldPosition(false);
1301 // Note that this function is copied from simgear. for maintanance purposes, it's probabtl better to make a general function out of that.
1302 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
1303 double lon, double elev, double hdg, double slope)
1305 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
1306 obj_pos = makeZUpFrame(geod);
1307 // hdg is not a compass heading, but a counter-clockwise rotation
1308 // around the Z axis
1309 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
1311 obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS,
1316 void FGStartupController::render(bool visible)
1319 SGMaterialLib *matlib = globals->get_matlib();
1322 globals->get_scenery()->get_scene_graph()->removeChild(group);
1323 //while (group->getNumChildren()) {
1324 // cerr << "Number of children: " << group->getNumChildren() << endl;
1325 //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
1326 //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0);
1327 //geode->releaseGLObjects();
1328 //group->removeChild(geode);
1333 group = new osg::Group;
1334 FGScenery * local_scenery = globals->get_scenery();
1335 //double elevation_meters = 0.0;
1336 //double elevation_feet = 0.0;
1339 //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
1341 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1342 for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) {
1343 if (i->isActive(300)) {
1344 // Handle start point
1345 int pos = i->getCurrentPosition();
1346 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "pos = " << pos << endl;
1348 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(pos);
1349 SGGeod start(SGGeod::fromDeg((i->getLongitude()), (i->getLatitude())));
1350 SGGeod end (segment->getEnd()->geod());
1352 double length = SGGeodesy::distanceM(start, end);
1353 //heading = SGGeodesy::headingDeg(start->geod(), end->geod());
1355 double az2, heading; //, distanceM;
1356 SGGeodesy::inverse(start, end, heading, az2, length);
1357 double coveredDistance = length * 0.5;
1359 SGGeodesy::direct(start, heading, coveredDistance, center, az2);
1360 //cerr << "Active Aircraft : Centerpoint = (" << center.getLatitudeDeg() << ", " << center.getLongitudeDeg() << "). Heading = " << heading << endl;
1361 ///////////////////////////////////////////////////////////////////////////////
1362 // Make a helper function out of this
1363 osg::Matrix obj_pos;
1364 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1365 obj_trans->setDataVariance(osg::Object::STATIC);
1366 // Experimental: Calculate slope here, based on length, and the individual elevations
1367 double elevationStart;
1368 if (isUserAircraft((i)->getAircraft())) {
1369 elevationStart = fgGetDouble("/position/ground-elev-m");
1371 elevationStart = ((i)->getAircraft()->_getAltitude() * SG_FEET_TO_METER);
1373 double elevationEnd = segment->getEnd()->getElevationM();
1374 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1375 SGGeod center2 = end;
1376 center2.setElevationM(SG_MAX_ELEVATION_M);
1377 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1378 //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1379 //elevation_meters += 0.5;
1382 elevationEnd = parent->getElevation();
1384 segment->getEnd()->setElevation(elevationEnd);
1387 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1388 double elevDiff = elevationEnd - elevationStart;
1390 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1392 //cerr << "1. Using mean elevation : " << elevationMean << " and " << slope << endl;
1394 WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5 + dx, -(heading), slope );
1397 obj_trans->setMatrix( obj_pos );
1398 //osg::Vec3 center(0, 0, 0)
1400 float width = length /2.0;
1401 osg::Vec3 corner(-width, 0, 0.25f);
1402 osg::Vec3 widthVec(2*width + 1, 0, 0);
1403 osg::Vec3 heightVec(0, 1, 0);
1404 osg::Geometry* geometry;
1405 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1406 simgear::EffectGeode* geode = new simgear::EffectGeode;
1407 geode->setName("test");
1408 geode->addDrawable(geometry);
1409 //osg::Node *custom_obj;
1411 if (segment->hasBlock(now)) {
1412 mat = matlib->find("UnidirectionalTaperRed");
1414 mat = matlib->find("UnidirectionalTaperGreen");
1417 geode->setEffect(mat->get_effect());
1418 obj_trans->addChild(geode);
1419 // wire as much of the scene graph together as we can
1420 //->addChild( obj_trans );
1421 group->addChild( obj_trans );
1422 /////////////////////////////////////////////////////////////////////
1424 //cerr << "BIG FAT WARNING: current position is here : " << pos << endl;
1426 for (intVecIterator j = (i)->getIntentions().begin(); j != (i)->getIntentions().end(); j++) {
1427 osg::Matrix obj_pos;
1430 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "intention = " << k << endl;
1431 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1432 obj_trans->setDataVariance(osg::Object::STATIC);
1433 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(k);
1435 double elevationStart = segment->getStart()->getElevationM();
1436 double elevationEnd = segment->getEnd ()->getElevationM();
1437 if ((elevationStart == 0) || (elevationStart == parent->getElevation())) {
1438 SGGeod center2 = segment->getStart()->geod();
1439 center2.setElevationM(SG_MAX_ELEVATION_M);
1440 if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) {
1441 //elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5;
1442 //elevation_meters += 0.5;
1445 elevationStart = parent->getElevation();
1447 segment->getStart()->setElevation(elevationStart);
1449 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1450 SGGeod center2 = segment->getEnd()->geod();
1451 center2.setElevationM(SG_MAX_ELEVATION_M);
1452 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1453 //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1454 //elevation_meters += 0.5;
1457 elevationEnd = parent->getElevation();
1459 segment->getEnd()->setElevation(elevationEnd);
1462 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1463 double elevDiff = elevationEnd - elevationStart;
1464 double length = segment->getLength();
1465 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1467 //cerr << "2. Using mean elevation : " << elevationMean << " and " << slope << endl;
1469 SGGeod segCenter(segment->getCenter());
1470 WorldCoordinate( obj_pos, segCenter.getLatitudeDeg(),
1471 segCenter.getLongitudeDeg(), elevationMean + 0.5 + dx, -(segment->getHeading()), slope );
1473 //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
1475 obj_trans->setMatrix( obj_pos );
1476 //osg::Vec3 center(0, 0, 0)
1478 float width = segment->getLength() /2.0;
1479 osg::Vec3 corner(-width, 0, 0.25f);
1480 osg::Vec3 widthVec(2*width + 1, 0, 0);
1481 osg::Vec3 heightVec(0, 1, 0);
1482 osg::Geometry* geometry;
1483 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1484 simgear::EffectGeode* geode = new simgear::EffectGeode;
1485 geode->setName("test");
1486 geode->addDrawable(geometry);
1487 //osg::Node *custom_obj;
1489 if (segment->hasBlock(now)) {
1490 mat = matlib->find("UnidirectionalTaperRed");
1492 mat = matlib->find("UnidirectionalTaperGreen");
1495 geode->setEffect(mat->get_effect());
1496 obj_trans->addChild(geode);
1497 // wire as much of the scene graph together as we can
1498 //->addChild( obj_trans );
1499 group->addChild( obj_trans );
1501 //cerr << "BIG FAT WARNING: k is here : " << pos << endl;
1507 globals->get_scenery()->get_scene_graph()->addChild(group);
1511 string FGStartupController::getName() {
1512 return string(parent->getId() + "-startup");
1515 void FGStartupController::update(double dt)
1522 /***************************************************************************
1523 * class FGApproachController
1525 **************************************************************************/
1526 FGApproachController::FGApproachController(FGAirportDynamics *par):
1533 void FGApproachController::announcePosition(int id,
1534 FGAIFlightPlan * intendedRoute,
1535 int currentPosition,
1536 double lat, double lon,
1537 double heading, double speed,
1538 double alt, double radius,
1539 int leg, FGAIAircraft * ref)
1542 TrafficVectorIterator i = activeTraffic.begin();
1543 // Search whether the current id alread has an entry
1544 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1545 if (activeTraffic.size()) {
1546 //while ((i->getId() != id) && i != activeTraffic.end()) {
1547 while (i != activeTraffic.end()) {
1548 if (i->getId() == id) {
1554 // Add a new TrafficRecord if no one exsists for this aircraft.
1555 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1556 FGTrafficRecord rec;
1559 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1560 rec.setRunway(intendedRoute->getRunway());
1562 //rec.setCallSign(callsign);
1563 rec.setAircraft(ref);
1564 activeTraffic.push_back(rec);
1566 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1570 void FGApproachController::updateAircraftInformation(int id, double lat, double lon,
1571 double heading, double speed, double alt,
1574 TrafficVectorIterator i = activeTraffic.begin();
1575 // Search search if the current id has an entry
1576 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1577 TrafficVectorIterator current, closest;
1578 if (activeTraffic.size()) {
1579 //while ((i->getId() != id) && i != activeTraffic.end()) {
1580 while (i != activeTraffic.end()) {
1581 if (i->getId() == id) {
1587 // // update position of the current aircraft
1588 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1589 SG_LOG(SG_ATC, SG_ALERT,
1590 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1592 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1594 //cerr << "ApproachController: checking for speed" << endl;
1596 current->getAircraft()->
1597 checkForArrivalTime(string("final001"));
1598 if (time_diff > 15) {
1599 current->setSpeedAdjustment(current->getAircraft()->
1600 getPerformance()->vDescent() *
1602 } else if (time_diff > 5) {
1603 current->setSpeedAdjustment(current->getAircraft()->
1604 getPerformance()->vDescent() *
1606 } else if (time_diff < -15) {
1607 current->setSpeedAdjustment(current->getAircraft()->
1608 getPerformance()->vDescent() *
1610 } else if (time_diff < -5) {
1611 current->setSpeedAdjustment(current->getAircraft()->
1612 getPerformance()->vDescent() *
1615 current->clearSpeedAdjustment();
1617 //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff);
1619 setDt(getDt() + dt);
1622 void FGApproachController::signOff(int id)
1624 TrafficVectorIterator i = activeTraffic.begin();
1625 // Search search if the current id alread has an entry
1626 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1627 if (activeTraffic.size()) {
1628 //while ((i->getId() != id) && i != activeTraffic.end()) {
1629 while (i != activeTraffic.end()) {
1630 if (i->getId() == id) {
1636 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1637 SG_LOG(SG_ATC, SG_ALERT,
1638 "AI error: Aircraft without traffic record is signing off from approach at " << SG_ORIGIN);
1640 i = activeTraffic.erase(i);
1644 void FGApproachController::update(double dt)
1651 bool FGApproachController::hasInstruction(int id)
1653 TrafficVectorIterator i = activeTraffic.begin();
1654 // Search search if the current id has an entry
1655 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1656 if (activeTraffic.size()) {
1657 //while ((i->getId() != id) && i != activeTraffic.end()) {
1658 while (i != activeTraffic.end()) {
1659 if (i->getId() == id) {
1665 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1666 SG_LOG(SG_ATC, SG_ALERT,
1667 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1669 return i->hasInstruction();
1675 FGATCInstruction FGApproachController::getInstruction(int id)
1677 TrafficVectorIterator i = activeTraffic.begin();
1678 // Search search if the current id has an entry
1679 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1680 if (activeTraffic.size()) {
1681 //while ((i->getId() != id) && i != activeTraffic.end()) {
1682 while (i != activeTraffic.end()) {
1683 if (i->getId() == id) {
1689 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1690 SG_LOG(SG_ATC, SG_ALERT,
1691 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1693 return i->getInstruction();
1695 return FGATCInstruction();
1699 ActiveRunway *FGApproachController::getRunway(const string& name)
1701 ActiveRunwayVecIterator rwy = activeRunways.begin();
1702 if (activeRunways.size()) {
1703 while (rwy != activeRunways.end()) {
1704 if (rwy->getRunwayName() == name) {
1710 if (rwy == activeRunways.end()) {
1711 ActiveRunway aRwy(name, 0);
1712 activeRunways.push_back(aRwy);
1713 rwy = activeRunways.end() - 1;
1718 void FGApproachController::render(bool visible) {
1719 //cerr << "FGApproachController::render function not yet implemented" << endl;
1724 string FGApproachController::getName() {
1725 return string(parent->getId() + "-approach");