Created
January 29, 2012 20:51
-
-
Save nevercast/1700580 to your computer and use it in GitHub Desktop.
Tree Populator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This file is part of Vanilla (http://www.spout.org/). | |
* | |
* Vanilla is licensed under the SpoutDev License Version 1. | |
* | |
* Vanilla is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Lesser General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* In addition, 180 days after any changes are published, you can use the | |
* software, incorporating those changes, under the terms of the MIT license, | |
* as described in the SpoutDev License Version 1. | |
* | |
* Vanilla is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public License, | |
* the MIT license and the SpoutDev license version 1 along with this program. | |
* If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public | |
* License and see <http://www.spout.org/SpoutDevLicenseV1.txt> for the full license, | |
* including the MIT license. | |
*/ | |
package org.spout.vanilla.generator.normal; | |
import java.util.Random; | |
import org.spout.api.generator.Populator; | |
import org.spout.api.geo.World; | |
import org.spout.api.geo.cuboid.Chunk; | |
import org.spout.api.geo.discrete.Point; | |
import org.spout.api.material.BlockMaterial; | |
import org.spout.api.material.MaterialData; | |
import org.spout.vanilla.VanillaBlocks; | |
public class TreePopulator implements Populator { | |
private Random random = new Random(); | |
private void generateTreeByType(Chunk c, Point point, BlockMaterial wood){ | |
/* Resolve to world position */ | |
point = point.add(c.getBase()); | |
int x = (int) point.getX(); | |
int y = (int) point.getY(); | |
int z = (int) point.getZ(); | |
World world = c.getWorld(); | |
BlockMaterial leaves = ( wood == VanillaBlocks.BIRCH_LOG ? VanillaBlocks.BIRCH_LEAVES : wood == VanillaBlocks.SPRUCE_LOG ? VanillaBlocks.SPRUCE_LEAVES : VanillaBlocks.LEAVES); | |
int treeBottom = y + 1; | |
world.setBlockMaterial(x,y,z,VanillaBlocks.DIRT, false, world); | |
/* Random trunk + 4 layer of leaves */ | |
int treeHeight = random.nextInt(3) + 4; | |
/* Iterate tree leave heights */ | |
for (int yy = (y - 3) + treeHeight; yy <= y + treeHeight; yy++) { | |
/* Calculate the width at this height */ | |
int yOff = yy - (y + treeHeight); | |
int treeLayerWidth = 1 - yOff / 2; | |
for (int xx = x - treeLayerWidth; xx <= x + treeLayerWidth; xx++) { | |
for (int zz = z - treeLayerWidth; zz <= z + treeLayerWidth; zz++) { | |
/* | |
* If the trees offset is on the most outset block, make it 50% chance. | |
* This makes the trees look more unique | |
*/ | |
if ((Math.abs(xx - x) != treeLayerWidth || Math.abs(zz - z) != treeLayerWidth || random.nextInt(2) != 0 && yOff != 0)){ | |
/* All conditions are met, set those leaves */ | |
world.setBlockMaterial(xx,yy,zz,leaves, false, world); | |
} | |
} | |
} | |
} | |
/* Create trunk */ | |
for(int yy = treeBottom; yy < treeBottom + treeHeight; yy++) | |
{ | |
world.setBlockMaterial(x,yy,z,wood, false, world); | |
} | |
} | |
public void generateNormalTree(Chunk c, Point point) { | |
generateTreeByType(c, point, VanillaBlocks.LOG); | |
} | |
public void generatePineTree(Chunk c, Point p) { | |
} | |
public void generateRedwoodTree(Chunk c, Point p) { | |
generateTreeByType(c, p, VanillaBlocks.SPRUCE_LOG); | |
} | |
public void generateBirchTree(Chunk c, Point point) { | |
generateTreeByType(c, point, VanillaBlocks.BIRCH_LOG); | |
} | |
@Override | |
public void populate(Chunk c) { | |
int treeCount = random.nextInt(10); /* Max of ten */ | |
for(int tree = 0; tree < treeCount; tree++){ | |
int x,y = -1,z; | |
/* Random horizontal plane */ | |
x = random.nextInt(15); | |
z = random.nextInt(15); | |
/* Heighest dirt or grass point */ | |
for(int yy = 15; yy >= 0; yy--){ | |
int id; | |
if((id = c.getBlockId(x,yy,z)) != 0 && id != 2 && id != 3) { | |
break; | |
}else if(id == 2 || id == 3){ | |
y = yy; | |
break; | |
} /* else air, continue on */ | |
} | |
if(y == -1) continue; | |
Point p= new Point(c.getWorld(), x, y, z); | |
/* Need biomes so we can have swamp trees, Jungle and forest(pines) */ | |
if(random.nextInt(10) == 0){ | |
generatePineTree(c, p); | |
}else if(random.nextInt(5) == 0){ | |
if(random.nextInt(5) == 0){ | |
generateBirchTree(c, p); | |
}else if(random.nextInt(3) == 0){ | |
generateRedwoodTree(c, p); | |
} | |
}else{ | |
generateNormalTree(c, p); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment