diff --git a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine.java b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine.java index 461bf159..03509dd9 100644 --- a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine.java +++ b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine.java @@ -110,57 +110,59 @@ public abstract class GT_MetaTileEntity_LargeTurbine extends GT_MetaTileEntity_M return ((addMaintenanceToMachineList(tTileEntity, getCasingTextureIndex())) || (addInputToMachineList(tTileEntity, getCasingTextureIndex())) || (addOutputToMachineList(tTileEntity, getCasingTextureIndex()))|| (addMufflerToMachineList(tTileEntity, getCasingTextureIndex()))); } - private int[] mLastTicks = new int[256]; - private int mCurrentTick; - private long mOverall; - - public int getAverage(int aCurrent){ - ++mCurrentTick; - mCurrentTick = mCurrentTick % 256; - mOverall = mOverall - mLastTicks[mCurrentTick]; - mOverall = mOverall + aCurrent; - mLastTicks[mCurrentTick] = aCurrent; - return (int) (mOverall/256); - } + @Override public void saveNBTData(NBTTagCompound aNBT) { super.saveNBTData(aNBT); - aNBT.setLong("mOverall", mOverall); } @Override public void loadNBTData(NBTTagCompound aNBT) { super.loadNBTData(aNBT); - mOverall = aNBT.getLong("mOverall"); - mOverall = mOverall - mOverall%256; - int tAverage = (int) (mOverall <<7); - for(int i = 0;i<256;i++){ - mLastTicks[i]=tAverage; - } } @Override public boolean checkRecipe(ItemStack aStack) { ArrayList tFluids = getStoredFluids(); - if (tFluids.size()>0){ - if(baseEff==0 || optFlow == 0 || counter >= 1000 || this.getBaseMetaTileEntity().hasWorkJustBeenEnabled() || this.getBaseMetaTileEntity().hasInventoryBeenModified()){ - counter = 0; - baseEff = (int) ((50.0F+(10.0F*((GT_MetaGenerated_Tool)aStack.getItem()).getToolCombatDamage(aStack)))*100); - optFlow = (int) Math.max(Float.MIN_NORMAL, ((GT_MetaGenerated_Tool)aStack.getItem()).getToolStats(aStack).getSpeedMultiplier() * ((GT_MetaGenerated_Tool)aStack.getItem()).getPrimaryMaterial(aStack).mToolSpeed*50); - }else{ - counter++;}} - this.mEUt = fluidIntoPower(tFluids, optFlow, baseEff); - this.mMaxProgresstime = 1; - this.mEfficiencyIncrease = (10); - if(mEUt<=0){ - mEfficiency=0; - mOverall=0; - mLastTicks = new int[256]; - stopMachine(); - return false; - }else{ - return true;} + if (tFluids.size() > 0) { + if (baseEff == 0 || optFlow == 0 || counter >= 1000 || this.getBaseMetaTileEntity().hasWorkJustBeenEnabled() + || this.getBaseMetaTileEntity().hasInventoryBeenModified()) { + counter = 0; + baseEff = (int) ((50.0F + + (10.0F * ((GT_MetaGenerated_Tool) aStack.getItem()).getToolCombatDamage(aStack))) * 100); + optFlow = (int) Math.max(Float.MIN_NORMAL, + ((GT_MetaGenerated_Tool) aStack.getItem()).getToolStats(aStack).getSpeedMultiplier() + * ((GT_MetaGenerated_Tool) aStack.getItem()).getPrimaryMaterial(aStack).mToolSpeed + * 50); + } else { + counter++; + } + } + + int newPower = fluidIntoPower(tFluids, optFlow, baseEff); // 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, (int) Math.ceil(Math.abs(difference) * 0.01)); + + 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; + + this.mMaxProgresstime = 1; + this.mEfficiencyIncrease = (10); + if (mEUt <= 0) { + mEfficiency = 0; + stopMachine(); + return false; + } else { + return true; + } } abstract int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff); diff --git a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Gas.java b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Gas.java index 010f05a0..2f750ab9 100644 --- a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Gas.java +++ b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Gas.java @@ -69,22 +69,48 @@ public class GT_MetaTileEntity_LargeTurbine_Gas extends GT_MetaTileEntity_LargeT return 10; } - @Override - int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff) { - int tEU=0; - int tOut=0; - int tOptFlow = aOptFlow; - boolean b = false; - for(int i=0;i0&&depleteInput(new FluidStack(aFluids.get(i),Math.max(tOptFlow/(fuelValue*2),1)))){ - tEU += tOptFlow/2;} - } - if(tEU>0)b=true; - tEU = getAverage(tEU); - if(b&&tEU<=0)tEU=3; - return tEU * aBaseEff / 10000; - } + @Override + int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff) { + int tEU = 0; + + int actualOptimalFlow = 0; + + if (aFluids.size() >= 1) { + FluidStack firstFuelType = new FluidStack(aFluids.get(0), 0); // Identify a SINGLE type of fluid to process. Doesn't matter which one. Ignore the rest! + int fuelValue = getFuelValue(firstFuelType); + actualOptimalFlow = (int) (aOptFlow / fuelValue); + + int remainingFlow = (int) (actualOptimalFlow * 1.25f); // Allowed to use up to 125% of optimal flow. Variable required outside of loop for multi-hatch scenarios. + int flow = 0; + int totalFlow = 0; + + for (int i = 0; i < aFluids.size(); i++) { + if (aFluids.get(i).isFluidEqual(firstFuelType)) { + flow = aFluids.get(i).amount; // Get all (steam) in hatch + flow = Math.min(flow, Math.min(remainingFlow, (int) (actualOptimalFlow * 1.25f))); // try to use up to 125% of optimal flow w/o exceeding remainingFlow + depleteInput(new FluidStack(aFluids.get(i), flow)); // deplete that amount + remainingFlow -= flow; // track amount we're allowed to continue depleting from hatches + totalFlow += flow; // track total input used + } + } + + tEU = (int) (Math.min((float) actualOptimalFlow, totalFlow) * fuelValue); + + if (totalFlow != actualOptimalFlow) { + float efficiency = 1.0f - Math.abs(((totalFlow - (float) actualOptimalFlow) / actualOptimalFlow)); + if (efficiency < 0) + efficiency = 0; // Can happen with really ludicrously poor inefficiency. + tEU *= efficiency; + tEU = Math.max(1, tEU * aBaseEff / 10000); + } else { + tEU = tEU * aBaseEff / 10000; + } + + return tEU; + + } + return 0; + } } diff --git a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_HPSteam.java b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_HPSteam.java index 4acae119..cc881a19 100644 --- a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_HPSteam.java +++ b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_HPSteam.java @@ -56,24 +56,35 @@ public class GT_MetaTileEntity_LargeTurbine_HPSteam extends GT_MetaTileEntity_La return 0; } - @Override - int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff) { - int tEU=0; - int tOut=0; - for(int i=0;i0&&tOut aFluids, int aOptFlow, int aBaseEff) { + int tEU = 0; + int totalFlow = 0; // Byproducts are based on actual flow + int flow = 0; + int remainingFlow = (int) (aOptFlow * 1.25f); // Allowed to use up to 125% of optimal flow + + for (int i = 0; i < aFluids.size() && remainingFlow > 0; i++) { + if (aFluids.get(i).getFluid().getUnlocalizedName(aFluids.get(i)).equals("ic2.fluidSuperheatedSteam")) { + flow = aFluids.get(i).amount; // Get all (steam) in hatch + flow = Math.min(flow, Math.min(remainingFlow, (int) (aOptFlow * 1.25f))); // try to use up to 125% of optimal flow w/o exceeding remainingFlow + depleteInput(new FluidStack(aFluids.get(i), flow)); // deplete that amount + remainingFlow -= flow; // track amount we're allowed to keep depleting from hatches + totalFlow += flow; // track total used + } + } + + tEU = (int) (Math.min((float) aOptFlow, totalFlow)); + addOutput(GT_ModHandler.getSteam(totalFlow)); + if (totalFlow > 0 && totalFlow != aOptFlow) { + float efficiency = 1.0f - Math.abs(((totalFlow - (float) aOptFlow) / aOptFlow)); + tEU *= efficiency; + tEU = Math.max(1, tEU * aBaseEff / 10000); + } else { + tEU = tEU * aBaseEff / 10000; + } + + return tEU; + } } diff --git a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Plasma.java b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Plasma.java index b1916b43..ceed528b 100644 --- a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Plasma.java +++ b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Plasma.java @@ -68,22 +68,50 @@ public class GT_MetaTileEntity_LargeTurbine_Plasma extends GT_MetaTileEntity_Lar return 0; } - @Override - int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff) { - int tEU=0; - int tOut=0; - int tOptFlow = aOptFlow * 40; - boolean b = false; - for(int i=0;i0&&depleteInput(new FluidStack(aFluids.get(i),Math.max(tOptFlow/(fuelValue*2),1)))){ - tEU += tOptFlow/2;} - } - if(tEU>0)b=true; - tEU = getAverage(tEU); - if(b&&tEU<=0)tEU=3; - return tEU * aBaseEff / 10000; - } + @Override + int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff) { + + aOptFlow *= 40; + int tEU = 0; + + int actualOptimalFlow = 0; + + if (aFluids.size() >= 1) { + FluidStack firstFuelType = new FluidStack(aFluids.get(0), 0); // Identify a SINGLE type of fluid to process. Doesn't matter which one. Ignore the rest! + int fuelValue = getFuelValue(firstFuelType); + actualOptimalFlow = (int) (aOptFlow / fuelValue); + + int remainingFlow = (int) (actualOptimalFlow * 1.25f); // Allowed to use up to 125% of optimal flow. Variable required outside of loop for multi-hatch scenarios. + int flow = 0; + int totalFlow = 0; + + for (int i = 0; i < aFluids.size(); i++) { + if (aFluids.get(i).isFluidEqual(firstFuelType)) { + flow = aFluids.get(i).amount; // Get all (steam) in hatch + flow = Math.min(flow, Math.min(remainingFlow, (int) (actualOptimalFlow * 1.25f))); // try to use up to 125% of optimal flow w/o exceeding remainingFlow + depleteInput(new FluidStack(aFluids.get(i), flow)); // deplete that amount + remainingFlow -= flow; // track amount we're allowed to continue depleting from hatches + totalFlow += flow; // track total input used + } + } + + tEU = (int) (Math.min((float) actualOptimalFlow, totalFlow) * fuelValue); + + if (totalFlow != actualOptimalFlow) { + float efficiency = 1.0f - Math.abs(((totalFlow - (float) actualOptimalFlow) / actualOptimalFlow)); + if (efficiency < 0) + efficiency = 0; // Can happen with really ludicrously poor inefficiency. + tEU *= efficiency; + tEU = Math.max(1, tEU * aBaseEff / 10000); + } else { + tEU = tEU * aBaseEff / 10000; + } + + return tEU; + + } + return 0; + } } diff --git a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Steam.java b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Steam.java index 46f15130..535069b3 100644 --- a/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Steam.java +++ b/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_LargeTurbine_Steam.java @@ -64,35 +64,35 @@ public class GT_MetaTileEntity_LargeTurbine_Steam extends GT_MetaTileEntity_Larg return usage; } - @Override - int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff) { - int tEU=0; - int averageFlow = 0; // To prevent closed water loops from breaking. EU is based on average flow - int totalFlow = 0; // Byproducts are based on actual flow - int flow = 0; - int remainingFlow = (int)(aOptFlow * 1.25f); // Allowed to use up to 125% of optimal flow. Variable required outside of loop for multi-hatch scenarios. + @Override + int fluidIntoPower(ArrayList aFluids, int aOptFlow, int aBaseEff) { + int tEU = 0; + int totalFlow = 0; // Byproducts are based on actual flow + int flow = 0; + int remainingFlow = (int) (aOptFlow * 1.25f); // Allowed to use up to 125% of optimal flow. Variable required outside of loop for multi-hatch scenarios. - for(int i=0;i 0;i++){ // loop through each hatch; extract inputs and track totals. - if(aFluids.get(i).getFluid().getUnlocalizedName(aFluids.get(i)).equals("fluid.steam")||aFluids.get(i).getFluid().getUnlocalizedName(aFluids.get(i)).equals("ic2.fluidSteam")){ - flow = aFluids.get(i).amount; // Get all (steam) in hatch - flow = Math.min(flow, Math.min(remainingFlow, (int)( aOptFlow * 1.25f))); // try to use up to 125% of optimal flow w/o exceeding remainingFlow - depleteInput(new FluidStack(aFluids.get(i), flow)); // deplete that amount - remainingFlow -= flow; // track amount we're allowed to continue depleting from hatches - totalFlow += flow; // track total input used - } - } - averageFlow = getAverage(totalFlow); // calculate recent average usage for power output purposes but NOT byproduct generation. We used what we used, and get byproducts from that. - - tEU = Math.min(aOptFlow, averageFlow); - addOutput(GT_ModHandler.getDistilledWater(useWater(totalFlow/160.0f))); - if(averageFlow > 0 && averageFlow != aOptFlow){ - float efficiency = 1.0f - Math.abs(((averageFlow - (float)aOptFlow) / aOptFlow)); - tEU *= efficiency; - tEU = Math.max(1, tEU * aBaseEff / 20000); - } - else { - tEU = tEU * aBaseEff / 20000; - } - return tEU; - } + for (int i = 0; i < aFluids.size() && remainingFlow > 0; i++) { // loop through each hatch; extract inputs and track totals. + if (aFluids.get(i).getFluid().getUnlocalizedName(aFluids.get(i)).equals("fluid.steam") + || aFluids.get(i).getFluid().getUnlocalizedName(aFluids.get(i)).equals("ic2.fluidSteam")) { + flow = aFluids.get(i).amount; // Get all (steam) in hatch + flow = Math.min(flow, Math.min(remainingFlow, (int) (aOptFlow * 1.25f))); // try to use up to 125% of optimal flow w/o exceeding remainingFlow + depleteInput(new FluidStack(aFluids.get(i), flow)); // deplete that amount + remainingFlow -= flow; // track amount we're allowed to continue depleting from hatches + totalFlow += flow; // track total input used + } + } + + tEU = (int) (Math.min((float) aOptFlow, totalFlow)); + int waterToOutput = useWater(totalFlow / 160.0f); + addOutput(GT_ModHandler.getDistilledWater(waterToOutput)); + if (totalFlow > 0 && totalFlow != aOptFlow) { + float efficiency = 1.0f - Math.abs(((totalFlow - (float) aOptFlow) / aOptFlow)); + tEU *= efficiency; + tEU = Math.max(1, tEU * aBaseEff / 20000); + } else { + tEU = tEU * aBaseEff / 20000; + } + + return tEU; + } }