User Tools

Site Tools

Urban Games

modding:terraingenerators

Terrain Generators

Terrain generators are used to generate a heightmap and distribute tree and rock assets on them. They are configured in .lua files in the res/config/terrain_generators/ folder. The basic structure is:

function data() 
return {
  climate = "temperate.clima.lua",
  order = 0,
  editorOnly = false,
  name = _("Temperate"),
  params = {...},
  updateFn = function(params)
   ...
  end
}
end

There are several simple properties:

  • climate is a reference to the climate in the res/config/climate/ for which this generator can be used.
  • order is used for the order in the dropdown where the generators can be selected.
  • editorOnly restricts the generator to only be available in the map editor (useful for pure asset generators). Unset or false results in the generator being available in the main menu too.
  • name is the name that should be displayed in the dropdown. It can be translated using a strings.lua file.

It is possible to provide several parameters with which the user can set some preferences for terrain generation. See the construction basics to learn more about parameter properties.

UpdateFn

The updateFn is a function that is called once the generation process should start. It gets several parameters in a data struct:

key type description
bounds.max.x integer The maximum value of map bounds in x direction
bounds.max.y integer The maximum value of map bounds in y direction
bounds.min.x integer The minimum value of map bounds in x direction
bounds.min.y integer The minimum value of map bounds in y direction
mapSizeX integer The actual map size in x direction
mapSizeY integer The actual map size in y direction
waterLevel integer The height of water level
<parameterKey> integer The selected value for each parameter provided in the user interface (see above)

The expected return data struct contains several properties:

{
  layers = { ... },
 
  heightmapLayer = "HM",
 
  assetsMap = "__t_5",
  assetsMapping = { [1] = "granite" }, 
 
  forestMap = "__t_8",
  treesMapping = {  [1] = "conifer", [2] = "shrub", [3] = "hills", [4] = "broadleaf", [5] = "river",
    [6] = "plains",  [255] = "single", },
 
  parallelFactor = 32,
}

The layers in layers usually are computed by using the layersutil.lua that can be found in res/scripts/terrain/ folder. It provides a lot of operations which can be used to generate the map layers.

The heightmapLayer is a reference to one of the layers in layers. It contains the height information for the map.

The assetsMap layer is a reference to another layer in layers. It contains the information about asset placement. Usually this only covers different kinds of rocks. The mapping from map values to asset classes is stored in the assetsMapping.

The forestMap layer is similar to the assetsMap layer but for trees. The mapping from map values to tree classes is stored in the treesMapping. See assets description for information on how models are assigned to an asset or tree class.


modding/terraingenerators.txt · Last modified: 2020/06/20 11:11 by yoshi