-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
268 lines (236 loc) · 7.87 KB
/
grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import re
from constants import *
from draw import draw_cell
class Cell(object):
letter = None
attr = defaultATTR.copy()
mode = 'normal'
frame = False
draw = True
neighbours = None
tile = None
_dirty = True
@property
def is_wall(self):
return self.attr['fgcolour'] == defaultFG and self.letter and (
self.mode == 'special' and self.letter in 'xqlkmjnvwut'
or self.mode == 'normal' and self.letter in '-|'
)
@property
def is_connecting(self):
return self.is_wall or (self.letter == '+' and self.attr['fgcolour'] == YELLOW)
def pp_top_left_corner(self, neighbours):
is_corner = (
self.is_wall
and not neighbours['up'].is_connecting
and not neighbours['left'].is_connecting
and neighbours['down'].is_connecting
and neighbours['right'].is_connecting
)
if is_corner:
self.mode = 'special'
self.letter = 'l'
def pp_top_right_corner(self, neighbours):
is_corner = (
self.is_wall
and not neighbours['up'].is_connecting
and neighbours['left'].is_connecting
and neighbours['down'].is_connecting
and not neighbours['right'].is_connecting
)
if is_corner:
self.mode = 'special'
self.letter = 'k'
def pp_bottom_right_corner(self, neighbours):
is_corner = (
self.is_wall
and neighbours['up'].is_connecting
and neighbours['left'].is_connecting
and not neighbours['down'].is_connecting
and not neighbours['right'].is_connecting
)
if is_corner:
self.mode = 'special'
self.letter = 'j'
def pp_bottom_left_corner(self, neighbours):
is_corner = (
self.is_wall
and neighbours['up'].is_connecting
and not neighbours['left'].is_connecting
and not neighbours['down'].is_connecting
and neighbours['right'].is_connecting
)
if is_corner:
self.mode = 'special'
self.letter = 'm'
def pp_top_junction(self, neighbours):
is_junction = (
self.is_wall
and not neighbours['up'].is_connecting
and neighbours['left'].is_connecting
and neighbours['down'].is_connecting
and neighbours['right'].is_connecting
)
if is_junction:
self.mode = 'special'
self.letter = 'w'
def pp_right_junction(self, neighbours):
is_junction = (
self.is_wall
and neighbours['up'].is_connecting
and neighbours['left'].is_connecting
and neighbours['down'].is_connecting
and not neighbours['right'].is_connecting
)
if is_junction:
self.mode = 'special'
self.letter = 'u'
def pp_bottom_junction(self, neighbours):
is_junction = (
self.is_wall
and neighbours['up'].is_connecting
and neighbours['left'].is_connecting
and not neighbours['down'].is_connecting
and neighbours['right'].is_connecting
)
if is_junction:
self.mode = 'special'
self.letter = 'v'
def pp_left_junction(self, neighbours):
is_junction = (
self.is_wall
and neighbours['up'].is_connecting
and not neighbours['left'].is_connecting
and neighbours['down'].is_connecting
and neighbours['right'].is_connecting
)
if is_junction:
self.mode = 'special'
self.letter = 't'
def pp_total_junction(self, neighbours):
is_junction = (
self.is_wall
and neighbours['up'].is_connecting
and neighbours['left'].is_connecting
and neighbours['down'].is_connecting
and neighbours['right'].is_connecting
)
if is_junction:
self.mode = 'special'
self.letter = 'n'
class FakeCell(object):
"""Used to represent cells that are out of bounds"""
def __getattr__(self, attr):
return None
class Grid(object):
_cells = None
after_x = None
after_y = None
def initial_cells(self):
return [
[Cell() for x in range(ttyW*2)]
for y in range(ttyH*2)
]
def __init__(self):
self._cells = self.initial_cells()
def set_cell(self, letter, x, y, mode, attr, tile=None):
cell = self._cells[y][x]
cell.letter = letter
cell.mode = mode
cell.attr = attr.copy()
cell.tile = tile
cell.draw = True
cell._dirty = True
def set_frame(self, x, y):
cell = self._cells[y][x]
cell.frame = True
cell._dirty = True
@property
def cells(self):
for y, row in enumerate(self._cells):
for x, cell in enumerate(row):
yield (cell, x, y)
def clear(self):
# for performance we just empty the screen and say nothing is dirty
from draw import draw_surface
draw_surface.fill(defaultBG)
self.__init__()
for cell, _, _ in self.cells:
cell._dirty = False
def get_neighbours(self, x, y):
coords = (
('up', x, y - 1),
('right', x + 1, y),
('down', x, y + 1),
('left', x - 1, y),
)
neighbours = {
'up': FakeCell(),
'right': FakeCell(),
'down': FakeCell(),
'left': FakeCell(),
}
for direction, nx, ny in coords:
try:
neighbours[direction] = self._cells[ny][nx]
except IndexError:
pass
return neighbours
def find_text_boxes(self):
# Search for boxes of text to not draw tiles inside
for y, row in enumerate(self._cells):
line = ''.join(cell.letter if cell.letter else ' ' for cell in row)
match = re.search(end_sequence_re, line)
if match:
x = match.start()
self.after_x = x - 2
self.after_y = y
self.no_tiles_in_area(x, y + 1)
def no_tiles_in_area(self, x, y):
for row in self._cells[:y]:
for cell in row[x:]:
# don't draw any tiles, this is text
cell.draw = False
# and undraw it if it was already drawn
cell._dirty = True
def process_individual_cells(self):
# Post process individual cells
for cell, x, y in self.cells:
if not cell._dirty or not cell.is_wall:
continue
neighbours = self.get_neighbours(x, y)
post_process_functions = (
f for f in dir(cell)
if f.startswith('pp_')
)
for f in post_process_functions:
getattr(cell, f)(neighbours)
def post_process(self):
pass
self.process_individual_cells()
self.find_text_boxes()
def draw(self):
for cell, x, y in self.cells:
if cell._dirty:
draw_cell(cell, x, y)
cell._dirty = False
if cell.frame:
cell.frame = False
cell._dirty = True
def after_draw(self):
# Draw a border around text boxes
import pygame
from draw import draw_surface
if self.after_y and self.after_y > 1:
x = (self.after_x + 1) * W
y = (self.after_y - 1) * H
max_x = max((x for cell, x, y in self.cells if cell.letter and cell.letter != ' '))
max_x = (max_x) * W
pygame.draw.rect(
draw_surface,
defaultFG,
(x, 0, max_x-x, y+H),
1,
)
self.after_x = None
self.after_y = None