Close #27 Use Forestry API for hive generation

This commit is contained in:
mezz committed 2015-01-13 23:55:31 -08:00
1 parent 8bf47d8f5f
commit 33b0af6682
13 files changed
+477 -655

No files matched your search

@@ -1,94 +0,0 @@
package magicbees.world;
import java.util.Random;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.IWorldGenerator;
import cpw.mods.fml.common.Mod;
import magicbees.main.Config;
import magicbees.main.utils.ChunkCoords;
import magicbees.world.feature.FeatureHive;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.WorldType;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.ChunkDataEvent;
public class WorldGeneratorHandler implements IWorldGenerator
{
public WorldTicker ticker;
public WorldGeneratorHandler()
{
this.ticker = new WorldTicker(this);
FMLCommonHandler.instance().bus().register(ticker);
MinecraftForge.EVENT_BUS.register(this);
FeatureHive.initialize();
}
@Mod.EventHandler
public void chunkSaveEventHandler(ChunkDataEvent.Save event)
{
NBTTagCompound tag = new NBTTagCompound();
if (Config.doHiveRetrogen)
{
tag.setBoolean("hiveRetrogen", true);
}
event.getData().setTag("MagicBees", tag);
}
@Mod.EventHandler
public void chunkLoadEventHandler(ChunkDataEvent.Load event)
{
boolean doRetrogen = false;
NBTTagCompound tag = (NBTTagCompound)event.getData().getTag("MagicBees");
if (tag == null)
{
doRetrogen = true;
}
else if (Config.doHiveRetrogen)
{
if (!tag.hasKey("hiveRetrogen") || Config.forceHiveRegen)
{
doRetrogen = true;
}
}
if (doRetrogen)
{
ChunkCoords coords = new ChunkCoords(event.getChunk());
ticker.queueChunkCoords(coords);
}
}
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider)
{
generateWorld(world, random, chunkX, chunkZ, true);
}
public void generateWorld(World world, Random random, int chunkX, int chunkZ, boolean initialGen)
{
boolean modified = false;
if (world.provider.terrainType != WorldType.FLAT)
{
if (initialGen || Config.doHiveRetrogen)
{
FeatureHive.generateHives(world, random, chunkX, chunkZ, initialGen);
modified = true;
}
}
if (!initialGen && modified)
{
world.getChunkFromChunkCoords(chunkX, chunkZ).setChunkModified();
}
}
}
@@ -1,62 +0,0 @@
package magicbees.world;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import magicbees.main.utils.ChunkCoords;
import net.minecraft.world.World;
public class WorldTicker
{
private WorldGeneratorHandler parent;
private HashMap<Integer, LinkedList<ChunkCoords>> chunkRegenList;
public WorldTicker(WorldGeneratorHandler p)
{
this.parent = p;
this.chunkRegenList = new HashMap<Integer, LinkedList<ChunkCoords>>();
}
@SubscribeEvent
public void onTick(TickEvent.WorldTickEvent event)
{
if (event.phase == TickEvent.Phase.END)
{
World world = event.world;
int dimensionID = world.provider.dimensionId;
LinkedList<ChunkCoords> chunkList = chunkRegenList.get(dimensionID);
if (chunkList != null && chunkList.peekFirst() != null)
{
ChunkCoords coords = chunkList.removeFirst();
// This bit is from FML's GameRegistry.generateWorld where the seed is constructed.
long worldSeed = world.getSeed();
Random random = new Random(worldSeed);
long xSeed = random.nextLong() >> 2 + 1L;
long zSeed = random.nextLong() >> 2 + 1L;
random.setSeed((xSeed * coords.xCoord + zSeed * coords.zCoord) ^ worldSeed);
parent.generateWorld(world, random, coords.xCoord, coords.zCoord, false);
}
}
}
public void queueChunkCoords(ChunkCoords coords)
{
if (!chunkRegenList.containsKey(coords.dimension))
{
chunkRegenList.put(coords.dimension, new LinkedList<ChunkCoords>());
}
chunkRegenList.get(coords.dimension).addLast(coords);
}
public LinkedList<ChunkCoords> getChunksToGen(int dimensionId)
{
return chunkRegenList.get(dimensionId);
}
}
@@ -1,322 +0,0 @@
package magicbees.world.feature;
import java.util.Random;
import magicbees.block.types.HiveType;
import magicbees.main.Config;
import magicbees.main.utils.LogHelper;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
public class FeatureHive
{
private static FeatureOreVein redstoneGen;
private static FeatureOreVein netherQuartzGen;
private static FeatureOreVein glowstoneGen;
private static FeatureOreVein endStoneGen;
private static boolean logSpawns = Config.logHiveSpawns;
public static void initialize()
{
redstoneGen = new FeatureOreVein(Blocks.redstone_ore, Blocks.stone);
netherQuartzGen = new FeatureOreVein(Blocks.quartz_ore, Blocks.netherrack);
glowstoneGen = new FeatureOreVein(Blocks.glowstone, Blocks.stone);
endStoneGen = new FeatureOreVein(Blocks.end_stone, Blocks.stone);
}
public static void generateHives(World world, Random random, int chunkX, int chunkZ, boolean initialGen)
{
for (HiveType type : HiveType.values())
{
type.generateHive(world, random, chunkX, chunkZ, initialGen);
}
}
public static boolean generateHiveCurious(World world, Random random, int coordX, int coordZ, boolean initialGen)
{
int coordY = getHeight(world, random, coordX, coordZ);
boolean doSpawn = false;
Block b = world.getBlock(coordX, coordY + 1, coordZ);
if (world.isAirBlock(coordX, coordY - 1, coordZ) &&
world.isAirBlock(coordX, coordY, coordZ) &&
b != null && b.isLeaves(world, coordX, coordY + 1, coordZ))
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.CURIOUS.ordinal(), 3);
if (logSpawns)
LogHelper.info("Spawning curious hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
doSpawn = true;
}
return doSpawn;
}
public static boolean generateHiveUnusual(World world, Random random, int coordX, int coordZ, boolean initialGen)
{
int coordY = getHeight(world, random, coordX, coordZ);
boolean doSpawn = false;
if (world.isAirBlock(coordX, coordY + 1, coordZ) &&
world.isAirBlock(coordX, coordY, coordZ) &&
canBlockReplaceAt(world, coordX, coordY - 1, coordZ, Blocks.grass))
{
doSpawn = true;
}
if (doSpawn)
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.UNUSUAL.ordinal(), 3);
if (logSpawns)
LogHelper.info("Spawning unusual hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
}
return doSpawn;
}
public static boolean generateHiveResonant(World world, Random random, int coordX, int coordZ, boolean initialGen)
{
int coordY = getHeight(world, random, coordX, coordZ);
boolean doSpawn = false;
if (world.rand.nextInt() < 40 && world.isAirBlock(coordX, coordY + 1, coordZ) &&
(world.isAirBlock(coordX, coordY, coordZ) || canBlockReplaceAt(world, coordX, coordY, coordZ,
Blocks.tallgrass)) &&
(canBlockReplaceAt(world, coordX, coordY - 1, coordZ, Blocks.grass) ||
canBlockReplaceAt(world, coordX, coordY - 1, coordZ, Blocks.sand)))
{
doSpawn = true;
}
if (doSpawn)
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.RESONANT.ordinal(), 3);
if (logSpawns)
LogHelper.info("Spawning resonant hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
}
return doSpawn;
}
public static boolean generateHiveDeep(World world, Random random, int coordX, int coordZ, boolean initialGen)
{
BiomeGenBase biome = world.getBiomeGenForCoords(coordX, coordZ);
int coordY = 10 + random.nextInt(10);
boolean doSpawn = false;
if (random.nextInt(100) < 40 &&
!world.isAirBlock(coordX, coordY, coordZ) && canBlockReplaceAt(world, coordX, coordY, coordZ,
Blocks.stone))
{
doSpawn = getSurroundCount(world, coordX, coordY, coordZ, Blocks.stone) >= 6;
}
if (doSpawn)
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.DEEP.ordinal(), 3);
if (logSpawns) LogHelper.info("Spawning deep hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
redstoneGen.generateVein(world, random, coordX + 1, coordY, coordZ, 2);
redstoneGen.generateVein(world, random, coordX - 1, coordY, coordZ, 2);
redstoneGen.generateVein(world, random, coordX, coordY + 1, coordZ, 2);
redstoneGen.generateVein(world, random, coordX, coordY - 1, coordZ, 2);
redstoneGen.generateVein(world, random, coordX, coordY, coordZ + 1, 2);
redstoneGen.generateVein(world, random, coordX, coordY, coordZ - 1, 2);
}
return doSpawn;
}
public static boolean generateHiveInfernal(World world, Random random, int coordX, int coordZ, boolean initialGen)
{
boolean doSpawn = false;
if (!world.provider.isSurfaceWorld())
{
int chop = random.nextInt(2) + 1;
int coordY = random.nextInt(60 / chop) + random.nextInt(chop) * (120 / chop) - 5;
if (!world.isAirBlock(coordX, coordY, coordZ) && world.getBlock(coordX, coordY, coordZ) != null &&
world.getBlock(coordX, coordY, coordZ).isReplaceableOreGen(world, coordX, coordY, coordZ,
Blocks.netherrack))
{
doSpawn = getSurroundCount(world, coordX, coordY, coordZ, Blocks.netherrack) >= 5;
}
if (doSpawn)
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.INFERNAL.ordinal(), 3);
if (logSpawns)
LogHelper.info("Spawning infernal hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
netherQuartzGen.generateVein(world, random, coordX + 1, coordY, coordZ, 4);
netherQuartzGen.generateVein(world, random, coordX - 1, coordY, coordZ, 4);
netherQuartzGen.generateVein(world, random, coordX, coordY + 1, coordZ, 4);
netherQuartzGen.generateVein(world, random, coordX, coordY - 1, coordZ, 4);
netherQuartzGen.generateVein(world, random, coordX, coordY, coordZ + 1, 4);
netherQuartzGen.generateVein(world, random, coordX, coordY, coordZ - 1, 4);
}
}
else if (Config.doSpecialHiveGen && world.rand.nextInt() < 11)
{
int coordY = random.nextInt(13) + 5;
if (!world.isAirBlock(coordX, coordY, coordZ) &&
canBlockReplaceAt(world, coordX, coordY, coordZ, Blocks.stone))
{
doSpawn = getSurroundCount(world, coordX, coordY, coordZ, Blocks.stone) >= 6;
}
if (doSpawn)
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.INFERNAL.ordinal(), 3);
if (logSpawns)
LogHelper.info("Spawning infernal hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
glowstoneGen.generateVein(world, random, coordX + 1, coordY, coordZ, world.rand.nextInt(4) + 1);
glowstoneGen.generateVein(world, random, coordX - 1, coordY, coordZ, world.rand.nextInt(4) + 1);
if (canBlockReplaceAt(world, coordX, coordY + 1, coordZ, Blocks.stone) ||
canBlockReplaceAt(world, coordX, coordY + 1, coordZ, Blocks.netherrack))
{
world.setBlock(coordX, coordY + 1, coordZ, Blocks.glowstone, 0, 3);
}
if (canBlockReplaceAt(world, coordX, coordY - 1, coordZ, Blocks.stone) ||
canBlockReplaceAt(world, coordX, coordY + 1, coordZ, Blocks.netherrack))
{
world.setBlock(coordX, coordY - 1, coordZ, Blocks.glowstone, 0, 3);
}
glowstoneGen.generateVein(world, random, coordX, coordY, coordZ + 1, world.rand.nextInt(4) + 1);
glowstoneGen.generateVein(world, random, coordX, coordY, coordZ - 1, world.rand.nextInt(4) + 1);
}
}
return doSpawn;
}
public static boolean generateHiveOblivion(World world, Random random, int coordX, int coordZ, boolean initialGen)
{
boolean doSpawn = false;
if (!world.provider.isSurfaceWorld())
{
// 1 per gen
int coordY = 10 + random.nextInt(25);
if (world.isAirBlock(coordX, coordY - 2, coordZ) &&
canBlockReplaceAt(world, coordX, coordY - 1, coordZ, Blocks.end_stone) &&
canBlockReplaceAt(world, coordX, coordY, coordZ, Blocks.end_stone))
{
doSpawn = true;
}
if (doSpawn)
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.OBLIVION.ordinal(), 3);
if (logSpawns)
LogHelper.info("Spawning oblivion hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
int obsidianSpikeHeight = random.nextInt(8) + 2;
for (int i = 1; i < obsidianSpikeHeight && coordY - i > 0; ++i)
{
world.setBlock(coordX, coordY - i, coordZ, Blocks.obsidian, 0, 2);
}
}
}
else if (Config.doSpecialHiveGen && world.rand.nextInt() < 8)
{
// 1 per gen
int coordY = random.nextInt(5) + 5;
do
{
--coordY;
if (!world.isAirBlock(coordX, coordY, coordZ) &&
canBlockReplaceAt(world, coordX, coordY, coordZ, Blocks.stone) ||
canBlockReplaceAt(world, coordX, coordY, coordZ, Blocks.end_stone))
{
doSpawn = getSurroundCount(world, coordX, coordY, coordZ, Blocks.end_stone) >= 5 ||
getSurroundCount(world, coordX, coordY, coordZ, Blocks.stone) >= 5;
}
}
while (!doSpawn && coordY > 5);
if (doSpawn)
{
world.setBlock(coordX, coordY, coordZ, Config.hive, HiveType.OBLIVION.ordinal(), 3);
if (logSpawns)
LogHelper.info("Spawning oblivion hive at: X " + coordX + ", Y: " + coordY + ", Z: " + coordZ);
endStoneGen.generateVein(world, random, coordX + 1, coordY, coordZ, world.rand.nextInt(6) + 1);
endStoneGen.generateVein(world, random, coordX - 1, coordY, coordZ, world.rand.nextInt(6) + 1);
endStoneGen.generateVein(world, random, coordX, coordY + 1, coordZ, world.rand.nextInt(3) + 1);
endStoneGen.generateVein(world, random, coordX, coordY - 1, coordZ, world.rand.nextInt(3) + 1);
endStoneGen.generateVein(world, random, coordX, coordY, coordZ + 1, world.rand.nextInt(6) + 1);
endStoneGen.generateVein(world, random, coordX, coordY, coordZ - 1, world.rand.nextInt(6) + 1);
}
}
return doSpawn;
}
private static int getHeight(World world, Random random, int coordX, int coordZ)
{
BiomeGenBase biome = world.getBiomeGenForCoords(coordX, coordZ);
int min = (int)(biome.rootHeight/*minHeight*/ * 64 + 62);
int max = (int)(biome.heightVariation/*maxHeight*/ * 64);
// sanity
min = (min > 1) ? min : 1;
max = (max > 10) ? max : 10;
return min + random.nextInt(max);
}
// Determines if surrounding material is "equivalent" to provided block using isGenMineableReplaceable.
private static int getSurroundCount(World world, int x, int y, int z, Block blockType)
{
int surroundCount = 0;
if (canBlockReplaceAt(world, x + 1, y, z, blockType))
{
++surroundCount;
}
if (canBlockReplaceAt(world, x - 1, y, z, blockType))
{
++surroundCount;
}
if (canBlockReplaceAt(world, x, y + 1, z, blockType))
{
++surroundCount;
}
if (canBlockReplaceAt(world, x, y - 1, z, blockType))
{
++surroundCount;
}
if (canBlockReplaceAt(world, x, y, z + 1, blockType))
{
++surroundCount;
}
if (canBlockReplaceAt(world, x, y, z - 1, blockType))
{
++surroundCount;
}
return surroundCount;
}
private static boolean canBlockReplaceAt(World world, int x, int y, int z, Block block)
{
return world.getBlock(x, y, z) != null &&
world.getBlock(x, y, z).isReplaceableOreGen(world, x, y, z, block);
}
}
@@ -3,13 +3,29 @@ package magicbees.world.feature;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import magicbees.main.utils.BlockUtil;
public class FeatureOreVein
{
private Block veinBlock;
private int veinBlockMeta;
private Block replacesBlock;
public static final FeatureOreVein redstoneGen;
public static final FeatureOreVein netherQuartzGen;
public static final FeatureOreVein glowstoneGen;
public static final FeatureOreVein endStoneGen;
static
{
redstoneGen = new FeatureOreVein(Blocks.redstone_ore, Blocks.stone);
netherQuartzGen = new FeatureOreVein(Blocks.quartz_ore, Blocks.netherrack);
glowstoneGen = new FeatureOreVein(Blocks.glowstone, Blocks.stone);
endStoneGen = new FeatureOreVein(Blocks.end_stone, Blocks.stone);
}
private final Block veinBlock;
private final int veinBlockMeta;
private final Block replacesBlock;
public FeatureOreVein(Block block, Block replacesBlock)
{
@@ -35,7 +51,7 @@ public class FeatureOreVein
{
++spawnAttempts;
if (!world.isAirBlock(currentX, currentY, currentZ) && canBlockReplaceAt(world, currentX, currentY, currentZ, replacesBlock))
if (!world.isAirBlock(currentX, currentY, currentZ) && BlockUtil.canBlockReplaceAt(world, currentX, currentY, currentZ, replacesBlock))
{
world.setBlock(currentX, currentY, currentZ, this.veinBlock, this.veinBlockMeta, 2);
++spawnCount;
@@ -64,10 +80,4 @@ public class FeatureOreVein
}
}
}
private static boolean canBlockReplaceAt(World world, int x, int y, int z, Block block)
{
return world.getBlock(x, y, z) != null &&
world.getBlock(x, y, z).isReplaceableOreGen(world, x, y, z, block);
}
}
@@ -0,0 +1,34 @@
package magicbees.world.feature;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
public class HiveGenNether extends HiveGenUnderground {
public HiveGenNether(int minLevel, int range, int surroundCount)
{
super(minLevel, range, Blocks.netherrack, surroundCount);
}
@Override
public int getYForHive(World world, int x, int z)
{
int y = super.getYForHive(world, x, z);
if (!isValidLocation(world, x, y, z))
{
int searchDirection = world.rand.nextBoolean() ? 4 : -4;
while (!Block.isEqualTo(world.getBlock(x, y, z), replace))
{
y += searchDirection;
if (y < minLevel || y > minLevel + range)
{
return -1;
}
}
}
return y;
}
}
@@ -0,0 +1,63 @@
package magicbees.world.feature;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import forestry.api.apiculture.hives.IHiveGen;
import magicbees.main.utils.BlockUtil;
public class HiveGenOblivion implements IHiveGen {
@Override
public int getYForHive(World world, int x, int z) {
int surface = world.getHeightValue(x, z);
if (surface == 0)
{
return -1;
}
int y = 10;
// get to the end stone
while (!Block.isEqualTo(world.getBlock(x, y - 1, z), Blocks.end_stone) && y < surface)
{
y += 8;
}
// hive must be embedded one deep into the stone
while (!Block.isEqualTo(world.getBlock(x, y - 2, z), Blocks.air) && y > -1)
{
y--;
}
return y;
}
@Override
public boolean isValidLocation(World world, int x, int y, int z) {
if (BlockUtil.getSurroundCount(world, x, y, z, Blocks.end_stone) != 6)
{
return false;
}
for (int i = 2; i < 4; i++)
{
if (!world.isAirBlock(x, y - i, z))
{
return false;
}
}
return true;
}
@Override
public boolean canReplace(World world, int x, int y, int z) {
Block block = world.getBlock(x, y, z);
return Block.isEqualTo(block, Blocks.end_stone);
}
}
@@ -0,0 +1,47 @@
package magicbees.world.feature;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import forestry.api.apiculture.hives.IHiveGen;
import magicbees.main.utils.BlockUtil;
public class HiveGenUnderground implements IHiveGen
{
protected final int minLevel;
protected final int range;
protected final Block replace;
private final int surroundCount;
public HiveGenUnderground(int minLevel, int range, int surroundCount) {
this(minLevel, range, Blocks.stone, surroundCount);
}
public HiveGenUnderground(int minLevel, int range, Block replace, int surroundCount)
{
this.minLevel = minLevel;
this.range = range;
this.replace = replace;
this.surroundCount = surroundCount;
}
@Override
public int getYForHive(World world, int x, int z)
{
return minLevel + world.rand.nextInt(range);
}
@Override
public boolean isValidLocation(World world, int x, int y, int z)
{
return BlockUtil.getSurroundCount(world, x, y, z, replace) >= surroundCount;
}
@Override
public boolean canReplace(World world, int x, int y, int z)
{
return !world.isAirBlock(x, y, z) && BlockUtil.canBlockReplaceAt(world, x, y, z, replace);
}
}