-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.cs
214 lines (184 loc) · 6.15 KB
/
Map.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
using System;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using System.Text;
// using Pastel;
namespace Map
{
public struct Coord
{
public int x;
public int y;
public int? z;
public Coord (int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Coord (int x, int y)
{
this.x = x;
this.y = y;
this.z = null;
}
}
[Serializable]
public class Unit : ICloneable
{
/*
A unit is a single point on a grid, with it's own Flag, Sprite, character, and color
information.
*/
public Dictionary<string, object> Flags;
public char Character {get; set;}
public Color? ColorValue {get; set;}
public Unit (char Character, Color? ColorValue = null)
{
this.Character = Character;
this.ColorValue = ColorValue;
this.Flags = new Dictionary<string, object>();
}
public T GetFlag<T>(string flag)
{
return (T)Flags[flag];
}
public void SetFlag (string flag, object value)
{
if (Flags.ContainsKey(flag))
Flags.Remove(flag);
Flags.Add(flag, value);
}
public bool HasFlag(string flag)
{
return Flags.ContainsKey(flag);
}
public object Clone ()
{
Unit C = (Unit)this.MemberwiseClone();
C.Flags = new Dictionary<string, object>();
foreach(string flag in Flags.Keys)
C.Flags.Add(flag, Flags[flag]);
return (object)C;
}
}
[Serializable]
public class Grid : ICloneable
{
public Unit [,,] posGrid;
public int width, height, depth;
public Grid (int width, int height, int depth)
{
this.width = width;
this.height = height;
this.depth = depth;
this.posGrid = new Unit [this.depth, this.height, this.width];
for (int z = 0; z < this.depth; z++)
for (int y = 0; y < this.height; y++)
for (int x = 0; x < this.width; x++)
this[z, y, x] = null;
}
public Unit this[int z, int y, int x]
{
get
{
return posGrid[z, y, x];
}
set
{
posGrid[z, y, x] = value;
}
}
public Unit this[Coord C]
{
get
{
return posGrid[C.z ?? 0, C.y, C.x];
}
set
{
posGrid[C.z ?? 0, C.y, C.x] = value;
}
}
public object Clone()
{
Grid clone = (Grid)this.MemberwiseClone();
/*
memberwiseclone doesn't clone arrays, so we must clone each
individual element (unit) to a new array.
*/
Unit [,,] posGridClone = new Unit [this.depth, this.height, this.width];
for (int z = 0; z < depth; z++)
for (int y = 0; y < height; y++)
for (int u = 0; u < width; u++)
if (this[z, y, u] != null)
posGridClone[z, y, u] = (Unit)clone[z, y, u].Clone();
else
posGridClone[z, y, u] = null;
clone.posGrid = posGridClone;
return (object)clone;
}
}
public static class GridTools
{
public static void FillLayer (this Grid G, int z, Unit u)
{
for (int y = 0; y < G.height; y++)
for (int x = 0; x < G.width; x++)
{
G[z, y, x] = (Unit)u.Clone();
}
}
public static void SetLayer (this Grid G, int z, Grid B)
{
for (int y = 0; y < G.height; y++)
for (int x = 0; x < G.width; x++)
{
G[z, y, x] = (Unit)B[z, y, x].Clone();
}
}
public static Grid ApproximateImage(string path, int width, int height)
{
Grid product = new Grid(width, height, 1);
product.FillLayer(0, new Unit('\u2593'));
Bitmap image = new Bitmap(path);
int wR = image.Width / width;
int hR = image.Height / height;
for (int bY = 0; bY < height; bY++)
{
for (int bX = 0; bX < width; bX++)
{
int AvgR = 0;
int AvgG = 0;
int AvgB = 0;
int AvgA = 0;
int pixelCount = 0;
for (int y = (hR * bY); y < hR + (hR * bY); y++)
for (int x = (wR * bX); x < wR + (wR * bX); x++)
{
// Console.WriteLine((wR + (wR * bX)));
if (x % 2 == 0)
{
Color pixel = image.GetPixel(x ,y);
AvgR += pixel.R;
AvgG += pixel.G;
AvgB += pixel.B;
AvgA += pixel.A;
pixelCount++;
}
}
AvgR = AvgR/pixelCount;
AvgG = AvgG/pixelCount;
AvgB = AvgB/pixelCount;
AvgA = AvgA/pixelCount;
if (AvgA >225)
product[0, bY, bX].ColorValue = Color.FromArgb(AvgR, AvgG, AvgB);
else
product[0, bY, bX].ColorValue = null;
}
}
return product;
}
}
}