]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIStorm.cxx
372e69e705c128970e89aa2bed7bc25ece5d6029
[flightgear.git] / src / AIModel / AIStorm.cxx
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., 675 Mass Ave, Cambridge, MA 02139, 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 #include <cstdlib>
32
33 SG_USING_STD(string);
34
35 #include "AIStorm.hxx"
36
37
38 FGAIStorm::FGAIStorm(FGAIManager* mgr) {
39    manager = mgr;   
40    _type_str = "thunderstorm";
41    _otype = otStorm;
42    delay = 3.6;
43    subflashes = 1;
44    timer = 0.0;
45    random_delay = 3.6;
46    flash_node = fgGetNode("/environment/lightning/flash", true);
47    flash_node->setBoolValue(false);
48    flashed = 0; 
49    flashing = false;
50    subflash_index = -1;
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;
59 }
60
61
62 FGAIStorm::~FGAIStorm() {
63 }
64
65
66 bool FGAIStorm::init() {
67    return FGAIBase::init();
68 }
69
70 void FGAIStorm::bind() {
71     FGAIBase::bind();
72 }
73
74 void FGAIStorm::unbind() {
75     FGAIBase::unbind();
76 }
77
78
79 void FGAIStorm::update(double dt) {
80    FGAIBase::update(dt);
81    Run(dt);
82    Transform();
83 }
84
85
86 void FGAIStorm::Run(double dt) {
87
88    FGAIStorm::dt = dt;
89         
90    double speed_north_deg_sec;
91    double speed_east_deg_sec;
92
93    // convert speed to degrees per second
94    speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
95                           * speed * 1.686 / ft_per_deg_lat;
96    speed_east_deg_sec  = sin( hdg / SG_RADIANS_TO_DEGREES )
97                           * speed * 1.686 / ft_per_deg_lon;
98
99    // set new position
100    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
101    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
102
103    // do calculations for radar //
104    UpdateRadar(manager);
105
106    // do lightning
107    if (timer > random_delay) {
108      srand((unsigned)time(0));
109      random_delay = delay + (rand()%3) - 1.0;
110      //cout << "random_delay = " << random_delay << endl;
111      timer = 0.0;
112      flashing = true;
113      subflash_index++;
114      if (subflash_index == 8) subflash_index = 0; 
115      subflashes = subflash_array[subflash_index]; 
116    }
117
118    if (flashing) {
119      if (flashed < subflashes) {
120          timer += dt;
121          if (timer < 0.2) { 
122          flash_node->setBoolValue(true);
123          } else {
124             flash_node->setBoolValue(false);
125             if (timer > 0.4) {
126               timer = 0.0;
127               flashed++;
128             }
129          }
130      } else {
131        flashing = false;
132        timer = 0.0;
133        flashed = 0;
134      }
135    }
136    else {
137      timer += dt;
138    }  
139      
140 }
141