Caffeine

Procedural

Reference World Last updated: Lua 12 · Dryad 0

Deterministic noise, chunk streaming and terrain generation helpers for procedural worlds.

All namespaces

Namespace: caffeine.procedural

hash#

Lua: caffeine.procedural.hash

text
hash(x, z, seed, salt) -> number

Deterministic hash of a cell, seed and salt.

ParametersTypeDescription
xnumberInteger X.
znumberInteger Z.
seednumberSeed.
saltnumberExtra salt.

Returns: number. Hash value (named hash01 in the engine).

Example

lua
local r = caffeine.procedural.hash(3, 4, 42, 0)

noise2d#

Lua: caffeine.procedural.noise2d

text
noise2d(x, z, seed) -> number

2D value noise.

ParametersTypeDescription
xnumberX coordinate.
znumberZ coordinate.
seednumberSeed.

Returns: number. Noise value.

Example

lua
local n = caffeine.procedural.noise2d(1.5, 2.5, 42)

fbm2d#

Lua: caffeine.procedural.fbm2d

text
fbm2d(x, z, seed, octaves, lacunarity, persistence) -> number

2D fractal Brownian motion noise.

ParametersTypeDescription
xnumberX coordinate.
znumberZ coordinate.
seednumberSeed.
octavesnumberOctave count.
lacunaritynumberFrequency multiplier per octave.
persistencenumberAmplitude multiplier per octave.

Returns: number. Noise value.

Example

lua
local h = caffeine.procedural.fbm2d(x * 0.02, z * 0.02, 42, 5, 2.0, 0.5)

worldToChunk#

Lua: caffeine.procedural.worldToChunk

text
worldToChunk(worldCoord, chunkSize) -> number

Converts a world coordinate to a chunk index.

ParametersTypeDescription
worldCoordnumberWorld coordinate.
chunkSizenumberChunk size in world units.

Returns: number. Chunk index.

Example

lua
local cx = caffeine.procedural.worldToChunk(pos.x, 64)

findTerrain#

Lua: caffeine.procedural.findTerrain

text
findTerrain() -> number

Finds the terrain entity.

Returns: number. Terrain entity id, or 0 if none.

Example

lua
local t = caffeine.procedural.findTerrain()

getFocusChunk#

Lua: caffeine.procedural.getFocusChunk

text
getFocusChunk(directorId, chunkSize, focusId) -> table

Returns the chunk the focus entity is in.

ParametersTypeDescription
directorIdnumberEntity id of the procedural world director.
chunkSizenumberChunk size, used if the director has no procedural settings.
focusIdnumberFocus entity id. Optional.

Returns: table. Table with cx and cz (0, 0 if unavailable).

Example

lua
local c = caffeine.procedural.getFocusChunk(dir, 64, player)
print(c.cx, c.cz)

isChunkLoaded#

Lua: caffeine.procedural.isChunkLoaded

text
isChunkLoaded(directorId, cx, cz) -> boolean

Checks whether a chunk is loaded for the director.

ParametersTypeDescription
directorIdnumberEntity id of the procedural world director.
cxnumberChunk X index.
cznumberChunk Z index.

Returns: boolean. true if the chunk is loaded.

Example

lua
print(caffeine.procedural.isChunkLoaded(dir, 0, 0))

unloadChunk#

Lua: caffeine.procedural.unloadChunk

text
unloadChunk(directorId, cx, cz)

Unloads a chunk of the director.

ParametersTypeDescription
directorIdnumberEntity id of the procedural world director.
cxnumberChunk X index.
cznumberChunk Z index.

Returns: none

Example

lua
caffeine.procedural.unloadChunk(dir, 0, 0)

stream#

Lua: caffeine.procedural.stream

text
stream(directorId, terrainId, cx, cz, preset)

Streams chunks around the given chunk, adding procedural settings to the director if needed.

ParametersTypeDescription
directorIdnumberEntity id of the procedural world director.
terrainIdnumberTerrain entity id. Optional.
cxnumberChunk X index.
cznumberChunk Z index.
presetstringPreset name. Optional.

Returns: none

Example

lua
caffeine.procedural.stream(dir, caffeine.procedural.findTerrain(), 0, 0)

generateTerrainChunk#

Lua: caffeine.procedural.generateTerrainChunk

text
generateTerrainChunk(terrainId, cx, cz, seed, preset)

Generates one terrain chunk.

ParametersTypeDescription
terrainIdnumberTerrain entity id.
cxnumberChunk X index.
cznumberChunk Z index.
seednumberSeed.
presetstringPreset name. Optional.

Returns: none

Example

lua
caffeine.procedural.generateTerrainChunk(terrain, 0, 0, 42)

spawnCube#

Lua: caffeine.procedural.spawnCube

text
spawnCube(directorId, cx, cz, x, y, z, sx, sy, sz) -> number

Spawns a cube entity tagged with the director and chunk.

ParametersTypeDescription
directorIdnumberEntity id of the procedural world director.
cxnumberChunk X index.
cznumberChunk Z index.
xnumberWorld X.
ynumberWorld Y.
znumberWorld Z.
sxnumberScale X.
synumberScale Y.
sznumberScale Z.

Returns: number. Id of the new entity, or 0 if no world.

Example

lua
local c = caffeine.procedural.spawnCube(dir, 0, 0, 1, 0.5, 1, 1, 1, 1)

fillTerrainNoise#

Lua: caffeine.procedural.fillTerrainNoise

text
fillTerrainNoise(terrainId, seed, amplitude)

Fills the terrain heightmap with fractal noise.

ParametersTypeDescription
terrainIdnumberTerrain entity id.
seednumberSeed.
amplitudenumberHeight multiplier; results are clamped to 0..1.

Returns: none

Example

lua
caffeine.procedural.fillTerrainNoise(terrain, 42, 1.0)