]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
Move fgSetupWind() from options.cxx to util.cxx
[flightgear.git] / src / Main / util.cxx
1 // util.cxx - general-purpose utility functions.
2 // Copyright (C) 2002  Curtis L. Olson  - curt@me.umn.edu
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., 675 Mass Ave, Cambridge, MA 02139, 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 #ifdef FG_WEATHERCM
103   // convert to fps
104   speed *= SG_NM_TO_METER * SG_METER_TO_FEET * (1.0/3600);
105   while (min_hdg > 360)
106     min_hdg -= 360;
107   while (min_hdg <= 0)
108     min_hdg += 360;
109   min_hdg *= SGD_DEGREES_TO_RADIANS;
110   fgSetDouble("/environment/wind-from-north-fps", speed * cos(dir));
111   fgSetDouble("/environment/wind-from-east-fps", speed * sin(dir));
112 #endif // FG_WEATHERCM
113 }
114
115
116 void
117 fgExit (int status)
118 {
119     SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
120
121     globals->get_io()->shutdown_all();
122     exit(status);
123 }
124
125
126 // Originally written by Alex Perry.
127 double
128 fgGetLowPass (double current, double target, double timeratio)
129 {
130     if ( timeratio < 0.0 ) {
131         if ( timeratio < -1.0 ) {
132                                 // time went backwards; kill the filter
133                 current = target;
134         } else {
135                                 // ignore mildly negative time
136         }
137     } else if ( timeratio < 0.2 ) {
138                                 // Normal mode of operation; fast
139                                 // approximation to exp(-timeratio)
140         current = current * (1.0 - timeratio) + target * timeratio;
141     } else if ( timeratio > 5.0 ) {
142                                 // Huge time step; assume filter has settled
143         current = target;
144     } else {
145                                 // Moderate time step; non linear response
146         double keep = exp(-timeratio);
147         current = current * keep + target * (1.0 - keep);
148     }
149
150     return current;
151 }
152
153 // end of util.cxx
154