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