-
Notifications
You must be signed in to change notification settings - Fork 2
/
Snow.cs
266 lines (232 loc) · 7.45 KB
/
Snow.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Snow.cs
/*
* This class represents the Snow random event. Snow prevents movement through mountains and
* slows movement elsewhere in the affected area, and prevents rail building.
*
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace Rails
{
public class Snow : Disaster, IDisposable
{
static Random rand = new Random(); // shared by all instances of the Snow class
private Bitmap bitmap; // picture of the snowstorm
private bool[,] mountains; // which mountains are at the center of the storm
private bool[,] affected; // which mileposts are affected
private TerrainType terrain; // whether it's alpine, or both alpine and mountain
public Snow(int player, Map map, bool alpine) : base(player, map)
{
// create flags indicating the terrain types that prevent movement
terrain = TerrainType.Alpine;
if (!alpine)
terrain |= TerrainType.Mountain;
// find a suitable location for the storm
int x = 0, y = 0;
while (true)
{
// pick a spot at random
x = rand.Next(map.GridSize.Width);
y = rand.Next(map.GridSize.Height);
// it has to be the correct terrain type
if ((map[x, y].Terrain & terrain) == 0)
continue;
// it has to be adjacent to at least three other mountains
int n = 0;
int i, j;
for (int d=0; d<6; d++)
if (map.GetAdjacent(x, y, d, out i, out j))
if (map[i, j].Terrain == TerrainType.Mountain || map[i, j].Terrain == TerrainType.Alpine)
n++;
if (n < 3)
continue;
break;
}
// find the contiguous chain of mountains connected to our starting mountain,
// but only go up to three mileposts away
map.ResetFloodMap();
map.Flood(x, y, new FloodMethod(AdjacentMountainsMethod));
// create the shape of the snowstorm
Region region = new Region();
region.MakeEmpty();
mountains = new bool[map.GridSize.Width, map.GridSize.Height];
for (x=0; x<map.GridSize.Width; x++)
for (y=0; y<map.GridSize.Height; y++)
if (map[x, y].Value != int.MaxValue)
{
mountains[x, y] = true;
map.SetValue(x, y, 0);
GraphicsPath path = new GraphicsPath();
int xx, yy;
map.GetCoord(x, y, out xx, out yy);
path.AddEllipse(xx - 55, yy - 55, 110, 110);
region.Union(path);
}
// draw the snowstorm
bitmap = new Bitmap(map.ImageSize.Width, map.ImageSize.Height);
Graphics g = Graphics.FromImage(bitmap);
// semi-transparent gray background
Brush brush = new SolidBrush(Color.FromArgb(128, Color.Gray));
g.FillRegion(brush, region);
brush.Dispose();
// find all mileposts within three units of the affected mountain chain
map.Flood(new FloodMethod(LinearDistanceMethod));
affected = new bool[map.GridSize.Width, map.GridSize.Height];
for (x=0; x<map.GridSize.Width; x++)
for (y=0; y<map.GridSize.Height; y++)
if (map[x, y].Value < 4)
{
affected[x, y] = true;
int xx, yy;
map.GetCoord(x, y, out xx, out yy);
// Draw random snowflakes.
// See Graphics.exe companion program for snowflake generator.
g.DrawImageUnscaled(Images.Snowflake[rand.Next(10)], xx - 15 + rand.Next(10), yy - 15 + rand.Next(10));
}
g.Dispose();
}
public override void Save(BinaryWriter writer)
{
base.Save(writer);
writer.Write((int) 0); // version
writer.Write((int) terrain);
int m = 0, n = 0;
for (int x=0; x<map.GridSize.Width; x++)
for (int y=0; y<map.GridSize.Height; y++)
{
if (mountains[x, y])
m++;
if (affected[x, y])
n++;
}
writer.Write(m);
for (int x=0; x<map.GridSize.Width; x++)
for (int y=0; y<map.GridSize.Height; y++)
if (mountains[x, y])
{
writer.Write(x);
writer.Write(y);
}
writer.Write(n);
for (int x=0; x<map.GridSize.Width; x++)
for (int y=0; y<map.GridSize.Height; y++)
if (affected[x, y])
{
writer.Write(x);
writer.Write(y);
}
}
public Snow(BinaryReader reader, Map map) : base(reader, map)
{
int version = reader.ReadInt32();
terrain = (TerrainType) reader.ReadInt32();
int m = reader.ReadInt32();
mountains = new bool[map.GridSize.Width, map.GridSize.Height];
for (int i=0; i<m; i++)
{
int x = reader.ReadInt32();
int y = reader.ReadInt32();
mountains[x, y] = true;
}
int n = reader.ReadInt32();
affected = new bool[map.GridSize.Width, map.GridSize.Height];
for (int i=0; i<n; i++)
{
int x = reader.ReadInt32();
int y = reader.ReadInt32();
affected[x, y] = true;
}
}
// find adjacent mountain chain in path-finding algorithm
public bool AdjacentMountainsMethod(int x, int y, int d, int i, int j, out int cost)
{
cost = 1;
// don't go more than three dots (0, 1, 2)
if (map[x, y].Value > 2)
return false;
// only go to mountain spots
return (map[i, j].Terrain == TerrainType.Mountain || map[i, j].Terrain == TerrainType.Alpine);
}
public override void Draw(Graphics g)
{
if (bitmap == null)
{
// create the shape of the snowstorm
using (Region region = new Region())
{
region.MakeEmpty();
for (int x=0; x<map.GridSize.Width; x++)
for (int y=0; y<map.GridSize.Height; y++)
if (mountains[x, y])
{
int xx, yy;
map.GetCoord(x, y, out xx, out yy);
using (GraphicsPath path = new GraphicsPath())
{
path.AddEllipse(xx - 55, yy - 55, 110, 110);
region.Union(path);
}
}
// draw the snowstorm
bitmap = new Bitmap(map.ImageSize.Width, map.ImageSize.Height);
using (Graphics gr = Graphics.FromImage(bitmap))
{
// semi-transparent gray background
using (Brush brush = new SolidBrush(Color.FromArgb(128, Color.Gray)))
{
gr.FillRegion(brush, region);
}
// find all mileposts within three units of the affected mountain chain
for (int x=0; x<map.GridSize.Width; x++)
for (int y=0; y<map.GridSize.Height; y++)
if (affected[x, y])
{
int xx, yy;
map.GetCoord(x, y, out xx, out yy);
// Draw random snowflakes.
// See Graphics.exe companion program for snowflake generator.
gr.DrawImageUnscaled(Images.Snowflake[rand.Next(10)], xx - 15 + rand.Next(10), yy - 15 + rand.Next(10));
}
}
}
}
g.DrawImageUnscaled(bitmap, 0, 0);
}
public override void Dispose()
{
base.Dispose();
if (bitmap != null)
bitmap.Dispose();
}
~Snow()
{
Dispose();
}
public override string ToString()
{
if (terrain == TerrainType.Alpine)
return "Severe Winter Storm\rAlpine passes closed";
else
return "Severe Winter Storm\rMountains passes closed";
}
public override bool AdjustMovementCost(int x, int y, int d, int i, int j, ref int cost)
{
// half movement speed
if (affected[x, y] || affected[i, j])
cost *= 2;
// no movement in mountains/alpine, or just alpine
if (affected[i, j] && (map[i, j].Terrain & terrain) != 0)
return false;
return true;
}
public override bool AdjustBuildingCost(int x, int y, int d, int i, int j, ref int cost)
{
// no building in storm area
if (affected[i, j] && (map[i, j].Terrain & terrain) != 0)
return false;
return true;
}
}
}