forked from genstein/trizbort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompassPoint.cs
363 lines (343 loc) · 13.5 KB
/
CompassPoint.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
Copyright (c) 2010 by Genstein
This file is (or was originally) part of Trizbort, the Interactive Fiction Mapper.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace Trizbort
{
/// <summary>
/// A abstract point on the compass rose.
/// </summary>
internal enum CompassPoint
{
North,
NorthNorthEast,
NorthEast,
EastNorthEast,
East,
EastSouthEast,
SouthEast,
SouthSouthEast,
South,
SouthSouthWest,
SouthWest,
WestSouthWest,
West,
WestNorthWest,
NorthWest,
NorthNorthWest,
Min = North,
Max = NorthNorthWest,
}
internal static class CompassPointHelper
{
private static readonly string[] Names =
{
"n",
"nne",
"ne",
"ene",
"e",
"ese",
"se",
"sse",
"s",
"ssw",
"sw",
"wsw",
"w",
"wnw",
"nw",
"nnw",
};
public static bool ToName(CompassPoint point, out string name)
{
int index = (int)point;
if (index >= 0 && index < Names.Length)
{
name = Names[index];
return true;
}
name = string.Empty;
return false;
}
public static bool FromName(string name, out CompassPoint point)
{
for (int index = 0; index < Names.Length; ++index)
{
if (StringComparer.InvariantCultureIgnoreCase.Compare(name ?? string.Empty, Names[index]) == 0)
{
point = (CompassPoint)index;
return true;
}
}
point = CompassPoint.North;
return false;
}
/// <summary>
/// Rotate a point clockwise to find its neighbour on that side.
/// </summary>
public static CompassPoint RotateClockwise(CompassPoint point)
{
point = (CompassPoint)((int)point + 1);
if (point > CompassPoint.Max)
{
point = CompassPoint.Min;
}
return point;
}
/// <summary>
/// Rotate a point anti-clockwise to find its neighbour on that side.
/// </summary>
public static CompassPoint RotateAntiClockwise(CompassPoint point)
{
point = (CompassPoint)((int)point - 1);
if (point < CompassPoint.Min)
{
point = CompassPoint.Max;
}
return point;
}
/// <summary>
/// Get the geometric opposite of a compass point on the compass rose.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public static CompassPoint GetOpposite(CompassPoint point)
{
for (int index = 0; index < (int)(CompassPoint.Max - CompassPoint.Min + 1) / 2; ++index)
{
point = RotateClockwise(point);
}
return point;
}
/// <summary>
/// Get the compass point which is the opposite of the given point,
/// for the purpose of connecting rooms during automapping.
/// </summary>
/// <remarks>
/// Treating the room as a box, and mapping compass points onto it,
/// the automap opposite is the corresponding point on the opposite
/// side of the box.
/// </remarks>
public static CompassPoint GetAutomapOpposite(CompassPoint point)
{
switch (point)
{
case CompassPoint.North:
return CompassPoint.South;
case CompassPoint.NorthNorthEast:
return CompassPoint.SouthSouthEast; // vertical
case CompassPoint.NorthEast:
return CompassPoint.SouthWest;
case CompassPoint.EastNorthEast:
return CompassPoint.WestNorthWest; // horizontal
case CompassPoint.East:
return CompassPoint.West;
case CompassPoint.EastSouthEast:
return CompassPoint.WestSouthWest; // horizontal
case CompassPoint.SouthEast:
return CompassPoint.NorthWest;
case CompassPoint.SouthSouthEast:
return CompassPoint.NorthNorthEast; // vertical
case CompassPoint.South:
return CompassPoint.North;
case CompassPoint.SouthSouthWest:
return CompassPoint.NorthNorthWest; // vertical
case CompassPoint.SouthWest:
return CompassPoint.NorthEast;
case CompassPoint.WestSouthWest:
return CompassPoint.EastSouthEast; // horizontal
case CompassPoint.West:
return CompassPoint.East;
case CompassPoint.WestNorthWest:
return CompassPoint.EastNorthEast; // horizontal
case CompassPoint.NorthWest:
return CompassPoint.SouthEast;
case CompassPoint.NorthNorthWest:
return CompassPoint.SouthSouthWest; // vertical
default:
Debug.Assert(false, "Opposite compass point not found.");
return CompassPoint.North;
}
}
/// <summary>
/// Get the direction delta (where x and y are range -1 to 1) in which
/// we would place a new room given a connection in the given direction.
/// </summary>
/// <remarks>
/// Two compass points have the same direction vector if they are mapped
/// onto the the same side of a box drawn to represent the room.
/// </remarks>
public static Vector GetAutomapDirectionVector(CompassPoint compassPoint)
{
switch (compassPoint)
{
case CompassPoint.NorthNorthWest:
case CompassPoint.North:
case CompassPoint.NorthNorthEast:
return new Vector(0, -1);
case CompassPoint.NorthEast:
return new Vector(1, -1);
case CompassPoint.EastNorthEast:
case CompassPoint.East:
case CompassPoint.EastSouthEast:
return new Vector(1, 0);
case CompassPoint.SouthEast:
return new Vector(1, 1);
case CompassPoint.SouthSouthEast:
case CompassPoint.South:
case CompassPoint.SouthSouthWest:
return new Vector(0, 1);
case CompassPoint.SouthWest:
return new Vector(-1, 1);
case CompassPoint.WestSouthWest:
case CompassPoint.West:
case CompassPoint.WestNorthWest:
return new Vector(-1, 0);
case CompassPoint.NorthWest:
return new Vector(-1, -1);
default:
Debug.Assert(false, "Direction vector not found.");
return new Vector(0, -1);
}
}
public static CompassPoint GetCompassPointFromAutomapDirectionVector(Vector vector)
{
if (vector.X < 0)
{
if (vector.Y < 0)
{
return CompassPoint.NorthWest;
}
if (vector.Y > 0)
{
return CompassPoint.SouthWest;
}
return CompassPoint.West;
}
else if (vector.X > 0)
{
if (vector.Y < 0)
{
return CompassPoint.NorthEast;
}
if (vector.Y > 0)
{
return CompassPoint.SouthEast;
}
return CompassPoint.East;
}
else
{
if (vector.Y < 0)
{
return CompassPoint.North;
}
if (vector.Y > 0)
{
return CompassPoint.South;
}
Debug.Assert(false, "Automap direction vector should not be zero.");
return CompassPoint.North;
}
}
/// <summary>
/// Convert an automap direction into a compass point.
/// Compass directions map directly; up/down/in/out are assigned specific other diretions.
/// </summary>
public static CompassPoint GetCompassDirection(AutomapDirection direction)
{
switch (direction)
{
case AutomapDirection.Up:
return CompassPoint.NorthNorthWest;
case AutomapDirection.Down:
return CompassPoint.SouthSouthWest;
case AutomapDirection.In:
return CompassPoint.EastNorthEast;
case AutomapDirection.Out:
return CompassPoint.WestNorthWest;
case AutomapDirection.North:
return CompassPoint.North;
case AutomapDirection.South:
return CompassPoint.South;
case AutomapDirection.East:
return CompassPoint.East;
case AutomapDirection.West:
return CompassPoint.West;
case AutomapDirection.NorthEast:
return CompassPoint.NorthEast;
case AutomapDirection.NorthWest:
return CompassPoint.NorthWest;
case AutomapDirection.SouthEast:
return CompassPoint.SouthEast;
case AutomapDirection.SouthWest:
return CompassPoint.SouthWest;
default:
Debug.Assert(false, "Couldn't work out the compass direction for the given automap direction.");
return CompassPoint.North;
}
}
/// <summary>
/// "Round" the compass point to the nearest cardinal or ordinal direction.
/// </summary>
public static CompassPoint GetNearestCardinalOrOrdinal(CompassPoint compassPoint)
{
return GetCompassPointFromAutomapDirectionVector(GetAutomapDirectionVector(compassPoint));
}
/// <summary>
/// Get the literal opposite of any direction.
/// </summary>
public static AutomapDirection GetOpposite(AutomapDirection direction)
{
switch (direction)
{
case AutomapDirection.North:
return AutomapDirection.South;
case AutomapDirection.South:
return AutomapDirection.North;
case AutomapDirection.East:
return AutomapDirection.West;
case AutomapDirection.West:
return AutomapDirection.East;
case AutomapDirection.NorthEast:
return AutomapDirection.SouthWest;
case AutomapDirection.NorthWest:
return AutomapDirection.SouthEast;
case AutomapDirection.SouthEast:
return AutomapDirection.NorthWest;
case AutomapDirection.SouthWest:
return AutomapDirection.NorthEast;
case AutomapDirection.Up:
return AutomapDirection.Down;
case AutomapDirection.Down:
return AutomapDirection.Up;
case AutomapDirection.In:
return AutomapDirection.Out;
case AutomapDirection.Out:
return AutomapDirection.In;
default:
Debug.Assert(false, "Couldn't work out the opposite of the given direction.");
return AutomapDirection.North;
}
}
}
}