]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.cxx
Add the AIModel based air traffic subsystem from Durk Talsma.
[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/structure/exception.hxx>
28 #include <simgear/magvar/magvar.hxx>
29 #include <simgear/timing/sg_time.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/scene/material/matlib.hxx>
32 #include <simgear/sound/soundmgr_openal.hxx>
33
34 #include STL_IOSTREAM
35
36 #include <ATC/ATCdisplay.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Time/tmp.hxx>
39 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
40 #include <Environment/environment.hxx>
41
42 #include <GUI/gui.h>
43
44 #include "globals.hxx"
45 #include "fg_props.hxx"
46
47 SG_USING_STD(istream);
48 SG_USING_STD(ostream);
49
50 static bool winding_ccw = true; // FIXME: temporary
51
52 static bool fdm_data_logging = false; // FIXME: temporary
53
54 static bool frozen = false;     // FIXME: temporary
55
56
57 \f
58 ////////////////////////////////////////////////////////////////////////
59 // Default property bindings (not yet handled by any module).
60 ////////////////////////////////////////////////////////////////////////
61
62 struct LogClassMapping {
63   sgDebugClass c;
64   string name;
65   LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
66 };
67
68 LogClassMapping log_class_mappings [] = {
69   LogClassMapping(SG_NONE, "none"),
70   LogClassMapping(SG_TERRAIN, "terrain"),
71   LogClassMapping(SG_ASTRO, "astro"),
72   LogClassMapping(SG_FLIGHT, "flight"),
73   LogClassMapping(SG_INPUT, "input"),
74   LogClassMapping(SG_GL, "gl"),
75   LogClassMapping(SG_VIEW, "view"),
76   LogClassMapping(SG_COCKPIT, "cockpit"),
77   LogClassMapping(SG_GENERAL, "general"),
78   LogClassMapping(SG_MATH, "math"),
79   LogClassMapping(SG_EVENT, "event"),
80   LogClassMapping(SG_AIRCRAFT, "aircraft"),
81   LogClassMapping(SG_AUTOPILOT, "autopilot"),
82   LogClassMapping(SG_IO, "io"),
83   LogClassMapping(SG_CLIPPER, "clipper"),
84   LogClassMapping(SG_NETWORK, "network"),
85   LogClassMapping(SG_UNDEFD, "")
86 };
87
88
89 /**
90  * Get the logging classes.
91  */
92 static const char *
93 getLoggingClasses ()
94 {
95   sgDebugClass classes = logbuf::get_log_classes();
96   static string result = "";    // FIXME
97   for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
98     if ((classes&log_class_mappings[i].c) > 0) {
99       if (!result.empty())
100         result += '|';
101       result += log_class_mappings[i].name;
102     }
103   }
104   return result.c_str();
105 }
106
107
108 static void
109 addLoggingClass (const string &name)
110 {
111   sgDebugClass classes = logbuf::get_log_classes();
112   for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
113     if (name == log_class_mappings[i].name) {
114       logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
115       return;
116     }
117   }
118   SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
119 }
120
121
122 /**
123  * Set the logging classes.
124  */
125 static void
126 setLoggingClasses (const char * c)
127 {
128   string classes = c;
129   logbuf::set_log_classes(SG_NONE);
130
131   if (classes == "none") {
132     SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
133     return;
134   }
135
136   if (classes.empty() || classes == "all") { // default
137     logbuf::set_log_classes(SG_ALL);
138     SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
139            << getLoggingClasses());
140     return;
141   }
142
143   string rest = classes;
144   string name = "";
145   int sep = rest.find('|');
146   while (sep > 0) {
147     name = rest.substr(0, sep);
148     rest = rest.substr(sep+1);
149     addLoggingClass(name);
150     sep = rest.find('|');
151   }
152   addLoggingClass(rest);
153   SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
154          << getLoggingClasses());
155 }
156
157
158 /**
159  * Get the logging priority.
160  */
161 static const char *
162 getLoggingPriority ()
163 {
164   switch (logbuf::get_log_priority()) {
165   case SG_BULK:
166     return "bulk";
167   case SG_DEBUG:
168     return "debug";
169   case SG_INFO:
170     return "info";
171   case SG_WARN:
172     return "warn";
173   case SG_ALERT:
174     return "alert";
175   default:
176     SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
177            << logbuf::get_log_priority());
178     return "unknown";
179   }
180 }
181
182
183 /**
184  * Set the logging priority.
185  */
186 static void
187 setLoggingPriority (const char * p)
188 {
189   if (p == 0)
190       return;
191   string priority = p;
192   if (priority == "bulk") {
193     logbuf::set_log_priority(SG_BULK);
194   } else if (priority == "debug") {
195     logbuf::set_log_priority(SG_DEBUG);
196   } else if (priority.empty() || priority == "info") { // default
197     logbuf::set_log_priority(SG_INFO);
198   } else if (priority == "warn") {
199     logbuf::set_log_priority(SG_WARN);
200   } else if (priority == "alert") {
201     logbuf::set_log_priority(SG_ALERT);
202   } else {
203     SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
204   }
205   SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
206 }
207
208
209 /**
210  * Return the current frozen state.
211  */
212 static bool
213 getFreeze ()
214 {
215   return frozen;
216 }
217
218
219 /**
220  * Set the current frozen state.
221  */
222 static void
223 setFreeze (bool f)
224 {
225     frozen = f;
226
227     // Stop sound on a pause
228     SGSoundMgr *s = globals->get_soundmgr();
229     if ( s != NULL ) {
230         if ( f ) {
231             s->pause();
232         } else {
233             s->resume();
234         }
235     }
236 }
237
238
239 /**
240  * Return the current aircraft directory (UIUC) as a string.
241  */
242 static const char *
243 getAircraftDir ()
244 {
245   return aircraft_dir.c_str();
246 }
247
248
249 /**
250  * Set the current aircraft directory (UIUC).
251  */
252 static void
253 setAircraftDir (const char * dir)
254 {
255   aircraft_dir = dir;
256 }
257
258
259 /**
260  * Return the number of milliseconds elapsed since simulation started.
261  */
262 static double
263 getElapsedTime_sec ()
264 {
265   return globals->get_sim_time_sec();
266 }
267
268
269 /**
270  * Return the current Zulu time.
271  */
272 static const char *
273 getDateString ()
274 {
275   static char buf[64];          // FIXME
276   struct tm * t = globals->get_time_params()->getGmt();
277   sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
278           t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
279           t->tm_hour, t->tm_min, t->tm_sec);
280   return buf;
281 }
282
283
284 /**
285  * Set the current Zulu time.
286  */
287 static void
288 setDateString (const char * date_string)
289 {
290   static const SGPropertyNode *cur_time_override
291         = fgGetNode("/sim/time/cur-time-override", true);
292
293   SGTime * st = globals->get_time_params();
294   struct tm * current_time = st->getGmt();
295   struct tm new_time;
296
297                                 // Scan for basic ISO format
298                                 // YYYY-MM-DDTHH:MM:SS
299   int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
300                    &(new_time.tm_year), &(new_time.tm_mon),
301                    &(new_time.tm_mday), &(new_time.tm_hour),
302                    &(new_time.tm_min), &(new_time.tm_sec));
303
304                                 // Be pretty picky about this, so
305                                 // that strange things don't happen
306                                 // if the save file has been edited
307                                 // by hand.
308   if (ret != 6) {
309     SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
310            << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
311     return;
312   }
313
314                                 // OK, it looks like we got six
315                                 // values, one way or another.
316   new_time.tm_year -= 1900;
317   new_time.tm_mon -= 1;
318
319                                 // Now, tell flight gear to use
320                                 // the new time.  This was far
321                                 // too difficult, by the way.
322   long int warp =
323     mktime(&new_time) - mktime(current_time) + globals->get_warp();
324   double lon = current_aircraft.fdm_state->get_Longitude();
325   double lat = current_aircraft.fdm_state->get_Latitude();
326   globals->set_warp(warp);
327   st->update(lon, lat, cur_time_override->getLongValue(), warp);
328 }
329
330 /**
331  * Return the GMT as a string.
332  */
333 static const char *
334 getGMTString ()
335 {
336   static char buf[16];          // FIXME
337   struct tm *t = globals->get_time_params()->getGmt();
338   sprintf(buf, " %.2d:%.2d:%.2d",
339           t->tm_hour, t->tm_min, t->tm_sec);
340   // cout << t << " " << buf << endl;
341   return buf;
342 }
343
344 /**
345  * Return the magnetic variation
346  */
347 static double
348 getMagVar ()
349 {
350   return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
351 }
352
353
354 /**
355  * Return the magnetic dip
356  */
357 static double
358 getMagDip ()
359 {
360   return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
361 }
362
363
364 /**
365  * Return the current heading in degrees.
366  */
367 static double
368 getHeadingMag ()
369 {
370   double magheading;
371   magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
372   if (magheading < 0) magheading += 360;
373   return magheading;
374 }
375
376 static long
377 getWarp ()
378 {
379   return globals->get_warp();
380 }
381
382 static void
383 setWarp (long warp)
384 {
385   globals->set_warp(warp);
386 }
387
388 static long
389 getWarpDelta ()
390 {
391   return globals->get_warp_delta();
392 }
393
394 static void
395 setWarpDelta (long delta)
396 {
397   globals->set_warp_delta(delta);
398 }
399
400 static bool
401 getWindingCCW ()
402 {
403   return winding_ccw;
404 }
405
406 static void
407 setWindingCCW (bool state)
408 {
409   winding_ccw = state;
410   if ( winding_ccw )
411     glFrontFace ( GL_CCW );
412   else
413     glFrontFace ( GL_CW );
414 }
415
416 static bool
417 getFullScreen ()
418 {
419 #if defined(FX) && !defined(WIN32)
420   return globals->get_fullscreen();
421 #else
422   return false;
423 #endif
424 }
425
426 static void
427 setFullScreen (bool state)
428 {
429 #if defined(FX) && !defined(WIN32)
430   globals->set_fullscreen(state);
431 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
432   XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
433 #  endif
434 #endif
435 }
436
437 static bool
438 getFDMDataLogging ()
439 {
440   return fdm_data_logging;
441 }
442
443 static void
444 setFDMDataLogging (bool state)
445 {
446                                 // kludge; no getter or setter available
447   if (state != fdm_data_logging) {
448     fgToggleFDMdataLogging();
449     fdm_data_logging = state;
450   }
451 }
452
453 \f
454 ////////////////////////////////////////////////////////////////////////
455 // Tie the properties.
456 ////////////////////////////////////////////////////////////////////////
457
458 FGProperties::FGProperties ()
459 {
460 }
461
462 FGProperties::~FGProperties ()
463 {
464 }
465
466 void
467 FGProperties::init ()
468 {
469 }
470
471 void
472 FGProperties::bind ()
473 {
474                                 // Simulation
475   fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
476   fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
477   fgTie("/sim/freeze/master", getFreeze, setFreeze);
478   fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
479
480   fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
481   fgTie("/sim/time/gmt", getDateString, setDateString);
482   fgSetArchivable("/sim/time/gmt");
483   fgTie("/sim/time/gmt-string", getGMTString);
484
485                                 // Orientation
486   fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
487
488   fgTie("/environment/magnetic-variation-deg", getMagVar);
489   fgTie("/environment/magnetic-dip-deg", getMagDip);
490
491   fgTie("/sim/time/warp", getWarp, setWarp, false);
492   fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
493
494                                 // Misc. Temporary junk.
495   fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
496   fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
497   fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
498 }
499
500 void
501 FGProperties::unbind ()
502 {
503                                 // Simulation
504   fgUntie("/sim/logging/priority");
505   fgUntie("/sim/logging/classes");
506   fgUntie("/sim/freeze/master");
507   fgUntie("/sim/aircraft-dir");
508
509   fgUntie("/sim/time/elapsed-sec");
510   fgUntie("/sim/time/gmt");
511   fgUntie("/sim/time/gmt-string");
512
513                                 // Orientation
514   fgUntie("/orientation/heading-magnetic-deg");
515
516                                 // Environment
517   fgUntie("/environment/magnetic-variation-deg");
518   fgUntie("/environment/magnetic-dip-deg");
519
520   fgUntie("/sim/time/warp");
521   fgUntie("/sim/time/warp-delta");
522
523                                 // Misc. Temporary junk.
524   fgUntie("/sim/temp/winding-ccw");
525   fgUntie("/sim/temp/full-screen");
526   fgUntie("/sim/temp/fdm-data-logging");
527 }
528
529 void
530 FGProperties::update (double dt)
531 {
532                                 // Date and time
533     struct tm * t = globals->get_time_params()->getGmt();
534
535     fgSetInt("/sim/time/utc/year", t->tm_year + 1900);
536     fgSetInt("/sim/time/utc/month", t->tm_mon + 1);
537     fgSetInt("/sim/time/utc/day", t->tm_mday);
538     fgSetInt("/sim/time/utc/hour", t->tm_hour);
539     fgSetInt("/sim/time/utc/minute", t->tm_min);
540     fgSetInt("/sim/time/utc/second", t->tm_sec);
541
542     fgSetDouble("/sim/time/utc/day-seconds",
543                 t->tm_hour * 3600 +
544                 t->tm_min * 60 +
545                 t->tm_sec);
546 }
547
548
549 \f
550 ////////////////////////////////////////////////////////////////////////
551 // Save and restore.
552 ////////////////////////////////////////////////////////////////////////
553
554
555 /**
556  * Save the current state of the simulator to a stream.
557  */
558 bool
559 fgSaveFlight (ostream &output, bool write_all)
560 {
561
562   fgSetBool("/sim/presets/onground", false);
563   fgSetArchivable("/sim/presets/onground");
564   fgSetBool("/sim/presets/trim", false);
565   fgSetArchivable("/sim/presets/trim");
566   fgSetString("/sim/presets/speed-set", "UVW");
567   fgSetArchivable("/sim/presets/speed-set");
568
569   try {
570     writeProperties(output, globals->get_props(), write_all);
571   } catch (const sg_exception &e) {
572     guiErrorMessage("Error saving flight: ", e);
573     return false;
574   }
575   return true;
576 }
577
578
579 /**
580  * Restore the current state of the simulator from a stream.
581  */
582 bool
583 fgLoadFlight (istream &input)
584 {
585   SGPropertyNode props;
586   try {
587     readProperties(input, &props);
588   } catch (const sg_exception &e) {
589     guiErrorMessage("Error reading saved flight: ", e);
590     return false;
591   }
592
593   fgSetBool("/sim/presets/onground", false);
594   fgSetBool("/sim/presets/trim", false);
595   fgSetString("/sim/presets/speed-set", "UVW");
596
597   copyProperties(&props, globals->get_props());
598   // When loading a flight, make it the
599   // new initial state.
600   globals->saveInitialState();
601   return true;
602 }
603
604
605 bool
606 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
607 {
608     string fullpath;
609     if (in_fg_root) {
610         SGPath loadpath(globals->get_fg_root());
611         loadpath.append(path);
612         fullpath = loadpath.str();
613     } else {
614         fullpath = path;
615     }
616
617     try {
618         readProperties(fullpath, props);
619     } catch (const sg_exception &e) {
620         guiErrorMessage("Error reading properties: ", e);
621         return false;
622     }
623     return true;
624 }
625
626
627 \f
628 ////////////////////////////////////////////////////////////////////////
629 // Property convenience functions.
630 ////////////////////////////////////////////////////////////////////////
631
632 SGPropertyNode *
633 fgGetNode (const char * path, bool create)
634 {
635   return globals->get_props()->getNode(path, create);
636 }
637
638 SGPropertyNode * 
639 fgGetNode (const char * path, int index, bool create)
640 {
641   return globals->get_props()->getNode(path, index, create);
642 }
643
644 bool
645 fgHasNode (const char * path)
646 {
647   return (fgGetNode(path, false) != 0);
648 }
649
650 void
651 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
652 {
653   fgGetNode(path, true)->addChangeListener(listener);
654 }
655
656 void
657 fgAddChangeListener (SGPropertyChangeListener * listener,
658                      const char * path, int index)
659 {
660   fgGetNode(path, index, true)->addChangeListener(listener);
661 }
662
663 bool
664 fgGetBool (const char * name, bool defaultValue)
665 {
666   return globals->get_props()->getBoolValue(name, defaultValue);
667 }
668
669 int
670 fgGetInt (const char * name, int defaultValue)
671 {
672   return globals->get_props()->getIntValue(name, defaultValue);
673 }
674
675 int
676 fgGetLong (const char * name, long defaultValue)
677 {
678   return globals->get_props()->getLongValue(name, defaultValue);
679 }
680
681 float
682 fgGetFloat (const char * name, float defaultValue)
683 {
684   return globals->get_props()->getFloatValue(name, defaultValue);
685 }
686
687 double
688 fgGetDouble (const char * name, double defaultValue)
689 {
690   return globals->get_props()->getDoubleValue(name, defaultValue);
691 }
692
693 const char *
694 fgGetString (const char * name, const char * defaultValue)
695 {
696   return globals->get_props()->getStringValue(name, defaultValue);
697 }
698
699 bool
700 fgSetBool (const char * name, bool val)
701 {
702   return globals->get_props()->setBoolValue(name, val);
703 }
704
705 bool
706 fgSetInt (const char * name, int val)
707 {
708   return globals->get_props()->setIntValue(name, val);
709 }
710
711 bool
712 fgSetLong (const char * name, long val)
713 {
714   return globals->get_props()->setLongValue(name, val);
715 }
716
717 bool
718 fgSetFloat (const char * name, float val)
719 {
720   return globals->get_props()->setFloatValue(name, val);
721 }
722
723 bool
724 fgSetDouble (const char * name, double val)
725 {
726   return globals->get_props()->setDoubleValue(name, val);
727 }
728
729 bool
730 fgSetString (const char * name, const char * val)
731 {
732   return globals->get_props()->setStringValue(name, val);
733 }
734
735 void
736 fgSetArchivable (const char * name, bool state)
737 {
738   SGPropertyNode * node = globals->get_props()->getNode(name);
739   if (node == 0)
740     SG_LOG(SG_GENERAL, SG_DEBUG,
741            "Attempt to set archive flag for non-existant property "
742            << name);
743   else
744     node->setAttribute(SGPropertyNode::ARCHIVE, state);
745 }
746
747 void
748 fgSetReadable (const char * name, bool state)
749 {
750   SGPropertyNode * node = globals->get_props()->getNode(name);
751   if (node == 0)
752     SG_LOG(SG_GENERAL, SG_DEBUG,
753            "Attempt to set read flag for non-existant property "
754            << name);
755   else
756     node->setAttribute(SGPropertyNode::READ, state);
757 }
758
759 void
760 fgSetWritable (const char * name, bool state)
761 {
762   SGPropertyNode * node = globals->get_props()->getNode(name);
763   if (node == 0)
764     SG_LOG(SG_GENERAL, SG_DEBUG,
765            "Attempt to set write flag for non-existant property "
766            << name);
767   else
768     node->setAttribute(SGPropertyNode::WRITE, state);
769 }
770
771 void
772 fgUntie (const char * name)
773 {
774   if (!globals->get_props()->untie(name))
775     SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
776 }
777
778
779 // end of fg_props.cxx