2 from plomrogue.errors import ArgError
6 class YX(collections.namedtuple('YX', ('y', 'x'))):
8 def __add__(self, other):
9 return YX(self.y + other.y, self.x + other.x)
11 def __sub__(self, other):
12 return YX(self.y - other.y, self.x - other.x)
15 return 'Y:%s,X:%s' % (self.y, self.x)
21 def __init__(self, size):
25 def get_directions(self):
27 for name in dir(self):
28 if name[:5] == 'move_':
29 directions += [name[5:]]
32 def get_neighbors(self, pos):
34 for direction in self.get_directions():
35 neighbors[direction] = self.move(pos, direction)
38 def get_neighbors_i(self, i):
39 if i in self.neighbors_i:
40 return self.neighbors_i[i]
41 pos = YX(i // self.size.x, i % self.size.x)
42 neighbors_pos = self.get_neighbors(pos)
44 for direction in neighbors_pos:
45 pos = neighbors_pos[direction]
47 neighbors_i[direction] = None
49 neighbors_i[direction] = pos.y * self.size.x + pos.x
50 self.neighbors_i[i] = neighbors_i
51 return self.neighbors_i[i]
53 def move(self, start_pos, direction):
54 mover = getattr(self, 'move_' + direction)
55 target = mover(start_pos)
56 if target.y < 0 or target.x < 0 or \
57 target.y >= self.size.y or target.x >= self.size.x:
63 class MapGeometryWithLeftRightMoves(MapGeometry):
65 def move_LEFT(self, start_pos):
66 return YX(start_pos.y, start_pos.x - 1)
68 def move_RIGHT(self, start_pos):
69 return YX(start_pos.y, start_pos.x + 1)
73 class MapGeometrySquare(MapGeometryWithLeftRightMoves):
75 def __init__(self, *args, **kwargs):
76 super().__init__(*args, **kwargs)
77 self.fov_map_class = FovMapSquare
79 def move_UP(self, start_pos):
80 return YX(start_pos.y - 1, start_pos.x)
82 def move_DOWN(self, start_pos):
83 return YX(start_pos.y + 1, start_pos.x)
86 class MapGeometryHex(MapGeometryWithLeftRightMoves):
88 def __init__(self, *args, **kwargs):
89 super().__init__(*args, **kwargs)
90 self.fov_map_class = FovMapHex
92 def move_UPLEFT(self, start_pos):
93 start_indented = start_pos.y % 2
95 return YX(start_pos.y - 1, start_pos.x)
97 return YX(start_pos.y - 1, start_pos.x - 1)
99 def move_UPRIGHT(self, start_pos):
100 start_indented = start_pos.y % 2
102 return YX(start_pos.y - 1, start_pos.x + 1)
104 return YX(start_pos.y - 1, start_pos.x)
106 def move_DOWNLEFT(self, start_pos):
107 start_indented = start_pos.y % 2
109 return YX(start_pos.y + 1, start_pos.x)
111 return YX(start_pos.y + 1, start_pos.x - 1)
113 def move_DOWNRIGHT(self, start_pos):
114 start_indented = start_pos.y % 2
116 return YX(start_pos.y + 1, start_pos.x + 1)
118 return YX(start_pos.y + 1, start_pos.x)
124 def __init__(self, map_size):
126 self.terrain = '.' * self.size_i
128 def __getitem__(self, yx):
129 return self.terrain[self.get_position_index(yx)]
131 def __setitem__(self, yx, c):
132 pos_i = self.get_position_index(yx)
134 self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
136 self.terrain[pos_i] = c
139 """Iterate over YX position coordinates."""
140 for y in range(self.size.y):
141 for x in range(self.size.x):
146 return self.size.y * self.size.x
148 def set_line(self, y, line):
149 height_map = self.size.y
150 width_map = self.size.x
152 raise ArgError('too large row number %s' % y)
153 width_line = len(line)
154 if width_line != width_map:
155 raise ArgError('map line width %s unequal map width %s' % (width_line, width_map))
156 self.terrain = self.terrain[:y * width_map] + line +\
157 self.terrain[(y + 1) * width_map:]
159 def get_position_index(self, yx):
160 return yx.y * self.size.x + yx.x
164 for y in range(self.size.y):
165 yield (y, self.terrain[y * width:(y + 1) * width])
170 # FIXME: player visibility asymmetrical (A can see B when B can't see A)
172 def __init__(self, source_map, center):
173 self.source_map = source_map
174 self.size = self.source_map.size
175 self.fov_radius = 12 # (self.size.y / 2) - 0.5
176 self.start_indented = True #source_map.start_indented
177 self.terrain = '?' * self.size_i
179 self[self.center] = '.'
180 self.shadow_cones = []
181 self.geometry = self.geometry_class(self.size)
182 self.circle_out(self.center, self.shadow_process)
184 def shadow_process(self, yx, distance_to_center, dir_i, dir_progress):
185 # Possible optimization: If no shadow_cones yet and self[yx] == '.',
187 CIRCLE = 360 # Since we'll float anyways, number is actually arbitrary.
189 def correct_arm(arm):
194 def in_shadow_cone(new_cone):
195 for old_cone in self.shadow_cones:
196 if old_cone[0] <= new_cone[0] and \
197 new_cone[1] <= old_cone[1]:
199 # We might want to also shade tiles whose middle arm is inside a
200 # shadow cone for a darker FOV. Note that we then could not for
201 # optimization purposes rely anymore on the assumption that a
202 # shaded tile cannot add growth to existing shadow cones.
205 def merge_cone(new_cone):
207 for old_cone in self.shadow_cones:
208 if new_cone[0] < old_cone[0] and \
209 (new_cone[1] > old_cone[0] or
210 math.isclose(new_cone[1], old_cone[0])):
211 old_cone[0] = new_cone[0]
213 if new_cone[1] > old_cone[1] and \
214 (new_cone[0] < old_cone[1] or
215 math.isclose(new_cone[0], old_cone[1])):
216 old_cone[1] = new_cone[1]
221 if in_shadow_cone(cone):
224 if self.source_map[yx] == 'X':
226 while merge_cone(cone):
229 self.shadow_cones += [cone]
231 step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
232 number_steps = dir_i * distance_to_center + dir_progress
233 left_arm = correct_arm(step_size/2 + step_size*number_steps)
234 right_arm = correct_arm(left_arm + step_size)
236 # Optimization potential: left cone could be derived from previous
237 # right cone. Better even: Precalculate all cones.
238 if right_arm < left_arm:
239 eval_cone([left_arm, CIRCLE])
240 eval_cone([0, right_arm])
242 eval_cone([left_arm, right_arm])
244 def basic_circle_out_move(self, pos, direction):
245 """Move position pos into direction. Return whether still in map."""
246 mover = getattr(self.geometry, 'move_' + direction)
247 pos = mover(pos) #, self.start_indented)
248 if pos.y < 0 or pos.x < 0 or \
249 pos.y >= self.size.y or pos.x >= self.size.x:
253 def circle_out(self, yx, f):
254 # Optimization potential: Precalculate movement positions. (How to check
255 # circle_in_map then?)
256 # Optimization potential: Precalculate what tiles are shaded by what tile
257 # and skip evaluation of already shaded tile. (This only works if tiles
258 # shading implies they completely lie in existing shades; otherwise we
259 # would lose shade growth through tiles at shade borders.)
264 if distance > self.fov_radius:
266 circle_in_map = False
267 yx, _ = self.basic_circle_out_move(yx, 'RIGHT')
268 for dir_i in range(len(self.circle_out_directions)):
269 for dir_progress in range(distance):
270 direction = self.circle_out_directions[dir_i]
271 yx, test = self.circle_out_move(yx, direction)
273 f(yx, distance, dir_i, dir_progress)
279 class FovMapHex(FovMap):
280 circle_out_directions = ('DOWNLEFT', 'LEFT', 'UPLEFT',
281 'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
282 geometry_class = MapGeometryHex
284 def circle_out_move(self, yx, direction):
285 return self.basic_circle_out_move(yx, direction)
289 class FovMapSquare(FovMap):
290 circle_out_directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
291 ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
292 geometry_class = MapGeometrySquare
294 def circle_out_move(self, yx, direction):
295 yx, _ = self.basic_circle_out_move(yx, direction[0])
296 return self.basic_circle_out_move(yx, direction[1])