]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
Merge branch 'next' of D:\Git_New\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 // Originally written by Alex Perry.
109 double
110 fgGetLowPass (double current, double target, double timeratio)
111 {
112     if ( timeratio < 0.0 ) {
113         if ( timeratio < -1.0 ) {
114                                 // time went backwards; kill the filter
115                 current = target;
116         } else {
117                                 // ignore mildly negative time
118         }
119     } else if ( timeratio < 0.2 ) {
120                                 // Normal mode of operation; fast
121                                 // approximation to exp(-timeratio)
122         current = current * (1.0 - timeratio) + target * timeratio;
123     } else if ( timeratio > 5.0 ) {
124                                 // Huge time step; assume filter has settled
125         current = target;
126     } else {
127                                 // Moderate time step; non linear response
128         double keep = exp(-timeratio);
129         current = current * keep + target * (1.0 - keep);
130     }
131
132     return current;
133 }
134
135
136 string
137 fgUnescape (const char *s)
138 {
139     string r;
140     while (*s) {
141         if (*s != '\\') {
142             r += *s++;
143             continue;
144         }
145         if (!*++s)
146             break;
147         if (*s == '\\') {
148             r += '\\';
149         } else if (*s == 'n') {
150             r += '\n';
151         } else if (*s == 'r') {
152             r += '\r';
153         } else if (*s == 't') {
154             r += '\t';
155         } else if (*s == 'v') {
156             r += '\v';
157         } else if (*s == 'f') {
158             r += '\f';
159         } else if (*s == 'a') {
160             r += '\a';
161         } else if (*s == 'b') {
162             r += '\b';
163         } else if (*s == 'x') {
164             if (!*++s)
165                 break;
166             int v = 0;
167             for (int i = 0; i < 2 && isxdigit(*s); i++, s++)
168                 v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a');
169             r += v;
170             continue;
171
172         } else if (*s >= '0' && *s <= '7') {
173             int v = *s++ - '0';
174             for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++)
175                 v = v * 8 + *s - '0';
176             r += v;
177             continue;
178
179         } else {
180             r += *s;
181         }
182         s++;
183     }
184     return r;
185 }
186
187
188 // Write out path to validation node and read it back in. A Nasal
189 // listener is supposed to replace the path with a validated version
190 // or an empty string otherwise.
191 const char *fgValidatePath (const char *str, bool write)
192 {
193     static SGPropertyNode_ptr r, w;
194     if (!r) {
195         r = fgGetNode("/sim/paths/validate/read", true);
196         r->setAttribute(SGPropertyNode::READ, true);
197         r->setAttribute(SGPropertyNode::WRITE, true);
198
199         w = fgGetNode("/sim/paths/validate/write", true);
200         w->setAttribute(SGPropertyNode::READ, true);
201         w->setAttribute(SGPropertyNode::WRITE, true);
202     }
203     SGPropertyNode *prop = write ? w : r;
204     prop->setStringValue(str);
205     const char *result = prop->getStringValue();
206     return result[0] ? result : 0;
207 }
208
209 // end of util.cxx
210