Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. #treegen.py
  2. #Creates generic trees in the selected area.
  3.  
  4. import random
  5. from pymclevel import MCSchematic
  6.  
  7. #Use the inputs variable to tell MCEdit we want a Density option. See filterdemo.py for more info.
  8. inputs = (
  9.   ("Density", (10, 1, 100)),  #Integer input, default: 10, min: 1, max: 100.
  10. )
  11.  
  12. #The perform function is where we receive a reference to the level object, a BoundingBox object for the current selection, and an options dict holding the options the user specified.
  13.  
  14. def perform(level, box, options):
  15.     density = options["Density"]
  16.     treeChance = 0.05 * float(density) / 100.0 #Scale Density input to the range 0.0% - 5.0%
  17.  
  18.     treeSchematic = makeTreeSchematic()
  19.  
  20.     #Visit each column and check the top block for dirt or grass
  21.     #Adjust the min and max inward because the tree is 5x5
  22.     for x in xrange(box.minx+2, box.maxx-2):
  23.       for z in xrange(box.minz+2, box.maxz-2):
  24.  
  25.         #Loop through the column from top to bottom until we find the top block
  26.         for y in reversed(xrange(box.miny, box.maxy)):
  27.           block = level.blockAt(x, y, z)
  28.  
  29.           if block != 0: #found the first non-air block
  30.             if block == 2 or block == 3: #dirt or grass
  31.               if random.random() < treeChance:
  32.                 #copy the tree from the schematic
  33.                 level.copyBlocksFrom(treeSchematic, treeSchematic.bounds, (x-2, y+1, z-2))
  34.  
  35.             break; #next column
  36.  
  37. def makeTreeSchematic():
  38.     schem = MCSchematic(shape = (5,6,5)) #shape is x,y,z
  39.     #Here I use array slicing to fill out the different parts of the tree.
  40.  
  41.     #Leaves, crown
  42.     schem.Blocks[1:4,1:4,5] = [
  43.       [0, 18, 0],
  44.       [18, 18, 18],
  45.       [0, 18, 0],
  46.     ]
  47.     schem.Blocks[1:4,1:4,4] = 18
  48.  
  49.     #Leaves, midsection
  50.     schem.Blocks[:,:,2:4] = 18
  51.  
  52.     #Trunk
  53.     schem.Blocks[2,2,0:5] = 17
  54.  
  55.     return schem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement