Jump to content

Efficient cable network


MrRiegel

Recommended Posts

Hey guys,

 

I created a network with cables and one cable controller (similiar to AE).

The list of blockPos of the cables is a field in the cable controller.

Every time a cable is placed or removed I need to refresh the entire list (recursive).

Problem: If the network has many cables (>500), a refresh decreases the fps.

How can I prevent this?

AE doesn't have this problem.

 

Can I increases the fps by refreshing the list in another thread?

Link to comment
Share on other sites

You can do some optimizations:

 

  • If a cable is placed down, there are three cases you need to handle:
    • It has no connected cables. In this case create a new network
    • It has one connection to another network. In this case simply join the newly placed cable to the connected cable's network. Note I do not say one cable here, if you have three nearby cables but they are all on the same network you only have to join that one network still.
    • It has two or more connected networks. In this case you have to merge the networks. Choose the biggest network (most nodes) as the survivor, add the nodes from all other networks to the surviving one and kill all but the surviving network.

    [*]If a cable is destroyed, there are again three cases:

    • It has no connected cables. In this case simply destroy the network
    • It is connected to one cable (not network, every cable is only in one network), simply leave the network of that other cable.
    • It is connected to more than one cable. This is the most tricky bit, because here you actually have to traverse the world. For every attached cable create a Set (not a list!) of nodes. Start walking the attached cables adding nodes to the Set for that cable (recursively). If you at any point encounter a node that is already in the set for any other attached cable, you can stop searching on the current cable (e.g. you are moving along cable B and you find a node that is already in the set for cable A you can discard cable B in your calculations since it's connected to cable A). If at any point you only have one cable to traverse left (because all others have been removed by the previous step) you can stop and just leave that cable's network. Otherwise if you finish traversing all cables and have more than one Set of nodes left, create a new network for each set of nodes you have left and assign that network to the cable for that Set.
       
      This is quite complex, I know. Ask if you have questions :D

 

The above assumes that a Network is a List of nodes and every cable and connected block counts as a node. Every node knows it's network and thereby all the nodes it's connected to directly or indirectly.

Link to comment
Share on other sites

@Choonster

I did, but I don't get it. The AE code is pretty complicated.

 

@7

Thanks for your hints.

A cable should not have a network by itself. A controller is necessary.

And of course there's only one controller per network. If I connect 2 networks, one of the controllers drops.

 

current method to fill the list

private void addCables(final BlockPos pos) {
	for (BlockPos bl : getNeighbors(pos)) {
		if (worldObj.getTileEntity(bl) instanceof TileMaster && !bl.equals(this.pos) && worldObj.getChunkFromBlockCoords(bl) != null && worldObj.getChunkFromBlockCoords(bl).isLoaded()) {
			worldObj.getBlockState(bl).getBlock().dropBlockAsItem(worldObj, bl, worldObj.getBlockState(bl), 0);
			worldObj.setBlockToAir(bl);
			worldObj.removeTileEntity(bl);
			continue;
		}
		if (worldObj.getTileEntity(bl) instanceof IConnectable && !connectables.contains(bl) && worldObj.getChunkFromBlockCoords(bl).isLoaded()) {
			connectables.add(bl);
			((IConnectable) worldObj.getTileEntity(bl)).setMaster(this.pos);
			addCables(bl);
		}
	}

}

 

Link to comment
Share on other sites

What you call "controller" is what I called network. Network = collection of nodes. It's much easier to think of cables as nodes as well. When I did this, I did not have an explicit master. Just whenever something is placed that can be part of a network it either joins a nearby one or creates one itself.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.