]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_init.cxx
Tidying up the SGEphemeris class.
[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 #include <simgear/timing/sg_time.hxx>
58
59 #include <Aircraft/aircraft.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/sp_panel.hxx>
67 #include <FDM/Balloon.h>
68 #include <FDM/External.hxx>
69 #include <FDM/JSBsim.hxx>
70 #include <FDM/LaRCsim.hxx>
71 #include <FDM/MagicCarpet.hxx>
72 #include <Include/general.hxx>
73 #include <Joystick/joystick.hxx>
74 #include <Objects/matlib.hxx>
75 #include <Navaids/fixlist.hxx>
76 #include <Navaids/ilslist.hxx>
77 #include <Navaids/navlist.hxx>
78 #include <Scenery/scenery.hxx>
79 #include <Scenery/tilemgr.hxx>
80 #include <Time/event.hxx>
81 #include <Time/light.hxx>
82 #include <Time/sunpos.hxx>
83 #include <Time/moonpos.hxx>
84 #include <Time/tmp.hxx>
85
86 #ifndef FG_OLD_WEATHER
87 #  include <WeatherCM/FGLocalWeatherDatabase.h>
88 #else
89 #  include <Weather/weather.hxx>
90 #endif
91
92 #include "fg_init.hxx"
93 #include "fg_io.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 initial position and orientation
140 bool fgInitPosition( void ) {
141     string id;
142     FGInterface *f;
143
144     f = current_aircraft.fdm_state;
145
146     id = current_options.get_airport_id();
147     if ( id.length() ) {
148         // set initial position from airport id
149
150         FGPath path( current_options.get_fg_root() );
151         path.append( "Airports" );
152         path.append( "simple.mk4" );
153         FGAirports airports( path.c_str() );
154         FGAirport a;
155
156         FG_LOG( FG_GENERAL, FG_INFO,
157                 "Attempting to set starting position from airport code "
158                 << id );
159
160         // FGPath inpath( current_options.get_fg_root() );
161         // inpath.append( "Airports" );
162         // inpath.append( "apt_simple" );
163         // airports.load( inpath.c_str() );
164
165         // FGPath outpath( current_options.get_fg_root() );
166         // outpath.append( "Airports" );
167         // outpath.append( "simple.gdbm" );
168         // airports.dump_gdbm( outpath.c_str() );
169
170         if ( ! airports.search( id, &a ) ) {
171             FG_LOG( FG_GENERAL, FG_ALERT,
172                     "Failed to find " << id << " in database." );
173             exit(-1);
174         } else {
175             f->set_Longitude( a.longitude * DEG_TO_RAD );
176             f->set_Latitude( a.latitude * DEG_TO_RAD );
177         }
178     } else {
179         // set initial position from default or command line coordinates
180
181         f->set_Longitude( current_options.get_lon() * DEG_TO_RAD );
182         f->set_Latitude( current_options.get_lat() * DEG_TO_RAD );
183     }
184
185     FG_LOG( FG_GENERAL, FG_INFO,
186             "starting altitude is = " << current_options.get_altitude() );
187
188     f->set_Altitude( current_options.get_altitude() * METER_TO_FEET );
189     fgFDMSetGroundElevation( current_options.get_flight_model(),
190                              (f->get_Altitude() - 3.758099) * FEET_TO_METER );
191
192     FG_LOG( FG_GENERAL, FG_INFO,
193             "Initial position is: ("
194             << (f->get_Longitude() * RAD_TO_DEG) << ", "
195             << (f->get_Latitude() * RAD_TO_DEG) << ", "
196             << (f->get_Altitude() * FEET_TO_METER) << ")" );
197
198     return true;
199 }
200
201
202 // General house keeping initializations
203 bool fgInitGeneral( void ) {
204     string root;
205
206 #if defined(FX) && defined(XMESA)
207     char *mesa_win_state;
208 #endif
209
210     FG_LOG( FG_GENERAL, FG_INFO, "General Initialization" );
211     FG_LOG( FG_GENERAL, FG_INFO, "======= ==============" );
212
213     root = current_options.get_fg_root();
214     if ( ! root.length() ) {
215         // No root path set? Then bail ...
216         FG_LOG( FG_GENERAL, FG_ALERT,
217                 "Cannot continue without environment variable FG_ROOT"
218                 << "being defined." );
219         exit(-1);
220     }
221     FG_LOG( FG_GENERAL, FG_INFO, "FG_ROOT = " << '"' << root << '"' << endl );
222
223 #if defined(FX) && defined(XMESA)
224     // initialize full screen flag
225     global_fullscreen = false;
226     if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
227         // Test for the MESA_GLX_FX env variable
228         if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
229             // test if we are fullscreen mesa/glide
230             if ( (mesa_win_state[0] == 'f') ||
231                  (mesa_win_state[0] == 'F') ) {
232                 global_fullscreen = true;
233             }
234         }
235     }
236 #endif
237
238     return true;
239 }
240
241
242 // This is the top level init routine which calls all the other
243 // initialization routines.  If you are adding a subsystem to flight
244 // gear, its initialization call should located in this routine.
245 // Returns non-zero if a problem encountered.
246 bool fgInitSubsystems( void ) {
247     fgLIGHT *l = &cur_light_params;
248
249     FG_LOG( FG_GENERAL, FG_INFO, "Initialize Subsystems");
250     FG_LOG( FG_GENERAL, FG_INFO, "========== ==========");
251
252     if ( current_options.get_flight_model() == FGInterface::FG_LARCSIM ) {
253         cur_fdm_state = new FGLaRCsim;
254     } else if ( current_options.get_flight_model() == FGInterface::FG_JSBSIM ) {
255         cur_fdm_state = new FGJSBsim;
256     } else if ( current_options.get_flight_model() == 
257                 FGInterface::FG_BALLOONSIM ) {
258         cur_fdm_state = new FGBalloonSim;
259     } else if ( current_options.get_flight_model() == 
260                 FGInterface::FG_MAGICCARPET ) {
261         cur_fdm_state = new FGMagicCarpet;
262     } else if ( current_options.get_flight_model() == 
263                 FGInterface::FG_EXTERNAL ) {
264         cur_fdm_state = new FGExternal;
265     } else {
266         FG_LOG( FG_GENERAL, FG_ALERT,
267                 "No flight model, can't init aircraft" );
268         exit(-1);
269     }
270
271     // allocates structures so must happen before any of the flight
272     // model or control parameters are set
273     fgAircraftInit();   // In the future this might not be the case.
274
275     // set the initial position
276     fgInitPosition();
277
278     // Initialize the material property lib
279     FGPath mpath( current_options.get_fg_root() );
280     mpath.append( "materials" );
281     if ( material_lib.load( mpath.str() ) ) {
282     } else {
283         FG_LOG( FG_GENERAL, FG_ALERT, "Error loading material lib!" );
284         exit(-1);
285     }
286
287     // Initialize the Scenery Management subsystem
288     if ( fgSceneryInit() ) {
289         // Material lib initialized ok.
290     } else {
291         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Scenery initialization!" );
292         exit(-1);
293     }
294
295     if( global_tile_mgr.init() ) {
296         // Load the local scenery data
297         global_tile_mgr.update( cur_fdm_state->get_Longitude(),
298                                 cur_fdm_state->get_Latitude() );
299     } else {
300         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
301         exit(-1);
302     }
303
304     FG_LOG( FG_GENERAL, FG_DEBUG,
305             "Current terrain elevation after tile mgr init " <<
306             scenery.cur_elev );
307
308     // Calculate ground elevation at starting point (we didn't have
309     // tmp_abs_view_pos calculated when fgTileMgrUpdate() was called above
310     //
311     // calculalate a cartesian point somewhere along the line between
312     // the center of the earth and our view position.  Doesn't have to
313     // be the exact elevation (this is good because we don't know it
314     // yet :-)
315
316     // now handled inside of the fgTileMgrUpdate()
317
318     /*
319     geod_pos = Point3D( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(), 0.0);
320     tmp_abs_view_pos = fgGeodToCart(geod_pos);
321
322     FG_LOG( FG_GENERAL, FG_DEBUG,
323             "Initial abs_view_pos = " << tmp_abs_view_pos );
324     scenery.cur_elev =
325         fgTileMgrCurElev( cur_fdm_state->get_Longitude(), cur_fdm_state->get_Latitude(),
326                           tmp_abs_view_pos );
327     FG_LOG( FG_GENERAL, FG_DEBUG,
328             "Altitude after update " << scenery.cur_elev );
329     */
330
331     fgFDMSetGroundElevation( current_options.get_flight_model(),
332                              scenery.cur_elev );
333
334     // Reset our altitude if we are below ground
335     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
336     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " <<
337             cur_fdm_state->get_Runway_altitude() );
338
339     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
340         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
341     }
342
343     FG_LOG( FG_GENERAL, FG_INFO,
344             "Updated position (after elevation adj): ("
345             << (cur_fdm_state->get_Latitude() * RAD_TO_DEG) << ", "
346             << (cur_fdm_state->get_Longitude() * RAD_TO_DEG) << ", "
347             << (cur_fdm_state->get_Altitude() * FEET_TO_METER) << ")" );
348
349     // We need to calculate a few more values here that would normally
350     // be calculated by the FDM so that the current_view.UpdateViewMath()
351     // routine doesn't get hosed.
352
353     double sea_level_radius_meters;
354     double lat_geoc;
355     // Set the FG variables first
356     fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(),
357                   &sea_level_radius_meters, &lat_geoc);
358     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(),
359                                 cur_fdm_state->get_Altitude() +
360                                 (sea_level_radius_meters * METER_TO_FEET) );
361     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
362
363     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
364     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
365         
366     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
367     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
368
369     // The following section sets up the flight model EOM parameters
370     // and should really be read in from one or more files.
371
372     // Initial Velocity
373     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
374                              current_options.get_vBody(),
375                              current_options.get_wBody());
376
377     // Initial Orientation
378     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
379                          current_options.get_pitch() * DEG_TO_RAD,
380                          current_options.get_heading() * DEG_TO_RAD );
381
382     // Initial Angular Body rates
383     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
384
385     cur_fdm_state->set_Earth_position_angle( 0.0 );
386
387     // Mass properties and geometry values
388     cur_fdm_state->set_Inertias( 8.547270E+01,
389                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
390
391     // CG position w.r.t. ref. point
392     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
393
394     // Initialize the event manager
395     global_events.Init();
396
397     // Output event stats every 60 seconds
398     global_events.Register( "fgEVENT_MGR::PrintStats()",
399                             fgMethodCallback<fgEVENT_MGR>( &global_events,
400                                                    &fgEVENT_MGR::PrintStats),
401                             fgEVENT::FG_EVENT_READY, 60000 );
402
403     // Initialize view parameters
404     FG_LOG( FG_GENERAL, FG_DEBUG, "Before current_view.init()");
405     current_view.Init();
406     pilot_view.Init();
407     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
408     current_view.UpdateViewMath(*cur_fdm_state);
409     pilot_view.UpdateViewMath(*cur_fdm_state);
410     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
411     // current_view.UpdateWorldToEye(f);
412
413     // Initialize the planetary subsystem
414     // global_events.Register( "fgPlanetsInit()", fgPlanetsInit,
415     //                      fgEVENT::FG_EVENT_READY, 600000);
416
417     // Initialize the sun's position
418     // global_events.Register( "fgSunInit()", fgSunInit,
419     //                      fgEVENT::FG_EVENT_READY, 30000 );
420
421     // Intialize the moon's position
422     // global_events.Register( "fgMoonInit()", fgMoonInit,
423     //                      fgEVENT::FG_EVENT_READY, 600000 );
424
425     // fgUpdateSunPos() needs a few position and view parameters set
426     // so it can calculate local relative sun angle and a few other
427     // things for correctly orienting the sky.
428     fgUpdateSunPos();
429     fgUpdateMoonPos();
430     global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
431                             fgEVENT::FG_EVENT_READY, 60000);
432     global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
433                             fgEVENT::FG_EVENT_READY, 60000);
434
435     // Initialize Lighting interpolation tables
436     l->Init();
437
438     // update the lighting parameters (based on sun angle)
439     global_events.Register( "fgLight::Update()",
440                             fgMethodCallback<fgLIGHT>( &cur_light_params,
441                                                        &fgLIGHT::Update),
442                             fgEVENT::FG_EVENT_READY, 30000 );
443     // update the current timezone each 30 minutes
444     global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
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, current_options.get_fg_root() );
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     // Initialize the BFI
585     FGBFI::init();
586
587     FG_LOG( FG_GENERAL, FG_INFO, endl);
588
589     return true;
590 }
591
592
593 void fgReInitSubsystems( void )
594 {
595     int toggle_pause = current_options.get_pause();
596     
597     if( !toggle_pause )
598         current_options.toggle_pause();
599     
600     fgInitPosition();
601     if( global_tile_mgr.init() ) {
602         // Load the local scenery data
603         global_tile_mgr.update( cur_fdm_state->get_Longitude(),
604                                 cur_fdm_state->get_Latitude() );
605     } else {
606         FG_LOG( FG_GENERAL, FG_ALERT, "Error in Tile Manager initialization!" );
607                 exit(-1);
608     }
609     fgFDMSetGroundElevation( current_options.get_flight_model(), 
610                              scenery.cur_elev );
611
612     // Reset our altitude if we are below ground
613     FG_LOG( FG_GENERAL, FG_DEBUG, "Current altitude = " << cur_fdm_state->get_Altitude() );
614     FG_LOG( FG_GENERAL, FG_DEBUG, "Current runway altitude = " << 
615             cur_fdm_state->get_Runway_altitude() );
616
617     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
618         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
619     }
620     double sea_level_radius_meters;
621     double lat_geoc;
622     // Set the FG variables first
623     fgGeodToGeoc( cur_fdm_state->get_Latitude(), cur_fdm_state->get_Altitude(), 
624                   &sea_level_radius_meters, &lat_geoc);
625     cur_fdm_state->set_Geocentric_Position( lat_geoc, cur_fdm_state->get_Longitude(), 
626                                 cur_fdm_state->get_Altitude() + 
627                                 (sea_level_radius_meters * METER_TO_FEET) );
628     cur_fdm_state->set_Sea_level_radius( sea_level_radius_meters * METER_TO_FEET );
629         
630     cur_fdm_state->set_sin_cos_longitude(cur_fdm_state->get_Longitude());
631     cur_fdm_state->set_sin_cos_latitude(cur_fdm_state->get_Latitude());
632         
633     cur_fdm_state->set_sin_lat_geocentric(sin(lat_geoc));
634     cur_fdm_state->set_cos_lat_geocentric(cos(lat_geoc));
635
636     // The following section sets up the flight model EOM parameters
637     // and should really be read in from one or more files.
638
639     // Initial Velocity
640     cur_fdm_state->set_Velocities_Local( current_options.get_uBody(),
641                              current_options.get_vBody(),
642                              current_options.get_wBody());
643
644     // Initial Orientation
645     cur_fdm_state->set_Euler_Angles( current_options.get_roll() * DEG_TO_RAD,
646                          current_options.get_pitch() * DEG_TO_RAD,
647                          current_options.get_heading() * DEG_TO_RAD );
648
649     // Initial Angular Body rates
650     cur_fdm_state->set_Omega_Body( 7.206685E-05, 0.0, 9.492658E-05 );
651
652     cur_fdm_state->set_Earth_position_angle( 0.0 );
653
654     // Mass properties and geometry values
655     cur_fdm_state->set_Inertias( 8.547270E+01, 
656                      1.048000E+03, 3.000000E+03, 3.530000E+03, 0.000000E+00 );
657
658     // CG position w.r.t. ref. point
659     cur_fdm_state->set_CG_Position( 0.0, 0.0, 0.0 );
660
661     // Initialize view parameters
662     current_view.set_view_offset( 0.0 );
663     current_view.set_goal_view_offset( 0.0 );
664     pilot_view.set_view_offset( 0.0 );
665     pilot_view.set_goal_view_offset( 0.0 );
666
667     FG_LOG( FG_GENERAL, FG_DEBUG, "After current_view.init()");
668     current_view.UpdateViewMath(*cur_fdm_state);
669     pilot_view.UpdateViewMath(*cur_fdm_state);
670     FG_LOG( FG_GENERAL, FG_DEBUG, "  abs_view_pos = " << current_view.get_abs_view_pos());
671
672     // fgFDMInit( current_options.get_flight_model(), cur_fdm_state, 
673     //            1.0 / current_options.get_model_hz() );
674     cur_fdm_state->init( 1.0 / current_options.get_model_hz() );
675
676     scenery.cur_elev = cur_fdm_state->get_Runway_altitude() * FEET_TO_METER;
677
678     if ( cur_fdm_state->get_Altitude() < cur_fdm_state->get_Runway_altitude() + 3.758099) {
679         cur_fdm_state->set_Altitude( cur_fdm_state->get_Runway_altitude() + 3.758099 );
680     }
681
682     controls.reset_all();
683     current_autopilot->reset();
684
685     if( !toggle_pause )
686         current_options.toggle_pause();
687 }