001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import java.util.Iterator;
006    import java.util.List;
007    
008    public class EntityWitch extends EntityMob implements IRangedAttackMob
009    {
010        private static final int[] field_82199_d = new int[] {Item.lightStoneDust.shiftedIndex, Item.sugar.shiftedIndex, Item.redstone.shiftedIndex, Item.spiderEye.shiftedIndex, Item.glassBottle.shiftedIndex, Item.gunpowder.shiftedIndex, Item.stick.shiftedIndex, Item.stick.shiftedIndex};
011        private int field_82200_e = 0;
012    
013        public EntityWitch(World par1World)
014        {
015            super(par1World);
016            this.texture = "/mob/villager/witch.png";
017            this.moveSpeed = 0.25F;
018            this.tasks.addTask(1, new EntityAISwimming(this));
019            this.tasks.addTask(2, new EntityAIArrowAttack(this, this.moveSpeed, 60, 10.0F));
020            this.tasks.addTask(2, new EntityAIWander(this, this.moveSpeed));
021            this.tasks.addTask(3, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
022            this.tasks.addTask(3, new EntityAILookIdle(this));
023            this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
024            this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 16.0F, 0, true));
025        }
026    
027        protected void entityInit()
028        {
029            super.entityInit();
030            this.getDataWatcher().addObject(21, Byte.valueOf((byte)0));
031        }
032    
033        /**
034         * Returns the sound this mob makes while it's alive.
035         */
036        protected String getLivingSound()
037        {
038            return "mob.witch.idle";
039        }
040    
041        /**
042         * Returns the sound this mob makes when it is hurt.
043         */
044        protected String getHurtSound()
045        {
046            return "mob.witch.hurt";
047        }
048    
049        /**
050         * Returns the sound this mob makes on death.
051         */
052        protected String getDeathSound()
053        {
054            return "mob.witch.death";
055        }
056    
057        public void func_82197_f(boolean par1)
058        {
059            this.getDataWatcher().updateObject(21, Byte.valueOf((byte)(par1 ? 1 : 0)));
060        }
061    
062        public boolean func_82198_m()
063        {
064            return this.getDataWatcher().getWatchableObjectByte(21) == 1;
065        }
066    
067        public int getMaxHealth()
068        {
069            return 26;
070        }
071    
072        /**
073         * Returns true if the newer Entity AI code should be run
074         */
075        public boolean isAIEnabled()
076        {
077            return true;
078        }
079    
080        /**
081         * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
082         * use this to react to sunlight and start to burn.
083         */
084        public void onLivingUpdate()
085        {
086            if (!this.worldObj.isRemote)
087            {
088                if (this.func_82198_m())
089                {
090                    if (this.field_82200_e-- <= 0)
091                    {
092                        this.func_82197_f(false);
093                        ItemStack var1 = this.getHeldItem();
094                        this.func_70062_b(0, (ItemStack)null);
095    
096                        if (var1 != null && var1.itemID == Item.potion.shiftedIndex)
097                        {
098                            List var2 = Item.potion.getEffects(var1);
099    
100                            if (var2 != null)
101                            {
102                                Iterator var3 = var2.iterator();
103    
104                                while (var3.hasNext())
105                                {
106                                    PotionEffect var4 = (PotionEffect)var3.next();
107                                    this.addPotionEffect(new PotionEffect(var4));
108                                }
109                            }
110                        }
111                    }
112                }
113                else
114                {
115                    short var5 = -1;
116    
117                    if (this.rand.nextFloat() < 0.15F && this.isBurning() && !this.isPotionActive(Potion.fireResistance))
118                    {
119                        var5 = 16307;
120                    }
121                    else if (this.rand.nextFloat() < 0.05F && this.health < this.getMaxHealth())
122                    {
123                        var5 = 16341;
124                    }
125                    else if (this.rand.nextFloat() < 0.25F && this.getAttackTarget() != null && !this.isPotionActive(Potion.moveSpeed) && this.getAttackTarget().getDistanceSqToEntity(this) > 121.0D)
126                    {
127                        var5 = 16274;
128                    }
129                    else if (this.rand.nextFloat() < 0.25F && this.getAttackTarget() != null && !this.isPotionActive(Potion.moveSpeed) && this.getAttackTarget().getDistanceSqToEntity(this) > 121.0D)
130                    {
131                        var5 = 16274;
132                    }
133    
134                    if (var5 > -1)
135                    {
136                        this.func_70062_b(0, new ItemStack(Item.potion, 1, var5));
137                        this.field_82200_e = this.getHeldItem().getMaxItemUseDuration();
138                        this.func_82197_f(true);
139                    }
140                }
141    
142                if (this.rand.nextFloat() < 7.5E-4F)
143                {
144                    this.worldObj.setEntityState(this, (byte)15);
145                }
146            }
147    
148            super.onLivingUpdate();
149        }
150    
151        /**
152         * Reduces damage, depending on potions
153         */
154        protected int applyPotionDamageCalculations(DamageSource par1DamageSource, int par2)
155        {
156            par2 = super.applyPotionDamageCalculations(par1DamageSource, par2);
157    
158            if (par1DamageSource.getEntity() == this)
159            {
160                par2 = 0;
161            }
162    
163            if (par1DamageSource.func_82725_o())
164            {
165                par2 = (int)((double)par2 * 0.15D);
166            }
167    
168            return par2;
169        }
170    
171        @SideOnly(Side.CLIENT)
172        public void handleHealthUpdate(byte par1)
173        {
174            if (par1 == 15)
175            {
176                for (int var2 = 0; var2 < this.rand.nextInt(35) + 10; ++var2)
177                {
178                    this.worldObj.spawnParticle("witchMagic", this.posX + this.rand.nextGaussian() * 0.12999999523162842D, this.boundingBox.maxY + 0.5D + this.rand.nextGaussian() * 0.12999999523162842D, this.posZ + this.rand.nextGaussian() * 0.12999999523162842D, 0.0D, 0.0D, 0.0D);
179                }
180            }
181            else
182            {
183                super.handleHealthUpdate(par1);
184            }
185        }
186    
187        /**
188         * This method returns a value to be applied directly to entity speed, this factor is less than 1 when a slowdown
189         * potion effect is applied, more than 1 when a haste potion effect is applied and 2 for fleeing entities.
190         */
191        public float getSpeedModifier()
192        {
193            float var1 = super.getSpeedModifier();
194    
195            if (this.func_82198_m())
196            {
197                var1 *= 0.75F;
198            }
199    
200            return var1;
201        }
202    
203        /**
204         * Drop 0-2 items of this living's type
205         */
206        protected void dropFewItems(boolean par1, int par2)
207        {
208            int var3 = this.rand.nextInt(3) + 1;
209    
210            for (int var4 = 0; var4 < var3; ++var4)
211            {
212                int var5 = this.rand.nextInt(3);
213                int var6 = field_82199_d[this.rand.nextInt(field_82199_d.length)];
214    
215                if (par2 > 0)
216                {
217                    var5 += this.rand.nextInt(par2 + 1);
218                }
219    
220                for (int var7 = 0; var7 < var5; ++var7)
221                {
222                    this.dropItem(var6, 1);
223                }
224            }
225        }
226    
227        public void func_82196_d(EntityLiving par1EntityLiving)
228        {
229            if (!this.func_82198_m())
230            {
231                EntityPotion var2 = new EntityPotion(this.worldObj, this, 32732);
232                var2.rotationPitch -= -20.0F;
233                double var3 = par1EntityLiving.posX + par1EntityLiving.motionX - this.posX;
234                double var5 = par1EntityLiving.posY + (double)par1EntityLiving.getEyeHeight() - 1.100000023841858D - this.posY;
235                double var7 = par1EntityLiving.posZ + par1EntityLiving.motionZ - this.posZ;
236                float var9 = MathHelper.sqrt_double(var3 * var3 + var7 * var7);
237    
238                if (var9 >= 8.0F && !par1EntityLiving.isPotionActive(Potion.moveSlowdown))
239                {
240                    var2.func_82340_a(32698);
241                }
242                else if (par1EntityLiving.getHealth() >= 8 && !par1EntityLiving.isPotionActive(Potion.poison))
243                {
244                    var2.func_82340_a(32660);
245                }
246                else if (var9 <= 3.0F && !par1EntityLiving.isPotionActive(Potion.weakness) && this.rand.nextFloat() < 0.25F)
247                {
248                    var2.func_82340_a(32696);
249                }
250    
251                var2.setThrowableHeading(var3, var5 + (double)(var9 * 0.2F), var7, 0.75F, 8.0F);
252                this.worldObj.spawnEntityInWorld(var2);
253            }
254        }
255    }