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