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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
package gregtech.api.util;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.function.Function;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap;
import com.gtnewhorizons.modularui.api.fluids.IFluidTankLong;
import gregtech.api.interfaces.fluid.IFluidStore;
import gregtech.api.interfaces.tileentity.IVoidable;
import gregtech.api.logic.FluidInventoryLogic;
import gregtech.api.logic.ItemInventoryLogic;
/**
* Helper class to calculate how many parallels of items / fluids can fit in the output buses / hatches.
*/
public class VoidProtectionHelper {
/**
* Machine used for calculation
*/
private IVoidable machine;
/**
* Does void protection enabled for items
*/
private boolean protectExcessItem;
/**
* Does void protection enabled for fluids
*/
private boolean protectExcessFluid;
/**
* The maximum possible parallel possible for the multiblock
*/
private int maxParallel = 1;
/**
* If item output is full.
*/
private boolean isItemFull;
/**
* If fluid output is full.
*/
private boolean isFluidFull;
/**
* The item outputs to check
*/
private ItemStack[] itemOutputs;
/**
* The fluid outputs to check
*/
private FluidStack[] fluidOutputs;
/**
* The item output inventory
*/
private ItemInventoryLogic itemOutputInventory;
/**
* The fluid output inventory
*/
private FluidInventoryLogic fluidOutputInventory;
/**
* Has this helper been built?
*/
private boolean built;
/**
* Is this helper working for a MuTE?
*/
private boolean muteMode;
/**
* Multiplier by which the output will be multiplied
*/
private int outputMultiplier = 1;
/**
* Multiplier that is applied on the output chances
*/
private double chanceMultiplier = 1;
private Function<Integer, Integer> chanceGetter = i -> 10000;
public VoidProtectionHelper() {}
/**
* Sets machine, with current configuration for void protection mode.
*/
public VoidProtectionHelper setMachine(IVoidable machine) {
return setMachine(machine, machine.protectsExcessItem(), machine.protectsExcessFluid());
}
/**
* Sets machine, with void protection mode forcibly.
*/
public VoidProtectionHelper setMachine(IVoidable machine, boolean protectExcessItem, boolean protectExcessFluid) {
this.protectExcessItem = protectExcessItem;
this.protectExcessFluid = protectExcessFluid;
this.machine = machine;
return this;
}
public VoidProtectionHelper setItemOutputs(ItemStack[] itemOutputs) {
this.itemOutputs = itemOutputs;
return this;
}
public VoidProtectionHelper setFluidOutputs(FluidStack[] fluidOutputs) {
this.fluidOutputs = fluidOutputs;
return this;
}
/**
* Sets the MaxParallel a multi can handle
*/
public VoidProtectionHelper setMaxParallel(int maxParallel) {
this.maxParallel = maxParallel;
return this;
}
public VoidProtectionHelper setItemOutputInventory(ItemInventoryLogic itemOutputInventory) {
this.itemOutputInventory = itemOutputInventory;
return this;
}
public VoidProtectionHelper setFluidOutputInventory(FluidInventoryLogic fluidOutputInventory) {
this.fluidOutputInventory = fluidOutputInventory;
return this;
}
public VoidProtectionHelper setMuTEMode(boolean muteMode) {
this.muteMode = muteMode;
return this;
}
public VoidProtectionHelper setOutputMultiplier(int outputMultiplier) {
this.outputMultiplier = outputMultiplier;
return this;
}
public VoidProtectionHelper setChanceMultiplier(double chanceMultiplier) {
this.chanceMultiplier = chanceMultiplier;
return this;
}
public VoidProtectionHelper setChangeGetter(Function<Integer, Integer> getter) {
this.chanceGetter = getter;
return this;
}
/**
* Finishes the VoidProtectionHelper. Anything changed after this will not affect anything
*/
public VoidProtectionHelper build() {
if (built) {
throw new IllegalStateException("Tried to build twice");
}
if (machine == null) {
throw new IllegalStateException("Machine is not set");
}
built = true;
determineParallel();
return this;
}
/**
* @return The current parallels possible by the multiblock
*/
public int getMaxParallel() {
if (!built) {
throw new IllegalStateException("Tried to get parallels before building");
}
return maxParallel;
}
/**
* @return If the calculation resulted in item output being full.
*/
public boolean isItemFull() {
if (!built) {
throw new IllegalStateException("Tried to get isItemFull before building");
}
return isItemFull;
}
/**
* @return If the calculation resulted in fluid output being full.
*/
public boolean isFluidFull() {
if (!built) {
throw new IllegalStateException("Tried to get isFluidFull before building");
}
return isFluidFull;
}
/**
* Called by {@link #build()}. Determines the parallels and everything else that needs to be done at build time
*/
private void determineParallel() {
if (itemOutputs == null) {
itemOutputs = new ItemStack[0];
}
if (fluidOutputs == null) {
fluidOutputs = new FluidStack[0];
}
// Don't check IVoidable#protectsExcessItem nor #protectsExcessFluid here,
// to allow more involved setting for void protections (see ComplexParallelProcessingLogic)
if (protectExcessItem && itemOutputs.length > 0 && !machine.canDumpItemToME()) {
maxParallel = Math.min(calculateMaxItemParallels(), maxParallel);
if (maxParallel <= 0) {
isItemFull = true;
return;
}
}
if (protectExcessFluid && fluidOutputs.length > 0) {
maxParallel = Math.min(calculateMaxFluidParallels(), maxParallel);
if (maxParallel <= 0) {
isFluidFull = true;
}
}
}
/**
* Calculates the max parallel for fluids if void protection is turned on
*/
private int calculateMaxFluidParallels() {
List<? extends IFluidStore> hatches = machine.getFluidOutputSlots(fluidOutputs);
if (hatches.size() < fluidOutputs.length) {
return 0;
}
// A map to hold the items we will be 'inputting' into the output hatches. These fluidstacks are actually
// the recipe outputs.
Map<FluidStack, Integer> tFluidOutputMap = new HashMap<>();
// Map that keeps track of the number of parallel crafts we can accommodate for each fluid output.
// In the pair, we keep track of number of full crafts plus mb of fluid in a partial craft, to avoid
// issues with floating point math not being completely accurate when summing.
Map<FluidStack, ParallelData> tParallels = new HashMap<>();
// Iterate over the outputs, calculating require stack spacing they will require.
for (FluidStack aY : fluidOutputs) {
if (aY == null || aY.amount <= 0) {
continue;
}
tFluidOutputMap.merge(aY, aY.amount, Integer::sum);
tParallels.put(aY, new ParallelData(0, 0));
}
if (tFluidOutputMap.isEmpty()) {
// nothing to output, bail early
return maxParallel;
}
for (IFluidStore tHatch : hatches) {
int tSpaceLeft = tHatch.getAvailableSpace();
// check if hatch filled
if (tSpaceLeft <= 0) continue;
// check if hatch is empty and unrestricted
if (tHatch.isEmptyAndAcceptsAnyFluid()) continue;
for (Map.Entry<FluidStack, ParallelData> entry : tParallels.entrySet()) {
FluidStack tFluidOutput = entry.getKey();
if (!tHatch.canStoreFluid(tFluidOutput)) continue;
// this fluid is not prevented by restrictions on output hatch
ParallelData tParallel = entry.getValue();
Integer tCraftSize = tFluidOutputMap.get(tFluidOutput);
tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
}
}
// now that all partial/restricted hatches have been counted, create a priority queue for our outputs
// the lowest priority fluid is the number of complete parallel crafts we can support
PriorityQueue<ParallelStackInfo<FluidStack>> aParallelQueue = new PriorityQueue<>(
Comparator.comparing(i -> i.batch));
for (Map.Entry<FluidStack, ParallelData> entry : tParallels.entrySet()) {
aParallelQueue
.add(new ParallelStackInfo<>(entry.getValue().batch, entry.getValue().partial, entry.getKey()));
}
// add extra parallels for open slots as well
for (IFluidStore tHatch : hatches) {
// partially filled or restricted hatch. done in the last pass
if (!tHatch.isEmptyAndAcceptsAnyFluid()) continue;
ParallelStackInfo<FluidStack> tParallel = aParallelQueue.poll();
assert tParallel != null; // will always be true, specifying assert here to avoid IDE/compiler warnings
Integer tCraftSize = tFluidOutputMap.get(tParallel.stack);
int tSpaceLeft = tHatch.getAvailableSpace();
tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
aParallelQueue.add(tParallel);
}
return aParallelQueue.element().batch;
}
private int calculateMaxFluidParallelsMuTE() {
if (fluidOutputs.length > fluidOutputInventory.getInventory()
.getTanks()) {
return 0;
}
// A map to hold the items we will be 'inputting' into the output hatches. These fluidstacks are actually
// the recipe outputs.
Map<FluidStack, Integer> tFluidOutputMap = new HashMap<>();
// Map that keeps track of the number of parallel crafts we can accommodate for each fluid output.
// In the pair, we keep track of number of full crafts plus mb of fluid in a partial craft, to avoid
// issues with floating point math not being completely accurate when summing.
Map<FluidStack, ParallelData> tParallels = new HashMap<>();
// Iterate over the outputs, calculating require stack spacing they will require.
for (FluidStack aY : fluidOutputs) {
if (aY == null) continue;
int fluidAmount = aY.amount * outputMultiplier;
if (fluidAmount <= 0) continue;
tFluidOutputMap.merge(aY, fluidAmount, Integer::sum);
tParallels.put(aY, new ParallelData(0, 0));
}
if (tFluidOutputMap.isEmpty()) {
// nothing to output, bail early
return maxParallel;
}
for (int i = 0; i < fluidOutputInventory.getInventory()
.getTanks(); i++) {
IFluidTankLong tank = fluidOutputInventory.getInventory()
.getFluidTank(i);
long tSpaceLeft = tank.getCapacityLong() - tank.getFluidAmountLong();
// check if hatch filled
if (tSpaceLeft <= 0) continue;
// check if hatch is empty and unrestricted
if (tank.getStoredFluid() == null) continue;
for (Map.Entry<FluidStack, ParallelData> entry : tParallels.entrySet()) {
FluidStack tFluidOutput = entry.getKey();
if (tank.fill(tFluidOutput.getFluid(), tFluidOutput.amount, false) == tFluidOutput.amount) continue;
// this fluid is not prevented by restrictions on output hatch
ParallelData tParallel = entry.getValue();
Integer tCraftSize = tFluidOutputMap.get(tFluidOutput);
tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
}
}
// now that all partial/restricted hatches have been counted, create a priority queue for our outputs
// the lowest priority fluid is the number of complete parallel crafts we can support
PriorityQueue<ParallelStackInfo<FluidStack>> aParallelQueue = new PriorityQueue<>(
Comparator.comparing(i -> i.batch));
for (Map.Entry<FluidStack, ParallelData> entry : tParallels.entrySet()) {
aParallelQueue
.add(new ParallelStackInfo<>(entry.getValue().batch, entry.getValue().partial, entry.getKey()));
}
// add extra parallels for open slots as well
for (int i = 0; i < fluidOutputInventory.getInventory()
.getTanks(); i++) {
IFluidTankLong tank = fluidOutputInventory.getInventory()
.getFluidTank(i);
// partially filled or restricted hatch. done in the last pass
if (tank.getStoredFluid() != null) continue;
ParallelStackInfo<FluidStack> tParallel = aParallelQueue.poll();
assert tParallel != null; // will always be true, specifying assert here to avoid IDE/compiler warnings
Integer tCraftSize = tFluidOutputMap.get(tParallel.stack);
long tSpaceLeft = tank.getCapacityLong();
tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
aParallelQueue.add(tParallel);
}
return aParallelQueue.element().batch;
}
/**
* Calculates the max parallels one can do with items if void protection is on
*/
private int calculateMaxItemParallels() {
List<ItemStack> busStacks;
if (muteMode) {
busStacks = itemOutputInventory.getInventory()
.getStacks();
} else {
busStacks = machine.getItemOutputSlots(itemOutputs);
}
// A map to hold the items we will be 'inputting' into the output buses. These itemstacks are actually the
// recipe outputs.
Map<ItemStack, Integer> tItemOutputMap = new ItemStackMap<>();
// Map that keeps track of the number of parallel crafts we can accommodate for each item output.
// In the pair, we keep track of number of full crafts plus number of items in a partial craft, to avoid
// issues with floating point math not being completely accurate when summing.
Map<ItemStack, ParallelData> tParallels = new ItemStackMap<>();
int tSlotsFree = 0;
int index = 0;
for (ItemStack tItem : itemOutputs) {
// GTRecipeBuilder doesn't handle null item output
if (tItem == null) continue;
int itemStackSize = (int) (tItem.stackSize * outputMultiplier
* Math.ceil(chanceMultiplier * chanceGetter.apply(index++) / 10000));
if (itemStackSize <= 0) continue;
tItemOutputMap.merge(tItem, itemStackSize, Integer::sum);
tParallels.put(tItem, new ParallelData(0, 0));
}
if (tItemOutputMap.isEmpty()) {
// nothing to output, bail early
return maxParallel;
}
if (itemOutputs.length > 0) {
for (ItemStack tBusStack : busStacks) {
if (tBusStack == null) {
tSlotsFree++;
} else {
// get the real stack size
// we ignore the bus inventory stack limit here as no one set it to anything other than 64
int tMaxBusStackSize = tBusStack.getMaxStackSize();
if (tBusStack.stackSize >= tMaxBusStackSize)
// this bus stack is full. no checking
continue;
int tSpaceLeft = tMaxBusStackSize - tBusStack.stackSize;
Integer tCraftSize = tItemOutputMap.get(tBusStack);
if (tCraftSize == null) {
// we don't have a matching stack to output, ignore this bus stack
continue;
}
ParallelData tParallel = tParallels.get(tBusStack);
tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
}
}
// now that all partial stacks have been counted, create a priority queue for our outputs
// the lowest priority item is the number of complete parallel crafts we can support
PriorityQueue<ParallelStackInfo<ItemStack>> aParallelQueue = new PriorityQueue<>(
Comparator.comparing(i -> i.batch));
for (Map.Entry<ItemStack, ParallelData> entry : tParallels.entrySet()) {
aParallelQueue
.add(new ParallelStackInfo<>(entry.getValue().batch, entry.getValue().partial, entry.getKey()));
}
while (tSlotsFree > 0) {
ParallelStackInfo<ItemStack> tParallel = aParallelQueue.poll();
assert tParallel != null; // will always be true, specifying assert here to avoid IDE/compiler warnings
Integer tCraftSize = tItemOutputMap.get(tParallel.stack);
int tStackSize = tParallel.stack.getMaxStackSize();
tParallel.batch += (tParallel.partial + tStackSize) / tCraftSize;
tParallel.partial = (tParallel.partial + tStackSize) % tCraftSize;
aParallelQueue.add(tParallel);
--tSlotsFree;
}
return aParallelQueue.element().batch;
}
return 0;
}
private static class ParallelData {
private int batch;
private long partial;
private ParallelData(int batch, long partial) {
this.batch = batch;
this.partial = partial;
}
}
private static class ParallelStackInfo<T> {
private int batch;
private long partial;
private final T stack;
private ParallelStackInfo(int batch, long partial, T stack) {
this.batch = batch;
this.partial = partial;
this.stack = stack;
}
}
}
|