    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.ArrayList;
    006    import java.util.Hashtable;
    007    import java.util.Iterator;
    008    import java.util.List;
    009    import java.util.Map.Entry;
    010    import java.util.Random;
    011
    012    import net.minecraftforge.client.ForgeHooksClient;
    013    import net.minecraftforge.common.ForgeHooks;
    014    import com.google.common.collect.ArrayListMultimap;
    015    import com.google.common.collect.Multimap;
    016
    017    import org.lwjgl.opengl.GL11;
    018
    019
    020    @SideOnly(Side.CLIENT)
    021    public class EffectRenderer
    022    {
    023        /** Reference to the World object. */
    024        protected World worldObj;
    025        private List[] fxLayers = new List[4];
    026        private RenderEngine renderer;
    027
    028        /** RNG. */
    029        private Random rand = new Random();
    030
    031        private Multimap<String, EntityFX> effectList = ArrayListMultimap.create();
    032
    033        public EffectRenderer(World par1World, RenderEngine par2RenderEngine)
    034        {
    035            if (par1World != null)
    036            {
    037                this.worldObj = par1World;
    038            }
    039
    040            this.renderer = par2RenderEngine;
    041
    042            for (int var3 = 0; var3 < 4; ++var3)
    043            {
    044                this.fxLayers[var3] = new ArrayList();
    045            }
    046        }
    047
    048        public void addEffect(EntityFX par1EntityFX)
    049        {
    050            int var2 = par1EntityFX.getFXLayer();
    051
    052            if (this.fxLayers[var2].size() >= 4000)
    053            {
    054                this.fxLayers[var2].remove(0);
    055            }
    056
    057            this.fxLayers[var2].add(par1EntityFX);
    058        }
    059
    060        public void updateEffects()
    061        {
    062            for (int var1 = 0; var1 < 4; ++var1)
    063            {
    064                for (int var2 = 0; var2 < this.fxLayers[var1].size(); ++var2)
    065                {
    066                    EntityFX var3 = (EntityFX)this.fxLayers[var1].get(var2);
    067
    068                    if (var3 != null)
    069                    {
    070                        var3.onUpdate();
    071                    }
    072
    073                    if (var3 == null || var3.isDead)
    074                    {
    075                        this.fxLayers[var1].remove(var2--);
    076                    }
    077                }
    078            }
    079
    080            Iterator<Entry<String, EntityFX>> itr = effectList.entries().iterator();
    081            while (itr.hasNext())
    082            {
    083                EntityFX fx = itr.next().getValue();
    084                fx.onUpdate();
    085                if (fx.isDead)
    086                {
    087                    itr.remove();
    088                }
    089            }
    090        }
    091
    092        /**
    093         * Renders all current particles. Args player, partialTickTime
    094         */
    095        public void renderParticles(Entity par1Entity, float par2)
    096        {
    097            float var3 = ActiveRenderInfo.rotationX;
    098            float var4 = ActiveRenderInfo.rotationZ;
    099            float var5 = ActiveRenderInfo.rotationYZ;
    100            float var6 = ActiveRenderInfo.rotationXY;
    101            float var7 = ActiveRenderInfo.rotationXZ;
    102            EntityFX.interpPosX = par1Entity.lastTickPosX + (par1Entity.posX - par1Entity.lastTickPosX) * (double)par2;
    103            EntityFX.interpPosY = par1Entity.lastTickPosY + (par1Entity.posY - par1Entity.lastTickPosY) * (double)par2;
    104            EntityFX.interpPosZ = par1Entity.lastTickPosZ + (par1Entity.posZ - par1Entity.lastTickPosZ) * (double)par2;
    105
    106            for (int var8 = 0; var8 < 3; ++var8)
    107            {
    108                if (!this.fxLayers[var8].isEmpty())
    109                {
    110                    int var9 = 0;
    111
    112                    if (var8 == 0)
    113                    {
    114                        var9 = this.renderer.getTexture("/particles.png");
    115                    }
    116
    117                    if (var8 == 1)
    118                    {
    119                        var9 = this.renderer.getTexture("/terrain.png");
    120                    }
    121
    122                    if (var8 == 2)
    123                    {
    124                        var9 = this.renderer.getTexture("/gui/items.png");
    125                    }
    126
    127                    GL11.glBindTexture(GL11.GL_TEXTURE_2D, var9);
    128                    Tessellator var10 = Tessellator.instance;
    129                    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    130                    var10.startDrawingQuads();
    131
    132                    for (int var11 = 0; var11 < this.fxLayers[var8].size(); ++var11)
    133                    {
    134                        EntityFX var12 = (EntityFX)this.fxLayers[var8].get(var11);
    135                        if (var12 == null) continue;
    136                        var10.setBrightness(var12.getBrightnessForRender(par2));
    137                        var12.renderParticle(var10, par2, var3, var7, var4, var5, var6);
    138                    }
    139
    140                    var10.draw();
    141                }
    142            }
    143
    144            for (String key : effectList.keySet())
    145            {
    146                ForgeHooksClient.bindTexture(key, 0);
    147                for (EntityFX entry : effectList.get(key))
    148                {
    149                    if (entry == null) continue;
    150                    Tessellator tessallator = Tessellator.instance;
    151                    //GL11.glBindTexture(GL11.GL_TEXTURE_2D, renderer.getTexture(key));
    152                    tessallator.startDrawingQuads();
    153
    154                    if (entry.getFXLayer() != 3)
    155                    {
    156                        tessallator.setBrightness(entry.getBrightnessForRender(par2));
    157                        entry.renderParticle(tessallator, par2, var3, var7, var4, var5, var6);
    158                    }
    159
    160                    tessallator.draw();
    161                }
    162                ForgeHooksClient.unbindTexture();
    163            }
    164        }
    165
    166        public void renderLitParticles(Entity par1Entity, float par2)
    167        {
    168            float var4 = MathHelper.cos(par1Entity.rotationYaw * 0.017453292F);
    169            float var5 = MathHelper.sin(par1Entity.rotationYaw * 0.017453292F);
    170            float var6 = -var5 * MathHelper.sin(par1Entity.rotationPitch * 0.017453292F);
    171            float var7 = var4 * MathHelper.sin(par1Entity.rotationPitch * 0.017453292F);
    172            float var8 = MathHelper.cos(par1Entity.rotationPitch * 0.017453292F);
    173            byte var9 = 3;
    174
    175            if (!this.fxLayers[var9].isEmpty())
    176            {
    177                Tessellator var10 = Tessellator.instance;
    178
    179                for (int var11 = 0; var11 < this.fxLayers[var9].size(); ++var11)
    180                {
    181                    EntityFX var12 = (EntityFX)this.fxLayers[var9].get(var11);
    182                    if (var12 == null) continue;
    183                    var10.setBrightness(var12.getBrightnessForRender(par2));
    184                    var12.renderParticle(var10, par2, var4, var8, var5, var6, var7);
    185                }
    186            }
    187        }
    188
    189        public void clearEffects(World par1World)
    190        {
    191            this.worldObj = par1World;
    192
    193            for (int var2 = 0; var2 < 4; ++var2)
    194            {
    195                this.fxLayers[var2].clear();
    196            }
    197
    198            effectList.clear();
    199        }
    200
    201        public void addBlockDestroyEffects(int par1, int par2, int par3, int par4, int par5)
    202        {
    203            Block var6 = Block.blocksList[par4];
    204            if (var6 != null && !var6.addBlockDestroyEffects(worldObj, par1, par2, par3, par5, this))
    205            {
    206                byte var7 = 4;
    207
    208                for (int var8 = 0; var8 < var7; ++var8)
    209                {
    210                    for (int var9 = 0; var9 < var7; ++var9)
    211                    {
    212                        for (int var10 = 0; var10 < var7; ++var10)
    213                        {
    214                            double var11 = (double)par1 + ((double)var8 + 0.5D) / (double)var7;
    215                            double var13 = (double)par2 + ((double)var9 + 0.5D) / (double)var7;
    216                            double var15 = (double)par3 + ((double)var10 + 0.5D) / (double)var7;
    217                            int var17 = this.rand.nextInt(6);
    218                            this.addEffect((new EntityDiggingFX(this.worldObj, var11, var13, var15, var11 - (double)par1 - 0.5D, var13 - (double)par2 - 0.5D, var15 - (double)par3 - 0.5D, var6, var17, par5)).func_70596_a(par1, par2, par3), var6);
    219                        }
    220                    }
    221                }
    222            }
    223        }
    224
    225        /**
    226         * Adds block hit particles for the specified block. Args: x, y, z, sideHit
    227         */
    228        public void addBlockHitEffects(int par1, int par2, int par3, int par4)
    229        {
    230            int var5 = this.worldObj.getBlockId(par1, par2, par3);
    231
    232            if (var5 != 0)
    233            {
    234                Block var6 = Block.blocksList[var5];
    235                float var7 = 0.1F;
    236                double var8 = (double)par1 + this.rand.nextDouble() * (var6.func_83007_w() - var6.func_83009_v() - (double)(var7 * 2.0F)) + (double)var7 + var6.func_83009_v();
    237                double var10 = (double)par2 + this.rand.nextDouble() * (var6.func_83010_y() - var6.func_83008_x() - (double)(var7 * 2.0F)) + (double)var7 + var6.func_83008_x();
    238                double var12 = (double)par3 + this.rand.nextDouble() * (var6.func_83006_A() - var6.func_83005_z() - (double)(var7 * 2.0F)) + (double)var7 + var6.func_83005_z();
    239
    240                if (par4 == 0)
    241                {
    242                    var10 = (double)par2 + var6.func_83008_x() - (double)var7;
    243                }
    244
    245                if (par4 == 1)
    246                {
    247                    var10 = (double)par2 + var6.func_83010_y() + (double)var7;
    248                }
    249
    250                if (par4 == 2)
    251                {
    252                    var12 = (double)par3 + var6.func_83005_z() - (double)var7;
    253                }
    254
    255                if (par4 == 3)
    256                {
    257                    var12 = (double)par3 + var6.func_83006_A() + (double)var7;
    258                }
    259
    260                if (par4 == 4)
    261                {
    262                    var8 = (double)par1 + var6.func_83009_v() - (double)var7;
    263                }
    264
    265                if (par4 == 5)
    266                {
    267                    var8 = (double)par1 + var6.func_83007_w() + (double)var7;
    268                }
    269
    270                this.addEffect((new EntityDiggingFX(this.worldObj, var8, var10, var12, 0.0D, 0.0D, 0.0D, var6, par4, this.worldObj.getBlockMetadata(par1, par2, par3))).func_70596_a(par1, par2, par3).multiplyVelocity(0.2F).multipleParticleScaleBy(0.6F), var6);
    271            }
    272        }
    273
    274        public String getStatistics()
    275        {
    276            int size = 0;
    277            for (List x : fxLayers)
    278            {
    279                size += x.size();
    280            }
    281            size += effectList.size();
    282            return Integer.toString(size);
    283        }
    284
    285        public void addEffect(EntityFX effect, Object obj)
    286        {
    287            if (obj == null || !(obj instanceof Block || obj instanceof Item))
    288            {
    289                addEffect(effect);
    290                return;
    291            }
    292
    293            if (obj instanceof Item && ((Item)obj).isDefaultTexture)
    294            {
    295                addEffect(effect);
    296                return;
    297            }
    298
    299            if (obj instanceof Block && ((Block)obj).isDefaultTexture)
    300            {
    301                addEffect(effect);
    302                return;
    303            }
    304
    305            String texture = "/terrain.png";
    306            if (effect.getFXLayer() == 0)
    307            {
    308                texture = "/particles.png";
    309            }
    310            else if (effect.getFXLayer() == 2)
    311            {
    312                texture = "/gui/items.png";
    313            }
    314            texture = ForgeHooks.getTexture(texture, obj);
    315            effectList.put(texture, effect);
    316        }
    317
    318        public void addBlockHitEffects(int x, int y, int z, MovingObjectPosition target)
    319        {
    320            Block block = Block.blocksList[worldObj.getBlockId(x, y, z)];
    321            if (block != null && !block.addBlockHitEffects(worldObj, target, this))
    322            {
    323                addBlockHitEffects(x, y, z, target.sideHit);
    324            }
    325        }
    326    }