Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Weather.cpp
Go to the documentation of this file.
1 /* Project Starshatter 5.0
2  Destroyer Studios LLC
3  Copyright © 1997-2007. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: Weather.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Manages local weather conditions according to the system stardate
13 */
14 
15 #include "MemDebug.h"
16 #include "Weather.h"
17 #include "StarSystem.h"
18 #include "Game.h"
19 
20 // +--------------------------------------------------------------------+
21 
23 {
24  state = CLEAR;
25  period = 7 * 20 * 3600;
26  ceiling = 0;
27  visibility = 1;
28 
29  for (int i = 0; i < NUM_STATES; i++)
30  chances[i] = 0;
31 
32  chances[0] = 1;
33 }
34 
35 // +--------------------------------------------------------------------+
36 
38 { }
39 
40 // +--------------------------------------------------------------------+
41 
42 void
43 Weather::SetChance(int n, double c)
44 {
45  if (n >= 0 && n < NUM_STATES) {
46  if (c > 1 && c <= 100)
47  chances[n] = c / 100;
48 
49  else if (c < 1)
50  chances[n] = c;
51  }
52 }
53 
54 void
56 {
57  double total = 0;
58 
59  for (int i = 1; i < NUM_STATES; i++)
60  total += chances[i];
61 
62  if (total <= 1) {
63  chances[0] = 1 - total;
64  }
65 
66  else {
67  chances[0] = 0;
68 
69  for (int i = 1; i < NUM_STATES; i++)
70  chances[i] /= total;
71  }
72 
73  int index = 0;
74  double level = 0;
75 
76  for (int i = 0; i < NUM_STATES; i++) {
77  if (chances[i] > 0) {
78  level += chances[i];
79 
80  active_states[index] = (STATE) i;
81  thresholds[index] = level;
82 
83  index++;
84  }
85  }
86 
87  while (index < NUM_STATES)
88  thresholds[index++] = 10;
89 }
90 
91 // +--------------------------------------------------------------------+
92 
93 void
95 {
97 
98  double weather = (sin(StarSystem::Stardate() * 2 * PI / period)+1)/2;
99 
100  state = active_states[0];
101 
102  for (int i = 1; i < NUM_STATES; i++) {
103  if (weather > thresholds[i-1] && weather <= thresholds[i]) {
104  state = active_states[i];
105  break;
106  }
107  }
108 
109  switch (state) {
110  default:
111  case CLEAR:
112  ceiling = 0;
113  visibility = 1.0;
114  break;
115 
116  case HIGH_CLOUDS:
117  ceiling = 0;
118  visibility = 0.9;
119  break;
120 
121  case MODERATE_CLOUDS:
122  ceiling = 0;
123  visibility = 0.8;
124  break;
125 
126  case OVERCAST:
127  ceiling = 6000;
128  visibility = 0.7;
129  break;
130 
131  case FOG:
132  ceiling = 3500;
133  visibility = 0.6;
134  break;
135 
136  case STORM:
137  ceiling = 7500;
138  visibility = 0.5;
139  break;
140  }
141 }
142 
143 // +--------------------------------------------------------------------+
144 
145 Text
147 {
148  Text description;
149 
150  switch (state) {
151  default:
152  case CLEAR:
153  description = Game::GetText("weather.clear");
154  break;
155 
156  case HIGH_CLOUDS:
157  description = Game::GetText("weather.high-clouds");
158  break;
159 
160  case MODERATE_CLOUDS:
161  description = Game::GetText("weather.partly-cloudy");
162  break;
163 
164  case OVERCAST:
165  description = Game::GetText("weather.overcast");
166  break;
167 
168  case FOG:
169  description = Game::GetText("weather.fog");
170  break;
171 
172  case STORM:
173  description = Game::GetText("weather.storm");
174  break;
175  }
176 
177  return description;
178 }