FlightGear next
AIStorm.cxx
Go to the documentation of this file.
1// FGAIStorm - FGAIBase-derived class creates an AI thunderstorm or cloud
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#include <cmath>
22#include <cstdlib>
23#include <string>
24#include <time.h>
25
26#include <Main/fg_props.hxx>
27#include <Main/globals.hxx>
28#include <Scenery/scenery.hxx>
29
30#include "AIStorm.hxx"
31
32
34{
35 delay = 3.6;
36 subflashes = 1;
37 timer = 0.0;
38 random_delay = 3.6;
39 flash_node = fgGetNode("/environment/lightning/flash", true);
40 flash_node->setBoolValue(false);
41 flashed = 0;
42 flashing = false;
43 subflash_index = -1;
44 subflash_array[0] = 1;
45 subflash_array[1] = 2;
46 subflash_array[2] = 1;
47 subflash_array[3] = 3;
48 subflash_array[4] = 2;
49 subflash_array[5] = 1;
50 subflash_array[6] = 1;
51 subflash_array[7] = 2;
52
53 turb_mag_node = fgGetNode("/environment/turbulence/magnitude-norm", true);
54 turb_rate_node = fgGetNode("/environment/turbulence/rate-hz", true);
55}
56
57
58void FGAIStorm::readFromScenario(SGPropertyNode* scFileNode) {
59 if (!scFileNode)
60 return;
61
63
64 setDiameter(scFileNode->getDoubleValue("diameter-ft", 0.0)/6076.11549);
65 setHeight(scFileNode->getDoubleValue("height-msl", 5000.0));
66 setStrengthNorm(scFileNode->getDoubleValue("strength-norm", 1.0));
67}
68
69void FGAIStorm::update(double dt) {
71 Run(dt);
72 Transform();
73}
74
75
76void FGAIStorm::Run(double dt) {
78 double speed_east_deg_sec;
79
80 // convert speed to degrees per second
81 speed_north_deg_sec = cos(hdg / SG_RADIANS_TO_DEGREES) * speed * 1.686 / ft_per_deg_lat;
82 speed_east_deg_sec = sin(hdg / SG_RADIANS_TO_DEGREES) * speed * 1.686 / ft_per_deg_lon;
83
84 // set new position
85 pos.setLatitudeDeg( pos.getLatitudeDeg() + speed_north_deg_sec * dt);
86 pos.setLongitudeDeg( pos.getLongitudeDeg() + speed_east_deg_sec * dt);
87
88 // do calculations for weather radar display
90
91 // **************************************************
92 // * do lightning *
93 // **************************************************
94
95 if (timer > random_delay) {
96 srand((unsigned)time(0));
97 random_delay = delay + (rand()%3) - 1.0;
98 //cout << "random_delay = " << random_delay << endl;
99 timer = 0.0;
100 flashing = true;
101 subflash_index++;
102 if (subflash_index == 8) subflash_index = 0;
103 subflashes = subflash_array[subflash_index];
104 }
105
106 if (flashing) {
107 if (flashed < subflashes) {
108 timer += dt;
109 if (timer < 0.1) {
110 flash_node->setBoolValue(true);
111 } else {
112 flash_node->setBoolValue(false);
113 if (timer > 0.2) {
114 timer = 0.0;
115 flashed++;
116 }
117 }
118 } else {
119 flashing = false;
120 timer = 0.0;
121 flashed = 0;
122 }
123 } else {
124 timer += dt;
125 }
126
127 // ***************************************************
128 // * do turbulence *
129 // ***************************************************
130
131 // copy user's position from the AIManager
132 double d = dist(SGVec3d::fromGeod(pos), globals->get_aircraft_position_cart());
133 double rangeNm = d * SG_METER_TO_NM;
134 double user_altitude = globals->get_aircraft_position().getElevationFt();
135
136 if (rangeNm < (diameter * 0.5) &&
137 user_altitude > (altitude_ft - 1000.0) &&
138 user_altitude < height) {
139 turb_mag_node->setDoubleValue(strength_norm);
140 turb_rate_node->setDoubleValue(0.5);
141 }
142}
double altitude_ft
Definition AIBase.hxx:218
SGGeod pos
Definition AIBase.hxx:212
virtual void readFromScenario(SGPropertyNode *scFileNode)
Definition AIBase.cxx:249
FGAIBase(object_type ot, bool enableHot)
Definition AIBase.cxx:146
virtual void update(double dt)
Definition AIBase.cxx:294
double speed
Definition AIBase.hxx:216
double UpdateRadar(FGAIManager *manager)
Definition AIBase.cxx:819
double hdg
Definition AIBase.hxx:213
void Transform()
Definition AIBase.cxx:518
double ft_per_deg_lon
Definition AIBase.hxx:225
double speed_east_deg_sec
Definition AIBase.hxx:221
double ft_per_deg_lat
Definition AIBase.hxx:226
double speed_north_deg_sec
Definition AIBase.hxx:220
FGAIManager * manager
Definition AIBase.hxx:209
void setStrengthNorm(double s)
Definition AIStorm.hxx:27
void update(double dt) override
Definition AIStorm.cxx:69
void setHeight(double h)
Definition AIStorm.hxx:29
void readFromScenario(SGPropertyNode *scFileNode) override
Definition AIStorm.cxx:58
void setDiameter(double d)
Definition AIStorm.hxx:28
SGGeod get_aircraft_position() const
Definition globals.cxx:611
SGVec3d get_aircraft_position_cart() const
Definition globals.cxx:619
FGGlobals * globals
Definition globals.cxx:142
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27