]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
David Megginson's changes:
[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/fg_geodesy.hxx>
53 #include <simgear/math/point3d.hxx>
54 #include <simgear/math/polar3d.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/timing/sg_time.hxx>
57
58 #include <Aircraft/aircraft.hxx>
59 #include <Airports/simple.hxx>
60 #include <Autopilot/auto_gui.hxx>
61 #include <Autopilot/newauto.hxx>
62 #include <Cockpit/cockpit.hxx>
63 #include <Cockpit/radiostack.hxx>
64 #include <Cockpit/panel.hxx>
65 #include <Cockpit/sp_panel.hxx>
66 #include <FDM/Balloon.h>
67 #include <FDM/External.hxx>
68 #include <FDM/JSBsim.hxx>
69 #include <FDM/LaRCsim.hxx>
70 #include <FDM/MagicCarpet.hxx>
71 #include <Include/general.hxx>
72 #include <Joystick/joystick.hxx>
73 #include <Objects/matlib.hxx>
74 #include <Navaids/fixlist.hxx>
75 #include <Navaids/ilslist.hxx>
76 #include <Navaids/navlist.hxx>
77 #include <Scenery/scenery.hxx>
78 #include <Scenery/tilemgr.hxx>
79 #include <Time/event.hxx>
80 #include <Time/light.hxx>
81 #include <Time/sunpos.hxx>
82 #include <Time/moonpos.hxx>
83 #include <Time/tmp.hxx>
84
85 #ifndef FG_OLD_WEATHER
86 #  include <WeatherCM/FGLocalWeatherDatabase.h>
87 #else
88 #  include <Weather/weather.hxx>
89 #endif
90
91 #include "fg_init.hxx"
92 #include "fg_io.hxx"
93 #include "globals.hxx"
94 #include "options.hxx"
95 #include "views.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)
108 bool fgInitConfig ( int argc, char **argv ) {
109     // Attempt to locate and parse a config file
110     // First check fg_root
111     FGPath config( current_options.get_fg_root() );
112     config.append( "system.fgfsrc" );
113     current_options.parse_config_file( 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         current_options.parse_config_file( config.str() );
121     }
122
123     // Parse remaining command line options
124     // These will override anything specified in a config file
125     if ( current_options.parse_command_line(argc, argv) !=
126          fgOPTIONS::FG_OPTIONS_OK )
127     {
128         // Something must have gone horribly wrong with the command
129         // line parsing or maybe the user just requested help ... :-)
130         current_options.usage();
131         FG_LOG( FG_GENERAL, FG_ALERT, "\nExiting ...");
132         return false;
133     }
134
135    return true;
136 }
137
138
139 // Set current_options lon/lat given an airport id
140 bool fgSetPosFromAirportID( const string& id ) {
141     FGAirport a;
142     double lon, lat;
143
144     if ( id.length() ) {
145         // set initial position from airport id
146
147         FGPath path( current_options.get_fg_root() );
148         path.append( "Airports" );
149         path.append( "simple.mk4" );
150         FGAirports airports( path.c_str() );
151
152         FG_LOG( FG_GENERAL, FG_INFO,
153                 "Attempting to set starting position from airport code "
154                 << id );
155
156         // FGPath inpath( current_options.get_fg_root() );
157         // inpath.append( "Airports" );
158         // inpath.append( "apt_simple" );
159         // airports.load( inpath.c_str() );
160
161         // FGPath outpath( current_options.get_fg_root() );
162         // outpath.append( "Airports" );
163         // outpath.append( "simple.gdbm" );
164         // airports.dump_gdbm( outpath.c_str() );
165
166         if ( ! airports.search( id, &a ) ) {
167             FG_LOG( FG_GENERAL, FG_ALERT,
168                     "Failed to find " << id << " in database." );
169             return false;
170         } else {
171             current_options.set_lon( a.longitude );
172             current_options.set_lat( a.latitude );
173         }
174     } else {
175         return false;
176     }
177
178     FG_LOG( FG_GENERAL, FG_INFO,
179             "Position for " << id << " is ("
180             << a.longitude << ", "
181             << a.latitude << ")" );
182
183     return true;
184 }
185
186 // Set initial position and orientation
187 bool fgInitPosition( void ) {
188     FGInterface *f = current_aircraft.fdm_state;
189     string id = current_options.get_airport_id();
190
191     if ( id.length() ) {
192         // set initial position from airport id
193         if ( ! fgSetPosFromAirportID( id ) ) {
194             exit(-1);
195         }
196     }
197
198     // set initial position from default or command line coordinates
199     f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
200     f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
201
202     FG_LOG( FG_GENERAL, FG_INFO,
203             "starting altitude is = " << current_options.get_altitude() );
204
205     f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
206     fgFDMSetGroundElevation( current_options.get_flight_model(),
207                              (f->get_Altitude() - 3.758099) * FEET_TO_METER );
208
209     FG_LOG( FG_GENERAL, FG_INFO,
210             "Initial position is: ("
211             << (f->get_Longitude() * RAD_TO_DEG) << ", "
212             << (f->get_Latitude() * RAD_TO_DEG) << ", "
213             << (f->get_Altitude() * FEET_TO_METER) << ")" );
214
215     return true;
216 }
217
218
219 // General house keeping initializations
220 bool fgInitGeneral( void ) {
221     string root;
222
223 #if defined(FX) && defined(XMESA)
224     char *mesa_win_state;
225 #endif
226
227     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
228     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
229
230     root = current_options.get_fg_root();
231     if ( ! root.length() ) {
232         // No root path set? Then bail ...
233         FG_LOG( FG_GENERAL, FG_ALERT,
234                 "Cannot continue without environment variable FG_ROOT"
235                 << "being defined." );
236         exit(-1);
237     }
238     FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
239
240 #if defined(FX) && defined(XMESA)
241     // initialize full screen flag
242     global_fullscreen = false;
243     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
244         // Test for the MESA_GLX_FX env variable
245         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
246             // test if we are fullscreen mesa/glide
247             if ( (mesa_win_state[0] == 'f') ||
248                  (mesa_win_state[0] == 'F') ) {
249                 global_fullscreen = true;
250             }
251         }
252     }
253 #endif
254
255     return true;
256 }
257
258
259 // This is the top level init routine which calls all the other
260 // initialization routines.  If you are adding a subsystem to flight
261 // gear, its initialization call should located in this routine.
262 // Returns non-zero if a problem encountered.
263 bool fgInitSubsystems( void ) {
264     fgLIGHT *l = &cur_light_params;
265
266     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
267     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
268
269     if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
270         cur_fdm_state = new FGLaRCsim;
271     } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
272         cur_fdm_state = new FGJSBsim;
273     } else if ( current_options.get_flight_model() == 
274                 FGInterface::FG_BALLOONSIM ) {
275         cur_fdm_state = new FGBalloonSim;
276     } else if ( current_options.get_flight_model() == 
277                 FGInterface::FG_MAGICCARPET ) {
278         cur_fdm_state = new FGMagicCarpet;
279     } else if ( current_options.get_flight_model() == 
280                 FGInterface::FG_EXTERNAL ) {
281         cur_fdm_state = new FGExternal;
282     } else {
283         FG_LOG( FG_GENERAL, FG_ALERT,
284                 "No flight model, can't init aircraft" );
285         exit(-1);
286     }
287
288     // allocates structures so must happen before any of the flight
289     // model or control parameters are set
290     fgAircraftInit();   // In the future this might not be the case.
291
292     // set the initial position
293     fgInitPosition();
294
295     // Initialize the material property lib
296     FGPath mpath( current_options.get_fg_root() );
297     mpath.append( "materials" );
298     if ( material_lib.load( mpath.str() ) ) {
299     } else {
300         FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
301         exit(-1);
302     }
303
304     // Initialize the Scenery Management subsystem
305     if ( fgSceneryInit() ) {
306         // Material lib initialized ok.
307     } else {
308         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
309         exit(-1);
310     }
311
312     if( global_tile_mgr.init() ) {
313         // Load the local scenery data
314         global_tile_mgr.update( cur_fdm_state->get_Longitude(),
315                                 cur_fdm_state->get_Latitude() );
316     } else {
317         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
318         exit(-1);
319     }
320
321     FG_LOG( FG_GENERAL, FG_DEBUG,
322             "Current terrain elevation after tile mgr init " <<
323             scenery.cur_elev );
324
325     // Calculate ground elevation at starting point (we didn't have
326     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
327     //
328     // calculalate a cartesian point somewhere along the line between
329     // the center of the earth and our view position.  Doesn't have to
330     // be the exact elevation (this is good because we don't know it
331     // yet :-)
332
333     // now handled inside of the fgTileMgrUpdate()
334
335     /*
336     geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
337     tmp_abs_view_pos = fgGeodToCart(geod_pos);
338
339     FG_LOG( FG_GENERAL, FG_DEBUG,
340             "Initial abs_view_pos = " << tmp_abs_view_pos );
341     scenery.cur_elev =
342         fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
343                           tmp_abs_view_pos );
344     FG_LOG( FG_GENERAL, FG_DEBUG,
345             "Altitude after update " << scenery.cur_elev );
346     */
347
348     fgFDMSetGroundElevation( current_options.get_flight_model(),
349                              scenery.cur_elev );
350
351     // Reset our altitude if we are below ground
352     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
353     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
354             cur_fdm_state->get_Runway_altitude() );
355
356     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
357         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
358     }
359
360     FG_LOG( FG_GENERAL, FG_INFO,
361             "Updated position (after elevation adj): ("
362             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
363             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
364             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
365
366     // We need to calculate a few more values here that would normally
367     // be calculated by the FDM so that the current_view.UpdateViewMath()
368     // routine doesn't get hosed.
369
370     double sea_level_radius_meters;
371     double lat_geoc;
372     // Set the FG variables first
373     fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
374                   &sea_level_radius_meters, &lat_geoc);
375     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
376                                 cur_fdm_state->get_Altitude() +
377                                 (sea_level_radius_meters * METER_TO_FEET) );
378     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
379
380     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
381     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
382         
383     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
384     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
385
386     // The following section sets up the flight model EOM parameters
387     // and should really be read in from one or more files.
388
389     // Initial Velocity
390     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
391                              current_options.get_vBody(),
392                              current_options.get_wBody());
393
394     // Initial Orientation
395     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
396                          current_options.get_pitch() * DEG_TO_RAD,
397                          current_options.get_heading() * DEG_TO_RAD );
398
399     // Initial Angular Body rates
400     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
401
402     cur_fdm_state->set_Earth_position_angle( 0.0 );
403
404     // Mass properties and geometry values
405     cur_fdm_state->set_Inertias( 8.547270E+01,
406                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
407
408     // CG position w.r.t. ref. point
409     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
410
411     // Initialize the event manager
412     global_events.Init();
413
414     // Output event stats every 60 seconds
415     global_events.Register( "fgEVENT_MGR::PrintStats()",
416                             fgMethodCallback<fgEVENT_MGR>( &global_events,
417                                                    &fgEVENT_MGR::PrintStats),
418                             fgEVENT::FG_EVENT_READY, 60000 );
419
420     // Initialize view parameters
421     FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
422     current_view.Init();
423     pilot_view.Init();
424     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
425     current_view.UpdateViewMath(*cur_fdm_state);
426     pilot_view.UpdateViewMath(*cur_fdm_state);
427     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
428     // current_view.UpdateWorldToEye(f);
429
430     // Initialize the planetary subsystem
431     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
432     //                      fgEVENT::FG_EVENT_READY, 600000);
433
434     // Initialize the sun's position
435     // global_events.Register( "fgSunInit()", fgSunInit,
436     //                      fgEVENT::FG_EVENT_READY, 30000 );
437
438     // Intialize the moon's position
439     // global_events.Register( "fgMoonInit()", fgMoonInit,
440     //                      fgEVENT::FG_EVENT_READY, 600000 );
441
442     // fgUpdateSunPos() needs a few position and view parameters set
443     // so it can calculate local relative sun angle and a few other
444     // things for correctly orienting the sky.
445     fgUpdateSunPos();
446     fgUpdateMoonPos();
447     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
448                             fgEVENT::FG_EVENT_READY, 60000);
449     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
450                             fgEVENT::FG_EVENT_READY, 60000);
451
452     // Initialize Lighting interpolation tables
453     l->Init();
454
455     // update the lighting parameters (based on sun angle)
456     global_events.Register( "fgLight::Update()",
457                             fgMethodCallback<fgLIGHT>( &cur_light_params,
458                                                        &fgLIGHT::Update),
459                             fgEVENT::FG_EVENT_READY, 30000 );
460     // update the current timezone each 30 minutes
461     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
462                             fgEVENT::FG_EVENT_READY, 1800000);
463
464     // Initialize the weather modeling subsystem
465 #ifndef FG_OLD_WEATHER
466     // Initialize the WeatherDatabase
467     FG_LOG(FG_GENERAL, FG_INFO, "Creating LocalWeatherDatabase");
468     sgVec3 position;
469     sgSetVec3( position, current_aircraft.fdm_state->get_Latitude(),
470                current_aircraft.fdm_state->get_Longitude(),
471                current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
472     FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 
473         new FGLocalWeatherDatabase( position, current_options.get_fg_root() );
474     // cout << theFGLocalWeatherDatabase << endl;
475     // cout << "visibility = " 
476     //      << theFGLocalWeatherDatabase->getWeatherVisibility() << endl;
477
478     WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
479      
480     // register the periodic update of the weather
481     global_events.Register( "weather update", fgUpdateWeatherDatabase,
482                             fgEVENT::FG_EVENT_READY, 30000);
483 #else
484     current_weather.Init();
485 #endif
486
487     // Initialize vor/ndb/ils/fix list management and query systems
488     FG_LOG(FG_GENERAL, FG_INFO, "Loading Navaids");
489
490     FG_LOG(FG_GENERAL, FG_INFO, "  VOR/NDB");
491     current_navlist = new FGNavList;
492     FGPath p_nav( current_options.get_fg_root() );
493     p_nav.append( "Navaids/default.nav" );
494     current_navlist->init( p_nav );
495
496     FG_LOG(FG_GENERAL, FG_INFO, "  ILS");
497     current_ilslist = new FGILSList;
498     FGPath p_ils( current_options.get_fg_root() );
499     p_ils.append( "Navaids/default.ils" );
500     current_ilslist->init( p_ils );
501
502     FG_LOG(FG_GENERAL, FG_INFO, "  Fixes");
503     current_fixlist = new FGFixList;
504     FGPath p_fix( current_options.get_fg_root() );
505     p_fix.append( "Navaids/default.fix" );
506     current_fixlist->init( p_fix );
507
508     // Initialize the underlying radio stack model
509     current_radiostack = new FGRadioStack;
510
511     current_radiostack->set_nav1_freq( 117.30 );
512     current_radiostack->set_nav1_alt_freq( 110.30 );
513     current_radiostack->set_nav1_sel_radial( 119.0 );
514
515     current_radiostack->set_nav2_freq( 111.80 );
516     current_radiostack->set_nav2_alt_freq( 115.70 );
517     current_radiostack->set_nav2_sel_radial( 029.0 );
518
519     current_radiostack->set_adf_freq( 266.0 );
520
521 #if 0
522     // This block of settings are Alex's defaults for San Diego
523     current_radiostack->set_nav1_freq( 111.70 );
524     current_radiostack->set_nav1_alt_freq( 115.30 );
525     current_radiostack->set_nav1_sel_radial( 280.0 );
526     current_radiostack->set_nav2_freq( 117.80 );
527     current_radiostack->set_nav2_alt_freq( 114.00 );
528     current_radiostack->set_nav2_sel_radial( 68.0 );
529     current_radiostack->set_adf_freq( 210.0 );
530     // End of Alex's custom settings
531 #endif
532
533     current_radiostack->search( cur_fdm_state->get_Longitude(),
534                                 cur_fdm_state->get_Latitude(),
535                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
536
537     current_radiostack->update( cur_fdm_state->get_Longitude(),
538                                 cur_fdm_state->get_Latitude(),
539                                 cur_fdm_state->get_Altitude() * FEET_TO_METER );
540
541     // Search radio database once per second
542     global_events.Register( "fgRadioSearch()", fgRadioSearch,
543                             fgEVENT::FG_EVENT_READY, 1000);
544
545
546     // Initialize the Cockpit subsystem
547     if( fgCockpitInit( &current_aircraft )) {
548         // Cockpit initialized ok.
549     } else {
550         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Cockpit initialization!" );
551         exit(-1);
552     }
553
554     // Initialize the flight model subsystem data structures base on
555     // above values
556
557     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state,
558     //            1.0 / current_options.get_model_hz() );
559     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
560
561     // I'm just sticking this here for now, it should probably move
562     // eventually
563     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
564
565     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
566         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
567     }
568
569     FG_LOG( FG_GENERAL, FG_INFO,
570             "Updated position (after elevation adj): ("
571             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
572             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
573             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
574     // end of thing that I just stuck in that I should probably move
575
576     // Joystick support
577     if ( fgJoystickInit() ) {
578         // Joystick initialized ok.
579     } else {
580         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Joystick initialization!" );
581     }
582
583     // Autopilot init
584     current_autopilot = new FGAutopilot;
585     current_autopilot->init();
586
587     // initialize the gui parts of the autopilot
588     NewTgtAirportInit();
589     fgAPAdjustInit() ;
590     NewHeadingInit();
591     NewAltitudeInit();
592
593     // Initialize I/O channels
594 #if ! defined( MACOS )
595     fgIOInit();
596 #endif
597
598     // Initialize the 2D panel.
599     current_panel = fgCreateSmallSinglePropPanel(0, 0, 1024, 768);
600
601     // Initialize the BFI
602     FGBFI::init();
603
604     FG_LOG( FG_GENERAL, FG_INFO, endl);
605
606     return true;
607 }
608
609
610 void fgReInitSubsystems( void )
611 {
612     bool freeze = globals->get_freeze();
613     if( !freeze )
614         globals->set_freeze( true );
615     
616     fgInitPosition();
617     if( global_tile_mgr.init() ) {
618         // Load the local scenery data
619         global_tile_mgr.update( cur_fdm_state->get_Longitude(),
620                                 cur_fdm_state->get_Latitude() );
621     } else {
622         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
623                 exit(-1);
624     }
625     fgFDMSetGroundElevation( current_options.get_flight_model(), 
626                              scenery.cur_elev );
627
628     // Reset our altitude if we are below ground
629     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
630     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " << 
631             cur_fdm_state->get_Runway_altitude() );
632
633     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
634         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
635     }
636     double sea_level_radius_meters;
637     double lat_geoc;
638     // Set the FG variables first
639     fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
640                   &sea_level_radius_meters, &lat_geoc);
641     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(), 
642                                 cur_fdm_state->get_Altitude() + 
643                                 (sea_level_radius_meters * METER_TO_FEET) );
644     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
645         
646     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
647     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
648         
649     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
650     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
651
652     // The following section sets up the flight model EOM parameters
653     // and should really be read in from one or more files.
654
655     // Initial Velocity
656     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
657                              current_options.get_vBody(),
658                              current_options.get_wBody());
659
660     // Initial Orientation
661     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
662                          current_options.get_pitch() * DEG_TO_RAD,
663                          current_options.get_heading() * DEG_TO_RAD );
664
665     // Initial Angular Body rates
666     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
667
668     cur_fdm_state->set_Earth_position_angle( 0.0 );
669
670     // Mass properties and geometry values
671     cur_fdm_state->set_Inertias( 8.547270E+01, 
672                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
673
674     // CG position w.r.t. ref. point
675     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
676
677     // Initialize view parameters
678     current_view.set_view_offset( 0.0 );
679     current_view.set_goal_view_offset( 0.0 );
680     pilot_view.set_view_offset( 0.0 );
681     pilot_view.set_goal_view_offset( 0.0 );
682
683     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
684     current_view.UpdateViewMath(*cur_fdm_state);
685     pilot_view.UpdateViewMath(*cur_fdm_state);
686     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
687
688     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state, 
689     //            1.0 / current_options.get_model_hz() );
690     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
691
692     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
693
694     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
695         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
696     }
697
698     controls.reset_all();
699     current_autopilot->reset();
700
701     if( !freeze )
702         globals->set_freeze( false );
703 }