From 6939d3ca6fc02b24d7991f407e2220ece0e7ea8d Mon Sep 17 00:00:00 2001 From: Rseifert Date: Sun, 7 Apr 2013 02:56:41 -0400 Subject: [PATCH] got pathfinder simi functional not sure what i f'd up between this and my test build repo. What ever happened it works for water now and not for lava. --- .../common/machines/pipes/TileEntityPipe.java | 55 +++++++++---------- .../fluidmech/common/pump/BlockDrain.java | 1 - .../common/pump/PathfinderCheckerLiquid.java | 24 ++++++-- ...stBlock.java => PathfinderFindLiquid.java} | 15 +++-- .../pump/TileEntityConstructionPump.java | 26 ++++++--- .../common/pump/TileEntityDrain.java | 33 +++++++---- src/minecraft/hydraulic/api/IDrain.java | 5 +- .../fluidnetwork/HydraulicNetwork.java | 18 +++--- .../fluidnetwork/HydraulicNetworkHelper.java | 12 +++- .../prefab/tile/TileEntityFluidStorage.java | 12 ++-- 10 files changed, 126 insertions(+), 75 deletions(-) rename src/minecraft/fluidmech/common/pump/{PathfinderFindHighestBlock.java => PathfinderFindLiquid.java} (72%) diff --git a/src/minecraft/fluidmech/common/machines/pipes/TileEntityPipe.java b/src/minecraft/fluidmech/common/machines/pipes/TileEntityPipe.java index 6d202677..0f69b5ef 100644 --- a/src/minecraft/fluidmech/common/machines/pipes/TileEntityPipe.java +++ b/src/minecraft/fluidmech/common/machines/pipes/TileEntityPipe.java @@ -200,10 +200,8 @@ public class TileEntityPipe extends TileEntityAdvanced implements ITankContainer public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); - - LiquidStack liquid = new LiquidStack(0, 0, 0); - liquid.loadLiquidStackFromNBT(nbt.getCompoundTag("stored")); - if (Item.itemsList[liquid.itemID] != null && liquid.amount > 0) + LiquidStack liquid = LiquidStack.loadLiquidStackFromNBT(nbt.getCompoundTag("tank")); + if (liquid != null) { this.fakeTank.setLiquid(liquid); } @@ -216,6 +214,28 @@ public class TileEntityPipe extends TileEntityAdvanced implements ITankContainer } } + /** + * Writes a tile entity to NBT. + */ + @Override + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + if (this.fakeTank.containsValidLiquid()) + { + nbt.setTag("stored", this.fakeTank.getLiquid().writeToNBT(new NBTTagCompound())); + } + for (int i = 0; i < 6; i++) + { + if (this.subEntities[i] != null) + { + NBTTagCompound tag = new NBTTagCompound(); + ((TileEntity) this.subEntities[i]).writeToNBT(tag); + nbt.setTag("Addon" + i, tag); + } + } + } + public boolean addNewExtention(int side, Class partClass) { if (partClass == null) @@ -300,29 +320,6 @@ public class TileEntityPipe extends TileEntityAdvanced implements ITankContainer } - /** - * Writes a tile entity to NBT. - */ - @Override - public void writeToNBT(NBTTagCompound nbt) - { - super.writeToNBT(nbt); - LiquidStack stack = this.fakeTank.getLiquid(); - if (stack != null && LiquidDictionary.findLiquidName(stack) != null) - { - nbt.setTag("stored", stack.writeToNBT(new NBTTagCompound())); - } - for (int i = 0; i < 6; i++) - { - if (this.subEntities[i] != null) - { - NBTTagCompound tag = new NBTTagCompound(); - ((TileEntity) this.subEntities[i]).writeToNBT(tag); - nbt.setTag("Addon" + i, tag); - } - } - } - /** * gets the current color mark of the pipe */ @@ -405,7 +402,7 @@ public class TileEntityPipe extends TileEntityAdvanced implements ITankContainer { return 0; } - return this.getNetwork().addFluidToNetwork(this, resource,doFill); + return this.getNetwork().addFluidToNetwork(this, resource, doFill); } @Override @@ -510,7 +507,7 @@ public class TileEntityPipe extends TileEntityAdvanced implements ITankContainer LiquidStack stack = tankContainer.drain(dir, LiquidContainerRegistry.BUCKET_VOLUME, false); if (stack != null && stack.amount > 0) { - int fill = this.getNetwork().addFluidToNetwork((TileEntity) tankContainer, stack,true); + int fill = this.getNetwork().addFluidToNetwork((TileEntity) tankContainer, stack, true); tankContainer.drain(dir, fill, true); } } diff --git a/src/minecraft/fluidmech/common/pump/BlockDrain.java b/src/minecraft/fluidmech/common/pump/BlockDrain.java index 174e9238..7685702e 100644 --- a/src/minecraft/fluidmech/common/pump/BlockDrain.java +++ b/src/minecraft/fluidmech/common/pump/BlockDrain.java @@ -53,7 +53,6 @@ public class BlockDrain extends BlockAdvanced if(!world.isRemote) { world.setBlockMetadataWithNotify(x, y, z, side, 3); - return true; } return this.onUseWrench(world, x, y, z, entityPlayer, side, hitX, hitY, hitZ); } diff --git a/src/minecraft/fluidmech/common/pump/PathfinderCheckerLiquid.java b/src/minecraft/fluidmech/common/pump/PathfinderCheckerLiquid.java index 07f77ad2..f90d2233 100644 --- a/src/minecraft/fluidmech/common/pump/PathfinderCheckerLiquid.java +++ b/src/minecraft/fluidmech/common/pump/PathfinderCheckerLiquid.java @@ -3,8 +3,10 @@ package fluidmech.common.pump; import fluidmech.common.FluidMech; import hydraulic.helpers.FluidHelper; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import net.minecraft.tileentity.TileEntity; @@ -17,21 +19,26 @@ import universalelectricity.core.vector.Vector3; public class PathfinderCheckerLiquid extends Pathfinder { - public PathfinderCheckerLiquid(final World world, final int maxResources, final LiquidStack resource, final Vector3... ignoreList) + public List targetList = new ArrayList(); + + public PathfinderCheckerLiquid(final World world, final Vector3 callLoc, final LiquidStack resource, final Vector3... ignoreList) { super(new IPathCallBack() { @Override public Set getConnectedNodes(Pathfinder finder, Vector3 currentNode) { + System.out.println("AN:" + currentNode.toString()); Set neighbors = new HashSet(); for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) { Vector3 pos = currentNode.clone().modifyPositionFromSide(direction); - - if (FluidHelper.isLiquidBlock(world,pos) && FluidHelper.getLiquidFromBlockId(pos.getBlockID(world)).equals(resource)) + System.out.println("AN:" + direction.ordinal() + ":" + pos.toString() + " " + pos.getBlockID(world) + ":" + pos.getBlockMetadata(world)); + LiquidStack liquid = FluidHelper.getLiquidFromBlockId(pos.getBlockID(world)); + if (liquid != null && (liquid.equals(resource) || resource == null)) { + System.out.println("ADD:" + pos.toString()); neighbors.add(pos); } } @@ -42,7 +49,16 @@ public class PathfinderCheckerLiquid extends Pathfinder @Override public boolean onSearch(Pathfinder finder, Vector3 node) { - if (finder.closedSet.size() >= maxResources) + LiquidStack liquid = FluidHelper.getLiquidFromBlockId(node.getBlockID(world)); + if (liquid != null && (liquid.equals(resource) || resource == null) && node.getBlockMetadata(world) == 0) + { + TileEntity entity = callLoc.getTileEntity(world); + if (entity instanceof TileEntityDrain) + { + ((TileEntityDrain) entity).addVectorToQue(node); + } + } + if (finder.closedSet.size() >= 2000) { return true; } diff --git a/src/minecraft/fluidmech/common/pump/PathfinderFindHighestBlock.java b/src/minecraft/fluidmech/common/pump/PathfinderFindLiquid.java similarity index 72% rename from src/minecraft/fluidmech/common/pump/PathfinderFindHighestBlock.java rename to src/minecraft/fluidmech/common/pump/PathfinderFindLiquid.java index 32f354f8..82696e71 100644 --- a/src/minecraft/fluidmech/common/pump/PathfinderFindHighestBlock.java +++ b/src/minecraft/fluidmech/common/pump/PathfinderFindLiquid.java @@ -15,11 +15,11 @@ import universalelectricity.core.path.IPathCallBack; import universalelectricity.core.path.Pathfinder; import universalelectricity.core.vector.Vector3; -public class PathfinderFindHighestBlock extends Pathfinder +public class PathfinderFindLiquid extends Pathfinder { public static int highestY = 0; - public PathfinderFindHighestBlock(final World world, final int blockID, final Vector3... ignoreList) + public PathfinderFindLiquid(final World world, final LiquidStack resource, final Vector3... ignoreList) { super(new IPathCallBack() { @@ -27,8 +27,10 @@ public class PathfinderFindHighestBlock extends Pathfinder public Set getConnectedNodes(Pathfinder finder, Vector3 currentNode) { Set neighbors = new HashSet(); + Vector3 pos = currentNode.clone().modifyPositionFromSide(ForgeDirection.UP); - if (pos.getBlockID(world) == blockID) + LiquidStack liquid = FluidHelper.getLiquidFromBlockId(pos.getBlockID(world)); + if (liquid != null && (liquid.equals(resource) || resource == null)) { neighbors.add(pos); if (pos.intY() > highestY) @@ -37,11 +39,12 @@ public class PathfinderFindHighestBlock extends Pathfinder } return neighbors; } + for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) { pos = currentNode.clone().modifyPositionFromSide(direction); - - if (pos.getBlockID(world) == blockID) + liquid = FluidHelper.getLiquidFromBlockId(pos.getBlockID(world)); + if (liquid != null && (liquid.equals(resource) || resource == null)) { neighbors.add(pos); if (pos.intY() > highestY) @@ -57,7 +60,7 @@ public class PathfinderFindHighestBlock extends Pathfinder @Override public boolean onSearch(Pathfinder finder, Vector3 node) { - if (finder.closedSet.size() >= 10000 || highestY == 256) + if (finder.closedSet.size() >= 1000 || highestY >= 255) { return true; } diff --git a/src/minecraft/fluidmech/common/pump/TileEntityConstructionPump.java b/src/minecraft/fluidmech/common/pump/TileEntityConstructionPump.java index 8591d2b4..0112a54d 100644 --- a/src/minecraft/fluidmech/common/pump/TileEntityConstructionPump.java +++ b/src/minecraft/fluidmech/common/pump/TileEntityConstructionPump.java @@ -1,6 +1,7 @@ package fluidmech.common.pump; import hydraulic.api.IPipeConnection; +import hydraulic.fluidnetwork.HydraulicNetworkHelper; import hydraulic.fluidnetwork.IFluidNetworkPart; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; @@ -34,24 +35,28 @@ public class TileEntityConstructionPump extends TileEntityElectricityRunnable im this.outputSide = ForgeDirection.getOrientation(meta); this.inputSide = outputSide.getOpposite(); } + public ForgeDirection getFacing() { int meta = 2; - if(worldObj != null) + if (worldObj != null) { - meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord); + meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord); } return ForgeDirection.getOrientation(meta); - + } + @Override public void updateEntity() { - if (!worldObj.isRemote && this.wattsReceived >= this.WATTS_PER_TICK) + // this.wattsReceived >= this.WATTS_PER_TICK + if (!worldObj.isRemote) { if (this.ticks % 10 == 0) // TODO add electric Drain { boolean called = false; + TileEntity inputTile = VectorHelper.getTileEntityFromSide(worldObj, new Vector3(this), inputSide); TileEntity outputTile = VectorHelper.getTileEntityFromSide(worldObj, new Vector3(this), outputSide); if (inputTile instanceof IFluidNetworkPart) @@ -89,7 +94,7 @@ public class TileEntityConstructionPump extends TileEntityElectricityRunnable im @Override public int fill(ForgeDirection from, LiquidStack resource, boolean doFill) { - if (from != inputSide) + if (from != inputSide.getOpposite()) { return 0; } @@ -106,7 +111,8 @@ public class TileEntityConstructionPump extends TileEntityElectricityRunnable im TileEntity entity = VectorHelper.getTileEntityFromSide(this.worldObj, new Vector3(this), outputSide); if (entity instanceof ITankContainer) { - return ((ITankContainer) entity).fill(outputSide.getOpposite(), resource, doFill); + return resource.amount; + // return ((ITankContainer) entity).fill(outputSide.getOpposite(), resource, doFill); } return 0; } @@ -149,7 +155,11 @@ public class TileEntityConstructionPump extends TileEntityElectricityRunnable im { return dir == this.inputSide || dir == this.outputSide; } - - + @Override + public void invalidate() + { + super.invalidate(); + HydraulicNetworkHelper.invalidate(this); + } } diff --git a/src/minecraft/fluidmech/common/pump/TileEntityDrain.java b/src/minecraft/fluidmech/common/pump/TileEntityDrain.java index 0d43c0d0..e1f01537 100644 --- a/src/minecraft/fluidmech/common/pump/TileEntityDrain.java +++ b/src/minecraft/fluidmech/common/pump/TileEntityDrain.java @@ -7,6 +7,7 @@ import hydraulic.prefab.tile.TileEntityFluidDevice; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map.Entry; @@ -24,7 +25,7 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta { private boolean drainSources = true; /* MAX BLOCKS DRAINED PER 1/2 SECOND */ - public static int MAX_DRAIN_PER_PROCESS = 15; + public static int MAX_DRAIN_PER_PROCESS = 30; private int currentDrains = 0; /* LIST OF PUMPS AND THERE REQUESTS FOR THIS DRAIN */ private HashMap requestMap = new HashMap(); @@ -71,10 +72,12 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta { break; } - if (((IFluidNetworkPart) pipe).getNetwork().isConnected(request.getKey())) + if (((IFluidNetworkPart) pipe).getNetwork().isConnected(request.getKey()) && targetSources.size() > 0) { - for (Vector3 loc : targetSources) + Iterator it = this.targetSources.iterator(); + while (it.hasNext()) { + Vector3 loc = (Vector3) it.next(); if (this.currentDrains >= MAX_DRAIN_PER_PROCESS) { break; @@ -87,7 +90,7 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta if (stack != null && requestStack != null && (requestStack.isLiquidEqual(stack) || requestStack.itemID == -1)) { - if (request.getKey().fill(0, stack, false) >= stack.amount) + if (request.getKey().fill(0, stack, false) > 0) { int requestAmmount = requestStack.amount - request.getKey().fill(0, stack, true); if (requestAmmount <= 0) @@ -101,6 +104,8 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta loc.setBlock(this.worldObj, 0); this.currentDrains++; + it.remove(); + } } } @@ -141,27 +146,34 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta } @Override - public void stopRequesting(TileEntityConstructionPump pump) + public void stopRequesting(TileEntity pump) { if (this.requestMap.containsKey(pump)) { this.requestMap.remove(pump); } } - + public void addVectorToQue(Vector3 vector) + { + if(!this.targetSources.contains(vector)) + { + this.targetSources.add(vector); + } + } /** * Finds more liquid blocks using a path finder to be drained */ public void getNextFluidBlock() { + System.out.println("Before Targets:"+this.targetSources.size()); /* FIND HIGHEST DRAIN POINT FIRST */ - PathfinderFindHighestBlock path = new PathfinderFindHighestBlock(this.worldObj, Block.waterStill.blockID); - path.init(new Vector3(xCoord + this.getFacing().offsetX, yCoord + this.getFacing().offsetY, zCoord + this.getFacing().offsetZ)); + PathfinderFindLiquid path = new PathfinderFindLiquid(this.worldObj, null); + path.init(new Vector3(this.xCoord + this.getFacing().offsetX, this.yCoord + this.getFacing().offsetY, this.zCoord + this.getFacing().offsetZ)); int y = path.highestY; /* FIND 10 UNMARKED SOURCES */ - PathfinderCheckerLiquid pathFinder = new PathfinderCheckerLiquid(worldObj, this.MAX_DRAIN_PER_PROCESS, null, (Vector3[]) this.targetSources.toArray()); - pathFinder.init(new Vector3(xCoord, y, zCoord)); + PathfinderCheckerLiquid pathFinder = new PathfinderCheckerLiquid(this.worldObj, new Vector3(this), null); + pathFinder.init(new Vector3(this.xCoord, y, this.zCoord)); for (Vector3 loc : pathFinder.closedSet) { if (!this.targetSources.contains(loc)) @@ -169,6 +181,7 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta this.targetSources.add(loc); } } + System.out.println("Targets:"+this.targetSources.size()); } @Override diff --git a/src/minecraft/hydraulic/api/IDrain.java b/src/minecraft/hydraulic/api/IDrain.java index f848a773..d5840e38 100644 --- a/src/minecraft/hydraulic/api/IDrain.java +++ b/src/minecraft/hydraulic/api/IDrain.java @@ -1,6 +1,7 @@ package hydraulic.api; import fluidmech.common.pump.TileEntityConstructionPump; +import net.minecraft.tileentity.TileEntity; import net.minecraftforge.liquids.ITankContainer; import net.minecraftforge.liquids.LiquidStack; @@ -38,7 +39,7 @@ public interface IDrain extends ITankContainer * removed from the request map after being filled. However, this can be used too stop a request * short if the pump becomes full before the request is filled * - * @param pump - requesting pump + * @param tileEntity - requesting pump */ - public void stopRequesting(TileEntityConstructionPump pump); + public void stopRequesting(TileEntity tileEntity); } diff --git a/src/minecraft/hydraulic/fluidnetwork/HydraulicNetwork.java b/src/minecraft/hydraulic/fluidnetwork/HydraulicNetwork.java index 3376335d..59e307ee 100644 --- a/src/minecraft/hydraulic/fluidnetwork/HydraulicNetwork.java +++ b/src/minecraft/hydraulic/fluidnetwork/HydraulicNetwork.java @@ -140,6 +140,8 @@ public class HydraulicNetwork { fluidTanks.remove(ent); } + this.removeLoad(ent); + this.removeSource(ent); } /** @@ -264,7 +266,7 @@ public class HydraulicNetwork */ public int addFluidToNetwork(TileEntity source, LiquidStack stack, boolean doFill) { - return this.addFluidToNetwork(source, stack, doFill, true); + return this.addFluidToNetwork(source, stack, doFill, false); } /** @@ -306,7 +308,7 @@ public class HydraulicNetwork /* FIND THE FILL TARGET FROM THE LIST OF FLUID RECIEVERS */ for (ITankContainer tankContainer : fluidTanks) { - if (tankContainer instanceof TileEntity && tankContainer != source) + if (tankContainer instanceof TileEntity && tankContainer != source && !(tankContainer instanceof IFluidNetworkPart)) { TileEntity[] connectedTiles = connectionHelper.getSurroundingTileEntities((TileEntity) tankContainer); @@ -353,17 +355,17 @@ public class HydraulicNetwork if (primaryFill != null) { used = primaryFill.fill(fillDir, stack, doFill); - System.out.println("Primary Target " + used + doFill); + //System.out.println("Primary Target " + used + doFill); } else if (secondayFill != null) { used = secondayFill.fill(fillDir, stack, doFill); - System.out.println("Seconday Target " + used + doFill); + //System.out.println("Seconday Target " + used + doFill); } else if (allowStore && (this.combinedStorage.getLiquid() == null || this.combinedStorage.getLiquid().amount < this.combinedStorage.getCapacity())) { used = this.combinedStorage.fill(stack, doFill); - System.out.println("Network Target filled for " + used + doFill); + //System.out.println("Network Target filled for " + used + doFill); filledMain = true; } /* IF THE COMBINED STORAGE OF THE PIPES HAS LIQUID MOVE IT FIRST */ @@ -382,7 +384,7 @@ public class HydraulicNetwork used = Math.min(used, Math.max(used - this.combinedStorage.getLiquid().amount, 0)); drainStack = this.combinedStorage.drain(pUsed - used, doFill); } - System.out.println("Pulling " + (drainStack != null ? drainStack.amount : 0) + " from combined leaving " + (this.combinedStorage.getLiquid() != null ? this.combinedStorage.getLiquid().amount : 0)); + //System.out.println("Pulling " + (drainStack != null ? drainStack.amount : 0) + " from combined leaving " + (this.combinedStorage.getLiquid() != null ? this.combinedStorage.getLiquid().amount : 0)); } if (prevCombined != null && this.combinedStorage.getLiquid() != null && prevCombined.amount != this.combinedStorage.getLiquid().amount) @@ -614,7 +616,7 @@ public class HydraulicNetwork { if (node != splitPoint) { - newNetwork.getFluidNetworkParts().add((IFluidNetworkPart) node); + newNetwork.getFluidNetworkParts().add((IFluidNetworkPart) entity); parts++; } } @@ -681,7 +683,7 @@ public class HydraulicNetwork public String getStorageFluid() { - if (combinedStorage.getLiquid() == null) + if (!combinedStorage.containsValidLiquid()) { return "Zero"; } diff --git a/src/minecraft/hydraulic/fluidnetwork/HydraulicNetworkHelper.java b/src/minecraft/hydraulic/fluidnetwork/HydraulicNetworkHelper.java index a74229e0..f43e8185 100644 --- a/src/minecraft/hydraulic/fluidnetwork/HydraulicNetworkHelper.java +++ b/src/minecraft/hydraulic/fluidnetwork/HydraulicNetworkHelper.java @@ -1,8 +1,10 @@ package hydraulic.fluidnetwork; +import hydraulic.api.IDrain; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; +import net.minecraftforge.liquids.ITankContainer; import universalelectricity.core.vector.Vector3; import universalelectricity.core.vector.VectorHelper; @@ -26,8 +28,14 @@ public class HydraulicNetworkHelper if (network != null) { - network.removeLoad(tileEntity); - network.removeSource(tileEntity); + network.removeEntity(tileEntity); + for(ITankContainer tank : network.fluidTanks) + { + if(tank instanceof IDrain) + { + ((IDrain)tank).stopRequesting(tileEntity); + } + } } } } diff --git a/src/minecraft/hydraulic/prefab/tile/TileEntityFluidStorage.java b/src/minecraft/hydraulic/prefab/tile/TileEntityFluidStorage.java index 69c54899..da2d1df6 100644 --- a/src/minecraft/hydraulic/prefab/tile/TileEntityFluidStorage.java +++ b/src/minecraft/hydraulic/prefab/tile/TileEntityFluidStorage.java @@ -109,18 +109,20 @@ public abstract class TileEntityFluidStorage extends TileEntityFluidDevice imple { super.readFromNBT(nbt); - LiquidStack liquid = new LiquidStack(0, 0, 0); - liquid.readFromNBT(nbt.getCompoundTag("stored")); - tank.setLiquid(liquid); + LiquidStack liquid = LiquidStack.loadLiquidStackFromNBT(nbt.getCompoundTag("stored")); + if (liquid != null) + { + tank.setLiquid(liquid); + } } @Override public void writeToNBT(NBTTagCompound nbt) { super.writeToNBT(nbt); - if (tank.getLiquid() != null) + if (this.tank.containsValidLiquid()) { - nbt.setTag("stored", tank.getLiquid().writeToNBT(new NBTTagCompound())); + nbt.setTag("stored", this.tank.getLiquid().writeToNBT(new NBTTagCompound())); } }