1 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=YX(0, 0), init_char = '?'):
23 self.terrain = init_char*self.size_i
25 def __getitem__(self, yx):
26 return self.terrain[self.get_position_index(yx)]
28 def __setitem__(self, yx, c):
29 pos_i = self.get_position_index(yx)
31 self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
33 self.terrain[pos_i] = c
36 """Iterate over YX position coordinates."""
37 for y in range(self.size.y):
38 for x in range(self.size.x):
43 return self.size.y * self.size.x
45 def set_line(self, y, line):
46 height_map = self.size.y
47 width_map = self.size.x
49 raise ArgError('too large row number %s' % y)
50 width_line = len(line)
51 if width_line > width_map:
52 raise ArgError('too large map line width %s' % width_line)
53 self.terrain = self.terrain[:y * width_map] + line +\
54 self.terrain[(y + 1) * width_map:]
56 def get_position_index(self, yx):
57 return yx.y * self.size.x + yx.x
61 for y in range(self.size.y):
62 yield (y, self.terrain[y * width:(y + 1) * width])
68 def get_directions(self):
70 for name in dir(self):
71 if name[:5] == 'move_':
72 directions += [name[5:]]
75 def get_neighbors(self, pos, map_size):
77 if not hasattr(self, 'neighbors_to'):
78 self.neighbors_to = {}
79 if not map_size in self.neighbors_to:
80 self.neighbors_to[map_size] = {}
81 if pos in self.neighbors_to[map_size]:
82 return self.neighbors_to[map_size][pos]
83 for direction in self.get_directions():
84 neighbors[direction] = self.move(pos, direction, map_size)
85 self.neighbors_to[map_size][pos] = neighbors
88 def pos_in_projection(self, pos, offset, maps_size):
89 pos_y = pos[1].y + (maps_size.y * pos[0].y) - offset.y
90 pos_x = pos[1].x + (maps_size.x * pos[0].x) - offset.x
91 return YX(pos_y, pos_x)
93 def correct_double_coordinate(self, map_size, big_yx, little_yx):
96 maps_crossed = little_yx[axis] // map_size[axis]
97 new_big = big_yx[axis] + maps_crossed
98 new_little = little_yx[axis] % map_size[axis]
99 return new_big, new_little
101 new_big_y, new_little_y = adapt_axis(0)
102 new_big_x, new_little_x = adapt_axis(1)
103 return YX(new_big_y, new_big_x), YX(new_little_y, new_little_x)
105 def move(self, start_pos, direction, map_size):
106 mover = getattr(self, 'move_' + direction)
107 big_yx, little_yx = start_pos
108 uncorrected_target = mover(little_yx)
109 return self.correct_double_coordinate(map_size, big_yx,
114 class MapGeometryWithLeftRightMoves(MapGeometry):
116 def move_LEFT(self, start_pos):
117 return YX(start_pos.y, start_pos.x - 1)
119 def move_RIGHT(self, start_pos):
120 return YX(start_pos.y, start_pos.x + 1)
124 class MapGeometrySquare(MapGeometryWithLeftRightMoves):
126 def move_UP(self, start_pos):
127 return YX(start_pos.y - 1, start_pos.x)
129 def move_DOWN(self, start_pos):
130 return YX(start_pos.y + 1, start_pos.x)
134 class MapGeometryHex(MapGeometryWithLeftRightMoves):
136 def __init__(self, *args, **kwargs):
137 super().__init__(*args, **kwargs)
138 self.fov_map_type = FovMapHex
140 def move_UPLEFT(self, start_pos):
141 if start_pos.y % 2 == 1:
142 return YX(start_pos.y - 1, start_pos.x - 1)
144 return YX(start_pos.y - 1, start_pos.x)
146 def move_UPRIGHT(self, start_pos):
147 if start_pos.y % 2 == 1:
148 return YX(start_pos.y - 1, start_pos.x)
150 return YX(start_pos.y - 1, start_pos.x + 1)
152 def move_DOWNLEFT(self, start_pos):
153 if start_pos.y % 2 == 1:
154 return YX(start_pos.y + 1, start_pos.x - 1)
156 return YX(start_pos.y + 1, start_pos.x)
158 def move_DOWNRIGHT(self, start_pos):
159 if start_pos.y % 2 == 1:
160 return YX(start_pos.y + 1, start_pos.x)
162 return YX(start_pos.y + 1, start_pos.x + 1)
168 def __init__(self, source_map, center):
169 self.source_map = source_map
170 self.size = self.source_map.size
171 self.fov_radius = (self.size.y / 2) - 0.5
172 self.terrain = '?' * self.size_i
174 self.shadow_cones = []
175 self.circle_out(center, self.shadow_process_hex)
177 def shadow_process_hex(self, yx, distance_to_center, dir_i, dir_progress):
178 # Possible optimization: If no shadow_cones yet and self[yx] == '.',
180 CIRCLE = 360 # Since we'll float anyways, number is actually arbitrary.
182 def correct_arm(arm):
187 def in_shadow_cone(new_cone):
188 for old_cone in self.shadow_cones:
189 if old_cone[0] >= new_cone[0] and \
190 new_cone[1] >= old_cone[1]:
191 #print('DEBUG shadowed by:', old_cone)
193 # We might want to also shade hexes whose middle arm is inside a
194 # shadow cone for a darker FOV. Note that we then could not for
195 # optimization purposes rely anymore on the assumption that a
196 # shaded hex cannot add growth to existing shadow cones.
199 def merge_cone(new_cone):
201 for old_cone in self.shadow_cones:
202 if new_cone[0] > old_cone[0] and \
203 (new_cone[1] < old_cone[0] or
204 math.isclose(new_cone[1], old_cone[0])):
205 #print('DEBUG merging to', old_cone)
206 old_cone[0] = new_cone[0]
207 #print('DEBUG merged cone:', old_cone)
209 if new_cone[1] < old_cone[1] and \
210 (new_cone[0] > old_cone[1] or
211 math.isclose(new_cone[0], old_cone[1])):
212 #print('DEBUG merging to', old_cone)
213 old_cone[1] = new_cone[1]
214 #print('DEBUG merged cone:', old_cone)
219 #print('DEBUG CONE', cone, '(', step_size, distance_to_center, number_steps, ')')
220 if in_shadow_cone(cone):
223 if self.source_map[yx] != '.':
224 #print('DEBUG throws shadow', cone)
226 while merge_cone(cone):
229 self.shadow_cones += [cone]
232 step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
233 number_steps = dir_i * distance_to_center + dir_progress
234 left_arm = correct_arm(-(step_size/2) - step_size*number_steps)
235 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, 0])
240 eval_cone([CIRCLE, 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)
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 hexes are shaded by what hex
257 # and skip evaluation of already shaded hexes. (This only works if hex
258 # shading implies they completely lie in existing shades; otherwise we
259 # would lose shade growth through hexes at shade borders.)
261 # TODO: Start circling only in earliest obstacle distance.
262 # TODO: get rid of circle_in_map logic
266 #print('DEBUG CIRCLE_OUT', yx)
268 if distance > self.fov_radius:
270 circle_in_map = False
271 yx, _ = self.basic_circle_out_move(yx, 'RIGHT')
272 for dir_i in range(len(self.circle_out_directions)):
273 for dir_progress in range(distance):
274 direction = self.circle_out_directions[dir_i]
275 yx, test = self.circle_out_move(yx, direction)
277 f(yx, distance, dir_i, dir_progress)
283 class FovMapHex(FovMap):
284 circle_out_directions = ('DOWNLEFT', 'LEFT', 'UPLEFT',
285 'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
287 def __init__(self, *args, **kwargs):
288 self.geometry = MapGeometryHex()
289 super().__init__(*args, **kwargs)
291 def circle_out_move(self, yx, direction):
292 return self.basic_circle_out_move(yx, direction)
296 class FovMapSquare(FovMap):
297 circle_out_directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
298 ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
300 def __init__(self, *args, **kwargs):
301 self.geometry = MapGeometrySquare()
302 super().__init__(*args, **kwargs)
304 def circle_out_move(self, yx, direction):
305 self.basic_circle_out_move(yx, direction[0])
306 return self.basic_circle_out_move(yx, direction[1])