-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile_gen.py
370 lines (294 loc) · 12.2 KB
/
tile_gen.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Tools:
import networkx as nx
import random as rd
import matplotlib.pyplot as plt
import uuid
import pprint as pp
# Configuration:
import conf
####################################
# POINT GRAPH
####################################
# Base Graph:
class PointGraph (object):
def __init__ (self):
self.P = nx.Graph()
# Adding nodes in the graph AND connections between nodes:
# ( nodes <=> class Point )
def create (self):
self.P = add_random_nodes()
# Print graph:
def printContent (self, show = True):
pos = dict([(item, (item.row, item.col)) for item in self.P])
nx.draw(self.P, pos, with_labels = True)
if show:
plt.show()
# Base Node:
class Point (object):
def __init__ (self, row, col):
self.row = row
self.col = col
self._id = uuid.uuid4()
def get_set (self):
_set = set()
_set.add(str(self.row) + ':row')
_set.add(str(self.col) + ':col')
return _set
def get_coord (self):
return (self.row, self.col)
def __str__ (self):
return '{}:{}'.format(self.row, self.col)
####################################
# RUN
####################################
def add_random_nodes ():
P = nx.Graph()
# Add INIT Points / nodes:
listPoints = [ Point(0,0),
Point(0, conf.baseSize[1] - 1),
Point( conf.baseSize[0] - 1, conf.baseSize[1] - 1),
Point( conf.baseSize[0] - 1, 0)]
listConnections = [ (0, 1),
(0, 3),
(2, 1),
(2, 3)]
for connect in listConnections:
P.add_edge(listPoints[connect[0]], listPoints[connect[1]])
del(listPoints)
del(listConnections)
# Add random Points / nodes:
counter = 1
while conf.room_number > counter:
# Select Points / nodes:
random_node_1 = rd.choice(list(P.nodes()))
random_node_2 = rd.choice(list(P.neighbors(random_node_1)))
comun = random_node_1.get_set() & random_node_2.get_set()
number_pos, type_pos = comun.pop().split(':')
del(comun)
# Debugger
if conf.debugger:
print('random_node_1: {}'.format(random_node_1))
print('random_node_2: {}'.format(random_node_2))
print('number_pos: {}'.format(number_pos))
print('type_pos: {}'.format(type_pos))
# Chose new Point / node:
# Creating a new HORIZONTAL wall:
if type_pos == 'col' and abs(random_node_1.row - random_node_2.row) > 2 * conf.border_limit:
try:
new_row = rd.randrange(random_node_1.row + conf.border_limit, random_node_2.row - conf.border_limit + 1)
except:
new_row = rd.randrange(random_node_2.row + conf.border_limit, random_node_1.row - conf.border_limit + 1)
new_point_1 = Point(int(new_row), int(number_pos))
# Find the TWO walls you can collide with and select ONE:
collider = detect_collision(P, new_point_1, type_pos)
collider_wall = rd.choice(collider)
# Verifying that intersection points pass "border_limit" test:
_max = max(point.row for point in collider_wall)
_min = min(point.row for point in collider_wall)
if _min + conf.border_limit < new_row and new_row < _max - conf.border_limit:
# Create new_point_2:
new_point_2 = Point(int(new_row), int(collider_wall[0].col))
else:
# Note: If there's only ONE wall program will crash
if len(collider) > 1:
collider.remove(collider_wall)
collider_wall = rd.choice(collider)
# Verifying that intersection points pass "border_limit" test:
_max = max(point.row for point in collider_wall)
_min = min(point.row for point in collider_wall)
if _min + conf.border_limit < new_row and new_row < _max - conf.border_limit:
# Create new_point_2:
new_point_2 = Point(int(new_row), int(collider_wall[0].col))
else:
continue
else:
continue
# Creating a new VERTICAL wall:
elif type_pos == 'row' and abs(random_node_1.col - random_node_2.col) > 2 * conf.border_limit:
try:
new_col = rd.randrange(random_node_1.col + conf.border_limit, random_node_2.col - conf.border_limit + 1)
except:
new_col = rd.randrange(random_node_2.col + conf.border_limit, random_node_1.col - conf.border_limit + 1)
new_point_1 = Point(int(number_pos), int(new_col))
# Find the TWO walls you can collide with and select ONE:
collider = detect_collision(P, new_point_1, type_pos)
collider_wall = rd.choice(collider)
# Verifying that intersection points pass "border_limit" test:
_max = max(point.col for point in collider_wall)
_min = min(point.col for point in collider_wall)
if _min + conf.border_limit < new_col and new_col < _max - conf.border_limit:
# Create new_point_2:
new_point_2 = Point(int(collider_wall[0].row), int(new_col))
else:
# Note: If there's only ONE wall program will crash
if len(collider) > 1:
collider.remove(collider_wall)
collider_wall = rd.choice(collider)
# Verifying that intersection points pass "border_limit" test:
_max = max(point.col for point in collider_wall)
_min = min(point.col for point in collider_wall)
if _min + conf.border_limit < new_col and new_col < _max - conf.border_limit:
# Create new_point_2:
new_point_2 = Point(int(collider_wall[0].row), int(new_col))
else:
continue
else:
continue
else:
continue
# Add new connections new_point_1:
P.remove_edge(random_node_1, random_node_2)
P.add_edge(random_node_1, new_point_1)
P.add_edge(random_node_2, new_point_1)
# Debugger
if conf.debugger:
print('new_point_2: {}'.format(new_point_2))
print('collider:')
pp.pprint(collider)
print('\n')
# Add new connections collider_wall:
if len(collider_wall) == 3:
# We replace 'new_point_2' by it's equivalent from the 'collider_wall' list:
for item in collider_wall:
if len(item.get_set() & new_point_2.get_set()) == 2:
collider_wall.remove(item)
new_point_2 = item
break
P.add_edge(new_point_1, new_point_2)
for item in collider_wall:
P.add_edge(item, new_point_2)
elif len(collider_wall) == 2:
P.remove_edge(collider_wall[0], collider_wall[1])
P.add_edge(new_point_1, new_point_2)
for item in collider_wall:
P.add_edge(item, new_point_2)
# Add to counter the new room:
counter += 1
# Debugger PRO:
if conf.debugger_pro:
nx.draw(P, with_labels=True)
plt.show()
return P
def detect_collision (P, point, type_pos_point):
# Debugger
if conf.debugger:
print('_______________ENTER________________')
C = nx.Graph()
for node_1 in P:
for node_2 in P.neighbors(node_1):
# Find all vertical wall or horizontal walls of P
comun = node_1.get_set() & node_2.get_set()
number_pos, type_pos = comun.pop().split(':')
del(comun)
del(number_pos)
# Vertical wall:
if type_pos_point == type_pos and type_pos == 'col':
if point.row > node_1.row and point.row < node_2.row:
C.add_edge(node_1, node_2)
elif point.row < node_1.row and point.row > node_2.row:
C.add_edge(node_1, node_2)
elif point.row == node_1.row and point.row > node_2.row:
C.add_edge(node_1, node_2)
elif point.row == node_1.row and point.row < node_2.row:
C.add_edge(node_1, node_2)
elif point.row < node_1.row and point.row == node_2.row:
C.add_edge(node_1, node_2)
elif point.row > node_1.row and point.row == node_2.row:
C.add_edge(node_1, node_2)
else:
continue
# Horizontal wall:
elif type_pos_point == type_pos and type_pos == 'row':
if point.col > node_1.col and point.col < node_2.col:
C.add_edge(node_1, node_2)
elif point.col < node_1.col and point.col > node_2.col:
C.add_edge(node_1, node_2)
elif point.col == node_1.col and point.col > node_2.col:
C.add_edge(node_1, node_2)
elif point.col == node_1.col and point.col < node_2.col:
C.add_edge(node_1, node_2)
elif point.col < node_1.col and point.col == node_2.col:
C.add_edge(node_1, node_2)
elif point.col > node_1.col and point.col == node_2.col:
C.add_edge(node_1, node_2)
else:
continue
# Debugger
if conf.debugger:
print('new_point_1: {}'.format(point))
if conf.debugger_pro:
nx.draw(C, with_labels=True)
plt.show()
# Find the nearest walls you can collide with:
# Vertical wall:
if type_pos_point == 'col':
# Tabulate wall by proximity:
dict_point = dict()
for node in C:
new_index = node.col - point.col
if new_index in dict_point:
dict_point[new_index].append(node)
else:
dict_point[new_index] = [node]
# Debugger
if conf.debugger:
print('dict_point:')
pp.pprint(dict_point)
print('\n')
# Eliminate the wall where 'point' is located:
del(dict_point[0])
# Nearest wall right:
try:
index_1 = min(key for key, item in dict_point.items() if key > 0)
# If there is'nt any wall:
except:
index_1 = None
# Nearest wall left:
try:
index_2 = max(key for key, item in dict_point.items() if key < 0)
# If there is'nt any wall:
except:
index_2 = None
# Return list of points to "play" with:
# RIGHT / LEFT
if not index_1: return [dict_point[index_2]]
elif not index_2: return [dict_point[index_1]]
else: return [dict_point[index_1], dict_point[index_2]]
# Horizontal wall:
else:
# Tabulate wall by proximity:
dict_point = dict()
for node in C:
new_index = node.row - point.row
if new_index in dict_point:
dict_point[new_index].append(node)
else:
dict_point[new_index] = [node]
# Eliminate the wall where 'point' is located:
del(dict_point[0])
# Debugger
if conf.debugger:
print('dict_point:')
pp.pprint(dict_point)
print('\n')
# Nearest wall up:
try:
index_1 = max(key for key, item in dict_point.items() if key < 0)
# If there is'nt any wall:
except:
index_1 = None
# Nearest wall down:
try:
index_2 = min(key for key, item in dict_point.items() if key > 0)
# If there is'nt any wall:
except:
index_2 = None
# Return list of points to "play" with:
# UP / DOWN
if not index_1: return [dict_point[index_2]]
elif not index_2: return [dict_point[index_1]]
else: return [dict_point[index_1], dict_point[index_2]]
if __name__ == '__main__':
P_Graph = PointGraph()
P_Graph.create()
P_Graph.printContent()