Skip to main content

Terrain Generation

info

This API is only available on servers.

Mods can implement their own terrain generators to customize the world as they see fit. A good use of a terrain generator is for minigame arenas. Using a terrain generator for an arena means that the chunks can easily be regenerated for the next round of the minigame.

Defining a World Generator

To generate custom terrain, a mod must extend the WorldGenerator class and implement the GenerateChunk abstract method. The Name attribute is used to give the generator a good human readable name, and the Key attribute is the key that the generator uses.

[Name("Custom Generator")]
[Key("custom_generator")]
public class CustomGenerator : WorldGenerator
{
public override void GenerateChunk(int chunkX, int chunkY, int chunkZ, ChunkData chunkData)
{
}
}

By default the chunk is filled with air voxels. To populate the chunk, the ChunkData.SetVoxel(x, y, z, VoxelDef) method is used. Since a chunk is 16x16x16, the SetVoxel x, y, and z parameters can only range from [0, 15] (inclusive).

Registering a World Generator

To register a world generator, the LoadGeneratorsStage method must be overrided in your mod's class that extends CubivoxMod.

[ServerOnly]
public override void LoadGeneratorsStage(GeneratorRegistry generatorRegistry)
{
generatorRegistry.RegisterWorldGenerator(this, new CustomGenerator());
}
note

LoadGeneratorsStage is only called on servers. It will never be executed on clients.

Examples

Spleef Arena

[Name("Cubivox Spleef")]
[Key("spleef")]
public class SpleefGenerator : WorldGenerator
{
private VoxelDef testVoxel;
private VoxelDef spleefVoxel;

public SpleefGenerator()
{
var testKey = ControllerKeyUtils.CubivoxControllerKey("testblock");
var spleefVoxelKey = ControllerKey.Create("cubivoxspleef", "spleefvoxel");

testVoxel = Cubivox.GetItemRegistry().GetVoxelDefinition( testKey );
spleefVoxel = Cubivox.GetItemRegistry().GetVoxelDefinition( spleefVoxelKey );
}

public override void GenerateChunk(int chunkX, int chunkY, int chunkZ, ChunkData chunkData)
{
// Main Platform
if (chunkX <= 1 && chunkX >= -1 &&
chunkY == 6 &&
chunkZ <= 1 && chunkZ >= 1)
{
for (var x = 0; x < 16; x++)
{
for (var z = 0; z < 16; z++)
{
chunkData.SetVoxel(x, 8, z, spleefVoxel);
}
}
}

// Spectator Platform
if (chunkX == 0 && chunkY == 7 && chunkZ == -2)
{
for (var x = 0; x < 16; x++)
{
for (var z = 0; z < 16; z++)
{
chunkData.SetVoxel(x, 8, z, testVoxel);
}
}
}
}
}