From 76d9927483014ce3cfb9a73b384851276dacf9ce Mon Sep 17 00:00:00 2001 From: Rseifert Date: Fri, 1 Mar 2013 11:50:10 -0500 Subject: [PATCH] added a copy of Calclavia's Path Finder For the moment its just added and not used. I will have to rewrite it in a way that it doesn't care for TileEntities. This way i can use it for the pump to make sure that water sources are connected to the pump. --- buildnumber.txt | 2 +- info.txt | 2 + .../implement/ILiquidConnectionProvider.java | 27 +++++ .../core/implement/ILiquidConnector.java | 18 +++ .../core/liquids/HydraulicNetwork.java | 29 +++-- .../hydraulic/core/liquids/LiquidHandler.java | 4 + .../hydraulic/core/path/Pathfinder.java | 108 ++++++++++++++++++ .../core/path/PathfinderChecker.java | 48 ++++++++ .../core/path/PathfinderConductor.java | 39 +++++++ 9 files changed, 266 insertions(+), 11 deletions(-) create mode 100644 src/minecraft/hydraulic/core/implement/ILiquidConnectionProvider.java create mode 100644 src/minecraft/hydraulic/core/implement/ILiquidConnector.java create mode 100644 src/minecraft/hydraulic/core/path/Pathfinder.java create mode 100644 src/minecraft/hydraulic/core/path/PathfinderChecker.java create mode 100644 src/minecraft/hydraulic/core/path/PathfinderConductor.java diff --git a/buildnumber.txt b/buildnumber.txt index e3efa925..f021f03f 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -25 +27 diff --git a/info.txt b/info.txt index 0e60a3f2..d74e285f 100644 --- a/info.txt +++ b/info.txt @@ -23,3 +23,5 @@ x Fluid-Mechanics_v0.2.7.21.jar hydraulic_v0.2.7.21_api.zip @ Fluid-Mechanics_v0.2.8.23.jar hydraulic_v0.2.8.23_api.zip * Fluid-Mechanics_v0.2.8.24.jar hydraulic_v0.2.8.24_api.zip * Fluid-Mechanics_v0.2.8.25.jar hydraulic_v0.2.8.25_api.zip +* Fluid-Mechanics_v0.2.8.26.jar hydraulic_v0.2.8.26_api.zip +* Fluid-Mechanics_v0.2.8.27.jar hydraulic_v0.2.8.27_api.zip diff --git a/src/minecraft/hydraulic/core/implement/ILiquidConnectionProvider.java b/src/minecraft/hydraulic/core/implement/ILiquidConnectionProvider.java new file mode 100644 index 00000000..02e3d600 --- /dev/null +++ b/src/minecraft/hydraulic/core/implement/ILiquidConnectionProvider.java @@ -0,0 +1,27 @@ +package hydraulic.core.implement; + +import net.minecraft.tileentity.TileEntity; + +/** + * Applied to TileEntities. + * + * @author Calclavia + * + */ +public interface ILiquidConnectionProvider extends ILiquidConnector +{ + + /** + * Gets a list of all the connected TileEntities that this conductor is connected to. The + * array's length should be always the 6 adjacent wires. + * + * @return + */ + public TileEntity[] getAdjacentConnections(); + + /** + * Instantly refreshes all connected blocks around the conductor, recalculating the connected + * blocks. + */ + public void updateAdjacentConnections(); +} diff --git a/src/minecraft/hydraulic/core/implement/ILiquidConnector.java b/src/minecraft/hydraulic/core/implement/ILiquidConnector.java new file mode 100644 index 00000000..1ef980e9 --- /dev/null +++ b/src/minecraft/hydraulic/core/implement/ILiquidConnector.java @@ -0,0 +1,18 @@ +package hydraulic.core.implement; + +import net.minecraftforge.common.ForgeDirection; + +/** + * Applied to TileEntities that can connect to an electrical network. + * + * @author Calclavia + * + */ +public interface ILiquidConnector +{ + + /** + * @return If the connection is possible. + */ + public boolean canConnect(ForgeDirection direction); +} diff --git a/src/minecraft/hydraulic/core/liquids/HydraulicNetwork.java b/src/minecraft/hydraulic/core/liquids/HydraulicNetwork.java index ada79984..70b35866 100644 --- a/src/minecraft/hydraulic/core/liquids/HydraulicNetwork.java +++ b/src/minecraft/hydraulic/core/liquids/HydraulicNetwork.java @@ -4,6 +4,7 @@ import hydraulic.core.helpers.connectionHelper; import hydraulic.core.implement.ColorCode; import hydraulic.core.implement.IFluidPipe; import hydraulic.core.implement.IPsiCreator; +import hydraulic.core.implement.IPsiMachine; import hydraulic.core.implement.IPsiReciever; import java.util.ArrayList; @@ -22,11 +23,7 @@ public class HydraulicNetwork public final List conductors = new ArrayList(); /* MACHINES THAT USE THE FORGE LIQUID API TO RECEIVE LIQUID ** */ - public final List fluidReceivers = new ArrayList(); - - /* MACHINES THAT DEAL WITH PRESSURE ** */ - public final List pressureProducers = new ArrayList(); - public final List pressureReceivers = new ArrayList(); + public final List receivers = new ArrayList(); public ColorCode color = ColorCode.NONE; @@ -63,10 +60,11 @@ public class HydraulicNetwork boolean found = false; - for (ITankContainer tank : fluidReceivers) + for (TileEntity ent : receivers) { - if (tank instanceof TileEntity) + if (ent instanceof ITankContainer) { + ITankContainer tank = (ITankContainer) ent; TileEntity[] surroundings = connectionHelper.getSurroundingTileEntities((TileEntity) tank); for (int i = 0; i < 6; i++) { @@ -153,9 +151,20 @@ public class HydraulicNetwork */ public void removeEntity(TileEntity ent) { - fluidReceivers.remove(ent); - pressureProducers.remove(ent); - pressureReceivers.remove(ent); + if (receivers.contains(ent)) + { + receivers.remove(ent); + } + } + /** + * Adds a tileEntity to the list if its valid + */ + public void addEntity(TileEntity ent) + { + if(!receivers.contains(ent) && (ent instanceof ITankContainer || ent instanceof IPsiMachine || ent instanceof IPsiCreator)) + { + receivers.add(ent); + } } public void addConductor(IFluidPipe newConductor) diff --git a/src/minecraft/hydraulic/core/liquids/LiquidHandler.java b/src/minecraft/hydraulic/core/liquids/LiquidHandler.java index c14d2996..782e1541 100644 --- a/src/minecraft/hydraulic/core/liquids/LiquidHandler.java +++ b/src/minecraft/hydraulic/core/liquids/LiquidHandler.java @@ -197,9 +197,13 @@ public class LiquidHandler if (stack.stackSize == 1) { if (stack.getItem().hasContainerItem()) + { return stack.getItem().getContainerItemStack(stack); + } else + { return null; + } } else { diff --git a/src/minecraft/hydraulic/core/path/Pathfinder.java b/src/minecraft/hydraulic/core/path/Pathfinder.java new file mode 100644 index 00000000..f52dfd0d --- /dev/null +++ b/src/minecraft/hydraulic/core/path/Pathfinder.java @@ -0,0 +1,108 @@ +package hydraulic.core.path; + +import hydraulic.core.implement.ILiquidConnectionProvider; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.ForgeDirection; + +/** + * A class that allows flexible path finding in Minecraft Blocks. + * + * @author Calclavia + * + */ +public class Pathfinder +{ + public interface IPathCallBack + { + /** + * Is this a valid node to search for? + * + * @return + */ + public boolean isValidNode(Pathfinder finder, ForgeDirection direction, ILiquidConnectionProvider provider, ILiquidConnectionProvider node); + + /** + * Called when looping through nodes. + * + * @param finder + * @param provider + * @return True to stop the path finding operation. + */ + public boolean onSearch(Pathfinder finder, ILiquidConnectionProvider provider); + } + + /** + * A pathfinding call back interface used to call back on paths. + */ + public IPathCallBack callBackCheck; + + /** + * A list of nodes that the pathfinder went through. + */ + public List iteratedNodes; + + /** + * The results and findings found by the pathfinder. + */ + public List results; + + public Pathfinder(IPathCallBack callBack) + { + this.callBackCheck = callBack; + this.clear(); + } + + public boolean findNodes(ILiquidConnectionProvider provider) + { + TileEntity[] connectedBlocks = provider.getAdjacentConnections(); + + this.iteratedNodes.add(provider); + + if (this.callBackCheck.onSearch(this, provider)) + { + return false; + } + + for (int i = 0; i < connectedBlocks.length; i++) + { + TileEntity connectedBlock = connectedBlocks[i]; + + if (connectedBlock instanceof ILiquidConnectionProvider) + { + if (!iteratedNodes.contains(connectedBlock)) + { + if (this.callBackCheck.isValidNode(this, ForgeDirection.getOrientation(i), provider, (ILiquidConnectionProvider) connectedBlock)) + { + if (!this.findNodes((ILiquidConnectionProvider) connectedBlock)) + { + return false; + } + + } + } + } + } + + return true; + } + + /** + * Called to execute the pathfinding operation. + */ + public Pathfinder init(ILiquidConnectionProvider provider) + { + this.findNodes(provider); + return this; + } + + public Pathfinder clear() + { + this.iteratedNodes = new ArrayList(); + this.results = new ArrayList(); + return this; + } +} diff --git a/src/minecraft/hydraulic/core/path/PathfinderChecker.java b/src/minecraft/hydraulic/core/path/PathfinderChecker.java new file mode 100644 index 00000000..e91beed5 --- /dev/null +++ b/src/minecraft/hydraulic/core/path/PathfinderChecker.java @@ -0,0 +1,48 @@ +package hydraulic.core.path; + +import hydraulic.core.implement.ILiquidConnectionProvider; +import hydraulic.core.implement.ILiquidConnector; + +import java.util.Arrays; + +import net.minecraftforge.common.ForgeDirection; + +/** + * Check if a conductor connects with another. + * + * @author Calclavia + * + */ +public class PathfinderChecker extends Pathfinder +{ + public PathfinderChecker(final ILiquidConnectionProvider targetConnector, final ILiquidConnectionProvider... ignoreConnector) + { + super(new IPathCallBack() + { + @Override + public boolean isValidNode(Pathfinder finder, ForgeDirection direction, ILiquidConnectionProvider provider, ILiquidConnectionProvider connectedBlock) + { + if (connectedBlock instanceof ILiquidConnector && !Arrays.asList(ignoreConnector).contains(connectedBlock)) + { + if (((ILiquidConnector) connectedBlock).canConnect(direction.getOpposite())) + { + return true; + } + } + return false; + } + + @Override + public boolean onSearch(Pathfinder finder, ILiquidConnectionProvider provider) + { + if (provider == targetConnector) + { + finder.results.add(provider); + return true; + } + + return false; + } + }); + } +} diff --git a/src/minecraft/hydraulic/core/path/PathfinderConductor.java b/src/minecraft/hydraulic/core/path/PathfinderConductor.java new file mode 100644 index 00000000..1ee8bbfb --- /dev/null +++ b/src/minecraft/hydraulic/core/path/PathfinderConductor.java @@ -0,0 +1,39 @@ +package hydraulic.core.path; + +import hydraulic.core.implement.ILiquidConnectionProvider; +import hydraulic.core.implement.ILiquidConnector; +import net.minecraftforge.common.ForgeDirection; + +/** + * Finds all the possible conductors. + * + * @author Calclavia + * + */ +public class PathfinderConductor extends Pathfinder +{ + public PathfinderConductor() + { + super(new IPathCallBack() + { + @Override + public boolean isValidNode(Pathfinder finder, ForgeDirection direction, ILiquidConnectionProvider provider, ILiquidConnectionProvider connectedBlock) + { + if (connectedBlock instanceof ILiquidConnector) + { + if (((ILiquidConnector) connectedBlock).canConnect(direction.getOpposite())) + { + return true; + } + } + return false; + } + + @Override + public boolean onSearch(Pathfinder finder, ILiquidConnectionProvider provider) + { + return false; + } + }); + } +}