Guest User

HiveSpawnControl.java

a guest
Jul 30th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. package com.chaosbeing.hivemined.ai;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Random;
  8. import java.util.TreeMap;
  9.  
  10. import net.minecraft.block.Block;
  11. import net.minecraft.client.Minecraft;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.util.ChunkCoordinates;
  14. import net.minecraft.world.ChunkPosition;
  15. import net.minecraft.world.World;
  16. import net.minecraft.world.biome.BiomeGenBase;
  17. import net.minecraft.world.chunk.Chunk;
  18. import net.minecraft.world.chunk.IChunkProvider;
  19. import cpw.mods.fml.common.IWorldGenerator;
  20.  
  21. public class HiveSpawnControl implements IWorldGenerator {
  22.  
  23.     protected ArrayList<HiveController> hives = new ArrayList<HiveController>();
  24.    
  25.     public static int frequency;
  26.     public static int phase;
  27.  
  28.     public static final int FINAL_PHASE = 7;   
  29.    
  30.     public static HashMap<Integer, HiveSpawnControl> controllers = new HashMap<Integer, HiveSpawnControl>();
  31.    
  32.    
  33.     public static HiveSpawnControl getController(int dimension) {
  34.        
  35.         if (controllers.containsKey(dimension)) {
  36.            
  37.             return controllers.get(dimension);
  38.         }
  39.        
  40.         else {
  41.            
  42.             controllers.put(dimension, new HiveSpawnControl());
  43.             return controllers.get(dimension);
  44.         }
  45.     }
  46.    
  47.    
  48.     @Override
  49.     public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator,
  50.             IChunkProvider chunkProvider) {
  51.        
  52.         // <Debug>
  53.         if (chunkX == 0 && chunkZ == 0) {
  54.            
  55.             int x = chunkX * 16 + random.nextInt(16);
  56.             int z = chunkZ * 16 + random.nextInt(16);
  57.             int y = 10 + world.getTopSolidOrLiquidBlock(x, z);
  58.            
  59.             int spawnPhase = phase + (random.nextInt(4) - 2);
  60.            
  61.             HiveSpawnControl controller = getController(world.provider.dimensionId);
  62.            
  63.             //debug
  64.             System.out.println("Created debug hive! X: " + x + " Y: " + y + " Z: " + z);
  65.                    
  66.             controller.hives.add(new HiveController(x, y, z, world, spawnPhase, controller.hives.size(), true));
  67.         }
  68.        
  69.         // </Debug>
  70.        
  71.         // We don't want to run on every chunk generation.
  72.         if (random.nextInt(100) >= 10) {
  73.            
  74.             return;
  75.         }
  76.        
  77.        
  78.         if (!canSpawnHive(world, random, chunkX, chunkZ)) {
  79.            
  80.             return;
  81.         }
  82.        
  83.                
  84.         int spawnPhase = random.nextInt(FINAL_PHASE);
  85.  
  86.         if (spawnPhase < 1) {
  87.  
  88.             spawnPhase = 1;
  89.         }
  90.  
  91.         if (spawnPhase > FINAL_PHASE) {
  92.  
  93.             spawnPhase = FINAL_PHASE;
  94.         }
  95.        
  96.        
  97.         int x = chunkX * 16 + random.nextInt(16);
  98.         int z = chunkZ * 16 + random.nextInt(16);
  99.         int y = world.getTopSolidOrLiquidBlock(x, z);
  100.        
  101.         HiveSpawnControl controller = getController(world.provider.dimensionId);
  102.  
  103.         // Create and spawn new hive.
  104.         controller.hives.add(new HiveController(x, y, z, world, spawnPhase, controller.hives.size(), true));
  105.  
  106.         //debug
  107.         System.out.println("Hive ID " + (controller.hives.size() - 1) + " has been generated at: X:" + x + " Y:" + y + " Z:" + z);
  108.     }
  109.    
  110.     public boolean canSpawnHive(World world, Random random, int chunkX, int chunkZ) {
  111.        
  112.         //Can a hive spawn in this biome?
  113.         BiomeGenBase biome = world.getBiomeGenForCoords(chunkX, chunkZ);
  114.         if (biome.temperature <= 0.7f || biome.rainfall >= 0.5f || biome.rainfall < 0.1f) {
  115.  
  116.             return false;
  117.         }
  118.        
  119.        
  120.         int x = chunkX * 16 + random.nextInt(16);
  121.         int z = chunkZ * 16 + random.nextInt(16);
  122.         int y = world.getTopSolidOrLiquidBlock(x, z);
  123.        
  124.         // How frequently should hives be spawning? (Configurable in config file.)
  125.         int spawnFreq = frequency + (random.nextInt(frequency / 20) - frequency / 10);
  126.        
  127.         //Do not spawn on liquids/etc.
  128.         if (!world.isBlockNormalCubeDefault(x, y, z, false)) {
  129.            
  130.             return false;
  131.         }
  132.        
  133.        
  134.         HiveSpawnControl controller = getController(world.provider.dimensionId);
  135.  
  136.         //Make sure we aren't near any other hives.
  137.         for (HiveController h : controller.hives) {
  138.  
  139.             if ((h.origin[0] - x) * (h.origin[0] - x) + (h.origin[1] - y) * (h.origin[1] - y) +
  140.                     (h.origin[2] - z) * (h.origin[2] - z) < spawnFreq * spawnFreq) {
  141.                
  142.                 return false;
  143.             }
  144.         }
  145.        
  146.  
  147.         //Make sure we aren't near world spawn.
  148.         ChunkCoordinates worldSpawn = world.getSpawnPoint();
  149.  
  150.         if ((worldSpawn.posX - x) * (worldSpawn.posX - x) + (worldSpawn.posY - y) * (worldSpawn.posY - y) +
  151.                 (worldSpawn.posZ - z) * (worldSpawn.posZ - z) < 300 * 300) {
  152.            
  153.             return false;
  154.         }
  155.        
  156.  
  157.         //Make sure we aren't near a Stronghold.
  158.         ChunkPosition cp = world.findClosestStructure("Stronghold", x, y, z);
  159.  
  160.         if ((cp.chunkPosX - chunkX) * (cp.chunkPosX - chunkX) + (cp.chunkPosZ - chunkZ) * (cp.chunkPosZ - chunkZ) < 300 * 300) {
  161.            
  162.             return false;
  163.         }
  164.        
  165.        
  166.         //Make sure we aren't near any player spawns.
  167.         Minecraft.getMinecraft().getIntegratedServer().getAllUsernames();
  168.         for (String user : Minecraft.getMinecraft().getIntegratedServer().getAllUsernames()) {
  169.            
  170.             EntityPlayer player = world.getPlayerEntityByName(user);
  171.            
  172.             ChunkCoordinates spawnLoc = player.getBedLocation(world.provider.dimensionId);
  173.            
  174.             if (spawnLoc == null) {
  175.                
  176.                 spawnLoc = worldSpawn;
  177.             }
  178.  
  179.             if (spawnLoc.getDistanceSquared(x, y, z) < spawnFreq * spawnFreq) {
  180.                
  181.                 return false;
  182.             }
  183.         }
  184.        
  185.         return true;
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment