| 1 | /* |
| 2 | * SPDX-FileName: environment_mgr.cxx |
| 3 | * SPDX-FileComment: manager for natural environment information |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2002 David Megginson - [email protected] |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | # include <config.h> |
| 10 | #endif |
| 11 | |
| 12 | #include <cstring> |
| 13 | |
| 14 | #include <simgear/constants.h> |
| 15 | #include <simgear/debug/logstream.hxx> |
| 16 | |
| 17 | #include <simgear/scene/sky/sky.hxx> |
| 18 | #include <simgear/scene/model/particles.hxx> |
| 19 | #include <simgear/structure/event_mgr.hxx> |
| 20 | |
| 21 | #include <Main/main.hxx> |
| 22 | #include <Main/fg_props.hxx> |
| 23 | #include <Viewer/renderer.hxx> |
| 24 | #include <Viewer/ViewPropertyEvaluator.hxx> |
| 25 | |
| 26 | #include <FDM/flight.hxx> |
| 27 | |
| 28 | #include "environment.hxx" |
| 29 | #include "environment_mgr.hxx" |
| 30 | #include "environment_ctrl.hxx" |
| 31 | #include "realwx_ctrl.hxx" |
| 32 | #include "fgclouds.hxx" |
| 33 | #include "precipitation_mgr.hxx" |
| 34 | #include "ridge_lift.hxx" |
| 35 | #include "terrainsampler.hxx" |
| 36 | #include "Airports/airport.hxx" |
| 37 | #include "gravity.hxx" |
| 38 | #include "climate.hxx" |
| 39 | #include "magvarmanager.hxx" |
| 40 | |
| 41 | #include "AIModel/AINotifications.hxx" |
| 42 | |
| 43 | class FG3DCloudsListener : public SGPropertyChangeListener { |
| 44 | public: |
| 45 | FG3DCloudsListener( FGClouds * fgClouds ); |
| 46 | virtual ~FG3DCloudsListener(); |
| 47 | |
| 48 | virtual void valueChanged (SGPropertyNode * node); |
| 49 | |
| 50 | private: |
| 51 | FGClouds * _fgClouds; |
| 52 | SGPropertyNode_ptr _enableNode; |
| 53 | }; |
| 54 | |
| 55 | FG3DCloudsListener::FG3DCloudsListener( FGClouds * fgClouds ) : |
| 56 | _fgClouds( fgClouds ) |
| 57 | { |
| 58 | _enableNode = fgGetNode( "/sim/rendering/clouds3d-enable", true ); |
| 59 | _enableNode->addChangeListener( this ); |
| 60 | |
| 61 | valueChanged( _enableNode ); |
| 62 | } |
| 63 | |
| 64 | FG3DCloudsListener::~FG3DCloudsListener() |
| 65 | { |
| 66 | _enableNode->removeChangeListener( this ); |
| 67 | } |
| 68 | |
| 69 | void FG3DCloudsListener::valueChanged( SGPropertyNode * node ) |
| 70 | { |
| 71 | _fgClouds->set_3dClouds( _enableNode->getBoolValue() ); |
| 72 | } |
| 73 | |
| 74 | FGEnvironmentMgr::FGEnvironmentMgr() : _environment(new FGEnvironment()), |
| 75 | _sky(globals->get_renderer()->getSky()), |
| 76 | nearestCarrier(nullptr), |
| 77 | nearestAirport(nullptr) |
| 78 | { |
| 79 | fgClouds = new FGClouds; |
| 80 | _3dCloudsEnableListener = new FG3DCloudsListener(fgClouds); |
| 81 | set_subsystem("controller", Environment::LayerInterpolateController::createInstance( fgGetNode("/environment/config", true ) )); |
| 82 | |
| 83 | set_subsystem("climate", new FGClimate); |
| 84 | set_subsystem("precipitation", new FGPrecipitationMgr); |
| 85 | set_subsystem("realwx", Environment::RealWxController::createInstance( fgGetNode("/environment/realwx", true ) ), 1.0 ); |
| 86 | set_subsystem("terrainsampler", Environment::TerrainSampler::createInstance( fgGetNode("/environment/terrain", true ) )); |
| 87 | set_subsystem("ridgelift", new FGRidgeLift); |
| 88 | |
| 89 | set_subsystem("magvar", new FGMagVarManager); |
| 90 | max_tower_height_feet = fgGetDouble("/sim/airport/max-tower-height-ft", 70); |
| 91 | min_tower_height_feet = fgGetDouble("/sim/airport/min-tower-height-ft", 6); |
| 92 | default_tower_height_feet = fgGetDouble("default-tower-height-ft", 30); |
| 93 | } |
| 94 | |
| 95 | FGEnvironmentMgr::~FGEnvironmentMgr () |
| 96 | { |
| 97 | remove_subsystem( "ridgelift" ); |
| 98 | remove_subsystem( "terrainsampler" ); |
| 99 | remove_subsystem("precipitation"); |
| 100 | remove_subsystem("realwx"); |
| 101 | remove_subsystem("controller"); |
| 102 | remove_subsystem("magvar"); |
| 103 | |
| 104 | delete fgClouds; |
| 105 | delete _3dCloudsEnableListener; |
| 106 | delete _environment; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | struct FGEnvironmentMgrPropertyListener : SGPropertyChangeListener { |
| 111 | FGEnvironmentMgrPropertyListener(FGEnvironmentMgr* environmentmgr) : _environmentmgr(environmentmgr) |
| 112 | { |
| 113 | _modelViewNode = fgGetNode("/sim/current-view/model-view", true /*create*/); |
| 114 | _modelViewNode->addChangeListener(this); |
| 115 | _viewNumberNode = fgGetNode("/sim/current-view/view-number-raw", true /*create*/); |
| 116 | _viewNumberNode->addChangeListener(this); |
| 117 | _airportIdNode = fgGetNode("/sim/tower/airport-id", true /*create*/); |
| 118 | _airportIdNode->addChangeListener(this); |
| 119 | _autoTowerNode = fgGetNode("/sim/tower/auto-position", true /*create*/); |
| 120 | _autoTowerNode->addChangeListener(this); |
| 121 | } |
| 122 | |
| 123 | void valueChanged(SGPropertyNode* node) override |
| 124 | { |
| 125 | if ((node == _modelViewNode) || (_autoTowerNode == node)) { |
| 126 | _environmentmgr->updateClosestAirport(); |
| 127 | } |
| 128 | |
| 129 | if ((node == _viewNumberNode) || (node == _airportIdNode)) { |
| 130 | _environmentmgr->onTowerAirportIDChanged(); |
| 131 | } |
| 132 | } |
| 133 | virtual ~FGEnvironmentMgrPropertyListener() |
| 134 | { |
| 135 | _modelViewNode->removeChangeListener(this); |
| 136 | _viewNumberNode->removeChangeListener(this); |
| 137 | _airportIdNode->removeChangeListener(this); |
| 138 | _airportIdNode->removeChangeListener(this); |
| 139 | } |
| 140 | private: |
| 141 | FGEnvironmentMgr* _environmentmgr; |
| 142 | SGPropertyNode_ptr _modelViewNode; |
| 143 | SGPropertyNode_ptr _viewNumberNode; |
| 144 | SGPropertyNode_ptr _airportIdNode; |
| 145 | SGPropertyNode_ptr _autoTowerNode; |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | SGSubsystem::InitStatus FGEnvironmentMgr::incrementalInit() |
| 150 | { |
| 151 | |
| 152 | InitStatus r = SGSubsystemGroup::incrementalInit(); |
| 153 | if (r == INIT_DONE) { |
| 154 | fgClouds->Init(); |
| 155 | _listener.reset(new FGEnvironmentMgrPropertyListener(this)); |
| 156 | globals->get_event_mgr()->addTask("updateClosestAirport", |
| 157 | [this](){ this->updateClosestAirport(); }, 10 ); |
| 158 | } |
| 159 | |
| 160 | return r; |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | FGEnvironmentMgr::shutdown() |
| 165 | { |
| 166 | globals->get_event_mgr()->removeTask("updateClosestAirport"); |
| 167 | _listener.reset(); |
| 168 | SGSubsystemGroup::shutdown(); |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | FGEnvironmentMgr::reinit () |
| 173 | { |
| 174 | SG_LOG( SG_ENVIRONMENT, SG_INFO, "Reinitializing environment subsystem"); |
| 175 | SGSubsystemGroup::reinit(); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | FGEnvironmentMgr::bind () |
| 180 | { |
| 181 | SGSubsystemGroup::bind(); |
| 182 | _environment->Tie( fgGetNode("/environment", true ) ); |
| 183 | |
| 184 | _tiedProperties.setRoot( fgGetNode( "/environment", true ) ); |
| 185 | |
| 186 | _tiedProperties.Tie( "effective-visibility-m", _sky, |
| 187 | &SGSky::get_visibility ); |
| 188 | |
| 189 | _tiedProperties.Tie("rebuild-layers", fgClouds, |
| 190 | &FGClouds::get_update_event, |
| 191 | &FGClouds::set_update_event); |
| 192 | // _tiedProperties.Tie("turbulence/use-cloud-turbulence", &sgEnviro, |
| 193 | // &SGEnviro::get_turbulence_enable_state, |
| 194 | // &SGEnviro::set_turbulence_enable_state); |
| 195 | |
| 196 | for (int i = 0; i < MAX_CLOUD_LAYERS; i++) { |
| 197 | SGPropertyNode_ptr layerNode = fgGetNode("/environment/clouds",true)->getChild("layer", i, true ); |
| 198 | |
| 199 | _tiedProperties.Tie( layerNode->getNode("span-m",true), this, i, |
| 200 | &FGEnvironmentMgr::get_cloud_layer_span_m, |
| 201 | &FGEnvironmentMgr::set_cloud_layer_span_m); |
| 202 | |
| 203 | _tiedProperties.Tie( layerNode->getNode("elevation-ft",true), this, i, |
| 204 | &FGEnvironmentMgr::get_cloud_layer_elevation_ft, |
| 205 | &FGEnvironmentMgr::set_cloud_layer_elevation_ft); |
| 206 | |
| 207 | _tiedProperties.Tie( layerNode->getNode("thickness-ft",true), this, i, |
| 208 | &FGEnvironmentMgr::get_cloud_layer_thickness_ft, |
| 209 | &FGEnvironmentMgr::set_cloud_layer_thickness_ft); |
| 210 | |
| 211 | _tiedProperties.Tie( layerNode->getNode("transition-ft",true), this, i, |
| 212 | &FGEnvironmentMgr::get_cloud_layer_transition_ft, |
| 213 | &FGEnvironmentMgr::set_cloud_layer_transition_ft); |
| 214 | |
| 215 | _tiedProperties.Tie( layerNode->getNode("coverage",true), this, i, |
| 216 | &FGEnvironmentMgr::get_cloud_layer_coverage, |
| 217 | &FGEnvironmentMgr::set_cloud_layer_coverage); |
| 218 | |
| 219 | _tiedProperties.Tie( layerNode->getNode("coverage-type",true), this, i, |
| 220 | &FGEnvironmentMgr::get_cloud_layer_coverage_type, |
| 221 | &FGEnvironmentMgr::set_cloud_layer_coverage_type); |
| 222 | |
| 223 | _tiedProperties.Tie( layerNode->getNode( "visibility-m",true), this, i, |
| 224 | &FGEnvironmentMgr::get_cloud_layer_visibility_m, |
| 225 | &FGEnvironmentMgr::set_cloud_layer_visibility_m); |
| 226 | |
| 227 | _tiedProperties.Tie( layerNode->getNode( "alpha",true), this, i, |
| 228 | &FGEnvironmentMgr::get_cloud_layer_maxalpha, |
| 229 | &FGEnvironmentMgr::set_cloud_layer_maxalpha); |
| 230 | } |
| 231 | |
| 232 | _tiedProperties.setRoot( fgGetNode("/sim/rendering", true ) ); |
| 233 | |
| 234 | _tiedProperties.Tie( "clouds3d-density", _sky, |
| 235 | &SGSky::get_3dCloudDensity, |
| 236 | &SGSky::set_3dCloudDensity); |
| 237 | |
| 238 | _tiedProperties.Tie("clouds3d-vis-range", _sky, |
| 239 | &SGSky::get_3dCloudVisRange, |
| 240 | &SGSky::set_3dCloudVisRange); |
| 241 | |
| 242 | _tiedProperties.Tie("clouds3d-impostor-range", _sky, |
| 243 | &SGSky::get_3dCloudImpostorDistance, |
| 244 | &SGSky::set_3dCloudImpostorDistance); |
| 245 | |
| 246 | _tiedProperties.Tie("clouds3d-lod1-range", _sky, |
| 247 | &SGSky::get_3dCloudLoD1Range, |
| 248 | &SGSky::set_3dCloudLoD1Range); |
| 249 | |
| 250 | _tiedProperties.Tie("clouds3d-lod2-range", _sky, |
| 251 | &SGSky::get_3dCloudLoD2Range, |
| 252 | &SGSky::set_3dCloudLoD2Range); |
| 253 | |
| 254 | _tiedProperties.Tie("clouds3d-wrap", _sky, |
| 255 | &SGSky::get_3dCloudWrap, |
| 256 | &SGSky::set_3dCloudWrap); |
| 257 | |
| 258 | _tiedProperties.Tie("clouds3d-use-impostors", _sky, |
| 259 | &SGSky::get_3dCloudUseImpostors, |
| 260 | &SGSky::set_3dCloudUseImpostors); |
| 261 | |
| 262 | _automaticTowerEnableNode = fgGetNode("/sim/tower/auto-position", true); |
| 263 | } |
| 264 | |
| 265 | void |
| 266 | FGEnvironmentMgr::unbind () |
| 267 | { |
| 268 | _tiedProperties.Untie(); |
| 269 | _environment->Untie(); |
| 270 | SGSubsystemGroup::unbind(); |
| 271 | } |
| 272 | |
| 273 | void |
| 274 | FGEnvironmentMgr::update (double dt) |
| 275 | { |
| 276 | SGGeod aircraftPos(globals->get_aircraft_position()); |
| 277 | |
| 278 | SGSubsystemGroup::update(dt); |
| 279 | |
| 280 | _environment->set_elevation_ft( aircraftPos.getElevationFt() ); |
| 281 | |
| 282 | auto particlesManager = simgear::ParticlesGlobalManager::instance(); |
| 283 | particlesManager->setWindFrom(_environment->get_wind_from_heading_deg(), |
| 284 | _environment->get_wind_speed_kt()); |
| 285 | particlesManager->update(dt, globals->get_aircraft_position()); |
| 286 | |
| 287 | if( _cloudLayersDirty ) { |
| 288 | _cloudLayersDirty = false; |
| 289 | fgClouds->set_update_event( fgClouds->get_update_event()+1 ); |
| 290 | } |
| 291 | updateDynamicTowerPosition(); |
| 292 | |
| 293 | fgSetDouble( "/environment/gravitational-acceleration-mps2", |
| 294 | Environment::Gravity::instance()->getGravity(aircraftPos)); |
| 295 | } |
| 296 | |
| 297 | void FGEnvironmentMgr::onTowerAirportIDChanged() |
| 298 | { |
| 299 | FGAirportRef apt; |
| 300 | const auto automaticTowerActive = _automaticTowerEnableNode->getBoolValue(); |
| 301 | |
| 302 | if (automaticTowerActive) { |
| 303 | apt = FGAirport::findByIdent(fgGetString("/sim/airport/closest-airport-id")); |
| 304 | } else { |
| 305 | apt = FGAirport::findByIdent(fgGetString("/sim/tower/airport-id")); |
| 306 | } |
| 307 | |
| 308 | if (!apt) { |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | SGGeod towerPos; |
| 313 | if (apt->hasTower()) { |
| 314 | towerPos = apt->getTowerLocation(); |
| 315 | SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "airport-id=" << apt->getId() << " tower_pos=" << towerPos); |
| 316 | } else { |
| 317 | towerPos = apt->geod(); |
| 318 | SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "no tower for airport-id=" << apt->getId()); |
| 319 | } |
| 320 | //Ensure that the tower isn't at ground level by adding a nominal amount |
| 321 | // TODO: (fix the data so that too short or too tall towers aren't present in the data) |
| 322 | auto towerAirpotDistance = abs(towerPos.getElevationFt() - apt->geod().getElevationFt()); |
| 323 | if (towerAirpotDistance < min_tower_height_feet) { |
| 324 | towerPos.setElevationFt(towerPos.getElevationFt() + default_tower_height_feet); |
| 325 | SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "Tower altitude adjusted because it was at below minimum height above ground (" << min_tower_height_feet << "feet) for airport " << apt->getId()); |
| 326 | } else if (towerAirpotDistance > max_tower_height_feet) { |
| 327 | towerPos.setElevationFt(towerPos.getElevationFt() + default_tower_height_feet); |
| 328 | SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "Tower altitude adjusted because it was taller than the permitted maximum of (" << max_tower_height_feet << "feet) for airport " << apt->getId()); |
| 329 | } |
| 330 | |
| 331 | std::string path = ViewPropertyEvaluator::getStringValue("(/sim/view[(/sim/current-view/view-number-raw)]/config/root)/sim/tower/"); |
| 332 | |
| 333 | fgSetDouble(path + "latitude-deg", towerPos.getLatitudeDeg()); |
| 334 | fgSetDouble(path + "longitude-deg", towerPos.getLongitudeDeg()); |
| 335 | fgSetDouble(path + "altitude-ft", towerPos.getElevationFt()); |
| 336 | } |
| 337 | |
| 338 | void FGEnvironmentMgr::updateDynamicTowerPosition() |
| 339 | { |
| 340 | if (towerViewPositionLatDegNode != nullptr && towerViewPositionLonDegNode != nullptr && towerViewPositionAltFtNode != nullptr) { |
| 341 | const auto automaticTowerActive = _automaticTowerEnableNode->getBoolValue(); |
| 342 | |
| 343 | fgSetDouble("/sim/airport/nearest-tower-latitude-deg", towerViewPositionLatDegNode->getDoubleValue()); |
| 344 | fgSetDouble("/sim/airport/nearest-tower-longitude-deg", towerViewPositionLonDegNode->getDoubleValue()); |
| 345 | fgSetDouble("/sim/airport/nearest-tower-altitude-ft", towerViewPositionAltFtNode->getDoubleValue()); |
| 346 | |
| 347 | if (automaticTowerActive) { |
| 348 | fgSetDouble("/sim/tower/latitude-deg", towerViewPositionLatDegNode->getDoubleValue()); |
| 349 | fgSetDouble("/sim/tower/longitude-deg", towerViewPositionLonDegNode->getDoubleValue()); |
| 350 | fgSetDouble("/sim/tower/altitude-ft", towerViewPositionAltFtNode->getDoubleValue()); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void FGEnvironmentMgr::updateClosestAirport() |
| 356 | { |
| 357 | SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "FGEnvironmentMgr::update: updating closest airport"); |
| 358 | |
| 359 | SGGeod pos = globals->get_aircraft_position(); |
| 360 | |
| 361 | // |
| 362 | // If we are viewing a multiplayer aircraft, find nearest airport so that |
| 363 | // Tower View etc works. |
| 364 | std::string view_config_root = ViewPropertyEvaluator::getStringValue("(/sim/view[(/sim/current-view/view-number-raw)]/config/root)"); |
| 365 | |
| 366 | if (view_config_root != "/" && view_config_root != "") { |
| 367 | /* We are currently viewing a multiplayer aircraft. */ |
| 368 | pos = SGGeod::fromDegFt( |
| 369 | ViewPropertyEvaluator::getDoubleValue("((/sim/view[(/sim/current-view/view-number-raw)]/config/root)/position/longitude-deg)"), |
| 370 | ViewPropertyEvaluator::getDoubleValue("((/sim/view[(/sim/current-view/view-number-raw)]/config/root)/position/latitude-deg)"), |
| 371 | ViewPropertyEvaluator::getDoubleValue("((/sim/view[(/sim/current-view/view-number-raw)]/config/root)/position/altitude-ft)")); |
| 372 | } |
| 373 | |
| 374 | // nearest tower logic; |
| 375 | // 1. find nearest airport |
| 376 | // 2. find nearest carrier |
| 377 | // - select the nearest one as the tower. |
| 378 | |
| 379 | auto nearestAirport = FGAirport::findClosest(pos, 100.0); |
| 380 | const auto automaticTowerActive = _automaticTowerEnableNode->getBoolValue(); |
| 381 | |
| 382 | SGGeod nearestTowerPosition; |
| 383 | std::string nearestIdent; |
| 384 | double towerDistance = std::numeric_limits<double>::max(); |
| 385 | if (nearestAirport) { |
| 386 | const std::string currentId = fgGetString("/sim/airport/closest-airport-id", ""); |
| 387 | if (currentId != nearestAirport->ident()) { |
| 388 | SG_LOG(SG_ENVIRONMENT, SG_INFO, "FGEnvironmentMgr::updateClosestAirport: selected:" << nearestAirport->ident()); |
| 389 | fgSetString("/sim/airport/closest-airport-id", nearestAirport->ident().c_str()); |
| 390 | } |
| 391 | |
| 392 | nearestTowerPosition = nearestAirport->geod(); |
| 393 | nearestIdent = nearestAirport->ident(); |
| 394 | towerDistance = SGGeodesy::distanceM(nearestTowerPosition, pos); |
| 395 | |
| 396 | // clear these so we don't do dynamic updates unless a carrier is active |
| 397 | towerViewPositionLatDegNode = towerViewPositionLonDegNode = towerViewPositionAltFtNode = nullptr; |
| 398 | } |
| 399 | else { |
| 400 | SG_LOG(SG_ENVIRONMENT, SG_INFO, "FGEnvironmentMgr::update: No airport within 100NM range"); |
| 401 | } |
| 402 | |
| 403 | // check for closer carrier |
| 404 | auto nctn = SGSharedPtr< NearestCarrierToNotification> (new NearestCarrierToNotification(pos)); |
| 405 | if (simgear::Emesary::ReceiptStatus::OK == simgear::Emesary::GlobalTransmitter::instance()->NotifyAll(nctn)) { |
| 406 | if (nearestCarrier != nctn->GetCarrier()) { |
| 407 | nearestCarrier = nctn->GetCarrier(); |
| 408 | fgSetString("/sim/airport/nearest-carrier", nctn->GetCarrierIdent()); |
| 409 | } |
| 410 | } else { |
| 411 | fgSetString("/sim/airport/nearest-carrier", ""); |
| 412 | fgSetDouble("/sim/airport/nearest-carrier-latitude-deg", 0); |
| 413 | fgSetDouble("/sim/airport/nearest-carrier-longitude-deg", 0); |
| 414 | fgSetDouble("/sim/airport/nearest-carrier-altitude-ft", 0); |
| 415 | fgSetDouble("/sim/airport/nearest-carrier-deck-height", 0); |
| 416 | nearestCarrier = nullptr; |
| 417 | } |
| 418 | |
| 419 | // figure out if the carrier's tower is closer |
| 420 | if (nearestCarrier && (nctn->GetDistanceMeters() < towerDistance)) { |
| 421 | nearestIdent = nctn->GetCarrierIdent(); |
| 422 | |
| 423 | // |
| 424 | // these will be used to determine and update the tower position |
| 425 | towerViewPositionLatDegNode = nctn->GetViewPositionLatNode(); |
| 426 | towerViewPositionLonDegNode = nctn->GetViewPositionLonNode(); |
| 427 | towerViewPositionAltFtNode = nctn->GetViewPositionAltNode(); |
| 428 | |
| 429 | // although the carrier is moving - these values can afford to be 10 seconds old so we don't need to |
| 430 | // update them. |
| 431 | fgSetDouble("/sim/airport/nearest-carrier-latitude-deg", nctn->GetPosition()->getLatitudeDeg()); |
| 432 | fgSetDouble("/sim/airport/nearest-carrier-longitude-deg", nctn->GetPosition()->getLongitudeDeg()); |
| 433 | fgSetDouble("/sim/airport/nearest-carrier-altitude-ft", nctn->GetPosition()->getElevationFt()); |
| 434 | fgSetDouble("/sim/airport/nearest-carrier-deck-height", nctn->GetDeckheight()); |
| 435 | } |
| 436 | |
| 437 | if (fgGetString("/sim/airport/nearest-tower-ident") != nearestIdent) { |
| 438 | SG_LOG(SG_ENVIRONMENT, SG_INFO, "Nearest airport tower now " << nearestIdent); |
| 439 | fgSetString("/sim/airport/nearest-tower-ident", nearestIdent); |
| 440 | } |
| 441 | if (automaticTowerActive) { |
| 442 | if (fgGetString("/sim/tower/airport-id") != nearestIdent) { |
| 443 | fgSetString("/sim/tower/airport-id", nearestIdent); |
| 444 | SG_LOG(SG_ENVIRONMENT, SG_INFO, "Auto Tower: now " << nearestIdent); |
| 445 | } |
| 446 | |
| 447 | std::string path = ViewPropertyEvaluator::getStringValue("(/sim/view[(/sim/current-view/view-number-raw)]/config/root)/sim/tower/"); |
| 448 | auto currentViewAirportIdNode = fgGetNode(path + "airport-id", true); |
| 449 | const auto curId = currentViewAirportIdNode->getStringValue(); |
| 450 | if (curId != nearestIdent) { |
| 451 | currentViewAirportIdNode->setStringValue(nearestIdent); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | updateDynamicTowerPosition(); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | FGEnvironment |
| 460 | FGEnvironmentMgr::getEnvironment () const |
| 461 | { |
| 462 | return *_environment; |
| 463 | } |
| 464 | |
| 465 | const FGEnvironment* FGEnvironmentMgr::getAircraftEnvironment() const |
| 466 | { |
| 467 | return _environment; |
| 468 | } |
| 469 | |
| 470 | FGEnvironment |
| 471 | FGEnvironmentMgr::getEnvironmentAtPosition(const SGGeod& aPos) const |
| 472 | { |
| 473 | // Always returns the same environment |
| 474 | // for now; we'll make it interesting |
| 475 | // later. |
| 476 | FGEnvironment env = *_environment; |
| 477 | env.set_elevation_ft(aPos.getElevationFt()); |
| 478 | return env; |
| 479 | |
| 480 | } |
| 481 | |
| 482 | double |
| 483 | FGEnvironmentMgr::get_cloud_layer_span_m (int index) const |
| 484 | { |
| 485 | return _sky->get_cloud_layer(index)->getSpan_m(); |
| 486 | } |
| 487 | |
| 488 | void |
| 489 | FGEnvironmentMgr::set_cloud_layer_span_m (int index, double span_m) |
| 490 | { |
| 491 | _sky->get_cloud_layer(index)->setSpan_m(span_m); |
| 492 | } |
| 493 | |
| 494 | double |
| 495 | FGEnvironmentMgr::get_cloud_layer_elevation_ft (int index) const |
| 496 | { |
| 497 | return _sky->get_cloud_layer(index)->getElevation_m() * SG_METER_TO_FEET; |
| 498 | } |
| 499 | |
| 500 | void |
| 501 | FGEnvironmentMgr::set_cloud_layer_elevation_ft (int index, double elevation_ft) |
| 502 | { |
| 503 | FGEnvironment env = *_environment; |
| 504 | env.set_elevation_ft(elevation_ft); |
| 505 | |
| 506 | _sky->get_cloud_layer(index) |
| 507 | ->setElevation_m(elevation_ft * SG_FEET_TO_METER); |
| 508 | |
| 509 | _sky->get_cloud_layer(index) |
| 510 | ->setSpeed(env.get_wind_speed_kt() * 0.5151); // 1 kt = 0.5151 m/s |
| 511 | |
| 512 | _sky->get_cloud_layer(index) |
| 513 | ->setDirection(env.get_wind_from_heading_deg()); |
| 514 | } |
| 515 | |
| 516 | double |
| 517 | FGEnvironmentMgr::get_cloud_layer_thickness_ft (int index) const |
| 518 | { |
| 519 | return _sky->get_cloud_layer(index)->getThickness_m() * SG_METER_TO_FEET; |
| 520 | } |
| 521 | |
| 522 | void |
| 523 | FGEnvironmentMgr::set_cloud_layer_thickness_ft (int index, double thickness_ft) |
| 524 | { |
| 525 | _sky->get_cloud_layer(index) |
| 526 | ->setThickness_m(thickness_ft * SG_FEET_TO_METER); |
| 527 | } |
| 528 | |
| 529 | double |
| 530 | FGEnvironmentMgr::get_cloud_layer_transition_ft (int index) const |
| 531 | { |
| 532 | return _sky->get_cloud_layer(index)->getTransition_m() * SG_METER_TO_FEET; |
| 533 | } |
| 534 | |
| 535 | void |
| 536 | FGEnvironmentMgr::set_cloud_layer_transition_ft (int index, |
| 537 | double transition_ft) |
| 538 | { |
| 539 | _sky->get_cloud_layer(index) |
| 540 | ->setTransition_m(transition_ft * SG_FEET_TO_METER); |
| 541 | } |
| 542 | |
| 543 | const char * |
| 544 | FGEnvironmentMgr::get_cloud_layer_coverage (int index) const |
| 545 | { |
| 546 | return _sky->get_cloud_layer(index)->getCoverageString().c_str(); |
| 547 | } |
| 548 | |
| 549 | void |
| 550 | FGEnvironmentMgr::set_cloud_layer_coverage (int index, |
| 551 | const char * coverage_name) |
| 552 | { |
| 553 | if( _sky->get_cloud_layer(index)->getCoverageString() == coverage_name ) |
| 554 | return; |
| 555 | |
| 556 | _sky->get_cloud_layer(index)->setCoverageString(coverage_name); |
| 557 | _cloudLayersDirty = true; |
| 558 | } |
| 559 | |
| 560 | int |
| 561 | FGEnvironmentMgr::get_cloud_layer_coverage_type (int index) const |
| 562 | { |
| 563 | return _sky->get_cloud_layer(index)->getCoverage(); |
| 564 | } |
| 565 | |
| 566 | double |
| 567 | FGEnvironmentMgr::get_cloud_layer_visibility_m (int index) const |
| 568 | { |
| 569 | return _sky->get_cloud_layer(index)->getVisibility_m(); |
| 570 | } |
| 571 | |
| 572 | void |
| 573 | FGEnvironmentMgr::set_cloud_layer_visibility_m (int index, double visibility_m) |
| 574 | { |
| 575 | _sky->get_cloud_layer(index)->setVisibility_m(visibility_m); |
| 576 | } |
| 577 | |
| 578 | double |
| 579 | FGEnvironmentMgr::get_cloud_layer_maxalpha (int index ) const |
| 580 | { |
| 581 | return _sky->get_cloud_layer(index)->getMaxAlpha(); |
| 582 | } |
| 583 | |
| 584 | void |
| 585 | FGEnvironmentMgr::set_cloud_layer_maxalpha (int index, double maxalpha) |
| 586 | { |
| 587 | _sky->get_cloud_layer(index)->setMaxAlpha(maxalpha); |
| 588 | } |
| 589 | |
| 590 | void |
| 591 | FGEnvironmentMgr::set_cloud_layer_coverage_type (int index, int type ) |
| 592 | { |
| 593 | if( type < 0 || type >= SGCloudLayer::SG_MAX_CLOUD_COVERAGES ) { |
| 594 | SG_LOG(SG_ENVIRONMENT,SG_WARN,"Unknown cloud layer type " << type << " ignored" ); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | if( static_cast<SGCloudLayer::Coverage>(type) == _sky->get_cloud_layer(index)->getCoverage() ) |
| 599 | return; |
| 600 | |
| 601 | _sky->get_cloud_layer(index)->setCoverage(static_cast<SGCloudLayer::Coverage>(type)); |
| 602 | _cloudLayersDirty = true; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | // Register the subsystem. |
| 607 | SGSubsystemMgr::Registrant<FGEnvironmentMgr> registrantFGEnvironmentMgr; |
| 608 | |
| 609 | // end of environment-mgr.cxx |