/
MazeRoom.cs
253 lines (202 loc) · 6.79 KB
/
MazeRoom.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
using System;
using System.Collections;
using System.Xml;
using System.Drawing;
using Geometry;
namespace MazeEditor
{
public enum MazeSpaceType
{
room, staricase, lift, junction, corridor, hall
}
public class MazeSpace : MazeIdentifiable
{
public ArrayList Walls;
protected Polygon2D roomPolygon;
public Polygon2D RoomPolygon
{
get
{
return roomPolygon;
}
}
public MazeSpaceType MazeRoomType = MazeSpaceType.room;
public ArrayList BorderNodes;
public MazeNode CetralNode;
public string Function;
public int ExpectedPersonCount;
public int Searched;
protected static Random random = new Random();
public string Name;
public MazeSpace(ArrayList walls) : base ()
{
this.Walls = (ArrayList)walls.Clone();
BorderNodes = new ArrayList();
BuildPolygon();
ExpectedPersonCount = (int)(Area * random.NextDouble());
int doorCount = 0;
foreach (MazeWall wall in Walls)
if (wall.MazeWallType == MazeWallType.gate)
doorCount++;
if (doorCount > 2)
{
MazeRoomType = MazeEditor.MazeSpaceType.corridor;
ExpectedPersonCount = 0;
}
}
/// <summary>
///
/// </summary>
private void BuildPolygon()
{
Point2D[] points = new Point2D[Walls.Count];
points[0] = (Walls[0] as MazeWall).points[0];
ArrayList wallsCopy = (ArrayList) Walls.Clone();
wallsCopy.RemoveAt(0);
for (int i = 1; i < points.Length; i++)
{
//find adjacent wall... the closest
foreach (MazeWall wall in wallsCopy)
{
if (Point2D.GetDistance(wall.points[0], points[i - 1]) < 0.01)
{
points[i] = wall.points[1];
wallsCopy.Remove(wall);
break;
}
else if (Point2D.GetDistance(wall.points[1], points[i - 1]) < 0.01)
{
points[i] = wall.points[0];
wallsCopy.Remove(wall);
break;
}
}
}
roomPolygon = new Polygon2D(points);
}
/*
//find adjacent wall... the closest
double minDistance = Double.MaxValue;
Point2D pointToAdd = new Point2D(0, 0);
MazeWall wallToAdd = null;
foreach (MazeWall wall in wallsCopy)
{
if (Point2D.GetDistance(wall.points[0], points[i - 1]) < minDistance)
{
minDistance = Point2D.GetDistance(wall.points[0], points[i - 1]);
pointToAdd = wall.points[1];
wallToAdd = wall;
}
else if (Point2D.GetDistance(wall.points[1], points[i - 1]) < minDistance)
{
minDistance = Point2D.GetDistance(wall.points[1], points[i - 1]);
pointToAdd = wall.points[0];
wallToAdd = wall;
}
}
if (wallToAdd != null)
{
points[i] = pointToAdd;
wallsCopy.Remove(wallToAdd);
}
*/
public Point2D CenterPoint
{
get { return RoomPolygon.GetCentroid(); }
}
public Point2D[] Points
{
get { return RoomPolygon.points; }
}
public override bool Equals(object obj)
{
MazeSpace w = obj as MazeSpace;
if (w == null)
return false;
foreach (MazeWall wall in w.Walls)
if (!Walls.Contains(wall))
return false;
return true;
}
public bool ContainsPoint(Point2D p)
{
int i;
bool value;
double x1;
double x2;
double y1;
double y2;
value = false;
for (i = 0; i < RoomPolygon.points.Length; i++)
{
x1 = RoomPolygon.points[i].x;
y1 = RoomPolygon.points[i].y;
if (i < RoomPolygon.points.Length - 1)
{
x2 = RoomPolygon.points[i + 1].x;
y2 = RoomPolygon.points[i + 1].y;
}
else
{
x2 = RoomPolygon.points[0].x;
y2 = RoomPolygon.points[0].y;
}
if ((y1 < p.y && p.y <= y2) ||
(p.y <= y1 && y2 < p.y))
{
if ((p.x - x1) - (p.y - y1) * (x2 - x1) / (y2 - y1) < 0)
{
value = !value;
}
}
}
return value;
}
public double Diameter
{
get
{
double diameter;
int i;
int j;
diameter = 0.0;
for (i = 0; i < RoomPolygon.points.Length; i++)
{
for (j = i + 1; j < RoomPolygon.points.Length; j++)
{
diameter = Math.Max(diameter,
Math.Sqrt((RoomPolygon.points[i].x - RoomPolygon.points[j].x) * (RoomPolygon.points[i].x - RoomPolygon.points[j].x)
+ (RoomPolygon.points[i].y - RoomPolygon.points[j].y) * (RoomPolygon.points[i].y - RoomPolygon.points[j].y)));
}
}
return diameter/100;
}
}
public double Area
{
get {
double area;
int i;
int im1;
int ip1;
area = 0.0;
for (i = 0; i < RoomPolygon.points.Length; i++)
{
im1 = i - 1;
ip1 = i + 1;
if (im1 < 0)
{
im1 = RoomPolygon.points.Length -1;
}
if (RoomPolygon.points.Length <= ip1)
{
ip1 = 0;
}
area = area + RoomPolygon.points[i].x * (RoomPolygon.points[ip1].y - RoomPolygon.points[im1].y);
}
area = 0.5 * area;
return Math.Abs(area/10000);
}
}
}
}