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