aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/tectech/thing/metaTileEntity/multi/base/Parameters.java
blob: 27eb961166eb08cb4f0ebbf02bd9d903e1e79bea (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
package tectech.thing.metaTileEntity.multi.base;

import java.util.ArrayList;

/**
 * Instantiate parameters as field in parametersInstantiation_EM();
 */
public class Parameters {

    private static final IStatusFunction<?> LED_STATUS_FUNCTION_DEFAULT = (b, p) -> LedStatus.STATUS_UNDEFINED;
    private static final INameFunction<?> NAME_FUNCTION_DEFAULT = (b, p) -> "Undefined";

    final Group[] groups = new Group[10];

    double[] iParamsIn = new double[20]; // number I from parametrizers
    double[] iParamsOut = new double[20]; // number O to parametrizers
    final ArrayList<Group.ParameterIn> parameterInArrayList = new ArrayList<>();
    final ArrayList<Group.ParameterOut> parameterOutArrayList = new ArrayList<>();

    // package private for use in gui
    LedStatus[] eParamsInStatus = LedStatus.makeArray(20, LedStatus.STATUS_UNUSED); // LED status for I
    LedStatus[] eParamsOutStatus = LedStatus.makeArray(20, LedStatus.STATUS_UNUSED); // LED status for O

    double getIn(int hatchNo, int parameterId) {
        return iParamsIn[hatchNo + 10 * parameterId];
    }

    double getOut(int hatchNo, int parameterId) {
        return iParamsOut[hatchNo + 10 * parameterId];
    }

    LedStatus getStatusIn(int hatchNo, int parameterId) {
        return eParamsInStatus[hatchNo + 10 * parameterId];
    }

    LedStatus getStatusOut(int hatchNo, int parameterId) {
        return eParamsOutStatus[hatchNo + 10 * parameterId];
    }

    private final TTMultiblockBase parent;

    Parameters(TTMultiblockBase parent) {
        this.parent = parent;
    }

    public boolean trySetParameters(int hatch, double parameter0, double parameter1) {
        Group p = groups[hatch];
        if (parent.mMaxProgresstime <= 0 || (p != null && p.updateWhileRunning)) {
            iParamsIn[hatch] = parameter0;
            iParamsIn[hatch + 10] = parameter1;
            return true;
        }
        return false;
    }

    @SuppressWarnings("unused") // Used in GTNH-Intergalactic, do not delete.
    public boolean trySetParameters(int hatchNo, int parameterId, double parameter) {
        Group p = groups[hatchNo];
        if (parent.mMaxProgresstime <= 0 || (p != null && p.updateWhileRunning)) {
            iParamsIn[hatchNo + 10 * parameterId] = parameter;
            return true;
        }
        return false;
    }

    public void setToDefaults(int hatch, boolean defaultIn, boolean defaultOut) {
        Group p = groups[hatch];
        if (p == null) {
            if (defaultIn) {
                iParamsIn[hatch] = 0;
                iParamsIn[hatch + 10] = 0;
            }
            if (defaultOut) {
                iParamsOut[hatch] = 0;
                iParamsOut[hatch + 10] = 0;
            }
        } else {
            p.setToDefaults(defaultIn, defaultOut);
        }
    }

    public void setToDefaults(boolean defaultIn, boolean defaultOut) {
        for (int hatch = 0; hatch < 10; hatch++) {
            setToDefaults(hatch, defaultIn, defaultOut);
        }
    }

    public Group getGroup(int hatchNo, boolean updateWhileRunning) {
        return groups[hatchNo] != null ? groups[hatchNo] : new Group(hatchNo, updateWhileRunning);
    }

    public Group getGroup(int hatchNo) {
        return groups[hatchNo] != null ? groups[hatchNo] : new Group(hatchNo, false);
    }

    public interface IParameter {

        double get();

        double getDefault();

        void updateStatus();

        LedStatus getStatus(boolean update);

        int id();

        int hatchId();

        int parameterId();

        String getBrief();
    }

    /**
     * most likely used locally in parametersInstantiation_EM()
     */
    public class Group {

        private final int hatchNo;
        public final ParameterIn[] parameterIn = new ParameterIn[2];
        public final ParameterOut[] parameterOut = new ParameterOut[2];
        public boolean updateWhileRunning;

        private Group(int hatchNo, boolean updateWhileRunning) {
            if (hatchNo < 0 || hatchNo >= 10) {
                throw new IllegalArgumentException("Hatch id must be in 0 to 9 range");
            }
            this.hatchNo = hatchNo;
            this.updateWhileRunning = updateWhileRunning;
            groups[hatchNo] = this;
        }

        public ParameterIn makeInParameter(int paramID, double defaultValue, INameFunction<?> name,
            IStatusFunction<?> status) {
            return new ParameterIn(paramID, defaultValue, name, status);
        }

        public ParameterOut makeOutParameter(int paramID, double defaultValue, INameFunction<?> name,
            IStatusFunction<?> status) {
            return new ParameterOut(paramID, defaultValue, name, status);
        }

        public void setToDefaults(boolean defaultIn, boolean defaultOut) {
            if (defaultIn) {
                if (this.parameterIn[0] != null) {
                    this.parameterIn[0].setDefault();
                } else {
                    iParamsIn[hatchNo] = 0;
                }
                if (this.parameterIn[1] != null) {
                    this.parameterIn[1].setDefault();
                } else {
                    iParamsIn[hatchNo + 10] = 0;
                }
            }
            if (defaultOut) {
                if (this.parameterOut[0] != null) {
                    this.parameterOut[0].setDefault();
                } else {
                    iParamsOut[hatchNo] = 0;
                }
                if (this.parameterOut[1] != null) {
                    this.parameterOut[1].setDefault();
                } else {
                    iParamsOut[hatchNo + 10] = 0;
                }
            }
        }

        /**
         * Make a field out of this...
         */
        public class ParameterOut implements IParameter {

            public final int id;
            public final double defaultValue;
            IStatusFunction status;
            INameFunction name;

            private ParameterOut(int paramID, double defaultValue, INameFunction<?> name, IStatusFunction<?> status) {
                this.name = name == null ? NAME_FUNCTION_DEFAULT : name;
                if (paramID < 0 || paramID > 2) {
                    throw new IllegalArgumentException("Parameter id must be in 0 to 1 range");
                }
                if (parameterOut[paramID] != null) {
                    throw new IllegalArgumentException("Parameter id already occupied");
                }
                this.id = hatchNo + 10 * paramID;
                this.defaultValue = defaultValue;
                this.status = status == null ? LED_STATUS_FUNCTION_DEFAULT : status;
                parameterOutArrayList.add(this);
                parameterOut[paramID] = this;
            }

            void setDefault() {
                set(defaultValue);
            }

            @Override
            public double get() {
                return iParamsOut[id];
            }

            @Override
            public double getDefault() {
                return defaultValue;
            }

            public void set(double value) {
                iParamsOut[id] = value;
            }

            @SuppressWarnings("unchecked")
            @Override
            public void updateStatus() {
                eParamsOutStatus[id] = status.apply(parent, this);
            }

            @Override
            public LedStatus getStatus(boolean update) {
                if (update) {
                    updateStatus();
                }
                return eParamsOutStatus[id];
            }

            @Override
            public String getBrief() {
                return name.apply(parent, this);
            }

            @Override
            public int id() {
                return id;
            }

            @Override
            public int hatchId() {
                return id % 10;
            }

            @Override
            public int parameterId() {
                return id / 10;
            }
        }

        /**
         * Make a field out of this...
         */
        public class ParameterIn implements IParameter {

            public final int id;
            public final double defaultValue;
            IStatusFunction status;
            INameFunction name;

            private ParameterIn(int paramID, double defaultValue, INameFunction<?> name, IStatusFunction<?> status) {
                this.name = name == null ? NAME_FUNCTION_DEFAULT : name;
                this.id = hatchNo + 10 * paramID;
                if (paramID < 0 || paramID > 2) {
                    throw new IllegalArgumentException("Parameter id must be in 0 to 1 range");
                }
                if (parameterIn[paramID] != null) {
                    throw new IllegalArgumentException("Parameter id already occupied");
                }
                this.defaultValue = defaultValue;
                this.status = status == null ? LED_STATUS_FUNCTION_DEFAULT : status;
                parameterInArrayList.add(this);
                parameterIn[paramID] = this;
            }

            void setDefault() {
                set(defaultValue);
            }

            @Override
            public double get() {
                return iParamsIn[id];
            }

            void set(double value) {
                iParamsIn[id] = value;
            }

            @Override
            public double getDefault() {
                return defaultValue;
            }

            @SuppressWarnings("unchecked")
            @Override
            public void updateStatus() {
                eParamsInStatus[id] = status.apply(parent, this);
            }

            @Override
            public LedStatus getStatus(boolean update) {
                if (update) {
                    updateStatus();
                }
                return eParamsInStatus[id];
            }

            @Override
            public String getBrief() {
                return name.apply(parent, this);
            }

            @Override
            public int id() {
                return id;
            }

            @Override
            public int hatchId() {
                return id % 10;
            }

            @Override
            public int parameterId() {
                return id / 10;
            }
        }
    }
}