From 36acbb9baed7dae4e19fde7a95852b7d002b2f5c Mon Sep 17 00:00:00 2001 From: Abdiel Kavash <19243993+AbdielKavash@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:08:11 -0600 Subject: Follow up fixes for multiple muffler hatches. (#3505) Co-authored-by: Martin Robertz --- .../MTEExtendedPowerMultiBlockBase.java | 8 +-- .../implementations/MTEHatchMuffler.java | 17 +++-- .../implementations/MTEMultiBlockBase.java | 84 ++++++++++++++++------ 3 files changed, 76 insertions(+), 33 deletions(-) (limited to 'src/main/java/gregtech/api/metatileentity/implementations') diff --git a/src/main/java/gregtech/api/metatileentity/implementations/MTEExtendedPowerMultiBlockBase.java b/src/main/java/gregtech/api/metatileentity/implementations/MTEExtendedPowerMultiBlockBase.java index 09bfda2d0a..9ff7f34fe3 100644 --- a/src/main/java/gregtech/api/metatileentity/implementations/MTEExtendedPowerMultiBlockBase.java +++ b/src/main/java/gregtech/api/metatileentity/implementations/MTEExtendedPowerMultiBlockBase.java @@ -2,7 +2,6 @@ package gregtech.api.metatileentity.implementations; import static gregtech.api.enums.GTValues.VN; import static gregtech.api.util.GTUtility.filterValidMTEs; -import static gregtech.api.util.GTUtility.validMTEList; import java.util.ArrayList; import java.util.List; @@ -138,11 +137,6 @@ public abstract class MTEExtendedPowerMultiBlockBase(mMufflerHatches.size()); - validMTEList(mMufflerHatches).forEach(validMufflers::add); - Collections.shuffle(validMufflers); - for (MTEHatchMuffler tHatch : validMufflers) { - if (mPollution >= 10000) { - if (tHatch.polluteEnvironment(this)) { - mPollution -= 10000; - } + if (mPollution < VENT_AMOUNT) return true; + if (mMufflerHatches.size() == 0) { + // No muffler present. Fail. + return false; + } else if (mMufflerHatches.size() == 1) { + // One muffler, use simple method for performance. + MTEHatchMuffler muffler = mMufflerHatches.get(0); + if (muffler == null || !muffler.isValid()) { + // Muffler invalid. Fail. + mMufflerHatches.remove(0); + return false; } else { - break; + if (muffler.polluteEnvironment(this, VENT_AMOUNT)) { + mPollution -= VENT_AMOUNT; + } else { + // Muffler blocked. Fail. + return false; + } } + } else { + // Multiple mufflers, split pollution output evenly between all of them. + int mufflerCount = 0; + int ventAmount = 0; // Allow venting of up to VENT_AMOUNT of pollution per muffler. + for (MTEHatchMuffler muffler : validMTEList(mMufflerHatches)) { + mufflerCount++; + if (ventAmount + VENT_AMOUNT <= mPollution) { + ventAmount += VENT_AMOUNT; + } + } + // This might lose some small amount of pollution due to rounding, this is fine. + ventAmount /= mufflerCount; + + for (MTEHatchMuffler muffler : validMTEList(mMufflerHatches)) { + if (muffler.polluteEnvironment(this, ventAmount)) { + mPollution -= ventAmount; + } else { + // Muffler blocked. Fail. + return false; + } + } + } + return mPollution < VENT_AMOUNT; + } + + /** + * How much pollution this outputs to the environment. 100 = outputs all, 0 = outputs none. Calculated as an average + * across all muffler hatches. + * + * @return Fraction of pollution output to the environment (out of 100). + */ + public int getAveragePollutionPercentage() { + int pollutionPercent = 0; + int mufflerCount = 0; + for (MTEHatchMuffler muffler : validMTEList(mMufflerHatches)) { + pollutionPercent += muffler.calculatePollutionReduction(100); + mufflerCount++; + } + if (mufflerCount > 0) { + pollutionPercent /= mufflerCount; + } else { + pollutionPercent = 100; } - return mPollution < 10000; + return pollutionPercent; } protected void sendStartMultiBlockSoundLoop() { @@ -1826,16 +1876,6 @@ public abstract class MTEMultiBlockBase extends MetaTileEntity @Override public String[] getInfoData() { - int mPollutionReduction = 0; - var validMufflers = new ArrayList(mMufflerHatches.size()); - validMTEList(mMufflerHatches).forEach(validMufflers::add); - if (validMufflers.size() > 0) { - for (MTEHatchMuffler tHatch : validMufflers) { - mPollutionReduction += tHatch.calculatePollutionReduction(100); - } - mPollutionReduction /= validMufflers.size(); - } - long storedEnergy = 0; long maxEnergy = 0; for (MTEHatchEnergy tHatch : validMTEList(mEnergyHatches)) { @@ -1891,7 +1931,7 @@ public abstract class MTEMultiBlockBase extends MetaTileEntity + " %", /* 6 */ StatCollector.translateToLocal("GT5U.multiblock.pollution") + ": " + EnumChatFormatting.GREEN - + mPollutionReduction + + getAveragePollutionPercentage() + EnumChatFormatting.RESET + " %" }; } -- cgit