mmb.content.electric.machines.FuelBurner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multimachinebuilder Show documentation
Show all versions of multimachinebuilder Show documentation
Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world.
THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<
/**
*
*/
package mmb.content.electric.machines;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import mmb.content.electric.Battery;
import mmb.engine.inv.Inventory;
import mmb.engine.inv.ItemRecord;
import mmb.engine.item.ItemEntry;
/**
* @author oskar
* A helper class to help burn fuels
*/
public class FuelBurner {
public final double mul;
public final Inventory inv;
public final Battery bat;
public final Object2DoubleMap fuels;
/**
* @param mul efficiency multiplier (1 is equal to normal furnace efficiency)
* @param inv the inventory to extract fuels from
* @param bat the target power output
*/
public FuelBurner(double mul, Inventory inv, Battery bat, Object2DoubleMap fuels) {
super();
this.mul = mul;
this.inv = inv;
this.bat = bat;
this.fuels = fuels;
}
/**
* Polls the inventory and burns any fuels within
*/
public void cycle(){
for(ItemRecord ir: inv) {
if(ir.amount() == 0) continue; //The item record is empty
double fuelEnergy = fuels.getOrDefault(ir.item(), Double.NaN)*mul; //Fuel energy in joules
if(Double.isNaN(fuelEnergy)) continue; //The item is not a fuel
double remainCharge = bat.remain();
double fuelCharge = fuelEnergy/bat.voltage.volts; //Fuel energy in coulombs
if(remainCharge < fuelCharge) return; //There is not enough room for the fuel
int extract = ir.extract(1);
if(extract == 0) continue; //The extraction failed
bat.stored += fuelCharge;
}
}
}