1 // fg_commands.cxx - internal FGFS commands.
7 #include <string.h> // strcmp()
9 #include <simgear/compiler.h>
14 #include <simgear/sg_inlines.h>
15 #include <simgear/debug/logstream.hxx>
16 #include <simgear/math/sg_random.h>
17 #include <simgear/scene/material/mat.hxx>
18 #include <simgear/structure/exception.hxx>
19 #include <simgear/structure/commands.hxx>
20 #include <simgear/props/props.hxx>
22 #include <Cockpit/panel.hxx>
23 #include <Cockpit/panel_io.hxx>
24 #include <Cockpit/hud.hxx>
25 #include <Environment/environment.hxx>
26 #include <FDM/flight.hxx>
28 #include <GUI/new_gui.hxx>
29 #include <GUI/dialog.hxx>
30 #include <Aircraft/replay.hxx>
31 #include <Scenery/tilemgr.hxx>
32 #include <Scenery/scenery.hxx>
33 #include <Scripting/NasalSys.hxx>
34 #include <Sound/fg_fx.hxx>
35 #include <Time/sunsolver.hxx>
36 #include <Time/tmp.hxx>
38 #include "fg_init.hxx"
40 #include "fg_commands.hxx"
41 #include "fg_props.hxx"
42 #include "globals.hxx"
45 #include "viewmgr.hxx"
49 SG_USING_STD(ifstream);
50 SG_USING_STD(ofstream);
54 ////////////////////////////////////////////////////////////////////////
55 // Static helper functions.
56 ////////////////////////////////////////////////////////////////////////
59 static inline SGPropertyNode *
60 get_prop (const SGPropertyNode * arg)
62 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
65 static inline SGPropertyNode *
66 get_prop2 (const SGPropertyNode * arg)
68 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
73 * Get a double value and split it as required.
76 split_value (double full_value, const char * mask,
77 double * unmodifiable, double * modifiable)
79 if (!strcmp("integer", mask)) {
80 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
81 *unmodifiable = full_value - *modifiable;
82 } else if (!strcmp("decimal", mask)) {
83 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
84 *modifiable = full_value - *unmodifiable;
86 if (strcmp("all", mask))
87 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
88 << " assuming 'all'");
90 *modifiable = full_value;
96 * Clamp or wrap a value as specified.
99 limit_value (double * value, const SGPropertyNode * arg)
101 const SGPropertyNode * min_node = arg->getChild("min");
102 const SGPropertyNode * max_node = arg->getChild("max");
104 bool wrap = arg->getBoolValue("wrap");
106 if (min_node == 0 || max_node == 0)
109 if (wrap) { // wrap such that min <= x < max
110 double min_val = min_node->getDoubleValue();
111 double max_val = max_node->getDoubleValue();
112 double resolution = arg->getDoubleValue("resolution");
113 if (resolution > 0.0) {
114 // snap to (min + N*resolution), taking special care to handle imprecision
115 int n = (int)floor((*value - min_val) / resolution + 0.5);
116 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
117 SG_NORMALIZE_RANGE(n, 0, steps);
118 *value = min_val + resolution * n;
120 // plain circular wrapping
121 SG_NORMALIZE_RANGE(*value, min_val, max_val);
123 } else { // clamp such that min <= x <= max
124 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
125 *value = min_node->getDoubleValue();
126 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
127 *value = max_node->getDoubleValue();
132 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
134 switch (value1->getType()) {
135 case SGPropertyNode::BOOL:
136 return (value1->getBoolValue() == value2->getBoolValue());
137 case SGPropertyNode::INT:
138 return (value1->getIntValue() == value2->getIntValue());
139 case SGPropertyNode::LONG:
140 return (value1->getLongValue() == value2->getLongValue());
141 case SGPropertyNode::FLOAT:
142 return (value1->getFloatValue() == value2->getFloatValue());
143 case SGPropertyNode::DOUBLE:
144 return (value1->getDoubleValue() == value2->getDoubleValue());
146 return !strcmp(value1->getStringValue(), value2->getStringValue());
152 ////////////////////////////////////////////////////////////////////////
153 // Command implementations.
154 ////////////////////////////////////////////////////////////////////////
158 * Built-in command: do nothing.
161 do_null (const SGPropertyNode * arg)
167 * Built-in command: run a Nasal script.
170 do_nasal (const SGPropertyNode * arg)
172 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
176 * Built-in command: exit FlightGear.
178 * status: the exit status to return to the operating system (defaults to 0)
181 do_exit (const SGPropertyNode * arg)
183 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
184 fgSetBool("/sim/signals/exit", true);
186 if (fgGetBool("/sim/startup/save-on-exit")) {
188 char* envp = ::getenv( "APPDATA" );
189 if ( envp != NULL ) {
190 SGPath config( envp );
191 config.append( "flightgear.org" );
193 if ( homedir != NULL ) {
194 SGPath config( homedir );
195 config.append( ".fgfs" );
197 config.append( "autosave.xml" );
198 config.create_dir( 0700 );
199 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
201 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
202 } catch (const sg_exception &e) {
203 guiErrorMessage("Error writing autosave.xml: ", e);
206 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
209 fgExit(arg->getIntValue("status", 0));
215 * Built-in command: reinitialize one or more subsystems.
217 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
218 * none is specified, reinitialize all of them.
221 do_reinit (const SGPropertyNode * arg)
225 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
226 if (subsystems.size() == 0) {
227 globals->get_subsystem_mgr()->reinit();
229 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
230 const char * name = subsystems[i]->getStringValue();
231 SGSubsystem * subsystem = globals->get_subsystem(name);
232 if (subsystem == 0) {
234 SG_LOG( SG_GENERAL, SG_ALERT,
235 "Subsystem " << name << "not found" );
242 globals->get_event_mgr()->reinit();
249 // these routines look useful ??? but are never used in the code ???
253 * Built-in command: suspend one or more subsystems.
255 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
258 do_suspend (const SGPropertyNode * arg)
262 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
263 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
264 const char * name = subsystems[i]->getStringValue();
265 SGSubsystem * subsystem = globals->get_subsystem(name);
266 if (subsystem == 0) {
268 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
270 subsystem->suspend();
277 * Built-in command: suspend one or more subsystems.
279 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
282 do_resume (const SGPropertyNode * arg)
286 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
287 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
288 const char * name = subsystems[i]->getStringValue();
289 SGSubsystem * subsystem = globals->get_subsystem(name);
290 if (subsystem == 0) {
292 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
304 * Built-in command: load flight.
306 * file (optional): the name of the file to load (relative to current
307 * directory). Defaults to "fgfs.sav"
310 do_load (const SGPropertyNode * arg)
312 const string &file = arg->getStringValue("file", "fgfs.sav");
313 ifstream input(file.c_str());
314 if (input.good() && fgLoadFlight(input)) {
316 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
319 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
326 * Built-in command: save flight.
328 * file (optional): the name of the file to save (relative to the
329 * current directory). Defaults to "fgfs.sav".
332 do_save (const SGPropertyNode * arg)
334 const string &file = arg->getStringValue("file", "fgfs.sav");
335 bool write_all = arg->getBoolValue("write-all", false);
336 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
337 ofstream output(file.c_str());
338 if (output.good() && fgSaveFlight(output, write_all)) {
340 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
343 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
350 * Built-in command: (re)load the panel.
352 * path (optional): the file name to load the panel from
353 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
354 * and if that's unspecified, to "Panels/Default/default.xml".
357 do_panel_load (const SGPropertyNode * arg)
360 arg->getStringValue("path",
361 fgGetString("/sim/panel/path",
362 "Panels/Default/default.xml"));
363 FGPanel * new_panel = fgReadPanel(panel_path);
364 if (new_panel == 0) {
365 SG_LOG(SG_INPUT, SG_ALERT,
366 "Error reading new panel from " << panel_path);
369 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
370 globals->get_current_panel()->unbind();
371 delete globals->get_current_panel();
372 globals->set_current_panel( new_panel );
373 globals->get_current_panel()->bind();
379 * Built-in command: pass a mouse click to the panel.
381 * button: the mouse button number, zero-based.
382 * is-down: true if the button is down, false if it is up.
383 * x-pos: the x position of the mouse click.
384 * y-pos: the y position of the mouse click.
387 do_panel_mouse_click (const SGPropertyNode * arg)
389 if (globals->get_current_panel() != 0)
390 return globals->get_current_panel()
391 ->doMouseAction(arg->getIntValue("button"),
392 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
393 arg->getIntValue("x-pos"),
394 arg->getIntValue("y-pos"));
401 * Built-in command: (re)load preferences.
403 * path (optional): the file name to load the panel from (relative
404 * to FG_ROOT). Defaults to "preferences.xml".
407 do_preferences_load (const SGPropertyNode * arg)
410 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
411 globals->get_props());
412 } catch (const sg_exception &e) {
413 guiErrorMessage("Error reading global preferences: ", e);
416 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
424 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
425 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
426 if ( globals->get_viewmgr()->get_current() == 1 ) {
427 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
435 globals->get_current_view()->setHeadingOffset_deg(0.0);
436 globals->get_viewmgr()->next_view();
437 fix_hud_visibility();
443 globals->get_current_view()->setHeadingOffset_deg(0.0);
444 globals->get_viewmgr()->prev_view();
445 fix_hud_visibility();
449 * Built-in command: cycle view.
452 do_view_cycle (const SGPropertyNode * arg)
454 globals->get_current_view()->setHeadingOffset_deg(0.0);
455 globals->get_viewmgr()->next_view();
456 fix_hud_visibility();
461 * Built-in command: capture screen.
464 do_screen_capture (const SGPropertyNode * arg)
466 return fgDumpSnapShot();
470 do_dump_scene_graph (const SGPropertyNode*)
477 do_dump_terrain_branch (const SGPropertyNode*)
479 fgDumpTerrainBranch();
481 double lon_deg = fgGetDouble("/position/longitude-deg");
482 double lat_deg = fgGetDouble("/position/latitude-deg");
483 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
484 SGVec3d zero = SGVec3d::fromGeod(geodPos);
486 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
487 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
488 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
494 * Built-in command: hires capture screen.
497 do_hires_screen_capture (const SGPropertyNode * arg)
505 * Reload the tile cache.
508 do_tile_cache_reload (const SGPropertyNode * arg)
510 static const SGPropertyNode *master_freeze
511 = fgGetNode("/sim/freeze/master");
512 bool freeze = master_freeze->getBoolValue();
513 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
515 fgSetBool("/sim/freeze/master", true);
517 if ( globals->get_tile_mgr()->init() ) {
518 // Load the local scenery data
519 double visibility_meters = fgGetDouble("/environment/visibility-m");
520 globals->get_tile_mgr()->update( visibility_meters );
522 SG_LOG( SG_GENERAL, SG_ALERT,
523 "Error in Tile Manager initialization!" );
527 fgSetBool("/sim/freeze/master", false);
534 * Set the sea level outside air temperature and assigning that to all
535 * boundary and aloft environment layers.
538 do_set_sea_level_degc (const SGPropertyNode * arg)
540 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
542 SGPropertyNode *node, *child;
545 node = fgGetNode( "/environment/config/boundary" );
546 if ( node != NULL ) {
548 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
549 child->setDoubleValue( "temperature-sea-level-degc",
550 temp_sea_level_degc );
556 node = fgGetNode( "/environment/config/aloft" );
557 if ( node != NULL ) {
559 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
560 child->setDoubleValue( "temperature-sea-level-degc",
561 temp_sea_level_degc );
571 * Set the outside air temperature at the "current" altitude by first
572 * calculating the corresponding sea level temp, and assigning that to
573 * all boundary and aloft environment layers.
576 do_set_oat_degc (const SGPropertyNode * arg)
578 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
580 // check for an altitude specified in the arguments, otherwise use
581 // current aircraft altitude.
582 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
583 if ( altitude_ft == NULL ) {
584 altitude_ft = fgGetNode("/position/altitude-ft");
587 FGEnvironment dummy; // instantiate a dummy so we can leech a method
588 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
589 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
590 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
592 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
593 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
594 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
596 SGPropertyNode *node, *child;
599 node = fgGetNode( "/environment/config/boundary" );
600 if ( node != NULL ) {
602 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
603 child->setDoubleValue( "temperature-sea-level-degc",
604 temp_sea_level_degc );
610 node = fgGetNode( "/environment/config/aloft" );
611 if ( node != NULL ) {
613 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
614 child->setDoubleValue( "temperature-sea-level-degc",
615 temp_sea_level_degc );
624 * Set the sea level outside air dewpoint and assigning that to all
625 * boundary and aloft environment layers.
628 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
630 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
632 SGPropertyNode *node, *child;
635 node = fgGetNode( "/environment/config/boundary" );
636 if ( node != NULL ) {
638 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
639 child->setDoubleValue( "dewpoint-sea-level-degc",
640 dewpoint_sea_level_degc );
646 node = fgGetNode( "/environment/config/aloft" );
647 if ( node != NULL ) {
649 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
650 child->setDoubleValue( "dewpoint-sea-level-degc",
651 dewpoint_sea_level_degc );
661 * Set the outside air dewpoint at the "current" altitude by first
662 * calculating the corresponding sea level dewpoint, and assigning
663 * that to all boundary and aloft environment layers.
666 do_set_dewpoint_degc (const SGPropertyNode * arg)
668 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
670 // check for an altitude specified in the arguments, otherwise use
671 // current aircraft altitude.
672 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
673 if ( altitude_ft == NULL ) {
674 altitude_ft = fgGetNode("/position/altitude-ft");
677 FGEnvironment dummy; // instantiate a dummy so we can leech a method
678 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
679 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
680 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
682 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
683 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
684 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
686 SGPropertyNode *node, *child;
689 node = fgGetNode( "/environment/config/boundary" );
690 if ( node != NULL ) {
692 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
693 child->setDoubleValue( "dewpoint-sea-level-degc",
694 dewpoint_sea_level_degc );
700 node = fgGetNode( "/environment/config/aloft" );
701 if ( node != NULL ) {
703 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
704 child->setDoubleValue( "dewpoint-sea-level-degc",
705 dewpoint_sea_level_degc );
714 * Update the lighting manually.
717 do_timeofday (const SGPropertyNode * arg)
719 const string &offset_type = arg->getStringValue("timeofday", "noon");
721 static const SGPropertyNode *longitude
722 = fgGetNode("/position/longitude-deg");
723 static const SGPropertyNode *latitude
724 = fgGetNode("/position/latitude-deg");
725 static const SGPropertyNode *cur_time_override
726 = fgGetNode("/sim/time/cur-time-override", true);
728 int orig_warp = globals->get_warp();
729 SGTime *t = globals->get_time_params();
730 time_t cur_time = t->get_cur_time();
731 // cout << "cur_time = " << cur_time << endl;
732 // cout << "orig_warp = " << orig_warp << endl;
735 if ( offset_type == "real" ) {
737 } else if ( offset_type == "dawn" ) {
738 warp = fgTimeSecondsUntilSunAngle( cur_time,
739 longitude->getDoubleValue()
740 * SGD_DEGREES_TO_RADIANS,
741 latitude->getDoubleValue()
742 * SGD_DEGREES_TO_RADIANS,
744 } else if ( offset_type == "morning" ) {
745 warp = fgTimeSecondsUntilSunAngle( cur_time,
746 longitude->getDoubleValue()
747 * SGD_DEGREES_TO_RADIANS,
748 latitude->getDoubleValue()
749 * SGD_DEGREES_TO_RADIANS,
751 } else if ( offset_type == "noon" ) {
752 warp = fgTimeSecondsUntilSunAngle( cur_time,
753 longitude->getDoubleValue()
754 * SGD_DEGREES_TO_RADIANS,
755 latitude->getDoubleValue()
756 * SGD_DEGREES_TO_RADIANS,
758 } else if ( offset_type == "afternoon" ) {
759 warp = fgTimeSecondsUntilSunAngle( cur_time,
760 longitude->getDoubleValue()
761 * SGD_DEGREES_TO_RADIANS,
762 latitude->getDoubleValue()
763 * SGD_DEGREES_TO_RADIANS,
765 } else if ( offset_type == "dusk" ) {
766 warp = fgTimeSecondsUntilSunAngle( cur_time,
767 longitude->getDoubleValue()
768 * SGD_DEGREES_TO_RADIANS,
769 latitude->getDoubleValue()
770 * SGD_DEGREES_TO_RADIANS,
772 } else if ( offset_type == "evening" ) {
773 warp = fgTimeSecondsUntilSunAngle( cur_time,
774 longitude->getDoubleValue()
775 * SGD_DEGREES_TO_RADIANS,
776 latitude->getDoubleValue()
777 * SGD_DEGREES_TO_RADIANS,
779 } else if ( offset_type == "midnight" ) {
780 warp = fgTimeSecondsUntilSunAngle( cur_time,
781 longitude->getDoubleValue()
782 * SGD_DEGREES_TO_RADIANS,
783 latitude->getDoubleValue()
784 * SGD_DEGREES_TO_RADIANS,
787 // cout << "warp = " << warp << endl;
788 globals->set_warp( orig_warp + warp );
790 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
791 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
792 cur_time_override->getLongValue(),
793 globals->get_warp() );
800 * Built-in command: toggle a bool property value.
802 * property: The name of the property to toggle.
805 do_property_toggle (const SGPropertyNode * arg)
807 SGPropertyNode * prop = get_prop(arg);
808 return prop->setBoolValue(!prop->getBoolValue());
813 * Built-in command: assign a value to a property.
815 * property: the name of the property to assign.
816 * value: the value to assign; or
817 * property[1]: the property to copy from.
820 do_property_assign (const SGPropertyNode * arg)
822 SGPropertyNode * prop = get_prop(arg);
823 const SGPropertyNode * prop2 = get_prop2(arg);
824 const SGPropertyNode * value = arg->getNode("value");
827 return prop->setUnspecifiedValue(value->getStringValue());
829 return prop->setUnspecifiedValue(prop2->getStringValue());
836 * Built-in command: increment or decrement a property value.
838 * If the 'step' argument is present, it will be used; otherwise,
839 * the command uses 'offset' and 'factor', usually from the mouse.
841 * property: the name of the property to increment or decrement.
842 * step: the amount of the increment or decrement (default: 0).
843 * offset: offset from the current setting (used for the mouse; multiplied
845 * factor: scaling amount for the offset (defaults to 1).
846 * min: the minimum allowed value (default: no minimum).
847 * max: the maximum allowed value (default: no maximum).
848 * mask: 'integer' to apply only to the left of the decimal point,
849 * 'decimal' to apply only to the right of the decimal point,
850 * or 'all' to apply to the whole number (the default).
851 * wrap: true if the value should be wrapped when it passes min or max;
852 * both min and max must be present for this to work (default:
856 do_property_adjust (const SGPropertyNode * arg)
858 SGPropertyNode * prop = get_prop(arg);
861 if (arg->hasValue("step"))
862 amount = arg->getDoubleValue("step");
864 amount = (arg->getDoubleValue("factor", 1)
865 * arg->getDoubleValue("offset"));
867 double unmodifiable, modifiable;
868 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
869 &unmodifiable, &modifiable);
870 modifiable += amount;
871 limit_value(&modifiable, arg);
873 prop->setDoubleValue(unmodifiable + modifiable);
880 * Built-in command: multiply a property value.
882 * property: the name of the property to multiply.
883 * factor: the amount by which to multiply.
884 * min: the minimum allowed value (default: no minimum).
885 * max: the maximum allowed value (default: no maximum).
886 * mask: 'integer' to apply only to the left of the decimal point,
887 * 'decimal' to apply only to the right of the decimal point,
888 * or 'all' to apply to the whole number (the default).
889 * wrap: true if the value should be wrapped when it passes min or max;
890 * both min and max must be present for this to work (default:
894 do_property_multiply (const SGPropertyNode * arg)
896 SGPropertyNode * prop = get_prop(arg);
897 double factor = arg->getDoubleValue("factor", 1);
899 double unmodifiable, modifiable;
900 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
901 &unmodifiable, &modifiable);
902 modifiable *= factor;
903 limit_value(&modifiable, arg);
905 prop->setDoubleValue(unmodifiable + modifiable);
912 * Built-in command: swap two property values.
914 * property[0]: the name of the first property.
915 * property[1]: the name of the second property.
918 do_property_swap (const SGPropertyNode * arg)
920 SGPropertyNode * prop1 = get_prop(arg);
921 SGPropertyNode * prop2 = get_prop2(arg);
923 // FIXME: inefficient
924 const string & tmp = prop1->getStringValue();
925 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
926 prop2->setUnspecifiedValue(tmp.c_str()));
931 * Built-in command: Set a property to an axis or other moving input.
933 * property: the name of the property to set.
934 * setting: the current input setting, usually between -1.0 and 1.0.
935 * offset: the offset to shift by, before applying the factor.
936 * factor: the factor to multiply by (use negative to reverse).
939 do_property_scale (const SGPropertyNode * arg)
941 SGPropertyNode * prop = get_prop(arg);
942 double setting = arg->getDoubleValue("setting");
943 double offset = arg->getDoubleValue("offset", 0.0);
944 double factor = arg->getDoubleValue("factor", 1.0);
945 bool squared = arg->getBoolValue("squared", false);
946 int power = arg->getIntValue("power", (squared ? 2 : 1));
948 int sign = (setting < 0 ? -1 : 1);
954 setting = setting * setting * sign;
957 setting = setting * setting * setting;
960 setting = setting * setting * setting * setting * sign;
963 setting = pow(setting, power);
964 if ((power % 2) == 0)
969 return prop->setDoubleValue((setting + offset) * factor);
974 * Built-in command: cycle a property through a set of values.
976 * If the current value isn't in the list, the cycle will
977 * (re)start from the beginning.
979 * property: the name of the property to cycle.
980 * value[*]: the list of values to cycle through.
983 do_property_cycle (const SGPropertyNode * arg)
985 SGPropertyNode * prop = get_prop(arg);
986 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
988 int nSelections = values.size();
990 if (nSelections < 1) {
991 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
995 // Try to find the current selection
996 for (int i = 0; i < nSelections; i++) {
997 if (compare_values(prop, values[i])) {
1003 // Default or wrap to the first selection
1004 if (selection < 0 || selection >= nSelections)
1007 prop->setUnspecifiedValue(values[selection]->getStringValue());
1013 * Built-in command: randomize a numeric property value.
1015 * property: the name of the property value to randomize.
1016 * min: the minimum allowed value.
1017 * max: the maximum allowed value.
1020 do_property_randomize (const SGPropertyNode * arg)
1022 SGPropertyNode * prop = get_prop(arg);
1023 double min = arg->getDoubleValue("min", DBL_MIN);
1024 double max = arg->getDoubleValue("max", DBL_MAX);
1025 prop->setDoubleValue(sg_random() * (max - min) + min);
1031 * Built-in command: reinit the data logging system based on the
1032 * current contents of the /logger tree.
1035 do_data_logging_commit (const SGPropertyNode * arg)
1037 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1043 * Built-in command: Add a dialog to the GUI system. Does *not*
1044 * display the dialog. The property node should have the same format
1045 * as a dialog XML configuration. It must include:
1047 * name: the name of the GUI dialog for future reference.
1050 do_dialog_new (const SGPropertyNode * arg)
1052 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1054 // Note the casting away of const: this is *real*. Doing a
1055 // "dialog-apply" command later on will mutate this property node.
1056 // I'm not convinced that this isn't the Right Thing though; it
1057 // allows client to create a node, pass it to dialog-new, and get
1058 // the values back from the dialog by reading the same node.
1059 // Perhaps command arguments are not as "const" as they would
1061 gui->newDialog((SGPropertyNode*)arg);
1066 * Built-in command: Show an XML-configured dialog.
1068 * dialog-name: the name of the GUI dialog to display.
1071 do_dialog_show (const SGPropertyNode * arg)
1073 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1074 gui->showDialog(arg->getStringValue("dialog-name"));
1080 * Built-in Command: Hide the active XML-configured dialog.
1083 do_dialog_close (const SGPropertyNode * arg)
1085 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1086 if(arg->hasValue("dialog-name"))
1087 return gui->closeDialog(arg->getStringValue("dialog-name"));
1088 return gui->closeActiveDialog();
1093 * Update a value in the active XML-configured dialog.
1095 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1098 do_dialog_update (const SGPropertyNode * arg)
1100 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1102 if (arg->hasValue("dialog-name"))
1103 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1105 dialog = gui->getActiveDialog();
1108 dialog->updateValues(arg->getStringValue("object-name"));
1117 * Apply a value in the active XML-configured dialog.
1119 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1122 do_dialog_apply (const SGPropertyNode * arg)
1124 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1126 if (arg->hasValue("dialog-name"))
1127 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1129 dialog = gui->getActiveDialog();
1132 dialog->applyValues(arg->getStringValue("object-name"));
1141 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1145 do_gui_redraw (const SGPropertyNode * arg)
1147 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1154 * Set mouse cursor coordinates and cursor shape.
1157 do_set_cursor (const SGPropertyNode * arg)
1159 if (arg->hasValue("x") || arg->hasValue("y")) {
1160 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1161 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1162 int x = arg->getIntValue("x", mx->getIntValue());
1163 int y = arg->getIntValue("y", my->getIntValue());
1169 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1170 if (cursor->getType() != SGPropertyNode::NONE)
1171 fgSetMouseCursor(cursor->getIntValue());
1173 cursor->setIntValue(fgGetMouseCursor());
1179 * Built-in command: play an audio message (i.e. a wav file) This is
1180 * fire and forget. Call this once per message and it will get dumped
1181 * into a queue. Messages are played sequentially so they do not
1185 do_play_audio_sample (const SGPropertyNode * arg)
1187 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1188 string path = arg->getStringValue("path");
1189 string file = arg->getStringValue("file");
1190 double volume = arg->getDoubleValue("volume");
1191 // cout << "playing " << path << " / " << file << endl;
1193 fx->play_message( path, file, volume );
1196 } catch (const sg_io_exception& e) {
1197 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1198 "failed to load" << path << '/' << file);
1204 * Built-in command: commit presets (read from in /sim/presets/)
1207 do_presets_commit (const SGPropertyNode * arg)
1209 // unbind the current fdm state so property changes
1210 // don't get lost when we subsequently delete this fdm
1211 // and create a new one.
1212 cur_fdm_state->unbind();
1214 // set position from presets
1217 fgReInitSubsystems();
1219 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1222 if ( ! fgGetBool("/sim/presets/onground") ) {
1223 fgSetBool( "/sim/freeze/master", true );
1224 fgSetBool( "/sim/freeze/clock", true );
1232 * Built-in command: set log level (0 ... 7)
1235 do_log_level (const SGPropertyNode * arg)
1237 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1243 * Built-in command: replay the FDR buffer
1246 do_replay (const SGPropertyNode * arg)
1248 // freeze the master fdm
1249 fgSetInt( "/sim/freeze/replay-state", 1 );
1251 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1253 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1254 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1255 double duration = fgGetDouble( "/sim/replay/duration" );
1256 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1257 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1259 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1262 // cout << "start = " << r->get_start_time()
1263 // << " end = " << r->get_end_time() << endl;
1270 do_decrease_visibility (const SGPropertyNode * arg)
1272 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1273 fgSetDouble("/environment/visibility-m", new_value);
1274 fgDefaultWeatherValue("visibility-m", new_value);
1275 globals->get_subsystem("environment")->reinit();
1281 do_increase_visibility (const SGPropertyNode * arg)
1283 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1284 fgSetDouble("/environment/visibility-m", new_value);
1285 fgDefaultWeatherValue("visibility-m", new_value);
1286 globals->get_subsystem("environment")->reinit();
1292 do_hud_init(const SGPropertyNode *)
1294 fgHUDInit(0); // minimal HUD
1299 do_hud_init2(const SGPropertyNode *)
1301 fgHUDInit2(0); // normal HUD
1307 * An fgcommand to allow loading of xml files via nasal,
1308 * the xml file's structure will be made available within
1309 * a property tree node defined under argument "targetnode",
1310 * or in the given argument tree under "data" otherwise.
1312 * @param filename a string to hold the complete path & filename of an XML file
1313 * @param targetnode a string pointing to a location within the property tree
1314 * where to store the parsed XML file. If <targetnode> is undefined, then the
1315 * file contents are stored under a node <data> in the argument tree.
1319 do_load_xml_to_proptree(const SGPropertyNode * arg)
1321 SGPath file(arg->getStringValue("filename"));
1322 if (file.str().empty())
1325 if (file.extension() != "xml")
1326 file.concat(".xml");
1328 SGPropertyNode *targetnode;
1329 if (arg->hasValue("targetnode"))
1330 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1332 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1335 readProperties(file.c_str(), targetnode, true);
1336 } catch (const sg_exception &e) {
1337 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1346 * An fgcommand to allow saving of xml files via nasal,
1347 * the file's structure will be determined based on what's
1348 * encountered in the passed (source) property tree node
1350 * @param filename a string to hold the complete path & filename of the (new)
1352 * @param sourcenode a string pointing to a location within the property tree
1353 * where to find the nodes that should be written recursively into an XML file
1354 * @param data if no sourcenode is given, then the file contents are taken from
1355 * the argument tree's "data" node.
1359 do_save_xml_from_proptree(const SGPropertyNode * arg)
1361 SGPath file(arg->getStringValue("filename"));
1362 if (file.str().empty())
1365 if (file.extension() != "xml")
1366 file.concat(".xml");
1368 SGPropertyNode *sourcenode;
1369 if (arg->hasValue("sourcenode"))
1370 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1371 else if (arg->getNode("data", false))
1372 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1377 writeProperties (file.c_str(), sourcenode, true);
1378 } catch (const sg_exception &e) {
1379 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1387 do_press_cockpit_button (const SGPropertyNode *arg)
1389 const char *prefix = arg->getStringValue("prefix");
1391 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1394 string prop = string(prefix) + "-button";
1397 if (arg->getBoolValue("latching"))
1398 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1402 fgSetDouble(prop.c_str(), value);
1403 fgSetBool(arg->getStringValue("discrete"), value > 0);
1409 do_release_cockpit_button (const SGPropertyNode *arg)
1411 const char *prefix = arg->getStringValue("prefix");
1413 if (arg->getBoolValue("guarded")) {
1414 string prop = string(prefix) + "-guard";
1415 if (fgGetDouble(prop.c_str()) < 1) {
1416 fgSetDouble(prop.c_str(), 1);
1421 if (! arg->getBoolValue("latching")) {
1422 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1423 fgSetBool(arg->getStringValue("discrete"), false);
1430 ////////////////////////////////////////////////////////////////////////
1432 ////////////////////////////////////////////////////////////////////////
1436 * Table of built-in commands.
1438 * New commands do not have to be added here; any module in the application
1439 * can add a new command using globals->get_commands()->addCommand(...).
1443 SGCommandMgr::command_t command;
1445 { "null", do_null },
1446 { "nasal", do_nasal },
1447 { "exit", do_exit },
1448 { "reinit", do_reinit },
1449 { "suspend", do_reinit },
1450 { "resume", do_reinit },
1451 { "load", do_load },
1452 { "save", do_save },
1453 { "panel-load", do_panel_load },
1454 { "panel-mouse-click", do_panel_mouse_click },
1455 { "preferences-load", do_preferences_load },
1456 { "view-cycle", do_view_cycle },
1457 { "screen-capture", do_screen_capture },
1458 { "hires-screen-capture", do_hires_screen_capture },
1459 { "tile-cache-reload", do_tile_cache_reload },
1460 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1461 { "set-outside-air-temp-degc", do_set_oat_degc },
1462 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1463 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1464 { "timeofday", do_timeofday },
1465 { "property-toggle", do_property_toggle },
1466 { "property-assign", do_property_assign },
1467 { "property-adjust", do_property_adjust },
1468 { "property-multiply", do_property_multiply },
1469 { "property-swap", do_property_swap },
1470 { "property-scale", do_property_scale },
1471 { "property-cycle", do_property_cycle },
1472 { "property-randomize", do_property_randomize },
1473 { "data-logging-commit", do_data_logging_commit },
1474 { "dialog-new", do_dialog_new },
1475 { "dialog-show", do_dialog_show },
1476 { "dialog-close", do_dialog_close },
1477 { "dialog-update", do_dialog_update },
1478 { "dialog-apply", do_dialog_apply },
1479 { "gui-redraw", do_gui_redraw },
1480 { "set-cursor", do_set_cursor },
1481 { "play-audio-sample", do_play_audio_sample },
1482 { "presets-commit", do_presets_commit },
1483 { "log-level", do_log_level },
1484 { "replay", do_replay },
1485 { "decrease-visibility", do_decrease_visibility },
1486 { "increase-visibility", do_increase_visibility },
1487 { "hud-init", do_hud_init },
1488 { "hud-init2", do_hud_init2 },
1489 { "loadxml", do_load_xml_to_proptree},
1490 { "savexml", do_save_xml_from_proptree },
1491 { "press-cockpit-button", do_press_cockpit_button },
1492 { "release-cockpit-button", do_release_cockpit_button },
1493 { "dump-scenegraph", do_dump_scene_graph },
1494 { "dump-terrainbranch", do_dump_terrain_branch },
1495 { 0, 0 } // zero-terminated
1500 * Initialize the default built-in commands.
1502 * Other commands may be added by other parts of the application.
1507 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1508 for (int i = 0; built_ins[i].name != 0; i++) {
1509 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1510 globals->get_commands()->addCommand(built_ins[i].name,
1511 built_ins[i].command);
1514 typedef bool (*dummy)();
1515 fgTie( "/command/view/next", dummy(0), do_view_next );
1516 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1519 // end of fg_commands.cxx