Added energy management to turbine and turbine valves

This commit is contained in:
aidancbrady 2016-01-23 00:40:30 -05:00
parent 769a8fd480
commit afe8969c37
10 changed files with 427 additions and 19 deletions

View file

@ -15,7 +15,6 @@ import mekanism.api.EnumColor;
import mekanism.api.IConfigurable;
import mekanism.api.MekanismConfig.general;
import mekanism.api.Range4D;
import mekanism.api.transmitters.IGridTransmitter;
import mekanism.api.transmitters.ITransmitterTile;
import mekanism.common.Mekanism;
import mekanism.common.base.IActiveState;
@ -108,12 +107,6 @@ public class TileEntityInductionPort extends TileEntityInductionCasing implement
return EnumSet.noneOf(ForgeDirection.class);
}
@Override
public boolean canUpdate()
{
return true;
}
@Method(modid = "IC2")
public void register()
{

View file

@ -3,6 +3,7 @@ package mekanism.generators.client;
import mekanism.generators.client.gui.GuiBioGenerator;
import mekanism.generators.client.gui.GuiGasGenerator;
import mekanism.generators.client.gui.GuiHeatGenerator;
import mekanism.generators.client.gui.GuiIndustrialTurbine;
import mekanism.generators.client.gui.GuiNeutronCapture;
import mekanism.generators.client.gui.GuiReactorController;
import mekanism.generators.client.gui.GuiReactorFuel;
@ -102,6 +103,8 @@ public class GeneratorsClientProxy extends GeneratorsCommonProxy
return new GuiBioGenerator(player.inventory, (TileEntityBioGenerator)tileEntity);
case 5:
return new GuiWindGenerator(player.inventory, (TileEntityWindGenerator)tileEntity);
case 6:
return new GuiIndustrialTurbine(player.inventory, (TileEntityTurbineCasing)tileEntity);
case 10:
return new GuiReactorController(player.inventory, (TileEntityReactorController)tileEntity);
case 11:

View file

@ -0,0 +1,20 @@
package mekanism.generators.client.gui;
import mekanism.client.gui.GuiMekanism;
import mekanism.common.inventory.container.ContainerFilter;
import mekanism.generators.common.tile.turbine.TileEntityTurbineCasing;
import net.minecraft.entity.player.InventoryPlayer;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class GuiIndustrialTurbine extends GuiMekanism
{
public TileEntityTurbineCasing tileEntity;
public GuiIndustrialTurbine(InventoryPlayer inventory, TileEntityTurbineCasing tentity)
{
super(tentity, new ContainerFilter(inventory, tentity));
tileEntity = tentity;
}
}

View file

@ -33,7 +33,7 @@ public class RenderIndustrialTurbine extends TileEntitySpecialRenderer
public void renderAModelAt(TileEntityTurbineCasing tileEntity, double x, double y, double z, float partialTick)
{
if(tileEntity.clientHasStructure && tileEntity.isRendering && tileEntity.structure != null /* && there is fluid */)
if(tileEntity.clientHasStructure && tileEntity.isRendering && tileEntity.structure != null && tileEntity.structure.fluidStored != null && tileEntity.structure.fluidStored.amount != 0)
{
RenderData data = new RenderData();
@ -43,6 +43,26 @@ public class RenderIndustrialTurbine extends TileEntitySpecialRenderer
data.width = tileEntity.structure.volWidth;
bindTexture(MekanismRenderer.getBlocksTexture());
if(data.location != null && data.height >= 3 && tileEntity.structure.fluidStored.getFluid() != null)
{
push();
GL11.glTranslated(getX(data.location.xCoord), getY(data.location.yCoord), getZ(data.location.zCoord));
MekanismRenderer.glowOn(tileEntity.structure.fluidStored.getFluid().getLuminosity());
MekanismRenderer.colorFluid(tileEntity.structure.fluidStored.getFluid());
DisplayInteger[] displayList = getListAndRender(data, tileEntity.getWorldObj());
GL11.glColor4f(1F, 1F, 1F, Math.min(1, ((float)tileEntity.structure.fluidStored.amount / (float)tileEntity.structure.getFluidCapacity())+0.3F));
displayList[getStages(data.height)-1].render();
MekanismRenderer.glowOff();
MekanismRenderer.resetColor();
pop();
}
}
}

View file

@ -2,6 +2,7 @@ package mekanism.generators.common;
import mekanism.api.MekanismConfig.generators;
import mekanism.common.Mekanism;
import mekanism.common.inventory.container.ContainerFilter;
import mekanism.common.inventory.container.ContainerNull;
import mekanism.common.tile.TileEntityContainerBlock;
import mekanism.generators.common.inventory.container.ContainerBioGenerator;
@ -172,6 +173,8 @@ public class GeneratorsCommonProxy
return new ContainerBioGenerator(player.inventory, (TileEntityBioGenerator)tileEntity);
case 5:
return new ContainerWindGenerator(player.inventory, (TileEntityWindGenerator)tileEntity);
case 6:
return new ContainerFilter(player.inventory, (TileEntityTurbineCasing)tileEntity);
case 10:
return new ContainerReactorController(player.inventory, (TileEntityReactorController)tileEntity);
case 11:

View file

@ -9,6 +9,8 @@ public class SynchronizedTurbineData extends SynchronizedData<SynchronizedTurbin
public FluidStack prevFluid;
public double electricityStored;
public int blades;
public int vents;
public int coils;
@ -20,6 +22,11 @@ public class SynchronizedTurbineData extends SynchronizedData<SynchronizedTurbin
return lowerVolume*TurbineUpdateProtocol.FLUID_PER_TANK;
}
public double getEnergyCapacity()
{
return volume*16000000; //16 MJ energy capacity per volume
}
public boolean needsRenderUpdate()
{
if((fluidStored == null && prevFluid != null) || (fluidStored != null && prevFluid == null))

View file

@ -7,17 +7,20 @@ import net.minecraftforge.fluids.FluidStack;
public class TurbineCache extends MultiblockCache<SynchronizedTurbineData>
{
public FluidStack fluid;
public double electricity;
@Override
public void apply(SynchronizedTurbineData data)
{
data.fluidStored = fluid;
data.electricityStored = electricity;
}
@Override
public void sync(SynchronizedTurbineData data)
{
fluid = data.fluidStored;
electricity = data.electricityStored;
}
@Override
@ -27,6 +30,8 @@ public class TurbineCache extends MultiblockCache<SynchronizedTurbineData>
{
fluid = FluidStack.loadFluidStackFromNBT(nbtTags.getCompoundTag("cachedFluid"));
}
electricity = nbtTags.getDouble("electricity");
}
@Override
@ -36,5 +41,7 @@ public class TurbineCache extends MultiblockCache<SynchronizedTurbineData>
{
nbtTags.setTag("cachedFluid", fluid.writeToNBT(new NBTTagCompound()));
}
nbtTags.setDouble("electricity", electricity);
}
}

View file

@ -255,6 +255,8 @@ public class TurbineUpdateProtocol extends UpdateProtocol<SynchronizedTurbineDat
{
structureFound.fluidStored.amount = Math.min(structureFound.fluidStored.amount, structureFound.getFluidCapacity());
}
structureFound.electricityStored = Math.min(structureFound.electricityStored, structureFound.getEnergyCapacity());
}
public class CoilCounter

View file

@ -6,13 +6,14 @@ import java.util.ArrayList;
import mekanism.api.Coord4D;
import mekanism.api.Range4D;
import mekanism.api.energy.IStrictEnergyStorage;
import mekanism.common.Mekanism;
import mekanism.common.content.tank.TankUpdateProtocol;
import mekanism.common.multiblock.MultiblockCache;
import mekanism.common.multiblock.MultiblockManager;
import mekanism.common.multiblock.UpdateProtocol;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.tile.TileEntityMultiblock;
import mekanism.common.util.MekanismUtils;
import mekanism.generators.common.MekanismGenerators;
import mekanism.generators.common.content.turbine.SynchronizedTurbineData;
import mekanism.generators.common.content.turbine.TurbineCache;
@ -21,10 +22,8 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
public class TileEntityTurbineCasing extends TileEntityMultiblock<SynchronizedTurbineData>
public class TileEntityTurbineCasing extends TileEntityMultiblock<SynchronizedTurbineData> implements IStrictEnergyStorage
{
public int clientCapacity;
public float prevScale;
public TileEntityTurbineCasing()
@ -46,7 +45,7 @@ public class TileEntityTurbineCasing extends TileEntityMultiblock<SynchronizedTu
{
if(structure != null && clientHasStructure && isRendering)
{
float targetScale = (float)(structure.fluidStored != null ? structure.fluidStored.amount : 0)/clientCapacity;
float targetScale = (float)(structure.fluidStored != null ? structure.fluidStored.amount : 0)/structure.getFluidCapacity();
if(Math.abs(prevScale - targetScale) > 0.01)
{
@ -84,7 +83,7 @@ public class TileEntityTurbineCasing extends TileEntityMultiblock<SynchronizedTu
if(!player.isSneaking() && structure != null)
{
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(this)));
//player.openGui(Mekanism.instance, 49, worldObj, xCoord, yCoord, zCoord);
player.openGui(Mekanism.instance, 6, worldObj, xCoord, yCoord, zCoord);
return true;
}
@ -92,14 +91,36 @@ public class TileEntityTurbineCasing extends TileEntityMultiblock<SynchronizedTu
return false;
}
@Override
public double getEnergy()
{
return structure != null ? structure.electricityStored : 0;
}
@Override
public double getMaxEnergy()
{
return structure.getEnergyCapacity();
}
@Override
public void setEnergy(double energy)
{
if(structure != null)
{
structure.electricityStored = Math.max(Math.min(energy, getMaxEnergy()), 0);
MekanismUtils.saveChunk(this);
}
}
public int getScaledFluidLevel(int i)
{
if(clientCapacity == 0 || structure.fluidStored == null)
if(structure.getFluidCapacity() == 0 || structure.fluidStored == null)
{
return 0;
}
return structure.fluidStored.amount*i / clientCapacity;
return structure.fluidStored.amount*i / structure.getFluidCapacity();
}
@Override
@ -109,7 +130,8 @@ public class TileEntityTurbineCasing extends TileEntityMultiblock<SynchronizedTu
if(structure != null)
{
data.add(structure.getFluidCapacity());
data.add(structure.volume);
data.add(structure.electricityStored);
if(structure.fluidStored != null)
{
@ -132,7 +154,8 @@ public class TileEntityTurbineCasing extends TileEntityMultiblock<SynchronizedTu
if(clientHasStructure)
{
clientCapacity = dataStream.readInt();
structure.volume = dataStream.readInt();
structure.electricityStored = dataStream.readDouble();
if(dataStream.readInt() == 1)
{

View file

@ -1,17 +1,44 @@
package mekanism.generators.common.tile.turbine;
import ic2.api.energy.EnergyNet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergyConductor;
import ic2.api.energy.tile.IEnergyTile;
import java.util.EnumSet;
import mekanism.api.Coord4D;
import mekanism.api.MekanismConfig.general;
import mekanism.api.transmitters.ITransmitterTile;
import mekanism.common.base.IEnergyWrapper;
import mekanism.common.util.CableUtils;
import mekanism.common.util.LangUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.PipeUtils;
import mekanism.generators.common.content.turbine.TurbineFluidTank;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import cpw.mods.fml.common.Optional.Interface;
import cpw.mods.fml.common.Optional.InterfaceList;
import cpw.mods.fml.common.Optional.Method;
public class TileEntityTurbineValve extends TileEntityTurbineCasing implements IFluidHandler
@InterfaceList({
@Interface(iface = "ic2.api.energy.tile.IEnergySink", modid = "IC2"),
@Interface(iface = "ic2.api.energy.tile.IEnergySource", modid = "IC2"),
@Interface(iface = "ic2.api.tile.IEnergyStorage", modid = "IC2"),
@Interface(iface = "cofh.api.energy.IEnergyHandler", modid = "CoFHCore"),
})
public class TileEntityTurbineValve extends TileEntityTurbineCasing implements IFluidHandler, IEnergyWrapper
{
public boolean ic2Registered = false;
public TurbineFluidTank fluidTank;
public TileEntityTurbineValve()
@ -19,6 +46,309 @@ public class TileEntityTurbineValve extends TileEntityTurbineCasing implements I
super("TurbineValve");
fluidTank = new TurbineFluidTank(this);
}
@Override
public void onUpdate()
{
super.onUpdate();
if(!ic2Registered && MekanismUtils.useIC2())
{
register();
}
if(!worldObj.isRemote)
{
if(structure != null)
{
double prev = getEnergy();
CableUtils.emit(this);
}
}
}
@Override
public EnumSet<ForgeDirection> getOutputtingSides()
{
if(structure != null)
{
EnumSet set = EnumSet.allOf(ForgeDirection.class);
set.remove(ForgeDirection.UNKNOWN);
for(ForgeDirection side : ForgeDirection.VALID_DIRECTIONS)
{
if(structure.locations.contains(Coord4D.get(this).getFromSide(side)))
{
set.remove(side);
}
}
return set;
}
return EnumSet.noneOf(ForgeDirection.class);
}
@Override
public EnumSet<ForgeDirection> getConsumingSides()
{
return EnumSet.noneOf(ForgeDirection.class);
}
@Override
public boolean canUpdate()
{
return true;
}
@Method(modid = "IC2")
public void register()
{
if(!worldObj.isRemote)
{
TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord);
if(registered != this)
{
if(registered instanceof IEnergyTile)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered));
}
else if(registered == null)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
ic2Registered = true;
}
}
}
}
@Method(modid = "IC2")
public void deregister()
{
if(!worldObj.isRemote)
{
TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord);
if(registered instanceof IEnergyTile)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered));
}
}
}
@Override
public double getMaxOutput()
{
return structure != null ? structure.getEnergyCapacity() : 0;
}
@Override
public void onAdded()
{
super.onAdded();
if(MekanismUtils.useIC2())
{
register();
}
}
@Override
public void onChunkUnload()
{
if(MekanismUtils.useIC2())
{
deregister();
}
super.onChunkUnload();
}
@Override
public void invalidate()
{
super.invalidate();
if(MekanismUtils.useIC2())
{
deregister();
}
}
@Override
@Method(modid = "CoFHCore")
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate)
{
return 0;
}
@Override
@Method(modid = "CoFHCore")
public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate)
{
if(getOutputtingSides().contains(from))
{
double toSend = Math.min(getEnergy(), Math.min(getMaxOutput(), maxExtract*general.FROM_TE));
if(!simulate)
{
setEnergy(getEnergy() - toSend);
}
return (int)Math.round(toSend*general.TO_TE);
}
return 0;
}
@Override
@Method(modid = "CoFHCore")
public boolean canConnectEnergy(ForgeDirection from)
{
return structure != null;
}
@Override
@Method(modid = "CoFHCore")
public int getEnergyStored(ForgeDirection from)
{
return (int)Math.round(getEnergy()*general.TO_TE);
}
@Override
@Method(modid = "CoFHCore")
public int getMaxEnergyStored(ForgeDirection from)
{
return (int)Math.round(getMaxEnergy()*general.TO_TE);
}
@Override
@Method(modid = "IC2")
public int getSinkTier()
{
return 4;
}
@Override
@Method(modid = "IC2")
public int getSourceTier()
{
return 4;
}
@Override
@Method(modid = "IC2")
public void setStored(int energy)
{
setEnergy(energy*general.FROM_IC2);
}
@Override
@Method(modid = "IC2")
public int addEnergy(int amount)
{
return (int)Math.round(getEnergy()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public boolean isTeleporterCompatible(ForgeDirection side)
{
return canOutputTo(side);
}
@Override
public boolean canOutputTo(ForgeDirection side)
{
return getOutputtingSides().contains(side);
}
@Override
@Method(modid = "IC2")
public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction)
{
return false;
}
@Override
@Method(modid = "IC2")
public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction)
{
return getOutputtingSides().contains(direction) && receiver instanceof IEnergyConductor;
}
@Override
@Method(modid = "IC2")
public int getStored()
{
return (int)Math.round(getEnergy()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public int getCapacity()
{
return (int)Math.round(getMaxEnergy()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public int getOutput()
{
return (int)Math.round(getMaxOutput()*general.TO_IC2);
}
@Override
@Method(modid = "IC2")
public double getDemandedEnergy()
{
return 0;
}
@Override
@Method(modid = "IC2")
public double getOfferedEnergy()
{
return Math.min(getEnergy(), getMaxOutput())*general.TO_IC2;
}
@Override
public boolean canReceiveEnergy(ForgeDirection side)
{
return false;
}
@Override
@Method(modid = "IC2")
public double getOutputEnergyUnitsPerTick()
{
return getMaxOutput()*general.TO_IC2;
}
@Override
@Method(modid = "IC2")
public double injectEnergy(ForgeDirection direction, double amount, double voltage)
{
return amount;
}
@Override
@Method(modid = "IC2")
public void drawEnergy(double amount)
{
if(structure != null)
{
double toDraw = Math.min(amount*general.FROM_IC2, getMaxOutput());
setEnergy(Math.max(getEnergy() - toDraw, 0));
}
}
@Override
public double transferEnergyToAcceptor(ForgeDirection side, double amount)
{
return 0;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from)