From 077c39f5bc757fce477a859c9d77074d77809226 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Tue, 12 May 2015 00:20:09 -0400 Subject: [PATCH] New BFS-based network search; search area increased to 25x25 cube centered on controller. --- build.gradle | 2 +- .../block/tile/TileEntityController.java | 74 +++++++++++-------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index 3e6be7c..24e7d88 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { apply plugin: 'forge' -version = "1.7.10-1.4.5" +version = "1.7.10-1.4.6" group= "com.jaquadro.minecraft.storagedrawers" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "StorageDrawers" diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index e15c31e..622fecd 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -52,6 +52,9 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS } } + private Queue searchQueue = new LinkedList(); + private Set searchDiscovered = new HashSet(); + private Map storage = new HashMap(); private List invSlotList = new ArrayList(); private List drawerSlotList = new ArrayList(); @@ -132,7 +135,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS resetCache(); - populateNode(xCoord, yCoord, zCoord, 0); + populateNodes(xCoord, yCoord, zCoord); flattenLists(); inventorySlots = sortInventorySlots(); @@ -298,42 +301,55 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IS } } - private void populateNode (int x, int y, int z, int depth) { - if (depth > DEPTH_LIMIT) - return; + private void populateNodes (int x, int y, int z) { + BlockCoord root = new BlockCoord(x, y, z); - Block block = worldObj.getBlock(x, y, z); - if (!(block instanceof INetworked)) - return; + searchQueue.clear(); + searchQueue.add(root); - BlockCoord coord = new BlockCoord(x, y, z); - StorageRecord record = storage.get(coord); - if (record == null) { - record = new StorageRecord(); - storage.put(coord, record); - } + searchDiscovered.clear(); + searchDiscovered.add(root); - if (block instanceof BlockSlave) { - ((BlockSlave) block).getTileEntitySafe(worldObj, x, y, z); - } + while (!searchQueue.isEmpty()) { + BlockCoord coord = searchQueue.remove(); + int depth = Math.max(Math.max(Math.abs(coord.x() - x), Math.abs(coord.y() - y)), Math.abs(coord.z() - z)); + if (depth > DEPTH_LIMIT) + continue; - updateRecordInfo(coord, record, worldObj.getTileEntity(x, y, z)); - record.mark = true; + Block block = worldObj.getBlock(coord.x(), coord.y(), coord.z()); + if (!(block instanceof INetworked)) + continue; - if (record.distance > depth) { + StorageRecord record = storage.get(coord); + if (record == null) { + record = new StorageRecord(); + storage.put(coord, record); + } + + if (block instanceof BlockSlave) { + ((BlockSlave) block).getTileEntitySafe(worldObj, coord.x(), coord.y(), coord.z()); + } + + updateRecordInfo(coord, record, worldObj.getTileEntity(coord.x(), coord.y(), coord.z())); record.mark = true; record.distance = depth; - populateNeighborNodes(x, y, z, depth + 1); - } - } - private void populateNeighborNodes (int x, int y, int z, int depth) { - populateNode(x - 1, y, z, depth); - populateNode(x + 1, y, z, depth); - populateNode(x, y - 1, z, depth); - populateNode(x, y + 1, z, depth); - populateNode(x, y, z - 1, depth); - populateNode(x, y, z + 1, depth); + BlockCoord[] neighbors = new BlockCoord[] { + new BlockCoord(coord.x() + 1, coord.y(), coord.z()), + new BlockCoord(coord.x() - 1, coord.y(), coord.z()), + new BlockCoord(coord.x(), coord.y(), coord.z() + 1), + new BlockCoord(coord.x(), coord.y(), coord.z() - 1), + new BlockCoord(coord.x(), coord.y() + 1, coord.z()), + new BlockCoord(coord.x(), coord.y() - 1, coord.z()), + }; + + for (BlockCoord n : neighbors) { + if (!searchDiscovered.contains(n)) { + searchQueue.add(n); + searchDiscovered.add(n); + } + } + } } private IDrawerGroup getGroupForInvSlot (int invSlot) {