1 // fg_commands.cxx - internal FGFS commands.
3 #include <string.h> // strcmp()
5 #include <simgear/compiler.h>
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>
17 #include <Cockpit/panel.hxx>
18 #include <Cockpit/panel_io.hxx>
19 #include <Environment/environment.hxx>
20 #include <FDM/flight.hxx>
22 #include <GUI/new_gui.hxx>
23 #include <GUI/dialog.hxx>
24 #include <Replay/replay.hxx>
25 #include <Scenery/tilemgr.hxx>
26 #if defined(HAVE_PLIB_PSL)
27 # include <Scripting/scriptmgr.hxx>
29 #include <Scripting/NasalSys.hxx>
30 #include <Time/sunsolver.hxx>
31 #include <Time/tmp.hxx>
33 #include "fg_init.hxx"
35 #include "fg_commands.hxx"
36 #include "fg_props.hxx"
37 #include "globals.hxx"
40 #include "viewmgr.hxx"
43 SG_USING_STD(ifstream);
44 SG_USING_STD(ofstream);
48 ////////////////////////////////////////////////////////////////////////
49 // Static helper functions.
50 ////////////////////////////////////////////////////////////////////////
53 static inline SGPropertyNode *
54 get_prop (const SGPropertyNode * arg)
56 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
59 static inline SGPropertyNode *
60 get_prop2 (const SGPropertyNode * arg)
62 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
67 * Get a double value and split it as required.
70 split_value (double full_value, const char * mask,
71 double * unmodifiable, double * modifiable)
73 if (!strcmp("integer", mask)) {
74 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
75 *unmodifiable = full_value - *modifiable;
76 } else if (!strcmp("decimal", mask)) {
77 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
78 *modifiable = full_value - *unmodifiable;
80 if (strcmp("all", mask))
81 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
82 << " assuming 'all'");
84 *modifiable = full_value;
90 * Clamp or wrap a value as specified.
93 limit_value (double * value, const SGPropertyNode * arg)
95 const SGPropertyNode * min_node = arg->getChild("min");
96 const SGPropertyNode * max_node = arg->getChild("max");
98 bool wrap = arg->getBoolValue("wrap");
100 if (min_node == 0 || max_node == 0)
103 if (wrap) { // wrap such that min <= x < max
104 double min_val = min_node->getDoubleValue();
105 double max_val = max_node->getDoubleValue();
106 double resolution = arg->getDoubleValue("resolution");
107 if (resolution > 0.0) {
108 // snap to (min + N*resolution), taking special care to handle imprecision
109 int n = (int)floor((*value - min_val) / resolution + 0.5);
110 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
111 SG_NORMALIZE_RANGE(n, 0, steps);
112 *value = min_val + resolution * n;
114 // plain circular wrapping
115 SG_NORMALIZE_RANGE(*value, min_val, max_val);
117 } else { // clamp such that min <= x <= max
118 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
119 *value = min_node->getDoubleValue();
120 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
121 *value = max_node->getDoubleValue();
126 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
128 switch (value1->getType()) {
129 case SGPropertyNode::BOOL:
130 return (value1->getBoolValue() == value2->getBoolValue());
131 case SGPropertyNode::INT:
132 return (value1->getIntValue() == value2->getIntValue());
133 case SGPropertyNode::LONG:
134 return (value1->getLongValue() == value2->getLongValue());
135 case SGPropertyNode::FLOAT:
136 return (value1->getFloatValue() == value2->getFloatValue());
137 case SGPropertyNode::DOUBLE:
138 return (value1->getDoubleValue() == value2->getDoubleValue());
140 return !strcmp(value1->getStringValue(), value2->getStringValue());
146 ////////////////////////////////////////////////////////////////////////
147 // Command implementations.
148 ////////////////////////////////////////////////////////////////////////
152 * Built-in command: do nothing.
155 do_null (const SGPropertyNode * arg)
160 #if defined(HAVE_PLIB_PSL)
162 * Built-in command: run a PSL script.
164 * script: the PSL script to execute
167 do_script (const SGPropertyNode * arg)
169 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
170 return mgr->run(arg->getStringValue("script"));
172 #endif // HAVE_PLIB_PSL
175 * Built-in command: run a Nasal script.
178 do_nasal (const SGPropertyNode * arg)
180 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
184 * Built-in command: exit FlightGear.
186 * status: the exit status to return to the operating system (defaults to 0)
189 do_exit (const SGPropertyNode * arg)
191 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
192 fgExit(arg->getIntValue("status", 0));
198 * Built-in command: reinitialize one or more subsystems.
200 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
201 * none is specified, reinitialize all of them.
204 do_reinit (const SGPropertyNode * arg)
208 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
209 if (subsystems.size() == 0) {
210 globals->get_subsystem_mgr()->reinit();
212 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
213 const char * name = subsystems[i]->getStringValue();
214 SGSubsystem * subsystem = globals->get_subsystem(name);
215 if (subsystem == 0) {
217 SG_LOG( SG_GENERAL, SG_ALERT,
218 "Subsystem " << name << "not found" );
225 globals->get_event_mgr()->reinit();
232 // these routines look useful ??? but are never used in the code ???
236 * Built-in command: suspend one or more subsystems.
238 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
241 do_suspend (const SGPropertyNode * arg)
245 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
246 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
247 const char * name = subsystems[i]->getStringValue();
248 SGSubsystem * subsystem = globals->get_subsystem(name);
249 if (subsystem == 0) {
251 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
253 subsystem->suspend();
260 * Built-in command: suspend one or more subsystems.
262 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
265 do_resume (const SGPropertyNode * arg)
269 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
270 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
271 const char * name = subsystems[i]->getStringValue();
272 SGSubsystem * subsystem = globals->get_subsystem(name);
273 if (subsystem == 0) {
275 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
287 * Built-in command: load flight.
289 * file (optional): the name of the file to load (relative to current
290 * directory). Defaults to "fgfs.sav"
293 do_load (const SGPropertyNode * arg)
295 const string &file = arg->getStringValue("file", "fgfs.sav");
296 ifstream input(file.c_str());
297 if (input.good() && fgLoadFlight(input)) {
299 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
302 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
309 * Built-in command: save flight.
311 * file (optional): the name of the file to save (relative to the
312 * current directory). Defaults to "fgfs.sav".
315 do_save (const SGPropertyNode * arg)
317 const string &file = arg->getStringValue("file", "fgfs.sav");
318 bool write_all = arg->getBoolValue("write-all", false);
319 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
320 ofstream output(file.c_str());
321 if (output.good() && fgSaveFlight(output, write_all)) {
323 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
326 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
333 * Built-in command: (re)load the panel.
335 * path (optional): the file name to load the panel from
336 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
337 * and if that's unspecified, to "Panels/Default/default.xml".
340 do_panel_load (const SGPropertyNode * arg)
343 arg->getStringValue("path",
344 fgGetString("/sim/panel/path",
345 "Panels/Default/default.xml"));
346 FGPanel * new_panel = fgReadPanel(panel_path);
347 if (new_panel == 0) {
348 SG_LOG(SG_INPUT, SG_ALERT,
349 "Error reading new panel from " << panel_path);
352 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
353 globals->get_current_panel()->unbind();
354 delete globals->get_current_panel();
355 globals->set_current_panel( new_panel );
356 globals->get_current_panel()->bind();
362 * Built-in command: pass a mouse click to the panel.
364 * button: the mouse button number, zero-based.
365 * is-down: true if the button is down, false if it is up.
366 * x-pos: the x position of the mouse click.
367 * y-pos: the y position of the mouse click.
370 do_panel_mouse_click (const SGPropertyNode * arg)
372 if (globals->get_current_panel() != 0)
373 return globals->get_current_panel()
374 ->doMouseAction(arg->getIntValue("button"),
375 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
376 arg->getIntValue("x-pos"),
377 arg->getIntValue("y-pos"));
384 * Built-in command: (re)load preferences.
386 * path (optional): the file name to load the panel from (relative
387 * to FG_ROOT). Defaults to "preferences.xml".
390 do_preferences_load (const SGPropertyNode * arg)
393 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
394 globals->get_props());
395 } catch (const sg_exception &e) {
396 guiErrorMessage("Error reading global preferences: ", e);
399 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
407 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
408 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
409 if ( globals->get_viewmgr()->get_current() == 1 ) {
410 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
418 globals->get_current_view()->setHeadingOffset_deg(0.0);
419 globals->get_viewmgr()->next_view();
420 fix_hud_visibility();
421 globals->get_tile_mgr()->refresh_view_timestamps();
427 globals->get_current_view()->setHeadingOffset_deg(0.0);
428 globals->get_viewmgr()->prev_view();
429 fix_hud_visibility();
430 globals->get_tile_mgr()->refresh_view_timestamps();
434 * Built-in command: cycle view.
437 do_view_cycle (const SGPropertyNode * arg)
439 globals->get_current_view()->setHeadingOffset_deg(0.0);
440 globals->get_viewmgr()->next_view();
441 fix_hud_visibility();
442 globals->get_tile_mgr()->refresh_view_timestamps();
443 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
448 * Built-in command: capture screen.
451 do_screen_capture (const SGPropertyNode * arg)
459 * Reload the tile cache.
462 do_tile_cache_reload (const SGPropertyNode * arg)
464 static const SGPropertyNode *master_freeze
465 = fgGetNode("/sim/freeze/master");
466 bool freeze = master_freeze->getBoolValue();
467 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
469 fgSetBool("/sim/freeze/master", true);
472 if ( globals->get_tile_mgr()->init() ) {
473 // Load the local scenery data
474 double visibility_meters = fgGetDouble("/environment/visibility-m");
475 globals->get_tile_mgr()->update( visibility_meters );
477 SG_LOG( SG_GENERAL, SG_ALERT,
478 "Error in Tile Manager initialization!" );
483 fgSetBool("/sim/freeze/master", false);
490 * Set the sea level outside air temperature and assigning that to all
491 * boundary and aloft environment layers.
494 do_set_sea_level_degc (const SGPropertyNode * arg)
496 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
498 SGPropertyNode *node, *child;
501 node = fgGetNode( "/environment/config/boundary" );
502 if ( node != NULL ) {
504 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
505 child->setDoubleValue( "temperature-sea-level-degc",
506 temp_sea_level_degc );
512 node = fgGetNode( "/environment/config/aloft" );
513 if ( node != NULL ) {
515 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
516 child->setDoubleValue( "temperature-sea-level-degc",
517 temp_sea_level_degc );
527 * Set the outside air temperature at the "current" altitude by first
528 * calculating the corresponding sea level temp, and assigning that to
529 * all boundary and aloft environment layers.
532 do_set_oat_degc (const SGPropertyNode * arg)
534 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
536 // check for an altitude specified in the arguments, otherwise use
537 // current aircraft altitude.
538 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
539 if ( altitude_ft == NULL ) {
540 altitude_ft = fgGetNode("/position/altitude-ft");
543 FGEnvironment dummy; // instantiate a dummy so we can leech a method
544 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
545 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
546 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
548 cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
549 cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
550 cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
552 SGPropertyNode *node, *child;
555 node = fgGetNode( "/environment/config/boundary" );
556 if ( node != NULL ) {
558 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
559 child->setDoubleValue( "temperature-sea-level-degc",
560 temp_sea_level_degc );
566 node = fgGetNode( "/environment/config/aloft" );
567 if ( node != NULL ) {
569 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
570 child->setDoubleValue( "temperature-sea-level-degc",
571 temp_sea_level_degc );
580 * Set the sea level outside air dewpoint and assigning that to all
581 * boundary and aloft environment layers.
584 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
586 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
588 SGPropertyNode *node, *child;
591 node = fgGetNode( "/environment/config/boundary" );
592 if ( node != NULL ) {
594 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
595 child->setDoubleValue( "dewpoint-sea-level-degc",
596 dewpoint_sea_level_degc );
602 node = fgGetNode( "/environment/config/aloft" );
603 if ( node != NULL ) {
605 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
606 child->setDoubleValue( "dewpoint-sea-level-degc",
607 dewpoint_sea_level_degc );
617 * Set the outside air dewpoint at the "current" altitude by first
618 * calculating the corresponding sea level dewpoint, and assigning
619 * that to all boundary and aloft environment layers.
622 do_set_dewpoint_degc (const SGPropertyNode * arg)
624 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
626 // check for an altitude specified in the arguments, otherwise use
627 // current aircraft altitude.
628 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
629 if ( altitude_ft == NULL ) {
630 altitude_ft = fgGetNode("/position/altitude-ft");
633 FGEnvironment dummy; // instantiate a dummy so we can leech a method
634 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
635 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
636 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
638 cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
639 cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
640 cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
642 SGPropertyNode *node, *child;
645 node = fgGetNode( "/environment/config/boundary" );
646 if ( node != NULL ) {
648 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
649 child->setDoubleValue( "dewpoint-sea-level-degc",
650 dewpoint_sea_level_degc );
656 node = fgGetNode( "/environment/config/aloft" );
657 if ( node != NULL ) {
659 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
660 child->setDoubleValue( "dewpoint-sea-level-degc",
661 dewpoint_sea_level_degc );
670 * Update the lighting manually.
673 do_timeofday (const SGPropertyNode * arg)
675 const string &offset_type = arg->getStringValue("timeofday", "noon");
677 static const SGPropertyNode *longitude
678 = fgGetNode("/position/longitude-deg");
679 static const SGPropertyNode *latitude
680 = fgGetNode("/position/latitude-deg");
681 static const SGPropertyNode *cur_time_override
682 = fgGetNode("/sim/time/cur-time-override", true);
684 int orig_warp = globals->get_warp();
685 SGTime *t = globals->get_time_params();
686 time_t cur_time = t->get_cur_time();
687 // cout << "cur_time = " << cur_time << endl;
688 // cout << "orig_warp = " << orig_warp << endl;
691 if ( offset_type == "real" ) {
693 } else if ( offset_type == "dawn" ) {
694 warp = fgTimeSecondsUntilSunAngle( cur_time,
695 longitude->getDoubleValue()
696 * SGD_DEGREES_TO_RADIANS,
697 latitude->getDoubleValue()
698 * SGD_DEGREES_TO_RADIANS,
700 } else if ( offset_type == "morning" ) {
701 warp = fgTimeSecondsUntilSunAngle( cur_time,
702 longitude->getDoubleValue()
703 * SGD_DEGREES_TO_RADIANS,
704 latitude->getDoubleValue()
705 * SGD_DEGREES_TO_RADIANS,
707 } else if ( offset_type == "noon" ) {
708 warp = fgTimeSecondsUntilSunAngle( cur_time,
709 longitude->getDoubleValue()
710 * SGD_DEGREES_TO_RADIANS,
711 latitude->getDoubleValue()
712 * SGD_DEGREES_TO_RADIANS,
714 } else if ( offset_type == "afternoon" ) {
715 warp = fgTimeSecondsUntilSunAngle( cur_time,
716 longitude->getDoubleValue()
717 * SGD_DEGREES_TO_RADIANS,
718 latitude->getDoubleValue()
719 * SGD_DEGREES_TO_RADIANS,
721 } else if ( offset_type == "dusk" ) {
722 warp = fgTimeSecondsUntilSunAngle( cur_time,
723 longitude->getDoubleValue()
724 * SGD_DEGREES_TO_RADIANS,
725 latitude->getDoubleValue()
726 * SGD_DEGREES_TO_RADIANS,
728 } else if ( offset_type == "evening" ) {
729 warp = fgTimeSecondsUntilSunAngle( cur_time,
730 longitude->getDoubleValue()
731 * SGD_DEGREES_TO_RADIANS,
732 latitude->getDoubleValue()
733 * SGD_DEGREES_TO_RADIANS,
735 } else if ( offset_type == "midnight" ) {
736 warp = fgTimeSecondsUntilSunAngle( cur_time,
737 longitude->getDoubleValue()
738 * SGD_DEGREES_TO_RADIANS,
739 latitude->getDoubleValue()
740 * SGD_DEGREES_TO_RADIANS,
743 // cout << "warp = " << warp << endl;
744 globals->set_warp( orig_warp + warp );
746 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
747 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
748 cur_time_override->getLongValue(),
749 globals->get_warp() );
756 * Built-in command: toggle a bool property value.
758 * property: The name of the property to toggle.
761 do_property_toggle (const SGPropertyNode * arg)
763 SGPropertyNode * prop = get_prop(arg);
764 return prop->setBoolValue(!prop->getBoolValue());
769 * Built-in command: assign a value to a property.
771 * property: the name of the property to assign.
772 * value: the value to assign; or
773 * property[1]: the property to copy from.
776 do_property_assign (const SGPropertyNode * arg)
778 SGPropertyNode * prop = get_prop(arg);
779 const SGPropertyNode * prop2 = get_prop2(arg);
780 const SGPropertyNode * value = arg->getNode("value");
783 return prop->setUnspecifiedValue(value->getStringValue());
785 return prop->setUnspecifiedValue(prop2->getStringValue());
792 * Built-in command: increment or decrement a property value.
794 * If the 'step' argument is present, it will be used; otherwise,
795 * the command uses 'offset' and 'factor', usually from the mouse.
797 * property: the name of the property to increment or decrement.
798 * step: the amount of the increment or decrement (default: 0).
799 * offset: offset from the current setting (used for the mouse; multiplied
801 * factor: scaling amount for the offset (defaults to 1).
802 * min: the minimum allowed value (default: no minimum).
803 * max: the maximum allowed value (default: no maximum).
804 * mask: 'integer' to apply only to the left of the decimal point,
805 * 'decimal' to apply only to the right of the decimal point,
806 * or 'all' to apply to the whole number (the default).
807 * wrap: true if the value should be wrapped when it passes min or max;
808 * both min and max must be present for this to work (default:
812 do_property_adjust (const SGPropertyNode * arg)
814 SGPropertyNode * prop = get_prop(arg);
817 if (arg->hasValue("step"))
818 amount = arg->getDoubleValue("step");
820 amount = (arg->getDoubleValue("factor", 1)
821 * arg->getDoubleValue("offset"));
823 double unmodifiable, modifiable;
824 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
825 &unmodifiable, &modifiable);
826 modifiable += amount;
827 limit_value(&modifiable, arg);
829 prop->setDoubleValue(unmodifiable + modifiable);
836 * Built-in command: multiply a property value.
838 * property: the name of the property to multiply.
839 * factor: the amount by which to multiply.
840 * min: the minimum allowed value (default: no minimum).
841 * max: the maximum allowed value (default: no maximum).
842 * mask: 'integer' to apply only to the left of the decimal point,
843 * 'decimal' to apply only to the right of the decimal point,
844 * or 'all' to apply to the whole number (the default).
845 * wrap: true if the value should be wrapped when it passes min or max;
846 * both min and max must be present for this to work (default:
850 do_property_multiply (const SGPropertyNode * arg)
852 SGPropertyNode * prop = get_prop(arg);
853 double factor = arg->getDoubleValue("factor", 1);
855 double unmodifiable, modifiable;
856 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
857 &unmodifiable, &modifiable);
858 modifiable *= factor;
859 limit_value(&modifiable, arg);
861 prop->setDoubleValue(unmodifiable + modifiable);
868 * Built-in command: swap two property values.
870 * property[0]: the name of the first property.
871 * property[1]: the name of the second property.
874 do_property_swap (const SGPropertyNode * arg)
876 SGPropertyNode * prop1 = get_prop(arg);
877 SGPropertyNode * prop2 = get_prop2(arg);
879 // FIXME: inefficient
880 const string & tmp = prop1->getStringValue();
881 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
882 prop2->setUnspecifiedValue(tmp.c_str()));
887 * Built-in command: Set a property to an axis or other moving input.
889 * property: the name of the property to set.
890 * setting: the current input setting, usually between -1.0 and 1.0.
891 * offset: the offset to shift by, before applying the factor.
892 * factor: the factor to multiply by (use negative to reverse).
895 do_property_scale (const SGPropertyNode * arg)
897 SGPropertyNode * prop = get_prop(arg);
898 double setting = arg->getDoubleValue("setting");
899 double offset = arg->getDoubleValue("offset", 0.0);
900 double factor = arg->getDoubleValue("factor", 1.0);
901 bool squared = arg->getBoolValue("squared", false);
902 int power = arg->getIntValue("power", (squared ? 2 : 1));
904 int sign = (setting < 0 ? -1 : 1);
910 setting = setting * setting * sign;
913 setting = setting * setting * setting;
916 setting = setting * setting * setting * setting * sign;
919 setting = pow(setting, power);
920 if ((power % 2) == 0)
925 return prop->setDoubleValue((setting + offset) * factor);
930 * Built-in command: cycle a property through a set of values.
932 * If the current value isn't in the list, the cycle will
933 * (re)start from the beginning.
935 * property: the name of the property to cycle.
936 * value[*]: the list of values to cycle through.
939 do_property_cycle (const SGPropertyNode * arg)
941 SGPropertyNode * prop = get_prop(arg);
942 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
944 int nSelections = values.size();
946 if (nSelections < 1) {
947 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
951 // Try to find the current selection
952 for (int i = 0; i < nSelections; i++) {
953 if (compare_values(prop, values[i])) {
959 // Default or wrap to the first selection
960 if (selection < 0 || selection >= nSelections)
963 prop->setUnspecifiedValue(values[selection]->getStringValue());
969 * Built-in command: randomize a numeric property value.
971 * property: the name of the property value to randomize.
972 * min: the minimum allowed value.
973 * max: the maximum allowed value.
976 do_property_randomize (const SGPropertyNode * arg)
978 SGPropertyNode * prop = get_prop(arg);
979 double min = arg->getDoubleValue("min", DBL_MIN);
980 double max = arg->getDoubleValue("max", DBL_MAX);
981 prop->setDoubleValue(sg_random() * (max - min) + min);
987 * Built-in command: reinit the data logging system based on the
988 * current contents of the /logger tree.
991 do_data_logging_commit (const SGPropertyNode * arg)
993 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
999 * Built-in command: Add a dialog to the GUI system. Does *not*
1000 * display the dialog. The property node should have the same format
1001 * as a dialog XML configuration. It must include:
1003 * name: the name of the GUI dialog for future reference.
1006 do_dialog_new (const SGPropertyNode * arg)
1008 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1010 // Note the casting away of const: this is *real*. Doing a
1011 // "dialog-apply" command later on will mutate this property node.
1012 // I'm not convinced that this isn't the Right Thing though; it
1013 // allows client to create a node, pass it to dialog-new, and get
1014 // the values back from the dialog by reading the same node.
1015 // Perhaps command arguments are not as "const" as they would
1017 gui->newDialog((SGPropertyNode*)arg);
1022 * Built-in command: Show an XML-configured dialog.
1024 * dialog-name: the name of the GUI dialog to display.
1027 do_dialog_show (const SGPropertyNode * arg)
1029 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1030 gui->showDialog(arg->getStringValue("dialog-name"));
1036 * Built-in Command: Hide the active XML-configured dialog.
1039 do_dialog_close (const SGPropertyNode * arg)
1041 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1042 if(arg->hasValue("dialog-name"))
1043 return gui->closeDialog(arg->getStringValue("dialog-name"));
1044 return gui->closeActiveDialog();
1049 * Update a value in the active XML-configured dialog.
1051 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1054 do_dialog_update (const SGPropertyNode * arg)
1056 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1057 FGDialog * dialog = gui->getActiveDialog();
1059 if (arg->hasValue("object-name")) {
1060 dialog->updateValue(arg->getStringValue("object-name"));
1062 dialog->updateValues();
1072 * Apply a value in the active XML-configured dialog.
1074 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1077 do_dialog_apply (const SGPropertyNode * arg)
1079 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1080 FGDialog * dialog = gui->getActiveDialog();
1082 if (arg->hasValue("object-name")) {
1083 const char * name = arg->getStringValue("object-name");
1084 dialog->applyValue(name);
1085 dialog->updateValue(name);
1087 dialog->applyValues();
1088 dialog->updateValues();
1098 * Built-in command: commit presets (read from in /sim/presets/)
1101 do_presets_commit (const SGPropertyNode * arg)
1103 // unbind the current fdm state so property changes
1104 // don't get lost when we subsequently delete this fdm
1105 // and create a new one.
1106 cur_fdm_state->unbind();
1108 // set position from presets
1112 fgReInitSubsystems();
1114 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1117 if ( ! fgGetBool("/sim/presets/onground") ) {
1118 fgSetBool( "/sim/freeze/master", true );
1119 fgSetBool( "/sim/freeze/clock", true );
1127 * Built-in command: set log level (0 ... 7)
1130 do_log_level (const SGPropertyNode * arg)
1132 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1138 * Built-in command: replay the FDR buffer
1141 do_replay (const SGPropertyNode * arg)
1143 // freeze the master fdm
1144 fgSetBool( "/sim/freeze/replay", true );
1146 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1148 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1149 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1150 double duration = fgGetDouble( "/sim/replay/duration" );
1151 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1152 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1154 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1157 cout << "start = " << r->get_start_time()
1158 << " end = " << r->get_end_time() << endl;
1166 do_decrease_visibility (const SGPropertyNode * arg)
1168 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1169 fgSetDouble("/environment/visibility-m", new_value);
1170 fgDefaultWeatherValue("visibility-m", new_value);
1171 globals->get_subsystem("environment")->reinit();
1177 do_increase_visibility (const SGPropertyNode * arg)
1179 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1180 fgSetDouble("/environment/visibility-m", new_value);
1181 fgDefaultWeatherValue("visibility-m", new_value);
1182 globals->get_subsystem("environment")->reinit();
1188 ////////////////////////////////////////////////////////////////////////
1190 ////////////////////////////////////////////////////////////////////////
1194 * Table of built-in commands.
1196 * New commands do not have to be added here; any module in the application
1197 * can add a new command using globals->get_commands()->addCommand(...).
1201 SGCommandMgr::command_t command;
1203 { "null", do_null },
1204 #if defined(HAVE_PLIB_PSL)
1205 { "script", do_script },
1206 #endif // HAVE_PLIB_PSL
1207 { "nasal", do_nasal },
1208 { "exit", do_exit },
1209 { "reinit", do_reinit },
1210 { "suspend", do_reinit },
1211 { "resume", do_reinit },
1212 { "load", do_load },
1213 { "save", do_save },
1214 { "panel-load", do_panel_load },
1215 { "panel-mouse-click", do_panel_mouse_click },
1216 { "preferences-load", do_preferences_load },
1217 { "view-cycle", do_view_cycle },
1218 { "screen-capture", do_screen_capture },
1219 { "tile-cache-reload", do_tile_cache_reload },
1220 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1221 { "set-outside-air-temp-degc", do_set_oat_degc },
1222 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1223 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1224 { "timeofday", do_timeofday },
1225 { "property-toggle", do_property_toggle },
1226 { "property-assign", do_property_assign },
1227 { "property-adjust", do_property_adjust },
1228 { "property-multiply", do_property_multiply },
1229 { "property-swap", do_property_swap },
1230 { "property-scale", do_property_scale },
1231 { "property-cycle", do_property_cycle },
1232 { "property-randomize", do_property_randomize },
1233 { "data-logging-commit", do_data_logging_commit },
1234 { "dialog-new", do_dialog_new },
1235 { "dialog-show", do_dialog_show },
1236 { "dialog-close", do_dialog_close },
1237 { "dialog-show", do_dialog_show },
1238 { "dialog-update", do_dialog_update },
1239 { "dialog-apply", do_dialog_apply },
1240 { "presets-commit", do_presets_commit },
1241 { "log-level", do_log_level },
1242 { "replay", do_replay },
1243 { "decrease-visibility", do_decrease_visibility },
1244 { "increase-visibility", do_increase_visibility },
1245 { 0, 0 } // zero-terminated
1250 * Initialize the default built-in commands.
1252 * Other commands may be added by other parts of the application.
1257 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1258 for (int i = 0; built_ins[i].name != 0; i++) {
1259 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1260 globals->get_commands()->addCommand(built_ins[i].name,
1261 built_ins[i].command);
1264 typedef bool (*dummy)();
1265 fgTie( "/command/view/next", dummy(0), do_view_next );
1266 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1269 // end of fg_commands.cxx