From 27bfc697f99bac048136e8ccdf942db48599bd55 Mon Sep 17 00:00:00 2001 From: tgame14 Date: Thu, 17 Apr 2014 11:41:48 +0300 Subject: [PATCH] Adding comparators to sort through nodes, including logic to choose target for items to fire at --- .../electrical/itemrailing/GridRailing.java | 15 ++-- .../electrical/itemrailing/NodeRailing.java | 4 +- .../electrical/itemrailing/PartRailing.java | 16 +++- .../RailingDistanceComparator.java | 74 +++++++++++++++++++ .../itemrailing/interfaces/IItemRailing.java | 7 ++ 5 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 electrical/src/main/scala/resonantinduction/electrical/itemrailing/RailingDistanceComparator.java diff --git a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/GridRailing.java b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/GridRailing.java index 66091f2a..b6c9a89d 100644 --- a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/GridRailing.java +++ b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/GridRailing.java @@ -4,6 +4,7 @@ import calclavia.lib.grid.NodeGrid; import com.google.common.collect.Sets; import resonantinduction.electrical.itemrailing.interfaces.IItemRailing; import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer; +import universalelectricity.api.vector.IVector3; import java.util.*; @@ -27,21 +28,17 @@ public class GridRailing extends NodeGrid return findNearestColoredTarget(itemwrapper); } - //TODO: Finish public IItemRailing findNearestInventory(IItemRailingTransfer itemwrapper) { - IItemRailing endGoal = null; IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray(); - - for (NodeRailing node : this.getNodes()) - { - - } - return null; + Arrays.sort(arr, new RailingDistanceComparator.RailingInventoryDistanceComparator()); + return arr[0]; } public IItemRailing findNearestColoredTarget(IItemRailingTransfer itemwrapper) { - return null; + IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray(); + Arrays.sort(arr, new RailingDistanceComparator.RailingColoredDistanceComparator(itemwrapper.getColor())); + return arr[0]; } } diff --git a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/NodeRailing.java b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/NodeRailing.java index 066cd3ac..a4bfe601 100644 --- a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/NodeRailing.java +++ b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/NodeRailing.java @@ -32,7 +32,7 @@ public class NodeRailing extends Node im * * @return possibly null, a IInventory to target */ - public IInventory getInventoriesNearby() + public IInventory[] getInventoriesNearby() { List invList = new ArrayList(); for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) @@ -43,7 +43,7 @@ public class NodeRailing extends Node im invList.add((IInventory) te); } } - return invList.get(parent.getWorldPos().world().rand.nextInt(invList.size())); + return (IInventory[]) invList.toArray(); } public boolean isLeaf() diff --git a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/PartRailing.java b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/PartRailing.java index 566a3d8d..c13e41f4 100644 --- a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/PartRailing.java +++ b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/PartRailing.java @@ -27,7 +27,7 @@ import java.lang.reflect.InvocationTargetException; * @since 16/03/14 * @author tgame14 */ -public class PartRailing extends PartFramedConnection implements IConductor, TSlottedPart, JNormalOcclusion, IHollowConnect, IItemRailing +public class PartRailing extends PartFramedConnection implements IConductor, IItemRailing { public static enum EnumRailing @@ -102,6 +102,20 @@ public class PartRailing extends PartFramedConnection +{ + + @Override + public int compare(IItemRailing o1, IItemRailing o2) + { + return (int) o1.getWorldPos().floor().distance(o2.getWorldPos()); + } + + public static class RailingInventoryDistanceComparator extends RailingDistanceComparator + { + @Override + public int compare(IItemRailing o1, IItemRailing o2) + { + if (o1.getInventoriesNearby() != null && o2.getInventoriesNearby() != null) + { + return super.compare(o1, o2); + } + + else if (o1.getInventoriesNearby() == null && o2.getInventoriesNearby() == null) + { + return super.compare(o1, o2); + } + + if (o1.getInventoriesNearby() != null) + { + return 1; + } + return -1; + } + } + + public static class RailingColoredDistanceComparator extends RailingDistanceComparator + { + private EnumColor color; + + public RailingColoredDistanceComparator(EnumColor color) + { + this.color = color; + } + + @Override + public int compare(IItemRailing o1, IItemRailing o2) + { + if (o1.getRailingColor() == o2.getRailingColor()) + { + return super.compare(o1, o2); + } + + else if (color == o1.getRailingColor()) + { + return 1; + } + + else if (color == o2.getRailingColor()) + { + return -1; + } + + return super.compare(o1, o2); + } + } +} diff --git a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/interfaces/IItemRailing.java b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/interfaces/IItemRailing.java index 32bafe83..7607ce27 100644 --- a/electrical/src/main/scala/resonantinduction/electrical/itemrailing/interfaces/IItemRailing.java +++ b/electrical/src/main/scala/resonantinduction/electrical/itemrailing/interfaces/IItemRailing.java @@ -2,7 +2,9 @@ package resonantinduction.electrical.itemrailing.interfaces; import calclavia.lib.grid.INodeProvider; import calclavia.lib.render.EnumColor; +import net.minecraft.inventory.IInventory; import net.minecraftforge.common.ForgeDirection; +import universalelectricity.api.vector.IVectorWorld; import universalelectricity.api.vector.VectorWorld; /** @@ -11,6 +13,7 @@ import universalelectricity.api.vector.VectorWorld; * @since 16/03/14 * @author tgame14 */ +// TODO: Fix up IVectorWorld implementation to somehow overturn error with ChickenBones Multiparts returning doubles public interface IItemRailing extends INodeProvider { public boolean canItemEnter (IItemRailingTransfer item); @@ -23,6 +26,10 @@ public interface IItemRailing extends INodeProvider public VectorWorld getWorldPos(); + public IInventory[] getInventoriesNearby(); + + public boolean isLeaf(); + }