aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/tectech/thing/metaTileEntity/hatch/MTEHatchUncertainty.java
blob: 38b90e47709c57df956606636967c60139df4075 (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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package tectech.thing.metaTileEntity.hatch;

import static net.minecraft.util.StatCollector.translateToLocal;
import static net.minecraft.util.StatCollector.translateToLocalFormatted;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.glBlendFunc;
import static org.lwjgl.opengl.GL11.glColor4f;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnable;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;

import com.gtnewhorizons.modularui.api.GlStateManager;
import com.gtnewhorizons.modularui.api.drawable.UITexture;
import com.gtnewhorizons.modularui.api.math.Pos2d;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.widget.ButtonWidget;
import com.gtnewhorizons.modularui.common.widget.DrawableWidget;
import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget;
import com.gtnewhorizons.modularui.common.widget.TextWidget;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.enums.Textures;
import gregtech.api.gui.modularui.GTUIInfos;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.modularui.IAddGregtechLogo;
import gregtech.api.interfaces.modularui.IAddUIWidgets;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.MTEHatch;
import gregtech.api.objects.GTRenderedTexture;
import gregtech.mixin.interfaces.accessors.EntityPlayerMPAccessor;
import tectech.TecTech;
import tectech.thing.gui.TecTechUITextures;
import tectech.util.CommonValues;
import tectech.util.TTUtility;

/**
 * Created by danie_000 on 15.12.2016.
 */
public class MTEHatchUncertainty extends MTEHatch implements IAddGregtechLogo, IAddUIWidgets {

    private static Textures.BlockIcons.CustomIcon ScreenON;
    private static Textures.BlockIcons.CustomIcon ScreenOFF;
    public short[] matrix = new short[] { 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500,
        500 };
    public byte selection = -1, mode = 0, status = -128; // all 8 bits set

    private String clientLocale = "en_US";

    public MTEHatchUncertainty(int aID, String aName, String aNameRegional, int aTier) {
        super(aID, aName, aNameRegional, aTier, 0, "");
        TTUtility.setTier(aTier, this);
        regenerate();
    }

    public MTEHatchUncertainty(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) {
        super(aName, aTier, 0, aDescription, aTextures);
        regenerate();
    }

    @Override
    @SideOnly(Side.CLIENT)
    public void registerIcons(IIconRegister aBlockIconRegister) {
        super.registerIcons(aBlockIconRegister);
        ScreenOFF = new Textures.BlockIcons.CustomIcon("iconsets/UC");
        ScreenON = new Textures.BlockIcons.CustomIcon("iconsets/UC_ACTIVE");
    }

    @Override
    public ITexture[] getTexturesActive(ITexture aBaseTexture) {
        return new ITexture[] { aBaseTexture, new GTRenderedTexture(ScreenON) };
    }

    @Override
    public ITexture[] getTexturesInactive(ITexture aBaseTexture) {
        return new ITexture[] { aBaseTexture, new GTRenderedTexture(ScreenOFF) };
    }

    @Override
    public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
        if (aBaseMetaTileEntity.isServerSide() && (aTick & 15) == 0) {
            if (mode == 0) {
                aBaseMetaTileEntity.setActive(false);
                status = -128;
            } else {
                aBaseMetaTileEntity.setActive(true);
                shift();
                compute();
            }
        }
    }

    @Override
    public IMetaTileEntity newMetaEntity(IGregTechTileEntity iGregTechTileEntity) {
        return new MTEHatchUncertainty(mName, mTier, mDescriptionArray, mTextures);
    }

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

    @Override
    public String[] getInfoData() {
        return new String[] {
            translateToLocalFormatted("tt.keyword.Status", clientLocale) + ": " + EnumChatFormatting.GOLD + status };
    }

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

    @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 void saveNBTData(NBTTagCompound aNBT) {
        super.saveNBTData(aNBT);
        aNBT.setByte("mSel", selection);
        aNBT.setByte("mMode", mode);
        aNBT.setByte("mStatus", status);
        NBTTagCompound mat = new NBTTagCompound();
        for (int i = 0; i < 16; i++) {
            mat.setShort(Integer.toString(i), matrix[i]);
        }
        aNBT.setTag("mMat", mat);
    }

    @Override
    public void loadNBTData(NBTTagCompound aNBT) {
        super.loadNBTData(aNBT);
        selection = aNBT.getByte("mSel");
        mode = aNBT.getByte("mMode");
        status = aNBT.getByte("mStatus");
        NBTTagCompound mat = aNBT.getCompoundTag("mMat");
        for (int i = 0; i < 16; i++) {
            matrix[i] = mat.getShort(Integer.toString(i));
        }
    }

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

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

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

    @Override
    public boolean isLiquidInput(ForgeDirection side) {
        return false;
    }

    @Override
    public boolean isFluidInputAllowed(FluidStack aFluid) {
        return false;
    }

    @Override
    public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {
        if (aBaseMetaTileEntity.isClientSide()) {
            return true;
        }
        if (aPlayer instanceof EntityPlayerMPAccessor) {
            clientLocale = ((EntityPlayerMPAccessor) aPlayer).gt5u$getTranslator();
        }
        GTUIInfos.openGTTileEntityUI(aBaseMetaTileEntity, aPlayer);
        return true;
    }

    @Override
    public String[] getDescription() {
        return new String[] { CommonValues.TEC_MARK_EM, translateToLocal("gt.blockmachines.hatch.certain.desc.0"), // Feeling
                                                                                                                   // certain,
                                                                                                                   // or
                                                                                                                   // not?
            EnumChatFormatting.AQUA.toString() + EnumChatFormatting.BOLD
                + translateToLocal("gt.blockmachines.hatch.certain.desc.1") // Schrödinger equation in a box
        };
    }

    private boolean balanceCheck(int sideLenY, short... masses) {
        float inequality = 0;
        for (int i = 0; i < masses.length >> 1; i++) {
            inequality += Math.abs(masses[i] - masses[masses.length - i - 1]);
        }
        return inequality < masses.length << 7;
    }

    public void regenerate() {
        for (int i = 0; i < matrix.length; i++) {
            matrix[i] = (short) TecTech.RANDOM.nextInt(1000);
        }
    }

    public byte compute() {
        int result = 0;
        switch (mode) {
            case 1: // ooo oxo ooo
                result = balanceCheck(4, matrix) ? 0 : 1;
                break;
            case 2: // ooo xox ooo
                result += balanceCheck(
                    4,
                    matrix[0],
                    matrix[4],
                    matrix[1],
                    matrix[5],
                    matrix[2],
                    matrix[6],
                    matrix[3],
                    matrix[7]) ? 0 : 1;
                result += balanceCheck(
                    4,
                    matrix[8],
                    matrix[12],
                    matrix[9],
                    matrix[13],
                    matrix[10],
                    matrix[14],
                    matrix[11],
                    matrix[15]) ? 0 : 2;
                break;
            case 3: // oxo xox oxo
                result += balanceCheck(
                    2,
                    matrix[0],
                    matrix[4],
                    matrix[8],
                    matrix[12],
                    matrix[1],
                    matrix[5],
                    matrix[9],
                    matrix[13]) ? 0 : 1;
                result += balanceCheck(
                    4,
                    matrix[0],
                    matrix[4],
                    matrix[1],
                    matrix[5],
                    matrix[2],
                    matrix[6],
                    matrix[3],
                    matrix[7]) ? 0 : 2;
                result += balanceCheck(
                    4,
                    matrix[8],
                    matrix[12],
                    matrix[9],
                    matrix[13],
                    matrix[10],
                    matrix[14],
                    matrix[11],
                    matrix[15]) ? 0 : 4;
                result += balanceCheck(
                    2,
                    matrix[2],
                    matrix[6],
                    matrix[10],
                    matrix[14],
                    matrix[3],
                    matrix[7],
                    matrix[11],
                    matrix[15]) ? 0 : 8;
                break;
            case 4: // xox ooo xox
                result += balanceCheck(2, matrix[0], matrix[4], matrix[1], matrix[5]) ? 0 : 1;
                result += balanceCheck(2, matrix[8], matrix[12], matrix[9], matrix[13]) ? 0 : 2;
                result += balanceCheck(2, matrix[2], matrix[6], matrix[3], matrix[7]) ? 0 : 4;
                result += balanceCheck(2, matrix[10], matrix[14], matrix[11], matrix[15]) ? 0 : 8;
                break;
            case 5: // xox oxo xox
                result += balanceCheck(2, matrix[0], matrix[4], matrix[1], matrix[5]) ? 0 : 1;
                result += balanceCheck(2, matrix[8], matrix[12], matrix[9], matrix[13]) ? 0 : 2;
                result += balanceCheck(4, matrix) ? 0 : 4;
                result += balanceCheck(2, matrix[2], matrix[6], matrix[3], matrix[7]) ? 0 : 8;
                result += balanceCheck(2, matrix[10], matrix[14], matrix[11], matrix[15]) ? 0 : 16;
                break;
        }
        return status = (byte) result;
    }

    private void shift() {
        int i = TecTech.RANDOM.nextInt(16), j = TecTech.RANDOM.nextInt(128);
        matrix[i] += ((matrix[i] & 1) == 0 ? 2 : -2) * j >> 5;
        matrix[i] += j == 0 ? 1 : 0;
        if (matrix[i] < 0) {
            matrix[i] = 0;
        } else if (matrix[i] > 1000) {
            matrix[i] = 999;
        }
    }

    public byte update(int newMode) {
        if (newMode == mode) {
            return status;
        }
        if (newMode < 0 || newMode > 5) {
            newMode = 0;
        }
        mode = (byte) newMode;
        regenerate();
        compute();
        return status;
    }

    @Override
    public void addGregTechLogo(ModularWindow.Builder builder) {
        builder.widget(
            new DrawableWidget().setDrawable(TecTechUITextures.PICTURE_TECTECH_LOGO_DARK)
                .setSize(18, 18)
                .setPos(112, 55));
    }

    @Override
    public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
        final boolean isAdvanced = mTier > 7;

        builder.widget(
            new DrawableWidget().setDrawable(TecTechUITextures.BACKGROUND_SCREEN_BLUE)
                .setPos(43, 4)
                .setSize(90, 72))
            .widget(
                new DrawableWidget().setDrawable(TecTechUITextures.PICTURE_UNCERTAINTY_MONITOR)
                    .setPos(46, 27)
                    .setSize(46, 46));

        int[] xPositions = new int[] { 7, 25, 133, 151 };
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                final int index = i * 4 + j;
                builder.widget(new ButtonWidget().setOnClick((clickData, widget) -> {
                    TecTech.proxy.playSound(getBaseMetaTileEntity(), "fx_click");
                    if (selection == -1) {
                        selection = (byte) index;
                    } else {
                        short temp = matrix[selection];
                        matrix[selection] = matrix[index];
                        matrix[index] = temp;
                        selection = -1;
                    }
                    compute();
                })
                    .setPlayClickSound(false)
                    .setBackground(GTUITextures.BUTTON_STANDARD, TecTechUITextures.OVERLAY_BUTTON_UNCERTAINTY[index])
                    .setPos(xPositions[i], 4 + j * 18)
                    .setSize(18, 18))
                    .widget(new FakeSyncWidget.ShortSyncer(() -> matrix[index], val -> matrix[index] = val));
            }
        }
        builder.widget(new FakeSyncWidget.ByteSyncer(() -> selection, val -> selection = val))
            .widget(new FakeSyncWidget.ByteSyncer(() -> mode, val -> mode = val))
            .widget(new FakeSyncWidget.ByteSyncer(() -> status, val -> status = val));

        builder.widget(
            new TextWidget().setStringSupplier(() -> "Status: " + (status == 0 ? "OK" : "NG"))
                .setDefaultColor(COLOR_TEXT_WHITE.get())
                .setPos(46, 7));

        for (int i = 0; i < 9; i++) {
            final int index = i;
            builder.widget(new DrawableWidget().setDrawable(() -> {
                UITexture valid = TecTechUITextures.PICTURE_UNCERTAINTY_VALID[index];
                UITexture invalid = TecTechUITextures.PICTURE_UNCERTAINTY_INVALID[index];
                switch (mode) {
                    case 1: // ooo oxo ooo
                        if (index == 4) return status == 0 ? valid : invalid;
                        break;
                    case 2: // ooo xox ooo
                        if (index == 3) return (status & 1) == 0 ? valid : invalid;
                        if (index == 5) return (status & 2) == 0 ? valid : invalid;
                        break;
                    case 3: // oxo xox oxo
                        if (index == 1) return (status & 1) == 0 ? valid : invalid;
                        if (index == 3) return (status & 2) == 0 ? valid : invalid;
                        if (index == 5) return (status & 4) == 0 ? valid : invalid;
                        if (index == 7) return (status & 8) == 0 ? valid : invalid;
                        break;
                    case 4: // xox ooo xox
                        if (index == 0) return (status & 1) == 0 ? valid : invalid;
                        if (index == 2) return (status & 2) == 0 ? valid : invalid;
                        if (index == 6) return (status & 4) == 0 ? valid : invalid;
                        if (index == 8) return (status & 8) == 0 ? valid : invalid;
                        break;
                    case 5: // xox oxo xox
                        if (index == 0) return (status & 1) == 0 ? valid : invalid;
                        if (index == 2) return (status & 2) == 0 ? valid : invalid;
                        if (index == 4) return (status & 4) == 0 ? valid : invalid;
                        if (index == 6) return (status & 8) == 0 ? valid : invalid;
                        if (index == 8) return (status & 16) == 0 ? valid : invalid;
                        break;
                }
                return null;
            })
                .setPos(55 + (index % 3) * 12, 36 + (index / 3) * 12)
                .setSize(4, 4));
        }

        for (int i = 0; i < 16; i++) {
            final int index = i;
            builder.widget(new DrawableWidget() {

                @Override
                public void draw(float partialTicks) {
                    if (isAdvanced) {
                        glEnable(GL_BLEND);
                        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                        glColor4f(1f, 1f, 1f, (float) matrix[index] / 1000f);

                        // super.draw but without disabling blend
                        GlStateManager.pushMatrix();
                        getDrawable().draw(Pos2d.ZERO, getSize(), partialTicks);
                        GlStateManager.popMatrix();

                        glDisable(GL_BLEND);
                        glColor4f(1f, 1f, 1f, 1f);
                    } else {
                        if (TecTech.RANDOM.nextInt(1000) < matrix[index]) {
                            super.draw(partialTicks);
                        }
                    }
                }
            }.setDrawable(TecTechUITextures.PICTURE_UNCERTAINTY_INDICATOR)
                .setPos(47 + (i / 4) * 12, 28 + (i % 4) * 12)
                .setSize(8, 8))
                .widget(
                    new DrawableWidget()
                        .setDrawable(() -> selection == index ? TecTechUITextures.PICTURE_UNCERTAINTY_SELECTED : null)
                        .setPos(46 + (i / 4) * 12, 27 + (i % 4) * 12)
                        .setSize(10, 10));
        }
    }
}