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