]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
A grab bag of tweaks and patches from Norman Vine.
[flightgear.git] / src / Main / fg_init.cxx
1 // fg_init.cxx -- Flight Gear top level initialization routines
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
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.
11 //
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.
16 //
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 // For BC 5.01 this must be included before OpenGL includes.
30 #ifdef SG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #ifdef HAVE_WINDOWS_H
35 #  include <windows.h>
36 #endif
37
38 #include <GL/glut.h>
39
40 #include <stdio.h>
41 #include <stdlib.h>
42
43
44 #if defined( unix ) || defined( __CYGWIN__ )
45 #  include <unistd.h>           // for gethostname()
46 #endif
47
48 // work around a stdc++ lib bug in some versions of linux, but doesn't
49 // seem to hurt to have this here for all versions of Linux.
50 #ifdef linux
51 #  define _G_NO_EXTERN_TEMPLATES
52 #endif
53
54 #include <simgear/compiler.h>
55 #include <simgear/misc/exception.hxx>
56
57 #include STL_STRING
58
59 #include <simgear/constants.h>
60 #include <simgear/debug/logstream.hxx>
61 #include <simgear/math/point3d.hxx>
62 #include <simgear/math/polar3d.hxx>
63 #include <simgear/math/sg_geodesy.hxx>
64 #include <simgear/misc/sg_path.hxx>
65 #include <simgear/timing/sg_time.hxx>
66
67 #include <Aircraft/aircraft.hxx>
68 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
69 #include <Airports/runways.hxx>
70 #include <Airports/simple.hxx>
71 #include <ATC/ATCdisplay.hxx>
72 #include <Autopilot/auto_gui.hxx>
73 #include <Autopilot/newauto.hxx>
74 #include <Cockpit/cockpit.hxx>
75 #include <Cockpit/radiostack.hxx>
76 #include <Cockpit/panel.hxx>
77 #include <Cockpit/panel_io.hxx>
78 #include <FDM/ADA.hxx>
79 #include <FDM/Balloon.h>
80 #include <FDM/External.hxx>
81 #include <FDM/JSBSim.hxx>
82 #include <FDM/LaRCsim.hxx>
83 #include <FDM/MagicCarpet.hxx>
84 #include <FDM/NullFDM.hxx>
85 #include <FDM/YASim/YASim.hxx>
86 #include <Include/general.hxx>
87 #include <Input/input.hxx>
88 // #include <Joystick/joystick.hxx>
89 #include <Objects/matlib.hxx>
90 #include <Navaids/fixlist.hxx>
91 #include <Navaids/ilslist.hxx>
92 #include <Navaids/mkrbeacons.hxx>
93 #include <Navaids/navlist.hxx>
94 #include <Scenery/scenery.hxx>
95 #include <Scenery/tilemgr.hxx>
96 #include <Sound/fg_fx.hxx>
97 #include <Sound/soundmgr.hxx>
98 #include <Time/event.hxx>
99 #include <Time/light.hxx>
100 #include <Time/sunpos.hxx>
101 #include <Time/moonpos.hxx>
102 #include <Time/tmp.hxx>
103
104 #ifndef FG_OLD_WEATHER
105 #  include <WeatherCM/FGLocalWeatherDatabase.h>
106 #else
107 #  include <Weather/weather.hxx>
108 #endif
109
110 #include "fg_init.hxx"
111 #include "fg_io.hxx"
112 #include "fg_commands.hxx"
113 #include "fg_props.hxx"
114 #include "options.hxx"
115 #include "globals.hxx"
116 #include "viewmgr.hxx"
117
118 #if defined(FX) && defined(XMESA)
119 #include <GL/xmesa.h>
120 #endif
121
122 SG_USING_STD(string);
123
124 extern const char *default_root;
125
126
127 // Read in configuration (file and command line) and just set fg_root
128 bool fgInitFGRoot ( int argc, char **argv ) {
129     string root;
130     char* envp;
131
132     // First parse command line options looking for fg-root, this will
133     // override anything specified in a config file
134     root = fgScanForRoot(argc, argv);
135
136 #if defined( unix ) || defined( __CYGWIN__ )
137     // Next check home directory for .fgfsrc.hostname file
138     if ( root == "" ) {
139         envp = ::getenv( "HOME" );
140         if ( envp != NULL ) {
141             SGPath config( envp );
142             config.append( ".fgfsrc" );
143             char name[256];
144             gethostname( name, 256 );
145             config.concat( "." );
146             config.concat( name );
147             root = fgScanForRoot(config.str());
148         }
149     }
150 #endif
151
152     // Next check home directory for .fgfsrc file
153     if ( root == "" ) {
154         envp = ::getenv( "HOME" );
155         if ( envp != NULL ) {
156             SGPath config( envp );
157             config.append( ".fgfsrc" );
158             root = fgScanForRoot(config.str());
159         }
160     }
161     
162     // Next check if fg-root is set as an env variable
163     if ( root == "" ) {
164         envp = ::getenv( "FG_ROOT" );
165         if ( envp != NULL ) {
166             root = envp;
167         }
168     }
169
170     // Otherwise, default to a random compiled-in location if we can't
171     // find fg-root any other way.
172     if ( root == "" ) {
173 #if defined( __CYGWIN__ )
174         root = "/FlightGear";
175 #elif defined( WIN32 )
176         root = "\\FlightGear";
177 #elif defined( macintosh )
178         root = "";
179 #else
180         root = PKGLIBDIR;
181 #endif
182     }
183
184     SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
185     globals->set_fg_root(root);
186
187     return true;
188 }
189
190
191 // Return the current base package version
192 string fgBasePackageVersion() {
193     SGPath base_path( globals->get_fg_root() );
194     base_path.append("version");
195
196     sg_gzifstream in( base_path.str() );
197     if ( !in.is_open() ) {
198         SGPath old_path( globals->get_fg_root() );
199         old_path.append( "Thanks" );
200         sg_gzifstream old( old_path.str() );
201         if ( !old.is_open() ) {
202             return "[none]";
203         } else {
204             return "[old version]";
205         }
206     }
207
208     string version;
209     in >> version;
210
211     return version;
212 }
213
214
215 // Read in configuration (file and command line)
216 bool fgInitConfig ( int argc, char **argv ) {
217
218                                 // First, set some sane default values
219     fgSetDefaults();
220
221     // Read global preferences from $FG_ROOT/preferences.xml
222     SGPath props_path(globals->get_fg_root());
223     props_path.append("preferences.xml");
224     SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences");
225     try {
226       readProperties(props_path.str(), globals->get_props());
227     } catch (const sg_exception &e) {
228       string message = "Error reading global preferences: ";
229       message += e.getFormattedMessage();
230       SG_LOG(SG_INPUT, SG_ALERT, message);
231       exit(2);
232     }
233     SG_LOG(SG_INPUT, SG_INFO, "Finished Reading global preferences");
234
235     // Read the default aircraft config file.
236     string aircraft = fgGetString("/sim/aircraft", "");
237     if (aircraft.size() > 0) {
238       SGPath aircraft_path(globals->get_fg_root());
239       aircraft_path.append("Aircraft");
240       aircraft_path.append(aircraft);
241       aircraft_path.concat("-set.xml");
242       SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft
243              << " from " << props_path.str());
244       try {
245         readProperties(aircraft_path.str(), globals->get_props());
246       } catch (const sg_exception &e) {
247         string message = "Error reading default aircraft: ";
248         message += e.getFormattedMessage();
249         SG_LOG(SG_INPUT, SG_ALERT, message);
250         exit(2);
251       }
252     } else {
253       SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified");
254     }
255
256     // Attempt to locate and parse the various config files in order
257     // from least precidence to greatest precidence
258
259     // Check for $fg_root/system.fgfsrc
260     SGPath config( globals->get_fg_root() );
261     config.append( "system.fgfsrc" );
262     fgParseOptions(config.str());
263
264     char name[256];
265 #if defined( unix ) || defined( __CYGWIN__ )
266     // Check for $fg_root/system.fgfsrc.hostname
267     gethostname( name, 256 );
268     config.concat( "." );
269     config.concat( name );
270     fgParseOptions(config.str());
271 #endif
272
273     // Check for ~/.fgfsrc
274     char* envp = ::getenv( "HOME" );
275     if ( envp != NULL ) {
276         config.set( envp );
277         config.append( ".fgfsrc" );
278         fgParseOptions(config.str());
279     }
280
281 #if defined( unix ) || defined( __CYGWIN__ )
282     // Check for ~/.fgfsrc.hostname
283     gethostname( name, 256 );
284     config.concat( "." );
285     config.concat( name );
286     fgParseOptions(config.str());
287 #endif
288
289     // Parse remaining command line options
290     // These will override anything specified in a config file
291     fgParseArgs(argc, argv);
292
293     return true;
294 }
295
296
297 // find basic airport location info from airport database
298 bool fgFindAirportID( const string& id, FGAirport *a ) {
299     if ( id.length() ) {
300         SGPath path( globals->get_fg_root() );
301         path.append( "Airports" );
302         path.append( "simple.mk4" );
303         FGAirports airports( path.c_str() );
304
305         SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
306
307         if ( ! airports.search( id, a ) ) {
308             SG_LOG( SG_GENERAL, SG_ALERT,
309                     "Failed to find " << id << " in " << path.str() );
310             return false;
311         }
312     } else {
313         return false;
314     }
315
316     SG_LOG( SG_GENERAL, SG_INFO,
317             "Position for " << id << " is ("
318             << a->longitude << ", "
319             << a->latitude << ")" );
320
321     return true;
322 }
323
324
325 // Set current_options lon/lat given an airport id
326 bool fgSetPosFromAirportID( const string& id ) {
327     FGAirport a;
328     // double lon, lat;
329
330     SG_LOG( SG_GENERAL, SG_INFO,
331             "Attempting to set starting position from airport code " << id );
332
333     if ( fgFindAirportID( id, &a ) ) {
334         fgSetDouble("/position/longitude-deg",  a.longitude );
335         fgSetDouble("/position/latitude-deg",  a.latitude );
336         SG_LOG( SG_GENERAL, SG_INFO,
337                 "Position for " << id << " is ("
338                 << a.longitude << ", "
339                 << a.latitude << ")" );
340
341         return true;
342     } else {
343         return false;
344     }
345
346 }
347
348
349 // Set current_options lon/lat given an airport id and heading (degrees)
350 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
351     FGRunway r;
352     FGRunway found_r;
353     double found_dir = 0.0;
354
355     if ( id.length() ) {
356         // set initial position from runway and heading
357
358         SGPath path( globals->get_fg_root() );
359         path.append( "Airports" );
360         path.append( "runways.mk4" );
361         FGRunways runways( path.c_str() );
362
363         SG_LOG( SG_GENERAL, SG_INFO,
364                 "Attempting to set starting position from runway code "
365                 << id << " heading " << tgt_hdg );
366
367         // SGPath inpath( globals->get_fg_root() );
368         // inpath.append( "Airports" );
369         // inpath.append( "apt_simple" );
370         // airports.load( inpath.c_str() );
371
372         // SGPath outpath( globals->get_fg_root() );
373         // outpath.append( "Airports" );
374         // outpath.append( "simple.gdbm" );
375         // airports.dump_gdbm( outpath.c_str() );
376
377         if ( ! runways.search( id, &r ) ) {
378             SG_LOG( SG_GENERAL, SG_ALERT,
379                     "Failed to find " << id << " in database." );
380             return false;
381         }
382
383         double diff;
384         double min_diff = 360.0;
385
386         while ( r.id == id ) {
387             // forward direction
388             diff = tgt_hdg - r.heading;
389             while ( diff < -180.0 ) { diff += 360.0; }
390             while ( diff >  180.0 ) { diff -= 360.0; }
391             diff = fabs(diff);
392             SG_LOG( SG_GENERAL, SG_INFO,
393                     "Runway " << r.rwy_no << " heading = " << r.heading <<
394                     " diff = " << diff );
395             if ( diff < min_diff ) {
396                 min_diff = diff;
397                 found_r = r;
398                 found_dir = 0;
399             }
400
401             // reverse direction
402             diff = tgt_hdg - r.heading - 180.0;
403             while ( diff < -180.0 ) { diff += 360.0; }
404             while ( diff >  180.0 ) { diff -= 360.0; }
405             diff = fabs(diff);
406             SG_LOG( SG_GENERAL, SG_INFO,
407                     "Runway -" << r.rwy_no << " heading = " <<
408                     r.heading + 180.0 <<
409                     " diff = " << diff );
410             if ( diff < min_diff ) {
411                 min_diff = diff;
412                 found_r = r;
413                 found_dir = 180.0;
414             }
415
416             runways.next( &r );
417         }
418
419         SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << found_r.rwy_no
420                 << " + " << found_dir );
421
422     } else {
423         return false;
424     }
425
426     double heading = found_r.heading + found_dir;
427     while ( heading >= 360.0 ) { heading -= 360.0; }
428
429     double lat2, lon2, az2;
430     double azimuth = found_r.heading + found_dir + 180.0;
431     while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
432
433     SG_LOG( SG_GENERAL, SG_INFO,
434             "runway =  " << found_r.lon << ", " << found_r.lat
435             << " length = " << found_r.length * SG_FEET_TO_METER * 0.5 
436             << " heading = " << azimuth );
437     
438     geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
439                         azimuth, found_r.length * SG_FEET_TO_METER * 0.5 - 5.0,
440                         &lat2, &lon2, &az2 );
441
442     if ( fabs( fgGetDouble("/sim/startup/offset-distance") ) > SG_EPSILON ) {
443         double olat, olon;
444         double odist = fgGetDouble("/sim/startup/offset-distance");
445         odist *= SG_NM_TO_METER;
446         double oaz = azimuth;
447         if ( fabs(fgGetDouble("/sim/startup/offset-azimuth")) > SG_EPSILON ) {
448             oaz = fgGetDouble("/sim/startup/offset-azimuth") + 180;
449         }
450         while ( oaz >= 360.0 ) { oaz -= 360.0; }
451         geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
452         lat2=olat;
453         lon2=olon;
454     }
455         
456     fgSetDouble("/position/longitude-deg",  lon2 );
457     fgSetDouble("/position/latitude-deg",  lat2 );
458     fgSetDouble("/orientation/heading-deg", heading );
459
460     SG_LOG( SG_GENERAL, SG_INFO,
461             "Position for " << id << " is ("
462             << lon2 << ", "
463             << lat2 << ") new heading is "
464             << heading );
465
466     return true;
467 }
468
469
470 // General house keeping initializations
471 bool fgInitGeneral( void ) {
472     string root;
473
474 #if defined(FX) && defined(XMESA)
475     char *mesa_win_state;
476 #endif
477
478     SG_LOG( SG_GENERAL, SG_INFO, "General Initialization" );
479     SG_LOG( SG_GENERAL, SG_INFO, "======= ==============" );
480
481     root = globals->get_fg_root();
482     if ( ! root.length() ) {
483         // No root path set? Then bail ...
484         SG_LOG( SG_GENERAL, SG_ALERT,
485                 "Cannot continue without a path to the base package "
486                 << "being defined." );
487         exit(-1);
488     }
489     SG_LOG( SG_GENERAL, SG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
490
491 #if defined(FX) && defined(XMESA)
492     // initialize full screen flag
493     globals->set_fullscreen(false);
494     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
495         // Test for the MESA_GLX_FX env variable
496         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
497             // test if we are fullscreen mesa/glide
498             if ( (mesa_win_state[0] == 'f') ||
499                  (mesa_win_state[0] == 'F') ) {
500                 globals->set_fullscreen(true);
501             }
502         }
503     }
504 #endif
505
506     return true;
507 }
508
509
510 // Initialize the flight model subsystem.  This just creates the
511 // object.  The actual fdm initialization is delayed until we get a
512 // proper scenery elevation hit.  This is checked for in main.cxx
513
514 void fgInitFDM() {
515
516     if ( cur_fdm_state ) {
517         delete cur_fdm_state;
518         cur_fdm_state = 0;
519     }
520
521     double dt = 1.0 / fgGetInt("/sim/model-hz");
522     aircraft_dir = fgGetString("/sim/aircraft-dir");
523     const string &model = fgGetString("/sim/flight-model");
524
525     try {
526         if (model == "larcsim") {
527             cur_fdm_state = new FGLaRCsim( dt );
528         } else if (model == "jsb") {
529             cur_fdm_state = new FGJSBsim( dt );
530         } else if (model == "ada") {
531             cur_fdm_state = new FGADA( dt );
532         } else if (model == "balloon") {
533             cur_fdm_state = new FGBalloonSim( dt );
534         } else if (model == "magic") {
535             cur_fdm_state = new FGMagicCarpet( dt );
536         } else if (model == "external") {
537             cur_fdm_state = new FGExternal( dt );
538         } else if (model == "null") {
539             cur_fdm_state = new FGNullFDM( dt );
540         } else if (model == "yasim") {
541             cur_fdm_state = new YASim( dt );
542         } else {
543             SG_LOG(SG_GENERAL, SG_ALERT,
544                    "Unrecognized flight model '" << model
545                    << "', cannot init flight dynamics model.");
546             exit(-1);
547         }
548     } catch ( ... ) {
549         SG_LOG(SG_GENERAL, SG_ALERT, "FlightGear aborting\n\n");
550         exit(-1);
551     }
552 }
553
554
555 // Initialize view parameters
556 void fgInitView() {
557     // Initialize pilot view
558     static const SGPropertyNode *longitude
559         = fgGetNode("/position/longitude-deg");
560     static const SGPropertyNode *latitude
561         = fgGetNode("/position/latitude-deg");
562     static const SGPropertyNode *altitude
563         = fgGetNode("/position/altitude-ft");
564
565     FGViewerRPH *pilot_view
566         = (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
567
568     pilot_view->set_geod_view_pos( longitude->getDoubleValue()
569                                      * SGD_DEGREES_TO_RADIANS, 
570                                    latitude->getDoubleValue()
571                                      * SGD_DEGREES_TO_RADIANS,
572                                    altitude->getDoubleValue()
573                                      * SG_FEET_TO_METER );
574     pilot_view->set_rph( cur_fdm_state->get_Phi(),
575                          cur_fdm_state->get_Theta(),
576                          cur_fdm_state->get_Psi() );
577
578     // set current view to 0 (first) which is our main pilot view
579     globals->set_current_view( pilot_view );
580
581     SG_LOG( SG_GENERAL, SG_DEBUG, "  abs_view_pos = "
582             << globals->get_current_view()->get_abs_view_pos());
583 }
584
585
586 // This is the top level init routine which calls all the other
587 // initialization routines.  If you are adding a subsystem to flight
588 // gear, its initialization call should located in this routine.
589 // Returns non-zero if a problem encountered.
590 bool fgInitSubsystems( void ) {
591     static const SGPropertyNode *longitude
592         = fgGetNode("/position/longitude-deg");
593     static const SGPropertyNode *latitude
594         = fgGetNode("/position/latitude-deg");
595     static const SGPropertyNode *altitude
596         = fgGetNode("/position/altitude-ft");
597
598     fgLIGHT *l = &cur_light_params;
599
600     SG_LOG( SG_GENERAL, SG_INFO, "Initialize Subsystems");
601     SG_LOG( SG_GENERAL, SG_INFO, "========== ==========");
602
603
604     ////////////////////////////////////////////////////////////////////
605     // Initialize the material property subsystem.
606     ////////////////////////////////////////////////////////////////////
607
608     SGPath mpath( globals->get_fg_root() );
609     mpath.append( "materials.xml" );
610     if ( ! material_lib.load( mpath.str() ) ) {
611         SG_LOG( SG_GENERAL, SG_ALERT, "Error loading material lib!" );
612         exit(-1);
613     }
614
615
616     ////////////////////////////////////////////////////////////////////
617     // Initialize the scenery management subsystem.
618     ////////////////////////////////////////////////////////////////////
619
620     scenery.init();
621     scenery.bind();
622
623     if ( global_tile_mgr.init() ) {
624         // Load the local scenery data
625         global_tile_mgr.update( fgGetDouble("/position/longitude-deg"),
626                                 fgGetDouble("/position/latitude-deg") );
627     } else {
628         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
629         exit(-1);
630     }
631
632     SG_LOG( SG_GENERAL, SG_DEBUG,
633             "Current terrain elevation after tile mgr init " <<
634             scenery.get_cur_elev() );
635
636
637     ////////////////////////////////////////////////////////////////////
638     // Initialize the flight model subsystem.
639     ////////////////////////////////////////////////////////////////////
640
641     fgInitFDM();
642         
643     // allocates structures so must happen before any of the flight
644     // model or control parameters are set
645     fgAircraftInit();   // In the future this might not be the case.
646
647
648     ////////////////////////////////////////////////////////////////////
649     // Initialize the event manager subsystem.
650     ////////////////////////////////////////////////////////////////////
651
652     global_events.Init();
653
654     // Output event stats every 60 seconds
655     global_events.Register( "fgEVENT_MGR::PrintStats()",
656                             fgMethodCallback<fgEVENT_MGR>( &global_events,
657                                                    &fgEVENT_MGR::PrintStats),
658                             fgEVENT::FG_EVENT_READY, 60000 );
659
660
661     ////////////////////////////////////////////////////////////////////
662     // Initialize the view manager subsystem.
663     ////////////////////////////////////////////////////////////////////
664
665     fgInitView();
666
667
668     ////////////////////////////////////////////////////////////////////
669     // Initialize the lighting subsystem.
670     ////////////////////////////////////////////////////////////////////
671
672     // fgUpdateSunPos() needs a few position and view parameters set
673     // so it can calculate local relative sun angle and a few other
674     // things for correctly orienting the sky.
675     fgUpdateSunPos();
676     fgUpdateMoonPos();
677     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
678                             fgEVENT::FG_EVENT_READY, 60000);
679     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
680                             fgEVENT::FG_EVENT_READY, 60000);
681
682     // Initialize Lighting interpolation tables
683     l->Init();
684
685     // update the lighting parameters (based on sun angle)
686     global_events.Register( "fgLight::Update()",
687                             fgMethodCallback<fgLIGHT>( &cur_light_params,
688                                                        &fgLIGHT::Update),
689                             fgEVENT::FG_EVENT_READY, 30000 );
690
691
692     ////////////////////////////////////////////////////////////////////
693     // Initialize the local time subsystem.
694     ////////////////////////////////////////////////////////////////////
695
696     // update the current timezone each 30 minutes
697     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
698                             fgEVENT::FG_EVENT_READY, 1800000);
699
700
701     ////////////////////////////////////////////////////////////////////
702     // Initialize the weather subsystem.
703     ////////////////////////////////////////////////////////////////////
704
705     // Initialize the weather modeling subsystem
706 #ifndef FG_OLD_WEATHER
707     // Initialize the WeatherDatabase
708     SG_LOG(SG_GENERAL, SG_INFO, "Creating LocalWeatherDatabase");
709     sgVec3 position;
710     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
711                current_aircraft.fdm_state->get_Longitude(),
712                current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER );
713     double init_vis = fgGetDouble("/environment/visibility-m");
714
715     FGLocalWeatherDatabase::DatabaseWorkingType working_type;
716
717     if (fgGetString("/environment/weather/working-type") == "internet")
718     {
719       working_type = FGLocalWeatherDatabase::use_internet;
720     } else {
721       working_type = FGLocalWeatherDatabase::default_mode;
722     }
723     
724     if ( init_vis > 0 ) {
725       FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
726         new FGLocalWeatherDatabase( position,
727                                     globals->get_fg_root(),
728                                     working_type,
729                                     init_vis );
730     } else {
731       FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
732         new FGLocalWeatherDatabase( position,
733                                     globals->get_fg_root(),
734                                     working_type );
735     }
736
737     // cout << theFGLocalWeatherDatabase << endl;
738     // cout << "visibility = " 
739     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
740
741     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
742
743     // register the periodic update of the weather
744     global_events.Register( "weather update", fgUpdateWeatherDatabase,
745                             fgEVENT::FG_EVENT_READY, 30000);
746 #else
747     current_weather.Init();
748 #endif
749
750     ////////////////////////////////////////////////////////////////////
751     // Initialize vor/ndb/ils/fix list management and query systems
752     ////////////////////////////////////////////////////////////////////
753
754     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaids");
755
756     SG_LOG(SG_GENERAL, SG_INFO, "  VOR/NDB");
757     current_navlist = new FGNavList;
758     SGPath p_nav( globals->get_fg_root() );
759     p_nav.append( "Navaids/default.nav" );
760     current_navlist->init( p_nav );
761
762     SG_LOG(SG_GENERAL, SG_INFO, "  ILS and Marker Beacons");
763     current_beacons = new FGMarkerBeacons;
764     current_beacons->init();
765     current_ilslist = new FGILSList;
766     SGPath p_ils( globals->get_fg_root() );
767     p_ils.append( "Navaids/default.ils" );
768     current_ilslist->init( p_ils );
769
770     SG_LOG(SG_GENERAL, SG_INFO, "  Fixes");
771     current_fixlist = new FGFixList;
772     SGPath p_fix( globals->get_fg_root() );
773     p_fix.append( "Navaids/default.fix" );
774     current_fixlist->init( p_fix );
775
776     ////////////////////////////////////////////////////////////////////
777     // Initialize ATC list management and query systems
778     ////////////////////////////////////////////////////////////////////
779
780     //DCL
781     SG_LOG(SG_GENERAL, SG_INFO, "  ATIS");
782     current_atislist = new FGATISList;
783     SGPath p_atis( globals->get_fg_root() );
784     p_atis.append( "ATC/default.atis" );
785     current_atislist->init( p_atis );
786
787     ////////////////////////////////////////////////////////////////////
788     // Initialise ATC display system
789     ////////////////////////////////////////////////////////////////////
790
791     //DCL
792     SG_LOG(SG_GENERAL, SG_INFO, "  ATC Display");
793     current_atcdisplay = new FGATCDisplay;
794     current_atcdisplay->init();   
795
796     ////////////////////////////////////////////////////////////////////
797     // Initialize the built-in commands.
798     ////////////////////////////////////////////////////////////////////
799     fgInitCommands();
800
801
802 #ifdef ENABLE_AUDIO_SUPPORT
803     ////////////////////////////////////////////////////////////////////
804     // Initialize the sound subsystem.
805     ////////////////////////////////////////////////////////////////////
806
807     globals->set_soundmgr(new FGSoundMgr);
808     globals->get_soundmgr()->init();
809     globals->get_soundmgr()->bind();
810
811
812     ////////////////////////////////////////////////////////////////////
813     // Initialize the sound-effects subsystem.
814     ////////////////////////////////////////////////////////////////////
815     globals->set_fx(new FGFX);
816     globals->get_fx()->init();
817     globals->get_fx()->bind();
818
819 #endif
820
821     ////////////////////////////////////////////////////////////////////
822     // Initialize the radio stack subsystem.
823     ////////////////////////////////////////////////////////////////////
824
825                                 // A textbook example of how FGSubsystem
826                                 // should work...
827     current_radiostack = new FGRadioStack;
828     current_radiostack->init();
829     current_radiostack->bind();
830
831
832     ////////////////////////////////////////////////////////////////////
833     // Initialize the cockpit subsystem
834     ////////////////////////////////////////////////////////////////////
835
836     if( fgCockpitInit( &current_aircraft )) {
837         // Cockpit initialized ok.
838     } else {
839         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Cockpit initialization!" );
840         exit(-1);
841     }
842
843
844     ////////////////////////////////////////////////////////////////////
845     // Initialize the joystick subsystem.
846     ////////////////////////////////////////////////////////////////////
847
848     // if ( ! fgJoystickInit() ) {
849     //   SG_LOG( SG_GENERAL, SG_ALERT, "Error in Joystick initialization!" );
850     // }
851
852
853     ////////////////////////////////////////////////////////////////////
854     // Initialize the autopilot subsystem.
855     ////////////////////////////////////////////////////////////////////
856
857     current_autopilot = new FGAutopilot;
858     current_autopilot->init();
859
860     // initialize the gui parts of the autopilot
861     fgAPAdjustInit();
862     NewTgtAirportInit();
863     NewHeadingInit();
864     NewAltitudeInit();
865
866     ////////////////////////////////////////////////////////////////////
867     // Initialize I/O subsystem.
868     ////////////////////////////////////////////////////////////////////
869
870 #if ! defined( macintosh )
871     fgIOInit();
872 #endif
873
874     // Initialize the 2D panel.
875     string panel_path = fgGetString("/sim/panel/path",
876                                     "Panels/Default/default.xml");
877     current_panel = fgReadPanel(panel_path);
878     if (current_panel == 0) {
879         SG_LOG( SG_INPUT, SG_ALERT, 
880                 "Error reading new panel from " << panel_path );
881     } else {
882         SG_LOG( SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path );
883         current_panel->init();
884         current_panel->bind();
885     }
886
887     
888     ////////////////////////////////////////////////////////////////////
889     // Initialize the default (kludged) properties.
890     ////////////////////////////////////////////////////////////////////
891
892     fgInitProps();
893
894
895     ////////////////////////////////////////////////////////////////////
896     // Initialize the controls subsystem.
897     ////////////////////////////////////////////////////////////////////
898
899     globals->get_controls()->init();
900     globals->get_controls()->bind();
901
902
903     ////////////////////////////////////////////////////////////////////
904     // Initialize the input subsystem.
905     ////////////////////////////////////////////////////////////////////
906
907     current_input.init();
908     current_input.bind();
909
910
911     ////////////////////////////////////////////////////////////////////////
912     // End of subsystem initialization.
913     ////////////////////////////////////////////////////////////////////
914
915     SG_LOG( SG_GENERAL, SG_INFO, endl);
916
917                                 // Save the initial state for future
918                                 // reference.
919     globals->saveInitialState();
920
921     return true;
922 }
923
924
925 void fgReInitSubsystems( void )
926 {
927     static const SGPropertyNode *longitude
928         = fgGetNode("/position/longitude-deg");
929     static const SGPropertyNode *latitude
930         = fgGetNode("/position/latitude-deg");
931     static const SGPropertyNode *altitude
932         = fgGetNode("/position/altitude-ft");
933     static const SGPropertyNode *master_freeze
934         = fgGetNode("/sim/freeze/master");
935
936     SG_LOG( SG_GENERAL, SG_INFO,
937             "/position/altitude = " << altitude->getDoubleValue() );
938
939     bool freeze = master_freeze->getBoolValue();
940     if ( !freeze ) {
941         fgSetBool("/sim/freeze/master", true);
942     }
943     
944     // Initialize the Scenery Management subsystem
945     scenery.init();
946
947 #if 0
948     if( global_tile_mgr.init() ) {
949         Load the local scenery data
950         global_tile_mgr.update( longitude->getDoubleValue(),
951                                 latitude->getDoubleValue() );
952     } else {
953         SG_LOG( SG_GENERAL, SG_ALERT, "Error in Tile Manager initialization!" );
954         exit(-1);
955     }
956 #endif
957
958     fgInitFDM();
959
960     fgInitView();
961
962     globals->get_controls()->reset_all();
963     current_autopilot->reset();
964
965     fgUpdateSunPos();
966     fgUpdateMoonPos();
967     cur_light_params.Update();
968     fgUpdateLocalTime();
969
970     if ( !freeze ) {
971         fgSetBool("/sim/freeze/master", false);
972     }
973 }