home · contact · privacy
2edbea84809acbfc759aed7993e603ffdb9c74e6
[plomrogue2-experiments] / new2 / plomrogue / mapping.py
1 import collections
2
3
4
5 class YX(collections.namedtuple('YX', ('y', 'x'))):
6
7     def __add__(self, other):
8         return YX(self.y + other.y, self.x + other.x)
9
10     def __sub__(self, other):
11         return YX(self.y - other.y, self.x - other.x)
12
13     def __str__(self):
14         return 'Y:%s,X:%s' % (self.y, self.x)
15
16
17
18 class MapGeometry():
19
20     def __init__(self, size):
21         self.size = size
22
23     def get_directions(self):
24         directions = []
25         for name in dir(self):
26             if name[:5] == 'move_':
27                 directions += [name[5:]]
28         return directions
29
30     def get_neighbors(self, pos):
31         neighbors = {}
32         for direction in self.get_directions():
33             neighbors[direction] = self.move(pos, direction)
34         return neighbors
35
36     def move(self, start_pos, direction):
37         mover = getattr(self, 'move_' + direction)
38         target = mover(start_pos)
39         if target.y < 0 or target.x < 0 or \
40                 target.y >= self.size.y or target.x >= self.size.x:
41             return None
42         return target
43
44
45
46 class MapGeometryWithLeftRightMoves(MapGeometry):
47
48     def move_LEFT(self, start_pos):
49         return YX(start_pos.y, start_pos.x - 1)
50
51     def move_RIGHT(self, start_pos):
52         return YX(start_pos.y, start_pos.x + 1)
53
54
55
56 class MapGeometrySquare(MapGeometryWithLeftRightMoves):
57
58     def move_UP(self, start_pos):
59         return YX(start_pos.y - 1, start_pos.x)
60
61     def move_DOWN(self, start_pos):
62         return YX(start_pos.y + 1, start_pos.x)
63
64
65
66 class Map():
67
68     def __init__(self, map_size):
69         self.size = map_size
70         self.terrain = '.' * self.size_i
71
72     def __getitem__(self, yx):
73         return self.terrain[self.get_position_index(yx)]
74
75     def __setitem__(self, yx, c):
76         pos_i = self.get_position_index(yx)
77         if type(c) == str:
78             self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
79         else:
80             self.terrain[pos_i] = c
81
82     @property
83     def size_i(self):
84         return self.size.y * self.size.x
85
86     def set_line(self, y, line):
87         height_map = self.size.y
88         width_map = self.size.x
89         if y >= height_map:
90             raise ArgError('too large row number %s' % y)
91         width_line = len(line)
92         if width_line > width_map:
93             raise ArgError('too large map line width %s' % width_line)
94         self.terrain = self.terrain[:y * width_map] + line +\
95                        self.terrain[(y + 1) * width_map:]
96
97     def get_position_index(self, yx):
98         return yx.y * self.size.x + yx.x
99
100     def lines(self):
101         width = self.size.x
102         for y in range(self.size.y):
103             yield (y, self.terrain[y * width:(y + 1) * width])