-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathFuelRecipeLogic.java
executable file
·315 lines (273 loc) · 11.1 KB
/
FuelRecipeLogic.java
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
package gregtech.api.capability.impl;
import gregtech.api.GTValues;
import gregtech.api.capability.*;
import gregtech.api.metatileentity.MTETrait;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.recipes.machines.FuelRecipeMap;
import gregtech.api.recipes.recipes.FuelRecipe;
import gregtech.api.util.GTUtility;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.function.Supplier;
public class FuelRecipeLogic extends MTETrait implements IControllable, IFuelable, IThrottleable {
public final FuelRecipeMap recipeMap;
protected FuelRecipe previousRecipe;
protected final Supplier<IEnergyContainer> energyContainer;
protected final Supplier<IMultipleTankHandler> fluidTank;
public final long maxVoltage;
private int recipeDurationLeft;
private long recipeOutputVoltage;
private boolean isActive;
private boolean workingEnabled = true;
private boolean wasActiveAndNeedsUpdate = false;
private IThrottle throttle = IThrottle.FULL_THROTTLE;
public FuelRecipeLogic(MetaTileEntity metaTileEntity, FuelRecipeMap recipeMap, Supplier<IEnergyContainer> energyContainer, Supplier<IMultipleTankHandler> fluidTank, long maxVoltage) {
super(metaTileEntity);
this.recipeMap = recipeMap;
this.energyContainer = energyContainer;
this.fluidTank = fluidTank;
this.maxVoltage = maxVoltage;
}
@Override
public IThrottle getThrottle() {
return this.throttle;
}
@Override
public void setThrottle(final IThrottle throttle) {
this.throttle = throttle;
}
public long getRecipeOutputVoltage() {
return recipeOutputVoltage;
}
@Override
public String getName() {
return "FuelRecipeMapWorkable";
}
@Override
public int getNetworkID() {
return TraitNetworkIds.TRAIT_ID_WORKABLE;
}
@Override
public Collection<IFuelInfo> getFuels() {
if (!isReadyForRecipes())
return Collections.emptySet();
final IMultipleTankHandler fluidTanks = this.fluidTank.get();
if (fluidTanks == null)
return Collections.emptySet();
final LinkedHashMap<String, IFuelInfo> fuels = new LinkedHashMap<>();
// Fuel capacity is all tanks
int fuelCapacity = 0;
for (IFluidTank fluidTank : fluidTanks) {
fuelCapacity += fluidTank.getCapacity();
}
for (IFluidTank fluidTank : fluidTanks) {
final FluidStack tankContents = fluidTank.drain(Integer.MAX_VALUE, false);
if (tankContents == null || tankContents.amount <= 0)
continue;
int fuelRemaining = tankContents.amount;
FuelRecipe recipe = findRecipe(tankContents);
if (recipe == null)
continue;
int amountPerRecipe = (int) (calculateFuelAmount(recipe) * this.throttle.calculateConsumptionMultiplier());
int duration = (int) (calculateRecipeDuration(recipe) * this.throttle.calculateDurationMultiplier());
int fuelBurnTime = duration * fuelRemaining / amountPerRecipe;
FluidFuelInfo fuelInfo = (FluidFuelInfo) fuels.get(tankContents.getUnlocalizedName());
if (fuelInfo == null) {
fuelInfo = new FluidFuelInfo(tankContents, fuelRemaining, fuelCapacity, amountPerRecipe, fuelBurnTime);
fuels.put(tankContents.getUnlocalizedName(), fuelInfo);
}
else {
fuelInfo.addFuelRemaining(fuelRemaining);
fuelInfo.addFuelBurnTime(fuelBurnTime);
}
}
return fuels.values();
}
@Override
public <T> T getCapability(Capability<T> capability) {
if(capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}
if(capability == GregtechCapabilities.CAPABILITY_FUELABLE) {
return GregtechCapabilities.CAPABILITY_FUELABLE.cast(this);
}
if (capability == GregtechCapabilities.CAPABILITY_THROTTLEABLE) {
return GregtechCapabilities.CAPABILITY_THROTTLEABLE.cast(this);
}
return null;
}
@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;
}
}
}
if (recipeDurationLeft == 0 && isReadyForRecipes()) {
tryAcquireNewRecipe();
}
}
if (wasActiveAndNeedsUpdate) {
setActive(false);
this.wasActiveAndNeedsUpdate = false;
}
}
protected boolean isReadyForRecipes() {
return true;
}
protected boolean shouldVoidExcessiveEnergy() {
return false;
}
private void tryAcquireNewRecipe() {
IMultipleTankHandler fluidTanks = this.fluidTank.get();
for (IFluidTank fluidTank : fluidTanks) {
FluidStack tankContents = fluidTank.getFluid();
if (tankContents != null && tankContents.amount > 0) {
int fuelAmountUsed = tryAcquireNewRecipe(tankContents);
if (fuelAmountUsed > 0) {
fluidTank.drain(fuelAmountUsed, true);
break; //recipe is found and ready to use
}
}
}
}
public boolean isActive() {
return isActive;
}
private int tryAcquireNewRecipe(FluidStack fluidStack) {
FuelRecipe currentRecipe;
if (previousRecipe != null && previousRecipe.matches(getMaxVoltage(), fluidStack)) {
//if previous recipe still matches inputs, try to use it
currentRecipe = previousRecipe;
} else {
//else, try searching new recipe for given inputs
currentRecipe = recipeMap.findRecipe(getMaxVoltage(), fluidStack);
//if we found recipe that can be buffered, buffer it
if (currentRecipe != null) {
this.previousRecipe = currentRecipe;
}
}
if (currentRecipe != null && checkRecipe(currentRecipe)) {
int fuelAmountToUse = (int) (calculateFuelAmount(currentRecipe) * this.throttle.calculateConsumptionMultiplier());
if (fluidStack.amount >= fuelAmountToUse) {
this.recipeDurationLeft = (int) (calculateRecipeDuration(currentRecipe) * this.throttle.calculateDurationMultiplier());
this.recipeOutputVoltage = (long) (startRecipe(currentRecipe, fuelAmountToUse, this.recipeDurationLeft) * this.throttle.calculateOutputVoltageMultiplier());
if (wasActiveAndNeedsUpdate) {
this.wasActiveAndNeedsUpdate = false;
} else {
setActive(true);
}
return fuelAmountToUse;
}
}
return 0;
}
// Similar to tryAcquire but with no side effects
private FuelRecipe findRecipe(FluidStack fluidStack) {
FuelRecipe currentRecipe;
if (previousRecipe != null && previousRecipe.matches(getMaxVoltage(), fluidStack)) {
currentRecipe = previousRecipe;
} else {
currentRecipe = recipeMap.findRecipe(getMaxVoltage(), fluidStack);
}
if (currentRecipe != null && checkRecipe(currentRecipe))
return currentRecipe;
return null;
}
protected boolean checkRecipe(FuelRecipe recipe) {
return true;
}
public long getMaxVoltage() {
return maxVoltage;
}
protected int calculateFuelAmount(FuelRecipe currentRecipe) {
return currentRecipe.getRecipeFluid().amount * getVoltageMultiplier(getMaxVoltage(), currentRecipe.getMinVoltage());
}
protected int calculateRecipeDuration(FuelRecipe currentRecipe) {
return currentRecipe.getDuration();
}
/**
* Performs preparations for starting given recipe and determines it's output voltage
*
* @return recipe's output voltage
*/
protected long startRecipe(FuelRecipe currentRecipe, int fuelAmountUsed, int recipeDuration) {
return getMaxVoltage();
}
public static int getVoltageMultiplier(long maxVoltage, long minVoltage) {
return (int) (maxVoltage / minVoltage);
}
public static long getTieredVoltage(long voltage) {
return GTValues.V[GTUtility.getTierByVoltage(voltage)];
}
protected void setActive(boolean active) {
// If we are changing states (active -> inactive), clear remaining recipe duration
if (this.isActive && !active) {
recipeDurationLeft = 0;
}
this.isActive = active;
if (!metaTileEntity.getWorld().isRemote) {
metaTileEntity.markDirty();
writeCustomData(1, buf -> buf.writeBoolean(active));
}
}
@Override
public void setWorkingEnabled(boolean workingEnabled) {
this.workingEnabled = workingEnabled;
metaTileEntity.markDirty();
}
@Override
public boolean isWorkingEnabled() {
return workingEnabled;
}
@Override
public void receiveCustomData(int dataId, PacketBuffer buf) {
if (dataId == 1) {
this.isActive = buf.readBoolean();
getMetaTileEntity().getHolder().scheduleChunkForRenderUpdate();
}
}
@Override
public void writeInitialData(PacketBuffer buf) {
buf.writeBoolean(this.isActive);
}
@Override
public void receiveInitialData(PacketBuffer buf) {
this.isActive = buf.readBoolean();
}
@Override
public NBTTagCompound serializeNBT() {
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("WorkEnabled", this.workingEnabled);
compound.setInteger("RecipeDurationLeft", this.recipeDurationLeft);
if (recipeDurationLeft > 0) {
compound.setLong("RecipeOutputVoltage", this.recipeOutputVoltage);
}
return compound;
}
@Override
public void deserializeNBT(NBTTagCompound compound) {
if (!compound.hasKey("WorkEnabled", NBT.TAG_BYTE)) {
//change working mode only if there is a tag compound with it's value
this.workingEnabled = compound.getBoolean("WorkEnabled");
}
this.recipeDurationLeft = compound.getInteger("RecipeDurationLeft");
if (recipeDurationLeft > 0) {
this.recipeOutputVoltage = compound.getLong("RecipeOutputVoltage");
}
this.isActive = recipeDurationLeft > 0;
}
}