Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chaosbeing.hivemined.ai;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Random;
- import java.util.TreeMap;
- import net.minecraft.block.Block;
- import net.minecraft.client.Minecraft;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.util.ChunkCoordinates;
- import net.minecraft.world.ChunkPosition;
- import net.minecraft.world.World;
- import net.minecraft.world.biome.BiomeGenBase;
- import net.minecraft.world.chunk.Chunk;
- import net.minecraft.world.chunk.IChunkProvider;
- import cpw.mods.fml.common.IWorldGenerator;
- public class HiveSpawnControl implements IWorldGenerator {
- protected ArrayList<HiveController> hives = new ArrayList<HiveController>();
- public static int frequency;
- public static int phase;
- public static final int FINAL_PHASE = 7;
- public static HashMap<Integer, HiveSpawnControl> controllers = new HashMap<Integer, HiveSpawnControl>();
- public static HiveSpawnControl getController(int dimension) {
- if (controllers.containsKey(dimension)) {
- return controllers.get(dimension);
- }
- else {
- controllers.put(dimension, new HiveSpawnControl());
- return controllers.get(dimension);
- }
- }
- @Override
- public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator,
- IChunkProvider chunkProvider) {
- // <Debug>
- if (chunkX == 0 && chunkZ == 0) {
- int x = chunkX * 16 + random.nextInt(16);
- int z = chunkZ * 16 + random.nextInt(16);
- int y = 10 + world.getTopSolidOrLiquidBlock(x, z);
- int spawnPhase = phase + (random.nextInt(4) - 2);
- HiveSpawnControl controller = getController(world.provider.dimensionId);
- //debug
- System.out.println("Created debug hive! X: " + x + " Y: " + y + " Z: " + z);
- controller.hives.add(new HiveController(x, y, z, world, spawnPhase, controller.hives.size(), true));
- }
- // </Debug>
- // We don't want to run on every chunk generation.
- if (random.nextInt(100) >= 10) {
- return;
- }
- if (!canSpawnHive(world, random, chunkX, chunkZ)) {
- return;
- }
- int spawnPhase = random.nextInt(FINAL_PHASE);
- if (spawnPhase < 1) {
- spawnPhase = 1;
- }
- if (spawnPhase > FINAL_PHASE) {
- spawnPhase = FINAL_PHASE;
- }
- int x = chunkX * 16 + random.nextInt(16);
- int z = chunkZ * 16 + random.nextInt(16);
- int y = world.getTopSolidOrLiquidBlock(x, z);
- HiveSpawnControl controller = getController(world.provider.dimensionId);
- // Create and spawn new hive.
- controller.hives.add(new HiveController(x, y, z, world, spawnPhase, controller.hives.size(), true));
- //debug
- System.out.println("Hive ID " + (controller.hives.size() - 1) + " has been generated at: X:" + x + " Y:" + y + " Z:" + z);
- }
- public boolean canSpawnHive(World world, Random random, int chunkX, int chunkZ) {
- //Can a hive spawn in this biome?
- BiomeGenBase biome = world.getBiomeGenForCoords(chunkX, chunkZ);
- if (biome.temperature <= 0.7f || biome.rainfall >= 0.5f || biome.rainfall < 0.1f) {
- return false;
- }
- int x = chunkX * 16 + random.nextInt(16);
- int z = chunkZ * 16 + random.nextInt(16);
- int y = world.getTopSolidOrLiquidBlock(x, z);
- // How frequently should hives be spawning? (Configurable in config file.)
- int spawnFreq = frequency + (random.nextInt(frequency / 20) - frequency / 10);
- //Do not spawn on liquids/etc.
- if (!world.isBlockNormalCubeDefault(x, y, z, false)) {
- return false;
- }
- HiveSpawnControl controller = getController(world.provider.dimensionId);
- //Make sure we aren't near any other hives.
- for (HiveController h : controller.hives) {
- if ((h.origin[0] - x) * (h.origin[0] - x) + (h.origin[1] - y) * (h.origin[1] - y) +
- (h.origin[2] - z) * (h.origin[2] - z) < spawnFreq * spawnFreq) {
- return false;
- }
- }
- //Make sure we aren't near world spawn.
- ChunkCoordinates worldSpawn = world.getSpawnPoint();
- if ((worldSpawn.posX - x) * (worldSpawn.posX - x) + (worldSpawn.posY - y) * (worldSpawn.posY - y) +
- (worldSpawn.posZ - z) * (worldSpawn.posZ - z) < 300 * 300) {
- return false;
- }
- //Make sure we aren't near a Stronghold.
- ChunkPosition cp = world.findClosestStructure("Stronghold", x, y, z);
- if ((cp.chunkPosX - chunkX) * (cp.chunkPosX - chunkX) + (cp.chunkPosZ - chunkZ) * (cp.chunkPosZ - chunkZ) < 300 * 300) {
- return false;
- }
- //Make sure we aren't near any player spawns.
- Minecraft.getMinecraft().getIntegratedServer().getAllUsernames();
- for (String user : Minecraft.getMinecraft().getIntegratedServer().getAllUsernames()) {
- EntityPlayer player = world.getPlayerEntityByName(user);
- ChunkCoordinates spawnLoc = player.getBedLocation(world.provider.dimensionId);
- if (spawnLoc == null) {
- spawnLoc = worldSpawn;
- }
- if (spawnLoc.getDistanceSquared(x, y, z) < spawnFreq * spawnFreq) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment