| 1 | package net.minecraft.world.gen.feature;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Random;
|
| 6 | import net.minecraft.block.Block;
|
| 7 | import net.minecraft.block.BlockLeaves;
|
| 8 | import net.minecraft.block.BlockLog;
|
| 9 | import net.minecraft.block.material.Material;
|
| 10 | import net.minecraft.block.state.IBlockState;
|
| 11 | import net.minecraft.init.Blocks;
|
| 12 | import net.minecraft.util.BlockPos;
|
| 13 | import net.minecraft.util.MathHelper;
|
| 14 | import net.minecraft.world.World;
|
| 15 |
|
| 16 | public class WorldGenBigTree extends WorldGenAbstractTree
|
| 17 | {
|
| 18 | private Random rand;
|
| 19 | private World world;
|
| 20 | private BlockPos basePos = BlockPos.ORIGIN;
|
| 21 | int heightLimit;
|
| 22 | int height;
|
| 23 | double heightAttenuation = 0.618D;
|
| 24 | double branchSlope = 0.381D;
|
| 25 | double scaleWidth = 1.0D;
|
| 26 | double leafDensity = 1.0D;
|
| 27 | int trunkSize = 1;
|
| 28 | int heightLimitLimit = 12;
|
| 29 |
|
| 30 | /**
|
| 31 | * Sets the distance limit for how far away the generator will populate leaves from the base leaf node.
|
| 32 | */
|
| 33 | int leafDistanceLimit = 4;
|
| 34 | List<WorldGenBigTree.FoliageCoordinates> field_175948_j;
|
| 35 |
|
| 36 | public WorldGenBigTree(boolean p_i2008_1_)
|
| 37 | {
|
| 38 | super(p_i2008_1_);
|
| 39 | }
|
| 40 |
|
| 41 | /**
|
| 42 | * Generates a list of leaf nodes for the tree, to be populated by generateLeaves.
|
| 43 | */
|
| 44 | void generateLeafNodeList()
|
| 45 | {
|
| 46 | this.height = (int)((double)this.heightLimit * this.heightAttenuation);
|
| 47 |
|
| 48 | if (this.height >= this.heightLimit)
|
| 49 | {
|
| 50 | this.height = this.heightLimit - 1;
|
| 51 | }
|
| 52 |
|
| 53 | int i = (int)(1.382D + Math.pow(this.leafDensity * (double)this.heightLimit / 13.0D, 2.0D));
|
| 54 |
|
| 55 | if (i < 1)
|
| 56 | {
|
| 57 | i = 1;
|
| 58 | }
|
| 59 |
|
| 60 | int j = this.basePos.getY() + this.height;
|
| 61 | int k = this.heightLimit - this.leafDistanceLimit;
|
| 62 | this.field_175948_j = Lists.<WorldGenBigTree.FoliageCoordinates>newArrayList();
|
| 63 | this.field_175948_j.add(new WorldGenBigTree.FoliageCoordinates(this.basePos.up(k), j));
|
| 64 |
|
| 65 | for (; k >= 0; --k)
|
| 66 | {
|
| 67 | float f = this.layerSize(k);
|
| 68 |
|
| 69 | if (f >= 0.0F)
|
| 70 | {
|
| 71 | for (int l = 0; l < i; ++l)
|
| 72 | {
|
| 73 | double d0 = this.scaleWidth * (double)f * ((double)this.rand.nextFloat() + 0.328D);
|
| 74 | double d1 = (double)(this.rand.nextFloat() * 2.0F) * Math.PI;
|
| 75 | double d2 = d0 * Math.sin(d1) + 0.5D;
|
| 76 | double d3 = d0 * Math.cos(d1) + 0.5D;
|
| 77 | BlockPos blockpos = this.basePos.add(d2, (double)(k - 1), d3);
|
| 78 | BlockPos blockpos1 = blockpos.up(this.leafDistanceLimit);
|
| 79 |
|
| 80 | if (this.checkBlockLine(blockpos, blockpos1) == -1)
|
| 81 | {
|
| 82 | int i1 = this.basePos.getX() - blockpos.getX();
|
| 83 | int j1 = this.basePos.getZ() - blockpos.getZ();
|
| 84 | double d4 = (double)blockpos.getY() - Math.sqrt((double)(i1 * i1 + j1 * j1)) * this.branchSlope;
|
| 85 | int k1 = d4 > (double)j ? j : (int)d4;
|
| 86 | BlockPos blockpos2 = new BlockPos(this.basePos.getX(), k1, this.basePos.getZ());
|
| 87 |
|
| 88 | if (this.checkBlockLine(blockpos2, blockpos) == -1)
|
| 89 | {
|
| 90 | this.field_175948_j.add(new WorldGenBigTree.FoliageCoordinates(blockpos, blockpos2.getY()));
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | void func_181631_a(BlockPos p_181631_1_, float p_181631_2_, IBlockState p_181631_3_)
|
| 99 | {
|
| 100 | int i = (int)((double)p_181631_2_ + 0.618D);
|
| 101 |
|
| 102 | for (int j = -i; j <= i; ++j)
|
| 103 | {
|
| 104 | for (int k = -i; k <= i; ++k)
|
| 105 | {
|
| 106 | if (Math.pow((double)Math.abs(j) + 0.5D, 2.0D) + Math.pow((double)Math.abs(k) + 0.5D, 2.0D) <= (double)(p_181631_2_ * p_181631_2_))
|
| 107 | {
|
| 108 | BlockPos blockpos = p_181631_1_.add(j, 0, k);
|
| 109 | Material material = this.world.getBlockState(blockpos).getBlock().getMaterial();
|
| 110 |
|
| 111 | if (material == Material.air || material == Material.leaves)
|
| 112 | {
|
| 113 | this.setBlockAndNotifyAdequately(this.world, blockpos, p_181631_3_);
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | /**
|
| 121 | * Gets the rough size of a layer of the tree.
|
| 122 | */
|
| 123 | float layerSize(int p_76490_1_)
|
| 124 | {
|
| 125 | if ((float)p_76490_1_ < (float)this.heightLimit * 0.3F)
|
| 126 | {
|
| 127 | return -1.0F;
|
| 128 | }
|
| 129 | else
|
| 130 | {
|
| 131 | float f = (float)this.heightLimit / 2.0F;
|
| 132 | float f1 = f - (float)p_76490_1_;
|
| 133 | float f2 = MathHelper.sqrt_float(f * f - f1 * f1);
|
| 134 |
|
| 135 | if (f1 == 0.0F)
|
| 136 | {
|
| 137 | f2 = f;
|
| 138 | }
|
| 139 | else if (Math.abs(f1) >= f)
|
| 140 | {
|
| 141 | return 0.0F;
|
| 142 | }
|
| 143 |
|
| 144 | return f2 * 0.5F;
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | float leafSize(int p_76495_1_)
|
| 149 | {
|
| 150 | return p_76495_1_ >= 0 && p_76495_1_ < this.leafDistanceLimit ? (p_76495_1_ != 0 && p_76495_1_ != this.leafDistanceLimit - 1 ? 3.0F : 2.0F) : -1.0F;
|
| 151 | }
|
| 152 |
|
| 153 | /**
|
| 154 | * Generates the leaves surrounding an individual entry in the leafNodes list.
|
| 155 | */
|
| 156 | void generateLeafNode(BlockPos pos)
|
| 157 | {
|
| 158 | for (int i = 0; i < this.leafDistanceLimit; ++i)
|
| 159 | {
|
| 160 | this.func_181631_a(pos.up(i), this.leafSize(i), Blocks.leaves.getDefaultState().withProperty(BlockLeaves.CHECK_DECAY, Boolean.valueOf(false)));
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | void func_175937_a(BlockPos p_175937_1_, BlockPos p_175937_2_, Block p_175937_3_)
|
| 165 | {
|
| 166 | BlockPos blockpos = p_175937_2_.add(-p_175937_1_.getX(), -p_175937_1_.getY(), -p_175937_1_.getZ());
|
| 167 | int i = this.getGreatestDistance(blockpos);
|
| 168 | float f = (float)blockpos.getX() / (float)i;
|
| 169 | float f1 = (float)blockpos.getY() / (float)i;
|
| 170 | float f2 = (float)blockpos.getZ() / (float)i;
|
| 171 |
|
| 172 | for (int j = 0; j <= i; ++j)
|
| 173 | {
|
| 174 | BlockPos blockpos1 = p_175937_1_.add((double)(0.5F + (float)j * f), (double)(0.5F + (float)j * f1), (double)(0.5F + (float)j * f2));
|
| 175 | BlockLog.EnumAxis blocklog$enumaxis = this.func_175938_b(p_175937_1_, blockpos1);
|
| 176 | this.setBlockAndNotifyAdequately(this.world, blockpos1, p_175937_3_.getDefaultState().withProperty(BlockLog.LOG_AXIS, blocklog$enumaxis));
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | /**
|
| 181 | * Returns the absolute greatest distance in the BlockPos object.
|
| 182 | */
|
| 183 | private int getGreatestDistance(BlockPos posIn)
|
| 184 | {
|
| 185 | int i = MathHelper.abs_int(posIn.getX());
|
| 186 | int j = MathHelper.abs_int(posIn.getY());
|
| 187 | int k = MathHelper.abs_int(posIn.getZ());
|
| 188 | return k > i && k > j ? k : (j > i ? j : i);
|
| 189 | }
|
| 190 |
|
| 191 | private BlockLog.EnumAxis func_175938_b(BlockPos p_175938_1_, BlockPos p_175938_2_)
|
| 192 | {
|
| 193 | BlockLog.EnumAxis blocklog$enumaxis = BlockLog.EnumAxis.Y;
|
| 194 | int i = Math.abs(p_175938_2_.getX() - p_175938_1_.getX());
|
| 195 | int j = Math.abs(p_175938_2_.getZ() - p_175938_1_.getZ());
|
| 196 | int k = Math.max(i, j);
|
| 197 |
|
| 198 | if (k > 0)
|
| 199 | {
|
| 200 | if (i == k)
|
| 201 | {
|
| 202 | blocklog$enumaxis = BlockLog.EnumAxis.X;
|
| 203 | }
|
| 204 | else if (j == k)
|
| 205 | {
|
| 206 | blocklog$enumaxis = BlockLog.EnumAxis.Z;
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | return blocklog$enumaxis;
|
| 211 | }
|
| 212 |
|
| 213 | /**
|
| 214 | * Generates the leaf portion of the tree as specified by the leafNodes list.
|
| 215 | */
|
| 216 | void generateLeaves()
|
| 217 | {
|
| 218 | for (WorldGenBigTree.FoliageCoordinates worldgenbigtree$foliagecoordinates : this.field_175948_j)
|
| 219 | {
|
| 220 | this.generateLeafNode(worldgenbigtree$foliagecoordinates);
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | /**
|
| 225 | * Indicates whether or not a leaf node requires additional wood to be added to preserve integrity.
|
| 226 | */
|
| 227 | boolean leafNodeNeedsBase(int p_76493_1_)
|
| 228 | {
|
| 229 | return (double)p_76493_1_ >= (double)this.heightLimit * 0.2D;
|
| 230 | }
|
| 231 |
|
| 232 | /**
|
| 233 | * Places the trunk for the big tree that is being generated. Able to generate double-sized trunks by changing a
|
| 234 | * field that is always 1 to 2.
|
| 235 | */
|
| 236 | void generateTrunk()
|
| 237 | {
|
| 238 | BlockPos blockpos = this.basePos;
|
| 239 | BlockPos blockpos1 = this.basePos.up(this.height);
|
| 240 | Block block = Blocks.log;
|
| 241 | this.func_175937_a(blockpos, blockpos1, block);
|
| 242 |
|
| 243 | if (this.trunkSize == 2)
|
| 244 | {
|
| 245 | this.func_175937_a(blockpos.east(), blockpos1.east(), block);
|
| 246 | this.func_175937_a(blockpos.east().south(), blockpos1.east().south(), block);
|
| 247 | this.func_175937_a(blockpos.south(), blockpos1.south(), block);
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | /**
|
| 252 | * Generates additional wood blocks to fill out the bases of different leaf nodes that would otherwise degrade.
|
| 253 | */
|
| 254 | void generateLeafNodeBases()
|
| 255 | {
|
| 256 | for (WorldGenBigTree.FoliageCoordinates worldgenbigtree$foliagecoordinates : this.field_175948_j)
|
| 257 | {
|
| 258 | int i = worldgenbigtree$foliagecoordinates.func_177999_q();
|
| 259 | BlockPos blockpos = new BlockPos(this.basePos.getX(), i, this.basePos.getZ());
|
| 260 |
|
| 261 | if (!blockpos.equals(worldgenbigtree$foliagecoordinates) && this.leafNodeNeedsBase(i - this.basePos.getY()))
|
| 262 | {
|
| 263 | this.func_175937_a(blockpos, worldgenbigtree$foliagecoordinates, Blocks.log);
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | /**
|
| 269 | * Checks a line of blocks in the world from the first coordinate to triplet to the second, returning the distance
|
| 270 | * (in blocks) before a non-air, non-leaf block is encountered and/or the end is encountered.
|
| 271 | */
|
| 272 | int checkBlockLine(BlockPos posOne, BlockPos posTwo)
|
| 273 | {
|
| 274 | BlockPos blockpos = posTwo.add(-posOne.getX(), -posOne.getY(), -posOne.getZ());
|
| 275 | int i = this.getGreatestDistance(blockpos);
|
| 276 | float f = (float)blockpos.getX() / (float)i;
|
| 277 | float f1 = (float)blockpos.getY() / (float)i;
|
| 278 | float f2 = (float)blockpos.getZ() / (float)i;
|
| 279 |
|
| 280 | if (i == 0)
|
| 281 | {
|
| 282 | return -1;
|
| 283 | }
|
| 284 | else
|
| 285 | {
|
| 286 | for (int j = 0; j <= i; ++j)
|
| 287 | {
|
| 288 | BlockPos blockpos1 = posOne.add((double)(0.5F + (float)j * f), (double)(0.5F + (float)j * f1), (double)(0.5F + (float)j * f2));
|
| 289 |
|
| 290 | if (!this.func_150523_a(this.world.getBlockState(blockpos1).getBlock()))
|
| 291 | {
|
| 292 | return j;
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | return -1;
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | public void func_175904_e()
|
| 301 | {
|
| 302 | this.leafDistanceLimit = 5;
|
| 303 | }
|
| 304 |
|
| 305 | public boolean generate(World worldIn, Random rand, BlockPos position)
|
| 306 | {
|
| 307 | this.world = worldIn;
|
| 308 | this.basePos = position;
|
| 309 | this.rand = new Random(rand.nextLong());
|
| 310 |
|
| 311 | if (this.heightLimit == 0)
|
| 312 | {
|
| 313 | this.heightLimit = 5 + this.rand.nextInt(this.heightLimitLimit);
|
| 314 | }
|
| 315 |
|
| 316 | if (!this.validTreeLocation())
|
| 317 | {
|
| 318 | return false;
|
| 319 | }
|
| 320 | else
|
| 321 | {
|
| 322 | this.generateLeafNodeList();
|
| 323 | this.generateLeaves();
|
| 324 | this.generateTrunk();
|
| 325 | this.generateLeafNodeBases();
|
| 326 | return true;
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | /**
|
| 331 | * Returns a boolean indicating whether or not the current location for the tree, spanning basePos to to the height
|
| 332 | * limit, is valid.
|
| 333 | */
|
| 334 | private boolean validTreeLocation()
|
| 335 | {
|
| 336 | Block block = this.world.getBlockState(this.basePos.down()).getBlock();
|
| 337 |
|
| 338 | if (block != Blocks.dirt && block != Blocks.grass && block != Blocks.farmland)
|
| 339 | {
|
| 340 | return false;
|
| 341 | }
|
| 342 | else
|
| 343 | {
|
| 344 | int i = this.checkBlockLine(this.basePos, this.basePos.up(this.heightLimit - 1));
|
| 345 |
|
| 346 | if (i == -1)
|
| 347 | {
|
| 348 | return true;
|
| 349 | }
|
| 350 | else if (i < 6)
|
| 351 | {
|
| 352 | return false;
|
| 353 | }
|
| 354 | else
|
| 355 | {
|
| 356 | this.heightLimit = i;
|
| 357 | return true;
|
| 358 | }
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | static class FoliageCoordinates extends BlockPos
|
| 363 | {
|
| 364 | private final int field_178000_b;
|
| 365 |
|
| 366 | public FoliageCoordinates(BlockPos p_i45635_1_, int p_i45635_2_)
|
| 367 | {
|
| 368 | super(p_i45635_1_.getX(), p_i45635_1_.getY(), p_i45635_1_.getZ());
|
| 369 | this.field_178000_b = p_i45635_2_;
|
| 370 | }
|
| 371 |
|
| 372 | public int func_177999_q()
|
| 373 | {
|
| 374 | return this.field_178000_b;
|
| 375 | }
|
| 376 | }
|
| 377 | }
|
| 378 |
|