]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
2d2fa302f3ed4ef3faf0c9453d893f4d226940a0
[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         current_properties.setDoubleValue("/position/longitude",
203                                           a.longitude);
204         current_properties.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;
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     current_properties.setDoubleValue("/position/longitude", lon2);
315     current_properties.setDoubleValue("/position/latitude", lat2);
316     current_properties.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     current_properties.setDoubleValue("/position/longitude",
350                                       f->get_Longitude() * RAD_TO_DEG);
351     current_properties.setDoubleValue("/position/latitude",
352                                       f->get_Latitude() * RAD_TO_DEG);
353     current_properties.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 // This is the top level init routine which calls all the other
408 // initialization routines.  If you are adding a subsystem to flight
409 // gear, its initialization call should located in this routine.
410 // Returns non-zero if a problem encountered.
411 bool fgInitSubsystems( void ) {
412     fgLIGHT *l = &cur_light_params;
413
414     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
415     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
416
417     // Initialize the material property lib
418     FGPath mpath( globals->get_options()->get_fg_root() );
419     mpath.append( "materials" );
420     if ( material_lib.load( mpath.str() ) ) {
421     } else {
422         FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
423         exit(-1);
424     }
425
426     // Initialize the Scenery Management subsystem
427     if ( fgSceneryInit() ) {
428         // Material lib initialized ok.
429     } else {
430         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
431         exit(-1);
432     }
433
434     if ( global_tile_mgr.init() ) {
435         // Load the local scenery data
436         global_tile_mgr.update( globals->get_options()->get_lon(),
437                                 globals->get_options()->get_lat() );
438     } else {
439         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
440         exit(-1);
441     }
442
443     FG_LOG( FG_GENERAL, FG_DEBUG,
444             "Current terrain elevation after tile mgr init " <<
445             scenery.cur_elev );
446
447     if ( globals->get_options()->get_flight_model() == FGInterface::FG_LARCSIM ) {
448         cur_fdm_state = new FGLaRCsim;
449     } else if ( globals->get_options()->get_flight_model() == FGInterface::FG_JSBSIM ) {
450         cur_fdm_state = new FGJSBsim;
451     } else if ( globals->get_options()->get_flight_model() == FGInterface::FG_ADA ) {
452         cur_fdm_state = new FGADA;
453     } else if ( globals->get_options()->get_flight_model() == 
454                 FGInterface::FG_BALLOONSIM ) {
455         cur_fdm_state = new FGBalloonSim;
456     } else if ( globals->get_options()->get_flight_model() == 
457                 FGInterface::FG_MAGICCARPET ) {
458         cur_fdm_state = new FGMagicCarpet;
459     } else if ( globals->get_options()->get_flight_model() == 
460                 FGInterface::FG_EXTERNAL ) {
461         cur_fdm_state = new FGExternal;
462     } else {
463         FG_LOG( FG_GENERAL, FG_ALERT,
464                 "No flight model, can't init aircraft" );
465         exit(-1);
466     }
467
468     // allocates structures so must happen before any of the flight
469     // model or control parameters are set
470     fgAircraftInit();   // In the future this might not be the case.
471
472     // set the initial position
473     fgInitPosition();
474
475     // Calculate ground elevation at starting point (we didn't have
476     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
477     //
478     // calculalate a cartesian point somewhere along the line between
479     // the center of the earth and our view position.  Doesn't have to
480     // be the exact elevation (this is good because we don't know it
481     // yet :-)
482
483     // now handled inside of the fgTileMgrUpdate()
484
485     /*
486     geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
487     tmp_abs_view_pos = sgGeodToCart(geod_pos);
488
489     FG_LOG( FG_GENERAL, FG_DEBUG,
490             "Initial abs_view_pos = " << tmp_abs_view_pos );
491     scenery.cur_elev =
492         fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
493                           tmp_abs_view_pos );
494     FG_LOG( FG_GENERAL, FG_DEBUG,
495             "Altitude after update " << scenery.cur_elev );
496     */
497
498     fgFDMSetGroundElevation( globals->get_options()->get_flight_model(),
499                              scenery.cur_elev );
500
501     // Reset our altitude if we are below ground
502     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
503     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
504             cur_fdm_state->get_Runway_altitude() );
505
506     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
507         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
508     }
509
510     FG_LOG( FG_GENERAL, FG_INFO,
511             "Updated position (after elevation adj): ("
512             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
513             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
514             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
515
516     // We need to calculate a few more values here that would normally
517     // be calculated by the FDM so that the current_view.UpdateViewMath()
518     // routine doesn't get hosed.
519
520     double sea_level_radius_meters;
521     double lat_geoc;
522     // Set the FG variables first
523     sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
524                   &sea_level_radius_meters, &lat_geoc);
525     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
526                                 cur_fdm_state->get_Altitude() +
527                                 (sea_level_radius_meters * METER_TO_FEET) );
528     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
529
530     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
531     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
532         
533     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
534     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
535
536     // The following section sets up the flight model EOM parameters
537     // and should really be read in from one or more files.
538
539     // Initial Velocity
540     cur_fdm_state->set_Velocities_Local( globals->get_options()->get_uBody(),
541                              globals->get_options()->get_vBody(),
542                              globals->get_options()->get_wBody());
543
544     // Initial Orientation
545     cur_fdm_state->set_Euler_Angles( globals->get_options()->get_roll() * DEG_TO_RAD,
546                          globals->get_options()->get_pitch() * DEG_TO_RAD,
547                          globals->get_options()->get_heading() * DEG_TO_RAD );
548
549     // Initial Angular Body rates
550     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
551
552     cur_fdm_state->set_Earth_position_angle( 0.0 );
553
554     // Mass properties and geometry values
555     cur_fdm_state->set_Inertias( 8.547270E+01,
556                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
557
558     // CG position w.r.t. ref. point
559     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
560
561     // Initialize the event manager
562     global_events.Init();
563
564     // Output event stats every 60 seconds
565     global_events.Register( "fgEVENT_MGR::PrintStats()",
566                             fgMethodCallback<fgEVENT_MGR>( &global_events,
567                                                    &fgEVENT_MGR::PrintStats),
568                             fgEVENT::FG_EVENT_READY, 60000 );
569
570     // Initialize view parameters
571     FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
572     globals->get_current_view()->Init();
573     globals->get_pilot_view()->Init();
574     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
575     globals->get_current_view()->UpdateViewMath(*cur_fdm_state);
576     globals->get_pilot_view()->UpdateViewMath(*cur_fdm_state);
577     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = "
578             << globals->get_current_view()->get_abs_view_pos());
579     // current_view.UpdateWorldToEye(f);
580
581     // Initialize the planetary subsystem
582     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
583     //                      fgEVENT::FG_EVENT_READY, 600000);
584
585     // Initialize the sun's position
586     // global_events.Register( "fgSunInit()", fgSunInit,
587     //                      fgEVENT::FG_EVENT_READY, 30000 );
588
589     // Intialize the moon's position
590     // global_events.Register( "fgMoonInit()", fgMoonInit,
591     //                      fgEVENT::FG_EVENT_READY, 600000 );
592
593     // fgUpdateSunPos() needs a few position and view parameters set
594     // so it can calculate local relative sun angle and a few other
595     // things for correctly orienting the sky.
596     fgUpdateSunPos();
597     fgUpdateMoonPos();
598     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
599                             fgEVENT::FG_EVENT_READY, 60000);
600     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
601                             fgEVENT::FG_EVENT_READY, 60000);
602
603     // Initialize Lighting interpolation tables
604     l->Init();
605
606     // update the lighting parameters (based on sun angle)
607     global_events.Register( "fgLight::Update()",
608                             fgMethodCallback<fgLIGHT>( &cur_light_params,
609                                                        &fgLIGHT::Update),
610                             fgEVENT::FG_EVENT_READY, 30000 );
611     // update the current timezone each 30 minutes
612     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
613                             fgEVENT::FG_EVENT_READY, 1800000);
614
615     // Initialize the weather modeling subsystem
616 #ifndef FG_OLD_WEATHER
617     // Initialize the WeatherDatabase
618     FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
619     sgVec3 position;
620     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
621                current_aircraft.fdm_state->get_Longitude(),
622                current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
623     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
624         new FGLocalWeatherDatabase( position, globals->get_options()->get_fg_root() );
625     // cout << theFGLocalWeatherDatabase << endl;
626     // cout << "visibility = " 
627     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
628
629     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
630      
631     // register the periodic update of the weather
632     global_events.Register( "weather update", fgUpdateWeatherDatabase,
633                             fgEVENT::FG_EVENT_READY, 30000);
634 #else
635     current_weather.Init();
636 #endif
637
638     // Initialize vor/ndb/ils/fix list management and query systems
639     FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
640
641     FG_LOG(FG_GENERAL, FG_INFO, "  VOR/NDB");
642     current_navlist = new FGNavList;
643     FGPath p_nav( globals->get_options()->get_fg_root() );
644     p_nav.append( "Navaids/default.nav" );
645     current_navlist->init( p_nav );
646
647     FG_LOG(FG_GENERAL, FG_INFO, "  ILS");
648     current_ilslist = new FGILSList;
649     FGPath p_ils( globals->get_options()->get_fg_root() );
650     p_ils.append( "Navaids/default.ils" );
651     current_ilslist->init( p_ils );
652
653     FG_LOG(FG_GENERAL, FG_INFO, "  Fixes");
654     current_fixlist = new FGFixList;
655     FGPath p_fix( globals->get_options()->get_fg_root() );
656     p_fix.append( "Navaids/default.fix" );
657     current_fixlist->init( p_fix );
658
659     // Initialize the underlying radio stack model
660     current_radiostack = new FGRadioStack;
661
662 //     current_radiostack->set_nav1_freq( 117.30 );
663 //     current_radiostack->set_nav1_alt_freq( 110.30 );
664 //     current_radiostack->set_nav1_sel_radial( 119.0 );
665
666 //     current_radiostack->set_nav2_freq( 111.80 );
667 //     current_radiostack->set_nav2_alt_freq( 115.70 );
668 //     current_radiostack->set_nav2_sel_radial( 029.0 );
669
670 //     current_radiostack->set_adf_freq( 266.0 );
671
672 #if 0
673     // This block of settings are Alex's defaults for San Diego
674     current_radiostack->set_nav1_freq( 111.70 );
675     current_radiostack->set_nav1_alt_freq( 115.30 );
676     current_radiostack->set_nav1_sel_radial( 280.0 );
677     current_radiostack->set_nav2_freq( 117.80 );
678     current_radiostack->set_nav2_alt_freq( 114.00 );
679     current_radiostack->set_nav2_sel_radial( 68.0 );
680     current_radiostack->set_adf_freq( 210.0 );
681     // End of Alex's custom settings
682 #endif
683
684     current_radiostack->search( cur_fdm_state->get_Longitude(),
685                                 cur_fdm_state->get_Latitude(),
686                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
687
688     current_radiostack->update( cur_fdm_state->get_Longitude(),
689                                 cur_fdm_state->get_Latitude(),
690                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
691
692     // Search radio database once per second
693     global_events.Register( "fgRadioSearch()", fgRadioSearch,
694                             fgEVENT::FG_EVENT_READY, 1000);
695
696
697     // Initialize the Cockpit subsystem
698     if( fgCockpitInit( &current_aircraft )) {
699         // Cockpit initialized ok.
700     } else {
701         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
702         exit(-1);
703     }
704
705     // Initialize the flight model subsystem data structures base on
706     // above values
707
708     // fgFDMInit( globals->get_options()->get_flight_model(), cur_fdm_state,
709     //            1.0 / globals->get_options()->get_model_hz() );
710     if ( cur_fdm_state->init( 1.0 / globals->get_options()->get_model_hz() ) ) {
711         // fdm init successful
712     } else {
713         FG_LOG( FG_GENERAL, FG_ALERT, "FDM init() failed!  Cannot continue." );
714         exit(-1);
715     }
716
717     // I'm just sticking this here for now, it should probably move
718     // eventually
719     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
720
721     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
722         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
723     }
724
725     FG_LOG( FG_GENERAL, FG_INFO,
726             "Updated position (after elevation adj): ("
727             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
728             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
729             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
730     // end of thing that I just stuck in that I should probably move
731
732     // Joystick support
733     if ( fgJoystickInit() ) {
734         // Joystick initialized ok.
735     } else {
736         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
737     }
738
739     // Autopilot init
740     current_autopilot = new FGAutopilot;
741     current_autopilot->init();
742
743     // initialize the gui parts of the autopilot
744     NewTgtAirportInit();
745     fgAPAdjustInit() ;
746     NewHeadingInit();
747     NewAltitudeInit();
748
749     // Initialize I/O channels
750 #if ! defined( macintosh )
751     fgIOInit();
752 #endif
753
754     // Initialize the 2D panel.
755     string panel_path =
756         current_properties.getStringValue("/sim/panel/path",
757                                           "Panels/Default/default.xml");
758     current_panel = fgReadPanel(panel_path);
759     if (current_panel == 0) {
760         FG_LOG(FG_INPUT, FG_ALERT,
761                "Error reading new panel from " << panel_path);
762     }
763     FG_LOG(FG_INPUT, FG_INFO, "Loaded new panel from " << panel_path);
764
765     // Initialize the BFI
766     FGBFI::init();
767
768     FG_LOG( FG_GENERAL, FG_INFO, endl);
769
770     return true;
771 }
772
773
774 void fgReInitSubsystems( void )
775 {
776     bool freeze = globals->get_freeze();
777     if( !freeze )
778         globals->set_freeze( true );
779     
780     if( global_tile_mgr.init() ) {
781         // Load the local scenery data
782         global_tile_mgr.update( globals->get_options()->get_lon(),
783                                 globals->get_options()->get_lat() );
784     } else {
785         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
786                 exit(-1);
787     }
788
789     // cout << "current scenery elev = " << scenery.cur_elev << endl;
790
791     fgInitPosition();
792     fgFDMSetGroundElevation( globals->get_options()->get_flight_model(), 
793                              scenery.cur_elev );
794
795     // Reset our altitude if we are below ground
796     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
797     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " << 
798             cur_fdm_state->get_Runway_altitude() );
799
800     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
801         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
802     }
803     double sea_level_radius_meters;
804     double lat_geoc;
805     // Set the FG variables first
806     sgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
807                   &sea_level_radius_meters, &lat_geoc);
808     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(), 
809                                 cur_fdm_state->get_Altitude() + 
810                                 (sea_level_radius_meters * METER_TO_FEET) );
811     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
812         
813     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
814     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
815         
816     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
817     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
818
819     // The following section sets up the flight model EOM parameters
820     // and should really be read in from one or more files.
821
822     // Initial Velocity
823     cur_fdm_state->set_Velocities_Local( globals->get_options()->get_uBody(),
824                              globals->get_options()->get_vBody(),
825                              globals->get_options()->get_wBody());
826
827     // Initial Orientation
828     cur_fdm_state->set_Euler_Angles( globals->get_options()->get_roll() * DEG_TO_RAD,
829                          globals->get_options()->get_pitch() * DEG_TO_RAD,
830                          globals->get_options()->get_heading() * DEG_TO_RAD );
831
832     // Initial Angular Body rates
833     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
834
835     cur_fdm_state->set_Earth_position_angle( 0.0 );
836
837     // Mass properties and geometry values
838     cur_fdm_state->set_Inertias( 8.547270E+01, 
839                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
840
841     // CG position w.r.t. ref. point
842     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
843
844     // Initialize view parameters
845     globals->get_current_view()->set_view_offset( 0.0 );
846     globals->get_current_view()->set_goal_view_offset( 0.0 );
847     globals->get_pilot_view()->set_view_offset( 0.0 );
848     globals->get_pilot_view()->set_goal_view_offset( 0.0 );
849
850     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
851     globals->get_current_view()->UpdateViewMath(*cur_fdm_state);
852     globals->get_pilot_view()->UpdateViewMath(*cur_fdm_state);
853     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = "
854             << globals->get_current_view()->get_abs_view_pos());
855
856     // fgFDMInit( globals->get_options()->get_flight_model(), cur_fdm_state, 
857     //            1.0 / globals->get_options()->get_model_hz() );
858     cur_fdm_state->init( 1.0 / globals->get_options()->get_model_hz() );
859
860     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
861
862     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
863         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
864     }
865
866     controls.reset_all();
867     current_autopilot->reset();
868
869     if( !freeze )
870         globals->set_freeze( false );
871 }