    001    package net.minecraft.src;
    002
    003    import java.util.ArrayList;
    004    import java.util.Collections;
    005    import java.util.HashMap;
    006    import java.util.Iterator;
    007    import java.util.List;
    008    import java.util.Random;
    009
    010    import net.minecraftforge.common.MinecraftForge;
    011    import net.minecraftforge.event.entity.living.LivingSpecialSpawnEvent;
    012
    013    public final class SpawnerAnimals
    014    {
    015        /** The 17x17 area around the player where mobs can spawn */
    016        private static HashMap eligibleChunksForSpawning = new HashMap();
    017
    018        /** An array of entity classes that spawn at night. */
    019        protected static final Class[] nightSpawnEntities = new Class[] {EntitySpider.class, EntityZombie.class, EntitySkeleton.class};
    020
    021        /**
    022         * Given a chunk, find a random position in it.
    023         */
    024        protected static ChunkPosition getRandomSpawningPointInChunk(World par0World, int par1, int par2)
    025        {
    026            Chunk var3 = par0World.getChunkFromChunkCoords(par1, par2);
    027            int var4 = par1 * 16 + par0World.rand.nextInt(16);
    028            int var5 = par2 * 16 + par0World.rand.nextInt(16);
    029            int var6 = par0World.rand.nextInt(var3 == null ? par0World.getActualHeight() : var3.getTopFilledSegment() + 16 - 1);
    030            return new ChunkPosition(var4, var6, var5);
    031        }
    032
    033        /**
    034         * adds all chunks within the spawn radius of the players to eligibleChunksForSpawning. pars: the world,
    035         * hostileCreatures, passiveCreatures. returns number of eligible chunks.
    036         */
    037        public static final int findChunksForSpawning(WorldServer par0WorldServer, boolean par1, boolean par2, boolean par3)
    038        {
    039            if (!par1 && !par2)
    040            {
    041                return 0;
    042            }
    043            else
    044            {
    045                eligibleChunksForSpawning.clear();
    046                int var4;
    047                int var7;
    048
    049                for (var4 = 0; var4 < par0WorldServer.playerEntities.size(); ++var4)
    050                {
    051                    EntityPlayer var5 = (EntityPlayer)par0WorldServer.playerEntities.get(var4);
    052                    int var6 = MathHelper.floor_double(var5.posX / 16.0D);
    053                    var7 = MathHelper.floor_double(var5.posZ / 16.0D);
    054                    byte var8 = 8;
    055
    056                    for (int var9 = -var8; var9 <= var8; ++var9)
    057                    {
    058                        for (int var10 = -var8; var10 <= var8; ++var10)
    059                        {
    060                            boolean var11 = var9 == -var8 || var9 == var8 || var10 == -var8 || var10 == var8;
    061                            ChunkCoordIntPair var12 = new ChunkCoordIntPair(var9 + var6, var10 + var7);
    062
    063                            if (!var11)
    064                            {
    065                                eligibleChunksForSpawning.put(var12, Boolean.valueOf(false));
    066                            }
    067                            else if (!eligibleChunksForSpawning.containsKey(var12))
    068                            {
    069                                eligibleChunksForSpawning.put(var12, Boolean.valueOf(true));
    070                            }
    071                        }
    072                    }
    073                }
    074
    075                var4 = 0;
    076                ChunkCoordinates var32 = par0WorldServer.getSpawnPoint();
    077                EnumCreatureType[] var33 = EnumCreatureType.values();
    078                var7 = var33.length;
    079
    080                for (int var34 = 0; var34 < var7; ++var34)
    081                {
    082                    EnumCreatureType var35 = var33[var34];
    083
    084                    if ((!var35.getPeacefulCreature() || par2) && (var35.getPeacefulCreature() || par1) && (!var35.getAnimal() || par3) && par0WorldServer.countEntities(var35.getCreatureClass()) <= var35.getMaxNumberOfCreature() * eligibleChunksForSpawning.size() / 256)
    085                    {
    086                        Iterator var37 = eligibleChunksForSpawning.keySet().iterator();
    087                        ArrayList<ChunkCoordIntPair> tmp = new ArrayList(eligibleChunksForSpawning.keySet());
    088                        Collections.shuffle(tmp);
    089                        var37 = tmp.iterator();
    090                        label110:
    091
    092                        while (var37.hasNext())
    093                        {
    094                            ChunkCoordIntPair var36 = (ChunkCoordIntPair)var37.next();
    095
    096                            if (!((Boolean)eligibleChunksForSpawning.get(var36)).booleanValue())
    097                            {
    098                                ChunkPosition var38 = getRandomSpawningPointInChunk(par0WorldServer, var36.chunkXPos, var36.chunkZPos);
    099                                int var13 = var38.x;
    100                                int var14 = var38.y;
    101                                int var15 = var38.z;
    102
    103                                if (!par0WorldServer.isBlockNormalCube(var13, var14, var15) && par0WorldServer.getBlockMaterial(var13, var14, var15) == var35.getCreatureMaterial())
    104                                {
    105                                    int var16 = 0;
    106                                    int var17 = 0;
    107
    108                                    while (var17 < 3)
    109                                    {
    110                                        int var18 = var13;
    111                                        int var19 = var14;
    112                                        int var20 = var15;
    113                                        byte var21 = 6;
    114                                        SpawnListEntry var22 = null;
    115                                        int var23 = 0;
    116
    117                                        while (true)
    118                                        {
    119                                            if (var23 < 4)
    120                                            {
    121                                                label103:
    122                                                {
    123                                                    var18 += par0WorldServer.rand.nextInt(var21) - par0WorldServer.rand.nextInt(var21);
    124                                                    var19 += par0WorldServer.rand.nextInt(1) - par0WorldServer.rand.nextInt(1);
    125                                                    var20 += par0WorldServer.rand.nextInt(var21) - par0WorldServer.rand.nextInt(var21);
    126
    127                                                    if (canCreatureTypeSpawnAtLocation(var35, par0WorldServer, var18, var19, var20))
    128                                                    {
    129                                                        float var24 = (float)var18 + 0.5F;
    130                                                        float var25 = (float)var19;
    131                                                        float var26 = (float)var20 + 0.5F;
    132
    133                                                        if (par0WorldServer.getClosestPlayer((double)var24, (double)var25, (double)var26, 24.0D) == null)
    134                                                        {
    135                                                            float var27 = var24 - (float)var32.posX;
    136                                                            float var28 = var25 - (float)var32.posY;
    137                                                            float var29 = var26 - (float)var32.posZ;
    138                                                            float var30 = var27 * var27 + var28 * var28 + var29 * var29;
    139
    140                                                            if (var30 >= 576.0F)
    141                                                            {
    142                                                                if (var22 == null)
    143                                                                {
    144                                                                    var22 = par0WorldServer.spawnRandomCreature(var35, var18, var19, var20);
    145
    146                                                                    if (var22 == null)
    147                                                                    {
    148                                                                        break label103;
    149                                                                    }
    150                                                                }
    151
    152                                                                EntityLiving var39;
    153
    154                                                                try
    155                                                                {
    156                                                                    var39 = (EntityLiving)var22.entityClass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {par0WorldServer});
    157                                                                }
    158                                                                catch (Exception var31)
    159                                                                {
    160                                                                    var31.printStackTrace();
    161                                                                    return var4;
    162                                                                }
    163
    164                                                                var39.setLocationAndAngles((double)var24, (double)var25, (double)var26, par0WorldServer.rand.nextFloat() * 360.0F, 0.0F);
    165
    166                                                                if (var39.getCanSpawnHere())
    167                                                                {
    168                                                                    ++var16;
    169                                                                    par0WorldServer.spawnEntityInWorld(var39);
    170                                                                    creatureSpecificInit(var39, par0WorldServer, var24, var25, var26);
    171
    172                                                                    if (var16 >= var39.getMaxSpawnedInChunk())
    173                                                                    {
    174                                                                        continue label110;
    175                                                                    }
    176                                                                }
    177
    178                                                                var4 += var16;
    179                                                            }
    180                                                        }
    181                                                    }
    182
    183                                                    ++var23;
    184                                                    continue;
    185                                                }
    186                                            }
    187
    188                                            ++var17;
    189                                            break;
    190                                        }
    191                                    }
    192                                }
    193                            }
    194                        }
    195                    }
    196                }
    197
    198                return var4;
    199            }
    200        }
    201
    202        /**
    203         * Returns whether or not the specified creature type can spawn at the specified location.
    204         */
    205        public static boolean canCreatureTypeSpawnAtLocation(EnumCreatureType par0EnumCreatureType, World par1World, int par2, int par3, int par4)
    206        {
    207            if (par0EnumCreatureType.getCreatureMaterial() == Material.water)
    208            {
    209                return par1World.getBlockMaterial(par2, par3, par4).isLiquid() && par1World.getBlockMaterial(par2, par3 - 1, par4).isLiquid() && !par1World.isBlockNormalCube(par2, par3 + 1, par4);
    210            }
    211            else if (!par1World.doesBlockHaveSolidTopSurface(par2, par3 - 1, par4))
    212            {
    213                return false;
    214            }
    215            else
    216            {
    217                int var5 = par1World.getBlockId(par2, par3 - 1, par4);
    218                boolean spawnBlock = (Block.blocksList[var5] != null && Block.blocksList[var5].canCreatureSpawn(par0EnumCreatureType, par1World, par2, par3 - 1, par4));
    219                return spawnBlock && var5 != Block.bedrock.blockID && !par1World.isBlockNormalCube(par2, par3, par4) && !par1World.getBlockMaterial(par2, par3, par4).isLiquid() && !par1World.isBlockNormalCube(par2, par3 + 1, par4);
    220            }
    221        }
    222
    223        /**
    224         * determines if a skeleton spawns on a spider, and if a sheep is a different color
    225         */
    226        private static void creatureSpecificInit(EntityLiving par0EntityLiving, World par1World, float par2, float par3, float par4)
    227        {
    228            if (MinecraftForge.EVENT_BUS.post(new LivingSpecialSpawnEvent(par0EntityLiving, par1World, par2, par3, par4)))
    229            {
    230                return;
    231            }
    232
    233            par0EntityLiving.initCreature();
    234        }
    235
    236        /**
    237         * Called during chunk generation to spawn initial creatures.
    238         */
    239        public static void performWorldGenSpawning(World par0World, BiomeGenBase par1BiomeGenBase, int par2, int par3, int par4, int par5, Random par6Random)
    240        {
    241            List var7 = par1BiomeGenBase.getSpawnableList(EnumCreatureType.creature);
    242
    243            if (!var7.isEmpty())
    244            {
    245                while (par6Random.nextFloat() < par1BiomeGenBase.getSpawningChance())
    246                {
    247                    SpawnListEntry var8 = (SpawnListEntry)WeightedRandom.getRandomItem(par0World.rand, var7);
    248                    int var9 = var8.minGroupCount + par6Random.nextInt(1 + var8.maxGroupCount - var8.minGroupCount);
    249                    int var10 = par2 + par6Random.nextInt(par4);
    250                    int var11 = par3 + par6Random.nextInt(par5);
    251                    int var12 = var10;
    252                    int var13 = var11;
    253
    254                    for (int var14 = 0; var14 < var9; ++var14)
    255                    {
    256                        boolean var15 = false;
    257
    258                        for (int var16 = 0; !var15 && var16 < 4; ++var16)
    259                        {
    260                            int var17 = par0World.getTopSolidOrLiquidBlock(var10, var11);
    261
    262                            if (canCreatureTypeSpawnAtLocation(EnumCreatureType.creature, par0World, var10, var17, var11))
    263                            {
    264                                float var18 = (float)var10 + 0.5F;
    265                                float var19 = (float)var17;
    266                                float var20 = (float)var11 + 0.5F;
    267                                EntityLiving var21;
    268
    269                                try
    270                                {
    271                                    var21 = (EntityLiving)var8.entityClass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {par0World});
    272                                }
    273                                catch (Exception var23)
    274                                {
    275                                    var23.printStackTrace();
    276                                    continue;
    277                                }
    278
    279                                var21.setLocationAndAngles((double)var18, (double)var19, (double)var20, par6Random.nextFloat() * 360.0F, 0.0F);
    280                                par0World.spawnEntityInWorld(var21);
    281                                creatureSpecificInit(var21, par0World, var18, var19, var20);
    282                                var15 = true;
    283                            }
    284
    285                            var10 += par6Random.nextInt(5) - par6Random.nextInt(5);
    286
    287                            for (var11 += par6Random.nextInt(5) - par6Random.nextInt(5); var10 < par2 || var10 >= par2 + par4 || var11 < par3 || var11 >= par3 + par4; var11 = var13 + par6Random.nextInt(5) - par6Random.nextInt(5))
    288                            {
    289                                var10 = var12 + par6Random.nextInt(5) - par6Random.nextInt(5);
    290                            }
    291                        }
    292                    }
    293                }
    294            }
    295        }
    296    }