]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
since the submodel_mgr subsystem does now also hold references to AI models,
[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 #ifdef OSG_LIBRARY_STATIC
36 #include "osgDB/Registry"
37 #endif
38
39 void
40 fgDefaultWeatherValue (const char * propname, double value)
41 {
42     unsigned int i;
43
44     SGPropertyNode * branch = fgGetNode("/environment/config/boundary", true);
45     vector<SGPropertyNode_ptr> entries = branch->getChildren("entry");
46     for (i = 0; i < entries.size(); i++) {
47         entries[i]->setDoubleValue(propname, value);
48     }
49
50     branch = fgGetNode("/environment/config/aloft", true);
51     entries = branch->getChildren("entry");
52     for (i = 0; i < entries.size(); i++) {
53         entries[i]->setDoubleValue(propname, value);
54     }
55 }
56
57
58 void
59 fgSetupWind (double min_hdg, double max_hdg, double speed, double gust)
60 {
61                                 // Initialize to a reasonable state
62   fgDefaultWeatherValue("wind-from-heading-deg", min_hdg);
63   fgDefaultWeatherValue("wind-speed-kt", speed);
64
65   SG_LOG(SG_GENERAL, SG_INFO, "WIND: " << min_hdg << '@' <<
66          speed << " knots" << endl);
67
68                                 // Now, add some variety to the layers
69   min_hdg += 10;
70   if (min_hdg > 360)
71       min_hdg -= 360;
72   speed *= 1.1;
73   fgSetDouble("/environment/config/boundary/entry[1]/wind-from-heading-deg",
74               min_hdg);
75   fgSetDouble("/environment/config/boundary/entry[1]/wind-speed-kt",
76               speed);
77
78   min_hdg += 20;
79   if (min_hdg > 360)
80       min_hdg -= 360;
81   speed *= 1.1;
82   fgSetDouble("/environment/config/aloft/entry[0]/wind-from-heading-deg",
83               min_hdg);
84   fgSetDouble("/environment/config/aloft/entry[0]/wind-speed-kt",
85               speed);
86
87   min_hdg += 10;
88   if (min_hdg > 360)
89       min_hdg -= 360;
90   speed *= 1.1;
91   fgSetDouble("/environment/config/aloft/entry[1]/wind-from-heading-deg",
92               min_hdg);
93   fgSetDouble("/environment/config/aloft/entry[1]/wind-speed-kt",
94               speed);
95
96   min_hdg += 10;
97   if (min_hdg > 360)
98       min_hdg -= 360;
99   speed *= 1.1;
100   fgSetDouble("/environment/config/aloft/entry[2]/wind-from-heading-deg",
101               min_hdg);
102   fgSetDouble("/environment/config/aloft/entry[2]/wind-speed-kt",
103               speed);
104 }
105
106
107 void
108 fgExit (int status)
109 {
110     // remove subsystems first, which need access to other subsystems in their
111     // destructors (e.g. "nasal")
112     SGSubsystem *sub;
113
114     sub = globals->get_subsystem("ai_model");
115     globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
116     delete sub;
117
118     sub = globals->get_subsystem("submodel_mgr");
119     globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("submodel_mgr");
120     delete sub;
121
122 #ifdef OSG_LIBRARY_STATIC
123     osgDB::Registry::instance( true);
124 #endif
125
126     SG_LOG(SG_GENERAL, SG_INFO, "Exiting FlightGear with status " << status);
127     exit(status);
128 }
129
130
131 // Originally written by Alex Perry.
132 double
133 fgGetLowPass (double current, double target, double timeratio)
134 {
135     if ( timeratio < 0.0 ) {
136         if ( timeratio < -1.0 ) {
137                                 // time went backwards; kill the filter
138                 current = target;
139         } else {
140                                 // ignore mildly negative time
141         }
142     } else if ( timeratio < 0.2 ) {
143                                 // Normal mode of operation; fast
144                                 // approximation to exp(-timeratio)
145         current = current * (1.0 - timeratio) + target * timeratio;
146     } else if ( timeratio > 5.0 ) {
147                                 // Huge time step; assume filter has settled
148         current = target;
149     } else {
150                                 // Moderate time step; non linear response
151         double keep = exp(-timeratio);
152         current = current * keep + target * (1.0 - keep);
153     }
154
155     return current;
156 }
157
158 // end of util.cxx
159