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