]> git.mxchange.org Git - flightgear.git/blob - src/Main/options.cxx
Working on setting initial altitude correctly after a reset.
[flightgear.git] / src / Main / options.cxx
1 // options.cxx -- class to handle command line options
2 //
3 // Written by Curtis Olson, started April 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
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 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <math.h>            // rint()
31 #include <stdio.h>
32 #include <stdlib.h>          // atof(), atoi()
33 #include <string.h>
34
35 #include STL_STRING
36
37 #include <simgear/misc/fgstream.hxx>
38
39 // #include <Include/general.hxx>
40 // #include <Airports/simple.hxx>
41 // #include <Cockpit/cockpit.hxx>
42 // #include <FDM/flight.hxx>
43 // #include <FDM/UIUCModel/uiuc_aircraftdir.h>
44 #ifdef FG_NETWORK_OLK
45 #  include <NetworkOLK/network.h>
46 #endif
47
48 #include "globals.hxx"
49 #include "fg_init.hxx"
50 #include "fg_props.hxx"
51 #include "options.hxx"
52
53 FG_USING_STD(string);
54 FG_USING_NAMESPACE(std);
55
56
57 #define NEW_DEFAULT_MODEL_HZ 120
58
59 enum
60 {
61     FG_OPTIONS_OK = 0,
62     FG_OPTIONS_HELP = 1,
63     FG_OPTIONS_ERROR = 2
64 };
65
66 static double
67 atof( const string& str )
68 {
69
70 #ifdef __MWERKS__ 
71     // -dw- if ::atof is called, then we get an infinite loop
72     return std::atof( str.c_str() );
73 #else
74     return ::atof( str.c_str() );
75 #endif
76 }
77
78 static int
79 atoi( const string& str )
80 {
81 #ifdef __MWERKS__ 
82     // -dw- if ::atoi is called, then we get an infinite loop
83     return std::atoi( str.c_str() );
84 #else
85     return ::atoi( str.c_str() );
86 #endif
87 }
88
89
90 /**
91  * Set a few fail-safe default property values.
92  *
93  * These should all be set in $FG_ROOT/preferences.xml, but just
94  * in case, we provide some initial sane values here. This method 
95  * should be invoked *before* reading any init files.
96  */
97 void
98 fgSetDefaults ()
99 {
100     // set a possibly independent location for scenery data
101     char *envp = ::getenv( "FG_SCENERY" );
102
103     if ( envp != NULL ) {
104         // fg_root could be anywhere, so default to environmental
105         // variable $FG_ROOT if it is set.
106         globals->set_fg_scenery(envp);
107     } else {
108         // Otherwise, default to Scenery being in $FG_ROOT/Scenery
109         globals->set_fg_scenery("");
110     }
111                                 // Position (Globe, AZ)
112     fgSetDouble("/position/longitude", -110.6642444);
113     fgSetDouble("/position/latitude", 33.3528917);
114     fgSetDouble("/position/altitude", -9999.0);
115
116                                 // Orientation
117     fgSetDouble("/orientation/heading", 270);
118     fgSetDouble("/orientation/roll", 0);
119     fgSetDouble("/orientation/pitch", 0.424);
120
121                                 // Velocities
122     fgSetString("/sim/startup/speed-set", "knots");
123     fgSetDouble("/velocities/uBody", 0.0);
124     fgSetDouble("/velocities/vBody", 0.0);
125     fgSetDouble("/velocities/wBody", 0.0);
126     fgSetDouble("/velocities/speed-north", 0.0);
127     fgSetDouble("/velocities/speed-east", 0.0);
128     fgSetDouble("/velocities/speed-down", 0.0);
129     fgSetDouble("/velocities/airspeed", 0.0);
130     fgSetDouble("/velocities/mach", 0.0);
131
132                                 // Miscellaneous
133     fgSetBool("/sim/startup/game-mode", false);
134     fgSetBool("/sim/startup/splash-screen", true);
135     fgSetBool("/sim/startup/intro-music", true);
136     fgSetString("/sim/startup/mouse-pointer", "disabled");
137     fgSetString("/sim/control-mode", "joystick");
138     fgSetBool("/sim/auto-coordination", false);
139
140                                 // Features
141     fgSetBool("/sim/hud/visibility", false);
142     fgSetBool("/sim/panel/visibility", true);
143     fgSetBool("/sim/sound", true);
144     fgSetBool("/sim/hud/antialiased", false);
145
146                                 // Flight Model options
147     fgSetString("/sim/flight-model", "larcsim");
148     fgSetString("/sim/aircraft", "c172");
149     fgSetInt("/sim/model-hz", NEW_DEFAULT_MODEL_HZ);
150     fgSetInt("/sim/speed-up", 1);
151     fgSetBool("/sim/startup/trim", false);
152
153                                 // Rendering options
154     fgSetString("/sim/rendering/fog", "nicest");
155     fgSetBool("/environment/clouds/status", true);
156     fgSetDouble("/environment/clouds/altitude", 5000);
157     fgSetBool("/sim/startup/fullscreen", false);
158     fgSetBool("/sim/rendering/shading", true);
159     fgSetBool("/sim/rendering/skyblend", true);
160     fgSetBool("/sim/rendering/textures", true);
161     fgSetBool("/sim/rendering/wireframe", false);
162     fgSetInt("/sim/startup/xsize", 800);
163     fgSetInt("/sim/startup/ysize", 600);
164     fgSetInt("/sim/rendering/bits-per-pixel", 16);
165     fgSetString("/sim/view-mode", "pilot");
166     fgSetDouble("/sim/startup/view-offset", 0);
167     fgSetDouble("/environment/visibility", 20000);
168
169                                 // HUD options
170     fgSetString("/sim/startup/units", "feet");
171     fgSetString("/sim/hud/frame-stat-type", "tris");
172         
173                                 // Time options
174     fgSetInt("/sim/startup/time-offset", 0);
175     fgSetString("/sim/startup/time-offset-type", "system-offset");
176
177     fgSetBool("/sim/networking/network-olk", false);
178     fgSetString("/sim/networking/call-sign", "Johnny");
179 }
180
181 static double
182 parse_time(const string& time_in) {
183     char *time_str, num[256];
184     double hours, minutes, seconds;
185     double result = 0.0;
186     int sign = 1;
187     int i;
188
189     time_str = (char *)time_in.c_str();
190
191     // printf("parse_time(): %s\n", time_str);
192
193     // check for sign
194     if ( strlen(time_str) ) {
195         if ( time_str[0] == '+' ) {
196             sign = 1;
197             time_str++;
198         } else if ( time_str[0] == '-' ) {
199             sign = -1;
200             time_str++;
201         }
202     }
203     // printf("sign = %d\n", sign);
204
205     // get hours
206     if ( strlen(time_str) ) {
207         i = 0;
208         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
209             num[i] = time_str[0];
210             time_str++;
211             i++;
212         }
213         if ( time_str[0] == ':' ) {
214             time_str++;
215         }
216         num[i] = '\0';
217         hours = atof(num);
218         // printf("hours = %.2lf\n", hours);
219
220         result += hours;
221     }
222
223     // get minutes
224     if ( strlen(time_str) ) {
225         i = 0;
226         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
227             num[i] = time_str[0];
228             time_str++;
229             i++;
230         }
231         if ( time_str[0] == ':' ) {
232             time_str++;
233         }
234         num[i] = '\0';
235         minutes = atof(num);
236         // printf("minutes = %.2lf\n", minutes);
237
238         result += minutes / 60.0;
239     }
240
241     // get seconds
242     if ( strlen(time_str) ) {
243         i = 0;
244         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
245             num[i] = time_str[0];
246             time_str++;
247             i++;
248         }
249         num[i] = '\0';
250         seconds = atof(num);
251         // printf("seconds = %.2lf\n", seconds);
252
253         result += seconds / 3600.0;
254     }
255
256     return(sign * result);
257 }
258
259
260 static long int 
261 parse_date( const string& date)
262 {
263     struct tm gmt;
264     char * date_str, num[256];
265     int i;
266     // initialize to zero
267     gmt.tm_sec = 0;
268     gmt.tm_min = 0;
269     gmt.tm_hour = 0;
270     gmt.tm_mday = 0;
271     gmt.tm_mon = 0;
272     gmt.tm_year = 0;
273     gmt.tm_isdst = 0; // ignore daylight savings time for the moment
274     date_str = (char *)date.c_str();
275     // get year
276     if ( strlen(date_str) ) {
277         i = 0;
278         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
279             num[i] = date_str[0];
280             date_str++;
281             i++;
282         }
283         if ( date_str[0] == ':' ) {
284             date_str++;
285         }
286         num[i] = '\0';
287         gmt.tm_year = atoi(num) - 1900;
288     }
289     // get month
290     if ( strlen(date_str) ) {
291         i = 0;
292         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
293             num[i] = date_str[0];
294             date_str++;
295             i++;
296         }
297         if ( date_str[0] == ':' ) {
298             date_str++;
299         }
300         num[i] = '\0';
301         gmt.tm_mon = atoi(num) -1;
302     }
303     // get day
304     if ( strlen(date_str) ) {
305         i = 0;
306         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
307             num[i] = date_str[0];
308             date_str++;
309             i++;
310         }
311         if ( date_str[0] == ':' ) {
312             date_str++;
313         }
314         num[i] = '\0';
315         gmt.tm_mday = atoi(num);
316     }
317     // get hour
318     if ( strlen(date_str) ) {
319         i = 0;
320         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
321             num[i] = date_str[0];
322             date_str++;
323             i++;
324         }
325         if ( date_str[0] == ':' ) {
326             date_str++;
327         }
328         num[i] = '\0';
329         gmt.tm_hour = atoi(num);
330     }
331     // get minute
332     if ( strlen(date_str) ) {
333         i = 0;
334         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
335             num[i] = date_str[0];
336             date_str++;
337             i++;
338         }
339         if ( date_str[0] == ':' ) {
340             date_str++;
341         }
342         num[i] = '\0';
343         gmt.tm_min = atoi(num);
344     }
345     // get second
346     if ( strlen(date_str) ) {
347         i = 0;
348         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
349             num[i] = date_str[0];
350             date_str++;
351             i++;
352         }
353         if ( date_str[0] == ':' ) {
354             date_str++;
355         }
356         num[i] = '\0';
357         gmt.tm_sec = atoi(num);
358     }
359     time_t theTime = sgTimeGetGMT( gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
360                                    gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
361     //printf ("Date is %s\n", ctime(&theTime));
362     //printf ("in seconds that is %d\n", theTime);
363     //exit(1);
364     return (theTime);
365 }
366
367
368 /// parse degree in the form of [+/-]hhh:mm:ss
369 static double
370 parse_degree( const string& degree_str) {
371     double result = parse_time( degree_str );
372
373     // printf("Degree = %.4f\n", result);
374
375     return(result);
376 }
377
378
379 // parse time offset command line option
380 static int
381 parse_time_offset( const string& time_str) {
382     int result;
383
384     // printf("time offset = %s\n", time_str);
385
386 #ifdef HAVE_RINT
387     result = (int)rint(parse_time(time_str) * 3600.0);
388 #else
389     result = (int)(parse_time(time_str) * 3600.0);
390 #endif
391
392     // printf("parse_time_offset(): %d\n", result);
393
394     return( result );
395 }
396
397
398 // Parse --fov=x.xx type option 
399 static double
400 parse_fov( const string& arg ) {
401     double fov = atof(arg);
402
403     if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
404     if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
405
406     fgSetDouble("/sim/field-of-view", fov);
407
408     // printf("parse_fov(): result = %.4f\n", fov);
409
410     return fov;
411 }
412
413
414 // Parse I/O channel option
415 //
416 // Format is "--protocol=medium,direction,hz,medium_options,..."
417 //
418 //   protocol = { native, nmea, garmin, fgfs, rul, pve, etc. }
419 //   medium = { serial, socket, file, etc. }
420 //   direction = { in, out, bi }
421 //   hz = number of times to process channel per second (floating
422 //        point values are ok.
423 //
424 // Serial example "--nmea=serial,dir,hz,device,baud" where
425 // 
426 //  device = OS device name of serial line to be open()'ed
427 //  baud = {300, 1200, 2400, ..., 230400}
428 //
429 // Socket exacmple "--native=socket,dir,hz,machine,port,style" where
430 // 
431 //  machine = machine name or ip address if client (leave empty if server)
432 //  port = port, leave empty to let system choose
433 //  style = tcp or udp
434 //
435 // File example "--garmin=file,dir,hz,filename" where
436 // 
437 //  filename = file system file name
438
439 static bool 
440 parse_channel( const string& type, const string& channel_str ) {
441     // cout << "Channel string = " << channel_str << endl;
442
443     globals->get_channel_options_list().push_back( type + "," + channel_str );
444
445     return true;
446 }
447
448
449 // Parse --wp=ID[,alt]
450 static bool 
451 parse_wp( const string& arg ) {
452     string id, alt_str;
453     double alt = 0.0;
454
455     int pos = arg.find( "@" );
456     if ( pos != string::npos ) {
457         id = arg.substr( 0, pos );
458         alt_str = arg.substr( pos + 1 );
459         // cout << "id str = " << id << "  alt str = " << alt_str << endl;
460         alt = atof( alt_str.c_str() );
461         if ( fgGetString("/sim/startup/units") == "feet" ) {
462             alt *= FEET_TO_METER;
463         }
464     } else {
465         id = arg;
466     }
467
468     FGAirport a;
469     if ( fgFindAirportID( id, &a ) ) {
470         SGWayPoint wp( a.longitude, a.latitude, alt, SGWayPoint::WGS84, id );
471         globals->get_route()->add_waypoint( wp );
472
473         return true;
474     } else {
475         return false;
476     }
477 }
478
479
480 // Parse --flight-plan=[file]
481 static bool 
482 parse_flightplan(const string& arg)
483 {
484     fg_gzifstream infile(arg.c_str());
485     if (!infile) {
486         return false;
487     }
488     while ( true ) {
489         string line;
490 #ifdef GETLINE_NEEDS_TERMINATOR
491         getline( infile, line, '\n' );
492 #elif defined( macintosh )
493         getline( infile, line, '\r' );
494 #else
495         getline( infile, line );
496 #endif
497         if ( infile.eof() ) {
498             break;
499         }
500         parse_wp(line);
501     }
502
503     return true;
504 }
505
506
507 // Parse a single option
508 static int 
509 parse_option (const string& arg) 
510 {
511     // General Options
512     if ( (arg == "--help") || (arg == "-h") ) {
513         // help/usage request
514         return(FG_OPTIONS_HELP);
515     } else if ( arg == "--disable-game-mode") {
516         fgSetBool("/sim/startup/game-mode", false);
517     } else if ( arg == "--enable-game-mode" ) {
518         fgSetBool("/sim/startup/game-mode", true);
519     } else if ( arg == "--disable-splash-screen" ) {
520         fgSetBool("/sim/startup/splash-screen", false); 
521     } else if ( arg == "--enable-splash-screen" ) {
522         fgSetBool("/sim/startup/splash-screen", true);
523     } else if ( arg == "--disable-intro-music" ) {
524         fgSetBool("/sim/startup/intro-music", false);
525     } else if ( arg == "--enable-intro-music" ) {
526         fgSetBool("/sim/startup/intro-music", true);
527     } else if ( arg == "--disable-mouse-pointer" ) {
528         fgSetString("/sim/startup/mouse-pointer", "disabled");
529     } else if ( arg == "--enable-mouse-pointer" ) {
530         fgSetString("/sim/startup/mouse-pointer", "enabled");
531     } else if ( arg == "--disable-freeze" ) {
532         fgSetBool("/sim/freeze", false);
533     } else if ( arg == "--enable-freeze" ) {
534         fgSetBool("/sim/freeze", true);
535     } else if ( arg == "--disable-anti-alias-hud" ) {
536         fgSetBool("/sim/hud/antialiased", false);
537     } else if ( arg == "--enable-anti-alias-hud" ) {
538         fgSetBool("/sim/hud/antialiased", true);
539     } else if ( arg.find( "--control=") != string::npos ) {
540         fgSetString("/sim/control-mode", arg.substr(10));
541     } else if ( arg == "--disable-auto-coordination" ) {
542         fgSetBool("/sim/auto-coordination", false);
543     } else if ( arg == "--enable-auto-coordination" ) {
544         fgSetBool("/sim/auto-coordination", true);
545     } else if ( arg == "--disable-hud" ) {
546         fgSetBool("/sim/hud/visibility", false);
547     } else if ( arg == "--enable-hud" ) {
548         fgSetBool("/sim/hud/visibility", true);
549     } else if ( arg == "--disable-panel" ) {
550         fgSetBool("/sim/panel/visibility", false);
551     } else if ( arg == "--enable-panel" ) {
552         fgSetBool("/sim/panel/visibility", true);
553     } else if ( arg == "--disable-sound" ) {
554         fgSetBool("/sim/sound", false);
555     } else if ( arg == "--enable-sound" ) {
556         fgSetBool("/sim/sound", true);
557     } else if ( arg.find( "--airport-id=") != string::npos ) {
558                                 // NB: changed property name!!!
559         fgSetString("/sim/startup/airport-id", arg.substr(13));
560     } else if ( arg.find( "--lon=" ) != string::npos ) {
561         fgSetDouble("/position/longitude",
562                               parse_degree(arg.substr(6)));
563         fgSetString("/position/airport-id", "");
564     } else if ( arg.find( "--lat=" ) != string::npos ) {
565         fgSetDouble("/position/latitude",
566                               parse_degree(arg.substr(6)));
567         fgSetString("/position/airport-id", "");
568     } else if ( arg.find( "--altitude=" ) != string::npos ) {
569         if ( fgGetString("/sim/startup/units") == "feet" )
570             fgSetDouble("/position/altitude", atof(arg.substr(11)));
571         else
572             fgSetDouble("/position/altitude",
573                         atof(arg.substr(11)) * METER_TO_FEET);
574     } else if ( arg.find( "--uBody=" ) != string::npos ) {
575         fgSetString("/sim/startup/speed-set", "UVW");
576                                 // FIXME: the units are totally confused here
577         if ( fgGetString("/sim/startup/units") == "feet" )
578           fgSetDouble("/velocities/uBody", atof(arg.substr(8)));
579         else
580           fgSetDouble("/velocities/uBody",
581                                atof(arg.substr(8)) * FEET_TO_METER);
582     } else if ( arg.find( "--vBody=" ) != string::npos ) {
583         fgSetString("/sim/startup/speed-set", "UVW");
584                                 // FIXME: the units are totally confused here
585         if ( fgGetString("/sim/startup/units") == "feet" )
586           fgSetDouble("/velocities/vBody", atof(arg.substr(8)));
587         else
588           fgSetDouble("/velocities/vBody",
589                                atof(arg.substr(8)) * FEET_TO_METER);
590     } else if ( arg.find( "--wBody=" ) != string::npos ) {
591         fgSetString("/sim/startup/speed-set", "UVW");
592                                 // FIXME: the units are totally confused here
593         if ( fgGetString("/sim/startup/units") == "feet" )
594           fgSetDouble("/velocities/wBody", atof(arg.substr(8)));
595         else
596           fgSetDouble("/velocities/wBody",
597                                atof(arg.substr(8)) * FEET_TO_METER);
598     } else if ( arg.find( "--vNorth=" ) != string::npos ) {
599         fgSetString("/sim/startup/speed-set", "NED");
600                                 // FIXME: the units are totally confused here
601         if ( fgGetString("/sim/startup/units") == "feet" )
602           fgSetDouble("/velocities/speed-north", atof(arg.substr(8)));
603         else
604           fgSetDouble("/velocities/speed-north",
605                                atof(arg.substr(8)) * FEET_TO_METER);
606     } else if ( arg.find( "--vEast=" ) != string::npos ) {
607         fgSetString("/sim/startup/speed-set", "NED");
608                                 // FIXME: the units are totally confused here
609         if ( fgGetString("/sim/startup/units") == "feet" )
610           fgSetDouble("/velocities/speed-east", atof(arg.substr(8)));
611         else
612           fgSetDouble("/velocities/speed-east",
613                                atof(arg.substr(8)) * FEET_TO_METER);
614     } else if ( arg.find( "--vDown=" ) != string::npos ) {
615         fgSetString("/sim/startup/speed-set", "NED");
616                                 // FIXME: the units are totally confused here
617         if ( fgGetString("/sim/startup/units") == "feet" )
618           fgSetDouble("/velocities/speed-down", atof(arg.substr(8)));
619         else
620           fgSetDouble("/velocities/speed-down",
621                                atof(arg.substr(8)) * FEET_TO_METER);
622     } else if ( arg.find( "--vc=" ) != string::npos) {
623         fgSetString("/sim/startup/speed-set", "knots");
624         fgSetDouble("/velocities/airspeed", atof(arg.substr(5)));
625     } else if ( arg.find( "--mach=" ) != string::npos) {
626         fgSetString("/sim/startup/speed-set", "mach");
627         fgSetDouble("/velocities/mach", atof(arg.substr(7)));
628     } else if ( arg.find( "--heading=" ) != string::npos ) {
629         fgSetDouble("/orientation/heading", atof(arg.substr(10)));
630     } else if ( arg.find( "--roll=" ) != string::npos ) {
631         fgSetDouble("/orientation/roll", atof(arg.substr(7)));
632     } else if ( arg.find( "--pitch=" ) != string::npos ) {
633         fgSetDouble("/orientation/pitch", atof(arg.substr(8)));
634     } else if ( arg.find( "--fg-root=" ) != string::npos ) {
635         globals->set_fg_root(arg.substr( 10 ));
636     } else if ( arg.find( "--fg-scenery=" ) != string::npos ) {
637         globals->set_fg_scenery(arg.substr( 13 ));
638     } else if ( arg.find( "--fdm=" ) != string::npos ) {
639         fgSetString("/sim/flight-model", arg.substr(6));
640     } else if ( arg.find( "--aircraft=" ) != string::npos ) {
641         fgSetString("/sim/aircraft", arg.substr(11));
642     } else if ( arg.find( "--aircraft-dir=" ) != string::npos ) {
643         fgSetString("/sim/aircraft-dir", arg.substr(15));
644     } else if ( arg.find( "--model-hz=" ) != string::npos ) {
645         fgSetInt("/sim/model-hz", atoi(arg.substr(11)));
646     } else if ( arg.find( "--speed=" ) != string::npos ) {
647         fgSetInt("/sim/speed-up", atoi(arg.substr(8)));
648     } else if ( arg.find( "--trim") != string::npos) {
649         fgSetInt("/sim/startup/trim", true);
650     } else if ( arg.find( "--notrim") != string::npos) {
651         fgSetInt("/sim/startup/trim", false);
652     } else if ( arg == "--fog-disable" ) {
653         fgSetString("/sim/rendering/fog", "disabled");
654     } else if ( arg == "--fog-fastest" ) {
655         fgSetString("/sim/rendering/fog", "fastest");
656     } else if ( arg == "--fog-nicest" ) {
657         fgSetString("/sim/fog", "nicest");
658     } else if ( arg == "--disable-clouds" ) {
659         fgSetBool("/environment/clouds/status", false);
660     } else if ( arg == "--enable-clouds" ) {
661         fgSetBool("/environment/clouds/status", true);
662     } else if ( arg.find( "--clouds-asl=" ) != string::npos ) {
663                                 // FIXME: check units
664         if ( fgGetString("/sim/startup/units") == "feet" )
665           fgSetDouble("/environment/clouds/altitude",
666                                 atof(arg.substr(13)) * FEET_TO_METER);
667         else
668           fgSetDouble("/environment/clouds/altitude",
669                                 atof(arg.substr(13)));
670     } else if ( arg.find( "--fov=" ) != string::npos ) {
671         parse_fov( arg.substr(6) );
672     } else if ( arg == "--disable-fullscreen" ) {
673         fgSetBool("/sim/startup/fullscreen", false);
674     } else if ( arg== "--enable-fullscreen") {
675         fgSetBool("/sim/startup/fullscreen", true);
676     } else if ( arg == "--shading-flat") {
677         fgSetBool("/sim/rendering/shading", false);
678     } else if ( arg == "--shading-smooth") {
679         fgSetBool("/sim/rendering/shading", true);
680     } else if ( arg == "--disable-skyblend") {
681         fgSetBool("/sim/rendering/skyblend", false);
682     } else if ( arg== "--enable-skyblend" ) {
683         fgSetBool("/sim/rendering/skyblend", true);
684     } else if ( arg == "--disable-textures" ) {
685         fgSetBool("/sim/rendering/textures", false);
686     } else if ( arg == "--enable-textures" ) {
687         fgSetBool("/sim/rendering/textures", true);
688     } else if ( arg == "--disable-wireframe" ) {
689         fgSetBool("/sim/rendering/wireframe", false);
690     } else if ( arg == "--enable-wireframe" ) {
691         fgSetBool("/sim/rendering/wireframe", true);
692     } else if ( arg.find( "--geometry=" ) != string::npos ) {
693         bool geometry_ok = true;
694         int xsize = 0, ysize = 0;
695         string geometry = arg.substr( 11 );
696         string::size_type i = geometry.find('x');
697
698         if (i != string::npos) {
699             xsize = atoi(geometry.substr(0, i));
700             ysize = atoi(geometry.substr(i+1));
701         } else {
702             geometry_ok = false;
703         }
704
705         if ( xsize <= 0 || ysize <= 0 ) {
706             xsize = 640;
707             ysize = 480;
708             geometry_ok = false;
709         }
710
711         if ( !geometry_ok ) {
712             FG_LOG( FG_GENERAL, FG_ALERT, "Unknown geometry: " << geometry );
713             FG_LOG( FG_GENERAL, FG_ALERT,
714                     "Setting geometry to " << xsize << 'x' << ysize << '\n');
715         } else {
716           FG_LOG( FG_GENERAL, FG_INFO,
717                   "Setting geometry to " << xsize << 'x' << ysize << '\n');
718           fgSetInt("/sim/startup/xsize", xsize);
719           fgSetInt("/sim/startup/ysize", ysize);
720         }
721     } else if ( arg.find( "--bpp=" ) != string::npos ) {
722         string bits_per_pix = arg.substr( 6 );
723         if ( bits_per_pix == "16" ) {
724             fgSetInt("/sim/rendering/bits-per-pixel", 16);
725         } else if ( bits_per_pix == "24" ) {
726             fgSetInt("/sim/rendering/bits-per-pixel", 24);
727         } else if ( bits_per_pix == "32" ) {
728             fgSetInt("/sim/rendering/bits-per-pixel", 32);
729         } else {
730           FG_LOG(FG_GENERAL, FG_ALERT, "Unsupported bpp " << bits_per_pix);
731         }
732     } else if ( arg == "--units-feet" ) {
733         fgSetString("/sim/startup/units", "feet");
734     } else if ( arg == "--units-meters" ) {
735         fgSetString("/sim/startup/units", "meters");
736     } else if ( arg.find( "--time-offset" ) != string::npos ) {
737         fgSetInt("/sim/startup/time-offset",
738                            parse_time_offset( (arg.substr(14)) ));
739     } else if ( arg.find( "--time-match-real") != string::npos ) {
740         fgSetString("/sim/startup/time-offset_type",
741                               "system-offset");
742     } else if ( arg.find( "--time-match-local") != string::npos ) {
743         fgSetString("/sim/startup/time-offset_type",
744                               "latitude-offset");
745     } else if ( arg.find( "--start-date-sys=") != string::npos ) {
746         fgSetInt("/sim/startup/time-offset",
747                            parse_date((arg.substr(17))));
748         fgSetString("/sim/startup/time-offset-type", "system");
749     } else if ( arg.find( "--start-date-lat=") != string::npos ) {
750         fgSetInt("/sim/startup/time-offset",
751                            parse_date((arg.substr(17))));
752         fgSetString("/sim/startup/time-offset-type",
753                            "latitude");
754     } else if ( arg.find( "--start-date-gmt=") != string::npos ) {
755         fgSetInt("/sim/startup/time-offset",
756                            parse_date((arg.substr(17))));
757         fgSetString("/sim/startup/time-offset-type", "gmt");
758     } else if ( arg == "--hud-tris" ) {
759         fgSetString("/sim/hud/frame-stat-type", "tris");
760     } else if ( arg == "--hud-culled" ) {
761         fgSetString("/sim/hud/frame-stat-type", "culled");
762     } else if ( arg.find( "--native=" ) != string::npos ) {
763         parse_channel( "native", arg.substr(9) );
764     } else if ( arg.find( "--garmin=" ) != string::npos ) {
765         parse_channel( "garmin", arg.substr(9) );
766     } else if ( arg.find( "--nmea=" ) != string::npos ) {
767         parse_channel( "nmea", arg.substr(7) );
768     } else if ( arg.find( "--props=" ) != string::npos ) {
769         parse_channel( "props", arg.substr(8) );
770     } else if ( arg.find( "--pve=" ) != string::npos ) {
771         parse_channel( "pve", arg.substr(6) );
772     } else if ( arg.find( "--ray=" ) != string::npos ) {
773         parse_channel( "ray", arg.substr(6) );
774     } else if ( arg.find( "--rul=" ) != string::npos ) {
775         parse_channel( "rul", arg.substr(6) );
776     } else if ( arg.find( "--joyclient=" ) != string::npos ) {
777         parse_channel( "joyclient", arg.substr(12) );
778 #ifdef FG_NETWORK_OLK
779     } else if ( arg == "--disable-network-olk" ) {
780         fgSetBool("/sim/networking/olk", false);
781     } else if ( arg== "--enable-network-olk") {
782         fgSetBool("/sim/networking/olk", true);
783     } else if ( arg == "--net-hud" ) {
784         fgSetBool("/sim/hud/net-display", true);
785         net_hud_display = 1;    // FIXME
786     } else if ( arg.find( "--net-id=") != string::npos ) {
787         fgSetString("sim/networking/call-sign", arg.substr(9));
788 #endif
789     } else if ( arg.find( "--prop:" ) == 0 ) {
790         string assign = arg.substr(7);
791         int pos = assign.find('=');
792         if (pos == arg.npos || pos == 0) {
793             FG_LOG(FG_GENERAL, FG_ALERT, "Bad property assignment: " << arg);
794             return FG_OPTIONS_ERROR;
795         }
796         string name = assign.substr(0, pos);
797         string value = assign.substr(pos + 1);
798         fgSetString(name.c_str(), value);
799         FG_LOG(FG_GENERAL, FG_INFO, "Setting default value of property "
800                << name << " to \"" << value << '"');
801     // $$$ begin - added VS Renganathan, 14 Oct 2K
802     // for multi-window outside window imagery
803     } else if ( arg.find( "--view-offset=" ) != string::npos ) {
804         string woffset = arg.substr( 14 );
805         double default_view_offset = 0.0;
806         if ( woffset == "LEFT" ) {
807                default_view_offset = FG_PI * 0.25;
808         } else if ( woffset == "RIGHT" ) {
809             default_view_offset = FG_PI * 1.75;
810         } else if ( woffset == "CENTER" ) {
811             default_view_offset = 0.00;
812         } else {
813             default_view_offset = atof( woffset.c_str() ) * DEG_TO_RAD;
814         }
815         FGViewerRPH *pilot_view =
816             (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
817         pilot_view->set_view_offset( default_view_offset );
818         pilot_view->set_goal_view_offset( default_view_offset );
819         fgSetDouble("/sim/startup/view-offset", default_view_offset);
820     // $$$ end - added VS Renganathan, 14 Oct 2K
821     } else if ( arg.find( "--visibility=" ) != string::npos ) {
822         fgSetDouble("/environment/visibility", atof(arg.substr(13)));
823     } else if ( arg.find( "--visibility-miles=" ) != string::npos ) {
824         double visibility = atof(arg.substr(19)) * 5280.0 * FEET_TO_METER;
825         fgSetDouble("/environment/visibility", visibility);
826     } else if ( arg.find( "--wind=" ) == 0 ) {
827         string val = arg.substr(7);
828         int pos = val.find('@');
829         if (pos == string::npos) {
830           FG_LOG(FG_GENERAL, FG_ALERT, "bad wind value " << val);
831           return FG_OPTIONS_ERROR;
832         }
833         double dir = atof(val.substr(0,pos).c_str());
834         double speed = atof(val.substr(pos+1).c_str());
835         FG_LOG(FG_GENERAL, FG_INFO, "WIND: " << dir << '@' << 
836                speed << " knots" << endl);
837                                 // convert to fps
838         speed *= NM_TO_METER * METER_TO_FEET * (1.0/3600);
839         dir += 180;
840         if (dir >= 360)
841           dir -= 360;
842         dir *= DEG_TO_RAD;
843         fgSetDouble("/environment/wind-north",
844                                              speed * cos(dir));
845         fgSetDouble("/environment/wind-east",
846                                              speed * sin(dir));
847     } else if ( arg.find( "--wp=" ) != string::npos ) {
848         parse_wp( arg.substr( 5 ) );
849     } else if ( arg.find( "--flight-plan=") != string::npos) {
850         parse_flightplan ( arg.substr (14) );
851     } else {
852         FG_LOG( FG_GENERAL, FG_ALERT, "Unknown option '" << arg << "'" );
853         return FG_OPTIONS_ERROR;
854     }
855     
856     return FG_OPTIONS_OK;
857 }
858
859
860 // Scan the command line options for an fg_root definition and set
861 // just that.
862 string
863 fgScanForRoot (int argc, char **argv) 
864 {
865     int i = 1;
866
867     FG_LOG(FG_GENERAL, FG_INFO, "Scanning for root: command line");
868
869     while ( i < argc ) {
870         FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
871
872         string arg = argv[i];
873         if ( arg.find( "--fg-root=" ) != string::npos ) {
874             return arg.substr( 10 );
875         }
876
877         i++;
878     }
879
880     return "";
881 }
882
883
884 // Scan the config file for an fg_root definition and set just that.
885 string
886 fgScanForRoot (const string& path)
887 {
888     fg_gzifstream in( path );
889     if ( !in.is_open() )
890       return "";
891
892     FG_LOG( FG_GENERAL, FG_INFO, "Scanning for root: " << path );
893
894     in >> skipcomment;
895 #ifndef __MWERKS__
896     while ( ! in.eof() ) {
897 #else
898     char c = '\0';
899     while ( in.get(c) && c != '\0' ) {
900         in.putback(c);
901 #endif
902         string line;
903
904 #ifdef GETLINE_NEEDS_TERMINATOR
905         getline( in, line, '\n' );
906 #elif defined( macintosh )
907         getline( in, line, '\r' );
908 #else
909         getline( in, line );
910 #endif
911
912         if ( line.find( "--fg-root=" ) != string::npos ) {
913             return line.substr( 10 );
914         }
915
916         in >> skipcomment;
917     }
918
919     return "";
920 }
921
922
923 // Parse the command line options
924 void
925 fgParseOptions (int argc, char **argv) {
926     int i = 1;
927     int result;
928
929     FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
930
931     while ( i < argc ) {
932         FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
933
934         result = parse_option(argv[i]);
935         if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
936             fgUsage();
937             exit(-1);
938         }
939
940         i++;
941     }
942 }
943
944
945 // Parse config file options
946 void
947 fgParseOptions (const string& path) {
948     fg_gzifstream in( path );
949     if ( !in.is_open() )
950       return;
951
952     FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
953
954     in >> skipcomment;
955 #ifndef __MWERKS__
956     while ( ! in.eof() ) {
957 #else
958     char c = '\0';
959     while ( in.get(c) && c != '\0' ) {
960         in.putback(c);
961 #endif
962         string line;
963
964 #ifdef GETLINE_NEEDS_TERMINATOR
965         getline( in, line, '\n' );
966 #elif defined( macintosh )
967         getline( in, line, '\r' );
968 #else
969         getline( in, line );
970 #endif
971
972         if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
973             FG_LOG( FG_GENERAL, FG_ALERT, 
974                     "Config file parse error: " << path << " '" 
975                     << line << "'" );
976             fgUsage();
977             exit(-1);
978         }
979         in >> skipcomment;
980     }
981 }
982
983
984 // Print usage message
985 void 
986 fgUsage ()
987 {
988     cout << "Usage: fg [ options ... ]" << endl;
989     cout << endl;
990
991     cout << "General Options:" << endl;
992     cout << "\t--help -h:  print usage" << endl;
993     cout << "\t--fg-root=path:  specify the root path for all the data files"
994          << endl;
995     cout << "\t--fg-scenery=path:  specify the base path for all the scenery"
996          << " data." << endl
997          << "\t\tdefaults to $FG_ROOT/Scenery" << endl;
998     cout << "\t--disable-game-mode:  disable full-screen game mode" << endl;
999     cout << "\t--enable-game-mode:  enable full-screen game mode" << endl;
1000     cout << "\t--disable-splash-screen:  disable splash screen" << endl;
1001     cout << "\t--enable-splash-screen:  enable splash screen" << endl;
1002     cout << "\t--disable-intro-music:  disable introduction music" << endl;
1003     cout << "\t--enable-intro-music:  enable introduction music" << endl;
1004     cout << "\t--disable-mouse-pointer:  disable extra mouse pointer" << endl;
1005     cout << "\t--enable-mouse-pointer:  enable extra mouse pointer (i.e. for"
1006          << endl;
1007     cout << "\t\tfull screen voodoo/voodoo-II based cards.)" << endl;
1008     cout << "\t--disable-freeze:  start out in an running state" << endl;
1009     cout << "\t--enable-freeze:  start out in a frozen state" << endl;
1010     cout << "\t--control=mode:  primary control mode " 
1011          << "(joystick, keyboard, mouse)" << endl;
1012     cout << endl;
1013
1014     cout << "Features:" << endl;
1015     cout << "\t--disable-hud:  disable heads up display" << endl;
1016     cout << "\t--enable-hud:  enable heads up display" << endl;
1017     cout << "\t--disable-panel:  disable instrument panel" << endl;
1018     cout << "\t--enable-panel:  enable instrumetn panel" << endl;
1019     cout << "\t--disable-sound:  disable sound effects" << endl;
1020     cout << "\t--enable-sound:  enable sound effects" << endl;
1021     cout << "\t--disable-anti-alias-hud:  disable anti aliased hud" << endl;
1022     cout << "\t--enable-anti-alias-hud:  enable anti aliased hud" << endl;
1023     cout << endl;
1024  
1025     cout << "Flight Model:" << endl;
1026     cout << "\t--fdm=abcd:  selects the core flight model code." << endl;
1027     cout << "\t\tcan be one of jsb, larcsim, magic, external, balloon, or ada"
1028          << endl;
1029     cout << "\t--aircraft=abcd:  aircraft model to load" << endl;
1030     cout << "\t--model-hz=n:  run the FDM this rate (iterations per second)" 
1031          << endl;
1032     cout << "\t--speed=n:  run the FDM this much faster than real time" << endl;
1033     cout << "\t--notrim:  Do NOT attempt to trim the model when initializing JSBsim" << endl;
1034     cout << "\t--wind=DIR@SPEED: specify wind coming from DIR (degrees) at SPEED (knots)" << endl;
1035     cout << endl;
1036
1037     //(UIUC)
1038     cout <<"Aircraft model directory:" << endl;
1039     cout <<"\t--aircraft-dir=<path> path is relative to the path of the executable" << endl;
1040     cout << endl;
1041
1042     cout << "Initial Position and Orientation:" << endl;
1043     cout << "\t--airport-id=ABCD:  specify starting postion by airport id" 
1044          << endl;
1045     cout << "\t--lon=degrees:  starting longitude in degrees (west = -)" 
1046          << endl;
1047     cout << "\t--lat=degrees:  starting latitude in degrees (south = -)"
1048          << endl;
1049     cout << "\t--altitude=feet:  starting altitude in feet" << endl;
1050     cout << "\t\t(unless --units-meters specified" << endl;
1051     cout << "\t--heading=degrees:  heading (yaw) angle in degress (Psi)"
1052          << endl;
1053     cout << "\t--roll=degrees:  roll angle in degrees (Phi)" << endl;
1054     cout << "\t--pitch=degrees:  pitch angle in degrees (Theta)" << endl;
1055     cout << "\t--uBody=feet per second:  velocity along the body X axis"
1056          << endl;
1057     cout << "\t--vBody=feet per second:  velocity along the body Y axis"
1058          << endl;
1059     cout << "\t--wBody=feet per second:  velocity along the body Z axis"
1060          << endl;
1061     cout << "\t\t(unless --units-meters specified" << endl;
1062     cout << "\t--vc= initial airspeed in knots (--fdm=jsb only)" << endl;
1063     cout << "\t--mach= initial mach number (--fdm=jsb only)" << endl;
1064     cout << endl;
1065
1066     cout << "Rendering Options:" << endl;
1067     cout << "\t--fog-disable:  disable fog/haze" << endl;
1068     cout << "\t--fog-fastest:  enable fastest fog/haze" << endl;
1069     cout << "\t--fog-nicest:  enable nicest fog/haze" << endl;
1070     cout << "\t--enable-clouds:  enable demo cloud layer" << endl;
1071     cout << "\t--disable-clouds:  disable demo cloud layer" << endl;
1072     cout << "\t--clouds-asl=xxx:  specify altitude of cloud layer above sea level" << endl;
1073     cout << "\t--fov=xx.x:  specify initial field of view angle in degrees"
1074          << endl;
1075     cout << "\t--disable-fullscreen:  disable fullscreen mode" << endl;
1076     cout << "\t--enable-fullscreen:  enable fullscreen mode" << endl;
1077     cout << "\t--shading-flat:  enable flat shading" << endl;
1078     cout << "\t--shading-smooth:  enable smooth shading" << endl;
1079     cout << "\t--disable-skyblend:  disable sky blending" << endl;
1080     cout << "\t--enable-skyblend:  enable sky blending" << endl;
1081     cout << "\t--disable-textures:  disable textures" << endl;
1082     cout << "\t--enable-textures:  enable textures" << endl;
1083     cout << "\t--disable-wireframe:  disable wireframe drawing mode" << endl;
1084     cout << "\t--enable-wireframe:  enable wireframe drawing mode" << endl;
1085     cout << "\t--geometry=WWWxHHH:  window geometry: 640x480, 800x600, etc."
1086          << endl;
1087     cout << "\t--view-offset=xxx:  set the default forward view direction"
1088          << endl;
1089     cout << "\t\tas an offset from straight ahead.  Allowable values are"
1090          << endl;
1091     cout << "\t\tLEFT, RIGHT, CENTER, or a specific number of degrees" << endl;
1092     cout << "\t--visibility=xxx:  specify initial visibility in meters" << endl;
1093     cout << "\t--visibility-miles=xxx:  specify initial visibility in miles"
1094          << endl;
1095     cout << endl;
1096
1097     cout << "Scenery Options:" << endl;
1098     cout << "\t--tile-radius=n:  specify tile radius, must be 1 - 4" << endl;
1099     cout << endl;
1100
1101     cout << "Hud Options:" << endl;
1102     cout << "\t--units-feet:  Hud displays units in feet" << endl;
1103     cout << "\t--units-meters:  Hud displays units in meters" << endl;
1104     cout << "\t--hud-tris:  Hud displays number of triangles rendered" << endl;
1105     cout << "\t--hud-culled:  Hud displays percentage of triangles culled"
1106          << endl;
1107     cout << endl;
1108         
1109     cout << "Time Options:" << endl;
1110     cout << "\t--time-offset=[+-]hh:mm:ss: add this time offset" << endl;
1111     cout << "\t--time-match-real: Synchronize real-world and FlightGear" << endl
1112          << "\t\ttime. Can be used in combination with --time-offset." << endl;
1113     cout << "\t--time-match-local:Synchronize local real-world and " << endl
1114          << "\t\tFlightGear time" << endl;   
1115     cout << "\t--start-date-sys=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1116          << "\t\tdate/time. Uses your system time " << endl;
1117     cout << "\t--start-date-gmt=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1118          << "\t\tdate/time. Uses Greenwich Mean Time" << endl;
1119     cout << "\t--start-date-lat=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1120          << "\t\tdate/time. Uses Local Aircraft Time" << endl;
1121 #ifdef FG_NETWORK_OLK
1122     cout << endl;
1123
1124     cout << "Network Options:" << endl;
1125     cout << "\t--enable-network-olk:  enable Multipilot mode" << endl;
1126     cout << "\t--disable-network-olk:  disable Multipilot mode (default)" << endl;
1127     cout << "\t--net-hud:  Hud displays network info" << endl;
1128     cout << "\t--net-id=name:  specify your own callsign" << endl;
1129 #endif
1130
1131     cout << endl;
1132     cout << "Route/Way Point Options:" << endl;
1133     cout << "\t--wp=ID[@alt]:  specify a waypoint for the GC autopilot" << endl;
1134     cout << "\t\tYou can specify multiple waypoints (a route) with multiple"
1135          << endl;
1136     cout << "\t\tinstances of --wp=" << endl;
1137     cout << "\t--flight-plan=[file]: Read all waypoints from [file]" <<endl;
1138 }