]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
b0ab0d5164232af6b699f44c05f6e787d1774fd2
[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 "options.hxx"
97 #include "bfi.hxx"
98
99 #if defined(FX) && defined(XMESA)
100 #include <GL/xmesa.h>
101 #endif
102
103 FG_USING_STD(string);
104
105 extern const char *default_root;
106
107
108 // Read in configuration (file and command line) and just set fg_root
109 bool fgInitFGRoot ( int argc, char **argv ) {
110     // Attempt to locate and parse a config file
111     // First check fg_root
112     FGPath config( current_options.get_fg_root() );
113     config.append( "system.fgfsrc" );
114     current_options.scan_config_file_for_root( config.str() );
115
116     // Next check home directory
117     char* envp = ::getenv( "HOME" );
118     if ( envp != NULL ) {
119         config.set( envp );
120         config.append( ".fgfsrc" );
121         current_options.scan_config_file_for_root( config.str() );
122     }
123
124     // Parse remaining command line options
125     // These will override anything specified in a config file
126     current_options.scan_command_line_for_root(argc, argv);
127
128     return true;
129 }
130
131
132 // Read in configuration (file and command line)
133 bool fgInitConfig ( int argc, char **argv ) {
134     // Attempt to locate and parse a config file
135     // First check fg_root
136     FGPath config( current_options.get_fg_root() );
137     config.append( "system.fgfsrc" );
138     current_options.parse_config_file( config.str() );
139
140     // Next check home directory
141     char* envp = ::getenv( "HOME" );
142     if ( envp != NULL ) {
143         config.set( envp );
144         config.append( ".fgfsrc" );
145         current_options.parse_config_file( config.str() );
146     }
147
148     // Parse remaining command line options
149     // These will override anything specified in a config file
150     if ( current_options.parse_command_line(argc, argv) !=
151          fgOPTIONS::FG_OPTIONS_OK )
152     {
153         // Something must have gone horribly wrong with the command
154         // line parsing or maybe the user just requested help ... :-)
155         current_options.usage();
156         FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
157         return false;
158     }
159
160     return true;
161 }
162
163
164 // find basic airport location info from airport database
165 bool fgFindAirportID( const string& id, FGAirport *a ) {
166     if ( id.length() ) {
167         FGPath path( current_options.get_fg_root() );
168         path.append( "Airports" );
169         path.append( "simple.mk4" );
170         FGAirports airports( path.c_str() );
171
172         FG_LOG( FG_GENERAL, FG_INFO, "Searching for airport code = " << id );
173
174         if ( ! airports.search( id, a ) ) {
175             FG_LOG( FG_GENERAL, FG_ALERT,
176                     "Failed to find " << id << " in " << path.str() );
177             return false;
178         }
179     } else {
180         return false;
181     }
182
183     FG_LOG( FG_GENERAL, FG_INFO,
184             "Position for " << id << " is ("
185             << a->longitude << ", "
186             << a->latitude << ")" );
187
188     return true;
189 }
190
191
192 // Set current_options lon/lat given an airport id
193 bool fgSetPosFromAirportID( const string& id ) {
194     FGAirport a;
195     double lon, lat;
196
197     FG_LOG( FG_GENERAL, FG_INFO,
198             "Attempting to set starting position from airport code " << id );
199
200     if ( fgFindAirportID( id, &a ) ) {
201         current_options.set_lon( a.longitude );
202         current_options.set_lat( a.latitude );
203         current_properties.setDoubleValue("/position/longitude",
204                                           a.longitude);
205         current_properties.setDoubleValue("/position/latitude",
206                                           a.latitude);
207
208         FG_LOG( FG_GENERAL, FG_INFO,
209                 "Position for " << id << " is ("
210                 << a.longitude << ", "
211                 << a.latitude << ")" );
212
213         return true;
214     } else {
215         return false;
216     }
217
218 }
219
220
221 // Set current_options lon/lat given an airport id and heading (degrees)
222 bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
223     FGRunway r;
224     FGRunway found_r;
225     double found_dir;
226
227     if ( id.length() ) {
228         // set initial position from runway and heading
229
230         FGPath path( current_options.get_fg_root() );
231         path.append( "Airports" );
232         path.append( "runways.mk4" );
233         FGRunways runways( path.c_str() );
234
235         FG_LOG( FG_GENERAL, FG_INFO,
236                 "Attempting to set starting position from runway code "
237                 << id << " heading " << tgt_hdg );
238
239         // FGPath inpath( current_options.get_fg_root() );
240         // inpath.append( "Airports" );
241         // inpath.append( "apt_simple" );
242         // airports.load( inpath.c_str() );
243
244         // FGPath outpath( current_options.get_fg_root() );
245         // outpath.append( "Airports" );
246         // outpath.append( "simple.gdbm" );
247         // airports.dump_gdbm( outpath.c_str() );
248
249         if ( ! runways.search( id, &r ) ) {
250             FG_LOG( FG_GENERAL, FG_ALERT,
251                     "Failed to find " << id << " in database." );
252             return false;
253         }
254
255         double diff;
256         double min_diff = 360.0;
257
258         while ( r.id == id ) {
259             // forward direction
260             diff = tgt_hdg - r.heading;
261             while ( diff < -180.0 ) { diff += 360.0; }
262             while ( diff >  180.0 ) { diff -= 360.0; }
263             diff = fabs(diff);
264             FG_LOG( FG_GENERAL, FG_INFO,
265                     "Runway " << r.rwy_no << " heading = " << r.heading <<
266                     " diff = " << diff );
267             if ( diff < min_diff ) {
268                 min_diff = diff;
269                 found_r = r;
270                 found_dir = 0;
271             }
272
273             // reverse direction
274             diff = tgt_hdg - r.heading - 180.0;
275             while ( diff < -180.0 ) { diff += 360.0; }
276             while ( diff >  180.0 ) { diff -= 360.0; }
277             diff = fabs(diff);
278             FG_LOG( FG_GENERAL, FG_INFO,
279                     "Runway -" << r.rwy_no << " heading = " <<
280                     r.heading + 180.0 <<
281                     " diff = " << diff );
282             if ( diff < min_diff ) {
283                 min_diff = diff;
284                 found_r = r;
285                 found_dir = 180.0;
286             }
287
288             runways.next( &r );
289         }
290
291         FG_LOG( FG_GENERAL, FG_INFO, "closest runway = " << found_r.rwy_no
292                 << " + " << found_dir );
293
294     } else {
295         return false;
296     }
297
298     double heading = found_r.heading + found_dir;
299     while ( heading >= 360.0 ) { heading -= 360.0; }
300
301     double lat2, lon2, az2;
302     double azimuth = found_r.heading + found_dir + 180.0;
303     while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
304
305     FG_LOG( FG_GENERAL, FG_INFO,
306             "runway =  " << found_r.lon << ", " << found_r.lat
307             << " length = " << found_r.length * FEET_TO_METER * 0.5 
308             << " heading = " << azimuth );
309     geo_direct_wgs_84 ( 0, found_r.lat, found_r.lon, 
310                         azimuth, found_r.length * FEET_TO_METER * 0.5 - 5.0,
311                         &lat2, &lon2, &az2 );
312     current_options.set_lon( lon2 );
313     current_options.set_lat( lat2 );
314     current_options.set_heading( heading );
315     current_properties.setDoubleValue("/position/longitude", lon2);
316     current_properties.setDoubleValue("/position/latitude", lat2);
317     current_properties.setDoubleValue("/orientation/heading", heading);
318
319     FG_LOG( FG_GENERAL, FG_INFO,
320             "Position for " << id << " is ("
321             << lon2 << ", "
322             << lat2 << ") new heading is "
323             << heading );
324
325     return true;
326 }
327
328
329 // Set initial position and orientation
330 bool fgInitPosition( void ) {
331     FGInterface *f = current_aircraft.fdm_state;
332     string id = current_options.get_airport_id();
333
334     // set initial position from default or command line coordinates
335     f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
336     f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
337
338     if ( scenery.cur_elev > current_options.get_altitude() - 1) {
339         current_options.set_altitude( scenery.cur_elev + 1 );
340     }
341
342     FG_LOG( FG_GENERAL, FG_INFO,
343             "starting altitude is = " << current_options.get_altitude() );
344
345     f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
346     fgFDMSetGroundElevation( current_options.get_flight_model(),
347                              f->get_Altitude() * FEET_TO_METER );
348
349 #if 0
350     current_properties.setDoubleValue("/position/longitude",
351                                       f->get_Longitude() * RAD_TO_DEG);
352     current_properties.setDoubleValue("/position/latitude",
353                                       f->get_Latitude() * RAD_TO_DEG);
354     current_properties.setDoubleValue("/position/altitude",
355                                       f->get_Altitude() * RAD_TO_DEG);
356 #endif
357
358     FG_LOG( FG_GENERAL, FG_INFO,
359             "Initial position is: ("
360             << (f->get_Longitude() * RAD_TO_DEG) << ", "
361             << (f->get_Latitude() * RAD_TO_DEG) << ", "
362             << (f->get_Altitude() * FEET_TO_METER) << ")" );
363
364     return true;
365 }
366
367
368 // General house keeping initializations
369 bool fgInitGeneral( void ) {
370     string root;
371
372 #if defined(FX) && defined(XMESA)
373     char *mesa_win_state;
374 #endif
375
376     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
377     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
378
379     root = current_options.get_fg_root();
380     if ( ! root.length() ) {
381         // No root path set? Then bail ...
382         FG_LOG( FG_GENERAL, FG_ALERT,
383                 "Cannot continue without environment variable FG_ROOT"
384                 << "being defined." );
385         exit(-1);
386     }
387     FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
388
389 #if defined(FX) && defined(XMESA)
390     // initialize full screen flag
391     global_fullscreen = false;
392     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
393         // Test for the MESA_GLX_FX env variable
394         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
395             // test if we are fullscreen mesa/glide
396             if ( (mesa_win_state[0] == 'f') ||
397                  (mesa_win_state[0] == 'F') ) {
398                 global_fullscreen = true;
399             }
400         }
401     }
402 #endif
403
404     return true;
405 }
406
407
408 // This is the top level init routine which calls all the other
409 // initialization routines.  If you are adding a subsystem to flight
410 // gear, its initialization call should located in this routine.
411 // Returns non-zero if a problem encountered.
412 bool fgInitSubsystems( void ) {
413     fgLIGHT *l = &cur_light_params;
414
415     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
416     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
417
418     // Initialize the material property lib
419     FGPath mpath( current_options.get_fg_root() );
420     mpath.append( "materials" );
421     if ( material_lib.load( mpath.str() ) ) {
422     } else {
423         FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
424         exit(-1);
425     }
426
427     // Initialize the Scenery Management subsystem
428     if ( fgSceneryInit() ) {
429         // Material lib initialized ok.
430     } else {
431         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
432         exit(-1);
433     }
434
435     if ( global_tile_mgr.init() ) {
436         // Load the local scenery data
437         global_tile_mgr.update( current_options.get_lon(),
438                                 current_options.get_lat() );
439     } else {
440         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
441         exit(-1);
442     }
443
444     FG_LOG( FG_GENERAL, FG_DEBUG,
445             "Current terrain elevation after tile mgr init " <<
446             scenery.cur_elev );
447
448     if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
449         cur_fdm_state = new FGLaRCsim;
450     } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
451         cur_fdm_state = new FGJSBsim;
452     } else if ( current_options.get_flight_model() == FGInterface::FG_ADA ) {
453         cur_fdm_state = new FGADA;
454     } else if ( current_options.get_flight_model() == 
455                 FGInterface::FG_BALLOONSIM ) {
456         cur_fdm_state = new FGBalloonSim;
457     } else if ( current_options.get_flight_model() == 
458                 FGInterface::FG_MAGICCARPET ) {
459         cur_fdm_state = new FGMagicCarpet;
460     } else if ( current_options.get_flight_model() == 
461                 FGInterface::FG_EXTERNAL ) {
462         cur_fdm_state = new FGExternal;
463     } else {
464         FG_LOG( FG_GENERAL, FG_ALERT,
465                 "No flight model, can't init aircraft" );
466         exit(-1);
467     }
468
469     // allocates structures so must happen before any of the flight
470     // model or control parameters are set
471     fgAircraftInit();   // In the future this might not be the case.
472
473     // set the initial position
474     fgInitPosition();
475
476     // Calculate ground elevation at starting point (we didn't have
477     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
478     //
479     // calculalate a cartesian point somewhere along the line between
480     // the center of the earth and our view position.  Doesn't have to
481     // be the exact elevation (this is good because we don't know it
482     // yet :-)
483
484     // now handled inside of the fgTileMgrUpdate()
485
486     /*
487     geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
488     tmp_abs_view_pos = sgGeodToCart(geod_pos);
489
490     FG_LOG( FG_GENERAL, FG_DEBUG,
491             "Initial abs_view_pos = " << tmp_abs_view_pos );
492     scenery.cur_elev =
493         fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
494                           tmp_abs_view_pos );
495     FG_LOG( FG_GENERAL, FG_DEBUG,
496             "Altitude after update " << scenery.cur_elev );
497     */
498
499     fgFDMSetGroundElevation( current_options.get_flight_model(),
500                              scenery.cur_elev );
501
502     // Reset our altitude if we are below ground
503     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
504     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
505             cur_fdm_state->get_Runway_altitude() );
506
507     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
508         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
509     }
510
511     FG_LOG( FG_GENERAL, FG_INFO,
512             "Updated position (after elevation adj): ("
513             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
514             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
515             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
516
517     // We need to calculate a few more values here that would normally
518     // be calculated by the FDM so that the current_view.UpdateViewMath()
519     // routine doesn't get hosed.
520
521     double sea_level_radius_meters;
522     double lat_geoc;
523     // Set the FG variables first
524     sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
525                   &sea_level_radius_meters, &lat_geoc);
526     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
527                                 cur_fdm_state->get_Altitude() +
528                                 (sea_level_radius_meters * METER_TO_FEET) );
529     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
530
531     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
532     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
533         
534     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
535     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
536
537     // The following section sets up the flight model EOM parameters
538     // and should really be read in from one or more files.
539
540     // Initial Velocity
541     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
542                              current_options.get_vBody(),
543                              current_options.get_wBody());
544
545     // Initial Orientation
546     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
547                          current_options.get_pitch() * DEG_TO_RAD,
548                          current_options.get_heading() * DEG_TO_RAD );
549
550     // Initial Angular Body rates
551     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
552
553     cur_fdm_state->set_Earth_position_angle( 0.0 );
554
555     // Mass properties and geometry values
556     cur_fdm_state->set_Inertias( 8.547270E+01,
557                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
558
559     // CG position w.r.t. ref. point
560     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
561
562     // Initialize the event manager
563     global_events.Init();
564
565     // Output event stats every 60 seconds
566     global_events.Register( "fgEVENT_MGR::PrintStats()",
567                             fgMethodCallback<fgEVENT_MGR>( &global_events,
568                                                    &fgEVENT_MGR::PrintStats),
569                             fgEVENT::FG_EVENT_READY, 60000 );
570
571     // Initialize view parameters
572     FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
573     globals->get_current_view()->Init();
574     globals->get_pilot_view()->Init();
575     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
576     globals->get_current_view()->UpdateViewMath(*cur_fdm_state);
577     globals->get_pilot_view()->UpdateViewMath(*cur_fdm_state);
578     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = "
579             << globals->get_current_view()->get_abs_view_pos());
580     // current_view.UpdateWorldToEye(f);
581
582     // Initialize the planetary subsystem
583     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
584     //                      fgEVENT::FG_EVENT_READY, 600000);
585
586     // Initialize the sun's position
587     // global_events.Register( "fgSunInit()", fgSunInit,
588     //                      fgEVENT::FG_EVENT_READY, 30000 );
589
590     // Intialize the moon's position
591     // global_events.Register( "fgMoonInit()", fgMoonInit,
592     //                      fgEVENT::FG_EVENT_READY, 600000 );
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, current_options.get_fg_root() );
626     // cout << theFGLocalWeatherDatabase << endl;
627     // cout << "visibility = " 
628     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
629
630     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
631      
632     // register the periodic update of the weather
633     global_events.Register( "weather update", fgUpdateWeatherDatabase,
634                             fgEVENT::FG_EVENT_READY, 30000);
635 #else
636     current_weather.Init();
637 #endif
638
639     // Initialize vor/ndb/ils/fix list management and query systems
640     FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
641
642     FG_LOG(FG_GENERAL, FG_INFO, "  VOR/NDB");
643     current_navlist = new FGNavList;
644     FGPath p_nav( current_options.get_fg_root() );
645     p_nav.append( "Navaids/default.nav" );
646     current_navlist->init( p_nav );
647
648     FG_LOG(FG_GENERAL, FG_INFO, "  ILS");
649     current_ilslist = new FGILSList;
650     FGPath p_ils( current_options.get_fg_root() );
651     p_ils.append( "Navaids/default.ils" );
652     current_ilslist->init( p_ils );
653
654     FG_LOG(FG_GENERAL, FG_INFO, "  Fixes");
655     current_fixlist = new FGFixList;
656     FGPath p_fix( current_options.get_fg_root() );
657     p_fix.append( "Navaids/default.fix" );
658     current_fixlist->init( p_fix );
659
660     // Initialize the underlying radio stack model
661     current_radiostack = new FGRadioStack;
662
663 //     current_radiostack->set_nav1_freq( 117.30 );
664 //     current_radiostack->set_nav1_alt_freq( 110.30 );
665 //     current_radiostack->set_nav1_sel_radial( 119.0 );
666
667 //     current_radiostack->set_nav2_freq( 111.80 );
668 //     current_radiostack->set_nav2_alt_freq( 115.70 );
669 //     current_radiostack->set_nav2_sel_radial( 029.0 );
670
671 //     current_radiostack->set_adf_freq( 266.0 );
672
673 #if 0
674     // This block of settings are Alex's defaults for San Diego
675     current_radiostack->set_nav1_freq( 111.70 );
676     current_radiostack->set_nav1_alt_freq( 115.30 );
677     current_radiostack->set_nav1_sel_radial( 280.0 );
678     current_radiostack->set_nav2_freq( 117.80 );
679     current_radiostack->set_nav2_alt_freq( 114.00 );
680     current_radiostack->set_nav2_sel_radial( 68.0 );
681     current_radiostack->set_adf_freq( 210.0 );
682     // End of Alex's custom settings
683 #endif
684
685     current_radiostack->search( cur_fdm_state->get_Longitude(),
686                                 cur_fdm_state->get_Latitude(),
687                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
688
689     current_radiostack->update( cur_fdm_state->get_Longitude(),
690                                 cur_fdm_state->get_Latitude(),
691                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
692
693     // Search radio database once per second
694     global_events.Register( "fgRadioSearch()", fgRadioSearch,
695                             fgEVENT::FG_EVENT_READY, 1000);
696
697
698     // Initialize the Cockpit subsystem
699     if( fgCockpitInit( &current_aircraft )) {
700         // Cockpit initialized ok.
701     } else {
702         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
703         exit(-1);
704     }
705
706     // Initialize the flight model subsystem data structures base on
707     // above values
708
709     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
710     //            1.0 / current_options.get_model_hz() );
711     if ( cur_fdm_state->init( 1.0 / current_options.get_model_hz() ) ) {
712         // fdm init successful
713     } else {
714         FG_LOG( FG_GENERAL, FG_ALERT, "FDM init() failed!  Cannot continue." );
715         exit(-1);
716     }
717
718     // I'm just sticking this here for now, it should probably move
719     // eventually
720     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
721
722     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
723         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
724     }
725
726     FG_LOG( FG_GENERAL, FG_INFO,
727             "Updated position (after elevation adj): ("
728             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
729             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
730             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
731     // end of thing that I just stuck in that I should probably move
732
733     // Joystick support
734     if ( fgJoystickInit() ) {
735         // Joystick initialized ok.
736     } else {
737         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
738     }
739
740     // Autopilot init
741     current_autopilot = new FGAutopilot;
742     current_autopilot->init();
743
744     // initialize the gui parts of the autopilot
745     NewTgtAirportInit();
746     fgAPAdjustInit() ;
747     NewHeadingInit();
748     NewAltitudeInit();
749
750     // Initialize I/O channels
751 #if ! defined( macintosh )
752     fgIOInit();
753 #endif
754
755     // Initialize the 2D panel.
756     string panel_path =
757         current_properties.getStringValue("/sim/panel/path",
758                                           "Panels/Default/default.xml");
759     current_panel = fgReadPanel(panel_path);
760     if (current_panel == 0) {
761         FG_LOG(FG_INPUT, FG_ALERT,
762                "Error reading new panel from " << panel_path);
763     }
764     FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
765
766     // Initialize the BFI
767     FGBFI::init();
768
769     FG_LOG( FG_GENERAL, FG_INFO, endl);
770
771     return true;
772 }
773
774
775 void fgReInitSubsystems( void )
776 {
777     bool freeze = globals->get_freeze();
778     if( !freeze )
779         globals->set_freeze( true );
780     
781     if( global_tile_mgr.init() ) {
782         // Load the local scenery data
783         global_tile_mgr.update( current_options.get_lon(),
784                                 current_options.get_lat() );
785     } else {
786         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
787                 exit(-1);
788     }
789
790     // cout << "current scenery elev = " << scenery.cur_elev << endl;
791
792     fgInitPosition();
793     fgFDMSetGroundElevation( current_options.get_flight_model(), 
794                              scenery.cur_elev );
795
796     // Reset our altitude if we are below ground
797     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
798     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " << 
799             cur_fdm_state->get_Runway_altitude() );
800
801     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
802         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
803     }
804     double sea_level_radius_meters;
805     double lat_geoc;
806     // Set the FG variables first
807     sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
808                   &sea_level_radius_meters, &lat_geoc);
809     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(), 
810                                 cur_fdm_state->get_Altitude() + 
811                                 (sea_level_radius_meters * METER_TO_FEET) );
812     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
813         
814     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
815     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
816         
817     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
818     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
819
820     // The following section sets up the flight model EOM parameters
821     // and should really be read in from one or more files.
822
823     // Initial Velocity
824     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
825                              current_options.get_vBody(),
826                              current_options.get_wBody());
827
828     // Initial Orientation
829     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
830                          current_options.get_pitch() * DEG_TO_RAD,
831                          current_options.get_heading() * DEG_TO_RAD );
832
833     // Initial Angular Body rates
834     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
835
836     cur_fdm_state->set_Earth_position_angle( 0.0 );
837
838     // Mass properties and geometry values
839     cur_fdm_state->set_Inertias( 8.547270E+01, 
840                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
841
842     // CG position w.r.t. ref. point
843     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
844
845     // Initialize view parameters
846     globals->get_current_view()->set_view_offset( 0.0 );
847     globals->get_current_view()->set_goal_view_offset( 0.0 );
848     globals->get_pilot_view()->set_view_offset( 0.0 );
849     globals->get_pilot_view()->set_goal_view_offset( 0.0 );
850
851     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
852     globals->get_current_view()->UpdateViewMath(*cur_fdm_state);
853     globals->get_pilot_view()->UpdateViewMath(*cur_fdm_state);
854     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = "
855             << globals->get_current_view()->get_abs_view_pos());
856
857     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state, 
858     //            1.0 / current_options.get_model_hz() );
859     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
860
861     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
862
863     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
864         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
865     }
866
867     controls.reset_all();
868     current_autopilot->reset();
869
870     if( !freeze )
871         globals->set_freeze( false );
872 }