package com.hbm.tileentity.machine; import java.util.Random; import java.io.IOException; import com.google.gson.JsonObject; import com.google.gson.stream.JsonWriter; import com.hbm.blocks.BlockDummyable; import com.hbm.handler.CompatHandler; import com.hbm.inventory.fluid.FluidType; import com.hbm.inventory.fluid.Fluids; import com.hbm.inventory.fluid.tank.FluidTank; import com.hbm.inventory.fluid.trait.FT_Coolable; import com.hbm.inventory.fluid.trait.FT_Coolable.CoolingType; import com.hbm.main.MainRegistry; import com.hbm.sound.AudioWrapper; import com.hbm.tileentity.IBufPacketReceiver; import com.hbm.tileentity.IFluidCopiable; import com.hbm.tileentity.IConfigurableMachine; import com.hbm.tileentity.TileEntityLoadedBase; import com.hbm.util.CompatEnergyControl; import com.hbm.util.fauxpointtwelve.BlockPos; import com.hbm.util.fauxpointtwelve.DirPos; import api.hbm.energymk2.IEnergyProviderMK2; import api.hbm.fluid.IFluidStandardTransceiver; import api.hbm.tile.IInfoProviderEC; import cpw.mods.fml.common.Optional; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import io.netty.buffer.ByteBuf; import li.cil.oc.api.machine.Arguments; import li.cil.oc.api.machine.Callback; import li.cil.oc.api.machine.Context; import li.cil.oc.api.network.SimpleComponent; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.util.ForgeDirection; @Optional.InterfaceList({@Optional.Interface(iface = "li.cil.oc.api.network.SimpleComponent", modid = "OpenComputers")}) public class TileEntityChungus extends TileEntityLoadedBase implements IEnergyProviderMK2, IFluidStandardTransceiver, SimpleComponent, IInfoProviderEC, CompatHandler.OCComponent, IConfigurableMachine, IBufPacketReceiver, IFluidCopiable{ public long power; private int turnTimer; public float rotor; public float lastRotor; public float fanAcceleration = 0F; public FluidTank[] tanks; protected double[] info = new double[3]; private AudioWrapper audio; private float audioDesync; //Configurable values public static long maxPower = 100000000000L; public static int inputTankSize = 1_000_000_000; public static int outputTankSize = 1_000_000_000; public static double efficiency = 0.85D; public TileEntityChungus() { tanks = new FluidTank[2]; tanks[0] = new FluidTank(Fluids.STEAM, inputTankSize); tanks[1] = new FluidTank(Fluids.SPENTSTEAM, outputTankSize); Random rand = new Random(); audioDesync = rand.nextFloat() * 0.05F; } @Override public String getConfigName() { return "steamturbineLeviathan"; } @Override public void readIfPresent(JsonObject obj) { maxPower = IConfigurableMachine.grab(obj, "L:maxPower", maxPower); inputTankSize = IConfigurableMachine.grab(obj, "I:inputTankSize", inputTankSize); outputTankSize = IConfigurableMachine.grab(obj, "I:outputTankSize", outputTankSize); efficiency = IConfigurableMachine.grab(obj, "D:efficiency", efficiency); } @Override public void writeConfig(JsonWriter writer) throws IOException { writer.name("L:maxPower").value(maxPower); writer.name("INFO").value("leviathan steam turbine consumes all availible steam per tick"); writer.name("I:inputTankSize").value(inputTankSize); writer.name("I:outputTankSize").value(outputTankSize); writer.name("D:efficiency").value(efficiency); } @Override public void updateEntity() { if(!worldObj.isRemote) { this.power *= 0.95; this.info = new double[3]; boolean operational = false; FluidType in = tanks[0].getTankType(); boolean valid = false; if(in.hasTrait(FT_Coolable.class)) { FT_Coolable trait = in.getTrait(FT_Coolable.class); double eff = trait.getEfficiency(CoolingType.TURBINE) * efficiency; //85% efficiency by default if(eff > 0) { tanks[1].setTankType(trait.coolsTo); int inputOps = tanks[0].getFill() / trait.amountReq; int outputOps = (tanks[1].getMaxFill() - tanks[1].getFill()) / trait.amountProduced; int ops = Math.min(inputOps, outputOps); tanks[0].setFill(tanks[0].getFill() - ops * trait.amountReq); tanks[1].setFill(tanks[1].getFill() + ops * trait.amountProduced); this.power += (ops * trait.heatEnergy * eff); info[0] = ops * trait.amountReq; info[1] = ops * trait.amountProduced; info[2] = ops * trait.heatEnergy * eff; valid = true; operational = ops > 0; } } if(!valid) tanks[1].setTankType(Fluids.NONE); if(power > maxPower) power = maxPower; ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata() - BlockDummyable.offset); this.tryProvide(worldObj, xCoord - dir.offsetX * 11, yCoord, zCoord - dir.offsetZ * 11, dir.getOpposite()); for(DirPos pos : this.getConPos()) { this.sendFluid(tanks[1], worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir()); this.trySubscribe(tanks[0].getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir()); } if(power > maxPower) power = maxPower; turnTimer--; if(operational) turnTimer = 25; networkPackNT(150); } else { this.lastRotor = this.rotor; this.rotor += this.fanAcceleration; if(this.rotor >= 360) { this.rotor -= 360; this.lastRotor -= 360; } if(turnTimer > 0) { // Fan accelerates with a random offset to ensure the audio doesn't perfectly align, makes for a more pleasant hum this.fanAcceleration = Math.max(0F, Math.min(25F, this.fanAcceleration += 0.075F + audioDesync)); Random rand = worldObj.rand; ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata() - BlockDummyable.offset); ForgeDirection side = dir.getRotation(ForgeDirection.UP); for(int i = 0; i < 10; i++) { worldObj.spawnParticle("cloud", xCoord + 0.5 + dir.offsetX * (rand.nextDouble() + 1.25) + rand.nextGaussian() * side.offsetX * 0.65, yCoord + 2.5 + rand.nextGaussian() * 0.65, zCoord + 0.5 + dir.offsetZ * (rand.nextDouble() + 1.25) + rand.nextGaussian() * side.offsetZ * 0.65, -dir.offsetX * 0.2, 0, -dir.offsetZ * 0.2); } if(audio == null) { audio = MainRegistry.proxy.getLoopedSound("hbm:block.chungusTurbineRunning", xCoord, yCoord, zCoord, 1.0F, 20F, 1.0F); audio.startSound(); } float turbineSpeed = this.fanAcceleration / 25F; audio.updateVolume(getVolume(0.5f * turbineSpeed)); audio.updatePitch(0.25F + 0.75F * turbineSpeed); } else { this.fanAcceleration = Math.max(0F, Math.min(25F, this.fanAcceleration -= 0.1F)); if(audio != null) { if(this.fanAcceleration > 0) { float turbineSpeed = this.fanAcceleration / 25F; audio.updateVolume(getVolume(0.5f * turbineSpeed)); audio.updatePitch(0.25F + 0.75F * turbineSpeed); } else { audio.stopSound(); audio = null; } } } } } public void onLeverPull(FluidType previous) { for(BlockPos pos : getConPos()) { this.tryUnsubscribe(previous, worldObj, pos.getX(), pos.getY(), pos.getZ()); } } public DirPos[] getConPos() { ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata() - BlockDummyable.offset); ForgeDirection rot = dir.getRotation(ForgeDirection.UP); return new DirPos[] { new DirPos(xCoord + dir.offsetX * 5, yCoord + 2, zCoord + dir.offsetZ * 5, dir), new DirPos(xCoord + rot.offsetX * 3, yCoord, zCoord + rot.offsetZ * 3, rot), new DirPos(xCoord - rot.offsetX * 3, yCoord, zCoord - rot.offsetZ * 3, rot.getOpposite()) }; } @Override public void serialize(ByteBuf buf) { buf.writeLong(this.power); buf.writeInt(this.turnTimer); this.tanks[0].serialize(buf); this.tanks[1].serialize(buf); } @Override public void deserialize(ByteBuf buf) { this.power = buf.readLong(); this.turnTimer = buf.readInt(); this.tanks[0].deserialize(buf); this.tanks[1].deserialize(buf); } @Override public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); tanks[0].readFromNBT(nbt, "water"); tanks[1].readFromNBT(nbt, "steam"); power = nbt.getLong("power"); } @Override public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); tanks[0].writeToNBT(nbt, "water"); tanks[1].writeToNBT(nbt, "steam"); nbt.setLong("power", power); } @Override public AxisAlignedBB getRenderBoundingBox() { return TileEntity.INFINITE_EXTENT_AABB; } @Override @SideOnly(Side.CLIENT) public double getMaxRenderDistanceSquared() { return 65536.0D; } @Override public boolean canConnect(ForgeDirection dir) { return dir != ForgeDirection.UP && dir != ForgeDirection.DOWN && dir != ForgeDirection.UNKNOWN; } @Override public long getPower() { return power; } @Override public long getMaxPower() { return maxPower; } @Override public void setPower(long power) { this.power = power; } @Override @Optional.Method(modid = "OpenComputers") public String getComponentName() { return "ntm_turbine"; } @Override public void onChunkUnload() { super.onChunkUnload(); if(audio != null) { audio.stopSound(); audio = null; } } @Override public void invalidate() { super.invalidate(); if(audio != null) { audio.stopSound(); audio = null; } } @Callback(direct = true, doc = "function():table -- Gets current tanks state. The format is the following: , , , ") @Optional.Method(modid = "OpenComputers") public Object[] getFluid(Context context, Arguments args) { return new Object[] {tanks[0].getFill(), tanks[0].getMaxFill(), tanks[1].getFill(), tanks[1].getMaxFill()}; } @Callback(direct = true, doc = "function():number -- Gets the current input tank fluid type. 0 stands for steam, 1 for dense steam, 2 for super dense steam and 3 for ultra dense steam.") @Optional.Method(modid = "OpenComputers") public Object[] getType(Context context, Arguments args) { return CompatHandler.steamTypeToInt(tanks[0].getTankType()); } @Callback(direct = true, limit = 4, doc = "function(type:number) -- Sets the input tank fluid type. Refer getType() for the accepted values information.") @Optional.Method(modid = "OpenComputers") public Object[] setType(Context context, Arguments args) { tanks[0].setTankType(CompatHandler.intToSteamType(args.checkInteger(0))); return new Object[] {}; } @Callback(direct = true, doc = "function():number -- Gets the power buffer of the turbine.") @Optional.Method(modid = "OpenComputers") public Object[] getPower(Context context, Arguments args) { return new Object[] {power}; } @Callback(direct = true, doc = "function():table -- Gets information about this turbine. The format is the following: , , , , , ") @Optional.Method(modid = "OpenComputers") public Object[] getInfo(Context context, Arguments args) { return new Object[] {tanks[0].getFill(), tanks[0].getMaxFill(), tanks[1].getFill(), tanks[1].getMaxFill(), CompatHandler.steamTypeToInt(tanks[0].getTankType())[0], power}; } @Override @Optional.Method(modid = "OpenComputers") public String[] methods() { return new String[] { "getFluid", "getType", "setType", "getPower", "getInfo" }; } @Override @Optional.Method(modid = "OpenComputers") public Object[] invoke(String method, Context context, Arguments args) throws Exception { switch(method) { case ("getFluid"): return getFluid(context, args); case ("getType"): return getType(context, args); case ("setType"): return setType(context, args); case ("getPower"): return getPower(context, args); case ("getInfo"): return getInfo(context, args); } throw new NoSuchMethodException(); } @Override public FluidTank[] getSendingTanks() { return new FluidTank[] {tanks[1]}; } @Override public FluidTank[] getReceivingTanks() { return new FluidTank[] {tanks[0]}; } @Override public FluidTank[] getAllTanks() { return tanks; } @Override public void provideExtraInfo(NBTTagCompound data) { data.setBoolean(CompatEnergyControl.B_ACTIVE, info[1] > 0); data.setDouble(CompatEnergyControl.D_CONSUMPTION_MB, info[0]); data.setDouble(CompatEnergyControl.D_OUTPUT_MB, info[1]); data.setDouble(CompatEnergyControl.D_OUTPUT_HE, info[2]); } @Override public FluidTank getTankToPaste() { return null; } }