[ create a new paste ] login | about

Link: http://codepad.org/DOXJGVG0    [ raw code | fork ]

Python, pasted on Sep 15:

import sys,os,re
from pymclevel import *

# inclusive range
def irange(a,b):
	return xrange(a,b+1)

# count the number of zeroes in the list
def count_zeroes(lst):
	return sum(1 for x in lst if x == 0)
# take the given coordinate list, and iterate the next level of the menger sponge fractal
def menger_sponge_3d(coordlist):
	for (x,y,z) in coordlist:
		for dx in irange(-1,1):
			for dy in irange(-1,1):
				for dz in irange(-1,1):
					if count_zeroes((dx,dy,dz))<=1:
						yield (x+dx,y+dy,z+dz)
# find the minimum and maximum coordinates of any list of coordinates (regardless of dimension)
# returns two tuples: mincoords and maxcoords.
def bounding_box(coordlist):
	# find the min and max x,y,z
	mincoords = list(coordlist[0])
	maxcoords = list(coordlist[0])
	for c in coordlist[1:]: # for each coordinate
		for (i,v) in enumerate(c): # for each dimension
			if v < mincoords[i]: mincoords[i] = v
			if v > maxcoords[i]: maxcoords[i] = v
	return (mincoords,maxcoords)
# render a list of x,y,z coordinates as a minecraft schematic file
def make_schematic_3d(filename,coordlist,block_type=1,verbose=True):
	(mincoords,maxcoords) = bounding_box(coordlist)
	(xr,yr,zr) = zip(mincoords,maxcoords) # xr = (mincoords[0],maxcoords[0]), etc.
	size = (xr[1]-xr[0]+1, zr[1]-zr[0]+1, yr[1]-yr[0]+1) #NB: USES X, Z, Y
	schematic = MCSchematic(shape=size, mats='Alpha')
	blocks = 0
	for (x,y,z) in coordlist:
		blocks += 1
		schematic.setBlockAt(x - xr[0], z - zr[0], y - yr[0], block_type) #NB: USES X, Z, Y
	if verbose: sys.stderr.write("Wrote %d blocks to %s, world size %s.\n" % (blocks,filename,size))
# generate menger sponges up to level 4 (the largest that fits in the world, since the size of a sponge is 3^level)
coordlist = [(0,0,0)]
for i in xrange(1,5):
	coordlist = list(menger_sponge_3d(coordlist))
	make_schematic_3d("menger3d-%d.schematic" % i,coordlist)

Create a new paste based on this one