]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
set /sim/fg-current to current working directory; getcwd() is defined in
[flightgear.git] / src / Main / util.cxx
1 // util.cxx - general-purpose utility functions.
2 // Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //
18 // $Id$
19
20
21 #include <simgear/compiler.h>
22
23 #include <math.h>
24
25 #include <vector>
26 SG_USING_STD(vector);
27
28 #include <simgear/debug/logstream.hxx>
29
30 #include "fg_io.hxx"
31 #include "fg_props.hxx"
32 #include "globals.hxx"
33 #include "util.hxx"
34
35 #ifdef OSG_LIBRARY_STATIC
36 #include "osgDB/Registry"
37 #endif
38
39 void
40 fgDefaultWeatherValue (const char * propname, double value)
41 {
42     unsigned int i;
43
44     SGPropertyNode * branch = fgGetNode("/environment/config/boundary", true);
45     vector<SGPropertyNode_ptr> entries = branch->getChildren("entry");
46     for (i = 0; i < entries.size(); i++) {
47         entries[i]->setDoubleValue(propname, value);
48     }
49
50     branch = fgGetNode("/environment/config/aloft", true);
51     entries = branch->getChildren("entry");
52     for (i = 0; i < entries.size(); i++) {
53         entries[i]->setDoubleValue(propname, value);
54     }
55 }
56
57
58 void
59 fgSetupWind (double min_hdg, double max_hdg, double speed, double gust)
60 {
61                                 // Initialize to a reasonable state
62   fgDefaultWeatherValue("wind-from-heading-deg", min_hdg);
63   fgDefaultWeatherValue("wind-speed-kt", speed);
64
65   SG_LOG(SG_GENERAL, SG_INFO, "WIND: " << min_hdg << '@' <<
66          speed << " knots" << endl);
67
68                                 // Now, add some variety to the layers
69   min_hdg += 10;
70   if (min_hdg > 360)
71       min_hdg -= 360;
72   speed *= 1.1;
73   fgSetDouble("/environment/config/boundary/entry[1]/wind-from-heading-deg",
74               min_hdg);
75   fgSetDouble("/environment/config/boundary/entry[1]/wind-speed-kt",
76               speed);
77
78   min_hdg += 20;
79   if (min_hdg > 360)
80       min_hdg -= 360;
81   speed *= 1.1;
82   fgSetDouble("/environment/config/aloft/entry[0]/wind-from-heading-deg",
83               min_hdg);
84   fgSetDouble("/environment/config/aloft/entry[0]/wind-speed-kt",
85               speed);
86
87   min_hdg += 10;
88   if (min_hdg > 360)
89       min_hdg -= 360;
90   speed *= 1.1;
91   fgSetDouble("/environment/config/aloft/entry[1]/wind-from-heading-deg",
92               min_hdg);
93   fgSetDouble("/environment/config/aloft/entry[1]/wind-speed-kt",
94               speed);
95
96   min_hdg += 10;
97   if (min_hdg > 360)
98       min_hdg -= 360;
99   speed *= 1.1;
100   fgSetDouble("/environment/config/aloft/entry[2]/wind-from-heading-deg",
101               min_hdg);
102   fgSetDouble("/environment/config/aloft/entry[2]/wind-speed-kt",
103               speed);
104 }
105
106
107 void
108 fgExit (int status)
109 {
110 #ifdef OSG_LIBRARY_STATIC
111     osgDB::Registry::instance( true);
112 #endif
113
114     SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
115     exit(status);
116 }
117
118
119 // Originally written by Alex Perry.
120 double
121 fgGetLowPass (double current, double target, double timeratio)
122 {
123     if ( timeratio < 0.0 ) {
124         if ( timeratio < -1.0 ) {
125                                 // time went backwards; kill the filter
126                 current = target;
127         } else {
128                                 // ignore mildly negative time
129         }
130     } else if ( timeratio < 0.2 ) {
131                                 // Normal mode of operation; fast
132                                 // approximation to exp(-timeratio)
133         current = current * (1.0 - timeratio) + target * timeratio;
134     } else if ( timeratio > 5.0 ) {
135                                 // Huge time step; assume filter has settled
136         current = target;
137     } else {
138                                 // Moderate time step; non linear response
139         double keep = exp(-timeratio);
140         current = current * keep + target * (1.0 - keep);
141     }
142
143     return current;
144 }
145
146 // end of util.cxx
147