-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.2dpy
272 lines (233 loc) · 6.93 KB
/
maze.2dpy
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
def Init():
# author -- rickb
# --- Set parameter names
STR.SetFParam0Name("Zoom")
STR.SetFParam1Name("Speed")
STR.SetFParam2Name("Color")
# --- accent : Hide Traversed
# --- trigger : Hide Track
# --- Setup global variables for the scene. GLB is reset every time a shader is loaded
# or reset is called
GLB.count = 0
GLB.xmode = 0
def makeMaze(w,h):
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for _ in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for _ in range(h + 1)]
def walk(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
random.shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
walk(xx, yy)
walk(random.randrange(w), random.randrange(h))
s = ""
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
s = s.replace(" ","##")
s = s.replace(" ","#")
s = s.replace("--"," ")
s = s.replace("|"," ")
s = s.replace("+"," ")
return s
GLB.makeMaze = makeMaze
GLB.maze = []
GLB.w = 16
GLB.h = 16
GLB.tiles = 0
i = 0
for l in makeMaze(int(GLB.w/2),int(GLB.h/2)).split("\n"):
i = i + 1
r = []
j = 0
for c in l.strip('\n'):
j = j + 1
r.append(c)
if c == "#":
GLB.tiles = GLB.tiles+1
GLB.maze.append(r)
GLB.px = -1
c = 0
while(GLB.px == -1 and c < GLB.w*GLB.h):
i = c%GLB.w
j = int(c/GLB.w)
if GLB.maze[i][j]=="#":
GLB.maze[i][j]="*"
GLB.tiles = GLB.tiles - 1
GLB.px = i
GLB.py = j
# 3
# 2+0
# 1
GLB.dir = random.randint(0,3)
GLB.pdir = GLB.dir
GLB.fdir = 0
# This gets calculated
GLB.angle = (GLB.dir/4)*2*pi
GLB.step = 0.0
c = c + 1
def locExists(pdir=-1):
if pdir == -1:
ldir = GLB.dir
else:
ldir = pdir
# RIGHT
if ldir == 0:
if GLB.px+1 < GLB.w and GLB.maze[GLB.px+1][GLB.py]!=' ':
return True
else:
return False
# LEFT
if ldir == 2:
if GLB.px-1 >= 0 and GLB.maze[GLB.px-1][GLB.py]!=' ':
return True
else:
return False
# UP
if ldir == 3:
if GLB.py+1 < GLB.h and GLB.maze[GLB.px][GLB.py+1]!=' ':
return True
else:
return False
# DOWN
if ldir == 1:
if GLB.py-1 >= 0 and GLB.maze[GLB.px][GLB.py-1]!=' ':
return True
else:
return False
GLB.locExists = locExists
def Dirs():
c=0
for i in range(4):
if GLB.locExists(i):
c = c + 1
return c
GLB.Dirs = Dirs
def MoveX():
if GLB.dir == 0:
return GLB.px + GLB.step
elif GLB.dir == 2:
return GLB.px - GLB.step
else:
return GLB.px
GLB.MoveX = MoveX
def MoveY():
if GLB.dir == 3:
return GLB.py + GLB.step
elif GLB.dir == 1:
return GLB.py - GLB.step
else:
return GLB.py
GLB.MoveY = MoveY
def GetDist(frac):
if(abs(GLB.pdir - GLB.dir)==3):
if GLB.pdir == 0:
return -1*frac
else:
return 1*frac
else:
return (GLB.dir - GLB.pdir)*frac
GLB.GetDist = GetDist
def Render(cr):
# --- grabbing copies of local variables and demonstrating our mix methods as well
GLB.f0 = STR.F0mix(0.5,1.5)
GLB.f1 = STR.F1mix(0.05,0.5)
GLB.f2 = STR.F2mix(0.0,0.66)
r,g,b = hsv_to_rgb(GLB.f2,1,1)
r2,g2,b2 = hsv_to_rgb(GLB.f2+0.33,1,1)
t = STR.FT() + STR.IT()
# --- trigger can only be checked once, as it resets the internal flag (accent can be called multiple times)
trig = STR.CheckTrigger()
if trig:
GLB.xmode = 1 if GLB.xmode==0 else 0
acc = STR.CheckAccent()
# ---- clear screen (color set above relative to trigger)
cr.set_source_rgba(0,0,0,1)
cr.rectangle(0,0,320,240)
cr.fill()
cr.save()
size = GLB.f0*20
cr.translate(160,120)
cr.rotate(GLB.angle)
cr.translate(-160,-120)
cr.translate(160-GLB.MoveX()*size,120-GLB.MoveY()*size)
for i in range(GLB.w):
for j in range(GLB.h):
if GLB.maze[i][j] == "#" and not GLB.xmode:
cr.set_source_rgba(1,1,1,1)
cr.rectangle((i-0.5)*size,(j-0.5)*size,size,size)
cr.stroke()
if GLB.maze[i][j] == "*":
cr.set_source_rgba(b2,g2,r2,1)
if STR.CheckAccent():
pass
else:
cr.rectangle((i-0.5)*size,(j-0.5)*size,size+1,size+1)
cr.fill()
cr.restore()
cr.set_source_rgba(b,g,r,1)
cr.arc(160,120,size/2.2,0,2*pi)
cr.fill()
step = GLB.f1
# Now let's take a look and see if we can move in the direction we want
if GLB.dir != GLB.pdir:
GLB.fdir = GLB.fdir + 0.05
GLB.angle = ((GLB.pdir + GLB.GetDist(GLB.fdir))/4)*2*pi
#GLB.angle = (GLB.dir/4)*2*pi
if int(GLB.fdir) >= 1:
GLB.pdir = GLB.dir
GLB.fdir = 0.0
# 0 -> 1 EASY 1
# 0 -> 3 HARD 1
# 0 -> 2 EASY 2
# 1 -> 0 EASY 1
# 1 -> 2 EASY 1
# 1 -> 3 EASY 2
# 2 -> 1 EASY 1
# 2 -> 3 EASY 1
# 2 -> 0 EASY 2
# 3 -> 2 EASY 1
# 3 -> 0 HARD 1
# 3 -> 1 EASY 2
elif GLB.locExists():
if GLB.step < 1.0:
GLB.step = GLB.step + step
else:
#We are in the next square, mathematically so we need to reset step and move the pointer to the next location
#print("%d/%f -- %d/%f %f"%(GLB.px,GLB.MoveX(),GLB.py,GLB.MoveY(),GLB.step))
GLB.px = int(round(GLB.MoveX()))
GLB.py = int(round(GLB.MoveY()))
if GLB.maze[GLB.px][GLB.py] != '*':
GLB.maze[GLB.px][GLB.py] = '*'
GLB.tiles = GLB.tiles - 1
# See if we can go the same direction
if GLB.locExists():
GLB.step = step
else:
GLB.step = 0
#We always want to be looking left so if we see a space to the left and turn that way if possible
#if we are at a triple or quad point
if GLB.Dirs() > 2:
left = GLB.dir-1 if GLB.dir != 0 else 3
if GLB.locExists(left):
GLB.dir = left
else:
# Rotate since we cannot go the way we are facing, we do NOT want to go the direction we were unless we've tried all
# directions as this is a deadend
# check -1
newdir = GLB.dir-1 if GLB.dir > 0 else 3
if GLB.locExists(newdir):
GLB.dir = newdir
else:
# check +1
newdir = GLB.dir+1 if GLB.dir != 3 else 0
if GLB.locExists(newdir):
GLB.dir = newdir
else:
GLB.dir = (GLB.dir+2)%4
GLB.count = GLB.count + 1
if GLB.tiles <= 0:
Init()