    001    package net.minecraft.world;
    002
    003    import java.util.ArrayList;
    004    import java.util.HashMap;
    005    import java.util.HashSet;
    006    import java.util.Iterator;
    007    import java.util.List;
    008    import java.util.Map;
    009    import java.util.Random;
    010    import net.minecraft.block.Block;
    011    import net.minecraft.entity.Entity;
    012    import net.minecraft.entity.player.EntityPlayer;
    013    import net.minecraft.util.AxisAlignedBB;
    014    import net.minecraft.util.DamageSource;
    015    import net.minecraft.util.MathHelper;
    016    import net.minecraft.util.Vec3;
    017
    018    public class Explosion
    019    {
    020        /** whether or not the explosion sets fire to blocks around it */
    021        public boolean isFlaming = false;
    022
    023        /** whether or not this explosion spawns smoke particles */
    024        public boolean isSmoking = true;
    025        private int field_77289_h = 16;
    026        private Random explosionRNG = new Random();
    027        private World worldObj;
    028        public double explosionX;
    029        public double explosionY;
    030        public double explosionZ;
    031        public Entity exploder;
    032        public float explosionSize;
    033
    034        /** A list of ChunkPositions of blocks affected by this explosion */
    035        public List affectedBlockPositions = new ArrayList();
    036        private Map field_77288_k = new HashMap();
    037
    038        public Explosion(World par1World, Entity par2Entity, double par3, double par5, double par7, float par9)
    039        {
    040            this.worldObj = par1World;
    041            this.exploder = par2Entity;
    042            this.explosionSize = par9;
    043            this.explosionX = par3;
    044            this.explosionY = par5;
    045            this.explosionZ = par7;
    046        }
    047
    048        /**
    049         * Does the first part of the explosion (destroy blocks)
    050         */
    051        public void doExplosionA()
    052        {
    053            float var1 = this.explosionSize;
    054            HashSet var2 = new HashSet();
    055            int var3;
    056            int var4;
    057            int var5;
    058            double var15;
    059            double var17;
    060            double var19;
    061
    062            for (var3 = 0; var3 < this.field_77289_h; ++var3)
    063            {
    064                for (var4 = 0; var4 < this.field_77289_h; ++var4)
    065                {
    066                    for (var5 = 0; var5 < this.field_77289_h; ++var5)
    067                    {
    068                        if (var3 == 0 || var3 == this.field_77289_h - 1 || var4 == 0 || var4 == this.field_77289_h - 1 || var5 == 0 || var5 == this.field_77289_h - 1)
    069                        {
    070                            double var6 = (double)((float)var3 / ((float)this.field_77289_h - 1.0F) * 2.0F - 1.0F);
    071                            double var8 = (double)((float)var4 / ((float)this.field_77289_h - 1.0F) * 2.0F - 1.0F);
    072                            double var10 = (double)((float)var5 / ((float)this.field_77289_h - 1.0F) * 2.0F - 1.0F);
    073                            double var12 = Math.sqrt(var6 * var6 + var8 * var8 + var10 * var10);
    074                            var6 /= var12;
    075                            var8 /= var12;
    076                            var10 /= var12;
    077                            float var14 = this.explosionSize * (0.7F + this.worldObj.rand.nextFloat() * 0.6F);
    078                            var15 = this.explosionX;
    079                            var17 = this.explosionY;
    080                            var19 = this.explosionZ;
    081
    082                            for (float var21 = 0.3F; var14 > 0.0F; var14 -= var21 * 0.75F)
    083                            {
    084                                int var22 = MathHelper.floor_double(var15);
    085                                int var23 = MathHelper.floor_double(var17);
    086                                int var24 = MathHelper.floor_double(var19);
    087                                int var25 = this.worldObj.getBlockId(var22, var23, var24);
    088
    089                                if (var25 > 0)
    090                                {
    091                                    Block var26 = Block.blocksList[var25];
    092                                    float var27 = this.exploder != null ? this.exploder.func_82146_a(this, var26, var22, var23, var24) : var26.getExplosionResistance(this.exploder, worldObj, var22, var23, var24, explosionX, explosionY, explosionZ);
    093                                    var14 -= (var27 + 0.3F) * var21;
    094                                }
    095
    096                                if (var14 > 0.0F)
    097                                {
    098                                    var2.add(new ChunkPosition(var22, var23, var24));
    099                                }
    100
    101                                var15 += var6 * (double)var21;
    102                                var17 += var8 * (double)var21;
    103                                var19 += var10 * (double)var21;
    104                            }
    105                        }
    106                    }
    107                }
    108            }
    109
    110            this.affectedBlockPositions.addAll(var2);
    111            this.explosionSize *= 2.0F;
    112            var3 = MathHelper.floor_double(this.explosionX - (double)this.explosionSize - 1.0D);
    113            var4 = MathHelper.floor_double(this.explosionX + (double)this.explosionSize + 1.0D);
    114            var5 = MathHelper.floor_double(this.explosionY - (double)this.explosionSize - 1.0D);
    115            int var28 = MathHelper.floor_double(this.explosionY + (double)this.explosionSize + 1.0D);
    116            int var7 = MathHelper.floor_double(this.explosionZ - (double)this.explosionSize - 1.0D);
    117            int var29 = MathHelper.floor_double(this.explosionZ + (double)this.explosionSize + 1.0D);
    118            List var9 = this.worldObj.getEntitiesWithinAABBExcludingEntity(this.exploder, AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)var3, (double)var5, (double)var7, (double)var4, (double)var28, (double)var29));
    119            Vec3 var30 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.explosionX, this.explosionY, this.explosionZ);
    120
    121            for (int var11 = 0; var11 < var9.size(); ++var11)
    122            {
    123                Entity var31 = (Entity)var9.get(var11);
    124                double var13 = var31.getDistance(this.explosionX, this.explosionY, this.explosionZ) / (double)this.explosionSize;
    125
    126                if (var13 <= 1.0D)
    127                {
    128                    var15 = var31.posX - this.explosionX;
    129                    var17 = var31.posY + (double)var31.getEyeHeight() - this.explosionY;
    130                    var19 = var31.posZ - this.explosionZ;
    131                    double var33 = (double)MathHelper.sqrt_double(var15 * var15 + var17 * var17 + var19 * var19);
    132
    133                    if (var33 != 0.0D)
    134                    {
    135                        var15 /= var33;
    136                        var17 /= var33;
    137                        var19 /= var33;
    138                        double var32 = (double)this.worldObj.getBlockDensity(var30, var31.boundingBox);
    139                        double var34 = (1.0D - var13) * var32;
    140                        var31.attackEntityFrom(DamageSource.explosion, (int)((var34 * var34 + var34) / 2.0D * 8.0D * (double)this.explosionSize + 1.0D));
    141                        var31.motionX += var15 * var34;
    142                        var31.motionY += var17 * var34;
    143                        var31.motionZ += var19 * var34;
    144
    145                        if (var31 instanceof EntityPlayer)
    146                        {
    147                            this.field_77288_k.put((EntityPlayer)var31, this.worldObj.getWorldVec3Pool().getVecFromPool(var15 * var34, var17 * var34, var19 * var34));
    148                        }
    149                    }
    150                }
    151            }
    152
    153            this.explosionSize = var1;
    154        }
    155
    156        /**
    157         * Does the second part of the explosion (sound, particles, drop spawn)
    158         */
    159        public void doExplosionB(boolean par1)
    160        {
    161            this.worldObj.playSoundEffect(this.explosionX, this.explosionY, this.explosionZ, "random.explode", 4.0F, (1.0F + (this.worldObj.rand.nextFloat() - this.worldObj.rand.nextFloat()) * 0.2F) * 0.7F);
    162
    163            if (this.explosionSize >= 2.0F && this.isSmoking)
    164            {
    165                this.worldObj.spawnParticle("hugeexplosion", this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D);
    166            }
    167            else
    168            {
    169                this.worldObj.spawnParticle("largeexplode", this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D);
    170            }
    171
    172            Iterator var2;
    173            ChunkPosition var3;
    174            int var4;
    175            int var5;
    176            int var6;
    177            int var7;
    178
    179            if (this.isSmoking)
    180            {
    181                var2 = this.affectedBlockPositions.iterator();
    182
    183                while (var2.hasNext())
    184                {
    185                    var3 = (ChunkPosition)var2.next();
    186                    var4 = var3.x;
    187                    var5 = var3.y;
    188                    var6 = var3.z;
    189                    var7 = this.worldObj.getBlockId(var4, var5, var6);
    190
    191                    if (par1)
    192                    {
    193                        double var8 = (double)((float)var4 + this.worldObj.rand.nextFloat());
    194                        double var10 = (double)((float)var5 + this.worldObj.rand.nextFloat());
    195                        double var12 = (double)((float)var6 + this.worldObj.rand.nextFloat());
    196                        double var14 = var8 - this.explosionX;
    197                        double var16 = var10 - this.explosionY;
    198                        double var18 = var12 - this.explosionZ;
    199                        double var20 = (double)MathHelper.sqrt_double(var14 * var14 + var16 * var16 + var18 * var18);
    200                        var14 /= var20;
    201                        var16 /= var20;
    202                        var18 /= var20;
    203                        double var22 = 0.5D / (var20 / (double)this.explosionSize + 0.1D);
    204                        var22 *= (double)(this.worldObj.rand.nextFloat() * this.worldObj.rand.nextFloat() + 0.3F);
    205                        var14 *= var22;
    206                        var16 *= var22;
    207                        var18 *= var22;
    208                        this.worldObj.spawnParticle("explode", (var8 + this.explosionX * 1.0D) / 2.0D, (var10 + this.explosionY * 1.0D) / 2.0D, (var12 + this.explosionZ * 1.0D) / 2.0D, var14, var16, var18);
    209                        this.worldObj.spawnParticle("smoke", var8, var10, var12, var14, var16, var18);
    210                    }
    211
    212                    if (var7 > 0)
    213                    {
    214                        Block var25 = Block.blocksList[var7];
    215
    216                        if (var25.func_85103_a(this))
    217                        {
    218                            var25.dropBlockAsItemWithChance(this.worldObj, var4, var5, var6, this.worldObj.getBlockMetadata(var4, var5, var6), 0.3F, 0);
    219                        }
    220
    221                        if (this.worldObj.setBlockAndMetadataWithUpdate(var4, var5, var6, 0, 0, this.worldObj.isRemote))
    222                        {
    223                            this.worldObj.notifyBlocksOfNeighborChange(var4, var5, var6, 0);
    224                        }
    225
    226                        var25.onBlockDestroyedByExplosion(this.worldObj, var4, var5, var6);
    227                    }
    228                }
    229            }
    230
    231            if (this.isFlaming)
    232            {
    233                var2 = this.affectedBlockPositions.iterator();
    234
    235                while (var2.hasNext())
    236                {
    237                    var3 = (ChunkPosition)var2.next();
    238                    var4 = var3.x;
    239                    var5 = var3.y;
    240                    var6 = var3.z;
    241                    var7 = this.worldObj.getBlockId(var4, var5, var6);
    242                    int var24 = this.worldObj.getBlockId(var4, var5 - 1, var6);
    243
    244                    if (var7 == 0 && Block.opaqueCubeLookup[var24] && this.explosionRNG.nextInt(3) == 0)
    245                    {
    246                        this.worldObj.setBlockWithNotify(var4, var5, var6, Block.fire.blockID);
    247                    }
    248                }
    249            }
    250        }
    251
    252        public Map func_77277_b()
    253        {
    254            return this.field_77288_k;
    255        }
    256    }