Skip to content

Commit

Permalink
i suck
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Jan 27, 2025
1 parent 9d490af commit 583f305
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package net.frozenblock.wilderwild.worldgen.impl.foliage;

import com.mojang.datafixers.Products;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.frozenblock.wilderwild.registry.WWFeatures;
Expand All @@ -29,15 +30,23 @@
import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacerType;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class MapleFoliagePlacer extends FoliagePlacer {
public static final MapCodec<MapleFoliagePlacer> CODEC = RecordCodecBuilder.mapCodec(
(instance) -> foliagePlacerParts(instance)
.and(IntProvider.codec(0, 24).fieldOf("length").forGetter((placer) -> placer.length))
.apply(instance, MapleFoliagePlacer::new)
(instance) -> mapleFoliagePlacerParts(instance).apply(instance, MapleFoliagePlacer::new)
);
private final IntProvider length;

@Contract("_ -> new")
protected static <P extends MapleFoliagePlacer> Products.@NotNull P3<RecordCodecBuilder.Mu<P>, IntProvider, IntProvider, IntProvider> mapleFoliagePlacerParts(
RecordCodecBuilder.Instance<P> instance
) {
return foliagePlacerParts(instance)
.and(IntProvider.codec(0, 24).fieldOf("length").forGetter((placer) -> placer.length));
}

protected final IntProvider length;

public MapleFoliagePlacer(IntProvider radius, IntProvider offset, IntProvider foliageHeight) {
super(radius, offset);
Expand Down Expand Up @@ -85,8 +94,7 @@ protected void placeLeavesInCircle(
int trunkHeight
) {
BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
Vec3 center = centerPos.getCenter();
double radius = providedRadius + ((random.nextDouble() - 0.5D) * 0.4D);
double radius = providedRadius + ((random.nextDouble() - this.getRandomRadiusShrink()) * 0.4D);
double increment = radius * 0.05D;

for (double j = -radius; j <= radius; j += increment) {
Expand All @@ -99,6 +107,10 @@ protected void placeLeavesInCircle(
}
}

protected double getRandomRadiusShrink() {
return -0.5D;
}

protected boolean shouldSkipMapleLocationSigned(
double dx, double dz, double radius, boolean giantTrunk, int totalHeight, int currentHeight, int trunkHeight
) {
Expand All @@ -119,12 +131,11 @@ protected boolean shouldSkipMapleLocation(
double xDistance, double zDistance, double radius, int totalHeight, int currentHeight, int trunkHeight
) {
double mapleFunction = this.getMapleFoliageFunction(totalHeight, currentHeight, radius, currentHeight <= trunkHeight);
Vec3 vec3 = new Vec3(xDistance, 0, zDistance);
double distance = Vec3.ZERO.distanceTo(vec3);
double distance = new Vec3(xDistance, 0, zDistance).horizontalDistance();
return distance > mapleFunction && distance > 0.4D;
}

private double getMapleFoliageFunction(
protected double getMapleFoliageFunction(
double totalHeight,
double height,
double radius,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,17 @@
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.frozenblock.wilderwild.registry.WWFeatures;
import net.minecraft.core.BlockPos;
import net.minecraft.util.RandomSource;
import net.minecraft.util.valueproviders.IntProvider;
import net.minecraft.world.level.LevelSimulatedReader;
import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration;
import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacerType;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

public class RoundMapleFoliagePlacer extends FoliagePlacer {
public class RoundMapleFoliagePlacer extends MapleFoliagePlacer {
public static final MapCodec<RoundMapleFoliagePlacer> CODEC = RecordCodecBuilder.mapCodec(
(instance) -> foliagePlacerParts(instance)
.and(IntProvider.codec(0, 24).fieldOf("length").forGetter((placer) -> placer.length))
.apply(instance, RoundMapleFoliagePlacer::new)
(instance) -> mapleFoliagePlacerParts(instance).apply(instance, RoundMapleFoliagePlacer::new)
);
private final IntProvider length;

public RoundMapleFoliagePlacer(IntProvider radius, IntProvider offset, IntProvider foliageHeight) {
super(radius, offset);
this.length = foliageHeight;
super(radius, offset, foliageHeight);
}

@Override
Expand All @@ -50,87 +40,18 @@ public RoundMapleFoliagePlacer(IntProvider radius, IntProvider offset, IntProvid
}

@Override
protected void createFoliage(
LevelSimulatedReader world,
FoliageSetter placer,
@NotNull RandomSource random,
TreeConfiguration config,
int trunkHeight,
@NotNull FoliageAttachment node,
int foliageHeight,
int radius,
int offset
) {
BlockPos blockPos = node.pos();
int totalHeight = offset + foliageHeight;
int currentHeight = totalHeight;

for (int l = offset; l >= -foliageHeight; l--) {
this.placeLeavesInCircle(world, placer, random, config, blockPos, radius, l, node.doubleTrunk(), totalHeight, currentHeight, foliageHeight);
currentHeight -= 1;
}
}

protected void placeLeavesInCircle(
LevelSimulatedReader world,
FoliageSetter placer,
@NotNull RandomSource random,
TreeConfiguration config,
BlockPos centerPos,
int providedRadius,
int y,
boolean giantTrunk,
int totalHeight,
int currentHeight,
int trunkHeight
) {
BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
double radius = providedRadius + ((random.nextDouble() - 0.3D) * 0.5D);
double increment = radius * 0.05D;

for (double j = -radius; j <= radius; j += increment) {
for (double k = -radius; k <= radius; k += increment) {
if (!this.shouldSkipMapleLocationSigned(j, k, radius, giantTrunk, totalHeight, currentHeight, trunkHeight)) {
mutableBlockPos.setWithOffset(centerPos, (int) j, y, (int) k);
tryPlaceLeaf(world, placer, random, config, mutableBlockPos);
}
}
}
protected double getRandomRadiusShrink() {
return -0.3D;
}

protected boolean shouldSkipMapleLocationSigned(
double dx, double dz, double radius, boolean giantTrunk, int totalHeight, int currentHeight, int trunkHeight
) {
double i;
double j;
if (giantTrunk) {
i = Math.min(Math.abs(dx), Math.abs(dx - 1));
j = Math.min(Math.abs(dz), Math.abs(dz - 1));
} else {
i = Math.abs(dx);
j = Math.abs(dz);
}

return this.shouldSkipMapleLocation(i, j, radius, totalHeight, currentHeight, trunkHeight);
}

protected boolean shouldSkipMapleLocation(
double xDistance, double zDistance, double radius, int totalHeight, int currentHeight, int trunkHeight
) {
double mapleFunction = this.getMapleFoliageFunction(totalHeight, currentHeight, radius, currentHeight <= trunkHeight);
Vec3 vec3 = new Vec3(xDistance, 0, zDistance);
double distance = Vec3.ZERO.distanceTo(vec3);
return distance > mapleFunction && distance > 0.4D;
}

private double getMapleFoliageFunction(
@Override
protected double getMapleFoliageFunction(
double totalHeight,
double height,
double radius,
boolean hot
) {
double flippedHeight = totalHeight - height;
double function = Math.sin((flippedHeight * Math.PI) / totalHeight);
double function = Math.sin(((height * Math.PI) + (totalHeight * 0.5F)) / totalHeight);
double finalFunction = (function * (radius - 1)) + 1D;

double min = 0D;
Expand All @@ -140,14 +61,4 @@ private double getMapleFoliageFunction(

return Math.max(finalFunction, min);
}

@Override
public int foliageHeight(RandomSource random, int trunkHeight, TreeConfiguration config) {
return Math.max(5, trunkHeight - this.length.sample(random));
}

@Override
protected boolean shouldSkipLocation(RandomSource random, int dx, int y, int dz, int radius, boolean giantTrunk) {
return false;
}
}

0 comments on commit 583f305

Please sign in to comment.