|
| 1 | +package sunsetsatellite.catalyst.core.util.network; |
| 2 | + |
| 3 | +import com.google.common.collect.Maps; |
| 4 | +import com.mojang.nbt.CompoundTag; |
| 5 | +import com.mojang.nbt.ListTag; |
| 6 | +import net.minecraft.core.block.Block; |
| 7 | +import net.minecraft.core.block.entity.TileEntity; |
| 8 | +import net.minecraft.core.util.helper.Color; |
| 9 | +import net.minecraft.core.world.World; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import sunsetsatellite.catalyst.Catalyst; |
| 12 | +import sunsetsatellite.catalyst.core.util.Direction; |
| 13 | +import sunsetsatellite.catalyst.core.util.Vec3i; |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | + |
| 17 | +public class Network { |
| 18 | + |
| 19 | + protected final Map<Vec3i, NetworkComponent> networkBlocks = Maps.newHashMap(); |
| 20 | + protected final Map<Vec3i, BlockEntry> blocks = Maps.newHashMap(); |
| 21 | + protected final World world; |
| 22 | + protected final int id; |
| 23 | + protected final NetworkPathMap NET_PATH_DATA = new NetworkPathMap(); |
| 24 | + protected final Random random; |
| 25 | + public final Color color; |
| 26 | + public final @NotNull NetworkType type; |
| 27 | + |
| 28 | + public Network(World world, @NotNull NetworkType type) { |
| 29 | + this(world, NetworkManager.getUID(), type); |
| 30 | + } |
| 31 | + |
| 32 | + private Network(World world, int id, @NotNull NetworkType type) { |
| 33 | + this.world = world; |
| 34 | + this.id = id; |
| 35 | + this.type = type; |
| 36 | + this.random = new Random(id); |
| 37 | + color = new Color().setRGBA(random.nextInt(255),random.nextInt(255),random.nextInt(255),255); |
| 38 | + } |
| 39 | + |
| 40 | + public List<NetworkPath> getPathData(Vec3i pos){ |
| 41 | + List<NetworkPath> routes = NET_PATH_DATA.get(pos); |
| 42 | + if(routes == null){ |
| 43 | + routes = NetworkWalker.createNetworkPaths(world,pos); |
| 44 | + if(routes == null){ |
| 45 | + return Collections.emptyList(); |
| 46 | + } |
| 47 | + routes.sort(Comparator.comparingInt(NetworkPath::getDistance)); |
| 48 | + NET_PATH_DATA.put(pos,routes); |
| 49 | + } |
| 50 | + return routes; |
| 51 | + } |
| 52 | + |
| 53 | + public int getSize() { |
| 54 | + return blocks.size(); |
| 55 | + } |
| 56 | + |
| 57 | + public boolean existsOnPos(int x, int y, int z) { |
| 58 | + Vec3i pos = new Vec3i(x, y, z); |
| 59 | + return blocks.containsKey(pos); |
| 60 | + } |
| 61 | + |
| 62 | + public void addBlock(int x, int y, int z) { |
| 63 | + Block block = Block.blocksList[world.getBlockId(x, y, z)]; |
| 64 | + byte meta = (byte) world.getBlockMetadata(x, y, z); |
| 65 | + |
| 66 | + Vec3i pos = new Vec3i(x, y, z); |
| 67 | + blocks.put(pos, new BlockEntry(block, meta)); |
| 68 | + if (block instanceof NetworkComponent) { |
| 69 | + networkBlocks.put(pos, (NetworkComponent) block); |
| 70 | + if(world.getBlockTileEntity(x,y,z) instanceof NetworkComponentTile){ |
| 71 | + ((NetworkComponentTile) world.getBlockTileEntity(x,y,z)).networkChanged(this); |
| 72 | + } |
| 73 | + update(); |
| 74 | + } |
| 75 | + NET_PATH_DATA.clear(); |
| 76 | + } |
| 77 | + |
| 78 | + public List<Network> removeBlock(int x, int y, int z) { |
| 79 | + Vec3i pos = new Vec3i(x, y, z); |
| 80 | + NetworkComponent component = networkBlocks.get(pos); |
| 81 | + if (component != null) { |
| 82 | + if(world.getBlockTileEntity(x,y,z) instanceof NetworkComponentTile){ |
| 83 | + ((NetworkComponentTile) world.getBlockTileEntity(x,y,z)).removedFromNetwork(this); |
| 84 | + } |
| 85 | + } |
| 86 | + networkBlocks.remove(pos); |
| 87 | + blocks.remove(pos); |
| 88 | + update(); |
| 89 | + |
| 90 | + List<Vec3i> sideNets = new ArrayList<>(6); |
| 91 | + for (byte i = 0; i < 6; i++) { |
| 92 | + Vec3i offset = Direction.getVecs()[i]; |
| 93 | + Vec3i side = new Vec3i(x + offset.x, y + offset.y, z + offset.z); |
| 94 | + if (blocks.containsKey(side)) { |
| 95 | + sideNets.add(side); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + List<Set<Vec3i>> preNets = new ArrayList<>(); |
| 100 | + boolean[] ignore = new boolean[sideNets.size()]; |
| 101 | + for (byte i = 0; i < ignore.length; i++) { |
| 102 | + if (ignore[i]) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + Vec3i startBlock = sideNets.get(i); |
| 106 | + Set<Vec3i> netBlocks = floodFill(startBlock); |
| 107 | + preNets.add(netBlocks); |
| 108 | + if (i < ignore.length - 1) { |
| 109 | + for (byte j = (byte) (i + 1); j < ignore.length; j++) { |
| 110 | + if (netBlocks.contains(sideNets.get(j))) { |
| 111 | + ignore[j] = true; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + final int size = preNets.size(); |
| 118 | + if (size < 2) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + List<Network> result = new ArrayList<>(size); |
| 123 | + for (Set<Vec3i> preNet : preNets) { |
| 124 | + Network sideNet = new Network(world,type); |
| 125 | + |
| 126 | + preNet.forEach(blockPos -> { |
| 127 | + sideNet.blocks.put(blockPos, blocks.get(blockPos)); |
| 128 | + NetworkComponent netBlock = networkBlocks.get(blockPos); |
| 129 | + if (netBlock != null) { |
| 130 | + sideNet.networkBlocks.put(blockPos, netBlock); |
| 131 | + TileEntity tile = world.getBlockTileEntity(blockPos.x, blockPos.y, blockPos.z); |
| 132 | + if(tile instanceof NetworkComponentTile){ |
| 133 | + ((NetworkComponentTile) tile).networkChanged(sideNet); |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + if (sideNet.getSize() > 1) { |
| 139 | + result.add(sideNet); |
| 140 | + sideNet.update(); |
| 141 | + } |
| 142 | + } |
| 143 | + NET_PATH_DATA.clear(); |
| 144 | + return result; |
| 145 | + } |
| 146 | + |
| 147 | + public void mergeNetwork(Network net) { |
| 148 | + if(net.isOfSameType(net)) { |
| 149 | + blocks.putAll(net.blocks); |
| 150 | + networkBlocks.putAll(net.networkBlocks); |
| 151 | + } |
| 152 | + networkBlocks.forEach((pos, networkComponent) -> { |
| 153 | + TileEntity tile = world.getBlockTileEntity(pos.x, pos.y, pos.z); |
| 154 | + if(tile instanceof NetworkComponentTile){ |
| 155 | + ((NetworkComponentTile) tile).networkChanged(net); |
| 156 | + } |
| 157 | + }); |
| 158 | + NET_PATH_DATA.clear(); |
| 159 | + } |
| 160 | + |
| 161 | + public CompoundTag toTag() { |
| 162 | + CompoundTag net = new CompoundTag(); |
| 163 | + ListTag positions = new ListTag(); |
| 164 | + net.put("blocks", positions); |
| 165 | + net.putInt("id", id); |
| 166 | + net.putString("type",type.type); |
| 167 | + |
| 168 | + blocks.forEach((pos, entry) -> { |
| 169 | + CompoundTag tag = new CompoundTag(); |
| 170 | + tag.putInt("x", pos.x); |
| 171 | + tag.putInt("y", pos.y); |
| 172 | + tag.putInt("z", pos.z); |
| 173 | + tag.putInt("id", entry.block.id); |
| 174 | + tag.putInt("meta", entry.meta); |
| 175 | + positions.addTag(tag); |
| 176 | + }); |
| 177 | + |
| 178 | + return net; |
| 179 | + } |
| 180 | + |
| 181 | + public static Network fromTag(World world, CompoundTag root) { |
| 182 | + int id = root.getInteger("id"); |
| 183 | + ListTag positions = root.getList("blocks"); |
| 184 | + NetworkType networkType = new NetworkType(root.getString("type")); |
| 185 | + Network net = new Network(world, id, networkType); |
| 186 | + |
| 187 | + final int size = positions.tagCount(); |
| 188 | + for (int i = 0; i < size; i++) { |
| 189 | + CompoundTag tag = (CompoundTag) positions.tagAt(i); |
| 190 | + Block block = Block.blocksList[tag.getInteger("id")]; |
| 191 | + if (block != null) { |
| 192 | + int x = tag.getInteger("x"); |
| 193 | + int y = tag.getInteger("y"); |
| 194 | + int z = tag.getInteger("z"); |
| 195 | + byte meta = tag.getByte("meta"); |
| 196 | + net.blocks.put(new Vec3i(x, y, z), new BlockEntry(block, meta)); |
| 197 | + if(NetworkManager.canBeNet(block)){ |
| 198 | + net.networkBlocks.put(new Vec3i(x, y, z), (NetworkComponent) block); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + return net; |
| 204 | + } |
| 205 | + |
| 206 | + private Set<Vec3i> floodFill(Vec3i start) { |
| 207 | + List<Set<Vec3i>> edges = new ArrayList<>(); |
| 208 | + Set<Vec3i> result = new HashSet<>(); |
| 209 | + edges.add(Catalyst.setOf(start)); |
| 210 | + edges.add(new HashSet<>()); |
| 211 | + |
| 212 | + byte n = 0; |
| 213 | + boolean added = true; |
| 214 | + while (added) { |
| 215 | + Set<Vec3i> oldEdge = edges.get(n & 1); |
| 216 | + Set<Vec3i> newEdge = edges.get((n + 1) & 1); |
| 217 | + n = (byte) ((n + 1) & 1); |
| 218 | + oldEdge.forEach(pos -> { |
| 219 | + for (byte i = 0; i < 6; i++) { |
| 220 | + Vec3i offset = Direction.getVecs()[i]; |
| 221 | + Vec3i side = new Vec3i(pos.x + offset.x, pos.y + offset.y, pos.z + offset.z); |
| 222 | + if (blocks.containsKey(side) && !result.contains(side)) { |
| 223 | + newEdge.add(side); |
| 224 | + } |
| 225 | + } |
| 226 | + }); |
| 227 | + added = !oldEdge.isEmpty(); |
| 228 | + result.addAll(oldEdge); |
| 229 | + oldEdge.clear(); |
| 230 | + } |
| 231 | + |
| 232 | + return result; |
| 233 | + } |
| 234 | + |
| 235 | + public void update() { |
| 236 | + networkBlocks.forEach((pos, networkComponent) -> { |
| 237 | + TileEntity tile = world.getBlockTileEntity(pos.x, pos.y, pos.z); |
| 238 | + if(tile instanceof NetworkComponentTile){ |
| 239 | + ((NetworkComponentTile) tile).networkChanged(this); |
| 240 | + } |
| 241 | + }); |
| 242 | + } |
| 243 | + |
| 244 | + public boolean isOfSameType(NetworkComponent component){ |
| 245 | + return component.getType().equals(type); |
| 246 | + } |
| 247 | + |
| 248 | + public boolean isOfSameType(Network net){ |
| 249 | + return net.type.equals(type); |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + public int hashCode() { |
| 254 | + return id; |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public boolean equals(Object obj) { |
| 259 | + if (obj == this) { |
| 260 | + return true; |
| 261 | + } |
| 262 | + if (obj instanceof Network) { |
| 263 | + Network net = (Network) obj; |
| 264 | + Optional<Vec3i> optional = net.blocks.keySet().stream().findAny(); |
| 265 | + if (optional.isPresent()) { |
| 266 | + Vec3i pos = optional.get(); |
| 267 | + return blocks.containsKey(pos); |
| 268 | + } |
| 269 | + } |
| 270 | + return false; |
| 271 | + } |
| 272 | + |
| 273 | + public String toString() { |
| 274 | + return String.format("[ID: %d, Size: %d]", id, networkBlocks.size()); |
| 275 | + } |
| 276 | + |
| 277 | + protected static class BlockEntry { |
| 278 | + Block block; |
| 279 | + byte meta; |
| 280 | + |
| 281 | + private BlockEntry(Block block, byte meta) { |
| 282 | + this.block = block; |
| 283 | + this.meta = meta; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | +} |
0 commit comments