    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.EntityLiving;
    014import net.minecraft.entity.item.EntityTNTPrimed;
    015import net.minecraft.entity.player.EntityPlayer;
    016import net.minecraft.util.AxisAlignedBB;
    017import net.minecraft.util.DamageSource;
    018import net.minecraft.util.MathHelper;
    019import net.minecraft.util.Vec3;
    020
    021public class Explosion
    022{
    023    /** whether or not the explosion sets fire to blocks around it */
    024    public boolean isFlaming = false;
    025
    026    /** whether or not this explosion spawns smoke particles */
    027    public boolean isSmoking = true;
    028    private int field_77289_h = 16;
    029    private Random explosionRNG = new Random();
    030    private World worldObj;
    031    public double explosionX;
    032    public double explosionY;
    033    public double explosionZ;
    034    public Entity exploder;
    035    public float explosionSize;
    036
    037    /** A list of ChunkPositions of blocks affected by this explosion */
    038    public List affectedBlockPositions = new ArrayList();
    039    private Map field_77288_k = new HashMap();
    040
    041    public Explosion(World par1World, Entity par2Entity, double par3, double par5, double par7, float par9)
    042    {
    043        this.worldObj = par1World;
    044        this.exploder = par2Entity;
    045        this.explosionSize = par9;
    046        this.explosionX = par3;
    047        this.explosionY = par5;
    048        this.explosionZ = par7;
    049    }
    050
    051    /**
    052     * Does the first part of the explosion (destroy blocks)
    053     */
    054    public void doExplosionA()
    055    {
    056        float f = this.explosionSize;
    057        HashSet hashset = new HashSet();
    058        int i;
    059        int j;
    060        int k;
    061        double d0;
    062        double d1;
    063        double d2;
    064
    065        for (i = 0; i < this.field_77289_h; ++i)
    066        {
    067            for (j = 0; j < this.field_77289_h; ++j)
    068            {
    069                for (k = 0; k < this.field_77289_h; ++k)
    070                {
    071                    if (i == 0 || i == this.field_77289_h - 1 || j == 0 || j == this.field_77289_h - 1 || k == 0 || k == this.field_77289_h - 1)
    072                    {
    073                        double d3 = (double)((float)i / ((float)this.field_77289_h - 1.0F) * 2.0F - 1.0F);
    074                        double d4 = (double)((float)j / ((float)this.field_77289_h - 1.0F) * 2.0F - 1.0F);
    075                        double d5 = (double)((float)k / ((float)this.field_77289_h - 1.0F) * 2.0F - 1.0F);
    076                        double d6 = Math.sqrt(d3 * d3 + d4 * d4 + d5 * d5);
    077                        d3 /= d6;
    078                        d4 /= d6;
    079                        d5 /= d6;
    080                        float f1 = this.explosionSize * (0.7F + this.worldObj.rand.nextFloat() * 0.6F);
    081                        d0 = this.explosionX;
    082                        d1 = this.explosionY;
    083                        d2 = this.explosionZ;
    084
    085                        for (float f2 = 0.3F; f1 > 0.0F; f1 -= f2 * 0.75F)
    086                        {
    087                            int l = MathHelper.floor_double(d0);
    088                            int i1 = MathHelper.floor_double(d1);
    089                            int j1 = MathHelper.floor_double(d2);
    090                            int k1 = this.worldObj.getBlockId(l, i1, j1);
    091
    092                            if (k1 > 0)
    093                            {
    094                                Block block = Block.blocksList[k1];
    095                                float f3 = this.exploder != null ? this.exploder.func_82146_a(this, this.worldObj, l, i1, j1, block) : block.getExplosionResistance(this.exploder, worldObj, l, i1, j1, explosionX, explosionY, explosionZ);
    096                                f1 -= (f3 + 0.3F) * f2;
    097                            }
    098
    099                            if (f1 > 0.0F && (this.exploder == null || this.exploder.func_96091_a(this, this.worldObj, l, i1, j1, k1, f1)))
    100                            {
    101                                hashset.add(new ChunkPosition(l, i1, j1));
    102                            }
    103
    104                            d0 += d3 * (double)f2;
    105                            d1 += d4 * (double)f2;
    106                            d2 += d5 * (double)f2;
    107                        }
    108                    }
    109                }
    110            }
    111        }
    112
    113        this.affectedBlockPositions.addAll(hashset);
    114        this.explosionSize *= 2.0F;
    115        i = MathHelper.floor_double(this.explosionX - (double)this.explosionSize - 1.0D);
    116        j = MathHelper.floor_double(this.explosionX + (double)this.explosionSize + 1.0D);
    117        k = MathHelper.floor_double(this.explosionY - (double)this.explosionSize - 1.0D);
    118        int l1 = MathHelper.floor_double(this.explosionY + (double)this.explosionSize + 1.0D);
    119        int i2 = MathHelper.floor_double(this.explosionZ - (double)this.explosionSize - 1.0D);
    120        int j2 = MathHelper.floor_double(this.explosionZ + (double)this.explosionSize + 1.0D);
    121        List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this.exploder, AxisAlignedBB.getAABBPool().getAABB((double)i, (double)k, (double)i2, (double)j, (double)l1, (double)j2));
    122        Vec3 vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.explosionX, this.explosionY, this.explosionZ);
    123
    124        for (int k2 = 0; k2 < list.size(); ++k2)
    125        {
    126            Entity entity = (Entity)list.get(k2);
    127            double d7 = entity.getDistance(this.explosionX, this.explosionY, this.explosionZ) / (double)this.explosionSize;
    128
    129            if (d7 <= 1.0D)
    130            {
    131                d0 = entity.posX - this.explosionX;
    132                d1 = entity.posY + (double)entity.getEyeHeight() - this.explosionY;
    133                d2 = entity.posZ - this.explosionZ;
    134                double d8 = (double)MathHelper.sqrt_double(d0 * d0 + d1 * d1 + d2 * d2);
    135
    136                if (d8 != 0.0D)
    137                {
    138                    d0 /= d8;
    139                    d1 /= d8;
    140                    d2 /= d8;
    141                    double d9 = (double)this.worldObj.getBlockDensity(vec3, entity.boundingBox);
    142                    double d10 = (1.0D - d7) * d9;
    143                    entity.attackEntityFrom(DamageSource.func_94539_a(this), (int)((d10 * d10 + d10) / 2.0D * 8.0D * (double)this.explosionSize + 1.0D));
    144                    double d11 = EnchantmentProtection.func_92092_a(entity, d10);
    145                    entity.motionX += d0 * d11;
    146                    entity.motionY += d1 * d11;
    147                    entity.motionZ += d2 * d11;
    148
    149                    if (entity instanceof EntityPlayer)
    150                    {
    151                        this.field_77288_k.put((EntityPlayer)entity, this.worldObj.getWorldVec3Pool().getVecFromPool(d0 * d10, d1 * d10, d2 * d10));
    152                    }
    153                }
    154            }
    155        }
    156
    157        this.explosionSize = f;
    158    }
    159
    160    /**
    161     * Does the second part of the explosion (sound, particles, drop spawn)
    162     */
    163    public void doExplosionB(boolean par1)
    164    {
    165        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);
    166
    167        if (this.explosionSize >= 2.0F && this.isSmoking)
    168        {
    169            this.worldObj.spawnParticle("hugeexplosion", this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D);
    170        }
    171        else
    172        {
    173            this.worldObj.spawnParticle("largeexplode", this.explosionX, this.explosionY, this.explosionZ, 1.0D, 0.0D, 0.0D);
    174        }
    175
    176        Iterator iterator;
    177        ChunkPosition chunkposition;
    178        int i;
    179        int j;
    180        int k;
    181        int l;
    182
    183        if (this.isSmoking)
    184        {
    185            iterator = this.affectedBlockPositions.iterator();
    186
    187            while (iterator.hasNext())
    188            {
    189                chunkposition = (ChunkPosition)iterator.next();
    190                i = chunkposition.x;
    191                j = chunkposition.y;
    192                k = chunkposition.z;
    193                l = this.worldObj.getBlockId(i, j, k);
    194
    195                if (par1)
    196                {
    197                    double d0 = (double)((float)i + this.worldObj.rand.nextFloat());
    198                    double d1 = (double)((float)j + this.worldObj.rand.nextFloat());
    199                    double d2 = (double)((float)k + this.worldObj.rand.nextFloat());
    200                    double d3 = d0 - this.explosionX;
    201                    double d4 = d1 - this.explosionY;
    202                    double d5 = d2 - this.explosionZ;
    203                    double d6 = (double)MathHelper.sqrt_double(d3 * d3 + d4 * d4 + d5 * d5);
    204                    d3 /= d6;
    205                    d4 /= d6;
    206                    d5 /= d6;
    207                    double d7 = 0.5D / (d6 / (double)this.explosionSize + 0.1D);
    208                    d7 *= (double)(this.worldObj.rand.nextFloat() * this.worldObj.rand.nextFloat() + 0.3F);
    209                    d3 *= d7;
    210                    d4 *= d7;
    211                    d5 *= d7;
    212                    this.worldObj.spawnParticle("explode", (d0 + this.explosionX * 1.0D) / 2.0D, (d1 + this.explosionY * 1.0D) / 2.0D, (d2 + this.explosionZ * 1.0D) / 2.0D, d3, d4, d5);
    213                    this.worldObj.spawnParticle("smoke", d0, d1, d2, d3, d4, d5);
    214                }
    215
    216                if (l > 0)
    217                {
    218                    Block block = Block.blocksList[l];
    219
    220                    if (block.canDropFromExplosion(this))
    221                    {
    222                        block.dropBlockAsItemWithChance(this.worldObj, i, j, k, this.worldObj.getBlockMetadata(i, j, k), 1.0F / this.explosionSize, 0);
    223                    }
    224
    225                    this.worldObj.setBlockAndMetadataWithNotify(i, j, k, 0, 0, 3);
    226                    block.onBlockDestroyedByExplosion(this.worldObj, i, j, k, this);
    227                }
    228            }
    229        }
    230
    231        if (this.isFlaming)
    232        {
    233            iterator = this.affectedBlockPositions.iterator();
    234
    235            while (iterator.hasNext())
    236            {
    237                chunkposition = (ChunkPosition)iterator.next();
    238                i = chunkposition.x;
    239                j = chunkposition.y;
    240                k = chunkposition.z;
    241                l = this.worldObj.getBlockId(i, j, k);
    242                int i1 = this.worldObj.getBlockId(i, j - 1, k);
    243
    244                if (l == 0 && Block.opaqueCubeLookup[i1] && this.explosionRNG.nextInt(3) == 0)
    245                {
    246                    this.worldObj.func_94575_c(i, j, k, Block.fire.blockID);
    247                }
    248            }
    249        }
    250    }
    251
    252    public Map func_77277_b()
    253    {
    254        return this.field_77288_k;
    255    }
    256
    257    public EntityLiving func_94613_c()
    258    {
    259        return this.exploder == null ? null : (this.exploder instanceof EntityTNTPrimed ? ((EntityTNTPrimed)this.exploder).func_94083_c() : (this.exploder instanceof EntityLiving ? (EntityLiving)this.exploder : null));
    260    }
    261}