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