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 #if defined(HAVE_PLIB_PSL)
34 # include <Scripting/scriptmgr.hxx>
36 #include <Scripting/NasalSys.hxx>
37 #include <Sound/fg_fx.hxx>
38 #include <Time/sunsolver.hxx>
39 #include <Time/tmp.hxx>
41 #include "fg_init.hxx"
43 #include "fg_commands.hxx"
44 #include "fg_props.hxx"
45 #include "globals.hxx"
48 #include "viewmgr.hxx"
52 SG_USING_STD(ifstream);
53 SG_USING_STD(ofstream);
57 ////////////////////////////////////////////////////////////////////////
58 // Static helper functions.
59 ////////////////////////////////////////////////////////////////////////
62 static inline SGPropertyNode *
63 get_prop (const SGPropertyNode * arg)
65 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
68 static inline SGPropertyNode *
69 get_prop2 (const SGPropertyNode * arg)
71 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
76 * Get a double value and split it as required.
79 split_value (double full_value, const char * mask,
80 double * unmodifiable, double * modifiable)
82 if (!strcmp("integer", mask)) {
83 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
84 *unmodifiable = full_value - *modifiable;
85 } else if (!strcmp("decimal", mask)) {
86 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
87 *modifiable = full_value - *unmodifiable;
89 if (strcmp("all", mask))
90 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
91 << " assuming 'all'");
93 *modifiable = full_value;
99 * Clamp or wrap a value as specified.
102 limit_value (double * value, const SGPropertyNode * arg)
104 const SGPropertyNode * min_node = arg->getChild("min");
105 const SGPropertyNode * max_node = arg->getChild("max");
107 bool wrap = arg->getBoolValue("wrap");
109 if (min_node == 0 || max_node == 0)
112 if (wrap) { // wrap such that min <= x < max
113 double min_val = min_node->getDoubleValue();
114 double max_val = max_node->getDoubleValue();
115 double resolution = arg->getDoubleValue("resolution");
116 if (resolution > 0.0) {
117 // snap to (min + N*resolution), taking special care to handle imprecision
118 int n = (int)floor((*value - min_val) / resolution + 0.5);
119 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
120 SG_NORMALIZE_RANGE(n, 0, steps);
121 *value = min_val + resolution * n;
123 // plain circular wrapping
124 SG_NORMALIZE_RANGE(*value, min_val, max_val);
126 } else { // clamp such that min <= x <= max
127 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
128 *value = min_node->getDoubleValue();
129 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
130 *value = max_node->getDoubleValue();
135 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
137 switch (value1->getType()) {
138 case SGPropertyNode::BOOL:
139 return (value1->getBoolValue() == value2->getBoolValue());
140 case SGPropertyNode::INT:
141 return (value1->getIntValue() == value2->getIntValue());
142 case SGPropertyNode::LONG:
143 return (value1->getLongValue() == value2->getLongValue());
144 case SGPropertyNode::FLOAT:
145 return (value1->getFloatValue() == value2->getFloatValue());
146 case SGPropertyNode::DOUBLE:
147 return (value1->getDoubleValue() == value2->getDoubleValue());
149 return !strcmp(value1->getStringValue(), value2->getStringValue());
155 ////////////////////////////////////////////////////////////////////////
156 // Command implementations.
157 ////////////////////////////////////////////////////////////////////////
161 * Built-in command: do nothing.
164 do_null (const SGPropertyNode * arg)
169 #if defined(HAVE_PLIB_PSL)
171 * Built-in command: run a PSL script.
173 * script: the PSL script to execute
176 do_script (const SGPropertyNode * arg)
178 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
179 return mgr->run(arg->getStringValue("script"));
181 #endif // HAVE_PLIB_PSL
184 * Built-in command: run a Nasal script.
187 do_nasal (const SGPropertyNode * arg)
189 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
193 * Built-in command: exit FlightGear.
195 * status: the exit status to return to the operating system (defaults to 0)
198 do_exit (const SGPropertyNode * arg)
200 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
201 fgSetBool("/sim/signals/exit", true);
203 if (fgGetBool("/sim/startup/save-on-exit")) {
205 char* envp = ::getenv( "APPDATA" );
206 if ( envp != NULL ) {
207 SGPath config( envp );
208 config.append( "flightgear.org" );
210 if ( homedir != NULL ) {
211 SGPath config( homedir );
212 config.append( ".fgfs" );
214 config.append( "autosave.xml" );
215 config.create_dir( 0700 );
216 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
218 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
219 } catch (const sg_exception &e) {
220 guiErrorMessage("Error writing autosave.xml: ", e);
223 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
226 fgExit(arg->getIntValue("status", 0));
232 * Built-in command: reinitialize one or more subsystems.
234 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
235 * none is specified, reinitialize all of them.
238 do_reinit (const SGPropertyNode * arg)
242 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
243 if (subsystems.size() == 0) {
244 globals->get_subsystem_mgr()->reinit();
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,
252 "Subsystem " << name << "not found" );
259 globals->get_event_mgr()->reinit();
266 // these routines look useful ??? but are never used in the code ???
270 * Built-in command: suspend one or more subsystems.
272 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
275 do_suspend (const SGPropertyNode * arg)
279 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
280 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
281 const char * name = subsystems[i]->getStringValue();
282 SGSubsystem * subsystem = globals->get_subsystem(name);
283 if (subsystem == 0) {
285 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
287 subsystem->suspend();
294 * Built-in command: suspend one or more subsystems.
296 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
299 do_resume (const SGPropertyNode * arg)
303 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
304 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
305 const char * name = subsystems[i]->getStringValue();
306 SGSubsystem * subsystem = globals->get_subsystem(name);
307 if (subsystem == 0) {
309 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
321 * Built-in command: load flight.
323 * file (optional): the name of the file to load (relative to current
324 * directory). Defaults to "fgfs.sav"
327 do_load (const SGPropertyNode * arg)
329 const string &file = arg->getStringValue("file", "fgfs.sav");
330 ifstream input(file.c_str());
331 if (input.good() && fgLoadFlight(input)) {
333 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
336 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
343 * Built-in command: save flight.
345 * file (optional): the name of the file to save (relative to the
346 * current directory). Defaults to "fgfs.sav".
349 do_save (const SGPropertyNode * arg)
351 const string &file = arg->getStringValue("file", "fgfs.sav");
352 bool write_all = arg->getBoolValue("write-all", false);
353 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
354 ofstream output(file.c_str());
355 if (output.good() && fgSaveFlight(output, write_all)) {
357 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
360 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
367 * Built-in command: (re)load the panel.
369 * path (optional): the file name to load the panel from
370 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
371 * and if that's unspecified, to "Panels/Default/default.xml".
374 do_panel_load (const SGPropertyNode * arg)
377 arg->getStringValue("path",
378 fgGetString("/sim/panel/path",
379 "Panels/Default/default.xml"));
380 FGPanel * new_panel = fgReadPanel(panel_path);
381 if (new_panel == 0) {
382 SG_LOG(SG_INPUT, SG_ALERT,
383 "Error reading new panel from " << panel_path);
386 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
387 globals->get_current_panel()->unbind();
388 delete globals->get_current_panel();
389 globals->set_current_panel( new_panel );
390 globals->get_current_panel()->bind();
396 * Built-in command: pass a mouse click to the panel.
398 * button: the mouse button number, zero-based.
399 * is-down: true if the button is down, false if it is up.
400 * x-pos: the x position of the mouse click.
401 * y-pos: the y position of the mouse click.
404 do_panel_mouse_click (const SGPropertyNode * arg)
406 if (globals->get_current_panel() != 0)
407 return globals->get_current_panel()
408 ->doMouseAction(arg->getIntValue("button"),
409 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
410 arg->getIntValue("x-pos"),
411 arg->getIntValue("y-pos"));
418 * Built-in command: (re)load preferences.
420 * path (optional): the file name to load the panel from (relative
421 * to FG_ROOT). Defaults to "preferences.xml".
424 do_preferences_load (const SGPropertyNode * arg)
427 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
428 globals->get_props());
429 } catch (const sg_exception &e) {
430 guiErrorMessage("Error reading global preferences: ", e);
433 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
441 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
442 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
443 if ( globals->get_viewmgr()->get_current() == 1 ) {
444 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
452 globals->get_current_view()->setHeadingOffset_deg(0.0);
453 globals->get_viewmgr()->next_view();
454 fix_hud_visibility();
460 globals->get_current_view()->setHeadingOffset_deg(0.0);
461 globals->get_viewmgr()->prev_view();
462 fix_hud_visibility();
466 * Built-in command: cycle view.
469 do_view_cycle (const SGPropertyNode * arg)
471 globals->get_current_view()->setHeadingOffset_deg(0.0);
472 globals->get_viewmgr()->next_view();
473 fix_hud_visibility();
478 * Built-in command: capture screen.
481 do_screen_capture (const SGPropertyNode * arg)
488 do_dump_scene_graph (const SGPropertyNode*)
495 do_dump_terrain_branch (const SGPropertyNode*)
497 fgDumpTerrainBranch();
499 double lon_deg = fgGetDouble("/position/longitude-deg");
500 double lat_deg = fgGetDouble("/position/latitude-deg");
501 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
502 SGVec3d zero = SGVec3d::fromGeod(geodPos);
504 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
505 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
506 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
512 * Built-in command: hires capture screen.
515 do_hires_screen_capture (const SGPropertyNode * arg)
523 * Reload the tile cache.
526 do_tile_cache_reload (const SGPropertyNode * arg)
528 static const SGPropertyNode *master_freeze
529 = fgGetNode("/sim/freeze/master");
530 bool freeze = master_freeze->getBoolValue();
531 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
533 fgSetBool("/sim/freeze/master", true);
535 if ( globals->get_tile_mgr()->init() ) {
536 // Load the local scenery data
537 double visibility_meters = fgGetDouble("/environment/visibility-m");
538 globals->get_tile_mgr()->update( visibility_meters );
540 SG_LOG( SG_GENERAL, SG_ALERT,
541 "Error in Tile Manager initialization!" );
545 fgSetBool("/sim/freeze/master", false);
552 * Set the sea level outside air temperature and assigning that to all
553 * boundary and aloft environment layers.
556 do_set_sea_level_degc (const SGPropertyNode * arg)
558 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
560 SGPropertyNode *node, *child;
563 node = fgGetNode( "/environment/config/boundary" );
564 if ( node != NULL ) {
566 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
567 child->setDoubleValue( "temperature-sea-level-degc",
568 temp_sea_level_degc );
574 node = fgGetNode( "/environment/config/aloft" );
575 if ( node != NULL ) {
577 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
578 child->setDoubleValue( "temperature-sea-level-degc",
579 temp_sea_level_degc );
589 * Set the outside air temperature at the "current" altitude by first
590 * calculating the corresponding sea level temp, and assigning that to
591 * all boundary and aloft environment layers.
594 do_set_oat_degc (const SGPropertyNode * arg)
596 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
598 // check for an altitude specified in the arguments, otherwise use
599 // current aircraft altitude.
600 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
601 if ( altitude_ft == NULL ) {
602 altitude_ft = fgGetNode("/position/altitude-ft");
605 FGEnvironment dummy; // instantiate a dummy so we can leech a method
606 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
607 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
608 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
610 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
611 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
612 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
614 SGPropertyNode *node, *child;
617 node = fgGetNode( "/environment/config/boundary" );
618 if ( node != NULL ) {
620 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
621 child->setDoubleValue( "temperature-sea-level-degc",
622 temp_sea_level_degc );
628 node = fgGetNode( "/environment/config/aloft" );
629 if ( node != NULL ) {
631 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
632 child->setDoubleValue( "temperature-sea-level-degc",
633 temp_sea_level_degc );
642 * Set the sea level outside air dewpoint and assigning that to all
643 * boundary and aloft environment layers.
646 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
648 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
650 SGPropertyNode *node, *child;
653 node = fgGetNode( "/environment/config/boundary" );
654 if ( node != NULL ) {
656 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
657 child->setDoubleValue( "dewpoint-sea-level-degc",
658 dewpoint_sea_level_degc );
664 node = fgGetNode( "/environment/config/aloft" );
665 if ( node != NULL ) {
667 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
668 child->setDoubleValue( "dewpoint-sea-level-degc",
669 dewpoint_sea_level_degc );
679 * Set the outside air dewpoint at the "current" altitude by first
680 * calculating the corresponding sea level dewpoint, and assigning
681 * that to all boundary and aloft environment layers.
684 do_set_dewpoint_degc (const SGPropertyNode * arg)
686 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
688 // check for an altitude specified in the arguments, otherwise use
689 // current aircraft altitude.
690 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
691 if ( altitude_ft == NULL ) {
692 altitude_ft = fgGetNode("/position/altitude-ft");
695 FGEnvironment dummy; // instantiate a dummy so we can leech a method
696 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
697 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
698 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
700 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
701 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
702 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
704 SGPropertyNode *node, *child;
707 node = fgGetNode( "/environment/config/boundary" );
708 if ( node != NULL ) {
710 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
711 child->setDoubleValue( "dewpoint-sea-level-degc",
712 dewpoint_sea_level_degc );
718 node = fgGetNode( "/environment/config/aloft" );
719 if ( node != NULL ) {
721 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
722 child->setDoubleValue( "dewpoint-sea-level-degc",
723 dewpoint_sea_level_degc );
732 * Update the lighting manually.
735 do_timeofday (const SGPropertyNode * arg)
737 const string &offset_type = arg->getStringValue("timeofday", "noon");
739 static const SGPropertyNode *longitude
740 = fgGetNode("/position/longitude-deg");
741 static const SGPropertyNode *latitude
742 = fgGetNode("/position/latitude-deg");
743 static const SGPropertyNode *cur_time_override
744 = fgGetNode("/sim/time/cur-time-override", true);
746 int orig_warp = globals->get_warp();
747 SGTime *t = globals->get_time_params();
748 time_t cur_time = t->get_cur_time();
749 // cout << "cur_time = " << cur_time << endl;
750 // cout << "orig_warp = " << orig_warp << endl;
753 if ( offset_type == "real" ) {
755 } else if ( offset_type == "dawn" ) {
756 warp = fgTimeSecondsUntilSunAngle( cur_time,
757 longitude->getDoubleValue()
758 * SGD_DEGREES_TO_RADIANS,
759 latitude->getDoubleValue()
760 * SGD_DEGREES_TO_RADIANS,
762 } else if ( offset_type == "morning" ) {
763 warp = fgTimeSecondsUntilSunAngle( cur_time,
764 longitude->getDoubleValue()
765 * SGD_DEGREES_TO_RADIANS,
766 latitude->getDoubleValue()
767 * SGD_DEGREES_TO_RADIANS,
769 } else if ( offset_type == "noon" ) {
770 warp = fgTimeSecondsUntilSunAngle( cur_time,
771 longitude->getDoubleValue()
772 * SGD_DEGREES_TO_RADIANS,
773 latitude->getDoubleValue()
774 * SGD_DEGREES_TO_RADIANS,
776 } else if ( offset_type == "afternoon" ) {
777 warp = fgTimeSecondsUntilSunAngle( cur_time,
778 longitude->getDoubleValue()
779 * SGD_DEGREES_TO_RADIANS,
780 latitude->getDoubleValue()
781 * SGD_DEGREES_TO_RADIANS,
783 } else if ( offset_type == "dusk" ) {
784 warp = fgTimeSecondsUntilSunAngle( cur_time,
785 longitude->getDoubleValue()
786 * SGD_DEGREES_TO_RADIANS,
787 latitude->getDoubleValue()
788 * SGD_DEGREES_TO_RADIANS,
790 } else if ( offset_type == "evening" ) {
791 warp = fgTimeSecondsUntilSunAngle( cur_time,
792 longitude->getDoubleValue()
793 * SGD_DEGREES_TO_RADIANS,
794 latitude->getDoubleValue()
795 * SGD_DEGREES_TO_RADIANS,
797 } else if ( offset_type == "midnight" ) {
798 warp = fgTimeSecondsUntilSunAngle( cur_time,
799 longitude->getDoubleValue()
800 * SGD_DEGREES_TO_RADIANS,
801 latitude->getDoubleValue()
802 * SGD_DEGREES_TO_RADIANS,
805 // cout << "warp = " << warp << endl;
806 globals->set_warp( orig_warp + warp );
808 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
809 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
810 cur_time_override->getLongValue(),
811 globals->get_warp() );
818 * Built-in command: toggle a bool property value.
820 * property: The name of the property to toggle.
823 do_property_toggle (const SGPropertyNode * arg)
825 SGPropertyNode * prop = get_prop(arg);
826 return prop->setBoolValue(!prop->getBoolValue());
831 * Built-in command: assign a value to a property.
833 * property: the name of the property to assign.
834 * value: the value to assign; or
835 * property[1]: the property to copy from.
838 do_property_assign (const SGPropertyNode * arg)
840 SGPropertyNode * prop = get_prop(arg);
841 const SGPropertyNode * prop2 = get_prop2(arg);
842 const SGPropertyNode * value = arg->getNode("value");
845 return prop->setUnspecifiedValue(value->getStringValue());
847 return prop->setUnspecifiedValue(prop2->getStringValue());
854 * Built-in command: increment or decrement a property value.
856 * If the 'step' argument is present, it will be used; otherwise,
857 * the command uses 'offset' and 'factor', usually from the mouse.
859 * property: the name of the property to increment or decrement.
860 * step: the amount of the increment or decrement (default: 0).
861 * offset: offset from the current setting (used for the mouse; multiplied
863 * factor: scaling amount for the offset (defaults to 1).
864 * min: the minimum allowed value (default: no minimum).
865 * max: the maximum allowed value (default: no maximum).
866 * mask: 'integer' to apply only to the left of the decimal point,
867 * 'decimal' to apply only to the right of the decimal point,
868 * or 'all' to apply to the whole number (the default).
869 * wrap: true if the value should be wrapped when it passes min or max;
870 * both min and max must be present for this to work (default:
874 do_property_adjust (const SGPropertyNode * arg)
876 SGPropertyNode * prop = get_prop(arg);
879 if (arg->hasValue("step"))
880 amount = arg->getDoubleValue("step");
882 amount = (arg->getDoubleValue("factor", 1)
883 * arg->getDoubleValue("offset"));
885 double unmodifiable, modifiable;
886 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
887 &unmodifiable, &modifiable);
888 modifiable += amount;
889 limit_value(&modifiable, arg);
891 prop->setDoubleValue(unmodifiable + modifiable);
898 * Built-in command: multiply a property value.
900 * property: the name of the property to multiply.
901 * factor: the amount by which to multiply.
902 * min: the minimum allowed value (default: no minimum).
903 * max: the maximum allowed value (default: no maximum).
904 * mask: 'integer' to apply only to the left of the decimal point,
905 * 'decimal' to apply only to the right of the decimal point,
906 * or 'all' to apply to the whole number (the default).
907 * wrap: true if the value should be wrapped when it passes min or max;
908 * both min and max must be present for this to work (default:
912 do_property_multiply (const SGPropertyNode * arg)
914 SGPropertyNode * prop = get_prop(arg);
915 double factor = arg->getDoubleValue("factor", 1);
917 double unmodifiable, modifiable;
918 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
919 &unmodifiable, &modifiable);
920 modifiable *= factor;
921 limit_value(&modifiable, arg);
923 prop->setDoubleValue(unmodifiable + modifiable);
930 * Built-in command: swap two property values.
932 * property[0]: the name of the first property.
933 * property[1]: the name of the second property.
936 do_property_swap (const SGPropertyNode * arg)
938 SGPropertyNode * prop1 = get_prop(arg);
939 SGPropertyNode * prop2 = get_prop2(arg);
941 // FIXME: inefficient
942 const string & tmp = prop1->getStringValue();
943 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
944 prop2->setUnspecifiedValue(tmp.c_str()));
949 * Built-in command: Set a property to an axis or other moving input.
951 * property: the name of the property to set.
952 * setting: the current input setting, usually between -1.0 and 1.0.
953 * offset: the offset to shift by, before applying the factor.
954 * factor: the factor to multiply by (use negative to reverse).
957 do_property_scale (const SGPropertyNode * arg)
959 SGPropertyNode * prop = get_prop(arg);
960 double setting = arg->getDoubleValue("setting");
961 double offset = arg->getDoubleValue("offset", 0.0);
962 double factor = arg->getDoubleValue("factor", 1.0);
963 bool squared = arg->getBoolValue("squared", false);
964 int power = arg->getIntValue("power", (squared ? 2 : 1));
966 int sign = (setting < 0 ? -1 : 1);
972 setting = setting * setting * sign;
975 setting = setting * setting * setting;
978 setting = setting * setting * setting * setting * sign;
981 setting = pow(setting, power);
982 if ((power % 2) == 0)
987 return prop->setDoubleValue((setting + offset) * factor);
992 * Built-in command: cycle a property through a set of values.
994 * If the current value isn't in the list, the cycle will
995 * (re)start from the beginning.
997 * property: the name of the property to cycle.
998 * value[*]: the list of values to cycle through.
1001 do_property_cycle (const SGPropertyNode * arg)
1003 SGPropertyNode * prop = get_prop(arg);
1004 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
1006 int nSelections = values.size();
1008 if (nSelections < 1) {
1009 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
1013 // Try to find the current selection
1014 for (int i = 0; i < nSelections; i++) {
1015 if (compare_values(prop, values[i])) {
1021 // Default or wrap to the first selection
1022 if (selection < 0 || selection >= nSelections)
1025 prop->setUnspecifiedValue(values[selection]->getStringValue());
1031 * Built-in command: randomize a numeric property value.
1033 * property: the name of the property value to randomize.
1034 * min: the minimum allowed value.
1035 * max: the maximum allowed value.
1038 do_property_randomize (const SGPropertyNode * arg)
1040 SGPropertyNode * prop = get_prop(arg);
1041 double min = arg->getDoubleValue("min", DBL_MIN);
1042 double max = arg->getDoubleValue("max", DBL_MAX);
1043 prop->setDoubleValue(sg_random() * (max - min) + min);
1049 * Built-in command: reinit the data logging system based on the
1050 * current contents of the /logger tree.
1053 do_data_logging_commit (const SGPropertyNode * arg)
1055 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1061 * Built-in command: Add a dialog to the GUI system. Does *not*
1062 * display the dialog. The property node should have the same format
1063 * as a dialog XML configuration. It must include:
1065 * name: the name of the GUI dialog for future reference.
1068 do_dialog_new (const SGPropertyNode * arg)
1070 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1072 // Note the casting away of const: this is *real*. Doing a
1073 // "dialog-apply" command later on will mutate this property node.
1074 // I'm not convinced that this isn't the Right Thing though; it
1075 // allows client to create a node, pass it to dialog-new, and get
1076 // the values back from the dialog by reading the same node.
1077 // Perhaps command arguments are not as "const" as they would
1079 gui->newDialog((SGPropertyNode*)arg);
1084 * Built-in command: Show an XML-configured dialog.
1086 * dialog-name: the name of the GUI dialog to display.
1089 do_dialog_show (const SGPropertyNode * arg)
1091 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1092 gui->showDialog(arg->getStringValue("dialog-name"));
1098 * Built-in Command: Hide the active XML-configured dialog.
1101 do_dialog_close (const SGPropertyNode * arg)
1103 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1104 if(arg->hasValue("dialog-name"))
1105 return gui->closeDialog(arg->getStringValue("dialog-name"));
1106 return gui->closeActiveDialog();
1111 * Update a value in the active XML-configured dialog.
1113 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1116 do_dialog_update (const SGPropertyNode * arg)
1118 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1120 if (arg->hasValue("dialog-name"))
1121 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1123 dialog = gui->getActiveDialog();
1126 dialog->updateValues(arg->getStringValue("object-name"));
1135 * Apply a value in the active XML-configured dialog.
1137 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1140 do_dialog_apply (const SGPropertyNode * arg)
1142 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1144 if (arg->hasValue("dialog-name"))
1145 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1147 dialog = gui->getActiveDialog();
1150 dialog->applyValues(arg->getStringValue("object-name"));
1159 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1163 do_gui_redraw (const SGPropertyNode * arg)
1165 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1171 * Built-in command: play an audio message (i.e. a wav file) This is
1172 * fire and forget. Call this once per message and it will get dumped
1173 * into a queue. Messages are played sequentially so they do not
1177 do_play_audio_sample (const SGPropertyNode * arg)
1179 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1180 string path = arg->getStringValue("path");
1181 string file = arg->getStringValue("file");
1182 double volume = arg->getDoubleValue("volume");
1183 // cout << "playing " << path << " / " << file << endl;
1185 fx->play_message( path, file, volume );
1188 } catch (const sg_io_exception& e) {
1189 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1190 "failed to load" << path << '/' << file);
1196 * Built-in command: commit presets (read from in /sim/presets/)
1199 do_presets_commit (const SGPropertyNode * arg)
1201 // unbind the current fdm state so property changes
1202 // don't get lost when we subsequently delete this fdm
1203 // and create a new one.
1204 cur_fdm_state->unbind();
1206 // set position from presets
1209 fgReInitSubsystems();
1211 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1214 if ( ! fgGetBool("/sim/presets/onground") ) {
1215 fgSetBool( "/sim/freeze/master", true );
1216 fgSetBool( "/sim/freeze/clock", true );
1224 * Built-in command: set log level (0 ... 7)
1227 do_log_level (const SGPropertyNode * arg)
1229 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1235 * Built-in command: replay the FDR buffer
1238 do_replay (const SGPropertyNode * arg)
1240 // freeze the master fdm
1241 fgSetInt( "/sim/freeze/replay-state", 1 );
1243 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1245 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1246 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1247 double duration = fgGetDouble( "/sim/replay/duration" );
1248 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1249 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1251 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1254 // cout << "start = " << r->get_start_time()
1255 // << " end = " << r->get_end_time() << endl;
1262 do_decrease_visibility (const SGPropertyNode * arg)
1264 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1265 fgSetDouble("/environment/visibility-m", new_value);
1266 fgDefaultWeatherValue("visibility-m", new_value);
1267 globals->get_subsystem("environment")->reinit();
1273 do_increase_visibility (const SGPropertyNode * arg)
1275 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1276 fgSetDouble("/environment/visibility-m", new_value);
1277 fgDefaultWeatherValue("visibility-m", new_value);
1278 globals->get_subsystem("environment")->reinit();
1284 do_hud_init(const SGPropertyNode *)
1286 fgHUDInit(0); // minimal HUD
1291 do_hud_init2(const SGPropertyNode *)
1293 fgHUDInit2(0); // normal HUD
1299 * An fgcommand to allow loading of xml files via nasal,
1300 * the xml file's structure will be made available within
1301 * a property tree node defined under argument "targetnode",
1302 * or in the given argument tree under "data" otherwise.
1304 * @param filename a string to hold the complete path & filename of an XML file
1305 * @param targetnode a string pointing to a location within the property tree
1306 * where to store the parsed XML file. If <targetnode> is undefined, then the
1307 * file contents are stored under a node <data> in the argument tree.
1311 do_load_xml_to_proptree(const SGPropertyNode * arg)
1313 SGPath file(arg->getStringValue("filename"));
1314 if (file.str().empty())
1317 if (file.extension() != "xml")
1318 file.concat(".xml");
1320 SGPropertyNode *targetnode;
1321 if (arg->hasValue("targetnode"))
1322 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1324 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1327 readProperties(file.c_str(), targetnode, true);
1328 } catch (const sg_exception &e) {
1329 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1338 * An fgcommand to allow saving of xml files via nasal,
1339 * the file's structure will be determined based on what's
1340 * encountered in the passed (source) property tree node
1342 * @param filename a string to hold the complete path & filename of the (new)
1344 * @param sourcenode a string pointing to a location within the property tree
1345 * where to find the nodes that should be written recursively into an XML file
1346 * @param data if no sourcenode is given, then the file contents are taken from
1347 * the argument tree's "data" node.
1351 do_save_xml_from_proptree(const SGPropertyNode * arg)
1353 SGPath file(arg->getStringValue("filename"));
1354 if (file.str().empty())
1357 if (file.extension() != "xml")
1358 file.concat(".xml");
1360 SGPropertyNode *sourcenode;
1361 if (arg->hasValue("sourcenode"))
1362 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1363 else if (arg->getNode("data", false))
1364 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1369 writeProperties (file.c_str(), sourcenode, true);
1370 } catch (const sg_exception &e) {
1371 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1379 do_press_cockpit_button (const SGPropertyNode *arg)
1381 const char *prefix = arg->getStringValue("prefix");
1383 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1386 string prop = string(prefix) + "-button";
1389 if (arg->getBoolValue("latching"))
1390 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1394 fgSetDouble(prop.c_str(), value);
1395 fgSetBool(arg->getStringValue("discrete"), value > 0);
1401 do_release_cockpit_button (const SGPropertyNode *arg)
1403 const char *prefix = arg->getStringValue("prefix");
1405 if (arg->getBoolValue("guarded")) {
1406 string prop = string(prefix) + "-guard";
1407 if (fgGetDouble(prop.c_str()) < 1) {
1408 fgSetDouble(prop.c_str(), 1);
1413 if (! arg->getBoolValue("latching")) {
1414 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1415 fgSetBool(arg->getStringValue("discrete"), false);
1422 ////////////////////////////////////////////////////////////////////////
1424 ////////////////////////////////////////////////////////////////////////
1428 * Table of built-in commands.
1430 * New commands do not have to be added here; any module in the application
1431 * can add a new command using globals->get_commands()->addCommand(...).
1435 SGCommandMgr::command_t command;
1437 { "null", do_null },
1438 #if defined(HAVE_PLIB_PSL)
1439 { "script", do_script },
1440 #endif // HAVE_PLIB_PSL
1441 { "nasal", do_nasal },
1442 { "exit", do_exit },
1443 { "reinit", do_reinit },
1444 { "suspend", do_reinit },
1445 { "resume", do_reinit },
1446 { "load", do_load },
1447 { "save", do_save },
1448 { "panel-load", do_panel_load },
1449 { "panel-mouse-click", do_panel_mouse_click },
1450 { "preferences-load", do_preferences_load },
1451 { "view-cycle", do_view_cycle },
1452 { "screen-capture", do_screen_capture },
1453 { "hires-screen-capture", do_hires_screen_capture },
1454 { "tile-cache-reload", do_tile_cache_reload },
1455 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1456 { "set-outside-air-temp-degc", do_set_oat_degc },
1457 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1458 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1459 { "timeofday", do_timeofday },
1460 { "property-toggle", do_property_toggle },
1461 { "property-assign", do_property_assign },
1462 { "property-adjust", do_property_adjust },
1463 { "property-multiply", do_property_multiply },
1464 { "property-swap", do_property_swap },
1465 { "property-scale", do_property_scale },
1466 { "property-cycle", do_property_cycle },
1467 { "property-randomize", do_property_randomize },
1468 { "data-logging-commit", do_data_logging_commit },
1469 { "dialog-new", do_dialog_new },
1470 { "dialog-show", do_dialog_show },
1471 { "dialog-close", do_dialog_close },
1472 { "dialog-update", do_dialog_update },
1473 { "dialog-apply", do_dialog_apply },
1474 { "gui-redraw", do_gui_redraw },
1475 { "play-audio-sample", do_play_audio_sample },
1476 { "presets-commit", do_presets_commit },
1477 { "log-level", do_log_level },
1478 { "replay", do_replay },
1479 { "decrease-visibility", do_decrease_visibility },
1480 { "increase-visibility", do_increase_visibility },
1481 { "hud-init", do_hud_init },
1482 { "hud-init2", do_hud_init2 },
1483 { "loadxml", do_load_xml_to_proptree},
1484 { "savexml", do_save_xml_from_proptree },
1485 { "press-cockpit-button", do_press_cockpit_button },
1486 { "release-cockpit-button", do_release_cockpit_button },
1487 { "dump-scenegraph", do_dump_scene_graph },
1488 { "dump-terrainbranch", do_dump_terrain_branch },
1489 { 0, 0 } // zero-terminated
1494 * Initialize the default built-in commands.
1496 * Other commands may be added by other parts of the application.
1501 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1502 for (int i = 0; built_ins[i].name != 0; i++) {
1503 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1504 globals->get_commands()->addCommand(built_ins[i].name,
1505 built_ins[i].command);
1508 typedef bool (*dummy)();
1509 fgTie( "/command/view/next", dummy(0), do_view_next );
1510 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1513 // end of fg_commands.cxx