1 // fg_props.cxx -- support for FlightGear properties.
3 // Written by David Megginson, started 2000.
5 // Copyright (C) 2000, 2001 David Megginson - david@megginson.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # include <simgear/compiler.h>
27 #include <simgear/misc/exception.hxx>
28 #include <simgear/magvar/magvar.hxx>
29 #include <simgear/timing/sg_time.hxx>
33 #include <ATC/ATCdisplay.hxx>
34 #include <Aircraft/aircraft.hxx>
35 #include <Time/tmp.hxx>
36 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
37 #ifndef FG_NEW_ENVIRONMENT
38 # include <WeatherCM/FGLocalWeatherDatabase.h>
40 # include <Environment/environment.hxx>
41 #endif // FG_NEW_ENVIRONMENT
42 #include <Objects/matlib.hxx>
46 #include "globals.hxx"
48 #include "fg_props.hxx"
50 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
51 SG_USING_STD(istream);
52 SG_USING_STD(ostream);
55 #if !defined(FG_NEW_ENVIRONMENT)
56 static double getWindNorth ();
57 static double getWindEast ();
58 static double getWindDown ();
59 #endif // FG_NEW_ENVIRONMENT
61 static bool winding_ccw = true; // FIXME: temporary
63 static bool fdm_data_logging = false; // FIXME: temporary
67 ////////////////////////////////////////////////////////////////////////
68 // Default property bindings (not yet handled by any module).
69 ////////////////////////////////////////////////////////////////////////
71 struct LogClassMapping {
74 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
77 LogClassMapping log_class_mappings [] = {
78 LogClassMapping(SG_NONE, "none"),
79 LogClassMapping(SG_TERRAIN, "terrain"),
80 LogClassMapping(SG_ASTRO, "astro"),
81 LogClassMapping(SG_FLIGHT, "flight"),
82 LogClassMapping(SG_INPUT, "input"),
83 LogClassMapping(SG_GL, "gl"),
84 LogClassMapping(SG_VIEW, "view"),
85 LogClassMapping(SG_COCKPIT, "cockpit"),
86 LogClassMapping(SG_GENERAL, "general"),
87 LogClassMapping(SG_MATH, "math"),
88 LogClassMapping(SG_EVENT, "event"),
89 LogClassMapping(SG_AIRCRAFT, "aircraft"),
90 LogClassMapping(SG_AUTOPILOT, "autopilot"),
91 LogClassMapping(SG_IO, "io"),
92 LogClassMapping(SG_CLIPPER, "clipper"),
93 LogClassMapping(SG_NETWORK, "network"),
94 LogClassMapping(SG_UNDEFD, "")
99 * Get the logging classes.
104 sgDebugClass classes = logbuf::get_log_classes();
106 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
107 if ((classes&log_class_mappings[i].c) > 0) {
108 if (result != (string)"")
110 result += log_class_mappings[i].name;
117 static void addLoggingClass (const string &name)
119 sgDebugClass classes = logbuf::get_log_classes();
120 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
121 if (name == log_class_mappings[i].name) {
122 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
126 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown logging class: " << name);
131 * Set the logging classes.
134 setLoggingClasses (string classes)
136 logbuf::set_log_classes(SG_NONE);
138 if (classes == "none") {
139 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
143 if (classes == "" || classes == "all") { // default
144 logbuf::set_log_classes(SG_ALL);
145 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
146 << getLoggingClasses());
150 string rest = classes;
152 int sep = rest.find('|');
154 name = rest.substr(0, sep);
155 rest = rest.substr(sep+1);
156 addLoggingClass(name);
157 sep = rest.find('|');
159 addLoggingClass(rest);
160 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
161 << getLoggingClasses());
166 * Get the logging priority.
169 getLoggingPriority ()
171 switch (logbuf::get_log_priority()) {
183 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
184 << logbuf::get_log_priority());
191 * Set the logging priority.
194 setLoggingPriority (string priority)
196 if (priority == "bulk") {
197 logbuf::set_log_priority(SG_BULK);
198 } else if (priority == "debug") {
199 logbuf::set_log_priority(SG_DEBUG);
200 } else if (priority == "" || priority == "info") { // default
201 logbuf::set_log_priority(SG_INFO);
202 } else if (priority == "warn") {
203 logbuf::set_log_priority(SG_WARN);
204 } else if (priority == "alert") {
205 logbuf::set_log_priority(SG_ALERT);
207 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
209 SG_LOG(SG_GENERAL, SG_INFO, "Logging priority is " << getLoggingPriority());
215 * Get the pause state of the sim.
220 return globals->get_freeze();
225 * Set the pause state of the sim.
228 setFreeze (bool freeze)
230 globals->set_freeze(freeze);
233 current_atcdisplay->CancelRepeatingMessage();
234 current_atcdisplay->RegisterRepeatingMessage("**** SIM IS FROZEN **** SIM IS FROZEN ****");
237 current_atcdisplay->CancelRepeatingMessage();
243 * Return the current aircraft directory (UIUC) as a string.
253 * Set the current aircraft directory (UIUC).
256 setAircraftDir (string dir)
258 if (getAircraftDir() != dir) {
260 // needReinit(); FIXME!!
266 * Return the number of milliseconds elapsed since simulation started.
271 return globals->get_elapsed_time_ms();
276 * Return the current Zulu time.
283 struct tm * t = globals->get_time_params()->getGmt();
284 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
285 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
286 t->tm_hour, t->tm_min, t->tm_sec);
293 * Set the current Zulu time.
296 setDateString (string date_string)
298 static const SGPropertyNode *cur_time_override
299 = fgGetNode("/sim/time/cur-time-override", true);
301 SGTime * st = globals->get_time_params();
302 struct tm * current_time = st->getGmt();
305 // Scan for basic ISO format
306 // YYYY-MM-DDTHH:MM:SS
307 int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
308 &(new_time.tm_year), &(new_time.tm_mon),
309 &(new_time.tm_mday), &(new_time.tm_hour),
310 &(new_time.tm_min), &(new_time.tm_sec));
312 // Be pretty picky about this, so
313 // that strange things don't happen
314 // if the save file has been edited
317 SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
318 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
322 // OK, it looks like we got six
323 // values, one way or another.
324 new_time.tm_year -= 1900;
325 new_time.tm_mon -= 1;
327 // Now, tell flight gear to use
328 // the new time. This was far
329 // too difficult, by the way.
331 mktime(&new_time) - mktime(current_time) + globals->get_warp();
332 double lon = current_aircraft.fdm_state->get_Longitude();
333 double lat = current_aircraft.fdm_state->get_Latitude();
334 globals->set_warp(warp);
335 st->update(lon, lat, cur_time_override->getLongValue(), warp);
336 fgUpdateSkyAndLightingParams();
340 * Return the GMT as a string.
347 struct tm *t = globals->get_time_params()->getGmt();
348 sprintf(buf, " %.2d:%.2d:%.2d",
349 t->tm_hour, t->tm_min, t->tm_sec);
350 // cout << t << " " << buf << endl;
357 * Get the texture rendering state.
362 return (material_lib.get_step() == 0);
367 * Set the texture rendering state.
370 setTextures (bool textures)
373 material_lib.set_step(0);
375 material_lib.set_step(1);
380 * Return the magnetic variation
385 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
390 * Return the magnetic dip
395 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
400 * Return the current heading in degrees.
405 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
409 #if !defined(FG_NEW_ENVIRONMENT)
412 * Get the current visibility (meters).
417 return WeatherDatabase->getWeatherVisibility();
422 * Set the current visibility (meters).
425 setVisibility (double visibility)
427 WeatherDatabase->setWeatherVisibility(visibility);
431 * Get the current wind north velocity (feet/second).
436 return current_aircraft.fdm_state->get_V_north_airmass();
441 * Set the current wind north velocity (feet/second).
444 setWindNorth (double speed)
446 current_aircraft.fdm_state
447 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
452 * Get the current wind east velocity (feet/second).
457 return current_aircraft.fdm_state->get_V_east_airmass();
462 * Set the current wind east velocity (feet/second).
465 setWindEast (double speed)
467 cout << "Set wind-east to " << speed << endl;
468 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
475 * Get the current wind down velocity (feet/second).
480 return current_aircraft.fdm_state->get_V_down_airmass();
485 * Set the current wind down velocity (feet/second).
488 setWindDown (double speed)
490 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
495 #endif // FG_NEW_ENVIRONMENT
500 return globals->get_warp();
506 globals->set_warp(warp);
512 return globals->get_warp_delta();
516 setWarpDelta (long delta)
518 globals->set_warp_delta(delta);
528 setWindingCCW (bool state)
532 glFrontFace ( GL_CCW );
534 glFrontFace ( GL_CW );
540 #if defined(FX) && !defined(WIN32)
541 return globals->get_fullscreen();
548 setFullScreen (bool state)
550 #if defined(FX) && !defined(WIN32)
551 globals->set_fullscreen(state);
552 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
553 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
561 return fdm_data_logging;
565 setFDMDataLogging (bool state)
567 // kludge; no getter or setter available
568 if (state != fdm_data_logging) {
569 fgToggleFDMdataLogging();
570 fdm_data_logging = state;
575 ////////////////////////////////////////////////////////////////////////
576 // Tie the properties.
577 ////////////////////////////////////////////////////////////////////////
583 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
584 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
585 // fgTie("/sim/freeze", getFreeze, setFreeze);
586 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
588 fgTie("/sim/time/elapsed-ms", getElapsedTime_ms);
589 fgTie("/sim/time/gmt", getDateString, setDateString);
590 fgSetArchivable("/sim/time/gmt");
591 fgTie("/sim/time/gmt-string", getGMTString);
592 fgTie("/sim/rendering/textures", getTextures, setTextures);
595 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
598 #if !defined(FG_NEW_ENVIRONMENT)
599 fgTie("/environment/visibility-m", getVisibility, setVisibility);
600 fgSetArchivable("/environment/visibility-m");
601 fgTie("/environment/wind-from-north-fps", getWindNorth, setWindNorth);
602 fgSetArchivable("/environment/wind-from-north-fps");
603 fgTie("/environment/wind-from-east-fps", getWindEast, setWindEast);
604 fgSetArchivable("/environment/wind-from-east-fps");
605 fgTie("/environment/wind-from-down-fps", getWindDown, setWindDown);
606 fgSetArchivable("/environment/wind-from-down-fps");
609 fgTie("/environment/magnetic-variation-deg", getMagVar);
610 fgTie("/environment/magnetic-dip-deg", getMagDip);
612 fgTie("/sim/time/warp", getWarp, setWarp, false);
613 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
615 // Misc. Temporary junk.
616 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
617 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
618 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
630 ////////////////////////////////////////////////////////////////////////
632 ////////////////////////////////////////////////////////////////////////
636 * Save the current state of the simulator to a stream.
639 fgSaveFlight (ostream &output, bool write_all)
642 fgSetBool("/sim/startup/onground", false);
643 fgSetArchivable("/sim/startup/onground");
644 fgSetBool("/sim/startup/trim", false);
645 fgSetArchivable("/sim/startup/trim");
646 fgSetString("/sim/startup/speed-set", "UVW");
647 fgSetArchivable("/sim/startup/speed-set");
650 writeProperties(output, globals->get_props(), write_all);
651 } catch (const sg_exception &e) {
652 guiErrorMessage("Error saving flight: ", e);
660 * Restore the current state of the simulator from a stream.
663 fgLoadFlight (istream &input)
665 SGPropertyNode props;
667 readProperties(input, &props);
668 } catch (const sg_exception &e) {
669 guiErrorMessage("Error reading saved flight: ", e);
673 fgSetBool("/sim/startup/onground", false);
674 fgSetBool("/sim/startup/trim", false);
675 fgSetString("/sim/startup/speed-set", "UVW");
677 copyProperties(&props, globals->get_props());
678 // When loading a flight, make it the
679 // new initial state.
680 globals->saveInitialState();
686 ////////////////////////////////////////////////////////////////////////
687 // Implementation of FGCondition.
688 ////////////////////////////////////////////////////////////////////////
690 FGCondition::FGCondition ()
694 FGCondition::~FGCondition ()
700 ////////////////////////////////////////////////////////////////////////
701 // Implementation of FGPropertyCondition.
702 ////////////////////////////////////////////////////////////////////////
704 FGPropertyCondition::FGPropertyCondition (const string &propname)
705 : _node(fgGetNode(propname, true))
709 FGPropertyCondition::~FGPropertyCondition ()
715 ////////////////////////////////////////////////////////////////////////
716 // Implementation of FGNotCondition.
717 ////////////////////////////////////////////////////////////////////////
719 FGNotCondition::FGNotCondition (FGCondition * condition)
720 : _condition(condition)
724 FGNotCondition::~FGNotCondition ()
730 FGNotCondition::test () const
732 return !(_condition->test());
737 ////////////////////////////////////////////////////////////////////////
738 // Implementation of FGAndCondition.
739 ////////////////////////////////////////////////////////////////////////
741 FGAndCondition::FGAndCondition ()
745 FGAndCondition::~FGAndCondition ()
747 for (unsigned int i = 0; i < _conditions.size(); i++)
748 delete _conditions[i];
752 FGAndCondition::test () const
754 int nConditions = _conditions.size();
755 for (int i = 0; i < nConditions; i++) {
756 if (!_conditions[i]->test())
763 FGAndCondition::addCondition (FGCondition * condition)
765 _conditions.push_back(condition);
770 ////////////////////////////////////////////////////////////////////////
771 // Implementation of FGOrCondition.
772 ////////////////////////////////////////////////////////////////////////
774 FGOrCondition::FGOrCondition ()
778 FGOrCondition::~FGOrCondition ()
780 for (unsigned int i = 0; i < _conditions.size(); i++)
781 delete _conditions[i];
785 FGOrCondition::test () const
787 int nConditions = _conditions.size();
788 for (int i = 0; i < nConditions; i++) {
789 if (_conditions[i]->test())
796 FGOrCondition::addCondition (FGCondition * condition)
798 _conditions.push_back(condition);
803 ////////////////////////////////////////////////////////////////////////
804 // Implementation of FGComparisonCondition.
805 ////////////////////////////////////////////////////////////////////////
808 doComparison (const SGPropertyNode * left, const SGPropertyNode *right)
810 switch (left->getType()) {
811 case SGPropertyNode::BOOL: {
812 bool v1 = left->getBoolValue();
813 bool v2 = right->getBoolValue();
815 return FGComparisonCondition::LESS_THAN;
817 return FGComparisonCondition::GREATER_THAN;
819 return FGComparisonCondition::EQUALS;
822 case SGPropertyNode::INT: {
823 int v1 = left->getIntValue();
824 int v2 = right->getIntValue();
826 return FGComparisonCondition::LESS_THAN;
828 return FGComparisonCondition::GREATER_THAN;
830 return FGComparisonCondition::EQUALS;
833 case SGPropertyNode::LONG: {
834 long v1 = left->getLongValue();
835 long v2 = right->getLongValue();
837 return FGComparisonCondition::LESS_THAN;
839 return FGComparisonCondition::GREATER_THAN;
841 return FGComparisonCondition::EQUALS;
844 case SGPropertyNode::FLOAT: {
845 float v1 = left->getFloatValue();
846 float v2 = right->getFloatValue();
848 return FGComparisonCondition::LESS_THAN;
850 return FGComparisonCondition::GREATER_THAN;
852 return FGComparisonCondition::EQUALS;
855 case SGPropertyNode::DOUBLE: {
856 double v1 = left->getDoubleValue();
857 double v2 = right->getDoubleValue();
859 return FGComparisonCondition::LESS_THAN;
861 return FGComparisonCondition::GREATER_THAN;
863 return FGComparisonCondition::EQUALS;
866 case SGPropertyNode::STRING:
867 case SGPropertyNode::NONE:
868 case SGPropertyNode::UNSPECIFIED: {
869 string v1 = left->getStringValue();
870 string v2 = right->getStringValue();
872 return FGComparisonCondition::LESS_THAN;
874 return FGComparisonCondition::GREATER_THAN;
876 return FGComparisonCondition::EQUALS;
880 throw sg_exception("Unrecognized node type");
885 FGComparisonCondition::FGComparisonCondition (Type type, bool reverse)
894 FGComparisonCondition::~FGComparisonCondition ()
900 FGComparisonCondition::test () const
902 // Always fail if incompletely specified
903 if (_left_property == 0 ||
904 (_right_property == 0 && _right_value == 0))
907 // Get LESS_THAN, EQUALS, or GREATER_THAN
909 doComparison(_left_property,
910 (_right_property != 0 ? _right_property : _right_value));
912 return (cmp == _type);
914 return (cmp != _type);
918 FGComparisonCondition::setLeftProperty (const string &propname)
920 _left_property = fgGetNode(propname, true);
924 FGComparisonCondition::setRightProperty (const string &propname)
928 _right_property = fgGetNode(propname, true);
932 FGComparisonCondition::setRightValue (const SGPropertyNode *node)
936 _right_value = new SGPropertyNode(*node);
941 ////////////////////////////////////////////////////////////////////////
942 // Read a condition and use it if necessary.
943 ////////////////////////////////////////////////////////////////////////
945 // Forward declaration
946 static FGCondition * readCondition (const SGPropertyNode * node);
949 readPropertyCondition (const SGPropertyNode * node)
951 return new FGPropertyCondition(node->getStringValue());
955 readNotCondition (const SGPropertyNode * node)
957 int nChildren = node->nChildren();
958 for (int i = 0; i < nChildren; i++) {
959 const SGPropertyNode * child = node->getChild(i);
960 FGCondition * condition = readCondition(child);
962 return new FGNotCondition(condition);
964 SG_LOG(SG_COCKPIT, SG_ALERT, "Panel: empty 'not' condition");
969 readAndConditions (const SGPropertyNode * node)
971 FGAndCondition * andCondition = new FGAndCondition;
972 int nChildren = node->nChildren();
973 for (int i = 0; i < nChildren; i++) {
974 const SGPropertyNode * child = node->getChild(i);
975 FGCondition * condition = readCondition(child);
977 andCondition->addCondition(condition);
983 readOrConditions (const SGPropertyNode * node)
985 FGOrCondition * orCondition = new FGOrCondition;
986 int nChildren = node->nChildren();
987 for (int i = 0; i < nChildren; i++) {
988 const SGPropertyNode * child = node->getChild(i);
989 FGCondition * condition = readCondition(child);
991 orCondition->addCondition(condition);
997 readComparison (const SGPropertyNode * node,
998 FGComparisonCondition::Type type,
1001 FGComparisonCondition * condition = new FGComparisonCondition(type, reverse);
1002 condition->setLeftProperty(node->getStringValue("property[0]"));
1003 if (node->hasValue("property[1]"))
1004 condition->setRightProperty(node->getStringValue("property[1]"));
1006 condition->setRightValue(node->getChild("value", 0));
1011 static FGCondition *
1012 readCondition (const SGPropertyNode * node)
1014 const string &name = node->getName();
1015 if (name == "property")
1016 return readPropertyCondition(node);
1017 else if (name == "not")
1018 return readNotCondition(node);
1019 else if (name == "and")
1020 return readAndConditions(node);
1021 else if (name == "or")
1022 return readOrConditions(node);
1023 else if (name == "less-than")
1024 return readComparison(node, FGComparisonCondition::LESS_THAN, false);
1025 else if (name == "less-than-equals")
1026 return readComparison(node, FGComparisonCondition::GREATER_THAN, true);
1027 else if (name == "greater-than")
1028 return readComparison(node, FGComparisonCondition::GREATER_THAN, false);
1029 else if (name == "greater-than-equals")
1030 return readComparison(node, FGComparisonCondition::LESS_THAN, true);
1031 else if (name == "equals")
1032 return readComparison(node, FGComparisonCondition::EQUALS, false);
1033 else if (name == "not-equals")
1034 return readComparison(node, FGComparisonCondition::EQUALS, true);
1041 ////////////////////////////////////////////////////////////////////////
1042 // Implementation of FGConditional.
1043 ////////////////////////////////////////////////////////////////////////
1045 FGConditional::FGConditional ()
1050 FGConditional::~FGConditional ()
1056 FGConditional::setCondition (FGCondition * condition)
1059 _condition = condition;
1063 FGConditional::test () const
1065 return ((_condition == 0) || _condition->test());
1070 // The top-level is always an implicit 'and' group
1072 fgReadCondition (const SGPropertyNode * node)
1074 return readAndConditions(node);
1078 // end of fg_props.cxx