Jump to content

TileEntity networks


Sync Views

Recommended Posts

I was thinking about working on something that would require some fairly complex interactions between linked tileentities (so I guess kind of like LogisiticPipes, rather than normal Buildcraft pipes with the way it needs to know about everything to route stuff).

 

Ideally Id like this to work by forming a network which has references to all the relevant TileEntity instances, and runs its logic once per tick.

 

However not sure about some conceptual issues:

[*]If I have a separate Network object, how do I handle loading and saving of this object?

 

Its not part of any one TileEntity, they might not even be loaded and saved at the same time. Likewise it isn't really owned by any specific block, so cant be a TileEntity itself, and there needs to be a way for any loaded TileEntity in the network to find this object.

 

[*]How do I handle if the Network spans multiple chunks? My current idea is to make it so in the networks update method, it checks all the chunks for connected devices and only updates if there all loaded (it needs to get the TileEntity for each of those x/y/z's anyway since unloading/reloading a chunk changes the TileEntity object references unless there is also some way around that).

 

[*]When to recreate the network graph. Finding all connected devices provided all relevant chunks are loaded is easy, but when to update it. My best idea right now is to make every thing that might be part of the network, including blocks that are just acting as wires/connections be a TileEntity that just references the network (however 1. is solved). However won't such a large number of TileEntity instances that do nothing degrade performance? I know people complain about build craft pipes, logistic pipes, alternatives, paintings, item frames, etc. and blame them when the server lags, and I can think of many ways the simple presence of a number of TileEntity instances in the chunk might hurt performance, but I have no idea if this is truly a noticeable amount.

 

Link to comment
Share on other sites

I had a go at this in my mod, which is currently still in development. It uses a separate Network object like you described, which is shared between all connected TileEntities. When a TileEntity gets destroyed or unloaded it removes itself from the network. And when a TileEntity is loaded, it adds itself to any nearby network, or if none are found it creates a new one. This works, because Minecraft works synchronously, that means no 2 TileEntities can load at the exact same time. Even if they were to be placed in the exact same tick, they would be created one after another.

It includes a simple event system which lets you sent events through the network, all TileEntities in the network will then be notified and can act accordingly.

It's still work in progress, for example when a TileEntity get's removed that has two or more neighbors which are part of the network, the entire network has to be rebuilt.

You can find the source on github: https://github.com/diesieben07/CameraCraft3/tree/master/src/main/java/de/take_weiland/mods/cameracraft/networking

The most important part is in the NetworkUtil class. An example of an actual TileEntity that is part of the Network is the CardReader: https://github.com/diesieben07/CameraCraft3/blob/master/src/main/java/de/take_weiland/mods/cameracraft/tileentity/TileCardReader.java

 

Hope this gives you a starting point.

Link to comment
Share on other sites

Not looked through all the code, but looks like you did what I suggested and made the cable blocks a tile entity. Then on an entities first update it will attempt to create, join or merge networks (so things like the card reader are also like a cable, it cant connect to two separate unrelated networks).

 

Also as far as I can tell you never save the Network but just create it on demand (for both client and server?)? That was something I wanted to do so that the network can maintain state, although thinking about it I guess the devices etc. could store the relevant state for themselves in any situation I can think of. So if part of the network gets unloaded everything carries on but that bit doesn't send/get events?

 

 

Link to comment
Share on other sites

Also as far as I can tell you never save the Network but just create it on demand (for both client and server?)?

Yes. It never gets saved and only created on demand. So far the network is only present on the server, as the client doesn't need it.
That was something I wanted to do so that the network can maintain state, although thinking about it I guess the devices etc. could store the relevant state for themselves in any situation I can think of. So if part of the network gets unloaded everything carries on but that bit doesn't send/get events?

Also yes. That was my though process as well. Because the network doesn't really belong anywhere in the world, when should it be loaded, when unloaded? You can't bind it to chunks, because networks might cross chunk borders. It just all gets haywire. So every TE saves it's state on its own and recreates the network when needed.
Link to comment
Share on other sites

Think I got a basic network working. Had some issues with a multiple block machine. I didn't want to allow machines to allow through traffic, or confusing connected to multiple unique networks at once (for now at least, perhaps later I'll let machines have multiple terminals) so the machine would often just connect to itself and reject the cable connection lol. Think I solved that with a "TileEntity INetworkProxy.getReal()" method and more complex getAdjacentNetworks.

 

Didn't see any such things in your code, suspect when I test more tomorrow its going to give me lots of problems if the machine itself crosses a chunk boundary and is only partly loaded since I guess the proxy code (including for ISidedInventory) can fail giving me an object that claims to implement an interface but cant actually do anything with it...

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

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