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