1 // FGAIStorm - FGAIBase-derived class creates an AI thunderstorm or cloud
3 // Written by David Culp, started Feb 2004.
5 // Copyright (C) 2004 David P. Culp - davidculp2@comcast.net
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/math/point3d.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28 #include <Scenery/scenery.hxx>
35 #include "AIStorm.hxx"
38 FGAIStorm::FGAIStorm(FGAIManager* mgr) {
40 _type_str = "thunderstorm";
46 flash_node = fgGetNode("/environment/lightning/flash", true);
47 flash_node->setBoolValue(false);
51 subflash_array[0] = 1;
52 subflash_array[1] = 2;
53 subflash_array[2] = 1;
54 subflash_array[3] = 3;
55 subflash_array[4] = 2;
56 subflash_array[5] = 1;
57 subflash_array[6] = 1;
58 subflash_array[7] = 2;
60 turb_mag_node = fgGetNode("/environment/turbulence/magnitude-norm", true);
61 turb_rate_node = fgGetNode("/environment/turbulence/rate-hz", true);
65 FGAIStorm::~FGAIStorm() {
69 bool FGAIStorm::init() {
70 return FGAIBase::init();
73 void FGAIStorm::bind() {
77 void FGAIStorm::unbind() {
82 void FGAIStorm::update(double dt) {
89 void FGAIStorm::Run(double dt) {
93 double speed_north_deg_sec;
94 double speed_east_deg_sec;
96 // convert speed to degrees per second
97 speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
98 * speed * 1.686 / ft_per_deg_lat;
99 speed_east_deg_sec = sin( hdg / SG_RADIANS_TO_DEGREES )
100 * speed * 1.686 / ft_per_deg_lon;
103 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
104 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
106 // do calculations for weather radar display
107 UpdateRadar(manager);
109 // **************************************************
111 // **************************************************
113 if (timer > random_delay) {
114 srand((unsigned)time(0));
115 random_delay = delay + (rand()%3) - 1.0;
116 //cout << "random_delay = " << random_delay << endl;
120 if (subflash_index == 8) subflash_index = 0;
121 subflashes = subflash_array[subflash_index];
125 if (flashed < subflashes) {
128 flash_node->setBoolValue(true);
130 flash_node->setBoolValue(false);
146 // ***************************************************
148 // ***************************************************
150 // copy user's position from the AIManager
151 double user_latitude = manager->get_user_latitude();
152 double user_longitude = manager->get_user_longitude();
153 double user_altitude = manager->get_user_altitude();
155 // calculate range to target in feet and nautical miles
156 double lat_range = fabs(pos.lat() - user_latitude) * ft_per_deg_lat;
157 double lon_range = fabs(pos.lon() - user_longitude) * ft_per_deg_lon;
158 double range_ft = sqrt(lat_range*lat_range + lon_range*lon_range);
159 range = range_ft / 6076.11549;
161 if (range < (diameter * 0.5) &&
162 user_altitude > (altitude - 1000.0) &&
163 user_altitude < height) {
164 turb_mag_node->setDoubleValue(strength_norm);
165 turb_rate_node->setDoubleValue(0.5);