-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhexutil_test.py
238 lines (200 loc) · 6.94 KB
/
hexutil_test.py
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
import unittest
import hexutil
class HexMap(object):
def __init__(self, str):
self.source = str
player = None
target = None
tiles = {}
line_lengths = []
lights = []
for y, line in enumerate(str.split("\n")):
line_lengths.append(len(line))
for x, ch in enumerate(line):
if ch.isspace():
continue
position = hexutil.Hex(x, y)
if ch == '@':
player = position
ch = '.'
elif ch == '%':
ch = '.'
elif ch == 'T':
target = position
elif ch == '*':
lights.append(position)
tiles[position] = ch
self.player = player
self.target = target
self.tiles = tiles
self.line_lengths = line_lengths
self.lights = lights
def is_transparent(self, pos):
return self.tiles.get(pos, '#') != '#'
def is_passable(self, pos):
return self.tiles.get(pos, '#') not in "#~"
def field_of_view(self, max_distance):
return self.player.field_of_view(transparent=self.is_transparent, max_distance=max_distance)
def get_map(self, max_distance=None, path=frozenset()):
if max_distance is not None:
fov = self.field_of_view(max_distance)
if self.lights:
light_fov = {}
for light in self.lights:
light.field_of_view(transparent=self.is_transparent, max_distance=max_distance, visible=light_fov)
else:
light_fov = fov
else:
fov = light_fov = None
lines = []
for y, line_length in enumerate(self.line_lengths):
line = []
for x in range(line_length):
if (x + y) % 2 != 0:
ch = ' '
else:
pos = hexutil.Hex(x, y)
if pos == self.player:
ch = '@'
elif fov is None or (fov.get(pos, 0) & light_fov.get(pos, 0)):
if pos in path:
ch = '%'
else:
ch = self.tiles.get(pos, ' ')
else:
ch = ' '
line.append(ch)
lines.append("".join(line).rstrip())
return "\n".join(lines)
testmap1 = HexMap("""
# # # # # # # # #
# # # # # # # # # #
# . # # # # # # #
# # . # # # # # # #
# # # . . . . # #
# # # . @ # # . # #
# # ~ % . # # . #
# # ~ # % # # # # #
# # T % # # # # #
# # # # # # # # # #
""")
testmap1_out = """
# # # #
# . . .
# . @ #
# ~ . . #
# ~ # . #
# . #
# #
"""
testmap2 = HexMap("""
# # # # # # # # #
# # # # # # # # # #
# . # # # * # # #
# # . # # . # # # #
# # # . % % % # #
# # . . @ # # % # #
# . # . # # # T #
# # # # . . # # # #
# # . . . . # # #
# # # # # # # # # #
""")
testmap2_out = """
# #
* #
.
. . .
. @
# . #
# .
"""
class TestHex(unittest.TestCase):
def test_is_valid(self):
hexutil.Hex(-1, -3)
self.assertRaises(hexutil.InvalidHex, hexutil.Hex, 2, -3)
def test_rotations(self):
hex = hexutil.Hex(2, 0)
self.assertEqual([r(hex) for r in hexutil.Hex.rotations], hexutil.origin.neighbours())
def test_add(self):
self.assertEqual(hexutil.Hex(2, 4) + hexutil.Hex(4, 6), hexutil.Hex(6, 10))
def test_sub(self):
self.assertEqual(hexutil.Hex(2, 4) - hexutil.Hex(3, 7), hexutil.Hex(-1, -3))
def test_neg(self):
self.assertEqual(-hexutil.Hex(2, 4), hexutil.Hex(-2, -4))
self.assertEqual(-hexutil.origin, hexutil.origin)
def test_neighbours(self):
origin = hexutil.origin
nb = origin.neighbours()
self.assertEqual(len(nb), 6)
for h in nb:
self.assertTrue((-h) in nb)
self.assertTrue(origin in h.neighbours())
def test_distance(self):
h = hexutil.Hex(-1, -3)
for nb in h.neighbours():
self.assertEqual(nb.distance(nb), 0)
self.assertEqual(nb.distance(h), 1)
self.assertEqual(max(nb2.distance(h) for nb2 in nb.neighbours()), 2)
def test_rotate_left(self):
origin = hexutil.origin
neighbours = origin.neighbours()
nb = neighbours[0]
neighbours2 = []
for i in range(6):
neighbours2.append(nb)
nb = nb.rotate_left()
self.assertEqual(neighbours, neighbours2)
def test_rotate_right(self):
origin = hexutil.origin
for nb in hexutil.origin.neighbours():
self.assertEqual(nb.rotate_left().rotate_right(), nb)
class TestHexGrid(unittest.TestCase):
def test_height(self):
self.assertEqual(hexutil.HexGrid(32).height, 18)
def test_corners(self):
self.assertEqual(hexutil.HexGrid(32).corners(hexutil.Hex(1, 1)),
[(64, 72), (32, 90), (0, 72), (0, 36), (32, 18), (64, 36)])
def test_hex_at_coordinates(self):
hg = hexutil.HexGrid(32)
data = [
((0, 0), hexutil.Hex(0, 0)),
((33, 16), hexutil.Hex(2, 0)),
((30, 20), hexutil.Hex(1, 1))
]
for fx in (-1, 1):
for fy in (-1, 1):
for pixel, hex in data:
x, y = pixel
pixel = (fx*x, fy*y)
x, y = hex
hex = hexutil.Hex(fx*x, fy*y)
self.assertEqual(hg.hex_at_coordinate(*pixel), hex, pixel)
def test_center(self):
hg = hexutil.HexGrid(32)
self.assertEqual(hg.center(hexutil.Hex(1, 1)), (32, 54))
def test_bounding_box(self):
hg = hexutil.HexGrid(32)
self.assertEqual(hg.bounding_box(hexutil.Hex(0,2)),
hexutil.Rectangle(-32, 72, 64, 72))
def test_hexes_in_rectangle(self):
hg = hexutil.HexGrid(32)
self.assertEqual(
list(hg.hexes_in_rectangle(hg.bounding_box(hexutil.origin))),
[hexutil.Hex(-1, -1), hexutil.Hex(1, -1),
hexutil.Hex(-2, 0), hexutil.Hex(0, 0),
hexutil.Hex(-1, 1), hexutil.Hex(1, 1)]
)
class TestFov(unittest.TestCase):
def test_fov1(self):
self.assertEqual(testmap1.get_map(10), testmap1_out)
def test_fov2(self):
self.assertEqual(testmap2.get_map(10), testmap2_out)
class TestPathFinding(unittest.TestCase):
def test_path1(self):
path = frozenset(testmap1.player.find_path(testmap1.target, testmap1.is_passable)[:-1])
self.assertEqual(testmap1.get_map(path=path), testmap1.source)
def test_path2(self):
path = frozenset(testmap2.player.find_path(testmap2.target, testmap2.is_passable)[:-1])
self.assertEqual(testmap2.get_map(path=path), testmap2.source)
if __name__ == '__main__':
unittest.main()