Adding comparators to sort through nodes, including logic to choose target for items to fire at

This commit is contained in:
tgame14 2014-04-17 11:41:48 +03:00
parent 29b5af3e51
commit 27bfc697f9
5 changed files with 104 additions and 12 deletions

View file

@ -4,6 +4,7 @@ import calclavia.lib.grid.NodeGrid;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing; import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer; import resonantinduction.electrical.itemrailing.interfaces.IItemRailingTransfer;
import universalelectricity.api.vector.IVector3;
import java.util.*; import java.util.*;
@ -27,21 +28,17 @@ public class GridRailing extends NodeGrid<NodeRailing>
return findNearestColoredTarget(itemwrapper); return findNearestColoredTarget(itemwrapper);
} }
//TODO: Finish
public IItemRailing findNearestInventory(IItemRailingTransfer itemwrapper) public IItemRailing findNearestInventory(IItemRailingTransfer itemwrapper)
{ {
IItemRailing endGoal = null;
IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray(); IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray();
Arrays.sort(arr, new RailingDistanceComparator.RailingInventoryDistanceComparator());
for (NodeRailing node : this.getNodes()) return arr[0];
{
}
return null;
} }
public IItemRailing findNearestColoredTarget(IItemRailingTransfer itemwrapper) public IItemRailing findNearestColoredTarget(IItemRailingTransfer itemwrapper)
{ {
return null; IItemRailing[] arr = (IItemRailing[]) this.getNodes().toArray();
Arrays.sort(arr, new RailingDistanceComparator.RailingColoredDistanceComparator(itemwrapper.getColor()));
return arr[0];
} }
} }

View file

@ -32,7 +32,7 @@ public class NodeRailing extends Node<IItemRailing, GridRailing, NodeRailing> im
* *
* @return possibly null, a IInventory to target * @return possibly null, a IInventory to target
*/ */
public IInventory getInventoriesNearby() public IInventory[] getInventoriesNearby()
{ {
List<IInventory> invList = new ArrayList<IInventory>(); List<IInventory> invList = new ArrayList<IInventory>();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
@ -43,7 +43,7 @@ public class NodeRailing extends Node<IItemRailing, GridRailing, NodeRailing> im
invList.add((IInventory) te); invList.add((IInventory) te);
} }
} }
return invList.get(parent.getWorldPos().world().rand.nextInt(invList.size())); return (IInventory[]) invList.toArray();
} }
public boolean isLeaf() public boolean isLeaf()

View file

@ -27,7 +27,7 @@ import java.lang.reflect.InvocationTargetException;
* @since 16/03/14 * @since 16/03/14
* @author tgame14 * @author tgame14
*/ */
public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, IConductor, IEnergyNetwork> implements IConductor, TSlottedPart, JNormalOcclusion, IHollowConnect, IItemRailing public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, IConductor, IEnergyNetwork> implements IConductor, IItemRailing
{ {
public static enum EnumRailing public static enum EnumRailing
@ -102,6 +102,20 @@ public class PartRailing extends PartFramedConnection<PartRailing.EnumRailing, I
return new VectorWorld(getWorld(), x(), y(), z()); return new VectorWorld(getWorld(), x(), y(), z());
} }
//TODO: Handle the part as more of simply a host of Nodes, instead of the node itself
@Override
public IInventory[] getInventoriesNearby()
{
return node.getInventoriesNearby();
}
@Override
public boolean isLeaf()
{
return node.isLeaf();
}
@Override @Override
public float getResistance () public float getResistance ()
{ {

View file

@ -0,0 +1,74 @@
package resonantinduction.electrical.itemrailing;
import calclavia.lib.render.EnumColor;
import resonantinduction.electrical.itemrailing.interfaces.IItemRailing;
import java.util.Comparator;
/**
* @author tgame14
* @since 16/04/14
*/
public class RailingDistanceComparator implements Comparator<IItemRailing>
{
@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);
}
}
}

View file

@ -2,7 +2,9 @@ package resonantinduction.electrical.itemrailing.interfaces;
import calclavia.lib.grid.INodeProvider; import calclavia.lib.grid.INodeProvider;
import calclavia.lib.render.EnumColor; import calclavia.lib.render.EnumColor;
import net.minecraft.inventory.IInventory;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.vector.IVectorWorld;
import universalelectricity.api.vector.VectorWorld; import universalelectricity.api.vector.VectorWorld;
/** /**
@ -11,6 +13,7 @@ import universalelectricity.api.vector.VectorWorld;
* @since 16/03/14 * @since 16/03/14
* @author tgame14 * @author tgame14
*/ */
// TODO: Fix up IVectorWorld implementation to somehow overturn error with ChickenBones Multiparts returning doubles
public interface IItemRailing extends INodeProvider public interface IItemRailing extends INodeProvider
{ {
public boolean canItemEnter (IItemRailingTransfer item); public boolean canItemEnter (IItemRailingTransfer item);
@ -23,6 +26,10 @@ public interface IItemRailing extends INodeProvider
public VectorWorld getWorldPos(); public VectorWorld getWorldPos();
public IInventory[] getInventoriesNearby();
public boolean isLeaf();
} }