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 ((onBoardRadioFreqI0 == stationFreq)
729 || (onBoardRadioFreqI1 == stationFreq)) {
730 if (rec->allowTransmissions()) {
732 if( fgGetBool( "/sim/radio/use-itm-attenuation", false ) ) {
733 //cerr << "Using ITM radio propagation" << endl;
734 FGRadioTransmission* radio = new FGRadioTransmission();
736 double sender_alt_ft, sender_alt;
738 sender_alt_ft = parent->getElevation();
739 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
740 sender_pos= SGGeod::fromDegM( parent->getLongitude(),
741 parent->getLatitude(), sender_alt );
744 sender_alt_ft = rec->getAltitude();
745 sender_alt = sender_alt_ft * SG_FEET_TO_METER;
746 sender_pos= SGGeod::fromDegM( rec->getLongitude(),
747 rec->getLatitude(), sender_alt );
749 double frequency = ((double)stationFreq) / 100;
750 radio->receiveATC(sender_pos, frequency, text, ground_to_air);
754 fgSetString("/sim/messages/atc", text.c_str());
759 FGATCDialogNew::instance()->addEntry(1, text);
764 string FGATCController::formatATCFrequency3_2(int freq)
767 snprintf(buffer, 7, "%3.2f", ((float) freq / 100.0));
768 return string(buffer);
771 // TODO: Set transponder codes according to real-world routes.
772 // The current version just returns a random string of four octal numbers.
773 string FGATCController::genTransponderCode(string fltRules)
775 if (fltRules == "VFR") {
776 return string("1200");
779 snprintf(buffer, 5, "%d%d%d%d", rand() % 8, rand() % 8, rand() % 8,
781 return string(buffer);
785 void FGATCController::init()
788 FGATCManager *mgr = (FGATCManager*) globals->get_subsystem("ATC");
789 mgr->addController(this);
794 /***************************************************************************
795 * class FGTowerController
797 **************************************************************************/
798 FGTowerController::FGTowerController(FGAirportDynamics *par) :
805 void FGTowerController::announcePosition(int id,
806 FGAIFlightPlan * intendedRoute,
807 int currentPosition, double lat,
808 double lon, double heading,
809 double speed, double alt,
810 double radius, int leg,
814 TrafficVectorIterator i = activeTraffic.begin();
815 // Search whether the current id alread has an entry
816 // This might be faster using a map instead of a vector, but let's start by taking a safe route
817 if (activeTraffic.size()) {
818 //while ((i->getId() != id) && i != activeTraffic.end()) {
819 while (i != activeTraffic.end()) {
820 if (i->getId() == id) {
826 // Add a new TrafficRecord if no one exsists for this aircraft.
827 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
831 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
832 rec.setRunway(intendedRoute->getRunway());
834 //rec.setCallSign(callsign);
835 rec.setRadius(radius);
836 rec.setAircraft(ref);
837 activeTraffic.push_back(rec);
838 // Don't just schedule the aircraft for the tower controller, also assign if to the correct active runway.
839 ActiveRunwayVecIterator rwy = activeRunways.begin();
840 if (activeRunways.size()) {
841 while (rwy != activeRunways.end()) {
842 if (rwy->getRunwayName() == intendedRoute->getRunway()) {
848 if (rwy == activeRunways.end()) {
849 ActiveRunway aRwy(intendedRoute->getRunway(), id);
850 aRwy.addToDepartureCue(ref);
851 activeRunways.push_back(aRwy);
852 rwy = (activeRunways.end()-1);
854 rwy->addToDepartureCue(ref);
857 //cerr << ref->getTrafficRef()->getCallSign() << " You are number " << rwy->getDepartureCueSize() << " for takeoff " << endl;
859 i->setPositionAndHeading(lat, lon, heading, speed, alt);
863 void FGTowerController::updateAircraftInformation(int id, double lat, double lon,
864 double heading, double speed, double alt,
867 TrafficVectorIterator i = activeTraffic.begin();
868 // Search whether the current id has an entry
869 // This might be faster using a map instead of a vector, but let's start by taking a safe route
870 TrafficVectorIterator current, closest;
871 if (activeTraffic.size()) {
872 //while ((i->getId() != id) && i != activeTraffic.end()) {
873 while (i != activeTraffic.end()) {
874 if (i->getId() == id) {
880 // // update position of the current aircraft
881 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
882 SG_LOG(SG_ATC, SG_ALERT,
883 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
885 i->setPositionAndHeading(lat, lon, heading, speed, alt);
890 // see if we already have a clearance record for the currently active runway
891 // NOTE: dd. 2011-08-07: Because the active runway has been constructed in the announcePosition function, we may safely assume that is
892 // already exists here. So, we can simplify the current code.
894 ActiveRunwayVecIterator rwy = activeRunways.begin();
895 //if (parent->getId() == fgGetString("/sim/presets/airport-id")) {
896 // for (rwy = activeRunways.begin(); rwy != activeRunways.end(); rwy++) {
897 // rwy->printDepartureCue();
901 rwy = activeRunways.begin();
902 while (rwy != activeRunways.end()) {
903 if (rwy->getRunwayName() == current->getRunway()) {
909 // only bother running the following code if the current aircraft is the
910 // first in line for depature
911 /* if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
912 if (rwy->getCleared()) {
913 if (id == rwy->getCleared()) {
914 current->setHoldPosition(false);
916 current->setHoldPosition(true);
919 // For now. At later stages, this will probably be the place to check for inbound traffc.
923 // only bother with aircraft that have a takeoff status of 2, since those are essentially under tower control
924 FGAIAircraft* ac= rwy->getFirstAircraftInDepartureCue();
925 if (ac->getTakeOffStatus() == 1) {
926 ac->setTakeOffStatus(2);
928 if (current->getAircraft()->getTakeOffStatus() == 2) {
929 current -> setHoldPosition(false);
931 current->setHoldPosition(true);
933 int clearanceId = rwy->getCleared();
935 if (id == clearanceId) {
936 current->setHoldPosition(false);
939 if (current->getAircraft() == rwy->getFirstAircraftInDepartureCue()) {
941 FGAIAircraft *ac = rwy->getFirstOfStatus(1);
943 ac->setTakeOffStatus(2);
949 void FGTowerController::signOff(int id)
951 TrafficVectorIterator i = activeTraffic.begin();
952 // Search search if the current id alread has an entry
953 // This might be faster using a map instead of a vector, but let's start by taking a safe route
954 if (activeTraffic.size()) {
955 //while ((i->getId() != id) && i != activeTraffic.end()) {
956 while (i != activeTraffic.end()) {
957 if (i->getId() == id) {
963 // If this aircraft has left the runway, we can clear the departure record for this runway
964 ActiveRunwayVecIterator rwy = activeRunways.begin();
965 if (activeRunways.size()) {
966 //while ((rwy->getRunwayName() != i->getRunway()) && (rwy != activeRunways.end())) {
967 while (rwy != activeRunways.end()) {
968 if (rwy->getRunwayName() == i->getRunway()) {
973 if (rwy != activeRunways.end()) {
975 rwy->updateDepartureCue();
977 SG_LOG(SG_ATC, SG_ALERT,
978 "AI error: Attempting to erase non-existing runway clearance record in FGTowerController::signoff at " << SG_ORIGIN);
981 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
982 SG_LOG(SG_ATC, SG_ALERT,
983 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
985 i->getAircraft()->resetTakeOffStatus();
986 i = activeTraffic.erase(i);
987 //cerr << "Signing off from tower controller" << endl;
992 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
993 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
994 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
995 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
996 // Note that this function is probably obsolete
997 bool FGTowerController::hasInstruction(int id)
999 TrafficVectorIterator i = activeTraffic.begin();
1000 // Search search if the current id has an entry
1001 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1002 if (activeTraffic.size()) {
1003 //while ((i->getId() != id) && i != activeTraffic.end()) {
1004 while (i != activeTraffic.end()) {
1005 if (i->getId() == id) {
1011 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1012 SG_LOG(SG_ATC, SG_ALERT,
1013 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1015 return i->hasInstruction();
1021 FGATCInstruction FGTowerController::getInstruction(int id)
1023 TrafficVectorIterator i = activeTraffic.begin();
1024 // Search search if the current id has an entry
1025 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1026 if (activeTraffic.size()) {
1027 //while ((i->getId() != id) && i != activeTraffic.end()) {
1028 while (i != activeTraffic.end()) {
1029 if (i->getId() == id) {
1035 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1036 SG_LOG(SG_ATC, SG_ALERT,
1037 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1039 return i->getInstruction();
1041 return FGATCInstruction();
1044 void FGTowerController::render(bool visible) {
1045 //cerr << "FGTowerController::render function not yet implemented" << endl;
1048 string FGTowerController::getName() {
1049 return string(parent->getId() + "-tower");
1052 void FGTowerController::update(double dt)
1059 /***************************************************************************
1060 * class FGStartupController
1062 **************************************************************************/
1063 FGStartupController::FGStartupController(FGAirportDynamics *par):
1069 void FGStartupController::announcePosition(int id,
1070 FGAIFlightPlan * intendedRoute,
1071 int currentPosition, double lat,
1072 double lon, double heading,
1073 double speed, double alt,
1074 double radius, int leg,
1078 TrafficVectorIterator i = activeTraffic.begin();
1079 // Search whether the current id alread has an entry
1080 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1081 if (activeTraffic.size()) {
1082 //while ((i->getId() != id) && i != activeTraffic.end()) {
1083 while (i != activeTraffic.end()) {
1084 if (i->getId() == id) {
1090 // Add a new TrafficRecord if no one exsists for this aircraft.
1091 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1092 FGTrafficRecord rec;
1095 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1096 rec.setRunway(intendedRoute->getRunway());
1098 rec.setPositionAndIntentions(currentPosition, intendedRoute);
1099 //rec.setCallSign(callsign);
1100 rec.setAircraft(ref);
1101 rec.setHoldPosition(true);
1102 activeTraffic.push_back(rec);
1104 i->setPositionAndIntentions(currentPosition, intendedRoute);
1105 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1111 // IF WE MAKE TRAFFICRECORD A MEMBER OF THE BASE CLASS
1112 // THE FOLLOWING THREE FUNCTIONS: SIGNOFF, HAS INSTRUCTION AND GETINSTRUCTION CAN
1113 // BECOME DEVIRTUALIZED AND BE A MEMBER OF THE BASE ATCCONTROLLER CLASS
1114 // WHICH WOULD SIMPLIFY CODE MAINTENANCE.
1115 // Note that this function is probably obsolete
1116 bool FGStartupController::hasInstruction(int id)
1118 TrafficVectorIterator i = activeTraffic.begin();
1119 // Search search if the current id has an entry
1120 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1121 if (activeTraffic.size()) {
1122 //while ((i->getId() != id) && i != activeTraffic.end()) {
1123 while (i != activeTraffic.end()) {
1124 if (i->getId() == id) {
1130 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1131 SG_LOG(SG_ATC, SG_ALERT,
1132 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1134 return i->hasInstruction();
1140 FGATCInstruction FGStartupController::getInstruction(int id)
1142 TrafficVectorIterator i = activeTraffic.begin();
1143 // Search search if the current id has an entry
1144 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1145 if (activeTraffic.size()) {
1146 //while ((i->getId() != id) && i != activeTraffic.end()) {
1147 while (i != activeTraffic.end()) {
1148 if (i->getId() == id) {
1154 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1155 SG_LOG(SG_ATC, SG_ALERT,
1156 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1158 return i->getInstruction();
1160 return FGATCInstruction();
1163 void FGStartupController::signOff(int id)
1165 TrafficVectorIterator i = activeTraffic.begin();
1166 // Search search if the current id alread has an entry
1167 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1168 if (activeTraffic.size()) {
1169 //while ((i->getId() != id) && i != activeTraffic.end()) {
1170 while (i != activeTraffic.end()) {
1171 if (i->getId() == id) {
1177 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1178 SG_LOG(SG_ATC, SG_ALERT,
1179 "AI error: Aircraft without traffic record is signing off from tower at " << SG_ORIGIN);
1181 //cerr << i->getAircraft()->getCallSign() << " signing off from startupcontroller" << endl;
1182 i = activeTraffic.erase(i);
1186 bool FGStartupController::checkTransmissionState(int st, time_t now, time_t startTime, TrafficVectorIterator i, AtcMsgId msgId,
1189 int state = i->getState();
1190 if ((state == st) && available) {
1191 if ((msgDir == ATC_AIR_TO_GROUND) && isUserAircraft(i->getAircraft())) {
1193 //cerr << "Checking state " << st << " for " << i->getAircraft()->getCallSign() << endl;
1194 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
1195 int n = trans_num->getIntValue();
1197 trans_num->setIntValue(-1);
1198 // PopupCallback(n);
1199 //cerr << "Selected transmission message " << n << endl;
1200 FGATCDialogNew::instance()->removeEntry(1);
1202 //cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
1203 transmit(&(*i), &(*parent), msgId, msgDir, false);
1207 if (now > startTime) {
1208 //cerr << "Transmitting startup msg" << endl;
1209 transmit(&(*i), &(*parent), msgId, msgDir, true);
1211 lastTransmission = now;
1219 void FGStartupController::updateAircraftInformation(int id, double lat, double lon,
1220 double heading, double speed, double alt,
1223 TrafficVectorIterator i = activeTraffic.begin();
1224 // Search search if the current id has an entry
1225 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1226 TrafficVectorIterator current, closest;
1227 if (activeTraffic.size()) {
1228 //while ((i->getId() != id) && i != activeTraffic.end()) {
1229 while (i != activeTraffic.end()) {
1230 if (i->getId() == id) {
1236 // // update position of the current aircraft
1238 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1239 SG_LOG(SG_ATC, SG_ALERT,
1240 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1242 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1245 setDt(getDt() + dt);
1247 int state = i->getState();
1249 // The user controlled aircraft should have crased here, because it doesn't have a traffic reference.
1250 // NOTE: if we create a traffic schedule for the user aircraft, we can use this to plan a flight.
1251 time_t startTime = i->getAircraft()->getTrafficRef()->getDepartureTime();
1252 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1253 //cerr << i->getAircraft()->getTrafficRef()->getCallSign()
1254 // << " is scheduled to depart in " << startTime-now << " seconds. Available = " << available
1255 // << " at parking " << getGateName(i->getAircraft()) << endl;
1257 if ((now - lastTransmission) > 3 + (rand() % 15)) {
1261 checkTransmissionState(0, now, (startTime + 0 ), i, MSG_ANNOUNCE_ENGINE_START, ATC_AIR_TO_GROUND);
1262 checkTransmissionState(1, now, (startTime + 60 ), i, MSG_REQUEST_ENGINE_START, ATC_AIR_TO_GROUND);
1263 checkTransmissionState(2, now, (startTime + 80 ), i, MSG_PERMIT_ENGINE_START, ATC_GROUND_TO_AIR);
1264 checkTransmissionState(3, now, (startTime + 100), i, MSG_ACKNOWLEDGE_ENGINE_START, ATC_AIR_TO_GROUND);
1265 if (checkTransmissionState(4, now, (startTime + 130), i, MSG_ACKNOWLEDGE_SWITCH_GROUND_FREQUENCY, ATC_AIR_TO_GROUND)) {
1268 checkTransmissionState(5, now, (startTime + 140), i, MSG_INITIATE_CONTACT, ATC_AIR_TO_GROUND);
1269 checkTransmissionState(6, now, (startTime + 150), i, MSG_ACKNOWLEDGE_INITIATE_CONTACT, ATC_GROUND_TO_AIR);
1270 checkTransmissionState(7, now, (startTime + 180), i, MSG_REQUEST_PUSHBACK_CLEARANCE, ATC_AIR_TO_GROUND);
1274 if ((state == 8) && available) {
1275 if (now > startTime + 200) {
1276 if (i->pushBackAllowed()) {
1277 i->allowRepeatedTransmissions();
1278 transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE,
1279 ATC_GROUND_TO_AIR, true);
1282 transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE,
1283 ATC_GROUND_TO_AIR, true);
1284 i->suppressRepeatedTransmissions();
1286 lastTransmission = now;
1290 if ((state == 9) && available) {
1291 i->setHoldPosition(false);
1295 // Note that this function is copied from simgear. for maintanance purposes, it's probabtl better to make a general function out of that.
1296 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
1297 double lon, double elev, double hdg, double slope)
1299 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
1300 obj_pos = geod.makeZUpFrame();
1301 // hdg is not a compass heading, but a counter-clockwise rotation
1302 // around the Z axis
1303 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
1305 obj_pos.preMult(osg::Matrix::rotate(slope * SGD_DEGREES_TO_RADIANS,
1310 void FGStartupController::render(bool visible)
1313 SGMaterialLib *matlib = globals->get_matlib();
1316 globals->get_scenery()->get_scene_graph()->removeChild(group);
1317 //while (group->getNumChildren()) {
1318 // cerr << "Number of children: " << group->getNumChildren() << endl;
1319 //simgear::EffectGeode* geode = (simgear::EffectGeode*) group->getChild(0);
1320 //osg::MatrixTransform *obj_trans = (osg::MatrixTransform*) group->getChild(0);
1321 //geode->releaseGLObjects();
1322 //group->removeChild(geode);
1327 group = new osg::Group;
1328 FGScenery * local_scenery = globals->get_scenery();
1329 //double elevation_meters = 0.0;
1330 double elevation_feet = 0.0;
1333 //for ( FGTaxiSegmentVectorIterator i = segments.begin(); i != segments.end(); i++) {
1335 time_t now = time(NULL) + fgGetLong("/sim/time/warp");
1336 for (TrafficVectorIterator i = activeTraffic.begin(); i != activeTraffic.end(); i++) {
1337 if (i->isActive(300)) {
1338 // Handle start point
1339 int pos = i->getCurrentPosition();
1340 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "pos = " << pos << endl;
1342 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(pos);
1343 SGGeod start(SGGeod::fromDeg((i->getLongitude()), (i->getLatitude())));
1344 SGGeod end (SGGeod::fromDeg(segment->getEnd()->getLongitude(), segment->getEnd()->getLatitude()));
1346 double length = SGGeodesy::distanceM(start, end);
1347 //heading = SGGeodesy::headingDeg(start->getGeod(), end->getGeod());
1349 double az2, heading; //, distanceM;
1350 SGGeodesy::inverse(start, end, heading, az2, length);
1351 double coveredDistance = length * 0.5;
1353 SGGeodesy::direct(start, heading, coveredDistance, center, az2);
1354 //cerr << "Active Aircraft : Centerpoint = (" << center.getLatitudeDeg() << ", " << center.getLongitudeDeg() << "). Heading = " << heading << endl;
1355 ///////////////////////////////////////////////////////////////////////////////
1356 // Make a helper function out of this
1357 osg::Matrix obj_pos;
1358 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1359 obj_trans->setDataVariance(osg::Object::STATIC);
1360 // Experimental: Calculate slope here, based on length, and the individual elevations
1361 double elevationStart;
1362 if (isUserAircraft((i)->getAircraft())) {
1363 elevationStart = fgGetDouble("/position/ground-elev-m");
1365 elevationStart = ((i)->getAircraft()->_getAltitude() * SG_FEET_TO_METER);
1367 double elevationEnd = segment->getEnd()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1368 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1369 SGGeod center2 = end;
1370 center2.setElevationM(SG_MAX_ELEVATION_M);
1371 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1372 elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1373 //elevation_meters += 0.5;
1376 elevationEnd = parent->getElevation();
1378 segment->getEnd()->setElevation(elevationEnd);
1381 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1382 double elevDiff = elevationEnd - elevationStart;
1384 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1386 //cerr << "1. Using mean elevation : " << elevationMean << " and " << slope << endl;
1388 WorldCoordinate( obj_pos, center.getLatitudeDeg(), center.getLongitudeDeg(), elevationMean + 0.5 + dx, -(heading), slope );
1391 obj_trans->setMatrix( obj_pos );
1392 //osg::Vec3 center(0, 0, 0)
1394 float width = length /2.0;
1395 osg::Vec3 corner(-width, 0, 0.25f);
1396 osg::Vec3 widthVec(2*width + 1, 0, 0);
1397 osg::Vec3 heightVec(0, 1, 0);
1398 osg::Geometry* geometry;
1399 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1400 simgear::EffectGeode* geode = new simgear::EffectGeode;
1401 geode->setName("test");
1402 geode->addDrawable(geometry);
1403 //osg::Node *custom_obj;
1405 if (segment->hasBlock(now)) {
1406 mat = matlib->find("UnidirectionalTaperRed");
1408 mat = matlib->find("UnidirectionalTaperGreen");
1411 geode->setEffect(mat->get_effect());
1412 obj_trans->addChild(geode);
1413 // wire as much of the scene graph together as we can
1414 //->addChild( obj_trans );
1415 group->addChild( obj_trans );
1416 /////////////////////////////////////////////////////////////////////
1418 //cerr << "BIG FAT WARNING: current position is here : " << pos << endl;
1420 for (intVecIterator j = (i)->getIntentions().begin(); j != (i)->getIntentions().end(); j++) {
1421 osg::Matrix obj_pos;
1424 //cerr << "rendering for " << i->getAircraft()->getCallSign() << "intention = " << k << endl;
1425 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
1426 obj_trans->setDataVariance(osg::Object::STATIC);
1427 FGTaxiSegment *segment = parent->getGroundNetwork()->findSegment(k);
1429 double elevationStart = segment->getStart()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1430 double elevationEnd = segment->getEnd ()->getElevationM(parent->getElevation()*SG_FEET_TO_METER);
1431 if ((elevationStart == 0) || (elevationStart == parent->getElevation())) {
1432 SGGeod center2 = segment->getStart()->getGeod();
1433 center2.setElevationM(SG_MAX_ELEVATION_M);
1434 if (local_scenery->get_elevation_m( center2, elevationStart, NULL )) {
1435 elevation_feet = elevationStart * SG_METER_TO_FEET + 0.5;
1436 //elevation_meters += 0.5;
1439 elevationStart = parent->getElevation();
1441 segment->getStart()->setElevation(elevationStart);
1443 if ((elevationEnd == 0) || (elevationEnd == parent->getElevation())) {
1444 SGGeod center2 = segment->getEnd()->getGeod();
1445 center2.setElevationM(SG_MAX_ELEVATION_M);
1446 if (local_scenery->get_elevation_m( center2, elevationEnd, NULL )) {
1447 elevation_feet = elevationEnd * SG_METER_TO_FEET + 0.5;
1448 //elevation_meters += 0.5;
1451 elevationEnd = parent->getElevation();
1453 segment->getEnd()->setElevation(elevationEnd);
1456 double elevationMean = (elevationStart + elevationEnd) / 2.0;
1457 double elevDiff = elevationEnd - elevationStart;
1458 double length = segment->getLength();
1459 double slope = atan2(elevDiff, length) * SGD_RADIANS_TO_DEGREES;
1461 //cerr << "2. Using mean elevation : " << elevationMean << " and " << slope << endl;
1464 WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), elevationMean + 0.5 + dx, -(segment->getHeading()), slope );
1466 //WorldCoordinate( obj_pos, segment->getLatitude(), segment->getLongitude(), parent->getElevation()+8+dx, -(segment->getHeading()) );
1468 obj_trans->setMatrix( obj_pos );
1469 //osg::Vec3 center(0, 0, 0)
1471 float width = segment->getLength() /2.0;
1472 osg::Vec3 corner(-width, 0, 0.25f);
1473 osg::Vec3 widthVec(2*width + 1, 0, 0);
1474 osg::Vec3 heightVec(0, 1, 0);
1475 osg::Geometry* geometry;
1476 geometry = osg::createTexturedQuadGeometry(corner, widthVec, heightVec);
1477 simgear::EffectGeode* geode = new simgear::EffectGeode;
1478 geode->setName("test");
1479 geode->addDrawable(geometry);
1480 //osg::Node *custom_obj;
1482 if (segment->hasBlock(now)) {
1483 mat = matlib->find("UnidirectionalTaperRed");
1485 mat = matlib->find("UnidirectionalTaperGreen");
1488 geode->setEffect(mat->get_effect());
1489 obj_trans->addChild(geode);
1490 // wire as much of the scene graph together as we can
1491 //->addChild( obj_trans );
1492 group->addChild( obj_trans );
1494 //cerr << "BIG FAT WARNING: k is here : " << pos << endl;
1500 globals->get_scenery()->get_scene_graph()->addChild(group);
1504 string FGStartupController::getName() {
1505 return string(parent->getId() + "-startup");
1508 void FGStartupController::update(double dt)
1515 /***************************************************************************
1516 * class FGApproachController
1518 **************************************************************************/
1519 FGApproachController::FGApproachController(FGAirportDynamics *par):
1526 void FGApproachController::announcePosition(int id,
1527 FGAIFlightPlan * intendedRoute,
1528 int currentPosition,
1529 double lat, double lon,
1530 double heading, double speed,
1531 double alt, double radius,
1532 int leg, FGAIAircraft * ref)
1535 TrafficVectorIterator i = activeTraffic.begin();
1536 // Search whether the current id alread has an entry
1537 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1538 if (activeTraffic.size()) {
1539 //while ((i->getId() != id) && i != activeTraffic.end()) {
1540 while (i != activeTraffic.end()) {
1541 if (i->getId() == id) {
1547 // Add a new TrafficRecord if no one exsists for this aircraft.
1548 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1549 FGTrafficRecord rec;
1552 rec.setPositionAndHeading(lat, lon, heading, speed, alt);
1553 rec.setRunway(intendedRoute->getRunway());
1555 //rec.setCallSign(callsign);
1556 rec.setAircraft(ref);
1557 activeTraffic.push_back(rec);
1559 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1563 void FGApproachController::updateAircraftInformation(int id, double lat, double lon,
1564 double heading, double speed, double alt,
1567 TrafficVectorIterator i = activeTraffic.begin();
1568 // Search search if the current id has an entry
1569 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1570 TrafficVectorIterator current, closest;
1571 if (activeTraffic.size()) {
1572 //while ((i->getId() != id) && i != activeTraffic.end()) {
1573 while (i != activeTraffic.end()) {
1574 if (i->getId() == id) {
1580 // // update position of the current aircraft
1581 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1582 SG_LOG(SG_ATC, SG_ALERT,
1583 "AI error: updating aircraft without traffic record at " << SG_ORIGIN);
1585 i->setPositionAndHeading(lat, lon, heading, speed, alt);
1587 //cerr << "ApproachController: checking for speed" << endl;
1589 current->getAircraft()->
1590 checkForArrivalTime(string("final001"));
1591 if (time_diff > 15) {
1592 current->setSpeedAdjustment(current->getAircraft()->
1593 getPerformance()->vDescent() *
1595 } else if (time_diff > 5) {
1596 current->setSpeedAdjustment(current->getAircraft()->
1597 getPerformance()->vDescent() *
1599 } else if (time_diff < -15) {
1600 current->setSpeedAdjustment(current->getAircraft()->
1601 getPerformance()->vDescent() *
1603 } else if (time_diff < -5) {
1604 current->setSpeedAdjustment(current->getAircraft()->
1605 getPerformance()->vDescent() *
1608 current->clearSpeedAdjustment();
1610 //current->setSpeedAdjustment(current->getAircraft()->getPerformance()->vDescent() + time_diff);
1612 setDt(getDt() + dt);
1615 void FGApproachController::signOff(int id)
1617 TrafficVectorIterator i = activeTraffic.begin();
1618 // Search search if the current id alread has an entry
1619 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1620 if (activeTraffic.size()) {
1621 //while ((i->getId() != id) && i != activeTraffic.end()) {
1622 while (i != activeTraffic.end()) {
1623 if (i->getId() == id) {
1629 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1630 SG_LOG(SG_ATC, SG_ALERT,
1631 "AI error: Aircraft without traffic record is signing off from approach at " << SG_ORIGIN);
1633 i = activeTraffic.erase(i);
1637 void FGApproachController::update(double dt)
1644 bool FGApproachController::hasInstruction(int id)
1646 TrafficVectorIterator i = activeTraffic.begin();
1647 // Search search if the current id has an entry
1648 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1649 if (activeTraffic.size()) {
1650 //while ((i->getId() != id) && i != activeTraffic.end()) {
1651 while (i != activeTraffic.end()) {
1652 if (i->getId() == id) {
1658 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1659 SG_LOG(SG_ATC, SG_ALERT,
1660 "AI error: checking ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1662 return i->hasInstruction();
1668 FGATCInstruction FGApproachController::getInstruction(int id)
1670 TrafficVectorIterator i = activeTraffic.begin();
1671 // Search search if the current id has an entry
1672 // This might be faster using a map instead of a vector, but let's start by taking a safe route
1673 if (activeTraffic.size()) {
1674 //while ((i->getId() != id) && i != activeTraffic.end()) {
1675 while (i != activeTraffic.end()) {
1676 if (i->getId() == id) {
1682 if (i == activeTraffic.end() || (activeTraffic.size() == 0)) {
1683 SG_LOG(SG_ATC, SG_ALERT,
1684 "AI error: requesting ATC instruction for aircraft without traffic record at " << SG_ORIGIN);
1686 return i->getInstruction();
1688 return FGATCInstruction();
1692 ActiveRunway *FGApproachController::getRunway(string name)
1694 ActiveRunwayVecIterator rwy = activeRunways.begin();
1695 if (activeRunways.size()) {
1696 while (rwy != activeRunways.end()) {
1697 if (rwy->getRunwayName() == name) {
1703 if (rwy == activeRunways.end()) {
1704 ActiveRunway aRwy(name, 0);
1705 activeRunways.push_back(aRwy);
1706 rwy = activeRunways.end() - 1;
1711 void FGApproachController::render(bool visible) {
1712 //cerr << "FGApproachController::render function not yet implemented" << endl;
1717 string FGApproachController::getName() {
1718 return string(parent->getId() + "-approach");