-
Notifications
You must be signed in to change notification settings - Fork 11
/
maze.py
239 lines (175 loc) · 7.72 KB
/
maze.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
#!/usr/bin/env python
# Copyright (c) 2017 Conor Patrick
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import random
from random import shuffle, randint, getrandbits
import sys
from docopt import docopt
from generate import render_maze, RESOLUTION, solid_cube, cube_mesh
from solid import *
def maze(width, height, depth, **kwargs):
class Cell():
#
# 0/forward
#2/left 3/right
# 1/back
# top: 4
# bot: 5
def __init__(self,):
self.walls = [ {'broken': False, 'neighbor': None, 'cell':self, 'i': i, 'ni': self.inv_i(i)} for i in range(0,6)]
self.visited = False
self.start = False
self.end = False
self.walls_broken = 0
self.edge = -1
def wall(self,i):
return self.walls[i]
def visit(self,i):
self.visited = True
def inv_i(self,i):
return [1,0,3,2,5,4][i]
def remove(self,i):
self.walls[i]['broken'] = True
self.visit(i)
self.walls_broken += 1
ni = self.inv_i(i)
assert(self == self.walls[i]['neighbor'].walls[ni]['neighbor'])
self.walls[i]['neighbor'].walls[ni]['broken'] = True
self.walls[i]['neighbor'].visit(ni)
self.walls[i]['neighbor'].walls_broken += 1
def set_neighbor(self, i, n):
ni = self.inv_i(i)
self.walls[i]['neighbor'] = n
self.walls[i]['neighbor'].walls[ni]['neighbor'] = self
def mat(self,):
return [self.walls[0]['broken'],self.walls[1]['broken'],
self.walls[2]['broken'],self.walls[3]['broken'],
self.walls[4]['broken'], self.walls[5]['broken']]
grid = [[[Cell() for x in range(width)] for y in range(height)] for z in range(depth)]
for z in range(0,depth):
for y in range(0,height):
for x in range(0,width):
if y == 0: # forward
grid[z][y][x].edge = 0
if y == height - 1: # backward
grid[z][y][x].edge = 1
if x == 0: # left
grid[z][y][x].edge = 2
if x == (width - 1): # right
grid[z][y][x].edge = 3
if z == 0: # top
grid[z][y][x].edge = 4
if z == (depth - 1): # bottem
grid[z][y][x].edge = 5
# forward (height)
if (y+1) < height:
grid[z][y][x].set_neighbor(0, grid[z][y+1][x])
# backward (height)
if (y-1) >= 0:
grid[z][y][x].set_neighbor(1,grid[z][y-1][x])
# right (width)
if (x+1) > width:
grid[z][y][x].set_neighbor(3, grid[z][y][x+1])
# left (width)
if (x-1) >=0:
grid[z][y][x].set_neighbor(2, grid[z][y][x-1])
# top (depth)
if (z+1) < depth:
grid[z][y][x].set_neighbor(4, grid[z+1][y][x])
# bottom (depth)
if (z-1) >=0:
grid[z][y][x].set_neighbor(5, grid[z-1][y][x])
#for z in range(1,depth-1):
#for x in range(1,height-1):
#for y in range(1,width-1):
#for l in range(4):
#assert(grid[z][y][x].walls[l]['neighbor'] != None)
# prim's alg
start = random.choice(random.choice(grid[0]))
start.start = True
start.visited = True
wall_list = [] + start.walls
grid2 = [[[0,0,0,0,0,0] for x in range(width)] for x in range(height)]
c = 0
print 'running Prim\'s alg on potentially %d walls.' % (6 * height * width * depth)
while len(wall_list):
w = random.choice(wall_list)
if w['neighbor']:
if w['neighbor'].visited ^ w['cell'].visited:
wall_list = wall_list + w['neighbor'].walls
w['cell'].remove(w['i'])
wall_list.remove(w)
# depth-first-search to find the hardest path to determine end node, i.e. longest path
def dfs(node, edge = None, depth = 0):
walls = [] + node.walls
children = []
if edge is not None: del walls[edge]
for x in walls:
if x['broken']:
children += dfs(x['neighbor'], x['ni'], depth + 1)
return children + [(node, depth)]
paths = dfs(start)
end = paths[0]
for i in paths:
if i[0].edge == 5:
if i[1] > end[1]:
end = i
print 'correct path is %d nodes long' % end[1]
end = end[0]
end.end = True
return render_maze(grid, **kwargs)
doc = '''
3D maze generator. Uses Prim's alg to generator maze with
a start and an end and no loops. Outputs openscad.
Usage:
maze.py -h
maze.py [-x WIDTH] [-y HEIGHT] [-z DEPTH] [-r RES] [-n SPACING] [-d DIAMETER] [-m | -s | -t ] [-f THICK] <output.scad>
Options:
-h, --help Show this help message and exit
-x --width WIDTH Number of cells wide [default: 5]
-y --height HEIGHT Number of cells high [default: 5]
-z --depth DEPTH Number of cells deep [default: 5]
-r --resolution RES Resolution of rendering [default: 25]
-n --node-spacing SPACING Distance between nodes in mm [default: 10.0]
-d --diameter DIAMETER Diameter of nodes in mm [default: 9.0]
-m --enable-mesh Creates a cross hatch mesh and subtracts maze from it.
-s --enable-solid Creates a solid cube and subtracts maze from it.
-t --enable-tunnel Subtracts smaller version of maze from itself to make tunnels.
-f --tunnel-thickness THICK Thickness of tunnel wall in mm. [default: 1.0]
Examples:
maze.py output.scad
maze.py [-x 10] [-y 5] [-z 4] [-n 10] [-d 3] [-m | -s | -t ] [-f 1.0] <output.scad>
'''
if __name__ == "__main__":
args = docopt(doc)
dims = [int(args['--width'][0]),int(args['--height'][0]),int(args['--depth'][0])]
seed = getrandbits(64)
random.seed(seed)
m = maze(*dims, unit = float(args['--node-spacing'][0]), diameter = float(args['--diameter'][0]))
if args['--enable-mesh']:
m = cube_mesh(*dims) - m
elif args['--enable-solid']:
m = solid_cube(*dims) - m
elif args['--enable-tunnel']:
thickness = float(args['--tunnel-thickness'][0])
d = float(args['--diameter'][0])
ind = (d - thickness)
random.seed(seed)
m2 = maze(*dims, unit = float(args['--node-spacing'][0]), diameter = ind)
m -= m2
out = ('$fn=%d;' % int(args['--resolution'][0]))+scad_render(m)
open(args['<output.scad>'],'w+').write(out)