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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
package common.tileentities;
import com.github.bartimaeusnek.bartworks.API.BorosilicateGlass;
import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_DynamoMulti;
import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_DynamoTunnel;
import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_EnergyMulti;
import com.github.technus.tectech.thing.metaTileEntity.hatch.GT_MetaTileEntity_Hatch_EnergyTunnel;
import com.google.common.collect.ImmutableList;
import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.google.common.collect.ImmutableList;
import com.gtnewhorizon.structurelib.StructureLibAPI;
import com.gtnewhorizon.structurelib.alignment.IAlignmentLimits;
import com.gtnewhorizon.structurelib.alignment.constructable.ChannelDataAccessor;
import com.gtnewhorizon.structurelib.alignment.constructable.ConstructableUtility;
import com.gtnewhorizon.structurelib.alignment.constructable.ISurvivalConstructable;
import com.gtnewhorizon.structurelib.structure.IItemSource;
import com.gtnewhorizon.structurelib.alignment.constructable.ISurvivalConstructable;
import com.gtnewhorizon.structurelib.structure.IStructureDefinition;
import com.gtnewhorizon.structurelib.structure.IStructureElement;
import com.gtnewhorizon.structurelib.structure.IStructureElement.PlaceResult;
import com.gtnewhorizon.structurelib.structure.StructureUtility;
import com.gtnewhorizon.structurelib.util.ItemStackPredicate.NBTMode;
import common.Blocks;
import gregtech.api.enums.Dyes;
import gregtech.api.enums.GT_Values;
import gregtech.api.enums.Textures.BlockIcons;
import gregtech.api.gui.GT_GUIContainer_MultiMachine;
import gregtech.api.interfaces.IGlobalWirelessEnergy;
import gregtech.api.interfaces.IHatchElement;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.*;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GT_Multiblock_Tooltip_Builder;
import gregtech.api.util.GT_Utility;
import gregtech.api.util.IGT_HatchAdder;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.util.*;
import java.util.function.Consumer;
import static com.google.common.math.LongMath.pow;
import static com.gtnewhorizon.structurelib.structure.StructureUtility.*;
import static common.itemBlocks.IB_LapotronicEnergyUnit.*;
import static gregtech.api.enums.GT_HatchElement.Maintenance;
import static gregtech.api.util.GT_StructureUtility.buildHatchAdder;
import static gregtech.api.util.GT_StructureUtility.filterByMTEClass;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class GTMTE_LapotronicSuperCapacitor extends GT_MetaTileEntity_EnhancedMultiBlockBase<GTMTE_LapotronicSuperCapacitor> implements IGlobalWirelessEnergy, ISurvivalConstructable {
private enum TopState {
MayBeTop,
Top,
NotTop
}
private boolean wireless_mode = false;
private boolean not_processed_lsc = true;
private int counter = 1;
private final Queue<Long> energyInputValues = new LinkedList<>();
private final Queue<Long> energyOutputValues = new LinkedList<>();
private long max_passive_drain_eu_per_tick_per_uhv_cap = 1_000_000;
private enum Capacitor {
IV(2, BigInteger.valueOf(IV_cap_storage)),
LuV(3, BigInteger.valueOf(LuV_cap_storage)),
ZPM(4, BigInteger.valueOf(ZPM_cap_storage)),
UV(5, BigInteger.valueOf(UV_cap_storage)),
UHV(6, MAX_LONG),
None(0, BigInteger.ZERO),
EV(1, BigInteger.valueOf(EV_cap_storage));
private final int minimalGlassTier;
private final BigInteger providedCapacity;
static final Capacitor[] VALUES = values();
static final Capacitor[] VALUES_BY_TIER = Arrays.stream(values()).sorted(Comparator.comparingInt(Capacitor::getMinimalGlassTier)).toArray(Capacitor[]::new);
Capacitor(int minimalGlassTier, BigInteger providedCapacity) {
this.minimalGlassTier = minimalGlassTier;
this.providedCapacity = providedCapacity;
}
public int getMinimalGlassTier() {
return minimalGlassTier;
}
public BigInteger getProvidedCapacity() {
return providedCapacity;
}
public static int getIndexFromGlassTier(int glassTier) {
for (int index = 0; index < values().length; index++) {
if (values()[index].getMinimalGlassTier() == glassTier) {
return index;
}
}
return -1;
}
}
private static final String STRUCTURE_PIECE_BASE = "base";
private static final String STRUCTURE_PIECE_LAYER = "slice";
private static final String STRUCTURE_PIECE_TOP = "top";
private static final String STRUCTURE_PIECE_MID = "mid";
private static final Block LSC_PART = Blocks.lscLapotronicEnergyUnit;
private static final Item LSC_PART_ITEM = Item.getItemFromBlock(LSC_PART);
private static final int CASING_META = 0;
private static final int CASING_TEXTURE_ID = (42 << 7) | 127;
private static final int DURATION_AVERAGE_TICKS = 100;
// height channel for height.
// glass channel for glass
// capacitor channel for capacitor, but it really just pick whatever capacitor it can find in survival
private static final IStructureDefinition<GTMTE_LapotronicSuperCapacitor> STRUCTURE_DEFINITION = IStructureDefinition.<GTMTE_LapotronicSuperCapacitor>builder()
.addShape(STRUCTURE_PIECE_BASE, transpose(new String[][]{
{"bbbbb", "bbbbb", "bbbbb", "bbbbb", "bbbbb",},
{"bb~bb", "bbbbb", "bbbbb", "bbbbb", "bbbbb",},
}))
.addShape(STRUCTURE_PIECE_LAYER, transpose(new String[][]{
{"ggggg", "gcccg", "gcccg", "gcccg", "ggggg",},
}))
.addShape(STRUCTURE_PIECE_TOP, transpose(new String[][]{
{"ggggg", "ggggg", "ggggg", "ggggg", "ggggg",},
}))
.addShape(STRUCTURE_PIECE_MID, transpose(new String[][]{
{"ggggg", "gCCCg", "gCCCg", "gCCCg", "ggggg",},
}))
.addElement('b', buildHatchAdder(GTMTE_LapotronicSuperCapacitor.class)
.atLeast(LSCHatchElement.Energy, LSCHatchElement.Dynamo, Maintenance)
.hatchItemFilterAnd((t, h) -> ChannelDataAccessor.getChannelData(h, "glass") < 6 ?
filterByMTEClass(ImmutableList.of(GT_MetaTileEntity_Hatch_EnergyTunnel.class, GT_MetaTileEntity_Hatch_DynamoTunnel.class)).negate() :
s -> true)
.casingIndex(CASING_TEXTURE_ID)
.dot(1)
.buildAndChain(onElementPass(te -> te.casingAmount++, ofBlock(LSC_PART, CASING_META)))
)
.addElement('g', withChannel("glass", BorosilicateGlass.ofBoroGlass((byte) -1, (te, t) -> te.glassTier = t, te -> te.glassTier)))
.addElement('c', ofChain(
onlyIf(te -> te.topState != TopState.NotTop, onElementPass(te -> te.topState = TopState.Top, withChannel("glass", BorosilicateGlass.ofBoroGlass((byte) -1, (te, t) -> te.glassTier = t, te -> te.glassTier)))),
onlyIf(te -> te.topState != TopState.Top, onElementPass(te -> te.topState = TopState.NotTop,
new IStructureElement<GTMTE_LapotronicSuperCapacitor>() {
@Override
public boolean check(GTMTE_LapotronicSuperCapacitor t, World world, int x, int y, int z) {
Block worldBlock = world.getBlock(x, y, z);
int meta = worldBlock.getDamageValue(world, x, y, z);
if (LSC_PART != worldBlock || meta == 0)
return false;
t.capacitors[meta - 1]++;
return true;
}
private int getHint(ItemStack stack) {
return Capacitor.VALUES_BY_TIER[Math.min(Capacitor.VALUES_BY_TIER.length, ChannelDataAccessor.getChannelData(stack, "capacitor")) - 1].getMinimalGlassTier() + 1;
}
@Override
public boolean spawnHint(GTMTE_LapotronicSuperCapacitor t, World world, int x, int y, int z, ItemStack trigger) {
StructureLibAPI.hintParticle(world, x, y, z, LSC_PART, getHint(trigger));
return true;
}
@Override
public boolean placeBlock(GTMTE_LapotronicSuperCapacitor t, World world, int x, int y, int z, ItemStack trigger) {
world.setBlock(x, y, z, LSC_PART, getHint(trigger), 3);
return true;
}
@Override
public PlaceResult survivalPlaceBlock(GTMTE_LapotronicSuperCapacitor t, World world, int x, int y, int z, ItemStack trigger, IItemSource source, EntityPlayerMP actor, Consumer<IChatComponent> chatter) {
if (check(t, world, x, y, z)) return PlaceResult.SKIP;
int glassTier = ChannelDataAccessor.getChannelData(trigger, "glass") + 2;
ItemStack targetStack = source.takeOne(s -> s != null && s.stackSize >= 0 && s.getItem() == LSC_PART_ITEM && Capacitor.VALUES[Math.min(s.getItemDamage(), Capacitor.VALUES.length) - 1].getMinimalGlassTier() > glassTier, true);
if (targetStack == null) return PlaceResult.REJECT;
return StructureUtility.survivalPlaceBlock(targetStack, NBTMode.EXACT, targetStack.stackTagCompound, true, world, x, y, z, source, actor, chatter);
}
}
))
))
.addElement('C', ofBlock(LSC_PART, 1))
.build();
private static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
private final Set<GT_MetaTileEntity_Hatch_EnergyMulti> mEnergyHatchesTT = new HashSet<>();
private final Set<GT_MetaTileEntity_Hatch_DynamoMulti> mDynamoHatchesTT = new HashSet<>();
private final Set<GT_MetaTileEntity_Hatch_EnergyTunnel> mEnergyTunnelsTT = new HashSet<>();
private final Set<GT_MetaTileEntity_Hatch_DynamoTunnel> mDynamoTunnelsTT = new HashSet<>();
/**
* Count the amount of capacitors of each tier in each slot.
* Index = meta - 1
*/
private final int[] capacitors = new int[7];
private BigInteger capacity = BigInteger.ZERO;
private BigInteger stored = BigInteger.ZERO;
private long passiveDischargeAmount = 0;
private long inputLastTick = 0;
private long outputLastTick = 0;
private int repairStatusCache = 0;
private byte glassTier = -1;
private int casingAmount = 0;
private TopState topState = TopState.MayBeTop;
private long mMaxEUIn = 0;
private long mMaxEUOut = 0;
public GTMTE_LapotronicSuperCapacitor(int aID, String aName, String aNameRegional) {
super(aID, aName, aNameRegional);
}
public GTMTE_LapotronicSuperCapacitor(String aName) {
super(aName);
}
@Override
public IMetaTileEntity newMetaEntity(IGregTechTileEntity var1) {
return new GTMTE_LapotronicSuperCapacitor(super.mName);
}
@Override
public IStructureDefinition<GTMTE_LapotronicSuperCapacitor> getStructureDefinition() {
return STRUCTURE_DEFINITION;
}
@Override
protected IAlignmentLimits getInitialAlignmentLimits() {
return (d, r, f) -> d.offsetY == 0 && r.isNotRotated() && !f.isVerticallyFliped();
}
private void processInputHatch(GT_MetaTileEntity_Hatch aHatch, int aBaseCasingIndex) {
mMaxEUIn += aHatch.maxEUInput() * aHatch.maxAmperesIn();
aHatch.updateTexture(aBaseCasingIndex);
}
private void processOutputHatch(GT_MetaTileEntity_Hatch aHatch, int aBaseCasingIndex) {
mMaxEUOut += aHatch.maxEUOutput() * aHatch.maxAmperesOut();
aHatch.updateTexture(aBaseCasingIndex);
}
private boolean addBottomHatches(IGregTechTileEntity aTileEntity, int aBaseCasingIndex) {
if (aTileEntity == null || aTileEntity.isDead()) return false;
IMetaTileEntity aMetaTileEntity = aTileEntity.getMetaTileEntity();
if (!(aMetaTileEntity instanceof GT_MetaTileEntity_Hatch)) return false;
if (aMetaTileEntity instanceof GT_MetaTileEntity_Hatch_Maintenance) {
((GT_MetaTileEntity_Hatch) aMetaTileEntity).updateTexture(aBaseCasingIndex);
return GTMTE_LapotronicSuperCapacitor.this.mMaintenanceHatches.add((GT_MetaTileEntity_Hatch_Maintenance) aMetaTileEntity);
} else if (aMetaTileEntity instanceof GT_MetaTileEntity_Hatch_Energy) {
// Add GT hatches
final GT_MetaTileEntity_Hatch_Energy tHatch = ((GT_MetaTileEntity_Hatch_Energy) aMetaTileEntity);
processInputHatch(tHatch, aBaseCasingIndex);
return mEnergyHatches.add(tHatch);
} else if (aMetaTileEntity instanceof GT_MetaTileEntity_Hatch_EnergyTunnel) {
// Add TT Laser hatches
final GT_MetaTileEntity_Hatch_EnergyTunnel tHatch = ((GT_MetaTileEntity_Hatch_EnergyTunnel) aMetaTileEntity);
processInputHatch(tHatch, aBaseCasingIndex);
return mEnergyTunnelsTT.add(tHatch);
} else if (aMetaTileEntity instanceof GT_MetaTileEntity_Hatch_EnergyMulti) {
// Add TT hatches
final GT_MetaTileEntity_Hatch_EnergyMulti tHatch = (GT_MetaTileEntity_Hatch_EnergyMulti) aMetaTileEntity;
processInputHatch(tHatch, aBaseCasingIndex);
return mEnergyHatchesTT.add(tHatch);
} else if (aMetaTileEntity instanceof GT_MetaTileEntity_Hatch_Dynamo) {
// Add GT hatches
final GT_MetaTileEntity_Hatch_Dynamo tDynamo = (GT_MetaTileEntity_Hatch_Dynamo) aMetaTileEntity;
processOutputHatch(tDynamo, aBaseCasingIndex);
return mDynamoHatches.add(tDynamo);
} else if (aMetaTileEntity instanceof GT_MetaTileEntity_Hatch_DynamoTunnel) {
// Add TT Laser hatches
final GT_MetaTileEntity_Hatch_DynamoTunnel tDynamo = (GT_MetaTileEntity_Hatch_DynamoTunnel) aMetaTileEntity;
processOutputHatch(tDynamo, aBaseCasingIndex);
return mDynamoTunnelsTT.add(tDynamo);
} else if (aMetaTileEntity instanceof GT_MetaTileEntity_Hatch_DynamoMulti) {
// Add TT hatches
final GT_MetaTileEntity_Hatch_DynamoMulti tDynamo = (GT_MetaTileEntity_Hatch_DynamoMulti) aMetaTileEntity;
processOutputHatch(tDynamo, aBaseCasingIndex);
return mDynamoHatchesTT.add(tDynamo);
}
return false;
}
private int getUHVCapacitorCount() {
return capacitors[4];
}
@Override
protected GT_Multiblock_Tooltip_Builder createTooltip() {
final GT_Multiblock_Tooltip_Builder tt = new GT_Multiblock_Tooltip_Builder();
tt.addMachineType("Energy Storage")
.addInfo("Loses energy equal to 1% of the total capacity every 24 hours. Capped")
.addInfo("at " + EnumChatFormatting.RED + GT_Utility.formatNumbers(max_passive_drain_eu_per_tick_per_uhv_cap) + EnumChatFormatting.GRAY + "EU/t passive loss per " + GT_Values.TIER_COLORS[9] + GT_Values.VN[9] + EnumChatFormatting.GRAY + " capacitor.")
.addInfo("Passive loss is multiplied by the number of maintenance issues present.")
.addSeparator()
.addInfo("Glass shell has to be Tier - 3 of the highest capacitor tier.")
.addInfo(GT_Values.TIER_COLORS[8] + GT_Values.VN[8] + EnumChatFormatting.GRAY + "-tier glass required for " + EnumChatFormatting.BLUE + "Tec" + EnumChatFormatting.DARK_BLUE + "Tech" + EnumChatFormatting.GRAY + " Laser Hatches.")
.addInfo("Add more or better capacitors to increase capacity.")
.addSeparator()
.addInfo("Wireless mode can be enabled by right clicking with a screwdriver.")
.addInfo("This mode can only be enabled if you have a "+ GT_Values.TIER_COLORS[9] + GT_Values.VN[9] + EnumChatFormatting.GRAY + " capacitor in the multiblock.")
.addInfo("When enabled every " + EnumChatFormatting.BLUE + GT_Utility.formatNumbers(LSC_time_between_wireless_rebalance_in_ticks) + EnumChatFormatting.GRAY + " ticks the LSC will attempt to re-balance against your")
.addInfo("wireless EU network. If there is less than " + EnumChatFormatting.RED + GT_Utility.formatNumbers(LSC_wireless_eu_cap) + EnumChatFormatting.GRAY + "EU in the LSC")
.addInfo("it will withdraw from the network and add to the LSC. If there is more it will add")
.addInfo("the EU to the network and remove it from the LSC.")
.addSeparator()
.beginVariableStructureBlock(5, 5, 4, 18, 5, 5, false)
.addStructureInfo("Modular height of 4-18 blocks.")
.addController("Front center bottom")
.addOtherStructurePart("Lapotronic Super Capacitor Casing", "5x2x5 base (at least 17x)")
.addOtherStructurePart("Lapotronic Capacitor (" + GT_Values.TIER_COLORS[4] + GT_Values.VN[4] + EnumChatFormatting.GRAY + "-" + GT_Values.TIER_COLORS[8] + GT_Values.VN[8] + EnumChatFormatting.GRAY + "), Ultimate Capacitor (" + GT_Values.TIER_COLORS[9] + GT_Values.VN[9] + EnumChatFormatting.GRAY + ")", "Center 3x(1-15)x3 above base (9-135 blocks)")
.addStructureInfo("You can also use the Empty Capacitor to save materials if you use it for less than half the blocks")
.addOtherStructurePart("Borosilicate Glass (any)", "41-265x, Encase capacitor pillar")
.addEnergyHatch("Any casing")
.addDynamoHatch("Any casing")
.addOtherStructurePart("Laser Target/Source Hatches", "Any casing, must be using " + GT_Values.TIER_COLORS[8] + GT_Values.VN[8] + EnumChatFormatting.GRAY + "-tier glass")
.addStructureInfo("You can have several I/O Hatches")
.addSubChannelUsage("glass", "Borosilicate Glass Tier")
.addSubChannelUsage("capacitor", "Maximum Capacitor Tier")
.addSubChannelUsage("height", "Height of structure")
.addMaintenanceHatch("Any casing")
.toolTipFinisher("KekzTech");
return tt;
}
@Override
public ITexture[] getTexture(IGregTechTileEntity aBaseMetaTileEntity, byte aSide, byte aFacing, byte aColorIndex,
boolean aActive, boolean aRedstone) {
ITexture[] sTexture = new ITexture[]{TextureFactory.of(BlockIcons.MACHINE_CASING_FUSION_GLASS,
Dyes.getModulation(-1, Dyes._NULL.mRGBa))};
if (aSide == aFacing && aActive) {
sTexture = new ITexture[]{TextureFactory.of(BlockIcons.MACHINE_CASING_FUSION_GLASS_YELLOW,
Dyes.getModulation(-1, Dyes._NULL.mRGBa))};
}
return sTexture;
}
public Object getClientGUI(int aID, InventoryPlayer aPlayerInventory, IGregTechTileEntity aBaseMetaTileEntity) {
return new GT_GUIContainer_MultiMachine(aPlayerInventory, aBaseMetaTileEntity, this.getLocalName(),
"MultiblockDisplay.png");
}
private String global_energy_user_uuid;
@Override
public void onPreTick(IGregTechTileEntity tileEntity, long aTick) {
super.onPreTick(tileEntity, aTick);
// On first tick (aTick restarts from 0 upon world reload).
if (not_processed_lsc && tileEntity.isServerSide()) {
// Add user to wireless network.
strongCheckOrAddUser(tileEntity.getOwnerUuid(), tileEntity.getOwnerName());
// Get team UUID.
global_energy_user_uuid = getUUIDFromUsername(tileEntity.getOwnerName());
not_processed_lsc = false;
}
}
@Override
public boolean isCorrectMachinePart(ItemStack stack) {
return true;
}
@Override
public boolean checkRecipe(ItemStack stack) {
this.mProgresstime = 1;
this.mMaxProgresstime = 1;
this.mEUt = 0;
this.mEfficiencyIncrease = 10000;
return true;
}
@Override
public boolean checkMachine(IGregTechTileEntity thisController, ItemStack guiSlotItem) {
strongCheckOrAddUser(thisController.getOwnerUuid(), thisController.getOwnerName());
// Reset capacitor counts
Arrays.fill(capacitors, 0);
// Clear TT hatches
mEnergyHatchesTT.clear();
mDynamoHatchesTT.clear();
mEnergyTunnelsTT.clear();
mDynamoTunnelsTT.clear();
mMaxEUIn = 0;
mMaxEUOut = 0;
if (!checkPiece(STRUCTURE_PIECE_BASE, 2, 1, 0))
return false;
topState = TopState.NotTop; // need at least one layer of capacitor to form, obviously
int layer = 2;
while (true) {
if (!checkPiece(STRUCTURE_PIECE_LAYER, 2, layer, 0))
return false;
layer ++;
if (topState == TopState.Top)
break; // top found, break out
topState = TopState.MayBeTop;
if (layer > 18)
return false; // too many layers
}
// Make sure glass tier is T-2 of the highest tier capacitor in the structure
// Count down from the highest tier until an entry is found
// Borosilicate glass after 5 are just recolours of 0
for (int highestGlassTier = capacitors.length - 1; highestGlassTier >= 0; highestGlassTier--) {
int highestCapacitor = Capacitor.getIndexFromGlassTier(highestGlassTier);
if (capacitors[highestCapacitor] > 0) {
if (Capacitor.VALUES[highestCapacitor].getMinimalGlassTier() > glassTier)
return false;
break;
}
}
// Glass has to be at least UV-tier to allow TT Laser hatches
if (glassTier < 8) {
if(mEnergyTunnelsTT.size() > 0 || mDynamoTunnelsTT.size() > 0)
return false;
}
//Check if enough (more than 50%) non-empty caps
if (capacitors[5] > capacitors[0] + capacitors[1] + capacitors[2] + capacitors[3] + getUHVCapacitorCount() + capacitors[6])
return false;
// Calculate total capacity
capacity = BigInteger.ZERO;
for(int i = 0; i < capacitors.length; i++) {
int count = capacitors[i];
capacity = capacity.add(Capacitor.VALUES[i].getProvidedCapacity().multiply(BigInteger.valueOf(count)));
}
// Calculate how much energy to void each tick
passiveDischargeAmount = recalculateLossWithMaintenance(getRepairStatus());
return mMaintenanceHatches.size() == 1;
}
@Override
public void construct(ItemStack stackSize, boolean hintsOnly) {
int layer = min(stackSize.stackSize + 3, 18);
buildPiece(STRUCTURE_PIECE_BASE, stackSize, hintsOnly, 2, 1, 0);
for (int i = 2; i < layer - 1; i++)
buildPiece(STRUCTURE_PIECE_MID, stackSize, hintsOnly, 2, i, 0);
buildPiece(STRUCTURE_PIECE_TOP, stackSize, hintsOnly, 2, layer - 1, 0);
}
@Override
public int survivalConstruct(ItemStack stackSize, int elementBudget, IItemSource source, EntityPlayerMP actor) {
if (mMachine) return -1;
int layer = Math.min(ChannelDataAccessor.getChannelData(stackSize, "height") + 3, 18);
int built;
built = survivialBuildPiece(STRUCTURE_PIECE_BASE, stackSize, 2, 1, 0, elementBudget, source, actor, false, true);
if (built >= 0) return built;
for (int i = 2; i < layer - 1; i++)
built = survivialBuildPiece(STRUCTURE_PIECE_MID, stackSize, 2, i, 0, elementBudget, source, actor, false, true);
if (built >= 0) return built;
return survivialBuildPiece(STRUCTURE_PIECE_TOP, stackSize, 2, layer - 1, 0, elementBudget, source, actor, false, true);
}
@Override
public boolean onRunningTick(ItemStack stack){
// Reset I/O cache
inputLastTick = 0;
outputLastTick = 0;
long temp_stored = 0L;
// Draw energy from GT hatches
for(GT_MetaTileEntity_Hatch_Energy eHatch : super.mEnergyHatches) {
if(eHatch == null || eHatch.getBaseMetaTileEntity().isInvalidTileEntity()) {
continue;
}
final long power = getPowerToDraw(eHatch.maxEUInput() * eHatch.maxAmperesIn());
if(eHatch.getEUVar() >= power) {
eHatch.setEUVar(eHatch.getEUVar() - power);
temp_stored += power;
inputLastTick += power;
}
}
// Output energy to GT hatches
for(GT_MetaTileEntity_Hatch_Dynamo eDynamo : super.mDynamoHatches){
if(eDynamo == null || eDynamo.getBaseMetaTileEntity().isInvalidTileEntity()){
continue;
}
final long power = getPowerToPush(eDynamo.maxEUOutput() * eDynamo.maxAmperesOut());
if(power <= eDynamo.maxEUStore() - eDynamo.getEUVar()) {
eDynamo.setEUVar(eDynamo.getEUVar() + power);
temp_stored -= power;
outputLastTick += power;
}
}
// Draw energy from TT hatches
for(GT_MetaTileEntity_Hatch_EnergyMulti eHatch : mEnergyHatchesTT) {
if(eHatch == null || eHatch.getBaseMetaTileEntity().isInvalidTileEntity()) {
continue;
}
final long power = getPowerToDraw(eHatch.maxEUInput() * eHatch.maxAmperesIn());
if(eHatch.getEUVar() >= power) {
eHatch.setEUVar(eHatch.getEUVar() - power);
temp_stored += power;
inputLastTick += power;
}
}
// Output energy to TT hatches
for(GT_MetaTileEntity_Hatch_DynamoMulti eDynamo : mDynamoHatchesTT) {
if(eDynamo == null || eDynamo.getBaseMetaTileEntity().isInvalidTileEntity()){
continue;
}
final long power = getPowerToPush(eDynamo.maxEUOutput() * eDynamo.maxAmperesOut());
if(power <= eDynamo.maxEUStore() - eDynamo.getEUVar()) {
eDynamo.setEUVar(eDynamo.getEUVar() + power);
temp_stored -= power;
outputLastTick += power;
}
}
// Draw energy from TT Laser hatches
for(GT_MetaTileEntity_Hatch_EnergyTunnel eHatch : mEnergyTunnelsTT) {
if(eHatch == null || eHatch.getBaseMetaTileEntity().isInvalidTileEntity()) {
continue;
}
final long ttLaserWattage = eHatch.maxEUInput() * eHatch.Amperes - (eHatch.Amperes / 20);
final long power = getPowerToDraw(ttLaserWattage);
if(eHatch.getEUVar() >= power) {
eHatch.setEUVar(eHatch.getEUVar() - power);
temp_stored += power;
inputLastTick += power;
}
}
// Output energy to TT Laser hatches
for(GT_MetaTileEntity_Hatch_DynamoTunnel eDynamo : mDynamoTunnelsTT) {
if(eDynamo == null || eDynamo.getBaseMetaTileEntity().isInvalidTileEntity()){
continue;
}
final long ttLaserWattage = eDynamo.maxEUOutput() * eDynamo.Amperes - (eDynamo.Amperes / 20);
final long power = getPowerToPush(ttLaserWattage);
if(power <= eDynamo.maxEUStore() - eDynamo.getEUVar()) {
eDynamo.setEUVar(eDynamo.getEUVar() + power);
temp_stored -= power;
outputLastTick += power;
}
}
if (getUHVCapacitorCount() <= 0) {
wireless_mode = false;
}
// Every LSC_time_between_wireless_rebalance_in_ticks check against wireless network for re-balancing.
counter++;
if (wireless_mode && (counter == LSC_time_between_wireless_rebalance_in_ticks)) {
// Reset tick counter.
counter = 1;
// Find difference.
BigInteger transferred_eu = stored.subtract(LSC_wireless_eu_cap.multiply(BigInteger.valueOf(getUHVCapacitorCount())));
if (transferred_eu.signum() == 1) {
inputLastTick += transferred_eu.longValue();
} else {
outputLastTick += transferred_eu.longValue();
}
// If that difference can be added then do so.
if (addEUToGlobalEnergyMap(global_energy_user_uuid, transferred_eu)) {
// If it succeeds there was sufficient energy so set the internal capacity as such.
stored = LSC_wireless_eu_cap.multiply(BigInteger.valueOf(getUHVCapacitorCount()));
}
}
// Lose some energy.
// Re-calculate if the repair status changed.
if(super.getRepairStatus() != repairStatusCache) {
passiveDischargeAmount = recalculateLossWithMaintenance(super.getRepairStatus());
}
// This will break if you transfer more than 2^63 EU/t, so don't do that. Thanks <3
temp_stored -= passiveDischargeAmount;
stored = stored.add(BigInteger.valueOf(temp_stored));
// Check that the machine has positive EU stored.
stored = (stored.compareTo(BigInteger.ZERO) <= 0) ? BigInteger.ZERO : stored;
IGregTechTileEntity tBMTE = this.getBaseMetaTileEntity();
tBMTE.injectEnergyUnits((byte)ForgeDirection.UNKNOWN.ordinal(), inputLastTick, 1L);
tBMTE.drainEnergyUnits((byte)ForgeDirection.UNKNOWN.ordinal(), outputLastTick, 1L);
//Add I/O values to Queues
if(energyInputValues.size() > DURATION_AVERAGE_TICKS){
energyInputValues.remove();
}
energyInputValues.offer(inputLastTick);
if(energyOutputValues.size() > DURATION_AVERAGE_TICKS){
energyOutputValues.remove();
}
energyOutputValues.offer(outputLastTick);
return true;
}
/**
* To be called whenever the maintenance status changes or the capacity was recalculated
* @param repairStatus
* This machine's repair status
* @return new BigInteger instance for passiveDischargeAmount
*/
private long recalculateLossWithMaintenance(int repairStatus) {
repairStatusCache = repairStatus;
// This cannot overflow because there is a 135 capacitor maximum per LSC.
long temp_capacity_divided = capacity.divide(BigInteger.valueOf(100L * 86400L * 20L)).longValue();
// Passive loss is multiplied by number of UHV caps. Minimum of 1 otherwise loss is 0 for non-UHV caps calculations.
long uhv_cap_multiplier = min(temp_capacity_divided, max_passive_drain_eu_per_tick_per_uhv_cap * max(1, getUHVCapacitorCount()));
// Passive loss is multiplied by number of maintenance issues.
long total_passive_loss = uhv_cap_multiplier * (getIdealStatus() - repairStatus + 1);
// Maximum of 100,000 EU/t drained per UHV cell. The logic is 1% of EU capacity should be drained every 86400 seconds (1 day).
return total_passive_loss;
}
/**
* Calculate how much EU to draw from an Energy Hatch
* @param hatchWatts
* Hatch amperage * voltage
* @return EU amount
*/
private long getPowerToDraw(long hatchWatts){
final BigInteger remcapActual = capacity.subtract(stored);
final BigInteger recampLimited = (MAX_LONG.compareTo(remcapActual) > 0) ? remcapActual : MAX_LONG;
return min(hatchWatts, recampLimited.longValue());
}
/**
* Calculate how much EU to push into a Dynamo Hatch
* @param hatchWatts
* Hatch amperage * voltage
* @return EU amount
*/
private long getPowerToPush(long hatchWatts){
final BigInteger remStoredLimited = (MAX_LONG.compareTo(stored) > 0) ? stored : MAX_LONG;
return min(hatchWatts, remStoredLimited.longValue());
}
private long getAvgIn(){
long sum = 0L;
for(long l: energyInputValues){
sum += l;
}
return sum / Math.max(energyInputValues.size(), 1);
}
private long getAvgOut(){
long sum = 0L;
for(long l: energyOutputValues){
sum += l;
}
return sum / Math.max(energyOutputValues.size(), 1);
}
@Override
public String[] getInfoData() {
NumberFormat nf = NumberFormat.getNumberInstance();
int secInterval = DURATION_AVERAGE_TICKS/20;
final ArrayList<String> ll = new ArrayList<>();
ll.add(EnumChatFormatting.YELLOW + "Operational Data:" + EnumChatFormatting.RESET);
ll.add("Used Capacity: " + nf.format(stored) + "EU");
ll.add("Total Capacity: " + nf.format(capacity) + "EU");
ll.add("Passive Loss: " + nf.format(passiveDischargeAmount) + "EU/t");
ll.add("EU IN: " + GT_Utility.formatNumbers(inputLastTick) + "EU/t");
ll.add("EU OUT: " + GT_Utility.formatNumbers(outputLastTick) + "EU/t");
ll.add("Avg EU IN: " + nf.format(getAvgIn()) + " (last " + secInterval + " seconds)");
ll.add("Avg EU OUT: " + nf.format(getAvgOut()) + " (last " + secInterval + " seconds)");
ll.add("Maintenance Status: " + ((super.getRepairStatus() == super.getIdealStatus())
? EnumChatFormatting.GREEN + "Working perfectly" + EnumChatFormatting.RESET
: EnumChatFormatting.RED + "Has Problems" + EnumChatFormatting.RESET));
ll.add("Wireless mode: " + (wireless_mode
? EnumChatFormatting.GREEN + "enabled" + EnumChatFormatting.RESET
: EnumChatFormatting.RED + "disabled" + EnumChatFormatting.RESET));
ll.add(GT_Values.TIER_COLORS[9] + GT_Values.VN[9] + EnumChatFormatting.RESET + " Capacitors detected: " + getUHVCapacitorCount());
ll.add("Total wireless EU: " + EnumChatFormatting.RED + GT_Utility.formatNumbers(getUserEU(global_energy_user_uuid)));
ll.add("---------------------------------------------");
final String[] a = new String[ll.size()];
return ll.toArray(a);
}
@Override
public void saveNBTData(NBTTagCompound nbt) {
nbt = (nbt == null) ? new NBTTagCompound() : nbt;
nbt.setByteArray("capacity", capacity.toByteArray());
nbt.setByteArray("stored", stored.toByteArray());
nbt.setBoolean("wireless_mode", wireless_mode);
super.saveNBTData(nbt);
}
@Override
public void loadNBTData(NBTTagCompound nbt) {
nbt = (nbt == null) ? new NBTTagCompound() : nbt;
capacity = new BigInteger(nbt.getByteArray("capacity"));
stored = new BigInteger(nbt.getByteArray("stored"));
wireless_mode = nbt.getBoolean("wireless_mode");
super.loadNBTData(nbt);
}
@Override
public boolean isGivingInformation() {
return true;
}
@Override
public int getMaxEfficiency(ItemStack stack) { return 10000; }
@Override
public int getPollutionPerTick(ItemStack stack) { return 0; }
@Override
public int getDamageToComponent(ItemStack stack) { return 0; }
@Override
public boolean explodesOnComponentBreak(ItemStack stack) { return false; }
//called by the getEUCapacity() function in BaseMetaTileEntity
@Override
public long maxEUStore()
{
return capacity.longValue();
}
//called by the getEUStored() function in BaseMetaTileEntity
@Override
public long getEUVar()
{
return stored.longValue();
}
/* all of these are needed for the injectEnergyUnits() and drainEnergyUnits()
in IGregTechTileEntity
*/
@Override
public long maxEUInput()
{
return mMaxEUIn;
}
@Override
public long maxAmperesIn()
{
return 1L;
}
@Override
public long maxEUOutput()
{
return mMaxEUOut;
}
@Override
public long maxAmperesOut()
{
return 1L;
}
@Override
public boolean isEnetInput()
{
return true;
}
@Override
public boolean isEnetOutput()
{
return true;
}
@Override
public void onScrewdriverRightClick(byte aSide, EntityPlayer aPlayer, float aX, float aY, float aZ) {
if (getUHVCapacitorCount() != 0) {
wireless_mode = !wireless_mode;
GT_Utility.sendChatToPlayer(aPlayer, "Wireless network mode " + (wireless_mode ? "enabled." : "disabled."));
} else {
GT_Utility.sendChatToPlayer(aPlayer, "Wireless mode cannot be enabled without at least 1 " + GT_Values.TIER_COLORS[9] + GT_Values.VN[9] + EnumChatFormatting.RESET + " capacitor.");
wireless_mode = false;
}
}
private enum LSCHatchElement implements IHatchElement<GTMTE_LapotronicSuperCapacitor> {
Energy(GT_MetaTileEntity_Hatch_EnergyMulti.class, GT_MetaTileEntity_Hatch_Energy.class) {
@Override
public long count(GTMTE_LapotronicSuperCapacitor t) {
return t.mEnergyHatches.size() + t.mEnergyHatchesTT.size() + t.mEnergyTunnelsTT.size();
}
},
Dynamo(GT_MetaTileEntity_Hatch_DynamoMulti.class, GT_MetaTileEntity_Hatch_Dynamo.class) {
@Override
public long count(GTMTE_LapotronicSuperCapacitor t) {
return t.mDynamoHatches.size() + t.mDynamoHatchesTT.size() + t.mDynamoTunnelsTT.size();
}
},
;
private final List<? extends Class<? extends IMetaTileEntity>> mteClasses;
@SafeVarargs
LSCHatchElement(Class<? extends IMetaTileEntity>... mteClasses) {
this.mteClasses = Arrays.asList(mteClasses);
}
@Override
public List<? extends Class<? extends IMetaTileEntity>> mteClasses() {
return mteClasses;
}
@Override
public IGT_HatchAdder<? super GTMTE_LapotronicSuperCapacitor> adder() {
return GTMTE_LapotronicSuperCapacitor::addBottomHatches;
}
}
}
|