]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.cxx
b7966352e9aaeabc14efb2f411ddf3d795065898
[flightgear.git] / src / Main / fg_props.cxx
1 // fg_props.cxx -- support for FlightGear properties.
2 //
3 // Written by David Megginson, started 2000.
4 //
5 // Copyright (C) 2000, 2001 David Megginson - david@megginson.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear/compiler.h>
25 #endif
26
27 #include <simgear/misc/exception.hxx>
28
29 #include STL_IOSTREAM
30
31 #include <ATC/ATCdisplay.hxx>
32 #include <Aircraft/aircraft.hxx>
33 #include <Time/tmp.hxx>
34 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
35 #ifndef FG_NEW_ENVIRONMENT
36 #  include <WeatherCM/FGLocalWeatherDatabase.h>
37 #else
38 #  include <Environment/environment.hxx>
39 #endif // FG_NEW_ENVIRONMENT
40 #include <Objects/matlib.hxx>
41
42 #include <GUI/gui.h>
43
44 #include "globals.hxx"
45 #include "fgfs.hxx"
46 #include "fg_props.hxx"
47 #include "viewmgr.hxx"
48
49 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
50 SG_USING_STD(istream);
51 SG_USING_STD(ostream);
52 #endif
53
54 #if !defined(FG_NEW_ENVIRONMENT)
55 static double getWindNorth ();
56 static double getWindEast ();
57 static double getWindDown ();
58 #endif // FG_NEW_ENVIRONMENT
59
60 // Allow the view to be set from two axes (i.e. a joystick hat)
61 // This needs to be in FGViewer itself, somehow.
62 static double axisLong = 0.0;
63 static double axisLat = 0.0;
64
65 static bool winding_ccw = true; // FIXME: temporary
66
67 static bool fdm_data_logging = false; // FIXME: temporary
68
69
70 /**
71  * Utility function.
72  */
73 static inline void
74 _set_view_from_axes ()
75 {
76                                 // Take no action when hat is centered
77   if ( ( axisLong <  0.01 ) &&
78        ( axisLong > -0.01 ) &&
79        ( axisLat  <  0.01 ) &&
80        ( axisLat  > -0.01 )
81      )
82     return;
83
84   double viewDir = 999;
85
86   /* Do all the quick and easy cases */
87   if (axisLong < 0) {           // Longitudinal axis forward
88     if (axisLat == axisLong)
89       viewDir = 45;
90     else if (axisLat == - axisLong)
91       viewDir = 315;
92     else if (axisLat == 0)
93       viewDir = 0;
94   } else if (axisLong > 0) {    // Longitudinal axis backward
95     if (axisLat == - axisLong)
96       viewDir = 135;
97     else if (axisLat == axisLong)
98       viewDir = 225;
99     else if (axisLat == 0)
100       viewDir = 180;
101   } else if (axisLong == 0) {   // Longitudinal axis neutral
102     if (axisLat < 0)
103       viewDir = 90;
104     else if (axisLat > 0)
105       viewDir = 270;
106     else return; /* And assertion failure maybe? */
107   }
108
109   /* Do all the difficult cases */
110   if ( viewDir > 900 )
111     viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axisLat, -axisLong );
112   if ( viewDir < -1 ) viewDir += 360;
113
114 //  SG_LOG(SG_INPUT, SG_ALERT, "Joystick Lat=" << axisLat << "   and Long="
115 //      << axisLong << "  gave angle=" << viewDir );
116
117   globals->get_current_view()->set_goal_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
118 //   globals->get_current_view()->set_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
119 }
120
121 \f
122 ////////////////////////////////////////////////////////////////////////
123 // Default property bindings (not yet handled by any module).
124 ////////////////////////////////////////////////////////////////////////
125
126 struct LogClassMapping {
127   sgDebugClass c;
128   string name;
129   LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
130 };
131
132 LogClassMapping log_class_mappings [] = {
133   LogClassMapping(SG_NONE, "none"),
134   LogClassMapping(SG_TERRAIN, "terrain"),
135   LogClassMapping(SG_ASTRO, "astro"),
136   LogClassMapping(SG_FLIGHT, "flight"),
137   LogClassMapping(SG_INPUT, "input"),
138   LogClassMapping(SG_GL, "gl"),
139   LogClassMapping(SG_VIEW, "view"),
140   LogClassMapping(SG_COCKPIT, "cockpit"),
141   LogClassMapping(SG_GENERAL, "general"),
142   LogClassMapping(SG_MATH, "math"),
143   LogClassMapping(SG_EVENT, "event"),
144   LogClassMapping(SG_AIRCRAFT, "aircraft"),
145   LogClassMapping(SG_AUTOPILOT, "autopilot"),
146   LogClassMapping(SG_IO, "io"),
147   LogClassMapping(SG_CLIPPER, "clipper"),
148   LogClassMapping(SG_NETWORK, "network"),
149   LogClassMapping(SG_UNDEFD, "")
150 };
151
152
153 /**
154  * Get the logging classes.
155  */
156 static string
157 getLoggingClasses ()
158 {
159   sgDebugClass classes = logbuf::get_log_classes();
160   string result = "";
161   for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
162     if ((classes&log_class_mappings[i].c) > 0) {
163       if (result != (string)"")
164         result += '|';
165       result += log_class_mappings[i].name;
166     }
167   }
168   return result;
169 }
170
171
172 static void addLoggingClass (const string &name)
173 {
174   sgDebugClass classes = logbuf::get_log_classes();
175   for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
176     if (name == log_class_mappings[i].name) {
177       logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
178       return;
179     }
180   }
181   SG_LOG(SG_GENERAL, SG_ALERT, "Unknown logging class: " << name);
182 }
183
184
185 /**
186  * Set the logging classes.
187  */
188 static void
189 setLoggingClasses (string classes)
190 {
191   logbuf::set_log_classes(SG_NONE);
192
193   if (classes == "none") {
194     SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
195     return;
196   }
197
198   if (classes == "" || classes == "all") { // default
199     logbuf::set_log_classes(SG_ALL);
200     SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
201            << getLoggingClasses());
202     return;
203   }
204
205   string rest = classes;
206   string name = "";
207   int sep = rest.find('|');
208   while (sep > 0) {
209     name = rest.substr(0, sep);
210     rest = rest.substr(sep+1);
211     addLoggingClass(name);
212     sep = rest.find('|');
213   }
214   addLoggingClass(rest);
215   SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
216          << getLoggingClasses());
217 }
218
219
220 /**
221  * Get the logging priority.
222  */
223 static string
224 getLoggingPriority ()
225 {
226   switch (logbuf::get_log_priority()) {
227   case SG_BULK:
228     return "bulk";
229   case SG_DEBUG:
230     return "debug";
231   case SG_INFO:
232     return "info";
233   case SG_WARN:
234     return "warn";
235   case SG_ALERT:
236     return "alert";
237   default:
238     SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
239            << logbuf::get_log_priority());
240     return "unknown";
241   }
242 }
243
244
245 /**
246  * Set the logging priority.
247  */
248 static void
249 setLoggingPriority (string priority)
250 {
251   if (priority == "bulk") {
252     logbuf::set_log_priority(SG_BULK);
253   } else if (priority == "debug") {
254     logbuf::set_log_priority(SG_DEBUG);
255   } else if (priority == "" || priority == "info") { // default
256     logbuf::set_log_priority(SG_INFO);
257   } else if (priority == "warn") {
258     logbuf::set_log_priority(SG_WARN);
259   } else if (priority == "alert") {
260     logbuf::set_log_priority(SG_ALERT);
261   } else {
262     SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
263   }
264   SG_LOG(SG_GENERAL, SG_INFO, "Logging priority is " << getLoggingPriority());
265 }
266
267
268 #if 0
269 /**
270  * Get the pause state of the sim.
271  */
272 static bool
273 getFreeze ()
274 {
275   return globals->get_freeze();
276 }
277
278
279 /**
280  * Set the pause state of the sim.
281  */
282 static void
283 setFreeze (bool freeze)
284 {
285     globals->set_freeze(freeze);
286     if ( freeze ) {
287         // BusyCursor( 0 );
288         current_atcdisplay->CancelRepeatingMessage();
289         current_atcdisplay->RegisterRepeatingMessage("****    SIM IS FROZEN    ****    SIM IS FROZEN    ****");
290     } else {
291         // BusyCursor( 1 );
292         current_atcdisplay->CancelRepeatingMessage();
293     }
294 }
295 #endif
296
297 /**
298  * Return the current aircraft directory (UIUC) as a string.
299  */
300 static string 
301 getAircraftDir ()
302 {
303   return aircraft_dir;
304 }
305
306
307 /**
308  * Set the current aircraft directory (UIUC).
309  */
310 static void
311 setAircraftDir (string dir)
312 {
313   if (getAircraftDir() != dir) {
314     aircraft_dir = dir;
315 //     needReinit(); FIXME!!
316   }
317 }
318
319
320 /**
321  * Get the current view offset in degrees.
322  */
323 static double
324 getViewOffset ()
325 {
326   return (globals->get_current_view()
327           ->get_view_offset() * SGD_RADIANS_TO_DEGREES);
328 }
329
330
331 static void
332 setViewOffset (double offset)
333 {
334   globals->get_current_view()->set_view_offset(offset * SGD_DEGREES_TO_RADIANS);
335 }
336
337 static double
338 getGoalViewOffset ()
339 {
340   return (globals->get_current_view()
341           ->get_goal_view_offset() * SGD_RADIANS_TO_DEGREES);
342 }
343
344 static void
345 setGoalViewOffset (double offset)
346 {
347     while ( offset < 0 ) {
348         offset += 360.0;
349     }
350     while ( offset > 360.0 ) {
351         offset -= 360.0;
352     }
353     // Snap to center if we are close
354     if ( fabs(offset) < 1.0 ||  fabs(offset) > 359.0 ) {
355         offset = 0.0;
356     }
357
358     globals->get_current_view()
359         ->set_goal_view_offset(offset * SGD_DEGREES_TO_RADIANS);
360 }
361
362 /**
363  * Get the current view tilt in degrees.
364  */
365 static double
366 getViewTilt ()
367 {
368   return (globals->get_current_view()
369           ->get_view_tilt() * SGD_RADIANS_TO_DEGREES);
370 }
371
372
373 static void
374 setViewTilt (double tilt)
375 {
376   globals->get_current_view()->set_view_tilt(tilt * SGD_DEGREES_TO_RADIANS);
377 }
378
379 static double
380 getGoalViewTilt ()
381 {
382   return (globals->get_current_view()
383           ->get_goal_view_tilt() * SGD_RADIANS_TO_DEGREES);
384 }
385
386 static void
387 setGoalViewTilt (double tilt)
388 {
389     while ( tilt < 0 ) {
390         tilt += 360.0;
391     }
392     while ( tilt > 360.0 ) {
393         tilt -= 360.0;
394     }
395     // Snap to center if we are close
396     if ( fabs(tilt) < 1.0 ||  fabs(tilt) > 359.0 ) {
397         tilt = 0.0;
398     }
399
400     globals->get_current_view()
401         ->set_goal_view_tilt(tilt * SGD_DEGREES_TO_RADIANS);
402 }
403
404
405 /**
406  * Pilot position offset from CG.
407  */
408 static float
409 getPilotPositionXOffset ()
410 {
411   FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
412   float * offset = pilot_view->get_pilot_offset();
413   return offset[0];
414 }
415
416 static void
417 setPilotPositionXOffset (float x)
418 {
419   FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
420   float * offset = pilot_view->get_pilot_offset();
421   pilot_view->set_pilot_offset(x, offset[1], offset[2]);
422 }
423
424 static float
425 getPilotPositionYOffset ()
426 {
427   FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
428   float * offset = pilot_view->get_pilot_offset();
429   return offset[1];
430 }
431
432 static void
433 setPilotPositionYOffset (float y)
434 {
435   FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
436   float * offset = pilot_view->get_pilot_offset();
437   pilot_view->set_pilot_offset(offset[0], y, offset[2]);
438 }
439
440 static float
441 getPilotPositionZOffset ()
442 {
443   FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
444   float * offset = pilot_view->get_pilot_offset();
445   return offset[2];
446 }
447
448 static void
449 setPilotPositionZOffset (float z)
450 {
451   FGViewer * pilot_view = globals->get_viewmgr()->get_view(0);
452   float * offset = pilot_view->get_pilot_offset();
453   pilot_view->set_pilot_offset(offset[0], offset[1], z);
454 }
455
456
457 /**
458  * Return the current Zulu time.
459  */
460 static string 
461 getDateString ()
462 {
463   string out;
464   char buf[64];
465   struct tm * t = globals->get_time_params()->getGmt();
466   sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
467           t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
468           t->tm_hour, t->tm_min, t->tm_sec);
469   out = buf;
470   return out;
471 }
472
473
474 /**
475  * Set the current Zulu time.
476  */
477 static void
478 setDateString (string date_string)
479 {
480   static const SGPropertyNode *cur_time_override
481         = fgGetNode("/sim/time/cur-time-override", true);
482
483   SGTime * st = globals->get_time_params();
484   struct tm * current_time = st->getGmt();
485   struct tm new_time;
486
487                                 // Scan for basic ISO format
488                                 // YYYY-MM-DDTHH:MM:SS
489   int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
490                    &(new_time.tm_year), &(new_time.tm_mon),
491                    &(new_time.tm_mday), &(new_time.tm_hour),
492                    &(new_time.tm_min), &(new_time.tm_sec));
493
494                                 // Be pretty picky about this, so
495                                 // that strange things don't happen
496                                 // if the save file has been edited
497                                 // by hand.
498   if (ret != 6) {
499     SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
500            << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
501     return;
502   }
503
504                                 // OK, it looks like we got six
505                                 // values, one way or another.
506   new_time.tm_year -= 1900;
507   new_time.tm_mon -= 1;
508
509                                 // Now, tell flight gear to use
510                                 // the new time.  This was far
511                                 // too difficult, by the way.
512   long int warp =
513     mktime(&new_time) - mktime(current_time) + globals->get_warp();
514   double lon = current_aircraft.fdm_state->get_Longitude();
515   double lat = current_aircraft.fdm_state->get_Latitude();
516   globals->set_warp(warp);
517   st->update(lon, lat, cur_time_override->getLongValue(), warp);
518   fgUpdateSkyAndLightingParams();
519 }
520
521 /**
522  * Return the GMT as a string.
523  */
524 static string 
525 getGMTString ()
526 {
527   string out;
528   char buf[16];
529   struct tm *t = globals->get_time_params()->getGmt();
530   sprintf(buf, " %.2d:%.2d:%.2d",
531           t->tm_hour, t->tm_min, t->tm_sec);
532   // cout << t << " " << buf << endl;
533   out = buf;
534   return out;
535 }
536
537
538 /**
539  * Get the texture rendering state.
540  */
541 static bool
542 getTextures ()
543 {
544   return (material_lib.get_step() == 0);
545 }
546
547
548 /**
549  * Set the texture rendering state.
550  */
551 static void
552 setTextures (bool textures)
553 {
554   if (textures)
555     material_lib.set_step(0);
556   else
557     material_lib.set_step(1);
558 }
559
560
561 /**
562  * Return the magnetic variation
563  */
564 static double
565 getMagVar ()
566 {
567   return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
568 }
569
570
571 /**
572  * Return the magnetic dip
573  */
574 static double
575 getMagDip ()
576 {
577   return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
578 }
579
580
581 /**
582  * Return the current heading in degrees.
583  */
584 static double
585 getHeadingMag ()
586 {
587   return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
588 }
589
590
591 #if !defined(FG_NEW_ENVIRONMENT)
592
593 /**
594  * Get the current visibility (meters).
595  */
596 static double
597 getVisibility ()
598 {
599   return WeatherDatabase->getWeatherVisibility();
600 }
601
602
603 /**
604  * Set the current visibility (meters).
605  */
606 static void
607 setVisibility (double visibility)
608 {
609   WeatherDatabase->setWeatherVisibility(visibility);
610 }
611
612 /**
613  * Get the current wind north velocity (feet/second).
614  */
615 static double
616 getWindNorth ()
617 {
618   return current_aircraft.fdm_state->get_V_north_airmass();
619 }
620
621
622 /**
623  * Set the current wind north velocity (feet/second).
624  */
625 static void
626 setWindNorth (double speed)
627 {
628   current_aircraft.fdm_state
629     ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
630 }
631
632
633 /**
634  * Get the current wind east velocity (feet/second).
635  */
636 static double
637 getWindEast ()
638 {
639   return current_aircraft.fdm_state->get_V_east_airmass();
640 }
641
642
643 /**
644  * Set the current wind east velocity (feet/second).
645  */
646 static void
647 setWindEast (double speed)
648 {
649   cout << "Set wind-east to " << speed << endl;
650   current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
651                                                            speed,
652                                                            getWindDown());
653 }
654
655
656 /**
657  * Get the current wind down velocity (feet/second).
658  */
659 static double
660 getWindDown ()
661 {
662   return current_aircraft.fdm_state->get_V_down_airmass();
663 }
664
665
666 /**
667  * Set the current wind down velocity (feet/second).
668  */
669 static void
670 setWindDown (double speed)
671 {
672   current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
673                                                            getWindEast(),
674                                                            speed);
675 }
676
677 #endif // FG_NEW_ENVIRONMENT
678
679 static double
680 getFOV ()
681 {
682   return globals->get_current_view()->get_fov();
683 }
684
685 static void
686 setFOV (double fov)
687 {
688   if ( fov < 180 ) {
689       globals->get_current_view()->set_fov( fov );
690   }
691 }
692
693 static long
694 getWarp ()
695 {
696   return globals->get_warp();
697 }
698
699 static void
700 setWarp (long warp)
701 {
702   globals->set_warp(warp);
703 }
704
705 static long
706 getWarpDelta ()
707 {
708   return globals->get_warp_delta();
709 }
710
711 static void
712 setWarpDelta (long delta)
713 {
714   globals->set_warp_delta(delta);
715 }
716
717 static void
718 setViewAxisLong (double axis)
719 {
720   axisLong = axis;
721 }
722
723 static void
724 setViewAxisLat (double axis)
725 {
726   axisLat = axis;
727 }
728
729 static bool
730 getWindingCCW ()
731 {
732   return winding_ccw;
733 }
734
735 static void
736 setWindingCCW (bool state)
737 {
738   winding_ccw = state;
739   if ( winding_ccw )
740     glFrontFace ( GL_CCW );
741   else
742     glFrontFace ( GL_CW );
743 }
744
745 static bool
746 getFullScreen ()
747 {
748 #if defined(FX) && !defined(WIN32)
749   return globals->get_fullscreen();
750 #else
751   return false;
752 #endif
753 }
754
755 static void
756 setFullScreen (bool state)
757 {
758 #if defined(FX) && !defined(WIN32)
759   globals->set_fullscreen(state);
760 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
761   XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
762 #  endif
763 #endif
764 }
765
766 static bool
767 getFDMDataLogging ()
768 {
769   return fdm_data_logging;
770 }
771
772 static void
773 setFDMDataLogging (bool state)
774 {
775                                 // kludge; no getter or setter available
776   if (state != fdm_data_logging) {
777     fgToggleFDMdataLogging();
778     fdm_data_logging = state;
779   }
780 }
781
782 \f
783 ////////////////////////////////////////////////////////////////////////
784 // Tie the properties.
785 ////////////////////////////////////////////////////////////////////////
786
787 void
788 fgInitProps ()
789 {
790                                 // Simulation
791   fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
792   fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
793   // fgTie("/sim/freeze", getFreeze, setFreeze);
794   fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
795   fgTie("/sim/view/offset-deg", getViewOffset, setViewOffset, false);
796   fgSetArchivable("/sim/view/offset-deg");
797   fgTie("/sim/view/goal-offset-deg", getGoalViewOffset, setGoalViewOffset, false);
798   fgTie("/sim/view/tilt-deg", getViewTilt, setViewTilt, false);
799   fgSetArchivable("/sim/view/tilt-deg");
800   fgTie("/sim/view/goal-tilt-deg", getGoalViewTilt, setGoalViewTilt, false);
801   fgSetArchivable("/sim/view/goal-offset-deg");
802   fgTie("/sim/view/pilot/x-offset-m",
803         getPilotPositionXOffset, setPilotPositionXOffset);
804   fgSetArchivable("/sim/view/pilot/x-offset-m");
805   fgTie("/sim/view/pilot/y-offset-m",
806         getPilotPositionYOffset, setPilotPositionYOffset);
807   fgSetArchivable("/sim/view/pilot/y-offset-m");
808   fgTie("/sim/view/pilot/z-offset-m",
809         getPilotPositionZOffset, setPilotPositionZOffset);
810   fgSetArchivable("/sim/view/pilot/z-offset-m");
811   fgTie("/sim/time/gmt", getDateString, setDateString);
812   fgSetArchivable("/sim/time/gmt");
813   fgTie("/sim/time/gmt-string", getGMTString);
814   fgTie("/sim/rendering/textures", getTextures, setTextures);
815
816                                 // Orientation
817   fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
818
819                                 // Environment
820 #if !defined(FG_NEW_ENVIRONMENT)
821   fgTie("/environment/visibility-m", getVisibility, setVisibility);
822   fgSetArchivable("/environment/visibility-m");
823   fgTie("/environment/wind-from-north-fps", getWindNorth, setWindNorth);
824   fgSetArchivable("/environment/wind-from-north-fps");
825   fgTie("/environment/wind-from-east-fps", getWindEast, setWindEast);
826   fgSetArchivable("/environment/wind-from-east-fps");
827   fgTie("/environment/wind-from-down-fps", getWindDown, setWindDown);
828   fgSetArchivable("/environment/wind-from-down-fps");
829 #endif
830
831   fgTie("/environment/magnetic-variation-deg", getMagVar);
832   fgTie("/environment/magnetic-dip-deg", getMagDip);
833
834                                 // View
835   fgTie("/sim/field-of-view", getFOV, setFOV);
836   fgSetArchivable("/sim/field-of-view");
837   fgTie("/sim/time/warp", getWarp, setWarp, false);
838   fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
839   fgTie("/sim/view/axes/long", (double(*)())0, setViewAxisLong);
840   fgTie("/sim/view/axes/lat", (double(*)())0, setViewAxisLat);
841
842                                 // Misc. Temporary junk.
843   fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
844   fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
845   fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
846         
847 }
848
849
850 void
851 fgUpdateProps ()
852 {
853   _set_view_from_axes();
854 }
855
856
857 \f
858 ////////////////////////////////////////////////////////////////////////
859 // Save and restore.
860 ////////////////////////////////////////////////////////////////////////
861
862
863 /**
864  * Save the current state of the simulator to a stream.
865  */
866 bool
867 fgSaveFlight (ostream &output)
868 {
869   try {
870     writeProperties(output, globals->get_props());
871   } catch (const sg_exception &e) {
872     guiErrorMessage("Error saving flight: ", e);
873     return false;
874   }
875   return true;
876 }
877
878
879 /**
880  * Restore the current state of the simulator from a stream.
881  */
882 bool
883 fgLoadFlight (istream &input)
884 {
885   SGPropertyNode props;
886   try {
887     readProperties(input, &props);
888   } catch (const sg_exception &e) {
889     guiErrorMessage("Error reading saved flight: ", e);
890     return false;
891   }
892   copyProperties(&props, globals->get_props());
893   // When loading a flight, make it the
894   // new initial state.
895   globals->saveInitialState();
896   return true;
897 }
898
899
900 \f
901 ////////////////////////////////////////////////////////////////////////
902 // Implementation of FGCondition.
903 ////////////////////////////////////////////////////////////////////////
904
905 FGCondition::FGCondition ()
906 {
907 }
908
909 FGCondition::~FGCondition ()
910 {
911 }
912
913
914 \f
915 ////////////////////////////////////////////////////////////////////////
916 // Implementation of FGPropertyCondition.
917 ////////////////////////////////////////////////////////////////////////
918
919 FGPropertyCondition::FGPropertyCondition (const string &propname)
920   : _node(fgGetNode(propname, true))
921 {
922 }
923
924 FGPropertyCondition::~FGPropertyCondition ()
925 {
926 }
927
928
929 \f
930 ////////////////////////////////////////////////////////////////////////
931 // Implementation of FGNotCondition.
932 ////////////////////////////////////////////////////////////////////////
933
934 FGNotCondition::FGNotCondition (FGCondition * condition)
935   : _condition(condition)
936 {
937 }
938
939 FGNotCondition::~FGNotCondition ()
940 {
941   delete _condition;
942 }
943
944 bool
945 FGNotCondition::test () const
946 {
947   return !(_condition->test());
948 }
949
950
951 \f
952 ////////////////////////////////////////////////////////////////////////
953 // Implementation of FGAndCondition.
954 ////////////////////////////////////////////////////////////////////////
955
956 FGAndCondition::FGAndCondition ()
957 {
958 }
959
960 FGAndCondition::~FGAndCondition ()
961 {
962   for (unsigned int i = 0; i < _conditions.size(); i++)
963     delete _conditions[i];
964 }
965
966 bool
967 FGAndCondition::test () const
968 {
969   int nConditions = _conditions.size();
970   for (int i = 0; i < nConditions; i++) {
971     if (!_conditions[i]->test())
972       return false;
973   }
974   return true;
975 }
976
977 void
978 FGAndCondition::addCondition (FGCondition * condition)
979 {
980   _conditions.push_back(condition);
981 }
982
983
984 \f
985 ////////////////////////////////////////////////////////////////////////
986 // Implementation of FGOrCondition.
987 ////////////////////////////////////////////////////////////////////////
988
989 FGOrCondition::FGOrCondition ()
990 {
991 }
992
993 FGOrCondition::~FGOrCondition ()
994 {
995   for (unsigned int i = 0; i < _conditions.size(); i++)
996     delete _conditions[i];
997 }
998
999 bool
1000 FGOrCondition::test () const
1001 {
1002   int nConditions = _conditions.size();
1003   for (int i = 0; i < nConditions; i++) {
1004     if (_conditions[i]->test())
1005       return true;
1006   }
1007   return false;
1008 }
1009
1010 void
1011 FGOrCondition::addCondition (FGCondition * condition)
1012 {
1013   _conditions.push_back(condition);
1014 }
1015
1016
1017 \f
1018 ////////////////////////////////////////////////////////////////////////
1019 // Implementation of FGComparisonCondition.
1020 ////////////////////////////////////////////////////////////////////////
1021
1022 static int
1023 doComparison (const SGPropertyNode * left, const SGPropertyNode *right)
1024 {
1025   switch (left->getType()) {
1026   case SGPropertyNode::BOOL: {
1027     bool v1 = left->getBoolValue();
1028     bool v2 = right->getBoolValue();
1029     if (v1 < v2)
1030       return FGComparisonCondition::LESS_THAN;
1031     else if (v1 > v2)
1032       return FGComparisonCondition::GREATER_THAN;
1033     else
1034       return FGComparisonCondition::EQUALS;
1035     break;
1036   }
1037   case SGPropertyNode::INT: {
1038     int v1 = left->getIntValue();
1039     int v2 = right->getIntValue();
1040     if (v1 < v2)
1041       return FGComparisonCondition::LESS_THAN;
1042     else if (v1 > v2)
1043       return FGComparisonCondition::GREATER_THAN;
1044     else
1045       return FGComparisonCondition::EQUALS;
1046     break;
1047   }
1048   case SGPropertyNode::LONG: {
1049     long v1 = left->getLongValue();
1050     long v2 = right->getLongValue();
1051     if (v1 < v2)
1052       return FGComparisonCondition::LESS_THAN;
1053     else if (v1 > v2)
1054       return FGComparisonCondition::GREATER_THAN;
1055     else
1056       return FGComparisonCondition::EQUALS;
1057     break;
1058   }
1059   case SGPropertyNode::FLOAT: {
1060     float v1 = left->getFloatValue();
1061     float v2 = right->getFloatValue();
1062     if (v1 < v2)
1063       return FGComparisonCondition::LESS_THAN;
1064     else if (v1 > v2)
1065       return FGComparisonCondition::GREATER_THAN;
1066     else
1067       return FGComparisonCondition::EQUALS;
1068     break;
1069   }
1070   case SGPropertyNode::DOUBLE: {
1071     double v1 = left->getDoubleValue();
1072     double v2 = right->getDoubleValue();
1073     if (v1 < v2)
1074       return FGComparisonCondition::LESS_THAN;
1075     else if (v1 > v2)
1076       return FGComparisonCondition::GREATER_THAN;
1077     else
1078       return FGComparisonCondition::EQUALS;
1079     break;
1080   }
1081   case SGPropertyNode::STRING: 
1082   case SGPropertyNode::NONE:
1083   case SGPropertyNode::UNSPECIFIED: {
1084     string v1 = left->getStringValue();
1085     string v2 = right->getStringValue();
1086     if (v1 < v2)
1087       return FGComparisonCondition::LESS_THAN;
1088     else if (v1 > v2)
1089       return FGComparisonCondition::GREATER_THAN;
1090     else
1091       return FGComparisonCondition::EQUALS;
1092     break;
1093   }
1094   }
1095   throw sg_exception("Unrecognized node type");
1096   return 0;
1097 }
1098
1099
1100 FGComparisonCondition::FGComparisonCondition (Type type, bool reverse)
1101   : _type(type),
1102     _reverse(reverse),
1103     _left_property(0),
1104     _right_property(0),
1105     _right_value(0)
1106 {
1107 }
1108
1109 FGComparisonCondition::~FGComparisonCondition ()
1110 {
1111   delete _right_value;
1112 }
1113
1114 bool
1115 FGComparisonCondition::test () const
1116 {
1117                                 // Always fail if incompletely specified
1118   if (_left_property == 0 ||
1119       (_right_property == 0 && _right_value == 0))
1120     return false;
1121
1122                                 // Get LESS_THAN, EQUALS, or GREATER_THAN
1123   int cmp =
1124     doComparison(_left_property,
1125                  (_right_property != 0 ? _right_property : _right_value));
1126   if (!_reverse)
1127     return (cmp == _type);
1128   else
1129     return (cmp != _type);
1130 }
1131
1132 void
1133 FGComparisonCondition::setLeftProperty (const string &propname)
1134 {
1135   _left_property = fgGetNode(propname, true);
1136 }
1137
1138 void
1139 FGComparisonCondition::setRightProperty (const string &propname)
1140 {
1141   delete _right_value;
1142   _right_value = 0;
1143   _right_property = fgGetNode(propname, true);
1144 }
1145
1146 void
1147 FGComparisonCondition::setRightValue (const SGPropertyNode *node)
1148 {
1149   _right_property = 0;
1150   delete _right_value;
1151   _right_value = new SGPropertyNode(*node);
1152 }
1153
1154
1155 \f
1156 ////////////////////////////////////////////////////////////////////////
1157 // Read a condition and use it if necessary.
1158 ////////////////////////////////////////////////////////////////////////
1159
1160                                 // Forward declaration
1161 static FGCondition * readCondition (const SGPropertyNode * node);
1162
1163 static FGCondition *
1164 readPropertyCondition (const SGPropertyNode * node)
1165 {
1166   return new FGPropertyCondition(node->getStringValue());
1167 }
1168
1169 static FGCondition *
1170 readNotCondition (const SGPropertyNode * node)
1171 {
1172   int nChildren = node->nChildren();
1173   for (int i = 0; i < nChildren; i++) {
1174     const SGPropertyNode * child = node->getChild(i);
1175     FGCondition * condition = readCondition(child);
1176     if (condition != 0)
1177       return new FGNotCondition(condition);
1178   }
1179   SG_LOG(SG_COCKPIT, SG_ALERT, "Panel: empty 'not' condition");
1180   return 0;
1181 }
1182
1183 static FGCondition *
1184 readAndConditions (const SGPropertyNode * node)
1185 {
1186   FGAndCondition * andCondition = new FGAndCondition;
1187   int nChildren = node->nChildren();
1188   for (int i = 0; i < nChildren; i++) {
1189     const SGPropertyNode * child = node->getChild(i);
1190     FGCondition * condition = readCondition(child);
1191     if (condition != 0)
1192       andCondition->addCondition(condition);
1193   }
1194   return andCondition;
1195 }
1196
1197 static FGCondition *
1198 readOrConditions (const SGPropertyNode * node)
1199 {
1200   FGOrCondition * orCondition = new FGOrCondition;
1201   int nChildren = node->nChildren();
1202   for (int i = 0; i < nChildren; i++) {
1203     const SGPropertyNode * child = node->getChild(i);
1204     FGCondition * condition = readCondition(child);
1205     if (condition != 0)
1206       orCondition->addCondition(condition);
1207   }
1208   return orCondition;
1209 }
1210
1211 static FGCondition *
1212 readComparison (const SGPropertyNode * node,
1213                 FGComparisonCondition::Type type,
1214                 bool reverse)
1215 {
1216   FGComparisonCondition * condition = new FGComparisonCondition(type, reverse);
1217   condition->setLeftProperty(node->getStringValue("property[0]"));
1218   if (node->hasValue("property[1]"))
1219     condition->setRightProperty(node->getStringValue("property[1]"));
1220   else
1221     condition->setRightValue(node->getChild("value", 0));
1222
1223   return condition;
1224 }
1225
1226 static FGCondition *
1227 readCondition (const SGPropertyNode * node)
1228 {
1229   const string &name = node->getName();
1230   if (name == "property")
1231     return readPropertyCondition(node);
1232   else if (name == "not")
1233     return readNotCondition(node);
1234   else if (name == "and")
1235     return readAndConditions(node);
1236   else if (name == "or")
1237     return readOrConditions(node);
1238   else if (name == "less-than")
1239     return readComparison(node, FGComparisonCondition::LESS_THAN, false);
1240   else if (name == "less-than-equals")
1241     return readComparison(node, FGComparisonCondition::GREATER_THAN, true);
1242   else if (name == "greater-than")
1243     return readComparison(node, FGComparisonCondition::GREATER_THAN, false);
1244   else if (name == "greater-than-equals")
1245     return readComparison(node, FGComparisonCondition::LESS_THAN, true);
1246   else if (name == "equals")
1247     return readComparison(node, FGComparisonCondition::EQUALS, false);
1248   else if (name == "not-equals")
1249     return readComparison(node, FGComparisonCondition::EQUALS, true);
1250   else
1251     return 0;
1252 }
1253
1254
1255 \f
1256 ////////////////////////////////////////////////////////////////////////
1257 // Implementation of FGConditional.
1258 ////////////////////////////////////////////////////////////////////////
1259
1260 FGConditional::FGConditional ()
1261   : _condition (0)
1262 {
1263 }
1264
1265 FGConditional::~FGConditional ()
1266 {
1267   delete _condition;
1268 }
1269
1270 void
1271 FGConditional::setCondition (FGCondition * condition)
1272 {
1273   delete _condition;
1274   _condition = condition;
1275 }
1276
1277 bool
1278 FGConditional::test () const
1279 {
1280   return ((_condition == 0) || _condition->test());
1281 }
1282
1283
1284 \f
1285 // The top-level is always an implicit 'and' group
1286 FGCondition *
1287 fgReadCondition (const SGPropertyNode * node)
1288 {
1289   return readAndConditions(node);
1290 }
1291
1292
1293 // end of fg_props.cxx