]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
Merge commit 'refs/merge-requests/1552' of git@gitorious.org:fg/flightgear into next
[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 <cstdlib>
26
27 #include <vector>
28 using std::vector;
29
30 #include <simgear/debug/logstream.hxx>
31
32 #include "fg_io.hxx"
33 #include "fg_props.hxx"
34 #include "globals.hxx"
35 #include "util.hxx"
36
37 #ifdef OSG_LIBRARY_STATIC
38 #include "osgDB/Registry"
39 #endif
40
41 void
42 fgDefaultWeatherValue (const char * propname, double value)
43 {
44     unsigned int i;
45
46     SGPropertyNode * branch = fgGetNode("/environment/config/boundary", true);
47     vector<SGPropertyNode_ptr> entries = branch->getChildren("entry");
48     for (i = 0; i < entries.size(); i++) {
49         entries[i]->setDoubleValue(propname, value);
50     }
51
52     branch = fgGetNode("/environment/config/aloft", true);
53     entries = branch->getChildren("entry");
54     for (i = 0; i < entries.size(); i++) {
55         entries[i]->setDoubleValue(propname, value);
56     }
57 }
58
59
60 void
61 fgSetupWind (double min_hdg, double max_hdg, double speed, double gust)
62 {
63                                 // Initialize to a reasonable state
64   fgDefaultWeatherValue("wind-from-heading-deg", min_hdg);
65   fgDefaultWeatherValue("wind-speed-kt", speed);
66
67   SG_LOG(SG_GENERAL, SG_INFO, "WIND: " << min_hdg << '@' <<
68          speed << " knots" << endl);
69
70                                 // Now, add some variety to the layers
71   min_hdg += 10;
72   if (min_hdg > 360)
73       min_hdg -= 360;
74   speed *= 1.1;
75   fgSetDouble("/environment/config/boundary/entry[1]/wind-from-heading-deg",
76               min_hdg);
77   fgSetDouble("/environment/config/boundary/entry[1]/wind-speed-kt",
78               speed);
79
80   min_hdg += 20;
81   if (min_hdg > 360)
82       min_hdg -= 360;
83   speed *= 1.1;
84   fgSetDouble("/environment/config/aloft/entry[0]/wind-from-heading-deg",
85               min_hdg);
86   fgSetDouble("/environment/config/aloft/entry[0]/wind-speed-kt",
87               speed);
88
89   min_hdg += 10;
90   if (min_hdg > 360)
91       min_hdg -= 360;
92   speed *= 1.1;
93   fgSetDouble("/environment/config/aloft/entry[1]/wind-from-heading-deg",
94               min_hdg);
95   fgSetDouble("/environment/config/aloft/entry[1]/wind-speed-kt",
96               speed);
97
98   min_hdg += 10;
99   if (min_hdg > 360)
100       min_hdg -= 360;
101   speed *= 1.1;
102   fgSetDouble("/environment/config/aloft/entry[2]/wind-from-heading-deg",
103               min_hdg);
104   fgSetDouble("/environment/config/aloft/entry[2]/wind-speed-kt",
105               speed);
106 }
107
108
109 void
110 fgExit (int status)
111 {
112 #ifdef OSG_LIBRARY_STATIC
113     osgDB::Registry::instance( true);
114 #endif
115
116     SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
117     std::exit(status);
118 }
119
120
121 // Originally written by Alex Perry.
122 double
123 fgGetLowPass (double current, double target, double timeratio)
124 {
125     if ( timeratio < 0.0 ) {
126         if ( timeratio < -1.0 ) {
127                                 // time went backwards; kill the filter
128                 current = target;
129         } else {
130                                 // ignore mildly negative time
131         }
132     } else if ( timeratio < 0.2 ) {
133                                 // Normal mode of operation; fast
134                                 // approximation to exp(-timeratio)
135         current = current * (1.0 - timeratio) + target * timeratio;
136     } else if ( timeratio > 5.0 ) {
137                                 // Huge time step; assume filter has settled
138         current = target;
139     } else {
140                                 // Moderate time step; non linear response
141         double keep = exp(-timeratio);
142         current = current * keep + target * (1.0 - keep);
143     }
144
145     return current;
146 }
147
148
149 string
150 fgUnescape (const char *s)
151 {
152     string r;
153     while (*s) {
154         if (*s != '\\') {
155             r += *s++;
156             continue;
157         }
158         if (!*++s)
159             break;
160         if (*s == '\\') {
161             r += '\\';
162         } else if (*s == 'n') {
163             r += '\n';
164         } else if (*s == 'r') {
165             r += '\r';
166         } else if (*s == 't') {
167             r += '\t';
168         } else if (*s == 'v') {
169             r += '\v';
170         } else if (*s == 'f') {
171             r += '\f';
172         } else if (*s == 'a') {
173             r += '\a';
174         } else if (*s == 'b') {
175             r += '\b';
176         } else if (*s == 'x') {
177             if (!*++s)
178                 break;
179             int v = 0;
180             for (int i = 0; i < 2 && isxdigit(*s); i++, s++)
181                 v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a');
182             r += v;
183             continue;
184
185         } else if (*s >= '0' && *s <= '7') {
186             int v = *s++ - '0';
187             for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++)
188                 v = v * 8 + *s - '0';
189             r += v;
190             continue;
191
192         } else {
193             r += *s;
194         }
195         s++;
196     }
197     return r;
198 }
199
200
201 // Write out path to validation node and read it back in. A Nasal
202 // listener is supposed to replace the path with a validated version
203 // or an empty string otherwise.
204 const char *fgValidatePath (const char *str, bool write)
205 {
206     static SGPropertyNode_ptr r, w;
207     if (!r) {
208         r = fgGetNode("/sim/paths/validate/read", true);
209         r->setAttribute(SGPropertyNode::READ, true);
210         r->setAttribute(SGPropertyNode::WRITE, true);
211
212         w = fgGetNode("/sim/paths/validate/write", true);
213         w->setAttribute(SGPropertyNode::READ, true);
214         w->setAttribute(SGPropertyNode::WRITE, true);
215     }
216     SGPropertyNode *prop = write ? w : r;
217     prop->setStringValue(str);
218     const char *result = prop->getStringValue();
219     return result[0] ? result : 0;
220 }
221
222 // end of util.cxx
223