]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
Also properly reinit previously set time of day upon reset.
[flightgear.git] / src / Main / fg_commands.cxx
1 // fg_commands.cxx - internal FGFS commands.
2
3 #include <string.h>             // strcmp()
4
5 #include <simgear/compiler.h>
6
7 #include STL_STRING
8 #include STL_FSTREAM
9
10 #include <simgear/sg_inlines.h>
11 #include <simgear/debug/logstream.hxx>
12 #include <simgear/math/sg_random.h>
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/structure/commands.hxx>
15 #include <simgear/props/props.hxx>
16
17 #include <Cockpit/panel.hxx>
18 #include <Cockpit/panel_io.hxx>
19 #include <FDM/flight.hxx>
20 #include <GUI/gui.h>
21 #include <GUI/new_gui.hxx>
22 #include <GUI/dialog.hxx>
23 #include <Replay/replay.hxx>
24 #include <Scenery/tilemgr.hxx>
25 #if defined(HAVE_PLIB_PSL)
26 #  include <Scripting/scriptmgr.hxx>
27 #endif
28 #include <Time/sunsolver.hxx>
29 #include <Time/tmp.hxx>
30
31 #include "fg_init.hxx"
32 #include "fg_commands.hxx"
33 #include "fg_props.hxx"
34
35 SG_USING_STD(string);
36 SG_USING_STD(ifstream);
37 SG_USING_STD(ofstream);
38
39 #include "fg_props.hxx"
40 #include "fg_io.hxx"
41 #include "globals.hxx"
42 #include "util.hxx"
43 #include "viewmgr.hxx"
44
45
46 \f
47 ////////////////////////////////////////////////////////////////////////
48 // Static helper functions.
49 ////////////////////////////////////////////////////////////////////////
50
51
52 static inline SGPropertyNode *
53 get_prop (const SGPropertyNode * arg)
54 {
55     return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
56 }
57
58 static inline SGPropertyNode *
59 get_prop2 (const SGPropertyNode * arg)
60 {
61     return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
62 }
63
64
65 /**
66  * Get a double value and split it as required.
67  */
68 static void
69 split_value (double full_value, const char * mask,
70              double * unmodifiable, double * modifiable)
71 {
72     if (!strcmp("integer", mask)) {
73         *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
74         *unmodifiable = full_value - *modifiable;
75     } else if (!strcmp("decimal", mask)) {
76         *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
77         *modifiable = full_value - *unmodifiable;
78     } else {
79         if (strcmp("all", mask))
80             SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
81                    << " assuming 'all'");
82         *unmodifiable = 0;
83         *modifiable = full_value;
84     }
85 }
86
87
88 /**
89  * Clamp or wrap a value as specified.
90  */
91 static void
92 limit_value (double * value, const SGPropertyNode * arg)
93 {
94     const SGPropertyNode * min_node = arg->getChild("min");
95     const SGPropertyNode * max_node = arg->getChild("max");
96
97     bool wrap = arg->getBoolValue("wrap");
98
99     if (min_node == 0 || max_node == 0)
100         wrap = false;
101   
102     if (wrap) {                 // wrap such that min <= x < max
103         double min_val = min_node->getDoubleValue();
104         double max_val = max_node->getDoubleValue();
105         double resolution = arg->getDoubleValue("resolution");
106         if (resolution > 0.0) {
107             // snap to (min + N*resolution), taking special care to handle imprecision
108             int n = (int)floor((*value - min_val) / resolution + 0.5);
109             int steps = (int)floor((max_val - min_val) / resolution + 0.5);
110             SG_NORMALIZE_RANGE(n, 0, steps);
111             *value = min_val + resolution * n;
112         } else {
113             // plain circular wrapping
114             SG_NORMALIZE_RANGE(*value, min_val, max_val);
115         }
116     } else {                    // clamp such that min <= x <= max
117         if ((min_node != 0) && (*value < min_node->getDoubleValue()))
118             *value = min_node->getDoubleValue();
119         else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
120             *value = max_node->getDoubleValue();
121     }
122 }
123
124 static bool
125 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
126 {
127     switch (value1->getType()) {
128     case SGPropertyNode::BOOL:
129         return (value1->getBoolValue() == value2->getBoolValue());
130     case SGPropertyNode::INT:
131         return (value1->getIntValue() == value2->getIntValue());
132     case SGPropertyNode::LONG:
133         return (value1->getLongValue() == value2->getLongValue());
134     case SGPropertyNode::FLOAT:
135         return (value1->getFloatValue() == value2->getFloatValue());
136     case SGPropertyNode::DOUBLE:
137         return (value1->getDoubleValue() == value2->getDoubleValue());
138     default:
139         return !strcmp(value1->getStringValue(), value2->getStringValue());
140     }
141 }
142
143
144 \f
145 ////////////////////////////////////////////////////////////////////////
146 // Command implementations.
147 ////////////////////////////////////////////////////////////////////////
148
149
150 /**
151  * Built-in command: do nothing.
152  */
153 static bool
154 do_null (const SGPropertyNode * arg)
155 {
156   return true;
157 }
158
159 #if defined(HAVE_PLIB_PSL)
160 /**
161  * Built-in command: run a PSL script.
162  *
163  * script: the PSL script to execute
164  */
165 static bool
166 do_script (const SGPropertyNode * arg)
167 {
168     FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
169     return mgr->run(arg->getStringValue("script"));
170 }
171 #endif // HAVE_PLIB_PSL
172
173
174 /**
175  * Built-in command: exit FlightGear.
176  *
177  * status: the exit status to return to the operating system (defaults to 0)
178  */
179 static bool
180 do_exit (const SGPropertyNode * arg)
181 {
182   SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
183   fgExit(arg->getIntValue("status", 0));
184   return true;
185 }
186
187
188 /**
189  * Built-in command: reinitialize one or more subsystems.
190  *
191  * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
192  * none is specified, reinitialize all of them.
193  */
194 static bool
195 do_reinit (const SGPropertyNode * arg)
196 {
197     bool result = true;
198
199     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
200     if (subsystems.size() == 0) {
201         globals->get_subsystem_mgr()->reinit();
202     } else {
203         for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
204             const char * name = subsystems[i]->getStringValue();
205             SGSubsystem * subsystem = globals->get_subsystem(name);
206             if (subsystem == 0) {
207                 result = false;
208                 SG_LOG( SG_GENERAL, SG_ALERT,
209                         "Subsystem " << name << "not found" );
210             } else {
211                 subsystem->reinit();
212             }
213         }
214     }
215
216     globals->get_event_mgr()->reinit();
217
218     return result;
219 }
220
221 /**
222  * Built-in command: suspend one or more subsystems.
223  *
224  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
225  */
226 static bool
227 do_suspend (const SGPropertyNode * arg)
228 {
229     bool result = true;
230
231     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
232     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
233         const char * name = subsystems[i]->getStringValue();
234         SGSubsystem * subsystem = globals->get_subsystem(name);
235         if (subsystem == 0) {
236             result = false;
237             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
238         } else {
239             subsystem->suspend();
240         }
241     }
242     return result;
243 }
244
245 /**
246  * Built-in command: suspend one or more subsystems.
247  *
248  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
249  */
250 static bool
251 do_resume (const SGPropertyNode * arg)
252 {
253     bool result = true;
254
255     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
256     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
257         const char * name = subsystems[i]->getStringValue();
258         SGSubsystem * subsystem = globals->get_subsystem(name);
259         if (subsystem == 0) {
260             result = false;
261             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
262         } else {
263             subsystem->resume();
264         }
265     }
266     return result;
267 }
268
269
270 /**
271  * Built-in command: load flight.
272  *
273  * file (optional): the name of the file to load (relative to current
274  *   directory).  Defaults to "fgfs.sav"
275  */
276 static bool
277 do_load (const SGPropertyNode * arg)
278 {
279   const string &file = arg->getStringValue("file", "fgfs.sav");
280   ifstream input(file.c_str());
281   if (input.good() && fgLoadFlight(input)) {
282     input.close();
283     SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
284     return true;
285   } else {
286     SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
287     return false;
288   }
289 }
290
291
292 /**
293  * Built-in command: save flight.
294  *
295  * file (optional): the name of the file to save (relative to the
296  * current directory).  Defaults to "fgfs.sav".
297  */
298 static bool
299 do_save (const SGPropertyNode * arg)
300 {
301   const string &file = arg->getStringValue("file", "fgfs.sav");
302   bool write_all = arg->getBoolValue("write-all", false);
303   SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
304   ofstream output(file.c_str());
305   if (output.good() && fgSaveFlight(output, write_all)) {
306     output.close();
307     SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
308     return true;
309   } else {
310     SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
311     return false;
312   }
313 }
314
315
316 /**
317  * Built-in command: (re)load the panel.
318  *
319  * path (optional): the file name to load the panel from 
320  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
321  * and if that's unspecified, to "Panels/Default/default.xml".
322  */
323 static bool
324 do_panel_load (const SGPropertyNode * arg)
325 {
326   string panel_path =
327     arg->getStringValue("path",
328                         fgGetString("/sim/panel/path",
329                                     "Panels/Default/default.xml"));
330   FGPanel * new_panel = fgReadPanel(panel_path);
331   if (new_panel == 0) {
332     SG_LOG(SG_INPUT, SG_ALERT,
333            "Error reading new panel from " << panel_path);
334     return false;
335   }
336   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
337   globals->get_current_panel()->unbind();
338   delete globals->get_current_panel();
339   globals->set_current_panel( new_panel );
340   globals->get_current_panel()->bind();
341   return true;
342 }
343
344
345 /**
346  * Built-in command: pass a mouse click to the panel.
347  *
348  * button: the mouse button number, zero-based.
349  * is-down: true if the button is down, false if it is up.
350  * x-pos: the x position of the mouse click.
351  * y-pos: the y position of the mouse click.
352  */
353 static bool
354 do_panel_mouse_click (const SGPropertyNode * arg)
355 {
356   if (globals->get_current_panel() != 0)
357     return globals->get_current_panel()
358       ->doMouseAction(arg->getIntValue("button"),
359                       arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
360                       arg->getIntValue("x-pos"),
361                       arg->getIntValue("y-pos"));
362   else
363     return false;
364 }
365
366
367 /**
368  * Built-in command: (re)load preferences.
369  *
370  * path (optional): the file name to load the panel from (relative
371  * to FG_ROOT). Defaults to "preferences.xml".
372  */
373 static bool
374 do_preferences_load (const SGPropertyNode * arg)
375 {
376   try {
377     fgLoadProps(arg->getStringValue("path", "preferences.xml"),
378                 globals->get_props());
379   } catch (const sg_exception &e) {
380     guiErrorMessage("Error reading global preferences: ", e);
381     return false;
382   }
383   SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
384   return true;
385 }
386
387
388 static void
389 fix_hud_visibility()
390 {
391   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
392       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
393       if ( globals->get_viewmgr()->get_current() == 1 ) {
394           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
395       }
396   }
397 }
398
399 static void
400 do_view_next( bool )
401 {
402     globals->get_current_view()->setHeadingOffset_deg(0.0);
403     globals->get_viewmgr()->next_view();
404     fix_hud_visibility();
405     globals->get_tile_mgr()->refresh_view_timestamps();
406 }
407
408 static void
409 do_view_prev( bool )
410 {
411     globals->get_current_view()->setHeadingOffset_deg(0.0);
412     globals->get_viewmgr()->prev_view();
413     fix_hud_visibility();
414     globals->get_tile_mgr()->refresh_view_timestamps();
415 }
416
417 /**
418  * Built-in command: cycle view.
419  */
420 static bool
421 do_view_cycle (const SGPropertyNode * arg)
422 {
423   globals->get_current_view()->setHeadingOffset_deg(0.0);
424   globals->get_viewmgr()->next_view();
425   fix_hud_visibility();
426   globals->get_tile_mgr()->refresh_view_timestamps();
427 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
428   return true;
429 }
430
431 /**
432  * Built-in command: capture screen.
433  */
434 static bool
435 do_screen_capture (const SGPropertyNode * arg)
436 {
437   fgDumpSnapShot();
438   return true;
439 }
440
441
442 /**
443  * Reload the tile cache.
444  */
445 static bool
446 do_tile_cache_reload (const SGPropertyNode * arg)
447 {
448     static const SGPropertyNode *master_freeze
449         = fgGetNode("/sim/freeze/master");
450     bool freeze = master_freeze->getBoolValue();
451     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
452     if ( !freeze ) {
453         fgSetBool("/sim/freeze/master", true);
454     }
455     // BusyCursor(0);
456     if ( globals->get_tile_mgr()->init() ) {
457         // Load the local scenery data
458         double visibility_meters = fgGetDouble("/environment/visibility-m");
459         globals->get_tile_mgr()->update( visibility_meters );
460     } else {
461         SG_LOG( SG_GENERAL, SG_ALERT, 
462                 "Error in Tile Manager initialization!" );
463         exit(-1);
464     }
465     // BusyCursor(1);
466     if ( !freeze ) {
467         fgSetBool("/sim/freeze/master", false);
468     }
469     return true;
470 }
471
472
473 /**
474  * Update the lighting manually.
475  */
476 static bool
477 do_timeofday (const SGPropertyNode * arg)
478 {
479     const string &offset_type = arg->getStringValue("timeofday", "noon");
480
481     static const SGPropertyNode *longitude
482         = fgGetNode("/position/longitude-deg");
483     static const SGPropertyNode *latitude
484         = fgGetNode("/position/latitude-deg");
485     static const SGPropertyNode *cur_time_override
486         = fgGetNode("/sim/time/cur-time-override", true);
487
488     int orig_warp = globals->get_warp();
489     SGTime *t = globals->get_time_params();
490     time_t cur_time = t->get_cur_time();
491     // cout << "cur_time = " << cur_time << endl;
492     // cout << "orig_warp = " << orig_warp << endl;
493
494     int warp = 0;
495     if ( offset_type == "real" ) {
496         warp = -orig_warp;
497     } else if ( offset_type == "dawn" ) {
498         warp = fgTimeSecondsUntilSunAngle( cur_time,
499                                            longitude->getDoubleValue()
500                                              * SGD_DEGREES_TO_RADIANS,
501                                            latitude->getDoubleValue()
502                                              * SGD_DEGREES_TO_RADIANS,
503                                            90.0, true ); 
504     } else if ( offset_type == "morning" ) {
505         warp = fgTimeSecondsUntilSunAngle( cur_time,
506                                            longitude->getDoubleValue()
507                                              * SGD_DEGREES_TO_RADIANS,
508                                            latitude->getDoubleValue()
509                                              * SGD_DEGREES_TO_RADIANS,
510                                            75.0, true ); 
511     } else if ( offset_type == "noon" ) {
512         warp = fgTimeSecondsUntilSunAngle( cur_time,
513                                            longitude->getDoubleValue()
514                                              * SGD_DEGREES_TO_RADIANS,
515                                            latitude->getDoubleValue()
516                                              * SGD_DEGREES_TO_RADIANS,
517                                            0.0, true ); 
518     } else if ( offset_type == "afternoon" ) {
519         warp = fgTimeSecondsUntilSunAngle( cur_time,
520                                            longitude->getDoubleValue()
521                                              * SGD_DEGREES_TO_RADIANS,
522                                            latitude->getDoubleValue()
523                                              * SGD_DEGREES_TO_RADIANS,
524                                            60.0, false ); 
525      } else if ( offset_type == "dusk" ) {
526         warp = fgTimeSecondsUntilSunAngle( cur_time,
527                                            longitude->getDoubleValue()
528                                              * SGD_DEGREES_TO_RADIANS,
529                                            latitude->getDoubleValue()
530                                              * SGD_DEGREES_TO_RADIANS,
531                                            90.0, false ); 
532      } else if ( offset_type == "evening" ) {
533         warp = fgTimeSecondsUntilSunAngle( cur_time,
534                                            longitude->getDoubleValue()
535                                              * SGD_DEGREES_TO_RADIANS,
536                                            latitude->getDoubleValue()
537                                              * SGD_DEGREES_TO_RADIANS,
538                                            100.0, false ); 
539     } else if ( offset_type == "midnight" ) {
540         warp = fgTimeSecondsUntilSunAngle( cur_time,
541                                            longitude->getDoubleValue()
542                                              * SGD_DEGREES_TO_RADIANS,
543                                            latitude->getDoubleValue()
544                                              * SGD_DEGREES_TO_RADIANS,
545                                            180.0, false ); 
546     }
547     // cout << "warp = " << warp << endl;
548     globals->set_warp( orig_warp + warp );
549
550     t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
551                latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
552                cur_time_override->getLongValue(),
553                globals->get_warp() );
554
555     return true;
556 }
557
558
559 /**
560  * Built-in command: toggle a bool property value.
561  *
562  * property: The name of the property to toggle.
563  */
564 static bool
565 do_property_toggle (const SGPropertyNode * arg)
566 {
567   SGPropertyNode * prop = get_prop(arg);
568   return prop->setBoolValue(!prop->getBoolValue());
569 }
570
571
572 /**
573  * Built-in command: assign a value to a property.
574  *
575  * property: the name of the property to assign.
576  * value: the value to assign; or
577  * property[1]: the property to copy from.
578  */
579 static bool
580 do_property_assign (const SGPropertyNode * arg)
581 {
582   SGPropertyNode * prop = get_prop(arg);
583   const SGPropertyNode * prop2 = get_prop2(arg);
584   const SGPropertyNode * value = arg->getNode("value");
585
586   if (value != 0)
587       return prop->setUnspecifiedValue(value->getStringValue());
588   else if (prop2)
589       return prop->setUnspecifiedValue(prop2->getStringValue());
590   else
591       return false;
592 }
593
594
595 /**
596  * Built-in command: increment or decrement a property value.
597  *
598  * If the 'step' argument is present, it will be used; otherwise,
599  * the command uses 'offset' and 'factor', usually from the mouse.
600  *
601  * property: the name of the property to increment or decrement.
602  * step: the amount of the increment or decrement (default: 0).
603  * offset: offset from the current setting (used for the mouse; multiplied 
604  *         by factor)
605  * factor: scaling amount for the offset (defaults to 1).
606  * min: the minimum allowed value (default: no minimum).
607  * max: the maximum allowed value (default: no maximum).
608  * mask: 'integer' to apply only to the left of the decimal point, 
609  *       'decimal' to apply only to the right of the decimal point,
610  *       or 'all' to apply to the whole number (the default).
611  * wrap: true if the value should be wrapped when it passes min or max;
612  *       both min and max must be present for this to work (default:
613  *       false).
614  */
615 static bool
616 do_property_adjust (const SGPropertyNode * arg)
617 {
618   SGPropertyNode * prop = get_prop(arg);
619
620   double amount = 0;
621   if (arg->hasValue("step"))
622       amount = arg->getDoubleValue("step");
623   else
624       amount = (arg->getDoubleValue("factor", 1)
625                 * arg->getDoubleValue("offset"));
626           
627   double unmodifiable, modifiable;
628   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
629               &unmodifiable, &modifiable);
630   modifiable += amount;
631   limit_value(&modifiable, arg);
632
633   prop->setDoubleValue(unmodifiable + modifiable);
634
635   return true;
636 }
637
638
639 /**
640  * Built-in command: multiply a property value.
641  *
642  * property: the name of the property to multiply.
643  * factor: the amount by which to multiply.
644  * min: the minimum allowed value (default: no minimum).
645  * max: the maximum allowed value (default: no maximum).
646  * mask: 'integer' to apply only to the left of the decimal point, 
647  *       'decimal' to apply only to the right of the decimal point,
648  *       or 'all' to apply to the whole number (the default).
649  * wrap: true if the value should be wrapped when it passes min or max;
650  *       both min and max must be present for this to work (default:
651  *       false).
652  */
653 static bool
654 do_property_multiply (const SGPropertyNode * arg)
655 {
656   SGPropertyNode * prop = get_prop(arg);
657   double factor = arg->getDoubleValue("factor", 1);
658
659   double unmodifiable, modifiable;
660   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
661               &unmodifiable, &modifiable);
662   modifiable *= factor;
663   limit_value(&modifiable, arg);
664
665   prop->setDoubleValue(unmodifiable + modifiable);
666
667   return true;
668 }
669
670
671 /**
672  * Built-in command: swap two property values.
673  *
674  * property[0]: the name of the first property.
675  * property[1]: the name of the second property.
676  */
677 static bool
678 do_property_swap (const SGPropertyNode * arg)
679 {
680   SGPropertyNode * prop1 = get_prop(arg);
681   SGPropertyNode * prop2 = get_prop2(arg);
682
683                                 // FIXME: inefficient
684   const string & tmp = prop1->getStringValue();
685   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
686           prop2->setUnspecifiedValue(tmp.c_str()));
687 }
688
689
690 /**
691  * Built-in command: Set a property to an axis or other moving input.
692  *
693  * property: the name of the property to set.
694  * setting: the current input setting, usually between -1.0 and 1.0.
695  * offset: the offset to shift by, before applying the factor.
696  * factor: the factor to multiply by (use negative to reverse).
697  */
698 static bool
699 do_property_scale (const SGPropertyNode * arg)
700 {
701   SGPropertyNode * prop = get_prop(arg);
702   double setting = arg->getDoubleValue("setting");
703   double offset = arg->getDoubleValue("offset", 0.0);
704   double factor = arg->getDoubleValue("factor", 1.0);
705   bool squared = arg->getBoolValue("squared", false);
706   int power = arg->getIntValue("power", (squared ? 2 : 1));
707
708   int sign = (setting < 0 ? -1 : 1);
709
710   switch (power) {
711   case 1:
712       break;
713   case 2:
714       setting = setting * setting * sign;
715       break;
716   case 3:
717       setting = setting * setting * setting;
718       break;
719   case 4:
720       setting = setting * setting * setting * setting * sign;
721       break;
722   default:
723       setting =  pow(setting, power);
724       if ((power % 2) == 0)
725           setting *= sign;
726       break;
727   }
728
729   return prop->setDoubleValue((setting + offset) * factor);
730 }
731
732
733 /**
734  * Built-in command: cycle a property through a set of values.
735  *
736  * If the current value isn't in the list, the cycle will
737  * (re)start from the beginning.
738  *
739  * property: the name of the property to cycle.
740  * value[*]: the list of values to cycle through.
741  */
742 static bool
743 do_property_cycle (const SGPropertyNode * arg)
744 {
745     SGPropertyNode * prop = get_prop(arg);
746     vector<SGPropertyNode_ptr> values = arg->getChildren("value");
747     int selection = -1;
748     int nSelections = values.size();
749
750     if (nSelections < 1) {
751         SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
752         return false;
753     }
754
755                                 // Try to find the current selection
756     for (int i = 0; i < nSelections; i++) {
757         if (compare_values(prop, values[i])) {
758             selection = i + 1;
759             break;
760         }
761     }
762
763                                 // Default or wrap to the first selection
764     if (selection < 0 || selection >= nSelections)
765         selection = 0;
766
767     prop->setUnspecifiedValue(values[selection]->getStringValue());
768     return true;
769 }
770
771
772 /**
773  * Built-in command: randomize a numeric property value.
774  *
775  * property: the name of the property value to randomize.
776  * min: the minimum allowed value.
777  * max: the maximum allowed value.
778  */
779 static bool
780 do_property_randomize (const SGPropertyNode * arg)
781 {
782     SGPropertyNode * prop = get_prop(arg);
783     double min = arg->getDoubleValue("min", DBL_MIN);
784     double max = arg->getDoubleValue("max", DBL_MAX);
785     prop->setDoubleValue(sg_random() * (max - min) + min);
786     return true;
787 }
788
789
790 /**
791  * Built-in command: Show an XML-configured dialog.
792  *
793  * dialog-name: the name of the GUI dialog to display.
794  */
795 static bool
796 do_dialog_show (const SGPropertyNode * arg)
797 {
798     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
799     gui->showDialog(arg->getStringValue("dialog-name"));
800     return true;
801 }
802
803
804 /**
805  * Built-in Command: Hide the active XML-configured dialog.
806  */
807 static bool
808 do_dialog_close (const SGPropertyNode * arg)
809 {
810     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
811     return gui->closeActiveDialog();
812 }
813
814
815 /**
816  * Update a value in the active XML-configured dialog.
817  *
818  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
819  */
820 static bool
821 do_dialog_update (const SGPropertyNode * arg)
822 {
823     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
824     FGDialog * dialog = gui->getActiveDialog();
825     if (dialog != 0) {
826         if (arg->hasValue("object-name")) {
827             dialog->updateValue(arg->getStringValue("object-name"));
828         } else {
829             dialog->updateValues();
830         }
831         return true;
832     } else {
833         return false;
834     }
835 }
836
837
838 /**
839  * Apply a value in the active XML-configured dialog.
840  *
841  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
842  */
843 static bool
844 do_dialog_apply (const SGPropertyNode * arg)
845 {
846     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
847     FGDialog * dialog = gui->getActiveDialog();
848     if (dialog != 0) {
849         if (arg->hasValue("object-name")) {
850             const char * name = arg->getStringValue("object-name");
851             dialog->applyValue(name);
852             dialog->updateValue(name);
853         } else {
854             dialog->applyValues();
855             dialog->updateValues();
856         }
857         return true;
858     } else {
859         return false;
860     }
861 }
862
863
864 /**
865  * Built-in command: commit presets (read from in /sim/presets/)
866  */
867 static bool
868 do_presets_commit (const SGPropertyNode * arg)
869 {
870     // unbind the current fdm state so property changes
871     // don't get lost when we subsequently delete this fdm
872     // and create a new one.
873     cur_fdm_state->unbind();
874         
875     // set position from presets
876     fgInitPosition();
877
878     // BusyCursor(0);
879     fgReInitSubsystems();
880
881     globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
882
883 #if 0
884     if ( ! fgGetBool("/sim/presets/onground") ) {
885         fgSetBool( "/sim/freeze/master", true );
886         fgSetBool( "/sim/freeze/clock", true );
887     }
888 #endif
889
890     return true;
891 }
892
893 /**
894  * Built-in command: set log level (0 ... 7)
895  */
896 static bool
897 do_log_level (const SGPropertyNode * arg)
898 {
899    sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
900
901    return true;
902 }
903
904 /**
905  * Built-in command: replay the FDR buffer
906  */
907 static bool
908 do_replay (const SGPropertyNode * arg)
909 {
910     // freeze the master fdm
911     fgSetBool( "/sim/freeze/replay", true );
912
913     FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
914
915     fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
916     fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
917     double duration = fgGetDouble( "/sim/replay/duration" );
918     if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
919         fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
920     } else {
921         fgSetDouble( "/sim/replay/time", r->get_start_time() );
922     }
923
924     cout << "start = " << r->get_start_time()
925          << "  end = " << r->get_end_time() << endl;
926
927     return true;
928 }
929
930
931
932 static bool
933 do_decrease_visibility (const SGPropertyNode * arg)
934 {
935     double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
936     fgSetDouble("/environment/visibility-m", new_value);
937     fgDefaultWeatherValue("visibility-m", new_value);
938     globals->get_subsystem("environment")->reinit();
939
940     return true;
941 }
942  
943 static bool
944 do_increase_visibility (const SGPropertyNode * arg)
945 {
946     double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
947     fgSetDouble("/environment/visibility-m", new_value);
948     fgDefaultWeatherValue("visibility-m", new_value);
949     globals->get_subsystem("environment")->reinit();
950
951     return true;
952 }
953
954
955 ////////////////////////////////////////////////////////////////////////
956 // Command setup.
957 ////////////////////////////////////////////////////////////////////////
958
959
960 /**
961  * Table of built-in commands.
962  *
963  * New commands do not have to be added here; any module in the application
964  * can add a new command using globals->get_commands()->addCommand(...).
965  */
966 static struct {
967   const char * name;
968   SGCommandMgr::command_t command;
969 } built_ins [] = {
970     { "null", do_null },
971 #if defined(HAVE_PLIB_PSL)
972     { "script", do_script },
973 #endif // HAVE_PLIB_PSL
974     { "exit", do_exit },
975     { "reinit", do_reinit },
976     { "suspend", do_reinit },
977     { "resume", do_reinit },
978     { "load", do_load },
979     { "save", do_save },
980     { "panel-load", do_panel_load },
981     { "panel-mouse-click", do_panel_mouse_click },
982     { "preferences-load", do_preferences_load },
983     { "view-cycle", do_view_cycle },
984     { "screen-capture", do_screen_capture },
985     { "tile-cache-reload", do_tile_cache_reload },
986     { "timeofday", do_timeofday },
987     { "property-toggle", do_property_toggle },
988     { "property-assign", do_property_assign },
989     { "property-adjust", do_property_adjust },
990     { "property-multiply", do_property_multiply },
991     { "property-swap", do_property_swap },
992     { "property-scale", do_property_scale },
993     { "property-cycle", do_property_cycle },
994     { "property-randomize", do_property_randomize },
995     { "dialog-show", do_dialog_show },
996     { "dialog-close", do_dialog_close },
997     { "dialog-show", do_dialog_show },
998     { "dialog-update", do_dialog_update },
999     { "dialog-apply", do_dialog_apply },
1000     { "presets-commit", do_presets_commit },
1001     { "log-level", do_log_level },
1002     { "replay", do_replay },
1003     { "decrease-visibility", do_decrease_visibility },
1004     { "increase-visibility", do_increase_visibility },
1005     { 0, 0 }                    // zero-terminated
1006 };
1007
1008
1009 /**
1010  * Initialize the default built-in commands.
1011  *
1012  * Other commands may be added by other parts of the application.
1013  */
1014 void
1015 fgInitCommands ()
1016 {
1017   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1018   for (int i = 0; built_ins[i].name != 0; i++) {
1019     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
1020     globals->get_commands()->addCommand(built_ins[i].name,
1021                                         built_ins[i].command);
1022   }
1023
1024   typedef bool (*dummy)();
1025   fgTie( "/command/view/next", dummy(0), do_view_next );
1026   fgTie( "/command/view/prev", dummy(0), do_view_prev );
1027 }
1028
1029 // end of fg_commands.cxx