Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FuelRecipeLogic Rework #1632

Closed
wants to merge 16 commits into from
Closed
114 changes: 89 additions & 25 deletions src/main/java/gregtech/api/capability/impl/FuelRecipeLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class FuelRecipeLogic extends MTETrait implements IControllable, IFuelabl

private int recipeDurationLeft;
private long recipeOutputVoltage;
private long outputVoltage;
private boolean canProduceEnergy;
private boolean canProgress;
private int recipeDuration;
private int fuelAmount;

private boolean isActive;
private boolean workingEnabled = true;
Expand All @@ -43,8 +48,25 @@ public FuelRecipeLogic(MetaTileEntity metaTileEntity, FuelRecipeMap recipeMap, S
this.maxVoltage = maxVoltage;
}

/**
* Deprecated please use {@link #getOutputVoltage} to get the output voltage.
* overrides of this method should get moved to {@link #calculateRecipeOutputVoltage()}
*/
@Deprecated
public long getRecipeOutputVoltage() {
return recipeOutputVoltage;
return this.outputVoltage;
}

public long getOutputVoltage() {
return this.outputVoltage;
}

public boolean hasProducedEnergy() {
return this.canProduceEnergy;
}

public boolean hasRecipeProgressed() {
return this.canProgress;
}

@Override
Expand Down Expand Up @@ -88,8 +110,7 @@ public Collection<IFuelInfo> getFuels() {
if (fuelInfo == null) {
fuelInfo = new FluidFuelInfo(tankContents, fuelRemaining, fuelCapacity, amountPerRecipe, fuelBurnTime);
fuels.put(tankContents.getUnlocalizedName(), fuelInfo);
}
else {
} else {
fuelInfo.addFuelRemaining(fuelRemaining);
fuelInfo.addFuelBurnTime(fuelBurnTime);
}
Expand All @@ -99,10 +120,10 @@ public Collection<IFuelInfo> getFuels() {

@Override
public <T> T getCapability(Capability<T> capability) {
if(capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}
if(capability == GregtechCapabilities.CAPABILITY_FUELABLE) {
if (capability == GregtechCapabilities.CAPABILITY_FUELABLE) {
return GregtechCapabilities.CAPABILITY_FUELABLE.cast(this);
}
return null;
Expand All @@ -111,18 +132,33 @@ public <T> T getCapability(Capability<T> capability) {
@Override
public void update() {
if (getMetaTileEntity().getWorld().isRemote) return;

if (workingEnabled) {
if (recipeDurationLeft > 0) {
if (energyContainer.get().getEnergyCanBeInserted() >=
recipeOutputVoltage || shouldVoidExcessiveEnergy()) {
energyContainer.get().addEnergy(recipeOutputVoltage);
if (--this.recipeDurationLeft == 0) {
this.wasActiveAndNeedsUpdate = true;
}
}

/* we need to compute those two before doing anything
* to avoid loosing 1 tick of production
*/

this.canProduceEnergy = canProduceEnergy();

this.canProgress = canRecipeProgress();

if (this.canProgress) {
--this.recipeDurationLeft;
}
if (recipeDurationLeft == 0 && isReadyForRecipes()) {
tryAcquireNewRecipe();

if (this.canProduceEnergy) {
this.outputVoltage = calculateRecipeOutputVoltage();
energyContainer.get().addEnergy(outputVoltage);
}

if (this.recipeDurationLeft <= 0) {
if (isActive) {
this.wasActiveAndNeedsUpdate = true;
}
if (isReadyForRecipes()) {
tryAcquireNewRecipe();
}
}
}
if (wasActiveAndNeedsUpdate) {
Expand All @@ -132,7 +168,11 @@ public void update() {
}

protected boolean isReadyForRecipes() {
return true;
return true;
}

protected boolean shouldRecipeProgressWhenNotProducingEnergy() {
return false;
}

protected boolean shouldVoidExcessiveEnergy() {
Expand Down Expand Up @@ -171,16 +211,17 @@ private int tryAcquireNewRecipe(FluidStack fluidStack) {
}
}
if (currentRecipe != null && checkRecipe(currentRecipe)) {
int fuelAmountToUse = calculateFuelAmount(currentRecipe);
if (fluidStack.amount >= fuelAmountToUse) {
this.fuelAmount = calculateFuelAmount(currentRecipe);
if (fluidStack.amount >= this.fuelAmount) {
this.recipeDurationLeft = calculateRecipeDuration(currentRecipe);
this.recipeOutputVoltage = startRecipe(currentRecipe, fuelAmountToUse, recipeDurationLeft);
this.recipeDuration = this.recipeDurationLeft;
this.recipeOutputVoltage = startRecipe(currentRecipe, fuelAmount, recipeDurationLeft);
if (wasActiveAndNeedsUpdate) {
this.wasActiveAndNeedsUpdate = false;
} else {
setActive(true);
}
return fuelAmountToUse;
return this.fuelAmount;
}
}
return 0;
Expand Down Expand Up @@ -211,14 +252,36 @@ protected int calculateFuelAmount(FuelRecipe currentRecipe) {
return currentRecipe.getRecipeFluid().amount * getVoltageMultiplier(getMaxVoltage(), currentRecipe.getMinVoltage());
}

public int getFuelAmount() {
galyfray marked this conversation as resolved.
Show resolved Hide resolved
return this.fuelAmount;
}

protected int calculateRecipeDuration(FuelRecipe currentRecipe) {
return currentRecipe.getDuration();
}

public int getRecipeDuration() {
return this.recipeDuration;
}

public boolean canProduceEnergy() {
return this.recipeDurationLeft > 0 && energyContainer.get().getEnergyCanBeInserted() >= calculateRecipeOutputVoltage() ||
shouldVoidExcessiveEnergy();
}

public boolean canRecipeProgress() {
return this.recipeDurationLeft > 0 && (shouldRecipeProgressWhenNotProducingEnergy() || canProduceEnergy());
}

protected long calculateRecipeOutputVoltage() {
return this.recipeOutputVoltage;
}

/**
* Performs preparations for starting given recipe and determines it's output voltage
* Performs preparations for starting given recipe and determines it's base output voltage
* further computation can be done in {@link #calculateRecipeOutputVoltage}
*
* @return recipe's output voltage
* @return recipe's base output voltage
*/
protected long startRecipe(FuelRecipe currentRecipe, int fuelAmountUsed, int recipeDuration) {
galyfray marked this conversation as resolved.
Show resolved Hide resolved
return getMaxVoltage();
Expand All @@ -244,6 +307,7 @@ protected void setActive(boolean active) {
}
}


@Override
public void setWorkingEnabled(boolean workingEnabled) {
this.workingEnabled = workingEnabled;
Expand Down Expand Up @@ -278,7 +342,7 @@ public NBTTagCompound serializeNBT() {
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("WorkEnabled", this.workingEnabled);
compound.setInteger("RecipeDurationLeft", this.recipeDurationLeft);
if (recipeDurationLeft > 0) {
if (this.recipeDurationLeft > 0) {
compound.setLong("RecipeOutputVoltage", this.recipeOutputVoltage);
}
return compound;
Expand All @@ -291,10 +355,10 @@ public void deserializeNBT(NBTTagCompound compound) {
this.workingEnabled = compound.getBoolean("WorkEnabled");
}
this.recipeDurationLeft = compound.getInteger("RecipeDurationLeft");
if (recipeDurationLeft > 0) {
if (this.recipeDurationLeft > 0) {
this.recipeOutputVoltage = compound.getLong("RecipeOutputVoltage");
}
this.isActive = recipeDurationLeft > 0;
this.isActive = this.recipeDurationLeft > 0;
}

}