    001package net.minecraft.village;
    002
    003import java.util.Iterator;
    004import java.util.List;
    005import net.minecraft.entity.EnumCreatureType;
    006import net.minecraft.entity.monster.EntityZombie;
    007import net.minecraft.entity.player.EntityPlayer;
    008import net.minecraft.util.ChunkCoordinates;
    009import net.minecraft.util.MathHelper;
    010import net.minecraft.util.Vec3;
    011import net.minecraft.world.SpawnerAnimals;
    012import net.minecraft.world.World;
    013
    014public class VillageSiege
    015{
    016    private World worldObj;
    017    private boolean field_75535_b = false;
    018    private int field_75536_c = -1;
    019    private int field_75533_d;
    020    private int field_75534_e;
    021
    022    /** Instance of Village. */
    023    private Village theVillage;
    024    private int field_75532_g;
    025    private int field_75538_h;
    026    private int field_75539_i;
    027
    028    public VillageSiege(World par1World)
    029    {
    030        this.worldObj = par1World;
    031    }
    032
    033    /**
    034     * Runs a single tick for the village siege
    035     */
    036    public void tick()
    037    {
    038        boolean flag = false;
    039
    040        if (flag)
    041        {
    042            if (this.field_75536_c == 2)
    043            {
    044                this.field_75533_d = 100;
    045                return;
    046            }
    047        }
    048        else
    049        {
    050            if (this.worldObj.isDaytime())
    051            {
    052                this.field_75536_c = 0;
    053                return;
    054            }
    055
    056            if (this.field_75536_c == 2)
    057            {
    058                return;
    059            }
    060
    061            if (this.field_75536_c == 0)
    062            {
    063                float f = this.worldObj.getCelestialAngle(0.0F);
    064
    065                if ((double)f < 0.5D || (double)f > 0.501D)
    066                {
    067                    return;
    068                }
    069
    070                this.field_75536_c = this.worldObj.rand.nextInt(10) == 0 ? 1 : 2;
    071                this.field_75535_b = false;
    072
    073                if (this.field_75536_c == 2)
    074                {
    075                    return;
    076                }
    077            }
    078        }
    079
    080        if (!this.field_75535_b)
    081        {
    082            if (!this.func_75529_b())
    083            {
    084                return;
    085            }
    086
    087            this.field_75535_b = true;
    088        }
    089
    090        if (this.field_75534_e > 0)
    091        {
    092            --this.field_75534_e;
    093        }
    094        else
    095        {
    096            this.field_75534_e = 2;
    097
    098            if (this.field_75533_d > 0)
    099            {
    100                this.spawnZombie();
    101                --this.field_75533_d;
    102            }
    103            else
    104            {
    105                this.field_75536_c = 2;
    106            }
    107        }
    108    }
    109
    110    private boolean func_75529_b()
    111    {
    112        List list = this.worldObj.playerEntities;
    113        Iterator iterator = list.iterator();
    114
    115        while (iterator.hasNext())
    116        {
    117            EntityPlayer entityplayer = (EntityPlayer)iterator.next();
    118            this.theVillage = this.worldObj.villageCollectionObj.findNearestVillage((int)entityplayer.posX, (int)entityplayer.posY, (int)entityplayer.posZ, 1);
    119
    120            if (this.theVillage != null && this.theVillage.getNumVillageDoors() >= 10 && this.theVillage.getTicksSinceLastDoorAdding() >= 20 && this.theVillage.getNumVillagers() >= 20)
    121            {
    122                ChunkCoordinates chunkcoordinates = this.theVillage.getCenter();
    123                float f = (float)this.theVillage.getVillageRadius();
    124                boolean flag = false;
    125                int i = 0;
    126
    127                while (true)
    128                {
    129                    if (i < 10)
    130                    {
    131                        this.field_75532_g = chunkcoordinates.posX + (int)((double)(MathHelper.cos(this.worldObj.rand.nextFloat() * (float)Math.PI * 2.0F) * f) * 0.9D);
    132                        this.field_75538_h = chunkcoordinates.posY;
    133                        this.field_75539_i = chunkcoordinates.posZ + (int)((double)(MathHelper.sin(this.worldObj.rand.nextFloat() * (float)Math.PI * 2.0F) * f) * 0.9D);
    134                        flag = false;
    135                        Iterator iterator1 = this.worldObj.villageCollectionObj.getVillageList().iterator();
    136
    137                        while (iterator1.hasNext())
    138                        {
    139                            Village village = (Village)iterator1.next();
    140
    141                            if (village != this.theVillage && village.isInRange(this.field_75532_g, this.field_75538_h, this.field_75539_i))
    142                            {
    143                                flag = true;
    144                                break;
    145                            }
    146                        }
    147
    148                        if (flag)
    149                        {
    150                            ++i;
    151                            continue;
    152                        }
    153                    }
    154
    155                    if (flag)
    156                    {
    157                        return false;
    158                    }
    159
    160                    Vec3 vec3 = this.func_75527_a(this.field_75532_g, this.field_75538_h, this.field_75539_i);
    161
    162                    if (vec3 != null)
    163                    {
    164                        this.field_75534_e = 0;
    165                        this.field_75533_d = 20;
    166                        return true;
    167                    }
    168
    169                    break;
    170                }
    171            }
    172        }
    173
    174        return false;
    175    }
    176
    177    private boolean spawnZombie()
    178    {
    179        Vec3 vec3 = this.func_75527_a(this.field_75532_g, this.field_75538_h, this.field_75539_i);
    180
    181        if (vec3 == null)
    182        {
    183            return false;
    184        }
    185        else
    186        {
    187            EntityZombie entityzombie;
    188
    189            try
    190            {
    191                entityzombie = new EntityZombie(this.worldObj);
    192                entityzombie.initCreature();
    193                entityzombie.setVillager(false);
    194            }
    195            catch (Exception exception)
    196            {
    197                exception.printStackTrace();
    198                return false;
    199            }
    200
    201            entityzombie.setLocationAndAngles(vec3.xCoord, vec3.yCoord, vec3.zCoord, this.worldObj.rand.nextFloat() * 360.0F, 0.0F);
    202            this.worldObj.spawnEntityInWorld(entityzombie);
    203            ChunkCoordinates chunkcoordinates = this.theVillage.getCenter();
    204            entityzombie.setHomeArea(chunkcoordinates.posX, chunkcoordinates.posY, chunkcoordinates.posZ, this.theVillage.getVillageRadius());
    205            return true;
    206        }
    207    }
    208
    209    private Vec3 func_75527_a(int par1, int par2, int par3)
    210    {
    211        for (int l = 0; l < 10; ++l)
    212        {
    213            int i1 = par1 + this.worldObj.rand.nextInt(16) - 8;
    214            int j1 = par2 + this.worldObj.rand.nextInt(6) - 3;
    215            int k1 = par3 + this.worldObj.rand.nextInt(16) - 8;
    216
    217            if (this.theVillage.isInRange(i1, j1, k1) && SpawnerAnimals.canCreatureTypeSpawnAtLocation(EnumCreatureType.monster, this.worldObj, i1, j1, k1))
    218            {
    219                this.worldObj.getWorldVec3Pool().getVecFromPool((double)i1, (double)j1, (double)k1);
    220            }
    221        }
    222
    223        return null;
    224    }
    225}