Skip to content

atl/terrain.hpp

Terrain generation: sm.terrainData, sm.terrainTile, sm.terrainGeneration, sm.voxelTerrainCell, sm.terrainHarvestable and sm.tunnelGenerator, mostly for terrain scripts (sm::TerrainScript in script.hpp).

Data a world's terrain script saves once and reads back every time the world loads.

A terrain script usually builds its terrain in Create (from a seed, a list of tiles, and so on), stores what it needs with save(), and returns that data from load() in Load on every later start. The data belongs to the world, so each world has its own.

inline bool exists();

Checks whether this world has terrain data yet.

Call it in a terrain script's Load: false means the world is new and Create still has to generate the terrain, true means load() can read back what save() stored.

Returns bool: true if save() has been called for this world, false for a new world.

Lua: sm.terrainData.exists

inline Value load();

Reads the data that save() stored for this world.

Returns Value: the saved value, or nil if exists() is false.

Lua: sm.terrainData.load

template <class T> void save(const T &data);

Stores terrain data for this world and sends it to every client.

Call it on the server, usually at the end of Create. The data is kept with the world's save, so exists() and load() see it the next time the world starts.

Parameters

  • data (const T &): any value Lua can hold: a number, string, table, Vec3, ...

Lua: sm.terrainData.save

Reads the contents of .tile files: heights, colors, materials and the objects placed in each cell.

A tile is a square of cells made in the game's tile editor. Functions that read one cell take the tile's uuid (from tileUuid()) and the cell's position inside the tile, cellOffsetX and cellOffsetY, counted from 0 up to size() - 1.

inline Uuid tileUuid(StringView path);

Looks up the uuid of a tile file, which the other functions use to refer to the tile.

Parameters

  • path (StringView): the tile's path, for example "$CONTENT_DATA/Terrain/Tiles/start.tile".

Returns Uuid: the tile's uuid.

Lua: sm.terrainTile.getTileUuid

inline int size(StringView path);

The size of a tile, as the number of cells along one side. A tile is always square.

Parameters

  • path (StringView): the tile's path.

Returns int: the number of cells per side.

Lua: sm.terrainTile.getSize

inline String creatorId(StringView path);

The Steam id of the player who made a tile.

Parameters

  • path (StringView): the tile's path.

Returns String: the creator's id, as a string.

Lua: sm.terrainTile.getCreatorId

inline double heightAt(const Uuid &tile, int cellOffsetX, int cellOffsetY, int lod, int x, int y);

The terrain height at one vertex of a tile cell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • lod (int): the level of detail, 0 (full detail) to 5.
  • x (int): the vertex's x position inside the cell.
  • y (int): the vertex's y position inside the cell.

Returns double: the height in meters.

Lua: sm.terrainTile.getHeightAt

inline TerrainColor colorAt(const Uuid &tile, int cellOffsetX, int cellOffsetY, int lod, int x, int y);

The terrain color at one vertex of a tile cell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • lod (int): the level of detail, 0 (full detail) to 5.
  • x (int): the vertex's x position inside the cell.
  • y (int): the vertex's y position inside the cell.

Returns TerrainColor: the color's red, green and blue values, from 0 to 1.

Lua: sm.terrainTile.getColorAt

inline MaterialWeights materialAt(const Uuid &tile, int cellOffsetX, int cellOffsetY, int lod, int x, int y);

How much of each of the 8 ground materials is painted at one vertex of a tile cell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • lod (int): the level of detail, 0 (full detail) to 5.
  • x (int): the vertex's x position inside the cell.
  • y (int): the vertex's y position inside the cell.

Returns MaterialWeights: the 8 material weights, in the order of the world's material set.

Lua: sm.terrainTile.getMaterialAt

inline int clutterIdxAt(const Uuid &tile, int cellOffsetX, int cellOffsetY, int x, int y);

Which clutter (grass, small plants) grows at one point of a tile cell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • x (int): the point's x position inside the cell.
  • y (int): the point's y position inside the cell.

Returns int: the clutter index, or -1 for none.

Lua: sm.terrainTile.getClutterIdxAt

inline Value assetsForCell(const Uuid &tile, int cellOffsetX, int cellOffsetY, int sizeLevel);

The assets (rocks, buildings and other static meshes) placed in one tile cell.

The result is in the format a terrain script's GetAssetsForCell returns, so a script can pass it straight through.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • sizeLevel (int): which size class of assets to return, as the engine asks for them.

Returns Value: a list of asset tables.

Lua: sm.terrainTile.getAssetsForCell

inline Value harvestablesForCell(const Uuid &tile, int cellOffsetX, int cellOffsetY, int sizeLevel);

The harvestables (trees, stones, plants) placed in one tile cell, in the format of GetHarvestablesForCell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • sizeLevel (int): which size class of harvestables to return, 0 to 3.

Returns Value: a list of harvestable tables.

Lua: sm.terrainTile.getHarvestablesForCell

inline Value kinematicsForCell(const Uuid &tile, int cellOffsetX, int cellOffsetY, int sizeLevel);

The kinematics (moving scenery) placed in one tile cell, in the format of GetKinematicsForCell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • sizeLevel (int): which size class of kinematics to return, 0 to 3.

Returns Value: a list of kinematic tables.

Lua: sm.terrainTile.getKinematicsForCell

inline Value creationsForCell(const Uuid &tile, int cellOffsetX, int cellOffsetY);

The creations (blueprints) placed in one tile cell, in the format of GetCreationsForCell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.

Returns Value: a list of creation tables.

Lua: sm.terrainTile.getCreationsForCell

inline Value nodesForCell(const Uuid &tile, int cellOffsetX, int cellOffsetY);

The nodes (named points used by scripts, such as spawn points) placed in one tile cell, in the format of GetNodesForCell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.

Returns Value: a list of node tables.

Lua: sm.terrainTile.getNodesForCell

inline Value decalsForCell(const Uuid &tile, int cellOffsetX, int cellOffsetY);

The decals (painted ground details) placed in one tile cell, in the format of GetDecalsForCell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.

Returns Value: a list of decal tables.

Lua: sm.terrainTile.getDecalsForCell

inline Value prefabsForCell(const Uuid &tile, int cellOffsetX, int cellOffsetY);

The prefabs placed in one tile cell, in the format of GetPrefabsForCell.

Parameters

  • tile (const Uuid &): the tile's uuid, from tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.

Returns Value: a list of prefab tables.

Lua: sm.terrainTile.getPrefabsForCell

inline PrefabContent contentFromPrefab(StringView path, Optional<int> loadFlags = atl::none);

Reads everything placed in a prefab file.

Parameters

  • path (StringView): the prefab's path.
  • loadFlags (Optional<int>): which kinds of content to load, as the bit mask a terrain script's PrepareCell receives. Leave it out to load everything.

Returns PrefabContent: the prefab's creations, prefabs, nodes, assets, decals, harvestables and kinematics.

Lua: sm.terrainTile.getContentFromPrefab

Everything in a prefab file, as returned by contentFromPrefab(). Each field is a list in the same format as the matching ...ForCell function returns.

struct PrefabContent
Value creations, prefabs, nodes, assets, decals, harvestables, kinematics;

Storage that terrain scripts and game scripts share.

Terrain scripts run in their own Lua states on generator threads, so they can't reach the game script's variables. Values stored here under a key can be read from either side. "Game storage" is kept with the save and can be synced to clients; "temp data" only lasts for the current session.

template <class K> Value loadGameStorage(const K &key);

Reads a value from game storage.

Parameters

  • key (const K &): the key the value was stored under.

Returns Value: the stored value, or nil if nothing was stored under key.

Lua: sm.terrainGeneration.loadGameStorage

template <class K, class T> void saveGameStorage(const K &key, const T &data);

Stores a value in game storage and saves it, so later sessions can read it too. Clients don't get a copy; use saveAndSyncGameStorage() for that.

Parameters

  • key (const K &): the key to store the value under.
  • data (const T &): the value to store.

Lua: sm.terrainGeneration.saveGameStorage

template <class K, class T> void saveAndSyncGameStorage(const K &key, const T &data);

Stores a value in game storage, saves it, and sends it to every client.

Parameters

  • key (const K &): the key to store the value under.
  • data (const T &): the value to store.

Lua: sm.terrainGeneration.saveAndSyncGameStorage

template <class K, class T> void setGameStorageNoSave(const K &key, const T &data);

Stores a value in game storage for this session only, without saving it or sending it to clients.

Parameters

  • key (const K &): the key to store the value under.
  • data (const T &): the value to store.

Lua: sm.terrainGeneration.setGameStorageNoSave

template <class K, class T> void setAndSyncGameStorageNoSave(const K &key, const T &data);

Stores a value in game storage for this session only and sends it to every client. It isn't saved.

Parameters

  • key (const K &): the key to store the value under.
  • data (const T &): the value to store.

Lua: sm.terrainGeneration.setAndSyncGameStorageNoSave

template <class K> Value tempData(const K &key);

Reads a value stored with setTempData().

Parameters

  • key (const K &): the key the value was stored under.

Returns Value: the stored value, or nil if nothing was stored under key.

Lua: sm.terrainGeneration.getTempData

template <class K, class T> void setTempData(const K &key, const T &data);

Stores a temporary value that lasts until the game closes. It isn't saved or sent to clients.

Parameters

  • key (const K &): the key to store the value under.
  • data (const T &): the value to store.

Lua: sm.terrainGeneration.setTempData

Shapes the voxel terrain (the diggable ground) of the cell being generated.

Only usable while a terrain script generates a cell. Voxel data can be copied from tile cells or carved with convex hulls. Densities are normalized: 0 is empty, 1 is solid.

inline void copyTileCellVoxels(const Uuid &tile, int cellOffsetX, int cellOffsetY, int rotationStep, Optional<Vec3> srcPos = atl::none, Optional<Vec3> dstPos = atl::none, Optional<Vec3> srcSize = atl::none);

Copies the voxels of a tile cell into the cell being generated: all of them, or a box of chunks.

Parameters

  • tile (const Uuid &): the tile's uuid, from terrainTile::tileUuid().
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • rotationStep (int): how far to rotate the tile cell, in quarter turns (0 to 3).
  • srcPos (Optional<Vec3>): the first chunk to copy, in chunk coordinates inside the tile cell. Leave the last three arguments out to copy everything.
  • dstPos (Optional<Vec3>): where the box goes, in the generated cell's voxel grid.
  • srcSize (Optional<Vec3>): the size of the box to copy, in chunks.

Lua: sm.voxelTerrainCell.copyTileCellVoxels

inline void copyTileCellVoxelsOnlyAir(const Uuid &tile, int cellOffsetX, int cellOffsetY, int rotationStep, Vec3 srcPos, Vec3 dstPos, Vec3 srcSize);

Like copyTileCellVoxels(), but only copies the tile's empty voxels, carving holes and caves without adding ground.

Parameters

  • tile (const Uuid &): the tile's uuid.
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • rotationStep (int): how far to rotate the tile cell, in quarter turns (0 to 3).
  • srcPos (Vec3): the first chunk to copy, inside the tile cell.
  • dstPos (Vec3): where the box goes, in the generated cell's voxel grid.
  • srcSize (Vec3): the size of the box to copy, in chunks.

Lua: sm.voxelTerrainCell.copyTileCellVoxelsOnlyAir

inline void copyTileCellVoxelsOnlyFilled(const Uuid &tile, int cellOffsetX, int cellOffsetY, int rotationStep, Vec3 srcPos, Vec3 dstPos, Vec3 srcSize);

Like copyTileCellVoxels(), but only copies the tile's filled voxels, adding ground without removing any.

Parameters

  • tile (const Uuid &): the tile's uuid.
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • rotationStep (int): how far to rotate the tile cell, in quarter turns (0 to 3).
  • srcPos (Vec3): the first chunk to copy, inside the tile cell.
  • dstPos (Vec3): where the box goes, in the generated cell's voxel grid.
  • srcSize (Vec3): the size of the box to copy, in chunks.

Lua: sm.voxelTerrainCell.copyTileCellVoxelsOnlyFilled

inline void copyTileCellVoxelsOnlyAirAndMasked(const Uuid &tile, int cellOffsetX, int cellOffsetY, int rotationStep, Vec3 srcPos, Vec3 dstPos, Vec3 srcSize, int voxelFilter);

Like copyTileCellVoxelsOnlyAir(), but only carves into ground of the materials in voxelFilter.

Parameters

  • tile (const Uuid &): the tile's uuid.
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • rotationStep (int): how far to rotate the tile cell, in quarter turns (0 to 3).
  • srcPos (Vec3): the first chunk to copy, inside the tile cell.
  • dstPos (Vec3): where the box goes, in the generated cell's voxel grid.
  • srcSize (Vec3): the size of the box to copy, in chunks.
  • voxelFilter (int): a mask of the voxel materials that may be carved (sm.voxelTerrainCell.voxelFilter).

Lua: sm.voxelTerrainCell.copyTileCellVoxelsOnlyAirAndMasked

inline void writeTileCellVoxels(const Uuid &tile, int cellOffsetX, int cellOffsetY, int rotationStep);

Replaces the generated cell's voxels with all of a tile cell's voxels.

Parameters

  • tile (const Uuid &): the tile's uuid.
  • cellOffsetX (int): the cell's x position inside the tile.
  • cellOffsetY (int): the cell's y position inside the tile.
  • rotationStep (int): how far to rotate the tile cell, in quarter turns (0 to 3).

Lua: sm.voxelTerrainCell.writeTileCellVoxels

inline void setDensityConvexHull(const Vector<Vec3> &vertices, double margin, double density, Optional<int> voxelFilter = atl::none, const Noise &n = {});

Sets the density of every voxel inside a convex hull: 0 carves it out, 1 fills it.

Parameters

  • vertices (const Vector<Vec3> &): the corners of the hull, in the cell's voxel grid.
  • margin (double): extra distance around the hull that is affected too.
  • density (double): the new density, from 0 (empty) to 1 (solid).
  • voxelFilter (Optional<int>): a mask of the voxel materials that may change. Leave it out to change all.
  • n (const Noise &): optional noise to roughen the hull's surface.

Lua: sm.voxelTerrainCell.setDensityConvexHull

inline void setMaterialConvexHull(const Vector<Vec3> &vertices, double margin, int material, Optional<int> voxelFilter = atl::none, const Noise &n = {});

Sets the material of every voxel inside a convex hull, keeping its density.

Parameters

  • vertices (const Vector<Vec3> &): the corners of the hull, in the cell's voxel grid.
  • margin (double): extra distance around the hull that is affected too.
  • material (int): the new voxel material.
  • voxelFilter (Optional<int>): a mask of the voxel materials that may change. Leave it out to change all.
  • n (const Noise &): optional noise to roughen the hull's surface.

Lua: sm.voxelTerrainCell.setMaterialConvexHull

inline void setMaterialAndDensityConvexHull(const Vector<Vec3> &vertices, double margin, int material, double density, Optional<int> voxelFilter = atl::none, const Noise &n = {});

Sets both the material and the density of every voxel inside a convex hull.

Parameters

  • vertices (const Vector<Vec3> &): the corners of the hull, in the cell's voxel grid.
  • margin (double): extra distance around the hull that is affected too.
  • material (int): the new voxel material.
  • density (double): the new density, from 0 (empty) to 1 (solid).
  • voxelFilter (Optional<int>): a mask of the voxel materials that may change. Leave it out to change all.
  • n (const Noise &): optional noise to roughen the hull's surface.

Lua: sm.voxelTerrainCell.setMaterialAndDensityConvexHull

inline void fillEmptyConvexHull(const Vector<Vec3> &vertices, double margin, double densityThreshold, int material, double density, const Noise &n = {});

Fills only the empty space inside a convex hull, leaving existing ground alone.

Parameters

  • vertices (const Vector<Vec3> &): the corners of the hull, in the cell's voxel grid.
  • margin (double): extra distance around the hull that is affected too.
  • densityThreshold (double): voxels are filled only where their density is below this, from 0 to 1.
  • material (int): the material of the new ground.
  • density (double): the density of the new ground, from 0 to 1.
  • n (const Noise &): optional noise to roughen the hull's surface.

Lua: sm.voxelTerrainCell.fillEmptyConvexHull

Optional simplex noise that roughens the surface of a convex hull, so carved shapes don't look perfectly smooth. Both values are optional: frequency defaults to 1, intensity to 0 (no noise).

struct Noise
Optional<double> frequency, intensity;

Helpers for harvestables placed by terrain generation.

inline double initialHash(const Uuid &uuid, const World &world, Vec3 position, Quat rotation);

Works out the Harvestable::initialHash() a harvestable placed with these values will have, so a terrain script can recognise it later, for example to remember which trees were cut.

Parameters

  • uuid (const Uuid &): the harvestable's uuid.
  • world (const World &): the world it's placed in.
  • position (Vec3): where it's placed.
  • rotation (Quat): how it's rotated.

Returns double: the hash.

Lua: sm.terrainHarvestable.calculateInitialHash

Generates cave and tunnel paths through a volume of voxel terrain.

inline Value generate(int seed, Vec3 min, Vec3 max, const Value &pathingSettings, const Value &tunnelRequests, const Value &blockers, double gridSize);

Finds tunnel paths through the box from min to max. The three tables use the game's own formats; see the Scrap Mechanic Lua documentation for sm.tunnelGenerator.generate.

Parameters

  • seed (int): the random seed, so the same inputs give the same tunnels.
  • min (Vec3): the box's minimum corner.
  • max (Vec3): the box's maximum corner.
  • pathingSettings (const Value &): how tunnels may bend and branch.
  • tunnelRequests (const Value &): the tunnels to make, with their start and end points.
  • blockers (const Value &): areas tunnels must avoid.
  • gridSize (double): the size of one step of the path grid.

Returns Value: the generated tunnel paths.

Lua: sm.tunnelGenerator.generate