TerrainGenerator

Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object

A TerrainGenerator makes and changes terrain in bulk, for building test worlds quickly. It has four kinds of tool: 1. **Presets** fill a rectangle of samples with ground from noise: rolling hills, mountains, plateaus, canyons, or flat ground. The same seed always gives the same ground, and the noise is laid out by world position, so a preset looks the same at any vertex spacing. 2. **Stamps** press a shape into the ground in one go, like Ghost Recon Wildlands' "stamp a mountain in one click": a hill, a mountain, a mesa, a crater or a ridge. A stamp can be any size and turned to any angle, and any square of numbers can be used as a shape. 3. **Erosion** wears the ground the way weather does. Rain erosion sends drops of water downhill, each picking up ground where it speeds up and dropping it where it slows down, which carves channels and fills hollows. Slope settling lets ground slide off slopes steeper than a set angle. Both move ground around rather than adding or removing it. 4. **Auto paint** sets the splat weights by rules: rock on steep slopes, sand on low ground, dirt in hollows, and grass everywhere else, using the red (grass), green (rock), blue (dirt) and alpha (sand) layers. Everything works on samples that already have regions; it never adds regions. Changes go into the TerrainData's edited areas, so a WorldTerrain's Rebuild redraws them. To make them undoable, wrap them in a TerrainStroke with BeginOperation, CopyArea and End.

Example Code

use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Game.Graphics.Worlds.TerrainGenerator

TerrainData data
data:SetRegionSize(64)
data:AddRegion(0, 0)
TerrainGenerator generator
generator:GeneratePreset(data, generator:PRESET_MOUNTAINS, 0, 0, 64, 64, 40)
generator:Stamp(data, generator:MakeStampShape(generator:STAMP_CRATER, 65), 65, 16, 16, 10, 0, 6, generator:STAMP_ADD)
generator:Erode(data, 0, 0, 63, 63, 1000)
generator:AutoPaint(data, 0, 0, 63, 63)

Variables

  • integer STAMP_MESA
  • integer STAMP_HILL
  • integer PRESET_CANYONS
  • integer STAMP_CRATER
  • integer PRESET_ROLLING_HILLS
  • integer PRESET_FLAT
  • integer STAMP_RAISE
  • integer STAMP_ADD: to it (the ground stays where it is already higher).
  • integer STAMP_MOUNTAIN
  • integer STAMP_RIDGE: to it (the ground stays where it is already higher).
  • integer PRESET_PLATEAUS
  • integer STAMP_SUBTRACT
  • integer PRESET_MOUNTAINS

Actions

AutoPaint(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)

This action sets the splat weights of a rectangle of samples by rules: rock (green) where the ground is steep, fading in between two slopes; sand (alpha) below a height; dirt (blue) in hollows, where the ground is lower than the average around it; and grass (red) everywhere else. Rock comes first, then sand, then dirt, each taking its share of what is left. Samples with no region are skipped, and samples whose weights already match are left alone, so painting the same area twice changes nothing the second time.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.

Erode(Libraries.Game.Graphics.Worlds.TerrainData data, integer startMinimumX, integer startMinimumZ, integer startMaximumX, integer startMaximumZ, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)

This action wears ground as Erode does, but the drops of rain start in one rectangle and may run on through a larger one around it. Ground picked up under a brush is then carried downhill and away, rather than all settling back inside the brush, so eroding one spot again and again keeps carving instead of leveling it. Slope settling covers the larger rectangle.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • startMinimumX integer: The smallest sample along x where drops start.
  • startMinimumZ integer: The smallest sample along z where drops start.
  • startMaximumX integer: The largest sample along x where drops start.
  • startMaximumZ integer: The largest sample along z where drops start.
  • minimumX integer: The smallest sample along x drops may reach. Ground moves within this rectangle and one sample around it.
  • minimumZ integer: The smallest sample along z drops may reach.
  • maximumX integer: The largest sample along x drops may reach.
  • maximumZ integer: The largest sample along z drops may reach.
  • drops integer: How many drops of rain to send.

Erode(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)

This action wears a rectangle of ground as weather would: rain erosion with a number of drops, then a few passes of slope settling to soften what the rain left too steep. Ground only moves within the rectangle and one sample around it.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.
  • drops integer: How many drops of rain to send down the ground.

ErodeDrop(Libraries.Game.Graphics.Worlds.TerrainData data, number startX, number startZ, Libraries.Compute.Random random, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)

This action sends one drop of rain from a point: it rolls downhill, picking up ground where it can carry more and leaving it where it can carry less, as ErodeWithRain describes, until it leaves the rectangle, reaches ground with no region, runs dry, or has rolled maximumSteps samples. A brush's stroke uses it to rain where the brush is.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • startX number: Where the drop starts along x, in samples.
  • startZ number: Where the drop starts along z, in samples.
  • random Libraries.Compute.Random: The random numbers, for directions on flat ground.
  • minimumX integer: The smallest sample along x the drop may reach.
  • minimumZ integer: The smallest sample along z the drop may reach.
  • maximumX integer: The largest sample along x the drop may reach.
  • maximumZ integer: The largest sample along z the drop may reach.

ErodeWithRain(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)

This action sends drops of rain down a rectangle of ground. Each drop starts at a random sample, rolls downhill, picks up ground where it can carry more (it is fast, or the ground falls away steeply), and drops ground where it can carry less (it slows, or the ground rises). When it leaves the rectangle, reaches ground with no region, or runs dry, it drops what it carries. So ground is moved, not added or taken away. Ground picked up, and what a drop leaves when it stops, is shared among the samples near it (spreadRadius), as in Beyer's method, so single samples aren't dug into pits or piled into spikes.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.
  • drops integer: How many drops.

ErodeWithRain(Libraries.Game.Graphics.Worlds.TerrainData data, integer startMinimumX, integer startMinimumZ, integer startMaximumX, integer startMaximumZ, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)

This action sends drops of rain as the other ErodeWithRain does, but the drops start in one rectangle and may run on through a larger one.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • startMinimumX integer: The smallest sample along x where drops start.
  • startMinimumZ integer: The smallest sample along z where drops start.
  • startMaximumX integer: The largest sample along x where drops start.
  • startMaximumZ integer: The largest sample along z where drops start.
  • minimumX integer: The smallest sample along x drops may reach.
  • minimumZ integer: The smallest sample along z drops may reach.
  • maximumX integer: The largest sample along x drops may reach.
  • maximumZ integer: The largest sample along z drops may reach.
  • drops integer: How many drops.

FillAutoPaintGoal(Libraries.Game.Graphics.Worlds.TerrainData data, integer x, integer z, integer layerCount, Libraries.Containers.Number32BitArray goals, integer start)

This action works out the splat weights the painting rules give one sample, as AutoPaint describes, and puts its four numbers (red, green, blue and alpha) into an array, from a place in it. Layers past the terrain's count are folded into the first, as TerrainData's FoldSplatLayers does. A brush's stroke uses these as the goals it paints toward.

Parameters

GeneratePreset(Libraries.Game.Graphics.Worlds.TerrainData data, integer preset, integer startX, integer startZ, integer width, integer depth, number height)

This action fills a rectangle of samples with ground from a preset, from 0 up to the given height. Samples with no region are skipped.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • preset integer: The preset, such as PRESET_ROLLING_HILLS.
  • startX integer: The first sample along x.
  • startZ integer: The first sample along z.
  • width integer: How many samples along x.
  • depth integer: How many samples along z.
  • height number: The tallest the ground can be, in world units.

GetHollowDepth(Libraries.Game.Graphics.Worlds.TerrainData data, integer x, integer z)

This action returns how far a sample lies below the average of the ground around it, within hollowRadius samples, or 0 if it isn't lower. Every other sample is read, which gives nearly the same average at a quarter of the cost.

Parameters

Returns

number: The depth of the hollow, in world units.

GetPresetCount()

This action returns how many presets there are.

Returns

integer: The number of presets.

GetPresetName(integer preset)

This action returns a preset's name, for menus and speech.

Parameters

  • preset integer: The preset, such as PRESET_MOUNTAINS.

Returns

text: The name.

GetSeed()

This action returns the seed.

Returns

number: The seed.

GetStampCount()

This action returns how many stamp shapes there are.

Returns

integer: The number of stamp shapes.

GetStampMode(integer stamp)

This action returns how a stamp shape is usually pressed into the ground: a crater is added, since it has a dip below the ground; the others are raised to, so they keep their own shape over bumpy ground rather than adding the bumps to it, and leave ground that is already higher alone.

Parameters

  • stamp integer: The shape.

Returns

integer: STAMP_ADD or STAMP_RAISE.

GetStampName(integer stamp)

This action returns a stamp shape's name, for menus and speech.

Parameters

  • stamp integer: The shape, such as STAMP_CRATER.

Returns

text: The name.

MakeStampShape(integer stamp, integer width)

This action makes a stamp shape: a square of numbers, one row after another, mostly from 0 to 1. A crater's middle goes below 0. The middle of the square is the stamp's center, and its edges are the stamp's radius away.

Parameters

  • stamp integer: The shape, such as STAMP_MOUNTAIN.
  • width integer: How many numbers along each side. Odd numbers put a value exactly at the center.

Returns

Libraries.Containers.Number32BitArray: The shape.

SampleShape(Libraries.Containers.Number32BitArray shape, integer width, number across, number deep)

This action reads a shape between its numbers, blending the four nearest.

Parameters

  • shape Libraries.Containers.Number32BitArray: The shape.
  • width integer: How many numbers along each side.
  • across number: Where to read along the shape's rows, from 0 to 1.
  • deep number: Where to read down the shape's columns, from 0 to 1.

Returns

number: The blended value.

SetPaintRules(number rockLow, number rockHigh, number sandHeight, number sandFade, number hollowDepth)

This action sets auto paint's rules.

Parameters

  • rockLow number: The slope, in degrees, where rock starts.
  • rockHigh number: The slope where the ground is all rock. It must be more than rockLow.
  • sandHeight number: The height below which the ground is all sand.
  • sandFade number: How far above sandHeight sand fades out.
  • hollowDepth number: How far below the ground around it a hollow must be to be all dirt.

SetSeed(number seed)

This action sets the seed for presets, stamps' detail and rain erosion. The same seed always gives the same result.

Parameters

  • seed number: The seed.

Settle(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer passes, number degrees)

This action lets ground slide off slopes steeper than an angle. Each pass looks at every sample in a rectangle, and if it stands higher above its lowest neighbor than the angle allows, moves half the extra to that neighbor. Ground is moved, not added or taken away.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.
  • passes integer: How many times to go over the rectangle.
  • degrees number: The steepest slope that stays, in degrees.

SettleAfterRain(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)

This action lets ground slide off slopes steeper than the generator's settle angle, three passes over a rectangle, as erosion does after its rain. A brush's stroke uses it after raining.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.

Smooth(number value)

This action is a smooth step: 0 at or below 0, 1 at or above 1, and an S-shaped curve between.

Parameters

  • value number: The value.

Returns

number: The smoothed value, from 0 to 1.

Stamp(Libraries.Game.Graphics.Worlds.TerrainData data, Libraries.Containers.Number32BitArray shape, integer width, number centerX, number centerZ, number radius, number degrees, number height, integer mode)

This action presses a shape into the ground. The shape covers the square around a center, a radius away on each side, turned by an angle. Each sample under it changes by the shape's value there times the height: added (STAMP_ADD), taken away (STAMP_SUBTRACT), or with STAMP_RAISE, the ground is raised to the shape standing on the ground under the center, and left alone where it is already higher. Samples with no region are skipped.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain.
  • shape Libraries.Containers.Number32BitArray: The shape, width by width numbers, one row after another.
  • width integer: How many numbers along each side of the shape.
  • centerX number: The center along x, in world units.
  • centerZ number: The center along z, in world units.
  • radius number: Half the width of the square the shape covers, in world units.
  • degrees number: How far to turn the shape, in degrees.
  • height number: The height of a shape value of 1, in world units.
  • mode integer: STAMP_ADD, STAMP_SUBTRACT or STAMP_RAISE.

Terrace(number level, integer steps)

This action turns a level from 0 to 1 into flat steps: most of each step is level, and the last fifth rises steeply to the next, like plateaus and cliffs.

Parameters

  • level number: The level, from 0 to 1.
  • steps integer: How many steps from 0 to 1.

Returns

number: The stepped level.

Inherited Actions

3 actions from Libraries.Language.Object