1 // trafficrecord.cxx - Implementation of AIModels ATC code.
3 // Written by Durk Talsma, started September 2006.
5 // Copyright (C) 2006 Durk Talsma.
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <osg/Geometry>
31 #include <osg/MatrixTransform>
34 #include <simgear/scene/material/EffectGeode.hxx>
35 #include <simgear/scene/material/matlib.hxx>
36 #include <simgear/scene/material/mat.hxx>
37 #include <Scenery/scenery.hxx>
39 #include "trafficcontrol.hxx"
40 #include "atc_mgr.hxx"
41 #include <AIModel/AIAircraft.hxx>
42 #include <AIModel/AIFlightPlan.hxx>
43 #include <AIModel/performancedata.hxx>
44 #include <AIModel/performancedb.hxx>
45 #include <ATC/atc_mgr.hxx>
46 #include <Traffic/TrafficMgr.hxx>
47 #include <Airports/groundnetwork.hxx>
48 #include <Airports/dynamics.hxx>
49 #include <Airports/simple.hxx>
50 #include <Radio/radio.hxx>
55 /***************************************************************************
57 **************************************************************************/
58 time_t ActiveRunway::requestTimeSlot(time_t eta)
61 time_t separation = 90;
63 if (estimatedArrivalTimes.size() == 0) {
64 estimatedArrivalTimes.push_back(eta);
67 TimeVectorIterator i = estimatedArrivalTimes.begin();
68 //cerr << "Checking eta slots " << eta << ": " << endl;
69 for (i = estimatedArrivalTimes.begin();
70 i != estimatedArrivalTimes.end(); i++) {
71 //cerr << "Stored time : " << (*i) << endl;
73 i = estimatedArrivalTimes.begin();
74 if ((eta + separation) < (*i)) {
77 //cerr << "Storing at beginning" << endl;
79 while ((i != estimatedArrivalTimes.end()) && (!found)) {
80 TimeVectorIterator j = i + 1;
81 if (j == estimatedArrivalTimes.end()) {
82 if (((*i) + separation) < eta) {
83 //cerr << "Storing at end" << endl;
86 newEta = (*i) + separation;
87 //cerr << "Storing at end + separation" << endl;
90 if ((((*j) - (*i)) > (separation * 2))) { // found a potential slot
91 // now check whether this slot is usable:
92 // 1) eta should fall between the two points
93 // i.e. eta > i AND eta < j
95 //cerr << "Found potential slot after " << (*i) << endl;
96 if (eta > (*i) && (eta < (*j))) {
98 if (eta < ((*i) + separation)) {
99 newEta = (*i) + separation;
100 //cerr << "Using original" << (*i) << " + separation " << endl;
103 //cerr << "Using original after " << (*i) << endl;
105 } else if (eta < (*i)) {
107 newEta = (*i) + separation;
108 //cerr << "Using delayed slot after " << (*i) << endl;
111 if (((*j) - separation) < eta) {
113 if (((*i) + separation) < eta) {
115 cerr << "Using original after " << (*i) << endl;
117 newEta = (*i) + separation;
118 cerr << "Using " << (*i) << " + separation " << endl;
126 //cerr << ". done. New ETA : " << newEta << endl;
128 estimatedArrivalTimes.push_back(newEta);
129 sort(estimatedArrivalTimes.begin(), estimatedArrivalTimes.end());
130 // do some housekeeping : remove any timestamps that are past
131 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
132 TimeVectorIterator i = estimatedArrivalTimes.begin();
133 while (i != estimatedArrivalTimes.end()) {
135 //cerr << "Deleting timestamp " << (*i) << " (now = " << now << "). " << endl;
136 estimatedArrivalTimes.erase(i);
137 i = estimatedArrivalTimes.begin();
145 void ActiveRunway::printDepartureCue()
147 cout << "Departure cue for " << rwy << ": " << endl;
148 for (AircraftVecIterator atc = departureCue.begin(); atc != departureCue.end(); atc++) {
149 cout << " " << (*atc)->getCallSign() << " " << (*atc)->getTakeOffStatus();
150 cout << " " << (*atc)->_getLatitude() << " " << (*atc)->_getLongitude() << (*atc)-> getSpeed() << " " << (*atc)->getAltitude() << endl;
155 FGAIAircraft* ActiveRunway::getFirstOfStatus(int stat)
157 for (AircraftVecIterator atc =departureCue.begin(); atc != departureCue.end(); atc++) {
158 if ((*atc)->getTakeOffStatus() == stat)
166 /***************************************************************************
168 **************************************************************************/
169 FGTrafficRecord::FGTrafficRecord():
170 id(0), waitsForId(0),
175 allowTransmission(true),
178 latitude(0), longitude(0), heading(0), speed(0), altitude(0), radius(0)
182 void FGTrafficRecord::setPositionAndIntentions(int pos,
183 FGAIFlightPlan * route)
187 if (intentions.size()) {
188 intVecIterator i = intentions.begin();
190 SG_LOG(SG_ATC, SG_ALERT,
191 "Error in FGTrafficRecord::setPositionAndIntentions at " << SG_ORIGIN);
195 //FGAIFlightPlan::waypoint* const wpt= route->getCurrentWaypoint();
196 int size = route->getNrOfWayPoints();
197 //cerr << "Setting pos" << pos << " ";
198 //cerr << "setting intentions ";
199 for (int i = 2; i < size; i++) {
200 int val = route->getRouteIndex(i);
201 intentions.push_back(val);
206 * Check if another aircraft is ahead of the current one, and on the same
207 * return true / false is the is/isn't the case.
209 ****************************************************************************/
211 bool FGTrafficRecord::checkPositionAndIntentions(FGTrafficRecord & other)
214 //cerr << "Start check 1" << endl;
215 if (currentPos == other.currentPos) {
216 //cerr << callsign << ": Check Position and intentions: we are on the same taxiway" << other.callsign << "Index = " << currentPos << endl;
219 // else if (other.intentions.size())
221 // cerr << "Start check 2" << endl;
222 // intVecIterator i = other.intentions.begin();
223 // while (!((i == other.intentions.end()) || ((*i) == currentPos)))
225 // if (i != other.intentions.end()) {
226 // cerr << "Check Position and intentions: current matches other.intentions" << endl;
229 else if (intentions.size()) {
230 //cerr << "Start check 3" << endl;
231 intVecIterator i = intentions.begin();
232 //while (!((i == intentions.end()) || ((*i) == other.currentPos)))
233 while (i != intentions.end()) {
234 if ((*i) == other.currentPos) {
239 if (i != intentions.end()) {
240 //cerr << callsign << ": Check Position and intentions: .other.current matches" << other.callsign << "Index = " << (*i) << endl;
244 //cerr << "Done !!" << endl;
248 void FGTrafficRecord::setPositionAndHeading(double lat, double lon,
249 double hdg, double spd,
259 int FGTrafficRecord::crosses(FGGroundNetwork * net,
260 FGTrafficRecord & other)
262 if (checkPositionAndIntentions(other)
263 || (other.checkPositionAndIntentions(*this)))
266 int currentTargetNode = 0, otherTargetNode = 0;
268 currentTargetNode = net->findSegment(currentPos)->getEnd()->getIndex(); // OKAY,...
269 if (other.currentPos > 0)
270 otherTargetNode = net->findSegment(other.currentPos)->getEnd()->getIndex(); // OKAY,...
271 if ((currentTargetNode == otherTargetNode) && currentTargetNode > 0)
272 return currentTargetNode;
273 if (intentions.size()) {
274 for (i = intentions.begin(); i != intentions.end(); i++) {
276 if (currentTargetNode ==
277 net->findSegment(*i)->getEnd()->getIndex()) {
278 //cerr << "Current crosses at " << currentTargetNode <<endl;
279 return currentTargetNode;
284 if (other.intentions.size()) {
285 for (i = other.intentions.begin(); i != other.intentions.end();
288 if (otherTargetNode ==
289 net->findSegment(*i)->getEnd()->getIndex()) {
290 //cerr << "Other crosses at " << currentTargetNode <<endl;
291 return otherTargetNode;
296 if (intentions.size() && other.intentions.size()) {
297 for (i = intentions.begin(); i != intentions.end(); i++) {
298 for (j = other.intentions.begin(); j != other.intentions.end();
300 //cerr << "finding segment " << *i << " and " << *j << endl;
301 if (((*i) > 0) && ((*j) > 0)) {
303 net->findSegment(*i)->getEnd()->getIndex();
305 net->findSegment(*j)->getEnd()->getIndex();
306 if (currentTargetNode == otherTargetNode) {
307 //cerr << "Routes will cross at " << currentTargetNode << endl;
308 return currentTargetNode;
317 bool FGTrafficRecord::onRoute(FGGroundNetwork * net,
318 FGTrafficRecord & other)
320 int node = -1, othernode = -1;
322 node = net->findSegment(currentPos)->getEnd()->getIndex();
323 if (other.currentPos > 0)
325 net->findSegment(other.currentPos)->getEnd()->getIndex();
326 if ((node == othernode) && (node != -1))
328 if (other.intentions.size()) {
329 for (intVecIterator i = other.intentions.begin();
330 i != other.intentions.end(); i++) {
332 othernode = net->findSegment(*i)->getEnd()->getIndex();
333 if ((node == othernode) && (node > -1))
338 //if (other.currentPos > 0)
339 // othernode = net->findSegment(other.currentPos)->getEnd()->getIndex();
340 //if (intentions.size())
342 // for (intVecIterator i = intentions.begin(); i != intentions.end(); i++)
346 // node = net->findSegment(*i)->getEnd()->getIndex();
347 // if ((node == othernode) && (node > -1))
356 bool FGTrafficRecord::isOpposing(FGGroundNetwork * net,
357 FGTrafficRecord & other, int node)
359 // Check if current segment is the reverse segment for the other aircraft
361 //cerr << "Current segment " << currentPos << endl;
362 if ((currentPos > 0) && (other.currentPos > 0)) {
363 opp = net->findSegment(currentPos)->opposite();
365 if (opp->getIndex() == other.currentPos)
369 for (intVecIterator i = intentions.begin(); i != intentions.end();
371 if ((opp = net->findSegment(other.currentPos)->opposite())) {
373 if (opp->getIndex() ==
374 net->findSegment(*i)->getIndex()) {
375 if (net->findSegment(*i)->getStart()->getIndex() ==
378 //cerr << "Found the node " << node << endl;
384 if (other.intentions.size()) {
385 for (intVecIterator j = other.intentions.begin();
386 j != other.intentions.end(); j++) {
387 // cerr << "Current segment 1 " << (*i) << endl;
389 if ((opp = net->findSegment(*i)->opposite())) {
390 if (opp->getIndex() ==
391 net->findSegment(*j)->getIndex()) {
392 //cerr << "Nodes " << net->findSegment(*i)->getIndex()
393 // << " and " << net->findSegment(*j)->getIndex()
394 // << " are opposites " << endl;
395 if (net->findSegment(*i)->getStart()->
396 getIndex() == node) {
398 //cerr << "Found the node " << node << endl;
412 bool FGTrafficRecord::isActive(int margin)
414 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
415 time_t deptime = aircraft->getTrafficRef()->getDepartureTime();
416 return ((now + margin) > deptime);
420 void FGTrafficRecord::setSpeedAdjustment(double spd)
422 instruction.setChangeSpeed(true);
423 instruction.setSpeed(spd);
426 void FGTrafficRecord::setHeadingAdjustment(double heading)
428 instruction.setChangeHeading(true);
429 instruction.setHeading(heading);
432 bool FGTrafficRecord::pushBackAllowed()
434 return allowPushback;
440 /***************************************************************************
443 **************************************************************************/
444 FGATCInstruction::FGATCInstruction()
447 holdPosition = false;
449 changeHeading = false;
450 changeAltitude = false;
451 resolveCircularWait = false;
459 bool FGATCInstruction::hasInstruction()
461 return (holdPattern || holdPosition || changeSpeed || changeHeading
462 || changeAltitude || resolveCircularWait);
465 /***************************************************************************
468 **************************************************************************/
473 FGATCController::FGATCController()
475 //cerr << "running FGATController constructor" << endl;
478 lastTransmission = 0;
482 FGATCController::~FGATCController()
484 //cerr << "running FGATController destructor" << endl;
487 string FGATCController::getGateName(FGAIAircraft * ref)
489 return ref->atGate();
492 bool FGATCController::isUserAircraft(FGAIAircraft* ac)
494 return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false;
497 void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent, AtcMsgId msgId,
498 AtcMsgDir msgDir, bool audible)
500 string sender, receiver;
505 string atisInformation;
514 string transponderCode;
517 string instructionText;
521 sender = rec->getAircraft()->getTrafficRef()->getCallSign();
522 if (rec->getAircraft()->getTaxiClearanceRequest()) {
523 instructionText = "push-back and taxi";
525 instructionText = "taxi";
527 //cerr << "transmitting for: " << sender << "Leg = " << rec->getLeg() << endl;
528 switch (rec->getLeg()) {
531 freqId = rec->getNextFrequency();
533 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
534 getDynamics()->getGroundFrequency(rec->getLeg() + freqId);
536 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
537 getDynamics()->getGroundFrequency(2);
539 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
540 getDynamics()->getTowerFrequency(2);
542 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
543 getName() + "-Ground";
545 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
546 getDynamics()->getAtisSequence();
550 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
551 getName() + "-Tower";
554 // Swap sender and receiver value in case of a ground to air transmission
555 if (msgDir == ATC_GROUND_TO_AIR) {
562 case MSG_ANNOUNCE_ENGINE_START:
563 text = sender + ". Ready to Start up";
565 case MSG_REQUEST_ENGINE_START:
567 receiver + ", This is " + sender + ". Position " +
568 getGateName(rec->getAircraft()) + ". Information " +
569 atisInformation + ". " +
570 rec->getAircraft()->getTrafficRef()->getFlightRules() +
572 rec->getAircraft()->getTrafficRef()->getArrivalAirport()->
573 getName() + ". Request start-up";
575 // Acknowledge engine startup permission
576 // Assign departure runway
577 // Assign SID, if necessery (TODO)
578 case MSG_PERMIT_ENGINE_START:
579 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
581 heading = rec->getAircraft()->getTrafficRef()->getCourse();
582 fltType = rec->getAircraft()->getTrafficRef()->getFlightType();
584 rec->getAircraft()->GetFlightPlan()->
585 getRunwayClassFromTrafficType(fltType);
587 rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
588 getDynamics()->getActiveRunway(rwyClass, 1, activeRunway,
590 rec->getAircraft()->GetFlightPlan()->setRunway(activeRunway);
591 fp = rec->getAircraft()->getTrafficRef()->getDepartureAirport()->
592 getDynamics()->getSID(activeRunway, heading);
593 rec->getAircraft()->GetFlightPlan()->setSID(fp);
595 SID = fp->getName() + " departure";
597 SID = "fly runway heading ";
599 //snprintf(buffer, 7, "%3.2f", heading);
600 fltRules = rec->getAircraft()->getTrafficRef()->getFlightRules();
601 transponderCode = genTransponderCode(fltRules);
602 rec->getAircraft()->SetTransponderCode(transponderCode);
604 receiver + ". Start-up approved. " + atisInformation +
605 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
606 transponderCode + ". " +
607 "For "+ instructionText + " clearance call " + taxiFreqStr + ". " +
608 sender + " control.";
610 case MSG_DENY_ENGINE_START:
611 text = receiver + ". Standby";
613 case MSG_ACKNOWLEDGE_ENGINE_START:
614 fp = rec->getAircraft()->GetFlightPlan()->getSID();
617 rec->getAircraft()->GetFlightPlan()->getSID()->getName() +
620 SID = "fly runway heading ";
622 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
623 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
624 transponderCode = rec->getAircraft()->GetTransponderCode();
627 receiver + ". Start-up approved. " + atisInformation +
628 " correct, runway " + activeRunway + ", " + SID + ", squawk " +
629 transponderCode + ". " +
630 "For " + instructionText + " clearance call " + taxiFreqStr + ". " +
633 case MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY:
634 taxiFreqStr = formatATCFrequency3_2(taxiFreq);
635 text = receiver + ". Switching to " + taxiFreqStr + ". " + sender;
637 case MSG_INITIATE_CONTACT:
638 text = receiver + ". With you. " + sender;
640 case MSG_ACKNOWLEDGE_INITIATE_CONTACT:
641 text = receiver + ". Roger. " + sender;
643 case MSG_REQUEST_PUSHBACK_CLEARANCE:
644 if (rec->getAircraft()->getTaxiClearanceRequest()) {
645 text = receiver + ". Request push-back. " + sender;
647 text = receiver + ". Request Taxi clearance. " + sender;
650 case MSG_PERMIT_PUSHBACK_CLEARANCE:
651 if (rec->getAircraft()->getTaxiClearanceRequest()) {
652 text = receiver + ". Push-back approved. " + sender;
654 text = receiver + ". Cleared to Taxi." + sender;
657 case MSG_HOLD_PUSHBACK_CLEARANCE:
658 text = receiver + ". Standby. " + sender;
660 case MSG_REQUEST_TAXI_CLEARANCE:
661 text = receiver + ". Ready to Taxi. " + sender;
663 case MSG_ISSUE_TAXI_CLEARANCE:
664 text = receiver + ". Cleared to taxi. " + sender;
666 case MSG_ACKNOWLEDGE_TAXI_CLEARANCE:
667 text = receiver + ". Cleared to taxi. " + sender;
669 case MSG_HOLD_POSITION:
670 text = receiver + ". Hold Position. " + sender;
672 case MSG_ACKNOWLEDGE_HOLD_POSITION:
673 text = receiver + ". Holding Position. " + sender;
675 case MSG_RESUME_TAXI:
676 text = receiver + ". Resume Taxiing. " + sender;
678 case MSG_ACKNOWLEDGE_RESUME_TAXI:
679 text = receiver + ". Continuing Taxi. " + sender;
681 case MSG_REPORT_RUNWAY_HOLD_SHORT:
682 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
683 //activeRunway = "test";
684 text = receiver + ". Holding short runway "
688 //cerr << "1 Currently at leg " << rec->getLeg() << endl;
690 case MSG_ACKNOWLEDGE_REPORT_RUNWAY_HOLD_SHORT:
691 activeRunway = rec->getAircraft()->GetFlightPlan()->getRunway();
692 text = receiver + "Roger. Holding short runway "
696 //cerr << "2 Currently at leg " << rec->getLeg() << endl;
698 case MSG_SWITCH_TOWER_FREQUENCY:
699 towerFreqStr = formatATCFrequency3_2(towerFreq);
700 text = receiver + "Contact Tower at " + towerFreqStr + ". " + sender;
702 //cerr << "3 Currently at leg " << rec->getLeg() << endl;
704 case MSG_ACKNOWLEDGE_SWITCH_TOWER_FREQUENCY:
705 towerFreqStr = formatATCFrequency3_2(towerFreq);
706 text = receiver + "Roger, switching to tower at " + towerFreqStr + ". " + sender;
708 //cerr << "4 Currently at leg " << rec->getLeg() << endl;
712 text = text + sender + ". Transmitting unknown Message";
716 double onBoardRadioFreq0 =
717 fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
718 double onBoardRadioFreq1 =
719 fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
720 int onBoardRadioFreqI0 = (int) floor(onBoardRadioFreq0 * 100 + 0.5);
721 int onBoardRadioFreqI1 = (int) floor(onBoardRadioFreq1 * 100 + 0.5);
722 //cerr << "Using " << onBoardRadioFreq0 << ", " << onBoardRadioFreq1 << " and " << stationFreq << " for " << text << endl;
724 // Display ATC message only when one of the radios is tuned
725 // the relevant frequency.
726 // Note that distance attenuation is currently not yet implemented
728 if ((stationFreq > 0)&&
729 ((onBoardRadioFreqI0 == stationFreq)||
730 (onBoardRadioFreqI1 == stationFreq))) {
731 if (rec->allowTransmissions()) {
733 if( fgGetBool( "/sim/radio/use-itm-attenuation", false ) ) {
734 //cerr << "Using ITM radio propagation" << endl;
735 FGRadioTransmission* radio = new FGRadioTransmission();
737 double sender_alt_ft, sender_alt;
739 sender_alt_ft = parent->getElevation();
740 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
741 sender_pos= SGGeod::fromDegM( parent->getLongitude(),
742 parent->getLatitude(), sender_alt );
745 sender_alt_ft = rec->getAltitude();
746 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
747 sender_pos= SGGeod::fromDegM( rec->getLongitude(),
748 rec->getLatitude(), sender_alt );
750 double frequency = ((double)stationFreq) / 100;
751 radio->receiveATC(sender_pos, frequency, text, ground_to_air);
755 fgSetString("/sim/messages/atc", text.c_str());
760 FGATCDialogNew::instance()->addEntry(1, text);
765 string FGATCController::formatATCFrequency3_2(int freq)
768 snprintf(buffer, 7, "%3.2f", ((float) freq / 100.0));
769 return string(buffer);
772 // TODO: Set transponder codes according to real-world routes.
773 // The current version just returns a random string of four octal numbers.
774 string FGATCController::genTransponderCode(string fltRules)
776 if (fltRules == "VFR") {
777 return string("1200");
780 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8,
782 return string(buffer);
786 void FGATCController::init()
789 FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
790 mgr->addController(this);
795 /***************************************************************************
796 * class FGTowerController
798 **************************************************************************/
799 FGTowerController::FGTowerController(FGAirportDynamics *par) :
806 void FGTowerController::announcePosition(int id,
807 FGAIFlightPlan * intendedRoute,
808 int currentPosition, double lat,
809 double lon, double heading,
810 double speed, double alt,
811 double radius, int leg,
815 TrafficVectorIterator i = activeTraffic.begin();
816 // Search whether the current id alread has an entry
817 // This might be faster using a map instead of a vector, but let's start by taking a safe route
818 if (activeTraffic.size()) {
819 //while ((i->getId() != id) && i != activeTraffic.end()) {
820 while (i != activeTraffic.end()) {
821 if (i->getId() == id) {
827 // Add a new TrafficRecord if no one exsists for this aircraft.
828 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
832 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
833 rec.setRunway(intendedRoute->getRunway());
835 //rec.setCallSign(callsign);
836 rec.setRadius(radius);
837 rec.setAircraft(ref);
838 activeTraffic.push_back(rec);
839 // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
840 ActiveRunwayVecIterator rwy = activeRunways.begin();
841 if (activeRunways.size()) {
842 while (rwy != activeRunways.end()) {
843 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
849 if (rwy == activeRunways.end()) {
850 ActiveRunway aRwy(intendedRoute->getRunway(), id);
851 aRwy.addToDepartureCue(ref);
852 activeRunways.push_back(aRwy);
853 rwy = (activeRunways.end()-1);
855 rwy->addToDepartureCue(ref);
858 //cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() << " for takeoff " << endl;
860 i->setPositionAndHeading(lat, lon, heading, speed, alt);
864 void FGTowerController::updateAircraftInformation(int id, double lat, double lon,
865 double heading, double speed, double alt,
868 TrafficVectorIterator i = activeTraffic.begin();
869 // Search whether the current id has an entry
870 // This might be faster using a map instead of a vector, but let's start by taking a safe route
871 TrafficVectorIterator current, closest;
872 if (activeTraffic.size()) {
873 //while ((i->getId() != id) && i != activeTraffic.end()) {
874 while (i != activeTraffic.end()) {
875 if (i->getId() == id) {
881 // // update position of the current aircraft
882 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
883 SG_LOG(SG_ATC, SG_ALERT,
884 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
886 i->setPositionAndHeading(lat, lon, heading, speed, alt);
891 // see if we already have a clearance record for the currently active runway
892 // NOTE: dd. 2011-08-07: Because the active runway has been constructed in the announcePosition function, we may safely assume that is
893 // already exists here. So, we can simplify the current code.
895 ActiveRunwayVecIterator rwy = activeRunways.begin();
896 //if (parent->getId() == fgGetString("/sim/presets/airport-id")) {
897 // for (rwy = activeRunways.begin(); rwy != activeRunways.end(); rwy++) {
898 // rwy->printDepartureCue();
902 rwy = activeRunways.begin();
903 while (rwy != activeRunways.end()) {
904 if (rwy->getRunwayName() == current->getRunway()) {
910 // only bother running the following code if the current aircraft is the
911 // first in line for depature
912 /* if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
913 if (rwy->getCleared()) {
914 if (id == rwy->getCleared()) {
915 current->setHoldPosition(false);
917 current->setHoldPosition(true);
920 // For now. At later stages, this will probably be the place to check for inbound traffc.
924 // only bother with aircraft that have a takeoff status of 2, since those are essentially under tower control
925 FGAIAircraft* ac= rwy->getFirstAircraftInDepartureCue();
926 if (ac->getTakeOffStatus() == 1) {
927 ac->setTakeOffStatus(2);
929 if (current->getAircraft()->getTakeOffStatus() == 2) {
930 current -> setHoldPosition(false);
932 current->setHoldPosition(true);
934 int clearanceId = rwy->getCleared();
936 if (id == clearanceId) {
937 current->setHoldPosition(false);
940 if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
942 FGAIAircraft *ac = rwy->getFirstOfStatus(1);
944 ac->setTakeOffStatus(2);
950 void FGTowerController::signOff(int id)
952 TrafficVectorIterator i = activeTraffic.begin();
953 // Search search if the current id alread has an entry
954 // This might be faster using a map instead of a vector, but let's start by taking a safe route
955 if (activeTraffic.size()) {
956 //while ((i->getId() != id) && i != activeTraffic.end()) {
957 while (i != activeTraffic.end()) {
958 if (i->getId() == id) {
964 // If this aircraft has left the runway, we can clear the departure record for this runway
965 ActiveRunwayVecIterator rwy = activeRunways.begin();
966 if (activeRunways.size()) {
967 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
968 while (rwy != activeRunways.end()) {
969 if (rwy->getRunwayName() == i->getRunway()) {
974 if (rwy != activeRunways.end()) {
976 rwy->updateDepartureCue();
978 SG_LOG(SG_ATC, SG_ALERT,
979 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff at " << SG_ORIGIN);
982 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
983 SG_LOG(SG_ATC, SG_ALERT,
984 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
986 i->getAircraft()->resetTakeOffStatus();
987 i = activeTraffic.erase(i);
988 //cerr << "Signing off from tower controller" << endl;
993 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
994 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
995 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
996 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
997 // Note that this function is probably obsolete
998 bool FGTowerController::hasInstruction(int id)
1000 TrafficVectorIterator i = activeTraffic.begin();
1001 // Search search if the current id has an entry
1002 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1003 if (activeTraffic.size()) {
1004 //while ((i->getId() != id) && i != activeTraffic.end()) {
1005 while (i != activeTraffic.end()) {
1006 if (i->getId() == id) {
1012 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1013 SG_LOG(SG_ATC, SG_ALERT,
1014 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1016 return i->hasInstruction();
1022 FGATCInstruction FGTowerController::getInstruction(int id)
1024 TrafficVectorIterator i = activeTraffic.begin();
1025 // Search search if the current id has an entry
1026 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1027 if (activeTraffic.size()) {
1028 //while ((i->getId() != id) && i != activeTraffic.end()) {
1029 while (i != activeTraffic.end()) {
1030 if (i->getId() == id) {
1036 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1037 SG_LOG(SG_ATC, SG_ALERT,
1038 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1040 return i->getInstruction();
1042 return FGATCInstruction();
1045 void FGTowerController::render(bool visible) {
1046 //cerr << "FGTowerController::render function not yet implemented" << endl;
1049 string FGTowerController::getName() {
1050 return string(parent->getId() + "-tower");
1053 void FGTowerController::update(double dt)
1060 /***************************************************************************
1061 * class FGStartupController
1063 **************************************************************************/
1064 FGStartupController::FGStartupController(FGAirportDynamics *par):
1070 void FGStartupController::announcePosition(int id,
1071 FGAIFlightPlan * intendedRoute,
1072 int currentPosition, double lat,
1073 double lon, double heading,
1074 double speed, double alt,
1075 double radius, int leg,
1079 TrafficVectorIterator i = activeTraffic.begin();
1080 // Search whether the current id alread has an entry
1081 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1082 if (activeTraffic.size()) {
1083 //while ((i->getId() != id) && i != activeTraffic.end()) {
1084 while (i != activeTraffic.end()) {
1085 if (i->getId() == id) {
1091 // Add a new TrafficRecord if no one exsists for this aircraft.
1092 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1093 FGTrafficRecord rec;
1096 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1097 rec.setRunway(intendedRoute->getRunway());
1099 rec.setPositionAndIntentions(currentPosition, intendedRoute);
1100 //rec.setCallSign(callsign);
1101 rec.setAircraft(ref);
1102 rec.setHoldPosition(true);
1103 activeTraffic.push_back(rec);
1105 i->setPositionAndIntentions(currentPosition, intendedRoute);
1106 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1112 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
1113 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1114 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1115 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1116 // Note that this function is probably obsolete
1117 bool FGStartupController::hasInstruction(int id)
1119 TrafficVectorIterator i = activeTraffic.begin();
1120 // Search search if the current id has an entry
1121 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1122 if (activeTraffic.size()) {
1123 //while ((i->getId() != id) && i != activeTraffic.end()) {
1124 while (i != activeTraffic.end()) {
1125 if (i->getId() == id) {
1131 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1132 SG_LOG(SG_ATC, SG_ALERT,
1133 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1135 return i->hasInstruction();
1141 FGATCInstruction FGStartupController::getInstruction(int id)
1143 TrafficVectorIterator i = activeTraffic.begin();
1144 // Search search if the current id has an entry
1145 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1146 if (activeTraffic.size()) {
1147 //while ((i->getId() != id) && i != activeTraffic.end()) {
1148 while (i != activeTraffic.end()) {
1149 if (i->getId() == id) {
1155 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1156 SG_LOG(SG_ATC, SG_ALERT,
1157 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1159 return i->getInstruction();
1161 return FGATCInstruction();
1164 void FGStartupController::signOff(int id)
1166 TrafficVectorIterator i = activeTraffic.begin();
1167 // Search search if the current id alread has an entry
1168 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1169 if (activeTraffic.size()) {
1170 //while ((i->getId() != id) && i != activeTraffic.end()) {
1171 while (i != activeTraffic.end()) {
1172 if (i->getId() == id) {
1178 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1179 SG_LOG(SG_ATC, SG_ALERT,
1180 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
1182 //cerr << i->getAircraft()->getCallSign() << " signing off from startupcontroller" << endl;
1183 i = activeTraffic.erase(i);
1187 bool FGStartupController::checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
1190 int state = i->getState();
1191 if ((state == st) && available) {
1192 if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
1194 //cerr << "Checking state " << st << " for " << i->getAircraft()->getCallSign() << endl;
1195 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
1196 int n = trans_num->getIntValue();
1198 trans_num->setIntValue(-1);
1199 // PopupCallback(n);
1200 //cerr << "Selected transmission message " << n << endl;
1201 FGATCDialogNew::instance()->removeEntry(1);
1203 //cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
1204 transmit(&(*i), &(*parent), msgId, msgDir, false);
1208 if (now > startTime) {
1209 //cerr << "Transmitting startup msg" << endl;
1210 transmit(&(*i), &(*parent), msgId, msgDir, true);
1212 lastTransmission = now;
1220 void FGStartupController::updateAircraftInformation(int id, double lat, double lon,
1221 double heading, double speed, double alt,
1224 TrafficVectorIterator i = activeTraffic.begin();
1225 // Search search if the current id has an entry
1226 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1227 TrafficVectorIterator current, closest;
1228 if (activeTraffic.size()) {
1229 //while ((i->getId() != id) && i != activeTraffic.end()) {
1230 while (i != activeTraffic.end()) {
1231 if (i->getId() == id) {
1237 // // update position of the current aircraft
1239 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1240 SG_LOG(SG_ATC, SG_ALERT,
1241 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1243 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1246 setDt(getDt() + dt);
1248 int state = i->getState();
1250 // The user controlled aircraft should have crased here, because it doesn't have a traffic reference.
1251 // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight.
1252 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
1253 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1254 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
1255 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
1256 // << " at parking " << getGateName(i->getAircraft()) << endl;
1258 if ((now - lastTransmission) > 3 + (rand() % 15)) {
1262 checkTransmissionState(0, now, (startTime + 0 ), i, MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
1263 checkTransmissionState(1, now, (startTime + 60 ), i, MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
1264 checkTransmissionState(2, now, (startTime + 80 ), i, MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
1265 checkTransmissionState(3, now, (startTime + 100), i, MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
1266 if (checkTransmissionState(4, now, (startTime + 130), i, MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND)) {
1269 checkTransmissionState(5, now, (startTime + 140), i, MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
1270 checkTransmissionState(6, now, (startTime + 150), i, MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR);
1271 checkTransmissionState(7, now, (startTime + 180), i, MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
1275 if ((state == 8) && available) {
1276 if (now > startTime + 200) {
1277 if (i->pushBackAllowed()) {
1278 i->allowRepeatedTransmissions();
1279 transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE,
1280 ATC_GROUND_TO_AIR, true);
1283 transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE,
1284 ATC_GROUND_TO_AIR, true);
1285 i->suppressRepeatedTransmissions();
1287 lastTransmission = now;
1291 if ((state == 9) && available) {
1292 i->setHoldPosition(false);
1296 // Note that this function is copied from simgear. for maintanance purposes, it's probabtl better to make a general function out of that.
1297 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
1298 double lon, double elev, double hdg, double slope)
1300 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
1301 obj_pos = makeZUpFrame(geod);
1302 // hdg is not a compass heading, but a counter-clockwise rotation
1303 // around the Z axis
1304 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
1306 obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS,
1311 void FGStartupController::render(bool visible)
1314 SGMaterialLib *matlib = globals->get_matlib();
1317 globals->get_scenery()->get_scene_graph()->removeChild(group);
1318 //while (group->getNumChildren()) {
1319 // cerr << "Number of children: " << group->getNumChildren() << endl;
1320 //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
1321 //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0);
1322 //geode->releaseGLObjects();
1323 //group->removeChild(geode);
1328 group = new osg::Group;
1329 FGScenery * local_scenery = globals->get_scenery();
1330 //double elevation_meters = 0.0;
1331 //double elevation_feet = 0.0;
1334 //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
1336 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1337 for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) {
1338 if (i->isActive(300)) {
1339 // Handle start point
1340 int pos = i->getCurrentPosition();
1341 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "pos = " << pos << endl;
1343 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(pos);
1344 SGGeod start(SGGeod::fromDeg((i->getLongitude()), (i->getLatitude())));
1345 SGGeod end (SGGeod::fromDeg(segment->getEnd()->getLongitude(), segment->getEnd()->getLatitude()));
1347 double length = SGGeodesy::distanceM(start, end);
1348 //heading = SGGeodesy::headingDeg(start->getGeod(), end->getGeod());
1350 double az2, heading; //, distanceM;
1351 SGGeodesy::inverse(start, end, heading, az2, length);
1352 double coveredDistance = length * 0.5;
1354 SGGeodesy::direct(start, heading, coveredDistance, center, az2);
1355 //cerr << "Active Aircraft : Centerpoint = (" << center.getLatitudeDeg() << ", " << center.getLongitudeDeg() << "). Heading = " << heading << endl;
1356 ///////////////////////////////////////////////////////////////////////////////
1357 // Make a helper function out of this
1358 osg::Matrix obj_pos;
1359 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1360 obj_trans->setDataVariance(osg::Object::STATIC);
1361 // Experimental: Calculate slope here, based on length, and the individual elevations
1362 double elevationStart;
1363 if (isUserAircraft((i)->getAircraft())) {
1364 elevationStart = fgGetDouble("/position/ground-elev-m");
1366 elevationStart = ((i)->getAircraft()->_getAltitude() * SG_FEET_TO_METER);
1368 double elevationEnd = segment->getEnd()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1369 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1370 SGGeod center2 = end;
1371 center2.setElevationM(SG_MAX_ELEVATION_M);
1372 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1373 //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1374 //elevation_meters += 0.5;
1377 elevationEnd = parent->getElevation();
1379 segment->getEnd()->setElevation(elevationEnd);
1382 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1383 double elevDiff = elevationEnd - elevationStart;
1385 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1387 //cerr << "1. Using mean elevation : " << elevationMean << " and " << slope << endl;
1389 WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5 + dx, -(heading), slope );
1392 obj_trans->setMatrix( obj_pos );
1393 //osg::Vec3 center(0, 0, 0)
1395 float width = length /2.0;
1396 osg::Vec3 corner(-width, 0, 0.25f);
1397 osg::Vec3 widthVec(2*width + 1, 0, 0);
1398 osg::Vec3 heightVec(0, 1, 0);
1399 osg::Geometry* geometry;
1400 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1401 simgear::EffectGeode* geode = new simgear::EffectGeode;
1402 geode->setName("test");
1403 geode->addDrawable(geometry);
1404 //osg::Node *custom_obj;
1406 if (segment->hasBlock(now)) {
1407 mat = matlib->find("UnidirectionalTaperRed");
1409 mat = matlib->find("UnidirectionalTaperGreen");
1412 geode->setEffect(mat->get_effect());
1413 obj_trans->addChild(geode);
1414 // wire as much of the scene graph together as we can
1415 //->addChild( obj_trans );
1416 group->addChild( obj_trans );
1417 /////////////////////////////////////////////////////////////////////
1419 //cerr << "BIG FAT WARNING: current position is here : " << pos << endl;
1421 for (intVecIterator j = (i)->getIntentions().begin(); j != (i)->getIntentions().end(); j++) {
1422 osg::Matrix obj_pos;
1425 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "intention = " << k << endl;
1426 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1427 obj_trans->setDataVariance(osg::Object::STATIC);
1428 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(k);
1430 double elevationStart = segment->getStart()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1431 double elevationEnd = segment->getEnd ()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1432 if ((elevationStart == 0) || (elevationStart == parent->getElevation())) {
1433 SGGeod center2 = segment->getStart()->getGeod();
1434 center2.setElevationM(SG_MAX_ELEVATION_M);
1435 if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) {
1436 //elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5;
1437 //elevation_meters += 0.5;
1440 elevationStart = parent->getElevation();
1442 segment->getStart()->setElevation(elevationStart);
1444 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1445 SGGeod center2 = segment->getEnd()->getGeod();
1446 center2.setElevationM(SG_MAX_ELEVATION_M);
1447 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1448 //elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1449 //elevation_meters += 0.5;
1452 elevationEnd = parent->getElevation();
1454 segment->getEnd()->setElevation(elevationEnd);
1457 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1458 double elevDiff = elevationEnd - elevationStart;
1459 double length = segment->getLength();
1460 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1462 //cerr << "2. Using mean elevation : " << elevationMean << " and " << slope << endl;
1465 WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), elevationMean + 0.5 + dx, -(segment->getHeading()), slope );
1467 //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
1469 obj_trans->setMatrix( obj_pos );
1470 //osg::Vec3 center(0, 0, 0)
1472 float width = segment->getLength() /2.0;
1473 osg::Vec3 corner(-width, 0, 0.25f);
1474 osg::Vec3 widthVec(2*width + 1, 0, 0);
1475 osg::Vec3 heightVec(0, 1, 0);
1476 osg::Geometry* geometry;
1477 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1478 simgear::EffectGeode* geode = new simgear::EffectGeode;
1479 geode->setName("test");
1480 geode->addDrawable(geometry);
1481 //osg::Node *custom_obj;
1483 if (segment->hasBlock(now)) {
1484 mat = matlib->find("UnidirectionalTaperRed");
1486 mat = matlib->find("UnidirectionalTaperGreen");
1489 geode->setEffect(mat->get_effect());
1490 obj_trans->addChild(geode);
1491 // wire as much of the scene graph together as we can
1492 //->addChild( obj_trans );
1493 group->addChild( obj_trans );
1495 //cerr << "BIG FAT WARNING: k is here : " << pos << endl;
1501 globals->get_scenery()->get_scene_graph()->addChild(group);
1505 string FGStartupController::getName() {
1506 return string(parent->getId() + "-startup");
1509 void FGStartupController::update(double dt)
1516 /***************************************************************************
1517 * class FGApproachController
1519 **************************************************************************/
1520 FGApproachController::FGApproachController(FGAirportDynamics *par):
1527 void FGApproachController::announcePosition(int id,
1528 FGAIFlightPlan * intendedRoute,
1529 int currentPosition,
1530 double lat, double lon,
1531 double heading, double speed,
1532 double alt, double radius,
1533 int leg, FGAIAircraft * ref)
1536 TrafficVectorIterator i = activeTraffic.begin();
1537 // Search whether the current id alread has an entry
1538 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1539 if (activeTraffic.size()) {
1540 //while ((i->getId() != id) && i != activeTraffic.end()) {
1541 while (i != activeTraffic.end()) {
1542 if (i->getId() == id) {
1548 // Add a new TrafficRecord if no one exsists for this aircraft.
1549 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1550 FGTrafficRecord rec;
1553 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1554 rec.setRunway(intendedRoute->getRunway());
1556 //rec.setCallSign(callsign);
1557 rec.setAircraft(ref);
1558 activeTraffic.push_back(rec);
1560 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1564 void FGApproachController::updateAircraftInformation(int id, double lat, double lon,
1565 double heading, double speed, double alt,
1568 TrafficVectorIterator i = activeTraffic.begin();
1569 // Search search if the current id has an entry
1570 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1571 TrafficVectorIterator current, closest;
1572 if (activeTraffic.size()) {
1573 //while ((i->getId() != id) && i != activeTraffic.end()) {
1574 while (i != activeTraffic.end()) {
1575 if (i->getId() == id) {
1581 // // update position of the current aircraft
1582 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1583 SG_LOG(SG_ATC, SG_ALERT,
1584 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1586 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1588 //cerr << "ApproachController: checking for speed" << endl;
1590 current->getAircraft()->
1591 checkForArrivalTime(string("final001"));
1592 if (time_diff > 15) {
1593 current->setSpeedAdjustment(current->getAircraft()->
1594 getPerformance()->vDescent() *
1596 } else if (time_diff > 5) {
1597 current->setSpeedAdjustment(current->getAircraft()->
1598 getPerformance()->vDescent() *
1600 } else if (time_diff < -15) {
1601 current->setSpeedAdjustment(current->getAircraft()->
1602 getPerformance()->vDescent() *
1604 } else if (time_diff < -5) {
1605 current->setSpeedAdjustment(current->getAircraft()->
1606 getPerformance()->vDescent() *
1609 current->clearSpeedAdjustment();
1611 //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff);
1613 setDt(getDt() + dt);
1616 void FGApproachController::signOff(int id)
1618 TrafficVectorIterator i = activeTraffic.begin();
1619 // Search search if the current id alread has an entry
1620 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1621 if (activeTraffic.size()) {
1622 //while ((i->getId() != id) && i != activeTraffic.end()) {
1623 while (i != activeTraffic.end()) {
1624 if (i->getId() == id) {
1630 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1631 SG_LOG(SG_ATC, SG_ALERT,
1632 "AI error: Aircraft without traffic record is signing off from approach at " << SG_ORIGIN);
1634 i = activeTraffic.erase(i);
1638 void FGApproachController::update(double dt)
1645 bool FGApproachController::hasInstruction(int id)
1647 TrafficVectorIterator i = activeTraffic.begin();
1648 // Search search if the current id has an entry
1649 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1650 if (activeTraffic.size()) {
1651 //while ((i->getId() != id) && i != activeTraffic.end()) {
1652 while (i != activeTraffic.end()) {
1653 if (i->getId() == id) {
1659 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1660 SG_LOG(SG_ATC, SG_ALERT,
1661 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1663 return i->hasInstruction();
1669 FGATCInstruction FGApproachController::getInstruction(int id)
1671 TrafficVectorIterator i = activeTraffic.begin();
1672 // Search search if the current id has an entry
1673 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1674 if (activeTraffic.size()) {
1675 //while ((i->getId() != id) && i != activeTraffic.end()) {
1676 while (i != activeTraffic.end()) {
1677 if (i->getId() == id) {
1683 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1684 SG_LOG(SG_ATC, SG_ALERT,
1685 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1687 return i->getInstruction();
1689 return FGATCInstruction();
1693 ActiveRunway *FGApproachController::getRunway(string name)
1695 ActiveRunwayVecIterator rwy = activeRunways.begin();
1696 if (activeRunways.size()) {
1697 while (rwy != activeRunways.end()) {
1698 if (rwy->getRunwayName() == name) {
1704 if (rwy == activeRunways.end()) {
1705 ActiveRunway aRwy(name, 0);
1706 activeRunways.push_back(aRwy);
1707 rwy = activeRunways.end() - 1;
1712 void FGApproachController::render(bool visible) {
1713 //cerr << "FGApproachController::render function not yet implemented" << endl;
1718 string FGApproachController::getName() {
1719 return string(parent->getId() + "-approach");