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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
package gregtech.common.tileentities.machines.multi;
import static bartworks.API.GlassTier.getGlassTier;
import static gregtech.api.enums.GTValues.debugCleanroom;
import static gregtech.api.enums.Textures.BlockIcons.BLOCK_PLASCRETE;
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_TOP_CLEANROOM;
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_TOP_CLEANROOM_ACTIVE;
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_TOP_CLEANROOM_ACTIVE_GLOW;
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_TOP_CLEANROOM_GLOW;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nonnull;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.gtnewhorizon.structurelib.alignment.constructable.IConstructable;
import gregtech.api.GregTechAPI;
import gregtech.api.enums.TierEU;
import gregtech.api.interfaces.ICleanroom;
import gregtech.api.interfaces.ICleanroomReceiver;
import gregtech.api.interfaces.ISecondaryDescribable;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.MTEBasicHull;
import gregtech.api.metatileentity.implementations.MTEHatchEnergy;
import gregtech.api.metatileentity.implementations.MTEHatchMaintenance;
import gregtech.api.metatileentity.implementations.MTETooltipMultiBlockBase;
import gregtech.api.recipe.check.CheckRecipeResult;
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
import gregtech.api.recipe.check.SimpleCheckRecipeResult;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTLog;
import gregtech.api.util.MultiblockTooltipBuilder;
import gregtech.common.config.MachineStats;
public class MTECleanroom extends MTETooltipMultiBlockBase
implements IConstructable, ISecondaryDescribable, ICleanroom {
/**
* Maximum width (horizontal size) of the cleanroom. Includes walls.
*/
public static final int MAX_WIDTH = 15;
/**
* Maximum height of the cleanroom. Includes floor and ceiling.
*/
public static final int MAX_HEIGHT = 15;
/**
* List of other blocks allowed in the cleanroom.
* Format of entries is either just the block's unlocalized name, or <unlocalized name>:<meta>. The former matches
* all blocks of that name regardless of meta value. Read from config file.
*/
public static final HashSet<String> ALLOWED_BLOCKS = new HashSet<>();
// Plascrete blocks.
protected static Block CASING_BLOCK;
protected static final int CASING_META = 2;
// To color hatches.
protected static final int CASING_INDEX = 210;
// Filter casings.
protected static Block FILTER_BLOCK;
protected static final int FILTER_META = 11;
// Minimum valid tier of glass. All glasses of at least this tier are always allowed;
// lower tier glasses can be added separately in ALLOWED_BLOCKS.
protected static final int MIN_GLASS_TIER = 4; // EV
private final Set<ICleanroomReceiver> cleanroomReceivers = new HashSet<>();
private int mHeight = -1;
public MTECleanroom(int aID, String aName, String aNameRegional) {
super(aID, aName, aNameRegional);
}
public MTECleanroom(String aName) {
super(aName);
}
@Override
public IMetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
return new MTECleanroom(mName);
}
@Override
public int getCleanness() {
return mEfficiency;
}
@Override
public boolean isValidCleanroom() {
return isValid() && mMachine;
}
@Override
public void pollute() {
mEfficiency = 0;
mWrench = false;
mScrewdriver = false;
mSoftHammer = false;
mHardHammer = false;
mSolderingTool = false;
mCrowbar = false;
}
@Override
protected MultiblockTooltipBuilder createTooltip() {
final MultiblockTooltipBuilder tt = new MultiblockTooltipBuilder();
tt.addMachineType("Cleanroom")
.addInfo("Consumes 40 EU/t when first turned on, and 4 EU/t once at 100% efficiency.")
.addInfo("Can accept 2A from an LV energy hatch.")
.addInfo("Will overclock and gain efficiency faster starting from HV.")
.addSeparator()
.addInfo(EnumChatFormatting.RED + "Warning:")
.addInfo("Below 100% efficiency machines inside have a chance to void outputs!")
.addInfo("Each maintenance issue reduces maximum efficiency by 10%.")
.addInfo("Generating any pollution inside causes the cleanroom to shut down.")
.beginVariableStructureBlock(3, MAX_WIDTH, 4, MAX_HEIGHT, 3, MAX_WIDTH, true)
.addController("Top center.")
.addStructureInfo(" If width or length is even, it can be in either of the two middle positions.")
.addOtherStructurePart("Filter Machine Casing", "Top layer, except for edges.")
.addOtherStructurePart(
"Plascrete Blocks",
"Edges of top layer, all walls and floor. Minimum " + EnumChatFormatting.GOLD
+ MachineStats.cleanroom.minCasingCount
+ EnumChatFormatting.GRAY
+ ".")
.addEnergyHatch("Any Plascrete Block. Exactly one.")
.addMaintenanceHatch("Any Plascrete Block. Exactly one.")
.addStructureInfo("")
.addStructureInfo(
"Up to " + EnumChatFormatting.GOLD
+ MachineStats.cleanroom.maxReplacementPercentage
+ "%"
+ EnumChatFormatting.GRAY
+ " of plascrete blocks can be replaced by other valid blocks.")
.addStructureInfo("Try some of the following:")
.addStructureInfo(
"- Any " + EnumChatFormatting.DARK_GRAY + "EV+" + EnumChatFormatting.GRAY + " tier glass.")
.addStructureInfo("- Machine hulls or diodes for item and power transfer.")
.addStructureInfo(
"- Reinforced Doors (" + EnumChatFormatting.ITALIC
+ "IC2"
+ EnumChatFormatting.RESET
+ EnumChatFormatting.GRAY
+ "). Keep closed, no gaps allowed or efficiency will drop!")
.addStructureInfo(
"- Elevators (" + EnumChatFormatting.ITALIC
+ "OpenBlocks"
+ EnumChatFormatting.RESET
+ EnumChatFormatting.GRAY
+ ") or Travel Anchors ("
+ EnumChatFormatting.ITALIC
+ "EnderIO"
+ EnumChatFormatting.RESET
+ EnumChatFormatting.GRAY
+ ").")
.addStructureInfo(
"See " + EnumChatFormatting.DARK_GRAY
+ "config/GregTech/MachineStats.cfg"
+ EnumChatFormatting.GRAY
+ " for more valid blocks.")
.addStructureInfo(
EnumChatFormatting.YELLOW
+ "All non-plascrete blocks now share the same limit. Feel free to mix and match!")
.toolTipFinisher();
return tt;
}
@Override
public String[] getStructureDescription(ItemStack itemStack) {
return new String[] { "The base can be rectangular." };
}
@Nonnull
@Override
public CheckRecipeResult checkProcessing() {
mEfficiencyIncrease = 100;
final long inputVoltage = getMaxInputVoltage();
// only allow LV+ energy hatches
if (inputVoltage < TierEU.LV) {
return CheckRecipeResultRegistry.insufficientPower(40);
}
// use the standard overclock mechanism to determine duration and estimate a maximum consumption
// if the cleanroom is powered by an LV energy hatch, it will actually accept 2A instead of just 1A.
calculateOverclockedNessMultiInternal(
40,
45 * Math.max(1, mHeight - 1),
inputVoltage == TierEU.LV ? 2 : 1,
inputVoltage,
false);
// negate it to trigger the special energy consumption function. divide by 10 to get the actual final
// consumption.
mEUt /= -10;
return SimpleCheckRecipeResult.ofSuccess("cleanroom_running");
}
@Override
public boolean isFacingValid(ForgeDirection facing) {
return (facing.flag & (ForgeDirection.UP.flag | ForgeDirection.DOWN.flag)) == 0;
}
/*
* Structure check
*/
// Extent in all directions. Specifically the offset from the controller to each wall.
// Min values will always be negative, Max values positive.
protected int dxMin = 0, dxMax = 0, dzMin = 0, dzMax = 0, dyMin = 0;
// Total number of plascrete blocks in the structure.
protected int casingCount;
// Total number of other blocks in the structure. Does NOT count filter casings or the controller.
protected int otherCount;
// Whether the cleanroom contains a door that is "open", efficiency is constantly reduced.
protected boolean isDoorOpen;
private enum CleanroomBlockType {
CASING, // Plascrete block.
FILTER, // Filter casing.
GLASS, // Any EV+ tiered glass.
OTHER, // Another allowed replacement block.
DOOR, // Reinforced door (IC2).
HATCH_ENERGY, // Energy hatch.
HATCH_MAINTENANCE, // Maintenance hatch.
HATCH_DIODE, // Diode or machine hull.
INVALID // Invalid block.
}
// Specify which blocks are allowed where. This skips checks for other blocks.
private static final int MASK_CASING = 1;
private static final int MASK_FILTER = 1 << 1;
private static final int MASK_GLASS = 1 << 2;
private static final int MASK_OTHER = 1 << 3;
private static final int MASK_DOOR = 1 << 4;
private static final int MASK_HATCH = 1 << 5;
// Ceiling blocks NOT including edges.
private static final int MASK_CEILING_INTERNAL = MASK_FILTER;
// Edges of the ceiling layer. Includes corners of the top layer.
private static final int MASK_CEILING_EDGE = MASK_CASING | MASK_GLASS | MASK_OTHER | MASK_HATCH;
// Blocks in the wall, not including vertical edges.
private static final int MASK_WALL_INTERNAL = MASK_CASING | MASK_GLASS | MASK_OTHER | MASK_DOOR | MASK_HATCH;
// Vertical edges of walls, not including any corners.
private static final int MASK_WALL_EDGE = MASK_CASING | MASK_GLASS | MASK_OTHER | MASK_HATCH;
// Floor, not including edges or corners.
private static final int MASK_FLOOR_INTERNAL = MASK_CASING | MASK_GLASS | MASK_OTHER | MASK_HATCH;
// Bottom horizontal edges and corners.
private static final int MASK_FLOOR_EDGE = MASK_CASING | MASK_GLASS | MASK_OTHER | MASK_HATCH;
/**
* Determines the type of the block at a specified offset from the controller. Only types specified by allowedMask
* are checked, for efficiency. If a block is not one of the allowed types, CleanroomBlockType.INVALID is returned.
*/
private CleanroomBlockType getBlockType(IGregTechTileEntity aBaseMetaTileEntity, int dx, int dy, int dz,
int allowedMask) {
Block block = aBaseMetaTileEntity.getBlockOffset(dx, dy, dz);
int meta = aBaseMetaTileEntity.getMetaIDOffset(dx, dy, dz);
if ((allowedMask & MASK_CASING) != 0 && block == CASING_BLOCK && meta == CASING_META)
return CleanroomBlockType.CASING;
if ((allowedMask & MASK_FILTER) != 0 && block == FILTER_BLOCK && meta == FILTER_META)
return CleanroomBlockType.FILTER;
if ((allowedMask & MASK_GLASS) != 0 && getGlassTier(block, meta) >= MIN_GLASS_TIER)
return CleanroomBlockType.GLASS;
if ((allowedMask & MASK_OTHER) != 0 && (ALLOWED_BLOCKS.contains(block.getUnlocalizedName())
|| ALLOWED_BLOCKS.contains(block.getUnlocalizedName() + ":" + meta))) return CleanroomBlockType.OTHER;
if ((allowedMask & MASK_DOOR) != 0
// This allows doors on the edges, although their open/closed status will not be calculated correctly.
// The intent is that the wall check calling this method will not allow doors on edges.
&& block instanceof ic2.core.block.BlockIC2Door) {
if (!isDoorOpen) { // No need to check again if there is already an open door somewhere else.
int doorOrientation = getDoorOrientation(aBaseMetaTileEntity, dx, dy, dz);
if (doorOrientation < 0) {
// Somehow an invalid door block.
if (debugCleanroom)
GTLog.out.println("Cleanroom: Invalid block at offset (" + dx + ", " + dy + ", " + dz + ").");
return CleanroomBlockType.INVALID;
}
if (doorOrientation % 2 == 0) {
// Door on the W or E side (aligned with Z axis).
if (dx != dxMin && dx != dxMax)
// Door is in the N or S wall, definitely open.
isDoorOpen = true;
// Otherwise check adjacent blocks for other doors.
else if (dz > dzMin
&& aBaseMetaTileEntity.getBlockOffset(dx, dy, dz - 1) instanceof ic2.core.block.BlockIC2Door
&& doorOrientation != getDoorOrientation(aBaseMetaTileEntity, dx, dy, dz - 1))
isDoorOpen = true;
else if (dz < dzMax
&& aBaseMetaTileEntity.getBlockOffset(dx, dy, dz + 1) instanceof ic2.core.block.BlockIC2Door
&& doorOrientation != getDoorOrientation(aBaseMetaTileEntity, dx, dy, dz + 1))
isDoorOpen = true;
} else {
// Door on the N or S side (aligned with X axis).
if (dz != dzMin && dz != dzMax)
// Door is in the N or S wall, definitely open.
isDoorOpen = true;
// Check adjacent blocks for other doors.
else if (dx > dxMin
&& aBaseMetaTileEntity.getBlockOffset(dx - 1, dy, dz) instanceof ic2.core.block.BlockIC2Door
&& doorOrientation != getDoorOrientation(aBaseMetaTileEntity, dx - 1, dy, dz))
isDoorOpen = true;
else if (dx < dxMax
&& aBaseMetaTileEntity.getBlockOffset(dx + 1, dy, dz) instanceof ic2.core.block.BlockIC2Door
&& doorOrientation != getDoorOrientation(aBaseMetaTileEntity, dx + 1, dy, dz))
isDoorOpen = true;
}
if (debugCleanroom && isDoorOpen) {
GTLog.out.println("Cleanroom: Open door at offset (" + dx + ", " + dy + ", " + dz + ").");
}
}
return CleanroomBlockType.DOOR;
}
if ((allowedMask & MASK_HATCH) != 0) {
IGregTechTileEntity te = aBaseMetaTileEntity.getIGregTechTileEntityOffset(dx, dy, dz);
if (te != null) {
IMetaTileEntity mte = te.getMetaTileEntity();
if (mte instanceof MTEHatchMaintenance) return CleanroomBlockType.HATCH_MAINTENANCE;
else if (mte instanceof MTEHatchEnergy) return CleanroomBlockType.HATCH_ENERGY;
// Both hulls and diodes are instanceof MTEBasicHull.
else if (mte instanceof MTEBasicHull) return CleanroomBlockType.HATCH_DIODE;
else return CleanroomBlockType.INVALID;
}
}
return CleanroomBlockType.INVALID;
}
/**
* Add a block to the cleanroom which is at the specified offset. This properly increases the count of
* casings/non-casings, and if the block is a hatch, also adds it to the appropriate list.
*
* @param allowedMask specifies which types of blocks should be allowed at this position. Any other type of block is
* considered invalid.
* @return True on success (block was correctly added), false on failure (invalid block type).
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
protected boolean addStructureBlock(IGregTechTileEntity aBaseMetaTileEntity, int dx, int dy, int dz,
int allowedMask) {
switch (getBlockType(aBaseMetaTileEntity, dx, dy, dz, allowedMask)) {
case CASING:
++casingCount;
return true;
case FILTER:
return true;
case GLASS:
case OTHER:
case DOOR:
case HATCH_DIODE:
++otherCount;
return true;
case HATCH_ENERGY:
addEnergyInputToMachineList(aBaseMetaTileEntity.getIGregTechTileEntityOffset(dx, dy, dz), CASING_INDEX);
++otherCount;
return true;
case HATCH_MAINTENANCE:
addMaintenanceToMachineList(aBaseMetaTileEntity.getIGregTechTileEntityOffset(dx, dy, dz), CASING_INDEX);
++otherCount;
return true;
case INVALID:
if (debugCleanroom)
GTLog.out.println("Cleanroom: Invalid block at offset (" + dx + ", " + dy + ", " + dz + ").");
return false;
default:
throw new IllegalArgumentException(
"Cleanroom error: unknown block type at at offset (" + dx + ", " + dy + ", " + dz + ").");
}
}
/**
* Find the horizontal size of the cleanroom. Populates values dxMin, dxMax, dzMin, and dzMax.
*
* @return True on success, false on failure (which means an invalid structure).
*/
protected boolean checkSize(IGregTechTileEntity aBaseMetaTileEntity) {
// Footprint must be a rectangle. If the width is odd, the controller must be in the middle.
// If the width is even, controller must be one of the two middle blocks.
// X direction
for (dxMin = -1; dxMin >= -MAX_WIDTH / 2; --dxMin) {
if (getBlockType(aBaseMetaTileEntity, dxMin, 0, 0, MASK_CEILING_INTERNAL) == CleanroomBlockType.INVALID) {
break;
}
}
if (dxMin < -MAX_WIDTH / 2) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Too large (x-axis).");
return false;
}
for (dxMax = 1; dxMax <= MAX_WIDTH / 2; ++dxMax) {
if (getBlockType(aBaseMetaTileEntity, dxMax, 0, 0, MASK_CEILING_INTERNAL) == CleanroomBlockType.INVALID) {
break;
}
}
if (dxMax > MAX_WIDTH / 2) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Too large (x-axis).");
return false;
}
if (Math.abs(dxMin + dxMax) > 1) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Controller not centered (x-axis).");
return false;
}
// Z direction
for (dzMin = -1; dzMin >= -MAX_WIDTH / 2; --dzMin) {
if (getBlockType(aBaseMetaTileEntity, 0, 0, dzMin, MASK_CEILING_INTERNAL) == CleanroomBlockType.INVALID) {
break;
}
}
if (dzMin < -MAX_WIDTH / 2) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Too large (z-axis).");
return false;
}
for (dzMax = 1; dzMax <= MAX_WIDTH / 2; ++dzMax) {
if (getBlockType(aBaseMetaTileEntity, 0, 0, dzMax, MASK_CEILING_INTERNAL) == CleanroomBlockType.INVALID) {
break;
}
}
if (dzMax > MAX_WIDTH / 2) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Too large (z-axis).");
return false;
}
if (Math.abs(dzMin + dzMax) > 1) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Controller not centered (z-axis).");
return false;
}
if (debugCleanroom) GTLog.out.println(
"Cleanroom: dxMin = " + dxMin + ", dxMax = " + dxMax + ", dzMin = " + dzMin + ", dzMax = " + dzMax + ".");
return true;
}
/**
* Checks whether the ceiling layer of the cleanroom is complete. Assumes that
* {@link #checkSize(IGregTechTileEntity)} has already been run.
*
* @return True on success, false on failure.
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
protected boolean checkCeiling(IGregTechTileEntity aBaseMetaTileEntity) {
// Edges must be plascrete, everything else must be filters (except for the controller).
for (int dx = dxMin; dx <= dxMax; ++dx) {
for (int dz = dzMin; dz <= dzMax; ++dz) {
if (dx == 0 && dz == 0) {
// Controller.
continue;
} else if (dx == dxMin || dx == dxMax || dz == dzMin || dz == dzMax) {
// Edge.
if (!addStructureBlock(aBaseMetaTileEntity, dx, 0, dz, MASK_CEILING_EDGE)) return false;
} else {
// Internal block.
if (!addStructureBlock(aBaseMetaTileEntity, dx, 0, dz, MASK_CEILING_INTERNAL)) return false;
}
}
}
return true;
}
/**
* Checks the floor of the cleanroom. Note that if this fails, it is not necessarily because the structure is
* invalid, maybe the floor just isn't where we thought it was, and we're looking at a wall.
*
* @param dy Vertical offset of the floor from the controller.
* @return True on success, false on failure.
*/
protected boolean checkFloor(IGregTechTileEntity aBaseMetaTileEntity, int dy) {
// Save maintenance and energy hatches, if the check fails, we don't want to add them.
// We always add all hatches, even if we find more than one. This allows for better error reporting: if there
// are two energy hatches in the floor layer, we add both, and report the floor as complete. This way, the
// structure check fails due to multiple hatches, and not due to missing floor.
int addedCasings = 0;
int addedOther = 0;
ArrayList<IGregTechTileEntity> energy = new ArrayList<>();
ArrayList<IGregTechTileEntity> maintenance = new ArrayList<>();
for (int dx = dxMin + 1; dx <= dxMax - 1; ++dx) {
for (int dz = dzMin + 1; dz <= dzMax - 1; ++dz) {
switch (getBlockType(aBaseMetaTileEntity, dx, dy, dz, MASK_FLOOR_INTERNAL)) {
case CASING:
++addedCasings;
break;
case GLASS:
case OTHER:
case HATCH_DIODE:
case FILTER:
case DOOR: // Filters and doors should not be valid in the floor, but are included for completeness.
++addedOther;
break;
case HATCH_ENERGY:
energy.add(aBaseMetaTileEntity.getIGregTechTileEntityOffset(dx, dy, dz));
++addedOther;
break;
case HATCH_MAINTENANCE:
maintenance.add(aBaseMetaTileEntity.getIGregTechTileEntityOffset(dx, dy, dz));
++addedOther;
break;
case INVALID:
// Do not log an error, we might not be at the correct floor level yet.
return false;
default:
throw new IllegalArgumentException(
"Cleanroom error: unknown block type at at offset (" + dx + ", " + dy + ", " + dz + ").");
}
}
}
// If we get here, the entire floor is valid. Add hatches to the machine.
casingCount += addedCasings;
otherCount += addedOther;
for (var te : energy) addEnergyInputToMachineList(te, CASING_INDEX);
for (var te : maintenance) addMaintenanceToMachineList(te, CASING_INDEX);
return true;
}
/**
* Checks the walls of the cleanroom at a specified vertical offset.
*
* @param dy Vertical offset of the floor from the controller.
* @return True on success, false on failure.
*/
protected boolean checkWall(IGregTechTileEntity aBaseMetaTileEntity, int dy) {
for (int dx = dxMin + 1; dx <= dxMax - 1; ++dx) {
if (!addStructureBlock(aBaseMetaTileEntity, dx, dy, dzMin, MASK_WALL_INTERNAL)) return false;
if (!addStructureBlock(aBaseMetaTileEntity, dx, dy, dzMax, MASK_WALL_INTERNAL)) return false;
}
for (int dz = dzMin + 1; dz <= dzMax - 1; ++dz) {
if (!addStructureBlock(aBaseMetaTileEntity, dxMin, dy, dz, MASK_WALL_INTERNAL)) return false;
if (!addStructureBlock(aBaseMetaTileEntity, dxMax, dy, dz, MASK_WALL_INTERNAL)) return false;
}
if (!addStructureBlock(aBaseMetaTileEntity, dxMin, dy, dzMin, MASK_WALL_EDGE)) return false;
if (!addStructureBlock(aBaseMetaTileEntity, dxMin, dy, dzMax, MASK_WALL_EDGE)) return false;
if (!addStructureBlock(aBaseMetaTileEntity, dxMax, dy, dzMin, MASK_WALL_EDGE)) return false;
if (!addStructureBlock(aBaseMetaTileEntity, dxMax, dy, dzMax, MASK_WALL_EDGE)) return false;
return true;
}
@Override
public boolean checkMachine(IGregTechTileEntity aBaseMetaTileEntity, ItemStack aStack) {
mUpdate = 100;
cleanroomReceivers.forEach(r -> r.setCleanroom(null));
cleanroomReceivers.clear();
casingCount = 0;
otherCount = 0;
isDoorOpen = false;
if (debugCleanroom) GTLog.out.println("Cleanroom: Starting structure check.");
// Optimization: a vast majority of the time, the size of the CR won't change. Try checking it using the old
// size, and only if that fails, try to find a new size.
if (dyMin == 0 || !checkCeiling(aBaseMetaTileEntity)) {
if (!checkSize(aBaseMetaTileEntity)) return false;
if (!checkCeiling(aBaseMetaTileEntity)) return false;
}
// Check downward until we find a valid floor.
// We check specifically internal blocks for a valid floor. This means that in most cases this check
// immediately falls through, as the first block we check is already invalid (e.g., air or machine).
for (dyMin = -1; dyMin >= -(MAX_HEIGHT - 1); --dyMin) {
if (dyMin < -2 && checkFloor(aBaseMetaTileEntity, dyMin)) {
// Found a valid floor. Add its edges and finish.
for (int dx = dxMin; dx <= dxMax; ++dx) {
if (!addStructureBlock(aBaseMetaTileEntity, dx, dyMin, dzMin, MASK_FLOOR_EDGE)) return false;
if (!addStructureBlock(aBaseMetaTileEntity, dx, dyMin, dzMax, MASK_FLOOR_EDGE)) return false;
}
for (int dz = dzMin + 1; dz <= dzMax - 1; ++dz) {
if (!addStructureBlock(aBaseMetaTileEntity, dxMin, dyMin, dz, MASK_FLOOR_EDGE)) return false;
if (!addStructureBlock(aBaseMetaTileEntity, dxMax, dyMin, dz, MASK_FLOOR_EDGE)) return false;
}
break;
} else {
// Not floor yet, check for a wall.
if (!checkWall(aBaseMetaTileEntity, dyMin)) return false;
}
}
if (dyMin < -(MAX_HEIGHT - 1)) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Too tall.");
return false;
}
mHeight = -dyMin + 1;
if (debugCleanroom) GTLog.out.println(
"Cleanroom: Structure complete. Found " + casingCount + " casings, " + otherCount + " other blocks.");
// Validate structure.
if (this.mMaintenanceHatches.size() != 1 || this.mEnergyHatches.size() != 1) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Incorrect number of hatches.");
return false;
}
if (casingCount < MachineStats.cleanroom.minCasingCount) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Not enough plascrete blocks.");
return false;
}
if ((otherCount * 100) / (casingCount + otherCount) > MachineStats.cleanroom.maxReplacementPercentage) {
if (debugCleanroom) GTLog.out.println("Cleanroom: Too many non-plascrete blocks.");
return false;
}
if (isDoorOpen) {
this.mEfficiency = Math.max(0, this.mEfficiency - 200);
}
for (final ForgeDirection tSide : ForgeDirection.VALID_DIRECTIONS) {
final byte t = (byte) Math.max(1, (byte) (15 / (10000f / this.mEfficiency)));
aBaseMetaTileEntity.setInternalOutputRedstoneSignal(tSide, t);
}
// Re-add machines inside the cleanroom.
for (int dy = dyMin + 1; dy < 0; ++dy) {
for (int dx = dxMin + 1; dx <= dxMax - 1; ++dx) {
for (int dz = dzMin + 1; dz <= dzMax - 1; dz++) {
TileEntity te = aBaseMetaTileEntity.getTileEntityOffset(dx, dy, dz);
if (te instanceof ICleanroomReceiver receiver) {
receiver.setCleanroom(this);
cleanroomReceivers.add(receiver);
}
}
}
}
if (debugCleanroom) GTLog.out.println("Cleanroom: Check successful.");
return true;
}
@Override
public boolean allowGeneralRedstoneOutput() {
return true;
}
@Override
public ITexture[] getTexture(IGregTechTileEntity baseMetaTileEntity, ForgeDirection sideDirection,
ForgeDirection facingDirection, int colorIndex, boolean active, boolean redstoneLevel) {
if ((sideDirection.flag & (ForgeDirection.UP.flag | ForgeDirection.DOWN.flag)) != 0) {
return new ITexture[] { TextureFactory.of(BLOCK_PLASCRETE), active
? TextureFactory.of(
TextureFactory.of(OVERLAY_TOP_CLEANROOM_ACTIVE),
TextureFactory.builder()
.addIcon(OVERLAY_TOP_CLEANROOM_ACTIVE_GLOW)
.glow()
.build())
: TextureFactory.of(
TextureFactory.of(OVERLAY_TOP_CLEANROOM),
TextureFactory.builder()
.addIcon(OVERLAY_TOP_CLEANROOM_GLOW)
.glow()
.build()) };
}
return new ITexture[] { TextureFactory.of(BLOCK_PLASCRETE) };
}
@Override
public boolean isCorrectMachinePart(ItemStack aStack) {
return true;
}
@Override
public int getMaxEfficiency(ItemStack aStack) {
return 10000;
}
@Override
public int getDamageToComponent(ItemStack aStack) {
return 0;
}
@Override
public boolean explodesOnComponentBreak(ItemStack aStack) {
return false;
}
@Override
public void construct(ItemStack stackSize, boolean hintsOnly) {
int i = Math.min(stackSize.stackSize, 7);
IGregTechTileEntity baseEntity = this.getBaseMetaTileEntity();
World world = baseEntity.getWorld();
int x = baseEntity.getXCoord();
int y = baseEntity.getYCoord();
int z = baseEntity.getZCoord();
int yoff = Math.max(i * 2, 3);
for (int X = x - i; X <= x + i; X++) for (int Y = y; Y >= y - yoff; Y--) for (int Z = z - i; Z <= z + i; Z++) {
if (X == x && Y == y && Z == z) continue;
if (X == x - i || X == x + i || Z == z - i || Z == z + i || Y == y - yoff) {
if (hintsOnly) StructureLibAPI.hintParticle(world, X, Y, Z, CASING_BLOCK, CASING_META);
else world.setBlock(X, Y, Z, CASING_BLOCK, CASING_META, 2);
} else if (Y == y) {
if (hintsOnly) StructureLibAPI.hintParticle(world, X, Y, Z, FILTER_BLOCK, FILTER_META);
else world.setBlock(X, Y, Z, FILTER_BLOCK, FILTER_META, 2);
}
}
}
@Override
public void onConfigLoad() {
ALLOWED_BLOCKS.clear();
Collections.addAll(ALLOWED_BLOCKS, MachineStats.cleanroom.allowedBlocks);
CASING_BLOCK = GregTechAPI.sBlockReinforced;
FILTER_BLOCK = GregTechAPI.sBlockCasings3;
}
/**
* Doors are funny. So the meta value of the bottom part of the door determines where in the block the door is, when
* in the "closed" (inactive) position.
* 0 = lower x coordinate (west).
* 1 = lower z coordinate (north).
* 2 = upper x coordinate (east).
* 3 = upper z coordinate (south).
* If the door is opened, a 4 is added to this value.
* <p>
* The meta of the top part of the door determines which way the door opens.
* 8 = opens counterclockwise.
* 9 = opens clockwise.
* <p>
* Therefore, to find out where in the block the door currently is, we need to know both the top and the
* bottom part, as a door that is "closed" on the north side can "open" to either the west or east side.
* In both cases the meta of the bottom part will be the same (5).
* <p>
* This method takes the coordinates of a door block (it is already assumed that this is a door), and returns the
* direction where the door is. Return value is the same as a default closed door: 0 = west, 1 = north, 2 = east, 3
* = north.
*/
protected int getDoorOrientation(IGregTechTileEntity aBaseMetaTileEntity, int dx, int dy, int dz) {
int meta = aBaseMetaTileEntity.getMetaIDOffset(dx, dy, dz);
if (meta < 4) {
// Closed door, easy.
return meta;
} else if (meta < 8) {
// Bottom part of an open door.
if (aBaseMetaTileEntity.getBlockOffset(dx, dy + 1, dz) instanceof ic2.core.block.BlockIC2Door) {
return getDoorOrientation(meta, aBaseMetaTileEntity.getMetaIDOffset(dx, dy + 1, dz));
} else {
// Bottom part of a door without the top part? Cheater!
return -1;
}
} else if (meta < 10) {
// Top part of a door.
if (aBaseMetaTileEntity.getBlockOffset(dx, dy - 1, dz) instanceof ic2.core.block.BlockIC2Door) {
return getDoorOrientation(aBaseMetaTileEntity.getMetaIDOffset(dx, dy - 1, dz), meta);
} else {
// Top part of a door without the bottom part? Cheater!
return -1;
}
} else {
// Invalid meta value?
return -1;
}
}
protected int getDoorOrientation(int bottomMeta, int topMeta) {
if (bottomMeta < 4) {
// Closed door, easy.
return bottomMeta;
} else if (bottomMeta < 8) {
// Open door.
if (topMeta == 8) {
// Opens CCW, add one.
return (bottomMeta + 1) % 4;
} else if (topMeta == 9) {
// Opens CW, subtract one.
return (bottomMeta - 1) % 4;
}
}
// Invalid combination?
return -1;
}
}
|