]> git.mxchange.org Git - flightgear.git/blob - Main/fg_init.cxx
Edited cvs log messages in source files ... bad bad bad!
[flightgear.git] / 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 // (Log is kept at end of this file)
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <Include/fg_constants.h>
35 #include <Include/general.h>
36
37 #include <Aircraft/aircraft.h>
38 #include <Astro/moon.hxx>
39 #include <Astro/planets.hxx>
40 #include <Astro/sky.hxx>
41 #include <Astro/stars.hxx>
42 #include <Astro/sun.hxx>
43 #include <Autopilot/autopilot.h>
44 #include <Cockpit/cockpit.hxx>
45 #include <Debug/fg_debug.h>
46 #include <Joystick/joystick.h>
47 #include <Math/fg_random.h>
48 #include <Scenery/scenery.h>
49 #include <Scenery/tilemgr.hxx>
50 #include <Time/event.hxx>
51 #include <Time/fg_time.hxx>
52 #include <Time/light.hxx>
53 #include <Time/sunpos.hxx>
54 #include <Weather/weather.h>
55
56 #include "airports.hxx"
57 #include "fg_init.hxx"
58 #include "options.hxx"
59 #include "views.hxx"
60
61 // extern int show_hud;             // HUD state
62 extern int displayInstruments;
63 extern const char *default_root;
64
65
66 // Set initial position
67 int fgInitPosition( void ) {
68     fgFLIGHT *f;
69     fgOPTIONS *o;
70
71     f = current_aircraft.flight;
72     o = &current_options;
73
74     // If nothing else is specified, default initial position is
75     // Globe, AZ (P13)
76     FG_Longitude = ( -110.6642444 ) * DEG_TO_RAD;
77     FG_Latitude  = (  33.3528917 ) * DEG_TO_RAD;
78     FG_Runway_altitude = (3234.5 + 300);
79     FG_Altitude = FG_Runway_altitude + 3.758099;
80
81     // Initial Position north of the city of Globe
82     // FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD;
83     // FG_Latitude  = (  120625.64 / 3600.0 ) * DEG_TO_RAD;
84     // FG_Longitude = ( -397867.44 / 3600.0 ) * DEG_TO_RAD;
85     // FG_Latitude  = (  119548.21 / 3600.0 ) * DEG_TO_RAD;
86     // FG_Altitude = 0.0 + 3.758099;
87
88     // Initial Position near where I used to live in Globe, AZ
89     // FG_Longitude = ( -398757.6 / 3600.0 ) * DEG_TO_RAD;
90     // FG_Latitude  = (  120160.0 / 3600.0 ) * DEG_TO_RAD;
91     // FG_Runway_altitude = 4000.0;
92     // FG_Altitude = FG_Runway_altitude + 3.758099;
93
94     // Initial Position: 10125 Jewell St. NE
95     // FG_Longitude = ( -93.15 ) * DEG_TO_RAD;
96     // FG_Latitude  = (  45.15 ) * DEG_TO_RAD;
97     // FG_Runway_altitude = 950.0;
98     // FG_Altitude = FG_Runway_altitude + 3.758099;
99
100     // Initial Position: Somewhere near the Grand Canyon
101     // FG_Longitude = ( -112.5 ) * DEG_TO_RAD;
102     // FG_Latitude  = (  36.5 ) * DEG_TO_RAD;
103     // FG_Runway_altitude = 8000.0;
104     // FG_Altitude = FG_Runway_altitude + 3.758099;
105
106     // Initial Position: Jim Brennon's Kingmont Observatory
107     // FG_Longitude = ( -121.1131667 ) * DEG_TO_RAD;
108     // FG_Latitude  = (   38.8293917 ) * DEG_TO_RAD;
109     // FG_Runway_altitude = 920.0;
110     // FG_Altitude = FG_Runway_altitude + 3.758099;
111
112     // probably interesting for european team members
113     // That is: If I can get the scenery to work -;) (Durk)
114  
115     // Eclipse Watching w73.5 n10 (approx) 18:00 UT
116     // FG_Longitude = ( -73.5 ) * DEG_TO_RAD;
117     // FG_Latitude  = (  10.0 ) * DEG_TO_RAD;
118     // FG_Runway_altitude = 0.0;
119     // FG_Altitude = FG_Runway_altitude + 3.758099;
120
121     // Test Position
122     // FG_Longitude = ( -109.5 ) * DEG_TO_RAD;
123     // FG_Latitude  = (  32.5 ) * DEG_TO_RAD;
124     // FG_Runway_altitude = (2646 + 2000);
125     // FG_Altitude = FG_Runway_altitude + 3.758099;
126
127     if ( strlen(o->airport_id) ) {
128         fgAIRPORTS airports;
129         fgAIRPORT a;
130
131         fgPrintf( FG_GENERAL, FG_INFO, 
132                   "Attempting to set starting position from airport code %s.\n",
133                   o->airport_id);
134
135         airports.load("Airports");
136         a = airports.search(o->airport_id);
137         if ( strcmp(a.id, "none") == 0 ) {
138             fgPrintf( FG_GENERAL, FG_EXIT, 
139                       "Failed to find %s in database.\n", o->airport_id);
140         } else {
141             FG_Longitude = ( a.longitude ) * DEG_TO_RAD;
142             FG_Latitude  = ( a.latitude ) * DEG_TO_RAD;
143             FG_Runway_altitude = ( a.elevation + 300 );
144             FG_Altitude = FG_Runway_altitude + 3.758099;
145         }           
146     }
147     
148     fgPrintf( FG_GENERAL, FG_INFO, 
149               "Initial position is: (%.4f, %.4f, %.2f)\n", 
150               FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, 
151               FG_Altitude * FEET_TO_METER);
152
153     return(1);
154 }
155
156
157 // General house keeping initializations
158 int fgInitGeneral( void ) {
159     fgGENERAL *g;
160
161     g = &general;
162
163     fgPrintf( FG_GENERAL, FG_INFO, "General Initialization\n" );
164     fgPrintf( FG_GENERAL, FG_INFO, "======= ==============\n" );
165
166     // determine the fg root path.  
167     if( !(g->root_dir) ) { 
168         // If not set by command line test for environmental var..
169         g->root_dir = getenv("FG_ROOT");
170         if ( !g->root_dir ) { 
171             // No root path set? Then assume, we will exit if this is
172             // wrong when looking for support files.
173             fgPrintf( FG_GENERAL, FG_EXIT, "%s %s\n",
174                       "Cannot continue without environment variable FG_ROOT",
175                       "being defined.");
176         }
177     }
178     fgPrintf( FG_GENERAL, FG_INFO, "FG_ROOT = %s\n\n", g->root_dir);
179
180     return ( 1 ); 
181 }
182
183
184 // This is the top level init routine which calls all the other
185 // initialization routines.  If you are adding a subsystem to flight
186 // gear, its initialization call should located in this routine.
187 // Returns non-zero if a problem encountered.
188 int fgInitSubsystems( void ) {
189     double cur_elev;
190
191     fgFLIGHT *f;
192     struct fgLIGHT *l;
193     struct fgTIME *t;
194     struct fgVIEW *v;
195
196     l = &cur_light_params;
197     t = &cur_time_params;
198     v = &current_view;
199
200     fgPrintf( FG_GENERAL, FG_INFO, "Initialize Subsystems\n");
201     fgPrintf( FG_GENERAL, FG_INFO, "========== ==========\n");
202
203     // seed the random number generater
204     fg_srandom();
205
206     // The following section sets up the flight model EOM parameters
207     // and should really be read in from one or more files.
208
209     // Must happen before any of the flight model or control
210     // parameters are set
211
212     fgAircraftInit();   // In the future this might not be the case.
213     f = current_aircraft.flight;
214
215     // set the initial position
216     fgInitPosition();
217
218     // Initial Velocity
219     FG_V_north = 0.0;   //  7.287719E+00
220     FG_V_east  = 0.0;   //  1.521770E+03
221     FG_V_down  = 0.0;   // -1.265722E-05
222
223     // Initial Orientation
224     FG_Phi   = -2.658474E-06;
225     FG_Theta =  7.401790E-03;
226     FG_Psi   =  270.0 * DEG_TO_RAD;
227
228     // Initial Angular B rates
229     FG_P_body = 7.206685E-05;
230     FG_Q_body = 0.000000E+00;
231     FG_R_body = 9.492658E-05;
232
233     FG_Earth_position_angle = 0.000000E+00;
234
235     // Mass properties and geometry values
236     FG_Mass = 8.547270E+01;
237     FG_I_xx = 1.048000E+03;
238     FG_I_yy = 3.000000E+03;
239     FG_I_zz = 3.530000E+03;
240     FG_I_xz = 0.000000E+00;
241
242     // CG position w.r.t. ref. point
243     FG_Dx_cg = 0.000000E+00;
244     FG_Dy_cg = 0.000000E+00;
245     FG_Dz_cg = 0.000000E+00;
246
247     // Initialize the event manager
248     fgEventInit();
249
250     // Output event stats every 60 seconds
251     fgEventRegister( "fgEventPrintStats()", fgEventPrintStats,
252                      FG_EVENT_READY, 60000 );
253
254     // Initialize the time dependent variables
255     fgTimeInit(t);
256     fgTimeUpdate(f, t);
257
258     // Initialize view parameters
259     // ---->
260     fgViewInit(v);
261     fgViewUpdate(f, v, l);
262
263     // Initialize the orbital elements of sun, moon and mayor planets
264     fgSolarSystemInit(*t);
265
266     // Initialize the Stars subsystem
267     if( fgStarsInit() ) {
268         // Stars initialized ok.
269     } else {
270         fgPrintf( FG_GENERAL, FG_EXIT, "Error in Stars initialization!\n" );
271     }
272
273     // Initialize the planetary subsystem
274     fgEventRegister("fgPlanetsInit()", fgPlanetsInit, FG_EVENT_READY, 600000);
275
276     // Initialize the sun's position 
277     fgEventRegister("fgSunInit()", fgSunInit, FG_EVENT_READY, 30000 );
278
279     // Intialize the moon's position
280     fgEventRegister( "fgMoonInit()", fgMoonInit, FG_EVENT_READY, 600000 );
281
282     // fgUpdateSunPos() needs a few position and view parameters set
283     // so it can calculate local relative sun angle and a few other
284     // things for correctly orienting the sky.
285     // ---->
286     fgUpdateSunPos();
287
288     // Initialize Lighting interpolation tables
289     // ---->
290     fgLightInit();
291
292     // update the lighting parameters (based on sun angle)
293     fgEventRegister( "fgLightUpdate()", fgLightUpdate,
294                      FG_EVENT_READY, 30000 );
295
296     // Initialize the weather modeling subsystem
297     fgWeatherInit();
298
299     // update the weather for our current position
300     fgEventRegister( "fgWeatherUpdate()", fgWeatherUpdate,
301                      FG_EVENT_READY, 120000 );
302
303     // Initialize the Cockpit subsystem
304     if( fgCockpitInit( &current_aircraft )) {
305         // Cockpit initialized ok.
306     } else {
307         fgPrintf( FG_GENERAL, FG_EXIT, "Error in Cockpit initialization!\n" );
308     }
309
310     // Initialize the "sky"
311     // ---->
312     fgSkyInit();
313
314     // Initialize the Scenery Management subsystem
315     if ( fgSceneryInit() ) {
316         // Scenery initialized ok.
317     } else {
318         fgPrintf( FG_GENERAL, FG_EXIT, "Error in Scenery initialization!\n" );
319     }
320
321
322     if( fgTileMgrInit() ) {
323         // Load the local scenery data
324         fgTileMgrUpdate();
325     } else {
326         fgPrintf( FG_GENERAL, FG_EXIT, 
327                   "Error in Tile Manager initialization!\n" );
328     }
329
330     // I'm just sticking this here for now, it should probably move
331     // eventually
332         cur_elev = FG_Runway_altitude * FEET_TO_METER;
333         if ( cur_elev > -9990.0 ) {
334             FG_Runway_altitude = cur_elev * METER_TO_FEET;
335         }
336
337         if ( FG_Altitude < FG_Runway_altitude ) {
338             FG_Altitude = FG_Runway_altitude + 3.758099;
339         }
340
341         fgPrintf( FG_GENERAL, FG_INFO,
342              "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n",
343              FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG,
344              FG_Altitude * FEET_TO_METER);
345     // end of thing that I just stuck in that I should probably move
346                 
347     // Initialize the flight model subsystem data structures base on
348     // above values
349
350     fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
351
352     // Let's not show the instrument panel
353     displayInstruments = 0;
354
355     // Joystick support
356     if (fgJoystickInit(0) ) {
357         // Joystick initialized ok.
358     } else {
359         fgPrintf( FG_GENERAL, FG_EXIT, "Error in Joystick initialization!\n" );
360     }
361
362     // Autopilot init added here, by Jeff Goeke-Smith
363     fgAPInit(&current_aircraft);
364     
365     fgPrintf(FG_GENERAL, FG_INFO,"\n");
366
367     return(1);
368 }
369
370
371 // $Log$
372 // Revision 1.5  1998/04/25 22:06:30  curt
373 // Edited cvs log messages in source files ... bad bad bad!
374 //
375 // Revision 1.4  1998/04/25 20:24:01  curt
376 // Cleaned up initialization sequence to eliminate interdependencies
377 // between sun position, lighting, and view position.  This creates a
378 // valid single pass initialization path.
379 //
380 // Revision 1.3  1998/04/25 15:11:11  curt
381 // Added an command line option to set starting position based on airport ID.
382 //
383 // Revision 1.2  1998/04/24 00:49:20  curt
384 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
385 // Trying out some different option parsing code.
386 // Some code reorganization.
387 //
388 // Revision 1.1  1998/04/22 13:25:44  curt
389 // C++ - ifing the code.
390 // Starting a bit of reorganization of lighting code.
391 //
392 // Revision 1.56  1998/04/18 04:11:28  curt
393 // Moved fg_debug to it's own library, added zlib support.
394 //
395 // Revision 1.55  1998/04/14 02:21:03  curt
396 // Incorporated autopilot heading hold contributed by:  Jeff Goeke-Smith
397 // <jgoeke@voyager.net>
398 //
399 // Revision 1.54  1998/04/08 23:35:36  curt
400 // Tweaks to Gnu automake/autoconf system.
401 //
402 // Revision 1.53  1998/04/03 22:09:06  curt
403 // Converting to Gnu autoconf system.
404 //
405 // Revision 1.52  1998/03/23 21:24:38  curt
406 // Source code formating tweaks.
407 //
408 // Revision 1.51  1998/03/14 00:31:22  curt
409 // Beginning initial terrain texturing experiments.
410 //
411 // Revision 1.50  1998/03/09 22:46:19  curt
412 // Minor tweaks.
413 //
414 // Revision 1.49  1998/02/23 19:07:59  curt
415 // Incorporated Durk's Astro/ tweaks.  Includes unifying the sun position
416 // calculation code between sun display, and other FG sections that use this
417 // for things like lighting.
418 //
419 // Revision 1.48  1998/02/21 14:53:15  curt
420 // Added Charlie's HUD changes.
421 //
422 // Revision 1.47  1998/02/19 13:05:53  curt
423 // Incorporated some HUD tweaks from Michelle America.
424 // Tweaked the sky's sunset/rise colors.
425 // Other misc. tweaks.
426 //
427 // Revision 1.46  1998/02/18 15:07:06  curt
428 // Tweaks to build with SGI OpenGL (and therefor hopefully other accelerated
429 // drivers will work.)
430 //
431 // Revision 1.45  1998/02/16 13:39:43  curt
432 // Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
433 // tiles to occasionally be missing.
434 //
435 // Revision 1.44  1998/02/12 21:59:50  curt
436 // Incorporated code changes contributed by Charlie Hotchkiss
437 // <chotchkiss@namg.us.anritsu.com>
438 //
439 // Revision 1.43  1998/02/11 02:50:40  curt
440 // Minor changes.
441 //
442 // Revision 1.42  1998/02/09 22:56:58  curt
443 // Removed "depend" files from cvs control.  Other minor make tweaks.
444 //
445 // Revision 1.41  1998/02/09 15:07:50  curt
446 // Minor tweaks.
447 //
448 // Revision 1.40  1998/02/07 15:29:44  curt
449 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
450 // <chotchkiss@namg.us.anritsu.com>
451 //
452 // Revision 1.39  1998/02/03 23:20:25  curt
453 // Lots of little tweaks to fix various consistency problems discovered by
454 // Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
455 // passed arguments along to the real printf().  Also incorporated HUD changes
456 // by Michele America.
457 //
458 // Revision 1.38  1998/02/02 20:53:58  curt
459 // Incorporated Durk's changes.
460 //
461 // Revision 1.37  1998/02/01 03:39:54  curt
462 // Minor tweaks.
463 //
464 // Revision 1.36  1998/01/31 00:43:13  curt
465 // Added MetroWorks patches from Carmen Volpe.
466 //
467 // Revision 1.35  1998/01/27 00:47:57  curt
468 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
469 // system and commandline/config file processing code.
470 //
471 // Revision 1.34  1998/01/22 02:59:37  curt
472 // Changed #ifdef FILE_H to #ifdef _FILE_H
473 //
474 // Revision 1.33  1998/01/21 21:11:34  curt
475 // Misc. tweaks.
476 //
477 // Revision 1.32  1998/01/19 19:27:08  curt
478 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
479 // This should simplify things tremendously.
480 //
481 // Revision 1.31  1998/01/19 18:40:32  curt
482 // Tons of little changes to clean up the code and to remove fatal errors
483 // when building with the c++ compiler.
484 //
485 // Revision 1.30  1998/01/13 00:23:09  curt
486 // Initial changes to support loading and management of scenery tiles.  Note,
487 // there's still a fair amount of work left to be done.
488 //
489 // Revision 1.29  1998/01/08 02:22:08  curt
490 // Beginning to integrate Tile management subsystem.
491 //
492 // Revision 1.28  1998/01/07 03:18:58  curt
493 // Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
494 //
495 // Revision 1.27  1998/01/05 18:44:35  curt
496 // Add an option to advance/decrease time from keyboard.
497 //
498 // Revision 1.26  1997/12/30 23:09:04  curt
499 // Tweaking initialization sequences.
500 //
501 // Revision 1.25  1997/12/30 22:22:33  curt
502 // Further integration of event manager.
503 //
504 // Revision 1.24  1997/12/30 20:47:44  curt
505 // Integrated new event manager with subsystem initializations.
506 //
507 // Revision 1.23  1997/12/30 16:36:50  curt
508 // Merged in Durk's changes ...
509 //
510 // Revision 1.22  1997/12/19 23:34:05  curt
511 // Lot's of tweaking with sky rendering and lighting.
512 //
513 // Revision 1.21  1997/12/19 16:45:00  curt
514 // Working on scene rendering order and options.
515 //
516 // Revision 1.20  1997/12/18 23:32:33  curt
517 // First stab at sky dome actually starting to look reasonable. :-)
518 //
519 // Revision 1.19  1997/12/17 23:13:36  curt
520 // Began working on rendering a sky.
521 //
522 // Revision 1.18  1997/12/15 23:54:49  curt
523 // Add xgl wrappers for debugging.
524 // Generate terrain normals on the fly.
525 //
526 // Revision 1.17  1997/12/15 20:59:09  curt
527 // Misc. tweaks.
528 //
529 // Revision 1.16  1997/12/12 19:52:48  curt
530 // Working on lightling and material properties.
531 //
532 // Revision 1.15  1997/12/11 04:43:55  curt
533 // Fixed sun vector and lighting problems.  I thing the moon is now lit
534 // correctly.
535 //
536 // Revision 1.14  1997/12/10 22:37:47  curt
537 // Prepended "fg" on the name of all global structures that didn't have it yet.
538 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
539 //
540 // Revision 1.13  1997/11/25 19:25:32  curt
541 // Changes to integrate Durk's moon/sun code updates + clean up.
542 //
543 // Revision 1.12  1997/11/15 18:16:35  curt
544 // minor tweaks.
545 //
546 // Revision 1.11  1997/10/30 12:38:42  curt
547 // Working on new scenery subsystem.
548 //
549 // Revision 1.10  1997/10/25 03:24:23  curt
550 // Incorporated sun, moon, and star positioning code contributed by Durk Talsma.
551 //
552 // Revision 1.9  1997/09/23 00:29:39  curt
553 // Tweaks to get things to compile with gcc-win32.
554 //
555 // Revision 1.8  1997/09/22 14:44:20  curt
556 // Continuing to try to align stars correctly.
557 //
558 // Revision 1.7  1997/09/16 15:50:30  curt
559 // Working on star alignment and time issues.
560 //
561 // Revision 1.6  1997/09/05 14:17:30  curt
562 // More tweaking with stars.
563 //
564 // Revision 1.5  1997/09/04 02:17:36  curt
565 // Shufflin' stuff.
566 //
567 // Revision 1.4  1997/08/27 21:32:26  curt
568 // Restructured view calculation code.  Added stars.
569 //
570 // Revision 1.3  1997/08/27 03:30:19  curt
571 // Changed naming scheme of basic shared structures.
572 //
573 // Revision 1.2  1997/08/25 20:27:23  curt
574 // Merged in initial HUD and Joystick code.
575 //
576 // Revision 1.1  1997/08/23 01:46:20  curt
577 // Initial revision.
578 //
579