Structure scatter

This commit is contained in:
paulevsGitch 2020-10-03 00:11:18 +03:00
parent 8dfb71478a
commit 8eb15c8d73
4 changed files with 95 additions and 91 deletions

View file

@ -5,68 +5,39 @@ import java.util.Random;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.world.StructureWorldAccess; import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.blocks.basis.BlockDoublePlant; import ru.betterend.blocks.basis.BlockDoublePlant;
import ru.betterend.registry.BlockTagRegistry;
import ru.betterend.util.BlocksHelper; import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper; import ru.betterend.util.MHelper;
public class DoublePlantFeature extends DefaultFeature { public class DoublePlantFeature extends ScatterFeature {
private static final Mutable POS = new Mutable();
private final Block smallPlant; private final Block smallPlant;
private final Block largePlant; private final Block largePlant;
private final int radius; private Block plant;
public DoublePlantFeature(Block smallPlant, Block largePlant, int radius) { public DoublePlantFeature(Block smallPlant, Block largePlant, int radius) {
super(radius);
this.smallPlant = smallPlant; this.smallPlant = smallPlant;
this.largePlant = largePlant; this.largePlant = largePlant;
this.radius = radius;
} }
@Override @Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig featureConfig) { public boolean canGenerate(StructureWorldAccess world, Random random, BlockPos center, BlockPos blockPos, float radius) {
blockPos = getPosOnSurface(world, blockPos); float d = MHelper.length(center.getX() - blockPos.getX(), center.getZ() - blockPos.getZ()) / radius * 0.6F + random.nextFloat() * 0.4F;
plant = d < 0.5F ? largePlant : smallPlant;
if (blockPos.getY() < 5) { return plant.canPlaceAt(plant.getDefaultState(), world, blockPos);
return false;
}
if (!world.getBlockState(blockPos.down()).isIn(BlockTagRegistry.END_GROUND)) {
return false;
} }
float r = MHelper.randRange(radius * 0.5F, radius, random); @Override
int count = MHelper.floor(r * r * MHelper.randRange(1.5F, 3F, random)); public void generate(StructureWorldAccess world, Random random, BlockPos blockPos) {
Block block; if (plant instanceof BlockDoublePlant) {
for (int i = 0; i < count; i++) {
float pr = r * (float) Math.sqrt(random.nextFloat());
float theta = random.nextFloat() * MHelper.PI2;
float x = pr * (float) Math.cos(theta);
float z = pr * (float) Math.sin(theta);
POS.set(blockPos.getX() + x, blockPos.getY() + 5, blockPos.getZ() + z);
int down = BlocksHelper.downRay(world, POS, 16);
if (down > 10) continue;
POS.setY(POS.getY() - down);
float d = MHelper.length(x, z) / r * 0.6F + random.nextFloat() * 0.4F;
block = d < 0.5F ? largePlant : smallPlant;
if (block.canPlaceAt(block.getDefaultState(), world, POS)) {
if (block instanceof BlockDoublePlant) {
int rot = random.nextInt(4); int rot = random.nextInt(4);
BlockState state = block.getDefaultState().with(BlockDoublePlant.ROTATION, rot); BlockState state = plant.getDefaultState().with(BlockDoublePlant.ROTATION, rot);
BlocksHelper.setWithoutUpdate(world, POS, state); BlocksHelper.setWithoutUpdate(world, blockPos, state);
BlocksHelper.setWithoutUpdate(world, POS.up(), state.with(BlockDoublePlant.TOP, true)); BlocksHelper.setWithoutUpdate(world, blockPos.up(), state.with(BlockDoublePlant.TOP, true));
} }
else { else {
BlocksHelper.setWithoutUpdate(world, POS, block); BlocksHelper.setWithoutUpdate(world, blockPos, plant);
} }
} }
}
return true;
}
} }

View file

@ -7,6 +7,7 @@ import java.util.function.Function;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.block.Material;
import net.minecraft.client.util.math.Vector3f; import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
@ -154,6 +155,9 @@ public class MossyGlowshroomFeature extends DefaultFeature {
if (state.getBlock() != Blocks.END_STONE && state.isIn(BlockTagRegistry.END_GROUND)) { if (state.getBlock() != Blocks.END_STONE && state.isIn(BlockTagRegistry.END_GROUND)) {
return true; return true;
} }
if (state.getMaterial().equals(Material.PLANT)) {
return true;
}
return state.getMaterial().isReplaceable(); return state.getMaterial().isReplaceable();
}; };

View file

@ -0,0 +1,58 @@
package ru.betterend.world.features;
import java.util.Random;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.registry.BlockTagRegistry;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
public abstract class ScatterFeature extends DefaultFeature {
private static final Mutable POS = new Mutable();
private final int radius;
public ScatterFeature(int radius) {
this.radius = radius;
}
public abstract boolean canGenerate(StructureWorldAccess world, Random random, BlockPos center, BlockPos blockPos, float radius);
public abstract void generate(StructureWorldAccess world, Random random, BlockPos blockPos);
@Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos center, DefaultFeatureConfig featureConfig) {
center = getPosOnSurface(world, center);
if (center.getY() < 5) {
return false;
}
if (!world.getBlockState(center.down()).isIn(BlockTagRegistry.END_GROUND)) {
return false;
}
float r = MHelper.randRange(radius * 0.5F, radius, random);
int count = MHelper.floor(r * r * MHelper.randRange(1.5F, 3F, random));
for (int i = 0; i < count; i++) {
float pr = r * (float) Math.sqrt(random.nextFloat());
float theta = random.nextFloat() * MHelper.PI2;
float x = pr * (float) Math.cos(theta);
float z = pr * (float) Math.sin(theta);
POS.set(center.getX() + x, center.getY() + 5, center.getZ() + z);
int down = BlocksHelper.downRay(world, POS, 16);
if (down > 10) continue;
POS.setY(POS.getY() - down);
if (canGenerate(world, random, center, POS, r)) {
generate(world, random, POS);
}
}
return true;
}
}

View file

@ -5,62 +5,33 @@ import java.util.Random;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.world.StructureWorldAccess; import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.feature.DefaultFeatureConfig;
import ru.betterend.blocks.basis.BlockDoublePlant; import ru.betterend.blocks.basis.BlockDoublePlant;
import ru.betterend.registry.BlockTagRegistry;
import ru.betterend.util.BlocksHelper; import ru.betterend.util.BlocksHelper;
import ru.betterend.util.MHelper;
public class SinglePlantFeature extends DefaultFeature { public class SinglePlantFeature extends ScatterFeature {
private static final Mutable POS = new Mutable();
private final Block plant; private final Block plant;
private final int radius;
public SinglePlantFeature(Block plant, int radius) { public SinglePlantFeature(Block plant, int radius) {
super(radius);
this.plant = plant; this.plant = plant;
this.radius = radius;
} }
@Override @Override
public boolean generate(StructureWorldAccess world, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, DefaultFeatureConfig featureConfig) { public boolean canGenerate(StructureWorldAccess world, Random random, BlockPos center, BlockPos blockPos, float radius) {
blockPos = getPosOnSurface(world, blockPos); return plant.canPlaceAt(plant.getDefaultState(), world, blockPos);
if (blockPos.getY() < 5) {
return false;
}
if (!world.getBlockState(blockPos.down()).isIn(BlockTagRegistry.END_GROUND)) {
return false;
} }
float r = MHelper.randRange(radius * 0.5F, radius, random); @Override
int count = MHelper.floor(r * r * MHelper.randRange(1.5F, 3F, random)); public void generate(StructureWorldAccess world, Random random, BlockPos blockPos) {
for (int i = 0; i < count; i++) {
float pr = r * (float) Math.sqrt(random.nextFloat());
float theta = random.nextFloat() * MHelper.PI2;
float x = pr * (float) Math.cos(theta);
float z = pr * (float) Math.sin(theta);
POS.set(blockPos.getX() + x, blockPos.getY() + 5, blockPos.getZ() + z);
int down = BlocksHelper.downRay(world, POS, 16);
if (down > 10) continue;
POS.setY(POS.getY() - down);
if (plant.canPlaceAt(plant.getDefaultState(), world, POS)) {
if (plant instanceof BlockDoublePlant) { if (plant instanceof BlockDoublePlant) {
int rot = random.nextInt(4); int rot = random.nextInt(4);
BlockState state = plant.getDefaultState().with(BlockDoublePlant.ROTATION, rot); BlockState state = plant.getDefaultState().with(BlockDoublePlant.ROTATION, rot);
BlocksHelper.setWithoutUpdate(world, POS, state); BlocksHelper.setWithoutUpdate(world, blockPos, state);
BlocksHelper.setWithoutUpdate(world, POS.up(), state.with(BlockDoublePlant.TOP, true)); BlocksHelper.setWithoutUpdate(world, blockPos.up(), state.with(BlockDoublePlant.TOP, true));
} }
else { else {
BlocksHelper.setWithoutUpdate(world, POS, plant); BlocksHelper.setWithoutUpdate(world, blockPos, plant);
} }
} }
}
return true;
}
} }