forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tdl_map.py
More file actions
104 lines (84 loc) · 3.35 KB
/
Copy pathtest_tdl_map.py
File metadata and controls
104 lines (84 loc) · 3.35 KB
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
#!/usr/bin/env python
import unittest
import itertools
import pytest
import tdl
class MapTests(unittest.TestCase):
MAP = (
'############',
'# ### #',
'# ### #',
'# ### ####',
'## #### # ##',
'## ####',
'############',
)
WIDTH = len(MAP[0])
HEIGHT = len(MAP)
POINT_A = (2, 2)
POINT_B = (9, 2)
POINT_C = (9, 4)
POINTS_AB = POINT_A + POINT_B
POINTS_AC = POINT_A + POINT_C
@classmethod
def map_is_transparant(cls, x, y):
try:
return cls.MAP[y][x] == ' '
except IndexError:
return False
@classmethod
def path_cost(cls, src_x, src_y, dest_x, dest_y):
if cls.map_is_transparant(dest_x, dest_y):
return 1
return 0
def setUp(self):
self.map = tdl.map.Map(self.WIDTH, self.HEIGHT)
for y, line in enumerate(self.MAP):
for x, ch in enumerate(line):
trans = ch == ' '
self.map.transparent[x,y] = self.map.walkable[x,y] = trans
assert self.map.transparent[x,y] == trans
assert self.map.walkable[x,y] == trans
def test_map_compute_fov(self):
fov = self.map.compute_fov(*self.POINT_A)
assert list(fov), 'should be non-empty'
fov = self.map.compute_fov(*self.POINT_A, fov='PERMISSIVE8')
assert list(fov), 'should be non-empty'
with self.assertRaises(tdl.TDLError):
self.map.compute_fov(*self.POINT_A, fov='invalid option')
def test_map_compute_path(self):
self.assertTrue(self.map.compute_path(*self.POINTS_AB),
'should be non-empty')
self.assertFalse(self.map.compute_path(*self.POINTS_AC),
'invalid path should return an empty list')
def test_map_specials(self):
for x,y in self.map:
self.assertTrue((x, y) in self.map)
self.assertFalse((-1, -1) in self.map)
@pytest.mark.filterwarnings("ignore:This function is very slow.")
def test_quick_fov(self):
fov = tdl.map.quick_fov(self.POINT_B[0], self.POINT_B[1],
self.map_is_transparant, radius=2.5)
self.assertTrue(fov, 'should be non-empty')
def test_bresenham(self):
for x1, x2, y1, y2 in itertools.permutations([-4, -2, 4, 4], 4):
self.assertTrue(tdl.map.bresenham(x1, x2, y1, y2),
'should be non-empty')
def test_astar(self):
pathfinder = tdl.map.AStar(self.WIDTH, self.HEIGHT,
self.map_is_transparant)
self.assertTrue(pathfinder.get_path(*self.POINTS_AB))
pathfinder = tdl.map.AStar(self.WIDTH, self.HEIGHT,
self.path_cost, None, True)
self.assertTrue(pathfinder.get_path(*self.POINTS_AB))
self.assertFalse(pathfinder.get_path(*self.POINTS_AC),
'invalid path should return an empty list')
def test_map_fov_cumulative():
map_ = tdl.map.Map(3, 3)
map_.transparent[::2,:] = True # Add map with small divider.
assert not map_.fov.any()
map_.compute_fov(1, 0, cumulative=True) # Light left side.
assert map_.fov.any()
assert not map_.fov.all()
map_.compute_fov(1, 2, cumulative=True) # Light both sides.
assert map_.fov.all()