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
|
package gregtech.api.logic;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import gregtech.api.interfaces.tileentity.IVoidable;
import gregtech.api.recipe.RecipeMap;
import gregtech.api.recipe.check.CheckRecipeResult;
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
import gregtech.api.util.GTRecipe;
import gregtech.api.util.OverclockCalculator;
import gregtech.api.util.ParallelHelper;
/**
* Logic class to calculate result of recipe check from inputs.
*/
@SuppressWarnings({ "unused", "UnusedReturnValue" })
public abstract class AbstractProcessingLogic<P extends AbstractProcessingLogic<P>> {
protected IVoidable machine;
protected Supplier<RecipeMap<?>> recipeMapSupplier;
protected GTRecipe lastRecipe;
protected RecipeMap<?> lastRecipeMap;
protected ItemStack[] outputItems;
protected FluidStack[] outputFluids;
protected long calculatedEut;
protected int duration;
protected long availableVoltage;
protected long availableAmperage;
protected double overClockTimeReduction = 2.0;
protected double overClockPowerIncrease = 4.0;
protected boolean protectItems;
protected boolean protectFluids;
protected int maxParallel = 1;
protected Supplier<Integer> maxParallelSupplier;
protected int calculatedParallels = 0;
protected int batchSize = 1;
protected double euModifier = 1.0;
protected double speedBoost = 1.0;
protected boolean amperageOC = true;
protected boolean isCleanroom;
// #region Setters
/**
* Overwrites item output result of the calculation.
*/
public P setOutputItems(ItemStack... itemOutputs) {
this.outputItems = itemOutputs;
return getThis();
}
/**
* Overwrites fluid output result of the calculation.
*/
public P setOutputFluids(FluidStack... fluidOutputs) {
this.outputFluids = fluidOutputs;
return getThis();
}
public P setIsCleanroom(boolean isCleanroom) {
this.isCleanroom = isCleanroom;
return getThis();
}
/**
* Sets max amount of parallel.
*/
public P setMaxParallel(int maxParallel) {
this.maxParallel = maxParallel;
return getThis();
}
/**
* Sets method to get max amount of parallel.
*/
public P setMaxParallelSupplier(Supplier<Integer> supplier) {
this.maxParallelSupplier = supplier;
return getThis();
}
/**
* Sets batch size for batch mode.
*/
public P setBatchSize(int size) {
this.batchSize = size;
return getThis();
}
public P setRecipeMap(RecipeMap<?> recipeMap) {
return setRecipeMapSupplier(() -> recipeMap);
}
public P setRecipeMapSupplier(Supplier<RecipeMap<?>> supplier) {
this.recipeMapSupplier = supplier;
return getThis();
}
public P setEuModifier(double modifier) {
this.euModifier = modifier;
return getThis();
}
public P setSpeedBonus(double speedModifier) {
this.speedBoost = speedModifier;
return getThis();
}
/**
* Sets machine used for void protection logic.
*/
public P setMachine(IVoidable machine) {
this.machine = machine;
return getThis();
}
/**
* Overwrites duration result of the calculation.
*/
public P setDuration(int duration) {
this.duration = duration;
return getThis();
}
/**
* Overwrites EU/t result of the calculation.
*/
public P setCalculatedEut(long calculatedEut) {
this.calculatedEut = calculatedEut;
return getThis();
}
/**
* Sets voltage of the machine. It doesn't need to be actual voltage (excluding amperage) of the machine;
* For example, most of the multiblock machines set maximum possible input power (including amperage) as voltage
* and 1 as amperage. That way recipemap search will be executed with overclocked voltage.
*/
public P setAvailableVoltage(long voltage) {
availableVoltage = voltage;
return getThis();
}
/**
* Sets amperage of the machine. This amperage doesn't involve in EU/t when searching recipemap.
* Useful for preventing tier skip but still considering amperage for parallel.
*/
public P setAvailableAmperage(long amperage) {
availableAmperage = amperage;
return getThis();
}
public P setVoidProtection(boolean protectItems, boolean protectFluids) {
this.protectItems = protectItems;
this.protectFluids = protectFluids;
return getThis();
}
public P setOverclock(double timeReduction, double powerIncrease) {
this.overClockTimeReduction = timeReduction;
this.overClockPowerIncrease = powerIncrease;
return getThis();
}
/**
* Sets overclock ratio to 4/4.
*/
public P enablePerfectOverclock() {
return this.setOverclock(4.0, 4.0);
}
/**
* Sets whether the multi should use amperage to OC or not
*/
public P setAmperageOC(boolean amperageOC) {
this.amperageOC = amperageOC;
return getThis();
}
/**
* Clears calculated results (and provided machine inputs) to prepare for the next machine operation.
*/
public P clear() {
this.calculatedEut = 0;
this.duration = 0;
this.calculatedParallels = 0;
return getThis();
}
// #endregion
// #region Logic
/**
* Executes the recipe check: Find recipe from recipemap, Calculate parallel, overclock and outputs.
*/
@Nonnull
public abstract CheckRecipeResult process();
/**
* Refreshes recipemap to use. Remember to call this before {@link #process} to make sure correct recipemap is used.
*
* @return Recipemap to use now
*/
protected RecipeMap<?> preProcess() {
RecipeMap<?> recipeMap;
if (recipeMapSupplier == null) {
recipeMap = null;
} else {
recipeMap = recipeMapSupplier.get();
}
if (lastRecipeMap != recipeMap) {
lastRecipe = null;
lastRecipeMap = recipeMap;
}
if (maxParallelSupplier != null) {
maxParallel = maxParallelSupplier.get();
}
return recipeMap;
}
/**
* Check has been succeeded, so it applies the recipe and calculated parameters.
* At this point, inputs have been already consumed.
*/
@Nonnull
protected CheckRecipeResult applyRecipe(@Nonnull GTRecipe recipe, @Nonnull ParallelHelper helper,
@Nonnull OverclockCalculator calculator, @Nonnull CheckRecipeResult result) {
if (recipe.mCanBeBuffered) {
lastRecipe = recipe;
} else {
lastRecipe = null;
}
calculatedParallels = helper.getCurrentParallel();
if (calculator.getConsumption() == Long.MAX_VALUE) {
return CheckRecipeResultRegistry.POWER_OVERFLOW;
}
if (calculator.getDuration() == Integer.MAX_VALUE) {
return CheckRecipeResultRegistry.DURATION_OVERFLOW;
}
calculatedEut = calculator.getConsumption();
double finalDuration = calculateDuration(recipe, helper, calculator);
if (finalDuration >= Integer.MAX_VALUE) {
return CheckRecipeResultRegistry.DURATION_OVERFLOW;
}
duration = (int) finalDuration;
CheckRecipeResult hookResult = onRecipeStart(recipe);
if (!hookResult.wasSuccessful()) {
return hookResult;
}
outputItems = helper.getItemOutputs();
outputFluids = helper.getFluidOutputs();
return result;
}
/**
* Override to tweak final duration that will be set as a result of this logic class.
*/
protected double calculateDuration(@Nonnull GTRecipe recipe, @Nonnull ParallelHelper helper,
@Nonnull OverclockCalculator calculator) {
return calculator.getDuration() * helper.getDurationMultiplierDouble();
}
/**
* Override to do additional check for found recipe if needed.
*/
@Nonnull
protected CheckRecipeResult validateRecipe(@Nonnull GTRecipe recipe) {
return CheckRecipeResultRegistry.SUCCESSFUL;
}
/**
* Override to perform additional logic when recipe starts.
* <p>
* This is called when the recipe processing logic has finished all
* checks, consumed all inputs, but has not yet set the outputs to
* be produced. Returning a result other than SUCCESSFUL will void
* all inputs!
*/
@Nonnull
protected CheckRecipeResult onRecipeStart(@Nonnull GTRecipe recipe) {
return CheckRecipeResultRegistry.SUCCESSFUL;
}
/**
* Override to tweak overclock logic if needed.
*/
@Nonnull
protected OverclockCalculator createOverclockCalculator(@Nonnull GTRecipe recipe) {
return new OverclockCalculator().setRecipeEUt(recipe.mEUt)
.setAmperage(availableAmperage)
.setEUt(availableVoltage)
.setDuration(recipe.mDuration)
.setSpeedBoost(speedBoost)
.setEUtDiscount(euModifier)
.setAmperageOC(amperageOC)
.setDurationDecreasePerOC(overClockTimeReduction)
.setEUtIncreasePerOC(overClockPowerIncrease);
}
// #endregion
// #region Getters
public ItemStack[] getOutputItems() {
return outputItems;
}
public FluidStack[] getOutputFluids() {
return outputFluids;
}
public int getDuration() {
return duration;
}
public long getCalculatedEut() {
return calculatedEut;
}
public int getCurrentParallels() {
return calculatedParallels;
}
@SuppressWarnings("unchecked")
@Nonnull
public P getThis() {
return (P) this;
}
// #endregion
}
|