    001package net.minecraft.village;
    002
    003import java.util.ArrayList;
    004import java.util.Iterator;
    005import java.util.List;
    006import java.util.TreeMap;
    007import net.minecraft.block.Block;
    008import net.minecraft.entity.EntityLiving;
    009import net.minecraft.entity.monster.EntityIronGolem;
    010import net.minecraft.entity.passive.EntityVillager;
    011import net.minecraft.entity.player.EntityPlayer;
    012import net.minecraft.nbt.NBTTagCompound;
    013import net.minecraft.nbt.NBTTagList;
    014import net.minecraft.util.AxisAlignedBB;
    015import net.minecraft.util.ChunkCoordinates;
    016import net.minecraft.util.MathHelper;
    017import net.minecraft.util.Vec3;
    018import net.minecraft.world.World;
    019
    020public class Village
    021{
    022    private World worldObj;
    023
    024    /** list of VillageDoorInfo objects */
    025    private final List villageDoorInfoList = new ArrayList();
    026
    027    /**
    028     * This is the sum of all door coordinates and used to calculate the actual village center by dividing by the number
    029     * of doors.
    030     */
    031    private final ChunkCoordinates centerHelper = new ChunkCoordinates(0, 0, 0);
    032
    033    /** This is the actual village center. */
    034    private final ChunkCoordinates center = new ChunkCoordinates(0, 0, 0);
    035    private int villageRadius = 0;
    036    private int lastAddDoorTimestamp = 0;
    037    private int tickCounter = 0;
    038    private int numVillagers = 0;
    039
    040    /** Timestamp of tick count when villager last bred */
    041    private int noBreedTicks;
    042
    043    /** List of player reputations with this village */
    044    private TreeMap playerReputation = new TreeMap();
    045    private List villageAgressors = new ArrayList();
    046    private int numIronGolems = 0;
    047
    048    public Village() {}
    049
    050    public Village(World par1World)
    051    {
    052        this.worldObj = par1World;
    053    }
    054
    055    public void func_82691_a(World par1World)
    056    {
    057        this.worldObj = par1World;
    058    }
    059
    060    /**
    061     * Called periodically by VillageCollection
    062     */
    063    public void tick(int par1)
    064    {
    065        this.tickCounter = par1;
    066        this.removeDeadAndOutOfRangeDoors();
    067        this.removeDeadAndOldAgressors();
    068
    069        if (par1 % 20 == 0)
    070        {
    071            this.updateNumVillagers();
    072        }
    073
    074        if (par1 % 30 == 0)
    075        {
    076            this.updateNumIronGolems();
    077        }
    078
    079        int var2 = this.numVillagers / 10;
    080
    081        if (this.numIronGolems < var2 && this.villageDoorInfoList.size() > 20 && this.worldObj.rand.nextInt(7000) == 0)
    082        {
    083            Vec3 var3 = this.tryGetIronGolemSpawningLocation(MathHelper.floor_float((float)this.center.posX), MathHelper.floor_float((float)this.center.posY), MathHelper.floor_float((float)this.center.posZ), 2, 4, 2);
    084
    085            if (var3 != null)
    086            {
    087                EntityIronGolem var4 = new EntityIronGolem(this.worldObj);
    088                var4.setPosition(var3.xCoord, var3.yCoord, var3.zCoord);
    089                this.worldObj.spawnEntityInWorld(var4);
    090                ++this.numIronGolems;
    091            }
    092        }
    093    }
    094
    095    /**
    096     * Tries up to 10 times to get a valid spawning location before eventually failing and returning null.
    097     */
    098    private Vec3 tryGetIronGolemSpawningLocation(int par1, int par2, int par3, int par4, int par5, int par6)
    099    {
    100        for (int var7 = 0; var7 < 10; ++var7)
    101        {
    102            int var8 = par1 + this.worldObj.rand.nextInt(16) - 8;
    103            int var9 = par2 + this.worldObj.rand.nextInt(6) - 3;
    104            int var10 = par3 + this.worldObj.rand.nextInt(16) - 8;
    105
    106            if (this.isInRange(var8, var9, var10) && this.isValidIronGolemSpawningLocation(var8, var9, var10, par4, par5, par6))
    107            {
    108                return this.worldObj.getWorldVec3Pool().getVecFromPool((double)var8, (double)var9, (double)var10);
    109            }
    110        }
    111
    112        return null;
    113    }
    114
    115    private boolean isValidIronGolemSpawningLocation(int par1, int par2, int par3, int par4, int par5, int par6)
    116    {
    117        if (!this.worldObj.doesBlockHaveSolidTopSurface(par1, par2 - 1, par3))
    118        {
    119            return false;
    120        }
    121        else
    122        {
    123            int var7 = par1 - par4 / 2;
    124            int var8 = par3 - par6 / 2;
    125
    126            for (int var9 = var7; var9 < var7 + par4; ++var9)
    127            {
    128                for (int var10 = par2; var10 < par2 + par5; ++var10)
    129                {
    130                    for (int var11 = var8; var11 < var8 + par6; ++var11)
    131                    {
    132                        if (this.worldObj.isBlockNormalCube(var9, var10, var11))
    133                        {
    134                            return false;
    135                        }
    136                    }
    137                }
    138            }
    139
    140            return true;
    141        }
    142    }
    143
    144    private void updateNumIronGolems()
    145    {
    146        List var1 = this.worldObj.getEntitiesWithinAABB(EntityIronGolem.class, AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)(this.center.posX - this.villageRadius), (double)(this.center.posY - 4), (double)(this.center.posZ - this.villageRadius), (double)(this.center.posX + this.villageRadius), (double)(this.center.posY + 4), (double)(this.center.posZ + this.villageRadius)));
    147        this.numIronGolems = var1.size();
    148    }
    149
    150    private void updateNumVillagers()
    151    {
    152        List var1 = this.worldObj.getEntitiesWithinAABB(EntityVillager.class, AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)(this.center.posX - this.villageRadius), (double)(this.center.posY - 4), (double)(this.center.posZ - this.villageRadius), (double)(this.center.posX + this.villageRadius), (double)(this.center.posY + 4), (double)(this.center.posZ + this.villageRadius)));
    153        this.numVillagers = var1.size();
    154
    155        if (this.numVillagers == 0)
    156        {
    157            this.playerReputation.clear();
    158        }
    159    }
    160
    161    public ChunkCoordinates getCenter()
    162    {
    163        return this.center;
    164    }
    165
    166    public int getVillageRadius()
    167    {
    168        return this.villageRadius;
    169    }
    170
    171    /**
    172     * Actually get num village door info entries, but that boils down to number of doors. Called by
    173     * EntityAIVillagerMate and VillageSiege
    174     */
    175    public int getNumVillageDoors()
    176    {
    177        return this.villageDoorInfoList.size();
    178    }
    179
    180    public int getTicksSinceLastDoorAdding()
    181    {
    182        return this.tickCounter - this.lastAddDoorTimestamp;
    183    }
    184
    185    public int getNumVillagers()
    186    {
    187        return this.numVillagers;
    188    }
    189
    190    /**
    191     * Returns true, if the given coordinates are within the bounding box of the village.
    192     */
    193    public boolean isInRange(int par1, int par2, int par3)
    194    {
    195        return this.center.getDistanceSquared(par1, par2, par3) < (float)(this.villageRadius * this.villageRadius);
    196    }
    197
    198    /**
    199     * called only by class EntityAIMoveThroughVillage
    200     */
    201    public List getVillageDoorInfoList()
    202    {
    203        return this.villageDoorInfoList;
    204    }
    205
    206    public VillageDoorInfo findNearestDoor(int par1, int par2, int par3)
    207    {
    208        VillageDoorInfo var4 = null;
    209        int var5 = Integer.MAX_VALUE;
    210        Iterator var6 = this.villageDoorInfoList.iterator();
    211
    212        while (var6.hasNext())
    213        {
    214            VillageDoorInfo var7 = (VillageDoorInfo)var6.next();
    215            int var8 = var7.getDistanceSquared(par1, par2, par3);
    216
    217            if (var8 < var5)
    218            {
    219                var4 = var7;
    220                var5 = var8;
    221            }
    222        }
    223
    224        return var4;
    225    }
    226
    227    /**
    228     * Find a door suitable for shelter. If there are more doors in a distance of 16 blocks, then the least restricted
    229     * one (i.e. the one protecting the lowest number of villagers) of them is chosen, else the nearest one regardless
    230     * of restriction.
    231     */
    232    public VillageDoorInfo findNearestDoorUnrestricted(int par1, int par2, int par3)
    233    {
    234        VillageDoorInfo var4 = null;
    235        int var5 = Integer.MAX_VALUE;
    236        Iterator var6 = this.villageDoorInfoList.iterator();
    237
    238        while (var6.hasNext())
    239        {
    240            VillageDoorInfo var7 = (VillageDoorInfo)var6.next();
    241            int var8 = var7.getDistanceSquared(par1, par2, par3);
    242
    243            if (var8 > 256)
    244            {
    245                var8 *= 1000;
    246            }
    247            else
    248            {
    249                var8 = var7.getDoorOpeningRestrictionCounter();
    250            }
    251
    252            if (var8 < var5)
    253            {
    254                var4 = var7;
    255                var5 = var8;
    256            }
    257        }
    258
    259        return var4;
    260    }
    261
    262    public VillageDoorInfo getVillageDoorAt(int par1, int par2, int par3)
    263    {
    264        if (this.center.getDistanceSquared(par1, par2, par3) > (float)(this.villageRadius * this.villageRadius))
    265        {
    266            return null;
    267        }
    268        else
    269        {
    270            Iterator var4 = this.villageDoorInfoList.iterator();
    271            VillageDoorInfo var5;
    272
    273            do
    274            {
    275                if (!var4.hasNext())
    276                {
    277                    return null;
    278                }
    279
    280                var5 = (VillageDoorInfo)var4.next();
    281            }
    282            while (var5.posX != par1 || var5.posZ != par3 || Math.abs(var5.posY - par2) > 1);
    283
    284            return var5;
    285        }
    286    }
    287
    288    public void addVillageDoorInfo(VillageDoorInfo par1VillageDoorInfo)
    289    {
    290        this.villageDoorInfoList.add(par1VillageDoorInfo);
    291        this.centerHelper.posX += par1VillageDoorInfo.posX;
    292        this.centerHelper.posY += par1VillageDoorInfo.posY;
    293        this.centerHelper.posZ += par1VillageDoorInfo.posZ;
    294        this.updateVillageRadiusAndCenter();
    295        this.lastAddDoorTimestamp = par1VillageDoorInfo.lastActivityTimestamp;
    296    }
    297
    298    /**
    299     * Returns true, if there is not a single village door left. Called by VillageCollection
    300     */
    301    public boolean isAnnihilated()
    302    {
    303        return this.villageDoorInfoList.isEmpty();
    304    }
    305
    306    public void addOrRenewAgressor(EntityLiving par1EntityLiving)
    307    {
    308        Iterator var2 = this.villageAgressors.iterator();
    309        VillageAgressor var3;
    310
    311        do
    312        {
    313            if (!var2.hasNext())
    314            {
    315                this.villageAgressors.add(new VillageAgressor(this, par1EntityLiving, this.tickCounter));
    316                return;
    317            }
    318
    319            var3 = (VillageAgressor)var2.next();
    320        }
    321        while (var3.agressor != par1EntityLiving);
    322
    323        var3.agressionTime = this.tickCounter;
    324    }
    325
    326    public EntityLiving findNearestVillageAggressor(EntityLiving par1EntityLiving)
    327    {
    328        double var2 = Double.MAX_VALUE;
    329        VillageAgressor var4 = null;
    330
    331        for (int var5 = 0; var5 < this.villageAgressors.size(); ++var5)
    332        {
    333            VillageAgressor var6 = (VillageAgressor)this.villageAgressors.get(var5);
    334            double var7 = var6.agressor.getDistanceSqToEntity(par1EntityLiving);
    335
    336            if (var7 <= var2)
    337            {
    338                var4 = var6;
    339                var2 = var7;
    340            }
    341        }
    342
    343        return var4 != null ? var4.agressor : null;
    344    }
    345
    346    public EntityPlayer func_82685_c(EntityLiving par1EntityLiving)
    347    {
    348        double var2 = Double.MAX_VALUE;
    349        EntityPlayer var4 = null;
    350        Iterator var5 = this.playerReputation.keySet().iterator();
    351
    352        while (var5.hasNext())
    353        {
    354            String var6 = (String)var5.next();
    355
    356            if (this.isPlayerReputationTooLow(var6))
    357            {
    358                EntityPlayer var7 = this.worldObj.getPlayerEntityByName(var6);
    359
    360                if (var7 != null)
    361                {
    362                    double var8 = var7.getDistanceSqToEntity(par1EntityLiving);
    363
    364                    if (var8 <= var2)
    365                    {
    366                        var4 = var7;
    367                        var2 = var8;
    368                    }
    369                }
    370            }
    371        }
    372
    373        return var4;
    374    }
    375
    376    private void removeDeadAndOldAgressors()
    377    {
    378        Iterator var1 = this.villageAgressors.iterator();
    379
    380        while (var1.hasNext())
    381        {
    382            VillageAgressor var2 = (VillageAgressor)var1.next();
    383
    384            if (!var2.agressor.isEntityAlive() || Math.abs(this.tickCounter - var2.agressionTime) > 300)
    385            {
    386                var1.remove();
    387            }
    388        }
    389    }
    390
    391    private void removeDeadAndOutOfRangeDoors()
    392    {
    393        boolean var1 = false;
    394        boolean var2 = this.worldObj.rand.nextInt(50) == 0;
    395        Iterator var3 = this.villageDoorInfoList.iterator();
    396
    397        while (var3.hasNext())
    398        {
    399            VillageDoorInfo var4 = (VillageDoorInfo)var3.next();
    400
    401            if (var2)
    402            {
    403                var4.resetDoorOpeningRestrictionCounter();
    404            }
    405
    406            if (!this.isBlockDoor(var4.posX, var4.posY, var4.posZ) || Math.abs(this.tickCounter - var4.lastActivityTimestamp) > 1200)
    407            {
    408                this.centerHelper.posX -= var4.posX;
    409                this.centerHelper.posY -= var4.posY;
    410                this.centerHelper.posZ -= var4.posZ;
    411                var1 = true;
    412                var4.isDetachedFromVillageFlag = true;
    413                var3.remove();
    414            }
    415        }
    416
    417        if (var1)
    418        {
    419            this.updateVillageRadiusAndCenter();
    420        }
    421    }
    422
    423    private boolean isBlockDoor(int par1, int par2, int par3)
    424    {
    425        int var4 = this.worldObj.getBlockId(par1, par2, par3);
    426        return var4 <= 0 ? false : var4 == Block.doorWood.blockID;
    427    }
    428
    429    private void updateVillageRadiusAndCenter()
    430    {
    431        int var1 = this.villageDoorInfoList.size();
    432
    433        if (var1 == 0)
    434        {
    435            this.center.set(0, 0, 0);
    436            this.villageRadius = 0;
    437        }
    438        else
    439        {
    440            this.center.set(this.centerHelper.posX / var1, this.centerHelper.posY / var1, this.centerHelper.posZ / var1);
    441            int var2 = 0;
    442            VillageDoorInfo var4;
    443
    444            for (Iterator var3 = this.villageDoorInfoList.iterator(); var3.hasNext(); var2 = Math.max(var4.getDistanceSquared(this.center.posX, this.center.posY, this.center.posZ), var2))
    445            {
    446                var4 = (VillageDoorInfo)var3.next();
    447            }
    448
    449            this.villageRadius = Math.max(32, (int)Math.sqrt((double)var2) + 1);
    450        }
    451    }
    452
    453    /**
    454     * Return the village reputation for a player
    455     */
    456    public int getReputationForPlayer(String par1Str)
    457    {
    458        Integer var2 = (Integer)this.playerReputation.get(par1Str);
    459        return var2 != null ? var2.intValue() : 0;
    460    }
    461
    462    /**
    463     * Set the village reputation for a player.
    464     */
    465    public int setReputationForPlayer(String par1Str, int par2)
    466    {
    467        int var3 = this.getReputationForPlayer(par1Str);
    468        int var4 = MathHelper.clamp_int(var3 + par2, -30, 10);
    469        this.playerReputation.put(par1Str, Integer.valueOf(var4));
    470        return var4;
    471    }
    472
    473    /**
    474     * Return whether this player has a too low reputation with this village.
    475     */
    476    public boolean isPlayerReputationTooLow(String par1Str)
    477    {
    478        return this.getReputationForPlayer(par1Str) <= -15;
    479    }
    480
    481    /**
    482     * Read this village's data from NBT.
    483     */
    484    public void readVillageDataFromNBT(NBTTagCompound par1NBTTagCompound)
    485    {
    486        this.numVillagers = par1NBTTagCompound.getInteger("PopSize");
    487        this.villageRadius = par1NBTTagCompound.getInteger("Radius");
    488        this.numIronGolems = par1NBTTagCompound.getInteger("Golems");
    489        this.lastAddDoorTimestamp = par1NBTTagCompound.getInteger("Stable");
    490        this.tickCounter = par1NBTTagCompound.getInteger("Tick");
    491        this.noBreedTicks = par1NBTTagCompound.getInteger("MTick");
    492        this.center.posX = par1NBTTagCompound.getInteger("CX");
    493        this.center.posY = par1NBTTagCompound.getInteger("CY");
    494        this.center.posZ = par1NBTTagCompound.getInteger("CZ");
    495        this.centerHelper.posX = par1NBTTagCompound.getInteger("ACX");
    496        this.centerHelper.posY = par1NBTTagCompound.getInteger("ACY");
    497        this.centerHelper.posZ = par1NBTTagCompound.getInteger("ACZ");
    498        NBTTagList var2 = par1NBTTagCompound.getTagList("Doors");
    499
    500        for (int var3 = 0; var3 < var2.tagCount(); ++var3)
    501        {
    502            NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
    503            VillageDoorInfo var5 = new VillageDoorInfo(var4.getInteger("X"), var4.getInteger("Y"), var4.getInteger("Z"), var4.getInteger("IDX"), var4.getInteger("IDZ"), var4.getInteger("TS"));
    504            this.villageDoorInfoList.add(var5);
    505        }
    506
    507        NBTTagList var6 = par1NBTTagCompound.getTagList("Players");
    508
    509        for (int var7 = 0; var7 < var6.tagCount(); ++var7)
    510        {
    511            NBTTagCompound var8 = (NBTTagCompound)var6.tagAt(var7);
    512            this.playerReputation.put(var8.getString("Name"), Integer.valueOf(var8.getInteger("S")));
    513        }
    514    }
    515
    516    /**
    517     * Write this village's data to NBT.
    518     */
    519    public void writeVillageDataToNBT(NBTTagCompound par1NBTTagCompound)
    520    {
    521        par1NBTTagCompound.setInteger("PopSize", this.numVillagers);
    522        par1NBTTagCompound.setInteger("Radius", this.villageRadius);
    523        par1NBTTagCompound.setInteger("Golems", this.numIronGolems);
    524        par1NBTTagCompound.setInteger("Stable", this.lastAddDoorTimestamp);
    525        par1NBTTagCompound.setInteger("Tick", this.tickCounter);
    526        par1NBTTagCompound.setInteger("MTick", this.noBreedTicks);
    527        par1NBTTagCompound.setInteger("CX", this.center.posX);
    528        par1NBTTagCompound.setInteger("CY", this.center.posY);
    529        par1NBTTagCompound.setInteger("CZ", this.center.posZ);
    530        par1NBTTagCompound.setInteger("ACX", this.centerHelper.posX);
    531        par1NBTTagCompound.setInteger("ACY", this.centerHelper.posY);
    532        par1NBTTagCompound.setInteger("ACZ", this.centerHelper.posZ);
    533        NBTTagList var2 = new NBTTagList("Doors");
    534        Iterator var3 = this.villageDoorInfoList.iterator();
    535
    536        while (var3.hasNext())
    537        {
    538            VillageDoorInfo var4 = (VillageDoorInfo)var3.next();
    539            NBTTagCompound var5 = new NBTTagCompound("Door");
    540            var5.setInteger("X", var4.posX);
    541            var5.setInteger("Y", var4.posY);
    542            var5.setInteger("Z", var4.posZ);
    543            var5.setInteger("IDX", var4.insideDirectionX);
    544            var5.setInteger("IDZ", var4.insideDirectionZ);
    545            var5.setInteger("TS", var4.lastActivityTimestamp);
    546            var2.appendTag(var5);
    547        }
    548
    549        par1NBTTagCompound.setTag("Doors", var2);
    550        NBTTagList var7 = new NBTTagList("Players");
    551        Iterator var8 = this.playerReputation.keySet().iterator();
    552
    553        while (var8.hasNext())
    554        {
    555            String var9 = (String)var8.next();
    556            NBTTagCompound var6 = new NBTTagCompound(var9);
    557            var6.setString("Name", var9);
    558            var6.setInteger("S", ((Integer)this.playerReputation.get(var9)).intValue());
    559            var7.appendTag(var6);
    560        }
    561
    562        par1NBTTagCompound.setTag("Players", var7);
    563    }
    564
    565    /**
    566     * Prevent villager breeding for a fixed interval of time
    567     */
    568    public void endMatingSeason()
    569    {
    570        this.noBreedTicks = this.tickCounter;
    571    }
    572
    573    /**
    574     * Return whether villagers mating refractory period has passed
    575     */
    576    public boolean isMatingSeason()
    577    {
    578        return this.noBreedTicks == 0 || this.tickCounter - this.noBreedTicks >= 3600;
    579    }
    580
    581    public void func_82683_b(int par1)
    582    {
    583        Iterator var2 = this.playerReputation.keySet().iterator();
    584
    585        while (var2.hasNext())
    586        {
    587            String var3 = (String)var2.next();
    588            this.setReputationForPlayer(var3, par1);
    589        }
    590    }
    591}