Skip to content

Commit

Permalink
my maze
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed May 5, 2018
1 parent 599ab7b commit baec83f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 146 deletions.
145 changes: 0 additions & 145 deletions mcpipy/davef21370_maze.py

This file was deleted.

107 changes: 107 additions & 0 deletions mcpipy/maze.py
@@ -0,0 +1,107 @@
from mine import *
from sys import argv
from random import randint

DIRS = ((1,0),(0,1),(-1,0),(0,-1))

def generateMaze(xSize, ySize, start=(0,0), dirs=DIRS, inside=None):
if inside == None:
inside = lambda xy : 0 <= xy[0] < xSize and 0 <= xy[1] < ySize

def move(pos, dir):
return (pos[0]+dirs[dir][0],pos[1]+dirs[dir][1])

nDirs = len(dirs)

def findDir(v):
for i in range(nDirs):
if dirs[i] == v:
return i
raise Exception("Mismatched direction")

revDir = tuple( findDir((-dirs[i][0],-dirs[i][1])) for i in range(nDirs) )

visited = tuple([False for j in range(ySize)] for i in range(xSize))
walls = tuple(tuple( [True for j in range(nDirs)] for j in range(ySize) ) for i in range(xSize))
pos = start

def getVisited(pos):
return not inside(pos) or visited[pos[0]][pos[1]]

stack = []

while True:
visited[pos[0]][pos[1]] = True
nUnvisited = 0
for dir in range(nDirs):
if not getVisited(move(pos,dir)):
nUnvisited += 1

if nUnvisited == 0:
if stack:
pos = stack.pop()
continue
else:
break

n = randint(0,nUnvisited-1)
dir = 0
while True:
if not getVisited(move(pos,dir)):
if n == 0:
break
n -= 1
dir += 1

walls[pos[0]][pos[1]][dir] = False
pos = move(pos,dir)
walls[pos[0]][pos[1]][revDir[dir]] = False

stack.append(pos)

return walls

xSize = 40
ySize = 40
b = block.STONE

if len(argv)>1:
xSize = int(argv[1])
ySize = xSize
if len(argv)>2:
b = Block.byName(argv[2])

mc = Minecraft()

walls = generateMaze(xSize,ySize)
pos = mc.player.getTilePos()

my = pos.y

for x in range(xSize):
for y in range(ySize):
mx = 2*x + pos.x
mz = 2*y + pos.z
def set(d1,d2):
mc.setBlock(mx+d1,my,mz+d2,b)
mc.setBlock(mx+d1,my+1,mz+d2,b)

for dir in range(len(DIRS)):
if walls[x][y][dir]:
set(DIRS[dir][0],DIRS[dir][1])
set(1,1)
set(1,-1)
set(-1,1)
set(-1,-1)

mc.setBlock(pos.x-1,pos.y,pos.z,block.AIR)
mc.setBlock(pos.x-1,pos.y+1,pos.z,block.AIR)
mc.setBlock(pos.x+2*(xSize-1),pos.y-1,pos.z+2*(ySize-1),block.GOLD_BLOCK)
mc.setBlock(pos.x+2*(xSize-1)+1,pos.y-1,pos.z+2*(ySize-1),block.GOLD_BLOCK)

def makeSign(line1="",line2="",line3="",line4=""):
return Block(63, 15, '{Text4:"{\\"text\\":\\"%s\\"}",Text3:"{\\"text\\":\\"%s\\"}",Text2:"{\\"text\\":\\"%s\\"}",id:"minecraft:sign",Text1:"{\\"text\\":\\"%s\\"}"}'
% (line4,line3,line2,line1))

mc.setBlockWithNBT(pos.x+2*(xSize-1)+1,pos.y,pos.z+2*(ySize-1),makeSign('EXIT'))
mc.setBlock(pos.x+2*(xSize-1)+1,pos.y+1,pos.z+2*(ySize-1),block.AIR)
2 changes: 1 addition & 1 deletion mcpipy/sign.py
@@ -1,3 +1,3 @@
from mine import *
mc = Minecraft()
mc.conn.send("world.setBlocks",0,0,0,5,5,5,63,1,"{id:\"Sign\",Text1:\"My sign\"}")
mc.conn.send("world.setBlocks",0,0,0,5,5,5,Block(63, 15, '{Text4:"{\\"text\\":\\"\\"}",Text3:"{\\"text\\":\\"\\"}",Text2:"{\\"text\\":\\"\\"}",id:"minecraft:sign",Text1:"{\\"text\\":\\"hello\\"}"}'))

0 comments on commit baec83f

Please sign in to comment.