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