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
|
package gregtech.common.tileentities.machines.multi;
import static com.gtnewhorizon.structurelib.structure.StructureUtility.lazy;
import static com.gtnewhorizon.structurelib.structure.StructureUtility.ofBlock;
import static com.gtnewhorizon.structurelib.structure.StructureUtility.transpose;
import static gregtech.api.enums.HatchElement.Dynamo;
import static gregtech.api.enums.HatchElement.InputBus;
import static gregtech.api.enums.HatchElement.InputHatch;
import static gregtech.api.enums.HatchElement.Maintenance;
import static gregtech.api.enums.HatchElement.Muffler;
import static gregtech.api.enums.HatchElement.OutputBus;
import static gregtech.api.enums.HatchElement.OutputHatch;
import static gregtech.api.enums.Textures.BlockIcons.TURBINE_NEW;
import static gregtech.api.enums.Textures.BlockIcons.TURBINE_NEW_ACTIVE;
import static gregtech.api.enums.Textures.BlockIcons.TURBINE_NEW_EMPTY;
import static gregtech.api.util.GTStructureUtility.buildHatchAdder;
import static gregtech.api.util.GTUtility.filterValidMTEs;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import org.jetbrains.annotations.NotNull;
import com.gtnewhorizon.structurelib.alignment.IAlignmentLimits;
import com.gtnewhorizon.structurelib.alignment.constructable.ISurvivalConstructable;
import com.gtnewhorizon.structurelib.alignment.enumerable.ExtendedFacing;
import com.gtnewhorizon.structurelib.alignment.enumerable.Flip;
import com.gtnewhorizon.structurelib.alignment.enumerable.Rotation;
import com.gtnewhorizon.structurelib.structure.IStructureDefinition;
import com.gtnewhorizon.structurelib.structure.ISurvivalBuildEnvironment;
import com.gtnewhorizon.structurelib.structure.StructureDefinition;
import gregtech.api.enums.Dyes;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.items.MetaGeneratedTool;
import gregtech.api.metatileentity.implementations.MTEEnhancedMultiBlockBase;
import gregtech.api.metatileentity.implementations.MTEHatchDynamo;
import gregtech.api.metatileentity.implementations.MTEHatchMuffler;
import gregtech.api.recipe.check.CheckRecipeResult;
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
import gregtech.api.util.GTUtility;
import gregtech.api.util.LightingHelper;
import gregtech.api.util.TurbineStatCalculator;
import gregtech.api.util.shutdown.ShutDownReasonRegistry;
import gregtech.common.items.MetaGeneratedTool01;
import gregtech.common.render.GTRenderUtil;
public abstract class MTELargeTurbine extends MTEEnhancedMultiBlockBase<MTELargeTurbine>
implements ISurvivalConstructable {
private static final String STRUCTURE_PIECE_MAIN = "main";
private static final ClassValue<IStructureDefinition<MTELargeTurbine>> STRUCTURE_DEFINITION = new ClassValue<>() {
@Override
protected IStructureDefinition<MTELargeTurbine> computeValue(Class<?> type) {
return StructureDefinition.<MTELargeTurbine>builder()
.addShape(
STRUCTURE_PIECE_MAIN,
transpose(
new String[][] { { " ", " ", " ", " ", " ", },
{ " --- ", " ccc ", " hhh ", " hhh ", " hhh ", },
{ " --- ", " c~c ", " h-h ", " h-h ", " hdh ", },
{ " --- ", " ccc ", " hhh ", " hhh ", " hhh ", },
{ " ", " ", " ", " ", " ", }, }))
.addElement('c', lazy(t -> ofBlock(t.getCasingBlock(), t.getCasingMeta())))
.addElement('d', lazy(t -> Dynamo.newAny(t.getCasingTextureIndex(), 1)))
.addElement(
'h',
lazy(
t -> buildHatchAdder(MTELargeTurbine.class)
.atLeast(Maintenance, InputHatch, OutputHatch, OutputBus, InputBus, Muffler)
.casingIndex(t.getCasingTextureIndex())
.dot(2)
.buildAndChain(t.getCasingBlock(), t.getCasingMeta())))
.build();
}
};
protected int baseEff = 0;
protected int optFlow = 0;
protected double realOptFlow = 0;
protected int storedFluid = 0;
protected int counter = 0;
protected boolean looseFit = false;
protected int overflowMultiplier = 0;
protected final float[] flowMultipliers = new float[] { 1, 1, 1 };
// client side stuff
protected boolean mHasTurbine;
// mMachine got overwritten by StructureLib extended facing query response
// so we use a separate field for this
protected boolean mFormed;
public MTELargeTurbine(int aID, String aName, String aNameRegional) {
super(aID, aName, aNameRegional);
}
public MTELargeTurbine(String aName) {
super(aName);
}
@Override
public boolean isCorrectMachinePart(ItemStack aStack) {
return getMaxEfficiency(aStack) > 0;
}
@Override
public IStructureDefinition<MTELargeTurbine> getStructureDefinition() {
return STRUCTURE_DEFINITION.get(getClass());
}
@Override
protected IAlignmentLimits getInitialAlignmentLimits() {
return (d, r, f) -> r.isNotRotated() && f.isNotFlipped();
}
@Override
protected ExtendedFacing getCorrectedAlignment(ExtendedFacing aOldFacing) {
return aOldFacing.with(Flip.NONE)
.with(Rotation.NORMAL);
}
@Override
public boolean isFlipChangeAllowed() {
return false;
}
@Override
public boolean isRotationChangeAllowed() {
return false;
}
@Override
public boolean checkMachine(IGregTechTileEntity aBaseMetaTileEntity, ItemStack aStack) {
return checkPiece(STRUCTURE_PIECE_MAIN, 2, 2, 1) && mMaintenanceHatches.size() == 1
&& mMufflerHatches.isEmpty() == (getPollutionPerTick(null) == 0);
}
public abstract Block getCasingBlock();
public abstract byte getCasingMeta();
public abstract int getCasingTextureIndex();
public boolean isNewStyleRendering() {
return false;
}
public IIconContainer[] getTurbineTextureActive() {
return TURBINE_NEW_ACTIVE;
}
public IIconContainer[] getTurbineTextureFull() {
return TURBINE_NEW;
}
public IIconContainer[] getTurbineTextureEmpty() {
return TURBINE_NEW_EMPTY;
}
@Override
public boolean renderInWorld(IBlockAccess aWorld, int aX, int aY, int aZ, Block aBlock, RenderBlocks aRenderer) {
if (!isNewStyleRendering() || !mFormed) return false;
int[] tABCCoord = new int[] { -1, -1, 0 };
int[] tXYZOffset = new int[3];
final ForgeDirection tFacing = getBaseMetaTileEntity().getFrontFacing();
final ExtendedFacing tExtendedFacing = getExtendedFacing();
final ForgeDirection tDirection = tExtendedFacing.getDirection();
final LightingHelper tLighting = new LightingHelper(aRenderer);
// for some reason +x and -z need this field set to true, but not any other sides
if (tFacing == ForgeDirection.NORTH || tFacing == ForgeDirection.EAST) aRenderer.field_152631_f = true;
final Block tBlock = getCasingBlock();
IIconContainer[] tTextures;
if (getBaseMetaTileEntity().isActive()) tTextures = getTurbineTextureActive();
else if (hasTurbine()) tTextures = getTurbineTextureFull();
else tTextures = getTurbineTextureEmpty();
assert tTextures != null && tTextures.length == tABCCoord.length;
for (int i = 0; i < 9; i++) {
if (i != 4) { // do not draw ourselves again.
tExtendedFacing.getWorldOffset(tABCCoord, tXYZOffset);
// since structure check passed, we can assume it is turbine casing
int tX = tXYZOffset[0] + aX;
int tY = tXYZOffset[1] + aY;
int tZ = tXYZOffset[2] + aZ;
// we skip the occlusion test, as we always require a working turbine to have a block of air before it
// so the front face cannot be occluded whatsoever in the most cases.
Tessellator.instance.setBrightness(
tBlock.getMixedBrightnessForBlock(
aWorld,
aX + tDirection.offsetX,
tY + tDirection.offsetY,
aZ + tDirection.offsetZ));
tLighting.setupLighting(tBlock, tX, tY, tZ, tFacing)
.setupColor(tFacing, Dyes._NULL.mRGBa);
GTRenderUtil.renderBlockIcon(
aRenderer,
tBlock,
tX + tDirection.offsetX * 0.01,
tY + tDirection.offsetY * 0.01,
tZ + tDirection.offsetZ * 0.01,
tTextures[i].getIcon(),
tFacing);
}
if (++tABCCoord[0] == 2) {
tABCCoord[0] = -1;
tABCCoord[1]++;
}
}
aRenderer.field_152631_f = false;
return false;
}
@Override
public void onValueUpdate(byte aValue) {
mHasTurbine = (aValue & 0x1) != 0;
mFormed = (aValue & 0x2) != 0;
super.onValueUpdate(aValue);
}
@Override
public byte getUpdateData() {
return (byte) ((hasTurbine() ? 1 : 0) | (mMachine ? 2 : 0));
}
@Override
public boolean addToMachineList(IGregTechTileEntity tTileEntity, int aBaseCasingIndex) {
return addMaintenanceToMachineList(tTileEntity, getCasingTextureIndex())
|| addInputToMachineList(tTileEntity, getCasingTextureIndex())
|| addOutputToMachineList(tTileEntity, getCasingTextureIndex())
|| addMufflerToMachineList(tTileEntity, getCasingTextureIndex());
}
@Override
public void saveNBTData(NBTTagCompound aNBT) {
super.saveNBTData(aNBT);
}
@Override
public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
}
@Override
@NotNull
public CheckRecipeResult checkProcessing() {
ItemStack controllerSlot = getControllerSlot();
if ((counter & 7) == 0 && (controllerSlot == null || !(controllerSlot.getItem() instanceof MetaGeneratedTool)
|| controllerSlot.getItemDamage() < 170
|| controllerSlot.getItemDamage() > 179)) {
stopMachine(ShutDownReasonRegistry.NO_TURBINE);
return CheckRecipeResultRegistry.NO_TURBINE_FOUND;
}
TurbineStatCalculator turbine = new TurbineStatCalculator(
(MetaGeneratedTool) controllerSlot.getItem(),
controllerSlot);
ArrayList<FluidStack> tFluids = getStoredFluids();
if (!tFluids.isEmpty()) {
if (baseEff == 0 || optFlow == 0
|| counter >= 512
|| this.getBaseMetaTileEntity()
.hasWorkJustBeenEnabled()
|| this.getBaseMetaTileEntity()
.hasInventoryBeenModified()) {
counter = 0;
baseEff = (int) turbine.getEfficiency();
optFlow = (int) turbine.getOptimalFlow();
overflowMultiplier = turbine.getOverflowEfficiency();
if (optFlow <= 0 || baseEff <= 0) {
stopMachine(ShutDownReasonRegistry.NONE); // in case the turbine got removed
return CheckRecipeResultRegistry.NO_FUEL_FOUND;
}
} else {
counter++;
}
}
int newPower = fluidIntoPower(tFluids, turbine); // How much the
// turbine should
// be producing
// with this flow
int difference = newPower - this.mEUt; // difference between current output and new output
// Magic numbers: can always change by at least 10 eu/t, but otherwise by at most 1 percent of the difference in
// power level (per tick)
// This is how much the turbine can actually change during this tick
int maxChangeAllowed = Math.max(10, GTUtility.safeInt((long) Math.abs(difference) / 100));
if (Math.abs(difference) > maxChangeAllowed) { // If this difference is too big, use the maximum allowed change
int change = maxChangeAllowed * (difference > 0 ? 1 : -1); // Make the change positive or negative.
this.mEUt += change; // Apply the change
} else this.mEUt = newPower;
if (this.mEUt <= 0) {
// stopMachine();
this.mEUt = 0;
this.mEfficiency = 0;
return CheckRecipeResultRegistry.NO_FUEL_FOUND;
} else {
this.mMaxProgresstime = 1;
this.mEfficiencyIncrease = 10;
// Overvoltage is handled inside the MultiBlockBase when pushing out to dynamos. no need to do it here.
return CheckRecipeResultRegistry.GENERATING;
}
}
abstract int fluidIntoPower(ArrayList<FluidStack> aFluids, TurbineStatCalculator turbine);
abstract float getOverflowEfficiency(int totalFlow, int actualOptimalFlow, int overflowMultiplier);
// Gets the maximum output that the turbine currently can handle. Going above this will cause the turbine to explode
public long getMaximumOutput() {
long aTotal = 0;
for (MTEHatchDynamo aDynamo : filterValidMTEs(mDynamoHatches)) {
long aVoltage = aDynamo.maxEUOutput();
aTotal = aDynamo.maxAmperesOut() * aVoltage;
}
return aTotal;
}
@Override
public int getDamageToComponent(ItemStack aStack) {
return 1;
}
@Override
public int getMaxEfficiency(ItemStack aStack) {
if (GTUtility.isStackInvalid(aStack)) {
return 0;
}
if (aStack.getItem() instanceof MetaGeneratedTool01) {
return 10000;
}
return 0;
}
@Override
public boolean explodesOnComponentBreak(ItemStack aStack) {
return true;
}
@Override
public String[] getInfoData() {
int mPollutionReduction = 0;
for (MTEHatchMuffler tHatch : filterValidMTEs(mMufflerHatches)) {
mPollutionReduction = Math.max(tHatch.calculatePollutionReduction(100), mPollutionReduction);
}
String tRunning = mMaxProgresstime > 0
? EnumChatFormatting.GREEN + StatCollector.translateToLocal("GT5U.turbine.running.true")
+ EnumChatFormatting.RESET
: EnumChatFormatting.RED + StatCollector.translateToLocal("GT5U.turbine.running.false")
+ EnumChatFormatting.RESET;
String tMaintainance = getIdealStatus() == getRepairStatus()
? EnumChatFormatting.GREEN + StatCollector.translateToLocal("GT5U.turbine.maintenance.false")
+ EnumChatFormatting.RESET
: EnumChatFormatting.RED + StatCollector.translateToLocal("GT5U.turbine.maintenance.true")
+ EnumChatFormatting.RESET;
int tDura = 0;
if (mInventory[1] != null && mInventory[1].getItem() instanceof MetaGeneratedTool01) {
tDura = GTUtility.safeInt(
(long) (100.0f / MetaGeneratedTool.getToolMaxDamage(mInventory[1])
* (MetaGeneratedTool.getToolDamage(mInventory[1])) + 1));
}
long storedEnergy = 0;
long maxEnergy = 0;
for (MTEHatchDynamo tHatch : filterValidMTEs(mDynamoHatches)) {
storedEnergy += tHatch.getBaseMetaTileEntity()
.getStoredEU();
maxEnergy += tHatch.getBaseMetaTileEntity()
.getEUCapacity();
}
String[] ret = new String[] {
// 8 Lines available for information panels
tRunning + ": "
+ EnumChatFormatting.RED
+ GTUtility.formatNumbers(((long) mEUt * mEfficiency) / 10000)
+ EnumChatFormatting.RESET
+ " EU/t", /* 1 */
tMaintainance, /* 2 */
StatCollector.translateToLocal("GT5U.turbine.efficiency") + ": "
+ EnumChatFormatting.YELLOW
+ (mEfficiency / 100F)
+ EnumChatFormatting.RESET
+ "%", /* 2 */
StatCollector.translateToLocal("GT5U.multiblock.energy") + ": "
+ EnumChatFormatting.GREEN
+ GTUtility.formatNumbers(storedEnergy)
+ EnumChatFormatting.RESET
+ " EU / "
+ /* 3 */ EnumChatFormatting.YELLOW
+ GTUtility.formatNumbers(maxEnergy)
+ EnumChatFormatting.RESET
+ " EU",
StatCollector.translateToLocal("GT5U.turbine.flow") + ": "
+ EnumChatFormatting.YELLOW
+ GTUtility.formatNumbers(GTUtility.safeInt((long) realOptFlow))
+ EnumChatFormatting.RESET
+ " L/t"
+ /* 4 */ EnumChatFormatting.YELLOW
+ " ("
+ (looseFit ? StatCollector.translateToLocal("GT5U.turbine.loose")
: StatCollector.translateToLocal("GT5U.turbine.tight"))
+ ")", /* 5 */
StatCollector.translateToLocal("GT5U.turbine.fuel") + ": "
+ EnumChatFormatting.GOLD
+ GTUtility.formatNumbers(storedFluid)
+ EnumChatFormatting.RESET
+ "L", /* 6 */
StatCollector.translateToLocal(
"GT5U.turbine.dmg") + ": " + EnumChatFormatting.RED + tDura + EnumChatFormatting.RESET + "%", /* 7 */
StatCollector.translateToLocal("GT5U.multiblock.pollution") + ": "
+ EnumChatFormatting.GREEN
+ mPollutionReduction
+ EnumChatFormatting.RESET
+ " %" /* 8 */
};
if (!this.getClass()
.getName()
.contains("Steam"))
ret[4] = StatCollector.translateToLocal("GT5U.turbine.flow") + ": "
+ EnumChatFormatting.YELLOW
+ GTUtility.safeInt((long) realOptFlow)
+ EnumChatFormatting.RESET
+ " L/t";
return ret;
}
public boolean hasTurbine() {
return getBaseMetaTileEntity() != null && getBaseMetaTileEntity().isClientSide() ? mHasTurbine
: this.getMaxEfficiency(mInventory[1]) > 0;
}
public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) {
if (side == getBaseMetaTileEntity().getFrontFacing()) {
looseFit ^= true;
GTUtility.sendChatToPlayer(
aPlayer,
looseFit ? GTUtility.trans("500", "Fitting: Loose - More Flow")
: GTUtility.trans("501", "Fitting: Tight - More Efficiency"));
}
}
@Override
public boolean isGivingInformation() {
return true;
}
@Override
public void construct(ItemStack stackSize, boolean hintsOnly) {
buildPiece(STRUCTURE_PIECE_MAIN, stackSize, hintsOnly, 2, 2, 1);
}
@Override
public int survivalConstruct(ItemStack stackSize, int elementBudget, ISurvivalBuildEnvironment env) {
if (mMachine) return -1;
return survivialBuildPiece(STRUCTURE_PIECE_MAIN, stackSize, 2, 2, 1, elementBudget, env, false, true);
}
}
|