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):
144 # TODO: use this for more refactoring
145 def inside(self, yx):
146 if yx.y < 0 or yx.x < 0 or yx.y >= self.size.y or yx.x >= self.size.x:
152 return self.size.y * self.size.x
154 def set_line(self, y, line):
155 height_map = self.size.y
156 width_map = self.size.x
158 raise ArgError('too large row number %s' % y)
159 width_line = len(line)
160 if width_line != width_map:
161 raise ArgError('map line width %s unequal map width %s' % (width_line, width_map))
162 self.terrain = self.terrain[:y * width_map] + line +\
163 self.terrain[(y + 1) * width_map:]
165 def get_position_index(self, yx):
166 return yx.y * self.size.x + yx.x
170 for y in range(self.size.y):
171 yield (y, self.terrain[y * width:(y + 1) * width])
176 # TODO: player visibility asymmetrical (A can see B when B can't see A):
177 # does this make sense, or not?
179 def __init__(self, source_map, source_center):
180 self.source_map = source_map
182 self.set_size_offset_center(source_center)
183 self.terrain = '?' * self.size.y * self.size.x
184 self[self.center] = '.'
185 self.geometry = self.geometry_class(self.size)
186 self[self.center] = '.'
187 self.shadow_cones = []
188 self.circle_out(self.center, self.shadow_process)
190 def throws_shadow(self, source_yx):
191 return self.source_map[source_yx] == 'X'
193 def source_yx(self, yx):
194 source_yx = yx + self.offset
195 if not self.source_map.inside(source_yx):
199 def shadow_process(self, yx, source_yx, distance_to_center, dir_i, dir_progress):
200 # Possible optimization: If no shadow_cones yet and self[yx] == '.',
202 CIRCLE = 360 # Since we'll float anyways, number is actually arbitrary.
204 def correct_arm(arm):
209 def in_shadow_cone(new_cone):
210 for old_cone in self.shadow_cones:
211 if old_cone[0] <= new_cone[0] and \
212 new_cone[1] <= old_cone[1]:
214 # We might want to also shade tiles whose middle arm is inside a
215 # shadow cone for a darker FOV. Note that we then could not for
216 # optimization purposes rely anymore on the assumption that a
217 # shaded tile cannot add growth to existing shadow cones.
220 def merge_cone(new_cone):
222 for old_cone in self.shadow_cones:
223 if new_cone[0] < old_cone[0] and \
224 (new_cone[1] > old_cone[0] or
225 math.isclose(new_cone[1], old_cone[0])):
226 old_cone[0] = new_cone[0]
228 if new_cone[1] > old_cone[1] and \
229 (new_cone[0] < old_cone[1] or
230 math.isclose(new_cone[0], old_cone[1])):
231 old_cone[1] = new_cone[1]
236 if in_shadow_cone(cone):
239 if self.throws_shadow(source_yx):
241 while merge_cone(cone):
244 self.shadow_cones += [cone]
246 step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
247 number_steps = dir_i * distance_to_center + dir_progress
248 left_arm = correct_arm(step_size/2 + step_size*number_steps)
249 right_arm = correct_arm(left_arm + step_size)
251 # Optimization potential: left cone could be derived from previous
252 # right cone. Better even: Precalculate all cones.
253 if right_arm < left_arm:
254 eval_cone([left_arm, CIRCLE])
255 eval_cone([0, right_arm])
257 eval_cone([left_arm, right_arm])
259 def basic_circle_out_move(self, pos, direction):
260 #"""Move position pos into direction. Return whether still in map."""
261 mover = getattr(self.geometry, 'move_' + direction)
264 def circle_out(self, yx, f):
265 # Optimization potential: Precalculate movement positions. (How to check
266 # circle_in_map then?)
267 # Optimization potential: Precalculate what tiles are shaded by what tile
268 # and skip evaluation of already shaded tile. (This only works if tiles
269 # shading implies they completely lie in existing shades; otherwise we
270 # would lose shade growth through tiles at shade borders.)
274 while distance <= self.fov_radius:
275 yx = self.basic_circle_out_move(yx, 'RIGHT')
276 for dir_i in range(len(self.circle_out_directions)):
277 for dir_progress in range(distance):
278 direction = self.circle_out_directions[dir_i]
279 yx = self.circle_out_move(yx, direction)
280 source_yx = self.source_yx(yx)
282 f(yx, source_yx, distance, dir_i, dir_progress)
287 class FovMapHex(FovMap):
288 circle_out_directions = ('DOWNLEFT', 'LEFT', 'UPLEFT',
289 'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
290 geometry_class = MapGeometryHex
292 def set_size_offset_center(self, source_center):
293 indent = 1 if (source_center.y % 2) else 0
294 self.size = YX(2 * self.fov_radius + 1 + indent,
295 2 * self.fov_radius + 1)
296 self.offset = YX(source_center.y - self.fov_radius - indent,
297 source_center.x - self.fov_radius)
298 self.center = YX(self.fov_radius + indent, self.fov_radius)
300 def circle_out_move(self, yx, direction):
301 return self.basic_circle_out_move(yx, direction)
305 class FovMapSquare(FovMap):
306 circle_out_directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
307 ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
308 geometry_class = MapGeometrySquare
310 def set_size_offset_center(self, source_center):
311 self.size = YX(2 * self.fov_radius + 1, 2 * self.fov_radius + 1)
312 self.offset = YX(source_center.y - self.fov_radius,
313 source_center.x - self.fov_radius)
314 self.center = YX(self.fov_radius, self.fov_radius)
316 def circle_out_move(self, yx, direction):
317 yx = self.basic_circle_out_move(yx, direction[0])
318 return self.basic_circle_out_move(yx, direction[1])