My approach to Universal Cables rendering power. They render with

transparency proportional to the base 10 log of power, to compensate
for the wide range of power that can be transmitted, and minimise client
update packets.
This commit is contained in:
Ben Spiers 2013-08-04 01:48:13 +01:00
parent 8ee17b2a03
commit 49fc056c8c
6 changed files with 146 additions and 8 deletions

View file

@ -0,0 +1,41 @@
package mekanism.client;
import java.util.ArrayList;
import java.util.List;
import mekanism.api.Object3D;
import mekanism.common.EnergyNetwork.NetworkFinder;
import mekanism.common.IUniversalCable;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public class EnergyClientUpdate
{
public NetworkFinder finder;
public World worldObj;
public double energyScale;
public EnergyClientUpdate(TileEntity head, double power)
{
worldObj = head.worldObj;
energyScale = power;
finder = new NetworkFinder(head.worldObj, Object3D.get(head));
System.out.println(power);
}
public void clientUpdate()
{
List<Object3D> found = finder.exploreNetwork();
for(Object3D object : found)
{
TileEntity tileEntity = object.getTileEntity(worldObj);
if(tileEntity instanceof IUniversalCable)
{
((IUniversalCable)tileEntity).setEnergyScale(energyScale);
}
}
}
}

View file

@ -12,6 +12,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import cpw.mods.fml.common.FMLCommonHandler;
import mekanism.api.IStrictEnergyAcceptor; import mekanism.api.IStrictEnergyAcceptor;
import mekanism.api.ITransmitterNetwork; import mekanism.api.ITransmitterNetwork;
import mekanism.api.Object3D; import mekanism.api.Object3D;
@ -19,6 +21,8 @@ import mekanism.api.TransmitterNetworkRegistry;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.Event;
import net.minecraftforge.event.ForgeSubscribe; import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.world.ChunkEvent; import net.minecraftforge.event.world.ChunkEvent;
import buildcraft.api.power.IPowerReceptor; import buildcraft.api.power.IPowerReceptor;
@ -32,9 +36,11 @@ public class EnergyNetwork implements ITransmitterNetwork
public Set<TileEntity> possibleAcceptors = new HashSet<TileEntity>(); public Set<TileEntity> possibleAcceptors = new HashSet<TileEntity>();
public Map<TileEntity, ForgeDirection> acceptorDirections = new HashMap<TileEntity, ForgeDirection>(); public Map<TileEntity, ForgeDirection> acceptorDirections = new HashMap<TileEntity, ForgeDirection>();
private double lastPowerScale = 0;
private double joulesTransmitted = 0; private double joulesTransmitted = 0;
private double joulesLastTick = 0; private double joulesLastTick = 0;
private int ticksSinceCreate = 0; private int ticksSinceCreate = 0;
private int ticksSinceSecond = 0;
private boolean fixed = false; private boolean fixed = false;
public EnergyNetwork(IUniversalCable... varCables) public EnergyNetwork(IUniversalCable... varCables)
@ -210,6 +216,14 @@ public class EnergyNetwork implements ITransmitterNetwork
} }
} }
} }
double currentPowerScale = getPowerScale();
if(FMLCommonHandler.instance().getEffectiveSide().isServer())
{
lastPowerScale = currentPowerScale;
MinecraftForge.EVENT_BUS.post(new EnergyTransferEvent(this, currentPowerScale));
}
} }
public void merge(EnergyNetwork network) public void merge(EnergyNetwork network)
@ -393,6 +407,19 @@ public class EnergyNetwork implements ITransmitterNetwork
} }
} }
public static class EnergyTransferEvent extends Event
{
public final EnergyNetwork energyNetwork;
public final double power;
public EnergyTransferEvent(EnergyNetwork network, double currentPower)
{
energyNetwork = network;
power = currentPower;
}
}
public static class NetworkLoader public static class NetworkLoader
{ {
@ForgeSubscribe @ForgeSubscribe
@ -436,6 +463,19 @@ public class EnergyNetwork implements ITransmitterNetwork
fixMessedUpNetwork(cables.iterator().next()); fixMessedUpNetwork(cables.iterator().next());
} }
} }
double currentPowerScale = getPowerScale();
if(currentPowerScale != lastPowerScale && FMLCommonHandler.instance().getEffectiveSide().isServer())
{
lastPowerScale = currentPowerScale;
MinecraftForge.EVENT_BUS.post(new EnergyTransferEvent(this, currentPowerScale));
}
}
public double getPowerScale()
{
return joulesLastTick == 0 ? 0 : Math.min(Math.ceil(Math.log10(getPower())*2)/10, 1);
} }
public void clearJoulesTransmitted() public void clearJoulesTransmitted()

View file

@ -45,4 +45,6 @@ public interface IUniversalCable
*/ */
public void fixNetwork(); public void fixNetwork();
public void setEnergyScale(double energyScale);
} }

View file

@ -17,6 +17,7 @@ import mekanism.api.InfuseType;
import mekanism.api.InfusionInput; import mekanism.api.InfusionInput;
import mekanism.api.Object3D; import mekanism.api.Object3D;
import mekanism.client.SoundHandler; import mekanism.client.SoundHandler;
import mekanism.common.EnergyNetwork.EnergyTransferEvent;
import mekanism.common.FluidNetwork.FluidTransferEvent; import mekanism.common.FluidNetwork.FluidTransferEvent;
import mekanism.common.IFactory.RecipeType; import mekanism.common.IFactory.RecipeType;
import mekanism.common.MekanismUtils.ResourceType; import mekanism.common.MekanismUtils.ResourceType;
@ -1214,6 +1215,14 @@ public class Mekanism
logger.info("[Mekanism] Mod loaded."); logger.info("[Mekanism] Mod loaded.");
} }
@ForgeSubscribe
public void onEnergyTransferred(EnergyTransferEvent event)
{
try {
PacketHandler.sendPacket(Transmission.ALL_CLIENTS, new PacketTransmitterTransferUpdate().setParams(TransmitterTransferType.ENERGY, event.energyNetwork.cables.iterator().next(), event.power));
} catch(Exception e) {}
}
@ForgeSubscribe @ForgeSubscribe
public void onGasTransferred(GasTransferEvent event) public void onGasTransferred(GasTransferEvent event)
{ {

View file

@ -4,6 +4,10 @@ import java.util.HashSet;
import mekanism.api.Object3D; import mekanism.api.Object3D;
import mekanism.api.TransmitterNetworkRegistry; import mekanism.api.TransmitterNetworkRegistry;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World; import net.minecraft.world.World;
@ -22,6 +26,8 @@ public class TileEntityUniversalCable extends TileEntity implements IUniversalCa
/** The energy network currently in use by this cable segment. */ /** The energy network currently in use by this cable segment. */
public EnergyNetwork energyNetwork; public EnergyNetwork energyNetwork;
public double energyScale;
public TileEntityUniversalCable() public TileEntityUniversalCable()
{ {
powerHandler = new PowerHandler(this, PowerHandler.Type.STORAGE); powerHandler = new PowerHandler(this, PowerHandler.Type.STORAGE);
@ -40,12 +46,6 @@ public class TileEntityUniversalCable extends TileEntity implements IUniversalCa
return getNetwork(true); return getNetwork(true);
} }
public float getEnergyScale()
{
//TODO: Let the client know how much power's being transferred
return 1.F;
}
@Override @Override
public EnergyNetwork getNetwork(boolean createIfNull) public EnergyNetwork getNetwork(boolean createIfNull)
{ {
@ -161,4 +161,29 @@ public class TileEntityUniversalCable extends TileEntity implements IUniversalCa
invalidate(); invalidate();
TransmitterNetworkRegistry.getInstance().pruneEmptyNetworks(); TransmitterNetworkRegistry.getInstance().pruneEmptyNetworks();
} }
@Override
public void setEnergyScale(double scale)
{
energyScale = scale;
}
@Override
public Packet getDescriptionPacket()
{
energyScale = getNetwork().getPowerScale();
NBTTagCompound nbtTag = new NBTTagCompound();
nbtTag.setDouble("energyScale", energyScale);
return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
}
public void onDataPacket(INetworkManager net, Packet132TileEntityData packet)
{
setEnergyScale(packet.customParam1.getDouble("energyScale"));
}
public float getEnergyScale()
{
return (float)energyScale;
}
} }

View file

@ -3,6 +3,7 @@ package mekanism.common.network;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import mekanism.api.EnumGas; import mekanism.api.EnumGas;
import mekanism.client.EnergyClientUpdate;
import mekanism.client.GasClientUpdate; import mekanism.client.GasClientUpdate;
import mekanism.client.FluidClientUpdate; import mekanism.client.FluidClientUpdate;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@ -18,6 +19,8 @@ public class PacketTransmitterTransferUpdate implements IMekanismPacket
public TileEntity tileEntity; public TileEntity tileEntity;
public double power;
public String gasName; public String gasName;
public FluidStack fluidStack; public FluidStack fluidStack;
@ -36,6 +39,9 @@ public class PacketTransmitterTransferUpdate implements IMekanismPacket
switch(activeType) switch(activeType)
{ {
case ENERGY:
power = (double)data[2];
break;
case GAS: case GAS:
gasName = ((EnumGas)data[2]).name; gasName = ((EnumGas)data[2]).name;
break; break;
@ -57,6 +63,17 @@ public class PacketTransmitterTransferUpdate implements IMekanismPacket
int z = dataStream.readInt(); int z = dataStream.readInt();
if(transmitterType == 0) if(transmitterType == 0)
{
double powerLevel = dataStream.readDouble();
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if(tileEntity != null)
{
new EnergyClientUpdate(tileEntity, powerLevel).clientUpdate();
}
}
else if(transmitterType == 1)
{ {
EnumGas type = EnumGas.getFromName(dataStream.readUTF()); EnumGas type = EnumGas.getFromName(dataStream.readUTF());
@ -67,7 +84,7 @@ public class PacketTransmitterTransferUpdate implements IMekanismPacket
new GasClientUpdate(tileEntity, type).clientUpdate(); new GasClientUpdate(tileEntity, type).clientUpdate();
} }
} }
else if(transmitterType == 1) else if(transmitterType == 2)
{ {
TileEntity tileEntity = world.getBlockTileEntity(x, y, z); TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
FluidStack fluidStack = new FluidStack(dataStream.readInt(), dataStream.readInt()); FluidStack fluidStack = new FluidStack(dataStream.readInt(), dataStream.readInt());
@ -90,6 +107,9 @@ public class PacketTransmitterTransferUpdate implements IMekanismPacket
switch(activeType) switch(activeType)
{ {
case ENERGY:
dataStream.writeDouble(power);
break;
case GAS: case GAS:
dataStream.writeUTF(gasName); dataStream.writeUTF(gasName);
break; break;
@ -102,6 +122,7 @@ public class PacketTransmitterTransferUpdate implements IMekanismPacket
public static enum TransmitterTransferType public static enum TransmitterTransferType
{ {
ENERGY,
GAS, GAS,
FLUID FLUID
} }