]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIThermal.cxx
Improove FGTileMgr::scenery_available for small ranges.
[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 <Main/fg_props.hxx>
26 #include <Main/globals.hxx>
27 #include <Scenery/scenery.hxx>
28 #include <string>
29 #include <math.h>
30
31 using std::string;
32
33 #include "AIThermal.hxx"
34
35
36 FGAIThermal::FGAIThermal() : FGAIBase(otThermal) {
37    max_strength = 6.0;
38    diameter = 0.5;
39    strength = factor = 0.0;
40 }
41
42 FGAIThermal::~FGAIThermal() {
43 }
44
45 void FGAIThermal::readFromScenario(SGPropertyNode* scFileNode) {
46   if (!scFileNode)
47     return;
48
49   FGAIBase::readFromScenario(scFileNode);
50
51   setMaxStrength(scFileNode->getDoubleValue("strength-fps", 8.0)); 
52   setDiameter(scFileNode->getDoubleValue("diameter-ft", 0.0)/6076.11549);
53   setHeight(scFileNode->getDoubleValue("height-msl", 5000.0));
54 }
55
56 bool FGAIThermal::init(bool search_in_AI_path) {
57    factor = 8.0 * max_strength / (diameter * diameter * diameter);
58    setAltitude( height );
59    return FGAIBase::init(search_in_AI_path);
60 }
61
62 void FGAIThermal::bind() {
63     FGAIBase::bind();
64 }
65
66 void FGAIThermal::unbind() {
67     FGAIBase::unbind();
68 }
69
70
71 void FGAIThermal::update(double dt) {
72    FGAIBase::update(dt);
73    Run(dt);
74    Transform();
75 }
76
77
78 void FGAIThermal::Run(double dt) {
79
80    //###########################//
81    // do calculations for range //
82    //###########################//
83
84    // copy values from the AIManager
85    double user_latitude  = manager->get_user_latitude();
86    double user_longitude = manager->get_user_longitude();
87    double user_altitude  = manager->get_user_altitude();
88
89    // calculate range to target in feet and nautical miles
90    double lat_range = fabs(pos.getLatitudeDeg() - user_latitude) * ft_per_deg_lat;
91    double lon_range = fabs(pos.getLongitudeDeg() - user_longitude) * ft_per_deg_lon;
92    double range_ft = sqrt(lat_range*lat_range + lon_range*lon_range);
93    range = range_ft / 6076.11549;
94
95    // Calculate speed of rising air if within range. 
96    // Air vertical speed is maximum at center of thermal,
97    // and decreases to zero at the edge (as distance cubed).
98    if (range < (diameter * 0.5)) {
99      strength = max_strength - ( range * range * range * factor );
100    } else {
101      strength = 0.0;
102    }
103
104    // Stop lift at the top of the thermal (smoothly)
105    if (user_altitude > (height + 100.0)) {
106      strength = 0.0;
107    }
108    else if (user_altitude < height) {
109      // do nothing
110    }
111    else {
112      strength -= (strength * (user_altitude - height) * 0.01);
113    }
114 }
115