Changed RunnableMachine to function diffrent

This commit is contained in:
DarkGuardsman 2013-07-01 19:00:36 -04:00
parent fe90843d44
commit ac15cf12a0
2 changed files with 134 additions and 166 deletions

View file

@ -1,6 +1,5 @@
package dark.core.tile.network; package dark.core.tile.network;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
@ -8,8 +7,10 @@ import java.util.List;
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 universalelectricity.core.path.Pathfinder; import universalelectricity.core.path.Pathfinder;
import universalelectricity.core.vector.Vector3; import universalelectricity.core.vector.Vector3;
import universalelectricity.core.vector.VectorHelper;
import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.FMLLog;
import dark.core.api.INetworkPart; import dark.core.api.INetworkPart;
@ -23,13 +24,11 @@ public class NetworkTileEntities
this.networkMember.addAll(Arrays.asList(parts)); this.networkMember.addAll(Arrays.asList(parts));
} }
/** /** Adds a TileEntity to the network
* Adds a TileEntity to the network
* *
* @param ent - tileEntity instance * @param ent - tileEntity instance
* @param member - add to network member list * @param member - add to network member list
* @return * @return */
*/
public boolean addEntity(TileEntity ent, boolean member) public boolean addEntity(TileEntity ent, boolean member)
{ {
if (ent == null || this.isPartOfNetwork(ent)) if (ent == null || this.isPartOfNetwork(ent))
@ -48,33 +47,27 @@ public class NetworkTileEntities
return this.networkMember.contains(ent); return this.networkMember.contains(ent);
} }
/** /** Adds a new part to the network member list */
* Adds a new part to the network member list
*/
public boolean addNetworkPart(INetworkPart part) public boolean addNetworkPart(INetworkPart part)
{ {
if (!networkMember.contains(part) && this.isValidMember(part)) if (!networkMember.contains(part) && this.isValidMember(part))
{ {
networkMember.add(part); networkMember.add(part);
part.setTileNetwork(this); part.setTileNetwork(this);
this.cleanUpConductors(); this.cleanUpMembers();
return true; return true;
} }
return false; return false;
} }
/** /** Removes a tileEntity from any of the valid lists */
* Removes a tileEntity from any of the valid lists
*/
public void removeEntity(TileEntity ent) public void removeEntity(TileEntity ent)
{ {
this.networkMember.remove(ent); this.networkMember.remove(ent);
} }
/** /** Cleans the list of networkMembers and remove those that no longer belong */
* Cleans the list of networkMembers and remove those that no longer belong public void cleanUpMembers()
*/
public void cleanUpConductors()
{ {
Iterator<INetworkPart> it = this.networkMember.iterator(); Iterator<INetworkPart> it = this.networkMember.iterator();
@ -94,20 +87,16 @@ public class NetworkTileEntities
} }
/** /** Is this part a valid member of the network */
* Is this part a valid member of the network
*/
public boolean isValidMember(INetworkPart part) public boolean isValidMember(INetworkPart part)
{ {
return part != null && part instanceof TileEntity && !((TileEntity) part).isInvalid(); return part != null && part instanceof TileEntity && !((TileEntity) part).isInvalid();
} }
/** /** Refreshes the network... mainly the network member list */
* Refreshes the network... mainly the network member list
*/
public void refresh() public void refresh()
{ {
this.cleanUpConductors(); this.cleanUpMembers();
try try
{ {
Iterator<INetworkPart> it = this.networkMember.iterator(); Iterator<INetworkPart> it = this.networkMember.iterator();
@ -125,20 +114,16 @@ public class NetworkTileEntities
} }
} }
/** /** Gets the list of network members */
* Gets the list of network members
*/
public List<INetworkPart> getNetworkMemebers() public List<INetworkPart> getNetworkMemebers()
{ {
return this.networkMember; return this.networkMember;
} }
/** /** Combines two networks together into one
* Combines two networks together into one
* *
* @param network * @param network
* @param part * @param part */
*/
public void merge(NetworkTileEntities network, INetworkPart part) public void merge(NetworkTileEntities network, INetworkPart part)
{ {
if (network != null && network != this && network.getClass().equals(this.getClass())) if (network != null && network != this && network.getClass().equals(this.getClass()))
@ -150,43 +135,34 @@ public class NetworkTileEntities
} }
} }
/** /** Processing that needs too be done before the network merges
* Processing that needs too be done before the network merges
* *
* @return false if the merge needs to be canceled * @return false if the merge needs to be canceled */
*/
public boolean preMergeProcessing(NetworkTileEntities network, INetworkPart part) public boolean preMergeProcessing(NetworkTileEntities network, INetworkPart part)
{ {
return true; return true;
} }
/** /** Finalizing the merge of two networks by creating the new network and importing all network
* Finalizing the merge of two networks by creating the new network and importing all network * parts */
* parts
*/
public void postMergeProcessing(NetworkTileEntities network) public void postMergeProcessing(NetworkTileEntities network)
{ {
NetworkTileEntities newNetwork = new NetworkTileEntities(); NetworkTileEntities newNetwork = new NetworkTileEntities();
newNetwork.getNetworkMemebers().addAll(this.getNetworkMemebers()); newNetwork.getNetworkMemebers().addAll(this.getNetworkMemebers());
newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers()); newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers());
newNetwork.cleanUpConductors(); newNetwork.cleanUpMembers();
// newNetwork.balanceColletiveTank(true);
} }
/** /** Called when a peace of the network is remove from the network. Will split the network if it
* Called when a peace of the network is remove from the network. Will split the network if it * can no longer find a valid connection too all parts */
* can no longer find a valid connection too all parts
*/
public void splitNetwork(World world, INetworkPart splitPoint) public void splitNetwork(World world, INetworkPart splitPoint)
{ {
if (splitPoint instanceof TileEntity) if (splitPoint instanceof TileEntity)
{ {
this.getNetworkMemebers().remove(splitPoint); this.getNetworkMemebers().remove(splitPoint);
/** /** Loop through the connected blocks and attempt to see if there are connections between
* Loop through the connected blocks and attempt to see if there are connections between * the two points elsewhere. */
* the two points elsewhere.
*/
TileEntity[] connectedBlocks = splitPoint.getNetworkConnections(); TileEntity[] connectedBlocks = splitPoint.getNetworkConnections();
for (int i = 0; i < connectedBlocks.length; i++) for (int i = 0; i < connectedBlocks.length; i++)
@ -237,7 +213,7 @@ public class NetworkTileEntities
} }
} }
newNetwork.cleanUpConductors(); newNetwork.cleanUpMembers();
} }
} }
} }
@ -251,4 +227,17 @@ public class NetworkTileEntities
{ {
return "TileNetwork[" + this.hashCode() + "|parts:" + this.networkMember.size() + "]"; return "TileNetwork[" + this.hashCode() + "|parts:" + this.networkMember.size() + "]";
} }
public static void invalidate(TileEntity tileEntity)
{
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity checkTile = VectorHelper.getConnectorFromSide(tileEntity.worldObj, new Vector3(tileEntity), direction);
if (checkTile instanceof INetworkPart && ((INetworkPart) checkTile).getTileNetwork() != null)
{
((INetworkPart) checkTile).getTileNetwork().removeEntity(tileEntity);
}
}
}
} }

View file

@ -1,93 +1,68 @@
package dark.library.machine; package dark.library.machine;
import ic2.api.Direction; import java.util.EnumSet;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
import universalelectricity.core.UniversalElectricity; import universalelectricity.core.UniversalElectricity;
import universalelectricity.core.block.IConnector; import universalelectricity.core.block.IConnector;
import universalelectricity.core.block.IVoltage; import universalelectricity.core.block.IVoltage;
import universalelectricity.core.electricity.ElectricityNetworkHelper;
import universalelectricity.core.electricity.ElectricityPack; import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.prefab.tile.TileEntityElectrical;
import universalelectricity.prefab.tile.TileEntityElectricityRunnable; import universalelectricity.prefab.tile.TileEntityElectricityRunnable;
import buildcraft.api.power.IPowerProvider; import buildcraft.api.power.IPowerProvider;
import buildcraft.api.power.IPowerReceptor; import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerFramework; import buildcraft.api.power.PowerFramework;
import dark.core.PowerSystems; import dark.core.PowerSystems;
public abstract class TileEntityRunnableMachine extends TileEntityElectricityRunnable implements IPowerReceptor, IEnergySink, IConnector, IVoltage public abstract class TileEntityRunnableMachine extends TileEntityElectrical implements IPowerReceptor, IConnector, IVoltage
{ {
/** Forge Ore Directory name of the item to toggle power */
public static String powerToggleItemID = "battery"; public static String powerToggleItemID = "battery";
/** Should this machine run without power */
protected boolean runPowerless = false; protected boolean runPowerless = false;
/** BuildCraft power provider? */
private IPowerProvider powerProvider; private IPowerProvider powerProvider;
public TileEntityRunnableMachine() public double prevWatts, wattsReceived = 0;
{
} private PowerSystems[] powerList = new PowerSystems[] { PowerSystems.BUILDCRAFT, PowerSystems.MEKANISM };
@Override
public void initiate()
{
super.initiate();
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
}
@Override
public void invalidate()
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
super.invalidate();
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.wattsReceived = nbt.getDouble("wattsReceived");
this.runPowerless = nbt.getBoolean("shouldPower");
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setDouble("wattsReceived", this.wattsReceived);
nbt.setBoolean("shouldPower", this.runPowerless);
}
/**
* Sets this machine to run without power only if the given stack match an ore directory name
*/
public void toggleInfPower(ItemStack item)
{
for (ItemStack stack : OreDictionary.getOres(this.powerToggleItemID))
{
if (stack.isItemEqual(item))
{
this.runPowerless = !this.runPowerless;
break;
}
}
}
@Override @Override
public void updateEntity() public void updateEntity()
{ {
super.updateEntity(); super.updateEntity();
this.prevWatts = this.wattsReceived;
if (this.wattsReceived < this.getWattBuffer() && (this.runPowerless || PowerSystems.runPowerLess(PowerSystems.INDUSTRIALCRAFT, PowerSystems.BUILDCRAFT, PowerSystems.MEKANISM))) if (!this.worldObj.isRemote)
{
if ((this.runPowerless || PowerSystems.runPowerLess(powerList)) && this.wattsReceived < this.getWattBuffer())
{ {
this.wattsReceived += Math.max(this.getWattBuffer() - this.wattsReceived, 0); this.wattsReceived += Math.max(this.getWattBuffer() - this.wattsReceived, 0);
} }
else
{
this.doPowerUpdate();
}
}
}
public void doPowerUpdate()
{
// UNIVERSAL ELECTRICITY UPDATE
if (!this.isDisabled())
{
ElectricityPack electricityPack = ElectricityNetworkHelper.consumeFromMultipleSides(this, this.getConsumingSides(), ElectricityPack.getFromWatts(this.getRequest(), this.getVoltage()));
this.onReceive(electricityPack.voltage, electricityPack.amperes);
}
else
{
ElectricityNetworkHelper.consumeFromMultipleSides(this, new ElectricityPack());
}
// BUILDCRAFT POWER UPDATE
if (PowerFramework.currentFramework != null) if (PowerFramework.currentFramework != null)
{ {
if (this.powerProvider == null) if (this.powerProvider == null)
@ -98,65 +73,14 @@ public abstract class TileEntityRunnableMachine extends TileEntityElectricityRun
} }
if (this.powerProvider != null) if (this.powerProvider != null)
{ {
float requiredEnergy = (float) (this.getRequest().getWatts() * UniversalElectricity.TO_BC_RATIO); float requiredEnergy = (float) (this.getRequest() * UniversalElectricity.TO_BC_RATIO);
float energyReceived = this.powerProvider.useEnergy(0, requiredEnergy, true); float energyReceived = this.powerProvider.useEnergy(0, requiredEnergy, true);
this.onReceive(ElectricityPack.getFromWatts(UniversalElectricity.BC3_RATIO * energyReceived, this.getVoltage())); this.onReceive(this.getVoltage(), (UniversalElectricity.BC3_RATIO * energyReceived) / this.getVoltage());
} }
//TODO add other power systems
} }
/** /** Buildcraft */
* IC2
*/
@Override
public boolean acceptsEnergyFrom(TileEntity emitter, Direction direction)
{
if (this.getConsumingSides() != null)
{
return this.getConsumingSides().contains(direction.toForgeDirection());
}
else
{
return true;
}
}
@Override
public boolean isAddedToEnergyNet()
{
return this.ticks > 0;
}
@Override
public int demandsEnergy()
{
return (int) Math.ceil(this.getRequest().getWatts() * UniversalElectricity.TO_IC2_RATIO);
}
@Override
public int injectEnergy(Direction direction, int i)
{
double givenElectricity = i * UniversalElectricity.IC2_RATIO;
double rejects = 0;
if (givenElectricity > this.getWattBuffer())
{
rejects = givenElectricity - this.getRequest().getWatts();
}
this.onReceive(new ElectricityPack(givenElectricity / this.getVoltage(), this.getVoltage()));
return (int) (rejects * UniversalElectricity.TO_IC2_RATIO);
}
@Override
public int getMaxSafeInput()
{
return 2048;
}
/**
* Buildcraft
*/
@Override @Override
public void setPowerProvider(IPowerProvider provider) public void setPowerProvider(IPowerProvider provider)
{ {
@ -172,7 +96,6 @@ public abstract class TileEntityRunnableMachine extends TileEntityElectricityRun
@Override @Override
public void doWork() public void doWork()
{ {
} }
@Override @Override
@ -180,9 +103,65 @@ public abstract class TileEntityRunnableMachine extends TileEntityElectricityRun
{ {
if (this.canConnect(from)) if (this.canConnect(from))
{ {
return (int) Math.ceil(this.getRequest().getWatts() * UniversalElectricity.TO_BC_RATIO); return (int) Math.ceil(this.getRequest() * UniversalElectricity.TO_BC_RATIO);
} }
return 0; return 0;
} }
protected EnumSet<ForgeDirection> getConsumingSides()
{
return ElectricityNetworkHelper.getDirections(this);
}
/** Watts this tile needs a tick to function */
public abstract double getRequest();
public void onReceive(double voltage, double amperes)
{
if (voltage > this.getVoltage())
{
this.onDisable(2);
return;
}
this.wattsReceived = Math.min(this.wattsReceived + (voltage * amperes), this.getWattBuffer());
}
/** @return The amount of internal buffer that may be stored within this machine. This will make
* the machine run smoother as electricity might not always be consistent. */
public double getWattBuffer()
{
return this.getRequest() * 2;
}
/** Sets this machine to run without power only if the given stack match an ore directory name */
public void toggleInfPower(ItemStack item)
{
for (ItemStack stack : OreDictionary.getOres(this.powerToggleItemID))
{
if (stack.isItemEqual(item))
{
this.runPowerless = !this.runPowerless;
break;
}
}
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.wattsReceived = nbt.getDouble("wattsReceived");
this.runPowerless = nbt.getBoolean("shouldPower");
this.disabledTicks = nbt.getInteger("disabledTicks");
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setDouble("wattsReceived", this.wattsReceived);
nbt.setBoolean("shouldPower", this.runPowerless);
nbt.setInteger("disabledTicks", this.disabledTicks);
}
} }