]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
new FSF address
[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
36 void
37 fgDefaultWeatherValue (const char * propname, double value)
38 {
39     unsigned int i;
40
41     SGPropertyNode * branch = fgGetNode("/environment/config/boundary", true);
42     vector<SGPropertyNode_ptr> entries = branch->getChildren("entry");
43     for (i = 0; i < entries.size(); i++) {
44         entries[i]->setDoubleValue(propname, value);
45     }
46
47     branch = fgGetNode("/environment/config/aloft", true);
48     entries = branch->getChildren("entry");
49     for (i = 0; i < entries.size(); i++) {
50         entries[i]->setDoubleValue(propname, value);
51     }
52 }
53
54
55 void
56 fgSetupWind (double min_hdg, double max_hdg, double speed, double gust)
57 {
58                                 // Initialize to a reasonable state
59   fgDefaultWeatherValue("wind-from-heading-deg", min_hdg);
60   fgDefaultWeatherValue("wind-speed-kt", speed);
61
62   SG_LOG(SG_GENERAL, SG_INFO, "WIND: " << min_hdg << '@' <<
63          speed << " knots" << endl);
64
65                                 // Now, add some variety to the layers
66   min_hdg += 10;
67   if (min_hdg > 360)
68       min_hdg -= 360;
69   speed *= 1.1;
70   fgSetDouble("/environment/config/boundary/entry[1]/wind-from-heading-deg",
71               min_hdg);
72   fgSetDouble("/environment/config/boundary/entry[1]/wind-speed-kt",
73               speed);
74
75   min_hdg += 20;
76   if (min_hdg > 360)
77       min_hdg -= 360;
78   speed *= 1.1;
79   fgSetDouble("/environment/config/aloft/entry[0]/wind-from-heading-deg",
80               min_hdg);
81   fgSetDouble("/environment/config/aloft/entry[0]/wind-speed-kt",
82               speed);
83
84   min_hdg += 10;
85   if (min_hdg > 360)
86       min_hdg -= 360;
87   speed *= 1.1;
88   fgSetDouble("/environment/config/aloft/entry[1]/wind-from-heading-deg",
89               min_hdg);
90   fgSetDouble("/environment/config/aloft/entry[1]/wind-speed-kt",
91               speed);
92
93   min_hdg += 10;
94   if (min_hdg > 360)
95       min_hdg -= 360;
96   speed *= 1.1;
97   fgSetDouble("/environment/config/aloft/entry[2]/wind-from-heading-deg",
98               min_hdg);
99   fgSetDouble("/environment/config/aloft/entry[2]/wind-speed-kt",
100               speed);
101 }
102
103
104 void
105 fgExit (int status)
106 {
107     SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
108
109     exit(status);
110 }
111
112
113 // Originally written by Alex Perry.
114 double
115 fgGetLowPass (double current, double target, double timeratio)
116 {
117     if ( timeratio < 0.0 ) {
118         if ( timeratio < -1.0 ) {
119                                 // time went backwards; kill the filter
120                 current = target;
121         } else {
122                                 // ignore mildly negative time
123         }
124     } else if ( timeratio < 0.2 ) {
125                                 // Normal mode of operation; fast
126                                 // approximation to exp(-timeratio)
127         current = current * (1.0 - timeratio) + target * timeratio;
128     } else if ( timeratio > 5.0 ) {
129                                 // Huge time step; assume filter has settled
130         current = target;
131     } else {
132                                 // Moderate time step; non linear response
133         double keep = exp(-timeratio);
134         current = current * keep + target * (1.0 - keep);
135     }
136
137     return current;
138 }
139
140 // end of util.cxx
141