Skip to content

Instantly share code, notes, and snippets.

@CovertJaguar
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CovertJaguar/87e05a4c699f53b9a95f to your computer and use it in GitHub Desktop.
Save CovertJaguar/87e05a4c699f53b9a95f to your computer and use it in GitHub Desktop.
PoorOreGenerator.java
package mods.railcraft.common.worldgen;
import java.util.Random;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public abstract class NoiseGen {
public static final int OFFSET_RANGE = 50000;
protected final int xOffset, zOffset;
protected final double scale;
public NoiseGen(Random rand, double scale) {
this.scale = scale;
xOffset = rand.nextInt(OFFSET_RANGE) - (OFFSET_RANGE / 2);
zOffset = rand.nextInt(OFFSET_RANGE) - (OFFSET_RANGE / 2);
}
public abstract double noise(double x, double z);
public boolean isLessThan(double x, double z, double level) {
return noise(x, z) < level;
}
public boolean isGreaterThan(double x, double z, double level) {
return noise(x, z) > level;
}
public static class NoiseGenSimplex extends NoiseGen {
public NoiseGenSimplex(Random rand, double scale) {
super(rand, scale);
}
@Override
public double noise(double x, double z) {
return SimplexNoise.noise((x + xOffset) * scale, (z + zOffset) * scale);
}
}
}
package mods.railcraft.common.worldgen;
import com.google.common.collect.MapMaker;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import java.util.Map;
import java.util.Random;
import mods.railcraft.common.blocks.ore.BlockOre;
import mods.railcraft.common.blocks.ore.EnumOre;
import mods.railcraft.common.worldgen.NoiseGen.NoiseGenSimplex;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenMinable;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraftforge.event.terraingen.OreGenEvent;
import net.minecraftforge.event.terraingen.OreGenEvent.GenerateMinable.EventType;
import net.minecraftforge.event.terraingen.TerrainGen;
/**
*
* @author CovertJaguar <http://www.railcraft.info>
*/
public abstract class PoorOreGenerator {
private final EventType eventType;
private final WorldGenerator oreGen;
private final double scale, denseArea, fringeArea;
private final int yLevel, yRange, noiseSeed;
private final Map<World, NoiseGen> noiseMap = new MapMaker().weakKeys().makeMap();
public PoorOreGenerator(EventType eventType, EnumOre ore, int density, int yLevel, int yRange, int noiseSeed) {
this(eventType, ore, 0.0025, 0.85, 0.65, density, yLevel, yRange, noiseSeed);
}
public PoorOreGenerator(EventType eventType, EnumOre ore, double scale, double denseArea, double fringeArea, int density, int yLevel, int yRange, int noiseSeed) {
this.eventType = eventType;
this.scale = scale;
this.denseArea = denseArea;
this.fringeArea = fringeArea;
this.yLevel = yLevel;
this.yRange = yRange;
this.noiseSeed = noiseSeed;
if (density >= 4)
oreGen = new WorldGenMinable(BlockOre.getBlock(), ore.ordinal(), density, Blocks.stone);
else
oreGen = new WorldGenSmallDeposits(BlockOre.getBlock(), ore.ordinal(), density, Blocks.stone);
}
@SubscribeEvent
public void generate(OreGenEvent.Post event) {
World world = event.world;
Random rand = event.rand;
int worldX = event.worldX;
int worldZ = event.worldZ;
if (!TerrainGen.generateOre(world, rand, oreGen, worldX, worldZ, eventType))
return;
NoiseGen noise = noiseMap.get(world);
if (noise == null) {
long seed = world.getSeed();
seed += world.provider.dimensionId;
seed += noiseSeed;
noise = new NoiseGenSimplex(new Random(seed), scale);
noiseMap.put(world, noise);
}
if (canGen(world, rand, worldX, worldZ))
for (int i = 0; i < 32; i++) {
int x = worldX + rand.nextInt(16);
int z = worldZ + rand.nextInt(16);
double strength = noise.noise(x, z);
if (strength > denseArea || (strength > fringeArea && rand.nextFloat() > 0.7)) {
int y = yLevel + Math.round((float) rand.nextGaussian() * yRange);
oreGen.generate(world, rand, x, y, z);
}
}
}
protected boolean canGen(World world, Random rand, int x, int z) {
return true;
}
}
package mods.railcraft.common.worldgen;
import java.util.Random;
import mods.railcraft.common.plugins.forge.WorldPlugin;
import net.minecraft.block.Block;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraftforge.common.util.ForgeDirection;
import mods.railcraft.common.util.misc.MiscTools;
/**
*
* @author CovertJaguar <http://www.railcraft.info>
*/
public class WorldGenSmallDeposits extends WorldGenerator {
private final Block ore, replace;
private final int meta, number;
public WorldGenSmallDeposits(Block ore, int meta, int number, Block replace) {
this.ore = ore;
this.meta = meta;
this.number = number;
this.replace = replace;
}
@Override
public boolean generate(World world, Random rand, int x, int y, int z) {
if (canGen(world, x, y, z)) {
placeOre(world, rand, x, y, z);
return true;
}
return false;
}
protected boolean canGen(World world, int x, int y, int z) {
return true;
}
private void placeOre(World world, Random rand, int x, int y, int z) {
for (int num = 0; num < number; num++) {
Block block = WorldPlugin.getBlock(world, x, y, z);
if (block != null && block.isReplaceableOreGen(world, x, y, z, replace))
world.setBlock(x, y, z, ore, meta, 2);
ForgeDirection dir = ForgeDirection.getOrientation(rand.nextInt(6));
x = MiscTools.getXOnSide(x, dir);
y = MiscTools.getYOnSide(y, dir);
z = MiscTools.getZOnSide(z, dir);
if (!world.blockExists(x, y, z))
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment