aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/GoodGenerator/Blocks/TEs/NeutronActivator.java
blob: 30d73b9ff05421233112591e9c50bf4c9b5c415b (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package GoodGenerator.Blocks.TEs;

import GoodGenerator.Blocks.TEs.MetaTE.NeutronAccelerator;
import GoodGenerator.Blocks.TEs.MetaTE.NeutronSensor;
import GoodGenerator.Client.GUI.NeutronActivatorGUIClient;
import GoodGenerator.Common.Container.NeutronActivatorGUIContainer;
import GoodGenerator.Loader.Loaders;
import GoodGenerator.util.CharExchanger;
import GoodGenerator.util.DescTextLocalization;
import GoodGenerator.util.ItemRefer;
import GoodGenerator.util.MyRecipeAdder;
import com.github.technus.tectech.thing.metaTileEntity.multi.base.GT_MetaTileEntity_MultiblockBase_EM;
import com.gtnewhorizon.structurelib.alignment.constructable.IConstructable;
import com.gtnewhorizon.structurelib.structure.IStructureDefinition;
import com.gtnewhorizon.structurelib.structure.StructureDefinition;
import gregtech.api.GregTech_API;
import gregtech.api.enums.Materials;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GT_Multiblock_Tooltip_Builder;
import gregtech.api.util.GT_Recipe;
import gregtech.api.util.GT_Utility;
import ic2.core.Ic2Items;
import net.minecraft.block.Block;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraftforge.fluids.FluidStack;
import org.lwjgl.input.Keyboard;

import java.util.ArrayList;
import java.util.Collection;

import static GoodGenerator.util.DescTextLocalization.BLUE_PRINT_INFO;
import static GoodGenerator.util.StructureHelper.addFrame;
import static com.gtnewhorizon.structurelib.structure.StructureUtility.*;
import static gregtech.api.util.GT_StructureUtility.ofHatchAdder;
import static org.apache.commons.lang3.RandomUtils.nextInt;

public class NeutronActivator extends GT_MetaTileEntity_MultiblockBase_EM implements IConstructable {

    protected IStructureDefinition<NeutronActivator> multiDefinition = null;
    protected final ArrayList<NeutronAccelerator> mNeutronAccelerator = new ArrayList<>();
    protected final ArrayList<NeutronSensor> mNeutronSensor = new ArrayList<>();
    protected int casingAmount = 0;
    protected int eV = 0, mCeil = 0, mFloor = 0;

    private static final IIconContainer textureFontOn = new Textures.BlockIcons.CustomIcon("icons/NeutronActivator_On");
    private static final IIconContainer textureFontOn_Glow = new Textures.BlockIcons.CustomIcon("icons/NeutronActivator_On_GLOW");
    private static final IIconContainer textureFontOff = new Textures.BlockIcons.CustomIcon("icons/NeutronActivator_Off");
    private static final IIconContainer textureFontOff_Glow = new Textures.BlockIcons.CustomIcon("icons/NeutronActivator_Off_GLOW");

    public NeutronActivator(String name) {
        super(name);
    }

    public NeutronActivator(int id, String name, String nameRegional) {
        super(id, name, nameRegional);
    }

    @Override
    public boolean checkRecipe_EM(ItemStack aStack) {
        this.mEfficiency = 10000;

        ArrayList<FluidStack> tFluids = getStoredFluids();
        ArrayList<ItemStack> tItems = getStoredInputs();
        Collection<GT_Recipe> tRecipes = MyRecipeAdder.instance.NA.mRecipeList;

        for (int i = 0; i < tFluids.size() - 1; i++) {
            for (int j = i + 1; j < tFluids.size(); j++) {
                if (GT_Utility.areFluidsEqual(tFluids.get(i), tFluids.get(j))) {
                    if ((tFluids.get(i)).amount >= (tFluids.get(j)).amount) {
                        tFluids.remove(j--);
                    } else {
                        tFluids.remove(i--);
                        break;
                    }
                }
            }
        }

        for (int i = 0; i < tItems.size() - 1; i++) {
            for (int j = i + 1; j < tItems.size(); j++) {
                if (GT_Utility.areStacksEqual(tItems.get(i), tItems.get(j))) {
                    if ((tItems.get(i)).stackSize >= (tItems.get(j)).stackSize) {
                        tItems.remove(j--);
                    } else {
                        tItems.remove(i--);
                        break;
                    }
                }
            }
        }

        FluidStack[] inFluids = tFluids.toArray(new FluidStack[0]);
        ItemStack[] inItems = tItems.toArray(new ItemStack[0]);
        int minNKE, maxNKE;

        for (GT_Recipe recipe : tRecipes) {
            minNKE = (recipe.mSpecialValue % 10000) * 1000000;
            maxNKE = (recipe.mSpecialValue / 10000) * 1000000;
            if (recipe.isRecipeInputEqual(true, inFluids, inItems)) {
                mFloor = minNKE;
                mCeil = maxNKE;
                mMaxProgresstime = recipe.mDuration;
                if (eV <= maxNKE && eV >= minNKE) {
                    this.mOutputFluids = recipe.mFluidOutputs;
                    this.mOutputItems = recipe.mOutputs;
                } else {
                    this.mOutputFluids = null;
                    this.mOutputItems = new ItemStack[]{ItemRefer.Radioactive_Waste.get(4)};
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public int getMaxEfficiency(ItemStack aStack) {
        return 10000;
    }

    @Override
    public int getPollutionPerTick(ItemStack aStack) {
        return 0;
    }

    @Override
    public Object getClientGUI(int aID, InventoryPlayer aPlayerInventory, IGregTechTileEntity aBaseMetaTileEntity) {
        return new NeutronActivatorGUIClient(aPlayerInventory, aBaseMetaTileEntity, getLocalName(), "EMDisplay.png");
    }

    @Override
    public Object getServerGUI(int aID, InventoryPlayer aPlayerInventory, IGregTechTileEntity aBaseMetaTileEntity) {
        return new NeutronActivatorGUIContainer(aPlayerInventory, aBaseMetaTileEntity);
    }

    @Override
    public void loadNBTData(NBTTagCompound aNBT) {
        eV = aNBT.getInteger("mKeV");
        mCeil = aNBT.getInteger("mCeil");
        mFloor = aNBT.getInteger("mFloor");
        super.loadNBTData(aNBT);
    }

    @Override
    public void saveNBTData(NBTTagCompound aNBT) {
        aNBT.setInteger("mKeV", eV);
        aNBT.setInteger("mCeil", mCeil);
        aNBT.setInteger("mFloor", mFloor);
        super.saveNBTData(aNBT);
    }

    @Override
    public String[] getDescription() {
        final GT_Multiblock_Tooltip_Builder tt = new GT_Multiblock_Tooltip_Builder();
        tt.addMachineType("Neutron Activator")
                .addInfo("Controller block for the Neutron Activator")
                .addInfo("Superluminal-velocity Motion.")
                .addInfo("You need to input energy to the Neutron Accelerator to get it running.")
                .addInfo("It will output correct products with Specific Neutron Kinetic Energy.")
                .addInfo("Otherwise it will output trash.")
                .addInfo("The Neutron Kinetic Energy will decrease 72KeV/s when no Neutron Accelerator is running.")
                .addInfo("It will explode when the Neutron Kinetic Energy is over" + EnumChatFormatting.RED + " 1200MeV" + EnumChatFormatting.GRAY + ".")
                .addInfo("The structure is too complex!")
                .addInfo(BLUE_PRINT_INFO)
                .addSeparator()
                .beginStructureBlock(5, 6, 5, false)
                .addController("Front bottom")
                .addInputHatch("Hint block with dot 1")
                .addInputBus("Hint block with dot 1")
                .addOutputHatch("Hint block with dot 2")
                .addOutputBus("Hint block with dot 2")
                .addMaintenanceHatch("Hint block with dot 2")
                .addOtherStructurePart("Neutron Accelerator", "Hint block with dot 2")
                .addOtherStructurePart("Neutron Sensor", "Hint block with dot 2")
                .addCasingInfo("Clean Stainless Steel Machine Casing", 7)
                .toolTipFinisher("Good Generator");
        if (!Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
            return tt.getInformation();
        } else {
            return tt.getStructureInformation();
        }
    }

    @Override
    public IStructureDefinition<NeutronActivator> getStructure_EM() {
        if (multiDefinition == null) {
            multiDefinition = StructureDefinition
                    .<NeutronActivator>builder()
                    .addShape(mName,
                            transpose(new String[][]{
                                    {"CCCCC", "CDDDC", "CDDDC", "CDDDC", "CCCCC"},
                                    {"F   F", " GGG ", " GPG ", " GGG ", "F   F"},
                                    {"F   F", " GGG ", " GPG ", " GGG ", "F   F"},
                                    {"F   F", " GGG ", " GPG ", " GGG ", "F   F"},
                                    {"F   F", " GGG ", " GPG ", " GGG ", "F   F"},
                                    {"XX~XX", "XDDDX", "XDDDX", "XDDDX", "XXXXX"},
                            })
                    )
                    .addElement(
                            'C',
                            ofChain(
                                    ofHatchAdder(
                                            NeutronActivator::addClassicInputToMachineList, 49,
                                            1
                                    ),
                                    onElementPass(
                                            x -> x.casingAmount++,
                                            ofBlock(
                                                    GregTech_API.sBlockCasings4, 1
                                            )
                                    )
                            )
                    )
                    .addElement(
                            'D',
                            ofBlock(
                                    GregTech_API.sBlockCasings2, 6
                            )
                    )
                    .addElement(
                            'F',
                            addFrame(
                                    Materials.Steel
                            )
                    )
                    .addElement(
                            'G',
                            ofBlock(
                                    Block.getBlockFromItem(Ic2Items.reinforcedGlass.getItem()), 0
                            )
                    )
                    .addElement(
                            'P',
                            ofBlock(
                                    Loaders.speedingPipe, 0
                            )
                    )
                    .addElement(
                            'X',
                            ofChain(
                                    ofHatchAdder(
                                            NeutronActivator::addClassicOutputToMachineList, 49,
                                            2
                                    ),
                                    ofHatchAdder(
                                            NeutronActivator::addMaintenanceToMachineList, 49,
                                            2
                                    ),
                                    ofHatchAdder(
                                            NeutronActivator::addAcceleratorAndSensor, 49,
                                            2
                                    ),
                                    onElementPass(
                                            x -> x.casingAmount++,
                                            ofBlock(
                                                    GregTech_API.sBlockCasings4, 1
                                            )
                                    )
                            )
                    )
                    .build();
        }
        return multiDefinition;
    }

    @Override
    public boolean checkMachine_EM(IGregTechTileEntity aBaseMetaTileEntity, ItemStack aStack) {
        this.casingAmount = 0;
        this.mNeutronAccelerator.clear();
        this.mNeutronSensor.clear();
        return structureCheck_EM(mName, 2, 5, 0) && casingAmount >= 7;
    }

    public final boolean addAcceleratorAndSensor(IGregTechTileEntity aTileEntity, int aBaseCasingIndex) {
        if (aTileEntity == null) {
            return false;
        } else {
            IMetaTileEntity aMetaTileEntity = aTileEntity.getMetaTileEntity();
            if (aMetaTileEntity instanceof NeutronAccelerator) {
                ((GT_MetaTileEntity_Hatch) aMetaTileEntity).updateTexture(aBaseCasingIndex);
                return this.mNeutronAccelerator.add((NeutronAccelerator) aMetaTileEntity);
            } else if (aMetaTileEntity instanceof NeutronSensor) {
                ((GT_MetaTileEntity_Hatch) aMetaTileEntity).updateTexture(aBaseCasingIndex);
                return this.mNeutronSensor.add((NeutronSensor) aMetaTileEntity);
            }
        }
        return false;
    }

    public int maxNeutronKineticEnergy() {
        return 1200000000;
    }

    public int getCurrentNeutronKineticEnergy() {
        return eV;
    }

    @Override
    public IMetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
        return new NeutronActivator(this.mName);
    }

    @Override
    public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
        super.onPostTick(aBaseMetaTileEntity, aTick);
        boolean anyWorking = false;
        if (this.getBaseMetaTileEntity().isServerSide()) {
            for (NeutronAccelerator tHatch : mNeutronAccelerator) {
                if (tHatch.isRunning && this.getRepairStatus() == this.getIdealStatus()) {
                    anyWorking = true;
                    this.eV += nextInt(tHatch.getMaxEUConsume(), tHatch.getMaxEUConsume() * 2 + 1) * 10;
                }
            }
            if (!anyWorking) {
                if (this.eV >= 72000 && aTick % 20 == 0) {
                    this.eV -= 72000;
                } else if (this.eV > 0 && aTick % 20 == 0) {
                    this.eV = 0;
                }
            }
            if (this.eV < 0) this.eV = 0;
            if (this.eV > maxNeutronKineticEnergy()) doExplosion(4 * 32);

            for (NeutronSensor tHatch : mNeutronSensor) {
                String tText = tHatch.getText();
                if (CharExchanger.isValidCompareExpress(rawProcessExp(tText))) {
                    if (CharExchanger.compareExpression(rawProcessExp(tText), eV)) {
                        tHatch.outputRedstoneSignal();
                    } else tHatch.stopOutputRedstoneSignal();
                }
            }

            if (mProgresstime < mMaxProgresstime && (eV > mCeil || eV < mFloor)) {
                this.mOutputFluids = null;
                this.mOutputItems = new ItemStack[]{ItemRefer.Radioactive_Waste.get(4)};
            }
        }
    }

    protected String rawProcessExp(String exp) {
        StringBuilder ret = new StringBuilder();
        for (char c : exp.toCharArray()) {
            if (exp.length() - ret.length() == 3) {
                if (Character.isDigit(c)) ret.append(c);
                else {
                    if (c == 'K' || c == 'k') {
                        ret.append("000");
                    }
                    if (c == 'M' || c == 'm') {
                        ret.append("000000");
                    }
                }
                break;
            }
            ret.append(c);
        }
        return ret.toString();
    }

    @Override
    public void construct(ItemStack itemStack, boolean b) {
        structureBuild_EM(mName, 2, 5, 0, b, itemStack);
    }

    @Override
    public String[] getStructureDescription(ItemStack itemStack) {
        return DescTextLocalization.addText("NeutronActivator.hint", 7);
    }

    @Override
    public String[] getInfoData() {
        int currentNKEInput = 0;
        boolean anyWorking = false;
        for (NeutronAccelerator tHatch : mNeutronAccelerator) {
            if (tHatch.isRunning) {
                currentNKEInput += nextInt(tHatch.getMaxEUConsume(), tHatch.getMaxEUConsume() * 2 + 1) * 10;
                anyWorking = true;
            }
        }
        if (!anyWorking) currentNKEInput = -72000;
        return new String[] {
                "Progress:",
                EnumChatFormatting.GREEN + Integer.toString(this.mProgresstime / 20) + EnumChatFormatting.RESET + " s / " + EnumChatFormatting.YELLOW + this.mMaxProgresstime / 20 + EnumChatFormatting.RESET + " s",
                "Current Neutron Kinetic Energy Input: " + EnumChatFormatting.GREEN + GT_Utility.formatNumbers(currentNKEInput) + EnumChatFormatting.RESET + "eV",
                StatCollector.translateToLocal("scanner.info.NA") + " " + EnumChatFormatting.LIGHT_PURPLE + GT_Utility.formatNumbers(getCurrentNeutronKineticEnergy()) + EnumChatFormatting.RESET + "eV"
        };
    }

    @Override
    public ITexture[] getTexture(IGregTechTileEntity aBaseMetaTileEntity, byte aSide, byte aFacing, byte aColorIndex, boolean aActive, boolean aRedstone) {
        if(aSide == aFacing) {
            if(aActive) return new ITexture[]{
                    Textures.BlockIcons.getCasingTextureForId(49),
                    TextureFactory.of(textureFontOn),
                    TextureFactory.builder().addIcon(textureFontOn_Glow).glow().build()
            };
            else return new ITexture[]{
                    Textures.BlockIcons.getCasingTextureForId(49),
                    TextureFactory.of(textureFontOff),
                    TextureFactory.builder().addIcon(textureFontOff_Glow).glow().build()
            };
        }
        return new ITexture[]{Textures.BlockIcons.getCasingTextureForId(49)};
    }
}