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 <simgear/scene/util/OsgMath.hxx>
38 #include <Scenery/scenery.hxx>
40 #include "trafficcontrol.hxx"
41 #include "atc_mgr.hxx"
42 #include <AIModel/AIAircraft.hxx>
43 #include <AIModel/AIFlightPlan.hxx>
44 #include <AIModel/performancedata.hxx>
45 #include <AIModel/performancedb.hxx>
46 #include <ATC/atc_mgr.hxx>
47 #include <Traffic/TrafficMgr.hxx>
48 #include <Airports/groundnetwork.hxx>
49 #include <Airports/dynamics.hxx>
50 #include <Airports/simple.hxx>
51 #include <Radio/radio.hxx>
56 /***************************************************************************
58 **************************************************************************/
59 time_t ActiveRunway::requestTimeSlot(time_t eta)
62 time_t separation = 90;
64 if (estimatedArrivalTimes.size() == 0) {
65 estimatedArrivalTimes.push_back(eta);
68 TimeVectorIterator i = estimatedArrivalTimes.begin();
69 //cerr << "Checking eta slots " << eta << ": " << endl;
70 for (i = estimatedArrivalTimes.begin();
71 i != estimatedArrivalTimes.end(); i++) {
72 //cerr << "Stored time : " << (*i) << endl;
74 i = estimatedArrivalTimes.begin();
75 if ((eta + separation) < (*i)) {
78 //cerr << "Storing at beginning" << endl;
80 while ((i != estimatedArrivalTimes.end()) && (!found)) {
81 TimeVectorIterator j = i + 1;
82 if (j == estimatedArrivalTimes.end()) {
83 if (((*i) + separation) < eta) {
84 //cerr << "Storing at end" << endl;
87 newEta = (*i) + separation;
88 //cerr << "Storing at end + separation" << endl;
91 if ((((*j) - (*i)) > (separation * 2))) { // found a potential slot
92 // now check whether this slot is usable:
93 // 1) eta should fall between the two points
94 // i.e. eta > i AND eta < j
96 //cerr << "Found potential slot after " << (*i) << endl;
97 if (eta > (*i) && (eta < (*j))) {
99 if (eta < ((*i) + separation)) {
100 newEta = (*i) + separation;
101 //cerr << "Using original" << (*i) << " + separation " << endl;
104 //cerr << "Using original after " << (*i) << endl;
106 } else if (eta < (*i)) {
108 newEta = (*i) + separation;
109 //cerr << "Using delayed slot after " << (*i) << endl;
112 if (((*j) - separation) < eta) {
114 if (((*i) + separation) < eta) {
116 cerr << "Using original after " << (*i) << endl;
118 newEta = (*i) + separation;
119 cerr << "Using " << (*i) << " + separation " << endl;
127 //cerr << ". done. New ETA : " << newEta << endl;
129 estimatedArrivalTimes.push_back(newEta);
130 sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
131 // do some housekeeping : remove any timestamps that are past
132 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
133 TimeVectorIterator i = estimatedArrivalTimes.begin();
134 while (i != estimatedArrivalTimes.end()) {
136 //cerr << "Deleting timestamp " << (*i) << " (now = " << now << "). " << endl;
137 estimatedArrivalTimes.erase(i);
138 i = estimatedArrivalTimes.begin();
146 void ActiveRunway::printDepartureCue()
148 cout << "Departure cue for " << rwy << ": " << endl;
149 for (AircraftVecIterator atc = departureCue.begin(); atc != departureCue.end(); atc++) {
150 cout << " " << (*atc)->getCallSign() << " " << (*atc)->getTakeOffStatus();
151 cout << " " << (*atc)->_getLatitude() << " " << (*atc)->_getLongitude() << (*atc)-> getSpeed() << " " << (*atc)->getAltitude() << endl;
156 FGAIAircraft* ActiveRunway::getFirstOfStatus(int stat)
158 for (AircraftVecIterator atc =departureCue.begin(); atc != departureCue.end(); atc++) {
159 if ((*atc)->getTakeOffStatus() == stat)
167 /***************************************************************************
169 **************************************************************************/
170 FGTrafficRecord::FGTrafficRecord():
171 id(0), waitsForId(0),
176 allowTransmission(true),
179 latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0)
183 void FGTrafficRecord::setPositionAndIntentions(int pos,
184 FGAIFlightPlan * route)
188 if (intentions.size()) {
189 intVecIterator i = intentions.begin();
191 SG_LOG(SG_ATC, SG_ALERT,
192 "Error in FGTrafficRecord::setPositionAndIntentions at " << SG_ORIGIN);
196 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
197 int size = route->getNrOfWayPoints();
198 //cerr << "Setting pos" << pos << " ";
199 //cerr << "setting intentions ";
200 for (int i = 2; i < size; i++) {
201 int val = route->getRouteIndex(i);
202 intentions.push_back(val);
207 * Check if another aircraft is ahead of the current one, and on the same
208 * return true / false is the is/isn't the case.
210 ****************************************************************************/
212 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
215 //cerr << "Start check 1" << endl;
216 if (currentPos == other.currentPos) {
217 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
220 // else if (other.intentions.size())
222 // cerr << "Start check 2" << endl;
223 // intVecIterator i = other.intentions.begin();
224 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
226 // if (i != other.intentions.end()) {
227 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
230 else if (intentions.size()) {
231 //cerr << "Start check 3" << endl;
232 intVecIterator i = intentions.begin();
233 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
234 while (i != intentions.end()) {
235 if ((*i) == other.currentPos) {
240 if (i != intentions.end()) {
241 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
245 //cerr << "Done !!" << endl;
249 void FGTrafficRecord::setPositionAndHeading(double lat, double lon,
250 double hdg, double spd,
260 int FGTrafficRecord::crosses(FGGroundNetwork * net,
261 FGTrafficRecord & other)
263 if (checkPositionAndIntentions(other)
264 || (other.checkPositionAndIntentions(*this)))
267 int currentTargetNode = 0, otherTargetNode = 0;
269 currentTargetNode = net->findSegment(currentPos)->getEnd()->getIndex(); // OKAY,...
270 if (other.currentPos > 0)
271 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
272 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
273 return currentTargetNode;
274 if (intentions.size()) {
275 for (i = intentions.begin(); i != intentions.end(); i++) {
277 if (currentTargetNode ==
278 net->findSegment(*i)->getEnd()->getIndex()) {
279 //cerr << "Current crosses at " << currentTargetNode <<endl;
280 return currentTargetNode;
285 if (other.intentions.size()) {
286 for (i = other.intentions.begin(); i != other.intentions.end();
289 if (otherTargetNode ==
290 net->findSegment(*i)->getEnd()->getIndex()) {
291 //cerr << "Other crosses at " << currentTargetNode <<endl;
292 return otherTargetNode;
297 if (intentions.size() && other.intentions.size()) {
298 for (i = intentions.begin(); i != intentions.end(); i++) {
299 for (j = other.intentions.begin(); j != other.intentions.end();
301 //cerr << "finding segment " << *i << " and " << *j << endl;
302 if (((*i) > 0) && ((*j) > 0)) {
304 net->findSegment(*i)->getEnd()->getIndex();
306 net->findSegment(*j)->getEnd()->getIndex();
307 if (currentTargetNode == otherTargetNode) {
308 //cerr << "Routes will cross at " << currentTargetNode << endl;
309 return currentTargetNode;
318 bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
319 FGTrafficRecord & other)
321 int node = -1, othernode = -1;
323 node = net->findSegment(currentPos)->getEnd()->getIndex();
324 if (other.currentPos > 0)
326 net->findSegment(other.currentPos)->getEnd()->getIndex();
327 if ((node == othernode) && (node != -1))
329 if (other.intentions.size()) {
330 for (intVecIterator i = other.intentions.begin();
331 i != other.intentions.end(); i++) {
333 othernode = net->findSegment(*i)->getEnd()->getIndex();
334 if ((node == othernode) && (node > -1))
339 //if (other.currentPos > 0)
340 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
341 //if (intentions.size())
343 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
347 // node = net->findSegment(*i)->getEnd()->getIndex();
348 // if ((node == othernode) && (node > -1))
357 bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
358 FGTrafficRecord & other, int node)
360 // Check if current segment is the reverse segment for the other aircraft
362 //cerr << "Current segment " << currentPos << endl;
363 if ((currentPos > 0) && (other.currentPos > 0)) {
364 opp = net->findSegment(currentPos)->opposite();
366 if (opp->getIndex() == other.currentPos)
370 for (intVecIterator i = intentions.begin(); i != intentions.end();
372 if ((opp = net->findSegment(other.currentPos)->opposite())) {
374 if (opp->getIndex() ==
375 net->findSegment(*i)->getIndex()) {
376 if (net->findSegment(*i)->getStart()->getIndex() ==
379 //cerr << "Found the node " << node << endl;
385 if (other.intentions.size()) {
386 for (intVecIterator j = other.intentions.begin();
387 j != other.intentions.end(); j++) {
388 // cerr << "Current segment 1 " << (*i) << endl;
390 if ((opp = net->findSegment(*i)->opposite())) {
391 if (opp->getIndex() ==
392 net->findSegment(*j)->getIndex()) {
393 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
394 // << " and " << net->findSegment(*j)->getIndex()
395 // << " are opposites " << endl;
396 if (net->findSegment(*i)->getStart()->
397 getIndex() == node) {
399 //cerr << "Found the node " << node << endl;
413 bool FGTrafficRecord::isActive(int margin)
415 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
416 time_t deptime = aircraft->getTrafficRef()->getDepartureTime();
417 return ((now + margin) > deptime);
421 void FGTrafficRecord::setSpeedAdjustment(double spd)
423 instruction.setChangeSpeed(true);
424 instruction.setSpeed(spd);
427 void FGTrafficRecord::setHeadingAdjustment(double heading)
429 instruction.setChangeHeading(true);
430 instruction.setHeading(heading);
433 bool FGTrafficRecord::pushBackAllowed()
435 return allowPushback;
441 /***************************************************************************
444 **************************************************************************/
445 FGATCInstruction::FGATCInstruction()
448 holdPosition = false;
450 changeHeading = false;
451 changeAltitude = false;
452 resolveCircularWait = false;
460 bool FGATCInstruction::hasInstruction()
462 return (holdPattern || holdPosition || changeSpeed || changeHeading
463 || changeAltitude || resolveCircularWait);
466 /***************************************************************************
469 **************************************************************************/
474 FGATCController::FGATCController()
476 //cerr << "running FGATController constructor" << endl;
479 lastTransmission = 0;
483 FGATCController::~FGATCController()
485 //cerr << "running FGATController destructor" << endl;
488 string FGATCController::getGateName(FGAIAircraft * ref)
490 return ref->atGate();
493 bool FGATCController::isUserAircraft(FGAIAircraft* ac)
495 return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false;
498 void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent, AtcMsgId msgId,
499 AtcMsgDir msgDir, bool audible)
501 string sender, receiver;
506 string atisInformation;
515 string transponderCode;
518 string instructionText;
522 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
523 if (rec->getAircraft()->getTaxiClearanceRequest()) {
524 instructionText = "push-back and taxi";
526 instructionText = "taxi";
528 //cerr << "transmitting for: " << sender << "Leg = " << rec->getLeg() << endl;
529 switch (rec->getLeg()) {
532 freqId = rec->getNextFrequency();
534 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
535 getDynamics()->getGroundFrequency(rec->getLeg() + freqId);
537 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
538 getDynamics()->getGroundFrequency(2);
540 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
541 getDynamics()->getTowerFrequency(2);
543 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
544 getName() + "-Ground";
546 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
547 getDynamics()->getAtisSequence();
551 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
552 getName() + "-Tower";
555 // Swap sender and receiver value in case of a ground to air transmission
556 if (msgDir == ATC_GROUND_TO_AIR) {
563 case MSG_ANNOUNCE_ENGINE_START:
564 text = sender + ". Ready to Start up";
566 case MSG_REQUEST_ENGINE_START:
568 receiver + ", This is " + sender + ". Position " +
569 getGateName(rec->getAircraft()) + ". Information " +
570 atisInformation + ". " +
571 rec->getAircraft()->getTrafficRef()->getFlightRules() +
573 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->
574 getName() + ". Request start-up";
576 // Acknowledge engine startup permission
577 // Assign departure runway
578 // Assign SID, if necessery (TODO)
579 case MSG_PERMIT_ENGINE_START:
580 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
582 heading = rec->getAircraft()->getTrafficRef()->getCourse();
583 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
585 rec->getAircraft()->GetFlightPlan()->
586 getRunwayClassFromTrafficType(fltType);
588 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
589 getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
591 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
592 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
593 getDynamics()->getSID(activeRunway, heading);
594 rec->getAircraft()->GetFlightPlan()->setSID(fp);
596 SID = fp->getName() + " departure";
598 SID = "fly runway heading ";
600 //snprintf(buffer, 7, "%3.2f", heading);
601 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
602 transponderCode = genTransponderCode(fltRules);
603 rec->getAircraft()->SetTransponderCode(transponderCode);
605 receiver + ". Start-up approved. " + atisInformation +
606 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
607 transponderCode + ". " +
608 "For "+ instructionText + " clearance call " + taxiFreqStr + ". " +
609 sender + " control.";
611 case MSG_DENY_ENGINE_START:
612 text = receiver + ". Standby";
614 case MSG_ACKNOWLEDGE_ENGINE_START:
615 fp = rec->getAircraft()->GetFlightPlan()->getSID();
618 rec->getAircraft()->GetFlightPlan()->getSID()->getName() +
621 SID = "fly runway heading ";
623 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
624 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
625 transponderCode = rec->getAircraft()->GetTransponderCode();
628 receiver + ". Start-up approved. " + atisInformation +
629 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
630 transponderCode + ". " +
631 "For " + instructionText + " clearance call " + taxiFreqStr + ". " +
634 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
635 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
636 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
638 case MSG_INITIATE_CONTACT:
639 text = receiver + ". With you. " + sender;
641 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
642 text = receiver + ". Roger. " + sender;
644 case MSG_REQUEST_PUSHBACK_CLEARANCE:
645 if (rec->getAircraft()->getTaxiClearanceRequest()) {
646 text = receiver + ". Request push-back. " + sender;
648 text = receiver + ". Request Taxi clearance. " + sender;
651 case MSG_PERMIT_PUSHBACK_CLEARANCE:
652 if (rec->getAircraft()->getTaxiClearanceRequest()) {
653 text = receiver + ". Push-back approved. " + sender;
655 text = receiver + ". Cleared to Taxi." + sender;
658 case MSG_HOLD_PUSHBACK_CLEARANCE:
659 text = receiver + ". Standby. " + sender;
661 case MSG_REQUEST_TAXI_CLEARANCE:
662 text = receiver + ". Ready to Taxi. " + sender;
664 case MSG_ISSUE_TAXI_CLEARANCE:
665 text = receiver + ". Cleared to taxi. " + sender;
667 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
668 text = receiver + ". Cleared to taxi. " + sender;
670 case MSG_HOLD_POSITION:
671 text = receiver + ". Hold Position. " + sender;
673 case MSG_ACKNOWLEDGE_HOLD_POSITION:
674 text = receiver + ". Holding Position. " + sender;
676 case MSG_RESUME_TAXI:
677 text = receiver + ". Resume Taxiing. " + sender;
679 case MSG_ACKNOWLEDGE_RESUME_TAXI:
680 text = receiver + ". Continuing Taxi. " + sender;
682 case MSG_REPORT_RUNWAY_HOLD_SHORT:
683 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
684 //activeRunway = "test";
685 text = receiver + ". Holding short runway "
689 //cerr << "1 Currently at leg " << rec->getLeg() << endl;
691 case MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT:
692 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
693 text = receiver + "Roger. Holding short runway "
697 //cerr << "2 Currently at leg " << rec->getLeg() << endl;
699 case MSG_SWITCH_TOWER_FREQUENCY:
700 towerFreqStr = formatATCFrequency3_2(towerFreq);
701 text = receiver + "Contact Tower at " + towerFreqStr + ". " + sender;
703 //cerr << "3 Currently at leg " << rec->getLeg() << endl;
705 case MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY:
706 towerFreqStr = formatATCFrequency3_2(towerFreq);
707 text = receiver + "Roger, switching to tower at " + towerFreqStr + ". " + sender;
709 //cerr << "4 Currently at leg " << rec->getLeg() << endl;
713 text = text + sender + ". Transmitting unknown Message";
717 double onBoardRadioFreq0 =
718 fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
719 double onBoardRadioFreq1 =
720 fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
721 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
722 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
723 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << endl;
725 // Display ATC message only when one of the radios is tuned
726 // the relevant frequency.
727 // Note that distance attenuation is currently not yet implemented
729 if ((stationFreq > 0)&&
730 ((onBoardRadioFreqI0 == stationFreq)||
731 (onBoardRadioFreqI1 == stationFreq))) {
732 if (rec->allowTransmissions()) {
734 if( fgGetBool( "/sim/radio/use-itm-attenuation", false ) ) {
735 //cerr << "Using ITM radio propagation" << endl;
736 FGRadioTransmission* radio = new FGRadioTransmission();
738 double sender_alt_ft, sender_alt;
740 sender_alt_ft = parent->getElevation();
741 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
742 sender_pos= SGGeod::fromDegM( parent->getLongitude(),
743 parent->getLatitude(), sender_alt );
746 sender_alt_ft = rec->getAltitude();
747 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
748 sender_pos= SGGeod::fromDegM( rec->getLongitude(),
749 rec->getLatitude(), sender_alt );
751 double frequency = ((double)stationFreq) / 100;
752 radio->receiveATC(sender_pos, frequency, text, ground_to_air);
756 fgSetString("/sim/messages/atc", text.c_str());
761 FGATCDialogNew::instance()->addEntry(1, text);
766 string FGATCController::formatATCFrequency3_2(int freq)
769 snprintf(buffer, 7, "%3.2f", ((float) freq / 100.0));
770 return string(buffer);
773 // TODO: Set transponder codes according to real-world routes.
774 // The current version just returns a random string of four octal numbers.
775 string FGATCController::genTransponderCode(string fltRules)
777 if (fltRules == "VFR") {
778 return string("1200");
781 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8,
783 return string(buffer);
787 void FGATCController::init()
790 FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
791 mgr->addController(this);
796 /***************************************************************************
797 * class FGTowerController
799 **************************************************************************/
800 FGTowerController::FGTowerController(FGAirportDynamics *par) :
807 void FGTowerController::announcePosition(int id,
808 FGAIFlightPlan * intendedRoute,
809 int currentPosition, double lat,
810 double lon, double heading,
811 double speed, double alt,
812 double radius, int leg,
816 TrafficVectorIterator i = activeTraffic.begin();
817 // Search whether the current id alread has an entry
818 // This might be faster using a map instead of a vector, but let's start by taking a safe route
819 if (activeTraffic.size()) {
820 //while ((i->getId() != id) && i != activeTraffic.end()) {
821 while (i != activeTraffic.end()) {
822 if (i->getId() == id) {
828 // Add a new TrafficRecord if no one exsists for this aircraft.
829 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
833 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
834 rec.setRunway(intendedRoute->getRunway());
836 //rec.setCallSign(callsign);
837 rec.setRadius(radius);
838 rec.setAircraft(ref);
839 activeTraffic.push_back(rec);
840 // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
841 ActiveRunwayVecIterator rwy = activeRunways.begin();
842 if (activeRunways.size()) {
843 while (rwy != activeRunways.end()) {
844 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
850 if (rwy == activeRunways.end()) {
851 ActiveRunway aRwy(intendedRoute->getRunway(), id);
852 aRwy.addToDepartureCue(ref);
853 activeRunways.push_back(aRwy);
854 rwy = (activeRunways.end()-1);
856 rwy->addToDepartureCue(ref);
859 //cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() << " for takeoff " << endl;
861 i->setPositionAndHeading(lat, lon, heading, speed, alt);
865 void FGTowerController::updateAircraftInformation(int id, double lat, double lon,
866 double heading, double speed, double alt,
869 TrafficVectorIterator i = activeTraffic.begin();
870 // Search whether the current id has an entry
871 // This might be faster using a map instead of a vector, but let's start by taking a safe route
872 TrafficVectorIterator current, closest;
873 if (activeTraffic.size()) {
874 //while ((i->getId() != id) && i != activeTraffic.end()) {
875 while (i != activeTraffic.end()) {
876 if (i->getId() == id) {
882 // // update position of the current aircraft
883 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
884 SG_LOG(SG_ATC, SG_ALERT,
885 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
887 i->setPositionAndHeading(lat, lon, heading, speed, alt);
892 // see if we already have a clearance record for the currently active runway
893 // NOTE: dd. 2011-08-07: Because the active runway has been constructed in the announcePosition function, we may safely assume that is
894 // already exists here. So, we can simplify the current code.
896 ActiveRunwayVecIterator rwy = activeRunways.begin();
897 //if (parent->getId() == fgGetString("/sim/presets/airport-id")) {
898 // for (rwy = activeRunways.begin(); rwy != activeRunways.end(); rwy++) {
899 // rwy->printDepartureCue();
903 rwy = activeRunways.begin();
904 while (rwy != activeRunways.end()) {
905 if (rwy->getRunwayName() == current->getRunway()) {
911 // only bother running the following code if the current aircraft is the
912 // first in line for depature
913 /* if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
914 if (rwy->getCleared()) {
915 if (id == rwy->getCleared()) {
916 current->setHoldPosition(false);
918 current->setHoldPosition(true);
921 // For now. At later stages, this will probably be the place to check for inbound traffc.
925 // only bother with aircraft that have a takeoff status of 2, since those are essentially under tower control
926 FGAIAircraft* ac= rwy->getFirstAircraftInDepartureCue();
927 if (ac->getTakeOffStatus() == 1) {
928 ac->setTakeOffStatus(2);
930 if (current->getAircraft()->getTakeOffStatus() == 2) {
931 current -> setHoldPosition(false);
933 current->setHoldPosition(true);
935 int clearanceId = rwy->getCleared();
937 if (id == clearanceId) {
938 current->setHoldPosition(false);
941 if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
943 FGAIAircraft *ac = rwy->getFirstOfStatus(1);
945 ac->setTakeOffStatus(2);
951 void FGTowerController::signOff(int id)
953 TrafficVectorIterator i = activeTraffic.begin();
954 // Search search if the current id alread has an entry
955 // This might be faster using a map instead of a vector, but let's start by taking a safe route
956 if (activeTraffic.size()) {
957 //while ((i->getId() != id) && i != activeTraffic.end()) {
958 while (i != activeTraffic.end()) {
959 if (i->getId() == id) {
965 // If this aircraft has left the runway, we can clear the departure record for this runway
966 ActiveRunwayVecIterator rwy = activeRunways.begin();
967 if (activeRunways.size()) {
968 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
969 while (rwy != activeRunways.end()) {
970 if (rwy->getRunwayName() == i->getRunway()) {
975 if (rwy != activeRunways.end()) {
977 rwy->updateDepartureCue();
979 SG_LOG(SG_ATC, SG_ALERT,
980 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff at " << SG_ORIGIN);
983 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
984 SG_LOG(SG_ATC, SG_ALERT,
985 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
987 i->getAircraft()->resetTakeOffStatus();
988 i = activeTraffic.erase(i);
989 //cerr << "Signing off from tower controller" << endl;
994 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
995 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
996 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
997 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
998 // Note that this function is probably obsolete
999 bool FGTowerController::hasInstruction(int id)
1001 TrafficVectorIterator i = activeTraffic.begin();
1002 // Search search if the current id has an entry
1003 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1004 if (activeTraffic.size()) {
1005 //while ((i->getId() != id) && i != activeTraffic.end()) {
1006 while (i != activeTraffic.end()) {
1007 if (i->getId() == id) {
1013 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1014 SG_LOG(SG_ATC, SG_ALERT,
1015 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1017 return i->hasInstruction();
1023 FGATCInstruction FGTowerController::getInstruction(int id)
1025 TrafficVectorIterator i = activeTraffic.begin();
1026 // Search search if the current id has an entry
1027 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1028 if (activeTraffic.size()) {
1029 //while ((i->getId() != id) && i != activeTraffic.end()) {
1030 while (i != activeTraffic.end()) {
1031 if (i->getId() == id) {
1037 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1038 SG_LOG(SG_ATC, SG_ALERT,
1039 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1041 return i->getInstruction();
1043 return FGATCInstruction();
1046 void FGTowerController::render(bool visible) {
1047 //cerr << "FGTowerController::render function not yet implemented" << endl;
1050 string FGTowerController::getName() {
1051 return string(parent->getId() + "-tower");
1054 void FGTowerController::update(double dt)
1061 /***************************************************************************
1062 * class FGStartupController
1064 **************************************************************************/
1065 FGStartupController::FGStartupController(FGAirportDynamics *par):
1071 void FGStartupController::announcePosition(int id,
1072 FGAIFlightPlan * intendedRoute,
1073 int currentPosition, double lat,
1074 double lon, double heading,
1075 double speed, double alt,
1076 double radius, int leg,
1080 TrafficVectorIterator i = activeTraffic.begin();
1081 // Search whether the current id alread has an entry
1082 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1083 if (activeTraffic.size()) {
1084 //while ((i->getId() != id) && i != activeTraffic.end()) {
1085 while (i != activeTraffic.end()) {
1086 if (i->getId() == id) {
1092 // Add a new TrafficRecord if no one exsists for this aircraft.
1093 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1094 FGTrafficRecord rec;
1097 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1098 rec.setRunway(intendedRoute->getRunway());
1100 rec.setPositionAndIntentions(currentPosition, intendedRoute);
1101 //rec.setCallSign(callsign);
1102 rec.setAircraft(ref);
1103 rec.setHoldPosition(true);
1104 activeTraffic.push_back(rec);
1106 i->setPositionAndIntentions(currentPosition, intendedRoute);
1107 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1113 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
1114 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1115 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1116 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1117 // Note that this function is probably obsolete
1118 bool FGStartupController::hasInstruction(int id)
1120 TrafficVectorIterator i = activeTraffic.begin();
1121 // Search search if the current id has an entry
1122 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1123 if (activeTraffic.size()) {
1124 //while ((i->getId() != id) && i != activeTraffic.end()) {
1125 while (i != activeTraffic.end()) {
1126 if (i->getId() == id) {
1132 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1133 SG_LOG(SG_ATC, SG_ALERT,
1134 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1136 return i->hasInstruction();
1142 FGATCInstruction FGStartupController::getInstruction(int id)
1144 TrafficVectorIterator i = activeTraffic.begin();
1145 // Search search if the current id has an entry
1146 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1147 if (activeTraffic.size()) {
1148 //while ((i->getId() != id) && i != activeTraffic.end()) {
1149 while (i != activeTraffic.end()) {
1150 if (i->getId() == id) {
1156 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1157 SG_LOG(SG_ATC, SG_ALERT,
1158 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1160 return i->getInstruction();
1162 return FGATCInstruction();
1165 void FGStartupController::signOff(int id)
1167 TrafficVectorIterator i = activeTraffic.begin();
1168 // Search search if the current id alread has an entry
1169 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1170 if (activeTraffic.size()) {
1171 //while ((i->getId() != id) && i != activeTraffic.end()) {
1172 while (i != activeTraffic.end()) {
1173 if (i->getId() == id) {
1179 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1180 SG_LOG(SG_ATC, SG_ALERT,
1181 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
1183 //cerr << i->getAircraft()->getCallSign() << " signing off from startupcontroller" << endl;
1184 i = activeTraffic.erase(i);
1188 bool FGStartupController::checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
1191 int state = i->getState();
1192 if ((state == st) && available) {
1193 if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
1195 //cerr << "Checking state " << st << " for " << i->getAircraft()->getCallSign() << endl;
1196 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
1197 int n = trans_num->getIntValue();
1199 trans_num->setIntValue(-1);
1200 // PopupCallback(n);
1201 //cerr << "Selected transmission message " << n << endl;
1202 FGATCDialogNew::instance()->removeEntry(1);
1204 //cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
1205 transmit(&(*i), &(*parent), msgId, msgDir, false);
1209 if (now > startTime) {
1210 //cerr << "Transmitting startup msg" << endl;
1211 transmit(&(*i), &(*parent), msgId, msgDir, true);
1213 lastTransmission = now;
1221 void FGStartupController::updateAircraftInformation(int id, double lat, double lon,
1222 double heading, double speed, double alt,
1225 TrafficVectorIterator i = activeTraffic.begin();
1226 // Search search if the current id has an entry
1227 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1228 TrafficVectorIterator current, closest;
1229 if (activeTraffic.size()) {
1230 //while ((i->getId() != id) && i != activeTraffic.end()) {
1231 while (i != activeTraffic.end()) {
1232 if (i->getId() == id) {
1238 // // update position of the current aircraft
1240 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1241 SG_LOG(SG_ATC, SG_ALERT,
1242 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1244 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1247 setDt(getDt() + dt);
1249 int state = i->getState();
1251 // The user controlled aircraft should have crased here, because it doesn't have a traffic reference.
1252 // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight.
1253 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
1254 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1255 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
1256 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
1257 // << " at parking " << getGateName(i->getAircraft()) << endl;
1259 if ((now - lastTransmission) > 3 + (rand() % 15)) {
1263 checkTransmissionState(0, now, (startTime + 0 ), i, MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
1264 checkTransmissionState(1, now, (startTime + 60 ), i, MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
1265 checkTransmissionState(2, now, (startTime + 80 ), i, MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
1266 checkTransmissionState(3, now, (startTime + 100), i, MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
1267 if (checkTransmissionState(4, now, (startTime + 130), i, MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND)) {
1270 checkTransmissionState(5, now, (startTime + 140), i, MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
1271 checkTransmissionState(6, now, (startTime + 150), i, MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR);
1272 checkTransmissionState(7, now, (startTime + 180), i, MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
1276 if ((state == 8) && available) {
1277 if (now > startTime + 200) {
1278 if (i->pushBackAllowed()) {
1279 i->allowRepeatedTransmissions();
1280 transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE,
1281 ATC_GROUND_TO_AIR, true);
1284 transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE,
1285 ATC_GROUND_TO_AIR, true);
1286 i->suppressRepeatedTransmissions();
1288 lastTransmission = now;
1292 if ((state == 9) && available) {
1293 i->setHoldPosition(false);
1297 // Note that this function is copied from simgear. for maintanance purposes, it's probabtl better to make a general function out of that.
1298 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
1299 double lon, double elev, double hdg, double slope)
1301 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
1302 obj_pos = makeZUpFrame(geod);
1303 // hdg is not a compass heading, but a counter-clockwise rotation
1304 // around the Z axis
1305 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
1307 obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS,
1312 void FGStartupController::render(bool visible)
1315 SGMaterialLib *matlib = globals->get_matlib();
1318 globals->get_scenery()->get_scene_graph()->removeChild(group);
1319 //while (group->getNumChildren()) {
1320 // cerr << "Number of children: " << group->getNumChildren() << endl;
1321 //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
1322 //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0);
1323 //geode->releaseGLObjects();
1324 //group->removeChild(geode);
1329 group = new osg::Group;
1330 FGScenery * local_scenery = globals->get_scenery();
1331 //double elevation_meters = 0.0;
1332 //double elevation_feet = 0.0;
1335 //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
1337 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1338 for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) {
1339 if (i->isActive(300)) {
1340 // Handle start point
1341 int pos = i->getCurrentPosition();
1342 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "pos = " << pos << endl;
1344 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(pos);
1345 SGGeod start(SGGeod::fromDeg((i->getLongitude()), (i->getLatitude())));
1346 SGGeod end (SGGeod::fromDeg(segment->getEnd()->getLongitude(), segment->getEnd()->getLatitude()));
1348 double length = SGGeodesy::distanceM(start, end);
1349 //heading = SGGeodesy::headingDeg(start->getGeod(), end->getGeod());
1351 double az2, heading; //, distanceM;
1352 SGGeodesy::inverse(start, end, heading, az2, length);
1353 double coveredDistance = length * 0.5;
1355 SGGeodesy::direct(start, heading, coveredDistance, center, az2);
1356 //cerr << "Active Aircraft : Centerpoint = (" << center.getLatitudeDeg() << ", " << center.getLongitudeDeg() << "). Heading = " << heading << endl;
1357 ///////////////////////////////////////////////////////////////////////////////
1358 // Make a helper function out of this
1359 osg::Matrix obj_pos;
1360 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1361 obj_trans->setDataVariance(osg::Object::STATIC);
1362 // Experimental: Calculate slope here, based on length, and the individual elevations
1363 double elevationStart;
1364 if (isUserAircraft((i)->getAircraft())) {
1365 elevationStart = fgGetDouble("/position/ground-elev-m");
1367 elevationStart = ((i)->getAircraft()->_getAltitude() * SG_FEET_TO_METER);
1369 double elevationEnd = segment->getEnd()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1370 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1371 SGGeod center2 = end;
1372 center2.setElevationM(SG_MAX_ELEVATION_M);
1373 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1374 //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1375 //elevation_meters += 0.5;
1378 elevationEnd = parent->getElevation();
1380 segment->getEnd()->setElevation(elevationEnd);
1383 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1384 double elevDiff = elevationEnd - elevationStart;
1386 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1388 //cerr << "1. Using mean elevation : " << elevationMean << " and " << slope << endl;
1390 WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5 + dx, -(heading), slope );
1393 obj_trans->setMatrix( obj_pos );
1394 //osg::Vec3 center(0, 0, 0)
1396 float width = length /2.0;
1397 osg::Vec3 corner(-width, 0, 0.25f);
1398 osg::Vec3 widthVec(2*width + 1, 0, 0);
1399 osg::Vec3 heightVec(0, 1, 0);
1400 osg::Geometry* geometry;
1401 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1402 simgear::EffectGeode* geode = new simgear::EffectGeode;
1403 geode->setName("test");
1404 geode->addDrawable(geometry);
1405 //osg::Node *custom_obj;
1407 if (segment->hasBlock(now)) {
1408 mat = matlib->find("UnidirectionalTaperRed");
1410 mat = matlib->find("UnidirectionalTaperGreen");
1413 geode->setEffect(mat->get_effect());
1414 obj_trans->addChild(geode);
1415 // wire as much of the scene graph together as we can
1416 //->addChild( obj_trans );
1417 group->addChild( obj_trans );
1418 /////////////////////////////////////////////////////////////////////
1420 //cerr << "BIG FAT WARNING: current position is here : " << pos << endl;
1422 for (intVecIterator j = (i)->getIntentions().begin(); j != (i)->getIntentions().end(); j++) {
1423 osg::Matrix obj_pos;
1426 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "intention = " << k << endl;
1427 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1428 obj_trans->setDataVariance(osg::Object::STATIC);
1429 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(k);
1431 double elevationStart = segment->getStart()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1432 double elevationEnd = segment->getEnd ()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1433 if ((elevationStart == 0) || (elevationStart == parent->getElevation())) {
1434 SGGeod center2 = segment->getStart()->getGeod();
1435 center2.setElevationM(SG_MAX_ELEVATION_M);
1436 if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) {
1437 //elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5;
1438 //elevation_meters += 0.5;
1441 elevationStart = parent->getElevation();
1443 segment->getStart()->setElevation(elevationStart);
1445 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1446 SGGeod center2 = segment->getEnd()->getGeod();
1447 center2.setElevationM(SG_MAX_ELEVATION_M);
1448 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1449 //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1450 //elevation_meters += 0.5;
1453 elevationEnd = parent->getElevation();
1455 segment->getEnd()->setElevation(elevationEnd);
1458 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1459 double elevDiff = elevationEnd - elevationStart;
1460 double length = segment->getLength();
1461 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1463 //cerr << "2. Using mean elevation : " << elevationMean << " and " << slope << endl;
1466 WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), elevationMean + 0.5 + dx, -(segment->getHeading()), slope );
1468 //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
1470 obj_trans->setMatrix( obj_pos );
1471 //osg::Vec3 center(0, 0, 0)
1473 float width = segment->getLength() /2.0;
1474 osg::Vec3 corner(-width, 0, 0.25f);
1475 osg::Vec3 widthVec(2*width + 1, 0, 0);
1476 osg::Vec3 heightVec(0, 1, 0);
1477 osg::Geometry* geometry;
1478 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1479 simgear::EffectGeode* geode = new simgear::EffectGeode;
1480 geode->setName("test");
1481 geode->addDrawable(geometry);
1482 //osg::Node *custom_obj;
1484 if (segment->hasBlock(now)) {
1485 mat = matlib->find("UnidirectionalTaperRed");
1487 mat = matlib->find("UnidirectionalTaperGreen");
1490 geode->setEffect(mat->get_effect());
1491 obj_trans->addChild(geode);
1492 // wire as much of the scene graph together as we can
1493 //->addChild( obj_trans );
1494 group->addChild( obj_trans );
1496 //cerr << "BIG FAT WARNING: k is here : " << pos << endl;
1502 globals->get_scenery()->get_scene_graph()->addChild(group);
1506 string FGStartupController::getName() {
1507 return string(parent->getId() + "-startup");
1510 void FGStartupController::update(double dt)
1517 /***************************************************************************
1518 * class FGApproachController
1520 **************************************************************************/
1521 FGApproachController::FGApproachController(FGAirportDynamics *par):
1528 void FGApproachController::announcePosition(int id,
1529 FGAIFlightPlan * intendedRoute,
1530 int currentPosition,
1531 double lat, double lon,
1532 double heading, double speed,
1533 double alt, double radius,
1534 int leg, FGAIAircraft * ref)
1537 TrafficVectorIterator i = activeTraffic.begin();
1538 // Search whether the current id alread has an entry
1539 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1540 if (activeTraffic.size()) {
1541 //while ((i->getId() != id) && i != activeTraffic.end()) {
1542 while (i != activeTraffic.end()) {
1543 if (i->getId() == id) {
1549 // Add a new TrafficRecord if no one exsists for this aircraft.
1550 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1551 FGTrafficRecord rec;
1554 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1555 rec.setRunway(intendedRoute->getRunway());
1557 //rec.setCallSign(callsign);
1558 rec.setAircraft(ref);
1559 activeTraffic.push_back(rec);
1561 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1565 void FGApproachController::updateAircraftInformation(int id, double lat, double lon,
1566 double heading, double speed, double alt,
1569 TrafficVectorIterator i = activeTraffic.begin();
1570 // Search search if the current id has an entry
1571 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1572 TrafficVectorIterator current, closest;
1573 if (activeTraffic.size()) {
1574 //while ((i->getId() != id) && i != activeTraffic.end()) {
1575 while (i != activeTraffic.end()) {
1576 if (i->getId() == id) {
1582 // // update position of the current aircraft
1583 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1584 SG_LOG(SG_ATC, SG_ALERT,
1585 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1587 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1589 //cerr << "ApproachController: checking for speed" << endl;
1591 current->getAircraft()->
1592 checkForArrivalTime(string("final001"));
1593 if (time_diff > 15) {
1594 current->setSpeedAdjustment(current->getAircraft()->
1595 getPerformance()->vDescent() *
1597 } else if (time_diff > 5) {
1598 current->setSpeedAdjustment(current->getAircraft()->
1599 getPerformance()->vDescent() *
1601 } else if (time_diff < -15) {
1602 current->setSpeedAdjustment(current->getAircraft()->
1603 getPerformance()->vDescent() *
1605 } else if (time_diff < -5) {
1606 current->setSpeedAdjustment(current->getAircraft()->
1607 getPerformance()->vDescent() *
1610 current->clearSpeedAdjustment();
1612 //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff);
1614 setDt(getDt() + dt);
1617 void FGApproachController::signOff(int id)
1619 TrafficVectorIterator i = activeTraffic.begin();
1620 // Search search if the current id alread has an entry
1621 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1622 if (activeTraffic.size()) {
1623 //while ((i->getId() != id) && i != activeTraffic.end()) {
1624 while (i != activeTraffic.end()) {
1625 if (i->getId() == id) {
1631 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1632 SG_LOG(SG_ATC, SG_ALERT,
1633 "AI error: Aircraft without traffic record is signing off from approach at " << SG_ORIGIN);
1635 i = activeTraffic.erase(i);
1639 void FGApproachController::update(double dt)
1646 bool FGApproachController::hasInstruction(int id)
1648 TrafficVectorIterator i = activeTraffic.begin();
1649 // Search search if the current id has an entry
1650 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1651 if (activeTraffic.size()) {
1652 //while ((i->getId() != id) && i != activeTraffic.end()) {
1653 while (i != activeTraffic.end()) {
1654 if (i->getId() == id) {
1660 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1661 SG_LOG(SG_ATC, SG_ALERT,
1662 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1664 return i->hasInstruction();
1670 FGATCInstruction FGApproachController::getInstruction(int id)
1672 TrafficVectorIterator i = activeTraffic.begin();
1673 // Search search if the current id has an entry
1674 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1675 if (activeTraffic.size()) {
1676 //while ((i->getId() != id) && i != activeTraffic.end()) {
1677 while (i != activeTraffic.end()) {
1678 if (i->getId() == id) {
1684 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1685 SG_LOG(SG_ATC, SG_ALERT,
1686 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1688 return i->getInstruction();
1690 return FGATCInstruction();
1694 ActiveRunway *FGApproachController::getRunway(string name)
1696 ActiveRunwayVecIterator rwy = activeRunways.begin();
1697 if (activeRunways.size()) {
1698 while (rwy != activeRunways.end()) {
1699 if (rwy->getRunwayName() == name) {
1705 if (rwy == activeRunways.end()) {
1706 ActiveRunway aRwy(name, 0);
1707 activeRunways.push_back(aRwy);
1708 rwy = activeRunways.end() - 1;
1713 void FGApproachController::render(bool visible) {
1714 //cerr << "FGApproachController::render function not yet implemented" << endl;
1719 string FGApproachController::getName() {
1720 return string(parent->getId() + "-approach");