forked from mikemccllstr/mikemccllstr-python-minecraft
-
Notifications
You must be signed in to change notification settings - Fork 2
/
davef21370_maze.py
executable file
·143 lines (125 loc) · 4.58 KB
/
davef21370_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
#!/usr/bin/env python
# Quick rough & ready maze generator for Minecraft Pi edition.
# Dave Finch 2013
# mcpipy.com retrieved from URL below, written by davef21370
# https://github.com/brooksc/mcpipy/blob/master/davef21370_maze.py
import mcpi.minecraft as minecraft
import mcpi.block as block
import sys, random
from random import randint as rand
import server
# Connect to Minecraft.
try:
mc = minecraft.Minecraft.create(server.address)
except:
print "Cannot connect to Minecraft."
sys.exit(0)
# Create a function for picking a random direction.
def randDir():
r = rand(0,3)
if r == 0: rv = (0,-1) # Up.
if r == 1: rv = (0,1) # Down.
if r == 2: rv = (-1,0) # Left.
if r == 3: rv = (1,0) # Right.
return rv
# Create a function to initialize the maze.
# w and h are the width and height respectively.
def initMaze(w,h):
global maze,spl
# Create a 2 dimensional array.
maze = [[0]*h for x in range(w)]
# Create four walls around the maze.
# 1=wall, 0=walkway.
for x in range(0,w):
maze[x][0] = maze[x][h-1] = 1
makeWall(ppos.x+x,ppos.z+0)
makeWall(ppos.x+x,ppos.z+h-1)
for y in range(0,mazeYSize):
maze[0][y] = maze[w-1][y] = 1
makeWall(ppos.x,ppos.z+y)
makeWall(ppos.x+w-1,ppos.z+y)
# Make every other cell a starting point.
# 2=starting point.
# Also create a list of these points to speed up the main loop.
spl = []
for y in range(2,h-2,2):
for x in range(2,w-2,2):
maze[x][y] = 2
spl.append((x,y))
# Shuffle the list of points and we can choose a random point by
# simply "popping" it off the list.
random.shuffle(spl)
def makeWall(x,z):
mc.setBlock(x,ppos.y,z,block.STONE)
mc.setBlock(x,ppos.y+1,z,block.STONE)
mc.setBlock(x,ppos.y+2,z,block.STONE)
# Define the X and Y size of the maze including the outer walls.
# These values aren't checked but must be positive odd integers above 3.
mazeXSize = 35
mazeYSize = 35
# Set the maximum length of a wall.
maxWallLen = 1
# Find position of player and set base of maze 3 blocks lower.
ppos = mc.player.getPos()
ppos.y -= 3
# Clear an area for the maze.
for x in range(0,mazeXSize-1):
for z in range(mazeYSize-1):
mc.setBlock(ppos.x+x,ppos.y,ppos.z+z,block.STONE)
for y in range(1,5):
mc.setBlock(ppos.x+x,ppos.y+y,ppos.z+z,block.AIR)
# Create an empty maze.
initMaze(mazeXSize,mazeYSize)
# Loop until we have no more starting points (2's in the empty maze)
while filter(lambda x: 2 in x, maze):
# Get the X and Y values of the first point in our randomized list.
rx = spl[0][0]
ry = spl[0][1]
# Pop the first entry in the list, this deletes it and the rest move down.
spl.pop(0)
# Check to see if our chosen point is still a valid starting point.
ud = False
if maze[rx][ry] == 2:
ud = True
# Pick a random wall length up to the maximum.
rc = rand(0,maxWallLen)
# Pick a random direction.
rd = randDir()
fc = rd
loop = True
while loop:
# Look in each direction, if the current wall being built is stuck inside itself start again.
if maze[rx][ry-2] == 3 and maze[rx][ry+2] == 3 and maze[rx-2][ry] == 3 and maze[rx+2][ry] == 3:
#
# Code to clear maze area required
#
initMaze(mazeXSize,mazeYSize)
break
# Look ahead to see if we're okay to go in this direction.....
cx = rx + (rd[0]*2)
cy = ry + (rd[1]*2)
nc = maze[cx][cy]
if nc != 3:
for i in range(0,2):
maze[rx][ry] = 3
makeWall(ppos.x+rx,ppos.z+ry)
rx += rd[0]
ry += rd[1]
# .....if not choose another direction.
else: rd = randDir()
# If we hit an existing wall break out of the loop.
if nc == 1: loop = False
# Update our wall length counter. When this hits zero pick another direction.
# This also makes sure the new direction isn't the same as the current one.
rc -= 1
if rc <= 0:
rc = rand(0,maxWallLen)
dd = rd
de = (fc[0]*-1,fc[1]*-1)
while dd == rd or rd == de:
rd = randDir()
# The latest wall has been built so change all 3's (new wall) to 1's (existing wall)
if ud:
for x in range(0,mazeXSize):
for y in range(0,mazeYSize):
if maze[x][y] == 3: maze[x][y] = 1