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