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?

  • \$\begingroup\$ This currently looks like multiple distinct questions in one. To get useful answers quickly, I recommend slimming this down to ask just one question at a time. Your question about efficiently maintaining wire-like connections might be at least partly answered here: Efficiently determining which buildings are connected by roads. \$\endgroup\$
    – DMGregory
    Oct 28, 2023 at 12:48
  • \$\begingroup\$ Thank you for your suggestion, provided answer mostly satisfies how can I optimize energy grid calculations, so I'll remove that part. Apart from that how to plug in chunked map nature is still somewhat unclear to me. \$\endgroup\$
    – Zekfad
    Oct 28, 2023 at 18:41


You must log in to answer this question.

Browse other questions tagged .