From d36010e9a68deec19d7cdaab3d1aa42827f8cc28 Mon Sep 17 00:00:00 2001 From: Rseifert Date: Tue, 9 Apr 2013 00:00:10 -0400 Subject: [PATCH] Add a collection sorter to the fill method This should if done correctly place the closest and lowest Y fill-able blocks at the top of the list so they get fill first. So far in testing it looked like it worked as the water bubble created expanded evenly outward instead of more in one direction. --- .../common/pump/TileEntityDrain.java | 89 ++++++++++++------- .../common/pump/path/LiquidPathFinder.java | 10 ++- 2 files changed, 64 insertions(+), 35 deletions(-) diff --git a/src/minecraft/fluidmech/common/pump/TileEntityDrain.java b/src/minecraft/fluidmech/common/pump/TileEntityDrain.java index 85cdb023..e713a222 100644 --- a/src/minecraft/fluidmech/common/pump/TileEntityDrain.java +++ b/src/minecraft/fluidmech/common/pump/TileEntityDrain.java @@ -7,6 +7,8 @@ import hydraulic.helpers.FluidHelper; import hydraulic.prefab.tile.TileEntityFluidDevice; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -134,6 +136,16 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta } } } + else if (FluidHelper.getLiquidFromBlockId(loc.getBlockID(this.worldObj)) != null && loc.getBlockMetadata(this.worldObj) != 0) + { + loc.setBlock(this.worldObj, 0, 0, 2); + + /* ADD TO UPDATE QUE */ + if (!this.updateQue.contains(loc)) + { + this.updateQue.add(loc); + } + } } } } @@ -149,7 +161,7 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta pathFinder.init(new Vector3(this.xCoord + this.getFacing().offsetX, this.yCoord + this.getFacing().offsetY, this.zCoord + this.getFacing().offsetZ)); // System.out.println("Nodes:" + pathFinder.nodes.size() + "Results:" + // pathFinder.results.size()); - for (Vector3 vec : pathFinder.results) + for (Vector3 vec : pathFinder.nodes) { this.addVectorToQue(vec); } @@ -161,11 +173,14 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta if (this.ticks % 100 == 0 && updateQue.size() > 0) { Iterator pp = this.updateQue.iterator(); - while (pp.hasNext()) + int up = 0; + while (pp.hasNext() && up < this.MAX_WORLD_EDITS_PER_PROCESS) { Vector3 vec = (Vector3) pp.next(); - worldObj.notifyBlocksOfNeighborChange(vec.intX(), vec.intY(), vec.intZ(), vec.getBlockID(this.worldObj)); + worldObj.notifyBlockChange(vec.intX(), vec.intY(), vec.intZ(), vec.getBlockID(this.worldObj)); + worldObj.notifyBlockOfNeighborChange(vec.intX(), vec.intY(), vec.intZ(), vec.getBlockID(this.worldObj)); pp.remove(); + up++; } } /* CLEANUP REQUEST MAP AND REMOVE INVALID TILES */ @@ -241,8 +256,46 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta /* FIND ALL VALID BLOCKS ON LEVEL OR BELLOW */ LiquidPathFinder pathFinder = new LiquidPathFinder(this.worldObj, true, this.MAX_WORLD_EDITS_PER_PROCESS * 2); - pathFinder.init(new Vector3(this.xCoord + this.getFacing().offsetX, this.yCoord + this.getFacing().offsetY, this.zCoord + this.getFacing().offsetZ)); - System.out.println("Nodes:" + pathFinder.nodes.size() + "Results:" + pathFinder.results.size()); + final Vector3 faceVec = new Vector3(this.xCoord + this.getFacing().offsetX, this.yCoord + this.getFacing().offsetY, this.zCoord + this.getFacing().offsetZ); + pathFinder.init(faceVec); + + /* SORT RESULTS TO PUT THE LOWEST AND CLOSEST AT THE TOP */ + try + { + if (pathFinder.results.size() > 1) + { + Collections.sort(pathFinder.results, new Comparator() + { + @Override + public int compare(Object o1, Object o2) + { + if (o1 == o2) + { + return 0; + } + Vector3 a = (Vector3) o1; + Vector3 b = (Vector3) o2; + double da = Vector3.distance(a, faceVec); + double db = Vector3.distance(b, faceVec); + ; + if (a.equals(b)) + { + return 0; + } + if (Integer.compare(a.intY(), b.intY()) != 0) + { + return Integer.compare(a.intY(), b.intY()); + } + return Double.compare(da, db); + } + }); + } + } + catch (Exception e) + { + System.out.println("FluidMech: Error sorting fill collection \n"); + e.printStackTrace(); + } /* START FILLING IN OR CHECKING IF CAN FILL AREA */ int fillable = 0; for (Vector3 loc : pathFinder.results) @@ -296,32 +349,6 @@ public class TileEntityDrain extends TileEntityFluidDevice implements ITankConta return drained; } - /** - * Sorter used by the fill method to maker sure its filling the lowest blocks closest to the - * drain first - */ - public static void SortListClosestToOrLowest(ArrayList list, Vector3 target) - { - if (list.size() > 1) // check if the number of orders is larger than 1 - { - for (int x = 0; x < list.size(); x++) // bubble sort outer loop - { - Vector3 vec = list.get(x); - double distance = Vector3.distance(vec, target); - for (int i = 0; i < list.size(); i++) - { - Vector3 pos = list.get(x); - if (Vector3.distance(pos, target) < distance) - { - list.set(x, pos); - list.set(i, vec); - } - } - } - } - - } - @Override public boolean canPipeConnect(TileEntity entity, ForgeDirection dir) { diff --git a/src/minecraft/fluidmech/common/pump/path/LiquidPathFinder.java b/src/minecraft/fluidmech/common/pump/path/LiquidPathFinder.java index d09dddb6..4e26497f 100644 --- a/src/minecraft/fluidmech/common/pump/path/LiquidPathFinder.java +++ b/src/minecraft/fluidmech/common/pump/path/LiquidPathFinder.java @@ -2,7 +2,9 @@ package fluidmech.common.pump.path; import hydraulic.helpers.FluidHelper; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import net.minecraft.block.Block; @@ -17,8 +19,8 @@ import universalelectricity.core.vector.Vector3; public class LiquidPathFinder { private World world; /* MC WORLD */ - public Set nodes; /* LOCATIONs THE PATH FINDER HAS GONE OVER */ - public Set results;/* LOCATIONS THAT ARE VALID RESULTS */ + public List nodes = new ArrayList(); /* LOCATIONs THE PATH FINDER HAS GONE OVER */ + public List results = new ArrayList();/* LOCATIONS THAT ARE VALID RESULTS */ private boolean fill; /* ARE WE FILLING THE PATH OR DRAINING THE PATH */ private ForgeDirection priority; /* BASED ON fill -- WHICH DIRECTION WILL THE PATH GO FIRST */ private int resultLimit = 2000; @@ -123,8 +125,8 @@ public class LiquidPathFinder public LiquidPathFinder reset() { - this.nodes = new HashSet(); - this.results = new HashSet(); + this.nodes.clear(); + this.results.clear(); return this; } }