]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
3daeb991b712c4696cb481b59bc14dc958a73339
[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     // remove subsystems first, which need access to other subsystems in their
108     // destructors (e.g. "nasal")
109     SGSubsystem *sub = globals->get_subsystem("ai_model");
110     globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
111     delete sub;
112
113     SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
114     exit(status);
115 }
116
117
118 // Originally written by Alex Perry.
119 double
120 fgGetLowPass (double current, double target, double timeratio)
121 {
122     if ( timeratio < 0.0 ) {
123         if ( timeratio < -1.0 ) {
124                                 // time went backwards; kill the filter
125                 current = target;
126         } else {
127                                 // ignore mildly negative time
128         }
129     } else if ( timeratio < 0.2 ) {
130                                 // Normal mode of operation; fast
131                                 // approximation to exp(-timeratio)
132         current = current * (1.0 - timeratio) + target * timeratio;
133     } else if ( timeratio > 5.0 ) {
134                                 // Huge time step; assume filter has settled
135         current = target;
136     } else {
137                                 // Moderate time step; non linear response
138         double keep = exp(-timeratio);
139         current = current * keep + target * (1.0 - keep);
140     }
141
142     return current;
143 }
144
145 // end of util.cxx
146