More efficient transporter tree searching

This commit is contained in:
aidancbrady 2016-01-11 20:45:13 -05:00
parent b6306d0779
commit 72193075ab
5 changed files with 58 additions and 29 deletions

View file

@ -1,8 +1,10 @@
package mekanism.common.content.transporter; package mekanism.common.content.transporter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import mekanism.api.Coord4D; import mekanism.api.Coord4D;
@ -23,7 +25,7 @@ import cpw.mods.fml.common.Loader;
public class TransporterManager public class TransporterManager
{ {
public static Set<TransporterStack> flowingStacks = new HashSet<TransporterStack>(); public static Map<Coord4D, Set<TransporterStack>> flowingStacks = new HashMap<Coord4D, Set<TransporterStack>>();
public static void reset() public static void reset()
{ {
@ -32,19 +34,33 @@ public class TransporterManager
public static void add(TransporterStack stack) public static void add(TransporterStack stack)
{ {
flowingStacks.add(stack); Set<TransporterStack> set = new HashSet<TransporterStack>();
set.add(stack);
if(flowingStacks.get(stack.getDest()) == null)
{
flowingStacks.put(stack.getDest(), set);
}
else {
flowingStacks.get(stack.getDest()).addAll(set);
}
} }
public static void remove(TransporterStack stack) public static void remove(TransporterStack stack)
{ {
flowingStacks.remove(stack); if(stack.hasPath() && stack.pathType != Path.NONE)
{
flowingStacks.get(stack.getDest()).remove(stack);
}
} }
public static List<TransporterStack> getStacksToDest(Coord4D dest) public static List<TransporterStack> getStacksToDest(Coord4D dest)
{ {
List<TransporterStack> ret = new ArrayList<TransporterStack>(); List<TransporterStack> ret = new ArrayList<TransporterStack>();
for(TransporterStack stack : flowingStacks) if(flowingStacks.containsKey(dest))
{
for(TransporterStack stack : flowingStacks.get(dest))
{ {
if(stack != null && stack.pathType != Path.NONE && stack.hasPath()) if(stack != null && stack.pathType != Path.NONE && stack.hasPath())
{ {
@ -54,6 +70,7 @@ public class TransporterManager
} }
} }
} }
}
return ret; return ret;
} }

View file

@ -7,7 +7,6 @@ import java.util.List;
import mekanism.api.Coord4D; import mekanism.api.Coord4D;
import mekanism.api.EnumColor; import mekanism.api.EnumColor;
import mekanism.api.transmitters.IBlockableConnection;
import mekanism.common.PacketHandler; import mekanism.common.PacketHandler;
import mekanism.common.base.ILogisticalTransporter; import mekanism.common.base.ILogisticalTransporter;
import mekanism.common.base.ITransporterTile; import mekanism.common.base.ITransporterTile;
@ -31,7 +30,7 @@ public class TransporterStack
public ForgeDirection idleDir = ForgeDirection.UNKNOWN; public ForgeDirection idleDir = ForgeDirection.UNKNOWN;
public List<Coord4D> pathToTarget = new ArrayList<Coord4D>(); private List<Coord4D> pathToTarget = new ArrayList<Coord4D>();
public Coord4D originalLocation; public Coord4D originalLocation;
public Coord4D homeLocation; public Coord4D homeLocation;
@ -151,9 +150,26 @@ public class TransporterStack
return stack; return stack;
} }
public void setPath(List<Coord4D> path)
{
TransporterManager.remove(this);
pathToTarget = path;
if(pathType != Path.NONE)
{
TransporterManager.add(this);
}
}
public boolean hasPath() public boolean hasPath()
{ {
return pathToTarget != null && pathToTarget.size() >= 2; return getPath() != null && getPath().size() >= 2;
}
public List<Coord4D> getPath()
{
return pathToTarget;
} }
public ItemStack recalculatePath(ILogisticalTransporter transporter, int min) public ItemStack recalculatePath(ILogisticalTransporter transporter, int min)
@ -165,9 +181,9 @@ public class TransporterStack
return itemStack; return itemStack;
} }
pathToTarget = newPath.path;
pathType = Path.DEST; pathType = Path.DEST;
idleDir = ForgeDirection.UNKNOWN; idleDir = ForgeDirection.UNKNOWN;
setPath(newPath.path);
initiatedPath = true; initiatedPath = true;
return newPath.rejected; return newPath.rejected;
@ -182,9 +198,9 @@ public class TransporterStack
return itemStack; return itemStack;
} }
pathToTarget = newPath.path;
pathType = Path.DEST; pathType = Path.DEST;
idleDir = ForgeDirection.UNKNOWN; idleDir = ForgeDirection.UNKNOWN;
setPath(newPath.path);
initiatedPath = true; initiatedPath = true;
return newPath.rejected; return newPath.rejected;
@ -204,7 +220,7 @@ public class TransporterStack
idleDir = ForgeDirection.UNKNOWN; idleDir = ForgeDirection.UNKNOWN;
} }
pathToTarget = newPath; setPath(newPath);
originalLocation = transporter.coord(); originalLocation = transporter.coord();
initiatedPath = true; initiatedPath = true;
@ -310,7 +326,7 @@ public class TransporterStack
public Coord4D getDest() public Coord4D getDest()
{ {
return pathToTarget.get(0); return getPath().get(0);
} }
public static enum Path public static enum Path

View file

@ -20,7 +20,6 @@ import mekanism.common.tile.TileEntityLogisticalSorter;
import mekanism.common.util.InventoryUtils; import mekanism.common.util.InventoryUtils;
import mekanism.common.util.MekanismUtils; import mekanism.common.util.MekanismUtils;
import mekanism.common.util.TransporterUtils; import mekanism.common.util.TransporterUtils;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@ -80,7 +79,7 @@ public class MultipartTransporter extends MultipartTransmitter<IInventory, Inven
if(stack.hasPath()) if(stack.hasPath())
{ {
int currentIndex = stack.pathToTarget.indexOf(coord()); int currentIndex = stack.getPath().indexOf(coord());
if(currentIndex == 0) //Necessary for transition reasons, not sure why if(currentIndex == 0) //Necessary for transition reasons, not sure why
{ {
@ -88,7 +87,7 @@ public class MultipartTransporter extends MultipartTransmitter<IInventory, Inven
continue; continue;
} }
Coord4D next = stack.pathToTarget.get(currentIndex-1); Coord4D next = stack.getPath().get(currentIndex-1);
if(!stack.isFinal(this)) if(!stack.isFinal(this))
{ {
@ -287,7 +286,6 @@ public class MultipartTransporter extends MultipartTransmitter<IInventory, Inven
if(doEmit) if(doEmit)
{ {
transit.add(stack); transit.add(stack);
TransporterManager.add(stack);
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(coord(), getPart().getSyncPacket(stack, false)), new Range4D(coord())); Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(coord(), getPart().getSyncPacket(stack, false)), new Range4D(coord()));
MekanismUtils.saveChunk(getPart().tile()); MekanismUtils.saveChunk(getPart().tile());
} }
@ -323,7 +321,6 @@ public class MultipartTransporter extends MultipartTransmitter<IInventory, Inven
if(doEmit) if(doEmit)
{ {
transit.add(stack); transit.add(stack);
TransporterManager.add(stack);
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(coord(), getPart().getSyncPacket(stack, false)), new Range4D(coord())); Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(coord(), getPart().getSyncPacket(stack, false)), new Range4D(coord()));
MekanismUtils.saveChunk(getPart().tile()); MekanismUtils.saveChunk(getPart().tile());
} }

View file

@ -1,9 +1,10 @@
package mekanism.common.multipart; package mekanism.common.multipart;
import codechicken.lib.vec.Vector3;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.Collection;
import mekanism.api.Coord4D; import mekanism.api.Coord4D;
import mekanism.api.EnumColor; import mekanism.api.EnumColor;
import mekanism.api.Range4D; import mekanism.api.Range4D;
@ -35,9 +36,9 @@ import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IIcon; import net.minecraft.util.IIcon;
import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import codechicken.lib.vec.Vector3;
import java.util.ArrayList; import cpw.mods.fml.relauncher.Side;
import java.util.Collection; import cpw.mods.fml.relauncher.SideOnly;
public class PartLogisticalTransporter extends PartTransmitter<IInventory, InventoryNetwork> implements ITransporterTile public class PartLogisticalTransporter extends PartTransmitter<IInventory, InventoryNetwork> implements ITransporterTile
{ {
@ -341,7 +342,6 @@ public class PartLogisticalTransporter extends PartTransmitter<IInventory, Inven
TransporterStack stack = TransporterStack.readFromNBT(tagList.getCompoundTagAt(i)); TransporterStack stack = TransporterStack.readFromNBT(tagList.getCompoundTagAt(i));
getTransmitter().transit.add(stack); getTransmitter().transit.add(stack);
TransporterManager.add(stack);
} }
} }
} }

View file

@ -12,7 +12,6 @@ import mekanism.common.base.ITransporterTile;
import mekanism.common.content.transporter.TransporterManager; import mekanism.common.content.transporter.TransporterManager;
import mekanism.common.content.transporter.TransporterStack; import mekanism.common.content.transporter.TransporterStack;
import mekanism.common.tile.TileEntityLogisticalSorter; import mekanism.common.tile.TileEntityLogisticalSorter;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory; import net.minecraft.inventory.ISidedInventory;
@ -198,7 +197,7 @@ public final class TransporterUtils
{ {
float[] pos; float[] pos;
if(stack.pathToTarget != null) if(stack.hasPath())
{ {
pos = TransporterUtils.getStackPosition(tileEntity, stack, 0); pos = TransporterUtils.getStackPosition(tileEntity, stack, 0);
} }