| 1 | package net.minecraft.world.level.levelgen.feature.trunkplacers;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.function.BiConsumer;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.world.level.WorldGenLevel;
|
| 14 | import net.minecraft.world.level.block.RotatedPillarBlock;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.levelgen.feature.TreeFeature;
|
| 17 | import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
|
| 18 |
|
| 19 | public class FancyTrunkPlacer extends TrunkPlacer {
|
| 20 | public static final MapCodec<FancyTrunkPlacer> CODEC = RecordCodecBuilder.mapCodec(i -> trunkPlacerParts(i).apply(i, FancyTrunkPlacer::new));
|
| 21 | private static final double TRUNK_HEIGHT_SCALE = 0.618;
|
| 22 | private static final double CLUSTER_DENSITY_MAGIC = 1.382;
|
| 23 | private static final double BRANCH_SLOPE = 0.381;
|
| 24 | private static final double BRANCH_LENGTH_MAGIC = 0.328;
|
| 25 |
|
| 26 | public FancyTrunkPlacer(final int baseHeight, final int heightRandA, final int heightRandB) {
|
| 27 | super(baseHeight, heightRandA, heightRandB);
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | protected TrunkPlacerType<?> type() {
|
| 32 | return TrunkPlacerType.FANCY_TRUNK_PLACER;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public List<FoliagePlacer.FoliageAttachment> placeTrunk(
|
| 37 | final WorldGenLevel level,
|
| 38 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 39 | final RandomSource random,
|
| 40 | final int treeHeight,
|
| 41 | final BlockPos origin,
|
| 42 | final TreeFeature tree
|
| 43 | ) {
|
| 44 | int assumedFoliageHeight = 5;
|
| 45 | int height = treeHeight + 2;
|
| 46 | int trunkHeight = Mth.floor(height * 0.618);
|
| 47 | placeBelowTrunkBlock(level, trunkSetter, random, origin.below(), tree);
|
| 48 | double foliageDensity = 1.0;
|
| 49 | int clustersPerY = Math.min(1, Mth.floor(1.382 + Math.pow(1.0 * height / 13.0, 2.0)));
|
| 50 | int trunkTop = origin.getY() + trunkHeight;
|
| 51 | int relativeY = height - 5;
|
| 52 | List<FancyTrunkPlacer.FoliageCoords> foliageCoords = Lists.newArrayList();
|
| 53 | foliageCoords.add(new FancyTrunkPlacer.FoliageCoords(origin.above(relativeY), trunkTop));
|
| 54 |
|
| 55 | for (; relativeY >= 0; relativeY--) {
|
| 56 | float treeShape = treeShape(height, relativeY);
|
| 57 | if (!(treeShape < 0.0F)) {
|
| 58 | for (int i = 0; i < clustersPerY; i++) {
|
| 59 | double widthScale = 1.0;
|
| 60 | double radius = 1.0 * treeShape * (random.nextFloat() + 0.328);
|
| 61 | double angle = random.nextFloat() * 2.0F * Math.PI;
|
| 62 | double x = radius * Math.sin(angle) + 0.5;
|
| 63 | double z = radius * Math.cos(angle) + 0.5;
|
| 64 | BlockPos checkStart = origin.offset(Mth.floor(x), relativeY - 1, Mth.floor(z));
|
| 65 | BlockPos checkEnd = checkStart.above(5);
|
| 66 | if (this.makeLimb(level, trunkSetter, random, checkStart, checkEnd, false, tree)) {
|
| 67 | int dx = origin.getX() - checkStart.getX();
|
| 68 | int dz = origin.getZ() - checkStart.getZ();
|
| 69 | double branchHeight = checkStart.getY() - Math.sqrt(dx * dx + dz * dz) * 0.381;
|
| 70 | int branchTop = branchHeight > trunkTop ? trunkTop : (int)branchHeight;
|
| 71 | BlockPos checkBranchBase = new BlockPos(origin.getX(), branchTop, origin.getZ());
|
| 72 | if (this.makeLimb(level, trunkSetter, random, checkBranchBase, checkStart, false, tree)) {
|
| 73 | foliageCoords.add(new FancyTrunkPlacer.FoliageCoords(checkStart, checkBranchBase.getY()));
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | this.makeLimb(level, trunkSetter, random, origin, origin.above(trunkHeight), true, tree);
|
| 81 | this.makeBranches(level, trunkSetter, random, height, origin, foliageCoords, tree);
|
| 82 | List<FoliagePlacer.FoliageAttachment> attachments = Lists.newArrayList();
|
| 83 |
|
| 84 | for (FancyTrunkPlacer.FoliageCoords foliageCoord : foliageCoords) {
|
| 85 | if (this.trimBranches(height, foliageCoord.getBranchBase() - origin.getY())) {
|
| 86 | attachments.add(foliageCoord.attachment);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | return attachments;
|
| 91 | }
|
| 92 |
|
| 93 | private boolean makeLimb(
|
| 94 | final WorldGenLevel level,
|
| 95 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 96 | final RandomSource random,
|
| 97 | final BlockPos startPos,
|
| 98 | final BlockPos endPos,
|
| 99 | final boolean doPlace,
|
| 100 | final TreeFeature tree
|
| 101 | ) {
|
| 102 | if (!doPlace && Objects.equals(startPos, endPos)) {
|
| 103 | return true;
|
| 104 | }
|
| 105 |
|
| 106 | BlockPos delta = endPos.offset(-startPos.getX(), -startPos.getY(), -startPos.getZ());
|
| 107 | int steps = this.getSteps(delta);
|
| 108 | float dx = (float)delta.getX() / steps;
|
| 109 | float dy = (float)delta.getY() / steps;
|
| 110 | float dz = (float)delta.getZ() / steps;
|
| 111 |
|
| 112 | for (int i = 0; i <= steps; i++) {
|
| 113 | BlockPos blockPos = startPos.offset(Mth.floor(0.5F + i * dx), Mth.floor(0.5F + i * dy), Mth.floor(0.5F + i * dz));
|
| 114 | if (doPlace) {
|
| 115 | this.placeLog(level, trunkSetter, random, blockPos, tree, state -> state.trySetValue(RotatedPillarBlock.AXIS, this.getLogAxis(startPos, blockPos)));
|
| 116 | } else if (!this.isFree(level, blockPos)) {
|
| 117 | return false;
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | return true;
|
| 122 | }
|
| 123 |
|
| 124 | private int getSteps(final BlockPos pos) {
|
| 125 | int absX = Mth.abs(pos.getX());
|
| 126 | int absY = Mth.abs(pos.getY());
|
| 127 | int absZ = Mth.abs(pos.getZ());
|
| 128 | return Math.max(absX, Math.max(absY, absZ));
|
| 129 | }
|
| 130 |
|
| 131 | private Direction.Axis getLogAxis(final BlockPos startPos, final BlockPos blockPos) {
|
| 132 | Direction.Axis axis = Direction.Axis.Y;
|
| 133 | int xdiff = Math.abs(blockPos.getX() - startPos.getX());
|
| 134 | int zdiff = Math.abs(blockPos.getZ() - startPos.getZ());
|
| 135 | int maxdiff = Math.max(xdiff, zdiff);
|
| 136 | if (maxdiff > 0) {
|
| 137 | if (xdiff == maxdiff) {
|
| 138 | axis = Direction.Axis.X;
|
| 139 | } else {
|
| 140 | axis = Direction.Axis.Z;
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | return axis;
|
| 145 | }
|
| 146 |
|
| 147 | private boolean trimBranches(final int height, final int localY) {
|
| 148 | return localY >= height * 0.2;
|
| 149 | }
|
| 150 |
|
| 151 | private void makeBranches(
|
| 152 | final WorldGenLevel level,
|
| 153 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 154 | final RandomSource random,
|
| 155 | final int height,
|
| 156 | final BlockPos origin,
|
| 157 | final List<FancyTrunkPlacer.FoliageCoords> foliageCoords,
|
| 158 | final TreeFeature tree
|
| 159 | ) {
|
| 160 | for (FancyTrunkPlacer.FoliageCoords endCoord : foliageCoords) {
|
| 161 | int branchBase = endCoord.getBranchBase();
|
| 162 | BlockPos baseCoord = new BlockPos(origin.getX(), branchBase, origin.getZ());
|
| 163 | if (!baseCoord.equals(endCoord.attachment.pos()) && this.trimBranches(height, branchBase - origin.getY())) {
|
| 164 | this.makeLimb(level, trunkSetter, random, baseCoord, endCoord.attachment.pos(), true, tree);
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | private static float treeShape(final int height, final int y) {
|
| 170 | if (y < height * 0.3F) {
|
| 171 | return -1.0F;
|
| 172 | }
|
| 173 |
|
| 174 | float radius = height / 2.0F;
|
| 175 | float adjacent = radius - y;
|
| 176 | float distance = Mth.sqrt(radius * radius - adjacent * adjacent);
|
| 177 | if (adjacent == 0.0F) {
|
| 178 | distance = radius;
|
| 179 | } else if (Math.abs(adjacent) >= radius) {
|
| 180 | return 0.0F;
|
| 181 | }
|
| 182 |
|
| 183 | return distance * 0.5F;
|
| 184 | }
|
| 185 |
|
| 186 | private static class FoliageCoords {
|
| 187 | private final FoliagePlacer.FoliageAttachment attachment;
|
| 188 | private final int branchBase;
|
| 189 |
|
| 190 | public FoliageCoords(final BlockPos pos, final int branchBase) {
|
| 191 | this.attachment = new FoliagePlacer.FoliageAttachment(pos, 0, false);
|
| 192 | this.branchBase = branchBase;
|
| 193 | }
|
| 194 |
|
| 195 | public int getBranchBase() {
|
| 196 | return this.branchBase;
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|