-
Notifications
You must be signed in to change notification settings - Fork 7
/
MapMaker.cs
168 lines (144 loc) · 6.42 KB
/
MapMaker.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Voxel_Fortress
{
class MapMaker
{
public class Images
{
public Color[,] elevationMap;
public Color[,] colorMap;
public string imageFilename;
}
const int maxSize = 2048;
XRaw voxels;
int[,] landElevations = null;
bool IsWaterMap(Color[,] elevationMap)
{
foreach (var item in elevationMap)
{
if (item.R == 0 && item.G > 0 && item.B > 0)
return true;
}
return false;
}
int[,] LoadElevations( Color[,] elevationMap, object sender, out int min, out int max)
{
min = int.MaxValue;
max = int.MinValue;
int[,] elevations = new int[elevationMap.GetLength(0), elevationMap.GetLength(1)];
for (int x = 0; x < elevationMap.GetLength(0); x++)
{
for (int y = 0; y < elevationMap.GetLength(1); y++)
{
if (elevationMap[x, y].R == 0)
elevations[x, y] = elevationMap[x, y].B;
else
elevations[x, y] = elevationMap[x, y].B + 25;
min = Math.Min(min, elevations[x, y]);
max = Math.Max(max, elevations[x, y]);
}
(sender as BackgroundWorker).ReportProgress(x * 2048 / elevationMap.GetLength(0), string.Format("Loading Heightmap: {0} of {1}", x, elevationMap.GetLength(0)));
}
return elevations;
}
int[,] LoadWaterElevations(Color[,] elevationMap, object sender, out int min, out int max)
{
min = int.MaxValue;
max = int.MinValue;
int[,] elevations = new int[elevationMap.GetLength(0), elevationMap.GetLength(1)];
for (int x = 0; x < elevationMap.GetLength(0); x++)
{
for (int y = 0; y < elevationMap.GetLength(1); y++)
{
if (elevationMap[x, y].R > 0)
elevations[x, y] = elevationMap[x, y].B + 25; //This means it's not water.
else if (elevationMap[x, y].G > 0)
elevations[x, y] = elevationMap[x, y].B; //Rivers
else
elevations[x, y] = elevationMap[x, y].B + 25;
min = Math.Min(min, elevations[x, y]);
max = Math.Max(max, elevations[x, y]);
}
(sender as BackgroundWorker).ReportProgress(x * 2048 / elevationMap.GetLength(0), string.Format("Loading Water: {0} of {1}", x, elevationMap.GetLength(0)));
}
return elevations;
}
public static Color[,] ConvertBitmapImage(BitmapImage image)
{
int bytesPerPixel = (image.Format.BitsPerPixel + 7) / 8;
byte[] colorArray = new byte[image.PixelWidth * image.PixelHeight * bytesPerPixel];
image.CopyPixels(colorArray, bytesPerPixel * image.PixelWidth, 0);
Color[,] pixelArray = new Color[image.PixelWidth, image.PixelHeight];
for (int x = 0; x < image.PixelWidth; x++)
for (int y = 0; y < image.PixelHeight; y++)
{
int index = (x + y * image.PixelWidth) * bytesPerPixel;
pixelArray[x, y] = Color.FromRgb(colorArray[index + 2], colorArray[index + 1], colorArray[index + 0]);
}
return pixelArray;
}
public void LoadMap(object sender, DoWorkEventArgs e)
{
try
{
Images images = (Images)e.Argument;
int min, max;
if (images.elevationMap != null)
{
if (IsWaterMap(images.elevationMap))
landElevations = LoadWaterElevations(images.elevationMap, sender, out min, out max);
else
landElevations = LoadElevations(images.elevationMap, sender, out min, out max);
}
else if (images.colorMap != null)
{
landElevations = new int[images.colorMap.GetLength(0), images.colorMap.GetLength(1)];
min = 0;
max = 0;
}
else return; //nothing to work with.
int voxelWidth = landElevations.GetLength(0);
int voxelLength = landElevations.GetLength(1);
if (voxelWidth > maxSize)
voxelWidth = maxSize;
if (voxelLength > maxSize)
voxelLength = maxSize;
voxels = new XRaw(voxelWidth, voxelLength, 280);
for (int x = 0; x < voxelWidth; x++)
{
for (int y = 0; y < voxelLength; y++)
{
int zMin = landElevations[x, y];
if (x > 0)
zMin = Math.Min(zMin, landElevations[x - 1, y]);
if (y > 0)
zMin = Math.Min(zMin, landElevations[x, y - 1]);
if (x < voxelWidth - 1)
zMin = Math.Min(zMin, landElevations[x + 1, y]);
if (y < voxelLength - 1)
zMin = Math.Min(zMin, landElevations[x, y + 1]);
if (images.colorMap != null)
voxels.SetColumn(images.colorMap[x, y], x, y, zMin - min, landElevations[x, y] - min);
else
voxels.SetColumn(Colors.White, x, y, zMin - min, landElevations[x, y] - min);
}
(sender as BackgroundWorker).ReportProgress(x * 2048 / voxelWidth, string.Format("Generating Voxels: {0} of {1}", x, voxelWidth));
}
string savePath = Path.ChangeExtension(images.imageFilename, "xraw");
voxels.SaveFile(savePath, sender);
(sender as BackgroundWorker).ReportProgress(2048, "Finished!");
}
catch (Exception exception)
{
(sender as BackgroundWorker).ReportProgress(0, exception.Message);
}
}
}
}