Skip to content

Commit

Permalink
Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
UselessBullets committed Nov 10, 2023
1 parent d1ade6b commit b244700
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 65 deletions.
5 changes: 4 additions & 1 deletion src/main/java/useless/terrainapi/TerrainInitialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ public static void initializeDefaultValues(){
overworldConfig.addRandomGrassBlock(Biomes.OVERWORLD_TAIGA, Block.tallgrassFern);

overworldConfig.addLakeDensity(Biomes.OVERWORLD_SWAMPLAND, 2);
overworldConfig.addLakeDensity(Biomes.OVERWORLD_DESERT, 2);
overworldConfig.addLakeDensity(Biomes.OVERWORLD_DESERT, 0);
}
public static void initializeOverworldStructures() {
ChunkDecoratorOverworldAPI.structureFeatures.addFeature(OverworldFunctions::generateSwamp, null);
ChunkDecoratorOverworldAPI.structureFeatures.addFeature(OverworldFunctions::generateLakeFeature, null);
ChunkDecoratorOverworldAPI.structureFeatures.addFeature(OverworldFunctions::generateLavaLakeFeature, null);
ChunkDecoratorOverworldAPI.structureFeatures.addFeature(OverworldFunctions::generateDungeons, null);
ChunkDecoratorOverworldAPI.structureFeatures.addFeature(OverworldFunctions::generateLabyrinths, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package useless.terrainapi.generation.overworld;

import net.minecraft.core.block.Block;
import net.minecraft.core.block.material.Material;
import net.minecraft.core.world.biome.Biome;
import net.minecraft.core.world.generate.feature.WorldFeature;
import net.minecraft.core.world.generate.feature.WorldFeatureDungeon;
import net.minecraft.core.world.generate.feature.WorldFeatureLabyrinth;
import net.minecraft.core.world.generate.feature.WorldFeatureTallGrass;
import net.minecraft.core.world.biome.Biomes;
import net.minecraft.core.world.generate.feature.*;
import org.jetbrains.annotations.Nullable;
import useless.terrainapi.generation.Parameters;
import useless.terrainapi.generation.overworld.api.ChunkDecoratorOverworldAPI;
Expand Down Expand Up @@ -153,4 +152,90 @@ public static Void generateLabyrinths(Parameters parameters){
}
return null;
}
/**Vanilla swamp generation code
* @param parameters Parameters Container
* @return null
*/
@Nullable
public static Void generateSwamp(Parameters parameters){
if (parameters.biome != Biomes.OVERWORLD_SWAMPLAND) return null;
int x = parameters.chunk.xPosition * 16;
int z = parameters.chunk.zPosition * 16;
ChunkDecoratorOverworldAPI decorator = (ChunkDecoratorOverworldAPI) parameters.decorator;

Random swampRand = new Random(decorator.chunkSeed);

for (int dx = 0; dx < 16; ++dx) {
for (int dz = 0; dz < 16; ++dz) {
if (!(swampRand.nextFloat() < 0.5f)) continue;

int topBlock = decorator.world.getHeightValue(x + dx, z + dz);
int id = decorator.world.getBlockId(x + dx, topBlock - 1, z + dz);
if (id != Block.grass.id) continue;

int posXId = decorator.world.getBlockId(x + dx + 1, topBlock - 1, z + dz);
if (posXId == 0) continue;
int negXId = decorator.world.getBlockId(x + dx - 1, topBlock - 1, z + dz);
if (negXId == 0) continue;
int posZId = decorator.world.getBlockId(x + dx, topBlock - 1, z + dz + 1);
if (posZId == 0) continue;
int negZId = decorator.world.getBlockId(x + dx, topBlock - 1, z + dz - 1);
if (negZId == 0) continue;
int negYId = decorator.world.getBlockId(x + dx, topBlock - 2, z + dz);
if (negYId == 0) continue;

if ((!Block.blocksList[posXId].blockMaterial.isSolid() && Block.blocksList[posXId].blockMaterial != Material.water)
|| (!Block.blocksList[negXId].blockMaterial.isSolid() && Block.blocksList[negXId].blockMaterial != Material.water)
|| (!Block.blocksList[posZId].blockMaterial.isSolid() && Block.blocksList[posZId].blockMaterial != Material.water)
|| (!Block.blocksList[negZId].blockMaterial.isSolid() && Block.blocksList[negZId].blockMaterial != Material.water)
|| !Block.blocksList[negYId].blockMaterial.isSolid()) continue;
decorator.world.setBlock(x + dx, topBlock - 1, z + dz, Block.fluidWaterStill.id);
decorator.world.setBlock(x + dx, topBlock, z + dz, 0);
}
}
return null;
}
/**Vanilla lake generation code
* @param parameters Parameters Container
* @return null
*/
@Nullable
public static Void generateLakeFeature(Parameters parameters){
int lakeChance = overworldConfig.getLakeDensity(parameters.biome, overworldConfig.defaultLakeDensity);
int x = parameters.chunk.xPosition * 16;
int z = parameters.chunk.zPosition * 16;

ChunkDecoratorOverworldAPI decorator = (ChunkDecoratorOverworldAPI) parameters.decorator;

if (lakeChance != 0 && parameters.random.nextInt(lakeChance) == 0) {
int fluid = Block.fluidWaterStill.id;
if (parameters.biome.hasSurfaceSnow()) {
fluid = Block.ice.id;
}
int i1 = x + parameters.random.nextInt(16) + 8;
int l4 = decorator.minY + parameters.random.nextInt(decorator.rangeY);
int i8 = z + parameters.random.nextInt(16) + 8;
new WorldFeatureLake(fluid).generate(decorator.world, parameters.random, i1, l4, i8);
}
return null;
}
/**Vanilla lava lake generation code
* @param parameters Parameters Container
* @return null
*/
@Nullable
public static Void generateLavaLakeFeature(Parameters parameters){
int x = parameters.chunk.xPosition * 16;
int z = parameters.chunk.zPosition * 16;
ChunkDecoratorOverworldAPI decorator = (ChunkDecoratorOverworldAPI) parameters.decorator;
if (parameters.random.nextInt(8) == 0) {
int xf = x + parameters.random.nextInt(16) + 8;
int yf = decorator.minY + parameters.random.nextInt(parameters.random.nextInt(decorator.rangeY - decorator.rangeY / 16) + decorator.rangeY / 16);
int zf = z + parameters.random.nextInt(16) + 8;
if (yf < decorator.minY + decorator.rangeY / 2 || parameters.random.nextInt(10) == 0) {
new WorldFeatureLake(Block.fluidLavaStill.id).generate(decorator.world, parameters.random, xf, yf, zf);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ChunkDecoratorOverworldAPI extends ChunkDecoratorAPI {
public static OverworldOreFeatures oreFeatures = new OverworldOreFeatures(overworldConfig);
public static OverworldRandomFeatures randomFeatures = new OverworldRandomFeatures();
public static OverworldBiomeFeatures biomeFeatures = new OverworldBiomeFeatures();
public long chunkSeed;
protected ChunkDecoratorOverworldAPI(World world, int treeDensityOverride) {
super(world);
this.treeDensityOverride = treeDensityOverride;
Expand All @@ -56,19 +57,13 @@ public void decorate(Chunk chunk) {
Random random = new Random(this.world.getRandomSeed());
long l1 = random.nextLong() / 2L * 2L + 1L;
long l2 = random.nextLong() / 2L * 2L + 1L;
random.setSeed((long)chunkX * l1 + (long)chunkZ * l2 ^ this.world.getRandomSeed());
Random swampRand = new Random((long)chunkX * l1 + (long)chunkZ * l2 ^ this.world.getRandomSeed());
chunkSeed = (long)chunkX * l1 + (long)chunkZ * l2 ^ this.world.getRandomSeed();
random.setSeed(chunkSeed);

BlockSand.fallInstantly = true;

if (biome == Biomes.OVERWORLD_SWAMPLAND){
swampFeature(xCoord, zCoord, swampRand);
}

parameterBase = new Parameters(biome, random, chunk, this);

generateLakeFeature(overworldConfig.getLakeDensity(biome, overworldConfig.defaultLakeDensity), xCoord, zCoord, biome, random);

generateStructures(biome, chunk, random);
generateOreFeatures(biome, xCoord, zCoord, random, chunk);
generateBiomeFeature(biome,xCoord, zCoord, random, chunk);
Expand All @@ -83,58 +78,6 @@ public void decorate(Chunk chunk) {

}
@ApiStatus.Internal
public void swampFeature(int x, int z, Random random){
for (int dx = 0; dx < 16; ++dx) {
for (int dz = 0; dz < 16; ++dz) {
if (!(random.nextFloat() < 0.5f)) continue;

int topBlock = this.world.getHeightValue(x + dx, z + dz);
int id = this.world.getBlockId(x + dx, topBlock - 1, z + dz);
if (id != Block.grass.id) continue;

int posXId = this.world.getBlockId(x + dx + 1, topBlock - 1, z + dz);
if (posXId == 0) continue;
int negXId = this.world.getBlockId(x + dx - 1, topBlock - 1, z + dz);
if (negXId == 0) continue;
int posZId = this.world.getBlockId(x + dx, topBlock - 1, z + dz + 1);
if (posZId == 0) continue;
int negZId = this.world.getBlockId(x + dx, topBlock - 1, z + dz - 1);
if (negZId == 0) continue;
int negYId = this.world.getBlockId(x + dx, topBlock - 2, z + dz);
if (negYId == 0) continue;

if ((!Block.blocksList[posXId].blockMaterial.isSolid() && Block.blocksList[posXId].blockMaterial != Material.water)
|| (!Block.blocksList[negXId].blockMaterial.isSolid() && Block.blocksList[negXId].blockMaterial != Material.water)
|| (!Block.blocksList[posZId].blockMaterial.isSolid() && Block.blocksList[posZId].blockMaterial != Material.water)
|| (!Block.blocksList[negZId].blockMaterial.isSolid() && Block.blocksList[negZId].blockMaterial != Material.water)
|| !Block.blocksList[negYId].blockMaterial.isSolid()) continue;
this.world.setBlock(x + dx, topBlock - 1, z + dz, Block.fluidWaterStill.id);
this.world.setBlock(x + dx, topBlock, z + dz, 0);
}
}
}
@ApiStatus.Internal
public void generateLakeFeature(int lakeChance, int x, int z, Biome biome, Random random){
if (lakeChance != 0 && random.nextInt(lakeChance) == 0) {
int fluid = Block.fluidWaterStill.id;
if (biome.hasSurfaceSnow()) {
fluid = Block.ice.id;
}
int i1 = x + random.nextInt(16) + 8;
int l4 = minY + random.nextInt(rangeY);
int i8 = z + random.nextInt(16) + 8;
new WorldFeatureLake(fluid).generate(this.world, random, i1, l4, i8);
}
if (random.nextInt(8) == 0) {
int xf = x + random.nextInt(16) + 8;
int yf = minY + random.nextInt(random.nextInt(rangeY - rangeY / 16) + rangeY / 16);
int zf = z + random.nextInt(16) + 8;
if (yf < minY + rangeY / 2 || random.nextInt(10) == 0) {
new WorldFeatureLake(Block.fluidLavaStill.id).generate(this.world, random, xf, yf, zf);
}
}
}
@ApiStatus.Internal
public void generateStructures(Biome biome, Chunk chunk, Random random){
int featureSize = structureFeatures.featureFunctionList.size();
for (int i = 0; i < featureSize; i++) {
Expand Down

0 comments on commit b244700

Please sign in to comment.