Minecraft Mod

Procedurally-Growing Wasp Nests

The mod adds large, dynamically-growing wasp hives that can grow to contain thousands of blocks. Starting as a small hive with a single heart block, it releases wasps with custom AI to harvest materials from around the hive (they eat glowstone and live in the Nether), which return home to provide energy. The hive then uses this energy to grow, adding wax which hardens into carapace, more ports for more wasps to enter and exist, and further hearts to expand the size and reach of the hive. A number of heuristics are employed to grow the hive into interesting shapes.

Designing and implementing these hives involved a few interesting problems.

Each hive can grow multiple hearts, possibly quite a large number of them as it expands, as each heart is the centre of an area of the hive. From a gameplay perspective, it should appear as if each heart is operating to grow the hive: each heart should have more wax and hive organs growing out of it. However, from a technical perspective, there are issues here. Firstly, each heart would need to be a ticking BlockEntity, which is computationally expensive in large numbers. Each would have to be ticked separately by the server each tick. Further, they would have to share a lot of data and continually send it between each other, such as the amount of energy the hive has, or how large it is. Instead, we want to do all the work in one place.

To do this, we assign the first heart of each hive as a master heart. This heart tracks all the energy for the entire hive system. When a new heart grows, instead of adding a ticking BlockEntity like the master heart, we add an inert dummy block with the same appearance, only holding a reference to the master heart's position. We then store that dummy heart's position in the master heart, and when the world is saved to disk, this position is serialised in the master heart's level data. When the server ticks the hive, it only ticks the master heart. This heart then simulates the actions of all its slaves, performing growth ticks from each heart's position. From the player's perspective, it now appears as if all hearts are real and work in the same way, but computationally, we only need to deal with a single heart.

"Failover"

This works when the hive is operating in isolation. However, the player can come along and destroy parts of the hive. Indeed, the point of the hearts is to give the player a way to combat the hive's growth, by targeting the hearts. If one master heart is performing all the processing, the hive will become inert if the player breaks that one specific heart. Further, we need to handle what happens when the player breaks a dummy heart, as from the player's perspective, it is this heart that is controlling the growth in the small surrounding region.

Thankfully, handling destroyed dummy hearts is easy. Because they hold no information, all we need to do is notify the master heart whenever a block of the dummy heart's type is destroyed, we know where the master heart is from that block's data, and it can remove its position from the master heart's list of slaves, so it will no longer grow in the surrounding area.

For the master heart, it is more complex. First, we need to see if there are any extant dummy hearts. Then, we must replace one with a fully fledged ticking heart block. Then we need to move all the data, such as the list of slave hearts, all the other listed organs, the energy, and so on, to this new heart. We also need to go through all the organs and update their references to the master heart, so they know which BlockEntity to notify if they are later destroyed. Now, the player can destroy any heart and see the same effect, as if the hearts were all operating separately.