8 class Map(game_common.Map):
10 def __getitem__(self, yx):
11 return self.terrain[self.get_position_index(yx)]
13 def __setitem__(self, yx, c):
14 pos_i = self.get_position_index(yx)
16 self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
18 self.terrain[pos_i] = c
21 """Iterate over YX position coordinates."""
22 for y in range(self.size[0]):
23 for x in range(self.size[1]):
28 return self.__class__.__name__[3:]
32 for y in range(self.size[0]):
33 yield (y, self.terrain[y * width:(y + 1) * width])
35 def get_fov_map(self, yx):
36 fov_class_name = 'Fov' + self.__class__.__name__
37 fov_class = globals()[fov_class_name]
38 return fov_class(self, yx)
40 # The following is used nowhere, so not implemented.
42 # for y in range(self.size[0]):
43 # for x in range(self.size[1]):
44 # yield ([y, x], self.terrain[self.get_position_index([y, x])])
46 def get_directions(self):
48 for name in dir(self):
49 if name[:5] == 'move_':
50 directions += [name[5:]]
53 def get_neighbors(self, pos):
55 if not hasattr(self, 'neighbors_to'):
56 self.neighbors_to = {}
57 if pos in self.neighbors_to:
58 return self.neighbors_to[pos]
59 for direction in self.get_directions():
60 neighbors[direction] = None
62 neighbors[direction] = self.move(pos, direction)
63 except server_.game.GameError:
65 self.neighbors_to[pos] = neighbors
68 def new_from_shape(self, init_char):
70 new_map = copy.deepcopy(self)
72 new_map[pos] = init_char
75 def move(self, start_pos, direction):
76 mover = getattr(self, 'move_' + direction)
77 new_pos = mover(start_pos)
78 if new_pos[0] < 0 or new_pos[1] < 0 or \
79 new_pos[0] >= self.size[0] or new_pos[1] >= self.size[1]:
80 raise server_.game.GameError('would move outside map bounds')
83 def move_LEFT(self, start_pos):
84 return [start_pos[0], start_pos[1] - 1]
86 def move_RIGHT(self, start_pos):
87 return [start_pos[0], start_pos[1] + 1]
92 # The following is used nowhere, so not implemented.
93 #def are_neighbors(self, pos_1, pos_2):
94 # if pos_1[0] == pos_2[0] and abs(pos_1[1] - pos_2[1]) <= 1:
96 # elif abs(pos_1[0] - pos_2[0]) == 1:
97 # if pos_1[0] % 2 == 0:
98 # if pos_2[1] in (pos_1[1], pos_1[1] - 1):
100 # elif pos_2[1] in (pos_1[1], pos_1[1] + 1):
104 def move_UPLEFT(self, start_pos):
105 if start_pos[0] % 2 == 1:
106 return [start_pos[0] - 1, start_pos[1] - 1]
108 return [start_pos[0] - 1, start_pos[1]]
110 def move_UPRIGHT(self, start_pos):
111 if start_pos[0] % 2 == 1:
112 return [start_pos[0] - 1, start_pos[1]]
114 return [start_pos[0] - 1, start_pos[1] + 1]
116 def move_DOWNLEFT(self, start_pos):
117 if start_pos[0] % 2 == 1:
118 return [start_pos[0] + 1, start_pos[1] - 1]
120 return [start_pos[0] + 1, start_pos[1]]
122 def move_DOWNRIGHT(self, start_pos):
123 if start_pos[0] % 2 == 1:
124 return [start_pos[0] + 1, start_pos[1]]
126 return [start_pos[0] + 1, start_pos[1] + 1]
129 class MapSquare(Map):
131 # The following is used nowhere, so not implemented.
132 #def are_neighbors(self, pos_1, pos_2):
133 # return abs(pos_1[0] - pos_2[0]) <= 1 and abs(pos_1[1] - pos_2[1] <= 1)
135 def move_UP(self, start_pos):
136 return [start_pos[0] - 1, start_pos[1]]
138 def move_DOWN(self, start_pos):
139 return [start_pos[0] + 1, start_pos[1]]
144 def __init__(self, source_map, yx):
145 self.source_map = source_map
146 self.size = self.source_map.size
147 self.terrain = '?' * self.size_i
149 self.shadow_cones = []
150 self.circle_out(yx, self.shadow_process_hex)
152 def shadow_process_hex(self, yx, distance_to_center, dir_i, dir_progress):
153 # Possible optimization: If no shadow_cones yet and self[yx] == '.',
155 CIRCLE = 360 # Since we'll float anyways, number is actually arbitrary.
157 def correct_arm(arm):
162 def in_shadow_cone(new_cone):
163 for old_cone in self.shadow_cones:
164 if old_cone[0] >= new_cone[0] and \
165 new_cone[1] >= old_cone[1]:
166 #print('DEBUG shadowed by:', old_cone)
168 # We might want to also shade hexes whose middle arm is inside a
169 # shadow cone for a darker FOV. Note that we then could not for
170 # optimization purposes rely anymore on the assumption that a
171 # shaded hex cannot add growth to existing shadow cones.
174 def merge_cone(new_cone):
175 for old_cone in self.shadow_cones:
176 if new_cone[0] > old_cone[0] and \
177 (new_cone[1] < old_cone[0] or
178 math.isclose(new_cone[1], old_cone[0])):
179 #print('DEBUG merging to', old_cone)
180 old_cone[0] = new_cone[0]
181 #print('DEBUG merged cone:', old_cone)
183 if new_cone[1] < old_cone[1] and \
184 (new_cone[0] > old_cone[1] or
185 math.isclose(new_cone[0], old_cone[1])):
186 #print('DEBUG merging to', old_cone)
187 old_cone[1] = new_cone[1]
188 #print('DEBUG merged cone:', old_cone)
193 #print('DEBUG CONE', cone, '(', step_size, distance_to_center, number_steps, ')')
194 if in_shadow_cone(cone):
197 if self.source_map[yx] != '.':
198 #print('DEBUG throws shadow', cone)
200 while merge_cone(cone):
203 self.shadow_cones += [cone]
206 step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
207 number_steps = dir_i * distance_to_center + dir_progress
208 left_arm = correct_arm(-(step_size/2) - step_size*number_steps)
209 right_arm = correct_arm(left_arm - step_size)
210 # Optimization potential: left cone could be derived from previous
211 # right cone. Better even: Precalculate all cones.
212 if right_arm > left_arm:
213 eval_cone([left_arm, 0])
214 eval_cone([CIRCLE, right_arm])
216 eval_cone([left_arm, right_arm])
218 def basic_circle_out_move(self, pos, direction):
219 """Move position pos into direction. Return whether still in map."""
220 mover = getattr(self, 'move_' + direction)
222 if pos[0] < 0 or pos[1] < 0 or \
223 pos[0] >= self.size[0] or pos[1] >= self.size[1]:
227 def circle_out(self, yx, f):
228 # Optimization potential: Precalculate movement positions. (How to check
229 # circle_in_map then?)
230 # Optimization potential: Precalculate what hexes are shaded by what hex
231 # and skip evaluation of already shaded hexes. (This only works if hex
232 # shading implies they completely lie in existing shades; otherwise we
233 # would lose shade growth through hexes at shade borders.)
235 # TODO: Start circling only in earliest obstacle distance.
239 #print('DEBUG CIRCLE_OUT', yx)
241 circle_in_map = False
242 self.basic_circle_out_move(yx, 'RIGHT')
243 for dir_i in range(len(self.circle_out_directions)):
244 for dir_progress in range(distance):
245 direction = self.circle_out_directions[dir_i]
246 if self.circle_out_move(yx, direction):
247 f(yx, distance, dir_i, dir_progress)
252 class FovMapHex(FovMap, MapHex):
253 circle_out_directions = ('DOWNLEFT', 'LEFT', 'UPLEFT',
254 'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
256 def circle_out_move(self, yx, direction):
257 return self.basic_circle_out_move(yx, direction)
260 class FovMapSquare(FovMap, MapSquare):
261 circle_out_directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
262 ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
264 def circle_out_move(self, yx, direction):
265 self.basic_circle_out_move(yx, direction[0])
266 return self.basic_circle_out_move(yx, direction[1])
269 map_manager = game_common.MapManager((MapHex, MapSquare))