aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/metatileentity/implementations/MTEHatchMuffler.java
blob: d1c957b97676ed506bc9e6a8b97540fb7acb4d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package gregtech.api.metatileentity.implementations;

import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_MUFFLER;
import static gregtech.api.objects.XSTR.XSTR_INSTANCE;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.GTMod;
import gregtech.api.enums.ParticleFX;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTLanguageManager;
import gregtech.api.util.WorldSpawnedEventBuilder;
import gregtech.common.pollution.Pollution;

@SuppressWarnings("unused") // Unused API is expected within scope
public class MTEHatchMuffler extends MTEHatch {

    private static final String localizedDescFormat = GTLanguageManager.addStringLocalization(
        "gt.blockmachines.hatch.muffler.desc.format",
        "Outputs the Pollution (Might cause ... things)%n" + "DO NOT OBSTRUCT THE OUTPUT!%n"
            + "Reduces Pollution to %d%%%n"
            + "Recovers %d%% of CO2/CO/SO2");
    private final int pollutionReduction = calculatePollutionReduction(100);
    private final int pollutionRecover = 100 - pollutionReduction;
    private final String[] description = String.format(localizedDescFormat, pollutionReduction, pollutionRecover)
        .split("\\R");

    public MTEHatchMuffler(int aID, String aName, String aNameRegional, int aTier) {
        super(aID, aName, aNameRegional, aTier, 0, "");
    }

    public MTEHatchMuffler(int aID, String aName, String aNameRegional, int aTier, int aInvSlotCount,
        String[] aDescription, ITexture... aTextures) {
        super(aID, aName, aNameRegional, aTier, aInvSlotCount, aDescription, aTextures);
    }

    public MTEHatchMuffler(String aName, int aTier, String aDescription, ITexture[][][] aTextures) {
        this(aName, aTier, new String[] { aDescription }, aTextures);
    }

    public MTEHatchMuffler(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) {
        this(aName, aTier, 0, aDescription, aTextures);
    }

    public MTEHatchMuffler(String aName, int aTier, int aInvSlotCount, String[] aDescription,
        ITexture[][][] aTextures) {
        super(aName, aTier, aInvSlotCount, aDescription, aTextures);
    }

    @Override
    public String[] getDescription() {
        return description;
    }

    @Override
    public ITexture[] getTexturesActive(ITexture aBaseTexture) {
        return new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_MUFFLER) };
    }

    @Override
    public ITexture[] getTexturesInactive(ITexture aBaseTexture) {
        return new ITexture[] { aBaseTexture, TextureFactory.of(OVERLAY_MUFFLER) };
    }

    @Override
    public boolean isSimpleMachine() {
        return true;
    }

    @Override
    public boolean isValidSlot(int aIndex) {
        return false;
    }

    @Override
    public boolean allowPullStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
        ItemStack aStack) {
        return false;
    }

    @Override
    public boolean allowPutStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
        ItemStack aStack) {
        return false;
    }

    @Override
    public MetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
        return new MTEHatchMuffler(mName, mTier, mDescriptionArray, mTextures);
    }

    @Override
    public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
        super.onPostTick(aBaseMetaTileEntity, aTick);
        if (aBaseMetaTileEntity.isClientSide() && this.getBaseMetaTileEntity()
            .isActive()) {
            pollutionParticles(
                this.getBaseMetaTileEntity()
                    .getWorld(),
                ParticleFX.LARGE_SMOKE.toString());
        }
    }

    @Override
    public boolean isFacingValid(ForgeDirection facing) {
        return true;
    }

    @Override
    public boolean isAccessAllowed(EntityPlayer aPlayer) {
        return true;
    }

    @SideOnly(Side.CLIENT)
    public void pollutionParticles(World aWorld, String name) {
        boolean chk1, chk2, chk3;
        float ran1 = XSTR_INSTANCE.nextFloat(), ran2, ran3;
        chk1 = ran1 * 100 < calculatePollutionReduction(100);
        if (Pollution.getPollution(getBaseMetaTileEntity()) >= GTMod.gregtechproxy.mPollutionSmogLimit) {
            ran2 = XSTR_INSTANCE.nextFloat();
            ran3 = XSTR_INSTANCE.nextFloat();
            chk2 = ran2 * 100 < calculatePollutionReduction(100);
            chk3 = ran3 * 100 < calculatePollutionReduction(100);
            if (!(chk1 || chk2 || chk3)) return;
        } else {
            if (!chk1) return;
            ran2 = ran3 = 0.0F;
            chk2 = chk3 = false;
        }

        final IGregTechTileEntity aMuffler = this.getBaseMetaTileEntity();
        final ForgeDirection aDir = aMuffler.getFrontFacing();
        final float xPos = aDir.offsetX * 0.76F + aMuffler.getXCoord() + 0.25F;
        final float yPos = aDir.offsetY * 0.76F + aMuffler.getYCoord() + 0.25F;
        final float zPos = aDir.offsetZ * 0.76F + aMuffler.getZCoord() + 0.25F;

        final float ySpd = aDir.offsetY * 0.1F + 0.2F + 0.1F * XSTR_INSTANCE.nextFloat();
        final float xSpd;
        final float zSpd;

        if (aDir.offsetY == -1) {
            final float temp = XSTR_INSTANCE.nextFloat() * 2 * (float) Math.PI;
            xSpd = (float) Math.sin(temp) * 0.1F;
            zSpd = (float) Math.cos(temp) * 0.1F;
        } else {
            xSpd = aDir.offsetX * (0.1F + 0.2F * XSTR_INSTANCE.nextFloat());
            zSpd = aDir.offsetZ * (0.1F + 0.2F * XSTR_INSTANCE.nextFloat());
        }

        final WorldSpawnedEventBuilder.ParticleEventBuilder events = new WorldSpawnedEventBuilder.ParticleEventBuilder()
            .setIdentifier(name)
            .setWorld(aWorld)
            .setMotion(xSpd, ySpd, zSpd);

        if (chk1) {
            events
                .setPosition(
                    xPos + ran1 * 0.5F,
                    yPos + XSTR_INSTANCE.nextFloat() * 0.5F,
                    zPos + XSTR_INSTANCE.nextFloat() * 0.5F)
                .run();
        }
        if (chk2) {
            events
                .setPosition(
                    xPos + ran2 * 0.5F,
                    yPos + XSTR_INSTANCE.nextFloat() * 0.5F,
                    zPos + XSTR_INSTANCE.nextFloat() * 0.5F)
                .run();
        }
        if (chk3) {
            events
                .setPosition(
                    xPos + ran3 * 0.5F,
                    yPos + XSTR_INSTANCE.nextFloat() * 0.5F,
                    zPos + XSTR_INSTANCE.nextFloat() * 0.5F)
                .run();
        }
    }

    public int calculatePollutionReduction(int aPollution) {
        if (mTier < 2) {
            return aPollution;
        }
        return (int) ((float) aPollution * ((100F - 12.5F * ((float) mTier - 1F)) / 100F));
    }

    /**
     * @param mte             The multi-block controller's {@link MetaTileEntity} MetaTileEntity is passed so newer
     *                        muffler hatches can do wacky things with the multis
     * @param pollutionAmount How much pollution to output. Reduced by muffler efficiency.
     * @return pollution success
     */
    public boolean polluteEnvironment(MetaTileEntity mte, int pollutionAmount) {
        if (getBaseMetaTileEntity().getAirAtSide(getBaseMetaTileEntity().getFrontFacing())) {
            Pollution.addPollution(getBaseMetaTileEntity(), calculatePollutionReduction(pollutionAmount));
            return true;
        }
        return false;
    }

    /**
     * @deprecated Use {@link #polluteEnvironment(MetaTileEntity, int)}.
     */
    @Deprecated
    public boolean polluteEnvironment(MetaTileEntity mte) {
        return polluteEnvironment(mte, 10000);
    }
}