add weighted fluid sorting for diamond fluid pipes, mirroring diamond item pipes

This commit is contained in:
asiekierka 2015-04-06 09:53:31 +02:00
parent 0509624c89
commit 7f52097896
3 changed files with 30 additions and 28 deletions

View file

@ -12,6 +12,8 @@ import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
@ -408,16 +410,16 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
FluidStack testStack = pushStack.copy(); FluidStack testStack = pushStack.copy();
testStack.amount = flowRate; testStack.amount = flowRate;
// Move liquid from the center to the output sides // Move liquid from the center to the output sides
ArrayList<ForgeDirection> realDirections = new ArrayList<ForgeDirection>(); Multiset<ForgeDirection> realDirections = HashMultiset.create(6);
for (ForgeDirection direction : directions) { for (ForgeDirection direction : directions) {
if (transferState[direction.ordinal()] == TransferState.Output) { if (transferState[direction.ordinal()] == TransferState.Output) {
realDirections.add(direction); realDirections.add(direction);
} }
} }
container.pipe.eventBus.handleEvent(PipeEventFluid.FindDest.class, new PipeEventFluid.FindDest(container.pipe, pushStack, realDirections)); container.pipe.eventBus.handleEvent(PipeEventFluid.FindDest.class, new PipeEventFluid.FindDest(container.pipe, pushStack, realDirections));
for (ForgeDirection direction : realDirections) { for (ForgeDirection direction : realDirections.elementSet()) {
int available = internalTanks[direction.ordinal()].fill(testStack, false); int available = internalTanks[direction.ordinal()].fill(testStack, false);
int ammountToPush = (int) (available / (double) flowRate / outputCount * Math.min(flowRate, totalAvailable)); int ammountToPush = (int) (available / (double) flowRate / realDirections.size() * Math.min(flowRate, totalAvailable) * realDirections.count(direction));
if (ammountToPush < 1) { if (ammountToPush < 1) {
ammountToPush++; ammountToPush++;
} }

View file

@ -9,7 +9,9 @@
package buildcraft.transport.pipes; package buildcraft.transport.pipes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@ -128,39 +130,36 @@ public class PipeFluidsDiamond extends Pipe<PipeTransportFluids> implements IDia
public void eventHandler(PipeEventFluid.FindDest event) { public void eventHandler(PipeEventFluid.FindDest event) {
Fluid fluidInTank = event.fluidStack.getFluid(); Fluid fluidInTank = event.fluidStack.getFluid();
List<ForgeDirection> removedDestinations = new ArrayList<ForgeDirection>(); Set<ForgeDirection> originalDestinations = new HashSet<ForgeDirection>();
originalDestinations.addAll(event.destinations.elementSet());
boolean isFiltered = true;
int[] filterCount = new int[6];
boolean[] validFilter = new boolean[6]; for (ForgeDirection dir : originalDestinations) {
boolean isFiltered = false;
for (ForgeDirection dir : event.destinations) {
if (container.isPipeConnected(dir) && filters.filteredDirections[dir.ordinal()]) { if (container.isPipeConnected(dir) && filters.filteredDirections[dir.ordinal()]) {
for (int slot = dir.ordinal() * 9; slot < dir.ordinal() * 9 + 9; ++slot) { for (int slot = dir.ordinal() * 9; slot < dir.ordinal() * 9 + 9; ++slot) {
if (filters.fluids[slot] != null && filters.fluids[slot].getID() == fluidInTank.getID()) { if (filters.fluids[slot] != null && filters.fluids[slot].getID() == fluidInTank.getID()) {
validFilter[dir.ordinal()] = true; filterCount[dir.ordinal()]++;
isFiltered = true; isFiltered = true;
break;
} }
} }
} }
} }
for (ForgeDirection to : event.destinations) { event.destinations.clear();
// the direction is filtered and liquids match
if (filters.filteredDirections[to.ordinal()] && validFilter[to.ordinal()]) {
continue;
}
// we haven't found a filter for this liquid and the direction is free if (!isFiltered) {
if (!isFiltered && !filters.filteredDirections[to.ordinal()]) { for (ForgeDirection to : originalDestinations) {
continue; if (!filters.filteredDirections[to.ordinal()]) {
event.destinations.add(to);
}
}
} else {
for (ForgeDirection to : originalDestinations) {
if (filterCount[to.ordinal()] > 0) {
event.destinations.add(to, filterCount[to.ordinal()]);
} }
// we have a filter for the liquid, but not a valid direction
removedDestinations.add(to);
} }
for (ForgeDirection dir : removedDestinations) {
event.destinations.remove(dir);
} }
} }

View file

@ -1,6 +1,7 @@
package buildcraft.transport.pipes.events; package buildcraft.transport.pipes.events;
import java.util.List; import java.util.List;
import com.google.common.collect.Multiset;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import buildcraft.transport.Pipe; import buildcraft.transport.Pipe;
@ -14,9 +15,9 @@ public abstract class PipeEventFluid extends PipeEvent {
} }
public static class FindDest extends PipeEventFluid { public static class FindDest extends PipeEventFluid {
public final List<ForgeDirection> destinations; public final Multiset<ForgeDirection> destinations;
public FindDest(Pipe pipe, FluidStack fluidStack, List<ForgeDirection> destinations) { public FindDest(Pipe pipe, FluidStack fluidStack, Multiset<ForgeDirection> destinations) {
super(pipe, fluidStack); super(pipe, fluidStack);
this.destinations = destinations; this.destinations = destinations;
} }