home · contact · privacy
Add very basic pathfinding AI.
[plomrogue2-experiments] / server_ / map_.py
1 import sys
2 sys.path.append('../')
3 import game_common
4 import server_.game
5 import math
6
7
8 class Map(game_common.Map):
9
10     def __getitem__(self, yx):
11         return self.terrain[self.get_position_index(yx)]
12
13     def __setitem__(self, yx, c):
14         pos_i = self.get_position_index(yx)
15         if type(c) == str:
16             self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
17         else:
18             self.terrain[pos_i] = c
19
20     def __iter__(self):
21         """Iterate over YX position coordinates."""
22         for y in range(self.size[0]):
23             for x in range(self.size[1]):
24                 yield [y, x]
25
26     @property
27     def geometry(self):
28         return self.__class__.__name__[3:]
29
30     def lines(self):
31         width = self.size[1]
32         for y in range(self.size[0]):
33             yield (y, self.terrain[y * width:(y + 1) * width])
34
35     def get_fov_map(self, yx):
36         # TODO: Currently only have MapFovHex. Provide MapFovSquare.
37         fov_map_class = map_manager.get_map_class('Fov' + self.geometry)
38         return fov_map_class(self, yx)
39
40     # The following is used nowhere, so not implemented.
41     #def items(self):
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])])
45
46     def get_directions(self):
47         directions = []
48         for name in dir(self):
49             if name[:5] == 'move_':
50                 directions += [name[5:]]
51         return directions
52
53     def new_from_shape(self, init_char):
54         import copy
55         new_map = copy.deepcopy(self)
56         for pos in new_map:
57             new_map[pos] = init_char
58         return new_map
59
60     def move(self, start_pos, direction):
61         mover = getattr(self, 'move_' + direction)
62         new_pos = mover(start_pos)
63         if new_pos[0] < 0 or new_pos[1] < 0 or \
64                 new_pos[0] >= self.size[0] or new_pos[1] >= self.size[1]:
65             raise server_.game.GameError('would move outside map bounds')
66         return new_pos
67
68     def move_LEFT(self, start_pos):
69         return [start_pos[0], start_pos[1] - 1]
70
71     def move_RIGHT(self, start_pos):
72         return [start_pos[0], start_pos[1] + 1]
73
74
75 class MapHex(Map):
76
77     # The following is used nowhere, so not implemented.
78     #def are_neighbors(self, pos_1, pos_2):
79     #    if pos_1[0] == pos_2[0] and abs(pos_1[1] - pos_2[1]) <= 1:
80     #        return True
81     #    elif abs(pos_1[0] - pos_2[0]) == 1:
82     #        if pos_1[0] % 2 == 0:
83     #            if pos_2[1] in (pos_1[1], pos_1[1] - 1):
84     #                return True
85     #        elif pos_2[1] in (pos_1[1], pos_1[1] + 1):
86     #            return True
87     #    return False
88
89     def move_UPLEFT(self, start_pos):
90         if start_pos[0] % 2 == 1:
91             return [start_pos[0] - 1, start_pos[1] - 1]
92         else:
93             return [start_pos[0] - 1, start_pos[1]]
94
95     def move_UPRIGHT(self, start_pos):
96         if start_pos[0] % 2 == 1:
97             return [start_pos[0] - 1, start_pos[1]]
98         else:
99             return [start_pos[0] - 1, start_pos[1] + 1]
100
101     def move_DOWNLEFT(self, start_pos):
102         if start_pos[0] % 2 == 1:
103              return [start_pos[0] + 1, start_pos[1] - 1]
104         else:
105                return [start_pos[0] + 1, start_pos[1]]
106
107     def move_DOWNRIGHT(self, start_pos):
108         if start_pos[0] % 2 == 1:
109             return [start_pos[0] + 1, start_pos[1]]
110         else:
111             return [start_pos[0] + 1, start_pos[1] + 1]
112
113     def get_neighbors(self, pos):
114         # DOWNLEFT, DOWNRIGHT, LEFT, RIGHT, UPLEFT, UPRIGHT (alphabetically)
115         neighbors = [None, None, None, None, None, None]  # e, d, c, x, s, w
116         if pos[1] > 0:
117             neighbors[2] = [pos[0], pos[1] - 1]
118         if pos[1] < self.size[1] - 1:
119             neighbors[3] = [pos[0], pos[1] + 1]
120         # x, c, s, d, w, e  # 3->0, 2->1, 5->4, 0->5
121         if pos[0] % 2 == 1:
122             if pos[0] > 0 and pos[1] > 0:
123                 neighbors[4] = [pos[0] - 1, pos[1] - 1]
124             if pos[0] < self.size[0] - 1 and pos[1] > 0:
125                 neighbors[0] = [pos[0] + 1, pos[1] - 1]
126             if pos[0] > 0:
127                 neighbors[5] = [pos[0] - 1, pos[1]]
128             if pos[0] < self.size[0] - 1:
129                 neighbors[1] = [pos[0] + 1, pos[1]]
130         else:
131             if pos[0] > 0 and pos[1] < self.size[1] - 1:
132                 neighbors[5] = [pos[0] - 1, pos[1] + 1]
133             if pos[0] < self.size[0] - 1 and pos[1] < self.size[1] - 1:
134                 neighbors[1] = [pos[0] + 1, pos[1] + 1]
135             if pos[0] > 0:
136                 neighbors[4] = [pos[0] - 1, pos[1]]
137             if pos[0] < self.size[0] - 1:
138                 neighbors[0] = [pos[0] + 1, pos[1]]
139         return neighbors
140
141
142 class MapFovHex(MapHex):
143
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
148         self[yx] = '.'
149         self.shadow_cones = []
150         self.circle_out(yx, self.shadow_process_hex)
151
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] == '.',
154         # skip all.
155         CIRCLE = 360  # Since we'll float anyways, number is actually arbitrary.
156
157         def correct_arm(arm):
158             if arm < 0:
159                 arm += CIRCLE
160             return arm
161
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)
167                     return True
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.
172             return False
173
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)
182                     return True
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)
189                     return True
190             return False
191
192         def eval_cone(cone):
193             #print('DEBUG CONE', cone, '(', step_size, distance_to_center, number_steps, ')')
194             if in_shadow_cone(cone):
195                 return
196             self[yx] = '.'
197             if self.source_map[yx] != '.':
198                 #print('DEBUG throws shadow', cone)
199                 unmerged = True
200                 while merge_cone(cone):
201                     unmerged = False
202                 if unmerged:
203                     self.shadow_cones += [cone]
204
205         #print('DEBUG', yx)
206         step_size = (CIRCLE/6) / 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])
215         else:
216             eval_cone([left_arm, right_arm])
217
218     def circle_out(self, yx, f):
219         # Optimization potential: Precalculate movement positions. (How to check
220         # circle_in_map then?)
221         # Optimization potential: Precalculate what hexes are shaded by what hex
222         # and skip evaluation of already shaded hexes. (This only works if hex
223         # shading implies they completely lie in existing shades; otherwise we
224         # would lose shade growth through hexes at shade borders.)
225
226         def move(pos, direction):
227             """Move position pos into direction. Return whether still in map."""
228             mover = getattr(self, 'move_' + direction)
229             pos[:] = mover(pos)
230             if pos[0] < 0 or pos[1] < 0 or \
231                pos[0] >= self.size[0] or pos[1] >= self.size[1]:
232                 return False
233             return True
234
235         # TODO: Start circling only in earliest obstacle distance.
236         directions = ('DOWNLEFT', 'LEFT', 'UPLEFT', 'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
237         circle_in_map = True
238         distance = 1
239         yx = yx[:]
240         #print('DEBUG CIRCLE_OUT', yx)
241         while circle_in_map:
242             circle_in_map = False
243             move(yx, 'RIGHT')
244             for dir_i in range(len(directions)):
245                 for dir_progress in range(distance):
246                     direction = directions[dir_i]
247                     if move(yx, direction):
248                         f(yx, distance, dir_i, dir_progress)
249                         circle_in_map = True
250             distance += 1
251
252
253 class MapSquare(Map):
254
255     # The following is used nowhere, so not implemented.
256     #def are_neighbors(self, pos_1, pos_2):
257     #    return abs(pos_1[0] - pos_2[0]) <= 1 and abs(pos_1[1] - pos_2[1] <= 1)
258
259     def move_UP(self, start_pos):
260         return [start_pos[0] - 1, start_pos[1]]
261
262     def move_DOWN(self, start_pos):
263         return [start_pos[0] + 1, start_pos[1]]
264
265     def get_neighbors(self, pos):
266         # DOWN, LEFT, RIGHT, UP  (alphabetically)
267         neighbors = [None, None, None, None]
268         if pos[0] > 0:
269             neighbors[3] = [pos[0] - 1, pos[1]]
270         if pos[1] > 0:
271             neighbors[1] = [pos[0], pos[1] - 1]
272         if pos[0] < self.size[0] - 1:
273             neighbors[0] = [pos[0] + 1, pos[1]]
274         if pos[1] < self.size[1] - 1:
275             neighbors[2] = [pos[0], pos[1] + 1]
276         return neighbors
277
278
279 class MapFovSquare(MapSquare):
280     """Just a marginally and unsatisfyingly adapted variant of MapFovHex."""
281
282     def __init__(self, source_map, yx):
283         self.source_map = source_map
284         self.size = self.source_map.size
285         self.terrain = '?' * self.size_i
286         self[yx] = '.'
287         self.shadow_cones = []
288         self.circle_out(yx, self.shadow_process_hex)
289
290     def shadow_process_hex(self, yx, distance_to_center, dir_i, dir_progress):
291         CIRCLE = 360  # Since we'll float anyways, number is actually arbitrary.
292
293         def correct_arm(arm):
294             if arm < 0:
295                 arm += CIRCLE
296             return arm
297
298         def in_shadow_cone(new_cone):
299             for old_cone in self.shadow_cones:
300                 if old_cone[0] >= new_cone[0] and \
301                     new_cone[1] >= old_cone[1]:
302                     #print('DEBUG shadowed by:', old_cone)
303                     return True
304             return False
305
306         def merge_cone(new_cone):
307             for old_cone in self.shadow_cones:
308                 if new_cone[0] > old_cone[0] and \
309                     (new_cone[1] < old_cone[0] or
310                      math.isclose(new_cone[1], old_cone[0])):
311                     #print('DEBUG merging to', old_cone)
312                     old_cone[0] = new_cone[0]
313                     #print('DEBUG merged cone:', old_cone)
314                     return True
315                 if new_cone[1] < old_cone[1] and \
316                     (new_cone[0] > old_cone[1] or
317                      math.isclose(new_cone[0], old_cone[1])):
318                     #print('DEBUG merging to', old_cone)
319                     old_cone[1] = new_cone[1]
320                     #print('DEBUG merged cone:', old_cone)
321                     return True
322             return False
323
324         def eval_cone(cone):
325             new_cone = [left_arm, right_arm]
326             #print('DEBUG CONE', cone, '(', step_size, distance_to_center, number_steps, ')')
327             if in_shadow_cone(cone):
328                 return
329             self[yx] = '.'
330             if self.source_map[yx] != '.':
331                 #print('DEBUG throws shadow', cone)
332                 unmerged = True
333                 while merge_cone(cone):
334                     unmerged = False
335                 if unmerged:
336                     self.shadow_cones += [cone]
337
338         #print('DEBUG', yx)
339         step_size = (CIRCLE/4) / distance_to_center
340         number_steps = dir_i * distance_to_center + dir_progress
341         left_arm = correct_arm(-(step_size/2) - step_size*number_steps)
342         right_arm = correct_arm(left_arm - step_size)
343         if right_arm > left_arm:
344             eval_cone([left_arm, 0])
345             eval_cone([CIRCLE, right_arm])
346         else:
347             eval_cone([left_arm, right_arm])
348
349     def circle_out(self, yx, f):
350
351         def move(pos, direction):
352             """Move position pos into direction. Return whether still in map."""
353             mover = getattr(self, 'move_' + direction)
354             pos[:] = mover(pos)
355             if pos[0] < 0 or pos[1] < 0 or \
356                pos[0] >= self.size[0] or pos[1] >= self.size[1]:
357                 return False
358             return True
359
360         directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
361                       ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
362         circle_in_map = True
363         distance = 1
364         yx = yx[:]
365         #print('DEBUG CIRCLE_OUT', yx)
366         while circle_in_map:
367             circle_in_map = False
368             move(yx, 'RIGHT')
369             for dir_i in range(len(directions)):
370                 for dir_progress in range(distance):
371                     direction = directions[dir_i]
372                     move(yx, direction[0])
373                     if move(yx, direction[1]):
374                         f(yx, distance, dir_i, dir_progress)
375                         circle_in_map = True
376             distance += 1
377
378
379 map_manager = game_common.MapManager(globals())