I'm building a Server-side engine for 2D sandbox game similar to Terraria or Starbound.
The language I use is Dart (similar to JavaScript or C#).
Currently I'm trying to figure out how to make a good blocks system with advanced interactions similar to Minecraft redstone.
I've come up with a following system:
Static blocks - simple block that could only be placed or removed.
Dynamic blocks - support some interactions with world or between other dynamic block. Have their dedicated states and classes with logic.
I store blocks as an array of block IDs and references to states (if ID is higher than some value it's interpreted as a reference to list entry of chunk's dynamic blocks). Map is made of array of chunks.
In my current plan I have a system of events, where each event have origin and destination. Dynamic blocks generate events and accepts events on block tick.
Events are placed to global queue, then engine is supposed to execute them on a next tick.
This system have many flaws, some notable of them is: Event can potentially invalidate other events (say we moved block that generated other events) and I have no good solution for this. Block is only aware of neighbor blocks and can't produce more advanced events. Hard to find a place where to put player actions (like block placement or switch activation).
Next are questions that arise from these inconveniences.
How can I implement more advanced interactions, like an energy grid, that incorporates energy sources, consumers and wires? Such system is likely have to be aware of every block in a chain. Is there any good shortcuts and optimizations here? Example: Minecraft redstone triggers mechanisms in a single tick. Example 2: Terraria can switch multiple active blocks in a single tick.
How can I handle interactions on a chunk borders? What to do when neighbor chunk is unloaded? How to link that to advanced interactions?
Is there a better system that will make it easier to resolve these questions, I'm willing to start anew if it would make a better result?