home · contact · privacy
Refactor.
[plomrogue2-experiments] / server_ / map_.py
1 import sys
2 sys.path.append('../')
3 import game_common
4 import server_.game
5 import math
6 from server_.game_error import GameError
7
8
9 class Map(game_common.Map):
10
11     def __getitem__(self, yx):
12         return self.terrain[self.get_position_index(yx)]
13
14     def __setitem__(self, yx, c):
15         pos_i = self.get_position_index(yx)
16         if type(c) == str:
17             self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
18         else:
19             self.terrain[pos_i] = c
20
21     def __iter__(self):
22         """Iterate over YX position coordinates."""
23         for y in range(self.size[0]):
24             for x in range(self.size[1]):
25                 yield [y, x]
26
27     @property
28     def geometry(self):
29         return self.__class__.__name__[3:]
30
31     def lines(self):
32         width = self.size[1]
33         for y in range(self.size[0]):
34             yield (y, self.terrain[y * width:(y + 1) * width])
35
36     def get_fov_map(self, yx):
37         fov_class_name = 'Fov' + self.__class__.__name__
38         fov_class = globals()[fov_class_name]
39         return fov_class(self, yx)
40
41     # The following is used nowhere, so not implemented.
42     #def items(self):
43     #    for y in range(self.size[0]):
44     #        for x in range(self.size[1]):
45     #            yield ([y, x], self.terrain[self.get_position_index([y, x])])
46
47     def get_directions(self):
48         directions = []
49         for name in dir(self):
50             if name[:5] == 'move_':
51                 directions += [name[5:]]
52         return directions
53
54     def get_neighbors(self, pos):
55         neighbors = {}
56         if not hasattr(self, 'neighbors_to'):
57             self.neighbors_to = {}
58         if pos in self.neighbors_to:
59             return self.neighbors_to[pos]
60         for direction in self.get_directions():
61             neighbors[direction] = None
62             try:
63                 neighbors[direction] = self.move(pos, direction)
64             except GameError:
65                 pass
66         self.neighbors_to[pos] = neighbors
67         return neighbors
68
69     def new_from_shape(self, init_char):
70         import copy
71         new_map = copy.deepcopy(self)
72         for pos in new_map:
73             new_map[pos] = init_char
74         return new_map
75
76     def move(self, start_pos, direction):
77         mover = getattr(self, 'move_' + direction)
78         new_pos = mover(start_pos)
79         if new_pos[0] < 0 or new_pos[1] < 0 or \
80                 new_pos[0] >= self.size[0] or new_pos[1] >= self.size[1]:
81             raise GameError('would move outside map bounds')
82         return new_pos
83
84     def move_LEFT(self, start_pos):
85         return [start_pos[0], start_pos[1] - 1]
86
87     def move_RIGHT(self, start_pos):
88         return [start_pos[0], start_pos[1] + 1]
89
90
91 class MapHex(Map):
92
93     # The following is used nowhere, so not implemented.
94     #def are_neighbors(self, pos_1, pos_2):
95     #    if pos_1[0] == pos_2[0] and abs(pos_1[1] - pos_2[1]) <= 1:
96     #        return True
97     #    elif abs(pos_1[0] - pos_2[0]) == 1:
98     #        if pos_1[0] % 2 == 0:
99     #            if pos_2[1] in (pos_1[1], pos_1[1] - 1):
100     #                return True
101     #        elif pos_2[1] in (pos_1[1], pos_1[1] + 1):
102     #            return True
103     #    return False
104
105     def move_UPLEFT(self, start_pos):
106         if start_pos[0] % 2 == 1:
107             return [start_pos[0] - 1, start_pos[1] - 1]
108         else:
109             return [start_pos[0] - 1, start_pos[1]]
110
111     def move_UPRIGHT(self, start_pos):
112         if start_pos[0] % 2 == 1:
113             return [start_pos[0] - 1, start_pos[1]]
114         else:
115             return [start_pos[0] - 1, start_pos[1] + 1]
116
117     def move_DOWNLEFT(self, start_pos):
118         if start_pos[0] % 2 == 1:
119              return [start_pos[0] + 1, start_pos[1] - 1]
120         else:
121                return [start_pos[0] + 1, start_pos[1]]
122
123     def move_DOWNRIGHT(self, start_pos):
124         if start_pos[0] % 2 == 1:
125             return [start_pos[0] + 1, start_pos[1]]
126         else:
127             return [start_pos[0] + 1, start_pos[1] + 1]
128
129
130 class MapSquare(Map):
131
132     # The following is used nowhere, so not implemented.
133     #def are_neighbors(self, pos_1, pos_2):
134     #    return abs(pos_1[0] - pos_2[0]) <= 1 and abs(pos_1[1] - pos_2[1] <= 1)
135
136     def move_UP(self, start_pos):
137         return [start_pos[0] - 1, start_pos[1]]
138
139     def move_DOWN(self, start_pos):
140         return [start_pos[0] + 1, start_pos[1]]
141
142
143 class FovMap:
144
145     def __init__(self, source_map, yx):
146         self.source_map = source_map
147         self.size = self.source_map.size
148         self.terrain = '?' * self.size_i
149         self[yx] = '.'
150         self.shadow_cones = []
151         self.circle_out(yx, self.shadow_process_hex)
152
153     def shadow_process_hex(self, yx, distance_to_center, dir_i, dir_progress):
154         # Possible optimization: If no shadow_cones yet and self[yx] == '.',
155         # skip all.
156         CIRCLE = 360  # Since we'll float anyways, number is actually arbitrary.
157
158         def correct_arm(arm):
159             if arm < 0:
160                 arm += CIRCLE
161             return arm
162
163         def in_shadow_cone(new_cone):
164             for old_cone in self.shadow_cones:
165                 if old_cone[0] >= new_cone[0] and \
166                     new_cone[1] >= old_cone[1]:
167                     #print('DEBUG shadowed by:', old_cone)
168                     return True
169                 # We might want to also shade hexes whose middle arm is inside a
170                 # shadow cone for a darker FOV. Note that we then could not for
171                 # optimization purposes rely anymore on the assumption that a
172                 # shaded hex cannot add growth to existing shadow cones.
173             return False
174
175         def merge_cone(new_cone):
176             for old_cone in self.shadow_cones:
177                 if new_cone[0] > old_cone[0] and \
178                     (new_cone[1] < old_cone[0] or
179                      math.isclose(new_cone[1], old_cone[0])):
180                     #print('DEBUG merging to', old_cone)
181                     old_cone[0] = new_cone[0]
182                     #print('DEBUG merged cone:', old_cone)
183                     return True
184                 if new_cone[1] < old_cone[1] and \
185                     (new_cone[0] > old_cone[1] or
186                      math.isclose(new_cone[0], old_cone[1])):
187                     #print('DEBUG merging to', old_cone)
188                     old_cone[1] = new_cone[1]
189                     #print('DEBUG merged cone:', old_cone)
190                     return True
191             return False
192
193         def eval_cone(cone):
194             #print('DEBUG CONE', cone, '(', step_size, distance_to_center, number_steps, ')')
195             if in_shadow_cone(cone):
196                 return
197             self[yx] = '.'
198             if self.source_map[yx] != '.':
199                 #print('DEBUG throws shadow', cone)
200                 unmerged = True
201                 while merge_cone(cone):
202                     unmerged = False
203                 if unmerged:
204                     self.shadow_cones += [cone]
205
206         #print('DEBUG', yx)
207         step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
208         number_steps = dir_i * distance_to_center + dir_progress
209         left_arm = correct_arm(-(step_size/2) - step_size*number_steps)
210         right_arm = correct_arm(left_arm - step_size)
211         # Optimization potential: left cone could be derived from previous
212         # right cone. Better even: Precalculate all cones.
213         if right_arm > left_arm:
214             eval_cone([left_arm, 0])
215             eval_cone([CIRCLE, right_arm])
216         else:
217             eval_cone([left_arm, right_arm])
218
219     def basic_circle_out_move(self, pos, direction):
220         """Move position pos into direction. Return whether still in map."""
221         mover = getattr(self, 'move_' + direction)
222         pos[:] = mover(pos)
223         if pos[0] < 0 or pos[1] < 0 or \
224             pos[0] >= self.size[0] or pos[1] >= self.size[1]:
225             return False
226         return True
227
228     def circle_out(self, yx, f):
229         # Optimization potential: Precalculate movement positions. (How to check
230         # circle_in_map then?)
231         # Optimization potential: Precalculate what hexes are shaded by what hex
232         # and skip evaluation of already shaded hexes. (This only works if hex
233         # shading implies they completely lie in existing shades; otherwise we
234         # would lose shade growth through hexes at shade borders.)
235
236         # TODO: Start circling only in earliest obstacle distance.
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             self.basic_circle_out_move(yx, 'RIGHT')
244             for dir_i in range(len(self.circle_out_directions)):
245                 for dir_progress in range(distance):
246                     direction = self.circle_out_directions[dir_i]
247                     if self.circle_out_move(yx, direction):
248                         f(yx, distance, dir_i, dir_progress)
249                         circle_in_map = True
250             distance += 1
251
252
253 class FovMapHex(FovMap, MapHex):
254     circle_out_directions = ('DOWNLEFT', 'LEFT', 'UPLEFT',
255                              'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
256
257     def circle_out_move(self, yx, direction):
258         return self.basic_circle_out_move(yx, direction)
259
260
261 class FovMapSquare(FovMap, MapSquare):
262     circle_out_directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
263                              ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
264
265     def circle_out_move(self, yx, direction):
266         self.basic_circle_out_move(yx, direction[0])
267         return self.basic_circle_out_move(yx, direction[1])
268
269
270 map_manager = game_common.MapManager((MapHex, MapSquare))