1 // fg_init.cxx -- Flight Gear top level initialization routines
3 // Written by Curtis Olson, started August 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
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 <string.h> // strcmp()
32 #if defined( unix ) || defined( __CYGWIN__ )
33 # include <unistd.h> // for gethostname()
36 # include <direct.h> // for getcwd()
37 # define getcwd _getcwd
38 # include <io.h> // isatty()
39 # define isatty _isatty
40 # include "winsock2.h" // for gethostname()
43 // work around a stdc++ lib bug in some versions of linux, but doesn't
44 // seem to hurt to have this here for all versions of Linux.
46 # define _G_NO_EXTERN_TEMPLATES
49 #include <simgear/compiler.h>
52 #include <boost/algorithm/string/compare.hpp>
53 #include <boost/algorithm/string/predicate.hpp>
55 #include <simgear/constants.h>
56 #include <simgear/debug/logstream.hxx>
57 #include <simgear/structure/exception.hxx>
58 #include <simgear/structure/event_mgr.hxx>
59 #include <simgear/misc/sg_path.hxx>
60 #include <simgear/misc/sg_dir.hxx>
61 #include <simgear/misc/sgstream.hxx>
62 #include <simgear/misc/strutils.hxx>
63 #include <simgear/props/props_io.hxx>
65 #include <simgear/misc/interpolator.hxx>
66 #include <simgear/scene/material/matlib.hxx>
67 #include <simgear/scene/model/particles.hxx>
68 #include <simgear/sound/soundmgr_openal.hxx>
70 #include <Aircraft/controls.hxx>
71 #include <Aircraft/replay.hxx>
72 #include <Airports/apt_loader.hxx>
73 #include <Airports/runways.hxx>
74 #include <Airports/simple.hxx>
75 #include <Airports/dynamics.hxx>
77 #include <AIModel/AIManager.hxx>
79 #include <ATCDCL/ATCmgr.hxx>
80 #include <ATC/atc_mgr.hxx>
82 #include <Autopilot/route_mgr.hxx>
83 #include <Autopilot/autopilotgroup.hxx>
85 #include <Cockpit/panel.hxx>
86 #include <Cockpit/panel_io.hxx>
88 #include <GUI/new_gui.hxx>
89 #include <Input/input.hxx>
90 #include <Instrumentation/instrument_mgr.hxx>
91 #include <Model/acmodel.hxx>
92 #include <Model/modelmgr.hxx>
93 #include <AIModel/submodel.hxx>
94 #include <AIModel/AIManager.hxx>
95 #include <Navaids/navdb.hxx>
96 #include <Navaids/navlist.hxx>
97 #include <Navaids/fix.hxx>
98 #include <Navaids/fixlist.hxx>
99 #include <Navaids/airways.hxx>
100 #include <Scenery/scenery.hxx>
101 #include <Scenery/tilemgr.hxx>
102 #include <Scripting/NasalSys.hxx>
103 #include <Sound/voice.hxx>
104 #include <Systems/system_mgr.hxx>
105 #include <Time/light.hxx>
106 #include <Traffic/TrafficMgr.hxx>
107 #include <MultiPlayer/multiplaymgr.hxx>
108 #include <FDM/fdm_shell.hxx>
110 #include <Environment/environment_mgr.hxx>
112 #include "fg_init.hxx"
114 #include "fg_commands.hxx"
115 #include "fg_props.hxx"
116 #include "options.hxx"
117 #include "globals.hxx"
118 #include "logger.hxx"
119 #include "renderer.hxx"
120 #include "viewmgr.hxx"
125 using namespace boost::algorithm;
128 // Return the current base package version
129 string fgBasePackageVersion() {
130 SGPath base_path( globals->get_fg_root() );
131 base_path.append("version");
133 sg_gzifstream in( base_path.str() );
134 if ( !in.is_open() ) {
135 SGPath old_path( globals->get_fg_root() );
136 old_path.append( "Thanks" );
137 sg_gzifstream old( old_path.str() );
138 if ( !old.is_open() ) {
141 return "[old version]";
152 // Initialize the localization
153 SGPropertyNode *fgInitLocale(const char *language) {
154 SGPropertyNode *c_node = NULL, *d_node = NULL;
155 SGPropertyNode *intl = fgGetNode("/sim/intl");
157 SG_LOG(SG_GENERAL, SG_INFO, "Selecting language: " << language );
159 // localization not defined
164 // Select the proper language from the list
166 vector<SGPropertyNode_ptr> locale = intl->getChildren("locale");
167 for (unsigned int i = 0; i < locale.size(); i++) {
169 vector<SGPropertyNode_ptr> lang = locale[i]->getChildren("lang");
170 for (unsigned int j = 0; j < lang.size(); j++) {
172 if (!strcmp(lang[j]->getStringValue(), language)) {
181 d_node = intl->getChild("locale");
185 // Check for localized font
186 SGPropertyNode *font_n = c_node->getNode("font", true);
187 if ( !strcmp(font_n->getStringValue(), "") )
188 font_n->setStringValue(d_node->getStringValue("font", "typewriter.txf"));
192 // Load the default strings
194 SGPath d_path( globals->get_fg_root() );
196 const char *d_path_str = d_node->getStringValue("strings");
198 SG_LOG(SG_GENERAL, SG_ALERT, "No path in " << d_node->getPath() << "/strings.");
202 d_path.append(d_path_str);
203 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from " << d_path.str());
205 SGPropertyNode *strings = c_node->getNode("strings");
207 readProperties(d_path.str(), strings);
208 } catch (const sg_exception &) {
209 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings");
214 // Load the language specific strings
216 if (c_node != d_node) {
217 SGPath c_path( globals->get_fg_root() );
219 const char *c_path_str = c_node->getStringValue("strings");
221 SG_LOG(SG_GENERAL, SG_ALERT, "No path in " << c_node->getPath() << "/strings");
225 c_path.append(c_path_str);
226 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings from " << c_path.str());
229 readProperties(c_path.str(), strings);
230 } catch (const sg_exception &) {
231 SG_LOG(SG_GENERAL, SG_ALERT,
232 "Unable to read the localized strings from " << c_path.str());
242 // Initialize the localization routines
243 bool fgDetectLanguage() {
244 const char *language = ::getenv("LANG");
246 if (language == NULL) {
247 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
251 SGPropertyNode *locale = fgInitLocale(language);
253 SG_LOG(SG_GENERAL, SG_ALERT,
254 "No internationalization settings specified in preferences.xml" );
259 globals->set_locale( locale );
265 bool fgFindAircraftInDir(const SGPath& dirPath, T* obj, bool (T::*pred)(const SGPath& p))
267 if (!dirPath.exists()) {
268 SG_LOG(SG_GENERAL, SG_WARN, "fgFindAircraftInDir: no such path:" << dirPath.str());
273 simgear::Dir dir(dirPath);
274 simgear::PathList setFiles(dir.children(simgear::Dir::TYPE_FILE, "-set.xml"));
275 simgear::PathList::iterator p;
276 for (p = setFiles.begin(); p != setFiles.end(); ++p) {
277 // check file name ends with -set.xml
279 // if we found a -set.xml at this level, don't recurse any deeper
282 bool done = (obj->*pred)(*p);
286 } // of -set.xml iteration
292 simgear::PathList subdirs(dir.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT));
293 for (p = subdirs.begin(); p != subdirs.end(); ++p) {
294 if (p->file() == "CVS") {
298 if (fgFindAircraftInDir(*p, obj, pred)) {
301 } // of subdirs iteration
307 void fgFindAircraft(T* obj, bool (T::*pred)(const SGPath& p))
309 const string_list& paths(globals->get_aircraft_paths());
310 string_list::const_iterator it = paths.begin();
311 for (; it != paths.end(); ++it) {
312 bool done = fgFindAircraftInDir(SGPath(*it), obj, pred);
316 } // of aircraft paths iteration
318 // if we reach this point, search the default location (always last)
319 SGPath rootAircraft(globals->get_fg_root());
320 rootAircraft.append("Aircraft");
321 fgFindAircraftInDir(rootAircraft, obj, pred);
324 class FindAndCacheAircraft
327 FindAndCacheAircraft(SGPropertyNode* autoSave)
329 _cache = autoSave->getNode("sim/startup/path-cache", true);
334 std::string aircraft = fgGetString( "/sim/aircraft", "");
335 if (aircraft.empty()) {
336 SG_LOG(SG_GENERAL, SG_ALERT, "no aircraft specified");
340 _searchAircraft = aircraft + "-set.xml";
341 std::string aircraftDir = fgGetString("/sim/aircraft-dir", "");
342 if (!aircraftDir.empty()) {
343 // aircraft-dir was set, skip any searching at all, if it's valid
344 simgear::Dir acPath(aircraftDir);
345 SGPath setFile = acPath.file(_searchAircraft);
346 if (setFile.exists()) {
347 SG_LOG(SG_GENERAL, SG_INFO, "found aircraft in dir: " << aircraftDir );
350 readProperties(setFile.str(), globals->get_props());
351 } catch ( const sg_exception &e ) {
352 SG_LOG(SG_INPUT, SG_ALERT, "Error reading aircraft: " << e.getFormattedMessage());
358 SG_LOG(SG_GENERAL, SG_ALERT, "aircraft '" << _searchAircraft <<
359 "' not found in specified dir:" << aircraftDir);
365 // prepare cache for re-scan
366 SGPropertyNode *n = _cache->getNode("fg-root", true);
367 n->setStringValue(globals->get_fg_root().c_str());
368 n->setAttribute(SGPropertyNode::USERARCHIVE, true);
369 n = _cache->getNode("fg-aircraft", true);
370 n->setStringValue(getAircraftPaths().c_str());
371 n->setAttribute(SGPropertyNode::USERARCHIVE, true);
372 _cache->removeChildren("aircraft");
374 fgFindAircraft(this, &FindAndCacheAircraft::checkAircraft);
377 if (_foundPath.str().empty()) {
378 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot find specified aircraft: " << aircraft );
382 SG_LOG(SG_GENERAL, SG_INFO, "Loading aircraft -set file from:" << _foundPath.str());
383 fgSetString( "/sim/aircraft-dir", _foundPath.dir().c_str());
384 if (!_foundPath.exists()) {
385 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to find -set file:" << _foundPath.str());
390 readProperties(_foundPath.str(), globals->get_props());
391 } catch ( const sg_exception &e ) {
392 SG_LOG(SG_INPUT, SG_ALERT, "Error reading aircraft: " << e.getFormattedMessage());
400 SGPath getAircraftPaths() {
401 string_list pathList = globals->get_aircraft_paths();
402 SGPath aircraftPaths;
403 string_list::const_iterator it = pathList.begin();
404 if (it != pathList.end()) {
405 aircraftPaths.set(*it);
408 for (; it != pathList.end(); ++it) {
409 aircraftPaths.add(*it);
411 return aircraftPaths;
416 if (globals->get_fg_root() != _cache->getStringValue("fg-root", "")) {
417 return false; // cache mismatch
420 if (getAircraftPaths().str() != _cache->getStringValue("fg-aircraft", "")) {
421 return false; // cache mismatch
424 vector<SGPropertyNode_ptr> cache = _cache->getChildren("aircraft");
425 for (unsigned int i = 0; i < cache.size(); i++) {
426 const char *name = cache[i]->getStringValue("file", "");
427 if (!boost::equals(_searchAircraft, name, is_iequal())) {
431 SGPath xml(cache[i]->getStringValue("path", ""));
439 } // of aircraft in cache iteration
444 bool checkAircraft(const SGPath& p)
449 if (!_cache->getChild("aircraft", i++, false))
453 SGPropertyNode *n, *entry = _cache->getChild("aircraft", --i, true);
455 std::string fileName(p.file());
456 n = entry->getNode("file", true);
457 n->setStringValue(fileName);
458 n->setAttribute(SGPropertyNode::USERARCHIVE, true);
460 n = entry->getNode("path", true);
461 n->setStringValue(p.dir());
462 n->setAttribute(SGPropertyNode::USERARCHIVE, true);
464 if ( boost::equals(fileName, _searchAircraft.c_str(), is_iequal()) ) {
472 std::string _searchAircraft;
474 SGPropertyNode* _cache;
478 static SGPath platformDefaultDataPath()
480 char *envp = ::getenv( "APPDATA" );
481 SGPath config( envp );
482 config.append( "flightgear.org" );
487 #include <CoreServices/CoreServices.h>
489 static SGPath platformDefaultDataPath()
492 OSErr err = FSFindFolder(kUserDomain, kApplicationSupportFolderType, false, &ref);
497 unsigned char path[1024];
498 if (FSRefMakePath(&ref, path, 1024) != noErr) {
503 appData.set((const char*) path);
504 appData.append("flightgear.org");
508 static SGPath platformDefaultDataPath()
510 SGPath config( homedir );
511 config.append( ".fgfs" );
516 // Read in configuration (file and command line)
517 bool fgInitConfig ( int argc, char **argv ) {
519 flightgear::Options::sharedInstance()->init(argc, argv);
521 // Read global preferences from $FG_ROOT/preferences.xml
522 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
523 fgLoadProps("preferences.xml", globals->get_props());
524 SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
526 // Detect the required language as early as possible
527 if ( !fgDetectLanguage() ) {
531 SGPropertyNode autosave;
532 SGPath dataPath = platformDefaultDataPath();
534 const char *fg_home = getenv("FG_HOME");
538 simgear::Dir exportDir(simgear::Dir(dataPath).file("Export"));
539 if (!exportDir.exists()) {
540 exportDir.create(0777);
543 // Set /sim/fg-home and don't allow malign code to override it until
544 // Nasal security is set up. Use FG_HOME if necessary.
545 SGPropertyNode *home = fgGetNode("/sim", true);
546 home->removeChild("fg-home", 0, false);
547 home = home->getChild("fg-home", 0, true);
548 home->setStringValue(dataPath.c_str());
549 home->setAttribute(SGPropertyNode::WRITE, false);
551 SGPath autosaveFile = simgear::Dir(dataPath).file("autosave.xml");
552 if (autosaveFile.exists()) {
553 SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from " << autosaveFile.str());
555 readProperties(autosaveFile.str(), &autosave, SGPropertyNode::USERARCHIVE);
556 } catch (sg_exception& e) {
557 SG_LOG(SG_INPUT, SG_WARN, "failed to read user settings:" << e.getMessage()
558 << "(from " << e.getOrigin() << ")");
562 // Scan user config files and command line for a specified aircraft.
563 flightgear::Options::sharedInstance()->initAircraft();
565 FindAndCacheAircraft f(&autosave);
566 if (!f.loadAircraft()) {
570 copyProperties(&autosave, globals->get_props());
572 // parse options after loading aircraft to ensure any user
573 // overrides of defaults are honored.
574 flightgear::Options::sharedInstance()->processOptions();
579 // Set current tower position lon/lat given an airport id
580 static bool fgSetTowerPosFromAirportID( const string& id) {
581 const FGAirport *a = fgFindAirportID( id);
583 SGGeod tower = a->getTowerLocation();
584 fgSetDouble("/sim/tower/longitude-deg", tower.getLongitudeDeg());
585 fgSetDouble("/sim/tower/latitude-deg", tower.getLatitudeDeg());
586 fgSetDouble("/sim/tower/altitude-ft", tower.getElevationFt());
594 struct FGTowerLocationListener : SGPropertyChangeListener {
595 void valueChanged(SGPropertyNode* node) {
596 string id(node->getStringValue());
597 if (fgGetBool("/sim/tower/auto-position",true))
599 // enforce using closest airport when auto-positioning is enabled
600 const char* closest_airport = fgGetString("/sim/airport/closest-airport-id", "");
601 if (closest_airport && (id != closest_airport))
603 id = closest_airport;
604 node->setStringValue(id);
607 fgSetTowerPosFromAirportID(id);
611 struct FGClosestTowerLocationListener : SGPropertyChangeListener
613 void valueChanged(SGPropertyNode* )
615 // closest airport has changed
616 if (fgGetBool("/sim/tower/auto-position",true))
618 // update tower position
619 const char* id = fgGetString("/sim/airport/closest-airport-id", "");
621 fgSetString("/sim/tower/airport-id", id);
626 void fgInitTowerLocationListener() {
627 fgGetNode("/sim/tower/airport-id", true)
628 ->addChangeListener( new FGTowerLocationListener(), true );
629 FGClosestTowerLocationListener* ntcl = new FGClosestTowerLocationListener();
630 fgGetNode("/sim/airport/closest-airport-id", true)
631 ->addChangeListener(ntcl , true );
632 fgGetNode("/sim/tower/auto-position", true)
633 ->addChangeListener(ntcl, true );
636 static void fgApplyStartOffset(const SGGeod& aStartPos, double aHeading, double aTargetHeading = HUGE_VAL)
638 SGGeod startPos(aStartPos);
639 if (aTargetHeading == HUGE_VAL) {
640 aTargetHeading = aHeading;
643 if ( fabs( fgGetDouble("/sim/presets/offset-distance-nm") ) > SG_EPSILON ) {
644 double offsetDistance = fgGetDouble("/sim/presets/offset-distance-nm");
645 offsetDistance *= SG_NM_TO_METER;
646 double offsetAzimuth = aHeading;
647 if ( fabs(fgGetDouble("/sim/presets/offset-azimuth-deg")) > SG_EPSILON ) {
648 offsetAzimuth = fgGetDouble("/sim/presets/offset-azimuth-deg");
649 aHeading = aTargetHeading;
654 SGGeodesy::direct(startPos, offsetAzimuth + 180, offsetDistance, offset, az2);
659 fgSetDouble("/sim/presets/longitude-deg", startPos.getLongitudeDeg() );
660 fgSetDouble("/sim/presets/latitude-deg", startPos.getLatitudeDeg() );
661 fgSetDouble("/sim/presets/heading-deg", aHeading );
663 // other code depends on the actual values being set ...
664 fgSetDouble("/position/longitude-deg", startPos.getLongitudeDeg() );
665 fgSetDouble("/position/latitude-deg", startPos.getLatitudeDeg() );
666 fgSetDouble("/orientation/heading-deg", aHeading );
669 // Set current_options lon/lat given an airport id and heading (degrees)
670 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
674 // set initial position from runway and heading
675 SG_LOG( SG_GENERAL, SG_INFO,
676 "Attempting to set starting position from airport code "
677 << id << " heading " << tgt_hdg );
679 const FGAirport* apt = fgFindAirportID(id);
680 if (!apt) return false;
681 FGRunway* r = apt->findBestRunwayForHeading(tgt_hdg);
682 fgSetString("/sim/atc/runway", r->ident().c_str());
684 SGGeod startPos = r->pointOnCenterline(fgGetDouble("/sim/airport/runways/start-offset-m", 5.0));
685 fgApplyStartOffset(startPos, r->headingDeg(), tgt_hdg);
689 // Set current_options lon/lat given an airport id and parkig position name
690 static bool fgSetPosFromAirportIDandParkpos( const string& id, const string& parkpos ) {
694 // can't see an easy way around this const_cast at the moment
695 FGAirport* apt = const_cast<FGAirport*>(fgFindAirportID(id));
697 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to find airport " << id );
700 FGAirportDynamics* dcs = apt->getDynamics();
702 SG_LOG( SG_GENERAL, SG_ALERT,
703 "Airport " << id << "does not appear to have parking information available");
707 int park_index = dcs->getNrOfParkings() - 1;
709 double radius = fgGetDouble("/sim/dimensions/radius-m");
710 if ((parkpos == string("AVAILABLE")) && (radius > 0)) {
711 double lat, lon, heading;
716 acData = fgGetString("/sim/fg-home");
717 acData.append("aircraft-data");
718 string acfile = fgGetString("/sim/aircraft") + string(".xml");
719 acData.append(acfile);
721 readProperties(acData.str(), &root);
722 SGPropertyNode * node = root.getNode("sim");
723 fltType = node->getStringValue("aircraft-class", "NONE" );
724 acOperator = node->getStringValue("aircraft-operator", "NONE" );
725 } catch (const sg_exception &) {
726 SG_LOG(SG_GENERAL, SG_INFO,
727 "Could not load aircraft aircrat type and operator information from: " << acData.str() << ". Using defaults");
729 // cout << path.str() << endl;
731 if (fltType.empty() || fltType == "NONE") {
732 SG_LOG(SG_GENERAL, SG_INFO,
733 "Aircraft type information not found in: " << acData.str() << ". Using default value");
734 fltType = fgGetString("/sim/aircraft-class" );
736 if (acOperator.empty() || fltType == "NONE") {
737 SG_LOG(SG_GENERAL, SG_INFO,
738 "Aircraft operator information not found in: " << acData.str() << ". Using default value");
739 acOperator = fgGetString("/sim/aircraft-operator" );
742 cerr << "Running aircraft " << fltType << " of livery " << acOperator << endl;
743 string acType; // Currently not used by findAvailable parking, so safe to leave empty.
744 succes = dcs->getAvailableParking(&lat, &lon, &heading, &park_index, radius, fltType, acType, acOperator);
746 fgGetString("/sim/presets/parkpos");
747 fgSetString("/sim/presets/parkpos", dcs->getParking(park_index)->getName());
749 SG_LOG( SG_GENERAL, SG_ALERT,
750 "Failed to find a suitable parking at airport " << id );
754 //cerr << "We shouldn't get here when AVAILABLE" << endl;
755 while (park_index >= 0 && dcs->getParkingName(park_index) != parkpos) park_index--;
756 if (park_index < 0) {
757 SG_LOG( SG_GENERAL, SG_ALERT,
758 "Failed to find parking position " << parkpos <<
759 " at airport " << id );
763 FGParking* parking = dcs->getParking(park_index);
764 parking->setAvailable(false);
766 SGGeod::fromDeg(parking->getLongitude(), parking->getLatitude()),
767 parking->getHeading());
772 // Set current_options lon/lat given an airport id and runway number
773 static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy, bool rwy_req ) {
777 // set initial position from airport and runway number
778 SG_LOG( SG_GENERAL, SG_INFO,
779 "Attempting to set starting position for "
780 << id << ":" << rwy );
782 const FGAirport* apt = fgFindAirportID(id);
784 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to find airport:" << id);
788 if (!apt->hasRunwayWithIdent(rwy)) {
789 SG_LOG( SG_GENERAL, rwy_req ? SG_ALERT : SG_INFO,
790 "Failed to find runway " << rwy <<
791 " at airport " << id << ". Using default runway." );
795 FGRunway* r(apt->getRunwayByIdent(rwy));
796 fgSetString("/sim/atc/runway", r->ident().c_str());
797 SGGeod startPos = r->pointOnCenterline( fgGetDouble("/sim/airport/runways/start-offset-m", 5.0));
798 fgApplyStartOffset(startPos, r->headingDeg());
803 static void fgSetDistOrAltFromGlideSlope() {
804 // cout << "fgSetDistOrAltFromGlideSlope()" << endl;
805 string apt_id = fgGetString("/sim/presets/airport-id");
806 double gs = fgGetDouble("/sim/presets/glideslope-deg")
807 * SG_DEGREES_TO_RADIANS ;
808 double od = fgGetDouble("/sim/presets/offset-distance-nm");
809 double alt = fgGetDouble("/sim/presets/altitude-ft");
811 double apt_elev = 0.0;
812 if ( ! apt_id.empty() ) {
813 apt_elev = fgGetAirportElev( apt_id );
814 if ( apt_elev < -9990.0 ) {
821 if( fabs(gs) > 0.01 && fabs(od) > 0.1 && alt < -9990 ) {
822 // set altitude from glideslope and offset-distance
823 od *= SG_NM_TO_METER * SG_METER_TO_FEET;
824 alt = fabs(od*tan(gs)) + apt_elev;
825 fgSetDouble("/sim/presets/altitude-ft", alt);
826 fgSetBool("/sim/presets/onground", false);
827 SG_LOG( SG_GENERAL, SG_INFO, "Calculated altitude as: "
829 } else if( fabs(gs) > 0.01 && alt > 0 && fabs(od) < 0.1) {
830 // set offset-distance from glideslope and altitude
831 od = (alt - apt_elev) / tan(gs);
832 od *= -1*SG_FEET_TO_METER * SG_METER_TO_NM;
833 fgSetDouble("/sim/presets/offset-distance-nm", od);
834 fgSetBool("/sim/presets/onground", false);
835 SG_LOG( SG_GENERAL, SG_INFO, "Calculated offset distance as: "
837 } else if( fabs(gs) > 0.01 ) {
838 SG_LOG( SG_GENERAL, SG_ALERT,
839 "Glideslope given but not altitude or offset-distance." );
840 SG_LOG( SG_GENERAL, SG_ALERT, "Resetting glideslope to zero" );
841 fgSetDouble("/sim/presets/glideslope-deg", 0);
842 fgSetBool("/sim/presets/onground", true);
847 // Set current_options lon/lat given an airport id and heading (degrees)
848 static bool fgSetPosFromNAV( const string& id, const double& freq, FGPositioned::Type type ) {
850 const nav_list_type navlist
851 = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq, type );
853 if (navlist.size() == 0 ) {
854 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
855 << id << ":" << freq );
859 if( navlist.size() > 1 ) {
861 buf << "Ambigous NAV-ID: '" << id << "'. Specify id and frequency. Available stations:" << endl;
862 for( nav_list_type::const_iterator it = navlist.begin(); it != navlist.end(); ++it ) {
863 // NDB stored in kHz, VOR stored in MHz * 100 :-P
864 double factor = (*it)->type() == FGPositioned::NDB ? 1.0 : 1/100.0;
865 string unit = (*it)->type() == FGPositioned::NDB ? "kHz" : "MHz";
866 buf << (*it)->ident() << " "
867 << setprecision(5) << (double)((*it)->get_freq() * factor) << " "
868 << (*it)->get_lat() << "/" << (*it)->get_lon()
872 SG_LOG( SG_GENERAL, SG_ALERT, buf.str() );
876 FGNavRecord *nav = navlist[0];
877 fgApplyStartOffset(nav->geod(), fgGetDouble("/sim/presets/heading-deg"));
881 // Set current_options lon/lat given an aircraft carrier id
882 static bool fgSetPosFromCarrier( const string& carrier, const string& posid ) {
884 // set initial position from runway and heading
888 if (FGAIManager::getStartPosition(carrier, posid, geodPos, heading, uvw)) {
889 double lon = geodPos.getLongitudeDeg();
890 double lat = geodPos.getLatitudeDeg();
891 double alt = geodPos.getElevationFt();
893 SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
894 << carrier << " at lat = " << lat << ", lon = " << lon
895 << ", alt = " << alt << ", heading = " << heading);
897 fgSetDouble("/sim/presets/longitude-deg", lon);
898 fgSetDouble("/sim/presets/latitude-deg", lat);
899 fgSetDouble("/sim/presets/altitude-ft", alt);
900 fgSetDouble("/sim/presets/heading-deg", heading);
901 fgSetDouble("/position/longitude-deg", lon);
902 fgSetDouble("/position/latitude-deg", lat);
903 fgSetDouble("/position/altitude-ft", alt);
904 fgSetDouble("/orientation/heading-deg", heading);
906 fgSetString("/sim/presets/speed-set", "UVW");
907 fgSetDouble("/velocities/uBody-fps", uvw(0));
908 fgSetDouble("/velocities/vBody-fps", uvw(1));
909 fgSetDouble("/velocities/wBody-fps", uvw(2));
910 fgSetDouble("/sim/presets/uBody-fps", uvw(0));
911 fgSetDouble("/sim/presets/vBody-fps", uvw(1));
912 fgSetDouble("/sim/presets/wBody-fps", uvw(2));
914 fgSetBool("/sim/presets/onground", true);
918 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate aircraft carrier = "
924 // Set current_options lon/lat given an airport id and heading (degrees)
925 static bool fgSetPosFromFix( const string& id )
927 FGPositioned::TypeFilter fixFilter(FGPositioned::FIX);
928 FGPositioned* fix = FGPositioned::findNextWithPartialId(NULL, id, &fixFilter);
930 SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate fix = " << id );
934 fgApplyStartOffset(fix->geod(), fgGetDouble("/sim/presets/heading-deg"));
939 * Initialize vor/ndb/ils/fix list management and query systems (as
940 * well as simple airport db list)
945 SG_LOG(SG_GENERAL, SG_INFO, "Loading Airport Database ...");
947 SGPath aptdb( globals->get_fg_root() );
948 aptdb.append( "Airports/apt.dat" );
950 SGPath p_metar( globals->get_fg_root() );
951 p_metar.append( "Airports/metar.dat" );
953 fgAirportDBLoad( aptdb.str(), p_metar.str() );
954 FGAirport::installPropertyListener();
955 FGPositioned::installCommands();
957 FGNavList *navlist = new FGNavList;
958 FGNavList *loclist = new FGNavList;
959 FGNavList *gslist = new FGNavList;
960 FGNavList *dmelist = new FGNavList;
961 FGNavList *tacanlist = new FGNavList;
962 FGNavList *carrierlist = new FGNavList;
963 FGTACANList *channellist = new FGTACANList;
965 globals->set_navlist( navlist );
966 globals->set_loclist( loclist );
967 globals->set_gslist( gslist );
968 globals->set_dmelist( dmelist );
969 globals->set_tacanlist( tacanlist );
970 globals->set_carrierlist( carrierlist );
971 globals->set_channellist( channellist );
973 if ( !fgNavDBInit(navlist, loclist, gslist, dmelist, tacanlist, carrierlist, channellist) ) {
974 SG_LOG( SG_GENERAL, SG_ALERT,
975 "Problems loading one or more navigational database" );
978 SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
979 SGPath p_fix( globals->get_fg_root() );
980 p_fix.append( "Navaids/fix.dat" );
982 fixlist.init( p_fix ); // adds fixes to the DB in positioned.cxx
984 SG_LOG(SG_GENERAL, SG_INFO, " Airways");
985 flightgear::Airway::load();
991 // Set the initial position based on presets (or defaults)
992 bool fgInitPosition() {
993 // cout << "fgInitPosition()" << endl;
994 double gs = fgGetDouble("/sim/presets/glideslope-deg")
995 * SG_DEGREES_TO_RADIANS ;
996 double od = fgGetDouble("/sim/presets/offset-distance-nm");
997 double alt = fgGetDouble("/sim/presets/altitude-ft");
999 bool set_pos = false;
1001 // If glideslope is specified, then calculate offset-distance or
1002 // altitude relative to glide slope if either of those was not
1004 if ( fabs( gs ) > 0.01 ) {
1005 fgSetDistOrAltFromGlideSlope();
1009 // If we have an explicit, in-range lon/lat, don't change it, just use it.
1010 // If not, check for an airport-id and use that.
1011 // If not, default to the middle of the KSFO field.
1012 // The default values for lon/lat are deliberately out of range
1013 // so that the airport-id can take effect; valid lon/lat will
1014 // override airport-id, however.
1015 double lon_deg = fgGetDouble("/sim/presets/longitude-deg");
1016 double lat_deg = fgGetDouble("/sim/presets/latitude-deg");
1017 if ( lon_deg >= -180.0 && lon_deg <= 180.0
1018 && lat_deg >= -90.0 && lat_deg <= 90.0 )
1023 string apt = fgGetString("/sim/presets/airport-id");
1024 string rwy_no = fgGetString("/sim/presets/runway");
1025 bool rwy_req = fgGetBool("/sim/presets/runway-requested");
1026 string vor = fgGetString("/sim/presets/vor-id");
1027 double vor_freq = fgGetDouble("/sim/presets/vor-freq");
1028 string ndb = fgGetString("/sim/presets/ndb-id");
1029 double ndb_freq = fgGetDouble("/sim/presets/ndb-freq");
1030 string carrier = fgGetString("/sim/presets/carrier");
1031 string parkpos = fgGetString("/sim/presets/parkpos");
1032 string fix = fgGetString("/sim/presets/fix");
1033 SGPropertyNode *hdg_preset = fgGetNode("/sim/presets/heading-deg", true);
1034 double hdg = hdg_preset->getDoubleValue();
1036 // save some start parameters, so that we can later say what the
1037 // user really requested. TODO generalize that and move it to options.cxx
1038 static bool start_options_saved = false;
1039 if (!start_options_saved) {
1040 start_options_saved = true;
1041 SGPropertyNode *opt = fgGetNode("/sim/startup/options", true);
1043 opt->setDoubleValue("latitude-deg", lat_deg);
1044 opt->setDoubleValue("longitude-deg", lon_deg);
1045 opt->setDoubleValue("heading-deg", hdg);
1046 opt->setStringValue("airport", apt.c_str());
1047 opt->setStringValue("runway", rwy_no.c_str());
1051 hdg = fgGetDouble("/environment/config/boundary/entry/wind-from-heading-deg", 270);
1053 if ( !set_pos && !apt.empty() && !parkpos.empty() ) {
1054 // An airport + parking position is requested
1055 if ( fgSetPosFromAirportIDandParkpos( apt, parkpos ) ) {
1056 // set tower position
1057 fgSetString("/sim/airport/closest-airport-id", apt.c_str());
1058 fgSetString("/sim/tower/airport-id", apt.c_str());
1063 if ( !set_pos && !apt.empty() && !rwy_no.empty() ) {
1064 // An airport + runway is requested
1065 if ( fgSetPosFromAirportIDandRwy( apt, rwy_no, rwy_req ) ) {
1066 // set tower position (a little off the heading for single
1068 fgSetString("/sim/airport/closest-airport-id", apt.c_str());
1069 fgSetString("/sim/tower/airport-id", apt.c_str());
1074 if ( !set_pos && !apt.empty() ) {
1075 // An airport is requested (find runway closest to hdg)
1076 if ( fgSetPosFromAirportIDandHdg( apt, hdg ) ) {
1077 // set tower position (a little off the heading for single
1079 fgSetString("/sim/airport/closest-airport-id", apt.c_str());
1080 fgSetString("/sim/tower/airport-id", apt.c_str());
1085 if (hdg_preset->getDoubleValue() > 9990.0)
1086 hdg_preset->setDoubleValue(hdg);
1088 if ( !set_pos && !vor.empty() ) {
1089 // a VOR is requested
1090 if ( fgSetPosFromNAV( vor, vor_freq, FGPositioned::VOR ) ) {
1095 if ( !set_pos && !ndb.empty() ) {
1096 // an NDB is requested
1097 if ( fgSetPosFromNAV( ndb, ndb_freq, FGPositioned::NDB ) ) {
1102 if ( !set_pos && !carrier.empty() ) {
1103 // an aircraft carrier is requested
1104 if ( fgSetPosFromCarrier( carrier, parkpos ) ) {
1109 if ( !set_pos && !fix.empty() ) {
1110 // a Fix is requested
1111 if ( fgSetPosFromFix( fix ) ) {
1117 // No lon/lat specified, no airport specified, default to
1118 // middle of KSFO field.
1119 fgSetDouble("/sim/presets/longitude-deg", -122.374843);
1120 fgSetDouble("/sim/presets/latitude-deg", 37.619002);
1123 fgSetDouble( "/position/longitude-deg",
1124 fgGetDouble("/sim/presets/longitude-deg") );
1125 fgSetDouble( "/position/latitude-deg",
1126 fgGetDouble("/sim/presets/latitude-deg") );
1127 fgSetDouble( "/orientation/heading-deg", hdg_preset->getDoubleValue());
1129 // determine if this should be an on-ground or in-air start
1130 if ((fabs(gs) > 0.01 || fabs(od) > 0.1 || alt > 0.1) && carrier.empty()) {
1131 fgSetBool("/sim/presets/onground", false);
1133 fgSetBool("/sim/presets/onground", true);
1140 // General house keeping initializations
1141 bool fgInitGeneral() {
1144 SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
1145 SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
1147 root = globals->get_fg_root();
1148 if ( ! root.length() ) {
1149 // No root path set? Then bail ...
1150 SG_LOG( SG_GENERAL, SG_ALERT,
1151 "Cannot continue without a path to the base package "
1152 << "being defined." );
1155 SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
1157 globals->set_browser(fgGetString("/sim/startup/browser-app", "firefox %u"));
1159 char buf[512], *cwd = getcwd(buf, 511);
1161 SGPropertyNode *curr = fgGetNode("/sim", true);
1162 curr->removeChild("fg-current", 0, false);
1163 curr = curr->getChild("fg-current", 0, true);
1164 curr->setStringValue(cwd ? cwd : "");
1165 curr->setAttribute(SGPropertyNode::WRITE, false);
1167 fgSetBool("/sim/startup/stdout-to-terminal", isatty(1) != 0 );
1168 fgSetBool("/sim/startup/stderr-to-terminal", isatty(2) != 0 );
1172 // This is the top level init routine which calls all the other
1173 // initialization routines. If you are adding a subsystem to flight
1174 // gear, its initialization call should located in this routine.
1175 // Returns non-zero if a problem encountered.
1176 bool fgInitSubsystems() {
1177 // static const SGPropertyNode *longitude
1178 // = fgGetNode("/sim/presets/longitude-deg");
1179 // static const SGPropertyNode *latitude
1180 // = fgGetNode("/sim/presets/latitude-deg");
1181 // static const SGPropertyNode *altitude
1182 // = fgGetNode("/sim/presets/altitude-ft");
1184 SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
1185 SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
1187 ////////////////////////////////////////////////////////////////////
1188 // Initialize the event manager subsystem.
1189 ////////////////////////////////////////////////////////////////////
1191 globals->get_event_mgr()->init();
1192 globals->get_event_mgr()->setRealtimeProperty(fgGetNode("/sim/time/delta-realtime-sec", true));
1194 ////////////////////////////////////////////////////////////////////
1195 // Initialize the property interpolator subsystem. Put into the INIT
1196 // group because the "nasal" subsystem may need it at GENERAL take-down.
1197 ////////////////////////////////////////////////////////////////////
1198 globals->add_subsystem("interpolator", new SGInterpolator, SGSubsystemMgr::INIT);
1201 ////////////////////////////////////////////////////////////////////
1202 // Add the FlightGear property utilities.
1203 ////////////////////////////////////////////////////////////////////
1204 globals->add_subsystem("properties", new FGProperties);
1206 ////////////////////////////////////////////////////////////////////
1207 // Initialize the material property subsystem.
1208 ////////////////////////////////////////////////////////////////////
1210 SGPath mpath( globals->get_fg_root() );
1211 mpath.append( fgGetString("/sim/rendering/materials-file") );
1212 if ( ! globals->get_matlib()->load(globals->get_fg_root(), mpath.str(),
1213 globals->get_props()) ) {
1214 SG_LOG( SG_GENERAL, SG_ALERT,
1215 "Error loading materials file " << mpath.str() );
1220 ////////////////////////////////////////////////////////////////////
1221 // Initialize the scenery management subsystem.
1222 ////////////////////////////////////////////////////////////////////
1224 globals->get_scenery()->get_scene_graph()
1225 ->addChild(simgear::Particles::getCommonRoot());
1226 simgear::GlobalParticleCallback::setSwitch(fgGetNode("/sim/rendering/particles", true));
1228 ////////////////////////////////////////////////////////////////////
1229 // Initialize the flight model subsystem.
1230 ////////////////////////////////////////////////////////////////////
1232 globals->add_subsystem("flight", new FDMShell, SGSubsystemMgr::FDM);
1234 ////////////////////////////////////////////////////////////////////
1235 // Initialize the weather subsystem.
1236 ////////////////////////////////////////////////////////////////////
1238 // Initialize the weather modeling subsystem
1239 globals->add_subsystem("environment", new FGEnvironmentMgr);
1241 ////////////////////////////////////////////////////////////////////
1242 // Initialize the aircraft systems and instrumentation (before the
1244 ////////////////////////////////////////////////////////////////////
1246 globals->add_subsystem("instrumentation", new FGInstrumentMgr, SGSubsystemMgr::FDM);
1247 globals->add_subsystem("systems", new FGSystemMgr, SGSubsystemMgr::FDM);
1249 ////////////////////////////////////////////////////////////////////
1250 // Initialize the XML Autopilot subsystem.
1251 ////////////////////////////////////////////////////////////////////
1253 globals->add_subsystem( "xml-autopilot", FGXMLAutopilotGroup::createInstance(), SGSubsystemMgr::FDM );
1254 globals->add_subsystem( "route-manager", new FGRouteMgr );
1256 ////////////////////////////////////////////////////////////////////
1257 // Initialize the Input-Output subsystem
1258 ////////////////////////////////////////////////////////////////////
1259 globals->add_subsystem( "io", new FGIO );
1261 ////////////////////////////////////////////////////////////////////
1262 // Create and register the logger.
1263 ////////////////////////////////////////////////////////////////////
1265 globals->add_subsystem("logger", new FGLogger);
1267 ////////////////////////////////////////////////////////////////////
1268 // Create and register the XML GUI.
1269 ////////////////////////////////////////////////////////////////////
1271 globals->add_subsystem("gui", new NewGUI, SGSubsystemMgr::INIT);
1273 //////////////////////////////////////////////////////////////////////
1274 // Initialize the 2D cloud subsystem.
1275 ////////////////////////////////////////////////////////////////////
1276 fgGetBool("/sim/rendering/bump-mapping", false);
1280 ////////////////////////////////////////////////////////////////////
1281 // Initialise the ATC Manager
1282 // Note that this is old stuff, but might be necessesary for the
1283 // current ATIS implementation. Therefore, leave it in here
1284 // until the ATIS system is ported over to make use of the ATIS
1285 // sub system infrastructure.
1286 ////////////////////////////////////////////////////////////////////
1288 SG_LOG(SG_GENERAL, SG_INFO, " ATC Manager");
1289 globals->set_ATC_mgr(new FGATCMgr);
1290 globals->get_ATC_mgr()->init();
1292 ////////////////////////////////////////////////////////////////////
1293 // Initialize the ATC subsystem
1294 ////////////////////////////////////////////////////////////////////
1295 globals->add_subsystem("ATC", new FGATCManager, SGSubsystemMgr::POST_FDM);
1296 ////////////////////////////////////////////////////////////////////
1297 // Initialise the ATIS Subsystem
1298 ////////////////////////////////////////////////////////////////////
1299 //globals->add_subsystem("atis", new FGAtisManager, SGSubsystemMgr::POST_FDM);
1302 ////////////////////////////////////////////////////////////////////
1303 // Initialize multiplayer subsystem
1304 ////////////////////////////////////////////////////////////////////
1306 globals->add_subsystem("mp", new FGMultiplayMgr, SGSubsystemMgr::POST_FDM);
1308 ////////////////////////////////////////////////////////////////////
1309 // Initialise the AI Model Manager
1310 ////////////////////////////////////////////////////////////////////
1311 SG_LOG(SG_GENERAL, SG_INFO, " AI Model Manager");
1312 globals->add_subsystem("ai_model", new FGAIManager, SGSubsystemMgr::POST_FDM);
1313 globals->add_subsystem("submodel_mgr", new FGSubmodelMgr, SGSubsystemMgr::POST_FDM);
1316 // It's probably a good idea to initialize the top level traffic manager
1317 // After the AI and ATC systems have been initialized properly.
1318 // AI Traffic manager
1319 globals->add_subsystem("Traffic Manager", new FGTrafficManager, SGSubsystemMgr::POST_FDM);
1321 ////////////////////////////////////////////////////////////////////
1322 // Add a new 2D panel.
1323 ////////////////////////////////////////////////////////////////////
1325 string panel_path(fgGetString("/sim/panel/path"));
1326 if (!panel_path.empty()) {
1327 FGPanel* p = fgReadPanel(panel_path);
1329 globals->set_current_panel(p);
1332 SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
1334 SG_LOG( SG_INPUT, SG_ALERT,
1335 "Error reading new panel from " << panel_path );
1339 ////////////////////////////////////////////////////////////////////
1340 // Initialize the controls subsystem.
1341 ////////////////////////////////////////////////////////////////////
1343 globals->get_controls()->init();
1344 globals->get_controls()->bind();
1347 ////////////////////////////////////////////////////////////////////
1348 // Initialize the input subsystem.
1349 ////////////////////////////////////////////////////////////////////
1351 globals->add_subsystem("input", new FGInput);
1354 ////////////////////////////////////////////////////////////////////
1355 // Initialize the replay subsystem
1356 ////////////////////////////////////////////////////////////////////
1357 globals->add_subsystem("replay", new FGReplay);
1359 #ifdef ENABLE_AUDIO_SUPPORT
1360 ////////////////////////////////////////////////////////////////////
1361 // Initialize the sound-effects subsystem.
1362 ////////////////////////////////////////////////////////////////////
1363 globals->add_subsystem("voice", new FGVoiceMgr, SGSubsystemMgr::DISPLAY);
1366 ////////////////////////////////////////////////////////////////////
1367 // Initialize the lighting subsystem.
1368 ////////////////////////////////////////////////////////////////////
1370 globals->add_subsystem("lighting", new FGLight, SGSubsystemMgr::DISPLAY);
1372 // ordering here is important : Nasal (via events), then models, then views
1373 globals->add_subsystem("events", globals->get_event_mgr(), SGSubsystemMgr::DISPLAY);
1375 FGAircraftModel* acm = new FGAircraftModel;
1376 globals->set_aircraft_model(acm);
1377 globals->add_subsystem("aircraft-model", acm, SGSubsystemMgr::DISPLAY);
1379 FGModelMgr* mm = new FGModelMgr;
1380 globals->set_model_mgr(mm);
1381 globals->add_subsystem("model-manager", mm, SGSubsystemMgr::DISPLAY);
1383 FGViewMgr *viewmgr = new FGViewMgr;
1384 globals->set_viewmgr( viewmgr );
1385 globals->add_subsystem("view-manager", viewmgr, SGSubsystemMgr::DISPLAY);
1387 globals->add_subsystem("tile-manager", globals->get_tile_mgr(),
1388 SGSubsystemMgr::DISPLAY);
1390 ////////////////////////////////////////////////////////////////////
1391 // Bind and initialize subsystems.
1392 ////////////////////////////////////////////////////////////////////
1394 globals->get_subsystem_mgr()->bind();
1395 globals->get_subsystem_mgr()->init();
1397 ////////////////////////////////////////////////////////////////////////
1398 // Initialize the Nasal interpreter.
1399 // Do this last, so that the loaded scripts see initialized state
1400 ////////////////////////////////////////////////////////////////////////
1401 FGNasalSys* nasal = new FGNasalSys();
1402 globals->add_subsystem("nasal", nasal, SGSubsystemMgr::INIT);
1405 // initialize methods that depend on other subsystems.
1406 globals->get_subsystem_mgr()->postinit();
1408 ////////////////////////////////////////////////////////////////////////
1409 // End of subsystem initialization.
1410 ////////////////////////////////////////////////////////////////////
1412 fgSetBool("/sim/initialized", true);
1414 SG_LOG( SG_GENERAL, SG_INFO, endl);
1416 // Save the initial state for future
1418 globals->saveInitialState();
1423 // Reset: this is what the 'reset' command (and hence, GUI) is attached to
1424 void fgReInitSubsystems()
1426 static const SGPropertyNode *master_freeze
1427 = fgGetNode("/sim/freeze/master");
1429 SG_LOG( SG_GENERAL, SG_INFO, "fgReInitSubsystems()");
1431 // setup state to begin re-init
1432 bool freeze = master_freeze->getBoolValue();
1434 fgSetBool("/sim/freeze/master", true);
1437 fgSetBool("/sim/signals/reinit", true);
1438 fgSetBool("/sim/crashed", false);
1440 // do actual re-init steps
1441 globals->get_subsystem("flight")->unbind();
1443 // reset control state, before restoring initial state; -set or config files
1444 // may specify values for flaps, trim tabs, magnetos, etc
1445 globals->get_controls()->reset_all();
1447 globals->restoreInitialState();
1449 // update our position based on current presets
1452 // Force reupdating the positions of the ai 3d models. They are used for
1453 // initializing ground level for the FDM.
1454 globals->get_subsystem("ai_model")->reinit();
1456 // Initialize the FDM
1457 globals->get_subsystem("flight")->reinit();
1459 // reset replay buffers
1460 globals->get_subsystem("replay")->reinit();
1462 // reload offsets from config defaults
1463 globals->get_viewmgr()->reinit();
1465 globals->get_subsystem("time")->reinit();
1467 // need to bind FDMshell again, since we manually unbound it above...
1468 globals->get_subsystem("flight")->bind();
1470 // setup state to end re-init
1471 fgSetBool("/sim/signals/reinit", false);
1473 fgSetBool("/sim/freeze/master", false);
1475 fgSetBool("/sim/sceneryloaded",false);
1479 ///////////////////////////////////////////////////////////////////////////////
1480 // helper object to implement the --show-aircraft command.
1481 // resides here so we can share the fgFindAircraftInDir template above,
1482 // and hence ensure this command lists exectly the same aircraft as the normal
1489 _minStatus = getNumMaturity(fgGetString("/sim/aircraft-min-status", "all"));
1493 void show(const SGPath& path)
1495 fgFindAircraftInDir(path, this, &ShowAircraft::processAircraft);
1497 std::sort(_aircraft.begin(), _aircraft.end(), ciLessLibC());
1498 SG_LOG( SG_GENERAL, SG_ALERT, "" ); // To popup the console on Windows
1499 cout << "Available aircraft:" << endl;
1500 for ( unsigned int i = 0; i < _aircraft.size(); i++ ) {
1501 cout << _aircraft[i] << endl;
1506 bool processAircraft(const SGPath& path)
1508 SGPropertyNode root;
1510 readProperties(path.str(), &root);
1511 } catch (sg_exception& ) {
1516 string descStr(" ");
1517 descStr += path.file();
1518 // trim common suffix from file names
1519 int nPos = descStr.rfind("-set.xml");
1520 if (nPos == (int)(descStr.size() - 8)) {
1521 descStr.resize(nPos);
1524 SGPropertyNode *node = root.getNode("sim");
1526 SGPropertyNode* desc = node->getNode("description");
1527 // if a status tag is found, read it in
1528 if (node->hasValue("status")) {
1529 maturity = getNumMaturity(node->getStringValue("status"));
1533 if (descStr.size() <= 27+3) {
1534 descStr.append(29+3-descStr.size(), ' ');
1537 descStr.append( 32, ' ');
1539 descStr += desc->getStringValue();
1541 } // of have 'sim' node
1543 if (maturity < _minStatus) {
1547 _aircraft.push_back(descStr);
1552 int getNumMaturity(const char * str)
1554 // changes should also be reflected in $FG_ROOT/data/options.xml &
1555 // $FG_ROOT/data/Translations/string-default.xml
1556 const char* levels[] = {"alpha","beta","early-production","production"};
1558 if (!strcmp(str, "all")) {
1562 for (size_t i=0; i<(sizeof(levels)/sizeof(levels[0]));i++)
1563 if (strcmp(str,levels[i])==0)
1569 // recommended in Meyers, Effective STL when internationalization and embedded
1570 // NULLs aren't an issue. Much faster than the STL or Boost lex versions.
1571 struct ciLessLibC : public std::binary_function<string, string, bool>
1573 bool operator()(const std::string &lhs, const std::string &rhs) const
1575 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0 ? 1 : 0;
1580 string_list _aircraft;
1583 void fgShowAircraft(const SGPath &path)
1589 cout << "Hit a key to continue..." << endl;