]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIThermal.cxx
warnings--
[flightgear.git] / src / AIModel / AIThermal.cxx
1 // FGAIThermal - FGAIBase-derived class creates an AI thermal
2 //
3 // Written by David Culp, started Feb 2004.
4 //
5 // Copyright (C) 2004  David P. Culp - davidculp2@comcast.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/math/point3d.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28 #include <Scenery/scenery.hxx>
29 #include <string>
30 #include <math.h>
31
32 SG_USING_STD(string);
33
34 #include "AIThermal.hxx"
35
36
37 FGAIThermal::FGAIThermal() : FGAIBase(otThermal) {
38    max_strength = 6.0;
39    diameter = 0.5;
40    strength = factor = 0.0;
41 }
42
43 FGAIThermal::~FGAIThermal() {
44 }
45
46 void FGAIThermal::readFromScenario(SGPropertyNode* scFileNode) {
47   if (!scFileNode)
48     return;
49
50   FGAIBase::readFromScenario(scFileNode);
51
52   setMaxStrength(scFileNode->getDoubleValue("strength-fps", 8.0)); 
53   setDiameter(scFileNode->getDoubleValue("diameter-ft", 0.0)/6076.11549);
54   setHeight(scFileNode->getDoubleValue("height-msl", 5000.0));
55 }
56
57 bool FGAIThermal::init() {
58    factor = 8.0 * max_strength / (diameter * diameter * diameter);
59    setAltitude( height );
60    return FGAIBase::init();
61 }
62
63 void FGAIThermal::bind() {
64     FGAIBase::bind();
65 }
66
67 void FGAIThermal::unbind() {
68     FGAIBase::unbind();
69 }
70
71
72 void FGAIThermal::update(double dt) {
73    FGAIBase::update(dt);
74    Run(dt);
75    Transform();
76 }
77
78
79 void FGAIThermal::Run(double dt) {
80
81    //###########################//
82    // do calculations for range //
83    //###########################//
84
85    // copy values from the AIManager
86    double user_latitude  = manager->get_user_latitude();
87    double user_longitude = manager->get_user_longitude();
88    double user_altitude  = manager->get_user_altitude();
89
90    // calculate range to target in feet and nautical miles
91    double lat_range = fabs(pos.getLatitudeDeg() - user_latitude) * ft_per_deg_lat;
92    double lon_range = fabs(pos.getLongitudeDeg() - user_longitude) * ft_per_deg_lon;
93    double range_ft = sqrt(lat_range*lat_range + lon_range*lon_range);
94    range = range_ft / 6076.11549;
95
96    // Calculate speed of rising air if within range. 
97    // Air vertical speed is maximum at center of thermal,
98    // and decreases to zero at the edge (as distance cubed).
99    if (range < (diameter * 0.5)) {
100      strength = max_strength - ( range * range * range * factor );
101    } else {
102      strength = 0.0;
103    }
104
105    // Stop lift at the top of the thermal (smoothly)
106    if (user_altitude > (height + 100.0)) {
107      strength = 0.0;
108    }
109    else if (user_altitude < height) {
110      // do nothing
111    }
112    else {
113      strength -= (strength * (user_altitude - height) * 0.01);
114    }
115 }
116