| 1 | /****************************************************************************** |
| 2 | * atc_mgr.cxx |
| 3 | * Written by Durk Talsma, started August 1, 2010. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | * |
| 20 | **************************************************************************/ |
| 21 | |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | # include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <Airports/dynamics.hxx> |
| 28 | #include <Airports/airportdynamicsmanager.hxx> |
| 29 | #include <Airports/airport.hxx> |
| 30 | #include <Scenery/scenery.hxx> |
| 31 | #include <Main/globals.hxx> |
| 32 | #include <Main/fg_props.hxx> |
| 33 | #include <AIModel/AIAircraft.hxx> |
| 34 | #include <AIModel/AIManager.hxx> |
| 35 | #include <Traffic/Schedule.hxx> |
| 36 | #include <Traffic/SchedFlight.hxx> |
| 37 | #include <AIModel/AIFlightPlan.hxx> |
| 38 | |
| 39 | #include "atc_mgr.hxx" |
| 40 | |
| 41 | |
| 42 | using std::string; |
| 43 | |
| 44 | /** |
| 45 | Constructor, initializes values to private boolean and FGATCController instances |
| 46 | */ |
| 47 | FGATCManager::FGATCManager() : |
| 48 | controller(nullptr), |
| 49 | prevController(nullptr) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | Default destructor |
| 55 | */ |
| 56 | FGATCManager::~FGATCManager() { |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Sets up ATC subsystem parts depending on other subsystems |
| 61 | * Override of SGSubsystem::postinit() |
| 62 | * Will set private boolean flag "initSucceeded" to true upon conclusion |
| 63 | */ |
| 64 | void FGATCManager::postinit() |
| 65 | { |
| 66 | transNum = fgGetNode("/sim/atc/transmission-num", true); |
| 67 | splashAlpha = fgGetNode("/sim/startup/splash-alpha", true); |
| 68 | initControllers(); |
| 69 | initSucceeded = true; |
| 70 | } |
| 71 | |
| 72 | void FGATCManager::initControllers() { |
| 73 | int leg = 0; |
| 74 | |
| 75 | // Assign a controller to the user's aircraft. |
| 76 | // Three scenarios are considered: |
| 77 | // - Starting on ground at a parking position |
| 78 | // - Starting on ground at the runway. |
| 79 | // - Starting in the air |
| 80 | bool onGround = fgGetBool("/sim/presets/onground"); |
| 81 | string runway = fgGetString("/sim/atc/runway"); |
| 82 | string curAirport = fgGetString("/sim/presets/airport-id"); |
| 83 | string parking = fgGetString("/sim/presets/parkpos"); |
| 84 | |
| 85 | _routeManagerDestinationAirportNode = globals->get_props()->getNode("/autopilot/route-manager/destination/airport", true); |
| 86 | destination = _routeManagerDestinationAirportNode->getStringValue(); |
| 87 | |
| 88 | auto aiManager = globals->get_subsystem<FGAIManager>(); |
| 89 | auto userAircraft = aiManager->getUserAircraft(); |
| 90 | string callsign = userAircraft->getCallSign(); |
| 91 | |
| 92 | double aircraftRadius = 40; // note that this is currently hardcoded to a one-size-fits all JumboJet value. Should change later. |
| 93 | |
| 94 | // In case a destination is not set yet, make it equal to the current airport |
| 95 | if (destination.empty()) { |
| 96 | destination = curAirport; |
| 97 | } |
| 98 | |
| 99 | // NEXT UP: Create a traffic schedule and fill that with appropriate information. This we can use for flight planning. |
| 100 | // Note that these are currently only defaults. |
| 101 | userAircraftTrafficRef.reset(new FGAISchedule); |
| 102 | userAircraftTrafficRef->setFlightType("gate"); |
| 103 | |
| 104 | userAircraftScheduledFlight.reset(new FGScheduledFlight); |
| 105 | userAircraftScheduledFlight->setDepartureAirport(curAirport); |
| 106 | userAircraftScheduledFlight->setArrivalAirport(destination); |
| 107 | userAircraftScheduledFlight->initializeAirports(); |
| 108 | userAircraftScheduledFlight->setFlightRules("IFR"); |
| 109 | userAircraftScheduledFlight->setCallSign(callsign); |
| 110 | |
| 111 | userAircraftTrafficRef->assign(userAircraftScheduledFlight.get()); |
| 112 | std::unique_ptr<FGAIFlightPlan> fp ; |
| 113 | userAircraft->setTrafficRef(userAircraftTrafficRef.get()); |
| 114 | |
| 115 | // string flightPlanName = curAirport + "-" + _routeManagerDestinationAirportNode->getStringValue() + ".xml"; |
| 116 | // double cruiseAlt = 100; // Doesn't really matter right now. |
| 117 | // double courseToDest = 180; // Just use something neutral; this value might affect the runway that is used though... |
| 118 | // time_t deptime = 0; // just make sure how flightplan processing is affected by this... |
| 119 | |
| 120 | FGAirportDynamicsRef dcs(flightgear::AirportDynamicsManager::find(curAirport)); |
| 121 | if (dcs && onGround) {// && !runway.empty()) { |
| 122 | |
| 123 | ParkingAssignment pk; |
| 124 | |
| 125 | if (parking == "AVAILABLE") { |
| 126 | double radius = fgGetDouble("/sim/dimensions/radius-m"); |
| 127 | if (radius > 0) { |
| 128 | pk = dcs->getAvailableParking(radius, string(), string(), string()); |
| 129 | if (pk.isValid()) { |
| 130 | fgGetString("/sim/presets/parkpos"); |
| 131 | fgSetString("/sim/presets/parkpos", pk.parking()->getName()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!pk.isValid()) { |
| 136 | FGParkingList pkl(dcs->getParkings(true, "gate")); |
| 137 | if (!pkl.empty()) { |
| 138 | std::sort(pkl.begin(), pkl.end(), [](const FGParkingRef& a, const FGParkingRef& b) { |
| 139 | return a->getRadius() > b->getRadius(); |
| 140 | }); |
| 141 | pk = ParkingAssignment(pkl.front(), dcs); |
| 142 | fgSetString("/sim/presets/parkpos", pkl.front()->getName()); |
| 143 | } |
| 144 | } |
| 145 | } else if (!parking.empty()) { |
| 146 | pk = dcs->getAvailableParkingByName(parking); |
| 147 | } |
| 148 | |
| 149 | if (pk.isValid()) { |
| 150 | dcs->setParkingAvailable(pk.parking(), false); |
| 151 | fp.reset(new FGAIFlightPlan); |
| 152 | controller = dcs->getStartupController(); |
| 153 | int stationFreq = dcs->getGroundFrequency(1); |
| 154 | if (stationFreq > 0) |
| 155 | { |
| 156 | SG_LOG(SG_ATC, SG_DEBUG, "Setting radio frequency to : " << stationFreq); |
| 157 | fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0)); |
| 158 | } |
| 159 | leg = AILeg::STARTUP_PUSHBACK; |
| 160 | //double, lat, lon, head; // Unused variables; |
| 161 | //int getId = apt->getDynamics()->getParking(gateId, &lat, &lon, &head); |
| 162 | aircraftRadius = pk.parking()->getRadius(); |
| 163 | string fltType = pk.parking()->getType(); // gate / ramp, ga, etc etc. |
| 164 | string aircraftType; // Unused. |
| 165 | string airline; // Currently used for gate selection, but a fallback mechanism will apply when not specified. |
| 166 | fp->setGate(pk); |
| 167 | if (!(fp->createPushBack(userAircraft, |
| 168 | false, |
| 169 | dcs->parent(), |
| 170 | aircraftRadius, |
| 171 | fltType, |
| 172 | aircraftType, |
| 173 | airline))) { |
| 174 | controller = 0; |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | } else if (!runway.empty()) { |
| 181 | // on a runway |
| 182 | |
| 183 | controller = dcs->getTowerController(); |
| 184 | int stationFreq = dcs->getTowerFrequency(2); |
| 185 | if (stationFreq > 0) |
| 186 | { |
| 187 | SG_LOG(SG_ATC, SG_DEBUG, "Setting radio frequency to inair frequency : " << stationFreq); |
| 188 | fgSetDouble("/instrumentation/comm[0]/frequencies/selected-mhz", ((double) stationFreq / 100.0)); |
| 189 | } |
| 190 | fp.reset(new FGAIFlightPlan); |
| 191 | leg = AILeg::TAKEOFF; |
| 192 | string fltType = "ga"; |
| 193 | fp->setRunway(runway); |
| 194 | fp->createTakeOff(userAircraft, false, dcs->parent(), userAircraft->getGeodPos(), 0, fltType); |
| 195 | } else { |
| 196 | // We're on the ground somewhere. Handle this case later. |
| 197 | |
| 198 | // important : we are on the ground, so reset the AIFlightPlan back to |
| 199 | // a dummy one. Otherwise, in the reposition case, we end up with a |
| 200 | // stale flight-plan which confuses other code (eg, PositionInit::finalizeForParking) |
| 201 | // see unit test: PosInitTests::testRepositionAtOccupied |
| 202 | fp.reset(FGAIFlightPlan::createDummyUserPlan()); |
| 203 | userAircraft->FGAIBase::setFlightPlan(std::move(fp)); |
| 204 | controller = nullptr; |
| 205 | |
| 206 | initSucceeded = true; // should be false? |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | if (fp && !fp->empty()) { |
| 211 | fp->getLastWaypoint()->setName( fp->getLastWaypoint()->getName() + string("legend")); |
| 212 | } |
| 213 | } else { |
| 214 | controller = nullptr; |
| 215 | } |
| 216 | |
| 217 | // Create an initial flightplan and assign it to the ai_ac. We won't use this flightplan, but it is necessary to |
| 218 | // keep the ATC code happy. |
| 219 | // note in the reposition case, 'fp' is only the new FlightPlan; if we didn't create one here. |
| 220 | // we will continue using the existing flight plan (and not restart it, for example) |
| 221 | if (fp) { |
| 222 | fp->restart(); |
| 223 | fp->setLeg(leg); |
| 224 | userAircraft->FGAIBase::setFlightPlan(std::move(fp)); |
| 225 | } |
| 226 | |
| 227 | if (controller) { |
| 228 | FGAIFlightPlan* plan = userAircraft->GetFlightPlan(); |
| 229 | const int routeIndex = (plan && plan->getCurrentWaypoint()) ? plan->getCurrentWaypoint()->getRouteIndex() : 0; |
| 230 | controller->announcePosition(userAircraft->getID(), plan, |
| 231 | routeIndex, |
| 232 | userAircraft->_getLatitude(), |
| 233 | userAircraft->_getLongitude(), |
| 234 | userAircraft->_getHeading(), |
| 235 | userAircraft->getSpeed(), |
| 236 | userAircraft->getAltitude(), |
| 237 | aircraftRadius, leg, userAircraft); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | Shutdown method |
| 243 | Clears activeStations vector in preparation for clean shutdown |
| 244 | Override of SGSubsystem::shutdown() |
| 245 | */ |
| 246 | void FGATCManager::shutdown() |
| 247 | { |
| 248 | activeStations.clear(); |
| 249 | userAircraftTrafficRef.reset(); |
| 250 | userAircraftScheduledFlight.reset(); |
| 251 | _routeManagerDestinationAirportNode.clear(); |
| 252 | } |
| 253 | |
| 254 | void FGATCManager::reposition() |
| 255 | { |
| 256 | prevController = controller = nullptr; |
| 257 | |
| 258 | // remove any parking assignment form the user flight-plan, so it's |
| 259 | // available again. postinit() will recompute a new value if required |
| 260 | auto aiManager = globals->get_subsystem<FGAIManager>(); |
| 261 | auto userAircraft = aiManager->getUserAircraft(); |
| 262 | if (userAircraft) { |
| 263 | if (userAircraft->GetFlightPlan()) { |
| 264 | auto userAIFP = userAircraft->GetFlightPlan(); |
| 265 | userAIFP->setGate({}); // clear any assignment |
| 266 | } |
| 267 | |
| 268 | userAircraft->clearATCController(); |
| 269 | } |
| 270 | |
| 271 | initControllers(); // critical for position-init logic |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | Adds FGATCController instance to std::vector activeStations. |
| 276 | FGATCController is a basic class for every controller |
| 277 | */ |
| 278 | void FGATCManager::addController(FGATCController *controller) { |
| 279 | activeStations.push_back(controller); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | Searches for and removes FGATCController instance from std::vector activeStations |
| 284 | */ |
| 285 | void FGATCManager::removeController(FGATCController *controller) |
| 286 | { |
| 287 | AtcVecIterator it; |
| 288 | it = std::find(activeStations.begin(), activeStations.end(), controller); |
| 289 | if (it != activeStations.end()) { |
| 290 | activeStations.erase(it); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void FGATCManager::signalReady() |
| 295 | { |
| 296 | auto aiManager = globals->get_subsystem<FGAIManager>(); |
| 297 | auto userAircraft = aiManager->getUserAircraft(); |
| 298 | if (userAircraft) { |
| 299 | auto userAIFP = userAircraft->GetFlightPlan(); |
| 300 | if (userAIFP && |
| 301 | fgGetBool("/sim/ai/enabled") && |
| 302 | userAIFP->getLeg() == AILeg::TAKEOFF && |
| 303 | userAircraft->getTakeOffStatus() == AITakeOffStatus::NONE) { |
| 304 | userAircraft->setTakeOffStatus(AITakeOffStatus::QUEUED); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | Update the subsystem. |
| 311 | FlightGear invokes this method every time the subsystem should |
| 312 | update its state. |
| 313 | |
| 314 | @param time The delta time, in seconds, since the last |
| 315 | update. On first update, delta time will be 0. |
| 316 | */ |
| 317 | void FGATCManager::update ( double time ) { |
| 318 | // SG_LOG(SG_ATC, SG_BULK, "ATC update code is running at time: " << time); |
| 319 | |
| 320 | if (splashAlpha->getDoubleValue() == 0.0 && startTime == 0) { |
| 321 | // start after the splash has disapeared and a few seconds |
| 322 | startTime = globals->get_sim_time_sec() + 1.0 + rand()%10; |
| 323 | } |
| 324 | if (startTime > 0 && startTime < globals->get_sim_time_sec()) { |
| 325 | signalReady(); |
| 326 | } |
| 327 | // Test code: let my virtual co-pilot handle ATC |
| 328 | auto aiManager = globals->get_subsystem<FGAIManager>(); |
| 329 | FGAIAircraft* user_ai_ac = aiManager->getUserAircraft(); |
| 330 | FGAIFlightPlan *fp = user_ai_ac->GetFlightPlan(); |
| 331 | |
| 332 | // Update destination |
| 333 | string result = _routeManagerDestinationAirportNode->getStringValue(); |
| 334 | |
| 335 | if (destination != result && result != "") { |
| 336 | destination = result; |
| 337 | userAircraftScheduledFlight->setArrivalAirport(destination); |
| 338 | userAircraftScheduledFlight->initializeAirports(); |
| 339 | userAircraftTrafficRef->clearAllFlights(); |
| 340 | userAircraftTrafficRef->assign(userAircraftScheduledFlight.get()); |
| 341 | |
| 342 | auto userAircraft = aiManager->getUserAircraft(); |
| 343 | userAircraft->setTrafficRef(userAircraftTrafficRef.get()); |
| 344 | } |
| 345 | |
| 346 | /* test code : find out how the routing develops */ |
| 347 | if (fp) { |
| 348 | int size = fp->getNrOfWayPoints(); |
| 349 | //SG_LOG(SG_ATC, SG_DEBUG, "Setting pos" << pos << " "); |
| 350 | //SG_LOG(SG_ATC, SG_DEBUG, "Setting intentions"); |
| 351 | // This indicates that we have run out of waypoints: Im future versions, the |
| 352 | // user should be able to select a new route, but for now just shut down the |
| 353 | // system. |
| 354 | if (size < 3) { |
| 355 | return; |
| 356 | } |
| 357 | #if 0 |
| 358 | // Test code: Print how far we're progressing along the taxi route. |
| 359 | SG_LOG(SG_ATC, SG_DEBUG, "Size of waypoint queue " << size); |
| 360 | for (int i = 0; i < size; i++) { |
| 361 | int val = fp->getRouteIndex(i); |
| 362 | SG_LOG(SG_ATC, SG_BULK, fp->getWayPoint(i)->getName() << " "); |
| 363 | //if ((val) && (val != pos)) { |
| 364 | // intentions.push_back(val); |
| 365 | SG_LOG(SG_ATC, SG_BULK, "[done ]"); |
| 366 | //} |
| 367 | } |
| 368 | SG_LOG(SG_ATC, SG_BULK, "[done ]"); |
| 369 | #endif |
| 370 | } |
| 371 | if (fp) { |
| 372 | SG_LOG(SG_ATC, SG_BULK, "User aircraft currently at leg : " << fp->getLeg()); |
| 373 | } |
| 374 | |
| 375 | // Call getATCController method; returns what FGATCController presently controls the user aircraft |
| 376 | // - e.g. FGStartupController |
| 377 | // controller = user_ai_ac->getATCController(); |
| 378 | // FIXME the AIAircraft currently doesn't set this for the user aircraft |
| 379 | |
| 380 | // Update the ATC dialog |
| 381 | //FGATCDialogNew::instance()->update(time); |
| 382 | |
| 383 | // Controller manager - if controller is set, then will update controller |
| 384 | if (controller) { |
| 385 | // SG_LOG(SG_ATC, SG_DEBUG, "name of previous waypoint : " << fp->getPreviousWaypoint()->getName()); |
| 386 | SG_LOG(SG_ATC, SG_BULK, "Currently under control of " << controller->getName()); |
| 387 | |
| 388 | // update aircraft information (simulates transponder) |
| 389 | |
| 390 | controller->updateAircraftInformation(user_ai_ac->getID(), |
| 391 | user_ai_ac->getGeodPos(), |
| 392 | user_ai_ac->_getHeading(), |
| 393 | user_ai_ac->getSpeed(), |
| 394 | user_ai_ac->getAltitude(), time); |
| 395 | |
| 396 | if (fp) { |
| 397 | switch (fp->getLeg()) { |
| 398 | case AILeg::STARTUP_PUSHBACK: // Startup and Push back |
| 399 | if (userAircraftTrafficRef->getDepartureAirport()->getDynamics()) |
| 400 | controller = userAircraftTrafficRef->getDepartureAirport()->getDynamics()->getStartupController(); |
| 401 | break; |
| 402 | case AILeg::TAXI: // Taxiing to runway |
| 403 | if (userAircraftTrafficRef->getDepartureAirport()->getDynamics()->getGroundController()->exists()) |
| 404 | controller = userAircraftTrafficRef->getDepartureAirport()->getDynamics()->getGroundController(); |
| 405 | break; |
| 406 | case AILeg::TAKEOFF: //Take off tower controller |
| 407 | if (userAircraftTrafficRef->getDepartureAirport()->getDynamics()) { |
| 408 | controller = userAircraftTrafficRef->getDepartureAirport()->getDynamics()->getTowerController(); |
| 409 | } else { |
| 410 | SG_LOG(SG_AI, SG_BULK, "Error: Could not find Dynamics at airport : " << userAircraftTrafficRef->getDepartureAirport()->getId()); |
| 411 | } |
| 412 | break; |
| 413 | /* TODO: link up with state system? |
| 414 | case AILeg::APPROACH: |
| 415 | if (userAircraftTrafficRef->getArrivalAirport()->getDynamics()) { |
| 416 | controller = trafficRef->getArrivalAirport()->getDynamics()->getApproachController(); |
| 417 | } |
| 418 | break; |
| 419 | case AILeg::PARKING_TAXI: // Taxiing for parking |
| 420 | if (trafficRef->getArrivalAirport()->getDynamics()->getGroundController()->exists()) |
| 421 | controller = trafficRef->getArrivalAirport()->getDynamics()->getGroundController(); |
| 422 | break; |
| 423 | */ |
| 424 | default: |
| 425 | if(prevController) { |
| 426 | SG_LOG(SG_AI, SG_BULK, "Will be signing off user ai " << user_ai_ac->getID() << " from " << prevController->getName()); |
| 427 | } |
| 428 | controller = nullptr; |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | if ((controller != prevController) && prevController && !user_ai_ac->getDie()) { |
| 433 | //If we are dead we are automatically erased |
| 434 | prevController->signOff(user_ai_ac->getID()); |
| 435 | } |
| 436 | prevController = controller; |
| 437 | } |
| 438 | |
| 439 | //string airport = fgGetString("/sim/presets/airport-id"); |
| 440 | //FGAirport *apt = FGAirport::findByIdent(airport); |
| 441 | // AT this stage we should update the flightplan, so that waypoint incrementing is conducted as well as leg loading. |
| 442 | |
| 443 | // Ground network visibility: |
| 444 | // a) check to see if the message to toggle visibility was called |
| 445 | // b) if so, toggle network visibility and reset the transmission |
| 446 | // c) thereafter disable rendering for the old controller (TODO: should this be earlier?) |
| 447 | // d) and render if enabled for the new controller |
| 448 | int n = transNum->getIntValue(); |
| 449 | |
| 450 | if (n == 1) { |
| 451 | SG_LOG(SG_ATC, SG_DEBUG, "Toggling ground network visibility " << networkVisible); |
| 452 | networkVisible = !networkVisible; |
| 453 | transNum->setIntValue(-1); |
| 454 | } |
| 455 | |
| 456 | // stop rendering the old controller's groundnetwork |
| 457 | if ((controller != prevController) && (prevController)) { |
| 458 | prevController->render(false); |
| 459 | } |
| 460 | |
| 461 | if (controller) { |
| 462 | // render the path for the present controller if the ground network is set to visible |
| 463 | controller->render(networkVisible); |
| 464 | SG_LOG(SG_ATC, SG_BULK, "Adding ground network to the scenegraph::update"); |
| 465 | } |
| 466 | |
| 467 | // reset previous controller for next update() iteration |
| 468 | prevController = controller; |
| 469 | } |
| 470 | |
| 471 | // update the active ATC stations |
| 472 | for (AtcVecIterator atc = activeStations.begin(); atc != activeStations.end(); ++atc) { |
| 473 | (*atc)->update(time); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | |
| 478 | // Register the subsystem. |
| 479 | SGSubsystemMgr::Registrant<FGATCManager> registrantFGATCManager( |
| 480 | SGSubsystemMgr::POST_FDM, |
| 481 | {{"FGAIManager", SGSubsystemMgr::Dependency::HARD}}); |