7 class Map(game_common.Map):
9 def __getitem__(self, yx):
10 return self.terrain[self.get_position_index(yx)]
12 def __setitem__(self, yx, c):
13 pos_i = self.get_position_index(yx)
14 self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
17 """Iterate over YX position coordinates."""
18 for y in range(self.size[0]):
19 for x in range(self.size[1]):
24 for y in range(self.size[0]):
25 yield (y, self.terrain[y * width:(y + 1) * width])
27 # The following is used nowhere, so not implemented.
29 # for y in range(self.size[0]):
30 # for x in range(self.size[1]):
31 # yield ([y, x], self.terrain[self.get_position_index([y, x])])
33 def get_directions(self):
35 for name in dir(self):
36 if name[:5] == 'move_':
37 directions += [name[5:]]
40 def new_from_shape(self, init_char):
42 new_map = copy.deepcopy(self)
44 new_map[pos] = init_char
47 def move(self, start_pos, direction):
48 mover = getattr(self, 'move_' + direction)
49 new_pos = mover(start_pos)
50 if new_pos[0] < 0 or new_pos[1] < 0 or \
51 new_pos[0] >= self.size[0] or new_pos[1] >= self.size[1]:
52 raise server_.game.GameError('would move outside map bounds')
55 def move_LEFT(self, start_pos):
56 return [start_pos[0], start_pos[1] - 1]
58 def move_RIGHT(self, start_pos):
59 return [start_pos[0], start_pos[1] + 1]
64 def are_neighbors(self, pos_1, pos_2):
65 if pos_1[0] == pos_2[0] and abs(pos_1[1] - pos_2[1]) <= 1:
67 elif abs(pos_1[0] - pos_2[0]) == 1:
69 if pos_2[1] in (pos_1[1], pos_1[1] - 1):
71 elif pos_2[1] in (pos_1[1], pos_1[1] + 1):
75 def move_UPLEFT(self, start_pos):
76 if start_pos[0] % 2 == 0:
77 return [start_pos[0] - 1, start_pos[1] - 1]
79 return [start_pos[0] - 1, start_pos[1]]
81 def move_UPRIGHT(self, start_pos):
82 if start_pos[0] % 2 == 0:
83 return [start_pos[0] - 1, start_pos[1]]
85 return [start_pos[0] - 1, start_pos[1] + 1]
87 def move_DOWNLEFT(self, start_pos):
88 if start_pos[0] % 2 == 0:
89 return [start_pos[0] + 1, start_pos[1] - 1]
91 return [start_pos[0] + 1, start_pos[1]]
93 def move_DOWNRIGHT(self, start_pos):
94 if start_pos[0] % 2 == 0:
95 return [start_pos[0] + 1, start_pos[1]]
97 return [start_pos[0] + 1, start_pos[1] + 1]
100 class MapSquare(Map):
102 def are_neighbors(self, pos_1, pos_2):
103 return abs(pos_1[0] - pos_2[0]) <= 1 and abs(pos_1[1] - pos_2[1] <= 1)
105 def move_UP(self, start_pos):
106 return [start_pos[0] - 1, start_pos[1]]
108 def move_DOWN(self, start_pos):
109 return [start_pos[0] + 1, start_pos[1]]
112 def get_map_class(geometry):
113 return globals()['Map' + geometry]
116 map_manager = game_common.MapManager(globals())