aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/GT_CircuitryBehavior.java
blob: 4adf7e0f26125b49ce8ec1f98df1202bea3e7933 (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
package gregtech.api.util;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTech_API;
import gregtech.api.interfaces.IRedstoneCircuitBlock;

/**
 * Redstone Circuit Control Code
 * <p/>
 * This should make everything possible what a Redstone Computer or BuildCraft Gate could do.
 * It is intended to use this similar to BC-Gates (for acquiring Data) and RP Logic Gates.
 * You could write an extremely specified and complex Logic Gate, which works only for you Setup, like
 * with ComputerCraft, but you would have to write an extra Mod to add that, as it doesn't work Ingame.
 * <p/>
 * One can make use of the fact, that ItemStacks can be stored as Integer, so that you can scan
 * Inventories for specific Items using that. Luckily the Buttons in the GUI enable Copy/Paste of
 * ItemID+MetaData to Integer, including the WildCard Damage Value when you use rightclick to place it.
 * You just need to use @GT_Utility.stackToInt(ItemStack aStack) to get it.
 * <p/>
 * All Functions run usually in a seperate try/catch Block, so that failed Logic won't crash the TileEntity.
 */
public abstract class GT_CircuitryBehavior {
    /**
     * @param aIndex 0 - 1023 are my own Indices, so use other Numbers!
     */
    public GT_CircuitryBehavior(int aIndex) {
        GregTech_API.sCircuitryBehaviors.put(aIndex, this);
    }

    /**
     * returns if there is Redstone applied to any of the valid Inputs (OR)
     */
    public static final boolean getAnyRedstone(IRedstoneCircuitBlock aRedstoneCircuitBlock) {
        for (byte i = 0; i < 6; i++) {
            if (i != aRedstoneCircuitBlock.getOutputFacing() && aRedstoneCircuitBlock.getCover(i).letsRedstoneGoIn(i, aRedstoneCircuitBlock.getCoverID(i), aRedstoneCircuitBlock.getCoverVariable(i), aRedstoneCircuitBlock.getOwnTileEntity())) {
                if (aRedstoneCircuitBlock.getInputRedstone(i) > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * returns if there is Redstone applied to all the valid Inputs (AND)
     */
    public static final boolean getAllRedstone(IRedstoneCircuitBlock aRedstoneCircuitBlock) {
        for (byte i = 0; i < 6; i++) {
            if (i != aRedstoneCircuitBlock.getOutputFacing() && aRedstoneCircuitBlock.getCover(i).letsRedstoneGoIn(i, aRedstoneCircuitBlock.getCoverID(i), aRedstoneCircuitBlock.getCoverVariable(i), aRedstoneCircuitBlock.getOwnTileEntity())) {
                if (aRedstoneCircuitBlock.getInputRedstone(i) == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * returns if there is Redstone applied to exactly one of the valid Inputs (XOR)
     */
    public static final boolean getOneRedstone(IRedstoneCircuitBlock aRedstoneCircuitBlock) {
        int tRedstoneAmount = 0;
        for (byte i = 0; i < 6; i++) {
            if (i != aRedstoneCircuitBlock.getOutputFacing() && aRedstoneCircuitBlock.getCover(i).letsRedstoneGoIn(i, aRedstoneCircuitBlock.getCoverID(i), aRedstoneCircuitBlock.getCoverVariable(i), aRedstoneCircuitBlock.getOwnTileEntity())) {
                if (aRedstoneCircuitBlock.getInputRedstone(i) > 0) {
                    tRedstoneAmount++;
                }
            }
        }
        return tRedstoneAmount == 1;
    }

    /**
     * returns the strongest incoming RS-Power
     */
    public static final byte getStrongestRedstone(IRedstoneCircuitBlock aRedstoneCircuitBlock) {
        byte tRedstoneAmount = 0;
        for (byte i = 0; i < 6; i++) {
            if (i != aRedstoneCircuitBlock.getOutputFacing() && aRedstoneCircuitBlock.getCover(i).letsRedstoneGoIn(i, aRedstoneCircuitBlock.getCoverID(i), aRedstoneCircuitBlock.getCoverVariable(i), aRedstoneCircuitBlock.getOwnTileEntity())) {
                tRedstoneAmount = (byte) Math.max(tRedstoneAmount, aRedstoneCircuitBlock.getInputRedstone(i));
            }
        }
        return tRedstoneAmount;
    }

    /*****************
     * GUI Functions *
     *****************/

    /**
     * returns the weakest incoming non-zero RS-Power
     */
    public static final byte getWeakestNonZeroRedstone(IRedstoneCircuitBlock aRedstoneCircuitBlock) {
        if (!getAnyRedstone(aRedstoneCircuitBlock)) return 0;
        byte tRedstoneAmount = 15;
        for (byte i = 0; i < 6; i++) {
            if (i != aRedstoneCircuitBlock.getOutputFacing() && aRedstoneCircuitBlock.getCover(i).letsRedstoneGoIn(i, aRedstoneCircuitBlock.getCoverID(i), aRedstoneCircuitBlock.getCoverVariable(i), aRedstoneCircuitBlock.getOwnTileEntity())) {
                if (aRedstoneCircuitBlock.getInputRedstone(i) > 0)
                    tRedstoneAmount = (byte) Math.min(tRedstoneAmount, aRedstoneCircuitBlock.getInputRedstone(i));
            }
        }
        return tRedstoneAmount;
    }

    /**
     * returns the weakest incoming RS-Power
     */
    public static final byte getWeakestRedstone(IRedstoneCircuitBlock aRedstoneCircuitBlock) {
        if (!getAnyRedstone(aRedstoneCircuitBlock)) return 0;
        byte tRedstoneAmount = 15;
        for (byte i = 0; i < 6; i++) {
            if (i != aRedstoneCircuitBlock.getOutputFacing() && aRedstoneCircuitBlock.getCover(i).letsRedstoneGoIn(i, aRedstoneCircuitBlock.getCoverID(i), aRedstoneCircuitBlock.getCoverVariable(i), aRedstoneCircuitBlock.getOwnTileEntity())) {
                tRedstoneAmount = (byte) Math.min(tRedstoneAmount, aRedstoneCircuitBlock.getInputRedstone(i));
            }
        }
        return tRedstoneAmount;
    }

    /**
     * Initializes the Parameters of this Circuit, all Parameters have been set to 0 right before calling this
     *
     * @param aCircuitData,          The Data Storage you can use (8 Slots)
     * @param aRedstoneCircuitBlock, The Circuit Block MetaTileEntity itself
     */
    public abstract void initParameters(int[] aCircuitData, IRedstoneCircuitBlock aRedstoneCircuitBlock);

    /**
     * Validates the Parameters of this Circuit when a value has been changed by the GUI
     * Also called right after @initParameters and when the Chunk reloads
     *
     * @param aCircuitData,          The Data Storage you can use (8 Slots and only the first 4 are User definable)
     * @param aRedstoneCircuitBlock, The Circuit Block MetaTileEntity itself
     */
    public abstract void validateParameters(int[] aCircuitData, IRedstoneCircuitBlock aRedstoneCircuitBlock);


    /****************************
     * Useful Utility Functions *
     ****************************/

    /**
     * Called every tick if the Block has enough Energy and if the Block is Active
     *
     * @param aCircuitData,          The Data Storage you can use (8 Slots)
     * @param aRedstoneCircuitBlock, The Circuit Block MetaTileEntity itself
     */
    public abstract void onTick(int[] aCircuitData, IRedstoneCircuitBlock aRedstoneCircuitBlock);

    /**
     * If the ItemStack should be displayed. Parameters are between 0 and 3.
     */
    public abstract boolean displayItemStack(int[] aCircuitData, IRedstoneCircuitBlock aRedstoneCircuitBlock, int aIndex);

    /**
     * The Name of the Gate for the GUI
     */
    @SideOnly(Side.CLIENT)
    public abstract String getName();

    /**
     * The Description of the Gate for the GUI
     */
    @SideOnly(Side.CLIENT)
    public abstract String getDescription();

    /**
     * The Description of the Data Field for the GUI
     */
    @SideOnly(Side.CLIENT)
    public abstract String getDataDescription(int[] aCircuitData, int aCircuitDataIndex);

    /**
     * How the Integer should be displayed in the GUI.
     * null means, that it just displays as regular Number.
     */
    @SideOnly(Side.CLIENT)
    public String getDataDisplay(int[] aCircuitData, int aCircuitDataIndex) {
        return null;
    }
}