]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
Minor tweaks.
[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 FG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #include <GL/glut.h>
35 #include <simgear/xgl/xgl.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 // work around a stdc++ lib bug in some versions of linux, but doesn't
41 // seem to hurt to have this here for all versions of Linux.
42 #ifdef linux
43 #  define _G_NO_EXTERN_TEMPLATES
44 #endif
45
46 #include <simgear/compiler.h>
47
48 #include STL_STRING
49
50 #include <simgear/constants.h>
51 #include <simgear/debug/logstream.hxx>
52 #include <simgear/math/point3d.hxx>
53 #include <simgear/math/polar3d.hxx>
54 #include <simgear/math/sg_geodesy.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/timing/sg_time.hxx>
57
58 #include <Aircraft/aircraft.hxx>
59 #include <Airports/runways.hxx>
60 #include <Airports/simple.hxx>
61 #include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/cockpit.hxx>
64 #include <Cockpit/radiostack.hxx>
65 #include <Cockpit/panel.hxx>
66 #include <Cockpit/panel_io.hxx>
67 #include <FDM/ADA.hxx>
68 #include <FDM/Balloon.h>
69 #include <FDM/External.hxx>
70 #include <FDM/JSBSim.hxx>
71 #include <FDM/LaRCsim.hxx>
72 #include <FDM/MagicCarpet.hxx>
73 #include <Include/general.hxx>
74 #include <Joystick/joystick.hxx>
75 #include <Objects/matlib.hxx>
76 #include <Navaids/fixlist.hxx>
77 #include <Navaids/ilslist.hxx>
78 #include <Navaids/navlist.hxx>
79 #include <Scenery/scenery.hxx>
80 #include <Scenery/tilemgr.hxx>
81 #include <Time/event.hxx>
82 #include <Time/light.hxx>
83 #include <Time/sunpos.hxx>
84 #include <Time/moonpos.hxx>
85 #include <Time/tmp.hxx>
86
87 #ifndef FG_OLD_WEATHER
88 #  include <WeatherCM/FGLocalWeatherDatabase.h>
89 #else
90 #  include <Weather/weather.hxx>
91 #endif
92
93 #include "fg_init.hxx"
94 #include "fg_io.hxx"
95 #include "globals.hxx"
96 #include "bfi.hxx"
97
98 #if defined(FX) && defined(XMESA)
99 #include <GL/xmesa.h>
100 #endif
101
102 FG_USING_STD(string);
103
104 extern const char *default_root;
105
106
107 // Read in configuration (file and command line) and just set fg_root
108 bool fgInitFGRoot ( int argc, char **argv ) {
109     // Attempt to locate and parse a config file
110     // First check fg_root
111     FGPath config( globals->get_options()->get_fg_root() );
112     config.append( "system.fgfsrc" );
113     globals->get_options()->scan_config_file_for_root( config.str() );
114
115     // Next check home directory
116     char* envp = ::getenv( "HOME" );
117     if ( envp != NULL ) {
118         config.set( envp );
119         config.append( ".fgfsrc" );
120         globals->get_options()->scan_config_file_for_root( config.str() );
121     }
122
123     // Parse remaining command line options
124     // These will override anything specified in a config file
125     globals->get_options()->scan_command_line_for_root(argc, argv);
126
127     return true;
128 }
129
130
131 // Read in configuration (file and command line)
132 bool fgInitConfig ( int argc, char **argv ) {
133     // Attempt to locate and parse a config file
134     // First check fg_root
135     FGPath config( globals->get_options()->get_fg_root() );
136     config.append( "system.fgfsrc" );
137     globals->get_options()->parse_config_file( config.str() );
138
139     // Next check home directory
140     char* envp = ::getenv( "HOME" );
141     if ( envp != NULL ) {
142         config.set( envp );
143         config.append( ".fgfsrc" );
144         globals->get_options()->parse_config_file( config.str() );
145     }
146
147     // Parse remaining command line options
148     // These will override anything specified in a config file
149     if ( globals->get_options()->parse_command_line(argc, argv) !=
150          FGOptions::FG_OPTIONS_OK )
151     {
152         // Something must have gone horribly wrong with the command
153         // line parsing or maybe the user just requested help ... :-)
154         globals->get_options()->usage();
155         FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
156         return false;
157     }
158
159     return true;
160 }
161
162
163 // find basic airport location info from airport database
164 bool fgFindAirportID( const string& id, FGAirport *a ) {
165     if ( id.length() ) {
166         FGPath path( globals->get_options()->get_fg_root() );
167         path.append( "Airports" );
168         path.append( "simple.mk4" );
169         FGAirports airports( path.c_str() );
170
171         FG_LOG( FG_GENERAL, FG_INFO, "Searching for airport code = " << id );
172
173         if ( ! airports.search( id, a ) ) {
174             FG_LOG( FG_GENERAL, FG_ALERT,
175                     "Failed to find " << id << " in " << path.str() );
176             return false;
177         }
178     } else {
179         return false;
180     }
181
182     FG_LOG( FG_GENERAL, FG_INFO,
183             "Position for " << id << " is ("
184             << a->longitude << ", "
185             << a->latitude << ")" );
186
187     return true;
188 }
189
190
191 // Set current_options lon/lat given an airport id
192 bool fgSetPosFromAirportID( const string& id ) {
193     FGAirport a;
194     // double lon, lat;
195
196     FG_LOG( FG_GENERAL, FG_INFO,
197             "Attempting to set starting position from airport code " << id );
198
199     if ( fgFindAirportID( id, &a ) ) {
200         globals->get_options()->set_lon( a.longitude );
201         globals->get_options()->set_lat( a.latitude );
202         globals->get_props()->setDoubleValue("/position/longitude",
203                                           a.longitude);
204         globals->get_props()->setDoubleValue("/position/latitude",
205                                           a.latitude);
206
207         FG_LOG( FG_GENERAL, FG_INFO,
208                 "Position for " << id << " is ("
209                 << a.longitude << ", "
210                 << a.latitude << ")" );
211
212         return true;
213     } else {
214         return false;
215     }
216
217 }
218
219
220 // Set current_options lon/lat given an airport id and heading (degrees)
221 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
222     FGRunway r;
223     FGRunway found_r;
224     double found_dir = 0.0;
225
226     if ( id.length() ) {
227         // set initial position from runway and heading
228
229         FGPath path( globals->get_options()->get_fg_root() );
230         path.append( "Airports" );
231         path.append( "runways.mk4" );
232         FGRunways runways( path.c_str() );
233
234         FG_LOG( FG_GENERAL, FG_INFO,
235                 "Attempting to set starting position from runway code "
236                 << id << " heading " << tgt_hdg );
237
238         // FGPath inpath( globals->get_options()->get_fg_root() );
239         // inpath.append( "Airports" );
240         // inpath.append( "apt_simple" );
241         // airports.load( inpath.c_str() );
242
243         // FGPath outpath( globals->get_options()->get_fg_root() );
244         // outpath.append( "Airports" );
245         // outpath.append( "simple.gdbm" );
246         // airports.dump_gdbm( outpath.c_str() );
247
248         if ( ! runways.search( id, &r ) ) {
249             FG_LOG( FG_GENERAL, FG_ALERT,
250                     "Failed to find " << id << " in database." );
251             return false;
252         }
253
254         double diff;
255         double min_diff = 360.0;
256
257         while ( r.id == id ) {
258             // forward direction
259             diff = tgt_hdg - r.heading;
260             while ( diff < -180.0 ) { diff += 360.0; }
261             while ( diff >  180.0 ) { diff -= 360.0; }
262             diff = fabs(diff);
263             FG_LOG( FG_GENERAL, FG_INFO,
264                     "Runway " << r.rwy_no << " heading = " << r.heading <<
265                     " diff = " << diff );
266             if ( diff < min_diff ) {
267                 min_diff = diff;
268                 found_r = r;
269                 found_dir = 0;
270             }
271
272             // reverse direction
273             diff = tgt_hdg - r.heading - 180.0;
274             while ( diff < -180.0 ) { diff += 360.0; }
275             while ( diff >  180.0 ) { diff -= 360.0; }
276             diff = fabs(diff);
277             FG_LOG( FG_GENERAL, FG_INFO,
278                     "Runway -" << r.rwy_no << " heading = " <<
279                     r.heading + 180.0 <<
280                     " diff = " << diff );
281             if ( diff < min_diff ) {
282                 min_diff = diff;
283                 found_r = r;
284                 found_dir = 180.0;
285             }
286
287             runways.next( &r );
288         }
289
290         FG_LOG( FG_GENERAL, FG_INFO, "closest runway = " << found_r.rwy_no
291                 << " + " << found_dir );
292
293     } else {
294         return false;
295     }
296
297     double heading = found_r.heading + found_dir;
298     while ( heading >= 360.0 ) { heading -= 360.0; }
299
300     double lat2, lon2, az2;
301     double azimuth = found_r.heading + found_dir + 180.0;
302     while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
303
304     FG_LOG( FG_GENERAL, FG_INFO,
305             "runway =  " << found_r.lon << ", " << found_r.lat
306             << " length = " << found_r.length * FEET_TO_METER * 0.5 
307             << " heading = " << azimuth );
308     geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
309                         azimuth, found_r.length * FEET_TO_METER * 0.5 - 5.0,
310                         &lat2, &lon2, &az2 );
311     globals->get_options()->set_lon( lon2 );
312     globals->get_options()->set_lat( lat2 );
313     globals->get_options()->set_heading( heading );
314     globals->get_props()->setDoubleValue("/position/longitude", lon2);
315     globals->get_props()->setDoubleValue("/position/latitude", lat2);
316     globals->get_props()->setDoubleValue("/orientation/heading", heading);
317
318     FG_LOG( FG_GENERAL, FG_INFO,
319             "Position for " << id << " is ("
320             << lon2 << ", "
321             << lat2 << ") new heading is "
322             << heading );
323
324     return true;
325 }
326
327
328 // Set initial position and orientation
329 bool fgInitPosition( void ) {
330     FGInterface *f = current_aircraft.fdm_state;
331     string id = globals->get_options()->get_airport_id();
332
333     // set initial position from default or command line coordinates
334     f->set_Longitude( globals->get_options()->get_lon() * DEG_TO_RAD );
335     f->set_Latitude( globals->get_options()->get_lat() * DEG_TO_RAD );
336
337     if ( scenery.cur_elev > globals->get_options()->get_altitude() - 1) {
338         globals->get_options()->set_altitude( scenery.cur_elev + 1 );
339     }
340
341     FG_LOG( FG_GENERAL, FG_INFO,
342             "starting altitude is = " << globals->get_options()->get_altitude() );
343
344     f->set_Altitude( globals->get_options()->get_altitude() * METER_TO_FEET );
345     // fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
346     //                          f->get_Altitude() * FEET_TO_METER );
347
348 #if 0
349     globals->get_props()->setDoubleValue("/position/longitude",
350                                       f->get_Longitude() * RAD_TO_DEG);
351     globals->get_props()->setDoubleValue("/position/latitude",
352                                       f->get_Latitude() * RAD_TO_DEG);
353     globals->get_props()->setDoubleValue("/position/altitude",
354                                       f->get_Altitude() * RAD_TO_DEG);
355 #endif
356
357     FG_LOG( FG_GENERAL, FG_INFO,
358             "Initial position is: ("
359             << (f->get_Longitude() * RAD_TO_DEG) << ", "
360             << (f->get_Latitude() * RAD_TO_DEG) << ", "
361             << (f->get_Altitude() * FEET_TO_METER) << ")" );
362
363     return true;
364 }
365
366
367 // General house keeping initializations
368 bool fgInitGeneral( void ) {
369     string root;
370
371 #if defined(FX) && defined(XMESA)
372     char *mesa_win_state;
373 #endif
374
375     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
376     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
377
378     root = globals->get_options()->get_fg_root();
379     if ( ! root.length() ) {
380         // No root path set? Then bail ...
381         FG_LOG( FG_GENERAL, FG_ALERT,
382                 "Cannot continue without environment variable FG_ROOT"
383                 << "being defined." );
384         exit(-1);
385     }
386     FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
387
388 #if defined(FX) && defined(XMESA)
389     // initialize full screen flag
390     global_fullscreen = false;
391     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
392         // Test for the MESA_GLX_FX env variable
393         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
394             // test if we are fullscreen mesa/glide
395             if ( (mesa_win_state[0] == 'f') ||
396                  (mesa_win_state[0] == 'F') ) {
397                 global_fullscreen = true;
398             }
399         }
400     }
401 #endif
402
403     return true;
404 }
405
406
407 // set initial aircraft speed
408 bool fgVelocityInit( void ) {
409   switch(globals->get_options()->get_speedset()) {
410     case FGOptions::FG_VC:
411       current_aircraft.fdm_state->set_V_calibrated_kts( 
412                                     globals->get_options()->get_vc() );
413       break;
414     case FGOptions::FG_MACH:
415       current_aircraft.fdm_state->set_Mach_number(
416                                     globals->get_options()->get_mach() );
417       break;
418     case FGOptions::FG_VTUVW:
419       current_aircraft.fdm_state->set_Velocities_Wind_Body(
420                                     globals->get_options()->get_uBody(),
421                                     globals->get_options()->get_vBody(),
422                                     globals->get_options()->get_wBody() );
423       break;
424     case FGOptions::FG_VTNED:
425       current_aircraft.fdm_state->set_Velocities_Local(
426                                     globals->get_options()->get_vNorth(),
427                                     globals->get_options()->get_vEast(),
428                                     globals->get_options()->get_vDown() );
429       break;
430     default:
431       current_aircraft.fdm_state->set_V_calibrated_kts( 0.0 );
432     }
433     return true;  
434 }             
435
436         
437 // This is the top level init routine which calls all the other
438 // initialization routines.  If you are adding a subsystem to flight
439 // gear, its initialization call should located in this routine.
440 // Returns non-zero if a problem encountered.
441 bool fgInitSubsystems( void ) {
442     fgLIGHT *l = &cur_light_params;
443
444     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
445     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
446
447     // Initialize the material property lib
448     FGPath mpath( globals->get_options()->get_fg_root() );
449     mpath.append( "materials" );
450     if ( material_lib.load( mpath.str() ) ) {
451     } else {
452         FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
453         exit(-1);
454     }
455
456     // Initialize the Scenery Management subsystem
457     if ( fgSceneryInit() ) {
458         // Material lib initialized ok.
459     } else {
460         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
461         exit(-1);
462     }
463
464     if ( global_tile_mgr.init() ) {
465         // Load the local scenery data
466         global_tile_mgr.update( globals->get_options()->get_lon(),
467                                 globals->get_options()->get_lat() );
468     } else {
469         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
470         exit(-1);
471     }
472
473     FG_LOG( FG_GENERAL, FG_DEBUG,
474             "Current terrain elevation after tile mgr init " <<
475             scenery.cur_elev );
476
477     if ( globals->get_options()->get_flight_model() == FGInterface::FG_LARCSIM ) {
478         cur_fdm_state = new FGLaRCsim;
479     } else if ( globals->get_options()->get_flight_model() == FGInterface::FG_JSBSIM ) {
480         cur_fdm_state = new FGJSBsim;
481     } else if ( globals->get_options()->get_flight_model() == FGInterface::FG_ADA ) {
482         cur_fdm_state = new FGADA;
483     } else if ( globals->get_options()->get_flight_model() == 
484                 FGInterface::FG_BALLOONSIM ) {
485         cur_fdm_state = new FGBalloonSim;
486     } else if ( globals->get_options()->get_flight_model() == 
487                 FGInterface::FG_MAGICCARPET ) {
488         cur_fdm_state = new FGMagicCarpet;
489     } else if ( globals->get_options()->get_flight_model() == 
490                 FGInterface::FG_EXTERNAL ) {
491         cur_fdm_state = new FGExternal;
492     } else {
493         FG_LOG( FG_GENERAL, FG_ALERT,
494                 "No flight model, can't init aircraft" );
495         exit(-1);
496     }
497
498     // allocates structures so must happen before any of the flight
499     // model or control parameters are set
500     fgAircraftInit();   // In the future this might not be the case.
501
502     fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
503                              scenery.cur_elev );
504     
505     // set the initial position
506     fgInitPosition();
507
508     // Calculate ground elevation at starting point (we didn't have
509     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
510     //
511     // calculalate a cartesian point somewhere along the line between
512     // the center of the earth and our view position.  Doesn't have to
513     // be the exact elevation (this is good because we don't know it
514     // yet :-)
515
516     // now handled inside of the fgTileMgrUpdate()
517
518     // Reset our altitude if we are below ground
519     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = "
520             << cur_fdm_state->get_Altitude() );
521     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
522             cur_fdm_state->get_Runway_altitude() );
523
524     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() +
525          3.758099) {
526         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
527                                      3.758099 );
528     }
529
530     FG_LOG( FG_GENERAL, FG_INFO,
531             "Updated position (after elevation adj): ("
532             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
533             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
534             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
535
536     // We need to calculate a few sea_level_radius here so we can pass
537     // the correct value to the view class
538     double sea_level_radius_meters;
539     double lat_geoc;
540     sgGeodToGeoc( cur_fdm_state->get_Latitude(),
541                   cur_fdm_state->get_Altitude(),
542                   &sea_level_radius_meters, &lat_geoc);
543     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters *
544                                          METER_TO_FEET );
545
546     // The following section sets up the flight model EOM parameters
547     // and should really be read in from one or more files.
548
549     // Initial Velocity
550     fgVelocityInit();
551
552     // Initial Orientation
553     cur_fdm_state->
554         set_Euler_Angles( globals->get_options()->get_roll() * DEG_TO_RAD,
555                           globals->get_options()->get_pitch() * DEG_TO_RAD,
556                           globals->get_options()->get_heading() * DEG_TO_RAD );
557
558     // Initialize the event manager
559     global_events.Init();
560
561     // Output event stats every 60 seconds
562     global_events.Register( "fgEVENT_MGR::PrintStats()",
563                             fgMethodCallback<fgEVENT_MGR>( &global_events,
564                                                    &fgEVENT_MGR::PrintStats),
565                             fgEVENT::FG_EVENT_READY, 60000 );
566
567     // Initialize win_ratio parameters
568     for ( int i = 0; i < globals->get_viewmgr()->size(); ++i ) {
569         globals->get_viewmgr()->get_view(i)->
570             set_win_ratio( globals->get_options()->get_xsize() /
571                            globals->get_options()->get_ysize() );
572     }
573
574     // Initialize pilot view
575     FGViewerRPH *pilot_view =
576         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
577
578     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
579                                    cur_fdm_state->get_Lat_geocentric(), 
580                                    cur_fdm_state->get_Altitude() *
581                                    FEET_TO_METER );
582     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
583                                       FEET_TO_METER ); 
584     pilot_view->set_rph( cur_fdm_state->get_Phi(),
585                          cur_fdm_state->get_Theta(),
586                          cur_fdm_state->get_Psi() );
587
588     // set current view to 0 (first) which is our main pilot view
589     globals->set_current_view( pilot_view );
590
591     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = "
592             << globals->get_current_view()->get_abs_view_pos());
593
594     // fgUpdateSunPos() needs a few position and view parameters set
595     // so it can calculate local relative sun angle and a few other
596     // things for correctly orienting the sky.
597     fgUpdateSunPos();
598     fgUpdateMoonPos();
599     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
600                             fgEVENT::FG_EVENT_READY, 60000);
601     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
602                             fgEVENT::FG_EVENT_READY, 60000);
603
604     // Initialize Lighting interpolation tables
605     l->Init();
606
607     // update the lighting parameters (based on sun angle)
608     global_events.Register( "fgLight::Update()",
609                             fgMethodCallback<fgLIGHT>( &cur_light_params,
610                                                        &fgLIGHT::Update),
611                             fgEVENT::FG_EVENT_READY, 30000 );
612     // update the current timezone each 30 minutes
613     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
614                             fgEVENT::FG_EVENT_READY, 1800000);
615
616     // Initialize the weather modeling subsystem
617 #ifndef FG_OLD_WEATHER
618     // Initialize the WeatherDatabase
619     FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
620     sgVec3 position;
621     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
622                current_aircraft.fdm_state->get_Longitude(),
623                current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
624     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
625         new FGLocalWeatherDatabase( position,
626                                     globals->get_options()->get_fg_root() );
627     // cout << theFGLocalWeatherDatabase << endl;
628     // cout << "visibility = " 
629     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
630
631     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
632     
633     double init_vis = globals->get_options()->get_default_visibility();
634     if ( init_vis > 0 ) {
635         WeatherDatabase->setWeatherVisibility( init_vis );
636     }
637
638     // register the periodic update of the weather
639     global_events.Register( "weather update", fgUpdateWeatherDatabase,
640                             fgEVENT::FG_EVENT_READY, 30000);
641 #else
642     current_weather.Init();
643 #endif
644
645     // Initialize vor/ndb/ils/fix list management and query systems
646     FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
647
648     FG_LOG(FG_GENERAL, FG_INFO, "  VOR/NDB");
649     current_navlist = new FGNavList;
650     FGPath p_nav( globals->get_options()->get_fg_root() );
651     p_nav.append( "Navaids/default.nav" );
652     current_navlist->init( p_nav );
653
654     FG_LOG(FG_GENERAL, FG_INFO, "  ILS");
655     current_ilslist = new FGILSList;
656     FGPath p_ils( globals->get_options()->get_fg_root() );
657     p_ils.append( "Navaids/default.ils" );
658     current_ilslist->init( p_ils );
659
660     FG_LOG(FG_GENERAL, FG_INFO, "  Fixes");
661     current_fixlist = new FGFixList;
662     FGPath p_fix( globals->get_options()->get_fg_root() );
663     p_fix.append( "Navaids/default.fix" );
664     current_fixlist->init( p_fix );
665
666     // Initialize the underlying radio stack model
667     current_radiostack = new FGRadioStack;
668
669     current_radiostack->search( cur_fdm_state->get_Longitude(),
670                                 cur_fdm_state->get_Latitude(),
671                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
672
673     current_radiostack->update( cur_fdm_state->get_Longitude(),
674                                 cur_fdm_state->get_Latitude(),
675                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
676
677     // Search radio database once per second
678     global_events.Register( "fgRadioSearch()", fgRadioSearch,
679                             fgEVENT::FG_EVENT_READY, 1000);
680
681
682     // Initialize the Cockpit subsystem
683     if( fgCockpitInit( &current_aircraft )) {
684         // Cockpit initialized ok.
685     } else {
686         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
687         exit(-1);
688     }
689
690     // Initialize the flight model subsystem data structures base on
691     // above values
692
693     if ( cur_fdm_state->init( 1.0 / globals->get_options()->get_model_hz() ) ) {
694         // fdm init successful
695     } else {
696         FG_LOG( FG_GENERAL, FG_ALERT, "FDM init() failed!  Cannot continue." );
697         exit(-1);
698     }
699
700     // *ABCD* I'm just sticking this here for now, it should probably
701     // move eventually
702     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
703
704     if ( cur_fdm_state->get_Altitude() <
705          cur_fdm_state->get_Runway_altitude() + 3.758099)
706     {
707         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
708                                      3.758099 );
709     }
710
711     FG_LOG( FG_GENERAL, FG_INFO,
712             "Updated position (after elevation adj): ("
713             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
714             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
715             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
716     // *ABCD* end of thing that I just stuck in that I should probably
717     // move
718
719     // Joystick support
720     if ( ! fgJoystickInit() ) {
721         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
722     }
723
724     // Autopilot init
725     current_autopilot = new FGAutopilot;
726     current_autopilot->init();
727
728     // initialize the gui parts of the autopilot
729     NewTgtAirportInit();
730     fgAPAdjustInit() ;
731     NewHeadingInit();
732     NewAltitudeInit();
733
734     // Initialize I/O channels
735 #if ! defined( macintosh )
736     fgIOInit();
737 #endif
738
739     // Initialize the 2D panel.
740     string panel_path =
741         globals->get_props()->getStringValue("/sim/panel/path",
742                                             "Panels/Default/default.xml");
743     current_panel = fgReadPanel(panel_path);
744     if (current_panel == 0) {
745         FG_LOG( FG_INPUT, FG_ALERT, 
746                 "Error reading new panel from " << panel_path );
747     } else {
748         FG_LOG( FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path );
749     }
750
751     // Initialize the BFI
752     FGBFI::init();
753
754     controls.init();
755     controls.bind();
756
757     FG_LOG( FG_GENERAL, FG_INFO, endl);
758
759     return true;
760 }
761
762
763 void fgReInitSubsystems( void )
764 {
765     bool freeze = globals->get_freeze();
766     if( !freeze )
767         globals->set_freeze( true );
768     
769     if( global_tile_mgr.init() ) {
770         // Load the local scenery data
771         global_tile_mgr.update( globals->get_options()->get_lon(),
772                                 globals->get_options()->get_lat() );
773     } else {
774         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
775                 exit(-1);
776     }
777
778     // cout << "current scenery elev = " << scenery.cur_elev << endl;
779
780     fgInitPosition();
781     fgFDMSetGroundElevation( globals->get_options()->get_flight_model(), 
782                              scenery.cur_elev );
783
784     // Reset our altitude if we are below ground
785     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = "
786             << cur_fdm_state->get_Altitude() );
787     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = "
788             << cur_fdm_state->get_Runway_altitude() );
789
790     if ( cur_fdm_state->get_Altitude() <
791          cur_fdm_state->get_Runway_altitude() + 3.758099)
792     {
793         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
794                                      3.758099 );
795     }
796     double sea_level_radius_meters;
797     double lat_geoc;
798     sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
799                   &sea_level_radius_meters, &lat_geoc);
800     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters *
801                                          METER_TO_FEET );
802         
803     // The following section sets up the flight model EOM parameters
804     // and should really be read in from one or more files.
805
806     // Initial Velocity
807     fgVelocityInit();
808
809     // Initial Orientation
810     cur_fdm_state->
811         set_Euler_Angles( globals->get_options()->get_roll() * DEG_TO_RAD,
812                           globals->get_options()->get_pitch() * DEG_TO_RAD,
813                           globals->get_options()->get_heading() * DEG_TO_RAD );
814
815     // Initialize view parameters
816     FGViewerRPH *pilot_view =
817         (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
818
819     pilot_view->set_view_offset( 0.0 );
820     pilot_view->set_goal_view_offset( 0.0 );
821
822     pilot_view->set_geod_view_pos( cur_fdm_state->get_Longitude(), 
823                                    cur_fdm_state->get_Lat_geocentric(), 
824                                    cur_fdm_state->get_Altitude() *
825                                    FEET_TO_METER );
826     pilot_view->set_sea_level_radius( cur_fdm_state->get_Sea_level_radius() *
827                                       FEET_TO_METER ); 
828     pilot_view->set_rph( cur_fdm_state->get_Phi(),
829                          cur_fdm_state->get_Theta(),
830                          cur_fdm_state->get_Psi() );
831
832     // set current view to 0 (first) which is our main pilot view
833     globals->set_current_view( pilot_view );
834
835     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = "
836             << globals->get_current_view()->get_abs_view_pos());
837
838     cur_fdm_state->init( 1.0 / globals->get_options()->get_model_hz() );
839
840     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
841
842     if ( cur_fdm_state->get_Altitude() <
843          cur_fdm_state->get_Runway_altitude() + 3.758099)
844     {
845         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() +
846                                      3.758099 );
847     }
848
849     controls.reset_all();
850     current_autopilot->reset();
851
852     if( !freeze )
853         globals->set_freeze( false );
854 }