-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherForecast.cs
240 lines (219 loc) · 9.45 KB
/
WeatherForecast.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public static class WeatherForecast
{
public static List<string> weatherlessRegions = new List<string>();
public static Dictionary<string, int> regionWindDirection = new Dictionary<string, int>();
public static Dictionary<string, float> dynamicRegionStartingIntensity = new Dictionary<string, float>();
//Dictionary<string, dictionary<weatherType, float>>
//In ForecastConfig, a dictionary containing save data of all region and the list of possible weathers that can happen in them.
//Each weather has a percentage chance to occur -- unless the weather preference overrides it.
//Weather preference will not kick in if the chosen weather is not one that's been enabled for that region
//In that case, it will revert back to the percentage chances for the selected weathers
//If it fails to generate a new weather type, it will do the same weather again
public static Dictionary<string, Dictionary<Weather.WeatherType, float>> regionWeatherProbability = new Dictionary<string, Dictionary<Weather.WeatherType, float>>();
public static Dictionary<string, List<Weather.WeatherType>> regionWeatherForecasts = new Dictionary<string, List<Weather.WeatherType>>();
//Method to be replaced in ForecastConfig
public static void GenerateWeathers()
{
if(regionWeatherProbability.Keys.Count > 0)
{
//Already generated weather probability
return;
}
regionWeatherProbability = new Dictionary<string, Dictionary<Weather.WeatherType, float>>();
foreach(string reg in ForecastConfig.regionSettings.Keys)
{
Dictionary<Weather.WeatherType, float> weathers = new Dictionary<Weather.WeatherType, float>();
float chance = 1f / Enum.GetNames(typeof(Weather.WeatherType)).Length;
weathers.Add(Weather.WeatherType.LightRain, UnityEngine.Random.value);
weathers.Add(Weather.WeatherType.HeavyRain, UnityEngine.Random.value);
weathers.Add(Weather.WeatherType.Thunderstorm, UnityEngine.Random.value);
weathers.Add(Weather.WeatherType.Fog, UnityEngine.Random.value);
weathers.Add(Weather.WeatherType.Hail, UnityEngine.Random.value);
weathers.Add(Weather.WeatherType.LightSnow, UnityEngine.Random.value);
weathers.Add(Weather.WeatherType.HeavySnow, UnityEngine.Random.value);
weathers.Add(Weather.WeatherType.Blizzard, UnityEngine.Random.value);
regionWeatherProbability.Add(reg, weathers);
}
}
public static void InitialWeather()
{
if(regionWeatherForecasts.Keys.Count > 0)
{
//There are already existing forecasts
ForecastLog.Log("Initial weather already generated");
return;
}
regionWeatherForecasts = new Dictionary<string, List<Weather.WeatherType>>();
foreach(string region in regionWeatherProbability.Keys)
{
ForecastLog.Log($"FORECAST FOR {region}");
regionWeatherForecasts.Add(region, new List<Weather.WeatherType>());
regionWeatherForecasts[region].Add(RandomWeather(region));
regionWeatherForecasts[region].Add(NextWeather(region, regionWeatherForecasts[region][0]));
regionWeatherForecasts[region].Add(NextWeather(region, regionWeatherForecasts[region][1]));
ForecastLog.Log($"--------------------------------------------------------");
}
WeatherData.Save();
}
public static Weather.WeatherType NextWeather(string region, Weather.WeatherType lastWeather)
{
if (UnityEngine.Random.value < 0.75f)
{
float totalProbability = 0f;
foreach (var pair in regionWeatherProbability[region])
{
totalProbability += pair.Value;
}
float rand = UnityEngine.Random.Range(0,totalProbability);
List<Weather.WeatherType> shuffled = regionWeatherProbability[region].Keys.ToList();
Shuffle(shuffled);
for (int i = 0; i < shuffled.Count; i++)
{
rand -= regionWeatherProbability[region][shuffled[i]];
if (rand <= 0f)
{
ForecastLog.Log($"{region}: Preferred = {shuffled[i]}");
return shuffled[i];
}
}
ForecastLog.Log($"{region}: Repeat = {lastWeather}");
return lastWeather;
}
else
{
return RandomWeather(region);
}
}
public static Weather.WeatherType RandomWeather(string region)
{
float rand = UnityEngine.Random.value;
var nearestPair = regionWeatherProbability[region].OrderBy(kv => Math.Abs(kv.Value - rand)).First();
ForecastLog.Log($"{region}: Random = {nearestPair.Key}");
return nearestPair.Key;
}
static void Shuffle<T>(List<T> list)
{
System.Random random = new System.Random();
int n = list.Count;
for (int i = n - 1; i > 0; i--)
{
int j = random.Next(0, i + 1);
T temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
public static void ClearAll()
{
weatherlessRegions.Clear();
regionWindDirection.Clear();
dynamicRegionStartingIntensity.Clear();
}
public class Weather
{
public WeatherType type;
public float minIntensity;
public float maxIntensity;
public int weatherIndex;
public Dictionary<WeatherType, float> nextPreference;
public Weather(WeatherType type)
{
try
{
GenerateWeather(type);
}
catch(Exception e)
{
Debug.LogException(e);
}
}
public void GenerateWeather(WeatherType type)
{
nextPreference = new Dictionary<WeatherType, float>();
this.type = type;
switch (type)
{
case WeatherType.LightRain:
weatherIndex = 0;
minIntensity = 0f + UnityEngine.Random.Range(0f, 0.2f);
maxIntensity = 0.65f;
nextPreference.Add(WeatherType.HeavyRain, 0.5f);
nextPreference.Add(WeatherType.Fog, 0.15f);
nextPreference.Add(WeatherType.Hail, 0.1f);
break;
case WeatherType.HeavyRain:
weatherIndex = 0;
minIntensity = 0.5f + UnityEngine.Random.Range(0f, 0.2f);
maxIntensity = 1f;
nextPreference.Add(WeatherType.LightRain, 0.3f);
nextPreference.Add(WeatherType.Thunderstorm, 0.4f);
break;
case WeatherType.Thunderstorm:
weatherIndex = 0;
minIntensity = 0.85f;
maxIntensity = 1f;
nextPreference.Add(WeatherType.LightRain, 0.45f);
nextPreference.Add(WeatherType.Fog, 0.4f);
nextPreference.Add(WeatherType.HeavyRain, 0.15f);
break;
case WeatherType.Fog:
weatherIndex = 0;
minIntensity = 0f;
maxIntensity = 0.1f;
nextPreference.Add(WeatherType.LightRain, 0.35f);
nextPreference.Add(WeatherType.LightSnow, 0.2f);
break;
case WeatherType.Hail:
weatherIndex = 0;
minIntensity = 0f;
maxIntensity = 0.75f;
nextPreference.Add(WeatherType.LightSnow, 0.35f);
nextPreference.Add(WeatherType.HeavyRain, 0.2f);
nextPreference.Add(WeatherType.Thunderstorm, 0.15f);
break;
case WeatherType.LightSnow:
weatherIndex = 2;
minIntensity = 0f + UnityEngine.Random.Range(0f, 0.2f);
maxIntensity = 0.55f;
nextPreference.Add(WeatherType.HeavySnow, 0.6f);
nextPreference.Add(WeatherType.Blizzard, 0.3f);
nextPreference.Add(WeatherType.Hail, 0.1f);
break;
case WeatherType.HeavySnow:
weatherIndex = 2;
minIntensity = 0.45f;
maxIntensity = 0.9f;
nextPreference.Add(WeatherType.LightSnow, 0.35f);
nextPreference.Add(WeatherType.Blizzard, 0.35f);
nextPreference.Add(WeatherType.Fog, 0.1f);
break;
case WeatherType.Blizzard:
weatherIndex = 2;
minIntensity = 0.9f;
maxIntensity = 1f;
nextPreference.Add(WeatherType.LightRain, 0.1f);
nextPreference.Add(WeatherType.LightSnow, 0.3f);
nextPreference.Add(WeatherType.Hail, 0.1f);
nextPreference.Add(WeatherType.HeavySnow, 0.3f);
break;
}
}
public enum WeatherType
{
LightRain,
HeavyRain,
Thunderstorm,
Fog,
Hail,
LightSnow,
HeavySnow,
Blizzard,
}
}
}