From 8f3d78ce1d98d6e7e118b67cab7a3569e5c17b6b Mon Sep 17 00:00:00 2001 From: Calclavia Date: Tue, 24 Dec 2013 17:28:09 +0800 Subject: [PATCH] Worked on multiblock battery --- Calclavia-Library | 2 +- Universal-Electricity | 2 +- .../battery/BatteryStructure.java | 98 ++++++++ .../battery/BatteryUpdateProtocol.java | 220 ------------------ .../battery/BlockBattery.java | 14 +- .../battery/SynchronizedBatteryData.java | 86 ------- .../battery/TileBattery.java | 143 +++++------- .../levitator/TileEMLevitator.java | 4 +- src/resonantinduction/tesla/TileTesla.java | 14 -- .../wire/part/PartConductor.java | 30 ++- .../wire/part/PartFlatWire.java | 7 +- .../wire/part/TraitConductor.java | 12 + 12 files changed, 222 insertions(+), 410 deletions(-) create mode 100644 src/resonantinduction/battery/BatteryStructure.java delete mode 100644 src/resonantinduction/battery/BatteryUpdateProtocol.java delete mode 100644 src/resonantinduction/battery/SynchronizedBatteryData.java diff --git a/Calclavia-Library b/Calclavia-Library index 1d94973d4..8cf5a5604 160000 --- a/Calclavia-Library +++ b/Calclavia-Library @@ -1 +1 @@ -Subproject commit 1d94973d405930d7b68a25cc0040c82a671a8fc6 +Subproject commit 8cf5a56046328e7feacac579bfac2144dc2ee91d diff --git a/Universal-Electricity b/Universal-Electricity index deaf0e46b..b2b8aa468 160000 --- a/Universal-Electricity +++ b/Universal-Electricity @@ -1 +1 @@ -Subproject commit deaf0e46b7754fcce3b1ea22dc71f45e3a000131 +Subproject commit b2b8aa4684ee8b20d061c867ab7c14e5fc907135 diff --git a/src/resonantinduction/battery/BatteryStructure.java b/src/resonantinduction/battery/BatteryStructure.java new file mode 100644 index 000000000..f24b62967 --- /dev/null +++ b/src/resonantinduction/battery/BatteryStructure.java @@ -0,0 +1,98 @@ +package resonantinduction.battery; + +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +import net.minecraft.item.ItemStack; +import universalelectricity.api.energy.EnergyStorageHandler; +import universalelectricity.api.vector.Vector3; + +public class BatteryStructure extends EnergyStorageHandler +{ + public BatteryStructure(TileBattery battery) + { + super(TileBattery.STORAGE); + this.battery.add(battery); + } + + public Set battery = new LinkedHashSet(); + + public int length; + + public int width; + + public int height; + + public ItemStack tempStack; + + public boolean isMultiblock; + + public boolean didTick; + + public boolean wroteNBT; + + public int getVolume() + { + return this.battery.size(); + } + + /* + * @Override + * public int hashCode() + * { + * int code = 1; + * code = 31 * locations.hashCode(); + * code = 31 * length; + * code = 31 * width; + * code = 31 * height; + * return code; + * } + */ + @Override + public boolean equals(Object obj) + { + if (!(obj instanceof BatteryStructure)) + { + return false; + } + + BatteryStructure data = (BatteryStructure) obj; + + if (!data.battery.equals(battery)) + { + return false; + } + + if (data.length != length || data.width != width || data.height != height) + { + return false; + } + + return true; + } + + public void merge(TileBattery tile) + { + // Merge structure. + long energyToMerge = ((TileBattery) tile).structure.getEnergy(); + long capacityToMerge = ((TileBattery) tile).structure.getEnergyCapacity(); + this.battery.addAll(((TileBattery) tile).structure.battery); + ((TileBattery) tile).structure.battery.clear(); + this.resetReferences(); + this.setCapacity(capacityToMerge); + this.receiveEnergy(energyToMerge, true); + } + + public void resetReferences() + { + Iterator it = this.battery.iterator(); + + while (it.hasNext()) + { + TileBattery tile = it.next(); + tile.structure = this; + } + } +} diff --git a/src/resonantinduction/battery/BatteryUpdateProtocol.java b/src/resonantinduction/battery/BatteryUpdateProtocol.java deleted file mode 100644 index 2bae35a0b..000000000 --- a/src/resonantinduction/battery/BatteryUpdateProtocol.java +++ /dev/null @@ -1,220 +0,0 @@ -package resonantinduction.battery; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import net.minecraft.entity.item.EntityItem; -import net.minecraft.item.ItemStack; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.world.World; -import net.minecraftforge.common.ForgeDirection; -import resonantinduction.base.ListUtil; -import universalelectricity.api.vector.Vector3; - -/** - * Essentially a pathfinder for multiblock battery structures. - * - * @author Aidancbrady - * - */ -public class BatteryUpdateProtocol -{ - /** The battery nodes that have already been iterated over. */ - public Set iteratedNodes = new HashSet(); - - /** The structures found, all connected by some nodes to the pointer. */ - public SynchronizedBatteryData structureFound = null; - - /** The original block the calculation is getting run from. */ - public TileEntity pointer; - - public BatteryUpdateProtocol(TileEntity tileEntity) - { - pointer = tileEntity; - } - - public void updateBatteries() - { - loopThrough(this.pointer); - - if (structureFound != null) - { - - } - } - - private void loopThrough(TileEntity tile) - { - if (structureFound == null) - { - World worldObj = tile.worldObj; - - int origX = tile.xCoord, origY = tile.yCoord, origZ = tile.zCoord; - - boolean isCorner = true; - boolean rightBlocks = true; - - Set locations = new HashSet(); - - int xmin = 0, xmax = 0, ymin = 0, ymax = 0, zmin = 0, zmax = 0; - - int x = 0, y = 0, z = 0; - - if ((isBattery(origX + 1, origY, origZ) && isBattery(origX - 1, origY, origZ)) || (isBattery(origX, origY + 1, origZ) && isBattery(origX, origY - 1, origZ)) || (isBattery(origX, origY, origZ + 1) && isBattery(origX, origY, origZ - 1))) - { - isCorner = false; - } - - if (isCorner) - { - if (isBattery(origX + 1, origY, origZ)) - { - xmin = 0; - - while (isBattery(origX + x + 1, origY, origZ)) - { - x++; - } - - xmax = x; - } - else - { - xmax = 0; - - while (isBattery(origX + x - 1, origY, origZ)) - { - x--; - } - - xmin = x; - } - - if (isBattery(origX, origY + 1, origZ)) - { - ymin = 0; - - while (isBattery(origX, origY + y + 1, origZ)) - { - y++; - } - - ymax = y; - } - else - { - ymax = 0; - - while (isBattery(origX, origY + y - 1, origZ)) - { - y--; - } - - ymin = y; - } - - if (isBattery(origX, origY, origZ + 1)) - { - zmin = 0; - - while (isBattery(origX, origY, origZ + z + 1)) - { - z++; - } - - zmax = z; - } - else - { - zmax = 0; - - while (isBattery(origX, origY, origZ + z - 1)) - { - z--; - } - - zmin = z; - } - - for (x = xmin; x <= xmax; x++) - { - for (y = ymin; y <= ymax; y++) - { - for (z = zmin; z <= zmax; z++) - { - if (!isBattery(origX + x, origY + y, origZ + z)) - { - rightBlocks = false; - break; - } - else - { - locations.add(new Vector3(tile).translate(new Vector3(x, y, z))); - } - } - - if (!rightBlocks) - { - break; - } - } - - if (!rightBlocks) - { - break; - } - } - } - - if (locations.size() >= 1 && locations.size() < 512) - { - if (rightBlocks && isCorner) - { - SynchronizedBatteryData structure = new SynchronizedBatteryData(); - structure.locations = locations; - structure.length = Math.abs(xmax - xmin) + 1; - structure.height = Math.abs(ymax - ymin) + 1; - structure.width = Math.abs(zmax - zmin) + 1; - - if (structure.getVolume() > 1) - { - structure.isMultiblock = true; - } - - if (structure.locations.contains(new Vector3(pointer))) - { - structureFound = structure; - } - } - } - } - - iteratedNodes.add((TileBattery) tile); - - for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) - { - TileEntity tileEntity = new Vector3(tile).modifyPositionFromSide(side).getTileEntity(tile.worldObj); - - if (tileEntity instanceof TileBattery) - { - if (!iteratedNodes.contains(tileEntity)) - { - loopThrough(tileEntity); - } - } - } - } - - private boolean isBattery(int x, int y, int z) - { - if (pointer.worldObj.getBlockTileEntity(x, y, z) instanceof TileBattery) - { - return true; - } - - return false; - } - -} diff --git a/src/resonantinduction/battery/BlockBattery.java b/src/resonantinduction/battery/BlockBattery.java index ce8105fd2..4b81c6e1f 100644 --- a/src/resonantinduction/battery/BlockBattery.java +++ b/src/resonantinduction/battery/BlockBattery.java @@ -33,6 +33,16 @@ public class BlockBattery extends BlockBase implements ITileEntityProvider this.setTextureName(ResonantInduction.PREFIX + "machine"); } + @Override + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9) + { + if (!world.isRemote) + { + System.out.println(((TileBattery) world.getBlockTileEntity(x, y, z)).structure.hashCode()); + } + return true; + } + @Override public void onNeighborBlockChange(World world, int x, int y, int z, int id) { @@ -41,7 +51,7 @@ public class BlockBattery extends BlockBase implements ITileEntityProvider if (id == blockID) { TileBattery battery = (TileBattery) world.getBlockTileEntity(x, y, z); - battery.update(); + battery.updateStructure(); } } } @@ -52,7 +62,7 @@ public class BlockBattery extends BlockBase implements ITileEntityProvider if (!world.isRemote) { TileBattery battery = (TileBattery) world.getBlockTileEntity(x, y, z); - battery.update(); + battery.updateStructure(); } } diff --git a/src/resonantinduction/battery/SynchronizedBatteryData.java b/src/resonantinduction/battery/SynchronizedBatteryData.java deleted file mode 100644 index 6f695b63d..000000000 --- a/src/resonantinduction/battery/SynchronizedBatteryData.java +++ /dev/null @@ -1,86 +0,0 @@ -package resonantinduction.battery; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import net.minecraft.item.ItemStack; -import resonantinduction.base.ListUtil; -import universalelectricity.api.item.IEnergyItem; -import universalelectricity.api.vector.Vector3; - -public class SynchronizedBatteryData -{ - public Set locations = new HashSet(); - - public int length; - - public int width; - - public int height; - - public ItemStack tempStack; - - public boolean isMultiblock; - - public boolean didTick; - - public boolean wroteNBT; - - public int getVolume() - { - return length * width * height; - } - - public static SynchronizedBatteryData getBase(TileBattery tileEntity, List inventory) - { - SynchronizedBatteryData structure = getBase(tileEntity); - return structure; - } - - public static SynchronizedBatteryData getBase(TileBattery tileEntity) - { - SynchronizedBatteryData structure = new SynchronizedBatteryData(); - structure.length = 1; - structure.width = 1; - structure.height = 1; - structure.locations.add(new Vector3(tileEntity)); - - return structure; - } - - @Override - public int hashCode() - { - int code = 1; - code = 31 * locations.hashCode(); - code = 31 * length; - code = 31 * width; - code = 31 * height; - return code; - } - - @Override - public boolean equals(Object obj) - { - if (!(obj instanceof SynchronizedBatteryData)) - { - return false; - } - - SynchronizedBatteryData data = (SynchronizedBatteryData) obj; - - if (!data.locations.equals(locations)) - { - return false; - } - - if (data.length != length || data.width != width || data.height != height) - { - return false; - } - - return true; - } -} diff --git a/src/resonantinduction/battery/TileBattery.java b/src/resonantinduction/battery/TileBattery.java index 7bbc64c4d..aa495288b 100644 --- a/src/resonantinduction/battery/TileBattery.java +++ b/src/resonantinduction/battery/TileBattery.java @@ -4,19 +4,21 @@ package resonantinduction.battery; import java.util.ArrayList; -import java.util.EnumSet; import java.util.HashSet; import java.util.Set; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import resonantinduction.ResonantInduction; -import universalelectricity.api.energy.EnergyStorageHandler; +import universalelectricity.api.UniversalClass; +import universalelectricity.api.energy.IEnergyContainer; +import universalelectricity.api.energy.IEnergyInterface; import universalelectricity.api.vector.Vector3; import calclavia.lib.network.IPacketReceiver; import calclavia.lib.network.IPacketSender; -import calclavia.lib.tile.TileEntityElectrical; +import calclavia.lib.tile.TileAdvanced; import com.google.common.io.ByteArrayDataInput; @@ -24,28 +26,43 @@ import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; /** - * A modular battery with no GUI. + * A modular battery. * - * @author Calclavia, AidanBrady + * @author Calclavia */ -public class TileBattery extends TileEntityElectrical implements IPacketSender, IPacketReceiver +@UniversalClass +public class TileBattery extends TileAdvanced implements IPacketSender, IPacketReceiver, IEnergyInterface, IEnergyContainer { - public static final long STORAGE = 10000000; + public static final long STORAGE = 100000000; public Set playersUsing = new HashSet(); - public SynchronizedBatteryData structure = SynchronizedBatteryData.getBase(this); - public SynchronizedBatteryData prevStructure; + public BatteryStructure structure = new BatteryStructure(this); public float clientEnergy; public int clientCells; public float clientMaxEnergy; - private EnumSet inputSides = EnumSet.allOf(ForgeDirection.class); - - public TileBattery() + @Override + public void initiate() { - this.energy = new EnergyStorageHandler(1000); + this.updateStructure(); + } + + public void updateStructure() + { + if (!this.worldObj.isRemote) + { + for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) + { + TileEntity tile = new Vector3(this).modifyPositionFromSide(dir).getTileEntity(this.worldObj); + + if (tile instanceof TileBattery) + { + this.structure.merge((TileBattery) tile); + } + } + } } @Override @@ -55,30 +72,6 @@ public class TileBattery extends TileEntityElectrical implements IPacketSender, if (!this.worldObj.isRemote) { - if (this.ticks == 5 && !this.structure.isMultiblock) - { - this.update(); - } - - if (this.prevStructure != this.structure) - { - for (EntityPlayer player : playersUsing) - { - player.closeScreen(); - } - - updateClient(); - } - - this.prevStructure = structure; - - this.structure.wroteNBT = false; - this.structure.didTick = false; - - if (this.playersUsing.size() > 0) - { - updateClient(); - } for (EntityPlayer player : this.playersUsing) { @@ -89,6 +82,11 @@ public class TileBattery extends TileEntityElectrical implements IPacketSender, } } + private void produce() + { + + } + public float getTransferThreshhold() { return this.structure.getVolume() * 50; @@ -99,19 +97,11 @@ public class TileBattery extends TileEntityElectrical implements IPacketSender, PacketDispatcher.sendPacketToAllPlayers(ResonantInduction.PACKET_TILE.getPacket(this, getPacketData(0).toArray())); } - public void updateAllClients() - { - for (Vector3 vec : structure.locations) - { - TileBattery battery = (TileBattery) vec.getTileEntity(worldObj); - PacketDispatcher.sendPacketToAllPlayers(ResonantInduction.PACKET_TILE.getPacket(battery, battery.getPacketData(0).toArray())); - } - } - @Override - public void readFromNBT(NBTTagCompound nbtTags) + public void readFromNBT(NBTTagCompound nbt) { - super.readFromNBT(nbtTags); + super.readFromNBT(nbt); + this.structure.readFromNBT(nbt); } @Override @@ -121,23 +111,11 @@ public class TileBattery extends TileEntityElectrical implements IPacketSender, if (!structure.wroteNBT) { + this.structure.writeToNBT(nbt); structure.wroteNBT = true; } } - public void update() - { - if (!worldObj.isRemote && (structure == null || !structure.didTick)) - { - new BatteryUpdateProtocol(this).updateBatteries(); - - if (structure != null) - { - structure.didTick = true; - } - } - } - @Override public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player) { @@ -162,31 +140,38 @@ public class TileBattery extends TileEntityElectrical implements IPacketSender, } @Override - public EnumSet getInputDirections() + public boolean canConnect(ForgeDirection direction) { - return this.inputSides; + return true; } @Override - public EnumSet getOutputDirections() + public void setEnergy(ForgeDirection from, long energy) { - return EnumSet.complementOf(this.inputSides); + this.structure.setEnergy(energy); } - /** - * Toggles the input/output sides of the battery. - */ - public boolean toggleSide(ForgeDirection orientation) + @Override + public long getEnergy(ForgeDirection from) { - if (this.inputSides.contains(orientation)) - { - this.inputSides.remove(orientation); - return false; - } - else - { - this.inputSides.add(orientation); - return true; - } + return this.structure.getEnergy(); + } + + @Override + public long getEnergyCapacity(ForgeDirection from) + { + return this.structure.getEnergyCapacity(); + } + + @Override + public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) + { + return this.structure.receiveEnergy(receive, doReceive); + } + + @Override + public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract) + { + return this.structure.extractEnergy(extract, doExtract); } } diff --git a/src/resonantinduction/levitator/TileEMLevitator.java b/src/resonantinduction/levitator/TileEMLevitator.java index 77a64e902..76239faa3 100644 --- a/src/resonantinduction/levitator/TileEMLevitator.java +++ b/src/resonantinduction/levitator/TileEMLevitator.java @@ -27,7 +27,7 @@ import resonantinduction.tesla.TileTesla; import universalelectricity.api.vector.Vector3; import calclavia.lib.network.IPacketReceiver; import calclavia.lib.network.IPacketSender; -import calclavia.lib.tile.TileEntityAdvanced; +import calclavia.lib.tile.TileAdvanced; import calclavia.lib.utility.InventoryUtility; import com.google.common.io.ByteArrayDataInput; @@ -37,7 +37,7 @@ import com.google.common.io.ByteArrayDataInput; * @author Calclavia * */ -public class TileEMLevitator extends TileEntityAdvanced implements IPacketReceiver, IPacketSender +public class TileEMLevitator extends TileAdvanced implements IPacketReceiver, IPacketSender { public static int MAX_REACH = 40; public static int PUSH_DELAY = 5; diff --git a/src/resonantinduction/tesla/TileTesla.java b/src/resonantinduction/tesla/TileTesla.java index 66bf1104b..af761de7e 100644 --- a/src/resonantinduction/tesla/TileTesla.java +++ b/src/resonantinduction/tesla/TileTesla.java @@ -607,18 +607,4 @@ public class TileTesla extends TileEntityElectrical implements ITesla, IPacketSe } } } - - @Override - public EnumSet getInputDirections() - { - EnumSet input = EnumSet.allOf(ForgeDirection.class); - input.remove(ForgeDirection.DOWN); - return input; - } - - @Override - public EnumSet getOutputDirections() - { - return EnumSet.of(ForgeDirection.DOWN); - } } diff --git a/src/resonantinduction/wire/part/PartConductor.java b/src/resonantinduction/wire/part/PartConductor.java index d2b415cae..7cdfdcb3a 100644 --- a/src/resonantinduction/wire/part/PartConductor.java +++ b/src/resonantinduction/wire/part/PartConductor.java @@ -1,9 +1,9 @@ package resonantinduction.wire.part; -import codechicken.multipart.TMultiPart; import ic2.api.energy.event.EnergyTileLoadEvent; import ic2.api.energy.event.EnergyTileUnloadEvent; import ic2.api.energy.tile.IEnergyTile; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.MinecraftForge; @@ -15,6 +15,7 @@ import universalelectricity.api.energy.IConductor; import universalelectricity.api.energy.IEnergyNetwork; import universalelectricity.api.vector.Vector3; import universalelectricity.api.vector.VectorHelper; +import codechicken.multipart.TMultiPart; @UniversalClass public abstract class PartConductor extends PartAdvanced implements IConductor @@ -23,6 +24,8 @@ public abstract class PartConductor extends PartAdvanced implements IConductor protected Object[] connections = new Object[6]; + private long savedBuffer; + /** * Universal Electricity conductor functions. */ @@ -168,6 +171,31 @@ public abstract class PartConductor extends PartAdvanced implements IConductor } super.preRemove(); + } + @Override + public long getSavedBuffer() + { + return this.savedBuffer; + } + + @Override + public void setSaveBuffer(long energy) + { + this.savedBuffer = energy; + } + + @Override + public void save(NBTTagCompound nbt) + { + super.save(nbt); + nbt.setLong("savedBuffer", this.savedBuffer); + } + + @Override + public void load(NBTTagCompound nbt) + { + super.load(nbt); + this.savedBuffer = nbt.getLong("savedBuffer"); } } diff --git a/src/resonantinduction/wire/part/PartFlatWire.java b/src/resonantinduction/wire/part/PartFlatWire.java index 10a59d2aa..5879ba4ca 100644 --- a/src/resonantinduction/wire/part/PartFlatWire.java +++ b/src/resonantinduction/wire/part/PartFlatWire.java @@ -215,7 +215,7 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal } this.recalculateConnections(); - + super.onChunkLoad(); } @@ -259,7 +259,7 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal this.recalculateConnections(); } - + super.onPartChanged(part); } @@ -339,7 +339,7 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal } } } - + if (!calculatedSides[absDir]) { this.disconnect(absDir); @@ -902,5 +902,4 @@ public class PartFlatWire extends PartAdvancedWire implements TFacePart, JNormal CCRenderState.reset(); RenderFlatWire.renderBreakingOverlay(renderBlocks.overrideBlockTexture, this); } - } \ No newline at end of file diff --git a/src/resonantinduction/wire/part/TraitConductor.java b/src/resonantinduction/wire/part/TraitConductor.java index 02b78e596..fd1b51716 100644 --- a/src/resonantinduction/wire/part/TraitConductor.java +++ b/src/resonantinduction/wire/part/TraitConductor.java @@ -166,4 +166,16 @@ public class TraitConductor extends TileMultipart implements IConductor return capacitance; } + @Override + public long getSavedBuffer() + { + return 0; + } + + @Override + public void setSaveBuffer(long energy) + { + + } + }